UNPKG

@phren/cli

Version:

Knowledge layer for AI agents - CLI, MCP server, and data layer

446 lines (445 loc) • 21.1 kB
import * as fs from "fs"; import { createRequire } from "module"; import * as path from "path"; import { parseFindingsContent, readReviewQueue } from "../data/access.js"; import { isCitationLine, normalizeFindingText } from "../content/metadata.js"; import { parseCitationComment } from "../content/citation.js"; import { citationStateKey, readCitationStates } from "../content/citation-states.js"; import { listTopicFiles, NOW_START, NOW_END } from "../content/summarize.js"; import { getMachineName } from "../machine-identity.js"; import { atomicWriteText, debugLog, runtimeDir } from "../phren-paths.js"; import { readMemoryOpens, readRecallTrace, recallTraceFile } from "../recall/trace.js"; import { getStoreProjectDirs, resolveAllStores } from "../store-registry.js"; import { errorMessage, isValidProjectName } from "../utils.js"; import { withFileLock } from "./locks.js"; import { getRetentionPolicy, readRuntimeAgeOutState, RUNTIME_AGE_OUT_STATE, runtimeAgeOutActive } from "./policy.js"; function emptyCounts() { return { activeFindings: 0, archivedBullets: 0, olderBullets: 0, retiredBullets: 0, contradictedBullets: 0, bulletsWithoutFid: 0, duplicateFids: 0, duplicateTexts: 0, citations: { none: 0, timestampOnly: 0, withFile: 0, withCommit: 0, withSymbol: 0, withRemote: 0 }, checkableCitations: { any: 0, withFile: 0 }, reviewEntries: 0, }; } function parseBullets(content) { let inNow = false; const lines = content.split("\n").map(line => { if (line.startsWith(NOW_START)) { inNow = true; return ""; } if (line.startsWith(NOW_END)) { inNow = false; return ""; } return !inNow && (line.startsWith("- ") || isCitationLine(line)) ? line : ""; }); return parseFindingsContent(lines.join("\n"), { includeArchived: true }); } // Active bullets whose recorded citation state is decided here; a bare timestamp is never swept, so never counts. function checkableCitations(findingsPath, content, states) { const counts = { any: 0, withFile: 0 }; const lines = content.split("\n"); for (let i = 0; i < lines.length - 1; i++) { if (!lines[i].startsWith("- ")) continue; const citation = parseCitationComment(lines[i + 1]); const state = citation ? states[citationStateKey(findingsPath, lines[i])]?.state : undefined; if (!state || state === "unverifiable") continue; counts.any++; if (citation?.file) counts.withFile++; } return counts; } function projectHealth(storePath, project, states) { const counts = { project, ...emptyCounts() }; const findingsPath = path.join(storePath, project, "FINDINGS.md"); const activeContent = fs.existsSync(findingsPath) ? fs.readFileSync(findingsPath, "utf8") : ""; const active = activeContent ? parseBullets(activeContent) : []; counts.checkableCitations = checkableCitations(findingsPath, activeContent, states); const topics = listTopicFiles(storePath, project); const archived = topics.flatMap(topic => parseBullets(fs.readFileSync(topic.file, "utf8"))); const topicsDir = path.join(storePath, project, "reference", "topics"); const older = fs.existsSync(topicsDir) ? fs.readdirSync(topicsDir).filter(name => name.endsWith(".older.md")).sort() .flatMap(name => parseBullets(fs.readFileSync(path.join(topicsDir, name), "utf8"))) : []; counts.activeFindings = active.length; counts.archivedBullets = archived.length; counts.olderBullets = older.length; const fids = new Set(); const texts = new Set(); for (const bullet of [...active, ...archived, ...older]) { if (bullet.status === "superseded" || bullet.status === "retracted") counts.retiredBullets++; if (bullet.status === "contradicted") counts.contradictedBullets++; if (!bullet.stableId) counts.bulletsWithoutFid++; else if (fids.has(bullet.stableId)) counts.duplicateFids++; else fids.add(bullet.stableId); const text = normalizeFindingText(bullet.text); if (texts.has(text)) counts.duplicateTexts++; else texts.add(text); const citation = bullet.citationData; if (!citation) counts.citations.none++; else { if (citation.file) counts.citations.withFile++; if (citation.commit) counts.citations.withCommit++; if (citation.symbol) counts.citations.withSymbol++; if (citation.remote) counts.citations.withRemote++; if (!citation.file && !citation.commit && !citation.symbol && !citation.remote) counts.citations.timestampOnly++; } } const review = readReviewQueue(storePath, project); if (review.ok) counts.reviewEntries = review.data.length; return counts; } function bytes(file) { try { const stat = fs.lstatSync(file); if (stat.isFile()) return stat.size; if (stat.isDirectory()) return fs.readdirSync(file).reduce((sum, name) => sum + bytes(path.join(file, name)), 0); return 0; } catch (err) { if (err.code === "ENOENT") return 0; throw err; } } function recallHealth(phrenPath, now) { if (!fs.existsSync(recallTraceFile(phrenPath))) return null; const counts = { prompts: 0, injected: 0, abstained: 0, skipped: 0, suppressedRepeats: 0, byAbstainReason: {}, clankerIds: { injected: 0, opened: 0 } }; const reasons = new Map(); const injectedIds = new Map(); for (const entry of readRecallTrace(phrenPath)) { const at = Date.parse(entry.at); if (!Number.isFinite(at) || at < now.getTime() - 86_400_000 || at > now.getTime()) continue; counts.prompts++; // Injected counts prompts that received memory. if (Array.isArray(entry.injected) && entry.injected.length > 0) counts.injected++; if (typeof entry.skipped === "string" && entry.skipped) counts.skipped++; if (typeof entry.abstained === "string" && entry.abstained) { counts.abstained++; reasons.set(entry.abstained, (reasons.get(entry.abstained) ?? 0) + 1); } if (typeof entry.suppressedRepeats === "number" && Number.isFinite(entry.suppressedRepeats) && entry.suppressedRepeats > 0) { counts.suppressedRepeats += entry.suppressedRepeats; } for (const row of Array.isArray(entry.injected) && typeof entry.session === "string" ? entry.injected : []) { const key = `${entry.session}\n${row?.id}`; if (typeof row?.id === "string" && !injectedIds.has(key)) injectedIds.set(key, at); } } counts.byAbstainReason = Object.fromEntries(reasons); // The latest open per session and id; an injection counts as opened when that open came at or after it. const lastOpen = new Map(); for (const open of readMemoryOpens(phrenPath)) { const key = `${open.session}\n${open.id}`; lastOpen.set(key, Math.max(lastOpen.get(key) ?? -Infinity, Date.parse(open.at))); } counts.clankerIds = { injected: injectedIds.size, opened: [...injectedIds].filter(([key, at]) => (lastOpen.get(key) ?? -Infinity) >= at).length }; return counts; } function addCounts(into, from) { for (const [key, value] of Object.entries(into)) { if (typeof value === "number") into[key] = value + from[key]; else addCounts(value, from[key]); } } // Read-only: duplicate counts are extra occurrences within a project, across all tiers. // Citation coverage reads the states this computer's last sweep recorded; it runs no git. export function collectStoreHealth(phrenPath, now = new Date(), computer = getMachineName(), citationStates = readCitationStates(phrenPath)) { const stores = resolveAllStores(phrenPath, { migrateAttachments: false }).map(entry => { const store = entry.role === "primary" ? { ...entry, path: phrenPath } : entry; const projects = getStoreProjectDirs(store).sort().map(dir => projectHealth(store.path, path.basename(dir), citationStates)); const totals = emptyCounts(); for (const { project: _name, ...counts } of projects) addCounts(totals, counts); const citationCoverage = totals.activeFindings ? Math.round((totals.checkableCitations.any / totals.activeFindings) * 1000) / 1000 : 0; return { name: store.name, path: path.relative(phrenPath, store.path).split(path.sep).join("/") || ".", projects, totals, citationCoverage }; }); const runtime = runtimeDir(phrenPath); const runtimeBytes = Object.fromEntries(fs.existsSync(runtime) ? fs.readdirSync(runtime).sort().map(name => [name, bytes(path.join(runtime, name))]) : []); const runtimeClasses = { codeIndexes: 0, fanoutArchive: 0, fanouts: 0, sessions: 0, packages: 0, logs: 0, other: 0 }; for (const [name, size] of Object.entries(runtimeBytes)) runtimeClasses[runtimeClass(name)] += size; const over = stores.flatMap(store => store.projects.flatMap(({ project, reviewEntries }) => { const budgets = getRetentionPolicy(path.resolve(phrenPath, store.path), project).budgets; return [ overBudget("reviewEntries", reviewEntries, budgets.reviewEntries, { store: store.name, project }), ]; })); const global = getRetentionPolicy(phrenPath).budgets; const fanoutRow = overBudget("fanoutArchiveBytes", runtimeClasses.fanoutArchive, megabytes(global.fanoutArchiveMB)); if (fanoutRow && !runtimeAgeOutActive(phrenPath)) fanoutRow.effect = "reported only until runtime age-out is on"; over.push(overBudget("runtimeBytes", Object.values(runtimeBytes).reduce((sum, size) => sum + size, 0), megabytes(global.runtimeMB)), fanoutRow); return { at: now.toISOString(), computer, stores, runtimeBytes, runtimeClasses, budgets: { over: over.filter((row) => row !== null) }, recall: recallHealth(phrenPath, now), }; } // ── Budgets: which components are over, and what that stops ── const BUDGET_EFFECTS = { reviewEntries: "machine-generated entries are not queued", runtimeBytes: "reported only", fanoutArchiveBytes: "the archive sweep deletes the oldest finished folders", }; function overBudget(component, value, budget, where = {}) { return budget > 0 && value > budget ? { component, ...where, value, budget, effect: BUDGET_EFFECTS[component] } : null; } function megabytes(mb) { return Math.round(mb * 1024 * 1024); } function runtimeClass(name) { if (name === "code") return "codeIndexes"; if (name === "agent-fanouts-archive") return "fanoutArchive"; if (name === "agent-fanouts") return "fanouts"; if (name === "sessions") return "sessions"; if (name === "packages") return "packages"; return /\.(?:log|jsonl)$/.test(name) ? "logs" : "other"; } // ── Runtime age-out: regenerable files only, off by default, and never before this computer's week ── const DAY_MS = 86_400_000; const AGE_OUT_WAIT_DAYS = 7; const FINISHED_STATUSES = new Set(["completed", "failed", "cancelled"]); const requireModule = createRequire(import.meta.url); // node:sqlite, loaded without the ExperimentalWarning Node 22 prints on first use (maintenance runs in the background). function loadSqlite() { const emit = process.emitWarning; process.emitWarning = ((warning, ...rest) => { if (/SQLite is an experimental feature/i.test(typeof warning === "string" ? warning : warning.message)) return; emit.call(process, warning, ...rest); }); try { return requireModule("node:sqlite"); } finally { process.emitWarning = emit; } } // The checkout a code index was built from, as the indexer recorded it in its meta table; undefined when unreadable. function indexRepoRoot(file) { let db; try { const { DatabaseSync } = loadSqlite(); db = new DatabaseSync(file, { readOnly: true }); const row = db.prepare("SELECT value FROM meta WHERE key = 'repo_root'").get(); return typeof row?.value === "string" && row.value ? row.value : undefined; } catch { return undefined; } finally { try { db?.close(); } catch { /* already closed */ } } } // A folder that is a git checkout here: a repository or a worktree (`.git` file). function isCheckout(dir) { return fs.existsSync(path.join(dir, ".git")); } // When an archived fanout folder finished, mirroring the archive sweep: its manifest's finishedAt, else exit.txt's mtime. function archivedFinish(folder) { let manifest = {}; try { manifest = JSON.parse(fs.readFileSync(path.join(folder, "manifest.json"), "utf8")); } catch { /* none or torn */ } const exit = fs.lstatSync(path.join(folder, "exit.txt"), { throwIfNoEntry: false }); if (!FINISHED_STATUSES.has(String(manifest.status)) && !exit?.isFile()) return undefined; const finished = typeof manifest.finishedAt === "string" ? Date.parse(manifest.finishedAt) : NaN; return Number.isFinite(finished) ? finished : exit?.isFile() ? exit.mtimeMs : undefined; } /** * Code indexes whose recorded checkout is gone from this computer (missing, or no longer a git checkout) and * not rebuilt for `days`, and archived fanout folders finished `days` ago. An index without a readable record, * a locked one, a symlink, and everything else in `.runtime` or outside it are never candidates. */ function ageOutCandidates(phrenPath, days, now) { const cutoff = now.getTime() - days * DAY_MS; const candidates = []; const code = path.join(runtimeDir(phrenPath), "code"); for (const name of fs.existsSync(code) ? fs.readdirSync(code).sort() : []) { const project = name.endsWith(".sqlite") ? name.slice(0, -".sqlite".length) : ""; const file = path.join(code, name); const stat = fs.lstatSync(file, { throwIfNoEntry: false }); if (!isValidProjectName(project) || !stat?.isFile() || stat.mtimeMs > cutoff || fs.existsSync(`${file}.lock`)) continue; const repoRoot = indexRepoRoot(file); if (!repoRoot || isCheckout(repoRoot)) continue; candidates.push({ kind: "codeIndexes", target: file, bytes: stat.size }); } const archive = path.join(runtimeDir(phrenPath), "agent-fanouts-archive"); for (const name of fs.existsSync(archive) ? fs.readdirSync(archive).sort() : []) { const folder = path.join(archive, name); if (!fs.lstatSync(folder, { throwIfNoEntry: false })?.isDirectory()) continue; const finished = archivedFinish(folder); if (finished !== undefined && finished <= cutoff) candidates.push({ kind: "fanoutArchive", target: folder, bytes: bytes(folder) }); } return candidates; } function ageOutCounts(candidates) { const counts = { codeIndexes: { count: 0, bytes: 0 }, fanoutArchive: { count: 0, bytes: 0 } }; for (const { kind, bytes: size } of candidates) { counts[kind].count++; counts[kind].bytes += size; } return counts; } // Earlier daily lines that reported runtime classes; a torn line or one whose date cannot be read does not count. function reportLines(lines) { let count = 0; for (const line of lines) { try { const entry = JSON.parse(line); if (entry?.runtimeClasses && typeof entry.at === "string" && Number.isFinite(Date.parse(entry.at))) count++; } catch { /* a torn line */ } } return count; } /** * The opt-in this computer saw, kept in `.runtime` (never synced): its week starts the first day age-out is on here, * and turning it off forgets it. Ready once that week has passed and seven daily lines have reported runtime classes. */ function runtimeAgeOut(phrenPath, budgets, lines, now) { const stateFile = path.join(runtimeDir(phrenPath), RUNTIME_AGE_OUT_STATE); const candidates = ageOutCandidates(phrenPath, budgets.runtimeAgeOutDays, now); const reported = reportLines(lines); let enabledDays = null; let acted = false; if (!budgets.runtimeAgeOut) { fs.rmSync(stateFile, { force: true }); } else { const enabledSince = readRuntimeAgeOutState(phrenPath)?.enabledSince ?? now.toISOString(); enabledDays = Math.max(0, Math.floor((now.getTime() - Date.parse(enabledSince)) / DAY_MS)); acted = enabledDays >= AGE_OUT_WAIT_DAYS && reported >= AGE_OUT_WAIT_DAYS; atomicWriteText(stateFile, `${JSON.stringify({ enabledSince, ready: acted })}\n`); if (acted) for (const { target } of candidates) fs.rmSync(target, { recursive: true, force: true }); } return { enabled: budgets.runtimeAgeOut, days: budgets.runtimeAgeOutDays, enabledDays, reportLines: reported, acted, candidates: ageOutCounts(candidates) }; } function storeHealthFile(phrenPath) { return path.join(runtimeDir(phrenPath), "store-health.jsonl"); } export function recordStoreHealth(phrenPath, now = new Date(), computer = getMachineName()) { const file = storeHealthFile(phrenPath); return withFileLock(file, () => { const lines = fs.existsSync(file) ? fs.readFileSync(file, "utf8").split("\n").filter(line => line.trim()) : []; const today = now.toDateString(); for (const line of lines) { try { const entry = JSON.parse(line); if (typeof entry?.at === "string" && new Date(entry.at).toDateString() === today) return false; } catch { /* a torn line does not prevent the next daily sample */ } } // Age-out runs before the count, so the line's runtime bytes are what is left. A failed age-out never costs the day's line. let ageOut; try { ageOut = runtimeAgeOut(phrenPath, getRetentionPolicy(phrenPath).budgets, lines, now); } catch (err) { debugLog(`store health: runtime age-out failed: ${errorMessage(err)}`); } const entry = { ...collectStoreHealth(phrenPath, now, computer), ...(ageOut ? { runtimeAgeOut: ageOut } : {}) }; atomicWriteText(file, [...lines.slice(-399), JSON.stringify(entry)].join("\n") + "\n"); return true; }); } const COMPONENT_LABELS = { reviewEntries: "review entries", runtimeBytes: ".runtime", fanoutArchiveBytes: "fanout archive", }; export function budgetComponentName(row) { const scope = [row.store, row.project].filter(Boolean).join("/"); return scope ? `${scope} ${COMPONENT_LABELS[row.component]}` : COMPONENT_LABELS[row.component]; } // The newest valid daily line; one short line a day keeps a whole-file read cheap. export function readLatestStoreHealth(phrenPath) { const file = storeHealthFile(phrenPath); const lines = fs.existsSync(file) ? fs.readFileSync(file, "utf8").split("\n").filter(line => line.trim()) : []; for (const line of lines.reverse()) { try { const snapshot = JSON.parse(line); if (typeof snapshot?.at === "string" && Number.isFinite(Date.parse(snapshot.at)) && Array.isArray(snapshot.stores)) return snapshot; } catch { /* a torn or foreign line: try the one before */ } } return undefined; } /** Doctor's warn-only budgets line, from the latest daily health record: it never fails the check. */ export function budgetDoctorCheck(phrenPath) { const file = storeHealthFile(phrenPath); const lines = fs.existsSync(file) ? fs.readFileSync(file, "utf8").split("\n").filter(line => line.trim()) : []; let latest; for (const line of lines.reverse()) { try { latest = JSON.parse(line); } catch { continue; } if (latest?.budgets) break; latest = undefined; } if (!latest?.budgets || typeof latest.at !== "string") return { name: "budgets", ok: true, detail: "no store health recorded yet" }; const day = latest.at.slice(0, 10); if (!latest.budgets.over.length) return { name: "budgets", ok: true, detail: `within budget on ${day}` }; const mb = (n) => `${(n / 1024 / 1024).toFixed(1)} MB`; const rows = latest.budgets.over.map(row => { const amount = row.component.endsWith("Bytes") ? `${mb(row.value)} of ${mb(row.budget)}` : `${row.value} of ${row.budget}`; return `${budgetComponentName(row)} ${amount} (${row.effect})`; }); return { name: "budgets", ok: true, detail: `over budget on ${day}: ${rows.join("; ")}` }; }