/** * The chain-task store: the queue that turns a series of commands into one task * (ADR-0013). A chain holds step ids and a cursor; the steps are ordinary process * tasks that run one at a time. This store decides whose turn it is and reads every * outcome from the step's own record, so nothing about a step lives in two places * (ADR-0014). * * The chain is a Task like any other: same file, same status vocabulary, same id * namespace. What it adds is an order and a place to stand in it. * * @author mystery4f <40482524+mystery4f@users.noreply.github.com> */ import type { TaskScope } from "../base/runtime-state.js"; import type { ChainTask, ProcessTask } from "../base/task.js"; import type { OwnedTaskSession, TaskSessions } from "../base/task-sessions.js"; import type { ProcessTaskView, StartProcessTaskInput, StopTaskOptions, StopTaskResult } from "../process/process-tasks.js"; /** What a chain needs from the process store: queue a step, run it, stop it. */ export interface StepRunner { queueStep(input: StartProcessTaskInput): Promise; launch(session: OwnedTaskSession, id: string): Promise; stop(scope: TaskScope, id: string, options?: StopTaskOptions): Promise; } /** * One step's start, minus what a chain supplies itself. The chain owns the link and * the session; the caller owns the command and how it should run. */ export type QueueStepInput = Omit; /** A chain as it looks right now: the record, plus the time it has been alive. */ export interface ChainTaskView { task: ChainTask; /** Milliseconds from creation to end, or to now while it runs. */ elapsedMs: number; } /** The answer to queueing a step: the queue it joined and the step itself. */ export interface QueuedStep { /** The chain the step joined; a fresh one when the anchor was a task of no chain. */ chain: ChainTask; /** The step record: `waiting`, carrying the chain's id in `parentId`. */ step: ProcessTask; } /** How a chain stop answered. */ export interface StopChainResult { /** The chain after the stop. */ view: ChainTaskView; /** * `stopped` when the running step is gone, `tree-survived` when the kill could * not clear it — the chain is still ended either way, because its queue must not * advance, but the step that outlived the stop is not a clean stop. */ outcome: "stopped" | "already-finished" | "tree-survived"; /** Why the step outlived the stop, as the supervisor said it. */ detail?: string; /** The step that was running, as it ended: stopped, and no longer the cursor's job. */ step?: ProcessTaskView; } export interface ChainTasksOptions { /** * The store that runs the steps. A getter, because the two stores are built * together: the process store reports every end back into this one. */ processes: () => StepRunner; /** Shared session loader; the same instance every other store is given. */ sessions: TaskSessions; /** Clock seam for tests; defaults to `Date.now`. */ now?: () => number; /** * Called once when a chain reaches a final status. A step's end is announced by * the process store as it always was; this is the queue's own, coarser ending. */ onFinish?: (view: ChainTaskView) => void; } export declare class ChainTasks { private readonly processes; private readonly sessions; private readonly now; private readonly onFinish; constructor(options: ChainTasksOptions); /** * Queue a step behind a task or a chain (ADR-0013). The anchor may be a chain, a * step of one, or a process task of no chain — the last is adopted into a new * chain, so "queue behind this" always names a queue afterwards and a task that * belongs to no chain is never shown as a chain of one. The step record exists, * `waiting`, before this returns, and starts when the cursor reaches it. */ enqueue(scope: TaskScope, after: string, input: QueueStepInput): Promise; /** * Move a chain along after one of its steps ended. Called for every process task * that ends; a task of no chain, and a loop's fire, are not this store's business. */ advance(step: ProcessTaskView): Promise; /** Every chain of the session, oldest first, including loaded history. */ list(scope: TaskScope): Promise; /** One chain by id. Undefined when the session has no such chain. */ get(scope: TaskScope, id: string): Promise; /** * Stop a chain: the step at the cursor is stopped through the process store, and * every step still waiting is marked `stopped` — not `skipped`, because the queue * did not fail, the user ended it (ADR-0013). Undefined when the session has no * such chain; a chain that already ended is answered, not stopped twice. */ stop(scope: TaskScope, id: string, options?: StopTaskOptions): Promise; /** * The queue's next decision, taken at the cursor: launch the step whose turn it * is, or settle the chain. A new chain, a chain whose step just ended, and a chain * the user just appended to all arrive here, so one rule covers every arrival. */ private advanceChain; /** * End a chain, and tell every step that never got its turn why it did not run: a * failure skips the rest, and each skipped step names the queue and the step that * stopped it (ADR-0013). */ private end; /** * The chain an anchor belongs to, adopting a task of no chain into a fresh one. * The adoption is what makes `after: ` mean the same thing as * `after: ` a moment later, without inventing a second kind of queue. */ private chainOf; /** * A finished chain may take another step — a successful queue is a thing one can * extend — but a chain that failed or was stopped may not: its skipped steps are * history, and reviving it would quietly run work the reader was told was lost. */ private revive; /** The loaded session that holds a record, without loading anything new. */ private sessionOf; private view; }