@mastra/core
Version:
59 lines • 3.1 kB
TypeScript
import type { IMastraLogger } from '../logger/index.js';
import type { WorkflowsStorage } from '../storage/domains/workflows/base.js';
import type { WorkflowRunStatus } from './types.js';
/**
* A nested run is stored under its own id (one per `.foreach()` item), so its parent is the only
* thing that can collect its result. The recovery sweeps find it through the run itself and have
* to decide from the parent's stored status whether that is still possible.
*
* - `restart`: the parent can't re-drive it, so restarting it standalone is the only way forward.
* - `skip`: the parent is being driven (or will be), and its step restarts the nested run itself.
* - `cancel`: the parent is finished, so nothing will ever collect a restart of this run.
*/
export type NestedRunRecovery = 'restart' | 'skip' | 'cancel';
export declare function nestedRunRecoveryAction(parentStatus: WorkflowRunStatus | null): NestedRunRecovery;
/**
* The parent a stored run was started from, or `undefined` for a top-level run.
*/
export declare function nestedRunParentOf(snapshot: unknown): {
workflowName: string;
runId: string;
} | undefined;
/**
* The status a stored run was listed with, so a later write can compare-and-set on it.
*/
export declare function nestedRunStatusOf(snapshot: unknown): WorkflowRunStatus | undefined;
/**
* Reads parent statuses for one recovery sweep. A crashed `.foreach()` leaves one nested run per
* item, all pointing at the same parent, so a sweep that reads per run pays the parent lookup once
* per leftover item. The cache is per sweep on purpose: within one sweep the parent's status can
* only change in the window the sweep already races, and each write it makes is guarded by its own
* compare-and-set.
*
* A rejected lookup is cached too, so siblings under a parent whose read failed don't each retry a
* store that is already failing. Each of them is handled by its own try/catch, which logs the
* failure and leaves that run for the next sweep.
*/
export declare function createNestedRunParentStatusLookup({ store, }: {
store?: WorkflowsStorage;
}): (parent: {
workflowName: string;
runId: string;
}) => Promise<WorkflowRunStatus | null>;
/**
* Marks a nested run `canceled` when its parent finished without it, so the sweep stops finding it
* as an active run. Takes the run's ownership lease first: a live driver on another replica keeps
* the run, and the write must not race it. The write is a compare-and-set on the status the sweep
* listed, so a run that finished or suspended in between keeps its own result.
*
* Neither guard retries: a run another process is driving, or one whose status moved since the
* sweep listed it, stays `running` or keeps its newer status and the next sweep decides again.
*/
export declare function cancelNestedRun({ store, workflowName, runId, expectedStatus, logger, }: {
store: WorkflowsStorage;
workflowName: string;
runId: string;
expectedStatus?: WorkflowRunStatus;
logger?: IMastraLogger;
}): Promise<void>;
//# sourceMappingURL=nested-run-recovery.d.ts.map