The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(web): redial after clean 1012 service-restart closes (#95951)
liuhao1024d04c351dffbf
diff --git a/web/src/components/ChatSidebar.test.tsx b/web/src/components/ChatSidebar.test.tsxindex 997754a4c2f..62c6ac46676 100644--- a/web/src/components/ChatSidebar.test.tsx+++ b/web/src/components/ChatSidebar.test.tsx@@ -183,6 +183,30 @@ describe('ChatSidebar event socket', () => {expect(reloadMocks.maybeReloadForLoopbackWsAuthFailure).toHaveBeenCalledWith(4401)})++ it("auto-redials the JSON-RPC sidecar after a transient close (#95951)", async () => {+ const { ChatSidebar } = await import("./ChatSidebar");++ await render(<ChatSidebar channel="chat-1" />);++ // The sidecar subscribes state handlers via onState; the first+ // subscription's mock call receives the handler we can drive.+ await vi.waitFor(() => expect(gatewayMocks.onState).toHaveBeenCalled());+ expect(gatewayMocks.connect).toHaveBeenCalledTimes(1);++ // A service-restart close reports 'closed'. The connection owner+ // schedules a version bump after the 250ms first-attempt backoff,+ // which rebuilds the client and dials again. (onState call [0] is the+ // state badge subscription; the redial owner is call [1].)+ const stateHandler = gatewayMocks.onState.mock+ .calls[1][0] as (s: string) => void;+ act(() => stateHandler("closed"));++ await vi.waitFor(+ () => expect(gatewayMocks.connect).toHaveBeenCalledTimes(2),+ { timeout: 3000 },+ );+ });})describe('ChatSidebar event socket reconnect', () => {diff --git a/web/src/components/ChatSidebar.tsx b/web/src/components/ChatSidebar.tsxindex 6f77ea7f26b..155a766bde1 100644--- a/web/src/components/ChatSidebar.tsx+++ b/web/src/components/ChatSidebar.tsx@@ -62,6 +62,11 @@ interface SessionInfo {title?: string}+// Auto-redial budget for the JSON-RPC sidecar (#95951). After this many+// bounded-backoff attempts the manual Reconnect affordance stays the only+// path, mirroring the events feed's give-up contract.+const SIDE_CAR_MAX_RECONNECT_ATTEMPTS = 5;+const STATE_LABEL: Record<ConnectionState, string> = {idle: 'idle',connecting: 'connecting',@@ -205,6 +210,40 @@ export function ChatSidebar({}})+ // Auto-redial after a transient drop (#95951): a dashboard service+ // restart closes the sidecar's WebSocket with 1012, and GatewayClient+ // deliberately delegates reconnect policy to this connection owner.+ // Bounded exponential backoff — the same shape the PTY pane uses —+ // capped at SIDE_CAR_MAX_RECONNECT_ATTEMPTS; after that the manual+ // Reconnect affordance stays the only path. A successful open resets+ // the counter; unmount or a scope switch (version bump) cancels the+ // pending timer because this effect tears down with the old client.+ let redialTimer: ReturnType<typeof setTimeout> | null = null;+ let redialAttempt = 0;+ const offRedial = gw.onState((s) => {+ if (s === "open") {+ redialAttempt = 0;+ return;+ }+ if (s !== "closed" && s !== "error") {+ return;+ }+ if (cancelled || redialTimer) {+ return;+ }+ if (redialAttempt >= SIDE_CAR_MAX_RECONNECT_ATTEMPTS) {+ return;+ }+ const delayMs = Math.min(250 * 2 ** redialAttempt, 3000);+ redialAttempt += 1;+ redialTimer = setTimeout(() => {+ redialTimer = null;+ if (!cancelled) {+ setVersion((v) => v + 1);+ }+ }, delayMs);+ });+// Create the sidecar session so the gateway surfaces session-scoped// signals (connection state, credential warnings). It's independent of the// PTY pane's session by design. The model picker no longer rides this@@ -227,12 +266,16 @@ export function ChatSidebar({return () => {cancelled = true+ if (redialTimer) {+ clearTimeout(redialTimer)+ redialTimer = null+ }+ offRedial()offState()offSessionInfo()offError()gw.close()}- // `profile` is read from render; scope changes bump `version` → redial.// eslint-disable-next-line react-hooks/exhaustive-deps}, [gw, version])diff --git a/web/src/pages/ChatPage.test.tsx b/web/src/pages/ChatPage.test.tsxindex 203c7ec9f8a..7429b87dce5 100644--- a/web/src/pages/ChatPage.test.tsx+++ b/web/src/pages/ChatPage.test.tsx@@ -480,6 +480,31 @@ describe("ChatPage", () => {}});+ it("redials after a clean 1012 service-restart close (#95951)", async () => {+ const { default: ChatPage } = await import("./ChatPage");++ await render(+ <MemoryRouter initialEntries={["/chat"]}>+ <ChatPage isActive />+ </MemoryRouter>,+ );+ await vi.waitFor(() => expect(FakeWebSocket.instances).toHaveLength(1));++ // 1012 is a CLEAN close (wasClean=true) that is neither 1001 nor 1006 —+ // the old guard fell through to "[session ended]" with no retry. The+ // server is coming back, so the pane must redial: the 250ms first-attempt+ // backoff re-runs the connect effect and opens a fresh socket.+ FakeWebSocket.instances[0].onclose?.({+ code: 1012,+ reason: "",+ wasClean: true,+ });++ await vi.waitFor(() => expect(FakeWebSocket.instances).toHaveLength(2), {+ timeout: 3000,+ });+ });+it("attaches visualViewport keyboard-inset listeners only while the chat tab is active", async () => {// NS-434 follow-up: ChatPage stays mounted (hidden) on every dashboard// route. The keyboard-inset/scroll-pin listeners must only be live whilediff --git a/web/src/pages/ChatPage.tsx b/web/src/pages/ChatPage.tsxindex dafdde133f9..27ec31b15f7 100644--- a/web/src/pages/ChatPage.tsx+++ b/web/src/pages/ChatPage.tsx@@ -1491,10 +1491,17 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {setPtyState("closed");return;}- if (!ev.wasClean || ev.code === 1001 || ev.code === 1006) {- // Transient transport drop (refresh, sleep/wake, signal loss).- // Reconnect with backoff; the same ?attach= token reattaches to- // the still-living PTY, so the conversation continues in place.+ if (+ !ev.wasClean ||+ ev.code === 1001 ||+ ev.code === 1006 ||+ ev.code === 1012 ||+ ev.code === 1013+ ) {+ // Transient transport drop (refresh, sleep/wake, signal loss), or a+ // clean server-side restart signal: 1012 Service Restart / 1013 Try+ // Again Later mean the server is coming back, so redial instead of+ // stranding the pane on "[session ended]" (#95951).scheduleReconnect(ev.code);return;}