@sema-agent/core
Version:
Stateless, task-oriented AI agent core
915 lines • 77.4 kB
TypeScript
import { type SpendParent } from "../core/spend-account.js";
import type { TSchema } from "typebox";
import type { InheritedGate, RunInternals, RunnerSelfSeat } from "../core/runner/contracts.js";
import type { CheckpointStore, CheckpointToken, ResumeOutcome } from "../core/checkpoint-store.js";
import type { AgentDefinition, TaskEvent, TaskSpec, ThinkingLevel } from "../core/types.js";
import { type ParentSpecSeat } from "../core/parent-spec-seat.js";
import { type WorkflowRunStore } from "../core/workflow-run-store.js";
import type { WorkflowJournalStore, WorkflowLaunchRecord, ResumeClaimArgs } from "../core/workflow-journal-store.js";
import { type WorkflowAgentResult } from "./workflow-types.js";
import { type WorkflowClaimLapse } from "./workflow-park-adoption.js";
import { type WorkflowRunStopReceipt } from "./workflow-stop.js";
import type { WorkflowRun, WorkflowEvent, WorkflowAgentStopReceipt } from "./workflow-types.js";
import { type TaskArtifactSeat } from "../core/tool-result-store.js";
export * from "./workflow-types.js";
/** 198 `P0m` (pretty.js:447248) — prose state: the child's final text is the script-facing return value. */
export declare const WORKFLOW_SUBAGENT_PROMPT = "You are a subagent spawned by a workflow orchestration script. Use the tools available to complete the task.\n\nCRITICAL: Your final text response is returned **verbatim** as a string to the calling script \u2014 it is your return value, not a message to a human.\n- Output the literal result (data, JSON, text). Do NOT output confirmations like \"Done.\" or \"Sent.\"\n- If asked for JSON, return ONLY the raw JSON \u2014 no code fences, no prose, no markdown.\n- Do NOT address a human user \u2014 there is no user on the other end. Put your answer in your final text response.\n- Do NOT delegate to further sub-agents or start nested workflows; complete the task yourself.\n- Be concise. The script will parse your output.";
/** 198 `M0m` (pretty.js:447333) — schema state: the answer goes through the StructuredOutput tool, once. */
export declare const WORKFLOW_SUBAGENT_PROMPT_SCHEMA = "You are a subagent spawned by a workflow orchestration script. Use the tools available to complete the task.\n\nCRITICAL: You MUST call the StructuredOutput tool exactly once to return your final answer. The tool's input schema defines the required shape.\n- Do your work (Read files, run commands, etc.), then call StructuredOutput with your answer.\n- Do NOT put your answer in a text response. The script reads ONLY the StructuredOutput tool call.\n- If the schema validation fails, read the error and call StructuredOutput again with a corrected shape.\n- After calling StructuredOutput successfully, end your turn. No acknowledgment needed.\n- Do NOT delegate to further sub-agents or start nested workflows; complete the task yourself.";
/** 198 composite prose-append `O0m` (pretty.js:447255) — a script-supplied CUSTOM persona gets the return
* contract APPENDED (never replaced), mirroring 198's `opts.agentType` + schema composition. VERBATIM
* (PARITY-SPOT-WORKFLOW F6/B6): 198's constant opens with a `---` separator line and closes with the
* "Output the literal result; do not output confirmations…" tail — the previous sema paraphrase drifted. */
export declare const WORKFLOW_SUBAGENT_APPEND = "---\n\nNOTE: You are running inside a workflow script. Your final text response is returned verbatim as a string to the calling script \u2014 it is your return value, not a message to a human. Output the literal result; do not output confirmations like \"Done.\" Be concise \u2014 the script will parse your output.";
/** 198 composite schema-append `L0m` (pretty.js:447328) — VERBATIM incl. the `---` opener (F6/B6; the old
* sema text compressed the "Do your work, then call X" instruction out). */
export declare const WORKFLOW_SUBAGENT_APPEND_SCHEMA = "---\n\nNOTE: You are running inside a workflow script. You MUST return your final answer by calling the StructuredOutput tool exactly once \u2014 the tool's input schema defines the required shape. Do your work, then call StructuredOutput; do NOT put your answer in a text response (the script reads ONLY the tool call). If validation fails, read the error and call StructuredOutput again with a corrected shape.";
/** A workflow agent launched as an agent type: the spec its definition folded into, and — when the definition writes tool lists — the layer
* they make in the agent's selector, with the definition's name (`RunInternals.definitionToolLists`). */
export interface WorkflowAgentTypeLaunch {
readonly spec: TaskSpec;
readonly definitionToolLists?: NonNullable<RunInternals["definitionToolLists"]>;
/** The definition the agent was launched as (#4598): its name — the word the agent's hook identity and its row frames carry
* (`RunInternals.spawnAgentType`). Absent for the default workflow subagent, however the script asked for it (no type, or
* `"general-purpose"`, its alias in this lane): one subagent, one identity. */
readonly agentType?: string;
/** Definition MCP entries shadowed by servers already on the child spec (`name_already_mounted`). */
readonly droppedMcp?: ReadonlyArray<{
readonly name: string;
}>;
}
/**
* F4 agentType: resolve `opts.agentType` against the registry
* (deployment SHADOW over built-ins) and fold the definition into the child spec — persona as
* `systemPrompt` (so {@link withWorkflowChildPersona} composes the return-contract NOTE via the
* custom-persona APPEND arm = CC `O0m` semantics), model/thinking/maxTurns/skills/memory/
* memoryPersistenceCapable when the spec didn't pin them; its allow/denyTools are not folded into the spec: they come back as the
* definition's selector layer, which the launch hands the agent on its internals ({@link WorkflowAgentTypeLaunch}).
* The definition is DEPLOYMENT-TRUSTED (registry-declared, not script-authored), so its model bypasses
* the script-facing modelName allowlist by design — same trust tier as the Agent tool's registry.
*/
export declare function applyWorkflowAgentType(spec: TaskSpec, agentType: string | undefined, registry: AgentDefinition[]): WorkflowAgentTypeLaunch;
/** The agentType registry for one workflow run: built-ins (unless opted out) with deployment SHADOW. Each deployment definition is copied
* here, whole, where the run receives it — the delegation tool's form ({@link snapshotDefinitionLists}: its lists copied, its MCP
* declarations judged by their door, a refused one refusing the run's start); the run's agents are built from these copies and the run
* holds what they name. */
export declare function workflowAgentRegistry(opts: {
agents?: AgentDefinition[];
builtinAgents?: boolean;
}): AgentDefinition[];
/**
* design/97 CORE-1 (foundation) — the STABLE, deterministic identity of one `ctx.agent` call, so the resume
* journal (CORE-7) can key a cached result and the observe tree (CORE-3/5) can hang per-agent data off it.
* `ordinal` is the call's place in the run (taken synchronously, before the `sem.acquire` await — see
* `ordinalFor` in the run body: a fresh run's calls take 0, 1, 2… in the order they are made). `position` is the
* call's place on the chain it was made on — the run's root (`c<n>`), a `pipeline` item through its stages, a `parallel`
* thunk (`….p<k>.i<item>.c<n>`, `….f<k>.t<thunk>.c<n>`), counted with the fan-outs that chain started — written
* after the hash as `@<position>`. The `@` is the mark that the key's record was written with positions: a key without one
* was written before positions existed (a resume of such a record keeps writing that form, `callKeyOf`). The hash is over
* the WHOLE behaviour-defining spec (audit fix — a cherry-pick missed images/thinking/limits/maxTokens/model
* runtime fields), with non-behavioural identity (taskId/sessionId/signal) stripped and tools/mcp reduced to
* NAMES (their closures aren't serializable). SHARED pure fn (anti-drift): the journal MUST key through this.
*/
export declare function workflowAgentCallKey(ordinal: number, spec: TaskSpec, opts: {
schema?: TSchema;
isolation?: "worktree";
}, position?: string): string;
/** The chain the code running now is on, kept: the function it answers runs `fn` on that chain again (the root when there is
* none), whatever context it is called from — only the fan-out scope is restored, every other async store stays as it is. */
export declare function captureWorkflowFanOutScope(): <T>(fn: () => T) => T;
/**
* RB-394 — runtime rejection of an unsupported `isolation` value at the workflow spawn
* boundary. The TYPE (`isolation?: "worktree"`) only binds TS callers: a workflow script is plain JS at
* runtime, so a value like `"remote"` previously sailed through every `=== "worktree"` consumption gate
* as a truthy non-match — the trusted internals channel carried it verbatim, prepareTask's fail-closed
* worktree enforcement never fired (it keys on `=== "worktree"` too), and the agent ran SILENTLY in the
* shared working tree: the exact degrade the worktree contract calls out as forbidden. The same value at
* the Agent tool boundary dies a loud schema rejection — this is that boundary's workflow-lane
* counterpart. The literal `"worktree"` and absence pass unchanged. Module-level export for the script
* membrane (`safeAgentOptions` in workflow-primitives.ts, which must REJECT rather than silently DROP the
* value — the drop re-created the identical silent outcome on the LLM-script lane); not on the package face.
*
* RB-425 — ABSENCE has more than one runtime spelling. `null` / `false` / `""` is what a wire-parsed or
* defensively-defaulted script option looks like when the field is simply NOT SET, and every downstream
* consumption gate (`=== "worktree"`, or plain truthiness at the internals-channel and advisory sites)
* already treats them identically to `undefined`. Rejecting them turned a no-op option into a hard spawn
* failure while changing nothing about where the agent would actually run — a fail-closed gate firing
* where there is nothing to close. Only a TRUTHY non-match is the hazard above: a value that LOOKS like a
* request but silently degrades to the shared tree. That still dies loud.
*
* Not a narrowing assertion: the accepted set is "worktree" plus every falsy spelling of absence, which
* no type predicate states truthfully — and no caller needs the narrowing (all three read the value with
* `=== "worktree"` afterwards).
*/
export declare function assertSupportedAgentIsolation(isolation: unknown): void;
/** One failed fan-out slot, reported through the opt-in `errors` collector of `ctx.parallel`/`ctx.pipeline`.
* The default fan-out contract is unchanged (a thrown slot folds to `null`); a script that passes a
* collector array can discriminate WHY a slot folded (budget vs schema vs other) without any change to the
* shape of the results array. */
export interface WorkflowFanOutSlotError {
/** The failed slot's index in the input array (thunk index for `parallel`, item index for `pipeline`). */
index: number;
/** A stable discriminator: the error's `code` when it carries one (typed workflow errors), else the error
* class name, else `"Error"`. */
kind: string;
/** Bounded, redacted message summary (same egress discipline as the run log). */
message: string;
}
/** Opt-in options for `ctx.parallel` (second argument) and `ctx.pipeline` (trailing argument after the
* stages). Purely observational — omitting it keeps the exact legacy behavior. */
export interface WorkflowFanOutOptions {
/** Caller-supplied array; the engine pushes one {@link WorkflowFanOutSlotError} per failed slot (including
* budget-dropped slots, which fold silently otherwise). Results still fold to `null`. */
errors?: WorkflowFanOutSlotError[];
}
/** design/98 §D.6 hard cap: thrown when the script's returned result exceeds `maxResultChars` (a runaway
* script must not return an unbounded payload to the originator). `size` is the serialized length, `Infinity` when the result cannot be serialized. */
export declare class WorkflowResultTooLargeError extends Error {
readonly size: number;
readonly max: number;
readonly code = "workflow.result_too_large";
constructor(size: number, max: number);
}
/**
* Thrown by `ctx.agent` / `ctx.agentStream().result()` when the journal store did not answer the append of the
* call's result within {@link RunWorkflowOptions.journalAppendTimeoutMs}. A STORE fault, not a run timeout: the
* run's `totalTimeoutMs` is independent of it, and whichever comes first speaks (the earlier deadline's error
* is the one the caller sees). The script does not run past the agent whose result may be unrecorded; it is the
* same exit a rejecting store already has. The timeout does not cancel the write: the store may still land it
* late (appends are idempotent per ordinal), so a resume of this run replays the agent if the write landed and
* re-runs it live if not.
*/
export declare class WorkflowJournalAppendTimeoutError extends Error {
readonly runId: string;
readonly ordinal: number;
readonly timeoutMs: number;
readonly code = "workflow.journal_append_timeout";
constructor(runId: string, ordinal: number, timeoutMs: number);
}
/**
* A TRUSTED, run-scoped internal channel carrying the workflow **nesting depth** across a process boundary
* (design/98 §0.1 BLOCKER3). It is NOT a `TaskSpec` field and NOT a `run_workflow` tool param — a worker or
* an LLM-authored script can never set it. When a deployment (e.g. the service) initiates a workflow on
* behalf of a parent run that is itself inside a workflow, it passes `workflowDepth = parentDepth + 1` here
* so the entry guard fires cross-process. In-process nesting needs no internals — the
* {@link workflowDepthStore} AsyncLocalStorage propagates depth into every `runner.runTask` child
* automatically. `startWorkflow` takes depth = `internals?.workflowDepth ?? ALS.depth ?? 0`.
*/
export interface WorkflowInternals {
workflowDepth?: number;
/** The tool selector layers of the prepared run that launched this workflow (`ToolExecuteContext.toolSelector.layers`), threaded into
* EVERY spawned agent as its ceiling (`RunInternals.toolCeiling`) on both spawn legs. The run that holds them judged them where it
* wrote them, so they are not asked again here; a ceiling a host writes itself is {@link RunWorkflowOptions.toolCeiling}, which is.
* Given both, both bound every agent (these layers first) and only the host-written one is judged. */
toolCeiling?: readonly import("../core/tool-face.js").ToolSelectorLayer[];
/** The Workflow tool whose call starts this run (`createRunWorkflowTool`'s product; its own execute hands itself). The run holds, on
* the seat of the runner it runs its agents on, every reference that tool carries — the governance baseline's servers, peers and agent
* definitions' servers, its registry's servers, what the tools it hands its agents carry — from this call until it can start no agent (it has ended and none
* of its calls is in flight). Looked up by identity: an object the engine did not mint carries nothing. */
launchingTool?: object;
/** The workflow's resume claim the launching Workflow tool already holds for this run: the first of its keys (the root of its tree,
* as the tool walked to it before the run existed). The run's admission takes the claim under this same key, re-takes the tree's
* other key once it reads the tree under it, and refuses the resume when that tree reaches another root — a resume holds one set
* of keys, the tree's. */
resumeClaimKey?: string;
/** The run the model named, when the launching Workflow tool continues the last run of that run's line instead (`resume.asked`). */
resumeAsked?: string;
}
export interface WorkflowBudget {
/** The token ceiling, or null when none was set (then `remaining()` is Infinity). */
readonly total: number | null;
/** Cumulative tokens spent so far (own + nested across all agents). AUTHORITATIVE terminal accounting
* only: it advances when an agent attempt SETTLES. The per-turn runtime usage beat feeds the
* `run.stats` observation face (monitor rows move while a long agent runs) but never charges the
* budget gate — a provisional figure that may be rolled back must never have gated a spawn. */
spent(): number;
/** `max(0, total - spent())`, or Infinity when no budget was set. */
remaining(): number;
}
export interface WorkflowAgentOptions {
/** Display label for this agent-run (else `agent-N`). */
label?: string;
/**
* F4 (DEFER 解除 2026-07-11 — 前提「无 agent 注册表」被
* 1.262.0 agents F1 推翻): run this agent AS a named agent type from the deployment's registry
* (built-in Explore/Plan + `RunWorkflowOptions.agents`, deployment SHADOW wins on a name collision —
* the same registry the Agent tool's `subagent_type` resolves). The definition supplies persona
* (composed with the workflow return-contract NOTE — CC `O0m` append semantics), model (tier words OK,
* the catalog is tier-expanded), thinking, maxTurns, skills, memory and the allow/deny tool policy.
* An unknown name throws a teaching error listing the known types. Slot-tools carrier (design/140
* ②-3): a collab member slot's tool surface = its agent type's allow/denyTools — no new mechanism.
*/
agentType?: string;
/** Explicitly group this agent under a phase title (use this inside `parallel`/`pipeline` where the
* global enclosing-phase state races — design/97 §D race note). */
phase?: string;
/** Per-agent cancellation; falls back to the workflow signal. */
signal?: AbortSignal;
/**
* Force a STRUCTURED result for this sub-agent (design/98 §0.2). Sets the child task's `outputSchema` to
* this typebox schema; the validated object is surfaced as `TaskResult.structuredOutput`. **Strict**: if
* the child COMPLETES without a `structuredOutput` (the model answered in prose — core permits that),
* `ctx.agent` throws {@link WorkflowAgentSchemaError} rather than silently returning `undefined`. A child
* that ends non-`completed` is returned as-is (the script checks `status`). If the script's `spec` already
* carried an `outputSchema`, this option wins.
*/
schema?: TSchema;
/**
* design/97 CORE-6: run this agent in an ISOLATED git WORKTREE so parallel agents editing the same repo
* don't conflict. A SCRIPT-FACING OPTION (NOT a TaskSpec field — an untrusted spec can't self-select an env,
* design/44 §7 Q4): threaded via the TRUSTED RunInternals.isolation to the control-plane executionEnvFactory,
* which mints a worktree-rooted env (e.g. {@link addWorktree}). The worktree is INSIDE the one configured env
* (local OR a single E2B) — not a per-agent container. ISOLATE-ONLY: the runtime never merges; the
* orchestrator script reads each worktree's result and decides verify/merge in userland. Requires a
* worktree-capable `executionEnvFactory` — FAIL-CLOSED: a request the deployment cannot honor fails the
* spawn at prepare time (the child never starts; no silent fallback to the shared working tree, which
* would be unsafe under a relaxed `WorkflowGovernanceBaseline.worktreeBase`). Durable-suspend is
* incompatible (the factory env is non-remote).
*/
isolation?: "worktree";
}
/**
* design/97 CORE-5 (#6 steer) — the handle returned by {@link WorkflowRunContext.agentStream}: a STILL-RUNNING
* agent the launcher can STEER mid-flight and then await. The recording (callKey/groupId/prompt + stats/output +
* `agent_end`) and the concurrency slot are tied to the underlying stream settling (an eager completion), NOT to
* `result()` being called — so a script that defers `result()` still records and releases. ⚠️ EXCEPTION (audit
* MAJOR): if the script returns and the run FINALIZES before the stream settles, the eager completion hits the
* finalized-guard and skips the trailing record — so a fire-and-forget stream the body never awaits may be left
* as `running`. A leader that wants the outcome (the normal case) awaits `result()`, which is fully recorded.
*/
export interface WorkflowAgentHandle {
/** The enclosing workflow run id (NOT a per-agent id — agentStream does not mint a sub-run). */
runId: string;
/** This agent's display label. */
label: string;
/** This agent's deterministic {@link workflowAgentCallKey}. */
callKey: string;
/**
* Inject an operator/leader STEER into the running worker (design/47 `TaskStream.steer`). The content is
* FENCED (untrusted data — it can't pose as authority) inside a TRUSTED framing that asks the worker to TAG
* its reply with the returned marker. Returns that MARKER so the launcher can correlate the worker's tagged
* reply via the #5 transcript (the worker self-stamps; reply is best-effort). One-directional + leader-driven
* (the worker can't address the leader except by the marker). A steer issued BEFORE the worker's loop goes
* live is parked and delivered into the NEXT turn's context (birth-window delivery, bounded — ledger item 36); steers are
* delivered in call order, the birth window included. Rejects with `steering.not_running` once the task has
* finished (teardown included) — from the start on a handle a resume settled without running it (replayed from
* the journal, or settled at its parked ordinal).
*
* `opts.inputId` is a PASS-THROUGH of the underlying `TaskStream.steer` correlation/idempotency key
* (design/171 §6.3 — its whole contract, value domain and typed refusals are that verb's; absent ⇒ byte-identical
* to every pre-existing call). A launcher whose own ingress already minted a message id passes it here so the two
* legs of one steering ingress are equally replay-safe. ⚠️ ONE arm of that contract is NOT reachable through this
* handle, deliberately: the idempotent-REPLAY arm ("same id, same payload ⇒ injects nothing"). Each call mints a
* FRESH unguessable correlation marker into the framing it delivers, so a retry under the same id is a
* same-id-DIFFERENT-instruction call and refuses typed `steering.duplicate_input_id` (`detail.differs` always names
* `text` on this lane). What the key buys on this lane is therefore AT-MOST-ONCE: a retried ingress is refused
* LOUDLY instead of injecting a second copy.
*/
steer(content: string, opts?: {
inputId?: string;
}): Promise<string>;
/** Await the agent's {@link TaskResult} (the same value the eager recording used; idempotent) with the run's reading of how the
* member ended (`member`, the row's outcome). On a resume that replayed this call it is the journaled result, settled at once. */
result(): Promise<WorkflowAgentResult>;
/** #4760 — stop this member (by `parent`, the handle's holder): {@link WorkflowHandle.stopAgent} on its call key — its leg is
* signalled to stop and `result()` resolves with a `killed` member naming `parent` (a member of a run already cancelled names
* the run's stopper, as `stopAgent` answers); the run goes on. Optional: a handle a host
* assembles itself need not answer. */
stop?(): Promise<WorkflowAgentStopReceipt>;
}
export interface WorkflowRunContext {
readonly runId: string;
readonly budget: WorkflowBudget;
/** The composed workflow abort signal (caller signal + `cancel()` + `totalTimeoutMs` deadline). A script
* runner (the hard sandbox) should thread THIS into its execution so a cancel/timeout actually aborts the
* script body — not just the agents it spawns (workflow review, caps). */
readonly signal: AbortSignal;
/** Run one `runner.runTask(spec)` as a recorded agent-run (script-driven, NOT an LLM delegation). Returns
* the TaskResult (any terminal status — it does not throw on a task-level failure). Throws on workflow
* abort or budget exhaustion before spawning.
*
* F5 terminal semantics vs the CC anchor (206-pretty.js:17488900-17489600), verified 2026-07-12:
* - stall-retry EXHAUSTION **throws** `agent stalled on all N attempts…` — CC throws here too (null is
* NOT the stall terminal in CC). The thrown {@link WorkflowAgentStalledError} carries the last
* attempt's TaskResult (when one was produced) so partial output and the session handle survive.
* - CC's `agent() → null` legs are user-skip and a terminal API error (post inner query-layer retries).
* This seam has no user-skip, and a terminal API death surfaces as a **`status:"failed"` TaskResult**
* (the script checks `status`) — the seam adaptation of CC's null; recorded divergence, not a gap.
* - inside `parallel`/`pipeline` a THROWN leg folds to `null` (F11/B8), so the CC-taught
* `.filter(Boolean)` idiom still holds at the fan-out level.
* The value carries `member` — how this member ended, the row's outcome (the same object the completion frame and the notifier
* read). The two refusals the run makes itself still throw (the budget, a structured result not delivered), and their errors
* carry the same `member`. */
agent(spec: TaskSpec, opts?: WorkflowAgentOptions): Promise<WorkflowAgentResult>;
/** design/97 CORE-5 (#6): spawn a STEERABLE agent — same recording/caps/budget as {@link agent}, but returns a
* {@link WorkflowAgentHandle} the launcher can `steer()` mid-flight and then `await result()`. Resolves once
* a concurrency slot is acquired and the underlying stream has started (so `steer` won't hit a not-running
* task). Use for long-running workers a leader wants to redirect; use `agent` for fire-and-await. */
agentStream(spec: TaskSpec, opts?: WorkflowAgentOptions): Promise<WorkflowAgentHandle>;
/** Run thunks concurrently (capped by the workflow concurrency limit via `agent`). A thunk that throws
* resolves to `null` — filter before use. BARRIER: awaits all. Opt-in: pass `{ errors }` (a caller-owned
* array) to receive one {@link WorkflowFanOutSlotError} per folded slot — the results shape is unchanged. */
parallel<T>(thunks: Array<() => Promise<T>>, opts?: WorkflowFanOutOptions): Promise<Array<T | null>>;
/** Run each item through all stages independently, NO barrier between stages. Each stage receives
* `(prevResult, originalItem, index)`. A stage that throws drops that item to `null`. Opt-in: a trailing
* non-function `{ errors }` argument (after the stages) collects one {@link WorkflowFanOutSlotError} per
* folded item — the results shape is unchanged. The options bag must be the LAST argument (fidelity
* R4-5: the tuple type rejects other positions at compile time, and the runtime throws on them too —
* a mid-position bag used to be silently invoked as a stage, folding every item to null). */
pipeline<I>(items: I[], ...stagesAndOpts: Array<(prev: any, item: I, index: number) => Promise<any>> | [...Array<(prev: any, item: I, index: number) => Promise<any>>, WorkflowFanOutOptions]): Promise<any[]>;
/** Group work under a named phase (observability). TWO forms (CC-心智兼容):
* - scoped `phase(title, body)` — `body`'s agents default to this phase; closes when body settles.
* - bare `phase(title)` (CC marker style) — subsequent agents group under `title` until the NEXT
* `phase()` call, the end of the enclosing scoped `phase()` body (T2A-10: a marker never leaks its
* lexical scope), or the run's end — whichever comes first. Original contract line follows:
* `phase()` call (which closes it) or the run's end. Returns `undefined`. */
phase<T>(title: string, body?: () => Promise<T>): Promise<T | undefined>;
/**
* design/97 CORE-3: run `body` as a nested SUB-WORKFLOW — IN-PROCESS over the SAME ctx (shares budget / caps
* / abort / the cumulative agent counter / the run object), under a fresh sub-`groupId` so the observe tree
* can render it. It does NOT mint a new runId (it is NOT `startWorkflow`, so it never trips the run_workflow
* nesting guard). Agents/phases spawned inside carry the sub-group's id. Group nesting is depth-capped.
*/
workflow<T>(body: (ctx: WorkflowRunContext) => Promise<T>): Promise<T>;
/** Emit a narrator log line (observability only — not stored on the run). */
log(message: string): void;
}
/**
* #642 — one decision a resume carries for a PARKED ordinal of the prior run ({@link RunWorkflowOptions.parkedResume}):
* the checkpoint token the wa* row is parked on (the row's `parkedCheckpointToken`, the redemption key), the
* caller's decision for it (the same {@link ResumeOutcome} a top-level `runner.resume` takes), and the optional
* cross-process re-supply of the checkpoint's opaque parent constraints. The token is matched by string equality
* against the parked ordinal's token — a pending checkpoint no entry names re-parks, unless it parked on a scheduler's
* yield (`resource_limit` / `preempt`): nobody decides a yield, so a pending one is driven with `continue`.
*
* `outcome` is REQUIRED: an entry is a decision, and an entry without one is refused at the door that takes it
* (`config.invalid_argument`). A pending checkpoint is driven with it. A checkpoint the store no longer calls pending
* (resolved / expired / absent) is settled as a failed row (`workflow.park_not_pending`) and the script continues — with
* or without an entry for its token, and without reading the outcome: no decision can act on it.
*
* `decisionId` is the host's own opaque id for the decision (one per decision a person made). Present ⇒ what came of the decision is
* accounted on the run's `WorkflowRun.decisions`, whichever door it came through — handed to a resume, or delivered to the live run
* ({@link redeemWorkflowParks}) (#5005, #4833) — so a decision whose drive was refused is spent: a resume does not carry it twice,
* and the live run answers it `spent` instead of driving it again. Absent ⇒ it is not accounted.
*/
export interface WorkflowParkedResume {
token: CheckpointToken;
outcome: ResumeOutcome;
inheritedGate?: InheritedGate;
decisionId?: string;
}
export interface RunWorkflowOptions {
/** Trusted caller-supplied run id. Used by the unified task registry so tool-launched workflows expose
* `task_id === runId` (`w*`) without making the model choose an id. Omit for the legacy UUID mint path. */
runId?: string;
/** design/97 S1b/S1c — persist the {@link WorkflowRun} to a {@link WorkflowRunStore} for `/workflows`
* history + cross-replica visibility. **Opt-in (default none)**: with no store the workflow still runs
* + emits + is subscribable in-process (S1a behavior, unchanged). Persistence is **best-effort**: a store
* throw is swallowed and NEVER breaks the workflow (the run/script is the source of truth; the store is an
* observation layer). The owner writes its record while it runs (rev CAS); after it has finished, a later
* continuation of its chain writes it once more for each park that continuation comes to stand on without
* reaching it (the entry's `passedTo`, rev CAS), and the engine's continuation of it writes it to record a decision it will never
* use (a park entry's `settledDecisions`, rev CAS) — so the owner is not the record's only writer. The owner's own writes carry no
* `update` cause; those writes carry `cause: "park_pass"` / `"park_decision_settled"` (see {@link WorkflowRunStore.update}). */
store?: WorkflowRunStore;
/** Where the run's full record (its result and every agent's row) is written at its terminal, for `ReadToolResult` to
* read back: the engine writes it before `run_end` and records the outcome on {@link WorkflowRun.recordArtifact}
* (`stored` with its ref, `unconfirmed`, or — with no seat — `no_store`). The write is bounded
* ({@link WORKFLOW_STORE_WAIT_BOUND_MS}); a run store is not needed for it. */
taskArtifacts?: TaskArtifactSeat;
/** Tenant/grouping key for the run when its agents run as nobody: a {@link parentSpecSeat} that names an identity keys the run
* on that identity's isolation scope (the scope its agents' work lands in), and this is read only for a run with none
* (default {@link ANONYMOUS_SCOPE}, `"default"`; `""` is no scope). Given beside such a seat it must name the same scope, else the
* run is refused before anything is written (`config.invalid_argument`). */
scope?: string;
/** design/99 MF-W: the workflow's display name + description (the run_workflow tool passes the script's
* `meta.name`/`meta.description`) — recorded on the {@link WorkflowRun} for the /workflows monitor header. */
name?: string;
description?: string;
/** Recorded on the {@link WorkflowRun} as {@link WorkflowRun.editedFrom}: the saved workflow the run's script was edited from. */
editedFrom?: import("./workflow-types.js").WorkflowEditedFrom;
/** design/114 #3 — the host task id that started this workflow → recorded on the {@link WorkflowRun} so a
* crash-recovered consumer can re-associate the run with its initiator (the run_workflow tool passes it). */
sourceTaskId?: string;
/** The originating session id — recorded on the {@link WorkflowRun}(same rationale as
* sourceTaskId: live notify carries it, recovery otherwise can't). */
originatingSessionId?: string;
/** the ROOT host session of the delegation tree (the run-workflow tool passes
* `ctx.rootSessionId ?? originatingSessionId`); rides the wa* observer frames so a recovery face
* groups workflow-agent rows under the root host session too. */
rootSessionId?: string;
/** The occupant (`SessionMetadata.incarnation`) of {@link rootSessionId}'s row as the host tree holds it; rides the
* shared internals base beside the root anchor (`RunInternals.rootSessionIncarnation`). Absent = unknown. */
rootSessionIncarnation?: string;
/** The scope {@link rootSessionId}'s durable box mounts in, beside its occupant (`RunInternals.rootSessionScope`). Absent = none recorded. */
rootSessionScope?: string;
/** The principal name of {@link rootSessionId}'s root leg (`RunInternals.rootPrincipal`): a member shares the root's session-scope rules only
* where this is a name equal to its own principal. Rides only beside a root the caller named. Absent = not stated: no member shares. */
rootPrincipal?: string;
/** design/380 O1② (C12) — the host run's EXPLICIT placement fixed point
* (`RunInternals.placementRoot`, threaded by the run-workflow tool when the host carried one):
* every workflow-spawned agent inherits it verbatim so descendants of a cascade/verify leg stay
* placed at the ladder/gate root. Absent ⇒ nothing extra travels (the rootSessionId chain is the
* placement root through prepare's mint middle segment). */
placementRoot?: string;
/** The host run's own placement target (threaded by the run-workflow tool: what the host leg's env reported as its
* workspace settled) — every workflow-spawned agent, on either spawn leg, is minted under it as its fence
* (`RunInternals.placementTarget`). Absent ⇒ the agents carry no fence. */
placementTarget?: import("../core/remote-env.js").PlacementTarget;
/** The host run tree's cross-boundary token (`RunInternals.crossBoundaryTree`, threaded by the run-workflow tool): every
* workflow-spawned agent joins the tree it names, so a bit a workflow agent sets is read by the host and its siblings. */
crossBoundaryTree?: import("../core/cross-boundary-taint.js").CrossBoundaryTreeToken;
/** The host run tree's owner seat (`RunInternals.delegationOwner`, threaded by the run-workflow tool): every workflow-spawned agent
* holds it, so a child it delegates records the host tree's owner. */
delegationOwner?: import("../core/runner/delegation-owner.js").DelegationOwnerSeat;
/** design/149 — the PROCESS-level child observer (same seam as
* {@link import("../core/types.js").RunnerDeps.onBackgroundChildEvent}). When set, every workflow
* agent emits spawn/tick/terminal {@link import("../core/types.js").BackgroundChildEvent} frames with a
* synthetic `wa*` row key (runId+callKey hash — stable across resume replays), owner/parentTaskId = the
* workflow's own task id (the fleet row nests under the w* row), sessionScoped false. This lane is
* process-anchored — a host run that settles BEFORE the workflow finishes does not mute it (the
* run-leg uuid tick lane's settledLeg freeze). Swallow-guarded.
*/
onBackgroundChildEvent?: (event: import("../core/types.js").BackgroundChildEvent) => void;
/**
* What this run is launched with ({@link WorkflowLaunchRecord}: the resolved script, the merged args, the call's
* arguments as its gate saw them) — written ONCE to {@link journalStore} (`recordLaunch`) before the script body runs,
* so a continuation of this run reads back exactly what it started with. REQUIRES a journal store that keeps launch
* records (`journalKeepsLaunches`; refused at entry otherwise). A write that does not land inside
* {@link WORKFLOW_STORE_WAIT_BOUND_MS} leaves the run running but not parking: the host's park opt-in
* ({@link defaultDurableApproval}) is not applied to its children, and the run log says so. The Workflow tool passes it.
*/
launch?: WorkflowLaunchRecord;
/** The digest of the host fold this run's calls were folded under (`workflowHostFoldDigest`), read once at the run's
* terminal and recorded as {@link WorkflowRun.hostFoldDigest}. A getter that throws records none. The Workflow tool
* passes it; NOT call identity. */
hostFoldDigest?: () => string | undefined;
/** Cancels the whole workflow: propagated to each agent's runTask; new agents short-circuit once aborted. */
signal?: AbortSignal;
/**
* F4 agentType registry (2026-07-11): deployment agent definitions resolvable by `agent(…, {agentType})`.
* Same SHADOW rule as the Agent tool: a deployment definition whose name collides with a built-in
* (Explore/Plan) wins. The run_workflow tool threads the deployment's `agents` here.
*/
agents?: AgentDefinition[];
/** `false` removes the built-in Explore/Plan from the agentType registry (mirror of the Agent tool's
* `builtinAgents:false`, CC `CLAUDE_AGENT_SDK_DISABLE_BUILTIN_AGENTS` analog). Default true. */
builtinAgents?: boolean;
/** Token ceiling — `ctx.agent` throws {@link WorkflowBudgetExceededError} once spend reaches it. */
budget?: number;
/** Max concurrent agents (default `min(16, cpus-2)`, floor 1). Validated fail-loud, like the three
* §D.6 hard caps below (CLS-A-5) — a non-finite ceiling would silently remove itself. */
concurrency?: number;
/** design/98 §D.6 hard cap — max CUMULATIVE agents the workflow may spawn (LLM-authored bound). `ctx.agent`
* throws {@link WorkflowMaxAgentsError} once reached. Undefined ⇒ only `budget`/`concurrency` bound it. */
maxAgents?: number;
/** design/98 §D.6 hard cap — whole-workflow wall-clock deadline (ms). An internal AbortController cancels
* the run at the deadline (agents short-circuit as aborted; the run finalizes `failed`). */
totalTimeoutMs?: number;
/** design/98 §D.6 hard cap — cap on a single `log()` message length (chars); longer messages are truncated
* with an honest marker (a runaway script must not flood the log). */
maxLogChars?: number;
/** design/98 §D.6 hard cap — cap on the script's RETURNED result size (chars, JSON-serialized). Exceeding
* finalizes the run `failed` with {@link WorkflowResultTooLargeError}. */
maxResultChars?: number;
/** Progress sink (run/phase/agent/log/run_end events). */
onEvent?: (e: WorkflowEvent) => void;
/** RB-473 batch 2b — bounded disclosure when a host sink (`onEvent` / the bg-child event sink) throws
* inside this run's isolation scope: first failure per site, later ones counted only. */
onNotifyError?: (failure: import("../core/safe-notify.js").SafeNotifyFailure) => void;
/** Injected clock (design/87 §4.3 — testable without the wall clock). Default `Date.now`. */
now?: () => number;
/**
* design/97 CORE-7 — the LOAD-BEARING resume journal. With a `journalStore`, every agent result — `ctx.agent` and
* `ctx.agentStream` alike — is recorded keyed by its deterministic callKey. Supplying `resumeFromRunId` (a PRIOR run
* id, requires `journalStore`) replays the longest UNCHANGED PREFIX of those results instantly and runs only the
* first changed/new call + everything after it live (same script + args → 100% cache hit). A replayed
* `ctx.agentStream` call hands back an already-settled handle: `result()` is the journaled result, `steer()` is
* refused `steering.not_running` (nothing is running). A `journalStore` WITHOUT `resumeFromRunId` just records (so
* a future run can resume from THIS run). Replayed agents accumulate into `run.stats` (total work) but NOT into
* `ctx.budget.spent()` (live spend only — a resume is not re-charged for cached work). Sequential calls,
* `parallel(direct thunks)` and `ctx.pipeline` replay; every call is matched by its position — its place on the chain
* it is made on (the run's root, a pipeline item through its stages, a parallel thunk), counted with the fan-outs that
* chain started (#4388) — not by the order sibling slots finish in. What stops the replay at a fan-out: a pipeline
* item's call at a position the resumed run never recorded, a fan-out that ends without a call the resumed run made at
* its place, and a call made on the run's root chain while a pipeline runs (its place depends on how far the pipeline
* had got when the script runner lost the engine's context).
*/
journalStore?: WorkflowJournalStore;
/**
* How long (ms) a call waits for the journal store to answer ONE append before it is refused with
* {@link WorkflowJournalAppendTimeoutError} (`workflow.journal_append_timeout`). Default
* {@link WORKFLOW_JOURNAL_APPEND_TIMEOUT_MS}. One bound for both spawn lanes (`ctx.agent` and
* `ctx.agentStream().result()`) and every awaited append (a settled result, a refused call's failed
* entry, an oversize tombstone, a park record). Must be a finite number > 0 (a bad value is refused before the
* run starts, never replaced by the default).
*
* Independent of `totalTimeoutMs`: this one names a dead STORE, that one ends the RUN. Whichever deadline is
* earlier speaks — a run with no `totalTimeoutMs` is bounded by this alone, one whose `totalTimeoutMs` is
* shorter finalizes with the run timeout first (the late append fault is then silent). The timeout does not
* cancel the write; see {@link WorkflowJournalAppendTimeoutError}.
*/
journalAppendTimeoutMs?: number;
/** design/97 CORE-7 — resume by replaying a prior run's journal (requires `journalStore`). See `journalStore`. */
resumeFromRunId?: string;
/** The spend account the workflow hangs under — the seat of the run that started it (a tool hands on its
* `ToolExecuteContext.spendSeat` as it is) and the link. Absent ⇒ the workflow is a root of its own account. Its `budget` is
* its node's ceiling; every member spends from it. Anything that is not a seat the engine handed out is refused
* (`config.invalid_argument`). Trusted: a host or the engine's own tools set it, never a script. */
spendParent?: SpendParent;
/** The HOST task's effective working root — threaded into every spawned agent's trusted internals
* (`RunInternals.parentCwd`) so a TOC env factory roots children at the parent's cwd (CC parity;
* `isolation: "worktree"` on an individual agent wins over it). */
parentCwd?: string;
/** The HOST task's own task root — `RunInternals.callerRoot` on both spawn legs, the checkout a child's placement is judged
* against (the host's {@link parentCwd} can sit in a tree inside it). Absent: the host's root is where it works ({@link parentCwd}). */
callerRoot?: string;
/**
* The SPAWNING tool call's id (`run_workflow`'s own `ToolExecuteContext.toolCallId`) — threaded into every
* spawned agent's trusted internals (`RunInternals.parentToolCallId`) so each child's frames carry this id
* for attribution, the same contract as a `createSubagentTool` delegation. Set by the `run_workflow` tool
* wiring; a directly-started workflow (no launching tool call) leaves it unset — its children's row frames
* (start, ticks, terminal tick) still go out, without the attribution, and no id is ever fabricated.
*/
parentToolCallId?: string;
/** The HOST task's taskId — threaded as every spawned agent's `RunInternals.parentTaskId` so its
* `task_progress` ticks nest under the launching task in a live agent-tree view (same channel a
* subagent spawn threads from `ToolExecuteContext.taskId`). Optional, `parentToolCallId`'s companion. */
parentTaskId?: string;
/** design/173 §8.3 (r2-F1) — the HOST run's RESOLVED interaction posture, threaded as every
* spawned agent's `RunInternals.parentInteractionPosture` (trusted internals, never the child
* spec) so a root's per-run posture override governs its workflow children too. Set by the
* `run_workflow` tool wiring from `ToolExecuteContext.interactionPosture`. */
interactionPosture?: "interactive" | "headless";
/**
* design/383 §2.5 (rescan post-6.0.0-RC) — the HOST session's capture opt-out state, read AT
* EACH SPAWN (call-time getter, deliberately not a launch snapshot: `optedOut` is live — a
* mid-run `optOutMemoryCapture` flip must floor agents spawned after it). Threaded onto both
* spawn legs' internals as the same floor/indeterminate/queryDir/ancestor seats the subagent
* lane forwards, so a workflow child of an opted-out host commits nothing — the design's ruled
* disposition for the workflow HOST lane. Set by the `run_workflow` tool wiring (enriched ctx
* getters, else the auto-mount dep). Absent ⇒ no capture seat rides (a direct `runWorkflow`
* caller with no memory-mounted host — `originatingSessionId` still rides as `parentSessionId`
* for the record-query leg).
*/
parentMemoryCaptureState?: () => {
optedOut: boolean;
indeterminate: boolean;
controlDir?: string;
ancestors?: ReadonlyArray<{
sessionId: string;
controlDir?: string;
}>;
} | Promise<{
optedOut: boolean;
indeterminate: boolean;
controlDir?: string;
ancestors?: ReadonlyArray<{
sessionId: string;
controlDir?: string;
}>;
}>;
/** Call-time getter for the HOST run's RESOLVED Model object. A spawned agent whose
* fold chain (script spec → agentType → governance baseline) produced NO model inherits the
* parent's full object — baseUrl/key routing included — instead of falling to a string/role
* re-resolution that loses per-model routing (the 4/4-agents-404 incident: session model id
* re-resolved against the base gateway). Mirrors the subagent lane's `ctx.model` semantics. */
defaultModel?: () => import("../agents/child-model-seat.js").InheritedModelPick | undefined;
/** Call-time getter for the thinking level a spawned agent runs at when neither the script, the
* agentType definition nor the governance baseline set `thinking` — the child thinking seat's lower rungs
* (the deployment's STATED `roles.subagent.thinking`, else the host's current level), as the run-workflow
* mount folds them. Snapshotted ONCE at call entry and folded into the call identity like `defaultModel`;
* applied at the launch site AFTER the agentType fold ({@link withWorkflowChildSeats}). */
defaultThinking?: () => ThinkingLevel | undefined;
/** The HOST's per-model auth hook, inherited into every spawned agent's spec (the
* credential half of model inheritance; scripts can never set it — governance strips it — so
* this is always the host's). NOT call identity (auth, not behavior; a function, not data). */
defaultGetApiKeyAndHeaders?: TaskSpec["getApiKeyAndHeaders"];
/**
* #642 — the HOST run's `durableApproval` opt-in (value copy), inherited into every spawned agent whose
* fold left the seat unpinned (a deployment baseline that pins `durableApproval` wins; scripts can never set
* it — governance strips it). With it a workflow child's plain policy `ask` — an ask the host's approver
* answers `unavailable` for, or a durable mandate — PARKS durably instead of denying fail-closed, exactly as
* a background delegation child's does under the §7.3 park family: the wa* row records `status:"parked"` +
* the token, the run SUSPENDS (the script cannot continue past a parked call), and a resume drives the
* parked child on with the caller's decision ({@link parkedResume}). Tighten-only: a park capability,
* never a widening — the decision stays with a person. REQUIRES {@link store} (the wa* row's durable home;
* refused loudly at entry without it). Threaded by the Workflow tool from the host's trusted seat; a
* directly-started workflow passes its own. NOT call identity. REQUIRES {@link checkpointStore} as well: a
* durable approval whose checkpoint this workflow cannot read back is a park no resume can prove.
*/
defaultDurableApproval?: Exclude<TaskSpec["durableApproval"], false>;
/** The host's durable-approval opt-in was withheld from this run's agents because the workflow lacks these stores (the Workflow tool's
* arming condition; never beside {@link defaultDurableApproval}). Handed to each agent whose own seat is unpinned, on the trusted
* channel (`RunInternals.workflowParkGap`), so its refused ask's notice names them as data. NOT call identity. */
durableApprovalWithheld?: readonly import("../core/ask-unresolvable-notice.js").WorkflowParkGap[];
/**
* The HOST run's parent SPEC seat ({@link import("../core/parent-spec-seat.js").ParentSpecSeat}: principal / oneShot /
* clientContext / the frozen question face / the fs-fence widening / envFacts / the declared persistence statement),
* applied onto every spawned agent at the launch site AFTER the agentType fold by {@link applyParentSpecSeat} — the
* Task delegation lane's one law: a value the child spec carries (script, definition, baseline) WINS, the seat FILLS
* an unset one, the two removals (`oneShot: true`, `memoryPersistenceCapable: false`) land unconditionally. Threaded
* by the Workflow tool from the host's seat; a directly-started workflow passes its own. NOT call identity (host
* wiring, like the checkpoint seat). Absent ⇒ nothing is applied. The seat the Workflow tool threads carries NO
* `onQuestion`: the tool strips the host's per-request face (every agent it spawns outlives the launching request)
* and says so through {@link questionFaceStripped}; a directly-started workflow passes what its own lifetime allows.
*/
parentSpecSeat?: ParentSpecSeat;
/** The engine stripped the spawn turn's per-request question face from the seat above (pair-produced with the strip,
* `stripSpawnTurnQuestionFace`); rides every spawned agent's internals as `RunInternals.questionFaceStripped` — the
* wiring manifest's `question.wired: "stripped_bg_lane"` and the posture door's exemption read it. */
questionFaceStripped?: true;
/**
* The checkpoint store this run's children park in — the RESOLVED seat object itself (the same value the Workflow
* tool injects onto every child spec; a directly-started workflow passes the store its children park in). Its
* presence ARMS the park lane: a resume then proves every parked ordinal against `get(token)` before anything is
* dispatched (`WorkflowRun.parks` supplies the keys), and a child whose effective `checkpointStore` is a DIFFERENT
* object is refused at spawn (`config.invalid_checkpoint_store` — identity, not duck-typing: a caller-built `{get}`
* wrapper is not the seat). REQUIRES {@link store} (the park's durable row). Absent ⇒ the lane is not armed: a
* resume of a run that recorded parks is refused (`workflow.park_truth_unreadable` / `capability_not_armed`), a run
* without parks resumes as before. NOT call identity (deployment wiring, like the store seats).
*/
checkpointStore?: CheckpointStore;
/**
* #642 — the decisions a resume ({@link resumeFromRunId}) carries for PARKED ordinals of the prior run: at
* an ordinal whose journal entry is `parked` (a matching call key), the run drives the parked child's own
* resume — `runner.resumeStream(token, outcome, …)` on the pinned session, the child's spec rebuilt from
* the script call exactly as at spawn — and settles its result at that ordinal, so the script continues past
* the call it parked on. A parked ordinal whose checkpoint is still pending and that NO decision here names
* re-parks (the row and the journal say the same thing again, the run suspends again); it is never run live.
* One gate needs no decision: an ordinal parked on a scheduler's yield (`resource_limit` / `preempt`) is
* driven on with `continue` — nobody decides a yield, the run's next resume continues it. A parked ordinal
* whose checkpoint the store no longer calls pending settles failed (`workflow.park_not_pending`) and the
* script continues, whatever this list says for it. `inheritedGate` is the cross-process
* re-supply of the checkpoint's opaque parent constraints (the same seat the background lane's drive
* takes). TRUSTED: a host-supplied decision, never a script/model argument; requires `resumeFromRunId`. Read
* once at start, as `redeemWorkflowParks` reads its decisions (an entry without a token or an outcome is refused,
* `config.invalid_argument`).
*/
parkedResume?: ReadonlyArray<WorkflowParkedResume>;
/**
* The run's YIELD intent — the workflow twin of {@link TaskSpec.preemptSignal}. Raised, every leg this run has in
* flight parks at its own next clean turn boundary (`resource_limit` / `preempt`, its wa* row `parked`), and every
* leg it launches after that parks at its first one — the run then suspends on its parks
* ({@link WORKFLOW_AGENT_PARKED_ERROR_CODE}) and a resume of it ({@link resumeFromRunId}) continues them with no
* decision. Each leg (a spawn, a stall retry, a drive) is handed a FRESH signal as its own `preemptSignal`, at its
* prepare report — so a resumed leg's yield is never the one its resume entry strips. Only a run whose park lane is
* armed ({@link store} + {@link checkpointStore}) can hold a leg's park; on any other run a raised intent is answered
* by continuing, said once on the run log. The Workflow tool wires the intent of the runs it starts to the task
* registry's yield seat ({@link import("../core/task-registry.js").TaskRegistry.yieldBackground}); a directly-started
* workflow passes its own. TRUSTED host wiring, never a script/model argument; NOT call identity.
*/
preemptSignal?: AbortSignal;
/** The claim holder of the run on whose behalf the drives run (`RunInternals.holder`): every drive's resume carries it into its
* CAS (`expect.holder`), so a run whose grant was superseded applies none of the decisions above. TRUSTED, like the decisions. */
holder?: string;
/** The session tree's credential (`RunInternals.treeHolder`; TRUSTED, a non-empty string — refused at the entry otherwise): every agent
* this run starts carries it on its durable writes, and a drive's claim and a member stop carry it when no `holder` is handed. Read
* once at the entry. */
treeHolder?: string;
/** design/148 S1 — the HOST run's adopted center artifact (trusted, threaded by the Workflow tool
* from ctx): every spawned agent composes the same closure. */
parentCenterArtifactDigest?: string;
parentCenterSourceRevision?: string;
/** The launching run's display sink (`RunInternals.onForwardEvent` behind the runner's ctx wrapper:
* `task_progress` always, PLUS the children's content events — `text_delta`/`text_end`/
* `reasoning_delta`/`tool_start`/`tool_end`, UNTRUSTED-RAW: the consumer must redact — when the HOST spec set
* `forwardSubagentEvents: true`) — threaded into every spawned agent's trusted internals so the
* children's events bubble out of their isolated streams to the deployment's one sink (fleet
* footer/monitor rows). Display-only; absent ⇒ ticks stay in each child's own stream. */
onForwardEvent?: (event: TaskEvent) => void;
/** Parent effective-policy inheritance (tighten-only): the HOST task's evaluated gate chain
* (`ToolExecuteContext.inheritedGateForChildren()` — its session rules + toolPolicy/frozen onAsk +
* shellGate plus every ancestor's), threaded into EVERY spawned agent's trusted internals
* (`RunInternals.inheritedGate`) on both spawn legs (`agent()` and `agentStream()`). Set by the
* `run_workflow` tool wiring; a directly-started workflow may pass its own. Absent ⇒ children run
* under only their own gates (pre-seam behavior). TRUSTED: never a script/model argument. */
inheritedGate?: import("../core/runner/contracts.js").InheritedGate;
/** A tool selector ceiling the HOST writes, threaded into EVERY spawned agent's trusted internals as its ceiling
* (`RunInternals.toolCeiling`) on both spawn legs: nothing a workflow agent's spec names can widen it. Nobody judged what the host
* wrote, so each agent's spec is asked of it before it launches: another spelling of a tool the agent carries (a caller tool, the
* catalog, a listable delegation pool) refuses that agent — `config.tool_face_control`, `detail.reason: "near_miss"`. The ceiling of the
* prepared run that launched the workflow rides {@link WorkflowInternals.toolCeiling} instead; given both, both bound every agent and only
* this one is judged. Absent ⇒ the agents are bounded only by their own lists. */
toolCeiling?: readonly import("../core/tool-face.js").ToolSelectorLayer[];
/** RB-201 FO-3 (workflow lane) — the session's auto-mode classifier, consulted BEFORE each script-spawned
* child starts (objective + toolset + output schema, finalized). The delegation-tool lane reviews its
* children at one shared point; a workflow's children reach `runner.runTask` through this module instead,
* so they need their own review point rather than inheriting that one. Threaded by the `run_workflow` tool
* wiring from the host's `ToolExecuteContext.autoModeReview`; a directly-started workflow may pass its own.
* ABSENT ⇒ every leg here is a complete no-op (auto-mode not armed = byte-identical to before). Its `unavailable` seat
* hears a classification that did not rule (the child still starts; its record carries `unreviewed.spawn`).
* TRUSTED: never a script/model argument. */
autoModeReview?: NonNullable<import("../core/types.js").ToolExecuteContext["autoModeReview"]>;
/** The host leg's agent stations: each script-spawned child raises `agent.spawn` on the host's bus (as a workflow
* agent) before it starts — the delegation lane raises it at its own pre-launch point. ABSENT ⇒ nothing is raised.
* TRUSTED: never a script/model argument. */
agentStations?: import("../core/mods/agent-stations.js").AgentStations;
/** F7/B7 (CC pretty.js:446478): PRE-REGISTER the script's `meta.phases` as pending progress groups at run
* start — the plan is visible before execution; `phase(title)` adopts the matching pending entry. */
phases?: ReadonlyArray<{
title: string;
detail?: string;
model?: string;
}>;
/** F5/B4: override the per-agent stall watchdog (ms). Default {@link WORKFLOW_AGENT_STALL_MS}. A deployment
* may tune it; tests set it small to exercise the retry path without waiting 180s. */
stallMs?: number;
/** F5/B4: override the max stalled-attempt retries. Default {@link WORKFLOW_AGENT_MAX_RETRIES}. */
agentMaxRetries?: number;
/** F5/B4: override the throttle-degradation backoff (ms). Default {@link WORKFLOW_AGENT_THROTTLE_BACKOFF_MS}. */
throttleBackoffMs?: number;
/** F5/B4 (design/87 §4.3): injectable timers for the resilience layer (stall watchdog + throttle backoff
* sleep) — tests advance a virtual clock instead of really waiting {@link WORKFLOW_AGENT_STALL_MS}.
* Default: real unref'd `setTimeout`/`clearTimeout`. Pair with `now` for a fully virtual clock. */
timers?: WorkflowTimers;
}
export interface RunWorkflowResult<T> {
result: T;
runId: string;
run: WorkflowRun;
}
/**
* design/98 §D.4 (S8c) — the handle {@link startWorkflow} returns: the `runId` is available SYNCHRONOUSLY
* (the run row + live subscription exist before the body runs, so a caller can subscribe + return the id to an
* LLM immediately), `done` resolves/rejects exactly like {@link runWorkflow}, and `cancel` aborts the whole
* run (every agent's signal fires; new agents short-circuit).
*/
export interface WorkflowHandle<T> {
readonly runId: string;
readonly done: Promise<RunWorkflowResult<T>>;
/** Abort the whole run. `stoppedBy` names who stops it (defaults to `parent`; the model's `TaskStop` passes `manager`): the members
* the run's stop ends are `killed` by it — when this cancel is the first thing to abort the run (its deadline, its caller's signal
* or an earlier cancel that came first keeps its own word). A stopper that is not a name throws a `TypeError` and stops nothing. */
cancel(reason?: string, opts?: {
stoppedBy?: string;
}): void;
/** The run-level parked bit read off the LIVE run, now ({@link runParkedOf}): `true` while the run is still `running` and a
* leg of it is parked (on any gate); `false` otherwise, a settled run included. Synchronous and total — it reads the
* engine's own run record, nothing a script or a host provides. Optional: a handle a host assembles itself need not answer,
* and a reader treats anything but `true` as "not waiting". */
parked?(): boolean;
/** #4760 — stop ONE member by its call key (`WorkflowAgentRun.callKey`; `stoppedBy` defaults to `parent`): the member's agent is
* signalled to stop and its call RETURNS (to the script) a `killed` outcome naming `stoppedBy` — the run goes on; a member waiting
* on an approval card while the run runs has the card expired first (a decision that took it first continues it:
* `park_resume_won`). A resume replays a member this method stopped as stopped, without running it again — also one the run's
* end overtook while it unwound. Two answers say what that does not cover: a member of a run whose own signal is already
* aborted (its cancel or deadline) answers `stopped` naming the RUN's stopper (`system`, or its cancel's), not `stoppedBy` — the run's
* stop ended it, and a resume runs it again as for every member a run's cancel ended; a member that outlived the run, stopped
* after the run's journal was sealed (once `done` has settled), answers `already_terminal` — not stopped, and a resume runs it
* again. The answers: {@link WORKFLOW_AGENT_STOP_ANSWERS}. Stopping the whole run — one that waits on an approval included — is
* {@link stop}.
* Optional, like {@link parked}: a handle a host assembles itself need not answer. */
stopAgent?(callKey: string, opts?: {
stoppedBy?: string;
}): Promise<WorkflowAgentStopReceipt>;
/** #5291 — stop the WORKFLOW this run belongs to, for good (`stoppedBy` defaults to `parent`): a tombstone is written on its tree's
* claim (on the run's journal store), its runs in this process are cancelled, and every approval card of it still waiting is
* withdrawn — no run of it starts another step, whichever process continues it (a continuation is refused, `workflow_stopped`).
* `cancel()` (and TaskStop) only cancels this run: it can be continued. The answers: {@link WorkflowRunStopReceipt}. Optional,
* like {@link parked}. */
stop?(opts?: {
stoppedBy?: string;
}): Promise<WorkflowRunStopReceipt>;
/** #4851 — whether the run's record landed in its run store: `true` once the store took the create (or when no run store is
* wired: there is no record to land), `false` when it refused it or did not answer within the store wait. What is kept by the
* run's id and found through its record — the launch record, a copy of the script — is written only after `true`. Optional: a
* handle a host assembles itself need not answer, and a reader treats an absent member as `true`. */
recordLanded?(): Promise<boolean>;
}
/** Cap on items a single `parallel`/`pipeline` call accepts (matches the CC Workflow tool). */
export declare const MAX_WORKFLOW_ITEMS = 4096;
/**
* CC `MTy` (206-pretty.js:17501680; 198 `F0m`): no tool activity for this long marks the attempt STALLED
* (progress-based, unlike the governance perAgentTimeoutSec hard cap, which still bounds each attempt).
* `<= 0` DISABLES the watchdog (CC arms only `if (ae > 0)`, :17477830).
*
* DERIVED, not a number of its own (#682): an OUTER, COARSER clock must never fire before the engine's
* own. This watchdog does not re-send a model call — it aborts the attempt and restarts the whole agent
* on a FRESH session, so tool effects from before the stall can be repeated. The engine, one layer down,
* already answers a silent provider call the cheap way: it re-sends that ONE call. So the floor here is
* what a single provider call may legitimately occupy while producing NOTHING — the whole first-byte lane at the default knob
* ({@link firstByteLaneKnobMs}) plus the first-token wait; a floor (upload allowance and backoff between attempts not counted). It was a hand-written
* 180_000 and CC-same until the first-token bound rose to 600s past it (measured: the same silent call
* then produced two agent runs on two sessions instead of one run with a re-sent call), which is the
* systematic inversion a derived value cannot re-open: raise or lower the engine's bound and this follows.
*
* WHAT THIS IS NOT (measured, adversarial round — do not read the sum as a proof of ordering). The two
* clocks do not start together and this one does not reset per inner retry, so it can still fire first:
* this timer starts before the Runner prepares the attempt (an execution-env factory, a hook, a store
* read all run inside this window and outside the engine's), and the first-token timer only starts once
* headers arrive — so the margin by which the inner deadline can land AFTER this one is preparation plus
* the header wait actually spent, less the {@link STALL_FIRST_BYTE_MS} this window already budgets for it
* (instant headers and more than 300s of preparation put the first inner deadline past this timer). An engine retry does not re-arm this timer either, so two silent provider attempts
* exceed it by construction. The derivation removes the case that was guaranteed to invert; it does not
* make inversion impossible. A deployment that wants the old aggression passes `stallMs` explicitly, and
* a deployment that wants a real guarantee must set it from its own preparation budget.
*/
export declare const WORKFLOW_AGENT_STALL_MS: number;
/**
* The "took suspiciously long" half of the throttle-shaped-degradation predicate (CC `Nt`,
* 206-pretty.js:17487400, which spells it `MTy * 0.5` = 90s). It used to be computed from the stall
* window here too, and that was safe only while the window was CC's own 180_000 — the moment the window
* became derived from the engine's bounds (#682/#693) the same expression silently moved the throttle
* threshold 90s → 315s, and a 2-minute empty answer that used to earn a 45s-sleep-and-retry was simply
* returned. Two unrelated meanings were riding one number. This is the second meaning, at CC's own
* value, so the progress window can move without touching it.
*
* Still a MINIMUM with the window, not a replacement for it: a deployment that shortens `stallMs` below
* this is asking for a tighter agent, and a throttle threshold above its own stall window could never
* fire. So the effective threshold is `min(stallMs / 2, this)` — one rule, both directions.
*/
export declare const WORKFLOW_THROTTLE_SLOW_MS = 90000;
/** CC `j_d` (206-pretty.js:17501680; 198 `Mxl`): max stalled-attempt retries per `ctx.agent` call
* (initial + 5 = 6 attempts). */
export declare const WORKFLOW_AGENT_MAX_RETRIES = 5;
/** CC throttle backoff (206-pretty.js:17488618): ONE fixed 45s sleep-then-retry for a throttle-shaped
* degraded response (a flat sleep, NOT exponential backoff — and only on the FIRST attempt's result). */
export declare const WORKFLOW_AGENT_THROTTLE_BACKOFF_MS = 45000;
/** #4486 — when a run's terminal write did not land, the waits before each try again (after the run has answered; on the run's
* timers, which do not hold the process). Past the last, the write has stopped (`workflow.record_unwritten`). */
export declare const TERMINAL_WRITE_RETRY_DELAYS_MS: readonly [1000, 10000, 60000];
/**
* The default for {@link RunWorkflowOptions.journalAppendTimeoutMs}: how long a call waits for the journal store
* to answer ONE append before it is refused with {@link WorkflowJournalAppendTimeoutError}.
*
* The wait is load-bearing (the script must not run past an unrecorded agent), so the figure has to clear a
* healthy write by a wide margin: one row of at most `MAX_JOURNAL_RESULT_BYTES` to a remote, loaded database
* is seconds, never tens of seconds — a bound near that would misread a busy store as a dead one. It also has
* to be well inside the Workflow tool's 10-minute deadline, or the bound would add nothing there: a dead store
* is named after 30 s instead of silently eating the run's whole deadline. The terminal-boundary waits
* (5 s drain, 10 s each for the record write and the claim release) are shorter on purpose — they run after the
* outcome is fixed and give up into a stated answer; this one decides whether the script may continue.
*/
export declare const WORKFLOW_JOURNAL_APPEND_TIMEOUT_MS = 30000;
/** F5/B4 + design/87 §4.3 — the INJECTABLE timer seam the per-agent resilience layer schedules on (the
* stall watchdog + the throttle backoff sleep). Tests drive VIRTUAL time instead of really waiting
* 180s/45s. Default: the real `setTimeout`/`clearTimeout`, unref'd (a pending watchdog must never hold
* the process open). */
export interface WorkflowTimers {
setTimeout(fn: () => void, ms: number): unknown;
clearTimeout(handle: unknown): void;
}
export declare const workflowResumeClaimFallback: {
/** Grant (or idempotently re-confirm) the in-process claim on `(sourceRunId, scope)` for `store`. Pure
* synchronous Map read-then-write — no `await` between the check and the publish, so there is no window
* (matching the atomicity RB-354 requires of a store-level implementation) in which a second same-tick
* caller could observe the key as free. */
acquire(store: WorkflowJournalStore, input: ResumeClaimArgs): {
granted: boolean;
holder?: string;
};
/** Release a claim ONLY if `newRunId` still holds it — mirrors the store contract's `releaseResumeClaim`:
* a missing/foreign claim is a no-op, never an error (a claim already taken over by a later resume must
* never be unlinked by an earlier one's cleanup). */
release(store: WorkflowJournalStore, input: ResumeClaimArgs): void;
};
/** A resume admission claim as one attempt took it: the verdict and WHICH gate granted it (the store's `resumeClaim`, or the
* process-local fallback for a store without one), so the release goes to the same gate. */
export interface WorkflowResumeClaimVerdict {
readonly granted: boolean;
readonly holder?: string;
/** On a refusal by the store: the remaining time it answered, read once as the refusal arrived ({@link claimRemainingOf}) — what
* every refusal sentence names of when the claim lapses. */
readonly lapse?: WorkflowClaimLapse;
/** On a refusal because the workflow was stopped (the store's `ResumeClaimAnswer.stopped`): who stopped it. */
readonly stopped?: {
readonly by: string;
};
readonly fallback: boolean;
}
/**
* Take the resume admission claim on `(sourceRunId, scope)` for `newRunId` — the store's `resumeClaim` when it has one (it is
* authoritative and the only gate that sees another process), else {@link workflowResumeClaimFallback}. The one taking of the
* claim: a resume's admission takes it here, and the engine's continuation of a parked run takes it here BEFORE it starts the
* run (a claim it loses starts nothing); the run's own admission then re-takes it, which the contract grants to the same holder.
* A store that throws rejects (a store failure is not a refusal). A grant is noted as this process's (`carriesDecision`: taken for a
* continuation carrying a person's decision) until its release — what a stop of the workflow reads of this process.
*/
export declare function takeWorkflowResumeClaim(store: WorkflowJournalStore, input: ResumeClaimArgs, carriesDecision?: boolean): Promise<WorkflowResumeClaimVerdict>;
/** Take the claim under every key of `keys` in order (the root first: {@link workflowClaimKeysOf}) for `newRunId`: granted only when
* every key is; the first key refused (or a store that throws) gives back the keys already taken and answers that refusal. */
export declare function takeWorkflowResumeClaims(store: WorkflowJournalStore, keys: readonly string[], newRunId: string, scope: string, carriesDecision?: boolean): Promise<WorkflowResumeClaimVerdict>;
/** Release every key {@link takeWorkflowResumeClaims} granted (never throws). */
export declare function releaseWorkflowResumeClaims(store: WorkflowJournalStore, keys: readonly string[], newRunId: string, scope: string, fallback: boolean): Promise<void>;
/** Release a claim {@link takeWorkflowResumeClaim} granted, through the gate that granted it. Never throws; the store's own TTL is
* the backstop for a release that fails (a fallback release is a synchronous in-process write). */
export declare function releaseWorkflowResumeClaim(store: WorkflowJournalStore, input: ResumeClaimArgs, fallback: boolean): Promise<void>;
/**
* Start a workflow script and return its {@link WorkflowHandle} SYNCHRONOUSLY (design/98 §D.4): the `runId`,
* run row, and live subscription exist before the body runs — so a caller (e.g. the `run_workflow` tool) can
* subscribe + hand the id to an LLM immediately, then await `done`. Establishes a {@link WorkflowRunContext},
* runs `fn(ctx)` asynchronously, and resolves `done` with the script's value + the assembled
* {@link WorkflowRun} (status `completed`). If `fn` throws, the run finalizes `failed` (events emitted) and
* `done` rejects. `cancel(reason)` aborts the whole run. A nesting/validation violation throws SYNCHRONOUSLY
* from this call (before any handle exists). Thin composition over `runner.runTask` — no Runner changes.
*/
export declare function startWorkflow<T>(runner: RunnerSelfSeat, fn: (ctx: WorkflowRunContext) => Promise<T>, opts?: RunWorkflowOptions, internals?: WorkflowInternals): WorkflowHandle<T>;
/**
* Run a workflow to completion (design/97 S1a) — the thin await over {@link startWorkflow}: returns the
* script's value + the assembled {@link WorkflowRun}. A synchronous nesting/validation throw from
* `startWorkflow` becomes a rejected promise here (this function is `async`), preserving the original
* `runWorkflow` contract (`await runWorkflow(...)` rejects rather than throwing synchronously).
*/
export declare function runWorkflow<T>(runner: RunnerSelfSeat, fn: (ctx: WorkflowRunContext) => Promise<T>, opts?: RunWorkflowOptions, internals?: WorkflowInternals): Promise<RunWorkflowResult<T>>;