The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(ts): one reconnect backoff in apps/shared; web events feed rides the shared client and survives reconnects
teknium1bab5cece78ab
diff --git a/apps/desktop/src/api/plugins.ts b/apps/desktop/src/api/plugins.tsindex 32d482356aa..d22d5e9d4ec 100644--- a/apps/desktop/src/api/plugins.ts+++ b/apps/desktop/src/api/plugins.ts@@ -1,5 +1,6 @@+import { reconnectBackoffDelayMs } from '@hermes/shared'+import type { HermesConnection } from '@/global'-import { reconnectBackoffDelayMs } from '@/lib/reconnect-backoff'import { RECONNECT_ATTEMPT_TIMEOUT_MS, withTimeout } from '@/lib/with-timeout'import { getApiRequestConnection, getApiRequestProfile, hermesApi, profileScoped } from './client'diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.tsindex 7f1cec97068..4161359f15e 100644--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts@@ -1,4 +1,4 @@-import { type GatewayEvent, isGatewayReauthRequired, isGatewayWebSocketUrl, JsonRpcGatewayError, resolveGatewayWsUrl } from '@hermes/shared'+import { type GatewayEvent, isGatewayReauthRequired, isGatewayWebSocketUrl, JsonRpcGatewayError, reconnectBackoffDelayMs, resolveGatewayWsUrl } from '@hermes/shared'import { useEffect, useRef } from 'react'import { shouldApplyPostBootProgressError } from '@/components/boot-failure-reauth'@@ -7,7 +7,6 @@ import { HermesGateway } from '@/hermes'import { translateNow } from '@/i18n'import { desktopDefaultCwd } from '@/lib/desktop-fs'import { decideLivenessForceClose, LIVENESS_REPROBE_DELAY_MS } from '@/lib/gateway-liveness-policy'-import { reconnectBackoffDelayMs } from '@/lib/reconnect-backoff'import { BACKEND_BOOT_WAIT_TIMEOUT_MS, RECONNECT_ATTEMPT_TIMEOUT_MS, withTimeout } from '@/lib/with-timeout'import {$desktopBoot,diff --git a/apps/desktop/src/lib/reconnect-backoff.ts b/apps/desktop/src/lib/reconnect-backoff.tsdeleted file mode 100644index c24ce9a24fc..00000000000--- a/apps/desktop/src/lib/reconnect-backoff.ts+++ /dev/null@@ -1,45 +0,0 @@-/**- * Full-jitter exponential backoff for gateway WebSocket reconnects.- *- * A bare exponential backoff still lets every renderer in a fleet retry in- * lockstep — after a gateway restart (e.g. following an update), N desktop- * clients that all disconnected within the same instant all wake up and- * redial at the same instant too, which is a reconnect storm by another- * name. Full jitter (AWS's "Exponential Backoff And Jitter") spreads that- * out: each attempt sleeps a *random* duration between 0 and the exponential- * ceiling, so retries desynchronize instead of pulsing together.- *- * https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/- */--export interface ReconnectBackoffOptions {- /** Ceiling on the exponential delay before jitter is applied, in ms. */- capMs?: number- /** Delay for the first retry (attempt 0) before jitter is applied, in ms. */- baseDelayMs?: number-}--const DEFAULT_BASE_DELAY_MS = 300-const DEFAULT_CAP_MS = 15_000--/**- * Delay before reconnect attempt number `attempt` (0-indexed: the first- * retry after the initial failure is `attempt = 0`). Returns a value in- * `[0, min(capMs, baseDelayMs * 2 ** attempt))` — full jitter, not- * "equal jitter" or "decorrelated jitter", so it can occasionally return a- * very small delay even at a high attempt count. That's intentional: it's- * the variant with the best-documented storm-avoidance behavior and no- * accumulated-delay state to track between calls.- */-export function reconnectBackoffDelayMs(attempt: number, options: ReconnectBackoffOptions = {}): number {- const baseDelayMs = options.baseDelayMs ?? DEFAULT_BASE_DELAY_MS- const capMs = options.capMs ?? DEFAULT_CAP_MS- const safeAttempt = Math.max(0, attempt)-- // 2 ** attempt overflows to Infinity long before it matters (attempt would- // need to be ~1024), and Math.min against a finite cap keeps the ceiling- // sane regardless, so no extra clamping is needed here.- const ceiling = Math.min(capMs, baseDelayMs * 2 ** safeAttempt)-- return Math.random() * ceiling-}diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.tsindex d84208b5963..07d835b4532 100644--- a/apps/desktop/src/store/gateway.ts+++ b/apps/desktop/src/store/gateway.ts@@ -1,9 +1,8 @@-import { type ConnectionState, type GatewayEvent, registryBackendScopeKey, resolveGatewayWsUrl } from '@hermes/shared'+import { type ConnectionState, type GatewayEvent, reconnectBackoffDelayMs, registryBackendScopeKey, resolveGatewayWsUrl } from '@hermes/shared'import { atom } from 'nanostores'import type { HermesConnection } from '@/global'import { HermesGateway, setApiRequestConnection } from '@/hermes'-import { reconnectBackoffDelayMs } from '@/lib/reconnect-backoff'import { isTimeoutError, RECONNECT_ATTEMPT_TIMEOUT_MS, withTimeout } from '@/lib/with-timeout'import { markNativeNotifyBaseline } from '@/store/notify-baseline'import { setConnection, setGatewayState } from '@/store/session'diff --git a/apps/shared/src/index.ts b/apps/shared/src/index.tsindex 539a3ebbd94..d88f8b95caa 100644--- a/apps/shared/src/index.ts+++ b/apps/shared/src/index.ts@@ -110,6 +110,7 @@ export {JsonRpcGatewayClient,type WebSocketLike} from './json-rpc-gateway'+export { reconnectBackoffDelayMs, type ReconnectBackoffOptions } from './reconnect-backoff'export { skillInvocationText } from './skill-scaffold'export {type HermesSkin,diff --git a/apps/desktop/src/lib/reconnect-backoff.test.ts b/apps/shared/src/reconnect-backoff.test.tssimilarity index 82%rename from apps/desktop/src/lib/reconnect-backoff.test.tsrename to apps/shared/src/reconnect-backoff.test.tsindex 53e51601f43..8bca2b7f5c2 100644--- a/apps/desktop/src/lib/reconnect-backoff.test.ts+++ b/apps/shared/src/reconnect-backoff.test.ts@@ -1,6 +1,6 @@import { describe, expect, it, vi } from 'vitest'-import { reconnectBackoffDelayMs } from './reconnect-backoff'+import { reconnectBackoffDelayMs } from './reconnect-backoff.js'describe('reconnectBackoffDelayMs', () => {it('increases the delay ceiling across consecutive failed attempts', () => {@@ -89,4 +89,18 @@ describe('reconnectBackoffDelayMs', () => {randomSpy.mockRestore()}})++ it('jitter: false returns the exact ceiling — the ladder web prints in its banner', () => {+ const randomSpy = vi.spyOn(Math, 'random').mockReturnValue(0.1)++ try {+ expect([0, 1, 2, 3].map(a => reconnectBackoffDelayMs(a, { baseDelayMs: 1000, capMs: 30_000, jitter: false }))).toEqual(+ [1000, 2000, 4000, 8000]+ )+ expect(reconnectBackoffDelayMs(99, { baseDelayMs: 1000, capMs: 30_000, jitter: false })).toBe(30_000)+ expect(reconnectBackoffDelayMs(10_000, { jitter: false })).toBeLessThanOrEqual(15_000)+ } finally {+ randomSpy.mockRestore()+ }+ })})diff --git a/apps/shared/src/reconnect-backoff.ts b/apps/shared/src/reconnect-backoff.tsnew file mode 100644index 00000000000..8a1dd756b5e--- /dev/null+++ b/apps/shared/src/reconnect-backoff.ts@@ -0,0 +1,46 @@+/**+ * Exponential reconnect backoff shared by every Hermes front-end socket+ * (desktop gateway/plugins, web events feed, web PTY, Ink TUI attach mode).+ *+ * Default is full jitter: a bare exponential backoff still lets every client+ * in a fleet retry in lockstep — after a gateway restart, N clients that all+ * disconnected within the same instant all wake up and redial at the same+ * instant too, which is a reconnect storm by another name. Full jitter (AWS's+ * "Exponential Backoff And Jitter") spreads that out: each attempt sleeps a+ * *random* duration between 0 and the exponential ceiling.+ *+ * https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/+ *+ * `jitter: false` returns the ceiling itself — the deterministic ladder the+ * web dashboard renders into its "reconnecting in Ns" banner.+ */++export interface ReconnectBackoffOptions {+ /** Delay for the first retry (attempt 0) before jitter is applied, in ms. */+ baseDelayMs?: number+ /** Ceiling on the exponential delay before jitter is applied, in ms. */+ capMs?: number+ /** Full jitter (default) or the bare ceiling. */+ jitter?: boolean+}++const DEFAULT_BASE_DELAY_MS = 300+const DEFAULT_CAP_MS = 15_000+// 2 ** attempt overflows to Infinity long before it matters (attempt would+// need to be ~1024) and Math.min against a finite cap keeps the ceiling sane+// regardless; the clamp just keeps the arithmetic in finite territory.+const MAX_EXPONENT = 32++/**+ * Delay before reconnect attempt number `attempt` (0-indexed: the first retry+ * after the initial failure is `attempt = 0`). With jitter the value lies in+ * `[0, min(capMs, baseDelayMs * 2 ** attempt))`; without it, it IS that ceiling.+ */+export function reconnectBackoffDelayMs(attempt: number, options: ReconnectBackoffOptions = {}): number {+ const baseDelayMs = options.baseDelayMs ?? DEFAULT_BASE_DELAY_MS+ const capMs = options.capMs ?? DEFAULT_CAP_MS+ const exponent = Math.min(Math.max(0, Math.trunc(attempt)), MAX_EXPONENT)+ const ceiling = Math.min(capMs, baseDelayMs * 2 ** exponent)++ return options.jitter === false ? ceiling : Math.random() * ceiling+}diff --git a/web/src/components/ChatSidebar.test.tsx b/web/src/components/ChatSidebar.test.tsxindex 74042c55e9b..ed4b487751a 100644--- a/web/src/components/ChatSidebar.test.tsx+++ b/web/src/components/ChatSidebar.test.tsx@@ -1,455 +1,519 @@// @vitest-environment jsdom-import { act, type ReactNode } from "react";-import { createRoot, type Root } from "react-dom/client";-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { act, type ReactNode } from 'react'+import { createRoot, type Root } from 'react-dom/client'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'-import { EVENTS_CONNECT_TIMEOUT_MS } from "@/lib/events-reconnect";+import { EVENTS_CONNECT_TIMEOUT_MS } from '@/lib/events-reconnect'const apiMocks = vi.hoisted(() => ({- buildWsUrl: vi.fn(async () => "ws://localhost/api/events?channel=chat-1"),+ buildWsUrl: vi.fn(async () => 'ws://localhost/api/events?channel=chat-1'),getModelInfo: vi.fn(async () => ({capabilities: { supports_reasoning: false },- model: "test/model",- })),-}));+ model: 'test/model'+ }))+}))const gatewayMocks = vi.hoisted(() => {- const handlers = new Map<string, (event: unknown) => void>();+ const handlers = new Map<string, (event: unknown) => void>()return {+ constructed: 0,close: vi.fn(),connect: vi.fn(async () => undefined),handlers,on: vi.fn((event: string, handler: (event: unknown) => void) => {- handlers.set(event, handler);- return () => handlers.delete(event);+ handlers.set(event, handler)+ return () => handlers.delete(event)}),onState: vi.fn((handler: (state: string) => void) => {- handler("open");- return () => undefined;+ handler('open')+ return () => undefined}),- request: vi.fn(async () => ({ session_id: "sidecar-1" })),- };-});+ request: vi.fn(async () => ({ session_id: 'sidecar-1' }))+ }+})const reloadMocks = vi.hoisted(() => ({- maybeReloadForLoopbackWsAuthFailure: vi.fn(() => true),-}));+ maybeReloadForLoopbackWsAuthFailure: vi.fn(() => true)+}))-vi.mock("@/lib/api", () => ({+vi.mock('@/lib/api', () => ({api: { getModelInfo: apiMocks.getModelInfo },- buildWsUrl: apiMocks.buildWsUrl,-}));-vi.mock("@/lib/dashboard-auth-reload", () => ({- maybeReloadForLoopbackWsAuthFailure:- reloadMocks.maybeReloadForLoopbackWsAuthFailure,-}));-vi.mock("@/lib/gatewayClient", () => ({+ buildWsUrl: apiMocks.buildWsUrl+}))+vi.mock('@/lib/dashboard-auth-reload', () => ({+ maybeReloadForLoopbackWsAuthFailure: reloadMocks.maybeReloadForLoopbackWsAuthFailure+}))+vi.mock('@/lib/gatewayClient', () => ({GatewayClient: class {- close = gatewayMocks.close;- connect = gatewayMocks.connect;- on = gatewayMocks.on;- onState = gatewayMocks.onState;- request = gatewayMocks.request;- },-}));-vi.mock("@/components/ModelPickerDialog", () => ({- ModelPickerDialog: () => null,-}));-vi.mock("@/components/ModelReloadConfirm", () => ({- ModelReloadConfirm: () => null,-}));-vi.mock("@/components/ReasoningPicker", () => ({- ReasoningPicker: () => null,-}));-vi.mock("@nous-research/ui/ui/components/button", () => ({- Button: ({ children }: { children?: ReactNode }) => <button>{children}</button>,-}));-vi.mock("@nous-research/ui/ui/components/badge", () => ({- Badge: ({ children }: { children?: ReactNode }) => <span>{children}</span>,-}));-vi.mock("@nous-research/ui/ui/components/card", () => ({- Card: ({ children }: { children?: ReactNode }) => <div>{children}</div>,-}));--type EventLike = { code?: number; data?: string };+ constructor() {+ gatewayMocks.constructed += 1+ }+ close = gatewayMocks.close+ connect = gatewayMocks.connect+ on = gatewayMocks.on+ onState = gatewayMocks.onState+ request = gatewayMocks.request+ }+}))+vi.mock('@/components/ModelPickerDialog', () => ({+ ModelPickerDialog: () => null+}))+vi.mock('@/components/ModelReloadConfirm', () => ({+ ModelReloadConfirm: () => null+}))+vi.mock('@/components/ReasoningPicker', () => ({+ ReasoningPicker: () => null+}))+vi.mock('@nous-research/ui/ui/components/button', () => ({+ Button: ({ children, onClick }: { children?: ReactNode; onClick?: () => void }) => (+ <button onClick={onClick}>{children}</button>+ )+}))+vi.mock('@nous-research/ui/ui/components/badge', () => ({+ Badge: ({ children }: { children?: ReactNode }) => <span>{children}</span>+}))+vi.mock('@nous-research/ui/ui/components/card', () => ({+ Card: ({ children }: { children?: ReactNode }) => <div>{children}</div>+}))++type EventLike = { code?: number; data?: string }class FakeWebSocket {- static instances: FakeWebSocket[] = [];-- private listeners = new Map<string, Array<(event: EventLike) => void>>();- readonly url: string;- closed = false;+ static CONNECTING = 0+ static OPEN = 1+ static CLOSING = 2+ static CLOSED = 3+ static instances: FakeWebSocket[] = []++ private listeners = new Map<string, Array<(event: EventLike) => void>>()+ readonly url: string+ readyState = FakeWebSocket.CONNECTING+ closed = false+ sent: string[] = []constructor(url: string) {- this.url = url;- FakeWebSocket.instances.push(this);+ this.url = url+ FakeWebSocket.instances.push(this)}addEventListener(type: string, listener: (event: EventLike) => void) {- const listeners = this.listeners.get(type) ?? [];- listeners.push(listener);- this.listeners.set(type, listeners);+ const listeners = this.listeners.get(type) ?? []+ listeners.push(listener)+ this.listeners.set(type, listeners)+ }++ removeEventListener(type: string, listener: (event: EventLike) => void) {+ this.listeners.set(+ type,+ (this.listeners.get(type) ?? []).filter(l => l !== listener)+ )+ }++ send(data: string) {+ this.sent.push(data)}close() {- this.closed = true;+ if (this.closed) {+ return+ }+ this.closed = true+ // Real sockets deliver a close event for a client-initiated close too;+ // the shared client relies on it to settle a pending handshake.+ this.emit('close', { code: 1005 })}emit(type: string, event: EventLike) {- for (const listener of this.listeners.get(type) ?? []) {- listener(event);+ if (type === 'open') {+ this.readyState = FakeWebSocket.OPEN+ } else if (type === 'close') {+ this.readyState = FakeWebSocket.CLOSED+ }+ // Snapshot: `once` listeners remove themselves while we iterate.+ for (const listener of [...(this.listeners.get(type) ?? [])]) {+ listener(event)}}}-let container: HTMLDivElement;-let root: Root;+let container: HTMLDivElement+let root: Rootasync function render(ui: ReactNode) {- container = document.createElement("div");- document.body.append(container);- root = createRoot(container);- await act(async () => root.render(ui));+ container = document.createElement('div')+ document.body.append(container)+ root = createRoot(container)+ await act(async () => root.render(ui))}beforeEach(() => {- FakeWebSocket.instances = [];- vi.clearAllMocks();- apiMocks.buildWsUrl.mockReset();- apiMocks.buildWsUrl.mockResolvedValue(- "ws://localhost/api/events?channel=chat-1",- );- reloadMocks.maybeReloadForLoopbackWsAuthFailure.mockReturnValue(true);- vi.stubGlobal("WebSocket", FakeWebSocket);-});+ FakeWebSocket.instances = []+ vi.clearAllMocks()+ apiMocks.buildWsUrl.mockReset()+ apiMocks.buildWsUrl.mockResolvedValue('ws://localhost/api/events?channel=chat-1')+ reloadMocks.maybeReloadForLoopbackWsAuthFailure.mockReturnValue(true)+ vi.stubGlobal('WebSocket', FakeWebSocket)+})afterEach(async () => {- await act(async () => root?.unmount());- container?.remove();- vi.unstubAllGlobals();-});+ await act(async () => root?.unmount())+ container?.remove()+ vi.unstubAllGlobals()+})-describe("ChatSidebar event socket", () => {- it("routes loopback 4401 closes through stale-token recovery", async () => {- const { ChatSidebar } = await import("./ChatSidebar");+describe('ChatSidebar event socket', () => {+ it('routes loopback 4401 closes through stale-token recovery', async () => {+ const { ChatSidebar } = await import('./ChatSidebar')- await render(<ChatSidebar channel="chat-1" />);+ await render(<ChatSidebar channel="chat-1" />)- await vi.waitFor(() => expect(FakeWebSocket.instances).toHaveLength(1));- expect(apiMocks.buildWsUrl).toHaveBeenCalledWith("/api/events", {- channel: "chat-1",- });+ await vi.waitFor(() => expect(FakeWebSocket.instances).toHaveLength(1))+ expect(apiMocks.buildWsUrl).toHaveBeenCalledWith('/api/events', {+ channel: 'chat-1'+ })- FakeWebSocket.instances[0].emit("close", { code: 4401 });+ FakeWebSocket.instances[0].emit('close', { code: 4401 })- expect(- reloadMocks.maybeReloadForLoopbackWsAuthFailure,- ).toHaveBeenCalledWith(4401);- });-});+ expect(reloadMocks.maybeReloadForLoopbackWsAuthFailure).toHaveBeenCalledWith(4401)+ })+})-describe("ChatSidebar event socket reconnect", () => {+describe('ChatSidebar event socket reconnect', () => {beforeEach(() => {// Not loopback: exercise the gated-mode path so closes fall through to// the reconnect logic instead of triggering a page reload.- reloadMocks.maybeReloadForLoopbackWsAuthFailure.mockReturnValue(false);- vi.useFakeTimers();- });+ reloadMocks.maybeReloadForLoopbackWsAuthFailure.mockReturnValue(false)+ vi.useFakeTimers()+ })afterEach(() => {- vi.useRealTimers();- });+ vi.useRealTimers()+ })async function renderSidebar() {- const { ChatSidebar } = await import("./ChatSidebar");- await render(<ChatSidebar channel="chat-1" />);- await vi.waitFor(() => expect(FakeWebSocket.instances).toHaveLength(1));+ const { ChatSidebar } = await import('./ChatSidebar')+ await render(<ChatSidebar channel="chat-1" />)+ await vi.waitFor(() => expect(FakeWebSocket.instances).toHaveLength(1))}/** Advance timers and flush the async `connect()` that fires on the tick. */async function advance(ms: number) {await act(async () => {- await vi.advanceTimersByTimeAsync(ms);- });+ await vi.advanceTimersByTimeAsync(ms)+ })}- it("reconnects after a transient close", async () => {- await renderSidebar();+ it('reconnects after a transient close', async () => {+ await renderSidebar()await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });- expect(FakeWebSocket.instances).toHaveLength(1);+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })+ expect(FakeWebSocket.instances).toHaveLength(1)- await advance(1_000);- expect(FakeWebSocket.instances).toHaveLength(2);- expect(apiMocks.buildWsUrl).toHaveBeenCalledTimes(2);- });+ await advance(1_000)+ expect(FakeWebSocket.instances).toHaveLength(2)+ expect(apiMocks.buildWsUrl).toHaveBeenCalledTimes(2)+ })- it("keeps retrying when reconnect URL construction fails", async () => {- await renderSidebar();+ it('keeps retrying when reconnect URL construction fails', async () => {+ await renderSidebar()apiMocks.buildWsUrl- .mockRejectedValueOnce(new Error("ticket endpoint unavailable"))- .mockResolvedValue("ws://localhost/api/events?channel=chat-1");+ .mockRejectedValueOnce(new Error('ticket endpoint unavailable'))+ .mockResolvedValue('ws://localhost/api/events?channel=chat-1')await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })- await advance(1_000);- expect(FakeWebSocket.instances).toHaveLength(1);- expect(container.textContent).toContain("reconnecting in 2s");+ await advance(1_000)+ expect(FakeWebSocket.instances).toHaveLength(1)+ expect(container.textContent).toContain('reconnecting in 2s')- await advance(2_000);- expect(FakeWebSocket.instances).toHaveLength(2);- expect(apiMocks.buildWsUrl).toHaveBeenCalledTimes(3);- });+ await advance(2_000)+ expect(FakeWebSocket.instances).toHaveLength(2)+ expect(apiMocks.buildWsUrl).toHaveBeenCalledTimes(3)+ })- it("times out a stalled URL request and retries", async () => {- let resolveStalledRequest!: (url: string) => void;- await renderSidebar();+ it('times out a stalled URL request and retries', async () => {+ let resolveStalledRequest!: (url: string) => void+ await renderSidebar()apiMocks.buildWsUrl.mockImplementationOnce(() =>- new Promise<string>((resolve) => {- resolveStalledRequest = resolve;- }),+ new Promise<string>(resolve => {+ resolveStalledRequest = resolve+ }))- .mockResolvedValue("ws://localhost/api/events?channel=chat-1");+ .mockResolvedValue('ws://localhost/api/events?channel=chat-1')await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })- await advance(1_000 + EVENTS_CONNECT_TIMEOUT_MS);- expect(FakeWebSocket.instances).toHaveLength(1);- expect(container.textContent).toContain("reconnecting in 2s");+ await advance(1_000 + EVENTS_CONNECT_TIMEOUT_MS)+ expect(FakeWebSocket.instances).toHaveLength(1)+ expect(container.textContent).toContain('reconnecting in 2s')// A late ticket response from the timed-out attempt must not create a// superseded socket alongside the scheduled replacement.await act(async () => {- resolveStalledRequest("ws://localhost/api/events?channel=stale");- await Promise.resolve();- });- expect(FakeWebSocket.instances).toHaveLength(1);+ resolveStalledRequest('ws://localhost/api/events?channel=stale')+ await Promise.resolve()+ })+ expect(FakeWebSocket.instances).toHaveLength(1)- await advance(2_000);- expect(FakeWebSocket.instances).toHaveLength(2);- expect(apiMocks.buildWsUrl).toHaveBeenCalledTimes(3);- });+ await advance(2_000)+ expect(FakeWebSocket.instances).toHaveLength(2)+ expect(apiMocks.buildWsUrl).toHaveBeenCalledTimes(3)+ })- it("times out a stalled WebSocket handshake and retries", async () => {- await renderSidebar();+ it('times out a stalled WebSocket handshake and retries', async () => {+ await renderSidebar()await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });- await advance(1_000);- expect(FakeWebSocket.instances).toHaveLength(2);+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })+ await advance(1_000)+ expect(FakeWebSocket.instances).toHaveLength(2)- await advance(EVENTS_CONNECT_TIMEOUT_MS);- expect(FakeWebSocket.instances[1].closed).toBe(true);- expect(container.textContent).toContain("reconnecting in 2s");+ await advance(EVENTS_CONNECT_TIMEOUT_MS)+ expect(FakeWebSocket.instances[1].closed).toBe(true)+ expect(container.textContent).toContain('reconnecting in 2s')- await advance(2_000);- expect(FakeWebSocket.instances).toHaveLength(3);- });+ await advance(2_000)+ expect(FakeWebSocket.instances).toHaveLength(3)+ })- it("backs off exponentially across repeated failures", async () => {- await renderSidebar();+ it('backs off exponentially across repeated failures', async () => {+ await renderSidebar()// 1s, then 2s, then 4s — a socket that never opens keeps backing off.for (const [index, delay] of [1_000, 2_000, 4_000].entries()) {await act(async () => {- FakeWebSocket.instances[index].emit("close", { code: 1006 });- });+ FakeWebSocket.instances[index].emit('close', { code: 1006 })+ })// The previous (shorter) delay must not be enough to fire this one.if (index > 0) {- await advance(delay - 1);- expect(FakeWebSocket.instances).toHaveLength(index + 1);+ await advance(delay - 1)+ expect(FakeWebSocket.instances).toHaveLength(index + 1)}- await advance(delay);- expect(FakeWebSocket.instances).toHaveLength(index + 2);+ await advance(delay)+ expect(FakeWebSocket.instances).toHaveLength(index + 2)}- });+ })- it("schedules only one retry when error and close both fire", async () => {- await renderSidebar();+ it('schedules only one retry when error and close both fire', async () => {+ await renderSidebar()// A failed socket emits `error` then `close`. Scheduling from both// paths would queue two timers and leak the untracked one.await act(async () => {- FakeWebSocket.instances[0].emit("error", {});- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });+ FakeWebSocket.instances[0].emit('error', {})+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })- await advance(1_000);+ await advance(1_000)await act(async () => {- FakeWebSocket.instances[1].emit("open", {});- });- await advance(29_000);- expect(FakeWebSocket.instances).toHaveLength(2);- });+ FakeWebSocket.instances[1].emit('open', {})+ })+ await advance(29_000)+ expect(FakeWebSocket.instances).toHaveLength(2)+ })- it("resets the backoff after a successful reconnect", async () => {- await renderSidebar();+ it('resets the backoff after a successful reconnect', async () => {+ await renderSidebar()await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });- await advance(1_000);- expect(FakeWebSocket.instances).toHaveLength(2);+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })+ await advance(1_000)+ expect(FakeWebSocket.instances).toHaveLength(2)// Reconnected — the next drop should start from 1s again, not 2s.await act(async () => {- FakeWebSocket.instances[1].emit("open", {});- FakeWebSocket.instances[1].emit("close", { code: 1006 });- });- await advance(1_000);- expect(FakeWebSocket.instances).toHaveLength(3);- });+ FakeWebSocket.instances[1].emit('open', {})+ FakeWebSocket.instances[1].emit('close', { code: 1006 })+ })+ await advance(1_000)+ expect(FakeWebSocket.instances).toHaveLength(3)+ })- it("does not retry auth rejections", async () => {- await renderSidebar();+ it('does not retry auth rejections', async () => {+ await renderSidebar()await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 4403 });- });+ FakeWebSocket.instances[0].emit('close', { code: 4403 })+ })- await advance(60_000);- expect(FakeWebSocket.instances).toHaveLength(1);- });+ await advance(60_000)+ expect(FakeWebSocket.instances).toHaveLength(1)+ })- it("does not retry a normal closure", async () => {- await renderSidebar();+ it('does not retry a normal closure', async () => {+ await renderSidebar()await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1000 });- });+ FakeWebSocket.instances[0].emit('close', { code: 1000 })+ })- await advance(60_000);- expect(FakeWebSocket.instances).toHaveLength(1);- });+ await advance(60_000)+ expect(FakeWebSocket.instances).toHaveLength(1)+ })- it("gives up after the attempt cap instead of retrying forever", async () => {- await renderSidebar();+ it('gives up after the attempt cap instead of retrying forever', async () => {+ await renderSidebar()for (let i = 0; i < 40; i++) {- const socket =- FakeWebSocket.instances[FakeWebSocket.instances.length - 1];+ const socket = FakeWebSocket.instances[FakeWebSocket.instances.length - 1]await act(async () => {- socket.emit("close", { code: 1006 });- });- await advance(30_000);+ socket.emit('close', { code: 1006 })+ })+ await advance(30_000)}// 15 retries + the initial connection.- expect(FakeWebSocket.instances.length).toBeLessThanOrEqual(16);- });+ expect(FakeWebSocket.instances.length).toBeLessThanOrEqual(16)+ })- it("clears its own banner on a successful reconnect", async () => {- await renderSidebar();+ it('clears its own banner on a successful reconnect', async () => {+ await renderSidebar()await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });- expect(container.textContent).toContain("events feed disconnected");+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })+ expect(container.textContent).toContain('events feed disconnected')- await advance(1_000);+ await advance(1_000)await act(async () => {- FakeWebSocket.instances[1].emit("open", {});- });+ FakeWebSocket.instances[1].emit('open', {})+ })// Banner gone entirely — including the "reconnect events feed" button,// which only renders while `error` is set.- expect(container.textContent).not.toContain("events feed");- });+ expect(container.textContent).not.toContain('events feed')+ })- it("does not clear a credential warning when the feed recovers", async () => {- await renderSidebar();+ it('does not clear a credential warning when the feed recovers', async () => {+ await renderSidebar()await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });- await advance(1_000);+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })+ await advance(1_000)// A sidecar error lands while the events socket is still reconnecting.// The banner is shared, so a blind `setError(null)` on reconnect would// hide a real problem the user needs to see.await act(async () => {- gatewayMocks.handlers.get("error")?.({- payload: { message: "ANTHROPIC_API_KEY is not set" },- });- });+ gatewayMocks.handlers.get('error')?.({+ payload: { message: 'ANTHROPIC_API_KEY is not set' }+ })+ })await act(async () => {- FakeWebSocket.instances[1].emit("open", {});- });+ FakeWebSocket.instances[1].emit('open', {})+ })- expect(container.textContent).toContain("ANTHROPIC_API_KEY is not set");- });+ expect(container.textContent).toContain('ANTHROPIC_API_KEY is not set')+ })- it("does not overwrite a sidecar error when the feed drops", async () => {- await renderSidebar();+ it('does not overwrite a sidecar error when the feed drops', async () => {+ await renderSidebar()// A sidecar error is already on the banner...await act(async () => {- gatewayMocks.handlers.get("error")?.({- payload: { message: "ANTHROPIC_API_KEY is not set" },- });- });+ gatewayMocks.handlers.get('error')?.({+ payload: { message: 'ANTHROPIC_API_KEY is not set' }+ })+ })// ...when the events feed drops. `error` is that message's only home,// so overwriting it loses the warning permanently — the feed's own// banner would later clear itself to null and the sidecar never// re-emits.await act(async () => {- FakeWebSocket.instances[0].emit("error", {});- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });+ FakeWebSocket.instances[0].emit('error', {})+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })- expect(container.textContent).toContain("ANTHROPIC_API_KEY is not set");+ expect(container.textContent).toContain('ANTHROPIC_API_KEY is not set')// The disconnect message must not have replaced it. (Matching the// banner text specifically — "reconnect events feed" is the button// label, which is expected to be present whenever a banner shows.)- expect(container.textContent).not.toContain("events feed disconnected");- });+ expect(container.textContent).not.toContain('events feed disconnected')+ })- it("still reconnects while a foreign banner suppresses its message", async () => {- await renderSidebar();+ it('still reconnects while a foreign banner suppresses its message', async () => {+ await renderSidebar()await act(async () => {- gatewayMocks.handlers.get("error")?.({- payload: { message: "ANTHROPIC_API_KEY is not set" },- });- });+ gatewayMocks.handlers.get('error')?.({+ payload: { message: 'ANTHROPIC_API_KEY is not set' }+ })+ })await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })// Declining to write the banner must not disable the retry itself.- await advance(1_000);- expect(FakeWebSocket.instances).toHaveLength(2);- });+ await advance(1_000)+ expect(FakeWebSocket.instances).toHaveLength(2)+ })++ it('reuses one JSON-RPC client across reconnects so seq replay can fire', async () => {+ // The shared client only asks `session.events.since` for the gap when the+ // instance that recorded the watermarks is the one that redials.+ gatewayMocks.constructed = 0+ await renderSidebar()+ expect(gatewayMocks.constructed).toBe(1)+ expect(gatewayMocks.connect).toHaveBeenCalledTimes(1)++ // Drop the feed so the banner (and its reconnect button) renders.+ await act(async () => {+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })+ const reconnectButton = Array.from(container.querySelectorAll('button')).find(b =>+ /reconnect events feed/i.test(b.textContent ?? '')+ )+ expect(reconnectButton).toBeDefined()+ await act(async () => {+ reconnectButton!.click()+ })++ expect(gatewayMocks.close).toHaveBeenCalled()+ expect(gatewayMocks.connect).toHaveBeenCalledTimes(2)+ expect(gatewayMocks.constructed).toBe(1)+ })- it("clears the reconnect timer and closes the socket on unmount", async () => {- await renderSidebar();+ it('clears the reconnect timer and closes the socket on unmount', async () => {+ await renderSidebar()await act(async () => {- FakeWebSocket.instances[0].emit("close", { code: 1006 });- });- expect(vi.getTimerCount()).toBeGreaterThan(0);+ FakeWebSocket.instances[0].emit('close', { code: 1006 })+ })+ expect(vi.getTimerCount()).toBeGreaterThan(0)+ // Let the retry dial a second socket so unmount has a live one to close.+ await advance(1_000)+ expect(FakeWebSocket.instances).toHaveLength(2)+ await act(async () => {+ FakeWebSocket.instances[1].emit('open', {})+ FakeWebSocket.instances[1].emit('close', { code: 1006 })+ })+ expect(vi.getTimerCount()).toBeGreaterThan(0)+ await advance(1_000)+ expect(FakeWebSocket.instances).toHaveLength(3)- await act(async () => root.unmount());+ await act(async () => root.unmount())- expect(FakeWebSocket.instances[0].closed).toBe(true);+ expect(FakeWebSocket.instances[2].closed).toBe(true)// The pending retry timer must be cleared, not merely neutered by the// `unmounting` flag — a live timer keeps the effect closure alive.- expect(vi.getTimerCount()).toBe(0);+ expect(vi.getTimerCount()).toBe(0)- await advance(60_000);- expect(FakeWebSocket.instances).toHaveLength(1);- });-});+ await advance(60_000)+ expect(FakeWebSocket.instances).toHaveLength(3)+ })+})diff --git a/web/src/components/ChatSidebar.tsx b/web/src/components/ChatSidebar.tsxindex ab8f7b338c6..6a3eb6e8027 100644--- a/web/src/components/ChatSidebar.tsx+++ b/web/src/components/ChatSidebar.tsx@@ -25,19 +25,17 @@* terminal pane keeps working unimpaired.*/-import { Button } from "@nous-research/ui/ui/components/button";-import { Badge } from "@nous-research/ui/ui/components/badge";-import { Card } from "@nous-research/ui/ui/components/card";--import { ModelPickerDialog } from "@/components/ModelPickerDialog";-import { ModelReloadConfirm } from "@/components/ModelReloadConfirm";-import { ReasoningPicker } from "@/components/ReasoningPicker";-import { GatewayClient, type ConnectionState } from "@/lib/gatewayClient";-import { api, buildWsUrl } from "@/lib/api";-import { maybeReloadForLoopbackWsAuthFailure } from "@/lib/dashboard-auth-reload";+import { Button } from '@nous-research/ui/ui/components/button'+import { Badge } from '@nous-research/ui/ui/components/badge'+import { Card } from '@nous-research/ui/ui/components/card'++import { ModelPickerDialog } from '@/components/ModelPickerDialog'+import { ModelReloadConfirm } from '@/components/ModelReloadConfirm'+import { ReasoningPicker } from '@/components/ReasoningPicker'+import { GatewayClient, type ConnectionState } from '@/lib/gatewayClient'+import { EventsFeedClient } from '@/lib/eventsFeedClient'+import { api } from '@/lib/api'import {- EVENTS_CONNECT_TIMEOUT_MS,- EVENTS_DISCONNECTED_MESSAGE,EVENTS_MAX_RECONNECT_ATTEMPTS,eventsGaveUpMessage,eventsReconnectDelayMs,@@ -45,53 +43,45 @@ import {eventsRejectedMessage,isEventsAuthRejection,isEventsFeedMessage,- shouldRetryEventsClose,-} from "@/lib/events-reconnect";-import { titleFromSessionInfoPayload } from "@/lib/chat-title";+ shouldRetryEventsClose+} from '@/lib/events-reconnect'+import { titleFromSessionInfoPayload } from '@/lib/chat-title'-import { cn } from "@/lib/utils";-import { AlertCircle, ChevronDown, RefreshCw } from "lucide-react";-import { useCallback, useEffect, useMemo, useRef, useState } from "react";+import { cn } from '@/lib/utils'+import { AlertCircle, ChevronDown, RefreshCw } from 'lucide-react'+import { useCallback, useEffect, useMemo, useRef, useState } from 'react'interface SessionInfo {- cwd?: string;- model?: string;- provider?: string;- credential_warning?: string;- title?: string;+ cwd?: string+ model?: string+ provider?: string+ credential_warning?: string+ title?: string}-interface RpcEnvelope {- method?: string;- params?: { type?: string; payload?: unknown };+const STATE_LABEL: Record<ConnectionState, string> = {+ idle: 'idle',+ connecting: 'connecting',+ open: 'live',+ closed: 'closed',+ error: 'error'}-const STATE_LABEL: Record<ConnectionState, string> = {- idle: "idle",- connecting: "connecting",- open: "live",- closed: "closed",- error: "error",-};--const STATE_TONE: Record<- ConnectionState,- "secondary" | "warning" | "success" | "destructive"-> = {- idle: "secondary",- connecting: "warning",- open: "success",- closed: "secondary",- error: "destructive",-};+const STATE_TONE: Record<ConnectionState, 'secondary' | 'warning' | 'success' | 'destructive'> = {+ idle: 'secondary',+ connecting: 'warning',+ open: 'success',+ closed: 'secondary',+ error: 'destructive'+}interface ChatSidebarProps {- channel: string;+ channel: string/** Chat profile from the dashboard switcher / URL scope. */- profile?: string;- className?: string;- onDashboardNewSessionRequest?: () => void;- onSessionTitleChange?: (title: string | null) => void;+ profile?: string+ className?: string+ onDashboardNewSessionRequest?: () => void+ onSessionTitleChange?: (title: string | null) => void}/** Build the ``session.create`` params for the sidecar session.@@ -104,9 +94,9 @@ interface ChatSidebarProps {export function sidecarSessionCreateParams(profile?: string): Record<string, unknown> {return {close_on_disconnect: true,- source: "tool",- ...(profile ? { profile } : {}),- };+ source: 'tool',+ ...(profile ? { profile } : {})+ }}export function ChatSidebar({@@ -114,20 +104,21 @@ export function ChatSidebar({profile,className,onDashboardNewSessionRequest,- onSessionTitleChange,+ onSessionTitleChange}: ChatSidebarProps) {- // `version` bumps on reconnect; gw is derived so we never call setState- // for it inside an effect (React 19's set-state-in-effect rule). The- // counter is the dependency on purpose — it's not read in the memo body,- // it's the signal that says "rebuild the client".- const [version, setVersion] = useState(0);- // eslint-disable-next-line react-hooks/exhaustive-deps- const gw = useMemo(() => new GatewayClient(), [version]);-- const [state, setState] = useState<ConnectionState>("idle");- const [info, setInfo] = useState<SessionInfo>({});- const [modelOpen, setModelOpen] = useState(false);- const [error, setError] = useState<string | null>(null);+ // `version` bumps on reconnect (manual button, profile/channel switch) and+ // re-runs the socket effects. The clients themselves live for the whole+ // component: the shared client keeps per-session seq watermarks and asks+ // the gateway to replay the gap on the next `connect()`, which only works+ // when the SAME instance survives the drop.+ const [version, setVersion] = useState(0)+ const gw = useMemo(() => new GatewayClient(), [])+ const feed = useMemo(() => new EventsFeedClient(), [])++ const [state, setState] = useState<ConnectionState>('idle')+ const [info, setInfo] = useState<SessionInfo>({})+ const [modelOpen, setModelOpen] = useState(false)+ const [error, setError] = useState<string | null>(null)// The badge shows config.yaml's main model (`model.default`) via// `/api/model/info` — the same value the Models page writes and a new chat// session boots from. We deliberately don't use the sidecar's `session.info`@@ -136,80 +127,78 @@ export function ChatSidebar({// elsewhere, so the badge would go stale. Pass the chat profile explicitly so// this card stays scoped to the PTY even if the global dashboard switcher// changes while the chat is open.- const [effectiveModel, setEffectiveModel] = useState("");+ const [effectiveModel, setEffectiveModel] = useState('')// Whether the effective model supports reasoning effort — gates the// ReasoningPicker. Read from the same `/api/model/info` capabilities the// (currently unused) ModelInfoCard surfaces, so the dashboard exposes a// control to *set* the level, not just a read-only "Reasoning" badge.- const [supportsReasoning, setSupportsReasoning] = useState(false);+ const [supportsReasoning, setSupportsReasoning] = useState(false)// Bumped on model change/save so ReasoningPicker re-reads the saved effort// (config is profile-scoped the same way the model badge is).- const [modelRefreshKey, setModelRefreshKey] = useState(0);+ const [modelRefreshKey, setModelRefreshKey] = useState(0)// Set after the picker saves a model and the user declines the reload: config// is updated but the running session keeps its model until rebuilt.- const [modelNotice, setModelNotice] = useState<string | null>(null);+ const [modelNotice, setModelNotice] = useState<string | null>(null)// Short name of a just-saved model awaiting confirm to reload (a fresh chat// session is how the running chat adopts it; we confirm before discarding it).- const [pendingReloadModel, setPendingReloadModel] = useState<string | null>(- null,- );+ const [pendingReloadModel, setPendingReloadModel] = useState<string | null>(null)const refreshEffectiveModel = useCallback(() => {void api.getModelInfo(profile)- .then((r) => {- if (r?.model) setEffectiveModel(String(r.model));- setSupportsReasoning(!!r?.capabilities?.supports_reasoning);+ .then(r => {+ if (r?.model) setEffectiveModel(String(r.model))+ setSupportsReasoning(!!r?.capabilities?.supports_reasoning)// Bump so ReasoningPicker re-reads the saved effort for the new model.- setModelRefreshKey((k) => k + 1);+ setModelRefreshKey(k => k + 1)}).catch(() => {// Best-effort: keep the last known label rather than blanking it.- });- }, [profile]);+ })+ }, [profile])// Profile or PTY channel change tears down both WebSockets. Bump `version`// (same path as the manual Reconnect button) so the gateway client is// recreated and the events feed resubscribes — otherwise the old events// socket's close handler can leave a stale error banner after a switch.- const scopeKey = `${channel}\0${profile ?? ""}`;- const prevScopeKey = useRef<string | null>(null);+ const scopeKey = `${channel}\0${profile ?? ''}`+ const prevScopeKey = useRef<string | null>(null)useEffect(() => {if (prevScopeKey.current === null) {- prevScopeKey.current = scopeKey;- return;+ prevScopeKey.current = scopeKey+ return}- if (prevScopeKey.current === scopeKey) return;- prevScopeKey.current = scopeKey;- setError(null);- setVersion((v) => v + 1);- }, [scopeKey]);+ if (prevScopeKey.current === scopeKey) return+ prevScopeKey.current = scopeKey+ setError(null)+ setVersion(v => v + 1)+ }, [scopeKey])useEffect(() => {- let cancelled = false;+ let cancelled = falsequeueMicrotask(() => {- if (cancelled) return;- setInfo({});- setError(null);- });- const offState = gw.onState(setState);+ if (cancelled) return+ setInfo({})+ setError(null)+ })+ const offState = gw.onState(setState)- const offSessionInfo = gw.on("session.info", (ev) => {+ const offSessionInfo = gw.on('session.info', ev => {// session.info is surface-specific on the wire; narrow to the fields this sidebar reads.- const payload = ev.payload as SessionInfo | undefined;+ const payload = ev.payload as SessionInfo | undefinedif (payload) {- setInfo((prev) => ({ ...prev, ...payload }));+ setInfo(prev => ({ ...prev, ...payload }))}- });+ })- const offError = gw.on("error", (ev) => {- const message = ev.payload?.message;+ const offError = gw.on('error', ev => {+ const message = ev.payload?.messageif (message) {- setError(message);+ setError(message)}- });+ })// Create the sidecar session so the gateway surfaces session-scoped// signals (connection state, credential warnings). It's independent of the@@ -218,264 +207,184 @@ export function ChatSidebar({gw.connect().then(() => {if (cancelled) {- return;+ return}// close_on_disconnect: the gateway reaps this sidecar session (and its// slash_worker subprocess) when the WS drops, instead of leaking it.- return gw.request<{ session_id: string }>("session.create", sidecarSessionCreateParams(profile));+ return gw.request<{ session_id: string }>('session.create', sidecarSessionCreateParams(profile))}).catch((e: Error) => {if (!cancelled) {- setError(e.message);+ setError(e.message)}- });+ })return () => {- cancelled = true;- offState();- offSessionInfo();- offError();- gw.close();- };- // `profile` is read from render; scope changes bump `version` → new `gw`.+ cancelled = true+ offState()+ offSessionInfo()+ offError()+ gw.close()+ }+ // `profile` is read from render; scope changes bump `version` → redial.// eslint-disable-next-line react-hooks/exhaustive-deps- }, [gw]);+ }, [gw, version])// Event subscriber WebSocket — receives the rebroadcast of every// dispatcher emit from the PTY child's gateway. See /api/pub +// /api/events in hermes_cli/web_server.py for the broadcast hop.//- // Failures (auth/loopback rejection, server too old to expose the- // endpoint, transient drops) surface in the same banner as the- // JSON-RPC sidecar so the sidebar matches its documented best-effort- // UX and the user always has a reconnect affordance.+ // Framing, dispatch and connect timeout come from the shared JSON-RPC+ // client (`EventsFeedClient`); this effect owns only the retry policy and+ // the banner. Failures (auth/loopback rejection, server too old to expose+ // the endpoint, transient drops) surface in the same banner as the+ // JSON-RPC sidecar so the sidebar matches its documented best-effort UX+ // and the user always has a reconnect affordance.useEffect(() => {if (!channel) {- return;+ return}- // In loopback mode the legacy ?token=<session> path is fine; in gated- // mode we have to mint a single-use ticket from the cookie. `connect`- // keeps the outer effect synchronous so its ``return cleanup`` stays at- // the top level; `ws` is a closed-over binding the cleanup reads.- let unmounting = false;- let ws: WebSocket | null = null;- let reconnectTimer: ReturnType<typeof setTimeout> | null = null;- let connectTimer: ReturnType<typeof setTimeout> | null = null;- let connectGeneration = 0;- let attempt = 0;-- const clearConnectTimer = () => {- if (connectTimer) {- clearTimeout(connectTimer);- connectTimer = null;- }- };+ let unmounting = false+ let reconnectTimer: ReturnType<typeof setTimeout> | null = null+ let attempt = 0// The banner is shared with `info.credential_warning` and the JSON-RPC// sidecar, and `error` is those messages' only home — the sidecar does// not re-emit. So the events feed may only write over an empty banner// or one of its own messages, and may only clear its own.const surface = (msg: string) =>- !unmounting &&- setError((current) =>- isEventsFeedMessage(current) ? msg : (current ?? msg),- );-- const clearEventsBanner = () =>- !unmounting &&- setError((current) => (isEventsFeedMessage(current) ? null : current));-- // Single scheduling path. `close` always follows `error` for a failed- // socket, so scheduling from `error` too would queue two timers and- // leak the first — only the latest is tracked for cleanup.+ !unmounting && setError(current => (isEventsFeedMessage(current) ? msg : (current ?? msg)))++ const clearEventsBanner = () => !unmounting && setError(current => (isEventsFeedMessage(current) ? null : current))++ // Single scheduling path: the client collapses `error` + `close` of one+ // socket generation into one `closed` transition, so only one timer is+ // ever queued per drop.const scheduleReconnect = () => {if (unmounting || reconnectTimer) {- return;+ return}if (attempt >= EVENTS_MAX_RECONNECT_ATTEMPTS) {- surface(eventsGaveUpMessage());- return;+ surface(eventsGaveUpMessage())+ return}- const delay = eventsReconnectDelayMs(attempt);- attempt += 1;- surface(eventsReconnectingMessage(delay));+ const delay = eventsReconnectDelayMs(attempt)+ attempt += 1+ surface(eventsReconnectingMessage(delay))reconnectTimer = setTimeout(() => {- reconnectTimer = null;- void connect();- }, delay);- };+ reconnectTimer = null+ void connect()+ }, delay)+ }const connect = async () => {if (unmounting) {- return;+ return}-- const generation = ++connectGeneration;- let socket: WebSocket | null = null;-- // Cover the whole connection attempt, including gated-mode ticket- // minting. A failed or hanging pre-socket request otherwise emits no- // WebSocket close event and permanently strands the retry loop at its- // last "reconnecting in ..." banner.- clearConnectTimer();- connectTimer = setTimeout(() => {- connectTimer = null;- if (unmounting || generation !== connectGeneration) {- return;- }-- // Invalidate any late ticket result or socket event from this attempt- // before scheduling its replacement.- connectGeneration += 1;- if (socket && ws === socket) {- ws = null;- socket.close();- }- scheduleReconnect();- }, EVENTS_CONNECT_TIMEOUT_MS);-try {// Re-minted every attempt: tickets are single-use with a short TTL,// so a reconnect cannot replay the URL from the first connection.- const url = await buildWsUrl("/api/events", { channel });- if (unmounting || generation !== connectGeneration) {- return;- }- socket = new WebSocket(url);- ws = socket;+ await feed.connect(channel)} catch {- if (unmounting || generation !== connectGeneration) {- return;+ // Connect-phase failures (ticket mint, handshake timeout, close+ // during handshake) reach the reconnect ladder through the close+ // handler when a socket existed; a pre-socket failure has no close+ // event, so schedule here. `onClose` de-dupes via `reconnectTimer`.+ if (!unmounting && feed.lastCloseCode === null) {+ scheduleReconnect()}- clearConnectTimer();- connectGeneration += 1;- scheduleReconnect();- return;}+ }- // A superseded socket's late close must not schedule a retry on top- // of the one that replaced it.- const isCurrent = () => ws === socket;-- socket.addEventListener("open", () => {- if (!isCurrent()) {- return;- }- clearConnectTimer();- attempt = 0;- clearEventsBanner();- });-- // `unmounting` suppresses the banner during cleanup — `ws.close()`- // from the effect's return fires a close event with code 1005 that- // would otherwise look like an unexpected drop.- socket.addEventListener("error", () => {- if (isCurrent()) {- surface(EVENTS_DISCONNECTED_MESSAGE);- }- });-- socket.addEventListener("close", (ev) => {- if (!isCurrent()) {- return;- }- clearConnectTimer();- if (maybeReloadForLoopbackWsAuthFailure(ev.code)) {- return;- }- if (isEventsAuthRejection(ev.code)) {- surface(eventsRejectedMessage(ev.code));- return;- }- if (shouldRetryEventsClose(ev.code)) {- scheduleReconnect();- }- });-- socket.addEventListener("message", (ev) => {- let frame: RpcEnvelope;-- try {- frame = JSON.parse(ev.data);- } catch {- return;- }-- if (frame.method !== "event" || !frame.params) {- return;- }+ const offClose = feed.onClose(code => {+ if (unmounting) {+ return+ }+ if (code !== undefined && isEventsAuthRejection(code)) {+ surface(eventsRejectedMessage(code))+ return+ }+ // `undefined` = handshake timeout / error without a close frame.+ if (shouldRetryEventsClose(code)) {+ scheduleReconnect()+ }+ })- const { type, payload } = frame.params;+ const offState = feed.onState(state => {+ if (state === 'open') {+ attempt = 0+ clearEventsBanner()+ }+ })- if (type === "session.info") {- const title = titleFromSessionInfoPayload(payload);- if (title !== undefined) {- onSessionTitleChange?.(title);- }- } else if (type === "dashboard.new_session_requested") {- onDashboardNewSessionRequest?.();- }- });- };+ const offSessionInfo = feed.on('session.info', ev => {+ const title = titleFromSessionInfoPayload(ev.payload)+ if (title !== undefined) {+ onSessionTitleChange?.(title)+ }+ })+ const offNewSession = feed.on('dashboard.new_session_requested', () => {+ onDashboardNewSessionRequest?.()+ })- void connect();+ void connect()return () => {- unmounting = true;- connectGeneration += 1;- clearConnectTimer();+ unmounting = trueif (reconnectTimer) {- clearTimeout(reconnectTimer);- reconnectTimer = null;+ clearTimeout(reconnectTimer)+ reconnectTimer = null}- ws?.close();- };- }, [channel, onDashboardNewSessionRequest, onSessionTitleChange, version]);+ offClose()+ offState()+ offSessionInfo()+ offNewSession()+ feed.close()+ }+ }, [channel, feed, onDashboardNewSessionRequest, onSessionTitleChange, version])// Seed the badge on mount and re-read it whenever the sockets are rebuilt// (a profile/channel switch bumps `version`).useEffect(() => {- refreshEffectiveModel();- }, [refreshEffectiveModel, version]);+ refreshEffectiveModel()+ }, [refreshEffectiveModel, version])const reconnect = useCallback(() => {- setError(null);- setModelNotice(null);- setPendingReloadModel(null);- setVersion((v) => v + 1);- }, []);+ setError(null)+ setModelNotice(null)+ setPendingReloadModel(null)+ setVersion(v => v + 1)+ }, [])// The picker writes config.yaml over REST and reloads — it doesn't ride the// sidecar gateway session, so it's available whenever the sidebar is mounted.- const modelName = effectiveModel || info.model || "—";- const modelLabel = modelName.split("/").slice(-1)[0] ?? "—";- const banner = error ?? info.credential_warning ?? null;+ const modelName = effectiveModel || info.model || '—'+ const modelLabel = modelName.split('/').slice(-1)[0] ?? '—'+ const banner = error ?? info.credential_warning ?? nullreturn (<asideclassName={cn(- "flex h-full w-full min-w-0 shrink-0 flex-col gap-3 overflow-y-auto overflow-x-hidden pr-1",- className,+ 'flex h-full w-full min-w-0 shrink-0 flex-col gap-3 overflow-y-auto overflow-x-hidden pr-1',+ className)}><Card className="flex items-center justify-between gap-2 px-3 py-2"><div className="min-w-0 flex-1">- <div className="text-display text-xs tracking-wider text-text-tertiary">- model- </div>+ <div className="text-display text-xs tracking-wider text-text-tertiary">model</div><Buttonghostsize="sm"onClick={() => setModelOpen(true)}className={cn(- "max-w-full min-w-0 px-0 py-0",- "self-start normal-case tracking-normal text-sm font-medium",- "hover:underline disabled:no-underline",+ 'max-w-full min-w-0 px-0 py-0',+ 'self-start normal-case tracking-normal text-sm font-medium',+ 'hover:underline disabled:no-underline')}- title={modelName === "—" ? "switch model" : modelName}+ title={modelName === '—' ? 'switch model' : modelName}><span className="flex min-w-0 max-w-full items-center gap-1"><span className="truncate">{modelLabel}</span>@@ -496,9 +405,9 @@ export function ChatSidebar({currentModel={modelName}profile={profile}refreshKey={modelRefreshKey}- onChanged={(effort) =>+ onChanged={effort =>setModelNotice(- `Reasoning effort set to ${effort}. Run /new or refresh the page to apply it to this chat.`,+ `Reasoning effort set to ${effort}. Run /new or refresh the page to apply it to this chat.`)}/>@@ -509,9 +418,7 @@ export function ChatSidebar({<Card className="flex items-start gap-2 border-warning/40 bg-warning/5 px-3 py-2 text-xs"><AlertCircle className="mt-0.5 h-3.5 w-3.5 shrink-0 text-warning" />- <div className="wrap-break-word min-w-0 flex-1 text-text-secondary">- {modelNotice}- </div>+ <div className="wrap-break-word min-w-0 flex-1 text-text-secondary">{modelNotice}</div></Card>)}@@ -523,13 +430,7 @@ export function ChatSidebar({<div className="wrap-break-word text-destructive">{banner}</div>{error && (- <Button- size="sm"- outlined- className="mt-1"- onClick={reconnect}- prefix={<RefreshCw />}- >+ <Button size="sm" outlined className="mt-1" onClick={reconnect} prefix={<RefreshCw />}>reconnect events feed</Button>)}@@ -545,29 +446,29 @@ export function ChatSidebar({loader={() => api.getModelOptions(profile)}alwaysGlobalonApply={async ({ provider, model, confirmExpensiveModel }) => {- setModelNotice(null);- setPendingReloadModel(null);+ setModelNotice(null)+ setPendingReloadModel(null)const result = await api.setModelAssignment({confirm_expensive_model: confirmExpensiveModel,- scope: "main",+ scope: 'main',provider,- model,+ model},- profile,- );+ profile+ )// confirm_required => the dialog shows the expensive-model prompt// and calls back; don't announce until the user confirms.if (!result.confirm_required) {- refreshEffectiveModel();+ refreshEffectiveModel()// Ask before reloading: applying the model starts a fresh chat.- setPendingReloadModel(model.split("/").slice(-1)[0]);+ setPendingReloadModel(model.split('/').slice(-1)[0])}- return result;+ return result}}onClose={() => {- setModelOpen(false);- refreshEffectiveModel();+ setModelOpen(false)+ refreshEffectiveModel()}}/>)}@@ -575,13 +476,11 @@ export function ChatSidebar({<ModelReloadConfirmmodel={pendingReloadModel}onCancel={() => {- const m = pendingReloadModel;- setPendingReloadModel(null);- setModelNotice(- `Model set to ${m}. Run /new or refresh the page to apply it to this chat.`,- );+ const m = pendingReloadModel+ setPendingReloadModel(null)+ setModelNotice(`Model set to ${m}. Run /new or refresh the page to apply it to this chat.`)}}/></aside>- );+ )}diff --git a/web/src/lib/events-reconnect.ts b/web/src/lib/events-reconnect.tsindex 6babf3f2fd3..4714b357d87 100644--- a/web/src/lib/events-reconnect.ts+++ b/web/src/lib/events-reconnect.ts@@ -6,31 +6,32 @@* decision so both can be unit-tested without a fake WebSocket.*/-export const EVENTS_RECONNECT_BASE_MS = 1_000;-export const EVENTS_RECONNECT_MAX_MS = 30_000;-export const EVENTS_MAX_RECONNECT_ATTEMPTS = 15;+import { reconnectBackoffDelayMs } from '@hermes/shared'++export const EVENTS_RECONNECT_BASE_MS = 1_000+export const EVENTS_RECONNECT_MAX_MS = 30_000+export const EVENTS_MAX_RECONNECT_ATTEMPTS = 15/** Bound ticket minting plus the WebSocket opening handshake. */-export const EVENTS_CONNECT_TIMEOUT_MS = 15_000;+export const EVENTS_CONNECT_TIMEOUT_MS = 15_000/** Normal closure — the server said goodbye, don't chase it. */-const WS_CLOSE_NORMAL = 1000;+const WS_CLOSE_NORMAL = 1000/** Ticket rejected / forbidden: retrying just burns tickets, user must reload. */-const WS_CLOSE_AUTH_CODES = new Set([4401, 4403]);+const WS_CLOSE_AUTH_CODES = new Set([4401, 4403])/**- * Exponential backoff, 1s → 2s → 4s → … → 30s cap.+ * Exponential backoff, 1s → 2s → 4s → … → 30s cap. Deterministic (no jitter)+ * because the banner prints the exact delay.** `attempt` is 0-based: attempt 0 is the first retry after the initial* connection dropped.*/export function eventsReconnectDelayMs(attempt: number): number {- const exponent = Math.max(0, Math.trunc(attempt));-- // 2 ** exponent overflows to Infinity long before it matters; Math.min- // still clamps correctly, but guard anyway so the delay stays a number.- const raw = EVENTS_RECONNECT_BASE_MS * 2 ** Math.min(exponent, 32);-- return Math.min(raw, EVENTS_RECONNECT_MAX_MS);+ return reconnectBackoffDelayMs(attempt, {+ baseDelayMs: EVENTS_RECONNECT_BASE_MS,+ capMs: EVENTS_RECONNECT_MAX_MS,+ jitter: false+ })}/**@@ -42,32 +43,31 @@ export function eventsReconnectDelayMs(attempt: number): number {*/export function shouldRetryEventsClose(code: number | undefined): boolean {if (code === undefined) {- return true;+ return true}- return code !== WS_CLOSE_NORMAL && !WS_CLOSE_AUTH_CODES.has(code);+ return code !== WS_CLOSE_NORMAL && !WS_CLOSE_AUTH_CODES.has(code)}export function isEventsAuthRejection(code: number | undefined): boolean {- return code !== undefined && WS_CLOSE_AUTH_CODES.has(code);+ return code !== undefined && WS_CLOSE_AUTH_CODES.has(code)}// The sidebar's banner is shared with `info.credential_warning` and with the// JSON-RPC sidecar's errors, so the events socket may only clear a message it// wrote itself. Everything this module can put in the banner is listed here.-export const EVENTS_DISCONNECTED_MESSAGE =- "events feed disconnected — the chat title may not update";+export const EVENTS_DISCONNECTED_MESSAGE = 'events feed disconnected — the chat title may not update'export function eventsReconnectingMessage(delayMs: number): string {- return `events feed disconnected — reconnecting in ${Math.round(delayMs / 1000)}s…`;+ return `events feed disconnected — reconnecting in ${Math.round(delayMs / 1000)}s…`}export function eventsRejectedMessage(code: number): string {- return `events feed rejected (${code}) — reload the page`;+ return `events feed rejected (${code}) — reload the page`}export function eventsGaveUpMessage(): string {- return `events feed disconnected — gave up after ${EVENTS_MAX_RECONNECT_ATTEMPTS} attempts, reload the page`;+ return `events feed disconnected — gave up after ${EVENTS_MAX_RECONNECT_ATTEMPTS} attempts, reload the page`}/**@@ -77,8 +77,8 @@ export function eventsGaveUpMessage(): string {*/export function isEventsFeedMessage(message: string | null): boolean {if (!message) {- return false;+ return false}- return message.startsWith("events feed ");+ return message.startsWith('events feed ')}diff --git a/web/src/lib/eventsFeedClient.ts b/web/src/lib/eventsFeedClient.tsnew file mode 100644index 00000000000..22a1be3ba6e--- /dev/null+++ b/web/src/lib/eventsFeedClient.ts@@ -0,0 +1,93 @@+/**+ * `/api/events` subscriber for the dashboard chat sidebar.+ *+ * A notification-only view of the shared JSON-RPC client: the server never+ * answers requests on this socket (subscribers don't speak), so replay is+ * off and `request()` is not part of the surface. One instance lives for+ * the component's whole life — the owner calls `connect()` again after a+ * drop instead of building a new client — so the shared per-generation+ * guards (stale-socket close, connect timeout, `error`+`close` collapsed+ * into one `closed` transition) apply to every attempt.+ */++import { JsonRpcGatewayClient, type ConnectionState, type GatewayEvent, type GatewayEventName } from '@hermes/shared'++import { buildWsUrl } from '@/lib/api'+import { maybeReloadForLoopbackWsAuthFailure } from '@/lib/dashboard-auth-reload'+import { EVENTS_CONNECT_TIMEOUT_MS } from '@/lib/events-reconnect'++export type { ConnectionState, GatewayEvent, GatewayEventName }++type CloseHandler = (code: number | undefined) => void++export class EventsFeedClient extends JsonRpcGatewayClient {+ private readonly closeHandlers = new Set<CloseHandler>()+ private closeCode: number | null | undefined = undefined++ constructor() {+ super({+ closedErrorMessage: 'events feed closed',+ connectErrorMessage: 'events feed connection failed',+ connectTimeoutMs: EVENTS_CONNECT_TIMEOUT_MS,+ heartbeatDeadlineMs: 0,+ heartbeatIntervalMs: 0,+ onSocketClose: event => {+ this.closeCode = event.code+ // Loopback + stale token: the page reloads; nobody should retry.+ return maybeReloadForLoopbackWsAuthFailure(event.code)+ },+ replay: false,+ requestIdPrefix: 'e'+ })+ this.onState(state => {+ if (state === 'closed' || state === 'error') {+ const code = this.closeCode ?? undefined+ this.closeCode = undefined+ for (const handler of this.closeHandlers) {+ handler(code)+ }+ }+ })+ }++ /** Close code of the most recent drop; `null` before any socket was dialed. */+ get lastCloseCode(): number | null | undefined {+ return this.closeCode+ }++ /**+ * Called once per generation that ends without opening or after opening:+ * `code` is the WebSocket close code, or `undefined` when the handshake+ * timed out / errored without a close frame.+ */+ onClose(handler: CloseHandler): () => void {+ this.closeHandlers.add(handler)+ return () => this.closeHandlers.delete(handler)+ }++ /** Mint a fresh single-use ticket and dial `/api/events` for `channel`. */+ async connect(channel: string): Promise<void> {+ this.closeCode = null+ // Cover ticket minting with the same deadline as the handshake: a stalled+ // pre-socket request otherwise emits no close event and strands the retry+ // loop at its last "reconnecting in …" banner.+ const url = await withTimeout(buildWsUrl('/api/events', { channel }), EVENTS_CONNECT_TIMEOUT_MS)+ await super.connect(url)+ }+}++function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {+ return new Promise<T>((resolve, reject) => {+ const timer = setTimeout(() => reject(new Error('events feed ticket request timed out')), ms)+ promise.then(+ value => {+ clearTimeout(timer)+ resolve(value)+ },+ (error: unknown) => {+ clearTimeout(timer)+ reject(error instanceof Error ? error : new Error(String(error)))+ }+ )+ })+}diff --git a/web/src/lib/pty-reconnect.test.ts b/web/src/lib/pty-reconnect.test.tsindex 48543a65402..4c4b6fd4903 100644--- a/web/src/lib/pty-reconnect.test.ts+++ b/web/src/lib/pty-reconnect.test.ts@@ -1,123 +1,137 @@-import { describe, expect, it } from "vitest";+import { describe, expect, it } from 'vitest'import {+ PTY_RECONNECT_BASE_MS,+ PTY_RECONNECT_MAX_ATTEMPTS,+ PTY_RECONNECT_MAX_MS,+ ptyReconnectDelayMs,shouldBlockPtyInput,- shouldReconnectPtyOnPageResume,-} from "./pty-reconnect";+ shouldReconnectPtyOnPageResume+} from './pty-reconnect'-describe("shouldReconnectPtyOnPageResume", () => {- it("reconnects a missing socket when the active page becomes visible", () => {+describe('shouldReconnectPtyOnPageResume', () => {+ it('reconnects a missing socket when the active page becomes visible', () => {expect(shouldReconnectPtyOnPageResume({isActive: true,- visibilityState: "visible",+ visibilityState: 'visible',online: true,socketReadyState: null,- ptyState: "reconnecting",- }),- ).toBe(true);- });+ ptyState: 'reconnecting'+ })+ ).toBe(true)+ })- it("reconnects closed or closing sockets on visible resume", () => {+ it('reconnects closed or closing sockets on visible resume', () => {for (const socketReadyState of [2, 3]) {expect(shouldReconnectPtyOnPageResume({isActive: true,- visibilityState: "visible",+ visibilityState: 'visible',online: true,socketReadyState,- ptyState: "reconnecting",- }),- ).toBe(true);+ ptyState: 'reconnecting'+ })+ ).toBe(true)}- });+ })- it("does not reconnect an open socket on visible resume", () => {+ it('does not reconnect an open socket on visible resume', () => {expect(shouldReconnectPtyOnPageResume({isActive: true,- visibilityState: "visible",+ visibilityState: 'visible',online: true,socketReadyState: 1,- ptyState: "open",- }),- ).toBe(false);- });+ ptyState: 'open'+ })+ ).toBe(false)+ })- it("reconnects a still-connecting socket when the page is already in reconnecting state", () => {+ it('reconnects a still-connecting socket when the page is already in reconnecting state', () => {expect(shouldReconnectPtyOnPageResume({isActive: true,- visibilityState: "visible",+ visibilityState: 'visible',online: true,socketReadyState: 0,- ptyState: "reconnecting",- }),- ).toBe(true);- });+ ptyState: 'reconnecting'+ })+ ).toBe(true)+ })- it("does not reconnect while the page is hidden", () => {+ it('does not reconnect while the page is hidden', () => {expect(shouldReconnectPtyOnPageResume({isActive: true,- visibilityState: "hidden",+ visibilityState: 'hidden',online: true,socketReadyState: 3,- ptyState: "reconnecting",- }),- ).toBe(false);- });+ ptyState: 'reconnecting'+ })+ ).toBe(false)+ })- it("defers reconnect while offline", () => {+ it('defers reconnect while offline', () => {expect(shouldReconnectPtyOnPageResume({isActive: true,- visibilityState: "visible",+ visibilityState: 'visible',online: false,socketReadyState: 3,- ptyState: "reconnecting",- }),- ).toBe(false);- });+ ptyState: 'reconnecting'+ })+ ).toBe(false)+ })- it("does not fire a redundant reconnect while a connect is in flight (wsRef not yet assigned)", () => {+ it('does not fire a redundant reconnect while a connect is in flight (wsRef not yet assigned)', () => {// The async socket-open IIFE has begun but not yet assigned wsRef, so// socketReadyState reads null. Without the connectInFlight guard this// would return true and double-connect.expect(shouldReconnectPtyOnPageResume({isActive: true,- visibilityState: "visible",+ visibilityState: 'visible',online: true,socketReadyState: null,- ptyState: "connecting",- connectInFlight: true,- }),- ).toBe(false);- });+ ptyState: 'connecting',+ connectInFlight: true+ })+ ).toBe(false)+ })- it("still reconnects an in-flight connect when the page already believes it is closed", () => {+ it('still reconnects an in-flight connect when the page already believes it is closed', () => {// A stuck attempt the user is actively trying to recover (manual reconnect// or a closed state) must not be suppressed by the in-flight guard.expect(shouldReconnectPtyOnPageResume({isActive: true,- visibilityState: "visible",+ visibilityState: 'visible',online: true,socketReadyState: null,- ptyState: "closed",- connectInFlight: true,- }),- ).toBe(true);- });-});+ ptyState: 'closed',+ connectInFlight: true+ })+ ).toBe(true)+ })+})-describe("shouldBlockPtyInput", () => {- it("allows input only while the PTY socket is open", () => {- expect(shouldBlockPtyInput("open")).toBe(false);- expect(shouldBlockPtyInput("connecting")).toBe(true);- expect(shouldBlockPtyInput("reconnecting")).toBe(true);- expect(shouldBlockPtyInput("closed")).toBe(true);- expect(shouldBlockPtyInput("ended")).toBe(true);- });-});+describe('shouldBlockPtyInput', () => {+ it('allows input only while the PTY socket is open', () => {+ expect(shouldBlockPtyInput('open')).toBe(false)+ expect(shouldBlockPtyInput('connecting')).toBe(true)+ expect(shouldBlockPtyInput('reconnecting')).toBe(true)+ expect(shouldBlockPtyInput('closed')).toBe(true)+ expect(shouldBlockPtyInput('ended')).toBe(true)+ })+})++describe('ptyReconnectDelayMs', () => {+ it('doubles from the base on each attempt and clamps at the cap', () => {+ expect(Array.from({ length: PTY_RECONNECT_MAX_ATTEMPTS }, (_, i) => ptyReconnectDelayMs(i + 1))).toEqual([+ 250, 500, 1000, 2000, 3000+ ])+ expect(ptyReconnectDelayMs(1)).toBe(PTY_RECONNECT_BASE_MS)+ expect(ptyReconnectDelayMs(99)).toBe(PTY_RECONNECT_MAX_MS)+ })+})diff --git a/web/src/lib/pty-reconnect.ts b/web/src/lib/pty-reconnect.tsindex 7917bba4c7a..71f34961214 100644--- a/web/src/lib/pty-reconnect.ts+++ b/web/src/lib/pty-reconnect.ts@@ -1,29 +1,41 @@-export type PtyConnectionState =- | "connecting"- | "open"- | "reconnecting"- | "closed"- | "ended";+import { reconnectBackoffDelayMs } from '@hermes/shared'-export const PTY_RECONNECT_INPUT_MESSAGE =- "Chat is reconnecting. Input will resume when connected.";+export type PtyConnectionState = 'connecting' | 'open' | 'reconnecting' | 'closed' | 'ended'++export const PTY_RECONNECT_INPUT_MESSAGE = 'Chat is reconnecting. Input will resume when connected.'// Minimum gap (ms) between page-resume-triggered reconnect attempts, so a// burst of visibilitychange/pageshow/focus/online events on tab-return// collapses into a single reconnect.-export const PTY_RESUME_RECONNECT_THROTTLE_MS = 1000;+export const PTY_RESUME_RECONNECT_THROTTLE_MS = 1000// If a socket sits in WS_CONNECTING past this budget it is treated as wedged// (e.g. a half-open mobile socket after a radio handoff — the NS-591 case)// and force-closed so `onclose` → scheduleReconnect can recover it.-export const PTY_CONNECTING_TIMEOUT_MS = 8000;+export const PTY_CONNECTING_TIMEOUT_MS = 8000// The same budget for the phase *before* the socket exists: in gated mode a// connect first awaits a fresh single-use ticket. That request produces no// WebSocket, so a rejection or a hang is invisible to both `onclose` and the// CONNECTING timer above — the tab would sit on "connecting" forever with no// retry. Bound it so the failure routes into the ordinary backoff instead.-export const PTY_TICKET_TIMEOUT_MS = 8000;+export const PTY_TICKET_TIMEOUT_MS = 8000++// Short ladder, tight cap: the PTY is the user's live terminal, so a transient+// drop must come back fast and a dead backend must stop chasing quickly (the+// banner offers a manual Reconnect). Deterministic so the banner can print it.+export const PTY_RECONNECT_BASE_MS = 250+export const PTY_RECONNECT_MAX_MS = 3000+export const PTY_RECONNECT_MAX_ATTEMPTS = 5++/** Delay before PTY reconnect `attempt` (1-based: ChatPage bumps its counter before scheduling). */+export function ptyReconnectDelayMs(attempt: number): number {+ return reconnectBackoffDelayMs(attempt - 1, {+ baseDelayMs: PTY_RECONNECT_BASE_MS,+ capMs: PTY_RECONNECT_MAX_MS,+ jitter: false+ })+}// How long after a resumed socket opens we keep suppressing ANSI erase codes// (`ESC[K` / `ESC[X`) from the PTY stream. Ink's two-pass virtual scroll emits@@ -33,21 +45,21 @@ export const PTY_TICKET_TIMEOUT_MS = 8000;// session takes ~10-20s, so this is deliberately generous — over-running the// window only costs a few stale cells on a buffer that is about to be// repainted, while under-running it re-opens the blank-viewport bug.-export const PTY_RESUME_SANITIZE_WINDOW_MS = 30000;+export const PTY_RESUME_SANITIZE_WINDOW_MS = 30000export interface PtyResumeReconnectInput {- isActive: boolean;- visibilityState?: DocumentVisibilityState;- online: boolean;- socketReadyState?: number | null;- ptyState: PtyConnectionState;- connectInFlight?: boolean;+ isActive: boolean+ visibilityState?: DocumentVisibilityState+ online: boolean+ socketReadyState?: number | null+ ptyState: PtyConnectionState+ connectInFlight?: boolean}-const WS_CONNECTING = 0;-const WS_OPEN = 1;-const WS_CLOSING = 2;-const WS_CLOSED = 3;+const WS_CONNECTING = 0+const WS_OPEN = 1+const WS_CLOSING = 2+const WS_CLOSED = 3export function shouldReconnectPtyOnPageResume({isActive,@@ -55,39 +67,35 @@ export function shouldReconnectPtyOnPageResume({online,socketReadyState,ptyState,- connectInFlight,+ connectInFlight}: PtyResumeReconnectInput): boolean {- if (!isActive || !online || visibilityState === "hidden") {- return false;+ if (!isActive || !online || visibilityState === 'hidden') {+ return false}- if (ptyState === "ended") {- return false;+ if (ptyState === 'ended') {+ return false}if (socketReadyState === WS_OPEN) {- return false;+ return false}// A connect is mid-flight (the async socket-open IIFE is awaiting its// ticket URL and hasn't assigned wsRef yet, or the socket is still// CONNECTING on a non-stuck attempt). Don't fire a redundant reconnect// into that window unless the tab already believes it is reconnecting or// closed and needs a fresh attempt.- if (- (connectInFlight || socketReadyState === WS_CONNECTING) &&- ptyState !== "reconnecting" &&- ptyState !== "closed"- ) {- return false;+ if ((connectInFlight || socketReadyState === WS_CONNECTING) && ptyState !== 'reconnecting' && ptyState !== 'closed') {+ return false}return (socketReadyState === null ||socketReadyState === undefined ||socketReadyState === WS_CLOSING ||socketReadyState === WS_CLOSED ||- ptyState === "reconnecting" ||- ptyState === "closed"- );+ ptyState === 'reconnecting' ||+ ptyState === 'closed'+ )}export function shouldBlockPtyInput(ptyState: PtyConnectionState): boolean {- return ptyState !== "open";+ return ptyState !== 'open'}diff --git a/web/src/pages/ChatPage.tsx b/web/src/pages/ChatPage.tsxindex 2f21ae0dfae..5229e12094a 100644--- a/web/src/pages/ChatPage.tsx+++ b/web/src/pages/ChatPage.tsx@@ -44,10 +44,12 @@ import { PtyResumeSanitizer } from "@/lib/pty-resume-sanitizer";import {PTY_CONNECTING_TIMEOUT_MS,PTY_RECONNECT_INPUT_MESSAGE,+ PTY_RECONNECT_MAX_ATTEMPTS,PTY_RESUME_RECONNECT_THROTTLE_MS,PTY_RESUME_SANITIZE_WINDOW_MS,PTY_TICKET_TIMEOUT_MS,type PtyConnectionState,+ ptyReconnectDelayMs,shouldBlockPtyInput,shouldReconnectPtyOnPageResume,} from "@/lib/pty-reconnect";@@ -1151,9 +1153,9 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {if (reconnectTimerRef.current) {return;}- const attempt = Math.min(reconnectAttemptRef.current + 1, 5);+ const attempt = Math.min(reconnectAttemptRef.current + 1, PTY_RECONNECT_MAX_ATTEMPTS);reconnectAttemptRef.current = attempt;- const delayMs = Math.min(250 * 2 ** (attempt - 1), 3000);+ const delayMs = ptyReconnectDelayMs(attempt);setBanner(null);setLastCloseCode(code);setPtyState("reconnecting");