From c2e8638d240efdc202190fcfd81726c838aebad3 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 15 Aug 2026 01:48:29 +1000 Subject: [PATCH] fix(webui): style tool-call failure from a structured field, not a string prefix chatStreamEvent gained a Failed bool, set from ToolCallEvent.Err != "" -- the field the rest of the codebase already treats as authoritative -- rather than the browser sniffing summary.startsWith "failed:" on rendered text. A successful tool whose own output starts with that literal string (a grep hit, a log line read back) was being styled as a failed step. --- internal/webui/api_chat.go | 8 ++++++-- internal/webui/api_chat_test.go | 13 ++++++++++--- ui/src/chat/chat-tools.js | 8 ++++++-- ui/test/chat-tools.test.js | 16 ++++++++++++++-- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/internal/webui/api_chat.go b/internal/webui/api_chat.go index f9dc6cdac..4c878354d 100644 --- a/internal/webui/api_chat.go +++ b/internal/webui/api_chat.go @@ -202,8 +202,10 @@ func (s *Server) handleChatMessage(w http.ResponseWriter, r *http.Request) { } // chatStreamEvent is one `data: {...}` frame of the chat stream. A tool frame -// names the tool in Tool and carries its one-line outcome in Text, so the -// browser can style the two apart instead of parsing a rendered string. +// names the tool in Tool, carries its one-line outcome in Text, and signals +// failure through Failed -- a structured field, so the browser styles the +// two apart by reading it rather than by sniffing a "failed:" prefix out of +// Text, which a successful tool's own output can start with too. // // Text is always emitted, empty or not: the browser concatenates it and // assigns it, so a missing key would put the string "undefined" into the @@ -214,6 +216,7 @@ type chatStreamEvent struct { Tool string `json:"tool,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"` Parameters string `json:"parameters,omitempty"` + Failed bool `json:"failed,omitempty"` SessionID string `json:"session_id,omitempty"` } @@ -232,6 +235,7 @@ func (f chatStreamSink) ToolCall(event gateway.ToolCallEvent) { ToolCallID: event.ID, Parameters: event.Parameters, Text: event.Summary(), + Failed: event.Err != "", }) } diff --git a/internal/webui/api_chat_test.go b/internal/webui/api_chat_test.go index 6d7c4f85c..f6994071a 100644 --- a/internal/webui/api_chat_test.go +++ b/internal/webui/api_chat_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "io" "log/slog" "net/http" @@ -516,6 +517,7 @@ type chatSSEEvent struct { Tool string `json:"tool"` ToolCallID string `json:"tool_call_id"` Parameters string `json:"parameters"` + Failed bool `json:"failed"` SessionID string `json:"session_id"` } @@ -610,6 +612,10 @@ func TestChatStreamReportsToolCalls(t *testing.T) { turn.Delta("checking") turn.ToolCall(gateway.ToolCallEvent{ID: "call-1", Name: "shell", Parameters: `{"cmd":"true"}`, Output: "exit 0\nignored trailing line"}) turn.ToolCall(gateway.ToolCallEvent{Name: "read", Err: "no such file"}) + // A successful tool whose own output happens to start with + // "failed:" (a grep hit, a log line read back) must be reported + // as not failed: Failed is driven by Err, never by Text content. + turn.ToolCall(gateway.ToolCallEvent{Name: "grep", Output: "failed: no such file\nmatch"}) turn.Delta(" and answering") return "checking and answering", nil } @@ -646,13 +652,14 @@ func TestChatStreamReportsToolCalls(t *testing.T) { case "delta": got = append(got, "text:"+ev.Text) case "tool": - got = append(got, "tool:"+ev.ToolCallID+":"+ev.Tool+":"+ev.Parameters+":"+ev.Text) + got = append(got, fmt.Sprintf("tool:%s:%s:%s:%s:failed=%v", ev.ToolCallID, ev.Tool, ev.Parameters, ev.Text, ev.Failed)) } } want := []string{ "text:checking", - `tool:call-1:shell:{"cmd":"true"}:exit 0`, - "tool::read::failed: no such file", + `tool:call-1:shell:{"cmd":"true"}:exit 0:failed=false`, + "tool::read::failed: no such file:failed=true", + "tool::grep::failed: no such file:failed=false", "text: and answering", } if len(got) != len(want) { diff --git a/ui/src/chat/chat-tools.js b/ui/src/chat/chat-tools.js index baa0388b5..02fa8b4dc 100644 --- a/ui/src/chat/chat-tools.js +++ b/ui/src/chat/chat-tools.js @@ -21,8 +21,12 @@ export function appendToolCall(host, event) { parameters && el("code.chat-tool-parameters", parameters), el("span.chat-tool-summary", summary), ); - // A failure reads as a completed step unless it is marked as one. - if (summary.startsWith("failed:")) line.className = "chat-tool failed"; + // A failure reads as a completed step unless it is marked as one. Styled + // from the structured `failed` field the server sends, not by sniffing + // the summary text: a successful tool can print output that itself starts + // with "failed:" (e.g. a grep hit, a log line), which a string prefix + // check would misread as the tool call itself having failed. + if (event.failed) line.className = "chat-tool failed"; toolList(host).append(line); return line; diff --git a/ui/test/chat-tools.test.js b/ui/test/chat-tools.test.js index a36c51dba..b5d4c13f9 100644 --- a/ui/test/chat-tools.test.js +++ b/ui/test/chat-tools.test.js @@ -44,7 +44,7 @@ test("a failed tool call is marked so it does not read as success", () => { const row = assistantBubble(); const host = row.querySelector(".chat-bubble"); - appendToolCall(host, { tool: "read", text: "failed: no such file" }); + appendToolCall(host, { tool: "read", text: "failed: no such file", failed: true }); const line = host.querySelector(".chat-tool"); assert.match(line.className, /failed/); @@ -54,7 +54,19 @@ test("a successful tool call is not marked as failed", () => { const row = assistantBubble(); const host = row.querySelector(".chat-bubble"); - appendToolCall(host, { tool: "read", text: "42 lines" }); + appendToolCall(host, { tool: "read", text: "42 lines", failed: false }); + + assert.doesNotMatch(host.querySelector(".chat-tool").className, /failed/); +}); + +// The styling decision reads the structured `failed` field, not the summary +// text: a successful tool's own output can start with "failed:" (a grep +// hit, a log line it read back), and that must not be styled as an error. +test("a successful tool whose output starts with 'failed:' is not marked as failed", () => { + const row = assistantBubble(); + const host = row.querySelector(".chat-bubble"); + + appendToolCall(host, { tool: "shell", text: "failed: no such file (grep match, exit 0)", failed: false }); assert.doesNotMatch(host.querySelector(".chat-tool").className, /failed/); });