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): a turn's tools stay above its reply when activity is reported late (CHOO-3740)

raullopez-sandboxf1cbb9fe438d
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/activity-join.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/activity-join.ts
index 304d96afc..cb6685665 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/activity-join.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/activity-join.ts
@@ -104,6 +104,19 @@ export function settleTurnsByNotices(
});
}
+/**
+ * When a turn's first step happened, or null before it has one. Switch stores
+ * a step when the host reports it, which can be well after the step. The host
+ * stamps a step with its own clock, at its latest revision, so that time can
+ * be late too. The earliest of both holds up against either.
+ */
+function firstStepAt(turn: TurnActivity): number | null {
+ if (turn.items.length === 0) return null;
+ return Math.min(
+ ...turn.items.flatMap((item) => [Date.parse(item.occurredAt), Date.parse(item.createdAt)])
+ );
+}
+
/**
* Where one agent's turns show, by the message each shows under, in message
* order. A turn shows under the message that started it, unless that message
@@ -136,7 +149,7 @@ export function placeTurns(
const reply = messages.findIndex((each, index) => index > after && shown(each));
anchor = Math.max(at, reply === -1 ? previous : reply);
}
- const firstStep = turn.items[0] ? Date.parse(turn.items[0].createdAt) : null;
+ const firstStep = firstStepAt(turn);
for (let next = anchor + 1; firstStep !== null && next < messages.length; next++) {
const each = messages[next];
if (bound.has(each.messageId) || Date.parse(each.sentAt) >= firstStep) break;
@@ -176,7 +189,7 @@ export function foldTurnEndNotices(
!claimed.has(each.turnId) &&
// A notice from before the turn started is about an earlier run of its
// message (the one a retry ran again), not this one.
- !(each.items[0] && Date.parse(each.items[0].createdAt) > Date.parse(message.sentAt))
+ !((firstStepAt(each) ?? -Infinity) > Date.parse(message.sentAt))
);
if (!turn) return;
claimed.add(turn.turnId);
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/chat-activity.test.ts b/console/apps/switch-console-desktop/src/renderer/tests/chat-activity.test.ts
index ff3fb5bf7..335b836e1 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/chat-activity.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/tests/chat-activity.test.ts
@@ -161,7 +161,7 @@ describe('placeTurns', () => {
];
const started = {
...turn('t1'),
- items: [row('t1', 'tool', { createdAt: at(3) })],
+ items: [row('t1', 'tool', { occurredAt: at(3), createdAt: at(3) })],
};
expect(ids(placeTurns(messages, 'helper', new Map([['q1', started]])))).toEqual({
ack: ['t1'],
@@ -175,6 +175,27 @@ describe('placeTurns', () => {
});
});
+ it('stays above the reply when its steps were reported after it', () => {
+ const at = (second: number) => new Date(Date.UTC(2026, 0, 1, 0, 0, second)).toISOString();
+ const messages = [
+ { ...said('q1', null), sentAt: at(0) },
+ { ...said('ack', 'helper'), sentAt: at(1) },
+ { ...said('r1', 'helper'), sentAt: at(5) },
+ ] as ChatMessage[];
+ const late = {
+ ...turn('t1'),
+ items: [
+ row('t1', 'read', { occurredAt: at(3), createdAt: at(9) }),
+ row('t1', 'post', { occurredAt: at(6), createdAt: at(2) }),
+ ],
+ };
+ expect(ids(placeTurns(messages, 'helper', new Map([['q1', late]])))).toEqual({
+ ack: ['t1'],
+ });
+ const noAck = messages.filter((each) => each.messageId !== 'ack');
+ expect(ids(placeTurns(noAck, 'helper', new Map([['q1', late]])))).toEqual({ q1: ['t1'] });
+ });
+
it('never shows a turn under a Retry’s acknowledgement, which the chat leaves out', () => {
const at = (second: number) => new Date(Date.UTC(2026, 0, 1, 0, 0, second)).toISOString();
const messages = [