The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(web): make the sidecar redial budget survive client rebuilds (#95951)
liuhao1024121e557ed8be
diff --git a/web/src/components/ChatSidebar.test.tsx b/web/src/components/ChatSidebar.test.tsxindex 62c6ac46676..48d404aa5c8 100644--- a/web/src/components/ChatSidebar.test.tsx+++ b/web/src/components/ChatSidebar.test.tsx@@ -265,9 +265,95 @@ describe('ChatSidebar event socket reconnect', () => {expect(apiMocks.buildWsUrl).toHaveBeenCalledTimes(3)})+ it("surfaces a gave-up banner when the sidecar redial budget is exhausted (#95951)", async () => {+ // The file-wide onState mock immediately reports "open" to every new+ // subscription — that would reset the sidecar's redial counter after+ // every rebuild and the budget would never exhaust. Collect the+ // handlers and drive the state sequence ourselves.+ const originalImpl = gatewayMocks.onState.getMockImplementation();+ const stateHandlers: Array<(s: string) => void> = [];+ gatewayMocks.onState.mockImplementation((handler: (s: string) => void) => {+ stateHandlers.push(handler);+ return () => undefined;+ });++ try {+ const { ChatSidebar } = await import("./ChatSidebar");+ await render(<ChatSidebar channel="chat-1" />);+ expect(stateHandlers.length).toBeGreaterThanOrEqual(2);++ // Exhaust the budget: each failed attempt re-runs the socket effect+ // (new handler subscribed), so drive the LATEST subscription each+ // round and advance past that round's backoff (250 * 2^n, capped 3s).+ for (let round = 0; round < 5; round += 1) {+ const handler = stateHandlers[stateHandlers.length - 1];+ await act(async () => {+ handler("error");+ });+ await advance(4_000);+ expect(gatewayMocks.connect).toHaveBeenCalledTimes(2 + round);+ }++ // One more drop with the budget spent: no further connect is+ // scheduled, and the banner reports give-up.+ const finalHandler = stateHandlers[stateHandlers.length - 1];+ await act(async () => {+ finalHandler("closed");+ });+ await advance(4_000);+ expect(gatewayMocks.connect).toHaveBeenCalledTimes(6);+ expect(container?.textContent ?? "").toContain("gave up after 5 attempts");+ } finally {+ gatewayMocks.onState.mockImplementation(originalImpl!);+ }+ });+it('times out a stalled URL request and retries', async () => {let resolveStalledRequest!: (url: string) => voidawait renderSidebar()+ it("surfaces a gave-up banner when the sidecar redial budget is exhausted (#95951)", async () => {+ // The file-wide onState mock immediately reports "open" to every new+ // subscription — that would reset the sidecar's redial counter after+ // every rebuild and the budget would never exhaust. Collect the+ // handlers and drive the state sequence ourselves.+ const originalImpl = gatewayMocks.onState.getMockImplementation();+ const stateHandlers: Array<(s: string) => void> = [];+ gatewayMocks.onState.mockImplementation((handler: (s: string) => void) => {+ stateHandlers.push(handler);+ return () => undefined;+ });++ try {+ const { ChatSidebar } = await import("./ChatSidebar");+ await render(<ChatSidebar channel="chat-1" />);+ expect(stateHandlers.length).toBeGreaterThanOrEqual(2);++ // Exhaust the budget: each failed attempt rebuilds the client (new+ // handler subscribed), so drive the LATEST subscription each round and+ // advance past that round's backoff (250 * 2^n, capped at 3s).+ for (let round = 0; round < 5; round += 1) {+ const handler = stateHandlers[stateHandlers.length - 1];+ await act(async () => {+ handler("error");+ });+ await advance(4_000);+ expect(gatewayMocks.connect).toHaveBeenCalledTimes(2 + round);+ }++ // One more drop with the budget spent: no further connect is+ // scheduled, and the banner reports give-up.+ const finalHandler = stateHandlers[stateHandlers.length - 1];+ await act(async () => {+ finalHandler("closed");+ });+ await advance(4_000);+ expect(gatewayMocks.connect).toHaveBeenCalledTimes(6);+ expect(container?.textContent ?? "").toContain("gave up after 5 attempts");+ } finally {+ gatewayMocks.onState.mockImplementation(originalImpl!);+ }+ });+apiMocks.buildWsUrl.mockImplementationOnce(() =>diff --git a/web/src/components/ChatSidebar.tsx b/web/src/components/ChatSidebar.tsxindex 155a766bde1..4c8d1352c62 100644--- a/web/src/components/ChatSidebar.tsx+++ b/web/src/components/ChatSidebar.tsx@@ -67,6 +67,13 @@ interface SessionInfo {// path, mirroring the events feed's give-up contract.const SIDE_CAR_MAX_RECONNECT_ATTEMPTS = 5;+// Surfaced once when the redial budget is exhausted. Only this module may+// clear it (on the next successful open), matching how the events feed+// owns its own banner messages.+const SIDE_CAR_GAVE_UP_MESSAGE =+ "gateway sidecar disconnected — gave up after " ++ `${SIDE_CAR_MAX_RECONNECT_ATTEMPTS} attempts, use Reconnect`;+const STATE_LABEL: Record<ConnectionState, string> = {idle: 'idle',connecting: 'connecting',@@ -123,6 +130,12 @@ export function ChatSidebar({const [version, setVersion] = useState(0)const gw = useMemo(() => new GatewayClient(), [])const feed = useMemo(() => new EventsFeedClient(), [])+ // Sidecar auto-redial budget (#95951). A ref, NOT effect state: the counter+ // must survive the [gw, version] effect re-runs a redial triggers, or the+ // budget resets every attempt and never exhausts.+ // Reset on a successful open and on scope switches.+ const sidecarRedialAttemptRef = useRef(0)+ const sidecarGaveUpRef = useRef(false)const [state, setState] = useState<ConnectionState>('idle')const [info, setInfo] = useState<SessionInfo>({})@@ -180,6 +193,9 @@ export function ChatSidebar({if (prevScopeKey.current === scopeKey) returnprevScopeKey.current = scopeKeysetError(null)+ // Fresh scope, fresh sidecar redial budget (#95951).+ sidecarRedialAttemptRef.current = 0+ sidecarGaveUpRef.current = falsesetVersion(v => v + 1)}, [scopeKey])@@ -219,10 +235,15 @@ export function ChatSidebar({// 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;+ sidecarRedialAttemptRef.current = 0;+ if (sidecarGaveUpRef.current) {+ sidecarGaveUpRef.current = false;+ setError((current) =>+ current === SIDE_CAR_GAVE_UP_MESSAGE ? null : current,+ );+ }return;}if (s !== "closed" && s !== "error") {@@ -231,11 +252,23 @@ export function ChatSidebar({if (cancelled || redialTimer) {return;}- if (redialAttempt >= SIDE_CAR_MAX_RECONNECT_ATTEMPTS) {+ // The attempt counter lives in a ref: each redial rebuilds the client+ // and re-runs this effect, so a closure-local counter would reset and+ // the budget would never exhaust (#95951).+ if (sidecarRedialAttemptRef.current >= SIDE_CAR_MAX_RECONNECT_ATTEMPTS) {+ // Mirror the events feed's give-up contract: say so once, then the+ // manual Reconnect affordance stays the only path. Cleared again if+ // a later connection does open (manual reconnect followed by a+ // within-budget drop).+ if (!sidecarGaveUpRef.current) {+ sidecarGaveUpRef.current = true;+ setError((current) => current ?? SIDE_CAR_GAVE_UP_MESSAGE);+ }return;}- const delayMs = Math.min(250 * 2 ** redialAttempt, 3000);- redialAttempt += 1;+ const attempt = sidecarRedialAttemptRef.current;+ sidecarRedialAttemptRef.current += 1;+ const delayMs = Math.min(250 * 2 ** attempt, 3000);redialTimer = setTimeout(() => {redialTimer = null;if (!cancelled) {