/** * Brain Maintenance Runner * * Combines temporal decay, memory consolidation, and embedding backfill * into a single idempotent maintenance pass. Designed to be run on a * schedule or on-demand via `cleo brain maintenance`. * * Steps run in order: * 1. Temporal decay — reduce confidence of stale learnings * 2. Consolidation — merge duplicate/similar old observations * 3. Embedding backfill — populate vectors for observations without them * * Each step is individually opt-outable via skip flags, making the * operation safe to re-run at any frequency. * * @task T143 * @epic T134 * @why Enable scheduled brain optimization via single command * @what Combined maintenance runner with CLI command and progress reporting */ /** Temporal decay step result subset used in maintenance output. */ export interface BrainMaintenanceDecayResult { /** Number of learnings whose confidence was updated. */ affected: number; } /** Memory consolidation step result subset used in maintenance output. */ export interface BrainMaintenanceConsolidationResult { /** Number of new summary observations created. */ merged: number; /** Number of original observations archived. */ removed: number; } /** Orphaned reference reconciliation step result. */ export interface BrainMaintenanceReconciliationResult { /** Decisions with stale task/epic references fixed. */ decisionsFixed: number; /** Observations with stale session references fixed. */ observationsFixed: number; /** Memory links with stale task references removed. */ linksRemoved: number; } /** Embedding backfill step result. */ export interface BrainMaintenanceEmbeddingsResult { /** Observations successfully embedded. */ processed: number; /** Observations skipped (no provider or no narrative). */ skipped: number; /** Observations that failed embedding. */ errors: number; } /** Tier promotion step result. */ export interface BrainMaintenanceTierPromotionResult { /** Number of entries promoted (short→medium or medium→long). */ promoted: number; /** Number of stale short-tier entries soft-evicted. */ evicted: number; } /** Deriver batch step result (T1145). */ export interface BrainMaintenanceDeriverResult { /** Number of items successfully derived (completed). */ processed: number; /** Number of items that failed or were re-queued. */ failed: number; /** Number of stale in_progress items re-queued before the batch. */ staleRequeued: number; } /** * Options for {@link runPruneSweep}. */ export interface PruneSweepOptions { /** * When true, log the would-delete count but do NOT execute the DELETE. * Returns the full result struct with `deleted = 0` and `wouldDelete` set. * Default: false. */ dryRun?: boolean; /** * Cap total deletes across all tables per invocation. * Reads `brain.sweeper.maxDeletePerRun` from project config; falls back to * {@link DEFAULT_MAX_DELETE_PER_RUN} (500). */ maxDeletePerRun?: number; } /** * Result from a single {@link runPruneSweep} invocation. */ export interface PruneSweepResult { /** * Total rows deleted across all four typed brain tables. * Always 0 in dry-run mode. */ deleted: number; /** * Would-delete count when `dryRun=true`; 0 otherwise. * This is the count of qualifying rows before the cap is applied. */ wouldDelete: number; /** * Whether the run was a dry-run (no mutations made). */ dryRun: boolean; /** * Per-table breakdown of rows deleted (or would-delete in dry-run). */ byTable: Record; } /** * Aggregated result from a full brain maintenance run. * * All counts are zero when a step is skipped via the corresponding * `skip*` option. */ export interface BrainMaintenanceResult { /** Results from the temporal decay step. */ decay: BrainMaintenanceDecayResult; /** Results from the memory consolidation step. */ consolidation: BrainMaintenanceConsolidationResult; /** Results from the cross-DB orphaned reference reconciliation step. */ reconciliation: BrainMaintenanceReconciliationResult; /** Results from the tier promotion step. */ tierPromotion: BrainMaintenanceTierPromotionResult; /** Results from the embedding backfill step. */ embeddings: BrainMaintenanceEmbeddingsResult; /** Results from the Step 9f prune sweep (T995). */ pruneSweep: PruneSweepResult; /** Results from the deriver batch step (T1145). */ deriver: BrainMaintenanceDeriverResult; /** Total wall-clock duration of the maintenance run in milliseconds. */ duration: number; } /** * Options for {@link runBrainMaintenance}. * * All `skip*` flags default to `false` — the full maintenance pass runs * unless specific steps are disabled. */ export interface BrainMaintenanceOptions { /** Skip the temporal decay step. Default: false. */ skipDecay?: boolean; /** Skip the memory consolidation step. Default: false. */ skipConsolidation?: boolean; /** Skip the cross-DB orphaned reference reconciliation step. Default: false. */ skipReconciliation?: boolean; /** Skip the tier promotion step (short→medium, medium→long). Default: false. */ skipTierPromotion?: boolean; /** Skip the embedding backfill step. Default: false. */ skipEmbeddings?: boolean; /** Skip the Step 9f prune sweep (T995). Default: false. */ skipPruneSweep?: boolean; /** * Run Step 9f in dry-run mode (log count, no DELETE). * Default: false. */ pruneSweepDryRun?: boolean; /** Skip the deriver batch step (T1145). Default: false. */ skipDeriver?: boolean; /** * Progress callback invoked before each step starts and after * completion of each sub-item. * * @param step - Human-readable step name (e.g. "decay", "consolidation", "embeddings") * @param current - Items processed so far within the current step (0 before step starts) * @param total - Total items expected for the current step (0 if unknown before start) */ onProgress?: (step: string, current: number, total: number) => void; } /** * Hard-sweeper: DELETE brain entries that are confirmed noise. * * A row qualifies for deletion when ALL of: * - `prune_candidate = 1` (flagged by `correlateOutcomes` Step 9a.5) * - `quality_score < 0.2` (low quality) * - `citation_count = 0` (never cited) * - age > 30 days (`julianday('now') - julianday(created_at) > 30`) * * Safety mechanisms: * - **Dry-run mode** (`options.dryRun = true`) — counts qualifying rows and * returns without mutating the database. * - **Per-run cap** (`options.maxDeletePerRun`, default 500) — limits total * deletes across all tables per invocation to prevent runaway deletes. * - **Audit trail** — inserts a row into `brain_consolidation_events` with * `trigger = 'step-9f'` after each real (non-dry) delete run. * * This function is idempotent: rows that already do not exist or that no * longer meet the predicate are silently skipped. * * @param projectRoot - Absolute path to the project root (locates brain.db) * @param options - Dry-run and per-run cap controls * @returns - Deleted/would-delete counts plus per-table breakdown * * @task T995 * @epic T991 */ export declare function runPruneSweep(projectRoot: string, options?: PruneSweepOptions): Promise; /** * Run a combined brain maintenance pass: decay, consolidation, and embeddings. * * The three steps always run in the same order: * 1. `applyTemporalDecay` — decay stale learning confidence values * 2. `consolidateMemories` — merge clustered old observations * 3. `populateEmbeddings` — backfill missing vectors * * Each step is optional via the `skip*` flags. The function is idempotent: * re-running it when there is nothing to process returns zero counts. * * @param projectRoot - Absolute path to the project root (used to locate brain.db) * @param options - Optional skip flags and progress callback * @returns Aggregated counts from each step plus total wall-clock duration * * @example * ```ts * const result = await runBrainMaintenance('/my/project', { * onProgress: (step, current, total) => { * console.log(`[${step}] ${current}/${total}`); * }, * }); * console.log(`Done in ${result.duration}ms`); * ``` * * @task T143 * @epic T134 */ export declare function runBrainMaintenance(projectRoot: string, options?: BrainMaintenanceOptions): Promise; //# sourceMappingURL=brain-maintenance.d.ts.map