@sema-agent/core
Version:
Stateless, task-oriented AI agent core
972 lines • 69.2 kB
TypeScript
import { type AgentMessage, type CompactionSettings, type Session, type SessionRowExpectation, type ThinkingLevel } from "../internal/harness.js";
import { type Context, type Message, type Model, type Tool } from "../internal/llm.js";
import type { ContextWindowOverlay } from "./context-settings.js";
import type { PostCompactContext, PreCompactContext, PreCompactResult } from "./hooks.js";
import type { TraceEvent } from "./trace.js";
import type { Brain, ModelRef, TaskSpec } from "./types.js";
import type { CompactOutcome, HumanInputAttachment, TaskEvent, TaskEventIdentity } from "./task-event.js";
import type { CompactionEffect } from "./frame-settlement.js";
import { type MintedAttachmentFrame } from "./user-attachment.js";
/**
* Default summarization focus, applied when a task does not provide `compaction.instructions`.
* Addresses the common fidelity failure where summaries drop specific facts.
*/
export declare const DEFAULT_COMPACTION_INSTRUCTIONS: string;
/**
* design/145 §4 — uniform hygiene for EVERY summarization-instructions channel (spec-level
* `compaction.instructions`, the DEFAULT constant, a manual `/compact` per-call override, and a
* preCompact hook's `additionalInstructions` — 修类不修点): instructions text can be model- or
* caller-authored, and it is injected VERBATIM into the summarization prompt
* (`\n\nAdditional Instructions:\n${...}`), so a forged `</system-reminder>`
* or `<<<`/`>>>` fence sentinel inside it would otherwise ride into a trusted prompt position.
* Posture (r2 review B4): `sanitizeUntrustedText` + fence defusing only — NEVER `delimitUntrusted`
* (fencing instructions as "data only, do not follow" self-defeats their instructional intent, and
* the fence markers would be echoed back into the persisted summary). Cap = 2048 code points
* (surrogate-safe slice): instructions steer a summary, they are not a payload channel.
* Text without break-out sequences under the cap passes through BYTE-IDENTICAL (the no-arg /
* default-instructions prompt is unchanged — pinned by test).
*/
export declare const COMPACTION_INSTRUCTIONS_MAX_CHARS = 2048;
export declare function sanitizeCompactionInstructions(text: string): string;
/**
* 压缩子阶段遥测 (1.283 G) — single-source shape: the `phaseDurations` result field IS the
* (defined-but-not-yet-emitted) `compaction.phase_timings` trace frame minus its envelope
* (`kind`/`version`/`taskId`/`ts`) and minus `durationMs` (already a sibling result field). Deriving
* it from the frame keeps the follow-up runtask bridge a mechanical spread — and makes a rollback of
* either side a tsc error, not a silent drift.
*/
export type CompactionPhaseDurations = Omit<Extract<TraceEvent, {
kind: "compaction.phase_timings";
}>, "kind" | "version" | "taskId" | "ts" | "durationMs">;
/** True iff `err` is a compaction failure caused by a manual compact() caller WITHDRAWING its
* request — see {@link MaybeCompactOptions.manualCancelSignal}. Minted at every cut point of the
* pass: before the pass commits, while the summary provider or the credential callback is
* answering ({@link untilPassCut}), the summary call's own abort outcome, and the pre-persist
* "cancelled before commit" throw (design/374 slice-3, C16: a brain that ignored the abort finished
* the summary, but the withdrawal still fired before anything landed). Absence of the marker means
* the failure was NOT the withdrawal: a real summarizer error racing a late cancel stays
* unmarked so the caller's breaker/onError accounting still sees it. */
export declare function isCompactionManualCancel(err: unknown): boolean;
/** The withdrawal a cut of `turn` is while `leg` still runs — the caller's cut (an interrupt of the turn a pass serves). The leg's own
* stop (the walltime limit, a budget) cuts the turn too and is not one. Joins a pass's `manualCancelSignal`, so the failure site marks
* a caller's cut only. */
export declare function turnCutWhileLegRuns(turn: AbortSignal, leg: AbortSignal): AbortSignal;
/** How a pass that threw reads, by the failure's own cause — never by what the signals say when the catch runs: a withdrawal the
* failure site marked is the caller's cut (`cancelled`); the pass's own abort outcome with no such mark is the leg's stop
* (`task_ending`); anything else is a real failure (`failed`, the whole account) — a store's error after a stop included. The one
* reading of every lane's catch. */
export declare function compactionCutOf(err: unknown): "cancelled" | "task_ending" | "failed";
/** The ONE reason a failed compaction pass reports, read by every lane that accounts for one (the turn boundary, the end-of-task pass,
* the prompt-too-long recovery) for its trace frame and its `compaction_outcome{failed}` event: the thrown value's words through the
* same-principal tier, THEN bounded — minted before the cut, so the cut can leave no part of a value. */
export declare function compactionFailureReason(err: unknown): string;
/** The machine causes a failed pass's `compaction_outcome{failed}` frame names (its `cause`), as a value. Root-exported.
* `leaf_moved`: another writer moved the session's leaf while the pass ran, so the session's own leaf compare-and-set refused
* the summary record and nothing landed — the pass may be asked for again, over the branch as it is now. `holder.superseded`: the
* session refused the summary record for the grant the writer acts under (another leg holds the run, or the run or the verb's grant
* has ended) and nothing landed — do not ask again under that grant. A failure with no word here carries no `cause` (its `reason`
* is the whole account).
*
* @contract compaction.failure_cause.capability — this set is the one fact a host reads to learn whether the engine it loaded
* names a failure cause: `COMPACTION_FAILURE_CAUSES.includes(word)`, read from the loaded package the way its version is
* read from `CONTRACT_KIT_ENGINE_VERSION`; there is no second version word, and a word the set lacks is never on a frame. */
export declare const COMPACTION_FAILURE_CAUSES: readonly ["leaf_moved", "holder.superseded"];
/** A word of {@link COMPACTION_FAILURE_CAUSES}. */
export type CompactionFailureCause = (typeof COMPACTION_FAILURE_CAUSES)[number];
/** The ONE reading of a failed pass's machine cause, beside its {@link compactionFailureReason} and read at the same sites: the session's
* leaf compare-and-set refusing a write of the pass is `leaf_moved`; the session's holder fence refusing it is `holder.superseded`;
* anything else names no cause. */
export declare function compactionFailureCause(err: unknown): CompactionFailureCause | undefined;
/**
* SEMA-ONLY (trim-seam defense, no CC analogue) — stale-anchor freshness margin: a usage anchor is
* trusted only while the anchored estimate stays within `structural × margin + overhead` of the
* current message list. 2 covers the worst measured structural under-count (charsPerToken=3 vs a
* measured ~2.18 true ratio ⇒ ≤ ~1.4x) plus reasoning-block accounting slop, while still catching
* the live deception band (anchored estimates 2.9x–18x over the real context — docs/TOKEN-ESTIMATE-
* CALIBRATION-LIVE-2026-07-09.md §3/§4).
*/
export declare const STALE_ANCHOR_STRUCTURAL_MARGIN = 2;
export declare function sanitizeCompactionSettings(settings: CompactionSettings, contextWindow: number | undefined): CompactionSettings;
/**
* design/169-A — the CC-form fork context: the main lane's latest REAL brain request (same
* `systemPrompt` bytes, same message-list bytes, same tools). The summary request built from it is
* that request plus ONE appended user-role instruction message, so the provider's prompt cache
* re-serves the already-paid main prefix — the economics that make "summarize on the main model"
* cheap. Structural twin of the engine-side `CompactionForkContext` (engine/compaction/compaction.ts
* — the engine layer cannot import core, so the shape is declared on both faces and must stay
* assignable; `compact()`'s parameter type is the checked seam).
*/
export interface CompactionForkContext {
systemPrompt?: string;
/** The physical cache-span block layout of the main request, when it had one (Context.systemBlocks)
* — the fork must reproduce it or a multi-block pack's cache boundaries are silently lost. */
systemBlocks?: Array<{
text: string;
cacheControlBoundary: boolean;
}>;
messages: Message[];
tools?: Tool[];
/** The model id that served the recorded request — part of the provider cache key; the fork
* eligibility gate refuses a snapshot from a different model (see the engine-side twin's doc). */
modelId?: string;
/** The recorded request's locale of the engine's own texts (`Context.promptLocale`): a request built from the record
* (the fork, the advisor) carries it on. */
promptLocale?: Context["promptLocale"];
/** The recorded request's own controls as sent — thinking tier, try-undeclared posture, output cap: the reuse form sends
* them unchanged (the engine-side twin's doc says why). */
request?: {
reasoning?: ThinkingLevel;
tryUndeclared?: boolean;
maxTokens?: number;
promptCacheKey?: string;
};
}
/**
* What a compaction pass reports at the moment it commits ({@link MaybeCompactOptions.onStarted}) — and the
* whole content of its `compaction_started` wire frame. `pass` is minted HERE, by the one function every lane
* runs, so no lane can number its own passes; the frame that ends the pass carries the same value.
*/
export interface CompactionPassStart {
readonly pass: string;
/** The trigger this pass runs under — the value it hands the `preCompact` hook. */
readonly trigger: "auto" | "manual" | "forced";
/** Engine clock, epoch ms, at the commit. */
readonly startedAt: number;
}
/** What a landed compaction asks its restore for ({@link MaybeCompactOptions.workingFileAttachments}). */
export interface CompactionRestoreAsk {
/** The files to re-read, most recent first — already without the ones the context the summary leaves still shows. */
readonly candidates: readonly string[];
readonly maxFiles: number;
/** What one file may deliver, in its own text (before the Read's line numbers — the unit `maxCharsPerFile` counts),
* and all of them together, in the file contents the frames send (line numbers included — the unit of the
* window's share); an image costs the engine estimate's fixed weight × `charsPerToken` in both. */
readonly perFileChars: number;
readonly totalChars: number;
readonly charsPerToken: number;
}
/** Which of the three autocompact-window fields the `??` precedence selected (see `resolveTriggerWindow`). */
export type TriggerWindowField = "autoCompactTokens" | "contextTokens" | "contextWindow";
/**
* A pass's report that its window is unusable: the size it DID manage to estimate, the field that decided
* the window, and that field's unusable value when it carried one.
*/
export interface UnevaluableWindow {
estTokens: number;
/** WHICH of the three window fields the `??` precedence actually selected. A defined-but-unusable
* higher-precedence field (`autoCompactTokens: 0`) shadows a healthy lower one, so "no window is
* declared" is the wrong story on that path — the caller reports THIS field, not all three. */
windowField: TriggerWindowField;
/** The unusable value that field carried, when it carried one at all (absent = nothing declared). */
windowValue?: number;
modelId?: string;
}
/**
* How full the context is, and against what threshold. One pure measurement with three readers: the
* auto-compaction trigger, the `contextUsage` every `maybeCompact` return carries (the `context_usage`
* frame is built from it), and the per-turn read room (`runner/context-room.ts`). Same messages, model,
* overlay, settings and overhead ⇒ the same record. It never reads `settings.enabled`: a leg with
* compaction switched off still has a compaction point to measure against.
*/
export interface ContextMeasure {
/** `shouldCompact`'s first argument: usage-anchored when the anchor is fresh, structural + overhead otherwise. */
readonly tokens: number;
/** The AUTOCOMPACT window (may be the unknown sentinel: non-finite or ≤ 0). */
readonly window: number;
readonly windowKnown: boolean;
readonly windowField: TriggerWindowField;
/** The settings repaired for this window (`sanitizeCompactionSettings`). */
readonly settings: CompactionSettings;
/** The structural coefficient every quantity here lives in. */
readonly charsPerToken: number;
/** Present exactly when the window is known: `usedTokens > compactAtTokens` is the trigger predicate. */
readonly contextUsage?: {
usedTokens: number;
windowTokens: number;
compactAtTokens: number;
};
}
export declare function measureContext(input: {
readonly messages: AgentMessage[];
readonly model: Model;
readonly contextOverlay?: ContextWindowOverlay;
readonly settings: CompactionSettings;
readonly overheadTokens?: number;
}): ContextMeasure;
/** The unevaluable-window record of a measurement whose window is unknown (`undefined` when it is known). */
export declare function unevaluableWindowOf(measure: ContextMeasure, model: Model): UnevaluableWindow | undefined;
export interface MaybeCompactOptions {
/** Disclosure channel for CONTAINED host-callback failures in this scope (the RB-463
* isolation primitive's bounded first-failure per site). Absent ⇒ failures are counted only. */
onNotifyError?: (failure: import("./safe-notify.js").SafeNotifyFailure) => void;
/** The session to compact. Typed on the INTERFACE face (typecheck 车 2026-07-24): every member this
* module touches lives on `Session` (the optional `getPromptEpoch` degrades honestly via `?.()`). */
session: Session;
/** The leg's admission seat (`session.append`): a row this pass adds to the transcript passes it first. Absent ⇒ as made. */
admitRows?: import("../internal/harness.js").RowAdmission;
/** This run's prompt identity inputs (and the pinned center layer's provenance): the compaction restates the
* session's epoch when the identity is unchanged and advances it otherwise — the same identity the prepare-time
* record names, so a compaction never flaps the record. Absent ⇒ no restatement (a host compacting outside a run). */
promptIdentity?: {
readonly layers: import("../prompt-assembly/epoch.js").PromptIdentityLayers;
readonly sourceRevision?: string;
};
/** A compaction-boundary CENTER LAYER CHANGE, pre-assembled by the caller (prepare's seam): an adopted candidate,
* or an emptied center layer after an explicit revocation. The epoch advances to the identity after the change in
* the SAME CAS append as the new baseline, and `apply()` (the live prefix / fingerprint swap) runs ONLY after that
* append succeeded — a failed or blocked compaction swaps nothing. */
centerAdoption?: {
/** The run's identity inputs after the change (`layers.center` absent ⇒ the center layer is emptied). */
promptIdentity: {
readonly layers: import("../prompt-assembly/epoch.js").PromptIdentityLayers;
readonly sourceRevision?: string;
};
/** the post-swap prompt overhead in the trigger estimate's coordinate: used for THIS
* compaction's post-measurement so PTL recovery/anti-thrash see the swapped prefix size. */
overheadTokensAfter: number;
apply: (committedIdentity: string) => void;
};
/** Model whose context window bounds the budget (the model that runs the next turn). */
model: Model;
/** The deployment's LIVE per-model window overlay for THIS pass ({@link RunnerDeps.contextSettings}
* keyed by the leg's catalog word). Read by the caller at the moment of the pass, never snapshotted:
* every window number below — the trigger, the sanitize clamp, the disclosed `contextUsage` — is
* measured through it, so an operator who lowers a model's window reaches the legs already running.
* Absent ⇒ the model card's own declarations, unchanged. */
contextOverlay?: ContextWindowOverlay;
/**
* design/169-A — accessor for the main lane's request the summary reuses (see {@link CompactionForkContext}): the
* latest request a run recorded, or — on the turn-start lane — the request it is about to send. Consulted once per
* pass, after every gate that can end the pass; it may answer asynchronously. `undefined` (or an accessor answering
* `undefined`: no request yet, a lossy one, or a library caller driving maybeCompact directly) keeps the independent
* request form. A reuse-form prompt-too-long drops the oldest message groups and retries in the reuse form inside
* `compact()` (the reference's shape); an empty or unenveloped reuse-form answer falls back ONCE to the independent form.
*/
forkContext?: () => CompactionForkContext | undefined | Promise<CompactionForkContext | undefined>;
brain: Brain;
getApiKeyAndHeaders?: TaskSpec["getApiKeyAndHeaders"];
thinking?: ThinkingLevel;
settings?: CompactionSettings;
/** Appended to the summarization prompt to steer what must be preserved. Sanitized inside
* maybeCompact via {@link sanitizeCompactionInstructions} (design/145 §4 — every channel,
* including the preCompact hook's additions, gets the same treatment). */
customInstructions?: string;
/**
* The user's / project's own INSTRUCTION-FILE content (the CLAUDE.md family this
* deployment loads into an ordinary session), for the INDEPENDENT summary form.
*
* Why it needs a seat at all: the summarization prompt carries CC's closing invitation to follow
* "additional summarization instructions provided in the included context", and the independent
* form swaps in its own system prompt — so the loader-mounted files never reached that
* summarizer and the invitation named nothing this form delivers. Supplying this makes it true:
* the content is rendered as the request's first section, ahead of the conversation, with
* instruction standing for the summarization task only. The FORK form carries it too — in the ONE
* message that form appends, so the cached main prefix is untouched: it preserves whatever system
* prompt the main request had, and nothing establishes that this prompt contains the files, so a
* conforming fork response would otherwise succeed having never seen them. Budget for the seat's
* bytes on BOTH forms.
*
* TRUST TIER — host supply, same as `systemPrompt`: this string is handed to the model as
* instructions. Never fill it from tool output, model text, or any other in-band channel. It is
* NOT sanitized or length-capped here (unlike `customInstructions`, which is per-pass steering
* text of unknown provenance) precisely because it is the deployment's own prompt material.
*
* ABSENT (default) ⇒ every summarization request byte is what it was before this option existed.
* That invariant is over the summarization REQUEST; the one deliberate effect outside it — the
* new `<instruction-files>` authority envelope being defused on the fenced/shelled lanes for every
* deployment — is stated at `CompactionPreparation.contextInstructionFiles`.
*/
contextInstructionFiles?: string;
signal?: AbortSignal;
/**
* The pass's WITHDRAWAL signal — the run's caller-stop gate (the stop verbs and the task's own
* `signal`), composed with the cancel signals of the manual callers THIS pass claimed (a SUBSET
* of `signal`, which may additionally carry the leg's own abort).
* Used only for failure ATTRIBUTION: when the summary call's failure is its abort outcome and
* this signal has fired, the thrown error is stamped so {@link isCompactionManualCancel} holds —
* the caller then treats the pass as withdrawn ("mooted") instead of summarizer evidence. It
* never aborts anything by itself (the same signals already ride inside `signal`).
*/
manualCancelSignal?: AbortSignal;
/**
* Anti-thrash re-trigger floor (design/64 §25.2): ALSO require the estimated context to exceed this
* many tokens before compacting. The within-task caller sets it after each compaction (post-compaction
* size × regrowth factor) so a compaction that could not free much headroom — small window, chunky
* turns, large summary — does not re-fire a real summary LLM call at every subsequent boundary
* (LONGRUN-1b measured 26/39 boundaries firing, +135% wall, without it). 0/undefined = no floor.
*/
minTokens?: number;
/**
* Force a compaction even when context is UNDER the auto-threshold (and ignore `minTokens`) — the
* manual `/compact` path (TaskStream.compact, shell K-1c). It bypasses ONLY the threshold gate, not the
* structural floor: if there is no valid cut point (too little history to summarize) the call still
* returns `{compacted:false}` quietly. Default `false` = the auto-threshold policy.
*/
force?: boolean;
/**
* design/123 D4 (r5 final discriminant) — trim→compaction pressure propagation: force a compaction
* whenever the NATURAL gate would not fire, i.e. `est < max(threshold, minTokens-floor)`. Trim
* dropping real messages is external hard evidence that compaction is overdue, and it must override
* BOTH gates that can wrongly hold it back:
* - est < threshold — the deceived-estimate regime (request-only trim deflates the next usage
* anchor while the session stays full-size; the 16k-live sawtooth), and
* - threshold ≤ est < floor — the floor-blocked regime (post-compaction §25.2 floor above an
* HONEST over-threshold estimate; 5th live run: 27 boundaries trim-dropping while the floor
* held compaction off and input climbed 16k→27.4k).
* `est ≥ max(threshold, floor)` fires naturally anyway (no force needed/applied — the result then
* reports `naturalTrigger: true`). Spiral protection lives in the CALLER via
* {@link nextTrimForceBackoff}: any landed compaction that still posts over the threshold
* (incompressible transcript, the LONGRUN-1b shape) disables further trim-forces until a landed
* compaction posts under the threshold again. Like `force`, this never bypasses the structural
* floor (no valid cut point ⇒ `{compacted:false}`).
*/
forceUnderThreshold?: boolean;
/**
* The post-compaction restore: after a summary lands, re-read up to `maxFiles`
* of the files the conversation had read (or, without read tracking, modified) that the context the summary leaves
* no longer shows, and append each as an attachment frame — its own engine-note message after the compaction entry
* (summary, kept tail, restored files: the reference's post-compact order, cli296.js:249517 `hookResults`). Why: without it the model spends ≈2.3 extra read round-trips per
* compaction re-fetching what it just lost (LONGRUN-2). Bounded: a per-file cap in the file's own characters and a
* total, in the file contents the frames send (line numbers included), that self-scales to ~15% of the window; a
* file over either is a frame that names it and says it is too large, never part of its contents. The post measurement counts the frames, so the anti-thrash floor stays truthful. Absent ⇒ no restore.
* Pick order: most recently READ first (`recentlyReadFiles`), else most recently modified
* (`CompactionDetails.modifiedFilesByRecency`), else the alphabetical `modifiedFiles` of older details.
*/
workingFileAttachments?: {
/** Re-read the candidates and mint their frames — the engine's one read of a file for the model, into the context the
* summary leaves (each frame carries the file's contents; a request sends them by its own copy projection). Side-effect free: each frame
* carries the commit its delivery owes, run here once the frame is in the transcript. Wired by the Runner. */
restore: (ask: CompactionRestoreAsk) => Promise<ReadonlyArray<MintedAttachmentFrame>>;
/** A fault that kept a file from the transcript — the restore threw (every candidate it was asked for), or the transcript
* would not take a minted frame (not delivered, nothing committed) — with its cause: the operator's line. What became of
* each frame, refusals included, is its record on `restoreOutcomes`; this hears only the faults. */
undelivered?: (label: string, reason: string) => void;
/** Max files to restore. Default 3. */
maxFiles?: number;
/** Per-file cap in the file's own characters (≈4 chars/token). Default 16_000 (~4k tokens). */
maxCharsPerFile?: number;
/**
* CC-parity selection set: the files the task has READ, most recent
* first (the reference's post-compact restore sorts readFileState by timestamp — the read set is wider
* than the modified set and includes reference files the model consulted). When provided and
* non-empty it WINS over `modifiedFilesByRecency`; absent/empty falls back to the modified set
* (the pre-2026-07-03 behavior, and the right answer for callers with no read tracking).
*/
recentlyReadFiles?: () => string[];
/**
* RB-197② (CC `compact.ts:1415-1463` `shouldExcludeFromPostCompactRestore` parity): paths a caller
* re-states through a DIFFERENT channel (e.g. its own instruction files) are excluded from the
* `recentlyReadFiles`/fallback candidate set — re-attaching them here would duplicate content that
* channel carries — and their read-state entries survive the clear. A plain path list, not a
* hardcoded filename. The Runner itself supplies none: its `restore` leaves its instruction-file lane's
* members out of whatever candidates it is handed (their entries retire with the summary and the lane
* states them again), so this seat is a library caller's.
*/
instructionSourcePaths?: ReadonlyArray<string>;
/**
* `recentlyReadFiles` yields CANONICAL keys (resolveKey
* output — the same coordinate `ReadFileState` is keyed in, and the one an attachment frame's recorded
* read carries), but `instructionSourcePaths` and the kept tail's Read tool calls (raw model-typed
* arguments) do NOT share that coordinate — a model-typed relative path and its
* canonical form are different strings, so a plain `Set.has()` between them silently never matches
* even for the identical file. Optional callback (present exactly when there's a containment root
* to resolve against — i.e. hands enabled) that canonicalizes a raw path the SAME way the hands
* toolkit does, so `maybeCompact` can normalize every side of the comparison before excluding.
* Best-effort: a throw/rejection is caught by the caller and treated as "leave this one path
* unnormalized" (a safe direction — worst case is a missed exclusion, not a wrong one). Absent
* when there is no containment root (hands disabled) — in that regime every path source is
* ALREADY in the same raw coordinate, so the fallback exact-string comparison is already correct.
*/
normalizePath?: (raw: string) => Promise<string>;
};
/**
* Fires ONCE per LANDED compaction (after `appendCompaction` committed the new baseline and the restore frames were
* appended), immediately followed by the delivered restore frames' commits — the read state's clear and the restore's
* entries land together, after the baseline; a compaction that does not land touches neither. The Runner wires this to
* the read-state invalidation (`applyContextShrinkToReadFileState`): the summary just replaced the history the
* read-dedup stubs point at. Kept OUTSIDE `workingFileAttachments` deliberately — the invalidation is owed even when
* the restore is off. Best-effort: a throwing callback must not fail the landed compaction.
*
* `preserveReadState`: canonical-coordinate paths whose entry survives the clear — every file the context the summary
* leaves still shows (a kept-tail read or attachment frame) or that a declared instruction source re-seeds; the restore
* deduplicates against the same set.
*/
onApplied?: (preserveReadState: ReadonlyArray<string>) => void;
/**
* #1036 — THE COMMIT-BEAT EFFECT SINK: what this compaction did to the transcript, reported
* once, right after the `appendCompaction` that landed it. `summarizedEntryIds` = every entry id
* that fell into the summarized prefix (before `firstKeptEntryId`); `"unknown"` when the kept-tail
* anchor is not in the branch — the conservative reading (the same one
* {@link countSummarizedCrossSessionMessages} already takes) is that the whole branch went into
* the summary.
*
* Why HERE and not at the three call sites: a fact about a landed compaction has exactly one
* birthplace, and an effect minted per lane would be a rule written three times (and, the two
* times someone forgets, a lane where a summarized frame is silently never repaired). The three
* lanes reach it through the options assembly they already share.
*
* Observe-only, like every post-landing side effect here: a throwing sink must not fail a landed
* compaction.
*/
onCommitted?: (effect: CompactionEffect) => void;
/**
* env-tail migration — the git-status lane's compaction interaction (two-phase receipt). The git
* frame lives on the MESSAGE plane, does not enter the summary, and the projection strips it from
* serialized history — so a landed compaction would silently lose the model's git view while the
* announced hash keeps suppressing every re-send. When present:
* - `pending` is restated on the compaction entry's `details.gitAnnouncement` with
* `pending: true` in the SAME CAS as the new baseline (the promptEpoch/announcedListings
* restatement posture) — any reader between the landing and the re-assert receipt sees an
* OWED announcement, never a false "announced";
* - `land()` is invoked immediately after the landed baseline, BEFORE any context rebuild or
* retry (all three routes — turn-boundary auto, prompt-too-long recovery, end-of-task finish
* — funnel through this one append): it re-appends the current frame and writes the
* announced mirror once the append receipt is in hand. `land` contains its own failures (a
* failed re-assert leaves the pending restatement authoritative — the next boundary/leg
* re-announces); a throw here must never fail a LANDED compaction, so the call is belted.
*/
gitRestate?: {
pending: {
kind: "full" | "degraded" | "unavailable" | "non-repo";
hash: string;
};
land: () => Promise<void>;
};
/** The instruction files' post-compaction restatement (the runner's `compactionRestateOptions`): runs right after
* `gitRestate.land`, under the same belt — the landed compaction retired the entries of the instruction frames it
* summarized away, and the task root's own (owed on every load) must be in context for the next request. */
instructionsRestate?: () => Promise<void>;
/** The approval-posture directive's post-compaction restatement (the runner's `compactionRestateOptions`):
* runs right after `gitRestate.land`, under the same belt and for the same reason — the compacted
* context may have lost an engine frame the run still needs, and every reader of the new baseline
* (the next model request first) must see it restated. */
postureRestate?: () => Promise<void>;
/** The session plan file's post-compaction reference (`planFileRestateOption`, the runner's `compactionRestateOptions`):
* where the plan is and what it says, on a leg whose run admitted a plan file that holds one; runs right after
* `postureRestate`, under the same belt. */
planFileRestate?: () => Promise<void>;
/** The advisor tool's instructions' post-compaction restatement on a leg that mounts the tool (the runner's
* `compactionRestateOptions`): runs right after `postureRestate`, under the same belt, for the same reason. */
advisorRestate?: () => Promise<void>;
/** The declared reply language's post-compaction restatement (`languageRestateOption`, the runner's `compactionRestateOptions`):
* runs right after `advisorRestate`, under the same belt, for the same reason. */
languageRestate?: () => Promise<void>;
/** The run's sandbox instructions' post-compaction restatement (`sandboxFrameRestateOption`): runs right after `languageRestate`,
* under the same belt, for the same reason. */
sandboxFrameRestate?: () => Promise<void>;
/**
* Fixed per-request prompt overhead (system prompt + tool schemas, in tokens), applied ONLY in the
* anchor-less regime (design/64 §26.7, search [91] fourth candidate): when no assistant in the
* visible history carries real usage (custom Brain implementations that don't report usage), the
* chars/4 fallback counts MESSAGES only and is blind to this overhead — the threshold would compare
* apples to oranges and under-trigger. When usage-anchored, the anchor IS the provider-billed input
* (system+tools included), so the overhead is already in and this is not added.
* In every regime it is also the prefix size the pass judges before anything else: a prefix at or past the
* line makes a non-`force` pass return `windowBelowOverhead` without compacting. Absent ⇒ 0 (never judged so).
*/
overheadTokens?: number;
/**
* The engine frames every landing states again (the runner's `compactionRestateOptions`): the texts a landing writes again
* (`protectedTexts` — the permission posture's text; `restatedTexts` — the git frame), with the task root's instruction
* frames the context holds, counted whole (`restatedFramesTokens`).
* The "window below overhead" judgment reads them as a floor beside {@link overheadTokens}: a landing posts them again, so
* a prefix plus these frames at or past the line cannot be compacted under it. `overheadTokens` keeps its meaning — the
* anchor-less trigger estimate never adds this floor (the frames are messages the estimate already counts). Absent ⇒ no floor.
*/
restatedFrames?: {
readonly protectedTexts: readonly string[];
readonly restatedTexts: readonly string[];
};
/**
* design/84 Seam C — compact-boundary COST optimization (data-gated). Fidelity is already achieved by
* the iterative-merge UPDATE_SUMMARY path (compaction.ts); the only remaining cost is one LLM call per
* boundary. A `summaryProvider` lets a caller supply a summary WITHOUT an LLM call: when it returns a
* NON-EMPTY string, that string is reused verbatim as the summary (the real `generateSummary` LLM call
* is skipped, `reused:true`). Returning `null`/`undefined`/an empty-or-whitespace string falls back to
* the existing `generateSummary` path — IDENTICAL to current behavior. **Absent (default) = no behavior
* change whatsoever**: the LLM path runs byte-for-byte as before.
*
* THIS IS A THIN SEAM ONLY: no provider is implemented in core. The provider implementation is gated on
* design/82 cost-per-truly-correct data (§3/§7-5). Reuse is guarded against degradation (see below).
*
* Inputs:
* - `messagesToSummarize`: the message range that would be summarized this boundary.
* - `previousSummary`: the prior compaction's summary (present on the UPDATE/iterative-merge path).
* - `modifiedFiles`: files the summarized range modified, newest-first (same source the restore's
* attachments use), so a provider can decide whether the reused summary is still representative.
*/
summaryProvider?: (input: {
messagesToSummarize: AgentMessage[];
previousSummary?: string;
modifiedFiles: string[];
}) => string | null | undefined | Promise<string | null | undefined>;
/**
* design/84 Seam C reuse guard (c): after this many CONSECUTIVE provider-reuse compactions, the next
* compaction is FORCED through the real `generateSummary` LLM path even if the provider would hit — this
* bounds summary drift from indefinite reuse. The caller owns the counter via {@link onCompaction}
* (`reused`) and feeds it back via this option; core treats `summaryProvider` as if absent for that one
* boundary when `consecutiveProviderReuse >= maxConsecutiveProviderReuse`. Default `3`, a
* SEMA choice — the earlier "(CC parity)" label was unsupported and is withdrawn (anchoring
* re-check 2026-08-23: the reference has no external summary provider and no reuse concept at all;
* its three 3s are the PTL retry cap `b$d`, the consecutive-compaction-failure breaker `C$d` and
* the rapid-refill trip `Q3u`, none of them this). 3 is picked for the same reason those are: a
* small bound on how long a degraded path may keep answering before the real one is forced.
* `0`/undefined with no `consecutiveProviderReuse` = provider always consulted (no forced refresh).
*/
maxConsecutiveProviderReuse?: number;
/**
* design/84 Seam C reuse guard (c) — counter input: how many consecutive provider-reuse compactions have
* happened so far (caller-tracked from {@link onCompaction}'s `reused`). When this reaches
* `maxConsecutiveProviderReuse`, the provider is bypassed for this one boundary to force a real summary.
*/
consecutiveProviderReuse?: number;
/**
* design/84 Seam C — pure observer fired AFTER a successful compaction (never on a no-op/under-threshold
* return). Does NOT alter control flow. `reused` distinguishes a provider-supplied summary from a real
* LLM `generateSummary`. `freedTokens` is the §25.2 structural delta (may be ~0 when a reused summary
* frees little — see reuse guard (b): such a compaction does NOT raise the caller's anti-thrash floor).
* ABSENT when the post measurement failed — never a stand-in ("fully effective" is not true of a
* landing that leaves a summary behind). Every consumer reads absence as "no evidence": the floor
* stays where it is and the backoff does not flip.
*/
onCompaction?: (info: {
messagesCompacted: number;
freedTokens?: number;
tokensBefore: number;
reused: boolean;
}) => void;
/**
* design/134 §3.2 — which trigger this pass runs under, threaded into the pre/postCompact callback
* contexts and driving the block semantics: "auto" (threshold/boundary, the default) | "manual"
* (/compact) | "forced" (promptTooLong recovery and trim-pressure propagation — the compaction is
* not optional there, so a preCompact `block` is IGNORED on "forced").
*/
trigger?: "auto" | "manual" | "forced";
/**
* The pass has COMMITTED: the trigger gate passed, a valid cut point exists and no `preCompact` handler
* blocked it, and nothing has yet consulted the summary provider or called the model. Fired exactly once per
* pass — a retry inside the summary call is the same pass — and never for a pass that ends before this point
* (under threshold, held under the floor, disabled, nothing to compact, blocked, or cut by its own signal while
* the `preCompact` hook ran). A pass that fired it ends
* in `compacted: true` or a throw, so the caller that put a frame on the wire here owes the matching end.
* Observe-only: a throw — or the rejection of an `async` callback, which this seat does not await — is
* contained and never reaches the pass.
*/
onStarted?: (start: CompactionPassStart) => void;
/**
* design/134 (CC PreCompact parity) — fired AFTER the trigger gate and a CONFIRMED valid cut point
* (so every invocation corresponds to a compaction that would actually happen), BEFORE the
* provider/summary call. `block` skips this compaction on auto/manual (`{compacted:false,
* blocked:true}`); ignored on "forced". `additionalInstructions` is APPENDED to the summarization
* instructions (never replaces them). A throw is swallowed on every path — treated as
* no-block/no-instructions (the caller's wrapper owns tracing; an observer bug must never feed the
* caller's breaker or kill PTL recovery).
*/
preCompact?: (ctx: PreCompactContext) => PreCompactResult | undefined | Promise<PreCompactResult | undefined>;
/**
* design/134 (CC PostCompact parity) — observe-only, fired after the compaction LANDED
* (appendCompaction done, post measurement taken). A throw is swallowed (same isolation contract
* as {@link onCompaction}: the compaction already succeeded).
*/
postCompact?: (ctx: PostCompactContext) => void | Promise<void>;
/**
* MF-18 修② — observe-only: fired when a summary call's serialized conversation INPUT was
* truncated to fit the compaction model's context window (the ~300K-session shape that previously
* guaranteed a provider prompt-too-long throw at every boundary). The caller bridges it to the
* `compaction.input_truncated` trace frame (fidelity disclosure). Fired BEFORE the summary call, so
* it reports truncation even when the attempt later fails. `label` is always `"history"`: a pass sends
* one summary request over everything it folds (the kept tail starts at a group's opening, so no turn is
* split and no separate turn-prefix request exists).
*/
onInputTruncated?: (info: {
label: "history";
droppedChars: number;
keptChars: number;
}) => void;
/**
* The design/36 ToolSearch activation set LIVE at this pass (the
* runner threads `[...prepared.activeTools]` on all three lanes: boundary, PTL recovery, finish).
* Persisted into the landed compaction entry's details (`CompactionDetails.activeTools`) so the
* NEXT task boundary's re-derivation — whose transcript source (ToolSearch toolCall parts) this
* compaction just replaced with prose — re-seeds from a structured carrier instead (the
* checkpoint seed's `resume.seed.activeTools` sibling; prepare-task consumes it). Absent or
* empty ⇒ no key is written, byte-identical to pre-RB-402 entries.
*/
activeTools?: ReadonlyArray<string>;
}
/**
* The fixed request prefix (system prompt + tool schemas), with the engine frames every landing states again, is at or
* past the compaction line, so no summary can bring a request back under it: compaction shrinks conversation history and
* nothing else, and every landing would post over the line again. The numbers the judgment read, in the trigger's own
* coordinate (`compactAtTokens = windowTokens - reserveTokens`); `restatedTokens` (present when non-zero) is the frames'
* share of the floor.
*/
export interface WindowBelowOverhead {
overheadTokens: number;
restatedTokens?: number;
compactAtTokens: number;
windowTokens: number;
reserveTokens: number;
}
/**
* The one judgment of "even a full compaction cannot get below the line": the window is known and the floor — the fixed
* overhead plus the frames every landing states again — meets or exceeds `window - reserve`. Undefined when the window is
* unknown (that state has its own report, `unevaluableWindow`) or when history still has room to shrink into.
*/
export declare function windowBelowOverheadOf(overheadTokens: number, windowTokens: number, reserveTokens: number, restatedTokens?: number): WindowBelowOverhead | undefined;
/** The `compaction_outcome{outcome:"disabled"}` reason for {@link WindowBelowOverhead}: the word, then both numbers
* and the smallest window that would let compaction work with this floor and reserve. */
export declare function windowBelowOverheadReason(w: WindowBelowOverhead): string;
/**
* Compact the session in place when context usage exceeds the threshold.
*
* Two call sites, same policy: the within-task `turn_boundary` hook (design/64 §25 (A) — after the
* session flush, before the next request's context rebuild) and `finish()` at task end. The caller
* must ensure no session write is in flight (both sites are between model requests, post-flush).
*
* THROWS (council §26.6 #6 contract note): a summarization failure — including a summary that came
* back EMPTY after the analysis-block strip (a real LLM call was burned, history left untouched) —
* is thrown, not swallowed; callers are expected to catch, count it toward the §17.4 breaker, and
* surface it via onError. A legitimate nothing-to-compact (under threshold/floor, no cut point)
* returns `{compacted:false}` quietly.
* The summary runs on `model` (the main model) — never another one.
* Zero-config: uses DEFAULT_COMPACTION_SETTINGS unless overridden; settings pathological for the
* model's window are repaired via {@link sanitizeCompactionSettings}.
*/
export declare function maybeCompact(opts: MaybeCompactOptions): Promise<{
compacted: boolean;
/** design/134: set (with `compacted:false`) when a preCompact callback blocked this compaction
* (auto/manual triggers only — never on "forced"). NOT a failure: the caller must not count it
* toward the compaction breaker; the next boundary consults the callback again. */
blocked?: boolean;
/** MF-18 修①: set (with `compacted:false`) when this pass is DISABLED by its settings (`enabled:false`
* — the pass's own gate; a run's lanes resolve it from the seat of the pass they run, `autoCompact` or
* `manualCompact`). Lets the caller resolve a manual /compact with the honest `"disabled"` outcome (and
* trace frame) instead of a structural noop. */
disabled?: boolean;
/** Set (with `compacted:false`) when the pass PASSED the trigger gate
* (natural or forced) but found nothing to summarize — no valid cut point / empty history. Was
* previously indistinguishable from the quiet under-threshold return, so a forced pass with
* nothing to do looked identical to one that never wanted to run; the caller's outcome event
* (`compaction_outcome{outcome:"noop"}`) reads this flag. */
noop?: true;
/** The id of the pass this result concludes — the value {@link MaybeCompactOptions.onStarted} was handed.
* Set exactly when `compacted` is true: every other return of this function ends before the pass commits,
* and a pass that commits and does not land THROWS (the caller learned the id from the start callback). */
pass?: string;
tokensBefore?: number;
/** The §25.2 STRUCTURAL pre/post delta in ONE coordinate — the only subtractable quantity here.
* Absent ⇒ the post measurement failed and no delta was taken. */
freedTokens?: number;
triggerTokens?: number;
/** P-13: post-compaction context size in the SAME coordinate as `triggerTokens` (estimate + anchor-less
* overhead). The caller's §25.2 regrowth floor = `postTriggerTokens × factor`, single-coordinate — never
* `triggerTokens − freedTokens` (which mixed a usage-anchored trigger with a chars/4 freed delta). */
postTriggerTokens?: number;
/** design/99 MF-18 `preserved_segment`: the session-tree entry id where the KEPT TAIL begins (the floor of
* the surviving history after this compaction). Surfaced so the `compacted` wire event can carry it (CC
* `SDKCompactBoundary.preserved_segment` parity); a consumer maps it to wire messages via its eventId↔entryId
* map (the same one service keeps for resumeAt). Set only when `compacted` is true. */
firstKeptEntryId?: string;
/** design/84 Seam C: true when this compaction reused a `summaryProvider` summary (no LLM call). */
reused?: boolean;
/** The working files re-read into the summary, in attachment order —
* surfaced so the `compacted` wire event carries them and a shell renders CC's post-compact
* `Read {path} (…)` cards. Set only when `compacted` is true and attachments were added. */
attachedFiles?: Array<{
path: string;
chars: number;
truncated: boolean;
}>;
/** What became of each restore frame, frame order — its attachment record (`human_input.attachments`' own projection); a
* frame the transcript did not take is `not_attached`. Set only when `compacted` is true and the restore minted a frame. */
restoreOutcomes?: HumanInputAttachment[];
/** design/123 D4: true when the NATURAL gate fired (est ≥ max(threshold, minTokens) — no force was
* needed); false on a forced landing (`force`/`forceUnderThreshold`). Set only when `compacted`. */
naturalTrigger?: boolean;
/** design/123 D4: whether the post-compaction size (`postTriggerTokens` coordinate) is STILL over the
* sanitized auto threshold — the "couldn't compact it down" signal {@link nextTrimForceBackoff}
* consumes. Set only when `compacted` and the post measurement succeeded. */
postOverThreshold?: boolean;
/** TB telemetry B4: wall time of the whole compaction pass (context build → summary
* call → history rewrite → post measurement). Set only when `compacted` — the `compacted` wire event
* carries it so a consumer sees what a boundary pause actually cost. */
durationMs?: number;
/** 压缩子阶段遥测 (1.283 批 G 观察项 / design/145 §6 正交后续批 — TB 观测到整段 ~55s 停顿,需要
* 子阶段读数支撑归因): wall-time split of THIS landed pass, in the same Date.now() coordinate as
* `durationMs`. `prepareMs` = context build → trigger math → cut-point selection → hooks/provider
* consult → clamp dry run + 门A/门B gating (everything before the summary call). `summaryMs` = the
* summary LLM leg (`compact()`: serialization → clamp → completion, incl. any PTL retries) — ABSENT
* on a provider-reuse pass (no LLM call happened; its provider consult time sits in `prepareMs`).
* `persistMs` = restore reads → session rewrite (`appendCompaction`) → restore frames → post measurement →
* observers. The phases tile the pass exactly: prepareMs + (summaryMs ?? 0) + persistMs ===
* durationMs. `ptlRetries` = reactive prompt-too-long retries burned inside this pass's summary
* call(s) (0 on provider reuse). Set only when
* `compacted`. Exposure path note: runtask deliberately NOT touched this batch — the bridge to the
* `compaction.phase_timings` trace frame (trace.ts, defined-not-yet-emitted) is a follow-up wiring
* batch that adds an additive passthrough next to runtask's existing
* `...(comp.durationMs !== undefined ? { durationMs: comp.durationMs } : {})` spread. */
phaseDurations?: CompactionPhaseDurations;
/** C3: set (with `compacted:false`) when the pass was over the auto threshold but the
* anti-thrash floor (`minTokens`) suppressed it — the caller bridges this to `compaction.suppressed`. */
suppressedByFloor?: {
estTokens: number;
floor: number;
};
/** Set (with `compacted:false`, no summary call made) when a pass that was not `force` met a fixed prefix at or past
* its line ({@link windowBelowOverheadOf}): the pass is not attempted, at this boundary or any later one, while the
* prefix stays that large. `force` passes (manual, the prompt-too-long recovery, the in-turn guard compaction)
* are not judged here — they answer an overflow, not the line. The caller bridges this to one
* `compaction_outcome{outcome:"disabled"}` per task. */
windowBelowOverhead?: WindowBelowOverhead;
/**
* Set (with `compacted:false`) when the trigger predicate could not be EVALUATED at all: the model
* declares no usable autocompact window (non-finite or <= 0), so `shouldCompact` refuses on the
* sentinel and `contextUsage` is honestly absent.
*
* This is a different state from "under the threshold", and it used to be indistinguishable from it:
* both returned a bare `{compacted:false}` with no `contextUsage`, no trace and no error, so a
* deployment whose model config lost its window had auto-compaction permanently disabled and the
* only notification was that nothing ever happened. The context still grows, the provider still
* eventually refuses the request, and the operator has nothing to read. The caller bridges this to
* a once-per-task host disclosure — silence is the defect here, so a noisy line is the fix.
*
* `estTokens` is the size this pass DID manage to estimate (structural + prompt overhead when no
* usage anchor is available), so the disclosure can say how big the context already is even though
* it cannot say how big the window is.
*/
unevaluableWindow?: UnevaluableWindow;
/**
* The trigger inputs THIS pass used, reported on
* EVERY return path — including the ordinary "nothing to do" one. A consumer rendering "how full is the
* context" was otherwise forced to re-derive the numerator itself, and a re-derivation that drifts from
* the engine's own is the projection-antipattern this repo keeps paying for: the displayed percentage
* would disagree with when compaction actually fires.
*
* `usedTokens` is the exact first argument to `shouldCompact` (usage-anchored when the anchor is fresh,
* structural + fixed prompt overhead otherwise — see the derivation above). `windowTokens` is the
* AUTOCOMPACT window (`autoCompactTokens` when the model declares one, else its context window), NOT
* necessarily the physical request window. `compactAtTokens` is the threshold those two are compared
* against, so `usedTokens > compactAtTokens` is precisely the engine's own trigger predicate.
*/
contextUsage?: {
usedTokens: number;
windowTokens: number;
compactAtTokens: number;
};
}>;
/** What a landed compaction hands the wire frame. Structural, so the full `maybeCompact` result satisfies it. */
export interface CompactedFrameSource {
/** The pass this landing concludes (see {@link MaybeCompactOptions.onStarted}). */
pass?: string;
tokensBefore?: number;
freedTokens?: number;
postTriggerTokens?: number;
triggerTokens?: number;
durationMs?: number;
phaseDurations?: CompactionPhaseDurations;
firstKeptEntryId?: string;
attachedFiles?: Array<{
path: string;
chars: number;
truncated: boolean;
}>;
restoreOutcomes?: HumanInputAttachment[];
/** Cross-session message frames that sat in the summarized prefix (present iff ≥ 1). */
summarizedCrossSessionMessages?: number;
}
/**
* THE mint of the `compacted` wire frame — one function, three lanes (the turn boundary, the
* prompt-too-long recovery, the end-of-task settle). The three used to carry a verbatim copy of the
* same fourteen-line literal, which is how a field reaches the wire on two lanes out of three: a key
* added to one copy is invisible in the others' diffs, and the only guard against it was a test that
* grepped the SOURCE TEXT of all three for a matching line. A behavior can be pinned; a copy can only
* be spelled.
*
* What differs per lane is exactly `trigger` and the identity envelope, so those are the parameters;
* everything else is derived here, once. Absence semantics live on the event type
* ({@link import("./task-event.js").TaskEvent}) and are the same whichever lane fires.
*/
export declare function mintCompactedEvent(comp: CompactedFrameSource, trigger: "auto" | "manual" | "forced", identity: TaskEventIdentity): Extract<TaskEvent, {
type: "compacted";
}>;
/** `Runner.compactSession`'s request — the out-of-run manual compaction of a session no leg of the Runner holds. A plain
* data object; the keys are a closed set. */
export interface CompactSessionRequest {
/** The session to compact. It must exist. */
sessionId: string;
/** The model the session's next turn runs on — its window bounds the pass (resolved like `TaskSpec.model`). */
model: ModelRef;
/** This pass's summarization instructions: they REPLACE `compaction.instructions` and the default for this pass (the
* `TaskStream.compact` reading); sanitized and capped like every instructions channel. */
instructions?: string;
/** The members of `TaskSpec.compaction` a pass reads, as a run would carry them — of the two seats only `manualCompact`
* (this pass is a requested one; `false` answers `"disabled"`, and absent reads the deployment's twin, else on). */
compaction?: Pick<NonNullable<TaskSpec["compaction"]>, "manualCompact" | "reserveTokens" | "keepRecentTokens" | "instructions">;
/** Per-model credentials (`TaskSpec.getApiKeyAndHeaders`). */
getApiKeyAndHeaders?: TaskSpec["getApiKeyAndHeaders"];
/** The checkpoint store the session's runs park in, when a run named its own (`TaskSpec.checkpointStore`): the same three-valued
* seat, read by the same one resolver — a store wins, absent falls through to the deployment's `RunnerDeps.checkpointStore`, and
* `"disabled"` says the session has no store (no durable park to ask about). The pass's park check asks the store this names. */
checkpointStore?: TaskSpec["checkpointStore"];
/** Withdraws the pass: aborted before the session's lock is taken, the answer is `"mooted"` with nothing done; while the pass
* runs the summary call and the credential wait read it (store reads and appends, and the pre-flight's own 5 s timer, do not); a pass that already persisted is not undone. */
signal?: AbortSignal;
/** The occupant the caller read the session as: `{ row: { owner, incarnation } }` (a `SessionHead` serves as `row`). The
* session the store holds under `sessionId` must still be that row — the same owner and occupant cell, judged by
* `sessionRowMatches` on the acquired session — or nothing is compacted and the answer is `resume.session_not_found`
* (it is not the session the caller meant: deleted and created again under the id, or another owner's). Only the row:
* `row: null` and `log` are refused at the request door (`compact.invalid_request`) — the session exists, and the pass
* binds its own leaf. Absent ⇒ any session under the id. */
expect?: SessionRowExpectation;
/** The grant the host's verb acts under (`encodeGrantHolder`), the run's `RunInternals.holder` twin — the one statement of the
* three session verbs' `holder` (`RewindConversationRequest.holder` and `RewindFilesRequest.holder` read as this one): the summary record the pass
* writes carries it (`SessionWriteOptions.holder`), and the session store must declare `fence = { holder: "writes" }`
* (`config.invalid_session_store` otherwise, nothing compacted). A grant the store does not hold current refuses the write: the
* outcome is `failed` with `cause: "holder.superseded"`, nothing landed. A store that declares `requireHolder: true` refuses a
* request without one (`config.invalid_session_store`). A non-empty string when present (`compact.invalid_request`). */
holder?: string;
}
/** `Runner.compactSession`'s answer. */
export interface CompactSessionResult {
/** What the pass did — the closed set `TaskStream.compact` resolves with. */
outcome: CompactOutcome;
/**
* The pass's frames, in order, each with its own `eventId` and no task attribution: `compaction_started` once the
* pass committed, then the frame that ends it — `compacted` (`trigger: "manual"`) on a landing, else
* `compaction_outcome` (`trigger: "manual"`). Empty when the pass never ran (a withdrawn request). Returned, not
* streamed: a `compaction_started` here is a record of the pass, not a live progress signal.
*/
events: Array<Extract<TaskEvent, {
type: "compaction_started" | "compacted" | "compaction_outcome";
}>>;
/** The model calls the pass made (the summary, a hook's `$.model.complete`), priced as a run's side calls are; absent
* when none was made. `usageMissing` ⇒ a reply reported no usage (a lower bound); `unpriced` ⇒ a call ran on a model
* the deployment could not price. */
usage?: {
tokens: number;
totalInputTokens: number;
promptTokens: number;
cachedTokens: number;
cacheWriteTokens: number;
outputTokens: number;
costMicroUsd: number;
usageMissing?: true;
unpriced?: true;
};
}
/**
* The MANUAL outcome of a pass that returned (did not throw) — the one reading for every manual pass: the turn
* boundary serving a `TaskStream.compact` request and the out-of-run `Runner.compactSession`. A manual pass forces
* (`force` + `minTokens: 0`), so the floor's `suppressedByFloor` cannot occur and is not a case here.
*/
export declare function manualCompactOutcomeOf(comp: {
compacted: boolean;
blocked?: boolean;
disabled?: boolean;
}): CompactOutcome;
/**
* THE mint of the `compaction_started` wire frame — the opening half of the pair whose closing half is the
* `compacted` frame above (or a `compaction_outcome` that carries the same `pass`). One function, three
* lanes, as the landing's: the frame is the start report itself plus the identity envelope, so a lane that
* wants to announce a pass can only announce the one `maybeCompact` committed.
*/
export declare function mintCompactionStartedEvent(start: CompactionPassStart, identity: TaskEventIdentity): Extract<TaskEvent, {
type: "compaction_started";
}>;
/**
* One compaction pass's account on the wire: the `onStarted` seat to hand `maybeCompact`, and the id the frame
* that ENDS the pass must carry. A lane makes one per pass, hands `onStarted` to the pass, and spreads
* {@link CompactionPassReport.end} into every `compaction_outcome` the pass can end with after it committed — a
* pass that threw before it committed leaves it empty, which is exactly "no start was announced, so no `pass`
* names it". The landing needs no spread: `maybeCompact` returns the id and `mintCompactedEvent` reads it.
*/
export interface CompactionPassReport {
readonly onStarted: (start: CompactionPassStart) => void;
/** The announced pass's id; `undefined` until a start went on the wire. */
readonly pass: string | undefined;
/** `{ pass }` once a start was announced, `{}` before — for the frame that ends the pass. */
readonly end: {
pass?: string;
};
}
export declare function compactionPassReport(sink: {
push(event: TaskEvent): void;
}, ident: () => TaskEventIdentity): CompactionPassReport;
/**
* How many cross-session message frames the summarized prefix held: the engine-note user messages
* whose text is the drain's `<cross-session-message …>` envelope, before the kept tail begins. The
* frames were delivered (acked from the box) and are now only whatever the summary kept of them — the
* count is what lets a consumer say so; `0` is reported as absence.
*/
export declare function countSummarizedCrossSessionMessages(branch: ReadonlyArray<{
type: string;
id: string;
message?: {
role: string;
provenance?: string;
content: unknown;
};
}>, firstKeptEntryId: string): number;
/**
* design/123 D4 (r5) — trim-force BACKOFF transition (pure; the run-scoped bit lives in the caller).
* Spiral protection replacing the §25.2 floor's role on the trim-force path: trim pressure re-arms
* every request while the transcript is oversized, so an unconditional force would burn a summary
* call at every boundary when summaries CANNOT shrink the context (the LONGRUN-1b incompressible
* shape). The invariant: trim-force is permitted only while the LATEST landed compaction proved it
* can bring the context back under the threshold (or no compaction has landed yet). Transitions,
* evaluated after each maybeCompact result:
* 1. no compaction landed → state unchanged (a structural no-op proves nothing either way);
* 2. a landed compaction with `postOverThreshold` → "压不动": BACKOFF ON — compaction demonstrably
* cannot reach under-threshold, so forcing more of them only burns summary calls; the §25.2
* floor policy takes back over (natural triggers are never gated by this bit);
* 3. a landed compaction that posted UNDER the threshold → summaries compress again: BACKOFF OFF
* (this is how the natural floor-crossing compaction of design/123 D4's reset clause re-enables
* the trim-force — its healthy landing posts under threshold);
* 4. post measurement unavailable → state unchanged (never flip on missing evidence);
* 5. (COMPACTION-LIVE-AUDIT D-1, evaluated BEFORE rule 3) a TRIM-FORCED landing with
* `freedTokens ≤ 0` → negative-yield: BACKOFF ON. The trim layer deceives the usage-anchored
* estimate down into the sawtooth trough (live: est 18807 on a ~55k session), the forced pass
* finds little compressible history, and summary + restore frames make the transcript
* LARGER (live: 18807→44609, 12679→35214). `freedTokens` is the structural pre/post delta
* (clamped ≥0, so ≤0 means "freed nothing") — orthogonal to rule 2/3's threshold test, which
* misses this shape because the bloated post (44609) can still sit UNDER the threshold (44800),
* so rule 3 alone would re-enable the force and repeat the negative-yield pass every boundary.
* SEMA-ONLY DEFENSE, not a CC port — but the ORIGINAL justification for that label was wrong
* on both of its premises, and the corrected pair still supports it. (a) "CC's full compact
* keeps tail 0 and monotonically shrinks" describes the `GNo` branch, which a LOCAL threshold
* compaction never reaches (the reference routes local passes to the reactive
* pipeline, which preserves the last group verbatim); the reference's
* default posture therefore has a keep-tail, exactly as ours does. (b) "CC has no
* request-layer trim" holds only for the MAIN conversation request: CC does own a
* keep-the-newest-groups-that-fit primitive (`UPb` :645223), whose only caller is the
* prompt-hook evaluator's own transcript window, not a main-lane request. What actually makes
* this regime ours is the third factor — the trim seam feeding a deceived usage anchor, and
* attachment mass re-entering after the fold — not the absence of a keep-tail. Scoped to trim-forced
* passes only (`trimForced`), so Seam C reused-summary landings (freed≈0 by design) on the
* natural/manual paths never trip it. Release path unchanged: backoff gates only the FORCE, a
* later natural landing that posts under threshold (rule 3, non-trim-forced) turns it back off.
*/
export declare function nextTrimForceBackoff(prev: boolean, comp: {
compacted: boolean;
postOverThreshold?: boolean;
freedTokens?: number;
naturalTrigger?: boolean;
}, trimForced?: boolean): boolean;
/** design/119 S3 (rapid_refill_breaker): mutable per-task refill tracking state. */
export interface RapidRefillState {
lastCompactionTurn: number;
count: number;
}
/** Fresh tracking state (no compaction seen yet). */
export declare function createRapidRefillState(): RapidRefillState;
/**
* design/119 S3 (rapid_refill_breaker): record that a compaction just
* happened at `turn` and report whether the thrash breaker should TRIP. A compaction landing within
* `minGapTurns` of the previous one is a "rapid refill"; `tripAt` consecutive rapid refills = the
* context is dominated by incompressible content and further compaction only burns summary calls.
* CC terminates the task; sema's caller (runtask) deviates conservatively — it opens the existing
* compaction breaker (no more compaction this task) and lets budget/PTL/turn limits end the run.
*/
export declare function recordCompactionAndCheckRapidRefill(state: RapidRefillState, turn: number, minGapTurns?: number, tripAt?: number): boolean;