@sema-agent/core
Version:
Stateless, task-oriented AI agent core
128 lines (127 loc) • 7.93 kB
TypeScript
import type { PrefixChangeDeclaration, ToolFingerprintInput } from "../core/prefix-declaration.js";
/** Every prefix-shaping input a request carries — the CLASSIFICATION DOMAIN. Adding a field here
* without classifying it in {@link PREFIX_INPUT_CLASSIFICATION} is a compile error. */
export interface RawPrefixInputs {
/** Provider namespace (cache namespaces never span providers). */
provider: string;
/** Model id (a switch is a new cache namespace). */
modelId: string;
/** The run's prompt identity (`promptIdentityOf`: catalog, renderers, overlay layers, binding — the binding element
* digests the selector values, the prompt traits among them) — the manifest's `identity.artifact`. */
artifactDigest: string;
/** Physical block layout as SERVED (derived from the composed system blocks; single-block default). */
layoutGroups: ReadonlyArray<{
groupId: string;
cacheControlBoundary: boolean;
}>;
/** The composed stable system text (digested — text never enters the snapshot object) by the
* `promptHasher` {@link buildTurnPromptSnapshot} is handed: the run loop hands one keyed with the
* session's hash salt (a fixed public prefix + a short guessable suffix — an env fact, a memory
* line — would make an unsalted full-text sha enumerable), so the digest compares across the
* session's runs and processes while the salt is stored (a leg-only salt, the store could not keep
* it, compares within the leg), never across unrelated sessions; with no hasher, the content sha. */
stableSystemText: string;
/** Wire-face tool projections (name/description/schema — the mounted set, post-defer). */
toolWire: ReadonlyArray<ToolFingerprintInput>;
/** Request-shaping controls that alter the wire body. `thinkingLevel: null` = no level declared (the
* request carries no thinking key — provider default), which is NOT the same request as an explicit
* `"off"` (the disable key) and so must not share its identity. */
thinkingLevel: string | null;
maxTokens: number | undefined;
hasOutputSchema: boolean;
/** The deployment's prompt-cache retention word every call of the leg carries (absent ⇒ none written). */
cacheRetention?: string;
/** Routing/audit fields — provably NOT part of the cacheable prefix. */
sessionId: string;
taskId: string | undefined;
/** Per-call variables — never reusable, never identity. */
turn: number;
cacheRead: number;
}
export type PrefixInputClass = "cache-identity" | "volatile-no-reuse" | "proven-irrelevant";
/** The EXHAUSTIVE classification (§10.2). The `satisfies` bound is the CI gate: a new
* {@link RawPrefixInputs} field fails typecheck until classified here; the runtime pin
* (`test/turn-snapshot.test.ts`) asserts totality survives refactors. Reasons ride inline. */
export declare const PREFIX_INPUT_CLASSIFICATION: {
readonly provider: "cache-identity";
readonly modelId: "cache-identity";
readonly artifactDigest: "cache-identity";
readonly layoutGroups: "cache-identity";
readonly stableSystemText: "cache-identity";
readonly toolWire: "cache-identity";
readonly thinkingLevel: "cache-identity";
readonly maxTokens: "cache-identity";
readonly hasOutputSchema: "cache-identity";
readonly cacheRetention: "cache-identity";
readonly sessionId: "proven-irrelevant";
readonly taskId: "proven-irrelevant";
readonly turn: "volatile-no-reuse";
readonly cacheRead: "volatile-no-reuse";
};
/** The lowering face (§10.1): version + wire form + that form's KNOWN intentional divergences. */
export interface LoweringRecord {
version: string;
form: "openai-merged" | "anthropic-blocks" | "other";
intentionalDivergences: readonly string[];
}
export declare const LOWERING_VERSION = "sema-lowering@1";
export declare function loweringRecordFor(api: string): LoweringRecord;
/** The eight identity elements, individually digested (observable without shipping prompt text). */
export interface CacheIdentityElements {
provider: string;
modelId: string;
artifactDigest: string;
layoutDigest: string;
stableSystemDigest: string;
toolWireDigest: string;
requestPolicyDigest: string;
loweringVersion: string;
}
/** Which writer moved the tool table: a deferred activation, a roster refresh, or the per-request view. */
export type ToolTableSource = "activation" | "roster" | "view";
/** The leg's prefix identity AND its declaration seat: each in-leg change to the head is declared once, by the seat that
* makes it, through the refresher of the element it moves (the word fixed by the refresher) or {@link TurnPromptSnapshot.declare}. */
export interface TurnPromptSnapshot {
elements: CacheIdentityElements;
/** sha256(canonical(elements)) — the ONE eight-element identity value. */
cacheIdentity: string;
lowering: LoweringRecord;
/** invoked with every declaration (an identity move — after the refresh applied, so the snapshot never reads a mixed
* state — or {@link TurnPromptSnapshot.declare}); the session's prefix ledger reads it. */
onDeclared?: (declaration: PrefixChangeDeclaration) => void;
/** The tool table's wire digest as MOUNTED (moved by `activation` / `roster`, not the per-request `view`): the anchor
* compares legs in it, since prepare sees the table before any request's view re-applies the same hiding. */
readonly mountedToolWireDigest: string;
/** Refresh the tool half — declares `tool_table` when the tools block moved. */
refreshTools(toolWire: RawPrefixInputs["toolWire"], source: ToolTableSource): void;
/** Refresh the stable-system half (a compaction-boundary center change — pass the COMMITTED identity so the
* identity never mixes old-artifact/new-prompt) — declares `compaction` when it moved. */
refreshStableSystem(stableSystemText: string, artifactDigest?: string): void;
/** The request-policy element follows the controls the main lane's calls carry — declares `request_policy`. */
refreshRequestPolicy(p: {
thinkingLevel?: string | null;
maxTokens?: number | null;
cacheRetention?: string | null;
fastSpeed?: boolean;
}): void;
/** The namespace follows what served the answer — declares `model_namespace`. */
refreshModel(m: {
provider: string;
modelId: string;
}): void;
/** Declare a change no identity element carries (a history rewrite, a landed compaction's transcript). */
declare(declaration: PrefixChangeDeclaration): void;
}
/** Build the run's snapshot from classified raw inputs. Volatile/irrelevant fields are accepted so
* the CLASSIFICATION stays total over everything callers hold, but only identity-class fields can
* reach the digest — enforced structurally (this function reads exactly the identity fields).
*
* `promptHasher`: the digest applied to COMPOSED PROMPT TEXT — callers inject a
* KEYED/salted hasher (the run loop uses the session's hash salt): a fixed public prefix +
* short unknown suffix (an env fact, a memory note, a tenant string) makes an unsalted full-text
* sha ENUMERABLE, recreating exactly the disclosure the salted block hashes prevent. Consequence,
* documented: `stableSystemDigest` (and thus `cacheIdentity`) compares within one session (across its
* runs and engine processes) and between a fork and its source when the fork's active branch holds the source's salt
* entry, never across unrelated sessions; with no `promptHasher` it is the unsalted content sha (comparable anywhere);
* the CODE-class digests (artifact/toolWire) are unsalted. */
export declare function buildTurnPromptSnapshot(raw: Omit<RawPrefixInputs, "turn" | "cacheRead">, api: string, promptHasher?: (text: string) => string): TurnPromptSnapshot;