From d1c1e7c8d94e5cc2e75572d8135a0223fe8d9024 Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Tue, 24 Nov 2026 21:52:00 +0100 Subject: [PATCH] fix(claw): read a turn with no release as running or aborted The read-only transcript closes its newest turn with the record's last outcome, and a record that never released has none, so the turn closed as "handled". A turn a restart cut read back as finished work, with the model's first line standing in for the answer it never gave. No release now reads as what it is: the record holds the lease, so the turn is running, or it does not, so the turn was cut. Handled still means a dispatch finished and posted. --- crates/rhema-claw/src/http.rs | 5 ++- crates/rhema-claw/src/thread.rs | 59 ++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/crates/rhema-claw/src/http.rs b/crates/rhema-claw/src/http.rs index 26322de..f12f256 100644 --- a/crates/rhema-claw/src/http.rs +++ b/crates/rhema-claw/src/http.rs @@ -1330,7 +1330,10 @@ fn stashed_history( let outcome = record .as_ref() .and_then(|record| record.last_outcome.as_ref()); - let events = crate::thread::events(&session.core, outcome); + let leased = record + .as_ref() + .is_some_and(|record| record.state == rhema_rsd::KeyState::Leased); + let events = crate::thread::events(&session.core, outcome, leased); crate::thread::page(&events, before, limit) } None => (Vec::new(), false), diff --git a/crates/rhema-claw/src/thread.rs b/crates/rhema-claw/src/thread.rs index b248f67..a40d4e1 100644 --- a/crates/rhema-claw/src/thread.rs +++ b/crates/rhema-claw/src/thread.rs @@ -29,12 +29,15 @@ struct Call { /// `user_message`, the turn's deltas and tool rows, one `turn_done` per /// turn, in history order. `last` is the record's most recent outcome, /// which the newest turn's end marker carries; the older turns closed -/// the way the handler closes a finished one. +/// the way the handler closes a finished one. `leased` says the record +/// is mid-dispatch right now, and a stash whose newest turn never +/// released is not "handled" — the turn was cut, and its end marker says +/// so instead of borrowing the previous turn's word. /// /// Images and pastes ride a user message only when the stash recorded /// them: a forge or job prompt carries none, and a chat key never takes /// this path. -pub fn events(session: &CoreSession, last: Option<&Outcome>) -> Vec { +pub fn events(session: &CoreSession, last: Option<&Outcome>, leased: bool) -> Vec { let mut out: Vec = Vec::new(); let mut calls: HashMap = HashMap::new(); let mut open = false; @@ -94,7 +97,7 @@ pub fn events(session: &CoreSession, last: Option<&Outcome>) -> Vec { } } } - let outcome = outcome_label(last); + let outcome = outcome_label(last, leased); close(&mut out, &mut open, reply, &outcome); out } @@ -145,12 +148,17 @@ fn push_result(out: &mut Vec, calls: &HashMap, result: } /// The outcome label a turn's end marker carries, in the handler's own -/// words: a superseded dispatch was cut, a failed one broke. -fn outcome_label(outcome: Option<&Outcome>) -> String { +/// words: a superseded dispatch was cut, a failed one broke. A record +/// with no outcome has never released, so its newest turn is either the +/// one running now (the lease says so) or one a restart cut — never a +/// handled turn, which always records its release. +fn outcome_label(outcome: Option<&Outcome>, leased: bool) -> String { match outcome { Some(Outcome::Failed(_)) => "failed", Some(Outcome::Superseded) => "aborted", - Some(Outcome::Handled) | None => "handled", + Some(Outcome::Handled) => "handled", + None if leased => "running", + None => "aborted", } .to_owned() } @@ -245,7 +253,7 @@ mod tests { assistant(vec![Content::Text("LGTM.".to_owned())]), ]); assert_eq!( - events(&core, Some(&Outcome::Handled)), + events(&core, Some(&Outcome::Handled), false), vec![ TurnEvent::UserMessage { text: "review PR 7".to_owned(), @@ -281,7 +289,7 @@ mod tests { user("second"), assistant(vec![Content::Text("two".to_owned())]), ]); - let events = events(&core, Some(&Outcome::Failed("boom".to_owned()))); + let events = events(&core, Some(&Outcome::Failed("boom".to_owned())), false); let closes: Vec<&TurnEvent> = events .iter() .filter(|event| matches!(event, TurnEvent::TurnDone { .. })) @@ -308,7 +316,7 @@ mod tests { fn a_stash_that_starts_mid_turn_still_closes() { let core = session(vec![assistant(vec![Content::Text("orphan".to_owned())])]); assert_eq!( - events(&core, None).last(), + events(&core, Some(&Outcome::Handled), false).last(), Some(&TurnEvent::TurnDone { reply: Some("orphan".to_owned()), outcome: "handled".to_owned(), @@ -316,6 +324,33 @@ mod tests { ); } + /// A record with no release is not a handled turn: either the + /// dispatch is running now, or a restart cut it. Both read as what + /// they are instead of borrowing the previous turn's word. + #[test] + fn an_unreleased_turn_is_not_labelled_handled() { + let core = session(vec![ + user("cut"), + assistant(vec![Content::Text("I'll start by looking".to_owned())]), + ]); + let cut = events(&core, None, false); + assert_eq!( + cut.last(), + Some(&TurnEvent::TurnDone { + reply: Some("I'll start by looking".to_owned()), + outcome: "aborted".to_owned(), + }) + ); + let running = events(&core, None, true); + assert_eq!( + running.last(), + Some(&TurnEvent::TurnDone { + reply: Some("I'll start by looking".to_owned()), + outcome: "running".to_owned(), + }) + ); + } + #[test] fn images_in_the_history_do_not_invent_attachment_ids() { let mut message = user("look"); @@ -323,7 +358,7 @@ mod tests { media_type: rhema_core::ImageMediaType::Png, data: "iVBORw0KGgo=".to_owned(), })); - let events = events(&session(vec![message]), None); + let events = events(&session(vec![message]), None, false); assert_eq!( events[0], TurnEvent::UserMessage { @@ -344,7 +379,7 @@ mod tests { user("two"), assistant(vec![Content::Text("c".to_owned())]), ]); - let all = events(&core, None); + let all = events(&core, None, false); // The newest page holds the last turn only. let (newest, more) = page(&all, None, 2); assert!(more, "two turns sit above it"); @@ -370,7 +405,7 @@ mod tests { Content::Text("c".to_owned()), ]), ]); - let all = events(&core, None); + let all = events(&core, None, false); let (whole, more) = page(&all, None, 1); assert_eq!(whole.len(), all.len()); assert!(!more);