UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

130 lines (129 loc) • 9.76 kB
/** * #5156 / #5291 — stopping a whole workflow: the one rule is that whether a workflow's tree can start another step is answered by the * tree's row on the journal store's claim table alone, and a stop writes that row as a tombstone ({@link * import("../core/workflow-journal-store.js").WorkflowJournalStore.stopResumeClaim}). A run of the tree takes its claim under the same * keys ({@link workflowClaimKeysOf}) before it starts a step, so a tombstone refuses it whichever process it is in; whether a row still * binds is the store's one judgment (its expiry, or a holder process the store proved gone). * * The stop ({@link stopWorkflowTree}) answers only by what it read under its own tombstones: it writes them (root first, then the tree * id, then every other run of the tree it read — a run that later reads as a root of its own, the runs above it removed, is stopped * too), cancels this process's runs of the tree and waits for them to settle (bounded), reads the tree again, withdraws every card still * waiting on it, and reports what that second read found — the runs that appeared between the two reads (`continuedBefore`: a decision * that took the tree before the tombstone did), the records that still read `running` with no claim binding them (`unclaimedRunning`: * a process that died). A journal store without the verb stops nothing durably: the runs of this process are cancelled and the cards * withdrawn all the same, and the receipt says `durable: false`. * * The process-local tables here are what a stop reads of this process: the runs it runs ({@link holdLocalWorkflowRun}), each reached by * the record the stop read for it — the same id and the same creation — and never by its id alone (a run id names a run within one set * of stores), and the holders of the resume claims it took ({@link noteLocalClaimHolder}). A stop reaches the run whose handle stops it * by that handle. A run of the tree in this process that holds the claim — whatever it carries — has its claim turned into the * tombstone in place and is cancelled (a stop stops what runs); a claim of this process that carries a person's decision and has no * run yet (a continuation between taking its claim and starting) has won the card (`park_resume_won`); a claim of this process that * carries no decision and has no run behind it is turned in place too. A run of this process outside the tree that holds a key the * tree names (another workflow under a reused id) is never turned or cancelled: that key is not stopped. */ import type { CheckpointStore } from "../core/checkpoint-store.js"; import type { WorkflowJournalStore } from "../core/workflow-journal-store.js"; import { type WorkflowRunStore } from "../core/workflow-run-store.js"; import { MAX_WORKFLOW_STOPPER_CHARS, type WorkflowRun } from "./workflow-types.js"; /** * How a stop of a whole workflow answers ({@link WorkflowRunStopReceipt}) — its own closed set, not the member stop's (an ended tree is * stopped all the same, so there is no `already_terminal`; a card the checkpoint store does not withdraw is counted in `cardsLeft`, so * there is no `park_arbiter_unreachable`): * - `stopped` — the workflow's tree was stopped: no run of it starts another step (`durable: false` — the journal store keeps no stop: * this process's runs were cancelled and the cards withdrawn, and a later continuation is not refused); * - `not_found` — no run the caller can see (through the task registry); * - `not_local` — a run that is not one of this process's runs of the tree holds the tree's claim (another process's, or one this * process has not yet read into the tree): nothing was written (`holder`, when the store named a run); * - `park_resume_won` — a continuation of this process carrying a person's decision took the tree's claim and has not started its run * yet: the decision won the card, nothing was written; * - `partial` — the first key's tombstone was written and a later key is held by another run (`heldBy`) or its write failed: the part * under the written keys (`stoppedKeys`) starts no step; the keys from that one on (`notStoppedKeys`) were not written. */ export declare const WORKFLOW_RUN_STOP_ANSWERS: readonly ["stopped", "not_found", "not_local", "park_resume_won", "partial"]; export type WorkflowRunStopAnswer = (typeof WORKFLOW_RUN_STOP_ANSWERS)[number]; /** A whole-workflow stop's answer ({@link WORKFLOW_RUN_STOP_ANSWERS}) and what it says beside its word. */ export type WorkflowRunStopReceipt = { readonly answer: "stopped"; /** Who stopped it, read as a member stop reads it (`WorkflowAgentStopReceipt`'s `stoppedBy`, workflow-types.ts). */ readonly stoppedBy: string; /** How many of the tree's approval cards still wait after the stop withdrew them (a card the checkpoint store did not withdraw, * or every card when no checkpoint store was given): a decision for one is refused, `workflow_stopped`. */ readonly cardsLeft: number; /** How many pending cards the tree's records name that the park does not bind (the card belongs to another session, or the * record names no session for it — the binding a resume checks): not touched, not counted in `cardsLeft`. */ readonly cardsUnbound?: number; /** The runs of the tree the second read found and the first did not: a decision that continued it before the tombstone stood — * that run has run (and is ended, or was cancelled with the rest). */ readonly continuedBefore?: readonly string[]; /** The tree has no tree id (its root names a source that is gone, and an older engine wrote it): only this part was stopped. */ readonly fragment?: true; /** Runs whose records still read `running` while no claim binds them — a process that stopped without ending them. They start * no further step. */ readonly unclaimedRunning?: readonly string[]; /** The journal store keeps no stop (it has no `stopResumeClaim`): what runs here was cancelled and the cards withdrawn, but a * later continuation of the workflow is not refused. */ readonly durable?: false; } | { readonly answer: "partial"; readonly stoppedKeys: readonly string[]; readonly notStoppedKeys: readonly string[]; readonly heldBy?: string; } | { readonly answer: "not_local"; readonly holder?: string; } | { readonly answer: "park_resume_won"; readonly holder?: string; } | { readonly answer: "not_found"; }; export { MAX_WORKFLOW_STOPPER_CHARS }; /** The one reading of a stop's `stoppedBy` (a member's stop and a whole workflow's): absent ⇒ `parent`; anything but a non-blank string * of at most {@link MAX_WORKFLOW_STOPPER_CHARS} characters is refused loudly (`TypeError`, naming `door`). */ export declare function readWorkflowStopper(by: unknown, door: string): string; /** * A run this process runs, as a stop reaches it. A run id names a run within one set of stores only (another set of stores may hold a * run of the same id), so a stop reaches a run of this process by its record: the record the stop read under that id must be the run's * own — the same creation ({@link workflowRunCreationIdOf}) — and never by the id alone; the run whose handle stops is reached by that * handle. */ export interface LocalWorkflowRun { readonly runId: string; /** The creation of the run's own record (`undefined` while it has none). */ readonly creationId: () => string | undefined; readonly cancel: (reason: string, opts?: { stoppedBy?: string; }) => void; readonly done: Promise<unknown>; /** The handle the run's caller holds (a stop marks it: {@link workflowStopOf}). */ readonly handle: object; } /** Hold `run` as a run of this process until its `done` settles (a stop of its tree cancels it there). */ export declare function holdLocalWorkflowRun(run: LocalWorkflowRun): void; /** A resume claim this process was granted for `holder` (and whether it carries a person's decision) — until {@link dropLocalClaimHolder}. */ export declare function noteLocalClaimHolder(holder: string, carriesDecision: boolean): void; export declare function dropLocalClaimHolder(holder: string): void; /** The stopper of a durable stop that ended the run behind `handle` (its tree is stopped: no continuation of it is offered), if any. */ export declare function workflowStopOf(handle: object): string | undefined; /** What a stop reads and writes through. */ export interface WorkflowStopSeats { readonly runStore?: WorkflowRunStore; readonly journalStore?: WorkflowJournalStore; readonly checkpointStore?: CheckpointStore; } /** A stop's reading of the tree: `self` — the stopped run's own live record (read in place of the store's copy); `own` — the run whose * handle stops it (reached by that identity, whatever its id names elsewhere); `keysWithoutTree` — the keys when no run store is given * (the run's own claim keys). */ export interface WorkflowStopFrame { readonly self?: WorkflowRun; readonly own?: LocalWorkflowRun; readonly keysWithoutTree?: readonly string[]; } /** * Stop the workflow `runId` belongs to, in `scope`, as `stoppedBy` (already read: {@link readWorkflowStopper}). The caller judged the * caller's access before calling (no tree is read and nothing is written for a caller who may not see the run). A run store read that * fails rejects before anything is written; once a tombstone is written, every later read that fails is reported in the receipt, never * thrown. */ export declare function stopWorkflowTree(runId: string, scope: string, stoppedBy: string, seats: WorkflowStopSeats, frame?: WorkflowStopFrame): Promise<WorkflowRunStopReceipt>;