/** * Stage 4 — Compact (LLM call per chunk) * * Removes redundancy, repeated thinking, false starts from each chunk. * Preserves decisions, file paths, commands, errors, rationale. * * Budget: checks cap before each call, records after. * Throws BudgetExceeded if over cap (caller moves job to paused). */ import type { LLMProvider } from '../../contracts/index.js'; import { type Chunk } from './03-chunk.js'; import { BudgetTracker, BudgetExceeded } from '../../budget/tracker.js'; import type { DistillCostCapTracker } from '../cost-cap.js'; export { BudgetExceeded }; /** Which compaction posture to use. Mirrors ExtractProfile — and MUST be threaded * from the same source, or conversational transcripts get the coding-agent * compressor (the bug documented above). */ export type CompactProfile = 'engineering' | 'conversational'; export declare function compactSystemPrompt(profile: CompactProfile | null | undefined): string; export interface CompactResult { index: number; text: string; usageUsd: number; } /** * Compact a single chunk via LLM. * Returns the compressed text and actual USD cost. */ export declare function compactChunk(chk: Chunk, provider: LLMProvider, budget: BudgetTracker, capUsd: number, profile?: CompactProfile | null, /** Opt-in call-count/wall-clock cap (`config.distill.costCap`, see * `src/distill/cost-cap.ts`) — `undefined`/disabled is a pure no-op. * Checked BEFORE the LLM call; when it trips, this chunk is passed * through raw (same as the existing empty-text skip above) instead of * compacted, and no `provider.chat()` call is made. */ costCap?: DistillCostCapTracker): Promise; /** * Compact all chunks, up to `concurrency` at a time (FEAT-487). * * Stops dispatching further chunks at the first BudgetExceeded (or any other * error) — caller catches and pauses job. Results stay in chunk-index order * regardless of completion order. See src/distill/concurrency.ts for the * full invariant contract `mapWithConcurrency` provides. * * `concurrency` defaults to 1 — BYTE-IDENTICAL to the pre-FEAT-487 * sequential `for…of await` loop this replaces. Production callers resolve * the real value from config via `resolveChunkConcurrency` * (src/distill/pipeline.ts). * * `jobKey` (chunk-retry-amplification fix) opts into per-chunk * checkpointing: chunks whose result is already cached under `jobKey` (from * an earlier call — i.e. a worker-level job retry re-invoking compact() from * scratch) are skipped entirely rather than re-dispatched, and every fresh * success is cached before compact() returns. Omitted (the default), this * function is byte-identical to its pre-fix behavior — no cache lookup, no * cache write, every call re-dispatches every chunk. Production callers pass * `ctx.sourceHash` (see pipeline.ts) — stable across retries of the same job * because it's derived from transcript content, not a per-attempt id. */ export interface CompactOpts { profile?: CompactProfile | null; /** 'auto' (default): run for engineering, SKIP for conversational. * 'never': skip always. 'always': run always (pre-fix behavior). */ mode?: 'auto' | 'always' | 'never'; /** Opt-in call-count/wall-clock cap shared with extract() for this same * job — see `compactChunk`'s param doc and `src/distill/cost-cap.ts`. */ costCap?: DistillCostCapTracker; } /** * Should stage 4 actually call an LLM at all? * * Compaction shrinks a payload — a BANDWIDTH concern from when transcripts were * assumed to be shipped to a SaaS backend. Locally there is no payload, and stage * 3 has already capped chunks at MAX_CHUNK_CHARS (3200, ~800 tokens) against an * extraction num_ctx sized up to 16384. So compaction saves a few hundred tokens * out of a budget twenty times larger — while costing ONE LLM CALL PER CHUNK and, * on conversational content, DESTROYING FACTS (see COMPACT_SYSTEM_CONVERSATIONAL's * doc comment for the measured example: it deleted a named person, a relationship, * a place, a life event, a dated plan, and every speaker name). */ export declare function shouldCompact(opts?: CompactOpts): boolean; /** * #284 Bug 2 — the size half of the gate. shouldCompact() above is BLIND to * any individual chunk's size; it only rules out compaction categorically * (mode 'never', or mode 'auto' + conversational). Below this many * characters, an individual chunk is small enough that compaction cannot * meaningfully help it "fit" anything. * * Stage 3 already caps every chunk at DEFAULT_MAX_CHUNK_CHARS (3200 chars, * ~800 tokens — see 03-chunk.ts) against an extraction context sized up to * NUM_CTX_CAP (16384 tokens — see ollama.ts): a FULL-SIZED chunk was never * at real risk of not fitting in the first place, which is why compaction's * traditional "shrink it to fit" justification is already thin even for the * chunks that DO get compacted. This threshold picks out chunks well BELOW * even that already-modest packing target — a quarter of * DEFAULT_MAX_CHUNK_CHARS — so only chunks that are clearly, comfortably * small skip. Deliberate headroom: a chunk at or near the normal packing * target still compacts unchanged, per #284's explicit "keep it * conservative" instruction. A chunk this small has nothing meaningful left * to shrink, so running compaction on it only pays the over-compression / * fact-loss risk documented on COMPACT_SYSTEM_CONVERSATIONAL's doc comment * for a few hundred characters of savings. */ export declare const COMPACTION_FITS_THRESHOLD_CHARS: number; /** Does this chunk already comfortably fit — i.e. is it too small for * compaction to meaningfully help? See COMPACTION_FITS_THRESHOLD_CHARS. */ export declare function chunkAlreadyFits(text: string): boolean; /** Why a chunk's compaction was skipped — surfaced in pipeline.ts's * observability so a doctor/log reader can tell "compaction is off for this * profile" (mode-never / profile) apart from "this specific chunk didn't * need it" (size), rather than one undifferentiated boolean. */ export type CompactSkipReason = 'mode-never' | 'profile' | 'size'; export interface CompactionDecision { compact: boolean; skipReason?: CompactSkipReason; } /** * Per-chunk compaction decision — the size-aware sibling of shouldCompact(). * Precedence, highest first: 'never' skips unconditionally; 'always' compacts * unconditionally (an explicit operator override beats BOTH the profile gate * and the size heuristic below it); only under 'auto' does the profile gate * apply, and only then the size gate. Mirrored 1:1 by compact()'s own * per-chunk dispatch AND by pipeline.ts's stage-4 logging, so the log can * never drift from the decision the stage actually makes (same discipline as * shouldCompact()'s own doc comment in pipeline.ts). */ export declare function compactionDecisionFor(text: string, opts?: CompactOpts): CompactionDecision; export declare function compact(chunks: Chunk[], provider: LLMProvider, budget: BudgetTracker, capUsd: number, concurrency?: number, jobKey?: string, opts?: CompactOpts): Promise;