Skip to content

The agent that grows with you

fix(desktop): latch a resume that paints nothing when the list row is stale

Austin Pickettf454288d78e5
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
index 67ab9bd2275..f12f251593a 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
@@ -1898,6 +1898,85 @@ describe('resumeSession failure recovery', () => {
expect($activeSessionId.get()).toBe('runtime-1')
expect($messages.get().length).toBe(1)
})
+
+ it('arms the failure latch when a stale list row hides history the resume RPC still reports', async () => {
+ // #83154: a stale/empty sessions-list row (a compressed tip, or a list that
+ // has not refreshed since a backend respawn) can carry message_count 0 while
+ // the stored transcript is intact. Conditioning the latch on that row alone
+ // left the window on an empty thread with an ACTIVE runtime and no latch:
+ // no retry, no error, just a silently blank chat. The resume RPC is
+ // authoritative about the stored size even when it omits the transcript.
+ setSessions([storedSession({ message_count: 0 })])
+
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ if (method === 'session.resume') {
+ return {
+ info: {},
+ message_count: 12,
+ messages: [],
+ messages_omitted: true,
+ resumed: params?.session_id,
+ session_id: 'runtime-1'
+ } as never
+ }
+
+ return {} as never
+ })
+
+ // The REST page is transiently empty too (a respawn racing its state.db read).
+ vi.mocked(getLatestSessionMessages).mockResolvedValue({ messages: [], session_id: 'stored-1' } as never)
+
+ await runResume(requestGateway)
+
+ expect($resumeFailedSessionId.get()).toBe('stored-1')
+ expect($activeSessionId.get()).toBeNull()
+ expect($messages.get()).toEqual([])
+ })
+
+ it('arms the failure latch when a wake warm-resume bail falls to an empty cold resume', async () => {
+ // #82806: on sleep/wake the cached runtime id can be stale, so the warm path
+ // binds it, `session.activate` 404s (the backend respawned), the mapping is
+ // dropped, and the resume falls through to the cold path. If that cold
+ // resume paints nothing — REST and the omitted-messages resume both empty
+ // while the list row has not refreshed — the thread was left silently
+ // blank. It must arm the retry latch instead.
+ const runtimeIdByStoredSessionIdRef: MutableRefObject<Map<string, string>> = {
+ current: new Map([['stored-1', 'rt-stale']])
+ }
+
+ const sessionStateByRuntimeIdRef: MutableRefObject<Map<string, ClientSessionState>> = {
+ current: new Map([['rt-stale', clientState('stored-1')]])
+ }
+
+ // The list row has not caught up with the respawned backend yet.
+ setSessions([storedSession({ message_count: 0 })])
+
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ if (method === 'session.activate') {
+ throw new Error('404: Session not found')
+ }
+
+ if (method === 'session.resume') {
+ return {
+ info: {},
+ message_count: 9,
+ messages: [],
+ messages_omitted: true,
+ resumed: params?.session_id,
+ session_id: 'runtime-1'
+ } as never
+ }
+
+ return {} as never
+ })
+
+ vi.mocked(getLatestSessionMessages).mockResolvedValue({ messages: [], session_id: 'stored-1' } as never)
+
+ await runResume(requestGateway, { runtimeIdByStoredSessionIdRef, sessionStateByRuntimeIdRef })
+
+ expect($resumeFailedSessionId.get()).toBe('stored-1')
+ expect($activeSessionId.get()).toBeNull()
+ })
})
describe('session.resume turn timer contract', () => {
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 7dc9df3538b..9dca0a39922 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
@@ -2087,11 +2087,28 @@ export function useSessionActions({
// must not mask a lost transcript (a retry that reloads real history
// is safer than surfacing the in-flight turn alone). Recovery only
// ever appends, so this matches the final transcript's emptiness.
- if (sessionShouldHaveTranscript(stored) && preferredMessages.length === 0) {
+ //
+ // "Should have a transcript" is not the cached sessions-list row alone.
+ // That row is a cache of backend truth and lags the two flows that
+ // report a vanished thread: after a wake/reconnect the list can still
+ // carry the respawned backend's session at message_count 0, and a
+ // compression tip can show 0 rows while the stored transcript is
+ // intact. Conditioning the latch on it alone paints a blank thread
+ // UNLATCHED — no retry, no error, just an empty chat that looks like
+ // lost history. The resume RPC is authoritative and always reports the
+ // stored size (`message_count`, filled from state.db even when
+ // `messages_omitted`), so treat it — and a non-empty REST page — as the
+ // other rungs of the same ladder.
+ const saidToHaveTranscript =
+ sessionShouldHaveTranscript(stored) ||
+ (resumed.message_count || 0) > 0 ||
+ Boolean(prefetchedResult?.messages.length)
+
+ if (saidToHaveTranscript && preferredMessages.length === 0) {
// Roll back a provisional cached-tail paint and drop its entry: the
- // authoritative sources say this session has no transcript, so the
- // cache no longer reflects backend truth and must not survive to
- // mislead the retry (or the next wake).
+ // latched attempt painted no history from any source, so the
+ // display-only cache must not survive to mask the retry (or the next
+ // wake) as a transcript that loaded.
if (cachedTailPaint !== null && $messages.get() === cachedTailPaint) {
setMessages([])
dropTranscriptTail(storedSessionId, sessionRestScope)