@sema-agent/core
Version:
Stateless, task-oriented AI agent core
67 lines (66 loc) • 5.11 kB
TypeScript
import type { WorkflowRun } from "./workflow-types.js";
import { type WorkflowRunStore } from "../core/workflow-run-store.js";
import { type BoundedStoreRead } from "./workflow-park-adoption.js";
/**
* A workflow run's tree id ({@link WorkflowRun.treeId}) — where it comes from, read one way.
*
* The edge: run `b` continues run `a` ({@link isContinuationOf}) when `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 is not
* the run a continuation continued, so it is not its source — the same answer as a source that is gone.
*
* The tree id of a record: its own `treeId` when it carries one; else, up the edges, the first record that carries one; else the top
* record's id (a record that names no run it resumed). An edge that does not hold on the way up (the source gone, created again, or
* out of the scope) leaves the record with no tree id; so does a record whose resume admission never completed (it is no member of a
* tree). The walk is the one reading of the rule; a resume's admission and the stamp of an older finished record
* ({@link workflowTreeIdStampOf}) both go through it.
*/
/** A stored record's tree id member: the member when it is a string, else none. */
export declare function workflowRunTreeIdOf(run: WorkflowRun): string | undefined;
/** What the walk answers: the tree id, `"no_tree"` (an edge up does not hold), `"unreadable"` (a read failed or did not answer, or the
* walk met a run twice) or `"over_bound"` (more runs than {@link WORKFLOW_TREE_RUNS_MAX}). */
export type WorkflowTreeIdAnswer = {
readonly treeId: string;
} | "no_tree" | "unreadable" | "over_bound";
/** One read the walk asks for: a run id; the driver answers the record in the scope, `null` for none there, or `"unreadable"`. */
export type WorkflowTreeIdRead = WorkflowRun | null | "unreadable";
/**
* The walk itself, as a generator so that one rule has two drivers: it yields each run id it needs read and is handed the answer
* ({@link WorkflowTreeIdRead}). {@link treeIdOf} drives it with a store's reads; a store whose rows are in hand drives it itself.
*/
export declare function treeIdWalk(run: WorkflowRun): Generator<string, WorkflowTreeIdAnswer, WorkflowTreeIdRead>;
/** {@link treeIdWalk} driven by a store's reads in `scope`, each bounded by `within`. */
export declare function treeIdOf(store: Pick<WorkflowRunStore, "get">, run: WorkflowRun, scope: string, within: BoundedStoreRead): Promise<WorkflowTreeIdAnswer>;
/**
* What {@link workflowTreeIdStampOf} answers for one stored record:
* - `stamp` — write `update(run.id, scope, { ...run, treeId }, expect, { cause })`: the record as read with the tree id added, compared
* on the rev and the creation read; a write that loses is asked again on a fresh `get`, or left for the next pass;
* - `skip` — nothing to write: `stamped` (it carries a tree id), `running` (its owner writes it whole, and only a finished record is
* stamped), `admission_incomplete` (a continuation whose admission never completed is no member of a tree), `no_tree` (an edge up
* its line does not hold — the source gone or created again);
* - `"unreadable"` (a read up the line failed or did not answer within its bound, or the line met a run twice) / `"over_bound"` (the
* line is more runs than the walks read): this record is not stamped this pass.
*/
export type WorkflowTreeIdStamp = {
readonly kind: "stamp";
readonly treeId: string;
readonly expect: {
readonly rev: number;
readonly creationId: string | null;
};
readonly cause: "tree_id_stamp";
} | {
readonly kind: "skip";
readonly why: "stamped" | "running" | "admission_incomplete" | "no_tree";
} | "unreadable" | "over_bound";
/** The stamp decision for one record — the one rule, a generator like {@link treeIdWalk} so a store holding its rows drives it in hand. */
export declare function treeIdStampWalk(run: WorkflowRun): Generator<string, WorkflowTreeIdStamp, WorkflowTreeIdRead>;
/**
* Whether a FINISHED record an older engine wrote (no {@link WorkflowRun.treeId}) is to be given its tree id, which one, and the write
* that gives it ({@link WorkflowTreeIdStamp}) — the one reading a host's own pass over its rows calls (a store whose rows outlive an
* upgrade runs one pass, as the bundled file store does when it opens). Reads up the record's line in `scope`, each read bounded by
* `WORKFLOW_STORE_WAIT_BOUND_MS`. A running record is never stamped: its owner writes it whole. A host may leave a record unstamped
* (this release reads no tree id): such a record simply has none.
*/
export declare function workflowTreeIdStampOf(store: Pick<WorkflowRunStore, "get">, run: WorkflowRun, scope: string): Promise<WorkflowTreeIdStamp>;
/** {@link treeIdStampWalk} driven in hand: `read` answers each run id at once. */
export declare function workflowTreeIdStampInHand(run: WorkflowRun, read: (id: string) => WorkflowTreeIdRead): WorkflowTreeIdStamp;