/** * FEAT-548: ingest gatekeeper — an optional, chunk-level pre-extraction * substantiveness gate. Inserted right after stage 3 (chunk), BEFORE stage 4 * (compact) and stage 5 (extract) ever run — the opposite chokepoint from * `atom-triage.ts` (which filters ATOMS a metered call has already * produced). A `skip` verdict here means the metered extraction (and * compaction) call for that chunk is never made at all. * * Batching: a fixed CHUNK-COUNT batch (`distill.gatekeeper.batchSize`) over * the whole job's chunk list for the WHOLE JOB, never one call per chunk — * same call-count discipline `atom-triage.ts` documents, applied one stage * earlier (chunks are already discrete units here, so this batches purely to * cap total call count on a session with many chunks, not to work around any * loss of identity the way atom-triage's post-`reduce()` batching does). * * Fail-open, mirroring `atom-triage.ts`/`contradiction-judge.ts`'s posture * exactly: a parse failure or a thrown/rejected `chat()` call returns an * EMPTY verdict map rather than throwing — the caller (runPipeline) treats * every chunk with no verdict entry as an implicit 'keep' (ADR-015: a broken * gatekeeper provider must never silently drop a chunk; it must fail loud in * the log and fail open in behavior). */ import type { LLMProvider } from '../contracts/index.js'; import { RunCapTracker } from '../util/run-cap-tracker.js'; /** * Stable marker embedded in the system prompt, mirroring `ATOM_TRIAGE_MARKER` * (atom-triage.ts) — lets test mock providers route a batched gatekeeper * call to its own deterministic branch BEFORE the `opts.json === true` * extraction-call check would otherwise misclassify it (extraction, atom * triage, and the gatekeeper all ask for `json: true`). */ export declare const GATEKEEPER_MARKER = "astramem-gatekeeper-v1"; export declare const GATEKEEPER_VERDICT_LABELS: readonly ["keep", "skip"]; export type GatekeeperVerdictLabel = (typeof GATEKEEPER_VERDICT_LABELS)[number]; export interface GatekeeperVerdict { /** The position (in the ORIGINAL, whole-job chunk list) this verdict * applies to — echoed back by the model from the numbered prompt so the * caller can map a possibly-reordered/partial response back to the right * chunk. */ index: number; verdict: GatekeeperVerdictLabel; confidence: number; } export interface GateableChunk { /** Position in the whole-job chunk list — echoed back in the verdict so * the caller can reassemble a Map keyed by this same index. */ index: number; text: string; } /** * Classify one batch of chunks in a single LLM call. Never throws — a parse * failure or a thrown/rejected `chat()` call is logged and resolves to an * EMPTY Map (fail-open: the caller treats every chunk in this batch as an * implicit 'keep'). */ export declare function gatekeeperBatch(llm: LLMProvider, chunks: GateableChunk[], opts?: { log?: (message: string) => void; }): Promise>; export interface GatekeeperCaps { /** Cap on gatekeeper LLM calls (batches, not chunks) for a SINGLE * runPipeline call. Mirrors `AtomTriageCaps.maxPerRun`. */ maxPerRun: number; /** Wall-clock cap (ms) on the entire gatekeeper sweep within a single * runPipeline call. Mirrors `AtomTriageCaps.maxWallClockMs` — bounds * worst-case duration against a slow/rate-limited provider, which * maxPerRun alone cannot (FEAT-482 lesson). */ maxWallClockMs: number; } /** * Per-job call-count + wall-clock tracker for the gatekeeper batch sweep. * Mirrors `AtomTriageRunTracker` (src/distill/atom-triage.ts) exactly — see * that class's doc comment for the full rationale, restated here for the * chunk-level gate. * * `shouldStop()` is checked BEFORE each batch's LLM call; once it trips, * every remaining un-gated chunk in this job falls through un-gated — the * caller's "no verdict entry means keep" fail-open convention means this is * a graceful degrade to "extract unfiltered", never a drop-by-timeout * (FEAT-482 lesson, restated for this cap). */ export declare class GatekeeperRunTracker extends RunCapTracker { constructor(caps: GatekeeperCaps, log?: (message: string) => void); } export interface GatekeeperSweepResult { /** Verdict for every chunk the sweep actually classified (successfully * parsed responses only) — a chunk with no entry is implicitly 'keep'. */ verdicts: Map; /** Count of chunks ATTEMPTED (sent in a batch), regardless of whether that * batch's response was parseable — mirrors atom-triage's * atomsTriagedCount: a provider that fails open on every call still * consumed real job volume, and the rate denominator must reflect that * (canary-must-measure-rate-not-existence). */ chunksEvaluated: number; } /** * Run the full batched gatekeeper sweep over one job's chunk list. Returns * immediately with an empty result (no LLM call) when `chunks` is empty or * no provider is supplied — same no-op convention `atom-triage.ts`'s caller * loop (08-embed-index.ts) uses for `llmAtomTriage`. */ export declare function runGatekeeperSweep(chunks: GateableChunk[], llm: LLMProvider | undefined, opts: { batchSize: number; caps: GatekeeperCaps; log?: (message: string) => void; }): Promise;