UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

174 lines (173 loc) • 14.3 kB
/** * The engine's ATTESTATION record — the one carrier of what an engine station knows about a decision and a policy cannot * state: WHO settled the call's wait and WHICH question that answered (the settlement, the ask's origin word and class set, * the digest of the arguments the answer licenses), and the facts the classifier stations, the in-fold unresolvable arms, * the integrity guards and the policy fold record. It rides the decision as its `attestation` member and is read through * {@link attestationOf} alone; the decision object's identity carries nothing. * * Minted only by {@link mintAttestation} and combined only by {@link mergeAttestation}: both freeze the record and enter it * in a module-private brand set, so a value a caller composed — a literal, a copy, a structured clone — is not a record, and * the decision receipt refuses a decision carrying one. A record is bound to the call it was minted for (`toolCallId` + * `toolName`): handed back for another call it reads as absent. A spread of a decision keeps its record, and the record still * says only what the engine minted — a refusal record on an allow is a contradiction the gate's exit refuses, and a * resolution's answer binds the arguments it licensed (`inputDigest`). * * A leaf (type-only imports): the decision module that receives a record and the class derivation that reads one for its call * both depend on it, and neither depends on the other through it. */ import type { AskClass, AskClassMembers, AskOrigin, AskReason, UnansweredMint } from "./ask-origin.js"; import type { ClassifierDenyCause, DecidedVia, Settlement } from "./gate-outcome.js"; import type { ShellReadRefusalDetails } from "../tools/fs/shell-read-check.js"; import type { AutoModeClassifierRound, ClassifierSaid } from "./auto-mode.js"; import type { AskUnresolvableCause } from "./ask-unresolvable-notice.js"; import type { PathDenialEvidence } from "../tools/fs/safety.js"; import type { AskGuard } from "./tool-policy.js"; import type { RefusalEcho } from "./refusal-echo.js"; /** The engine's own account of a sentence it composed for a decision's `message`: the sentence (`text`); whether it is the * engine's generic question for the call, naming no asker's reason (`generic` — a write gate's default question, the plan * mode's); the integrity guard whose question the sentence is (`guard` — the removal guard's, the session-transcript guard's; * always one of the record's own {@link EngineAttestation.guards}, which answer a different question: every guard standing * behind the ask, a read station's silence included); and the literals it quotes, in the order it quotes them (`echoed`, the * refusal list's own form). The station that composed the sentence is its only writer, and freezes each listed literal itself. * Read through {@link engineSentenceOf} alone, which answers only while the decision's `message` IS `text`. */ export interface EngineSentence { readonly text: string; readonly generic?: true; readonly guard?: AskGuard; readonly echoed?: readonly RefusalEcho[]; } export interface EngineAttestation { readonly toolCallId: string; readonly toolName: string; /** What ended the call's wait. The answer group — `settlement`, `decidedVia`, `origin`, `classes`, `inputDigest` — travels as one. */ readonly settlement?: Settlement; /** Which machine seat refused for the deployment's policy ({@link DecidedVia}), as the screen read it — beside a `policy_refused` * settlement only, and only when the answer named one. Written by the ask resolver; read by the denial-limit stop's stations. */ readonly decidedVia?: DecidedVia; /** The settled ask's origin word. */ readonly origin?: AskOrigin; /** The settled ask's class set (`ASK_CLASSES` order), when the minting station held one. */ readonly classes?: readonly AskClass[]; /** `exactBoundInputHashOf` of the call's tool with the arguments the settlement licenses (an approver's edit, the snapshot shown, or the asked * args); absent when they have none (outside the bindable domain) — such a record licenses no bytes. */ readonly inputDigest?: string; /** The FORM of a classifier round that did not rule (a classifier deny's cause). */ readonly classifierCause?: ClassifierDenyCause; /** WHICH model answered a classification, on either arm. */ readonly classifierRound?: AutoModeClassifierRound; /** The question a classifier's ALLOW answered — the ask's class word and set — beside the model that allowed it (one group with * {@link classifierRound}: a later record replaces both). Absent on every other arm. The allowed disposition's `asked`. */ readonly classifierAsked?: AskClassMembers; /** What a classifier's RULING block said about itself and the record can carry ({@link ClassifierSaid}): the folded `reason` (untrusted * text, one line, bounded) and the `category` as a word of the closed set; a member the classifier left out is absent. Written by the * two judging stations beside the deny sentence, whose reason tail is the same folded string; read once at the gate's exit onto the * disposition. Absent on every non-ruling form (unavailable, no verdict) and on a block that said nothing the record can carry. */ readonly classifierBlock?: ClassifierSaid; /** Why an in-fold deny refused an ask nobody could resolve. */ readonly unresolvableCause?: AskUnresolvableCause; /** The classes of the OTHER asks a policy fold collapsed into this one (each contributor's classes, unioned) — so a fact the * fold does not copy (a guard, an unreadable-rule fact, a bare real-approval bit) is not lost behind the ask that won the * slot. Written by the policy fold and by the gate's own writers of a standing ask's carried classes; the one carrier of * those classes. */ readonly foldedClasses?: readonly AskClass[]; /** WHICH integrity guards stand behind this ask — a removal of a protected location, an unverifiable recursive delete, shell * text the engine cannot read (or an environment that did not answer the read station), the session-transcript write, * another person's shared artifact. Engine findings (the guard policies, the read station's silence, the artifact tool's * consent): the class derivation reads them as classes and as explainers of the real-approval bit, so only a record carries * them. Never copied onto the approval request or the park row. */ readonly guards?: readonly AskGuard[]; /** Where a path deny row refused the call — the row, the view of the target it met, the face the call is on. The gate's * exit mints the refusal's card from it; the judge that found the row is its only writer. */ readonly pathDenial?: PathDenialEvidence; /** A seat refused the call's arguments because no approval can be bound to them: the refusal's sentence, carried by the * refusal itself — the gate's deny exit reads it (the remedy it gives) instead of judging again. */ readonly bindingRefusal?: string; /** The ask resolver refused an ask nobody could answer with its one sentence for that ({@link UnansweredMint}): the cause, the * question it quoted and the sentence. Written only by the resolver arms that mint it; read once, by the gate's deny exit, which * re-mints the sentence over the same question in the form the park state allows. */ readonly unanswered?: UnansweredMint; /** The engine's own account of the decision's `message` ({@link EngineSentence}): the sentence a station composed, whether it * is the engine's generic question (the chain's entry mint, {@link import("./ask-origin.js").chainAskReasonsOf}, writes no * entry for one), and the literals it quotes. Valid only while the decision's `message` is still that sentence — a later * layer that spread the decision and put its own words in `message` keeps them, and the account then reads as absent. * Written only by the station that composed the sentence, for that sentence (the write gate's and the plan mode's questions, * the two integrity guards' questions — the removal guard's and the session-transcript guard's —, and a longer text a station * composes around one of them, {@link withRestatedSentence}; the ask resolver's refusal that quotes one of them); a composer * cannot state it (an unbranded record is refused). */ readonly sentence?: EngineSentence; /** The askers a station read off this ask where it first received it, before it narrated the sentence (an inherited station * floating an ancestor's ask back into this leg's fold: its mark frames, its denial-limit fallback's sentence). The chain's * entry mint takes these instead of the sentence; empty = the station read no asker. Written only by that float. */ readonly receivedAskReasons?: readonly AskReason[]; /** A gate station's refusal settled AHEAD of the station's own seat in the gate — where an earlier layer would otherwise * put a question about the call first (an inherited station settling an ancestor's ask): the station's layer word and its * card. Written by one mint, the refusing station's ({@link STATION_REFUSAL_LAYERS} names the stations that settle so); read * twice, each field once — the layer by the settlement ledger's `fromDecision` (the refusal's `deniedBy`), the card by the * gate's deny exit beside a path denial's card. */ readonly stationRefusal?: StationRefusal; } /** The stations whose refusal an earlier layer settles in their name ({@link EngineAttestation.stationRefusal}) — each word is * the station's own refusing-layer word. */ export declare const STATION_REFUSAL_LAYERS: readonly ["read_boundary"]; export type StationRefusalLayer = (typeof STATION_REFUSAL_LAYERS)[number]; /** {@link EngineAttestation.stationRefusal}: the refusing station's layer word and the card its refusal carries. */ export interface StationRefusal { readonly layer: StationRefusalLayer; readonly details?: ShellReadRefusalDetails; } /** The members a station attests — everything but the call binding. */ export type AttestationMembers = Omit<EngineAttestation, "toolCallId" | "toolName">; /** Mint a record for `call`. Engine stations only: not re-exported from `src/index.ts`. The values are DETACHED snapshots, so a * caller that later mutates the settlement object it was handed cannot rewrite what the engine attests. */ export declare function mintAttestation(call: { toolCallId: string; toolName: string; }, members: AttestationMembers): EngineAttestation; /** Is `v` a record the engine minted (the brand alone — no binding)? The receipt's test. */ export declare function isEngineAttestation(v: unknown): v is EngineAttestation; /** THE read: the record `d` carries FOR `call` — a branded record bound to that call, else `undefined` (a record bound to * another call is a replayed one and reads as absent). Read off the own data member, never through an accessor. */ export declare function attestationOf(d: unknown, call: { toolCallId: string; toolName: string; }): EngineAttestation | undefined; /** THE reading of "the host's OWN approval window for this ask closed with no one answering it" off a decision's record: the ask * resolver's account of its `window_expired` refusal (`unanswered`), or the cause a station attested refusing an ancestor's card so * answered (`unresolvableCause`). Read by the gate's deny exit (the refusal's cause) and the denial-limit stop's stations (its clause). */ export declare const hostWindowUnanswered: (record: EngineAttestation | undefined) => boolean; /** THE read of the engine's account of `d`'s sentence for `call` ({@link EngineAttestation.sentence}): present only while `d`'s * `message` is that sentence, byte for byte — a layer that rewrote the message, appended to it or put its own words over it * carries the record along (a spread keeps it) but no longer carries its account. `message` is read as every reader of the * decision's text reads it. */ export declare function engineSentenceOf(d: unknown, call: { toolCallId: string; toolName: string; }): EngineSentence | undefined; /** `longer` — a text a station composed around `standing`'s message — with the engine's account of `standing`'s sentence restated * over it: both halves are the engine's words and the station that composed the longer text is the account's only writer for it, * so the account (`generic`, `guard`, `echoed`) rides the longer text; a standing message that is not the engine's sentence keeps * no account. The one restatement: the write-protection tighten's appended sentence, an inherited station's mark frames. */ export declare function withRestatedSentence<T extends { readonly message?: string; }>(longer: T, standing: unknown, call: { toolCallId: string; toolName: string; }): T; /** * Combine two records for `call`, member GROUP by group: the later record's group stands where it has one, the earlier's * fills a group the later lacks. A record not bound to `call` contributes nothing. `answer` scope is the veto's merge: the * earlier record contributes ONLY its answer, and only when the later carries none — a person's yes rides a later layer's * deny (invariant I3) while the refusing station's own facts stay its own. */ export declare function mergeAttestation(call: { toolCallId: string; toolName: string; }, earlier: EngineAttestation | undefined, later: EngineAttestation | undefined, scope?: "all" | "answer"): EngineAttestation | undefined; /** `d` with `members` attested for `call`, merged over the record it already carries for that call (the new members' groups * win). Always a fresh object — the receipt hands out frozen ones. */ export declare function attest<T extends object>(d: T, call: { toolCallId: string; toolName: string; }, members: AttestationMembers): T; /** `d` carrying `record` (a fresh object), or `d` itself when there is no record to put on it. */ export declare function withAttestation<T extends object>(d: T, record: EngineAttestation | undefined): T;