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): zero-agent create and thread reply counting bugs

raullopez-sandbox3f7a83926d2c
diff --git a/console/apps/switch-console-desktop/src/main/core/chats/gateway.test.ts b/console/apps/switch-console-desktop/src/main/core/chats/gateway.test.ts
index ca91150de..ef1615ed8 100644
--- a/console/apps/switch-console-desktop/src/main/core/chats/gateway.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/chats/gateway.test.ts
@@ -79,6 +79,7 @@ describe('createChat', () => {
method: 'POST',
body: {
requestId: 'req-1',
+ agentIds: [],
memberUserIds: ['user-1', 'user-2'],
name: 'Test Group',
},
diff --git a/console/apps/switch-console-desktop/src/main/core/chats/gateway.ts b/console/apps/switch-console-desktop/src/main/core/chats/gateway.ts
index 3789792ed..c62e85689 100644
--- a/console/apps/switch-console-desktop/src/main/core/chats/gateway.ts
+++ b/console/apps/switch-console-desktop/src/main/core/chats/gateway.ts
@@ -124,7 +124,7 @@ export async function createChat(
requestId: input.requestId,
};
if (agentIds.length === 1) body.agentId = agentIds[0];
- else if (agentIds.length > 1) body.agentIds = agentIds;
+ else body.agentIds = agentIds;
if (input.memberUserIds) body.memberUserIds = input.memberUserIds;
if (input.name) body.name = input.name;
if (input.greeting !== null) body.greeting = input.greeting;
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 97e3df3f0..1c8574196 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
@@ -110,11 +110,17 @@ export class ChatTimeline {
return this.messages.find((message) => message.messageId === messageId);
}
- /** Get the live reply count for a thread root, or the server's count if the root is loaded. */
- threadReplyCount(rootId: string): number {
+ /** Thread reply count: server baseline + distinct live replies since, or null if root not loaded. */
+ threadReplyCount(rootId: string): number | null {
const root = this.byId(rootId);
- if (root?.threadReplyCount !== undefined) return root.threadReplyCount;
- return this.threadReplyCounts.get(rootId) ?? 0;
+ if (!root) return null;
+ const serverCount = root.threadReplyCount ?? 0;
+ const lastReplySeq = root.threadLastReplySeq ?? -1;
+ const newReplies = this.messages.filter(
+ (msg) => msg.threadRootId === rootId && msg.seq > lastReplySeq
+ );
+ const uniqueNewReplies = new Set(newReplies.map((m) => m.messageId)).size;
+ return serverCount + uniqueNewReplies;
}
/** Load thread messages for a root. */
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-threads.test.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-threads.test.ts
index 315c08555..00abac417 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-threads.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-threads.test.ts
@@ -35,51 +35,98 @@ const makeApi = (): TimelineApi => ({
});
describe('ChatTimeline thread reply counts', () => {
- it('increments thread reply count when a reply arrives', () => {
+ it('returns server baseline + new live replies', () => {
const timeline = new ChatTimeline('room-1', makeApi());
- const root = makeMessage({ messageId: 'root-1', seq: 1 });
- const reply = makeMessage({ messageId: 'reply-1', seq: 2, threadRootId: 'root-1' });
+ const root = makeMessage({
+ messageId: 'root-1',
+ seq: 10,
+ threadReplyCount: 5,
+ threadLastReplySeq: 14,
+ });
+ const newReply = makeMessage({ messageId: 'reply-new', seq: 15, threadRootId: 'root-1' });
timeline.apply(root);
- timeline.apply(reply);
+ timeline.apply(newReply);
- expect(timeline.threadReplyCount('root-1')).toBe(1);
+ expect(timeline.threadReplyCount('root-1')).toBe(6);
});
- it('tracks multiple replies to the same root', () => {
+ it('does not count fetched history already in server baseline', () => {
const timeline = new ChatTimeline('room-1', makeApi());
- const root = makeMessage({ messageId: 'root-1', seq: 1 });
- const reply1 = makeMessage({ messageId: 'reply-1', seq: 2, threadRootId: 'root-1' });
- const reply2 = makeMessage({ messageId: 'reply-2', seq: 3, threadRootId: 'root-1' });
+ const root = makeMessage({
+ messageId: 'root-1',
+ seq: 10,
+ threadReplyCount: 3,
+ threadLastReplySeq: 13,
+ });
+ const oldReply1 = makeMessage({ messageId: 'reply-1', seq: 11, threadRootId: 'root-1' });
+ const oldReply2 = makeMessage({ messageId: 'reply-2', seq: 12, threadRootId: 'root-1' });
timeline.apply(root);
- timeline.apply(reply1);
- timeline.apply(reply2);
+ timeline.apply(oldReply1);
+ timeline.apply(oldReply2);
- expect(timeline.threadReplyCount('root-1')).toBe(2);
+ expect(timeline.threadReplyCount('root-1')).toBe(3);
});
- it('tracks counts even if root is not loaded', () => {
+ it('returns null for unloaded root', () => {
const timeline = new ChatTimeline('room-1', makeApi());
- const reply = makeMessage({ messageId: 'reply-1', seq: 2, threadRootId: 'unloaded-root' });
+ const reply = makeMessage({ messageId: 'reply-1', seq: 15, threadRootId: 'unloaded-root' });
timeline.apply(reply);
- expect(timeline.threadReplyCount('unloaded-root')).toBe(1);
+ expect(timeline.threadReplyCount('unloaded-root')).toBeNull();
});
- it('prefers server threadReplyCount if root message has it', () => {
+ it('applies live replies once root loads without double counting', () => {
const timeline = new ChatTimeline('room-1', makeApi());
- const root = makeMessage({ messageId: 'root-1', seq: 1, threadReplyCount: 5 });
-
+ const liveReply1 = makeMessage({ messageId: 'reply-1', seq: 15, threadRootId: 'root-1' });
+ const liveReply2 = makeMessage({ messageId: 'reply-2', seq: 16, threadRootId: 'root-1' });
+
+ timeline.apply(liveReply1);
+ timeline.apply(liveReply2);
+ expect(timeline.threadReplyCount('root-1')).toBeNull();
+
+ const root = makeMessage({
+ messageId: 'root-1',
+ seq: 10,
+ threadReplyCount: 3,
+ threadLastReplySeq: 13,
+ });
timeline.apply(root);
expect(timeline.threadReplyCount('root-1')).toBe(5);
});
- it('returns 0 for a root with no replies', () => {
+ it('deduplicates replayed live events by messageId', () => {
const timeline = new ChatTimeline('room-1', makeApi());
- expect(timeline.threadReplyCount('nonexistent-root')).toBe(0);
+ const root = makeMessage({
+ messageId: 'root-1',
+ seq: 10,
+ threadReplyCount: 2,
+ threadLastReplySeq: 12,
+ });
+ const reply = makeMessage({ messageId: 'reply-1', seq: 15, threadRootId: 'root-1' });
+
+ timeline.apply(root);
+ timeline.apply(reply);
+ timeline.apply(reply);
+
+ expect(timeline.threadReplyCount('root-1')).toBe(3);
+ });
+
+ it('returns 0 for loaded root with no replies', () => {
+ const timeline = new ChatTimeline('room-1', makeApi());
+ const root = makeMessage({
+ messageId: 'root-1',
+ seq: 10,
+ threadReplyCount: 0,
+ threadLastReplySeq: null,
+ });
+
+ timeline.apply(root);
+
+ expect(timeline.threadReplyCount('root-1')).toBe(0);
});
it('calls threadMessages API when loading a thread', async () => {