Skip to content

The agent that grows with you

fix(desktop): a start_chat Retry stays on its own chat's card

alt-glitch1eac7a1bce44
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 d82968f8c01..8b7f8e84ce0 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
@@ -45,8 +45,10 @@ function reportStartChatHandoff(payload: GatewayEventPayload | undefined, sessio
return
}
- if (isStartChatCallerWatched(storedSessionIdForRuntimeId(sessionId) ?? sessionId)) {
- markLiveStartChat(payload.tool_id || payload.tool_call_id || payload.id || '')
+ const callerId = storedSessionIdForRuntimeId(sessionId) ?? sessionId
+
+ if (isStartChatCallerWatched(callerId)) {
+ markLiveStartChat(callerId, payload.tool_id || payload.tool_call_id || payload.id || '')
}
// From the setup chat, the handoff completes the guided first run (no-op elsewhere).
diff --git a/apps/desktop/src/components/assistant-ui/start-chat-tool.tsx b/apps/desktop/src/components/assistant-ui/start-chat-tool.tsx
index bbcfaea1a79..cc777b73eee 100644
--- a/apps/desktop/src/components/assistant-ui/start-chat-tool.tsx
+++ b/apps/desktop/src/components/assistant-ui/start-chat-tool.tsx
@@ -23,11 +23,18 @@ import {
isStartChatCallerWatched,
retryStartChat,
startChatOutcome,
+ startChatRetry,
startChatSuperseded,
takeLiveStartChat
} from '@/store/start-chat'
-import { requestedTitle, retryRequest, StartChatRejected, StartChatStarted, StartChatStarting } from './start-chat-tool-parts'
+import {
+ requestedTitle,
+ retryRequest,
+ StartChatRejected,
+ StartChatStarted,
+ StartChatStarting
+} from './start-chat-tool-parts'
async function openStartedChat(
chat: { profile: string; sessionId: string },
@@ -55,7 +62,9 @@ export function StartChatTool(props: ToolCallMessagePartProps & Pick<TimelinePar
const callerBusy = useStore(view.$busy)
const profiles = useStore($profiles)
const sessions = useStore($sessions)
- const retry = useStore($startChatRetries)[props.toolCallId]
+ // Same fallback as the live-start mark in gateway-event/tools.ts, so both sides build the same key.
+ const callerKey = callerId ?? callerRuntimeId ?? ''
+ const retry = startChatRetry(useStore($startChatRetries), callerKey, props.toolCallId)
const navigate = useNavigate()
const { result, toolResultMetadata } = props
@@ -66,7 +75,7 @@ export function StartChatTool(props: ToolCallMessagePartProps & Pick<TimelinePar
const superseded = useStoresSelector(
[view.$messages, $startChatRetries],
- () => outcome?.status === 'rejected' && startChatSuperseded(view.$messages.get(), props.toolCallId)
+ () => outcome?.status === 'rejected' && startChatSuperseded(view.$messages.get(), callerKey, props.toolCallId)
)
const started = outcome?.status === 'started' ? outcome : null
@@ -79,7 +88,7 @@ export function StartChatTool(props: ToolCallMessagePartProps & Pick<TimelinePar
return
}
- void retryStartChat(props.toolCallId, callerRuntimeId, retryRequest(args)).then(
+ void retryStartChat(callerKey, props.toolCallId, callerRuntimeId, retryRequest(args)).then(
next => {
if (next?.status === 'started') {
void openStartedChat(next, callerId, navigate).catch(error => notifyError(error, copy.openFailed))
@@ -90,14 +99,14 @@ export function StartChatTool(props: ToolCallMessagePartProps & Pick<TimelinePar
}
useEffect(() => {
- if (!started || !takeLiveStartChat(props.toolCallId)) {
+ if (!started || !takeLiveStartChat(callerKey, props.toolCallId)) {
return
}
const watching = () => Boolean(callerId) && isStartChatCallerWatched(callerId!)
void openStartedChat(started, callerId, navigate, watching).catch(error => notifyError(error, copy.openFailed))
- }, [callerId, copy.openFailed, navigate, props.toolCallId, started])
+ }, [callerId, callerKey, copy.openFailed, navigate, props.toolCallId, started])
if (props.result !== undefined && !outcome) {
return <ToolFallback {...props} />
diff --git a/apps/desktop/src/store/start-chat.test.ts b/apps/desktop/src/store/start-chat.test.ts
new file mode 100644
index 00000000000..8ab70416f23
--- /dev/null
+++ b/apps/desktop/src/store/start-chat.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from 'vitest'
+
+import { $gateway } from '@/store/gateway'
+
+import { $startChatRetries, markLiveStartChat, retryStartChat, startChatRetry, takeLiveStartChat } from './start-chat'
+
+describe('start_chat per-call state', () => {
+ it("keeps one chat's retry off another chat's call with the same tool call id", async () => {
+ const started = { profile: 'default', session_id: 'child-a', status: 'started', title: 'Task A' }
+
+ // SAFETY: retryStartChat only calls gateway.request, which this stub provides.
+ $gateway.set({ request: async () => started } as never)
+
+ await retryStartChat('chat-a', 'call_0', 'runtime-a', { message: 'do A' })
+
+ expect(startChatRetry($startChatRetries.get(), 'chat-a', 'call_0')).toMatchObject({ sessionId: 'child-a' })
+ expect(startChatRetry($startChatRetries.get(), 'chat-b', 'call_0')).toBeUndefined()
+ })
+
+ it("does not auto-open another chat's call that reused the same tool call id", () => {
+ markLiveStartChat('chat-a', 'call_0')
+
+ expect(takeLiveStartChat('chat-b', 'call_0')).toBe(false)
+ expect(takeLiveStartChat('chat-a', 'call_0')).toBe(true)
+ })
+})
diff --git a/apps/desktop/src/store/start-chat.ts b/apps/desktop/src/store/start-chat.ts
index 555191219e6..f342f56f1e6 100644
--- a/apps/desktop/src/store/start-chat.ts
+++ b/apps/desktop/src/store/start-chat.ts
@@ -34,14 +34,19 @@ export function readStartChatResult(result: unknown): null | StartChatOutcome {
return null
}
+// Tool call ids are unique only within one session (providers reuse ids like `call_0`), so per-call state is keyed by the calling session too.
+function callKey(callerId: string, toolCallId: string): string {
+ return `${callerId}\u0000${toolCallId}`
+}
+
const liveStarts = new Set<string>()
-export function markLiveStartChat(toolCallId: string): void {
- liveStarts.add(toolCallId)
+export function markLiveStartChat(callerId: string, toolCallId: string): void {
+ liveStarts.add(callKey(callerId, toolCallId))
}
-export function takeLiveStartChat(toolCallId: string): boolean {
- return liveStarts.delete(toolCallId)
+export function takeLiveStartChat(callerId: string, toolCallId: string): boolean {
+ return liveStarts.delete(callKey(callerId, toolCallId))
}
export function isStartChatCallerWatched(storedId: string): boolean {
@@ -50,13 +55,21 @@ export function isStartChatCallerWatched(storedId: string): boolean {
export const $startChatRetries = atom<Record<string, 'pending' | StartChatOutcome>>({})
-function setRetry(toolCallId: string, value: 'pending' | null | StartChatOutcome): void {
- const { [toolCallId]: _previous, ...rest } = $startChatRetries.get()
+type StartChatRetry = 'pending' | StartChatOutcome | undefined
- $startChatRetries.set(value ? { ...rest, [toolCallId]: value } : rest)
+export function startChatRetry(
+ retries: Record<string, 'pending' | StartChatOutcome>,
+ callerId: string,
+ toolCallId: string
+): StartChatRetry {
+ return retries[callKey(callerId, toolCallId)]
}
-type StartChatRetry = 'pending' | StartChatOutcome | undefined
+function setRetry(key: string, value: 'pending' | null | StartChatOutcome): void {
+ const { [key]: _previous, ...rest } = $startChatRetries.get()
+
+ $startChatRetries.set(value ? { ...rest, [key]: value } : rest)
+}
/** The card's Retry (live in this window, else recorded on the saved tool row) wins over the call's own result. */
export function startChatOutcome(
@@ -71,7 +84,7 @@ export function startChatOutcome(
}
/** A later start_chat in this chat already started: a Retry here could only start the task twice. */
-export function startChatSuperseded(messages: ChatMessage[], toolCallId: string): boolean {
+export function startChatSuperseded(messages: ChatMessage[], callerId: string, toolCallId: string): boolean {
const retries = $startChatRetries.get()
let after = false
@@ -83,7 +96,8 @@ export function startChatSuperseded(messages: ChatMessage[], toolCallId: string)
if (
after &&
- startChatOutcome(part, part.toolCallId ? retries[part.toolCallId] : undefined)?.status === 'started'
+ startChatOutcome(part, part.toolCallId ? startChatRetry(retries, callerId, part.toolCallId) : undefined)
+ ?.status === 'started'
) {
return true
}
@@ -96,6 +110,7 @@ export function startChatSuperseded(messages: ChatMessage[], toolCallId: string)
}
export async function retryStartChat(
+ callerId: string,
toolCallId: string,
callerRuntimeId: string,
args: StartChatArgs
@@ -106,7 +121,9 @@ export async function retryStartChat(
throw new Error('Gateway not connected')
}
- setRetry(toolCallId, 'pending')
+ const key = callKey(callerId, toolCallId)
+
+ setRetry(key, 'pending')
try {
const outcome = readStartChatResult(
@@ -118,11 +135,11 @@ export async function retryStartChat(
)
)
- setRetry(toolCallId, outcome)
+ setRetry(key, outcome)
return outcome
} catch (error) {
- setRetry(toolCallId, null)
+ setRetry(key, null)
throw error
}