Skip to content

Commit df8937e

Browse files
npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6wtellaho
andcommitted
fix(desktop): harden agent transcript reducer
Co-authored-by: Taylor Ho <[email protected]> Signed-off-by: Taylor Ho <[email protected]>
1 parent c8db319 commit df8937e

7 files changed

Lines changed: 202 additions & 300 deletions

‎desktop/src/features/agents/ui/AgentSessionTranscriptList.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export function AgentSessionTranscriptList({
7373
profiles,
7474
}: AgentTranscriptIdentityProps & {
7575
emptyDescription: string;
76-
isWorking?: boolean;
7776
items: TranscriptItem[];
7877
profiles?: UserProfileLookup;
7978
}) {

‎desktop/src/features/agents/ui/ManagedAgentSessionPanel.tsx‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ type ManagedAgentSessionPanelProps = {
3636
channelId?: string | null;
3737
className?: string;
3838
emptyDescription?: string;
39-
isWorking?: boolean;
4039
rawLayout?: "responsive" | "exclusive";
4140
showHeader?: boolean;
4241
showRaw?: boolean;
@@ -48,7 +47,6 @@ export function ManagedAgentSessionPanel({
4847
channelId = null,
4948
className,
5049
emptyDescription = "Mention this agent in a channel to watch the next turn.",
51-
isWorking = false,
5250
rawLayout = "responsive",
5351
showHeader = true,
5452
showRaw = true,
@@ -101,7 +99,6 @@ export function ManagedAgentSessionPanel({
10199
errorMessage={errorMessage}
102100
events={scopedEvents}
103101
hasObserver={hasObserver}
104-
isWorking={isWorking}
105102
profiles={profiles}
106103
rawLayout={rawLayout}
107104
showRaw={showRaw}
@@ -155,7 +152,6 @@ function SessionBody({
155152
errorMessage,
156153
events,
157154
hasObserver,
158-
isWorking,
159155
profiles,
160156
rawLayout,
161157
showRaw,
@@ -169,7 +165,6 @@ function SessionBody({
169165
errorMessage: string | null;
170166
events: ObserverEvent[];
171167
hasObserver: boolean;
172-
isWorking: boolean;
173168
profiles?: UserProfileLookup;
174169
rawLayout: "responsive" | "exclusive";
175170
showRaw: boolean;
@@ -211,7 +206,6 @@ function SessionBody({
211206
agentName={agentName}
212207
agentPubkey={agentPubkey}
213208
emptyDescription={emptyDescription}
214-
isWorking={isWorking}
215209
items={transcript}
216210
profiles={profiles}
217211
/>

‎desktop/src/features/agents/ui/agentSessionTranscript.test.mjs‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,190 @@ test("buildTranscript categorizes explicit Buzz tool calls for the activity bar"
194194
assert.deepEqual(item.args, { limit: 20 });
195195
assert.equal(item.status, "completed");
196196
});
197+
198+
function sessionUpdate(seq, update, overrides = {}) {
199+
return {
200+
...baseEvent,
201+
...overrides,
202+
seq,
203+
kind: "acp_read",
204+
payload: {
205+
method: "session/update",
206+
params: {
207+
sessionId: overrides.sessionId ?? baseEvent.sessionId,
208+
update,
209+
},
210+
},
211+
};
212+
}
213+
214+
function assistantChunk(seq, messageId, text, overrides = {}) {
215+
return sessionUpdate(
216+
seq,
217+
{
218+
sessionUpdate: "agent_message_chunk",
219+
messageId,
220+
content: { type: "text", text },
221+
},
222+
overrides,
223+
);
224+
}
225+
226+
test("buildTranscript de-duplicates repeated tool updates into one canonical row", () => {
227+
const items = toolItems([
228+
acpToolUpdate(40, {
229+
sessionUpdate: "tool_call",
230+
toolCallId: "call-dupe",
231+
status: "executing",
232+
title: "shell",
233+
kind: "shell",
234+
rawInput: { command: "echo hi" },
235+
}),
236+
acpToolUpdate(41, {
237+
sessionUpdate: "tool_call_update",
238+
toolCallId: "call-dupe",
239+
status: "completed",
240+
title: "shell",
241+
kind: "shell",
242+
rawOutput: "hi",
243+
}),
244+
acpToolUpdate(42, {
245+
sessionUpdate: "tool_call_update",
246+
toolCallId: "call-dupe",
247+
status: "completed",
248+
title: "shell",
249+
kind: "shell",
250+
rawOutput: "hi",
251+
}),
252+
]);
253+
254+
assert.equal(items.length, 1);
255+
assert.equal(items[0].id, `tool:${baseEvent.channelId}:call-dupe`);
256+
assert.equal(items[0].status, "completed");
257+
assert.equal(items[0].result, "hi");
258+
});
259+
260+
test("buildTranscript keeps a completed tool terminal when a late executing call arrives", () => {
261+
const [item] = toolItems([
262+
acpToolUpdate(50, {
263+
sessionUpdate: "tool_call_update",
264+
toolCallId: "call-regression",
265+
status: "completed",
266+
title: "shell",
267+
kind: "shell",
268+
rawOutput: "done",
269+
}),
270+
acpToolUpdate(51, {
271+
sessionUpdate: "tool_call",
272+
toolCallId: "call-regression",
273+
status: "executing",
274+
title: "shell",
275+
kind: "shell",
276+
rawInput: { command: "echo done" },
277+
}),
278+
]);
279+
280+
assert.equal(item.status, "completed");
281+
assert.equal(item.completedAt, baseEvent.timestamp);
282+
assert.deepEqual(item.args, { command: "echo done" });
283+
assert.equal(item.result, "done");
284+
});
285+
286+
test("buildTranscript rebuilds out-of-order tool frames as one canonical row with retained ids", () => {
287+
const [item] = toolItems([
288+
sessionUpdate(
289+
60,
290+
{
291+
sessionUpdate: "tool_call_update",
292+
toolCallId: "call-out-of-order",
293+
status: "completed",
294+
title: "read_file",
295+
kind: "read_file",
296+
rawOutput: "file contents",
297+
},
298+
{
299+
channelId: "22222222-2222-2222-2222-222222222222",
300+
sessionId: "sess-2",
301+
turnId: "turn-2",
302+
timestamp: "2026-06-18T00:00:05Z",
303+
},
304+
),
305+
sessionUpdate(
306+
61,
307+
{
308+
sessionUpdate: "tool_call",
309+
toolCallId: "call-out-of-order",
310+
status: "executing",
311+
title: "read_file",
312+
kind: "read_file",
313+
rawInput: { path: "AGENTS.md" },
314+
},
315+
{
316+
channelId: "22222222-2222-2222-2222-222222222222",
317+
sessionId: "sess-2",
318+
turnId: "turn-2",
319+
timestamp: "2026-06-18T00:00:04Z",
320+
},
321+
),
322+
]);
323+
324+
assert.equal(
325+
item.id,
326+
"tool:22222222-2222-2222-2222-222222222222:call-out-of-order",
327+
);
328+
assert.equal(item.status, "completed");
329+
assert.deepEqual(item.args, { path: "AGENTS.md" });
330+
assert.equal(item.channelId, "22222222-2222-2222-2222-222222222222");
331+
assert.equal(item.turnId, "turn-2");
332+
assert.equal(item.sessionId, "sess-2");
333+
});
334+
335+
test("buildTranscript coalesces assistant chunks until the message is sealed", () => {
336+
const messages = buildTranscript([
337+
assistantChunk(70, "msg-1", "Hello "),
338+
assistantChunk(71, "msg-1", "world"),
339+
]).filter((item) => item.type === "message" && item.role === "assistant");
340+
341+
assert.equal(messages.length, 1);
342+
assert.equal(messages[0].text, "Hello world");
343+
assert.equal(messages[0].id, `assistant:${baseEvent.channelId}:msg-1`);
344+
});
345+
346+
test("buildTranscript starts a continuation for same-message chunks after sealing", () => {
347+
const messages = buildTranscript([
348+
assistantChunk(80, "msg-2", "First"),
349+
acpToolUpdate(81, {
350+
sessionUpdate: "tool_call",
351+
toolCallId: "call-seal",
352+
status: "executing",
353+
title: "shell",
354+
kind: "shell",
355+
}),
356+
assistantChunk(82, "msg-2", "Second"),
357+
]).filter((item) => item.type === "message" && item.role === "assistant");
358+
359+
assert.equal(messages.length, 2);
360+
assert.equal(messages[0].text, "First");
361+
assert.equal(messages[1].text, "Second");
362+
assert.match(messages[1].id, /:c\d+$/);
363+
});
364+
365+
test("buildTranscript preserves channel, turn, and session ids through message updates", () => {
366+
const [message] = buildTranscript([
367+
assistantChunk(90, "msg-identity", "One ", {
368+
channelId: "33333333-3333-3333-3333-333333333333",
369+
sessionId: "sess-identity",
370+
turnId: "turn-identity",
371+
}),
372+
assistantChunk(91, "msg-identity", "Two", {
373+
channelId: "33333333-3333-3333-3333-333333333333",
374+
sessionId: null,
375+
turnId: null,
376+
}),
377+
]).filter((item) => item.type === "message" && item.role === "assistant");
378+
379+
assert.equal(message.text, "One Two");
380+
assert.equal(message.channelId, "33333333-3333-3333-3333-333333333333");
381+
assert.equal(message.turnId, "turn-identity");
382+
assert.equal(message.sessionId, "sess-identity");
383+
});

‎desktop/src/features/agents/ui/agentSessionTranscript.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,18 @@ function upsertMetadata(
234234
});
235235
}
236236

237+
function isTerminalToolStatus(status: ToolStatus) {
238+
return status === "completed" || status === "failed";
239+
}
240+
241+
function mergeToolStatus(existing: ToolStatus, next: ToolStatus): ToolStatus {
242+
if (isTerminalToolStatus(existing) && !isTerminalToolStatus(next)) {
243+
return existing;
244+
}
245+
246+
return next;
247+
}
248+
237249
function upsertTool(
238250
d: TranscriptDraft,
239251
id: string,
@@ -261,18 +273,18 @@ function upsertTool(
261273
} else if (!existing.buzzToolName && !isGenericToolTitle(toolName)) {
262274
updatedToolName = toolName;
263275
}
276+
const mergedStatus = mergeToolStatus(existing.status, status);
264277
replaceItem(d, id, {
265278
...existing,
266279
title: updatedTitle,
267280
toolName: updatedToolName,
268281
buzzToolName: updatedBuzzToolName,
269-
status,
282+
status: mergedStatus,
270283
args: Object.keys(args).length > 0 ? args : existing.args,
271284
result: result || existing.result,
272285
isError: isError || existing.isError,
273286
completedAt:
274-
(status === "completed" || status === "failed") &&
275-
existing.completedAt == null
287+
isTerminalToolStatus(mergedStatus) && existing.completedAt == null
276288
? timestamp
277289
: existing.completedAt,
278290
channelId: ctx.channelId,

‎desktop/src/features/agents/ui/agentSessionTranscriptPresentation.test.mjs‎

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from "node:assert/strict";
22
import test from "node:test";
33

44
import {
5-
buildTranscriptPresentation,
65
getActivityHeadline,
76
isMeaningfulItem,
87
} from "./agentSessionTranscriptPresentation.ts";
@@ -80,69 +79,3 @@ test("isMeaningfulItem ignores lifecycle noise and metadata", () => {
8079
true,
8180
);
8281
});
83-
84-
test("buildTranscriptPresentation marks running tools as active while working", () => {
85-
const items = [
86-
makeMessage({ id: "msg:user", role: "user", text: "Please help" }),
87-
makeTool({ id: "tool:running", status: "executing" }),
88-
];
89-
90-
const presentation = buildTranscriptPresentation(items, true);
91-
92-
assert.equal(presentation.state, "tool_running");
93-
assert.equal(presentation.headline, "Send Message");
94-
assert.equal(presentation.counts.tools, 1);
95-
assert.equal(presentation.counts.messages, 1);
96-
assert.ok(presentation.activeItemIds.has("tool:running"));
97-
});
98-
99-
test("buildTranscriptPresentation highlights assistant streaming while working", () => {
100-
const items = [
101-
makeMessage({ id: "msg:assistant", role: "assistant", text: "Drafting" }),
102-
];
103-
104-
const presentation = buildTranscriptPresentation(items, true);
105-
106-
assert.equal(presentation.state, "responding");
107-
assert.equal(presentation.headline, "Drafting");
108-
assert.ok(presentation.activeItemIds.has("msg:assistant"));
109-
});
110-
111-
test("buildTranscriptPresentation surfaces lifecycle errors", () => {
112-
const items = [
113-
makeTool({
114-
id: "tool:done",
115-
status: "completed",
116-
completedAt: "2026-06-14T19:00:05.000Z",
117-
}),
118-
{
119-
id: "life:error",
120-
type: "lifecycle",
121-
title: "Turn error",
122-
text: "timeout",
123-
timestamp: "2026-06-14T19:00:06.000Z",
124-
},
125-
];
126-
127-
const presentation = buildTranscriptPresentation(items, false);
128-
129-
assert.equal(presentation.state, "error");
130-
assert.equal(presentation.hasError, true);
131-
assert.equal(presentation.headline, "Turn error");
132-
});
133-
134-
test("buildTranscriptPresentation returns idle state when not working", () => {
135-
const items = [
136-
makeTool({
137-
id: "tool:done",
138-
status: "completed",
139-
completedAt: "2026-06-14T19:00:05.000Z",
140-
}),
141-
];
142-
143-
const presentation = buildTranscriptPresentation(items, false);
144-
145-
assert.equal(presentation.state, "idle");
146-
assert.equal(presentation.activeItemIds.size, 0);
147-
assert.equal(presentation.headline, "Send Message");
148-
});

0 commit comments

Comments
 (0)