A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
Retire workers whose registration hits a stop tombstone (#3402)
Sebastian Müller7f646a40f4f0
diff --git a/.changes/fix-tombstoned-worker-retirement.md b/.changes/fix-tombstoned-worker-retirement.mdnew file mode 100644index 0000000000..10ade7683c--- /dev/null+++ b/.changes/fix-tombstoned-worker-retirement.md@@ -0,0 +1 @@+- Session workers whose registration reaches an interrupted stop now retire and release their session lease instead of repeatedly registering and re-running the stop after a daemon restart.diff --git a/crates/pa-daemon/src/registration.rs b/crates/pa-daemon/src/registration.rsindex ba75f7305a..a131b20d3e 100644--- a/crates/pa-daemon/src/registration.rs+++ b/crates/pa-daemon/src/registration.rs@@ -29,6 +29,12 @@ fn is_definitive_rejection(message: &str) -> bool {message.contains(UNKNOWN_SESSION_WORKER_PREFIX)}+pub(crate) fn tombstoned_registration_refusal(active_session_id: &str) -> String {+ format!(+ "{UNKNOWN_SESSION_WORKER_PREFIX}: {active_session_id} is stopping: the stop was forwarded; registration refused"+ )+}+/// Backoff between failed registration attempts, mirroring the worker-restart backoff.const BASE_BACKOFF_MS: u64 = 250;const MAX_BACKOFF_MS: u64 = 30_000;@@ -516,6 +522,69 @@ mod tests {assert!(late.is_err(), "a retired registration never retries");}+ #[tokio::test]+ async fn a_tombstoned_registration_refusal_retires_the_worker() {+ let dir = tempfile::TempDir::new().expect("temp dir");+ let supervisor_socket = dir.path().join("supervisor.sock");+ let config = test_config(dir.path(), &supervisor_socket);+ let listener = bind_transport(&supervisor_socket).await.expect("bind");+ let handle = start(&config).expect("registration starts");++ let refusal = tombstoned_registration_refusal("abc123def456");+ let started = std::time::Instant::now();+ let attempts = Arc::new(std::sync::Mutex::new(Vec::<u128>::new()));+ let refuser = {+ let attempts = Arc::clone(&attempts);+ tokio::spawn(async move {+ loop {+ let Ok(stream) = listener.accept().await else {+ return;+ };+ let (read_half, mut write_half) = stream.split();+ let mut reader = BufReader::new(read_half);+ write_half+ .write_all(+ b"{\"type\":\"daemon_hello\",\"protocol\":{\"name\":\"prime-agent.daemon\",\"version\":7}}\n",+ )+ .await+ .expect("hello");+ let mut line = String::new();+ reader.read_line(&mut line).await.expect("register");+ let envelope: Value = serde_json::from_str(line.trim()).expect("envelope");+ let request_id = envelope["id"].as_str().unwrap_or_default().to_string();+ let response = json!({+ "id": request_id,+ "type": "response",+ "command": "worker_register",+ "success": false,+ "error": refusal,+ });+ attempts.lock().unwrap().push(started.elapsed().as_millis());+ write_half+ .write_all(format!("{response}\n").as_bytes())+ .await+ .expect("refuse");+ write_half.flush().await.expect("flush");+ }+ })+ };++ tokio::time::timeout(Duration::from_secs(6), handle.retired())+ .await+ .unwrap_or_else(|_| {+ let attempts = attempts.lock().unwrap();+ panic!(+ "the tombstoned refusal never retired the worker: {} re-registrations within 6s: {:?}",+ attempts.len(),+ *attempts+ );+ });++ let attempts = attempts.lock().unwrap();+ assert_eq!(attempts.len(), 1, "a retired registration never retries");+ refuser.abort();+ }+#[tokio::test]async fn a_definitive_rejection_retires_and_a_transient_one_retries() {let dir = tempfile::TempDir::new().expect("temp dir");diff --git a/crates/pa-daemon/src/supervisor/adoption.rs b/crates/pa-daemon/src/supervisor/adoption.rsindex 5165b46f6c..ec1eb132a9 100644--- a/crates/pa-daemon/src/supervisor/adoption.rs+++ b/crates/pa-daemon/src/supervisor/adoption.rs@@ -407,11 +407,11 @@ impl Supervisor {Err(error) => {let message = format!("{error:#}");// The definitive unknown-worker refusal is observable (log + telemetry)- // and the worker retires on it: a live worker with no descriptor would- // otherwise hold its lease forever.+ // and the worker retires on it: a live worker this supervisor will+ // never adopt would otherwise hold its lease forever.if message.starts_with(crate::registration::UNKNOWN_SESSION_WORKER_PREFIX) {self.log_line(&format!(- "session worker {active_session_id} registration refused (no descriptor on this supervisor); the worker retires and its session file stays resumable"+ "session worker {active_session_id} registration refused; the worker retires"));self.note_daemon_event("registration_refused", None);}@@ -570,7 +570,9 @@ impl Supervisor {self.apply_identity_pending(&resident).await;// A tombstoned identity is mid-stop (TS `adoptOrRecoverWorker`'s stopRequestedAt// branch): adoption finishes the stop and never adopts the worker as healthy.- // The refusal is transient: the next attempt converges on the stop's completion.+ // The refusal is definitive: the registrant is the process the stop must+ // retire, so it exits on the verdict instead of re-registering into the+ // same unfinished stop.if resident.descriptor.lock().await.stop_requested_at.is_some() {// The registering process is the identity the stop must retire: the persisted// descriptor carries the stopped worker's stale pid, so observing the@@ -588,8 +590,9 @@ impl Supervisor {}self.finish_tombstoned_stop(&resident, true).await;return Err(anyhow!(- "session worker {} is stopping: the stop was forwarded; registration refused",- registration.active_session_id+ crate::registration::tombstoned_registration_refusal(+ ®istration.active_session_id+ )));}self.connect_worker(&resident, worker_connect_deadline())