fix(mom): use coding-agent SessionManager instead of custom MomSessionManager

Removes MomSessionManager which was missing methods expected by AgentSession
(appendMessage, getBranch, etc.), causing runtime crashes.

Now uses the standard SessionManager from coding-agent with a fixed context.jsonl
path per channel. The syncLogToSessionManager function handles syncing messages
from log.jsonl using SessionManager's public API.

Ref #595
This commit is contained in:
Mario Zechner 2026-01-11 03:47:24 +01:00
parent 6db2d0770a
commit f0fd0a7d6a
4 changed files with 119 additions and 463 deletions

View file

@ -7,13 +7,14 @@ import {
formatSkillsForPrompt,
loadSkillsFromDir,
ModelRegistry,
SessionManager,
type Skill,
} from "@mariozechner/pi-coding-agent";
import { existsSync, readFileSync } from "fs";
import { mkdir, writeFile } from "fs/promises";
import { homedir } from "os";
import { join } from "path";
import { MomSessionManager, MomSettingsManager } from "./context.js";
import { MomSettingsManager, syncLogToSessionManager } from "./context.js";
import * as log from "./log.js";
import { createExecutor, type SandboxConfig } from "./sandbox.js";
import type { ChannelInfo, SlackContext, UserInfo } from "./slack.js";
@ -418,7 +419,9 @@ function createRunner(sandboxConfig: SandboxConfig, channelId: string, channelDi
const systemPrompt = buildSystemPrompt(workspacePath, channelId, memory, sandboxConfig, [], [], skills);
// Create session manager and settings manager
const sessionManager = new MomSessionManager(channelDir);
// Use a fixed context.jsonl file per channel (not timestamped like coding-agent)
const contextFile = join(channelDir, "context.jsonl");
const sessionManager = SessionManager.open(contextFile, channelDir);
const settingsManager = new MomSettingsManager(join(channelDir, ".."));
// Create AuthStorage and ModelRegistry
@ -439,7 +442,7 @@ function createRunner(sandboxConfig: SandboxConfig, channelId: string, channelDi
});
// Load existing messages
const loadedSession = sessionManager.buildSessionContex();
const loadedSession = sessionManager.buildSessionContext();
if (loadedSession.messages.length > 0) {
agent.replaceMessages(loadedSession.messages);
log.logInfo(`[${channelId}] Loaded ${loadedSession.messages.length} messages from context.jsonl`);
@ -448,7 +451,7 @@ function createRunner(sandboxConfig: SandboxConfig, channelId: string, channelDi
// Create AgentSession wrapper
const session = new AgentSession({
agent,
sessionManager: sessionManager as any,
sessionManager,
settingsManager: settingsManager as any,
modelRegistry,
});
@ -624,9 +627,16 @@ function createRunner(sandboxConfig: SandboxConfig, channelId: string, channelDi
// Ensure channel directory exists
await mkdir(channelDir, { recursive: true });
// Sync messages from log.jsonl that arrived while we were offline or busy
// Exclude the current message (it will be added via prompt())
const syncedCount = syncLogToSessionManager(sessionManager, channelDir, ctx.message.ts);
if (syncedCount > 0) {
log.logInfo(`[${channelId}] Synced ${syncedCount} messages from log.jsonl`);
}
// Reload messages from context.jsonl
// This picks up any messages synced from log.jsonl before this run
const reloadedSession = sessionManager.buildSessionContex();
// This picks up any messages synced above
const reloadedSession = sessionManager.buildSessionContext();
if (reloadedSession.messages.length > 0) {
agent.replaceMessages(reloadedSession.messages);
log.logInfo(`[${channelId}] Reloaded ${reloadedSession.messages.length} messages from context`);