fix(desktop): recover image/file attach and /compress after a stale session drop

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 <xxxigm@users.noreply.github.com>
Co-authored-by: zzz163519 <zzz163519@users.noreply.github.com>
Co-authored-by: luxles <luxles@users.noreply.github.com>
This commit is contained in:
bb
2026-08-07 13:21:51 -05:00
parent fab99e828a
commit 3c4f5c5217
3 changed files with 51 additions and 11 deletions
@@ -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]
)
@@ -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(
@@ -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<SessionCompressResponse>(
'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<SessionCompressResponse>(
'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