/** * memory_events append-only log + lifecycle operations (ADR-002 decision * point 1, migration 007). The log is the source of truth for state * *changes*; the materialized `memories` row is updated in the SAME * transaction as the event append (log + tables in one file, not full event * sourcing — no view rebuilding, no projection framework). */ import type { DB } from './db.js'; import type { MemoryScope } from '../contracts/index.js'; export type MemoryEventType = 'create' | 'invalidate' | 'supersede' | 'promote_scope' | 'erase_request' | 'usefulness' | 'archive' | 'restore' /** * FEAT-442 reversal primitive: compensating event for `unsupersede()`, * un-hiding an atom that was previously superseded (via supersede() or * mergeAsSupersede()) — most commonly to reverse an autonomous * consolidation auto-accept, but works for any supersede. Local-only, * like 'archive'/'restore' — see MemoryEventRepo.unsupersede() and * src/sync/shipper.ts's exclusion list for why this never ships. */ | 'unsupersede' /** * FEAT-467 reversible soft-delete generation: the compensating action for * one atom in a purge batch (ADR-013 Decision 4 gate for FEAT-458's proxy * capture tier). Payload: `{ batch_id, capture_source }`. Deliberately * NOT the existing 'invalidate' type — a purge-caused hide must stay * distinguishable from a human-initiated invalidate() both for * unpurge()'s own precondition and so a manual invalidate is never * accidentally reactivated by a purge-batch restore. See * MemoryEventRepo.purgeBatch(). */ | 'purge' /** * FEAT-467 reversal: un-hides one atom previously purged as part of an * open batch (within its restore window). Payload: `{ batch_id }`. See * MemoryEventRepo.restorePurgeBatch(). */ | 'unpurge'; export interface MemoryEvent { seq: number; event_id: string; event_type: MemoryEventType; atom_id: string; payload_json: string | null; content_hash: string | null; created_at: number; synced_at: number | null; } export interface AppendEventInput { event_type: MemoryEventType; atom_id: string; /** JSON-serialized on write; use listForAtom + JSON.parse to read back. */ payload?: unknown; content_hash?: string | null; created_at?: number; /** * Globally-unique event identity (ADR-011). Local single-device writers * never pass this — it is always freshly generated (node:crypto * randomUUID()). Only the pull-ingest path (src/sync/puller.ts) writes * remote-originated events with their OWN pre-existing event_id, and it * does so via a raw dedup-insert, not through this method — append() is * exclusively the local-origin path. */ event_id?: string; } /** Memory not found for the requested lifecycle op — maps to HTTP 404. */ export declare class MemoryNotFoundError extends Error { readonly atomId: string; constructor(atomId: string); } /** State conflict (e.g. already invalidated) — maps to HTTP 409. */ export declare class MemoryConflictError extends Error { constructor(message: string); } /** Invalid scope transition (e.g. downward promotion) — maps to HTTP 400. */ export declare class InvalidScopeTransitionError extends Error { constructor(message: string); } /** * FEAT-467: an unknown purge batch id was passed to restorePurgeBatch() / * finalizePurgeBatch(). Subclasses MemoryNotFoundError (not a sibling type) * so the existing `mapLifecycleError` (src/server/routes/lifecycle.ts) maps * it to 404 with no new error-status convention — `err.atomId` holds the * batch id for this call site, read the same way the base class's own * `id`-in-body wiring already does. */ export declare class PurgeBatchNotFoundError extends MemoryNotFoundError { constructor(batchId: string); } export interface PurgeBatchResult { batchId: string; count: number; } export interface PurgeRestoreResult { restored: number; } export interface PurgeFinalizeResult { erased: number; } /** * FEAT-442 provenance marker for a `supersede()` caused by autonomous * consolidation auto-accept (similarity >= `consolidation.autoAcceptThreshold`), * as opposed to a human-initiated accept (ProposalRepo.accept with no * provenance) or the containment auto-merge path. Recorded on the * `supersede` event's payload (reuses the existing event type — see the * design-review amendment this satisfies: least-invasive shape that keeps * every existing `event_type` consumer, e.g. history/why/corrections * queries, working unchanged) rather than a distinct event type, so * `unsupersede()`/`memory_history`/`why_memory` can show why a merge * happened without a human in the loop, and an operator can distinguish an * auto-accepted row from a manual one in the consolidation_proposals * listing's history. */ export interface SupersedeProvenance { autoAccepted: true; /** The `consolidation.autoAcceptThreshold` in effect for the run that made this call. */ threshold: number; /** Cosine similarity of the winner/loser pair that triggered auto-accept. */ similarity: number; } /** * issue #190: which side of the loop produced a `memory_corrected` usefulness * event. `SupersedeProvenance` above only distinguishes ONE narrow opt-in * feature (autoAcceptThreshold) — it is `undefined` for BOTH of consolidate.ts's * own default-on auto-merge tiers (containment + mergeThreshold, `consolidate.ts` * `mergeAsSupersede` direct calls) AND a genuine human accepting a proposal via * `resolve_consolidation_proposal`, which made those two cases indistinguishable * — exactly what let 854 self-generated supersedes read as if they were external * feedback. `'consolidation_auto'` means the correction was produced by the * consolidation job with no human decision in the loop (any of: containment * auto-merge, mergeThreshold auto-merge, the containment-sweep auto-accept of a * pending proposal, or the opt-in autoAcceptThreshold auto-accept). `'external'` * (the default when omitted) means a human or direct API/MCP caller decided — * `invalidate_memory`/`supersede_memory`, a human resolving a proposal, or a * bulk `consolidate accept --all` operator action. */ export type CorrectionSource = 'consolidation_auto' | 'external'; export declare class MemoryEventRepo { private db; constructor(db: DB); /** Low-level append. Caller owns the transaction boundary. */ append(event: AppendEventInput): number; /** Full event log for one atom, ordered by seq (append order). */ listForAtom(atomId: string): MemoryEvent[]; /** * Invalidate a memory: sets valid_to = now, appends an 'invalidate' event, * both in one transaction. Idempotent-safe: throws MemoryConflictError if * the memory is already invalid rather than silently double-invalidating. */ invalidate(atomId: string, reason?: string): void; /** * Supersede oldId with newId: sets old.valid_to = now AND * old.superseded_by = newId, appends a 'supersede' event on oldId — all in * one transaction. Validates both ids exist; oldId must currently be valid * (not already invalidated/superseded); newId must be valid (valid_to null). * * FEAT-401 fix: also co-issues newId.valid_from = the SAME `now` used for * old.valid_to. Without this, newId keeps the valid_from it was stamped * with at insert() time (which normally predates the supersede() call by * however long the caller took to decide to supersede), producing an * overlap window [newId.valid_from, oldId.valid_to) where an as_of query * would match BOTH generations — a bitemporal-window leak. Co-issuing the * boundary keeps the two windows contiguous and non-overlapping * (old.valid_to === new.valid_from), matching the half-open-interval * model as_of resolution assumes (ADR-001, FEAT-401 AC-1/AC-5): the * successor becomes "the believed value" exactly as of the supersede * instant, not as of whenever it happened to be inserted. * * FEAT-402 AC-3: also deletes oldId's memory_entities links in the same * transaction. The memories row itself is NOT deleted (bitemporal history * is kept for as_of queries), so this can't rely on the erase() path's * ON DELETE CASCADE — it's explicit here instead. A superseded memory's * facts should stop counting as current evidence for an entity even * though the row survives for audit. * * `provenance` is optional and additive (FEAT-442): omitted, the payload is * byte-identical to pre-FEAT-442 behavior (`{ superseded_by: newId }`). * Passed, it merges in `auto_accepted: true` + `threshold` + `similarity` * — see {@link SupersedeProvenance}. Reversible via `unsupersede()`. * * `correctionSource` (issue #190, additive, default `'external'`) is * INDEPENDENT of `provenance` — it tags the `memory_corrected` usefulness * event (not the `supersede` event payload above) with who decided this * pair merges, so `feedbackLoopSummary()` can tell a fully-automatic * consolidation self-merge apart from a human/API-initiated one. See * {@link CorrectionSource}. */ supersede(oldId: string, newId: string, provenance?: SupersedeProvenance, correctionSource?: CorrectionSource): void; /** * Archive a memory (FEAT-404 TTL/archival tiers): sets valid_to = now + * archived = 1 + archived_at = now, appends an 'archive' event — all in * one transaction. Reuses the exact non-destructive valid_to bookkeeping * invalidate()/supersede() already use (row survives, restorable via * restore()) — the only difference is *why* the row stopped being * current, which is why this is a distinct event type rather than a * relabeled invalidate(). * * Deliberately does NOT append a 'memory_corrected' usefulness signal the * way invalidate()/supersede() do: staleness-triggered archival is * routine housekeeping driven by low usefulness, not a correction of a * wrong/superseded fact — appending one would create a feedback loop * that further penalizes an atom the policy might restore later. * * Throws MemoryConflictError if the memory is already invalid (including * already archived) rather than silently double-archiving. */ archive(atomId: string, reason?: string): void; /** * Restore a previously-archived memory (FEAT-404 AC-4): clears valid_to * and archived/archived_at, appends a 'restore' event — all in one * transaction. The memory immediately re-qualifies for every * `valid_to IS NULL` candidate filter (search.ts, recall/pack.ts), so it * reappears in search with no further wiring needed. * * Throws MemoryNotFoundError if the id doesn't exist, MemoryConflictError * if it isn't currently archived (e.g. it was invalidated/superseded * through a different path, or was never archived). */ restore(atomId: string): void; /** * Reverse a supersede (FEAT-442 reversal primitive): un-hides a memory * that was superseded — clears `valid_to`/`superseded_by` via a * compensating `unsupersede` event (never a destructive row edit outside * a transaction that also appends the event, matching every other * lifecycle op in this file), restoring it to "current" status. * * Semantics — this is UN-HIDING, not UN-MERGING: it reverses the LOSER's * currency only. The WINNER (and the `derived_from` lineage it gained via * `mergeAsSupersede`) is untouched — the merge relationship stays on * record, and after this call BOTH atoms are simply valid/current again. * Nothing here removes the loser id from the winner's lineage, and * `memory_entities` links deleted by `supersede()`'s AC-3 cleanup are NOT * recomputed (that requires re-running entity extraction, not a * mechanical reversal) — a restored atom's entity links are a known * follow-up gap, not silently pretended-fixed here. If consolidation runs * again, the pair may be re-proposed or re-merged like any other similar * pair; this method does not suppress that. * * Exists primarily to undo an autonomous-consolidation auto-accept * (FEAT-442 `consolidation.autoAcceptThreshold`) that turns out to be a * bad merge, but it works for ANY supersede (manual accept, containment * auto-merge, or auto-accept) — scoped by state, not by cause. * * Throws MemoryNotFoundError if the id doesn't exist. Throws * MemoryConflictError if the memory is archived (archived === 1 — use * `restore()` instead, the two primitives never overlap) or was never * superseded (`superseded_by` is null — e.g. it is currently valid, or it * was invalidated/erased/archived through a different path). */ unsupersede(atomId: string, reason?: string): void; /** * Open a purge batch (FEAT-467 reversible soft-delete generation, ADR-013 * Decision 4 gate): soft-invalidates every atom in `atomIds` via a new * `purge` event (never `invalidate` — see the `MemoryEventType` doc * comment) and persists one `purge_batches` audit row (batch id, * `captureSource`, atom count, restore-deadline time range), all in one * transaction. Each `purge` event's own payload also carries `batch_id` * and `restore_deadline_at` (FEAT-467 AC-6): `GET /memory/:id/history` * and the MCP `memory_history`/`why_memory` tools surface `memory_events` * rows as-is with no batch lookup of their own, so the deadline must live * on the event payload itself to be visible there — `purge_batches` * remains the source of truth this is read from at write time. * `capture_source` is a plain parameter with no branching — this * primitive is identical for `'auto_capture'` and `'proxy'` (FEAT-467 * AC-5). * * Ids that don't exist, or are already invalid (`valid_to` set — including * previously purged/archived/superseded/erased), are silently skipped * rather than failing the whole batch — the same per-id tolerance * `runAutoCaptureDisable`'s existing erase loop already relies on. `count` * (and the persisted `atom_count`) reflect only atoms actually purged by * THIS call, which is why callers should pre-scope `atomIds` themselves * (e.g. `WHERE capture_source = ? AND valid_to IS NULL`) rather than rely * on this method to filter a large candidate set efficiently. * * `memories.purge_batch_id` is set to the new batch id on every purged * row — the single marker `restorePurgeBatch()`/`finalizePurgeBatch()` * scope their work by (see those methods' doc comments for why this * doubles as the "untouched by an unrelated lifecycle call" guard). */ purgeBatch(atomIds: string[], captureSource: string, restoreWindowDays: number): PurgeBatchResult; /** * Reverse an open purge batch within its restore window (FEAT-467 AC-2): * every atom still marked `purge_batch_id = batchId` and currently invalid * has `valid_to`/`purge_batch_id` cleared and an `unpurge` event appended; * the batch's audit record is marked `restored`. All in one transaction. * * Scoping the un-hide to `purge_batch_id = ? AND valid_to IS NOT NULL` * (rather than replaying `atomIds` from the batch's `purge` events * directly) is what leaves an atom independently changed since the purge * untouched: nothing else in this file's lifecycle API can move an * already-invalid atom's `valid_to` back to non-null except this method * and `unsupersede()`/`restore()` (which require `superseded_by`/`archived` * state a purge never sets), so an atom that no longer carries this * batch's marker was moved by something else and is correctly skipped. * * Throws `PurgeBatchNotFoundError` for an unknown batch id. * Throws `MemoryConflictError` if the batch is already `restored` or * `finalized`, or if `restore_deadline_at` has already elapsed — a stale * restore attempt is a conflict, not a silent no-op; call * `finalizePurgeBatch()` instead once the window has passed. */ restorePurgeBatch(batchId: string): PurgeRestoreResult; /** * Finalize a purge batch (FEAT-467 AC-3): every atom still marked * `purge_batch_id = batchId` and currently invalid is HARD-erased via the * existing, unmodified `erase()` (row + vector index + FTS gone, * `erase_request` tombstone appended) — preserving ADR-006's "erasure * always wins, eventually" guarantee. The batch's audit record is marked * `finalized`. All in one transaction (better-sqlite3 nests `erase()`'s * own transaction as a savepoint). * * Callable once the restore window has elapsed (the scheduled-sweep path) * OR explicitly early, by design (an operator who wants to finalize before * the window ends) — unlike `restorePurgeBatch()`, this method does not * check `restore_deadline_at`. * * Throws `PurgeBatchNotFoundError` for an unknown batch id. Throws * `MemoryConflictError` if the batch was already `finalized` or already * `restored` (nothing left to erase via this path). */ finalizePurgeBatch(batchId: string): PurgeFinalizeResult; /** * Promote a memory's scope: private -> team -> org (ADR-009). Only * upward promotions are allowed; same-scope or downward requests throw * InvalidScopeTransitionError. State update + event append in one * transaction. */ promoteScope(atomId: string, toScope: MemoryScope): void; /** * Erase a memory (erasure v1, ADR-006 W5): HARD-deletes the memories row * (FTS cleans up via the AFTER DELETE trigger) and its vector index row, * and appends an 'erase_request' event — the event IS the tombstone: * - payload.scope lets the sync shipper (ADR-009) ship the tombstone to * the cloud even though the memories row is gone, * - content_hash = the memory's hash powers the replay filter (stage 8 * refuses to resurrect an erased memory on re-distillation). * Erasure always wins over retention/replay — the text is unrecoverable * locally after this call. */ erase(atomId: string, reason?: string): void; /** * Replay filter (erasure v1): true when a memory with this content hash * has been erased — re-distillation must not resurrect it. */ isErasedHash(contentHash: string): boolean; }