@sema-agent/core
Version:
Stateless, task-oriented AI agent core
78 lines (77 loc) • 5.85 kB
TypeScript
import type { PausedCause, TaskResult, TerminalCause } from "../core/types.js";
/**
* The **nested durable-pause boundary** — the one place that says which orchestrators map a pause they meet and how a host
* continues it; the other places that mention it point here.
*
* An orchestrator that runs its work as *nested* `runTask`s cannot answer a durable gate (an approval, a resource slice, a platform
* park, a dry-run or plan review) from the inside. The cascade (`runCascade` / `resumeCascade`) and the verification gate
* (`runWithVerification` / `resumeWithVerification`) therefore **stop** at such a pause — no escalation or re-run (side effects), and
* no release of the pinned session (the checkpoint still references it) — and hand it back through {@link mapNestedSuspend}: a
* `failed` terminal with the code `"unexpected.suspended"` (an approval / resource / platform pause) or `"unexpected.needs_review"`
* (a review pause), **the pause's own cause on the failed terminal's `nestedPause` seat**, and a sentence that names the entry that
* continues it. That entry is the orchestrator's own: `resumeCascade` for a cascade, `resumeWithVerification` for a verification
* gate. Once the pause is decided, the host calls it with `nestedPause.token` and the decision.
*
* The other orchestrators do not map: the repair loop hands the paused terminal back as is, a workflow parks the run, and a
* subagent reports `suspended.awaiting_approval`.
*
* The structural prevention is upstream — an orchestrator should run nested tasks under a **non-durable** approval policy (e.g. the
* verifier already forces `handsReadOnly` + headless auto-deny) so a suspension cannot arise. This mapping is the defense-in-depth:
* a deployment that wires durable suspend into a nested orchestrator gets a surfaced pause, not silent errors.
*/
export declare const UNEXPECTED_SUSPENDED = "unexpected.suspended";
/** design/80 D-B: the review durable-pause family (dry-run `needs_review` AND `plan_review`) hits the SAME
* nested hard boundary as an approval pause — a distinct code so a caller can tell a review pause from an
* approval. */
export declare const UNEXPECTED_NEEDS_REVIEW = "unexpected.needs_review";
/**
* design/80 D-B: the durable-pause hard boundary covers EVERY `paused` cause — an approval gate, a resource
* slice, a dry-run review, a `plan_review` plan-gate. All of them persist a checkpoint + PIN their session,
* and the decision that resumes them comes from outside the orchestrator — so it must surface the pause and
* NOT release the pinned session (releasing it orphans the checkpoint). A type guard, so the caller reads the
* token/gate off the narrowed cause.
*/
export declare function isDurablePause(terminal: TerminalCause): terminal is PausedCause;
/** A result whose terminal is known to be a pause — what {@link isDurablePause} narrows a result's
* `terminal` to, spelled as a type so an orchestrator can HOLD a paused result without re-checking. */
export type PausedResult = TaskResult & {
terminal: PausedCause;
};
/** A resumed leg that ended before its approved action started — read off the row's outcome the leg states
* (`failed.unstartedRow`), never off its code (that still names what ended the leg) or its sentence. Its checkpoint is
* pending again, or the store declined, or its state is unprovable: in none of them was the approved work done, so an
* orchestrator stops there as at a durable pause — no re-run, no escalation, no session release. */
export declare function resumeNeverStarted(result: TaskResult): boolean;
/**
* Which family a pause belongs to — its gate kind's registry row: `"suspended"` (an approval gate, a
* resource slice, a platform park) or `"needs_review"` (a dry-run or plan review). The same row the
* status word is projected from; read here by the orchestrators that name the family in their own faces.
*/
export declare function pauseFamily(t: PausedCause): "suspended" | "needs_review";
/**
* The entry that continues what {@link mapNestedSuspend} hands back, as the orchestrator that calls it names it. The entry is the
* caller's to give — the mint does not look at who is calling — and a union, so a new mapper has to name its own to compile.
*/
export interface NestedResumeEntry {
/** The orchestrator's resume entry, by its exported name. */
readonly door: "resumeCascade" | "resumeWithVerification";
/**
* Only for an entry that takes the parked leg: where the host gets it. `"card"` — the parked card records it
* (`CheckpointState.compositionLeg`); `"caller"` — the leg was dispatched with none (the task spec named no session and no leg was
* handed in), so the card records none and the host supplies the leg itself. Absent — the entry takes no leg.
*/
readonly legFrom?: "card" | "caller";
}
/**
* If `result` durably paused, return it mapped to a `failed` result whose terminal carries the pause on
* `nestedPause`; otherwise return it unchanged. The orchestrator should also **stop** (not escalate/re-run)
* and **not release** the session when this maps a pause — the checkpoint still references that session.
*
* `resume` names the entry that continues the pause; the sentence of the failed terminal is one template with that entry (and, for
* an entry that takes the parked leg, where the host gets the leg) as its slots.
*
* The mapped result keeps the parked leg's `sessionId` — the parked row's own `Checkpoint.sessionId`. A composition's legs do
* not run in the caller's session, so a host links the row to its run by the row's `CheckpointState.compositionLeg`, never by
* assuming the row sits in the session it submitted.
*/
export declare function mapNestedSuspend(result: TaskResult, resume: NestedResumeEntry): TaskResult;