diff --git a/crates/rhema-claw/README.md b/crates/rhema-claw/README.md index 67a71f2..45d92c2 100644 --- a/crates/rhema-claw/README.md +++ b/crates/rhema-claw/README.md @@ -241,10 +241,13 @@ the boot pass overwrites it. SSE frames are `id: ` 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. diff --git a/crates/rhema-claw/src/http.rs b/crates/rhema-claw/src/http.rs index 870d7ad..a4c5831 100644 --- a/crates/rhema-claw/src/http.rs +++ b/crates/rhema-claw/src/http.rs @@ -891,6 +891,7 @@ async fn session_abort(State(state): State, Path(key): Path) TurnEvent::TurnDone { reply: None, outcome: "aborted".to_owned(), + reason: None, }, ); ( diff --git a/crates/rhema-claw/src/hub.rs b/crates/rhema-claw/src/hub.rs index 18026ab..a7b9ca1 100644 --- a/crates/rhema-claw/src/hub.rs +++ b/crates/rhema-claw/src/hub.rs @@ -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, }, /// 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" diff --git a/crates/rhema-claw/src/interactive.rs b/crates/rhema-claw/src/interactive.rs index 927bbdb..d2c3ccf 100644 --- a/crates/rhema-claw/src/interactive.rs +++ b/crates/rhema-claw/src/interactive.rs @@ -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!( diff --git a/crates/rhema-claw/src/openai.rs b/crates/rhema-claw/src/openai.rs index 5564474..be4666c 100644 --- a/crates/rhema-claw/src/openai.rs +++ b/crates/rhema-claw/src/openai.rs @@ -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. diff --git a/crates/rhema-claw/src/thread.rs b/crates/rhema-claw/src/thread.rs index a2aa071..3939c12 100644 --- a/crates/rhema-claw/src/thread.rs +++ b/crates/rhema-claw/src/thread.rs @@ -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, open: &mut bool, reply: Option, outcome: &str) { +fn close( + out: &mut Vec, + open: &mut bool, + reply: Option, + outcome: &str, + reason: Option, +) { 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, 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. 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) { 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, }) ); } diff --git a/crates/rhema-claw/src/turn_store.rs b/crates/rhema-claw/src/turn_store.rs index 20c6954..4ef694f 100644 --- a/crates/rhema-claw/src/turn_store.rs +++ b/crates/rhema-claw/src/turn_store.rs @@ -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, } ), ], diff --git a/web/claw/app/components/ChatPanel.vue b/web/claw/app/components/ChatPanel.vue index ea133d3..6b846c5 100644 --- a/web/claw/app/components/ChatPanel.vue +++ b/web/claw/app/components/ChatPanel.vue @@ -430,13 +430,29 @@ const userInitial = computed(() => (userLabel.value === 'you' ? 'Y' : 'D')) // The dispatch internals live behind the info modal, not in the header. const infoOpen = ref(false) -const infoRows = computed(() => [ - { label: 'state', value: props.session?.state ?? 'unknown' }, - { label: 'revision', value: props.session ? String(props.session.revision) : 'unknown' }, - { label: 'inbox depth', value: props.session ? String(props.session.inbox_depth) : 'unknown' }, - { label: 'last outcome', value: props.session?.last_outcome ?? 'unknown' }, - { label: 'key', value: props.activeKey }, -]) +/** The inventory's `last_outcome`, read out of the dispatcher's own + * serialization: a bare variant name, or `{"Failed": "reason"}`. The + * failure's reason gets its own row so the modal does not show JSON. */ +const lastOutcome = computed(() => { + const value = props.session?.last_outcome + if (value === null || value === undefined) return { outcome: 'unknown', reason: null } + if (typeof value === 'string') return { outcome: value.toLowerCase(), reason: null } + return { outcome: 'failed', reason: value.Failed } +}) + +const infoRows = computed(() => { + const rows = [ + { label: 'state', value: props.session?.state ?? 'unknown' }, + { label: 'revision', value: props.session ? String(props.session.revision) : 'unknown' }, + { label: 'inbox depth', value: props.session ? String(props.session.inbox_depth) : 'unknown' }, + { label: 'last outcome', value: lastOutcome.value.outcome }, + ] + if (lastOutcome.value.reason !== null) { + rows.push({ label: 'last failure', value: lastOutcome.value.reason }) + } + rows.push({ label: 'key', value: props.activeKey }) + return rows +}) const streamLabel = computed(() => props.streamStatus === 'open' ? 'live' : props.streamStatus, @@ -544,7 +560,8 @@ function streamTone(status: StreamStatus): 'ok' | 'warn' | 'error' { aborted: item.turn.outcome === 'aborted', }" > - {{ item.turn.outcome }} + {{ item.turn.outcome }} +

{{ item.turn.reason }}

@@ -863,23 +880,46 @@ function streamTone(status: StreamStatus): 'ok' | 'warn' | 'error' { } .outcome { + display: flex; + flex-direction: column; + gap: var(--sp-1); margin-top: var(--sp-1); - color: var(--muted); font-size: var(--fs-small); } -.outcome.failed { +.outcome-word { + color: var(--muted); +} + +.outcome.failed .outcome-word { color: var(--danger); } -.outcome.steered { +.outcome.steered .outcome-word { color: var(--accent); } -.outcome.aborted { +.outcome.aborted .outcome-word { color: var(--warn); } +/* The failure's own words, kept whole: a provider error names the + * endpoint, the status, and the body, and wrapping is what makes all of + * it readable. */ +.outcome-reason { + margin: 0; + padding: var(--sp-2) var(--sp-3); + border-left: 2px solid var(--border-strong); + border-radius: 0 var(--r-control) var(--r-control) 0; + background: var(--surface-2); + color: var(--text); + font-family: var(--font-mono); + font-size: var(--fs-code); + line-height: var(--lh-body); + white-space: pre-wrap; + overflow-wrap: anywhere; +} + .working { display: flex; align-items: center; diff --git a/web/claw/app/composables/useClaw.ts b/web/claw/app/composables/useClaw.ts index adc3efa..1f5d808 100644 --- a/web/claw/app/composables/useClaw.ts +++ b/web/claw/app/composables/useClaw.ts @@ -86,7 +86,19 @@ export type TurnEvent = | ({ type: 'consent_request' } & ConsentRequest) | { type: 'consent_resolved'; id: string; granted: boolean } | ({ type: 'attachment' } & ClawAttachment) - | { type: 'turn_done'; reply: string | null; outcome: string } + | { + type: 'turn_done' + reply: string | null + outcome: string + /** Why a `failed` turn failed: the handler's own words. null on + * every other outcome, and on rows recorded before the field. */ + reason: string | null + } + +/** A recorded outcome as the dispatcher serializes it: a bare variant + * name for `Handled` and `Superseded`, `{"Failed": "reason"}` for a + * failure. null when the key never released. */ +export type WireOutcome = string | { Failed: string } | null /** One entry of `GET /v1/sessions` (http.rs `sessions`). */ export interface ClawSession { @@ -94,7 +106,7 @@ export interface ClawSession { state: string revision: number inbox_depth: number - last_outcome: string | null + last_outcome: WireOutcome /** Whether the pause gate holds the key: queued work waits, nothing * dispatches, until resume lifts the hold. */ paused: boolean @@ -1250,6 +1262,7 @@ function parseTurnEvent(data: string): TurnEvent | null { type: 'turn_done', reply: typeof raw.reply === 'string' ? raw.reply : null, outcome: String(raw.outcome ?? ''), + reason: typeof raw.reason === 'string' ? raw.reason : null, } default: return null diff --git a/web/claw/app/composables/useTranscript.ts b/web/claw/app/composables/useTranscript.ts index 8bd980e..9afdd5d 100644 --- a/web/claw/app/composables/useTranscript.ts +++ b/web/claw/app/composables/useTranscript.ts @@ -39,6 +39,9 @@ export interface TurnBlock { entries: TurnEntry[] /** Set by `turn_done`: `handled`, `failed`, or whatever the claw reports. */ outcome: string | null + /** Set by `turn_done` on a failure: the handler's own words on why + * the turn broke. null for every other outcome. */ + reason: string | null } /** One thing the agent asked its human, as the card renders it. */ @@ -230,7 +233,7 @@ export function useTranscript() { const pendingSent: { text: string; images: number; itemId: number }[] = [] function ensureLive(): TurnBlock { - if (!live.value) live.value = { reply: '', entries: [], outcome: null } + if (!live.value) live.value = { reply: '', entries: [], outcome: null, reason: null } return live.value } @@ -371,6 +374,7 @@ export function useTranscript() { case 'turn_done': { const turn = ensureLive() turn.outcome = event.outcome + turn.reason = event.reason // The clean end marker wins for the reply: deltas may have been // dropped (best-effort stream), turn_done carries the final text. if (event.reply !== null) turn.reply = event.reply