/** * Tiered disk-artifact retention (FEAT-493). * * Boundary (arch-review 2026-07-14): this module owns the DISK-ARTIFACT axis * ONLY. It NEVER touches `memories` rows — that axis is * `src/storage/archival.ts` (FEAT-404)'s exclusive domain (non-destructive * valid_to+archived flag, age + trailing-usefulness policy). Do not entangle * the two. Distilled memories and transcripts are never deleted by this * module, in ANY mode/tier — see `scopedKinds` below for why 'all' still * only ever reaches `/artifacts//` files, never a DB row. * * NOT a DB-backed registry — deliberately. `artifacts` (job_id, kind, * content_path, created_at), the day-one table that looked like the obvious * home for this, was DROPPED in migrations/040-drop-dead-tables.sql (zero * writers ever, confirmed by grep + a live-DB COUNT(*), IRREVERSIBLE — see * that migration's own doc comment). Do not resurrect it. Instead this scans * a plain directory layout on disk: `/artifacts//` — * each immediate subdirectory name IS the `kind` bucket. Nothing writes into * this directory yet either (no producer exists in this repo today), so a * scan against a real install legitimately finds the directory absent and * returns zero candidates — expected, not a bug. * * Deletion (this slice — un-clamped from the #317 scaffolding): `'off'` is a * hard no-op (no filesystem access at all). `'dry-run'` scans and reports * only — never deletes. `'enabled'` scans, then unlinks every candidate file, * one at a time, tolerating a per-file unlink failure without aborting the * rest of the pass (same "never block a background pass on one bad path" * posture the rest of this pipeline uses — see cleanup.ts's vec-index * sweep). `config.ts`'s `applyRetentionGovernance` no longer downgrades * `'enabled'` to `'dry-run'` — see that function's doc comment for the * removed clamp. * * `config.retention.heavyArtifact` (size threshold + type list) is * deliberately config-driven rather than a hardcoded kind list, precisely so * a future producer (L1-1 / audio ingest / FEAT-489, consent-ADR gated, not * yet dispatched) can register real kinds (as subdirectory names under * `/artifacts/`) and finalize the predicate against its actual * output sizes without reworking this module's shape. TBD: pick real * `heavyArtifact.types` once that lands — `types` ships empty until then, so * every scan legitimately finds zero candidates on a fresh install. * * Wired onto the cleanup job's cadence (src/pipeline/handlers/cleanup.ts, * alongside `runArchivalPass` / `pruneIngestIdempotency`) using * `config.retention.tier` as the active dial. Sweep results are persisted to * a small JSON stats file (`writeSweepStats`/`readSweepStats` below) — a * plain file, not a DB table, for the same "don't resurrect the dropped * `artifacts` table" reason the candidate scan itself is filesystem-only. */ import type { Config } from '../config/config.js'; export type RetentionMode = Config['retention']['mode']; export type RetentionTier = 'media' | 'lean' | 'all'; export interface RetentionCandidate { kind: string; path: string; sizeBytes: number; mtimeMs: number; } export interface RetentionScanResult { tier: RetentionTier; mode: RetentionMode; /** Files that clear age + kind + size — what a dry-run pass reports, and * what an 'enabled' pass attempts to delete (see `RetentionSweepResult` * for the actual delete outcome: `deleted` may be a subset of this list * if any individual unlink failed). */ candidates: RetentionCandidate[]; totalBytes: number; byKind: Record; scannedAt: number; } /** Default artifacts root: `/artifacts`. Override via * `retention.artifactsDir` (test seam / non-default layouts). */ export declare function defaultArtifactsDir(dataDir: string): string; /** * Scans `/artifacts//` (one directory per scoped kind) for * files older than `retention.ageThresholdDays` (by mtime) and at/above * `heavyArtifact.minSizeBytes`. Read-only — never deletes anything; this is * the shared scan every mode (including `enabled`, once wired) will use. * * Missing directories (the expected state today — no producer exists yet) * and unreadable entries are skipped rather than thrown, matching the "never * block a background pass on one bad path" posture the rest of this * pipeline uses (see cleanup.ts's vec-index sweep for the same shape). */ export declare function scanRetentionCandidates(dataDir: string, retention: Config['retention'], tier: RetentionTier, now?: number): RetentionScanResult; export interface RetentionSweepResult extends RetentionScanResult { /** Candidates actually unlinked this pass. Always empty for `'off'` and * `'dry-run'` — only `'enabled'` ever populates this. */ deleted: RetentionCandidate[]; /** Sum of `deleted[].sizeBytes` — bytes actually reclaimed, as opposed to * `totalBytes` (bytes that WOULD be reclaimed, i.e. the dry-run number). */ deletedBytes: number; /** Per-file unlink failures. Non-fatal — a bad path (permission denied, * gone between scan and unlink) never aborts the rest of the pass. */ errors: Array<{ path: string; message: string; }>; } /** * Retention pass entry point — mirrors `runArchivalPass`'s mode-dispatch * shape (src/storage/archival.ts). `'off'` is a hard no-op (matches * `runArchivalPass`'s `!config.archival.enabled` short-circuit — no * filesystem access at all). `'dry-run'` scans and reports only. `'enabled'` * scans, then unlinks every candidate, tolerating per-file failures (see * module doc comment). */ export declare function runRetentionPass(dataDir: string, retention: Config['retention'], tier: RetentionTier, now?: number): RetentionSweepResult; export interface RetentionSweepStats { tier: RetentionTier; mode: RetentionMode; scannedAt: number; candidateCount: number; totalBytes: number; deletedCount: number; deletedBytes: number; byKind: Record; errorCount: number; } /** Default sweep-stats path: `/retention-last-sweep.json`. A plain * JSON file, not a DB table — same rationale as the candidate scan itself * (see module doc comment: don't resurrect the dropped `artifacts` table). * Override via the `path` param (test seam). */ export declare function defaultSweepStatsPath(dataDir: string): string; /** Best-effort — a stats-persistence failure (e.g. read-only disk) must * never fail the cleanup pass that just successfully reclaimed real disk * space, so this swallows and does not throw. */ export declare function writeSweepStats(dataDir: string, result: RetentionSweepResult, path?: string): void; /** Best-effort read of the last persisted sweep. Returns `null` when no * sweep has ever run (fresh install / retention still `'off'`) or the file * is missing/unreadable/corrupt — never throws. */ export declare function readSweepStats(dataDir: string, path?: string): RetentionSweepStats | null;