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.
This commit is contained in:
vhaudiquet committed 2026-11-24 21:52:00 +01:00
1 parent 70b53665d5
commit d1c1e7c8d9
2 files changed
+51 -13

No files matched your search

+4 -1
View File
@@ -1330,7 +1330,10 @@ fn stashed_history(
let outcome = record let outcome = record
.as_ref() .as_ref()
.and_then(|record| record.last_outcome.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) crate::thread::page(&events, before, limit)
} }
None => (Vec::new(), false), None => (Vec::new(), false),
+47 -12
View File
@@ -29,12 +29,15 @@ struct Call {
/// `user_message`, the turn's deltas and tool rows, one `turn_done` per /// `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, /// turn, in history order. `last` is the record's most recent outcome,
/// which the newest turn's end marker carries; the older turns closed /// 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 /// 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 /// them: a forge or job prompt carries none, and a chat key never takes
/// this path. /// this path.
pub fn events(session: &CoreSession, last: Option<&Outcome>) -> Vec<TurnEvent> { pub fn events(session: &CoreSession, last: Option<&Outcome>, leased: bool) -> Vec<TurnEvent> {
let mut out: Vec<TurnEvent> = Vec::new(); let mut out: Vec<TurnEvent> = Vec::new();
let mut calls: HashMap<String, Call> = HashMap::new(); let mut calls: HashMap<String, Call> = HashMap::new();
let mut open = false; let mut open = false;
@@ -94,7 +97,7 @@ pub fn events(session: &CoreSession, last: Option<&Outcome>) -> Vec<TurnEvent> {
} }
} }
} }
let outcome = outcome_label(last); let outcome = outcome_label(last, leased);
close(&mut out, &mut open, reply, &outcome); close(&mut out, &mut open, reply, &outcome);
out out
} }
@@ -145,12 +148,17 @@ fn push_result(out: &mut Vec<TurnEvent>, calls: &HashMap<String, Call>, result:
} }
/// The outcome label a turn's end marker carries, in the handler's own /// The outcome label a turn's end marker carries, in the handler's own
/// words: a superseded dispatch was cut, a failed one broke. /// words: a superseded dispatch was cut, a failed one broke. A record
fn outcome_label(outcome: Option<&Outcome>) -> String { /// 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 { match outcome {
Some(Outcome::Failed(_)) => "failed", Some(Outcome::Failed(_)) => "failed",
Some(Outcome::Superseded) => "aborted", Some(Outcome::Superseded) => "aborted",
Some(Outcome::Handled) | None => "handled", Some(Outcome::Handled) => "handled",
None if leased => "running",
None => "aborted",
} }
.to_owned() .to_owned()
} }
@@ -245,7 +253,7 @@ mod tests {
assistant(vec![Content::Text("LGTM.".to_owned())]), assistant(vec![Content::Text("LGTM.".to_owned())]),
]); ]);
assert_eq!( assert_eq!(
events(&core, Some(&Outcome::Handled)), events(&core, Some(&Outcome::Handled), false),
vec![ vec![
TurnEvent::UserMessage { TurnEvent::UserMessage {
text: "review PR 7".to_owned(), text: "review PR 7".to_owned(),
@@ -281,7 +289,7 @@ mod tests {
user("second"), user("second"),
assistant(vec![Content::Text("two".to_owned())]), 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 let closes: Vec<&TurnEvent> = events
.iter() .iter()
.filter(|event| matches!(event, TurnEvent::TurnDone { .. })) .filter(|event| matches!(event, TurnEvent::TurnDone { .. }))
@@ -308,7 +316,7 @@ mod tests {
fn a_stash_that_starts_mid_turn_still_closes() { fn a_stash_that_starts_mid_turn_still_closes() {
let core = session(vec![assistant(vec![Content::Text("orphan".to_owned())])]); let core = session(vec![assistant(vec![Content::Text("orphan".to_owned())])]);
assert_eq!( assert_eq!(
events(&core, None).last(), events(&core, Some(&Outcome::Handled), false).last(),
Some(&TurnEvent::TurnDone { Some(&TurnEvent::TurnDone {
reply: Some("orphan".to_owned()), reply: Some("orphan".to_owned()),
outcome: "handled".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] #[test]
fn images_in_the_history_do_not_invent_attachment_ids() { fn images_in_the_history_do_not_invent_attachment_ids() {
let mut message = user("look"); let mut message = user("look");
@@ -323,7 +358,7 @@ mod tests {
media_type: rhema_core::ImageMediaType::Png, media_type: rhema_core::ImageMediaType::Png,
data: "iVBORw0KGgo=".to_owned(), data: "iVBORw0KGgo=".to_owned(),
})); }));
let events = events(&session(vec![message]), None); let events = events(&session(vec![message]), None, false);
assert_eq!( assert_eq!(
events[0], events[0],
TurnEvent::UserMessage { TurnEvent::UserMessage {
@@ -344,7 +379,7 @@ mod tests {
user("two"), user("two"),
assistant(vec![Content::Text("c".to_owned())]), 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. // The newest page holds the last turn only.
let (newest, more) = page(&all, None, 2); let (newest, more) = page(&all, None, 2);
assert!(more, "two turns sit above it"); assert!(more, "two turns sit above it");
@@ -370,7 +405,7 @@ mod tests {
Content::Text("c".to_owned()), Content::Text("c".to_owned()),
]), ]),
]); ]);
let all = events(&core, None); let all = events(&core, None, false);
let (whole, more) = page(&all, None, 1); let (whole, more) = page(&all, None, 1);
assert_eq!(whole.len(), all.len()); assert_eq!(whole.len(), all.len());
assert!(!more); assert!(!more);