Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(console): reconnect the chat feeds now when asked during backoff (CHOO-3783)
raullopez-sandbox063aec7249b7
diff --git a/console/apps/switch-console-desktop/src/main/core/chats/activity-stream.test.ts b/console/apps/switch-console-desktop/src/main/core/chats/activity-stream.test.tsindex 6c16b196d..4c81faab8 100644--- a/console/apps/switch-console-desktop/src/main/core/chats/activity-stream.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/chats/activity-stream.test.ts@@ -83,4 +83,32 @@ describe('ChatActivityStream', () => {await vi.waitFor(() => expect(opened).toBe(2));stream.stop();});++ it('reconnects at once when started while waiting to retry', async () => {+ let opened = 0;+ const timers: { cancelled: boolean }[] = [];+ const stream = new ChatActivityStream({+ open: async () => {+ opened++;+ return new Response('Bad Gateway', { status: 502 });+ },+ isUnauthorized: () => false,+ backoffMs: () => 30_000,+ setTimer: () => {+ const timer = { cancelled: false };+ timers.push(timer);+ return () => {+ timer.cancelled = true;+ };+ },+ sink: { activity: () => {}, approval: () => {}, resync: () => {}, state: () => {} },+ });+ stream.start();+ await vi.waitFor(() => expect(timers).toHaveLength(2));+ stream.start();+ await vi.waitFor(() => expect(opened).toBe(2));+ expect(timers[1]!.cancelled).toBe(true);+ stream.stop();+ expect(timers.every((each) => each.cancelled)).toBe(true);+ });});diff --git a/console/apps/switch-console-desktop/src/main/core/chats/activity-stream.ts b/console/apps/switch-console-desktop/src/main/core/chats/activity-stream.tsindex e6cdd1114..2d72dd193 100644--- a/console/apps/switch-console-desktop/src/main/core/chats/activity-stream.ts+++ b/console/apps/switch-console-desktop/src/main/core/chats/activity-stream.ts@@ -37,7 +37,8 @@ export class ChatActivityStream {private stopped = false;private running = false;private abort: AbortController | null = null;- private cancelTimer: (() => void) | null = null;+ /** Ends the backoff wait early; set only while the loop is waiting to retry. */+ private wake: (() => void) | null = null;private attempt = 0;/** A server without the feed: not asked again until the next `start`. */private tooOld = false;@@ -59,18 +60,16 @@ export class ChatActivityStream {start(): void {this.stopped = false;- if (this.running) return;- this.cancelTimer?.();- this.cancelTimer = null;+ if (this.running && !this.wake) return;this.attempt = 0;this.tooOld = false;- void this.loop();+ if (this.wake) this.wake();+ else void this.loop();}stop(): void {this.stopped = true;- this.cancelTimer?.();- this.cancelTimer = null;+ this.wake?.();this.abort?.abort();this.abort = null;}@@ -97,10 +96,13 @@ export class ChatActivityStream {if (this.tooOld || this.deps.isUnauthorized(error)) return;const wait = this.deps.backoffMs(this.attempt++);await new Promise<void>((resolve) => {- this.cancelTimer = this.deps.setTimer(() => {- this.cancelTimer = null;+ let cancel = (): void => {};+ this.wake = () => {+ this.wake = null;+ cancel();resolve();- }, wait);+ };+ cancel = this.deps.setTimer(() => this.wake?.(), wait);});}}diff --git a/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.test.ts b/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.test.tsindex 6142004be..98a44fe9c 100644--- a/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.test.ts@@ -104,7 +104,7 @@ function harness(lists: ChatSummary[][], unauthorized = false) {timer.cancelled = true;timer.fn();};- return { deps, states, messages, removed, summaries, opened, feeds, fireRetry };+ return { deps, states, messages, removed, summaries, opened, feeds, timers, fireRetry };}describe('sseFrames', () => {@@ -174,6 +174,34 @@ describe('ChatStream', () => {stream.stop();});+ it('reconnects at once when started or resynced while waiting to retry, and stops cleanly', async () => {+ const h = harness([[summary('room-a', 1)]]);+ const stream = new ChatStream(h.deps);+ stream.start();+ await vi.waitFor(() => expect(h.feeds).toHaveLength(1));+ h.feeds[0]!.end();+ await vi.waitFor(() => expect(stream.state.state).toBe('offline'));++ stream.start();+ await vi.waitFor(() => expect(h.feeds).toHaveLength(2));+ expect(h.timers.filter((each) => each.ms < 60_000).every((each) => each.cancelled)).toBe(true);++ h.feeds[1]!.end();+ await vi.waitFor(() => expect(stream.state.state).toBe('offline'));+ stream.resync();+ await vi.waitFor(() => expect(h.feeds).toHaveLength(3));++ h.feeds[2]!.end();+ await vi.waitFor(() => expect(stream.state.state).toBe('offline'));+ stream.stop();+ expect(() => h.fireRetry()).toThrow('No retry is waiting.');+ // The stopped loop has ended, so a later start runs a fresh one.+ await new Promise((resolve) => setTimeout(resolve, 0));+ stream.start();+ await vi.waitFor(() => expect(h.feeds).toHaveLength(4));+ stream.stop();+ });+it('forgets a removed room and passes on the removal', async () => {const h = harness([[summary('room-a', 1)]]);const stream = new ChatStream(h.deps);diff --git a/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.ts b/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.tsindex fff9a6970..e578b1c5b 100644--- a/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.ts+++ b/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.ts@@ -90,7 +90,8 @@ export class ChatStream {private stopped = false;private running = false;private abort: AbortController | null = null;- private cancelTimer: (() => void) | null = null;+ /** Ends the backoff wait early; set only while the loop is waiting to retry. */+ private wake: (() => void) | null = null;private attempt = 0;private current: { state: ChatStreamState; detail: string | null } = {state: 'connecting',@@ -111,24 +112,22 @@ export class ChatStream {/** Start, or reconnect now if the feed is waiting to retry or gave up. */start(): void {this.stopped = false;- if (this.running) return;- this.cancelTimer?.();- this.cancelTimer = null;+ if (this.running && !this.wake) return;this.attempt = 0;- void this.loop();+ if (this.wake) this.wake();+ else void this.loop();}/** Drop the connection and reconnect from the cursors reached. */resync(): void {if (this.stopped) return;this.abort?.abort();- if (!this.running) this.start();+ this.start();}stop(): void {this.stopped = true;- this.cancelTimer?.();- this.cancelTimer = null;+ this.wake?.();this.abort?.abort();this.abort = null;}@@ -156,10 +155,13 @@ export class ChatStream {this.setState('offline', detail);const wait = this.deps.backoffMs(this.attempt++);await new Promise<void>((resolve) => {- this.cancelTimer = this.deps.setTimer(() => {- this.cancelTimer = null;+ let cancel = (): void => {};+ this.wake = () => {+ this.wake = null;+ cancel();resolve();- }, wait);+ };+ cancel = this.deps.setTimer(() => this.wake?.(), wait);});}}