import { type Database } from "./database.js"; import { type SecretRedactionMatcher } from "./session-secret-redaction-text.js"; export * from "./session-secret-redaction-text.js"; /** * The secret history scrub (docs/secrets.md, "Removing secrets from stored * history"): find every stored copy of a secret in chosen sessions and rewrite * it to a `[redacted: ...]` marker, or only count the copies (dry run). * * Every table is read in bounded pages, each page in its own transaction * under the caller's row-level security and the workspace's shared tenancy * fence, like any other session writer. A cell is rewritten only while it * still holds the text that was read (compare and swap on its md5), so a * concurrent writer is never overwritten; whatever it wrote is caught by the * next run. Matching is done here, never in SQL, and nothing in the report or * an error is ever text from a row. * * Cells the runtime role may not read or change (goal text, goal revisions, a * turn's frozen goal, task notes, the message search mirror, content blobs, * MCP results, the host export outbox and the repair audit) go through the two * 1091 owner routines. Their runtime EXECUTE is withheld for one release; until * it is granted those tables are reported as pending and what can be read is * still counted. */ export type SecretRedactionCounts = { /** Rows holding at least one match when read. */ rowsMatched: number; /** Rows rewritten (or blobs deleted). Always 0 in a dry run. */ rowsChanged: number; /** Matches found when read. */ matches: number; }; export type SessionSecretRedactionFailure = { sessionId: string | null; table: string; /** An error class or SQLSTATE; never a message, which could carry text. */ code: string; }; export type SessionSecretRedactionReport = { dryRun: boolean; sessions: Array<{ sessionId: string; archived: boolean; tables: Record; }>; tables: Record; /** Tables that need the 1091 routines, whose runtime grant is still pending. */ pending: string[]; failures: SessionSecretRedactionFailure[]; /** * Per session, content blobs a rewritten snapshot no longer refers to that * this run could not delete (the cell writer is not granted yet). Digests * only; never returned by the API. */ orphanedBlobs: Record; }; export type SessionSecretRedactionScope = { accountId: string; workspaceId: string; }; export type SessionSecretRedactionOptions = { sessionIds: readonly string[]; matchers: readonly SecretRedactionMatcher[]; dryRun: boolean; /** Rows per page and transaction. Default 50. */ pageRows?: number; /** * Rewrite model-context snapshots whose content lives in blobs even before * the 1091 cell writer is granted. Their old blobs then stay until an owner * deletes them (`orphanedBlobs` in the report). Only for an operator's * one-off run; the API leaves such snapshots pending instead. */ rewriteReferencedSnapshotsWhilePending?: boolean; }; /** Most sessions one run accepts, descendants included. */ export declare const SESSION_SECRET_REDACTION_MAX_SESSIONS = 200; /** Every table a run reports, in report order. */ export declare const SESSION_SECRET_REDACTION_TABLES: readonly string[]; /** * A table's content columns stored as text rather than JSON, so a row read * back as JSON (the archive bundle) uses the text form of the lossless codec. */ export declare function sessionSecretRedactionTextColumns(table: string): ReadonlySet; /** Whether the runtime may call both 1091 routines yet. A separate statement. */ export declare function sessionSecretRedactionRoutinesGranted(db: Pick): Promise; /** * The given sessions plus every descendant visible to the caller, in * discovery order, at most `limit`. Unknown or invisible IDs are dropped. */ export declare function listSessionsForSecretRedaction(db: Database, scope: SessionSecretRedactionScope, input: { sessionIds: readonly string[]; includeDescendants: boolean; limit?: number; }): Promise>; export declare class SessionSecretRedactionTooManySessionsError extends Error { readonly limit: number; readonly code = "secret_redaction_too_many_sessions"; constructor(limit: number); } /** * An archived session's manifest and the md5 of its stored JSON, which the * swap below compares before it writes. Null when the session is not archived. */ export declare function readSessionArchiveManifestForRedaction(db: Database, scope: SessionSecretRedactionScope, sessionId: string): Promise<{ manifest: Record; before: string; } | null>; /** * Point an archived session at rewritten archive objects, only while its * manifest is still the one read (`before`). The replaced objects are queued * for deletion in the same transaction. Needs the 1091 cell writer. */ export declare function replaceSessionArchiveManifestForRedaction(db: Database, scope: SessionSecretRedactionScope, input: { sessionId: string; before: string; manifest: Record; }): Promise; /** * Scrub (or, with `dryRun`, count) every stored copy the matchers find in the * given sessions. The caller authorizes the sessions; this never widens them. * Failures are recorded per table and session and never stop the run. */ export declare function scrubSessionSecretHistory(db: Database, scope: SessionSecretRedactionScope, options: SessionSecretRedactionOptions): Promise;