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.
This commit is contained in:
sam committed 2026-08-15 01:48:29 +10:00
1 parent ad93ffb9bb
commit d43416f2fe
4 files changed
+36 -9

No files matched your search

+6 -2
View File
@@ -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 != "",
})
}
+10 -3
View File
@@ -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) {
+6 -2
View File
@@ -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;
+14 -2
View File
@@ -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/);
});