@sema-agent/core
Version:
Stateless, task-oriented AI agent core
734 lines • 54.7 kB
TypeScript
import type { CheckpointStore, CheckpointToken, ResumeOutcome } from "../core/checkpoint-store.js";
import { type WorkflowRunStore } from "../core/workflow-run-store.js";
import type { WorkflowJournalStore, WorkflowLaunchRecord } from "../core/workflow-journal-store.js";
import type { InheritedGate } from "../core/runner/contracts.js";
import type { ToolExecuteContext } from "../core/types.js";
import type { EngineText } from "../prompt-assembly/engine-text.js";
import { type WorkflowParkCardState, type WorkflowParkWait, type WorkflowRun } from "./workflow-types.js";
/** One decision on the trusted channel, as the continuation reads it: the run it names, the card's token, the host's own id
* for the decision (one per decision a person made), the outcome, and an optional re-supplied parent chain. */
export interface WorkflowParkDecision {
readonly runId: string;
readonly token: CheckpointToken;
readonly decisionId: string;
readonly outcome: ResumeOutcome;
readonly inheritedGate?: InheritedGate;
}
/** Why a decision did not continue its run — the closed set `workflow.parked_decision_unused` carries as `detail.reason`. */
export declare const WORKFLOW_PARK_UNUSED_REASONS: readonly ["workflow_not_mounted", "entry_unreadable", "record_unreadable", "not_holder", "not_pending", "decision_spent", "launch_unrecorded", "launch_unreadable", "host_changed", "denied", "ask_unanswerable", "call_changed", "launch_refused", "continuing_elsewhere", "card_unresumable", "waiting_elsewhere", "run_not_ended", "workflow_stopped"];
export type WorkflowParkUnusedReason = (typeof WORKFLOW_PARK_UNUSED_REASONS)[number];
/** What is left to do with a decision a reason answers (#5208): `deliverable` — the same decision stays usable, and a later hand-over of
* it is judged again; `decide_again` — only a person's new decision for the card continues the run (this one is spent, or — kept —
* the hand-over that carried it does not read, so handing the same entry over again answers the same);
* `start_over` — the run cannot be continued from this card, start the workflow again; `withdraw` — the card no longer waits, nothing
* is left to do. */
export type WorkflowParkUnusedClass = "deliverable" | "decide_again" | "start_over" | "withdraw";
/** One reason's row of {@link WORKFLOW_PARK_UNUSED_REASON_CLASS}: the class when the answer leaves the decision kept (`kept` — a later
* hand-over is judged again), the class when it spends the decision for good (`spent`), and, for a reason that does either, the member
* of the answer that spends it (`spentWhen`). */
export type WorkflowParkUnusedClassRow = {
readonly kept: WorkflowParkUnusedClass;
readonly spent?: never;
readonly spentWhen?: never;
} | {
readonly spent: WorkflowParkSpentClass;
readonly kept?: never;
readonly spentWhen?: never;
} | {
readonly kept: WorkflowParkUnusedClass;
readonly spent: WorkflowParkSpentClass;
readonly spentWhen: "terminal" | "explicit";
};
/** The classes a spent decision can have: a decision spent for good is never still deliverable, and its card still waits. */
type WorkflowParkSpentClass = Extract<WorkflowParkUnusedClass, "decide_again" | "start_over">;
/**
* THE classification of {@link WORKFLOW_PARK_UNUSED_REASONS} (#5208): one row per word, every word filled (a word added to the set with
* no row does not compile). A reason whose row has a `spent` class spends the decision for good — no later hand-over of it continues the
* run — always (no `kept` class), or when the answer carries `spentWhen`: the card is one no engine release resumes (`card_unresumable`
* with `terminal`; a card another release reads stays deliverable to a worker of that release); a person, asked whether to run the
* Workflow call that continues it, answered no (`denied` with `explicit`; a deny rule of any layer, the deployment's policy or its
* approval seat, a hook, a classifier, the session's mode, a wait that ran out or was cancelled, an approver or a policy that failed
* leave it deliverable). Every other answer keeps the decision — `host_changed` among them: once what the run's agents run under is
* back to what it ran under, the same decision continues the run. The engine's own readings read it: whether an answer spends its
* decisions (the final mark, the frame's wording) and which words a final mark may carry. Read it with {@link workflowParkUnusedClassOf}:
* a `workflow.parked_decision_unused` notice by its `detail.reason` and `detail.spent`, a {@link workflowParkDecisionOf} reading by its
* `reason` and whether it is `spent` (a `blocked` reading is kept). A frozen literal of plain data: a reader may copy it at build time.
*/
export declare const WORKFLOW_PARK_UNUSED_REASON_CLASS: {
readonly workflow_not_mounted: {
readonly kept: "deliverable";
};
readonly entry_unreadable: {
readonly kept: "decide_again";
};
readonly record_unreadable: {
readonly kept: "deliverable";
};
readonly not_holder: {
readonly kept: "deliverable";
};
readonly not_pending: {
readonly kept: "withdraw";
};
readonly decision_spent: {
readonly spent: "decide_again";
};
readonly launch_unrecorded: {
readonly kept: "deliverable";
};
readonly launch_unreadable: {
readonly spent: "start_over";
};
readonly host_changed: {
readonly kept: "deliverable";
};
readonly denied: {
readonly kept: "deliverable";
readonly spent: "decide_again";
readonly spentWhen: "explicit";
};
readonly ask_unanswerable: {
readonly kept: "deliverable";
};
readonly call_changed: {
readonly kept: "deliverable";
};
readonly launch_refused: {
readonly kept: "deliverable";
};
readonly continuing_elsewhere: {
readonly kept: "deliverable";
};
readonly card_unresumable: {
readonly kept: "deliverable";
readonly spent: "start_over";
readonly spentWhen: "terminal";
};
readonly waiting_elsewhere: {
readonly kept: "deliverable";
};
readonly run_not_ended: {
readonly kept: "deliverable";
};
readonly workflow_stopped: {
readonly kept: "withdraw";
};
};
/** The class of an answer `reason` gave, as {@link WORKFLOW_PARK_UNUSED_REASON_CLASS} reads it — a lookup and nothing else: the row's
* `spent` class when `spent` is `true`, its `kept` class when it is `false`. Undefined — no guess, never the other arm — for a word
* outside the set (an answer of a later engine) or a `reason` that is not a string (judged before it is used as a key, so a value a
* custom store hands back is never converted), for an arm the word's row does not have (`not_pending` spent, `decision_spent` kept),
* and for a `spent` that is not a boolean (a notice of an engine older than the member). */
export declare function workflowParkUnusedClassOf(reason: string, spent: unknown): WorkflowParkUnusedClass | undefined;
/** The words that can spend a decision — the rows of {@link WORKFLOW_PARK_UNUSED_REASON_CLASS} with a `spent` class: the `reason` of a
* `spent` {@link workflowParkDecisionOf} reading, and the only words a final mark carries. */
export type WorkflowParkSettlingReason = {
[R in WorkflowParkUnusedReason]: (typeof WORKFLOW_PARK_UNUSED_REASON_CLASS)[R] extends {
readonly spent: WorkflowParkUnusedClass;
} ? R : never;
}[WorkflowParkUnusedReason];
/** What came of one host decision for one park, as the run records of the park's tree say ({@link workflowParkDecisionOf}):
* - `open` — no record says anything of it: a hand-over may continue the run (the card's own state and the holder law aside);
* - `carried` — a continuation (`runId`) carried it and ended before its script reached the parked step (`not_reached`): it is not
* spent, and handed over again on its own it only reaches the same wait — hand it over with a decision for what stopped that run;
* - `driven` — a continuation (`runId`) drove the parked step with it: the card was consumed;
* - `spent` — no later hand-over of it continues the run (`reason`, a {@link WorkflowParkSettlingReason}, and its
* `code`), and `by` says who has said so: `mark` — the engine's continuation judged it, told it, and recorded a final mark, read on
* any entry of the park's token in the tree (`runId`, the run whose entry carries it); `drive` — the drive of a run of the tree
* (`runId`) refused it, and no continuation has told it yet;
* - `blocked` (#4833) — the engine's continuation judged it on the record of the run that holds the park (`runId`) and did not use it
* in the state `condition` names (`reason`, its `code`, and the fold `inputs` a `host_changed` named — what the person was told):
* handed over again in that same state it is told nothing and not used; once the state changes it is judged again. `condition` is
* an opaque, fixed-length string — compare it, never parse it. A host that does not hand a blocked decision over again loses
* nothing: the decision stays unused, and a person's new decision for the card is judged on its own.
* Precedence: `driven`, then `spent` (a mark, then a refused drive), then `carried`, then `blocked`. A `spent` or `blocked` reading
* carries `class` (#5208): what is left to do with the decision, as {@link WORKFLOW_PARK_UNUSED_REASON_CLASS} answers it in the engine
* that reads the state — absent only where the lookup answers nothing. Read with the card's state (#5246, {@link workflowParkDecisionOf}'s
* `card`): a card no longer pending gives every state `class: "withdraw"`, a card not read gives none. */
export type WorkflowParkDecisionState = {
readonly kind: "open";
readonly class?: WorkflowParkUnusedClass;
} | {
readonly kind: "carried";
readonly runId: string;
readonly class?: WorkflowParkUnusedClass;
} | {
readonly kind: "driven";
readonly runId: string;
readonly class?: WorkflowParkUnusedClass;
} | {
readonly kind: "spent";
readonly runId: string;
readonly reason: WorkflowParkSettlingReason;
readonly code?: string;
readonly by: "mark" | "drive";
readonly class?: WorkflowParkUnusedClass;
} | {
readonly kind: "blocked";
readonly runId: string;
readonly reason: WorkflowParkUnusedReason;
readonly code?: string;
readonly inputs?: readonly HostFoldInput[];
readonly condition: string;
readonly class?: WorkflowParkUnusedClass;
};
/**
* What came of the host decision `decisionId` for the park on `token`, read off the park's tree as {@link readWorkflowRunTree} read
* it (from the run any of the host's coordinates for the card names): every run's decision account (`WorkflowRun.decisions`, every
* drive of the decision whichever door it came through — an earlier run's included) and the marks on the park's entries: a final
* mark belongs to the decision (the token) — on any entry of this token in the tree it counts; a recoverable mark belongs to the
* holder — it counts on the entry of the run the holder law ({@link workflowParkHolderOf}) names alone. The engine writes every mark
* on the holder's entry (a run that comes to hold a park carries its holder's marks onto its own entry). The one reading a host's "hand it over again?" judge and the engine's continuation share: the
* engine judges the same tree. Pure over the tree; whether the card is still pending is the checkpoint store's word, and which run to
* hand it to the holder law's, never this. A `driven` row wins, then a `spent` one (a mark, then a refused drive), then `carried`,
* then `blocked`. The decision account and the marks are keyed by `decisionId` and the park's call: a host gives every card its own
* `decisionId` (two cards of the same call in one tree that share one would read each other's outcome).
*
* @contract workflow.park_decision.same_state — handed over again in the state its `blocked` answer's `condition` names, a decision
* is told nothing, the model is not asked (no continuation starts, the first request's frame does not name the run) and no mark is
* written; handed over in another state, it is told once and its mark replaced. A host cannot compute the state (`condition` is
* opaque) and keeps no account of its own: handing the decision over again in the same state costs nothing.
*
* `card` (#5246, optional) — the card's state as the checkpoint store answers it ({@link WorkflowParkCardState}, the word
* {@link workflowParkWaitOf} takes; the engine's continuation hands the card it read): the one input that, with the tree, says what is
* left to do with the decision, as a hand-over's notice says it. A card no longer pending (`resolved` / `expired` / `absent`) leaves
* nothing to decide: every state carries `class: "withdraw"`. A card not read (`unread`) carries no `class` on any state: the card
* pending, the tree's class would hold; gone, withdraw — not known. `pending`, or no `card` at all, answers the tree's reading as it is.
* A value outside the word's set is refused (`TypeError`).
*/
export declare function workflowParkDecisionOf(token: string, decisionId: string, tree: WorkflowRunTree, card?: WorkflowParkCardState): WorkflowParkDecisionState;
/** What came of the decisions naming one run. */
export type WorkflowParkAdoption = {
readonly kind: "started";
readonly runId: string;
readonly decisionIds: readonly string[];
readonly continuedAs: string;
/** A carried decision refuses the parked step: that step is refused and the script goes on past it. */
readonly stepDenied?: true;
} | {
readonly kind: "unused";
readonly runId: string;
readonly decisionIds: readonly string[];
readonly reason: WorkflowParkUnusedReason;
/** `not_holder`: the run that holds the card, or every run of a fork. `continuing_elsewhere`: the run id the workflow's
* resume claim is held for — no more than that: the run may carry another decision, or may never have started. (For
* `waiting_elsewhere` the notice's detail carries `holder` read off {@link waiting}.) */
readonly holder?: string | readonly string[];
/** `continuing_elsewhere`: the latest instant the claim holds the workflow (epoch ms, this machine's clock), for a holder whose
* process stopped without giving it back — this machine's clock when the refusal arrived plus the remaining time the store
* answered ({@link claimRemainingOf}); absent when the store said none, or said one that does not read. */
readonly claimExpiresAt?: number;
/** `continuing_elsewhere`: the store answered a remaining time for the claim that does not read — no instant is named, and
* the sentence says so. Absent otherwise. */
readonly claimLapseUnreadable?: true;
/** `workflow_stopped`: who stopped the workflow, as the journal store reports its stop (`ResumeClaimAnswer.stopped.by`). Not on
* the notice; the sentence reads it. */
readonly stoppedBy?: string;
/** `waiting_elsewhere`: the lines of the workflow that wait and hold this run off — each line's head run and what it waits
* on ({@link WorkflowWaitingLine}). The notice's detail carries their runs as `holder` (the run, or every run when more
* than one line waits, as for a fork); a host finds each card by the holder law. */
readonly waiting?: readonly WorkflowWaitingLine[];
/** `decision_spent`: the refusal code of the drive that spent it; `host_changed` (parent chain): the chain judge's code;
* `denied`: the refusing layer; `ask_unanswerable`: `approval_window_expired` when the approver's own window closed unanswered; `card_unresumable`: the card's row verdict code; `launch_unreadable`: the launch record's
* member that does not read back (`host.thinking`, `face`, …; `record` for the whole value). */
readonly code?: string;
/** `card_unresumable`: the row verdict's own `reason` and fixed `sentence`, and its `terminal` mark (no engine release
* resumes the card: the host's to expire). */
readonly rowReason?: string;
readonly rowSentence?: string;
readonly terminal?: true;
/** `denied`: the refusal is a person's own answer to the call's question (it settles the decision; every other refusal leaves
* it deliverable). Not on the notice; the sentence reads it. */
readonly explicit?: true;
/** `card_unresumable`: the card the row judge refused is another card of the run, not this decision's own (#5208: the
* decisions it stops are told its facts without `terminal`). Not on the notice; the sentence reads it. */
readonly otherCard?: true;
/** `record_unreadable`: what did not read is the decision's own card (#5246), so the sentence does not say it is still waiting.
* Not on the notice; the sentence reads it. */
readonly cardUnread?: true;
/** `record_unreadable` / `continuing_elsewhere` (#5229): a person answered no to the continuation's question and that answer
* was not recorded — its record did not read back under the workflow's resume claim, or the claim was held by another leg (which
* may be continuing the run with the same decision) — so it is not told as settled. Not on the notice; the sentence reads it. */
readonly answerUnrecorded?: true;
/** `continuing_elsewhere` (#5229): the claim is held by another leg only to record a person's answer to continuing the workflow
* (no run holds it, and `holder` is absent: the claim's holder id is no run's). Not on the notice; the sentence reads it. */
readonly heldForAnswer?: true;
/** `not_pending`: the run that continued the run from this card (its record holds a `driven` row for the card's call). */
readonly continuedAs?: string;
/** `host_changed`: which half moved — `identity`: the settings every agent of the run is folded under (the deployment's
* baseline, its allowed models — the run's own launch model among them — its caps, the host's clamps and posture) or the
* tenant (principal, isolation scope); `parent_chain`: the permission rules the parked agents were stopped under. */
readonly changed?: "identity" | "parent_chain";
/** `host_changed` (identity): which of the fold's inputs moved ({@link HOST_FOLD_INPUTS}), when the run's stamp names them;
* the sentence names them. */
readonly inputs?: readonly HostFoldInput[];
/** `launch_refused`: the launch's own sentence, bounded. */
readonly why?: string;
};
/** What a refused resume claim tells of its hold: the run it is held for and when the store's expiry frees it, each when the
* store said — the members `continuing_elsewhere` carries; or, for a workflow that was stopped, who stopped it (`stoppedBy`, the
* member `workflow_stopped` carries: a stop holds the claim for good, for no run). */
export type WorkflowClaimHeld = Pick<Extract<WorkflowParkAdoption, {
kind: "unused";
}>, "claimExpiresAt" | "claimLapseUnreadable" | "heldForAnswer" | "stoppedBy"> & {
readonly holder?: string;
};
/** What the remaining time a refused resume claim's answer carries tells ({@link claimRemainingOf}): `unsaid` — the store
* answered none; `at` — the latest instant the refusal stands, epoch ms by this machine's clock; `unreadable` — the store
* answered one that does not read. */
export type WorkflowClaimLapse = {
readonly kind: "unsaid";
} | {
readonly kind: "at";
readonly at: number;
} | {
readonly kind: "unreadable";
};
/**
* THE one reader of a refused resume claim's remaining time (`ResumeClaimAnswer.remainingMs`), read once, as the refusal arrives
* (`now`: this machine's clock then). None answered ⇒ `unsaid`. A safe integer from 1 to `RESUME_CLAIM_REMAINING_MS_MAX` ⇒ `at`,
* `now` plus it — an upper bound (the answer's time on its way only makes it later than the store's own expiry), named in words
* as "at the latest" and never compared with anything. Anything else — 0 or less (refused while expired: a contradiction), not
* a number, not an integer, over the bound, or a sum no date holds ⇒ `unreadable`: no instant is named.
*/
export declare function claimRemainingOf(answer: {
readonly remainingMs?: unknown;
}, now: number): WorkflowClaimLapse;
export declare function answerRecordClaimHolder(): string;
export declare function isAnswerRecordClaimHolder(holder: string | undefined): boolean;
/** The hold a refused claim's verdict tells ({@link WorkflowClaimHeld}), its remaining time as {@link claimRemainingOf} read it; a
* holder that records a person's answer is no run, and is told as that ({@link isAnswerRecordClaimHolder}: `heldForAnswer`); a
* stop the store reports (`stopped`) is told as that alone — it is no run's hold and never lapses. */
export declare function claimHeldOf(verdict: {
readonly holder?: string;
readonly lapse?: WorkflowClaimLapse;
readonly stopped?: {
readonly by: string;
};
}): WorkflowClaimHeld;
/** The words a refusal says, in brackets, of a claim whose store answered a remaining time that does not read. */
export declare const CLAIM_REMAINING_UNREADABLE_NOTE = "the journal store answered a remaining time for the claim that does not read, so no time is named";
/** A claim's expiry (epoch ms, store-supplied) as the refusals name it — an ISO-8601 UTC instant — or `undefined` for anything
* that names no instant (absent, not a finite number, outside the range a date can hold): never a guessed time. */
export declare function claimLapseInstant(expiresAt: unknown): string | undefined;
/** The host's model (its catalog key; with `modelPinned`, the id of a model handed as an object) and thinking level — what a run's
* launch record keeps of its host. */
export interface WorkflowHostIdentity {
readonly model?: string;
readonly modelPinned?: true;
readonly thinking?: string;
}
/**
* The inputs a run's host fold is stamped by, one digest each (the run's `hostFoldDigest` is `v2;<input>=<digest>;…` in this
* order), so a continuation that finds the fold moved can say which: the tools the deployment gives workflow agents, its other
* settings for them (their rules and tool lists), the models workflows may use, the limits each agent runs under, this
* session's permission clamps and approval posture, the run's model (its configuration), the address that model is served
* from, the run's thinking level, and the account the run is for.
*/
export declare const HOST_FOLD_INPUTS: readonly ["tools", "baseline", "allowed_models", "agent_limits", "permissions", "model", "endpoint", "thinking", "account"];
export type HostFoldInput = (typeof HOST_FOLD_INPUTS)[number];
/** The stamp of a fold whose inputs digest to `digests`. */
export declare function hostFoldStampOf(digests: Readonly<Record<HostFoldInput, string>>): string;
/** This leg's gate for the `Workflow` call that continues a run: judged with every layer of the leg's gate, the ask resolved
* by the answer given here (a person's decision answers only an ask whose class is `ordinary`), never parked. A refusal says
* whether it is a person's own "no" to the call's question (`explicit`) — the one refusal that settles the decision. */
export interface WorkflowContinuationGate {
judge(call: {
readonly toolCallId: string;
readonly toolName: string;
readonly input: Readonly<Record<string, unknown>>;
}, answer: "person" | "none"): Promise<{
readonly kind: "allow";
} | {
readonly kind: "denied";
readonly layer: string;
readonly explicit: boolean;
} | {
readonly kind: "ask_unanswerable";
readonly code?: "approval_window_expired";
} | {
readonly kind: "call_changed";
}>;
}
/** What a continuation starts from: the run it continues, that run's launch record, the decisions it carries. */
export interface WorkflowContinuation {
readonly fromRunId: string;
readonly launch: WorkflowLaunchRecord;
readonly decisions: ReadonlyArray<{
readonly token: CheckpointToken;
readonly outcome: ResumeOutcome;
readonly decisionId: string;
readonly inheritedGate?: InheritedGate;
}>;
/** The keys the workflow's resume claim is taken under ({@link workflowClaimKeysOf} of `fromRunId`'s tree): its root first. */
readonly claimKeys: readonly string[];
}
/** The seats the Workflow tool's closure hands its adopter. Every seat that reads the call's context reads the one the leg
* hands (`WorkflowParkAdoptionLeg.ctxOf`): the context a model's own call of this leg is run with, so the continuation's
* scope, host fold and completion route are a model call's. */
export interface WorkflowParkAdopterSeats {
/** The trusted channel's entries, as the host gave them (judged here). */
readonly decisions: ReadonlyArray<unknown>;
/** The scope a run launched with this context is recorded under (records are read in it). */
readonly scopeOf: (ctx: ToolExecuteContext) => string;
readonly store: WorkflowRunStore | undefined;
readonly journalStore: WorkflowJournalStore | undefined;
/** The store the run's children park in — where a card's pending state is read. */
readonly parkStoreOf: (ctx: ToolExecuteContext) => CheckpointStore | undefined;
/** The digest of the host fold a continuation of the run `launch` records would fold every child under — this leg's settings,
* with the face, model and thinking level the record fixed (none recorded ⇒ this leg's) — the same function the tool folds
* every child under. */
readonly hostFoldDigest: (ctx: ToolExecuteContext, launch: WorkflowLaunchRecord | undefined) => string | undefined;
/** The parent chain this leg hands a driven child when the decision re-supplies none. */
readonly chainToHand: (ctx: ToolExecuteContext) => InheritedGate["parentConstraints"] | undefined;
/** Start the continuation (the tool's own launch, its third argument): the new run's id, the launch's refusal, or — the run's
* resume claim taken before the run exists and refused — its hold, as the store told it (no run was started). Once the claim is
* granted and before the run exists, the launch asks `confirm` (the adopter's card judgment read again, under the claim): the
* decisions the continuation carries — `continuation.decisions` less any another leg recorded as settled meanwhile — or
* `false` ⇒ it releases the claim and starts nothing (`superseded`). */
readonly launch: (continuation: WorkflowContinuation, ctx: ToolExecuteContext, confirm: () => Promise<WorkflowContinuation["decisions"] | false>) => Promise<{
readonly runId: string;
} | {
readonly refused: string;
} | {
readonly claimHeld: WorkflowClaimHeld;
} | {
readonly superseded: true;
}>;
/** Hold the workflow's resume claim — under `claimKeys` ({@link workflowClaimKeysOf}) in `scope`, taken as the launch takes it, no
* run started under it — while `body` runs, and give it back once `body` has settled: `value`, what `body` answered; `claimHeld`,
* the claim another run holds or a stop (as the store told it); `refused`, a store that threw or did not answer in time (`body`
* not run). */
readonly underClaim: <T>(claimKeys: readonly string[], scope: string, body: () => Promise<T>) => Promise<{
readonly value: T;
} | {
readonly claimHeld: WorkflowClaimHeld;
} | {
readonly refused: string;
}>;
/** Every store wait here is bounded by it. */
readonly waitBoundMs: number;
/** Tell the deployment's account what failed (`what`) and what the store threw — a store failure the adopter's answers name by
* class only, or by no words at all ({@link storeFaultReport} composes it; the seat contains a sink that fails). Absent ⇒ none. */
readonly reportStoreFault?: (what: string, err: unknown) => void;
/** The Workflow tool's name, the call the leg's gate judges. */
readonly toolName: string;
/** Leave ONE call what it is to do with a run, once: the model's own `Workflow({ resumeFromRunId })` call whose card was decided
* allow (`toolCallId`, the card's call), on a leg that resumed to run it — the run's decisions to carry (the one way a model's
* call carries a decision), or a refusal to answer it with instead of running (the run was already continued from the card,
* or the card is one this engine never resumes). */
readonly holdForCall: (runId: string, toolCallId: string, held: {
readonly decisions: WorkflowContinuation["decisions"];
} | {
readonly refusal: string;
}) => void;
}
/** The leg's half: its gate (absent ⇒ nothing gates the leg's calls), the call-id mint for each continuation, and the model's
* own `Workflow` call for a run when the leg resumed from that call's card ({@link resumedWorkflowCallOf}). */
export interface WorkflowParkAdoptionLeg {
readonly gate: WorkflowContinuationGate | undefined;
readonly mintCallId: () => string;
/** The context this leg runs a tool call with (its spec's enrichment over the call id). */
readonly ctxOf: (toolCallId: string) => ToolExecuteContext;
readonly resumedCall?: ResumedWorkflowCall;
}
/** The model's own `Workflow({ resumeFromRunId })` call a leg resumed to run: the run it names, the call's id, and whether its
* card was decided allow (by whichever seat answers the card — a person or the deployment's policy). */
export interface ResumedWorkflowCall {
readonly runId: string;
readonly toolCallId: string;
readonly approved: boolean;
}
/** The model's own `Workflow({ resumeFromRunId })` call a leg's resumed card holds, if it holds one: that call answers for its
* run on this leg — the adoption step tells and frames nothing for the run a second time, and the call carries the run's
* decisions only when its card was decided allow. */
export declare function resumedWorkflowCallOf(pendingCall: {
readonly toolCallId: string;
readonly toolName: string;
readonly args: unknown;
} | undefined, outcome: ResumeOutcome | undefined, toolName: string): ResumedWorkflowCall | undefined;
export type WorkflowParkAdopter = (leg: WorkflowParkAdoptionLeg) => Promise<WorkflowParkAdoption[]>;
/**
* How many runs of one workflow's tree its walks read, in all — the walk down a run's successors (the holder law's) and the walk
* up a line to its root. It counts runs, not generations: a plain workflow gains one run (one generation) each time a decided
* step is continued, so a line of steps continued one after another reads as many runs as it took continuations. Past it the
* tree is not read whole (`"over_bound"`): no holder is established and no continuation of the tree is judged, so none starts.
*/
export declare const WORKFLOW_TREE_RUNS_MAX = 4096;
/** What a sentence says a store threw: the error's class, with its `code` when it carries one — never its text, which may quote a
* record (tokens included) or the store's own connection details; the value's `typeof` when it names no class. Each part is one
* line of at most 64 code points (`foreignLine`: a store's words, a credential-shaped part withheld first), so the whole is bounded. The one wording of every workflow sentence that
* tells a store's failure (the park lane's refusal at a resume's admission, the Workflow tool's resume claim and launch read, the
* continuation's launch that threw). */
export declare function storeErrorClassOf(err: unknown): string;
/** The deployment's account of a store failure that a workflow sentence names by class only ({@link storeErrorClassOf}): `what`
* failed (it may name a run id the model wrote), then what the store threw — the whole line through the egress pass (credentials
* withheld) — for the operator's error sink, never a sentence the model or a person reads. */
export declare function storeFaultReport(what: string, err: unknown): Error;
/** A store read bounded by the caller's wait: `ok: false` for a store that threw or did not answer in time. */
export type BoundedStoreRead = <T>(p: Promise<T>) => Promise<{
ok: true;
value: T;
} | {
ok: false;
}>;
/** A {@link BoundedStoreRead} that waits at most `ms`. */
export declare function boundedStoreRead(ms: number): BoundedStoreRead;
/** One run record of `scope`, or `null` when the store holds none there. */
export declare function readWorkflowRunIn(store: Pick<WorkflowRunStore, "get">, id: string, scope: string, within: BoundedStoreRead): Promise<WorkflowRun | null | "unreadable">;
/** Whether `b` continues `a`: `b`'s `resume.fromRunId` is `a`'s id and, when `b` recorded which creation of that id it read
* (`resume.fromCreationId`), `a` is that same creation — a record created again under a reused id (or one an older engine created,
* carrying no creation id) is not the run `b` continued, the same answer as a source that is gone. The one edge every walk of a
* workflow's tree reads: the tree reader's walks up and down ({@link readWorkflowRunTree}) and the tree id's walk. */
export declare function isContinuationOf(a: WorkflowRun, b: WorkflowRun): boolean;
/** The mark only {@link readWorkflowRunTree} sets on what it answers (never exported: a value carrying it was read by the reader). */
declare const WORKFLOW_RUN_TREE: unique symbol;
/**
* One workflow's tree as {@link readWorkflowRunTree} read it, at one pass of the store: its `root` (the run with no source above it
* in the scope — the run the tree started from, or the topmost run whose edge up does not hold), and `records`, the root and every
* run below it, each once (the root first, then each layer as the walk read it). Every edge between them is {@link isContinuationOf}.
* Two things make one: the reader ({@link readWorkflowRunTree}: the whole tree) and {@link workflowRunTreeOfOne} (which says it holds
* one record alone). The judges that read a tree ({@link workflowParkDecisionOf}, {@link workflowParkWaitOf}, `workflowParkHolderOf`)
* take nothing else, so a collection a host gathers on its own does not compile there.
*/
export interface WorkflowRunTree {
readonly root: WorkflowRun;
readonly records: readonly WorkflowRun[];
/** The id the root names as its source when the reader found a record under it that is another creation (the source was removed and
* the id used again): that id now names another workflow's run. */
readonly sourceRecreated?: string;
readonly [WORKFLOW_RUN_TREE]: true;
}
/**
* A tree of one run alone: what a run with no run store to read reads of itself (it has nothing to walk). From the package root, for a
* host that holds one record and cannot read the store (a check made synchronously, under a row's lock; an index built per record).
* What a judge answers over it is what that record says of itself and nothing more: `workflowParkHolderOf(token, treeOfOne)` reads
* whether this record claims to hold the park on `token` (and at which generation), not who holds it on the line now — a later run that
* took the park over is not in it, and `workflowParkWaitOf` over it can never answer `moved_on`. Routing a person's decision, or
* judging whether a card is still waited on, reads the whole tree: {@link readWorkflowRunTree}.
*/
export declare function workflowRunTreeOfOne(run: WorkflowRun): WorkflowRunTree;
/**
* The names a workflow's tree answers to on the journal store's claim table (#5156): the keys its resume claim is taken under and a stop
* writes its tombstone on — the tree's root first, then the tree id the root records when it is another id (a root whose own edge up
* does not hold: its source was removed). A root that names no source is its own tree id (one key). A root that names a source it does
* not reach and records no tree id (an older engine's record, its line broken) has no tree id: `fragment`, and the root alone is its
* key — a stop of it stops that part of the workflow only. So is a tree id that the reader found used again by another workflow's run
* (`sourceRecreated`): that id is the other workflow's key now. Every reader that takes, re-takes or stops the claim reads it here.
*/
export declare function workflowClaimKeysOf(tree: WorkflowRunTree): {
readonly keys: readonly string[];
readonly fragment: boolean;
};
/** The one judgment of whether a park a run's record names binds a checkpoint row — read by a resume's admission before it acts on the
* row and by a stop before it withdraws the card: why it does not (the record names no token or no session for it; the store answered a
* row under another token; the row belongs to another session), or `undefined` when it binds. Without `row`, the key alone is judged. */
export declare function parkBindingFault(key: {
readonly token: string;
readonly sessionId: string;
}, row?: {
readonly token: string;
readonly sessionId: string;
}): string | undefined;
/** Whether two key lists ({@link workflowClaimKeysOf}) name the same keys in the same order. */
export declare function sameClaimKeys(a: readonly string[], b: readonly string[]): boolean;
/** The record of `id` in `tree`, if it is one of its runs. */
export declare function workflowRunInTree(tree: WorkflowRunTree, id: string): WorkflowRun | undefined;
/** The runs of `tree` below `id` (its successors, theirs, …), in the order the tree holds them. */
export declare function workflowRunsBelow(tree: WorkflowRunTree, id: string): WorkflowRun[];
/** The runs of `tree` that continue `id` directly. */
export declare function workflowRunChildrenIn(tree: WorkflowRunTree, id: string): WorkflowRun[];
/** `id` and every run above it in `tree`, `id` first (up to the root). */
export declare function workflowRunLineIn(tree: WorkflowRunTree, id: string): string[];
/** Where a resume that carries no decision continues when it names `runId` ({@link workflowLineTailOf}). */
export type WorkflowLineTail = {
readonly kind: "tail";
readonly runId: string;
} | {
readonly kind: "fork";
readonly leaves: readonly string[];
};
/**
* The run a resume that carries no decision continues when it names `runId` of `tree` (#5159): `runId` itself when nothing continued
* it; the last run of the line below it when the runs that continued it make one line (the workflow is one run that went on — the same
* run the reference's resume continues); `fork` when they split anywhere below it, with every line's last run (`leaves`), so which one
* is meant is not one answer. A resume that carries a decision is not moved: a decision belongs to the run that holds its card.
*/
export declare function workflowLineTailOf(tree: WorkflowRunTree, runId: string): WorkflowLineTail;
/** Why a resume naming a run whose line split is not continued, and the way out — one wording for the model's own call and the
* run's admission (`leaves` already bounded by the caller's echo). */
export declare function workflowLineForkReason(leaves: readonly string[]): string;
/**
* THE one reader of a workflow run's tree (#5156): from `runId`, up its edges ({@link isContinuationOf}) to the root — a record that
* names no source, or whose source has no record in `scope` or is not the run it continued (gone, created again) — then from the root
* down through every run that continues one of the tree's runs. Every read is bounded on its own (`WORKFLOW_STORE_WAIT_BOUND_MS`). Any
* run of the tree read this way reads the same tree. Answers:
* - the tree ({@link WorkflowRunTree}) — what the judges take;
* - `"unreadable"` — a read failed or did not answer; `runId` has no record in `scope` (the same answer as a record that does not
* read: a scope gives no word of what another scope holds); the walk met a run twice; a run read on the way up is not, with the same
* creation, among the runs read on the way down (the store's two answers disagree: a listing that left it out, or a record that
* came or was created again between the two reads) — never a union of the two; or a listing on the way down held a row that does not
* read (a damaged record: it may be a later run of the tree, and a tree without it could name an earlier holder of a park);
* - `"over_bound"` — the tree is more runs than {@link WORKFLOW_TREE_RUNS_MAX}.
* A tree of N runs whose named run is L runs below the root costs at most N listings (`listByScope` with `resumedFrom`, no limit) and
* L + N − 1 reads by id. A store that does not filter by `resumedFrom` answers its whole scope to every listing, and any damaged row in
* the scope then leaves every tree of it unread.
*/
export declare function readWorkflowRunTree(store: Pick<WorkflowRunStore, "get" | "listByScope">, runId: string, scope: string): Promise<WorkflowRunTree | "unreadable" | "over_bound">;
/** {@link readWorkflowRunTree} with the caller's bound on each read (the engine's own waits run on the run's timers). `self`: the
* reading run's own record as it holds it — read in place of the store's copy of it (a live run's own record is newer than what the
* store holds), and a member of the tree whether or not the store has its row yet. */
export declare function readWorkflowRunTreeWithin(store: Pick<WorkflowRunStore, "get" | "listByScope">, runId: string, scope: string, within: BoundedStoreRead, self?: WorkflowRun): Promise<WorkflowRunTree | "unreadable" | "over_bound">;
/** One line of a workflow that waits: its head run, and what that run waits on — the parked agent's label and the card's
* checkpoint id (each when it reads), and `yielded` when it is a scheduler's yield (nobody decides it; continuing the run moves
* it). */
export interface WorkflowWaitingLine {
readonly runId: string;
readonly label?: string;
readonly checkpointId?: string;
readonly yielded?: true;
}
/** What {@link otherWaitingLine} reads for a continuation of a run: nothing it would add a line to (`root` — the tree's root —
* keys the workflow's resume claim), lines that wait and hold it off (each line's head, and what it waits on), or a card did not
* read. */
export type WorkflowLineVerdict = {
readonly kind: "none";
readonly root: string;
} | {
readonly kind: "waiting";
readonly root: string;
readonly lines: readonly [WorkflowWaitingLine, ...WorkflowWaitingLine[]];
} | {
readonly kind: "unreadable";
};
/**
* One workflow runs or waits on one line at a time: a continuation that would add a waiting line starts nothing. Before
* `fromRunId` is continued, its whole tree is read for the runs that wait — a run whose record ended suspended on a park
* (`workflow.agent_parked`) and that the holder law ({@link workflowParkHolderOf}, over the tree) still names on a card the
* checkpoint store says is pending. Continuing `fromRunId` adds no line when it is itself the head of a waiting line — it waits,
* and no run below it does: its continuation takes that line on (an allow moves it, a deny closes the step it waits on), whatever
* other lines wait. Otherwise every run off `fromRunId`'s line (neither it nor a run above it) that waits holds it off — every one
* below it when it waits itself. The lines named are the heads of those (a run with a waiting run below it is not one). A holder
* whose record still says `running` has not ended on a park, so it is not one (a live run is held off by the resume claim; a run
* that died there must not lock the tree), nor one that ended any other way; a card a fork stands on is the line's own when one
* of the fork's runs is on the line. A yield (a scheduler's `resource_limit` checkpoint) is a wait too — nobody decides it;
* continuing that run moves it. A card that does not read starts nothing either (`unreadable`). Only `fromRunId`'s tree (as the
* caller read it, {@link readWorkflowRunTree}) and its own cards are read: never the host's pending listing.
*/
export declare function otherWaitingLine(tree: WorkflowRunTree, parkStore: CheckpointStore | undefined, fromRunId: string, within: BoundedStoreRead): Promise<WorkflowLineVerdict>;
/** The run among `records` that took the approval of the card on `token` at `callKey`: its record carries the card's entry (a run
* that reaches a card carries its entry, inherited or its own) and accounts a `driven` decision for the call. A run that drove the
* same call on another card (a sibling's card in the same tree, or a card the call re-parked on since) carries that card's entry, not
* this one, and is not it. One reading for the adopter's `continuedAs`, the wait judge and the sentence a resume settles that step with. */
export declare function runThatDrove(records: readonly WorkflowRun[], token: string, callKey: string): WorkflowRun | undefined;
/**
* #5146 — whether the row of run `runId` still waits on the park it made on `token`: the one answer to "is this line still waiting for
* an approval" ({@link WorkflowParkWait}). Hand it the tree the row's run belongs to, as {@link readWorkflowRunTree} read it, and the
* card's state as the checkpoint store answers it; a host reads both with {@link readWorkflowParkWaits}.
* Pure over what it is handed; the records are never written (the source record keeps what it ended with). In order: a later run of
* the tree that drove the parked step on this card answers `moved_on`; the holder law naming other runs answers `moved_on`; a card not
* read answers `unknown`; a card no longer pending answers `lapsed`; the holder law naming this run with the card pending answers
* `waiting` — unless a run below this one parked the same call on another card of its own, which answers `moved_on` naming it (the
* line asked that step again, #5183); a tree with no holder of the park (or without `runId`'s own entry for it) answers `unknown`. Which later run drove it is read
* off the runs' decision accounts, so a drive whose decision named no `decisionId`, or one still running the approved step (its row is
* written when the step returns), answers `lapsed` (the card is no longer pending) rather than `moved_on` — the row reads `interrupted`
* either way. Whether a pending card still has a line waiting on it at all is this judge asked of the card's holder (the holder law's
* run, or each run of a fork): `waiting` for any of them, the card is alive.
*/
export declare function workflowParkWaitOf(token: string, runId: string, tree: WorkflowRunTree, card: WorkflowParkCardState): WorkflowParkWait;
/** The host's two stores {@link readWorkflowParkWaits} reads, injected (it opens nothing of its own). An object, so a later member
* (a batch of runs) is additive. */
export interface WorkflowParkWaitSeats {
readonly runStore: Pick<WorkflowRunStore, "get" | "listByScope">;
readonly checkpointStore: Pick<CheckpointStore, "get">;
}
/**
* #5146 — {@link workflowParkWaitOf} for every park run `runId` made (its record's park entries naming it), read from the host's two
* stores: the run's tree from the run store ({@link readWorkflowRunTree}), the cards from the checkpoint store (together) — every
* read bounded by {@link WORKFLOW_STORE_WAIT_BOUND_MS}. A card that does not read — the store threw or did not answer, its row is not
* this token's, or its status is not one of `pending` / `resolved` / `expired` — answers `unknown` / `card_unread`; no row is `absent`.
* Answers `"unreadable"` / `"over_bound"` as the tree reader does.
*/
export declare function readWorkflowParkWaits(seats: WorkflowParkWaitSeats, runId: string, scope: string): Promise<ReadonlyMap<string, WorkflowParkWait> | "unreadable" | "over_bound">;
/**
* #4833 — the state a decision was not used in, as the person is told it: an opaque, fixed-length digest (sha256, the first 32 hex
* characters) of the notice's keys that say why — `reason`, `code`, `changed`, `inputs`, `holder` (a list in sorted order), `why`,
* `rowReason`, `terminal` — and no key that carries an instant (`claimExpiresAt`). Two answers with the same digest told the person
* the same thing: the same decision handed over again in that state is told nothing. One definition; no table per reason. Exported
* for the pins only (not at the package root): a consumer compares the digest, never computes it.
*/
export declare function conditionOf(o: Extract<WorkflowParkAdoption, {
kind: "unused";
}>): string;
/** The adopter the Workflow tool's closure registers: it reads its seats and starts each continuation through the tool's own
* launch, the one caller that passes it a continuation. */
export declare function createWorkflowParkAdopter(seats: WorkflowParkAdopterSeats): WorkflowParkAdopter;
/** Register a Workflow tool's adopter (the factory's own call, on the tool it built). */
export declare function registerWorkflowParkAdopter(tool: object, adopter: WorkflowParkAdopter): void;
/** The adopter of a Workflow tool this engine built, if `tool` is one. */
export declare function workflowParkAdopterOf(tool: object): WorkflowParkAdopter | undefined;
/** The decisions a leg carried while it mounts no Workflow tool: each run named is answered `workflow_not_mounted`. */
export declare function unmountedWorkflowParkAdoptions(raw: ReadonlyArray<unknown>): WorkflowParkAdoption[];
/**
* The sentence a reason is told in — to the person (the notice) and to the model (the first request's frame): what happened,
* whether the approval card is still waiting, and what can be done now. Every echoed value is bounded and neutralized.
*/
export declare function workflowParkUnusedSentence(o: Extract<WorkflowParkAdoption, {
kind: "unused";
}>): string;
/** The refusal of a model's `Workflow({ resumeFromRunId })` call for a run that is not continued: the reason's own sentence (the
* engine's continuation tells the person the same one), with the decision half only when the call carried decisions. */
export declare function notContinuedRefusalOf(runId: string, reason: "launch_unreadable" | "waiting_elsewhere" | "continuing_elsewhere" | "workflow_stopped", more?: Pick<Extract<WorkflowParkAdoption, {
kind: "unused";
}>, "code" | "holder" | "waiting" | "claimExpiresAt" | "claimLapseUnreadable" | "heldForAnswer" | "stoppedBy"> & {
readonly decisionIds?: readonly string[];
}): string;
/** Why a run of a tree past {@link WORKFLOW_TREE_RUNS_MAX} is not continued, and the way out — one wording for the model's own
* call and for the run's admission. */
export declare const WORKFLOW_TREE_OVER_BOUND_REASON = "its workflow has more runs than the 4096 the engine reads to judge a continuation (every continuation of a run adds one), so it is not continued and nothing was started. Start the workflow again \u2014 it runs from its first step";
/** The model's own `Workflow({ resumeFromRunId })` call for a run of such a tree. */
export declare function workflowTreeOverBoundSentence(runId: string): string;
/**
* The refusal the model's own `Workflow({ resumeFromRunId })` call is answered with, on the leg that resumed to run it, when that
* call must not run: the run was already continued from the card (`not_pending` naming the run that continued it), the card
* is one this engine never resumes (`card_unresumable`), or another line of the workflow waits on a card (`waiting_elsewhere`,
* told with the decision it carried). Undefined for every other answer (the call then runs as it is).
*/
export declare function resumedCallRefusalOf(o: WorkflowParkAdoption): string | undefined;
/** The two codes a leg's adoption step tells what came of the decisions it carried by (#5208): not used, and used. */
export type WorkflowParkDecisionNoticeCode = "workflow.parked_decision_unused" | "workflow.parked_decision_used";
/**
* A leg's workflow-park contributor to its adoption step: the decisions it carries are continued by the mounted Workflow tool's
* adopter (or, with no tool mounted, answered `workflow_not_mounted`); every decision that did not continue its run is told to
* the person once per run (`workflow.parked_decision_unused`, through `deliver`) — except one the engine already recorded as settled
* for good, or as not used in the very state it would be told in now (#4833), which is answered with nothing — and every continuation
* started is told once with the decisions it carries (`workflow.parked_decision_used`, #5208: off the screen, a record); all of them
* before the leg's first request to its model. The returned text — the first request's `workflow_continuation` frame — tells the model
* what came of each. On a leg that resumed to run the model's own `Workflow({ resumeFromRunId })` call for a run, the mounted tool's
* adopter tells neither code for the readable decisions handed for that run, whichever way the call's card was decided (the call answers
* for the run). Undefined when the leg carries no decision.
*/
export declare function workflowParksContributor(input: {
readonly decisions: ReadonlyArray<unknown> | undefined;
readonly adopter: WorkflowParkAdopter | undefined;
readonly gate: WorkflowContinuationGate | undefined;
readonly mintCallId: () => string;
/** The context this leg runs a tool call with. */
readonly ctxOf: (toolCallId: string) => ToolExecuteContext;
readonly toolName: string;
/** The leg's resumed card, when it holds a model's `Workflow({ resumeFromRunId })` call ({@link resumedWorkflowCallOf}). */
readonly resumedCall?: ResumedWorkflowCall;
readonly deliver: (notice: {
code: WorkflowParkDecisionNoticeCode;
message: string;
detail: Record<string, unknown>;
}) => void;
/** The leg's reading of the engine's messages (its locale): the frame is worded through it. */
readonly say: EngineText;
}): (() => Promise<string | undefined>) | undefined;
/** The first request's account of what came of the decisions (the `workflow_continuation` frame's body); `toolName` is the
* Workflow tool's name, which the model is told not to call for a continuation the engine started. A decision told nothing — one
* whose mark already names the state it was not used in (#4833) — is not among `outcomes`: the adopter leaves it out. */
export declare function workflowContinuationFrameBody(outcomes: readonly WorkflowParkAdoption[], toolName: string, say: EngineText): string | undefined;
export {};