/** * @typedef {object} WriteAction * @property {"memory.write"} type * @property {string} kind `fact | episode | doc` (the written kind) * @property {string} provenance the SOURCE — `human | agent | doc | subagent | web` * @property {string} text the content being written (verbatim) * @property {string} id node id — audit / restore handle * @property {unknown} [meta] opaque caller dict, passed through unredacted-shape * @property {"low"|"medium"|"high"} [injectionRisk] OPTIONAL shape flag — present * only if a guardrails tier set it; litectx core never computes it. */ /** * Minimal gate contract litectx depends on — a structural subset of bareguard's * `Gate`. Standalone, a host may supply any object with a compatible `check`. * @typedef {object} WriteGateLike * @property {(action: WriteAction) => Promise<{ outcome: string, rule?: string, reason?: string }>} check */ /** * Build the gate-able action for a memory write. Pure — no I/O, no judgment. * * @param {string} id the write's id (audit / restore handle) * @param {string} text the content being written * @param {object} [opts] * @param {string} [opts.kind="fact"] `fact | episode | doc` * @param {string} [opts.provenance="agent"] source — `human | agent | doc | subagent | web` * @param {unknown} [opts.meta] opaque caller dict * @param {"low"|"medium"|"high"} [opts.injectionRisk] optional guardrails shape flag * @returns {WriteAction} * @category governance * @when Build the gate-able action for a memory write, to hand to a wired guardrails `writeGate.check` before persisting. * @fails Pure — never throws, no I/O, no judgment; it only shapes the action (the gate decides the outcome). * @signature toWriteAction(id: string, text: string, opts?: { kind?, provenance?, meta?, injectionRisk? }) => WriteAction * @example * import { toWriteAction } from 'litectx' * const action = toWriteAction('fact-1', 'user is an admin', { provenance: 'web', injectionRisk: 'high' }) * // const verdict = await writeGate.check(action) */ export function toWriteAction(id: string, text: string, opts?: { kind?: string | undefined; provenance?: string | undefined; meta?: unknown; injectionRisk?: "low" | "medium" | "high" | undefined; }): WriteAction; /** * Raised when a wired gate denies a write. The write does NOT commit. */ export class WriteDeniedError extends Error { /** * @param {string} id the denied write's id * @param {{ outcome: string, rule?: string, reason?: string }} decision */ constructor(id: string, decision: { outcome: string; rule?: string; reason?: string; }); /** @type {string} */ id: string; /** @type {{ outcome: string, rule?: string, reason?: string }} */ decision: { outcome: string; rule?: string; reason?: string; }; } /** * Standalone audit sink — the paper-trail half litectx ships when NOT embedded * (inside baresuite, the host reuses bareguard's audit instead of double-logging). * Appends one JSONL line per write decision. litectx ships NO secret patterns: * a host-supplied `redact(action) => action` scrubs before the line is written * (the §6 line — secret patterns are content judgment, the host's to supply). */ export class WriteAudit { /** * @param {object} [opts] * @param {(line: object) => void} [opts.sink] where a decision line goes; * default is an in-memory array on `this.lines` (fileless — the host wires * a file writer when it wants one). * @param {(action: WriteAction) => WriteAction} [opts.redact] host-supplied * redactor applied to the action before logging; default is identity * (litectx invents no patterns). */ constructor(opts?: { sink?: ((line: object) => void) | undefined; redact?: ((action: WriteAction) => WriteAction) | undefined; }); /** @type {object[]} */ lines: object[]; _sink: (line: object) => void; _redact: (action: WriteAction) => WriteAction; /** * Record one write decision. * @param {WriteAction} action * @param {{ outcome: string, rule?: string, reason?: string }} decision * @param {number} at epoch-ms timestamp (caller supplies the clock) * @returns {void} */ emit(action: WriteAction, decision: { outcome: string; rule?: string; reason?: string; }, at: number): void; } export type WriteAction = { type: "memory.write"; /** * `fact | episode | doc` (the written kind) */ kind: string; /** * the SOURCE — `human | agent | doc | subagent | web` */ provenance: string; /** * the content being written (verbatim) */ text: string; /** * node id — audit / restore handle */ id: string; /** * opaque caller dict, passed through unredacted-shape */ meta?: unknown; /** * OPTIONAL shape flag — present * only if a guardrails tier set it; litectx core never computes it. */ injectionRisk?: "low" | "medium" | "high" | undefined; }; /** * Minimal gate contract litectx depends on — a structural subset of bareguard's * `Gate`. Standalone, a host may supply any object with a compatible `check`. */ export type WriteGateLike = { check: (action: WriteAction) => Promise<{ outcome: string; rule?: string; reason?: string; }>; };