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
This commit is contained in:
Mario Zechner 2025-12-11 13:26:45 +01:00
parent 5a231abe4c
commit cc71c0a49e
2 changed files with 14 additions and 18 deletions

View file

@ -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);

View file

@ -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;