Skip to content

The agent that grows with you

fix(desktop): recover routed submits after reconnect

Shawn Wang534719259cac
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index abb2c10c24e..e52defbc788 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -4652,6 +4652,62 @@ describe('usePromptActions busy-gateway churn tolerance (#64327)', () => {
})
})
+ it('submits once through authoritative recovery when routed resume publication lags (#90428)', async () => {
+ const staleStoredId = 'stored-previous-selection'
+ const staleRuntimeId = 'rt-previous-selection'
+ const calls: { method: string; params?: Record<string, unknown> }[] = []
+ const selectedStoredSessionIdRef: MutableRefObject<string | null> = { current: staleStoredId }
+ const activeSessionIdRef: MutableRefObject<string | null> = { current: staleRuntimeId }
+
+ const runtimeIdByStoredSessionIdRef: MutableRefObject<Map<string, string>> = {
+ current: new Map([[staleStoredId, staleRuntimeId]])
+ }
+
+ const resumeStoredSession = vi.fn(async () => undefined)
+
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ calls.push({ method, params })
+
+ if (method === 'session.resume') {
+ return { session_id: RESUMED_RUNTIME_ID } as never
+ }
+
+ return {} as never
+ })
+
+ let handle: HarnessHandle | null = null
+ render(
+ <Harness
+ activeSessionId={staleRuntimeId}
+ activeSessionIdRef={activeSessionIdRef}
+ getRoutedStoredSessionId={() => STORED_ID}
+ getRouteToken={() => `/${STORED_ID}::`}
+ getRuntimeIdForStoredSession={storedId => runtimeIdByStoredSessionIdRef.current.get(storedId) ?? null}
+ onReady={h => (handle = h)}
+ refreshSessions={async () => undefined}
+ requestGateway={requestGateway}
+ resumeStoredSession={resumeStoredSession}
+ runtimeIdByStoredSessionIdRef={runtimeIdByStoredSessionIdRef}
+ selectedStoredSessionIdRef={selectedStoredSessionIdRef}
+ storedSessionId={staleStoredId}
+ />
+ )
+ await waitFor(() => expect(handle).not.toBeNull())
+
+ expect(await handle!.submitText('deliver despite lagging local publication')).toBe(true)
+ expect(resumeStoredSession).toHaveBeenCalledOnce()
+ expect(calls).toEqual([
+ {
+ method: 'session.resume',
+ params: { session_id: STORED_ID, source: 'desktop', omit_messages: true }
+ },
+ {
+ method: 'prompt.submit',
+ params: { session_id: RESUMED_RUNTIME_ID, text: 'deliver despite lagging local publication' }
+ }
+ ])
+ })
+
it('still aborts when the user genuinely moves to a different chat mid-submit', async () => {
// The churn tolerance must not weaken the real guard: selection AND route
// moving to another actual chat is a user switch and must abort.
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
index aa48a36cfd1..97b060922fa 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
@@ -206,9 +206,9 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
// Queue drains carry their source session explicitly. A background drain
// must never inherit the currently selected session after the user moves
// to another chat.
- const targetStoredSessionId = options?.storedSessionId ?? selectedStoredSessionIdRef.current
+ let targetStoredSessionId = options?.storedSessionId ?? selectedStoredSessionIdRef.current
- const targetStartedInCurrentView =
+ let targetStartedInCurrentView =
!targetStoredSessionId || targetStoredSessionId === selectedStoredSessionIdRef.current
// A queued/background drain whose runtime binding was reaped must NOT
@@ -275,10 +275,25 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
startingActiveSessionId !== routedRuntimeId)
)
+ // For an ordinary foreground submit, the durable route is the authority
+ // when renderer selection publication is stale after reconnect. Pin the
+ // operation to that route before any recovery; explicit queue/tile targets
+ // remain authoritative and never inherit the foreground route.
+ if (!options?.storedSessionId && routedSessionNeedsResume && routedStoredSessionId) {
+ targetStoredSessionId = routedStoredSessionId
+ targetStartedInCurrentView = true
+ }
+
let startingStoredSessionId = routedSessionNeedsResume
? routedStoredSessionId
: (selectedStoredSessionId ?? routedStoredSessionId)
+ // Selection publishes independently from the durable route. Keep its
+ // entry snapshot as the drift baseline instead of rewriting history to
+ // the routed target: an already-stale ref is not evidence that the user
+ // switched chats while this submit was in flight.
+ let startingSelectedStoredSessionId = selectedStoredSessionId
+
let startingRouteToken = getRouteToken()
// Reason string (or null) for why the session context genuinely drifted
@@ -296,7 +311,7 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
? sessionContextDrift({
startRouteToken: startingRouteToken,
nowRouteToken: getRouteToken(),
- startSelectedStoredId: startingStoredSessionId,
+ startSelectedStoredId: startingSelectedStoredSessionId,
nowSelectedStoredId: selectedStoredSessionIdRef.current,
submitTargetStoredId: startingStoredSessionId,
composerScope: options?.composerScope,
@@ -534,20 +549,20 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
const recoveredRuntimeId = activeSessionIdRef.current
const validatedRuntimeId = getRuntimeIdForStoredSession(routedStoredSessionId)
- // Recovery only succeeded when both sides of the cache agree that the
- // live runtime belongs to the durable routed session. A failed profile
- // swap may leave the previous profile's runtime active, while a recycled
- // runtime id may leave a cross-wired stored-session mapping.
+ // Adopt the high-level resume only after its renderer-side ownership
+ // publications agree. Those refs/caches update independently, so a
+ // successful resume can legitimately return before they settle. That
+ // is not a failed recovery and not evidence of navigation: leave the
+ // runtime unresolved and let the direct, profile-aware session.resume
+ // rung below return an authoritative id for this exact durable target.
if (
- !recoveredRuntimeId ||
- recoveredRuntimeId !== validatedRuntimeId ||
- selectedStoredSessionIdRef.current !== routedStoredSessionId
+ recoveredRuntimeId &&
+ recoveredRuntimeId === validatedRuntimeId &&
+ selectedStoredSessionIdRef.current === routedStoredSessionId
) {
- return abortForSessionSwitch(null)
+ sessionId = recoveredRuntimeId
+ seedOptimistic(sessionId)
}
-
- sessionId = recoveredRuntimeId
- seedOptimistic(sessionId)
}
if (!sessionId && targetStoredSessionId) {
@@ -657,6 +672,7 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
// Re-pin the baseline to the created chat for the rest of the
// pipeline; the closures (seedOptimistic et al) see the new value.
startingStoredSessionId = selectedStoredSessionIdRef.current
+ startingSelectedStoredSessionId = selectedStoredSessionIdRef.current
startingRouteToken = getRouteToken()
seedOptimistic(sessionId)