The design language that makes your AI harness better at design.
JavaScript
Serve every connection of a burst and write question state atomically (#1071)
Paul Bakaus32400c658f12
diff --git a/crates/context/src/component_review/server.rs b/crates/context/src/component_review/server.rsindex 90e6e7df6..c08df87c4 100644--- a/crates/context/src/component_review/server.rs+++ b/crates/context/src/component_review/server.rs@@ -93,6 +93,7 @@ pub struct Limits {pub fn serve(dir: &Path, port: u16, io: &mut Io, limits: Limits, stop: Option<&AtomicBool>) -> Result<i32, String> {store::read(&dir.join("current.json"))?;let server = tiny_http::Server::http(("127.0.0.1", port)).map_err(|e| e.to_string())?;+ let _pool_nudge = crate::http_pool::keep_pool_moving(&server);let actual = server.server_addr().to_ip()diff --git a/crates/context/src/http_pool.rs b/crates/context/src/http_pool.rsnew file mode 100644index 000000000..b73c7412d--- /dev/null+++ b/crates/context/src/http_pool.rs@@ -0,0 +1,107 @@+//! Keeps tiny_http's connection pool from stranding a connection.+//!+//! tiny_http 0.12 hands every accepted connection to a `TaskPool` task that+//! serves that connection's requests until the client closes it. `spawn`+//! queues the task instead of starting a thread whenever the pool's count of+//! idle workers is above zero, but that count only drops once a woken worker+//! takes the queue lock again. A burst of connections that arrives faster+//! than the idle workers wake (a browser opening one socket per image on a+//! decision page) can queue more tasks than there are workers. The workers+//! that did wake then sit in a keep-alive read on their own sockets, which a+//! browser holds open for minutes, and the leftover tasks never run: those+//! requests hang until the server exits. The decision page's missing-comp+//! probes hit this on Linux CI, where the burst outran the wakeups on every+//! failing run; the regression test below reproduces it on macOS too.+//!+//! The pool still makes progress whenever a new connection arrives: with no+//! idle worker it starts a thread that drains the queue after its own task,+//! and with an idle one the notify wakes it. So a guard opens and drops an+//! empty connection to the server on a short interval. The empty connection+//! reads EOF and never surfaces as a request, so request handling and idle+//! timers are unaffected, and a stranded connection is served within one+//! interval.++use std::net::{SocketAddr, TcpStream};+use std::sync::atomic::{AtomicBool, Ordering};+use std::sync::Arc;+use std::time::Duration;++const NUDGE_INTERVAL: Duration = Duration::from_millis(250);++/// Stops the nudge thread when dropped. Hold it for the server's lifetime.+pub struct PoolNudge {+ stop: Arc<AtomicBool>,+}++impl Drop for PoolNudge {+ fn drop(&mut self) {+ self.stop.store(true, Ordering::SeqCst);+ }+}++/// Starts nudging the server's pool. Returns `None` for a non-TCP listener.+pub fn keep_pool_moving(server: &tiny_http::Server) -> Option<PoolNudge> {+ let addr = server.server_addr().to_ip()?;+ Some(nudge(addr))+}++fn nudge(addr: SocketAddr) -> PoolNudge {+ let stop = Arc::new(AtomicBool::new(false));+ let flag = stop.clone();+ std::thread::spawn(move || {+ while !flag.load(Ordering::SeqCst) {+ std::thread::sleep(NUDGE_INTERVAL);+ if flag.load(Ordering::SeqCst) {+ return;+ }+ if TcpStream::connect_timeout(&addr, Duration::from_secs(1)).is_err() {+ return;+ }+ }+ });+ PoolNudge { stop }+}++#[cfg(test)]+mod tests {+ use super::*;+ use std::io::{Read, Write};++ // Every connection of a burst that lands before the pool's idle workers+ // wake gets an answer. Without the nudge, some round of the six leaves+ // all but four (the pool's floor of workers) unanswered on macOS too.+ #[test]+ fn a_burst_of_keep_alive_connections_is_fully_served() {+ for _ in 0..6 {+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();+ let addr = listener.local_addr().unwrap();+ // Connected before the server accepts, so its accept loop drains+ // the backlog as one burst.+ let mut socks: Vec<TcpStream> = (0..8).map(|_| TcpStream::connect(addr).unwrap()).collect();+ let server = Arc::new(tiny_http::Server::from_listener(listener, None).unwrap());+ let _nudge = keep_pool_moving(&server).unwrap();+ let responder = server.clone();+ std::thread::spawn(move || {+ for rq in responder.incoming_requests() {+ let _ = rq.respond(tiny_http::Response::empty(404));+ }+ });+ for s in socks.iter_mut() {+ s.write_all(b"GET /img/0 HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n").unwrap();+ // Each nudge frees one stranded connection, so four take about a+ // second; without the nudge they never answer.+ s.set_read_timeout(Some(Duration::from_secs(5))).unwrap();+ }+ let answered = socks+ .iter_mut()+ .map(|s| {+ let mut buf = [0u8; 64];+ matches!(s.read(&mut buf), Ok(n) if n > 0)+ })+ .filter(|answered| *answered)+ .count();+ assert_eq!(answered, 8, "connections stranded in the pool");+ server.unblock();+ }+ }+}diff --git a/crates/context/src/lib.rs b/crates/context/src/lib.rsindex 5d4cd0a68..9317df51b 100644--- a/crates/context/src/lib.rs+++ b/crates/context/src/lib.rs@@ -6,6 +6,7 @@ pub mod jsp;pub mod util;pub mod url;pub mod http;+pub mod http_pool;pub mod provider;pub mod hook_markers;pub mod target_args;diff --git a/crates/context/src/serve_question.rs b/crates/context/src/serve_question.rsindex 9c9374b08..b5a54a7e1 100644--- a/crates/context/src/serve_question.rs+++ b/crates/context/src/serve_question.rs@@ -76,6 +76,20 @@ fn read_state(qdir: &str, key: &str) -> Option<Map<String, Value>> {serde_json::from_str::<Value>(&text).ok()?.as_object().cloned()}+/// The URL a detached daemon recorded in its state file, polled until+/// `deadline_ms`. A state file without a url is not ready yet: the daemon+/// replaces it atomically, but one built before that could still be caught+/// between creating the file and writing it.+fn wait_for_daemon_url(qdir: &str, key: &str, deadline_ms: f64) -> Option<String> {+ loop {+ let url = read_state(qdir, key).and_then(|s| s.get("url").and_then(Value::as_str).filter(|u| !u.is_empty()).map(str::to_string));+ if url.is_some() || now_ms() >= deadline_ms {+ return url;+ }+ sleep_ms(100);+ }+}+fn num_field(m: &Map<String, Value>, key: &str) -> Option<f64> {m.get(key).and_then(|v| v.as_f64())}@@ -1475,11 +1489,7 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {cmd.spawn()};let _ = spawned; // detached: never waited on- let deadline = now_ms() + 8000.0;- while now_ms() < deadline && !exists(&state_file(&qdir, &key)) {- sleep_ms(100);- }- if !exists(&state_file(&qdir, &key)) {+ let Some(daemon_url) = wait_for_daemon_url(&qdir, &key, now_ms() + 8000.0) else {let tail = safe_read(&log_file).map(|t| {let lines: Vec<&str> = crate::util::js_trim(&t).split('\n').collect();@@ -1491,12 +1501,11 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {let rel = jsp::relative(&cwd, &cwd, &log_file);io.err(&format!("serve-question: log at {}. A sandboxed exec that cannot listen on localhost causes exactly this; rerun this command once through the harness's network-enabled or unsandboxed command tool before falling back.\n", if rel.is_empty() { log_file.clone() } else { rel }));return 1;- }- let state = read_state(&qdir, &key).unwrap_or_default();+ };if let Some(notice) = tracked_runtime_notice(&cwd) {io.out(¬ice);}- io.out(&format!("QUESTION URL: {}\n", state.get("url").map(js_str).unwrap_or_default()));+ io.out(&format!("QUESTION URL: {}\n", daemon_url));io.out(&format!("QUESTION KEY: {}\n", key));io.out("Open the URL for the user now: in-app browser when the harness has one, otherwise the system opener (macOS `open`, Linux `xdg-open`), otherwise show the URL.\n");if comps_expected(&start_payload, &cwd, Some(&hand)) {@@ -1535,6 +1544,7 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {}};let actual_port = server.server_addr().to_ip().map(|a| a.port()).unwrap_or(port);+ let _pool_nudge = crate::http_pool::keep_pool_moving(&server);let url = format!("http://127.0.0.1:{}/", actual_port);if a.has("detached-serve") {let _ = std::fs::create_dir_all(&qdir);@@ -1545,7 +1555,7 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {if !comps.is_empty() {st["comps"] = json!(comps);}- let _ = std::fs::write(state_file(&qdir, &key), json_compact(&st));+ let _ = impeccable_common::fsx::write_atomic(state_file(&qdir, &key), json_compact(&st).as_bytes());} else {io.out(&format!("QUESTION URL: {}\n", url));io.out("Waiting for the user to choose in the browser (Ctrl-C aborts)...\n");@@ -1622,7 +1632,7 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {} else {s.insert("comps".into(), json!(comps));}- let _ = std::fs::write(state_file(&st.qdir, &k), json_compact(&Value::Object(s)));+ let _ = impeccable_common::fsx::write_atomic(state_file(&st.qdir, &k), json_compact(&Value::Object(s)).as_bytes());}}}@@ -1646,7 +1656,7 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {st.last_beat_write = Some(now);if let Some(mut s) = read_state(&st.qdir, &k) {s.insert("lastBeat".into(), Value::from(now as i64));- let _ = std::fs::write(state_file(&st.qdir, &k), json_compact(&Value::Object(s)));+ let _ = impeccable_common::fsx::write_atomic(state_file(&st.qdir, &k), json_compact(&Value::Object(s)).as_bytes());}}}@@ -2577,6 +2587,30 @@ mod tests {dir}+ // --start reads the daemon's URL from its state file. A file that exists+ // but has no url yet (empty, as a non-atomic write leaves it, or url-less)+ // keeps --start polling; only a recorded url ends the wait.+ #[test]+ fn start_waits_for_a_state_file_that_records_the_url() {+ let dir = temp_project("state-not-ready");+ let qdir = dir.join(".impeccable/questions").to_string_lossy().into_owned();+ let state = state_file(&qdir, "k1");+ std::fs::write(&state, "").unwrap();+ assert_eq!(wait_for_daemon_url(&qdir, "k1", now_ms() + 250.0), None);+ std::fs::write(&state, json_compact(&json!({ "pid": 1, "port": 0 }))).unwrap();+ assert_eq!(wait_for_daemon_url(&qdir, "k1", now_ms() + 250.0), None);+ let writer = {+ let state = state.clone();+ std::thread::spawn(move || {+ std::thread::sleep(std::time::Duration::from_millis(300));+ impeccable_common::fsx::write_atomic(&state, json_compact(&json!({ "pid": 1, "url": "http://127.0.0.1:9/" })).as_bytes()).unwrap();+ })+ };+ assert_eq!(wait_for_daemon_url(&qdir, "k1", now_ms() + 5000.0).as_deref(), Some("http://127.0.0.1:9/"));+ writer.join().unwrap();+ let _ = std::fs::remove_dir_all(&dir);+ }+fn run_captured(dir: &std::path::Path, args: &[&str]) -> (i32, String) {let env = Env::from([("IMPECCABLE_SKILL_DIR".into(), "/skill".into())]);let (mut io, cap) = Io::captured("", dir.to_path_buf(), env);