Skip to content

fix(desktop): recover session after sleep/wake gateway restart - #40776

Closed
bpasquini wants to merge 1 commit into
NousResearch:mainfrom
bpasquini:fix/desktop-session-recovery-after-sleep
Closed

fix(desktop): recover session after sleep/wake gateway restart#40776
bpasquini wants to merge 1 commit into
NousResearch:mainfrom
bpasquini:fix/desktop-session-recovery-after-sleep

Conversation

@bpasquini

Copy link
Copy Markdown

Problem

When a laptop running the Hermes desktop app sleeps and wakes, the TUI gateway's WebSocket reconnects but the in-memory session table (_sessions dict in tui_gateway/server.py) is cleared. The desktop app still holds the old activeSessionId, so the next message send calls prompt.submit with a stale session ID. The gateway returns error code 4001 (session not found), which surfaces to the user as an inline error bubble:

Prompt failed: session not found

The only workaround is to manually start a new chat or relaunch the app.

Root Cause

requestGateway in use-gateway-request.ts retries 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.submit in a try/catch inside usePromptActions. On a "session not found" error:

  1. Call session.resume with selectedStoredSessionIdRef.current — the durable SQLite session ID that survives gateway restarts
  2. Update activeSessionIdRef.current to the fresh live session_id returned by the gateway
  3. Retry prompt.submit once with the recovered ID

If 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:

  1. Open Hermes desktop app, start a chat
  2. Close the laptop lid / sleep the machine
  3. Wake it up and wait for the gateway WebSocket to reconnect
  4. Type a message and send

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 around prompt.submit (~32 lines)

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.
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have labels Jun 6, 2026
@OutThisLife
OutThisLife enabled auto-merge (squash) June 8, 2026 01:55
@teknium1

teknium1 commented Jun 9, 2026

Copy link
Copy Markdown
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.

@teknium1 teknium1 closed this Jun 9, 2026
auto-merge was automatically disabled June 9, 2026 10:17

Pull request was closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P3 Low — cosmetic, nice to have type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants