From 3c4f5c5217381ef1c8ade256ecb21b6bbad19d90 Mon Sep 17 00:00:00 2001 From: bb Date: Fri, 7 Aug 2026 13:21:51 -0500 Subject: [PATCH] fix(desktop): recover image/file attach and /compress after a stale session drop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After sleep/wake, a long idle, or a remote backend restart, the gateway drops its in-memory runtime session while the desktop still holds the old id. prompt.submit already recovered, so plain text kept working and the failure looked selective: attaching an image or running /compress died with a bare "session not found" and a new chat was the only workaround. Attach runs BEFORE prompt.submit, so submit's recovery never got a chance — image.attach_bytes / image.attach / file.attach failed first. Route them (and session.compress) through the shared resolver, and thread the recovered id back to the caller so the follow-up submit targets the live session instead of the dead one. Attachment bytes are read once, outside the retry, so recovering a large upload doesn't re-read it. /compress deliberately does NOT opt into timeout recovery: it is legitimately LLM-slow and retrying a timeout would double a minutes-long call. Reported-by: bapemonkey (Discord) Co-authored-by: xxxigm Co-authored-by: zzz163519 Co-authored-by: luxles --- .../src/app/chat/session-tile-actions.ts | 7 ++- .../session/hooks/use-prompt-actions/index.ts | 10 ++++- .../session/hooks/use-prompt-actions/slash.ts | 45 +++++++++++++++---- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/apps/desktop/src/app/chat/session-tile-actions.ts b/apps/desktop/src/app/chat/session-tile-actions.ts index fdaf7126d5..e5435e4ffe 100644 --- a/apps/desktop/src/app/chat/session-tile-actions.ts +++ b/apps/desktop/src/app/chat/session-tile-actions.ts @@ -367,7 +367,12 @@ export function useSessionTileActions({ runtimeId, scope, storedSessionId }: Ses // the primary chat so the two can't diverge. const submitRewind = useCallback( (text: string, truncateOrdinal: number | undefined, interruptFirst: boolean) => - runRewindSubmit(requestGateway, runtimeIdRef.current, text, truncateOrdinal, interruptFirst), + runRewindSubmit(requestGateway, runtimeIdRef.current, text, truncateOrdinal, interruptFirst, { + storedSessionId: storedIdRef.current, + onSessionRecovered: recoveredId => { + runtimeIdRef.current = recoveredId + } + }), [requestGateway] ) diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts index fcfe1d78df..478c868b0b 100644 --- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts +++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts @@ -820,8 +820,14 @@ export function usePromptActions({ // response interrupts + retries through the shared busy gate. const submitRewindPrompt = useCallback( (sessionId: string, text: string, truncateOrdinal: number | undefined, interruptFirst: boolean) => - runRewindSubmit(requestGateway, sessionId, text, truncateOrdinal, interruptFirst), - [requestGateway] + runRewindSubmit(requestGateway, sessionId, text, truncateOrdinal, interruptFirst, { + storedSessionId: selectedStoredSessionIdRef.current, + onSessionRecovered: recoveredId => { + activeSessionIdRef.current = recoveredId + setActiveSessionId(recoveredId) + } + }), + [activeSessionIdRef, requestGateway, selectedStoredSessionIdRef] ) const restoreToMessage = useCallback( diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts index d88d36548f..b4dff0f95d 100644 --- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts +++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts @@ -29,6 +29,7 @@ import { $sessions, $yoloActive, resolveComposerSessionKey, + setActiveSessionId, setCurrentUsage, setModelPickerOpen, setSessionPickerOpen, @@ -62,7 +63,8 @@ import { renderCommandsCatalog, renderRpcResult, slashStatusText, - type SubmitTextOptions + type SubmitTextOptions, + withSessionNotFoundResume } from './utils' // Manual compression is LLM-bound and routinely outlives the desktop's 30s @@ -499,7 +501,8 @@ export function useSlashCommand(deps: SlashCommandDeps) { return } - const { render: renderSlashOutput, sessionId, storedSessionId } = resolved + const { render: renderSlashOutput, sessionId: initialSessionId, storedSessionId } = resolved + let sessionId = initialSessionId const focusTopic = ctx.arg.trim() const noticeId = `session-compress:${sessionId}` @@ -518,15 +521,41 @@ export function useSlashCommand(deps: SlashCommandDeps) { }) try { - const result = await requestGateway( - 'session.compress', + // Same stale-runtime recovery as prompt.submit: after sleep/wake a + // dead id 404s session.compress while plain chat still works, so + // /compress reported "session not found" on a healthy session. + // NOT alsoTimeout — compress is legitimately LLM-slow and a + // timeout here must not be retried as a dead session. + const { result, sessionId: liveSessionId } = await withSessionNotFoundResume( + sessionId, + storedSessionId, + liveId => + requestGateway( + 'session.compress', + { + session_id: liveId, + ...(focusTopic ? { focus_topic: focusTopic } : {}) + }, + SESSION_COMPRESS_TIMEOUT_MS + ), { - session_id: sessionId, - ...(focusTopic ? { focus_topic: focusTopic } : {}) - }, - SESSION_COMPRESS_TIMEOUT_MS + requestGateway, + onRecovered: recoveredId => { + // Move the in-flight claim onto the live id so the coalesce + // guard releases the right key in `finally`. + compressInFlightRef.current.delete(sessionId) + compressInFlightRef.current.add(recoveredId) + + if (activeSessionIdRef.current === initialSessionId) { + activeSessionIdRef.current = recoveredId + setActiveSessionId(recoveredId) + } + } + } ) + sessionId = liveSessionId + // Replace the transcript with the post-compress history so the // summarized bubbles actually disappear. `messages` is the same // shape session.resume returns (_history_to_messages), so