Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(console): catch chat timelines up from complete history, including mid-load (CHOO-3783)

raullopez-sandboxba859aca9cdc
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts
index eb86b6985..ce5219e21 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts
@@ -106,6 +106,8 @@ export class ChatTimeline {
olderFailed = false;
/** A load was asked for while another was in flight; it runs once that one ends. */
private reloadQueued = false;
+ /** History is held without gaps through this sequence: the newest a successful load read. */
+ private completeThrough: number | null = null;
/** The person is not a member of the room. */
notMember = false;
/** The person was a member and lost access while the chat was known. */
@@ -173,11 +175,14 @@ export class ChatTimeline {
this.loading = true;
this.loadError = null;
this.olderFailed = false;
- const held = this.newestSeq;
+ const held = this.completeThrough;
try {
let page = await this.api.messages(this.roomId, null, PAGE_SIZE);
const fetched = [...page.messages];
- // Page back until the newest held message, so a gap since the last load is not skipped.
+ const newest = page.messages.length
+ ? Math.max(...page.messages.map((message) => message.seq))
+ : held;
+ // Page back to the history already complete, so a gap since the last load is not skipped.
let oldest = Math.min(...page.messages.map((message) => message.seq));
while (held !== null && page.hasMore && page.messages.length && oldest > held) {
page = await this.api.messages(this.roomId, oldest, PAGE_SIZE);
@@ -187,6 +192,7 @@ export class ChatTimeline {
runInAction(() => {
for (const message of fetched) this.apply(message);
if (!this.loaded) this.hasMore = page.hasMore;
+ this.completeThrough = newest;
this.loaded = true;
this.notMember = false;
});
@@ -292,6 +298,7 @@ export class ChatTimeline {
revoke(): void {
this.accessLost = true;
this.messages = [];
+ this.completeThrough = null;
this.pending = [];
}
@@ -414,10 +421,13 @@ export class ChatTimelines {
return this.timelines.get(`${serverId}\u0000${roomId}`);
}
- /** Load again each of the server's timelines that loaded or failed to, catching up to the live feed. */
+ /** Load again each of the server's timelines that loaded, is loading or failed to, catching up to the live feed. */
reload(serverId: string): void {
for (const [key, timeline] of this.timelines)
- if (key.startsWith(`${serverId}\u0000`) && (timeline.loaded || timeline.loadError))
+ if (
+ key.startsWith(`${serverId}\u0000`) &&
+ (timeline.loaded || timeline.loading || timeline.loadError)
+ )
void timeline.load();
}
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-store.test.ts b/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-store.test.ts
index 48e1f8cf4..d7db2c72b 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-store.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-store.test.ts
@@ -296,4 +296,33 @@ describe('ChatTimeline', () => {
await vi.waitFor(() => expect(timeline.messages.map((m) => m.seq)).toEqual([1, 2]));
expect(calls).toBe(2);
});
+
+ it('catches up from the history it completed, not from a live message after a failed catch-up', async () => {
+ let head = 3;
+ let failOlder = false;
+ const { value } = api({
+ messages: async (_room, before, limit) => {
+ if (before !== null && failOlder) throw networkError();
+ const top = Math.min(before ?? head + 1, head + 1);
+ const from = Math.max(1, top - limit);
+ const seqs = Array.from({ length: top - from }, (_, i) => from + i);
+ return { messages: seqs.map((seq) => message(seq)), headSeq: head, hasMore: from > 1 };
+ },
+ });
+ const timeline = new ChatTimeline('room-1', value);
+ await timeline.load();
+ head = 130;
+ failOlder = true;
+ await timeline.load();
+ expect(timeline.loadError).not.toBeNull();
+
+ head = 131;
+ timeline.apply(message(131));
+ failOlder = false;
+ await timeline.load();
+ expect(timeline.messages.map((m) => m.seq)).toEqual(
+ Array.from({ length: 131 }, (_, i) => i + 1)
+ );
+ expect(timeline.loadError).toBeNull();
+ });
});
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/chats-store.test.ts b/console/apps/switch-console-desktop/src/renderer/tests/chats-store.test.ts
index c36f8b51e..d48e6646c 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/chats-store.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/tests/chats-store.test.ts
@@ -260,6 +260,29 @@ describe('ChatsStore', () => {
await vi.waitFor(() => expect(timeline.messages.map((m) => m.seq)).toEqual([1, 2]));
});
+ it('loads again when the feed goes live during a first load', async () => {
+ let release!: () => void;
+ const first = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ let calls = 0;
+ const messages = vi.fn(async () => {
+ if (calls++ === 0) {
+ await first;
+ return { messages: [message('r1', 1)], headSeq: 1, hasMore: false };
+ }
+ return { messages: [message('r1', 1), message('r1', 2)], headSeq: 2, hasMore: false };
+ });
+ const { store, live } = setup(async () => [summary('r1', 2)], { messages });
+ await store.connect('s1');
+ const timeline = store.timelines.get('s1', 'r1');
+ const loading = timeline.load();
+ live();
+ release();
+ await loading;
+ await vi.waitFor(() => expect(timeline.messages.map((m) => m.seq)).toEqual([1, 2]));
+ });
+
it('revokes a chat unlisted earlier once access is lost', async () => {
const { store } = setup(async () => [summary('r1', 1)]);
await store.connect('s1');