fix(claw): state why a turn failed
android / debug (push) Successful in 2m39s
ci / docker (push) Canceled after 0s
android / release (push) Canceled after 1m54s
ci / checks (push) Canceled after 0s
ci / test (push) Canceled after 0s
ci / chart (push) Canceled after 0s
ci / web (push) Canceled after 0s
ci / publish-edge-images (deploy/docker/claw.Dockerfile, rhema-claw) (push) Canceled after 0s
ci / publish-edge-images (deploy/docker/ui.Dockerfile, rhema-claw-ui) (push) Canceled after 0s
ci / publish-edge-chart (push) Canceled after 0s

A failed turn reached the client as the bare word "failed": turn_done
carried only the outcome, so the provider's own error — the status line
and the body that name what went wrong — was dropped on the floor. The
session inventory did keep it, as {"Failed": reason}, but the web client
printed that object, and a read-only snapshot (a coding job's key, a
forge thread) hardcoded the label and lost the text entirely.

TurnDone gains a reason, set only on failure and omitted when absent, so
handled and steered frames stay byte-identical and rows recorded before
the field still replay. The stashed transcript carries it too, which is
the path job keys take. The web client renders it under the outcome word
in mono and full-wrap, and the session-info modal reads the inventory's
{"Failed": reason} into a last-failure row instead of showing JSON.

cargo test -p rhema-claw and nuxt typecheck pass; a failed turn renders
its complete reason in the transcript.
This commit is contained in:
vhaudiquet committed 2026-12-02 11:35:04 +01:00
1 parent ec52ae90b2
commit 597c2a98d2
10 files changed
+158 -40

No files matched your search

+7 -4
View File
@@ -241,10 +241,13 @@ the boot pass overwrites it.
SSE frames are `id: <seq>` plus `data: {..}`: `user_message`,
`thinking_delta`, `text_delta`, `tool_start`, `tool_result`,
`turn_done`. A `user_message` names its paste chips as the byte spans
they stand for in its plain text (`paste::PasteSpan`, the session
record's own shape) — a client renders from that record; rows recorded
before spans arrived carry the legacy mark form in the text instead. Every streamed event is persisted as it streams, so both a
`turn_done`. A failed turn's `turn_done` carries a `reason`: the
handler's own words on what broke, so a client can show the failure
instead of the bare outcome word. A `user_message` names its paste
chips as the byte spans they stand for in its plain text
(`paste::PasteSpan`, the session record's own shape) — a client renders
from that record; rows recorded before spans arrived carry the legacy
mark form in the text instead. Every streamed event is persisted as it streams, so both a
reload and a daemon restart rebuild the transcript, and a session that
runs for weeks keeps all of it.
+1
View File
@@ -891,6 +891,7 @@ async fn session_abort(State(state): State<ClawState>, Path(key): Path<String>)
TurnEvent::TurnDone {
reply: None,
outcome: "aborted".to_owned(),
reason: None,
},
);
(
+36 -2
View File
@@ -132,6 +132,11 @@ pub enum TurnEvent {
/// cut the turn short and a next turn runs instead of it — or
/// `aborted`, when the operator cut it and nothing follows.
outcome: String,
/// Why a `failed` turn failed: the handler's own words, when it
/// reported any. Absent on every other outcome, and on rows
/// recorded before the field existed.
#[serde(default, skip_serializing_if = "Option::is_none")]
reason: Option<String>,
},
/// The agent asked the human something only they can answer: a
/// credential to store, permission to act. A chat surface renders
@@ -600,6 +605,7 @@ mod tests {
TurnEvent::TurnDone {
reply: Some(reply.into()),
outcome: "handled".into(),
reason: None,
}
}
@@ -648,13 +654,40 @@ mod tests {
assert_eq!(
serde_json::to_string(&TurnEvent::TurnDone {
reply: Some("done".into()),
outcome: "handled".into()
outcome: "handled".into(),
reason: None,
})
.unwrap(),
r#"{"type":"turn_done","reply":"done","outcome":"handled"}"#
);
}
/// A failed turn carries the handler's own words; a marker recorded
/// before the field existed still loads, with no reason.
#[test]
fn a_failed_turn_serializes_its_reason() {
let failed = TurnEvent::TurnDone {
reply: None,
outcome: "failed".into(),
reason: Some("provider refused the request".into()),
};
assert_eq!(
serde_json::to_string(&failed).unwrap(),
r#"{"type":"turn_done","reply":null,"outcome":"failed","reason":"provider refused the request"}"#
);
let old: TurnEvent =
serde_json::from_str(r#"{"type":"turn_done","reply":null,"outcome":"failed"}"#)
.unwrap();
assert_eq!(
old,
TurnEvent::TurnDone {
reply: None,
outcome: "failed".into(),
reason: None,
}
);
}
/// Rows recorded before images rode the transcript carry no
/// `images` field; they replay as plain questions.
#[test]
@@ -689,7 +722,8 @@ mod tests {
assert_eq!(
TurnEvent::TurnDone {
reply: None,
outcome: "handled".into()
outcome: "handled".into(),
reason: None,
}
.kind(),
"turn_done"
+12 -4
View File
@@ -846,6 +846,7 @@ impl Handler for InteractiveHandler {
TurnEvent::TurnDone {
reply: None,
outcome: "steered".to_owned(),
reason: None,
},
);
round += 1;
@@ -869,6 +870,7 @@ impl Handler for InteractiveHandler {
TurnEvent::TurnDone {
reply: None,
outcome: "aborted".to_owned(),
reason: None,
},
);
return Outcome::Superseded;
@@ -881,6 +883,7 @@ impl Handler for InteractiveHandler {
TurnEvent::TurnDone {
reply,
outcome: "handled".to_owned(),
reason: None,
},
);
Outcome::Handled
@@ -894,6 +897,7 @@ impl Handler for InteractiveHandler {
TurnEvent::TurnDone {
reply: None,
outcome: "failed".to_owned(),
reason: Some(reason.clone()),
},
);
Outcome::Failed(reason)
@@ -2090,7 +2094,8 @@ mod tests {
receiver.try_recv().unwrap(),
TurnEvent::TurnDone {
reply: Some("hi back".into()),
outcome: "handled".into()
outcome: "handled".into(),
reason: None,
}
);
// The key is back to idle: the dispatch is done.
@@ -2198,7 +2203,8 @@ mod tests {
receiver.try_recv().unwrap(),
TurnEvent::TurnDone {
reply: None,
outcome: "steered".into()
outcome: "steered".into(),
reason: None,
}
);
assert_eq!(
@@ -2219,7 +2225,8 @@ mod tests {
receiver.try_recv().unwrap(),
TurnEvent::TurnDone {
reply: Some("the steered answer".into()),
outcome: "handled".into()
outcome: "handled".into(),
reason: None,
}
);
@@ -2344,7 +2351,8 @@ mod tests {
receiver.try_recv().unwrap(),
TurnEvent::TurnDone {
reply: None,
outcome: "aborted".into()
outcome: "aborted".into(),
reason: None,
}
);
assert!(
+1 -1
View File
@@ -278,7 +278,7 @@ pub async fn chat_completions(
let deadline = tokio::time::Instant::now() + TURN_WAIT;
let reply = loop {
let done = match tokio::time::timeout_at(deadline, receiver.recv()).await {
Ok(Ok(TurnEvent::TurnDone { reply, outcome })) => {
Ok(Ok(TurnEvent::TurnDone { reply, outcome, .. })) => {
// A steered chain's first end marker closes one
// interrupted round, not the completion: keep waiting
// for the round that answers.
+27 -14
View File
@@ -46,7 +46,7 @@ pub fn events(session: &CoreSession, last: Option<&Outcome>, leased: bool) -> Ve
for message in &session.history {
match message.role {
Role::User => {
close(&mut out, &mut open, reply.take(), "handled");
close(&mut out, &mut open, reply.take(), "handled", None);
out.push(TurnEvent::UserMessage {
text: text_of(message),
images: Vec::new(),
@@ -97,8 +97,8 @@ pub fn events(session: &CoreSession, last: Option<&Outcome>, leased: bool) -> Ve
}
}
}
let outcome = outcome_label(last, leased);
close(&mut out, &mut open, reply, &outcome);
let (outcome, reason) = outcome_label(last, leased);
close(&mut out, &mut open, reply, &outcome, reason);
out
}
@@ -116,13 +116,20 @@ fn text_of(message: &Message) -> String {
}
/// The end marker, only for a turn that was open.
fn close(out: &mut Vec<TurnEvent>, open: &mut bool, reply: Option<String>, outcome: &str) {
fn close(
out: &mut Vec<TurnEvent>,
open: &mut bool,
reply: Option<String>,
outcome: &str,
reason: Option<String>,
) {
if !*open {
return;
}
out.push(TurnEvent::TurnDone {
reply,
outcome: outcome.to_owned(),
reason,
});
*open = false;
}
@@ -148,19 +155,19 @@ 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
/// 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
/// words, plus the failure's own reason when the record kept one: 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 {
fn outcome_label(outcome: Option<&Outcome>, leased: bool) -> (String, Option<String>) {
match outcome {
Some(Outcome::Failed(_)) => "failed",
Some(Outcome::Superseded) => "aborted",
Some(Outcome::Handled) => "handled",
None if leased => "running",
None => "aborted",
Some(Outcome::Failed(reason)) => ("failed".to_owned(), Some(reason.clone())),
Some(Outcome::Superseded) => ("aborted".to_owned(), None),
Some(Outcome::Handled) => ("handled".to_owned(), None),
None if leased => ("running".to_owned(), None),
None => ("aborted".to_owned(), None),
}
.to_owned()
}
/// One page of a stashed transcript, cut the way the hub cuts its own
@@ -276,6 +283,7 @@ mod tests {
TurnEvent::TurnDone {
reply: Some("LGTM.".to_owned()),
outcome: "handled".to_owned(),
reason: None,
},
]
);
@@ -300,6 +308,7 @@ mod tests {
&TurnEvent::TurnDone {
reply: Some("one".to_owned()),
outcome: "handled".to_owned(),
reason: None,
}
);
// The newest turn carries the record's own outcome.
@@ -308,6 +317,7 @@ mod tests {
&TurnEvent::TurnDone {
reply: Some("two".to_owned()),
outcome: "failed".to_owned(),
reason: Some("boom".to_owned()),
}
);
}
@@ -320,6 +330,7 @@ mod tests {
Some(&TurnEvent::TurnDone {
reply: Some("orphan".to_owned()),
outcome: "handled".to_owned(),
reason: None,
})
);
}
@@ -339,6 +350,7 @@ mod tests {
Some(&TurnEvent::TurnDone {
reply: Some("I'll start by looking".to_owned()),
outcome: "aborted".to_owned(),
reason: None,
})
);
let running = events(&core, None, true);
@@ -347,6 +359,7 @@ mod tests {
Some(&TurnEvent::TurnDone {
reply: Some("I'll start by looking".to_owned()),
outcome: "running".to_owned(),
reason: None,
})
);
}
+2
View File
@@ -337,6 +337,7 @@ mod tests {
&[TurnEvent::TurnDone {
reply: Some("hi".into()),
outcome: "handled".into(),
reason: None,
}],
)
.expect("appends again");
@@ -359,6 +360,7 @@ mod tests {
TurnEvent::TurnDone {
reply: Some("hi".into()),
outcome: "handled".into(),
reason: None,
}
),
],