UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

153 lines (152 loc) • 13.7 kB
import type { Model } from "../internal/llm.js"; import type { TaskSpec } from "./task-spec.js"; import type { AssertAllKeysHandled } from "./ask-origin.js"; import type { RunnerDeps } from "./runner-deps.js"; import type { PlaintextOrigins } from "@sema-agent/primitives"; import { type ReadDenyDeploymentSeat } from "./read-face-seats.js"; /** * The closed set of hot-swappable deployment seats. * - `models` — the model catalog generation (the same-name redirect, re-pricing and retirement rules * of the former `swapModels`: a string ref resolves against the new catalog at the next * prepare; a `Model` object a leg already holds is untouched). * - `tiers` — the tier bindings, a BINDING OVER a catalog: it may only be swapped together with * `models` (the door refuses `tiers` alone — re-expanding bindings over an already * expanded catalog would keep the retired tier words as ordinary keys, which is exactly * the stale-generation shape the swap exists to end). Omitted beside `models` ⇒ the * current bindings are re-applied over the new catalog; present (including `undefined`) * ⇒ replaced. * - `readFace` — the deployment's read-face declaration ({@link RunnerDeps.readFace}); screened by the * same value gate every prepare applies (`assertContainmentFaceValue`: `"open"` | `"roots"` * | absent, anything else refused loudly). The RESOLVED face of a leg is still the * resolver's business (task seat, governance, the read-only mount); the swap changes the * deployment seat that resolution reads. * - `writeFace` — the deployment's write-face declaration ({@link RunnerDeps.writeFace}); the read seat's * twin (same value gate, same in-flight snapshot rule, its own notice) and independent of it. * - `contextSettings` — the per-model context-window overlay ({@link RunnerDeps.contextSettings}); screened * by the same value gate construction applies (`assertContextSettingsValue`). THE ONE SEAT * IN-FLIGHT LEGS RE-READ — see the header: it carries geometry, not route. * - `readDenyPatterns` / `readDenyBuiltinTiers` / `readDenyBuiltinExclude` / `readDenyFloorRequired` — the * deployment's read-deny declaration (additions, built-in tier selection, built-in row removal, * whether the byte floor must be kept); screened by the gates a prepare applies * ({@link screenReadDenySeat}). A resumed leg compiles against the seats in force at its prepare * (the checkpoint keeps additions only, stricter-wins). * - `memoryConsolidationDriver` — the memory consolidation seat ({@link RunnerDeps.memoryConsolidationDriver}); the door takes * it in six steps, and a step that throws leaves nothing written and no swap line: (1) its own frozen * snapshot ({@link receiveConsolidationDriverSeat}: five members each read once, any other member refused; an * endpoint triple read as `new Runner` reads it, its three members once and any other member unread; `contract` * copied whole; a chat function by reference); (2) the endpoint key taken in as a credential the engine holds * ({@link holdConsolidationSeatKey}, on the swap's own path {@link receiveSwappableDeps}) — a key read before a * later step refuses the swap STAYS held (failing closed), and a key that is no credential is said as the * intake always says it (`config.credential_not_withheld`), refused swap or not; (3) the screens its first use * applies ({@link screenConsolidationDriverSeat}; a chat function is host code, so the endpoint screens and the * key intake do not reach it — its explicit model id does; a refused root is shown as the start shows it, * KL-f6w1-r5-d); (4) the arming question of a swap (`assertSwapKeepsAutoConsolidationWirable`, in * `Runner.swapDeps`, on every swap): a swap may not take the Runner's consolidation down a level (a * consolidation starts → a seat the start refuses → no seat), by the judge the start itself asks; keeping or * raising the level, or a Runner not armed, is not this door's business; (5) the write; (6) one * `config.memory_consolidation_driver_swapped` ({@link announceConsolidationDriverSwap}, of the deps written). * A consolidation started after the swap runs on the new seat; one * already running finishes on the seat it started with. */ export declare const SWAPPABLE_DEP_SEATS: readonly ["models", "tiers", "readFace", "writeFace", "contextSettings", "readDenyPatterns", "readDenyBuiltinTiers", "readDenyBuiltinExclude", "readDenyFloorRequired", "readDenyBuiltinRequestExcludable", "memoryConsolidationDriver"]; export type SwappableDepSeat = (typeof SWAPPABLE_DEP_SEATS)[number]; /** The candidate a swap takes: the swappable seats of {@link RunnerDeps}, each optional (presence = instruction). */ export type SwappableDeps = Pick<RunnerDeps, SwappableDepSeat>; /** Whether a key names a swappable seat. */ export declare function isSwappableDepSeat(k: unknown): k is SwappableDepSeat; /** * Which notice a seat's successful swap is announced under — the disposition table over the set (a seat * with no row does not compile: {@link SwapNoticeTableCoversEverySeat}). `models` and `tiers` share the * catalog-generation line (one swap, one line, both counts); each containment face has its own. */ export declare const SWAP_SEAT_NOTICE: { readonly models: "config.models_swapped"; readonly tiers: "config.models_swapped"; readonly readFace: "config.read_face_swapped"; readonly writeFace: "config.write_face_swapped"; readonly contextSettings: "config.context_settings_swapped"; readonly readDenyPatterns: "config.read_deny_swapped"; readonly readDenyBuiltinTiers: "config.read_deny_swapped"; readonly readDenyBuiltinExclude: "config.read_deny_swapped"; readonly readDenyFloorRequired: "config.read_deny_swapped"; readonly readDenyBuiltinRequestExcludable: "config.read_deny_swapped"; readonly memoryConsolidationDriver: "config.memory_consolidation_driver_swapped"; }; /** The fence over the notice table: `never` while every seat has a row. */ export type SwapNoticeTableCoversEverySeat = AssertAllKeysHandled<Exclude<SwappableDepSeat, keyof typeof SWAP_SEAT_NOTICE>>; type Catalog = Readonly<Record<string, Model>>; export declare function noteCatalogSwap(): void; /** The seat the Runner takes from a host's function, `liveCatalog` reading the catalog of the Runner it is handed to; a seat * already taken, and anything that is not a function, comes back as it is. */ export declare function takeSeat(seat: TaskSpec["getApiKeyAndHeaders"], liveCatalog: () => Catalog | undefined): TaskSpec["getApiKeyAndHeaders"]; /** A snapshot the engine screened (never a caller's own object) with its per-model auth seat taken ({@link takeSeat}); the snapshot itself * when it has none to take. A caller's own object is never wrapped or copied to carry a taken seat: the entry that receives one * (`sideQuery`, `startWorkflow`) keeps the taken seat beside the object, and the seat is asked on the caller's object (`askPerModelAuth`). */ export declare function withSeatTaken<S extends object>(spec: S, liveCatalog: () => Catalog | undefined): S; /** A leg of a taken seat prepares under `catalog` — a catalog its model is resolved from: its own prepare's, or the one an engine spawn * resolved the child's model from ahead of that prepare (the delegation gate, the observer sidecar, the workflow mount's model seat). * The seat's lookup reads it as well (a child on another Runner, a leg whose entry is in neither the birth catalog nor the live one). */ export declare function seatLegPrepares(seat: unknown, catalog: Catalog | undefined): void; /** What the screen hands back: the seats present (set order) and the ONE read of each present seat's value, * copied at screen time — the door writes from THIS object and never re-reads the caller's. */ export interface ScreenedSwappableDeps { readonly seats: readonly SwappableDepSeat[]; /** A fresh plain object: the screened value of every present seat (`models`/`tiers` shallow-copied, * `readFace`/`writeFace` the screened word, `memoryConsolidationDriver` its frozen snapshot; an explicit `undefined` is kept as a * PRESENT own key so the door can tell "clear this seat" from "not named"). */ readonly candidate: SwappableDeps; } /** * Screen a swap candidate BEFORE anything is written: the object shape, the key set (every own key must * be a member of {@link SWAPPABLE_DEP_SEATS}; at least one), the `tiers`-rides-with-`models` rule, and * each present seat's own value gate. Returns the seats present (set order) and a SNAPSHOT of their values * — the one read. Throws on the first defect; the caller has written nothing yet, so the current deps stay * in force byte-identical. * * ONE READ, BY CONSTRUCTION: every seat value is read off the caller's object exactly once, here, and the * screened copy is what the door writes and announces. A getter-backed or concurrently-mutated candidate * therefore cannot present a legal value to the screen and a different one to the write (or to the * announcement, which runs AFTER the write — a throw there would have left a half-announced swap in * force). The prepare doors read their deps seats the same way. * * The per-seat gate is the SAME one the seat's prepare-time reader applies (both face seats ⇒ * `assertContainmentFaceValue`), so a value the door admits is a value every later prepare admits. * * A JUDGMENT, NOTHING ELSE: this export takes no key in and tells nothing (the consolidation seat's key is taken in on the swap's own * path, {@link receiveSwappableDeps}). Two of the door's questions are about the Runner rather than the candidate and are asked only in * `Runner.swapDeps`: the arming judgment (did the swap leave an armed deployment no consolidation to start) and the cross-seat * context-settings words — a candidate this screen admits can still be refused there. It holds no Runner's plain-http admission list, so * an endpoint seat's plain-http root is judged as with no list (only this machine): a root it refuses `Runner.swapDeps` may admit. */ export declare function screenSwappableDeps(next: unknown): ScreenedSwappableDeps; /** * The swap's own reading of a candidate (`Runner.swapDeps`): {@link screenSwappableDeps}, with the consolidation seat's key taken in * between its snapshot and its screen (step two of the seat's door; `onNotice` hears a key that is no credential). A key read here stays * held whatever a later step decides. An endpoint seat's root is judged over `plaintextOrigins`, the Runner's list (the start's). */ export declare function receiveSwappableDeps(next: unknown, onNotice: RunnerDeps["onNotice"], plaintextOrigins: PlaintextOrigins | undefined): ScreenedSwappableDeps; /** * The identity of a model-catalog generation as the swap announces it: a digest of the catalog's KEY MATERIAL — the model names the * candidate declares and the tier bindings over them (each tier word, and the catalog word it binds; a binding that is a `Model` object * has no word, so it stands as `null`) — never the catalog's content. Names and tier words are sorted, so the digest does not depend on * the order a driver spelled them in; it depends on nothing but those two tables, so every Runner a driver swaps to the SAME model names * under the SAME tier bindings announces the SAME generation (each notice still mints its own `occurrenceId`), and a swap that ends on * another model name or another tier binding announces another. Two generations with equal key material read as one; the digest names what a consumer can tell apart * from the line, which is the keys — the line is an operator breadcrumb, not a content fingerprint. */ export declare function catalogGenerationOf(models: Readonly<Record<string, unknown>>, tiers: Readonly<Record<string, unknown>> | undefined): string; /** * Announce a swap of the deployment's read-deny declaration, after the write: ONE `config.read_deny_swapped` * line however many of its three seats the swap named (the line's contract lives at the notice catalog, * `EngineNotice.code`). */ export declare function announceReadDenySwap(onNotice: RunnerDeps["onNotice"], seats: readonly SwappableDepSeat[], inForce: Pick<RunnerDeps, ReadDenyDeploymentSeat>): void; /** * Announce a swap of the memory consolidation seat, after the write: ONE `config.memory_consolidation_driver_swapped` line, key * material only — the seat's shape (`endpoint` | `function` | `roles` — no chat, the role chain decides at the run's start — | * `cleared`) and the model id the seat names (`null` when it names none), never the endpoint, the key or the knobs; a model id that * carries the seat's own key (a candidate whose two strings are one) is told with the key taken out. `written` is the deps this swap * wrote (never the Runner's live seat: a swap the host makes from a notice seat of this one announces itself). */ export declare function announceConsolidationDriverSwap(onNotice: RunnerDeps["onNotice"], seats: readonly SwappableDepSeat[], written: Pick<RunnerDeps, "memoryConsolidationDriver">): void; export {};