fix(desktop): recover session after sleep/wake gateway restart - #40776
Closed
bpasquini wants to merge 1 commit into
Closed
fix(desktop): recover session after sleep/wake gateway restart#40776bpasquini wants to merge 1 commit into
bpasquini wants to merge 1 commit into
Conversation
When the laptop sleeps and wakes, the WebSocket reconnects but the
gateway's in-memory session table is cleared. The desktop app still
holds the old activeSessionId, so the next prompt.submit call returns
error 4001 ('session not found'), surfaced to the user as:
'Prompt failed: session not found'
Fix: wrap prompt.submit in a try/catch. On 'session not found', call
session.resume with the durable SQLite session ID (selectedStoredSessionIdRef)
to re-register the session in the gateway, update activeSessionIdRef to
the fresh live session_id, then retry prompt.submit once.
If recovery fails or the error is unrelated, the original error is
re-thrown and surfaces normally.
OutThisLife
enabled auto-merge (squash)
June 8, 2026 01:55
Contributor
|
Merged via #42688. Your fix commit was cherry-picked onto current main with your authorship preserved in git log (72f522d) — it had to be re-merged under the attachment-sync text rewrite that landed since you opened this, and we added a vitest covering the recovery path on top. Thanks for the clean diagnosis and one-shot retry design. |
auto-merge was automatically disabled
June 9, 2026 10:17
Pull request was closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a laptop running the Hermes desktop app sleeps and wakes, the TUI gateway's WebSocket reconnects but the in-memory session table (
_sessionsdict intui_gateway/server.py) is cleared. The desktop app still holds the oldactiveSessionId, so the next message send callsprompt.submitwith a stale session ID. The gateway returns error code 4001 (session not found), which surfaces to the user as an inline error bubble:The only workaround is to manually start a new chat or relaunch the app.
Root Cause
requestGatewayinuse-gateway-request.tsretries on transport-level errors ("not connected", "connection closed") but not on gateway-level errors like "session not found". So after a sleep/wake reconnect, the first prompt always fails.Fix
Wrap
prompt.submitin a try/catch insideusePromptActions. On a "session not found" error:session.resumewithselectedStoredSessionIdRef.current— the durable SQLite session ID that survives gateway restartsactiveSessionIdRef.currentto the fresh livesession_idreturned by the gatewayprompt.submitonce with the recovered IDIf recovery fails or the error is something else, the original error is re-thrown and surfaces normally. This is a one-shot retry (no infinite loop risk).
Testing
Repro steps:
Before: "Prompt failed: session not found" error bubble appears, message not sent
After: Message sends transparently on the first attempt after wake
Files Changed
apps/desktop/src/app/session/hooks/use-prompt-actions.ts— added session recovery block aroundprompt.submit(~32 lines)