/** * @module memory/brain-sweep-executor * * T1147 Wave 7: BRAIN noise sweep executor. * * Applies the actions staged in `brain_observations_staging` (produced by W7-3 * `detectNoiseCandidates`) to the live brain tables, inside a single * SQLite transaction. The transaction is fenced by a Sentient self-healing * gate (Option A): the existing `killSwitch` field in `.cleo/sentient-state.json` * is toggled to `true` before the tx begins, and restored to `false` on both * commit and rollback. * * Self-healing gate semantics: * - Before tx: read existing `sentient-state.json`, save original `killSwitch` value. * - Write `{ ...existing, killSwitch: true, sweepGateActive: true, sweepRunId: }`. * - Open cutover tx: apply updates + deletes to live tables. * - Commit/rollback: write `{ ...existing, killSwitch: originalKillSwitch, sweepGateActive: false }`. * * Note: This temporarily co-opts `killSwitch` semantics for sweep gating. A * future cleanup (T1148 W8) should add a dedicated `sweepLock` field (Option B) * and teach the Sentient v1 daemon to check it separately. * * WAL deadlock mitigation: * - `PRAGMA busy_timeout = 10000` is set before the cutover tx. * - Document: sweep should not run while a CLEO session is active (session-end * hook triggers `runConsolidation` which also writes to brain.db). * * Vector sync: * - W7-4 does NOT trigger `populateEmbeddings` during sweep (OOM risk on 2440+ entries). * - For `purge` actions, entries are logically invalidated (invalid_at set); their * `brain_embeddings` rows are left orphaned. A separate optional post-sweep step * (`cleo memory sweep --rebuild-embeddings`) should clean them up. * * @task T1147 * @epic T1075 */ /** Options for the sweep executor. */ export interface SweepExecutorOptions { /** Absolute path to the project root. */ projectRoot: string; /** The run ID to approve (`brain_backfill_runs.id` of kind `noise-sweep-2440`). */ runId: string; /** Identity of the approver (agent ID or 'autonomous'). */ approvedBy?: string; } /** Result of the sweep executor. */ export interface SweepExecutorResult { /** Run ID that was applied. */ runId: string; /** Total candidates processed. */ totalCandidates: number; /** Number of entries that had `invalid_at` set (`purge` action). */ purged: number; /** Number of entries that had `provenance_class` set to `swept-clean`. */ kept: number; /** Number of entries skipped (already invalidated or not found). */ skipped: number; /** Whether the run completed successfully. */ success: boolean; /** Error message if `success === false`. */ errorMessage?: string; } /** * Applies the sweep actions from `brain_observations_staging` to the live brain tables. * * The execution flow: * 1. Verify the run exists in `brain_backfill_runs` with status `staged`. * 2. Load all `brain_observations_staging` rows for this run. * 3. Enable the self-healing gate (killSwitch → true). * 4. Set `PRAGMA busy_timeout = 10000` via the native DB handle. * 5. Open a single SQLite transaction: * a. For `purge` rows: `UPDATE SET invalid_at = ?, provenance_class = 'noise-purged'`. * b. For `keep`/`reclassify`/`promote` rows: `UPDATE
SET provenance_class = 'swept-clean' [, quality_score = ?]`. * c. Mark each candidate as `applied` or `skipped` in `brain_observations_staging`. * d. Update `brain_backfill_runs.status = 'approved'` + `approved_at` + `approved_by`. * 6. Commit tx → restore sweep gate. * 7. On any error → rollback tx (implicit via exception) → restore sweep gate. * * @param options - Executor options including projectRoot, runId, and approvedBy. * @returns Result summary of the sweep execution. * * @example * ```typescript * const result = await executeSweep({ * projectRoot: '/mnt/projects/cleocode', * runId: 'bfr-abc123-xyz789', * approvedBy: 'autonomous', * }); * console.log(`Purged: ${result.purged}, Kept: ${result.kept}`); * ``` */ export declare function executeSweep(options: SweepExecutorOptions): Promise; /** * Rolls back a staged sweep run without applying any changes. * * Sets `brain_backfill_runs.status = 'rolled-back'` and marks all pending * candidates as `skipped`. Does not toggle the self-healing gate (no tx needed). * * @param projectRoot - Absolute path to the project root. * @param runId - The run ID to roll back. * @returns True if the rollback succeeded, false if the run was not found or * already settled. */ export declare function rollbackSweep(projectRoot: string, runId: string): Promise; //# sourceMappingURL=brain-sweep-executor.d.ts.map