@voyantjs/workflows 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +52 -0
- package/README.md +46 -0
- package/package.json +78 -0
- package/src/auth/index.ts +156 -0
- package/src/conditions.ts +43 -0
- package/src/handler/index.ts +361 -0
- package/src/index.ts +18 -0
- package/src/protocol/index.ts +133 -0
- package/src/rate-limit/index.ts +182 -0
- package/src/runtime/ctx.ts +838 -0
- package/src/runtime/determinism.ts +75 -0
- package/src/runtime/errors.ts +58 -0
- package/src/runtime/executor.ts +427 -0
- package/src/runtime/journal.ts +79 -0
- package/src/testing/index.ts +729 -0
- package/src/trigger.ts +146 -0
- package/src/types.ts +81 -0
- package/src/workflow.ts +306 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Deterministic clock and RNG used by the workflow body.
|
|
2
|
+
//
|
|
3
|
+
// `ctx.now()` must return the timestamp of the event currently being
|
|
4
|
+
// consumed from the journal during replay (§4.5 of docs/design.md).
|
|
5
|
+
// `ctx.random()` / `ctx.randomUUID()` are seeded from the run id so
|
|
6
|
+
// replays produce the same values.
|
|
7
|
+
|
|
8
|
+
export interface ClockState {
|
|
9
|
+
/** Base wall-clock time recorded at run start. */
|
|
10
|
+
readonly baseWallClock: number;
|
|
11
|
+
/** Offset from baseWallClock at which ctx.now() should return — set by the executor when replaying journaled events. */
|
|
12
|
+
offset: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createClock(runStartedAt: number): ClockState {
|
|
16
|
+
return { baseWallClock: runStartedAt, offset: 0 };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function now(clock: ClockState): number {
|
|
20
|
+
return clock.baseWallClock + clock.offset;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Advance the clock to the event currently being replayed. */
|
|
24
|
+
export function advanceClockTo(clock: ClockState, eventAt: number): void {
|
|
25
|
+
clock.offset = eventAt - clock.baseWallClock;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Mulberry32 PRNG — fast, fine for workflow-determinism use. Seeded
|
|
30
|
+
* from a 32-bit hash of the run id. Not cryptographic.
|
|
31
|
+
*/
|
|
32
|
+
export function seededRandom(seed: number): () => number {
|
|
33
|
+
let state = seed >>> 0;
|
|
34
|
+
return () => {
|
|
35
|
+
state = (state + 0x6d2b79f5) >>> 0;
|
|
36
|
+
let t = state;
|
|
37
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
38
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
39
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function hashSeed(runId: string): number {
|
|
44
|
+
// FNV-1a 32-bit
|
|
45
|
+
let hash = 0x811c9dc5;
|
|
46
|
+
for (let i = 0; i < runId.length; i++) {
|
|
47
|
+
hash ^= runId.charCodeAt(i);
|
|
48
|
+
hash = Math.imul(hash, 0x01000193);
|
|
49
|
+
}
|
|
50
|
+
return hash >>> 0;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createRandom(runId: string): () => number {
|
|
54
|
+
return seededRandom(hashSeed(runId));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const HEX = "0123456789abcdef";
|
|
58
|
+
|
|
59
|
+
export function createRandomUUID(rng: () => number): () => string {
|
|
60
|
+
// v4-shaped deterministic UUID. Not cryptographically random; matches
|
|
61
|
+
// the style of crypto.randomUUID but is reproducible across replays.
|
|
62
|
+
return () => {
|
|
63
|
+
const bytes = new Uint8Array(16);
|
|
64
|
+
for (let i = 0; i < 16; i++) bytes[i] = Math.floor(rng() * 256);
|
|
65
|
+
bytes[6] = (bytes[6]! & 0x0f) | 0x40; // version 4 (high nibble of byte 6)
|
|
66
|
+
bytes[8] = (bytes[8]! & 0x3f) | 0x80; // variant RFC 4122 (high bits of byte 8)
|
|
67
|
+
let s = "";
|
|
68
|
+
for (let i = 0; i < 16; i++) {
|
|
69
|
+
const b = bytes[i]!;
|
|
70
|
+
s += HEX[b >>> 4]! + HEX[b & 0x0f]!;
|
|
71
|
+
}
|
|
72
|
+
// s is 32 hex chars; slice into 8-4-4-4-12 groups.
|
|
73
|
+
return `${s.slice(0, 8)}-${s.slice(8, 12)}-${s.slice(12, 16)}-${s.slice(16, 20)}-${s.slice(20, 32)}`;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Internal runtime signal errors. Distinct from user-facing `@voyantjs/workflows-errors`:
|
|
2
|
+
// these are thrown inside the executor to unwind the workflow body on
|
|
3
|
+
// waitpoint yield or run cancellation. They must not be caught by user
|
|
4
|
+
// code (the executor re-throws if it observes one being swallowed).
|
|
5
|
+
|
|
6
|
+
const WAITPOINT_PENDING = Symbol.for("voyant.workflows.waitpointPending");
|
|
7
|
+
const RUN_CANCELLED = Symbol.for("voyant.workflows.runCancelled");
|
|
8
|
+
const COMPENSATE_REQUESTED = Symbol.for("voyant.workflows.compensateRequested");
|
|
9
|
+
|
|
10
|
+
export class WaitpointPendingSignal extends Error {
|
|
11
|
+
readonly [WAITPOINT_PENDING] = true as const;
|
|
12
|
+
readonly waitpointId: string;
|
|
13
|
+
constructor(waitpointId: string) {
|
|
14
|
+
super(`waitpoint pending: ${waitpointId}`);
|
|
15
|
+
this.name = "WaitpointPendingSignal";
|
|
16
|
+
this.waitpointId = waitpointId;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class RunCancelledSignal extends Error {
|
|
21
|
+
readonly [RUN_CANCELLED] = true as const;
|
|
22
|
+
constructor(reason?: string) {
|
|
23
|
+
super(reason ?? "run cancelled");
|
|
24
|
+
this.name = "RunCancelledSignal";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function isWaitpointPending(err: unknown): err is WaitpointPendingSignal {
|
|
29
|
+
return (
|
|
30
|
+
typeof err === "object" &&
|
|
31
|
+
err !== null &&
|
|
32
|
+
(err as { [WAITPOINT_PENDING]?: true })[WAITPOINT_PENDING] === true
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isRunCancelled(err: unknown): err is RunCancelledSignal {
|
|
37
|
+
return (
|
|
38
|
+
typeof err === "object" &&
|
|
39
|
+
err !== null &&
|
|
40
|
+
(err as { [RUN_CANCELLED]?: true })[RUN_CANCELLED] === true
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class CompensateRequestedSignal extends Error {
|
|
45
|
+
readonly [COMPENSATE_REQUESTED] = true as const;
|
|
46
|
+
constructor() {
|
|
47
|
+
super("compensate requested");
|
|
48
|
+
this.name = "CompensateRequestedSignal";
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function isCompensateRequested(err: unknown): err is CompensateRequestedSignal {
|
|
53
|
+
return (
|
|
54
|
+
typeof err === "object" &&
|
|
55
|
+
err !== null &&
|
|
56
|
+
(err as { [COMPENSATE_REQUESTED]?: true })[COMPENSATE_REQUESTED] === true
|
|
57
|
+
);
|
|
58
|
+
}
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
// The workflow-step executor. Takes a registered workflow + journal and
|
|
2
|
+
// runs the body exactly once, yielding a response envelope.
|
|
3
|
+
//
|
|
4
|
+
// Corresponds to the tenant-side handler of POST /__voyant/workflow-step
|
|
5
|
+
// described in docs/runtime-protocol.md §2.1.
|
|
6
|
+
|
|
7
|
+
import type { RunTrigger, RunStatus, WaitpointKind } from "../types.js";
|
|
8
|
+
import type { SerializedError } from "../protocol/index.js";
|
|
9
|
+
import type { WorkflowDefinition, StepOptions } from "../workflow.js";
|
|
10
|
+
import type { JournalSlice, StepJournalEntry } from "./journal.js";
|
|
11
|
+
import { createClock, createRandom } from "./determinism.js";
|
|
12
|
+
import { buildCtx, type RuntimeCallbacks, type RuntimeEnvironment } from "./ctx.js";
|
|
13
|
+
import {
|
|
14
|
+
WaitpointPendingSignal,
|
|
15
|
+
RunCancelledSignal,
|
|
16
|
+
isWaitpointPending,
|
|
17
|
+
isRunCancelled,
|
|
18
|
+
isCompensateRequested,
|
|
19
|
+
} from "./errors.js";
|
|
20
|
+
import { durationToMs, type RateLimiter } from "../rate-limit/index.js";
|
|
21
|
+
|
|
22
|
+
export interface StepRunner {
|
|
23
|
+
/**
|
|
24
|
+
* Executes a step body and returns the journal entry to record.
|
|
25
|
+
*
|
|
26
|
+
* In-process runners (the default edge runner, local-dev passthrough)
|
|
27
|
+
* call `fn(stepCtx)` directly. Dispatching runners (e.g. the CF
|
|
28
|
+
* Container runner) ignore `fn` — they can't serialize a closure —
|
|
29
|
+
* and use the run/workflow identity + step options to address the
|
|
30
|
+
* remote container and POST the required context.
|
|
31
|
+
*/
|
|
32
|
+
(args: {
|
|
33
|
+
stepId: string;
|
|
34
|
+
attempt: number;
|
|
35
|
+
input: unknown;
|
|
36
|
+
fn: (stepCtx: import("../workflow.js").StepContext) => Promise<unknown>;
|
|
37
|
+
stepCtx: import("../workflow.js").StepContext;
|
|
38
|
+
/** Identity of the run — used by dispatching runners. */
|
|
39
|
+
runId: string;
|
|
40
|
+
workflowId: string;
|
|
41
|
+
workflowVersion: string;
|
|
42
|
+
/** Project / organization id from the runtime environment — used by
|
|
43
|
+
* dispatching runners to resolve per-tenant bundle storage keys. */
|
|
44
|
+
projectId: string;
|
|
45
|
+
organizationId: string;
|
|
46
|
+
/** Merged step options (runtime, machine, timeout, …). */
|
|
47
|
+
options: import("../workflow.js").StepOptions<unknown>;
|
|
48
|
+
/**
|
|
49
|
+
* Current journal slice at dispatch time — steps already completed,
|
|
50
|
+
* waitpoints already resolved, etc. Dispatching runners pass this
|
|
51
|
+
* to the remote executor so body replay there short-circuits on
|
|
52
|
+
* cached steps, and the container can stop cleanly after the
|
|
53
|
+
* target step runs.
|
|
54
|
+
*/
|
|
55
|
+
journal: JournalSlice;
|
|
56
|
+
}): Promise<StepJournalEntry>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface WaitpointRegistration {
|
|
60
|
+
clientWaitpointId: string;
|
|
61
|
+
kind: WaitpointKind;
|
|
62
|
+
meta: Record<string, unknown>;
|
|
63
|
+
timeoutMs?: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface MetadataMutation {
|
|
67
|
+
op: "set" | "increment" | "append" | "remove";
|
|
68
|
+
key: string;
|
|
69
|
+
value?: unknown;
|
|
70
|
+
target?: "self" | "parent" | "root";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface CompensationReport {
|
|
74
|
+
stepId: string;
|
|
75
|
+
status: "ok" | "err";
|
|
76
|
+
error?: SerializedError;
|
|
77
|
+
durationMs: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface StreamChunk {
|
|
81
|
+
streamId: string;
|
|
82
|
+
seq: number;
|
|
83
|
+
encoding: "text" | "json" | "base64";
|
|
84
|
+
chunk: unknown;
|
|
85
|
+
final: boolean;
|
|
86
|
+
at: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface ExecuteWorkflowStepRequest {
|
|
90
|
+
runId: string;
|
|
91
|
+
workflowId: string;
|
|
92
|
+
workflowVersion: string;
|
|
93
|
+
input: unknown;
|
|
94
|
+
journal: JournalSlice;
|
|
95
|
+
invocationCount: number;
|
|
96
|
+
environment: RuntimeEnvironment;
|
|
97
|
+
triggeredBy: RunTrigger;
|
|
98
|
+
runStartedAt: number;
|
|
99
|
+
tags: string[];
|
|
100
|
+
abortSignal?: AbortSignal;
|
|
101
|
+
/**
|
|
102
|
+
* Default step executor (the "edge" runtime) — runs step bodies
|
|
103
|
+
* in-process. Used for any step whose `options.runtime` is unset or
|
|
104
|
+
* explicitly `"edge"`.
|
|
105
|
+
*/
|
|
106
|
+
stepRunner: StepRunner;
|
|
107
|
+
/**
|
|
108
|
+
* Optional runner for steps declared with `options.runtime === "node"`.
|
|
109
|
+
* Typical impl dispatches to a Cloudflare Container sized for the
|
|
110
|
+
* step (or, in local dev, an in-process passthrough).
|
|
111
|
+
*
|
|
112
|
+
* If a step requests `"node"` and this is unset, the step fails with
|
|
113
|
+
* `NODE_RUNTIME_UNAVAILABLE` — declaring a runtime and then silently
|
|
114
|
+
* falling back to edge would hide deployment bugs.
|
|
115
|
+
*/
|
|
116
|
+
nodeStepRunner?: StepRunner;
|
|
117
|
+
/**
|
|
118
|
+
* Optional rate limiter. When a step declares `options.rateLimit`,
|
|
119
|
+
* the executor calls `rateLimiter.acquire(...)` before invoking the
|
|
120
|
+
* step runner. Without a limiter, a step that declares `rateLimit`
|
|
121
|
+
* fails with `RATE_LIMITER_MISSING` — declaring a limit and not
|
|
122
|
+
* enforcing it would be silently dangerous.
|
|
123
|
+
*/
|
|
124
|
+
rateLimiter?: RateLimiter;
|
|
125
|
+
/** `() => number` used for compensation durations. Defaults to Date.now. */
|
|
126
|
+
now?: () => number;
|
|
127
|
+
/**
|
|
128
|
+
* Optional per-chunk callback fired synchronously from
|
|
129
|
+
* `ctx.stream.*` as each chunk is produced. Enables live streaming
|
|
130
|
+
* (dashboards, queues) in-process before the invocation completes.
|
|
131
|
+
* Chunks are still accumulated in the response's `streamChunks`
|
|
132
|
+
* array so the at-end delivery keeps working.
|
|
133
|
+
*/
|
|
134
|
+
onStreamChunk?: (chunk: StreamChunk) => void;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export type ExecuteWorkflowStepResponse =
|
|
138
|
+
| {
|
|
139
|
+
status: "completed";
|
|
140
|
+
output: unknown;
|
|
141
|
+
metadataUpdates: MetadataMutation[];
|
|
142
|
+
journal: JournalSlice;
|
|
143
|
+
streamChunks: StreamChunk[];
|
|
144
|
+
}
|
|
145
|
+
| {
|
|
146
|
+
status: "failed";
|
|
147
|
+
error: SerializedError;
|
|
148
|
+
metadataUpdates: MetadataMutation[];
|
|
149
|
+
journal: JournalSlice;
|
|
150
|
+
streamChunks: StreamChunk[];
|
|
151
|
+
}
|
|
152
|
+
| {
|
|
153
|
+
status: "cancelled";
|
|
154
|
+
metadataUpdates: MetadataMutation[];
|
|
155
|
+
journal: JournalSlice;
|
|
156
|
+
compensations: CompensationReport[];
|
|
157
|
+
streamChunks: StreamChunk[];
|
|
158
|
+
}
|
|
159
|
+
| {
|
|
160
|
+
status: "waiting";
|
|
161
|
+
waitpoints: WaitpointRegistration[];
|
|
162
|
+
metadataUpdates: MetadataMutation[];
|
|
163
|
+
journal: JournalSlice;
|
|
164
|
+
streamChunks: StreamChunk[];
|
|
165
|
+
}
|
|
166
|
+
| {
|
|
167
|
+
status: "compensated";
|
|
168
|
+
/** Only set when compensation was triggered by an uncaught body error. */
|
|
169
|
+
error?: SerializedError;
|
|
170
|
+
compensations: CompensationReport[];
|
|
171
|
+
metadataUpdates: MetadataMutation[];
|
|
172
|
+
journal: JournalSlice;
|
|
173
|
+
streamChunks: StreamChunk[];
|
|
174
|
+
}
|
|
175
|
+
| {
|
|
176
|
+
status: "compensation_failed";
|
|
177
|
+
error?: SerializedError;
|
|
178
|
+
compensations: CompensationReport[];
|
|
179
|
+
metadataUpdates: MetadataMutation[];
|
|
180
|
+
journal: JournalSlice;
|
|
181
|
+
streamChunks: StreamChunk[];
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
interface Compensable {
|
|
185
|
+
stepId: string;
|
|
186
|
+
output: unknown;
|
|
187
|
+
compensate: (output: unknown) => Promise<void>;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export async function executeWorkflowStep(
|
|
191
|
+
def: WorkflowDefinition,
|
|
192
|
+
req: ExecuteWorkflowStepRequest,
|
|
193
|
+
): Promise<ExecuteWorkflowStepResponse> {
|
|
194
|
+
const abortSignal = req.abortSignal ?? new AbortController().signal;
|
|
195
|
+
const now = req.now ?? (() => Date.now());
|
|
196
|
+
const clock = createClock(req.runStartedAt);
|
|
197
|
+
const random = createRandom(req.runId);
|
|
198
|
+
const waitpoints: WaitpointRegistration[] = [];
|
|
199
|
+
const metadataUpdates: MetadataMutation[] = [];
|
|
200
|
+
const compensable: Compensable[] = [];
|
|
201
|
+
const streamChunks: StreamChunk[] = [];
|
|
202
|
+
const retryOverride: { current: import("../types.js").RetryPolicy | undefined } = { current: def.config.retry };
|
|
203
|
+
|
|
204
|
+
const callbacks: RuntimeCallbacks = {
|
|
205
|
+
invocationCount: req.invocationCount,
|
|
206
|
+
abortSignal,
|
|
207
|
+
async runStep(args) {
|
|
208
|
+
if (args.options.rateLimit) {
|
|
209
|
+
await acquireRateLimit({
|
|
210
|
+
spec: args.options.rateLimit,
|
|
211
|
+
stepId: args.stepId,
|
|
212
|
+
input: req.input,
|
|
213
|
+
runId: req.runId,
|
|
214
|
+
projectId: req.environment.project.id,
|
|
215
|
+
limiter: req.rateLimiter,
|
|
216
|
+
signal: abortSignal,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
const runtime = args.options.runtime ?? "edge";
|
|
220
|
+
const runner = runtime === "node" ? req.nodeStepRunner : req.stepRunner;
|
|
221
|
+
if (!runner) {
|
|
222
|
+
const e = new Error(
|
|
223
|
+
`step "${args.stepId}" declared runtime="node" but the handler has no nodeStepRunner wired; ` +
|
|
224
|
+
`pass { nodeStepRunner } to createStepHandler() or remove options.runtime`,
|
|
225
|
+
);
|
|
226
|
+
(e as Error & { code?: string }).code = "NODE_RUNTIME_UNAVAILABLE";
|
|
227
|
+
throw e;
|
|
228
|
+
}
|
|
229
|
+
const entry = await runner({
|
|
230
|
+
stepId: args.stepId,
|
|
231
|
+
attempt: args.attempt,
|
|
232
|
+
input: args.input,
|
|
233
|
+
fn: args.fn,
|
|
234
|
+
stepCtx: args.stepCtx,
|
|
235
|
+
runId: req.runId,
|
|
236
|
+
workflowId: req.workflowId,
|
|
237
|
+
workflowVersion: req.workflowVersion,
|
|
238
|
+
projectId: req.environment.project.id,
|
|
239
|
+
organizationId: req.environment.organization.id,
|
|
240
|
+
options: args.options,
|
|
241
|
+
journal: req.journal,
|
|
242
|
+
});
|
|
243
|
+
// Stamp the runtime on the journal entry so downstream consumers
|
|
244
|
+
// (journal persistence, dashboard events) can report where each
|
|
245
|
+
// step actually ran.
|
|
246
|
+
entry.runtime = runtime;
|
|
247
|
+
return entry;
|
|
248
|
+
},
|
|
249
|
+
registerWaitpoint(args) {
|
|
250
|
+
waitpoints.push(args);
|
|
251
|
+
},
|
|
252
|
+
pushMetadata(op) {
|
|
253
|
+
metadataUpdates.push(op);
|
|
254
|
+
},
|
|
255
|
+
recordCompensable(args) {
|
|
256
|
+
compensable.push(args);
|
|
257
|
+
},
|
|
258
|
+
compensableLength(): number {
|
|
259
|
+
return compensable.length;
|
|
260
|
+
},
|
|
261
|
+
spliceCompensable(fromIndex: number): Compensable[] {
|
|
262
|
+
return compensable.splice(fromIndex);
|
|
263
|
+
},
|
|
264
|
+
pushStreamChunk(args) {
|
|
265
|
+
const chunk = { ...args, at: now() };
|
|
266
|
+
streamChunks.push(chunk);
|
|
267
|
+
// Fire the live hook synchronously. Errors are swallowed — a
|
|
268
|
+
// misbehaving subscriber must not break the workflow body.
|
|
269
|
+
if (req.onStreamChunk) {
|
|
270
|
+
try {
|
|
271
|
+
req.onStreamChunk(chunk);
|
|
272
|
+
} catch {
|
|
273
|
+
/* ignore */
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const ctx = buildCtx({
|
|
280
|
+
env: req.environment,
|
|
281
|
+
journal: req.journal,
|
|
282
|
+
callbacks,
|
|
283
|
+
clock,
|
|
284
|
+
random,
|
|
285
|
+
retryOverride,
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
try {
|
|
289
|
+
const output = await def.config.run(req.input, ctx);
|
|
290
|
+
// If the body registered a waitpoint but a user try/catch swallowed
|
|
291
|
+
// the internal yield signal, honour the waitpoint — the body can't
|
|
292
|
+
// both register a waitpoint and complete in the same invocation.
|
|
293
|
+
if (waitpoints.length > 0) {
|
|
294
|
+
return { status: "waiting", waitpoints, metadataUpdates, journal: req.journal, streamChunks };
|
|
295
|
+
}
|
|
296
|
+
return { status: "completed", output, metadataUpdates, journal: req.journal, streamChunks };
|
|
297
|
+
} catch (err) {
|
|
298
|
+
if (isWaitpointPending(err)) {
|
|
299
|
+
return { status: "waiting", waitpoints, metadataUpdates, journal: req.journal, streamChunks };
|
|
300
|
+
}
|
|
301
|
+
// Same guard for the error path: a swallowed signal shouldn't let
|
|
302
|
+
// the body claim failure while waitpoints are pending.
|
|
303
|
+
if (waitpoints.length > 0 && !isRunCancelled(err) && !isCompensateRequested(err)) {
|
|
304
|
+
return { status: "waiting", waitpoints, metadataUpdates, journal: req.journal, streamChunks };
|
|
305
|
+
}
|
|
306
|
+
if (isRunCancelled(err)) {
|
|
307
|
+
// Default: compensate on cancel. Terminal status stays `cancelled`
|
|
308
|
+
// to reflect why the run ended.
|
|
309
|
+
const compensations = await runCompensations(compensable, now);
|
|
310
|
+
return { status: "cancelled", metadataUpdates, journal: req.journal, compensations, streamChunks };
|
|
311
|
+
}
|
|
312
|
+
if (isCompensateRequested(err)) {
|
|
313
|
+
const compensations = await runCompensations(compensable, now);
|
|
314
|
+
const anyErr = compensations.some((c) => c.status === "err");
|
|
315
|
+
return {
|
|
316
|
+
status: anyErr ? "compensation_failed" : "compensated",
|
|
317
|
+
compensations,
|
|
318
|
+
metadataUpdates,
|
|
319
|
+
journal: req.journal,
|
|
320
|
+
streamChunks,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Uncaught user error. Run compensations LIFO; adjust terminal status
|
|
325
|
+
// based on whether compensations were registered and all succeeded.
|
|
326
|
+
const compensations = await runCompensations(compensable, now);
|
|
327
|
+
const serialized = serializeError(err);
|
|
328
|
+
|
|
329
|
+
if (compensations.length === 0) {
|
|
330
|
+
return {
|
|
331
|
+
status: "failed",
|
|
332
|
+
error: serialized,
|
|
333
|
+
metadataUpdates,
|
|
334
|
+
journal: req.journal,
|
|
335
|
+
streamChunks,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const anyErr = compensations.some((c) => c.status === "err");
|
|
340
|
+
return {
|
|
341
|
+
status: anyErr ? "compensation_failed" : "compensated",
|
|
342
|
+
error: serialized,
|
|
343
|
+
compensations,
|
|
344
|
+
metadataUpdates,
|
|
345
|
+
journal: req.journal,
|
|
346
|
+
streamChunks,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async function runCompensations(
|
|
352
|
+
compensable: readonly Compensable[],
|
|
353
|
+
now: () => number,
|
|
354
|
+
): Promise<CompensationReport[]> {
|
|
355
|
+
const reports: CompensationReport[] = [];
|
|
356
|
+
// Reverse order: last-completed compensates first.
|
|
357
|
+
for (let i = compensable.length - 1; i >= 0; i--) {
|
|
358
|
+
const c = compensable[i]!;
|
|
359
|
+
const startedAt = now();
|
|
360
|
+
try {
|
|
361
|
+
await c.compensate(c.output);
|
|
362
|
+
reports.push({ stepId: c.stepId, status: "ok", durationMs: now() - startedAt });
|
|
363
|
+
} catch (err) {
|
|
364
|
+
reports.push({
|
|
365
|
+
stepId: c.stepId,
|
|
366
|
+
status: "err",
|
|
367
|
+
error: serializeError(err),
|
|
368
|
+
durationMs: now() - startedAt,
|
|
369
|
+
});
|
|
370
|
+
// Continue with remaining compensations; don't abort on one failure.
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return reports;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function serializeError(err: unknown): SerializedError {
|
|
377
|
+
if (err instanceof Error) {
|
|
378
|
+
const code = (err as { code?: string }).code;
|
|
379
|
+
const cause = (err as { cause?: unknown }).cause;
|
|
380
|
+
return {
|
|
381
|
+
category: "USER_ERROR",
|
|
382
|
+
code: typeof code === "string" ? code : "UNKNOWN",
|
|
383
|
+
message: err.message,
|
|
384
|
+
name: err.name,
|
|
385
|
+
stack: err.stack,
|
|
386
|
+
cause: cause !== undefined ? serializeError(cause) : undefined,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
return { category: "USER_ERROR", code: "UNKNOWN", message: String(err) };
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
async function acquireRateLimit(args: {
|
|
393
|
+
spec: NonNullable<StepOptions<unknown>["rateLimit"]>;
|
|
394
|
+
stepId: string;
|
|
395
|
+
input: unknown;
|
|
396
|
+
runId: string;
|
|
397
|
+
projectId: string;
|
|
398
|
+
limiter: RateLimiter | undefined;
|
|
399
|
+
signal: AbortSignal;
|
|
400
|
+
}): Promise<void> {
|
|
401
|
+
const ctx = { run: { id: args.runId }, project: { id: args.projectId } };
|
|
402
|
+
const key =
|
|
403
|
+
typeof args.spec.key === "function" ? args.spec.key(args.input, ctx) : args.spec.key;
|
|
404
|
+
const limit =
|
|
405
|
+
typeof args.spec.limit === "function" ? args.spec.limit(args.input) : args.spec.limit;
|
|
406
|
+
const units =
|
|
407
|
+
args.spec.units === undefined
|
|
408
|
+
? 1
|
|
409
|
+
: typeof args.spec.units === "function"
|
|
410
|
+
? args.spec.units(args.input)
|
|
411
|
+
: args.spec.units;
|
|
412
|
+
const windowMs = durationToMs(args.spec.window);
|
|
413
|
+
const onLimit = args.spec.onLimit ?? "queue";
|
|
414
|
+
|
|
415
|
+
if (!args.limiter) {
|
|
416
|
+
const e = new Error(
|
|
417
|
+
`step "${args.stepId}" declared options.rateLimit but the handler has no rateLimiter wired; ` +
|
|
418
|
+
`pass { rateLimiter } to createStepHandler()`,
|
|
419
|
+
);
|
|
420
|
+
(e as Error & { code?: string }).code = "RATE_LIMITER_MISSING";
|
|
421
|
+
throw e;
|
|
422
|
+
}
|
|
423
|
+
await args.limiter.acquire({ key, limit, units, windowMs, onLimit, signal: args.signal });
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export { WaitpointPendingSignal, RunCancelledSignal };
|
|
427
|
+
export type { RunStatus };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Tenant-side view of the journal sent by the orchestrator on every
|
|
2
|
+
// `/__voyant/workflow-step` invocation.
|
|
3
|
+
//
|
|
4
|
+
// Wire shape defined in docs/runtime-protocol.md §2.1 (JournalSlice).
|
|
5
|
+
|
|
6
|
+
import type { WaitpointKind } from "../types.js";
|
|
7
|
+
import type { SerializedError } from "../protocol/index.js";
|
|
8
|
+
|
|
9
|
+
export interface StepJournalEntry {
|
|
10
|
+
attempt: number;
|
|
11
|
+
status: "ok" | "err";
|
|
12
|
+
output?: unknown;
|
|
13
|
+
error?: SerializedError;
|
|
14
|
+
startedAt: number;
|
|
15
|
+
finishedAt: number;
|
|
16
|
+
/**
|
|
17
|
+
* Which runtime actually executed the step. Set by the executor when
|
|
18
|
+
* routing between `stepRunner` (edge) and `nodeStepRunner`.
|
|
19
|
+
* Informational only — doesn't affect replay.
|
|
20
|
+
*/
|
|
21
|
+
runtime?: "edge" | "node";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface WaitpointResolutionEntry {
|
|
25
|
+
kind: WaitpointKind;
|
|
26
|
+
resolvedAt: number;
|
|
27
|
+
matchedEventId?: string;
|
|
28
|
+
payload?: unknown;
|
|
29
|
+
source: "live" | "inbox" | "replay";
|
|
30
|
+
/** Populated for RUN waitpoints when the child run ended in a failure state. */
|
|
31
|
+
error?: SerializedError;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface CompensationJournalEntry {
|
|
35
|
+
status: "ok" | "err";
|
|
36
|
+
finishedAt: number;
|
|
37
|
+
error?: SerializedError;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface JournalSlice {
|
|
41
|
+
stepResults: Record<string, StepJournalEntry>;
|
|
42
|
+
waitpointsResolved: Record<string, WaitpointResolutionEntry>;
|
|
43
|
+
compensationsRun: Record<string, CompensationJournalEntry>;
|
|
44
|
+
metadataState: Record<string, unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* Stream ids whose generator has already been consumed in a prior
|
|
47
|
+
* invocation. The orchestrator already has the chunks on the run
|
|
48
|
+
* record; replaying the body must skip re-iterating the source
|
|
49
|
+
* (generators often have side effects — LLM calls, file reads,
|
|
50
|
+
* billable API usage).
|
|
51
|
+
*/
|
|
52
|
+
streamsCompleted: Record<string, { chunkCount: number }>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function emptyJournal(): JournalSlice {
|
|
56
|
+
return {
|
|
57
|
+
stepResults: {},
|
|
58
|
+
waitpointsResolved: {},
|
|
59
|
+
compensationsRun: {},
|
|
60
|
+
metadataState: {},
|
|
61
|
+
streamsCompleted: {},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Clone the journal into a slice that the body sees. Each step /
|
|
67
|
+
* waitpoint the body replays is *consumed* from the slice so we can
|
|
68
|
+
* detect leftover journal entries that the code no longer produces
|
|
69
|
+
* (which is a versioning hazard — see §6.7 of design.md).
|
|
70
|
+
*/
|
|
71
|
+
export function forkJournal(j: JournalSlice): JournalSlice {
|
|
72
|
+
return {
|
|
73
|
+
stepResults: { ...j.stepResults },
|
|
74
|
+
waitpointsResolved: { ...j.waitpointsResolved },
|
|
75
|
+
compensationsRun: { ...j.compensationsRun },
|
|
76
|
+
metadataState: { ...j.metadataState },
|
|
77
|
+
streamsCompleted: { ...j.streamsCompleted },
|
|
78
|
+
};
|
|
79
|
+
}
|