import { readdir, readFile } from "node:fs/promises" import path from "node:path" async function safeRead(filePath) { try { return await readFile(filePath, "utf8") } catch { return null } } export const FidelityCompaction = async ({ directory }) => { return { "experimental.session.compacting": async (_input, output) => { const baseFiles = [ "README.md", "core/README.md", "core/memory/operational-memory.md", "core/integrations/memory-vault-model.md", "core/integrations/communication-model.md", "profiles/fidelity/profile.md", "agent-memory/README.md", "agent-memory/behavior/agent-behavior.md", "agent-memory/memory/promotion-rules.md", "agent-memory/memory/context-maintenance.md", "agent-memory/integrations/technical-verification.md", "agent-memory/workflows/workspace-model.md", "agent-memory/workflows/ai-to-ai-prompting.md", "project-knowledge/00-start/start-here.md", "project-knowledge/01-current/current-work.md", "project-knowledge/01-current/work-items.md", "project-knowledge/03-context/project.md", "project-knowledge/03-context/ios/index.md", "project-knowledge/03-context/ios/current-practices.md", "project-knowledge/03-context/ios/project-swift-guidance.md", "project-knowledge/03-context/systems/index.md", "project-knowledge/03-context/workstreams/index.md", "project-knowledge/03-context/process/communication.md", "project-knowledge/04-people/manager.md", "project-knowledge/04-people/index.md", "project-knowledge/05-decisions/rest-vs-graphql.md", "project-knowledge/05-decisions/discourse-handling.md", ] const sections = [] for (const relPath of baseFiles) { const content = await safeRead(path.join(directory, relPath)) if (content) { sections.push(`### ${relPath}\n${content.trim()}`) } } try { const logsDir = path.join(directory, "project-knowledge/06-daily") const logs = (await readdir(logsDir)) .filter((name) => name.endsWith(".md")) .sort() .slice(-2) for (const logName of logs) { const content = await safeRead(path.join(logsDir, logName)) if (content) { sections.push(`### project-knowledge/06-daily/${logName}\n${content.trim()}`) } } } catch { // No logs available yet. } if (sections.length > 0) { output.context.push( [ "## AI Workspace Persistent Context", "Preserve this operational memory across compaction.", "If later messages introduced corrections or durable facts, prefer the corrected view over stale summaries.", "", sections.join("\n\n"), ].join("\n"), ) } }, } }