/** * 8-stage distillation pipeline. * * Stages: * 1. cleanup — deterministic * 2. normalize — deterministic * 3. chunk — deterministic * 4. compact — LLM call (budget-gated) * 5. extract — LLM call, JSON-mode (budget-gated, Zod-validated; parse * failures are transient and retried by the worker) * 6. reduce — deterministic * 7. memory-normalize — deterministic * 8. embed + index — provider call + DB writes */ import type { LLMProvider, EmbedProvider } from '../contracts/index.js'; import type { DB } from '../storage/db.js'; import { type Config } from '../config/config.js'; import type { DurabilityGateMode } from './durability-gate.js'; export { BudgetExceeded } from '../budget/tracker.js'; export interface PipelineContext { db: DB; llmCompaction: LLMProvider; llmExtraction: LLMProvider; /** Per-pass extraction provider override (FEAT-534). Optional in the type * — every real caller (distillHandler) always sets it via * ProviderSet.llm.extractionPassB, which `getProviders` itself always * populates (identity default), but hand-built test contexts (this file's * own test suite included) may omit it entirely. Consumers read it as * `ctx.llmExtractionPassB ?? ctx.llmExtraction` (runPipeline's stage 5b * below) — byte-identical to pre-FEAT-534 behavior whenever it's absent. * Only consulted when `extractionPasses: 2`; ignored entirely on * single-pass. */ llmExtractionPassB?: LLMProvider; /** LLM atom-triage provider override (FEAT-533). Optional in the type — * every real caller (distillHandler/distillEventsHandler) always sets it * via ProviderSet.llm.atomTriage, which `getProviders` itself always * populates (identity default to llmExtraction), but hand-built test * contexts may omit it entirely. Only consulted when * `config.distill.atomTriage.mode !== 'off'` — see * EmbedIndexContext.llmAtomTriage's doc comment. */ llmAtomTriage?: LLMProvider; /** Ingest gatekeeper provider (FEAT-548). Optional in the type — every * real caller (distillHandler) always sets it via ProviderSet.llm.gatekeeper, * which `getProviders` itself always populates (identity default to * llmExtraction), but hand-built test contexts may omit it entirely. Only * consulted when `config.distill.gatekeeper.mode !== 'off'` — see * runGatekeeperSweep's own no-op-without-provider convention. */ llmGatekeeper?: LLMProvider; embed: EmbedProvider; capUsd: number; sessionId: string | null; repo: string | null; project: string | null; branch: string | null; agent: string | null; sourceHash: string | null; /** See EmbedIndexContext.capturedAtMs — plumbed from the canonical envelope * via distillHandler. Also threaded one call deeper into extraction * (stage 5) as of the 2026-07-13 temporal-grounding fix — see * runPipeline's `sessionDateIso` local. A bare epoch has no timezone * information, so `sessionDateIso` prefers `capturedAtLocalDate` below * when present and only falls back to formatting THIS field (as a UTC * calendar date) for rows from before the 2026-07-13 timezone-offset fix * — see that field's own doc for why. */ capturedAtMs?: number | null; /** 2026-07-13 timezone-offset bugfix: the user's LOCAL calendar date at * capture (`YYYY-MM-DD`), plumbed from `transcripts.captured_at_local_date` * via distillHandler the same way capturedAtMs is above. Unlike * capturedAtMs (a bare epoch, timezone-agnostic by construction), this is * derived from the offset-bearing `captured_at` wire string's own * YYYY-MM-DD prefix at ingest time (src/server/routes/ingest.ts) — that * prefix IS the local wall-clock date the user actually captured at. * NULL for every row from before this fix and for legacy v0.0 ingests, * in which case `sessionDateIso` falls back to the old (UTC-derived-from- * epoch) behavior — see runPipeline's `sessionDateIso` local. */ capturedAtLocalDate?: string | null; /** FEAT-455 / ADR-012 decision 4, widened 2026-07-12, widened again * FEAT-458 S3 (ADR-013), FEAT-489 (Slice A), and FEAT-491: provenance tag * plumbed from the transcripts row via distillHandler — see * EmbedIndexContext.captureSource. */ captureSource?: 'auto_capture' | 'hook_close' | 'proxy' | 'audio_transcribe' | 'meeting_captions' | null; /** FEAT-489/FEAT-491 (ADR-017 decision 6): plumbed from * `transcripts.consent_disclosed` via distillHandler, the same "flows * straight through" pattern `captureSource` above already uses — * stamped onto every atom this transcript produces * (EmbedIndexContext.consentDisclosed). `null`/omitted for every envelope * whose source transcript has no `consent_disclosed` value. */ consentDisclosed?: boolean | null; /** FEAT-479 (D3 §3.2) AC-3: per-transcript extraction profile override, * plumbed from the transcripts row via distillHandler the same way * captureSource is above — NULL for every envelope that omits the * optional `extract_profile` field. `runPipeline` resolves the final * per-run profile as `extractProfile ?? config.distill.extractProfile ?? * 'engineering'` (AC-2/AC-3 precedence) before calling stage 5. */ extractProfile?: 'engineering' | 'conversational' | null; /** FEAT-441 durability gate mode — undefined behaves as 'off' (AC-2). */ durabilityGateMode?: DurabilityGateMode; /** Full runtime config, threaded through to EmbedIndexContext.redactConfig * so a gated atom's logged text is redacted with the real running config * (AC-7), not the fallback default. Optional — falls back to * defaultConfig() at the embed-index stage when omitted. */ config?: Config; } export interface PipelineResult { memoriesCreated: number; memoriesDeduped: number; atomsExtracted: number; chunksProcessed: number; /** IDs of every memory this run touched (both newly-created and deduped-existing), in stage-8 order. */ memoryIds: string[]; /** FEAT-441 AC-4: count of candidate atoms the durability gate rejected * this run (0 when the gate is 'off' or nothing matched). */ gatedOutCount: number; } /** * Run the full 8-stage distillation pipeline on a raw transcript. * Returns count of new memories created. * * Throws BudgetExceeded if the daily cap is hit during stages 4 or 5. * The caller (distill handler) catches this and moves the job to paused. */ export declare function runPipeline(transcript: string, ctx: PipelineContext): Promise;