UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

116 lines (115 loc) • 8.92 kB
/** * A model request's FALLBACK CHAIN — the catalog half and the deployment's gate. * * The rule, once: a model request has ONE chain — the requested catalog entry, then the entries its `Model.fallbacks` words name, * in order; every hop is served by the deployment's own Brain; only a failure BEFORE the first content, for a reason the gate * admits, moves the request to the next hop. Where a chain is declared (the catalog entry), how it is walked (the model lane, * `brain/model-lane.ts`) and why a hop is left (the gate below) each have one place. * * This module is what the catalog door resolves and what the deployment configures: * - the CHAIN TABLE: each catalog entry's words resolved over the catalog generation that received it, keyed by the received * entry object, so a leg in flight keeps the chain of the generation its model came from and a hot-swapped catalog writes the * chains of its own entries. The lane reads a chain by the Model object a request names — never by a word at dispatch time. * - the GATE: the closed reason words a hop failure is read in ({@link DEGRADE_REASONS}; the same words `TaskResult.degraded` * reports), the default gate, and the deployment's policy seat (`RunnerDeps.modelFallback`), received once. * - the code every refusal of a fallback declaration carries: `config.model_fallback_invalid`. */ import type { Model } from "../internal/llm.js"; /** * Why a request left a hop — the reason words of the fallback gate, the words `TaskResult.degraded.reason` reports, one list both * ways (a word a gate is configured with is a word a result can carry): * - `rate_limit` (`[rate_limit]`), `server_error` (`[server]`: 5xx incl. 529, 408/409, past the brain's own retry budget), * `last_resort` (`[http]`: 404 and other statuses no retry helps), `breaker_open` (a circuit breaker's open-circuit fast-fail) — * the default gate; * - `network` (`[network]` that is not an open circuit: a connection that never formed, a first-token timeout, the lane's own * per-hop first-token deadline), `auth` (`[auth]`: 401/403), `stream_torn` (`[stream_torn]`: a response lost before any content) — * admitted only when the deployment adds them; * - `budget` — the run's near-budget switch (`TaskSpec.limits.degrade`), never a gate word; * - `route_refused` — the requested model's own route was refused by the deployment Brain's judgment before it was dispatched * (nothing was sent; a refused hop always moves the walk on), never a gate word. */ export declare const DEGRADE_REASONS: readonly ["breaker_open", "rate_limit", "budget", "server_error", "last_resort", "network", "auth", "stream_torn", "route_refused"]; export type DegradeReason = (typeof DEGRADE_REASONS)[number]; /** Whether a value is one of the reason words. */ export declare function isDegradeReason(value: unknown): value is DegradeReason; /** The reasons that are not a failure of a dispatched hop — a gate reads a hop's failure, so neither is a gate word. */ type NonGateReason = "budget" | "route_refused"; /** The words a gate may hold: every reason but `budget` and `route_refused`. The one list the policy door judges a gate against and * every refusal sentence that names the words reads, and the one a deployment's own documentation or completion reads at runtime * (a root export, frozen). */ export declare const MODEL_FALLBACK_GATE_WORDS: readonly Exclude<DegradeReason, NonGateReason>[]; /** The gate a deployment that configures none walks on: the four reactive classes (a root export, frozen). */ export declare const DEFAULT_FALLBACK_GATE: readonly DegradeReason[]; /** What a gate row may admit besides a list of reason words: every failure the shared judge lets through at all. */ export declare const ANY_HOP_FAILURE: "any"; /** * One ROW of the hop gate — which failures before a hop's first content move a request to the next hop. There is ONE judge * of a hop's failure (`gateReadingOf`, `brain/model-lane.ts`); a row is only the data it reads: * - the MODEL row is the deployment's policy ({@link ReceivedModelFallbackPolicy.on}): reason words, the four reactive ones unless the * deployment adds more; a first hop's `invalid_request` and a failure the Brain stated no code for never move it; * - the GATEWAY row ({@link GATEWAY_HOP_GATE}, `createFailoverBrain`'s hops): any failure before content moves it — a gateway swap * serves the same model, so even an `invalid_request` or an uncoded failure may be that gateway's own. * Both rows share what never moves a hop: a caller's abort, anything after content, a refusal of the response's own kind * (`length_empty`, `image_unprocessable`, `tool_schema_refused`). */ export interface HopGate { readonly on: readonly DegradeReason[] | typeof ANY_HOP_FAILURE; } /** The gateway row of the hop gate. */ export declare const GATEWAY_HOP_GATE: HopGate; /** Whether a gate row admits `reason` (a list holds it, or the row admits any failure). */ export declare function hopGateAdmits(gate: HopGate, reason: DegradeReason): boolean; /** How many words one entry's chain may name (the reference implementation's cap of three); more is refused, never truncated. */ export declare const MAX_MODEL_FALLBACKS = 3; /** The code of every refusal of a fallback declaration: a catalog entry's words, a Model object carrying them outside the catalog, * the deployment's policy seat. */ export declare const MODEL_FALLBACK_INVALID = "config.model_fallback_invalid"; /** The refusal of a fallback declaration, at the seat that wrote it (`detail.field`). */ export declare function modelFallbackRefusal(field: string, message: string): Error & { code: typeof MODEL_FALLBACK_INVALID; detail: { field: string; }; }; /** The chain resolved for `model` over its catalog — the hops after it, nearest first; empty for a model without one (an entry * that declares none, a Model object that is no catalog entry). */ export declare function modelFallbackChainOf(model: Model): readonly Model[]; /** The chain recorded for `model`, or `undefined` when none was (the chain judgment's read, `roles.ts`; the lane reads * {@link modelFallbackChainOf}). */ export declare function recordedFallbackChainOf(model: Model): readonly Model[] | undefined; /** The chain's one write: `model`'s chain as a catalog resolved it — the catalog door's generation, a read of a caller's catalog, a * host's tier expansion (`roles.ts`, `chainedSnapshot`) — once per snapshot. */ export declare function recordFallbackChain(model: Model, hops: readonly Model[]): void; /** The deployment's fallback policy as written at `RunnerDeps.modelFallback`. */ export interface ModelFallbackPolicy { /** * The reasons a hop failure before its first content moves the request to the next hop; absent ⇒ {@link DEFAULT_FALLBACK_GATE} * (`network`, `auth` and `stream_torn` are the deployment's to add; `budget` and `route_refused` are not gate words — a hop the * deployment Brain's route judgment refuses is never dispatched and always moves the walk on). Never moved, whatever `on` * says: a first hop's `invalid_request` (a later hop's moves on), a refusal, an empty-length cut, an image or tool schema the * provider refused, the caller's abort, a failure the Brain stated no code for, and any failure after content (nothing is replayed). */ on?: readonly DegradeReason[]; /** * A first-token deadline for every hop that is not the chain's last: a hop that streams no content within this many ms is * abandoned as a `network` failure (its signal aborted with that coded reason, every wait on it cut), so `network` must be in * `on`. The last hop keeps only the Brain's own clocks. Absent ⇒ none. */ firstTokenMs?: number; } /** The policy the lane walks on — the declared shape, frozen, with the gate filled in. */ export interface ReceivedModelFallbackPolicy { readonly on: readonly DegradeReason[]; readonly firstTokenMs?: number; } /** The policy of a deployment that configures none. */ export declare const DEFAULT_MODEL_FALLBACK_POLICY: ReceivedModelFallbackPolicy; /** * THE policy door: `RunnerDeps.modelFallback` read once (a plain record of `on` / `firstTokenMs`, nothing else) and judged — `on` * a list of gate words ({@link MODEL_FALLBACK_GATE_WORDS}) without a repeat, `firstTokenMs` a whole number of milliseconds a timer can hold, * and a deadline only beside a gate that admits `network` (the deadline cuts a hop AS a network failure; without `network` it * would end the request on the hop it was meant to leave). Every refusal is `config.model_fallback_invalid`. Absent ⇒ the * default policy. A policy this door received comes back as itself. */ export declare function receiveModelFallbackPolicy(value: unknown, field: string): ReceivedModelFallbackPolicy; export {};