Skip to content

The agent that grows with you

fix(web): the events feed's reconnect ladder also waits for a stable open

Austin Pickettf16cbcde551d
diff --git a/web/src/components/ChatSidebar.test.tsx b/web/src/components/ChatSidebar.test.tsx
index 6cc4f828763..c2016ab9c46 100644
--- a/web/src/components/ChatSidebar.test.tsx
+++ b/web/src/components/ChatSidebar.test.tsx
@@ -3,7 +3,7 @@ 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, EVENTS_MAX_RECONNECT_ATTEMPTS } from '@/lib/events-reconnect'
const apiMocks = vi.hoisted(() => ({
buildWsUrl: vi.fn(async () => 'ws://localhost/api/events?channel=chat-1'),
@@ -411,7 +411,7 @@ describe('ChatSidebar event socket reconnect', () => {
expect(FakeWebSocket.instances).toHaveLength(2)
})
- it('resets the backoff after a successful reconnect', async () => {
+ it('resets the backoff after a connection stays open past the grace window', async () => {
await renderSidebar()
await act(async () => {
@@ -420,15 +420,55 @@ describe('ChatSidebar event socket reconnect', () => {
await advance(1_000)
expect(FakeWebSocket.instances).toHaveLength(2)
- // Reconnected — the next drop should start from 1s again, not 2s.
+ // Reconnected and stable — the next drop should start from 1s again, not 2s.
await act(async () => {
FakeWebSocket.instances[1].emit('open', {})
+ })
+ await advance(10_000)
+ await act(async () => {
FakeWebSocket.instances[1].emit('close', { code: 1006 })
})
await advance(1_000)
expect(FakeWebSocket.instances).toHaveLength(3)
})
+ it('does not reset the backoff when a connection opens only briefly', async () => {
+ await renderSidebar()
+
+ await act(async () => {
+ FakeWebSocket.instances[0].emit('close', { code: 1006 })
+ })
+ await advance(1_000)
+ expect(FakeWebSocket.instances).toHaveLength(2)
+
+ // Open → immediate close is a flap: the ladder continues at 2s, not 1s.
+ await act(async () => {
+ FakeWebSocket.instances[1].emit('open', {})
+ FakeWebSocket.instances[1].emit('close', { code: 1006 })
+ })
+ await advance(1_000)
+ expect(FakeWebSocket.instances).toHaveLength(2)
+ await advance(1_000)
+ expect(FakeWebSocket.instances).toHaveLength(3)
+ })
+
+ it('gives up after the attempt cap even when every socket opens briefly first', async () => {
+ await renderSidebar()
+
+ for (let i = 0; i < 40; i++) {
+ const socket = FakeWebSocket.instances[FakeWebSocket.instances.length - 1]
+ await act(async () => {
+ socket.emit('open', {})
+ socket.emit('close', { code: 1006 })
+ })
+ await advance(30_000)
+ }
+
+ // A flapping socket must not refill the ladder: 15 retries + the initial connection.
+ expect(FakeWebSocket.instances.length).toBeLessThanOrEqual(EVENTS_MAX_RECONNECT_ATTEMPTS + 1)
+ expect(container.textContent).toContain('stopped after')
+ })
+
it('does not retry auth rejections', async () => {
await renderSidebar()
@@ -615,7 +655,8 @@ describe('ChatSidebar event socket reconnect', () => {
FakeWebSocket.instances[1].emit('close', { code: 1006 })
})
expect(vi.getTimerCount()).toBeGreaterThan(0)
- await advance(1_000)
+ // The brief open did not refill the ladder, so this retry is the 2s rung.
+ await advance(2_000)
expect(FakeWebSocket.instances).toHaveLength(3)
await act(async () => root.unmount())
diff --git a/web/src/components/ChatSidebar.tsx b/web/src/components/ChatSidebar.tsx
index 27995d97226..b4069ecf4d5 100644
--- a/web/src/components/ChatSidebar.tsx
+++ b/web/src/components/ChatSidebar.tsx
@@ -67,6 +67,11 @@ interface SessionInfo {
// path, mirroring the events feed's give-up contract.
const SIDE_CAR_MAX_RECONNECT_ATTEMPTS = 5;
+// A socket that opens and dies within this window is a flap, not a recovery:
+// only a connection that stays open this long refills a reconnect budget.
+// Shared by the JSON-RPC sidecar and the events feed (#129393).
+const HEALTHY_OPEN_GRACE_MS = 10_000;
+
// 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.
@@ -269,7 +274,7 @@ export function ChatSidebar({
current === SIDE_CAR_GAVE_UP_MESSAGE ? null : current,
);
}
- }, 10_000);
+ }, HEALTHY_OPEN_GRACE_MS);
return;
}
if (s !== "closed" && s !== "error") {
@@ -362,6 +367,7 @@ export function ChatSidebar({
}
let unmounting = false
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
+ let healthyOpenTimer: ReturnType<typeof setTimeout> | null = null
let attempt = 0
// The banner is shared with `info.credential_warning` and the JSON-RPC
@@ -380,6 +386,10 @@ export function ChatSidebar({
if (unmounting || reconnectTimer) {
return
}
+ if (healthyOpenTimer) {
+ clearTimeout(healthyOpenTimer)
+ healthyOpenTimer = null
+ }
if (attempt >= EVENTS_MAX_RECONNECT_ATTEMPTS) {
surface(eventsGaveUpMessage())
return
@@ -431,8 +441,16 @@ export function ChatSidebar({
const offState = feed.onState(state => {
if (state === 'open') {
- attempt = 0
clearEventsBanner()
+ // Same rule as the sidecar: an open that dies within the grace window
+ // is a flap, not a recovery, so it must not refill the ladder.
+ if (healthyOpenTimer) {
+ clearTimeout(healthyOpenTimer)
+ }
+ healthyOpenTimer = setTimeout(() => {
+ healthyOpenTimer = null
+ attempt = 0
+ }, HEALTHY_OPEN_GRACE_MS)
}
})
@@ -454,6 +472,10 @@ export function ChatSidebar({
clearTimeout(reconnectTimer)
reconnectTimer = null
}
+ if (healthyOpenTimer) {
+ clearTimeout(healthyOpenTimer)
+ healthyOpenTimer = null
+ }
offClose()
offState()
offSessionInfo()