UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

121 lines (120 loc) • 10.1 kB
/** * The layers of an inherited constraint chain, minted in ONE place for both of their sources: a leg handing its chain down * (birth — `inheritedGateForChildren`, prepare-inherited-gate.ts) and a resume rebuilding a parked leg's chain on a runner that * does not hold it ({@link revivalParentConstraints}). Each layer is stamped with its kind (`installation` / `taskPolicy` / * `deploymentPolicy`, {@link ConstraintLayerKind}) where it is minted; the kind rides the chain verbatim, a park records it on the * layer's chain entry and the entry's digest binds it. * * WHERE A RESUMED LEG'S CHAIN COMES FROM — one function, {@link revivalParentConstraints}: a chain the caller handed live (a * trusted caller in this process — the workflow continuation hands its host leg's, a drive from a leg's context that leg's), else the * live chain this runner registered for the token when the leg parked on it (its spawn's chain, or the one an earlier revival on this * runner ran under), else the chain REBUILT layer by layer from the row's record and this runner's own inputs. A handed chain is taken * as handed (it is its giver's chain now). * THE ROOT TASK'S OWN LAYER (a `taskPolicy` layer stamped `rootLeg`) is the root task's policy the host hands for this revival * (`parkedResume.rootPolicy` → `RunInternals.rootPolicy`) on the live chain as on a rebuilt one — one reading of the root task's * policy, whichever runner the revival lands on; the live chain keeps that layer's regime (its approver, its mandates, its * classifier) and every other layer as registered. A live chain resumed with no root policy handed keeps the root layer as * registered (at the agent's last park on this runner), and says so (`permission.revival_root_policy_unrestated`; a rebuild without * one is refused, below). The rebuild: * · `installation` = `admitModChain(RunnerDeps.mods, the seat's mods)` — the seat's mods as they land on the revived leg's spec; * · `deploymentPolicy` = this runner's `RunnerDeps.toolPolicy`; * · `taskPolicy` = the root task's own policy the host handed (`parkedResume.rootPolicy` → `RunInternals.rootPolicy`), for a * layer the row records as the root leg's (`rootLeg`, #5125) only — a delegated leg's own task policy (a workflow agent's * governed spec, a child's own `TaskSpec.toolPolicy`) is named by no input, and refused, wherever it stands in the chain; * · each layer's durable / content mandates and its classifier posture copied from its recorded entry; * · the approver of every rebuilt layer is the revived leg's own resolved approval seat (the one the host handed for this leg, or * this runner's default) — none ⇒ the layer has none, and its asks are refused as an inherited ask with nobody to answer is; * a layer that recorded a durable mandate parks its asks without consulting it (the mandate precedes the approver); * · a layer that recorded an armed classifier is judged by the revived leg's own classifier while the ancestor runs `auto` as the * resume hands its mode now (the digest reads the recorded bit, never the mode handed) — its recorded recipe rides the layer for * the digest; a layer that recorded none is never armed; * · a rebuilt installation carries no hook environment (`hookEnv` is absent: its handlers see no `e.env`). * A layer one of these inputs is missing for is refused before the claim consumes anything (`resume.parent_constraint_unrebuildable`, * naming each layer and what it lacks); the rebuilt chain is then judged by the checkpoint screen exactly as a handed one is (its * count and its digest). */ import { type LegModBus } from "../mods/stations.js"; import { type Checkpoint, type CheckpointToken } from "../checkpoint-store.js"; import type { ToolPolicy } from "../tool-policy.js"; import type { AutoModeDecider } from "../auto-mode.js"; import type { EngineNotice, PermissionMode, RunnerDeps, TaskSpec } from "../types.js"; import type { InheritedGate } from "./contracts.js"; type Chain = NonNullable<InheritedGate["parentConstraints"]>; type Layer = Chain[number]; /** The frozen context a layer's questions are settled under: its approver, its two durable mandates, its classifier. */ export type LayerRegime = Pick<Layer, "onAsk" | "durableMandate" | "contentMandate" | "autoMode">; /** * One leg's own contribution to the chain it hands down: its screening installation (when it has one), then its caller policy * (when it resolved one) — the order its own gate judges in (screen first, policy second) — each under its regime and stamped * with its kind, and with `rootLeg` when the leg is the chain's root leg (#5125). The ONE place a chain layer is assembled: a leg's * birth hand-down, and a revival's rebuild of each recorded layer. */ export declare function legConstraintLayers(leg: { readonly installation?: { readonly bus: LegModBus; readonly hookEnv?: unknown; readonly regime: LayerRegime; } | undefined; readonly policy?: { readonly policy: ToolPolicy; readonly kind: "taskPolicy" | "deploymentPolicy"; readonly regime: LayerRegime; } | undefined; /** The leg is the one the chain starts at ({@link chainRootLeg}); a rebuilt layer passes its recorded mark. */ readonly rootLeg: boolean; }): Layer[]; /** * Whether a leg is the chain's ROOT leg — the one leg whose own task policy a host can hand back (`parkedResume.rootPolicy`): it * inherited no chain layer (the live chain it was handed, or the one its resume rebuilt — both arrive as the gate's * `parentConstraints`, so the answer is the same before a park and after it) and it is not a delegated child (the live fact, else the * one its row recorded). A cascade rung is one, and so is a leg a trusted caller handed only a floor or a mode; every leg a delegation * tool or a workflow started is not, nor is a revived child, whatever its resume was handed. */ export declare function chainRootLeg(inheritedChain: readonly unknown[] | undefined, isDelegatedChild: boolean): boolean; /** * The classifier a REBUILT layer carries when its entry recorded one: a stand-in the revived leg binds to its own classifier * ({@link bindRevivedLegClassifier}) — the ancestor's decider is a live object no row holds. Never dispatched itself. */ export declare const REVIVED_LEG_CLASSIFIER: AutoModeDecider; /** A chain layer as the revived leg holds it: a rebuilt layer's classifier stand-in bound to the leg's own classifier (`own`, read * at each call; none ⇒ no verdict, and the ask goes to the layer's approver), speaking only while `mode` — the ancestor's mode as the * resume hands it now — reads `auto`; every other layer as it is. Armed = the row recorded it armed ∧ the ancestor runs `auto` now ∧ * this leg holds a classifier: never armed where the row recorded none, never left speaking where the ancestor left `auto`. */ export declare function bindRevivedLegClassifier(layer: Layer, own: () => AutoModeDecider | undefined, mode: () => PermissionMode): Layer; /** What {@link revivalParentConstraints} reads. */ export interface RevivalChainInput { /** The token being resumed. */ readonly token: CheckpointToken; /** The row's recorded chain posture (`state.inheritedGate`). */ readonly record: Checkpoint["state"]["inheritedGate"]; /** A chain the caller handed live (`RunInternals.inheritedGate.parentConstraints`), when it handed one. */ readonly handed: InheritedGate["parentConstraints"] | undefined; /** This runner's same-process registry of the live chains of the legs it spawned, by token. */ readonly registry: ReadonlyMap<CheckpointToken, Chain>; /** This runner's deployment inputs. */ readonly deps: Pick<RunnerDeps, "mods" | "toolPolicy" | "onAsk">; /** The revived leg's spec as the resume judged it: the seat's mods it carries, and its own approval seat. */ readonly leg: Pick<TaskSpec, "mods" | "onAsk" | "approvalReach">; /** The root task's own caller policy the host handed (`RunInternals.rootPolicy`). */ readonly rootPolicy: ToolPolicy | undefined; /** Told when the live chain is taken with its root task layer as registered (no root policy handed for a chain that has one). * The callback only records a flag; the resume ladder mints the notice ({@link revivalRootPolicyUnrestatedNotice}) and delivers it * when the resume's claim is won (`runtask.ts`). */ readonly onRootLayerUnrestated: () => void; } /** * THE chain a resumed leg runs under (see the module header): handed live ⇒ that; registered live ⇒ that, its root task layer the * root policy handed now; recorded ⇒ rebuilt. `undefined` when the row requires no parent constraint and nothing was handed or * registered. Throws pre-claim: * `resume.parent_constraint_unrebuildable` (a layer whose input this resume or this runner lacks), or `reading_changed` (a chain an * earlier release recorded without kinds that this runner cannot read, below). Only a rebuild is asynchronous (its mods admission): * a handed or a live chain is returned on the calling tick, so the resume's next rung starts in the same continuation. */ export declare function revivalParentConstraints(input: RevivalChainInput): InheritedGate["parentConstraints"] | undefined | Promise<Chain>; /** The account of a resume on the runner that holds the parked agent's chain live, handed no root task policy for a chain that * carries the root task's own layer (`permission.revival_root_policy_unrestated`): the resume takes that layer as this runner * registered it when the agent last parked here (its spawn, or an earlier revival here) — on another runner the same resume is * refused for it (`resume.parent_constraint_unrebuildable`, field `rootPolicy`). The choice only records a flag; the resume ladder * mints this notice and delivers it once the claim is won (`runtask.ts`). */ export declare function revivalRootPolicyUnrestatedNotice(sessionId: string): EngineNotice; export {};