Skip to content

The agent that grows with you

fix(desktop): don't rebind a deleted chat from a leftover 4001 resume

xxxigm990879d6886c
diff --git a/apps/desktop/src/app/session/hooks/use-route-resume.ts b/apps/desktop/src/app/session/hooks/use-route-resume.ts
index 631f10e0072..691a89787e8 100644
--- a/apps/desktop/src/app/session/hooks/use-route-resume.ts
+++ b/apps/desktop/src/app/session/hooks/use-route-resume.ts
@@ -132,7 +132,13 @@ export function useRouteResume({
Boolean(cachedRuntime) &&
cachedRuntime === activeSessionIdRef.current
+ // A leftover 4001 recovery (`requestSessionResume`) must not rebind a
+ // chat the user just deleted: startFreshSessionDraft nulls selection one
+ // render before the pathname flips /:sid -> /new. Honoring that queued
+ // resume re-selects the doomed id, the RPC 404s, and Desktop toasts
+ // "Resume failed / Session not found".
const explicitlyRequested =
+ !freshDraftReady &&
sessionResumeRequest?.sessionId === routedSessionId &&
sessionResumeRequest.sequence > handledResumeRequestRef.current
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index a1224f0aa28..2204f911549 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -51,6 +51,8 @@ import {
} from '@/store/profile'
import {
$projectScope,
+ $removedSessionIds,
+ $sessionMutationsInFlight,
beginSessionMutation,
endSessionMutation,
resolveNewSessionCwd,
@@ -858,6 +860,13 @@ export function useSessionActions({
const resumeSession = useCallback(
async (storedSessionId: string, replaceRoute = false, capturedOwner?: SessionProfileRoute) => {
+ // Delete/archive tombstones the durable id before the route flips.
+ // A leftover 4001 rebind must not re-select that id and toast
+ // "Resume failed / Session not found".
+ if ($removedSessionIds.get().has(storedSessionId) || $sessionMutationsInFlight.get().has(storedSessionId)) {
+ return
+ }
+
const requestId = resumeRequestRef.current + 1
resumeRequestRef.current = requestId
const resumedSameSelectedSession = selectedStoredSessionIdRef.current === storedSessionId