/** * One-shot cleanup for junk `entities` rows written BEFORE the write-path guard * existed (issue #207 / PR #218-#219). * * The guard (`filterAtomEntities` -> `classifyJunkEntityName`) blocks NEW junk * from ever being persisted. It does not retroactively clean rows that predate * it — the doctor "Entity name quality" canary says exactly this, and reports * the standing debt (62/5012 = 1.2% on prod as of 2026-07-13). * * THE RULE IS NOT REDEFINED HERE. This imports `classifyJunkEntityName`, the * same function the write guard and the doctor canary call. If the purge used * its own copy of the rule, the three could drift and the purge would start * deleting rows the guard would happily have kept — which is exactly how "C" * and "R" (real programming languages) got eaten by a flat length floor in the * first place. One rule, three callers, no duplication. * * SCOPE — this deletes ENTITY rows, never MEMORY rows. A memory whose only * entity was junk keeps its text, its embedding, and its searchability; it * simply loses a link to a garbage name. Deleting memories is not something * this command can do, by construction. * * Cascade: `memory_entities` and `entity_relations` both declare * `REFERENCES entities(id) ON DELETE CASCADE`, and `src/storage/db.ts` sets * `PRAGMA foreign_keys = ON`, so removing an entity removes its links and its * relation triples in the same statement. The counts are measured BEFORE the * delete so the report says what was actually destroyed. */ import type { DB } from '../storage/db.js'; export interface JunkEntity { id: string; name: string; kind: string | null; reason: string; /** memory_entities rows that will cascade-delete with this entity. */ links: number; /** entity_relations rows (subject or object) that will cascade-delete. */ relations: number; } export interface PurgeJunkResult { /** Total entity rows scanned. */ scanned: number; /** Rows classified junk by `classifyJunkEntityName`. */ junk: JunkEntity[]; /** Junk rows actually deleted (0 when dryRun). */ deleted: number; /** memory_entities rows cascade-deleted (0 when dryRun). */ linksDeleted: number; /** entity_relations rows cascade-deleted (0 when dryRun). */ relationsDeleted: number; /** Memories that lost their LAST entity link — they are now unlinked. */ memoriesLeftUnlinked: number; dryRun: boolean; } /** * Identify (and optionally delete) junk entity rows. * * `dryRun` defaults to TRUE. Deleting rows from a user's production memory * store is irreversible, so the caller must opt in explicitly; a mistyped flag * should preview, never destroy. */ export declare function purgeJunkEntities(db: DB, opts?: { dryRun?: boolean; }): PurgeJunkResult;