Skip to content

Commit 07f8c14

Browse files
committed
fix(web): merge late tool results across interleaved work-log rows
completeTurn force-completes every in-flight tool before any late tool_result arrives. Adjacent-only collapse then misses the matching completed row. Index tool rows by toolCallId on web and mobile so a late item.updated folds into the original call even when another tool sits between them.
1 parent d24ff67 commit 07f8c14

4 files changed

Lines changed: 182 additions & 2 deletions

File tree

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,89 @@ describe("buildThreadFeed", () => {
338338
expect(group.activities[0]?.id).toBe("tool-late-updated");
339339
});
340340

341+
it("merges a late tool result onto a non-adjacent completed work-log row", () => {
342+
const thread = makeThread({
343+
id: ThreadId.make("thread-late-multi-tool"),
344+
projectId: ProjectId.make("project-1"),
345+
title: "Late multi-tool result",
346+
latestTurn: {
347+
turnId: TurnId.make("turn-1"),
348+
state: "completed",
349+
requestedAt: "2026-04-01T00:00:00.000Z",
350+
startedAt: "2026-04-01T00:00:01.000Z",
351+
completedAt: "2026-04-01T00:00:04.000Z",
352+
assistantMessageId: null,
353+
},
354+
activities: [
355+
makeActivity({
356+
id: EventId.make("tool-a-complete"),
357+
kind: "tool.completed",
358+
tone: "tool",
359+
summary: "Grep",
360+
createdAt: "2026-04-01T00:00:01.000Z",
361+
turnId: TurnId.make("turn-1"),
362+
payload: {
363+
title: "Grep",
364+
itemType: "dynamic_tool_call",
365+
data: {
366+
toolCallId: "tool-a",
367+
toolName: "Grep",
368+
input: { pattern: "foo" },
369+
},
370+
},
371+
}),
372+
makeActivity({
373+
id: EventId.make("tool-b-complete"),
374+
kind: "tool.completed",
375+
tone: "tool",
376+
summary: "Read File",
377+
createdAt: "2026-04-01T00:00:02.000Z",
378+
turnId: TurnId.make("turn-1"),
379+
payload: {
380+
title: "Read File",
381+
itemType: "dynamic_tool_call",
382+
data: {
383+
toolCallId: "tool-b",
384+
toolName: "Read",
385+
input: { path: "/tmp/a.ts" },
386+
},
387+
},
388+
}),
389+
makeActivity({
390+
id: EventId.make("tool-a-late"),
391+
kind: "tool.updated",
392+
tone: "tool",
393+
summary: "Grep",
394+
createdAt: "2026-04-01T00:00:03.000Z",
395+
turnId: TurnId.make("turn-1"),
396+
payload: {
397+
title: "Grep",
398+
itemType: "dynamic_tool_call",
399+
status: "completed",
400+
data: {
401+
toolCallId: "tool-a",
402+
toolName: "Grep",
403+
input: { pattern: "foo" },
404+
result: { content: "src/example.ts:1:foo" },
405+
},
406+
},
407+
}),
408+
],
409+
});
410+
411+
const feed = buildThreadFeed(thread);
412+
const group = feed[0];
413+
expect(group).toMatchObject({ type: "activity-group" });
414+
if (!group || group.type !== "activity-group") {
415+
return;
416+
}
417+
418+
expect(group.activities).toHaveLength(2);
419+
expect(new Set(group.activities.map((activity) => activity.id))).toEqual(
420+
new Set(["tool-a-late", "tool-b-complete"]),
421+
);
422+
});
423+
341424
it("keeps MCP inputs available to expanded mobile work rows", () => {
342425
const turnId = TurnId.make("turn-mcp");
343426
const thread = makeThread({

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ function collapseDerivedWorkLogEntries(
452452
// Subagent rows collapse by identity, not adjacency (quiet-timeline
453453
// guarantee; mirrors web's session-logic).
454454
const taskRowIndex = new Map<string, number>();
455+
const toolRowIndex = new Map<string, number>();
455456
for (const entry of entries) {
456457
const isTaskRow =
457458
entry.taskId !== undefined &&
@@ -468,12 +469,32 @@ function collapseDerivedWorkLogEntries(
468469
collapsed.push(entry);
469470
continue;
470471
}
472+
// Late tool_result after completeTurn force-completes every in-flight
473+
// tool is not adjacent to its row. Merge by toolCallId, not adjacency.
474+
const toolCallId =
475+
entry.activityKind === "tool.updated" || entry.activityKind === "tool.completed"
476+
? entry.toolCallId
477+
: undefined;
478+
if (toolCallId !== undefined) {
479+
const existingIndex = toolRowIndex.get(toolCallId);
480+
if (existingIndex !== undefined) {
481+
collapsed[existingIndex] = mergeDerivedWorkLogEntries(collapsed[existingIndex]!, entry);
482+
continue;
483+
}
484+
}
471485
const previous = collapsed.at(-1);
472486
if (previous && shouldCollapseToolLifecycleEntries(previous, entry)) {
473-
collapsed[collapsed.length - 1] = mergeDerivedWorkLogEntries(previous, entry);
487+
const merged = mergeDerivedWorkLogEntries(previous, entry);
488+
collapsed[collapsed.length - 1] = merged;
489+
if (merged.toolCallId !== undefined) {
490+
toolRowIndex.set(merged.toolCallId, collapsed.length - 1);
491+
}
474492
continue;
475493
}
476494
collapsed.push(entry);
495+
if (toolCallId !== undefined) {
496+
toolRowIndex.set(toolCallId, collapsed.length - 1);
497+
}
477498
}
478499
return collapsed;
479500
}

‎apps/web/src/session-logic.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,61 @@ describe("deriveWorkLogEntries", () => {
15201520
expect(entries[0]?.id).toBe("tool-late-updated");
15211521
});
15221522

1523+
it("merges a late result onto a non-adjacent completed row when toolCallId matches", () => {
1524+
const activities: OrchestrationThreadActivity[] = [
1525+
makeActivity({
1526+
id: "tool-a-complete",
1527+
createdAt: "2026-02-23T00:00:02.000Z",
1528+
kind: "tool.completed",
1529+
summary: "Grep",
1530+
payload: {
1531+
itemType: "dynamic_tool_call",
1532+
title: "Grep",
1533+
data: {
1534+
toolCallId: "tool-a",
1535+
toolName: "Grep",
1536+
input: { pattern: "foo" },
1537+
},
1538+
},
1539+
}),
1540+
makeActivity({
1541+
id: "tool-b-complete",
1542+
createdAt: "2026-02-23T00:00:03.000Z",
1543+
kind: "tool.completed",
1544+
summary: "Read File",
1545+
payload: {
1546+
itemType: "dynamic_tool_call",
1547+
title: "Read File",
1548+
data: {
1549+
toolCallId: "tool-b",
1550+
toolName: "Read",
1551+
input: { path: "/tmp/a.ts" },
1552+
},
1553+
},
1554+
}),
1555+
makeActivity({
1556+
id: "tool-a-late",
1557+
createdAt: "2026-02-23T00:00:04.000Z",
1558+
kind: "tool.updated",
1559+
summary: "Grep",
1560+
payload: {
1561+
itemType: "dynamic_tool_call",
1562+
title: "Grep",
1563+
status: "completed",
1564+
data: {
1565+
toolCallId: "tool-a",
1566+
toolName: "Grep",
1567+
input: { pattern: "foo" },
1568+
result: { content: "src/example.ts:1:foo" },
1569+
},
1570+
},
1571+
}),
1572+
];
1573+
1574+
const entries = deriveWorkLogEntries(activities);
1575+
expect(entries.map((entry) => entry.id)).toEqual(["tool-a-late", "tool-b-complete"]);
1576+
});
1577+
15231578
it("collapses same-timestamp lifecycle rows even when completed sorts before updated by id", () => {
15241579
const activities: OrchestrationThreadActivity[] = [
15251580
makeActivity({

‎apps/web/src/session-logic.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ function collapseDerivedWorkLogEntries(
943943
// contains or how their progress rows interleave (quiet-timeline
944944
// guarantee).
945945
const spawnRowIndex = new Map<string, number>();
946+
const toolRowIndex = new Map<string, number>();
946947
// Batch membership is decided once, at the FIRST row seen for a taskId.
947948
// Claude background subagents settle between turns, so their completion
948949
// rows carry fresh synthetic turn ids (or none) — keying each row by its
@@ -993,12 +994,32 @@ function collapseDerivedWorkLogEntries(
993994
});
994995
continue;
995996
}
997+
// Late tool_result after completeTurn force-completes every in-flight
998+
// tool is not adjacent to its row. Merge by toolCallId, not adjacency.
999+
const toolCallId =
1000+
entry.activityKind === "tool.updated" || entry.activityKind === "tool.completed"
1001+
? entry.toolCallId
1002+
: undefined;
1003+
if (toolCallId !== undefined) {
1004+
const existingIndex = toolRowIndex.get(toolCallId);
1005+
if (existingIndex !== undefined) {
1006+
collapsed[existingIndex] = mergeDerivedWorkLogEntries(collapsed[existingIndex]!, entry);
1007+
continue;
1008+
}
1009+
}
9961010
const previous = collapsed.at(-1);
9971011
if (previous && shouldCollapseToolLifecycleEntries(previous, entry)) {
998-
collapsed[collapsed.length - 1] = mergeDerivedWorkLogEntries(previous, entry);
1012+
const merged = mergeDerivedWorkLogEntries(previous, entry);
1013+
collapsed[collapsed.length - 1] = merged;
1014+
if (merged.toolCallId !== undefined) {
1015+
toolRowIndex.set(merged.toolCallId, collapsed.length - 1);
1016+
}
9991017
continue;
10001018
}
10011019
collapsed.push(entry);
1020+
if (toolCallId !== undefined) {
1021+
toolRowIndex.set(toolCallId, collapsed.length - 1);
1022+
}
10021023
}
10031024
return collapsed;
10041025
}

0 commit comments

Comments
 (0)