@sema-agent/core
Version:
Stateless, task-oriented AI agent core
137 lines (136 loc) • 9.19 kB
TypeScript
import { type EngineText, type EngineTextId } from "../prompt-assembly/engine-text.js";
import type { AskEffective } from "./wiring-manifest.js";
import type { PermissionMode } from "./ask-origin.js";
import { type EngineRegionMessage } from "./reminder-mint.js";
import { type ToolRoster } from "./tool-roster.js";
/** A posture message: one of the permission posture kind's catalog messages. */
export type PostureMessageId = Extract<EngineTextId, `core/event.permission-posture.${string}`>;
/** The mode facts of one leg, both the ENGINE's own resolution — never a requested value, never spec shape. */
export interface ApprovalPostureFacts {
/** The leg's EFFECTIVE permission mode (a requested mode the deployment refused has already folded back). */
mode: PermissionMode;
/** The seat's own ask derivation (`askSeatEffective`, no mode) — `auto_allow` means no person answers an ask on this leg.
* The LIVE `mode` is read before it: the manifest's reported word is the leg's start under its start mode, and a steer moves
* the mode after it was minted. */
askEffective: AskEffective;
}
/**
* The shape facts of one leg the planning rows word themselves by — read off what the leg IS (its minted tool roster, its
* minted delegation identity), never off the requested spec.
*/
export interface PostureLegFacts {
/** The plan-exit tool ({@link PRESENT_PLAN_TOOL_NAME}) is on this leg's roster — its wire name or an alias a call resolves. */
exitToolMounted: boolean;
/** This leg runs as a delegated child (every core spawn lane, a fork included). */
isChildLeg: boolean;
/** The session plan file the run admitted (`TaskSpec.planFile`), as the seat tells it to the model (`planFileShown`) —
* absent ⇒ the plan is the plan-exit tool's argument and no frame names a file. */
planFile?: string;
/** The Bash tool and an Edit or Write tool are on this leg's roster (a wire name or an alias a call resolves) — the tools the
* bypass row's paragraph tells the model to work through and away from. The reference implementation's `bashFirst`: without
* both, it states no bypass posture at all. */
bashFirstToolsMounted: boolean;
}
/** {@link PostureLegFacts.bashFirstToolsMounted} read off a minted roster: the shell, and at least one of the edit tools. */
export declare function bashFirstToolsMountedOn(roster: ToolRoster): boolean;
/** Every fact a row's `owedBy` reads: the mode facts and the leg's shape. */
export type PostureFacts = ApprovalPostureFacts & PostureLegFacts;
/** A posture family: one family stands in a context at a time per family, and its exit retires whichever of its rows stands. */
export type PostureFamily = "plan" | "auto";
/** One row of {@link POSTURE_TABLE}. */
export interface PostureRow {
readonly family: PostureFamily;
/** Whether the leg's mode facts call for this row. */
readonly owedBy: (facts: PostureFacts) => boolean;
/** What the frame's words need on the leg to be followed (absent ⇒ nothing). A row the mode facts call for whose need fails is
* not stated — and, the leg's posture being unchanged, nothing standing of its family is exited: the reference states no bypass
* attachment without `bashFirst`, and says its exit only when the mode leaves. */
readonly needs?: (facts: PostureFacts) => boolean;
/** The message that states the posture. */
readonly frame: PostureMessageId;
/** The message that retires it. */
readonly exit: PostureMessageId;
/**
* A restatement while the row stays owed and standing: due once `everyTurns` assistant turns have passed since
* the family's last frame; the restatement is the full frame when it is the family's `1 + k·fullEvery`-th frame
* since its last exit, else `sparse`. Absent ⇒ stated once and never restated while it stands.
*/
readonly cadence?: {
readonly everyTurns: number;
readonly fullEvery: number;
readonly sparse: PostureMessageId;
};
}
/** A posture message as `say` words it for this leg: the plan-exit tool's wire name, and the leg's plan file where the
* message names one. */
export declare function postureText(id: PostureMessageId, facts: Pick<PostureFacts, "planFile">, say: EngineText): string;
/**
* Every posture the engine states, in the order the owed row is chosen. `plan` first: it is the mode the leg
* was put in, and the bypass frame's shell-first instruction would contradict it. Within it the leg's shape picks the
* wording — a delegated child (the reference's sub-agent variant, which has no sparse arm: its restatement is its own
* body), else a leg with the plan-exit tool on its roster and a session plan file (the body that names both), else a leg
* with the plan-exit tool (the planning body that names it), else the body that names no tool. Then the reference
* implementation's own arm order for its one posture attachment: bypass (effective `bypassPermissions`, or — outside
* `dontAsk`, whose mode refuses every ask before any seat could answer — no person answers an ask; and only on a leg whose
* roster carries the tools its paragraph names, {@link PostureLegFacts.bashFirstToolsMounted}, as the reference owes it),
* then auto (the classifier is armed — a requested auto mode the engine folded back is not stated, because the run is not
* operating under it).
*/
export declare const POSTURE_TABLE: readonly PostureRow[];
/** The row the leg's facts call for and its words can be followed on, or undefined (no posture is owed). */
export declare function owedPostureRow(facts: PostureFacts): PostureRow | undefined;
/** What one family's standing frame is, as the fold over the active context left it. */
export interface StandingPosture {
readonly row: PostureRow;
/** The family's frames since its last exit (the cadence's full/sparse count). */
readonly statements: number;
/** Assistant turns since the family's last frame (the cadence's due clock). */
readonly turnsSince: number;
/** The plan file the standing frame named, as rendered (a frame that names one only): a leg whose plan file reads
* otherwise is owed the full frame again — the frame's row is the same, what it states is not. */
readonly file?: string;
}
/** The standing families of a context, keyed by family, in the order they first stood. */
export type PostureStanding = ReadonlyMap<PostureFamily, StandingPosture>;
/** A vocabulary text: a fixed text, or — a message that names the plan file — the rendering around the file. */
export type VocabularyText = string | {
readonly head: string;
readonly tail: string;
};
/** The fold's vocabulary, read-only: each entry's text, its row, and whether it is the row's exit — what the matching
* contract (one text, one meaning; no entry a prefix of another at a frame boundary) is held against. */
export declare function postureVocabulary(): ReadonlyArray<{
readonly text: VocabularyText;
readonly row: PostureRow;
readonly exit: boolean;
}>;
/**
* The ordered fold over the ACTIVE context's messages (the compaction boundary applied — never the raw ancestry,
* which still carries what a compaction replaced): the engine's marked reminder bodies, read off ENGINE REGIONS
* only (metadata, never text shape, so a caller's spliced copy — marked or not — cannot answer), per minted block;
* an assistant message advances every standing family's turn clock.
*/
export declare function postureStandingOf(messages: readonly EngineRegionMessage[], reminderMark: string): PostureStanding;
/**
* The ONE composition: the frames that make the active context state exactly the posture the leg's facts call for
* — the exit of every standing family the owed row does not belong to (but not of the row the mode facts still call for), then the owed row's frame (or its cadence's
* restatement) when it is owed — worded by `say`, the run's reading. Empty when the context already says it.
*/
export declare function composePostureFrames(facts: PostureFacts, standing: PostureStanding, say: EngineText): string[];
/**
* The run-local half of the posture: the newest posture carrier's text (the context lane's protection slot —
* a clipped request keeps or reinserts the message carrying it; the carrier of an exit takes the slot, so a
* retired frame is no longer protected); the owed row the last composition answered for (a boundary whose facts
* still call for that row, with no cadence to keep, asks nothing of the context — absent means the last
* composition read nothing or its carrier was not written, so the next boundary asks again); the frames a boundary
* could not write (stated on each request's view, never persisted, until a boundary writes them); and where a
* write failure is disclosed.
*/
export interface PostureFrameRef {
protectedText?: string;
composedFor?: PostureRow | null;
unwritten?: string;
report?: (err: Error) => void;
}
/** SHA-256 of a frame body — the pins' lock port, so the lock is computed the one way. */
export declare function directiveDigest(body: string): string;