Skip to content

The agent that grows with you

fix(desktop): a reused tool call id no longer overwrites a finished tool row (#128009)

Siddharth Balyan5dc1c084e932
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/tools.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/tools.ts
index 2fb7ba7ca73..b96f83e091e 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/tools.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/tools.ts
@@ -10,7 +10,6 @@ import { pruneDelegateFallbackSubagents, upsertSubagent } from '@/store/subagent
import { reportMcpToolResult } from '@/store/suggestion-providers/repair'
import { invalidateSkillSuggestionIndex } from '@/store/suggestion-providers/skill'
import { restoreSessionTodosFromSnapshot } from '@/store/todos'
-import { recordToolDiff } from '@/store/tool-diffs'
import { setSessionDraftingTool } from '@/store/tool-drafting'
import { notifyWorkspaceChanged, toolChangedPath, toolMayMutateFiles } from '@/store/workspace-events'
@@ -146,10 +145,6 @@ export function handleToolEvent(ctx: GatewayEventContext): boolean {
)
}
- if (typeof payload?.inline_diff === 'string' && payload.inline_diff.trim()) {
- recordToolDiff(payload.tool_id || payload.name || '', payload.inline_diff)
- }
-
// A file-mutating tool just finished — nudge the git-mirroring surfaces
// (coding rail, review pane, file tree) to refresh. Event-driven, not
// polled: fires exactly when the agent touches the tree.
diff --git a/apps/desktop/src/components/assistant-ui/tool/fallback.tsx b/apps/desktop/src/components/assistant-ui/tool/fallback.tsx
index daecfc810d1..e097dbf18ce 100644
--- a/apps/desktop/src/components/assistant-ui/tool/fallback.tsx
+++ b/apps/desktop/src/components/assistant-ui/tool/fallback.tsx
@@ -50,7 +50,6 @@ import { cn } from '@/lib/utils'
import { recordPreviewArtifact } from '@/store/preview-status'
import { sessionApprovalRequest } from '@/store/prompts'
import { $showToolActivity } from '@/store/tool-activity'
-import { $toolInlineDiff } from '@/store/tool-diffs'
import { $toolRowDismissed, dismissToolRow } from '@/store/tool-dismiss'
import {
$anyToolDisclosureOpen,
@@ -74,7 +73,6 @@ import {
looksRedundant,
type SearchResultRow,
selectMessageRunning,
- stripInlineDiffChrome,
toolCopyPayload,
toolEntryDisclosureId,
type ToolPart,
@@ -381,10 +379,7 @@ function ToolEntry({ part }: ToolEntryProps) {
const disclosureId = toolEntryDisclosureId(messageId, stablePart)
const dismissed = useStore($toolRowDismissed(disclosureId))
const isPending = messageRunning && result === undefined && completedAt === undefined
- // Subscribe to this tool's diff only, so a live patch for one tool doesn't
- // re-render every mounted tool row (the factory caches a per-id atom).
- const sideDiff = useStore($toolInlineDiff(toolCallId ?? ''))
- const inlineDiff = stripInlineDiffChrome(sideDiff) || inlineDiffFromResult(toolResultRecord(stablePart))
+ const inlineDiff = inlineDiffFromResult(toolResultRecord(stablePart))
const isFileEdit = isFileEditTool(toolName)
const defaultOpen = Boolean(inlineDiff) && !hideCodeDiffs
const disclosureOpen = useDisclosureOpen(disclosureId, defaultOpen)
diff --git a/apps/desktop/src/components/assistant-ui/tool/inline-edit-persistence.test.tsx b/apps/desktop/src/components/assistant-ui/tool/inline-edit-persistence.test.tsx
index 50104a3862e..eb7aeed87ec 100644
--- a/apps/desktop/src/components/assistant-ui/tool/inline-edit-persistence.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/tool/inline-edit-persistence.test.tsx
@@ -6,7 +6,6 @@ import { afterEach, beforeEach, expect, it } from 'vitest'
import { parseAnsi } from '@/lib/ansi'
import { type ChatMessage, type GatewayEventPayload, toChatMessages, upsertToolPart } from '@/lib/chat-messages'
import { toRuntimeMessage } from '@/lib/chat-runtime'
-import { getToolDiff } from '@/store/tool-diffs'
import { $toolDisclosureStates, setHideCodeDiffs, setToolViewMode } from '@/store/tool-view'
import type { SessionMessage } from '@/types/hermes'
@@ -92,7 +91,6 @@ const receipts: Record<string, Receipt> = process.env.INLINE_EDIT_RECEIPTS
: fixtures
beforeEach(() => {
- expect(getToolDiff('edit-call')).toBe('')
$toolDisclosureStates.set({})
setHideCodeDiffs(false)
setToolViewMode('product')
@@ -127,8 +125,6 @@ it.each(Object.entries(receipts))(
)
for (const rows of [receipt.rows, withCommentary, receipt.projected]) {
- expect(getToolDiff(receipt.completion.tool_id!)).toBe('')
-
for (const serialized of [false, true]) {
const history = rows.map(row =>
serialized && row.display_metadata ? { ...row, display_metadata: JSON.stringify(row.display_metadata) } : row
diff --git a/apps/desktop/src/lib/chat-messages/tool-parts.ts b/apps/desktop/src/lib/chat-messages/tool-parts.ts
index a3192f28f56..9a1e89357cd 100644
--- a/apps/desktop/src/lib/chat-messages/tool-parts.ts
+++ b/apps/desktop/src/lib/chat-messages/tool-parts.ts
@@ -171,12 +171,28 @@ function findToolPartIndex(
const overlaps = (index: number) => hasToolMatchOverlap(matchValues, toolPartMatchValues(parts[index]))
if (stableId) {
- const stableIndex = parts.findIndex(part => part.type === 'tool-call' && part.toolCallId === stableId)
+ const stableIndex = parts.findIndex(
+ part => part.type === 'tool-call' && part.toolCallId === stableId && !Object.hasOwn(part, 'result')
+ )
if (stableIndex >= 0) {
return stableIndex
}
+ const repeatedIndex =
+ phase === 'complete'
+ ? parts.findLastIndex(
+ part =>
+ part.type === 'tool-call' &&
+ part.toolCallId === stableId &&
+ (payload?.result === undefined || JSON.stringify(payload.result) === JSON.stringify(part.result))
+ )
+ : -1
+
+ if (repeatedIndex >= 0) {
+ return repeatedIndex
+ }
+
// Some live streams start without an id, then complete with one. Fall
// through to pending same-name/context matching so the completion updates
// the synthetic live row instead of appending a duplicate completed row.
diff --git a/apps/desktop/src/store/tool-diffs.test.ts b/apps/desktop/src/store/tool-diffs.test.ts
deleted file mode 100644
index c4421159ad6..00000000000
--- a/apps/desktop/src/store/tool-diffs.test.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-import { describe, expect, it } from 'vitest'
-
-import { $toolInlineDiff, getToolDiff, recordToolDiff } from './tool-diffs'
-
-describe('tool-diffs per-tool subscriptions', () => {
- it('returns a stable cached atom per toolCallId', () => {
- expect($toolInlineDiff('a')).toBe($toolInlineDiff('a'))
- expect($toolInlineDiff('a')).not.toBe($toolInlineDiff('b'))
- })
-
- it('notifies only the tool whose diff changed', () => {
- const aCalls: string[] = []
- const bCalls: string[] = []
- const unsubA = $toolInlineDiff('notify-a').listen(v => aCalls.push(v))
- const unsubB = $toolInlineDiff('notify-b').listen(v => bCalls.push(v))
-
- recordToolDiff('notify-a', 'diffA')
- expect(aCalls).toEqual(['diffA'])
- expect(bCalls).toEqual([]) // the unrelated tool row is never notified
-
- recordToolDiff('notify-b', 'diffB')
- expect(aCalls).toEqual(['diffA']) // still not re-notified
- expect(bCalls).toEqual(['diffB'])
-
- unsubA()
- unsubB()
- })
-
- it('does not re-notify when the same diff is recorded again', () => {
- const calls: string[] = []
- const unsub = $toolInlineDiff('same').listen(v => calls.push(v))
-
- recordToolDiff('same', 'x')
- recordToolDiff('same', 'x')
-
- expect(calls).toEqual(['x'])
- unsub()
- })
-
- it('reads the current diff for a tool and empty for unknown/blank ids', () => {
- recordToolDiff('read-me', 'value')
- expect(getToolDiff('read-me')).toBe('value')
- expect(getToolDiff('missing')).toBe('')
- expect(getToolDiff('')).toBe('')
- expect($toolInlineDiff('').get()).toBe('')
- })
-})
diff --git a/apps/desktop/src/store/tool-diffs.ts b/apps/desktop/src/store/tool-diffs.ts
deleted file mode 100644
index d4ca7d2ee41..00000000000
--- a/apps/desktop/src/store/tool-diffs.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import { atom, computed, type ReadableAtom } from 'nanostores'
-
-const $toolDiffs = atom<Record<string, string>>({})
-
-// Per-tool derived atoms, cached by toolCallId. A `ToolEntry` subscribes only
-// to its own id's diff, so recording a diff for one tool re-renders that one
-// row -- not every mounted tool row. computed() only notifies when the derived
-// string actually changes, so unrelated writes to the map are inert here.
-const inlineDiffCache = new Map<string, ReadableAtom<string>>()
-
-export function recordToolDiff(toolCallId: string, diff: string) {
- if (!toolCallId || !diff) {
- return
- }
-
- const current = $toolDiffs.get()
-
- if (current[toolCallId] === diff) {
- return
- }
-
- $toolDiffs.set({ ...current, [toolCallId]: diff })
-}
-
-export function getToolDiff(toolCallId: string): string {
- return toolCallId ? $toolDiffs.get()[toolCallId] || '' : ''
-}
-
-export function $toolInlineDiff(toolCallId: string): ReadableAtom<string> {
- let cached = inlineDiffCache.get(toolCallId)
-
- if (!cached) {
- cached = computed($toolDiffs, diffs => (toolCallId ? diffs[toolCallId] || '' : ''))
- inlineDiffCache.set(toolCallId, cached)
- }
-
- return cached
-}