Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

Fix duplicate messages in agent sessions on first open (#6111)

Eric Hayesf803458782fc
diff --git a/apps/web/src/lib/queries/agent-session/session-fold.test.ts b/apps/web/src/lib/queries/agent-session/session-fold.test.ts
index 2b7ee21799..7095301038 100644
--- a/apps/web/src/lib/queries/agent-session/session-fold.test.ts
+++ b/apps/web/src/lib/queries/agent-session/session-fold.test.ts
@@ -82,6 +82,10 @@ describe('buffer overlap', () => {
describe('shared session folds', () => {
it('buffers realtime overlap while fetching and replays only its suffix', async () => {
+ // This test verifies that buffered entries are replayed during opening.
+ // Sinks are registered AFTER getting the snapshot to prevent duplicate
+ // notifications: replayed entries update the worker but don't notify sinks,
+ // then the caller receives the full snapshot (including replayed messages).
let resolveFetch!: (
value: Result<AgentSessionLogResponse, unknown>
) => void;
diff --git a/apps/web/src/lib/queries/agent-session/session-fold.ts b/apps/web/src/lib/queries/agent-session/session-fold.ts
index 78b43302b6..8b872797be 100644
--- a/apps/web/src/lib/queries/agent-session/session-fold.ts
+++ b/apps/web/src/lib/queries/agent-session/session-fold.ts
@@ -80,8 +80,6 @@ export async function acquireAgentSessionFold(args: {
references: 0,
};
state.references += 1;
- if (onChange) state.foldedSinks.add(onChange);
- if (onMetadata) state.metadataSinks.add(onMetadata);
sessions.set(agentSessionId, state);
let released = false;
@@ -100,6 +98,14 @@ export async function acquireAgentSessionFold(args: {
const snapshot = await sessionMessages(agentSessionId);
const bot = state.bot;
if (!bot) throw new Error(`agent session ${agentSessionId} has no bot`);
+
+ // Add sinks after getting the initial snapshot to avoid duplicate
+ // notifications: buffered entries replayed during opening would otherwise
+ // notify the sinks, and then the caller would also process the snapshot
+ // (which includes those same messages).
+ if (onChange) state.foldedSinks.add(onChange);
+ if (onMetadata) state.metadataSinks.add(onMetadata);
+
return {
bot,
messages: snapshot.messages,