// The audit log: one line per tool call decision and outcome, across all sessions. // // Session logs answer "what did we talk about". The audit log answers "what did the assistant // try to do, what was decided, and who decided it". It is append-only like the session logs. import { appendFile, mkdir, readFile } from 'node:fs/promises'; import path from 'node:path'; import type { Decision } from './policy.ts'; export interface AuditEntry { ts: string; session: string; turnId: string; tool: string; /** One-line description of the call (no full file contents). */ summary: string; decision: Decision | 'approved' | 'denied-by-owner' | 'invalid'; reason: string; /** "ok", "error", "not-run", or "pending" (waiting for the owner). */ outcome: 'ok' | 'error' | 'not-run' | 'pending'; ms?: number; } export class AuditLog { readonly file: string; constructor(dataDir: string) { this.file = path.join(dataDir, 'audit.jsonl'); } async append(entry: AuditEntry): Promise { await mkdir(path.dirname(this.file), { recursive: true }); await appendFile(this.file, JSON.stringify(entry) + '\n', 'utf8'); } async tail(count: number): Promise { try { const lines = (await readFile(this.file, 'utf8')).trim().split('\n').filter(Boolean); return lines.slice(-count).flatMap((line) => { try { return [JSON.parse(line) as AuditEntry]; } catch { return []; } }); } catch (err) { if ((err as NodeJS.ErrnoException).code === 'ENOENT') return []; throw err; } } }