Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(ui): stop waking a live workbench event stream into a reconnect (#1593)
Call White6cc78767d1a0
diff --git a/ui/src/context/ApiContext.tsx b/ui/src/context/ApiContext.tsxindex 1b5613fdb..c7c03e7a6 100644--- a/ui/src/context/ApiContext.tsx+++ b/ui/src/context/ApiContext.tsx@@ -2799,6 +2799,31 @@ export const ApiProvider: React.FC<{ children: React.ReactNode }> = ({ childreneventBridgeConnectedRef.current = false;};+ /**+ * The stream's own handshake, or null while events cannot reach handlers. A+ * controller-sourced stream also needs its bridge up: the browser leg being+ * open says nothing about the controller leg behind it.+ */+ const workbenchEventHandshake = () => {+ const connection = eventConnectionRef.current;+ if (!connection) return null;+ if (connection.source === 'controller' && !eventBridgeConnectedRef.current) return null;+ return connection;+ };++ /**+ * Events are flowing end to end right now, so a reconnect would close no gap.+ *+ * Transport liveness is the browser's to own: it errors the stream on network+ * changes and on HTTP/2 ping timeouts, which is what `readyState` reports. The+ * server's own keep-alive is an SSE comment plus it only fires after 15s of+ * silence on the timeout branch, so it is invisible to EventSource and absent+ * from a stream busy delivering events this context filters out -- a+ * last-frame staleness check built on it would call healthy streams dead.+ */+ const isWorkbenchEventStreamLive = () =>+ eventSourceRef.current?.readyState === EventSource.OPEN && workbenchEventHandshake() !== null;+function reconnectWorkbenchEventSource(): void {if (eventHandlersRef.current.size === 0) return;closeActiveWorkbenchEventSource();@@ -2810,6 +2835,7 @@ export const ApiProvider: React.FC<{ children: React.ReactNode }> = ({ childreneventReconnectLoopRef.current = new WorkbenchEventReconnectLoop({reconnect: reconnectWorkbenchEventSource,isVisible: () => document.visibilityState === 'visible',+ isStreamLive: isWorkbenchEventStreamLive,});}return eventReconnectLoopRef.current;@@ -3051,7 +3077,9 @@ export const ApiProvider: React.FC<{ children: React.ReactNode }> = ({ childrenconst wakeWorkbenchEvents = () => {if (eventHandlersRef.current.size === 0 || document.visibilityState !== 'visible') return;- setWorkbenchEventConnectionState('reconnecting');+ // The indicator belongs to whoever opens a stream: openWorkbenchEventSource+ // marks it reconnecting on every attempt. Announcing it here instead made a+ // wake that keeps a live stream flash "reconnecting" over a healthy one.getWorkbenchEventReconnectLoop().wake();};wakeWorkbenchEventsRef.current = wakeWorkbenchEvents;@@ -3988,17 +4016,11 @@ export const ApiProvider: React.FC<{ children: React.ReactNode }> = ({ childrenhandlers.onConnectionState?.(eventConnectionStateRef.current);}});- if (- eventConnectionRef.current &&- (eventConnectionRef.current.source !== 'controller' || eventBridgeConnectedRef.current)- ) {+ if (workbenchEventHandshake()) {queueMicrotask(() => {- if (- eventHandlersRef.current.has(handlers) &&- eventConnectionRef.current &&- (eventConnectionRef.current.source !== 'controller' || eventBridgeConnectedRef.current)- ) {- handlers.onConnected?.(eventConnectionRef.current);+ const connected = workbenchEventHandshake();+ if (eventHandlersRef.current.has(handlers) && connected) {+ handlers.onConnected?.(connected);}});}diff --git a/ui/src/lib/workbenchEventConnection.test.ts b/ui/src/lib/workbenchEventConnection.test.tsindex 832909834..c2bcd67e4 100644--- a/ui/src/lib/workbenchEventConnection.test.ts+++ b/ui/src/lib/workbenchEventConnection.test.ts@@ -13,7 +13,11 @@ describe('WorkbenchEventReconnectLoop', () => {it('retries terminal failures forever with bounded backoff and resets after recovery', () => {vi.useFakeTimers();const reconnect = vi.fn();- const loop = new WorkbenchEventReconnectLoop({ reconnect, isVisible: () => true });+ const loop = new WorkbenchEventReconnectLoop({+ reconnect,+ isVisible: () => true,+ isStreamLive: () => false,+ });for (const delayMs of [1_000, 2_000, 4_000, 8_000, 15_000, 15_000]) {loop.failed();@@ -36,7 +40,11 @@ describe('WorkbenchEventReconnectLoop', () => {vi.useFakeTimers();let visible = true;const reconnect = vi.fn();- const loop = new WorkbenchEventReconnectLoop({ reconnect, isVisible: () => visible });+ const loop = new WorkbenchEventReconnectLoop({+ reconnect,+ isVisible: () => visible,+ isStreamLive: () => false,+ });loop.attemptStarted();vi.advanceTimersByTime(WORKBENCH_EVENT_OPEN_TIMEOUT_MS - 1);@@ -59,4 +67,41 @@ describe('WorkbenchEventReconnectLoop', () => {vi.advanceTimersByTime(60_000);expect(reconnect).toHaveBeenCalledTimes(2);});++ it('leaves a live stream alone on wake while still dropping the backoff', () => {+ vi.useFakeTimers();+ let streamLive = false;+ const reconnect = vi.fn();+ const loop = new WorkbenchEventReconnectLoop({+ reconnect,+ isVisible: () => true,+ isStreamLive: () => streamLive,+ });++ for (const delayMs of [1_000, 2_000, 4_000, 8_000, 15_000]) {+ loop.failed();+ vi.advanceTimersByTime(delayMs);+ }+ expect(reconnect).toHaveBeenCalledTimes(5);+ reconnect.mockClear();++ // A retry is waiting out the ceiling when the stream comes back up.+ loop.failed();+ streamLive = true;+ loop.wake();+ vi.advanceTimersByTime(60_000);+ // A stream that never dropped missed nothing, so waking must not recycle+ // it -- every consumer refetches on reconnect to close a gap that a live+ // stream does not have. The queued retry is cancelled, not deferred.+ expect(reconnect).not.toHaveBeenCalled();++ // Clearing the backoff is the other half of waking: the next real drop+ // retries from the shortest delay instead of resuming at the ceiling.+ streamLive = false;+ loop.failed();+ vi.advanceTimersByTime(999);+ expect(reconnect).not.toHaveBeenCalled();+ vi.advanceTimersByTime(1);+ expect(reconnect).toHaveBeenCalledTimes(1);+ });});diff --git a/ui/src/lib/workbenchEventConnection.ts b/ui/src/lib/workbenchEventConnection.tsindex ff8498ee8..846b66162 100644--- a/ui/src/lib/workbenchEventConnection.ts+++ b/ui/src/lib/workbenchEventConnection.ts@@ -9,12 +9,15 @@ type TimerHandle = ReturnType<typeof setTimeout>;interface WorkbenchEventReconnectLoopOptions {reconnect: () => void;isVisible: () => boolean;+ /** Whether events are flowing end to end right now, so no gap needs closing. */+ isStreamLive: () => boolean;}/** Owns the retry policy; EventSource wiring stays in ApiContext. */export class WorkbenchEventReconnectLoop {private readonly reconnect: () => void;private readonly isVisible: () => boolean;+ private readonly isStreamLive: () => boolean;private retryTimer: TimerHandle | null = null;private openTimer: TimerHandle | null = null;private retryAttempt = 0;@@ -23,6 +26,7 @@ export class WorkbenchEventReconnectLoop {constructor(options: WorkbenchEventReconnectLoopOptions) {this.reconnect = options.reconnect;this.isVisible = options.isVisible;+ this.isStreamLive = options.isStreamLive;}attemptStarted(): void {@@ -56,10 +60,19 @@ export class WorkbenchEventReconnectLoop {}, delayMs);}+ /** The page came back or the network returned: stop waiting out a backoff. */wake(): void {if (this.stopped || !this.isVisible()) return;+ // A backoff was sized while nobody was watching. Someone is watching now,+ // so the next attempt should not sit out the rest of a 15s delay.this.retryAttempt = 0;this.clearRetryTimer();+ // An unbroken stream missed nothing, so there is no gap to close. Recycling+ // it would manufacture the very outage -- and the catch-up read every+ // consumer runs on reconnect -- that waking exists to recover from.+ // Checked before clearOpenTimer() so a stream that is open at the transport+ // but has not completed its handshake keeps its watchdog armed.+ if (this.isStreamLive()) return;this.clearOpenTimer();this.reconnect();}