UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

169 lines (168 loc) • 13.8 kB
/** * The deployment's per-model CONTEXT-WINDOW overlay — the one deployment seat an IN-FLIGHT leg re-reads. * * WHY IT IS NOT ON THE MODEL. `Model.contextTokens` / `Model.autoCompactTokens` are two things at once: the * geometry a leg measures itself against, and part of the immutable routing card a leg was handed at its * prepare. The hot-swap law (`swappable-deps.ts`) is right about the card — nothing is re-routed, re-priced or * re-fenced mid-run, because a running request must reach the endpoint it was priced and credentialed for. * It was wrong about the geometry: an operator who lowers a model's usable window is not re-routing anything, * they are changing a SETTING, and a leg that keeps filling the retired window until it ends is the one thing * they were trying to prevent. The reference implementation keeps that setting off the model card for exactly * this reason and re-reads it on every request, clamping it to the model's physical window. * * So the overlay lives here, keyed by CATALOG WORD — the same word `ModelRef` already speaks. A leg carries * the ref that chose its model (`ResolvedRole.modelRef` → the harness → `ToolExecuteContext.modelRef`), so * the lookup is a plain read with no reverse search: an id→word search cannot answer (one model is reachable * under several words, two words may route to one id, and an object-pinned model has no word at all), and a * leg that knows its own word needs no search. * * WHAT IT DOES NOT DO. It carries no routing, credential, price or fence field, and never will: those are the * card, and the card stays snapshotted. An OBJECT-pinned leg (a spec that named a `Model`, a `select` criteria) * has no word and therefore no overlay — the pin is the caller saying "this exact model, as declared". */ import type { Model } from "../internal/llm.js"; import type { ModelRef } from "./model-seat.js"; /** * One model's overlay. Every member is a DECLARATION the deployment may change between requests; an absent * member means "whatever the model card says", never a value. * · `contextTokens` — the usable physical window (the model card's `contextTokens ?? contextWindow`). * · `autoCompactTokens` — the trigger-side window (dual-window semantics: lowers the compaction trigger and * the context-editing frontier without touching the physical request budget). Clamped to the physical * window by the one resolver, exactly as a model-card declaration is. * · `reserveTokens` — the trigger deduction (the compaction threshold is the trigger window minus it): the * deployment's per-model twin of `TaskSpec.compaction.reserveTokens`, under {@link compactionReserve}'s law — the * request's own value is the author, this fills an unset one, nothing declared is the engine's default. A positive * whole number, below every window this overlay itself declares (a window only the model card declares is judged * where the pass runs: the trigger repairs a reserve that would leave no threshold). * · `autoCompact` / `manualCompact` — the deployment's twins of the task's two compaction seats, each with its * seat's reach and each the default for its own seat only ({@link compactionEnabled}). */ export interface ContextWindowOverlay { readonly contextTokens?: number; readonly autoCompactTokens?: number; readonly reserveTokens?: number; readonly autoCompact?: boolean; readonly manualCompact?: boolean; } /** * The two compaction seats, one per kind of pass — which pass reads which seat is {@link compactionEnabled}'s * contract. A task's `compaction`, a `Runner.compactSession` request's `compaction` (which takes `manualCompact` * only — a pass outside a run is a requested one) and a deployment's overlay carry them, and every one of those * doors judges them with {@link compactionSeatsDefect}. */ export declare const COMPACTION_SEATS: readonly ["autoCompact", "manualCompact"]; export type CompactionSeat = (typeof COMPACTION_SEATS)[number]; /** * The seats' ONE judgment, for every bag that carries them (a task's `compaction`, a `Runner.compactSession` request's * `compaction`, a deployment overlay): the retired single seat `enabled` is refused by name with the sentence that names * the two ({@link RETIRED_COMPACTION_SEAT_KEYS}; never read as either), then a seat present (a defined value) that is not * a boolean. `subject` is the bag in the door's own words (`TaskSpec.compaction`, `compaction`, * `RunnerDeps: contextSettings["w"]`). Answers which and the sentence; each door throws it with its own code. */ export declare function compactionSeatsDefect(bag: object, subject: string): { readonly kind: "retired" | "value"; readonly sentence: string; } | undefined; /** * The seat: catalog word → overlay. Words absent from the table are "nothing declared". * * @contract context_settings.geometry_is_read_live — this is the ONE deployment seat a leg re-reads while it * runs: every window number a leg is measured against (the compaction trigger, the sanitize clamp, the * context-editing frontier, the request guard, the disclosed `contextUsage`) is taken from the overlay in * force at the moment of THAT question, keyed by the word that chose the leg's current model, so a swap * reaches boundaries of legs already in flight; the leg's ROUTING — endpoint, credentials, price, fence, the * `Model` object itself — stays the generation the leg prepared under, and this seat can never carry any of * it. A leg whose model was pinned as an object has no word and is never overlaid. */ export type ContextSettings = Readonly<Record<string, ContextWindowOverlay>>; /** * The overlay in force for a leg, read at the moment of the question. `ref` is the leg's own catalog word * (`AgentHarness.getModelRef()` / `ResolvedRole.modelRef`); a `Model` OBJECT answers `undefined` because a * pinned model has no word for the table to key on. OWN-property read: a word like `"constructor"` must mean * "a key this deployment declared", never `Object.prototype`'s answer. * * The leg's own word first; when it declares nothing, the words it STANDS IN FOR — a tier word the default * role answers with, a CC alias, `best` — as the tier expansion recorded them ({@link tierAliasVia}): only * words that name the very model the leg's word resolves to, so a deployment key that a tier word passes by * but that names ANOTHER model is never read. Nothing is found by searching for a model. */ export declare function contextOverlayFor(settings: ContextSettings | undefined, ref: ModelRef | undefined, catalog?: { models?: Record<string, Model>; }): ContextWindowOverlay | undefined; /** * Every word the overlay declares must be a word of the model catalog (the deployment's keys, the tier words * and aliases its `tiers` expanded) — `config.context_settings_unknown_word`. A misspelled word keys a leg * nobody can run, so it would govern nothing while reading as in force. Checked at construction and on every * swap against the catalog the swap leaves in force, so a catalog swap that would orphan a declared word is * refused too. Returns `settings`. */ export declare function assertContextSettingsWords<T extends ContextSettings | undefined>(settings: T, models: Record<string, Model> | undefined, where: string): T; /** * The seat's VALUE GATE — the same one the hot-swap door and the construction path apply, so a value the door * admits is a value every later read admits. A malformed overlay is refused LOUDLY rather than repaired * quietly: a window silently dropped back to the model card's is the misconfiguration this seat exists to make * visible. `undefined` (the seat cleared) passes through. * * `where` names the caller in the message (`"swapDeps"` / `"RunnerDeps"`), since the two paths are the two * moments an operator can get it wrong. */ export declare function assertContextSettingsValue(v: unknown, where: string): ContextSettings | undefined; /** * Is THIS seat on for a pass? * * @contract compaction.two_seats — every compaction pass asks exactly one seat, through this function, and the seat * is fixed by who starts the pass. `autoCompact` holds the three lanes the engine starts by itself: the turn-boundary * pass at the window threshold (with the trim-pressure pass it forces), the recovery pass (a provider's * prompt-too-long rejection, the request guard's compaction arm) and the end-of-task summary. `manualCompact` holds * the passes a caller asks for: `TaskStream.compact`, an extension's `$.session.compact` and `Runner.compactSession`. * One law per seat: the request's own value for the seat is the author, the deployment overlay's twin of the same seat * fills an unset one, nothing declared means ON — and switching one seat off never switches off the other. With * `autoCompact` off no recovery pass runs: a request the provider rejects as too long is neither summarized nor * retried, and the rejection ends the run as the terminal failure it is (the provider's code and sentence), never a * silent retry — except a rejection stating that the prompt fits and only prompt plus output cap exceed the window: * that request is handed to the model once more with only its output cap lowered (no summary, no message dropped), * announced on the status channel, as the Anthropic adapter already does for its own wording inside the call. With `manualCompact` off a requested pass is answered `"disabled"` with its `compaction_outcome` frame * (`trigger: "manual"`) and no model call, and the boundary goes on with its own pass. */ export declare function compactionEnabled(seat: CompactionSeat, requested: Readonly<Partial<Record<CompactionSeat, boolean>>> | undefined, overlay: ContextWindowOverlay | undefined): boolean; /** * The reserve a compaction pass subtracts from its trigger window — the same law the seats follow, for the number: the * request's own value is the author (`TaskSpec.compaction.reserveTokens`, a `compactSession` request's), the deployment's * overlay for the leg's model fills an unset one, and nothing declared is the engine's default (`undefined` here: the caller * holds the default). Read by the one settings assembly (`legCompactionSettings`) that every pass and the disclosure * estimating the same threshold share, so no pass and no estimate can read another number. */ export declare function compactionReserve(requested: number | undefined, overlay: ContextWindowOverlay | undefined): number | undefined; /** Is this a compaction reserve a person could have meant — a positive whole number of tokens? The one reading of that shape: the overlay * door's twin of the number (`assertContextSettingsValue`) and the task's own ({@link compactionReserveDefect}) both ask it. */ export declare function isReserveTokenCount(value: unknown): value is number; /** * The ONE gate of a DECLARED compaction reserve, asked at prepare where the leg's model is known. The request's value * (`TaskSpec.compaction.reserveTokens`) is a positive whole number of tokens when given — `null` or a string is refused, never a * fall-through to the overlay's value or the default, and neither is `NaN`, an infinity, a fraction, zero or a negative ({@link isReserveTokenCount}, the overlay's law) — and the value in force ({@link compactionReserve}) must be below the leg's physical window * ({@link physicalWindowOf}): at or above it no threshold is left, and the trigger's repair (a fraction of the window, * `sanitizeCompactionSettings`) is for the DEFAULT meeting a small window, not for a number someone wrote. Nothing declared, or a * card with no window of its own, answers `undefined`. Returns the refusing sentence. * * A delegated child's `compaction` travels whole from its parent, which judged it against the parent's model; the child's own * model may have a smaller window, where the trigger's repair stands — so for a child (`inherited`) a task value is held to the * type and shape gates only (never to the window), and the overlay's value (the deployment's declaration for the child's own model) is still held to the window. */ export declare function compactionReserveDefect(requested: unknown, overlay: ContextWindowOverlay | undefined, model: Model, inherited?: boolean): string | undefined; /** The prepare door of {@link compactionReserveDefect}: the refusal as `config.invalid_argument`, thrown before any resource is acquired, entered with * the input the reserve came from. */ export declare function assertCompactionReserve(requested: unknown, overlay: ContextWindowOverlay | undefined, model: Model, inherited: boolean): void; /** * The overlay's view of a model's PHYSICAL window — the model card's chain with the deployment's live * declaration CLAMPED over it. The single expression `guardBudget` and `resolveTriggerWindow` share. * * The clamp is the whole difference between a setting and a claim. An overlay says how much of a model a * deployment wants used; it cannot say what the endpoint will accept, and the endpoint a leg talks to is the * one it prepared under. Letting a declaration RAISE the window would let an operator publishing a larger * model and its larger window in one step hand the bigger number to legs still routed at the smaller * endpoint — the guard would stop protecting the limit it exists for. So: lower freely, never raise. (Same * clamp the reference implementation applies to its configured window: `min(model window, configured)`.) * A card with no usable window of its own has nothing to clamp against, and the declaration is then the only * number there is. */ export declare function physicalWindowOf(model: Model, overlay: ContextWindowOverlay | undefined): number;