/** * Live iterator types used by the live workflow interpreter. * * These values never enter durable history. They may own callbacks, promises, * timers, or other live handles while one process is running. * * ```text * durable workflow instruction * | * v * live interpreter * | * v * Branch -> Step -> live handle * | | * | +--> discard(done) acknowledges release * v * Scope -> child branches + disposable resources * ``` */ /** Why one live branch stopped. */ export type Cause = | Readonly<{ readonly type: 'failure'; readonly failure: unknown }> | Readonly<{ readonly type: 'fault'; readonly fault: unknown }> | Readonly<{ readonly type: 'cancelled'; readonly reason: unknown }> | Readonly<{ readonly type: 'multiple'; readonly causes: readonly Cause[] }>; /** Terminal result of one live branch. */ export type Exit = | Readonly<{ readonly type: 'success'; readonly value: Value }> | Readonly<{ readonly type: 'failure'; readonly cause: Cause }>; /** Completes one Step exactly once. */ export type Resolve = (exit: Exit) => void; /** Reports whether an active Step released the live state it owns successfully. */ export type DiscardDone = Resolve; /** Requests that an active Step stop and calls `done` after release completes. */ export type StepExit = (done: DiscardDone) => void; /** Minimal branch state a Step may observe. */ export interface ChildHandle { cancel(reason: unknown): Promise; settled(): Promise; result(): Promise>; } /** * Narrow Step-facing view of the Branch that owns the current operation. * * A Step can observe cancellation, register cleanup, and spawn explicitly * supervised children. It cannot advance the generator or mutate branch state * directly, which keeps reducer transitions serialized. * * @internal */ export interface BranchView { readonly signal: AbortSignal; checkpoint(): Promise; defer(dispose: () => void | PromiseLike): void; spawn(operation: WorkflowOperation, supervision: Supervision): ChildHandle; } /** * One cancellable live wait. * * `enter()` starts the wait and returns the exact discard function for that * wait. A branch never unwinds its generator until discard calls `done`. */ export interface Step { readonly name: string; enter(resolve: Resolve, branch: BranchView): StepExit; } /** Iterator program over live Steps. */ export interface WorkflowOperation { /** Yield the wrapped instruction once and return only its successful completion value. */ [Symbol.iterator](): Generator, Value, Exit>; } /** How a parent reacts when an owned child fails. */ export type Supervision = 'fail-fast' | 'collect' | 'isolate'; /** * Pull-based adapter over a synchronous iterator, asynchronous iterator, or * ReadableStream. * * The local iterator protocol reserves generator `yield` for control Steps. * Source values therefore remain behind an explicit `next()` operation instead * of being emitted through the same channel. * * ```text * host WorkflowOperation * | * +-- yield* source.next() -> one source item * | * +-- yield* source.next() -> next source item * | * `-- Scope close ----------> source.close() * ``` */ export interface PullSource extends AsyncDisposable { next(): WorkflowOperation>; close(reason?: unknown): Promise; }