Skip to content

The agent that grows with you

fix(tui): an offline session stops polling and reports the lost link once

Tekniume95daf51ef54
diff --git a/ui-tui/src/__tests__/inkOfflinePollRedial.test.ts b/ui-tui/src/__tests__/inkOfflinePollRedial.test.ts
new file mode 100644
index 00000000000..8bd77100eca
--- /dev/null
+++ b/ui-tui/src/__tests__/inkOfflinePollRedial.test.ts
@@ -0,0 +1,52 @@
+import { EventEmitter } from 'node:events'
+import { PassThrough } from 'node:stream'
+
+import { renderSync } from '@hermes/ink'
+import React from 'react'
+import { afterEach, expect, it, vi } from 'vitest'
+
+import { getUiState, patchUiState, resetUiState } from '../app/uiStore.js'
+import { useMainApp } from '../app/useMainApp.js'
+import { connectionLost } from '../app/userMessages.js'
+
+// B2-20: Ink keeps `sid` across a socket close (gatewayConnected:false). The 1.5 s refresh poll must
+// stop while offline (each request re-dials through ensureAttachedWebSocket and collapses the
+// reconnect backoff), and every failed redial's `exit` must not append another connectionLost line.
+const mounted: Array<() => void> = []
+afterEach(() => { for (const unmount of mounted.splice(0)) { unmount() } vi.useRealTimers(); resetUiState() })
+
+it('an offline session stops the refresh poll and reports the lost connection once', async () => {
+ vi.useFakeTimers()
+ resetUiState()
+ const gw = Object.assign(new EventEmitter(), {
+ attached: true, isCanonical: true, drain: () => {}, getLogTail: () => '', kill: () => {}, recordLog: () => {},
+ request: vi.fn((_method: string) => new Promise(() => {}))
+ })
+ let app!: ReturnType<typeof useMainApp>
+
+ function Harness() {
+ app = useMainApp(gw as never)
+
+ return null
+ }
+
+ const stream = () => Object.assign(new PassThrough(), { columns: 80, isTTY: false, rows: 24 })
+ // useMainApp's input hooks need raw mode, or Ink swaps the tree for an error panel.
+ const stdin = Object.assign(new PassThrough(), { isTTY: true, ref: () => {}, setRawMode: () => {}, unref: () => {} })
+ const instance = renderSync(React.createElement(Harness), { patchConsole: false, stderr: stream() as never, stdin: stdin as never, stdout: stream() as never })
+ mounted.push(() => { instance.unmount(); instance.cleanup() })
+
+ patchUiState({ sid: 'owner', storedSid: 'owner', gatewayConnected: true })
+ await vi.advanceTimersByTimeAsync(3100)
+ const polls = () => gw.request.mock.calls.filter(([method]) => method === 'subagent.list').length
+ expect(polls()).toBeGreaterThan(1)
+
+ for (let attempt = 0; attempt < 3; attempt++) { gw.emit('exit', 1006) }
+ await vi.advanceTimersByTimeAsync(0)
+ expect(getUiState().gatewayConnected).toBe(false)
+ const offline = polls()
+ await vi.advanceTimersByTimeAsync(6000)
+ expect(polls()).toBe(offline)
+ const lost = app.appTranscript.historyItems.filter(item => item.text === connectionLost())
+ expect(lost).toHaveLength(1)
+})
diff --git a/ui-tui/src/app/useMainApp.ts b/ui-tui/src/app/useMainApp.ts
index 1a69a70b48f..de71ed532b0 100644
--- a/ui-tui/src/app/useMainApp.ts
+++ b/ui-tui/src/app/useMainApp.ts
@@ -667,6 +667,10 @@ export function useMainApp(gw: GatewayClient) {
return
}
+ // Offline (sid kept across a socket close): each poll would re-dial through
+ // ensureAttachedWebSocket and collapse GatewayClient's backoff (B2-20).
+ if (ui.gatewayConnected === false) { return }
+
let stopped = false
applyAgentSnapshot(ui.sid)
applyProcessSnapshot(ui.sid)
@@ -739,7 +743,7 @@ export function useMainApp(gw: GatewayClient) {
stopped = true
clearInterval(timer)
}
- }, [gw, ui.sid])
+ }, [gw, ui.sid, ui.gatewayConnected])
// Tab title: `⚠` waiting on approval/sudo/secret/clarify, `⏳` busy, `✓` idle.
// Format: `<marker> <session name> · <model> · <cwd>` — name/cwd omitted when absent.
@@ -1114,7 +1118,8 @@ export function useMainApp(gw: GatewayClient) {
status: t('session.status.reconnecting')
})
- if (state.sid) {
+ // Once per outage: each failed redial emits another exit (main printed it once).
+ if (state.sid && state.gatewayConnected !== false) {
turnController.pushActivity(connectionLostActivity(), 'warn')
sys(connectionLost())
}