Skip to content

Commit 7b3972c

Browse files
committed
fix(ui): preserve terminal item-only tool outcomes
Carry live item completion through tool-message projection, history coalescing, and card extraction. A completed activity with no definite outcome now shows Outcome unknown while its enclosing run remains active. Keep item completion separate from result receipt so the UI preserves raw output and active history-placeholder behavior. Cover item-only completion, reconnect replay, and suppressed native events while retaining the original end-to-end assertions.
1 parent affb994 commit 7b3972c

6 files changed

Lines changed: 169 additions & 106 deletions

File tree

‎docs/web/control-ui/chat.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ and outcomes available. Completed group summaries retain their operation counts.
2020
Tool Search calls use the called tool's name, icon, and input details in tool rows
2121
and activity summaries.
2222

23+
A tool stops showing **Running** when its completion arrives, even while the
24+
parent turn continues. If that completion does not establish success or failure,
25+
the row shows **Outcome unknown**. Partial output alone does not finish a tool.
26+
2327
When the parent turn has ended but subagents are still active, the chat shows
2428
**Waiting on subagents**. A single active child already loaded in the pane can
2529
be opened from its name beside the indicator. Elapsed time appears when the

‎ui/src/lib/chat/tool-cards.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,12 @@ function extractToolCards(message: unknown): ToolCard[] {
380380
inputText: serializeToolInput(args),
381381
...(details !== undefined ? { details } : {}),
382382
...(isLiveToolStream
383-
? { live: true, completed: m["__openclawToolStreamResultReceived"] === true }
383+
? {
384+
live: true,
385+
completed:
386+
m["__openclawToolStreamResultReceived"] === true ||
387+
m["__openclawToolStreamItemEnded"] === true,
388+
}
384389
: {}),
385390
...(liveDiffStat ? { liveDiffStat } : {}),
386391
messageId: transcriptMessageId,
@@ -437,7 +442,7 @@ function extractToolCards(message: unknown): ToolCard[] {
437442
existing.parentToolCallId ??= parentToolCallId;
438443
// Live tool-stream messages emit a toolresult block for partial
439444
// `update` output too; completion there is owned by the stream's
440-
// resultReceived marker (set at card creation), not block presence —
445+
// terminal markers (set at card creation), not block presence —
441446
// otherwise a running tool flips to "succeeded" mid-execution.
442447
if (!isLiveToolStream) {
443448
existing.completed = true;

‎ui/src/pages/chat/chat-tool-activity-coalesce.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ function coalesceTurn(items: ChatItem[]): ChatItem[] {
311311
? owner.source.index
312312
: invocation.first;
313313
const result = invocation.result;
314-
const completed = result !== undefined && result.rank > 1;
314+
const resultReceived = result !== undefined && result.rank > 1;
315+
const itemEnded = invocation.live?.["__openclawToolStreamItemEnded"] === true;
316+
const completed = resultReceived || itemEnded;
315317
const transcript =
316318
message.messageId ??
317319
metadata?.id ??
@@ -328,6 +330,8 @@ function coalesceTurn(items: ChatItem[]): ChatItem[] {
328330
owner.runId,
329331
Boolean(invocation.live),
330332
completed,
333+
resultReceived,
334+
itemEnded,
331335
transcript,
332336
invocation.live?.["__openclawToolStreamDiffStat"],
333337
invocation.live?.["__openclawToolStreamReceivedAt"],
@@ -390,7 +394,8 @@ function coalesceTurn(items: ChatItem[]): ChatItem[] {
390394
...(invocation.live
391395
? {
392396
__openclawToolStreamLive: true,
393-
__openclawToolStreamResultReceived: completed,
397+
__openclawToolStreamResultReceived: resultReceived,
398+
__openclawToolStreamItemEnded: itemEnded,
394399
__openclawToolStreamDiffStat: completed
395400
? undefined
396401
: invocation.live["__openclawToolStreamDiffStat"],

‎ui/src/pages/chat/components/chat-tool-cards.test.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@ describe("tool-card outcomes", () => {
570570
[live, true],
571571
[history.messages[0], false],
572572
] as const) {
573-
const expectedLabel = status === undefined && runActive ? "Running" : label;
574573
const group = createToolGroup("outcome", [createMessageEntry("call", message)]);
575574
render(renderActivityGroup([group], { showReasoning: false, runActive }), container);
576575
expect(container.querySelectorAll(".chat-tool-failure")).toHaveLength(
@@ -585,14 +584,12 @@ describe("tool-card outcomes", () => {
585584
}),
586585
container,
587586
);
588-
expect(textOf(container, ".chat-tool-card__outcome")).toBe(expectedLabel);
589-
expect(container.querySelector(".chat-tool-row--running") !== null).toBe(
590-
status === undefined && runActive,
591-
);
587+
expect(textOf(container, ".chat-tool-card__outcome")).toBe(label);
588+
expect(container.querySelector(".chat-tool-row--running")).toBeNull();
592589
const card = extractToolCardsCached(message)[0]!;
593590
expect(card.outputText).toBeUndefined();
594591
expect(card.isError).toBeUndefined();
595-
expect(card.completed).not.toBe(true);
592+
expect(card.completed === true).toBe(message === live);
596593
}
597594
expect(live).toMatchObject({ __openclawToolStreamResultReceived: false });
598595
expect(saved).not.toHaveProperty("activity");

‎ui/src/pages/chat/tool-stream.node.test.ts‎

Lines changed: 138 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
33
import { readToolApprovalReviews } from "../../lib/chat/tool-approval-reviews.ts";
44
import { readPreparedActivity, summarizeToolGroup } from "../../lib/chat/tool-call-grouping.ts";
5-
import { extractToolCardsCached } from "../../lib/chat/tool-cards.ts";
5+
import { extractToolCardsCached, resolveToolCardOutcome } from "../../lib/chat/tool-cards.ts";
66
import { coalesceToolActivityMessages } from "./chat-tool-activity-coalesce.ts";
77
import type { ToolStreamEntry } from "./tool-stream-contract.ts";
88
import { buildToolStreamIdentity } from "./tool-stream-identity.ts";
@@ -46,106 +46,151 @@ afterAll(() => {
4646
afterEach(() => vi.useRealTimers());
4747

4848
describe("app-tool-stream approval lifecycle", () => {
49-
it("keeps raw details while terminal prepared activity wins history and reconnect replay", () => {
50-
const host = createHost({ chatRunId: "run-1" });
51-
const started = {
52-
itemId: "tool:call",
53-
toolCallId: "call",
54-
kind: "tool",
55-
name: "process",
56-
title: "Check process",
57-
phase: "start",
58-
status: "running",
59-
hideFromChannelProgress: true,
60-
};
61-
const completed = {
62-
...started,
63-
phase: "end",
64-
status: "completed",
65-
title: "Stop process",
66-
hideFromChannelProgress: false,
67-
};
68-
const completedEvent = Object.freeze({ ...completed, diagnostic: { source: "native-tool" } });
69-
handleAgentEvent(
70-
host,
71-
agentEvent("run-1", 1, "tool", {
49+
it.each([
50+
{ withResult: true, kind: "tool" },
51+
{ withResult: false, kind: "tool" },
52+
{ withResult: false, kind: "command" },
53+
])(
54+
"keeps terminal $kind activity through history and replay (result: $withResult)",
55+
({ withResult, kind }) => {
56+
const host = createHost({ chatRunId: "run-1" });
57+
const started = {
58+
itemId: "tool:call",
7259
toolCallId: "call",
60+
kind,
7361
name: "process",
62+
title: "Check process",
7463
phase: "start",
75-
args: { action: "poll" },
76-
}),
77-
);
78-
handleAgentEvent(host, agentEvent("run-1", 2, "item", started));
64+
status: "running",
65+
hideFromChannelProgress: true,
66+
};
67+
const completed = {
68+
...started,
69+
phase: "end",
70+
status: withResult ? "completed" : undefined,
71+
title: "Stop process",
72+
hideFromChannelProgress: false,
73+
};
74+
const completedEvent = Object.freeze({ ...completed, diagnostic: { source: "native-tool" } });
75+
handleAgentEvent(
76+
host,
77+
agentEvent("run-1", 1, "tool", {
78+
toolCallId: "call",
79+
name: "process",
80+
phase: "start",
81+
args: { action: "poll" },
82+
}),
83+
);
84+
handleAgentEvent(host, agentEvent("run-1", 2, "item", started));
85+
if (withResult) {
86+
handleAgentEvent(
87+
host,
88+
agentEvent("run-1", 3, "tool", {
89+
toolCallId: "call",
90+
name: "process",
91+
phase: "result",
92+
isError: false,
93+
result: "raw execution result",
94+
}),
95+
);
96+
}
97+
handleAgentEvent(host, agentEvent("run-1", 4, "item", completedEvent));
98+
handleAgentEvent(host, agentEvent("run-1", 2, "item", started));
99+
const live = [...host.toolStreamById.values()][0]!.message;
100+
const reconciled = coalesceToolActivityMessages([
101+
{
102+
kind: "message",
103+
key: "history",
104+
message: {
105+
role: "assistant",
106+
runId: "run-1",
107+
__openclaw: { id: "history-call" },
108+
content: [
109+
{ type: "toolCall", id: "call", name: "process", arguments: { action: "poll" } },
110+
],
111+
activity: [started],
112+
},
113+
},
114+
{ kind: "message", key: "live", message: live },
115+
]);
116+
const messages = reconciled.flatMap((item) =>
117+
item.kind === "message" ? [item.message] : [],
118+
);
119+
const activity = messages.flatMap(readPreparedActivity);
120+
expect(activity).toEqual([completed]);
121+
expect(summarizeToolGroup(activity)).toBe(
122+
withResult ? "1 other operation" : "1 other operation · 1 unknown",
123+
);
124+
expect(activity[0]).not.toHaveProperty("diagnostic");
125+
expect(completedEvent.diagnostic).toEqual({ source: "native-tool" });
126+
const cards = messages.flatMap(extractToolCardsCached);
127+
expect(cards).toMatchObject([{ args: { action: "poll" }, completed: true }]);
128+
expect(cards.map((card) => card.outputText)).toEqual([
129+
withResult ? "raw execution result" : undefined,
130+
]);
131+
expect(cards.map((card) => resolveToolCardOutcome(card, true))).toEqual([
132+
withResult ? "succeeded" : "unknown",
133+
]);
134+
expect(messages[0]).toMatchObject({ __openclawToolStreamResultReceived: withResult });
135+
const quietHistory = coalesceToolActivityMessages([
136+
{
137+
kind: "message",
138+
key: "call",
139+
message: {
140+
role: "assistant",
141+
runId: "run-1",
142+
activity: [started],
143+
content: [
144+
{ type: "toolCall", id: "call", name: "process", arguments: { action: "poll" } },
145+
],
146+
},
147+
},
148+
{
149+
kind: "message",
150+
key: "result",
151+
message: {
152+
role: "toolResult",
153+
runId: "run-1",
154+
toolCallId: "call",
155+
toolName: "process",
156+
isError: false,
157+
content: "raw wait result",
158+
activity: [],
159+
},
160+
},
161+
]);
162+
expect(
163+
quietHistory.flatMap((item) =>
164+
item.kind === "message" ? readPreparedActivity(item.message) : [],
165+
),
166+
).toEqual([]);
167+
resetToolStream(host);
168+
},
169+
);
170+
171+
it("keeps suppressed native item completion from settling the visible call", () => {
172+
const host = createHost({ chatRunId: "run-1" });
173+
emitTool(host, "run-1", 1, {
174+
toolCallId: "call",
175+
name: "exec",
176+
phase: "start",
177+
args: { command: "check" },
178+
});
79179
handleAgentEvent(
80180
host,
81-
agentEvent("run-1", 3, "tool", {
181+
agentEvent("run-1", 2, "item", {
182+
itemId: "native-call",
82183
toolCallId: "call",
83-
name: "process",
84-
phase: "result",
85-
isError: false,
86-
result: "raw execution result",
184+
kind: "command",
185+
name: "exec",
186+
title: "Check",
187+
phase: "end",
188+
suppressChannelProgress: true,
87189
}),
88190
);
89-
handleAgentEvent(host, agentEvent("run-1", 4, "item", completedEvent));
90-
handleAgentEvent(host, agentEvent("run-1", 2, "item", started));
91-
const live = [...host.toolStreamById.values()][0]!.message;
92-
const reconciled = coalesceToolActivityMessages([
93-
{
94-
kind: "message",
95-
key: "history",
96-
message: {
97-
role: "assistant",
98-
runId: "run-1",
99-
__openclaw: { id: "history-call" },
100-
content: [
101-
{ type: "toolCall", id: "call", name: "process", arguments: { action: "poll" } },
102-
],
103-
activity: [started],
104-
},
105-
},
106-
{ kind: "message", key: "live", message: live },
107-
]);
108-
const messages = reconciled.flatMap((item) => (item.kind === "message" ? [item.message] : []));
109-
const activity = messages.flatMap(readPreparedActivity);
110-
expect(activity).toEqual([completed]);
111-
expect(summarizeToolGroup(activity)).toBe("1 other operation");
112-
expect(activity[0]).not.toHaveProperty("diagnostic");
113-
expect(completedEvent.diagnostic).toEqual({ source: "native-tool" });
114-
expect(messages.flatMap(extractToolCardsCached)).toMatchObject([
115-
{ args: { action: "poll" }, outputText: "raw execution result", completed: true },
116-
]);
117-
const quietHistory = coalesceToolActivityMessages([
118-
{
119-
kind: "message",
120-
key: "call",
121-
message: {
122-
role: "assistant",
123-
runId: "run-1",
124-
activity: [started],
125-
content: [
126-
{ type: "toolCall", id: "call", name: "process", arguments: { action: "poll" } },
127-
],
128-
},
129-
},
130-
{
131-
kind: "message",
132-
key: "result",
133-
message: {
134-
role: "toolResult",
135-
runId: "run-1",
136-
toolCallId: "call",
137-
toolName: "process",
138-
isError: false,
139-
content: "raw wait result",
140-
activity: [],
141-
},
142-
},
143-
]);
144-
expect(
145-
quietHistory.flatMap((item) =>
146-
item.kind === "message" ? readPreparedActivity(item.message) : [],
147-
),
148-
).toEqual([]);
191+
const [card] = extractToolCardsCached(host.chatToolMessages[0]);
192+
expect(resolveToolCardOutcome(card!, true)).toBe("running");
193+
expect(card?.outputText).toBeUndefined();
149194
resetToolStream(host);
150195
});
151196

‎ui/src/pages/chat/tool-stream.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ function refreshSessionStatusModel(host: ToolStreamHost, data: Record<string, un
103103
}
104104

105105
function buildToolStreamMessage(entry: ToolStreamEntry): Record<string, unknown> {
106+
const itemEnded = entry.activity?.some(
107+
(item) =>
108+
(item.toolCallId ?? item.itemId) === entry.toolCallId &&
109+
item.phase === "end" &&
110+
!item.suppressChannelProgress,
111+
);
106112
const content: Array<Record<string, unknown>> = [];
107113
content.push({
108114
type: "toolcall",
@@ -132,12 +138,13 @@ function buildToolStreamMessage(entry: ToolStreamEntry): Record<string, unknown>
132138
content,
133139
timestamp: entry.startedAt,
134140
// Running-state markers: only live tool-stream cards may show a spinner,
135-
// and completion comes from the result event — partial `update` output
136-
// must not end the running state. Transcript messages never carry these,
141+
// and completion comes from a result or live item end, never partial output
142+
// or a status-less history placeholder. Transcript messages never carry these,
137143
// so historical output-less calls (aborted runs) stay inert.
138144
__openclawToolStreamLive: true,
139145
__openclawToolStreamResultReceived: entry.resultReceived === true,
140-
...(entry.resultReceived !== true && entry.liveDiffStat
146+
__openclawToolStreamItemEnded: itemEnded === true,
147+
...(entry.resultReceived !== true && !itemEnded && entry.liveDiffStat
141148
? { __openclawToolStreamDiffStat: entry.liveDiffStat }
142149
: {}),
143150
__openclawToolStreamReceivedAt: entry.receivedAt,

0 commit comments

Comments
 (0)