From cc71c0a49ee3ce2349655b4890751d35c54423cc Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Thu, 11 Dec 2025 13:26:45 +0100 Subject: [PATCH] mom: fix duplicate logging - Remove user message logging from agent.ts (Slack handler already logs it) - Add excludeTimestamp param to syncFromLog to skip current @mention - Sync happens once before each prompt with current message excluded --- packages/mom/src/agent.ts | 19 +++++-------------- packages/mom/src/context.ts | 13 +++++++++---- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/packages/mom/src/agent.ts b/packages/mom/src/agent.ts index 1f9e6860..8760f3d7 100644 --- a/packages/mom/src/agent.ts +++ b/packages/mom/src/agent.ts @@ -337,11 +337,12 @@ export function createAgentRunner(sandboxConfig: SandboxConfig): AgentRunner { // Update system prompt for existing session (memory may have changed) session.agent.setSystemPrompt(systemPrompt); - - // Sync any new messages from log.jsonl (e.g., messages that arrived while processing) - sessionManager.syncFromLog(); } + // Sync messages from log.jsonl to context.jsonl + // Exclude the current message - it will be added via prompt() + sessionManager.syncFromLog(ctx.message.ts); + currentSession = session; // Create logging context @@ -551,19 +552,9 @@ export function createAgentRunner(sandboxConfig: SandboxConfig): AgentRunner { try { // Build user message from Slack context + // Note: User message is already logged to log.jsonl by Slack event handler const userMessage = ctx.message.text; - // Log user message to log.jsonl (human-readable history) - await store.logMessage(ctx.message.channel, { - date: new Date().toISOString(), - ts: toSlackTs(), - user: ctx.message.user, - userName: ctx.message.userName, - text: userMessage, - attachments: ctx.message.attachments || [], - isBot: false, - }); - // Send prompt to agent session await session.prompt(userMessage); diff --git a/packages/mom/src/context.ts b/packages/mom/src/context.ts index 232a8495..02004073 100644 --- a/packages/mom/src/context.ts +++ b/packages/mom/src/context.ts @@ -70,9 +70,7 @@ export class MomSessionManager { } else { this.sessionId = uuidv4(); } - - // Sync missing messages from log.jsonl to context.jsonl - this.syncFromLog(); + // Note: syncFromLog() is called explicitly from agent.ts with excludeTimestamp } /** @@ -87,8 +85,10 @@ export class MomSessionManager { * Channel chatter is formatted as "[username]: message" to distinguish from direct @mentions. * * Called automatically on construction and should be called before each agent run. + * + * @param excludeTimestamp Optional timestamp to exclude (for the current @mention being processed) */ - syncFromLog(): void { + syncFromLog(excludeTimestamp?: string): void { if (!existsSync(this.logFile)) return; // Get timestamps of messages already in context @@ -118,12 +118,17 @@ export class MomSessionManager { try { const logMsg: LogMessage = JSON.parse(line); + // Use date for context timestamp (consistent key) const ts = logMsg.date || logMsg.ts; if (!ts) continue; // Skip if already in context if (contextTimestamps.has(ts)) continue; + // Skip the current message being processed (will be added via prompt()) + // Compare against Slack ts since that's what ctx.message.ts provides + if (excludeTimestamp && logMsg.ts === excludeTimestamp) continue; + // Skip bot messages - added through agent flow if (logMsg.isBot) continue;