Skip to content

The agent that grows with you

fix(tui): track the durable session id in ui state instead of on the replaceable info object

teknium15a4c3b32d0cb
diff --git a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
index 6cba2e85041..861c2496300 100644
--- a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
+++ b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
@@ -106,6 +106,29 @@ describe('createGatewayEventHandler', () => {
expect(getTurnState().tools).toEqual([])
})
+ it('keeps the durable session id when a session.info payload omits it', () => {
+ patchUiState({ sid: 'focused', storedSid: 'durable-1' })
+ const onEvent = createGatewayEventHandler(buildCtx([]))
+
+ // Agent-less producers (_fallback_session_info, lazy cwd switch) send no stored_session_id.
+ onEvent({
+ session_id: 'focused',
+ payload: { cwd: '/tmp/a', model: 'test', skills: {}, tools: {} },
+ type: 'session.info'
+ } as any)
+ expect(getUiState().storedSid).toBe('durable-1')
+ expect(getUiState().info?.stored_session_id).toBe('durable-1')
+
+ // A payload that carries one is authoritative.
+ onEvent({
+ session_id: 'focused',
+ payload: { model: 'test', skills: {}, stored_session_id: 'durable-2', tools: {} },
+ type: 'session.info'
+ } as any)
+ expect(getUiState().storedSid).toBe('durable-2')
+ expect(getUiState().info?.stored_session_id).toBe('durable-2')
+ })
+
it('archives incomplete todos into transcript flow at end of turn so they scroll up', () => {
const appended: Msg[] = []
diff --git a/ui-tui/src/__tests__/useSessionLifecycleStoredSid.test.ts b/ui-tui/src/__tests__/useSessionLifecycleStoredSid.test.ts
new file mode 100644
index 00000000000..ffcb928da8a
--- /dev/null
+++ b/ui-tui/src/__tests__/useSessionLifecycleStoredSid.test.ts
@@ -0,0 +1,79 @@
+import { PassThrough } from 'stream'
+
+import { renderSync } from '@hermes/ink'
+import React, { useEffect } from 'react'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { turnController } from '../app/turnController.js'
+import { resetTurnState } from '../app/turnStore.js'
+import { getUiState, resetUiState } from '../app/uiStore.js'
+import { useSessionLifecycle } from '../app/useSessionLifecycle.js'
+
+/** Mount the real hook and hand its API to the test once the first commit is done. */
+function mountLifecycle(request: (method: string, params: unknown) => Promise<unknown>) {
+ let api: null | ReturnType<typeof useSessionLifecycle> = null
+
+ function Probe() {
+ const lifecycle = useSessionLifecycle({
+ colsRef: { current: 80 },
+ composerActions: { setComposerTokens: vi.fn() } as any,
+ gw: { request } as any,
+ panel: vi.fn(),
+ rpc: vi.fn(async () => null),
+ scrollRef: { current: null },
+ setHistoryItems: vi.fn(),
+ setLastUserMsg: vi.fn(),
+ setSessionStartedAt: vi.fn(),
+ setStickyPrompt: vi.fn(),
+ setVoiceProcessing: vi.fn(),
+ setVoiceRecording: vi.fn(),
+ sys: vi.fn()
+ })
+
+ useEffect(() => {
+ api = lifecycle
+ })
+
+ return null
+ }
+
+ const stream = () => Object.assign(new PassThrough(), { columns: 80, isTTY: false, rows: 24 })
+
+ renderSync(React.createElement(Probe), {
+ patchConsole: false,
+ stderr: stream() as unknown as NodeJS.WriteStream,
+ stdin: stream() as unknown as NodeJS.ReadStream,
+ stdout: stream() as unknown as NodeJS.WriteStream
+ })
+
+ return () => api!
+}
+
+describe('useSessionLifecycle durable session id', () => {
+ beforeEach(() => {
+ resetUiState()
+ resetTurnState()
+ turnController.fullReset()
+ })
+
+ it('activating an agent-less session records its session_key as the recovery target', async () => {
+ const request = vi.fn(async () => ({
+ // _fallback_session_info shape: no stored_session_id on the info object.
+ info: { cwd: '/tmp/w', lazy: true, model: 'test', skills: {}, tools: {} },
+ messages: [],
+ running: false,
+ session_id: 'runtime-42',
+ session_key: 'durable-key-123',
+ status: 'idle'
+ }))
+
+ const api = mountLifecycle(request)
+
+ await vi.waitFor(() => expect(api()).toBeTruthy())
+ api().activateLiveSession('durable-key-123')
+
+ await vi.waitFor(() => expect(getUiState().sid).toBe('runtime-42'))
+ expect(request).toHaveBeenCalledWith('session.activate', { session_id: 'durable-key-123' })
+ expect(getUiState().storedSid).toBe('durable-key-123')
+ })
+})
diff --git a/ui-tui/src/app/createGatewayEventHandler.ts b/ui-tui/src/app/createGatewayEventHandler.ts
index 882beedd777..25519ada703 100644
--- a/ui-tui/src/app/createGatewayEventHandler.ts
+++ b/ui-tui/src/app/createGatewayEventHandler.ts
@@ -800,7 +800,7 @@ export function createGatewayEventHandler(ctx: GatewayEventHandlerContext): (ev:
return
case 'session.info': {
- const info = ev.payload as SessionInfo | undefined
+ let info = ev.payload as SessionInfo | undefined
if (!info) {
return
@@ -814,10 +814,20 @@ export function createGatewayEventHandler(ctx: GatewayEventHandlerContext): (ev:
setStatus('ready')
}
+ // Agent-less producers (lazy cwd switches, `_fallback_session_info`) send
+ // payloads without a durable id — keep the one we already track so a
+ // later reconnect still resumes this session.
+ const storedSid = info.stored_session_id || getUiState().storedSid
+
+ if (storedSid) {
+ info = { ...info, stored_session_id: storedSid }
+ }
+
patchUiState(state => ({
...state,
info,
status: state.status === 'starting agent…' ? 'ready' : state.status,
+ storedSid,
usage: info.usage ? mergeUsageStable(state.usage, info.usage) : state.usage
}))
diff --git a/ui-tui/src/app/interfaces.ts b/ui-tui/src/app/interfaces.ts
index ad586815331..bd14925065f 100644
--- a/ui-tui/src/app/interfaces.ts
+++ b/ui-tui/src/app/interfaces.ts
@@ -348,6 +348,10 @@ export interface UiState {
sid: null | string
status: string
statusBar: StatusBarMode
+ // Durable session id (state.db row) of the live session — what session.resume
+ // and the exit epilogue take. Kept apart from `info`, which producers replace
+ // wholesale with payloads that may omit `stored_session_id`.
+ storedSid: null | string
// display.status_bar.fields — visibility filter for status-rule segments,
// shared with the classic CLI bar. null = user has not customized (show
// the default set).
diff --git a/ui-tui/src/app/uiStore.ts b/ui-tui/src/app/uiStore.ts
index 415cf991dcb..46a0f7e1a0c 100644
--- a/ui-tui/src/app/uiStore.ts
+++ b/ui-tui/src/app/uiStore.ts
@@ -33,6 +33,7 @@ const buildUiState = (): UiState => ({
sid: null,
status: 'summoning hermes…',
statusBar: 'top',
+ storedSid: null,
statusBarFields: null,
streaming: true,
timestamps: false,
diff --git a/ui-tui/src/app/useMainApp.ts b/ui-tui/src/app/useMainApp.ts
index 0c9b196ec72..b3c6600dcbe 100644
--- a/ui-tui/src/app/useMainApp.ts
+++ b/ui-tui/src/app/useMainApp.ts
@@ -965,7 +965,7 @@ export function useMainApp(gw: GatewayClient) {
const exitHandler = (code: null | number) => {
turnController.reset()
const state = getUiState()
- const storedSid = state.info?.stored_session_id || null
+ const storedSid = state.storedSid
// Attached socket closed: the backend (and any live turn) is still there —
// GatewayClient owns the backoff reconnect, and the next gateway.ready
diff --git a/ui-tui/src/app/useSessionLifecycle.ts b/ui-tui/src/app/useSessionLifecycle.ts
index 5f05761c8ac..ca767736c49 100644
--- a/ui-tui/src/app/useSessionLifecycle.ts
+++ b/ui-tui/src/app/useSessionLifecycle.ts
@@ -148,7 +148,7 @@ export function useSessionLifecycle(opts: UseSessionLifecycleOptions) {
turnController.fullReset()
setVoiceRecording(false)
setVoiceProcessing(false)
- patchUiState({ bgTasks: new Set(), info: null, sid: null, usage: ZERO })
+ patchUiState({ bgTasks: new Set(), info: null, sid: null, storedSid: null, usage: ZERO })
setHistoryItems([])
setLastUserMsg('')
setStickyPrompt('')
@@ -222,6 +222,7 @@ export function useSessionLifecycle(opts: UseSessionLifecycleOptions) {
info,
sid: r.session_id,
status: info?.version ? 'ready' : 'starting agent…',
+ storedSid,
usage: usageFrom(info)
})
@@ -303,18 +304,22 @@ export function useSessionLifecycle(opts: UseSessionLifecycleOptions) {
}
const info = r.info ?? null
+ // Agent-less (lazy) activations answer with `_fallback_session_info`, which
+ // has no stored_session_id; the durable id is the response's session_key.
+ const storedSid = r.session_key || r.session_id
const running = Boolean(r.running || r.status === 'working' || r.status === 'waiting')
resetSession()
setSessionStartedAt(r.started_at ? r.started_at * 1000 : Date.now())
const transcript = [...toTranscriptMessages(r.messages), ...liveSessionInflightMessages(r.inflight)]
setHistoryItems(info ? [introMsg(info), ...transcript] : transcript)
- writeActiveSessionFile(r.session_key ?? r.session_id)
+ writeActiveSessionFile(storedSid)
patchUiState({
busy: running,
info,
sid: r.session_id,
status: statusFromLiveSession(r.status, running),
+ storedSid,
usage: usageFrom(info)
})
hydrateLiveSessionInflight(r.inflight)
@@ -354,9 +359,8 @@ export function useSessionLifecycle(opts: UseSessionLifecycleOptions) {
return patchUiState({ status: 'ready' })
}
- const info = r.info
- ? { ...r.info, stored_session_id: r.info.stored_session_id || r.stored_session_id || r.resumed || id }
- : null
+ const storedSid = r.info?.stored_session_id || r.stored_session_id || r.resumed || id
+ const info = r.info ? { ...r.info, stored_session_id: storedSid } : null
const running = Boolean(r.running || r.status === 'working' || r.status === 'waiting')
@@ -366,12 +370,13 @@ export function useSessionLifecycle(opts: UseSessionLifecycleOptions) {
const resumed = [...toTranscriptMessages(r.messages), ...liveSessionInflightMessages(r.inflight)]
setHistoryItems(info ? [introMsg(info), ...resumed] : resumed)
- writeActiveSessionFile(info?.stored_session_id || r.resumed || id)
+ writeActiveSessionFile(storedSid)
patchUiState({
busy: running,
info,
sid: r.session_id,
status: statusFromLiveSession(r.status ?? undefined, running),
+ storedSid,
usage: usageFrom(info)
})
hydrateLiveSessionInflight(r.inflight)