diff --git a/crates/rhema-claw/README.md b/crates/rhema-claw/README.md index 0ee940d..43ff56a 100644 --- a/crates/rhema-claw/README.md +++ b/crates/rhema-claw/README.md @@ -368,7 +368,11 @@ store, one dispatcher. Single-node is what makes restarts instant: at boot the daemon releases every lease the previous process left — outright, not at the dead lease's TTL — so a turn an update cut mid-flight answers its next message instead of stalling behind a ghost -(the committed tool call revives at the top of that next dispatch). +(the committed tool call revives at the top of that next dispatch). The +cut turn is released as superseded on the way, so a thread it never +finished reads as aborted instead of wearing the outcome of the turn +before it. Nothing re-runs it: the mention was drained before the turn +ran, and the thread waits for the next activity. The store and workspaces live on a PVC, `claw.yaml` in a ConfigMap (db and workspace paths are pinned to the mounted volume), credentials in a Secret, and the bearer token is the `token` value, required since diff --git a/crates/rhema-claw/src/daemon.rs b/crates/rhema-claw/src/daemon.rs index 7430eb5..433f36b 100644 --- a/crates/rhema-claw/src/daemon.rs +++ b/crates/rhema-claw/src/daemon.rs @@ -606,9 +606,39 @@ impl Daemon { // is failed interrupted before anything re-runs, and a // still-queued row is re-armed with a fresh dedup id. self.extensions.boot()?; + // The leases still in the store name the turns this restart cut. + // The three dispatchers share one store, so one read sees them + // all. Read before the sweep forgets, recover, then release each + // one as superseded: a record keeps the outcome of its last + // release, so a cut turn would otherwise wear the previous + // turn's word, and the read-only transcript would print + // "handled" beside text the turn never finished. + let cut: Vec = self + .forge_dispatcher + .list_active()? + .into_iter() + // An empty inbox is the turn that had already drained its + // news, which is the one a restart strands. A key with items + // waiting re-acquires on its own and records its own outcome. + .filter(|status| status.inbox_depth == 0) + .map(|status| status.key) + .collect(); self.forge_dispatcher.recover_all()?; self.jobs_dispatcher.recover_all()?; self.interactive_dispatcher.recover_all()?; + for key in cut { + let acquired = self.forge_dispatcher.acquire(key.clone(), "claw-boot"); + let lease = match acquired { + Ok((lease, _)) => lease, + Err(err) => { + eprintln!("[claw] warning: {key}: cut turn not marked aborted: {err}"); + continue; + } + }; + if let Err(err) = self.forge_dispatcher.release(&lease, Outcome::Superseded) { + eprintln!("[claw] warning: {key}: cut turn not marked aborted: {err}"); + } + } Ok(()) } diff --git a/crates/rhema-claw/tests/http.rs b/crates/rhema-claw/tests/http.rs index a296acf..2710ecd 100644 --- a/crates/rhema-claw/tests/http.rs +++ b/crates/rhema-claw/tests/http.rs @@ -3836,6 +3836,29 @@ async fn a_restart_releases_a_dead_lease_and_the_key_answers() { let base = serve(&daemon).await; let client = reqwest::Client::new(); + // The turn the restart cut is not left wearing an outcome it never + // earned: recovery releases it as superseded, and the read-only + // transcript ends the turn aborted, not handled. + let thread: Value = client + .get(format!("{base}/v1/sessions/interactive:web/history")) + .send() + .await + .expect("the history request goes out") + .json() + .await + .expect("the history body parses"); + let closes: Vec<&Value> = thread["events"] + .as_array() + .expect("events is a list") + .iter() + .filter(|event| event["event"]["type"] == "turn_done") + .collect(); + let last = closes.last().map(|event| event["event"]["outcome"].clone()); + assert!( + last.as_ref().is_none_or(|outcome| outcome == "aborted"), + "a cut turn is not handled: {thread}" + ); + // The key reads idle, not leased: the ghost lease is gone. let sessions: Value = client .get(format!("{base}/v1/sessions")) @@ -4097,6 +4120,75 @@ async fn a_leased_session_is_not_deletable() { assert_eq!(sessions[0]["state"], "leased", "{sessions:?}"); } +/// The turn a restart cut must not wear the previous turn's outcome. A +/// record keeps the outcome of its last release, so a dispatch that +/// checkpointed its prompt and the model's first words and then died +/// would read back as "handled" beside text it never finished: handled +/// is what the turn before it was. Boot releases the dead lease as +/// superseded, and the transcript says aborted. +#[tokio::test] +async fn a_restart_cut_turn_reads_aborted_not_the_previous_outcome() { + let dir = tempfile::tempdir().expect("tempdir"); + let db = dir.path().join("dispatch.db"); + let key = ResourceKey::from("github:pull:acme/widgets#2"); + { + let store = SqliteStore::open(&db).expect("the store opens"); + let mut record = rhema_rsd::KeyRecord::fresh(key.clone(), 1); + let mut session = rhema_rsd::Session::fresh(key.clone(), 1); + session.core.history = vec![ + Message::text(Role::User, "address the review feedback"), + Message::text(Role::Assistant, "I'll start by looking at the workspace."), + ]; + record.session = Some(session); + // The dispatch before this one finished handled, and its release + // is the record's last outcome. + record.last_outcome = Some(rhema_rsd::Outcome::Handled); + // This one held a lease when the process died. + record.state = rhema_rsd::KeyState::Leased; + record.lease = Some(rhema_rsd::LeaseRecord { + id: rhema_rsd::LeaseId::new("lease-old"), + holder: "the-old-process".to_owned(), + acquired_at: 1, + expires_at: u64::MAX, + }); + store.save(record, None).expect("the record seeds"); + } + + let daemon = Daemon::build( + &config(&dir, None), + mock_registry(Arc::new(MockProvider::new([]))), + rhema_rsd::DispatcherConfig::default(), + ) + .expect("the daemon assembles"); + daemon.bootstrap().await.expect("recovery runs"); + let base = serve(&daemon).await; + let client = reqwest::Client::new(); + + let body: Value = client + .get(format!( + "{base}/v1/sessions/github%3Apull%3Aacme%2Fwidgets%232/history" + )) + .send() + .await + .expect("the history request goes out") + .json() + .await + .expect("the history body parses"); + let events = body["events"].as_array().expect("events is a list"); + let close = events + .iter() + .find(|event| event["event"]["type"] == "turn_done") + .expect("the cut turn closes"); + assert_eq!( + close["event"]["outcome"], "aborted", + "the cut turn is not the handled turn before it: {body}" + ); + assert_eq!( + close["event"]["reply"], "I'll start by looking at the workspace.", + "what the turn said before it was cut still reads: {body}" + ); +} + /// A key the resident context does not own has no turn record: its /// handler never streams, and the events route refuses it. The history /// route still opens it, read-only, from the session stash the step