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", "ai/context/index.md", "ai/context/project.md", "ai/context/systems/index.md", "ai/context/workstreams/index.md", "ai/context/process/communication.md", "ai/context/people/manager.md", "ai/context/people/index.md", "ai/state/current.md", "ai/state/work-items.md", "ai/context/decisions/rest-vs-graphql.md", "ai/context/decisions/discourse-handling.md", "knowledge/workspace-model.md", "knowledge/communication-rules.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, "ai/logs") 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(`### ai/logs/${logName}\n${content.trim()}`) } } } catch { // No logs available yet. } if (sections.length > 0) { output.context.push( [ "## Fidelity 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"), ) } }, } }