// A scripted stand-in for `strata --serve`, compiled by tests/reasoning_budget_live.rs (std only). // // scripted_engine --serve DIR STOP_IDS // // DIR/script holds one line of comma-separated token ids per GEN/GENI the worker will send, in order. For each // request the engine prints `T id` for the first max_new ids of the next line, ends early after a stop id (finish // stop) or when it reads STOP (finish cancel), and logs "max_new ids" to DIR/requests. A STOP that arrives while no // request runs is ignored, as the real engine does. use std::io::{BufRead, Write}; use std::sync::mpsc; fn main() { let args: Vec = std::env::args().collect(); let dir = std::path::PathBuf::from(&args[2]); let stops: Vec = args[3].split(',').filter_map(|s| s.parse().ok()).collect(); let script: Vec = std::fs::read_to_string(dir.join("script")) .unwrap_or_default() .lines() .map(str::to_owned) .collect(); let (sender, lines) = mpsc::channel::(); std::thread::spawn(move || { for line in std::io::stdin().lock().lines() { let Ok(line) = line else { break }; if sender.send(line).is_err() { break; } } }); let out = std::io::stdout(); let mut out = out.lock(); writeln!(out, "READY 1000000 stop").unwrap(); out.flush().unwrap(); let mut call = 0; while let Ok(line) = lines.recv() { if line == "QUIT" { return; } if !line.starts_with("GEN") { continue; // a late STOP } let fields: Vec<&str> = line.split(' ').collect(); let max_new: usize = fields[1].parse().unwrap(); let prompt = fields.last().unwrap(); let mut log = std::fs::OpenOptions::new() .create(true) .append(true) .open(dir.join("requests")) .unwrap(); writeln!(log, "{max_new} {prompt}").unwrap(); let stream: Vec = script .get(call) .map(|l| l.split(',').filter_map(|s| s.parse().ok()).collect()) .unwrap_or_default(); call += 1; let (mut sent, mut finish) = (0usize, "length"); for id in stream.iter().take(max_new) { if let Ok(next) = lines.try_recv() { if next == "STOP" { finish = "cancel"; break; } } writeln!(out, "T {id}").unwrap(); out.flush().unwrap(); sent += 1; if stops.contains(id) { finish = "stop"; break; } } let prompt_tokens = prompt.split(',').filter(|s| !s.is_empty()).count(); writeln!(out, "DONE {sent} {prompt_tokens} 1.0 1.0 {finish}").unwrap(); out.flush().unwrap(); } }