@workflow/world-local 5.0.0-beta.10 → 5.0.0-beta.2
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/dist/config.d.ts +17 -5
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +18 -13
- package/dist/config.js.map +1 -1
- package/dist/fs.d.ts +58 -2
- package/dist/fs.d.ts.map +1 -1
- package/dist/fs.js +203 -24
- package/dist/fs.js.map +1 -1
- package/dist/index.d.ts +15 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +82 -5
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +91 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +268 -0
- package/dist/init.js.map +1 -0
- package/dist/instrumentObject.d.ts +8 -0
- package/dist/instrumentObject.d.ts.map +1 -0
- package/dist/instrumentObject.js +66 -0
- package/dist/instrumentObject.js.map +1 -0
- package/dist/queue.d.ts +8 -1
- package/dist/queue.d.ts.map +1 -1
- package/dist/queue.js +164 -53
- package/dist/queue.js.map +1 -1
- package/dist/storage/events-storage.d.ts +7 -0
- package/dist/storage/events-storage.d.ts.map +1 -0
- package/dist/storage/events-storage.js +744 -0
- package/dist/storage/events-storage.js.map +1 -0
- package/dist/storage/filters.d.ts +22 -0
- package/dist/storage/filters.d.ts.map +1 -0
- package/dist/storage/filters.js +33 -0
- package/dist/storage/filters.js.map +1 -0
- package/dist/storage/helpers.d.ts +18 -0
- package/dist/storage/helpers.d.ts.map +1 -0
- package/dist/storage/helpers.js +44 -0
- package/dist/storage/helpers.js.map +1 -0
- package/dist/storage/hooks-storage.d.ts +12 -0
- package/dist/storage/hooks-storage.d.ts.map +1 -0
- package/dist/storage/hooks-storage.js +92 -0
- package/dist/storage/hooks-storage.js.map +1 -0
- package/dist/storage/index.d.ts +12 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +32 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/legacy.d.ts +13 -0
- package/dist/storage/legacy.d.ts.map +1 -0
- package/dist/storage/legacy.js +73 -0
- package/dist/storage/legacy.js.map +1 -0
- package/dist/storage/runs-storage.d.ts +7 -0
- package/dist/storage/runs-storage.d.ts.map +1 -0
- package/dist/storage/runs-storage.js +58 -0
- package/dist/storage/runs-storage.js.map +1 -0
- package/dist/storage/steps-storage.d.ts +7 -0
- package/dist/storage/steps-storage.d.ts.map +1 -0
- package/dist/storage/steps-storage.js +49 -0
- package/dist/storage/steps-storage.js.map +1 -0
- package/dist/storage.d.ts +9 -2
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +8 -455
- package/dist/storage.js.map +1 -1
- package/dist/streamer.d.ts +4 -2
- package/dist/streamer.d.ts.map +1 -1
- package/dist/streamer.js +451 -104
- package/dist/streamer.js.map +1 -1
- package/dist/telemetry.d.ts +28 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +71 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/test-helpers.d.ts +54 -0
- package/dist/test-helpers.d.ts.map +1 -0
- package/dist/test-helpers.js +118 -0
- package/dist/test-helpers.js.map +1 -0
- package/dist/util.js.map +1 -1
- package/package.json +12 -11
|
@@ -0,0 +1,744 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { EntityConflictError, HookNotFoundError, RunExpiredError, RunNotSupportedError, TooEarlyError, WorkflowWorldError, } from '@workflow/errors';
|
|
4
|
+
import { EventSchema, HookSchema, isLegacySpecVersion, requiresNewerWorld, SPEC_VERSION_CURRENT, StepSchema, validateUlidTimestamp, WaitSchema, WorkflowRunSchema, } from '@workflow/world';
|
|
5
|
+
import { DEFAULT_RESOLVE_DATA_OPTION } from '../config.js';
|
|
6
|
+
import { deleteJSON, jsonReplacer, listJSONFiles, paginatedFileSystemQuery, readJSONWithFallback, taggedPath, writeExclusive, writeJSON, } from '../fs.js';
|
|
7
|
+
import { stripEventDataRefs } from './filters.js';
|
|
8
|
+
import { getObjectCreatedAt, hashToken, monotonicUlid } from './helpers.js';
|
|
9
|
+
import { deleteAllHooksForRun } from './hooks-storage.js';
|
|
10
|
+
import { handleLegacyEvent } from './legacy.js';
|
|
11
|
+
/**
|
|
12
|
+
* Helper function to delete all waits associated with a workflow run.
|
|
13
|
+
* Called when a run reaches a terminal state.
|
|
14
|
+
*/
|
|
15
|
+
async function deleteAllWaitsForRun(basedir, runId) {
|
|
16
|
+
const waitsDir = path.join(basedir, 'waits');
|
|
17
|
+
const files = await listJSONFiles(waitsDir);
|
|
18
|
+
for (const file of files) {
|
|
19
|
+
// fileIds may contain tag suffixes (e.g., "wrun_ABC-corrId.vitest-0")
|
|
20
|
+
// but startsWith still matches correctly since the tag is a suffix.
|
|
21
|
+
if (file.startsWith(`${runId}-`)) {
|
|
22
|
+
const waitPath = path.join(waitsDir, `${file}.json`);
|
|
23
|
+
await deleteJSON(waitPath);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Creates the events storage implementation using the filesystem.
|
|
29
|
+
* Implements the Storage['events'] interface with create, list, and listByCorrelationId operations.
|
|
30
|
+
*/
|
|
31
|
+
export function createEventsStorage(basedir, tag) {
|
|
32
|
+
return {
|
|
33
|
+
async create(runId, data, params) {
|
|
34
|
+
const eventId = `evnt_${monotonicUlid()}`;
|
|
35
|
+
const now = new Date();
|
|
36
|
+
// For run_created events, use client-provided runId or generate one server-side
|
|
37
|
+
let effectiveRunId;
|
|
38
|
+
if (data.eventType === 'run_created' && (!runId || runId === '')) {
|
|
39
|
+
effectiveRunId = `wrun_${monotonicUlid()}`;
|
|
40
|
+
}
|
|
41
|
+
else if (!runId) {
|
|
42
|
+
throw new Error('runId is required for non-run_created events');
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
effectiveRunId = runId;
|
|
46
|
+
}
|
|
47
|
+
// Validate client-provided runId timestamp is within acceptable threshold
|
|
48
|
+
if (data.eventType === 'run_created' && runId && runId !== '') {
|
|
49
|
+
const validationError = validateUlidTimestamp(effectiveRunId, 'wrun_');
|
|
50
|
+
if (validationError) {
|
|
51
|
+
throw new WorkflowWorldError(validationError);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// specVersion is always sent by the runtime, but we provide a fallback for safety
|
|
55
|
+
const effectiveSpecVersion = data.specVersion ?? SPEC_VERSION_CURRENT;
|
|
56
|
+
// Helper to check if run is in terminal state
|
|
57
|
+
const isRunTerminal = (status) => ['completed', 'failed', 'cancelled'].includes(status);
|
|
58
|
+
// Helper to check if step is in terminal state
|
|
59
|
+
const isStepTerminal = (status) => ['completed', 'failed', 'cancelled'].includes(status);
|
|
60
|
+
// Get current run state for validation (if not creating a new run)
|
|
61
|
+
// Skip run validation for step_completed and step_retrying - they only operate
|
|
62
|
+
// on running steps, and running steps are always allowed to modify regardless
|
|
63
|
+
// of run state. This optimization saves filesystem reads per step event.
|
|
64
|
+
let currentRun = null;
|
|
65
|
+
const skipRunValidationEvents = ['step_completed', 'step_retrying'];
|
|
66
|
+
if (data.eventType !== 'run_created' &&
|
|
67
|
+
!skipRunValidationEvents.includes(data.eventType)) {
|
|
68
|
+
currentRun = await readJSONWithFallback(basedir, 'runs', effectiveRunId, WorkflowRunSchema, tag);
|
|
69
|
+
// Resilient start: run_started on non-existent run with eventData
|
|
70
|
+
// creates the run first, so the queue can bootstrap a run that
|
|
71
|
+
// failed to create during start().
|
|
72
|
+
if (data.eventType === 'run_started' &&
|
|
73
|
+
!currentRun &&
|
|
74
|
+
'eventData' in data &&
|
|
75
|
+
data.eventData) {
|
|
76
|
+
const runInputData = data.eventData;
|
|
77
|
+
if (runInputData.deploymentId &&
|
|
78
|
+
runInputData.workflowName &&
|
|
79
|
+
runInputData.input !== undefined) {
|
|
80
|
+
// Atomically try to create the run entity. writeExclusive
|
|
81
|
+
// uses O_CREAT|O_EXCL so only the first writer wins,
|
|
82
|
+
// preventing a TOCTOU race where a concurrent run_created
|
|
83
|
+
// from start() could overwrite a run that was already
|
|
84
|
+
// transitioned to 'running'.
|
|
85
|
+
const createdRun = {
|
|
86
|
+
runId: effectiveRunId,
|
|
87
|
+
deploymentId: runInputData.deploymentId,
|
|
88
|
+
status: 'pending',
|
|
89
|
+
workflowName: runInputData.workflowName,
|
|
90
|
+
specVersion: effectiveSpecVersion,
|
|
91
|
+
executionContext: runInputData.executionContext,
|
|
92
|
+
input: runInputData.input,
|
|
93
|
+
output: undefined,
|
|
94
|
+
error: undefined,
|
|
95
|
+
startedAt: undefined,
|
|
96
|
+
completedAt: undefined,
|
|
97
|
+
createdAt: now,
|
|
98
|
+
updatedAt: now,
|
|
99
|
+
};
|
|
100
|
+
const runPath = taggedPath(basedir, 'runs', effectiveRunId, tag);
|
|
101
|
+
const created = await writeExclusive(runPath, JSON.stringify(createdRun, jsonReplacer));
|
|
102
|
+
if (created) {
|
|
103
|
+
// We created the run — also write the run_created event.
|
|
104
|
+
const runCreatedEventId = `evnt_${monotonicUlid()}`;
|
|
105
|
+
const runCreatedEvent = {
|
|
106
|
+
eventType: 'run_created',
|
|
107
|
+
runId: effectiveRunId,
|
|
108
|
+
eventId: runCreatedEventId,
|
|
109
|
+
createdAt: now,
|
|
110
|
+
specVersion: effectiveSpecVersion,
|
|
111
|
+
eventData: {
|
|
112
|
+
deploymentId: runInputData.deploymentId,
|
|
113
|
+
workflowName: runInputData.workflowName,
|
|
114
|
+
input: runInputData.input,
|
|
115
|
+
executionContext: runInputData.executionContext,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
const createdCompositeKey = `${effectiveRunId}-${runCreatedEventId}`;
|
|
119
|
+
await writeJSON(taggedPath(basedir, 'events', createdCompositeKey, tag), runCreatedEvent);
|
|
120
|
+
currentRun = createdRun;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// Run already exists (concurrent run_created won the
|
|
124
|
+
// race). Re-read it so downstream logic sees the real state.
|
|
125
|
+
currentRun = await readJSONWithFallback(basedir, 'runs', effectiveRunId, WorkflowRunSchema, tag);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// ============================================================
|
|
131
|
+
// VERSION COMPATIBILITY: Check run spec version
|
|
132
|
+
// ============================================================
|
|
133
|
+
// For events that have fetched the run, check version compatibility.
|
|
134
|
+
// Skip for run_created (no existing run) and runtime events (step_completed, step_retrying).
|
|
135
|
+
if (currentRun) {
|
|
136
|
+
// Check if run requires a newer world version
|
|
137
|
+
if (requiresNewerWorld(currentRun.specVersion)) {
|
|
138
|
+
throw new RunNotSupportedError(currentRun.specVersion, SPEC_VERSION_CURRENT);
|
|
139
|
+
}
|
|
140
|
+
// Route to legacy handler for pre-event-sourcing runs
|
|
141
|
+
if (isLegacySpecVersion(currentRun.specVersion)) {
|
|
142
|
+
return handleLegacyEvent(basedir, effectiveRunId, data, currentRun, params);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// ============================================================
|
|
146
|
+
// VALIDATION: Terminal state and event ordering checks
|
|
147
|
+
// ============================================================
|
|
148
|
+
// Run terminal state validation
|
|
149
|
+
if (currentRun && isRunTerminal(currentRun.status)) {
|
|
150
|
+
const runTerminalEvents = [
|
|
151
|
+
'run_started',
|
|
152
|
+
'run_completed',
|
|
153
|
+
'run_failed',
|
|
154
|
+
];
|
|
155
|
+
// Idempotent operation: run_cancelled on already cancelled run is allowed
|
|
156
|
+
if (data.eventType === 'run_cancelled' &&
|
|
157
|
+
currentRun.status === 'cancelled') {
|
|
158
|
+
// Return existing state (idempotent)
|
|
159
|
+
const event = {
|
|
160
|
+
...data,
|
|
161
|
+
runId: effectiveRunId,
|
|
162
|
+
eventId,
|
|
163
|
+
createdAt: now,
|
|
164
|
+
specVersion: effectiveSpecVersion,
|
|
165
|
+
};
|
|
166
|
+
const compositeKey = `${effectiveRunId}-${eventId}`;
|
|
167
|
+
await writeJSON(taggedPath(basedir, 'events', compositeKey, tag), event);
|
|
168
|
+
const resolveData = params?.resolveData ?? DEFAULT_RESOLVE_DATA_OPTION;
|
|
169
|
+
return {
|
|
170
|
+
event: stripEventDataRefs(event, resolveData),
|
|
171
|
+
run: currentRun,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
// For run_started on terminal runs, use RunExpiredError so the
|
|
175
|
+
// runtime knows to exit without retrying.
|
|
176
|
+
if (data.eventType === 'run_started') {
|
|
177
|
+
throw new RunExpiredError(`Workflow run "${effectiveRunId}" is already in terminal state "${currentRun.status}"`);
|
|
178
|
+
}
|
|
179
|
+
// Other run state transitions are not allowed on terminal runs
|
|
180
|
+
if (runTerminalEvents.includes(data.eventType) ||
|
|
181
|
+
data.eventType === 'run_cancelled') {
|
|
182
|
+
throw new EntityConflictError(`Cannot transition run from terminal state "${currentRun.status}"`);
|
|
183
|
+
}
|
|
184
|
+
// Creating new entities on terminal runs is not allowed
|
|
185
|
+
if (data.eventType === 'step_created' ||
|
|
186
|
+
data.eventType === 'hook_created' ||
|
|
187
|
+
data.eventType === 'wait_created') {
|
|
188
|
+
throw new EntityConflictError(`Cannot create new entities on run in terminal state "${currentRun.status}"`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// Step-related event validation (ordering and terminal state)
|
|
192
|
+
// Store existingStep so we can reuse it later (avoid double read)
|
|
193
|
+
let validatedStep = null;
|
|
194
|
+
const stepEvents = [
|
|
195
|
+
'step_started',
|
|
196
|
+
'step_completed',
|
|
197
|
+
'step_failed',
|
|
198
|
+
'step_retrying',
|
|
199
|
+
];
|
|
200
|
+
if (stepEvents.includes(data.eventType) && data.correlationId) {
|
|
201
|
+
const stepCompositeKey = `${effectiveRunId}-${data.correlationId}`;
|
|
202
|
+
validatedStep = await readJSONWithFallback(basedir, 'steps', stepCompositeKey, StepSchema, tag);
|
|
203
|
+
// Event ordering: step must exist before these events
|
|
204
|
+
if (!validatedStep) {
|
|
205
|
+
throw new WorkflowWorldError(`Step "${data.correlationId}" not found`);
|
|
206
|
+
}
|
|
207
|
+
// Step terminal state validation
|
|
208
|
+
if (isStepTerminal(validatedStep.status)) {
|
|
209
|
+
throw new EntityConflictError(`Cannot modify step in terminal state "${validatedStep.status}"`);
|
|
210
|
+
}
|
|
211
|
+
// On terminal runs: only allow completing/failing in-progress steps
|
|
212
|
+
if (currentRun && isRunTerminal(currentRun.status)) {
|
|
213
|
+
if (validatedStep.status !== 'running') {
|
|
214
|
+
throw new RunExpiredError(`Cannot modify non-running step on run in terminal state "${currentRun.status}"`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// Hook-related event validation (ordering)
|
|
219
|
+
const hookEventsRequiringExistence = ['hook_disposed', 'hook_received'];
|
|
220
|
+
if (hookEventsRequiringExistence.includes(data.eventType) &&
|
|
221
|
+
data.correlationId) {
|
|
222
|
+
const existingHook = await readJSONWithFallback(basedir, 'hooks', data.correlationId, HookSchema, tag);
|
|
223
|
+
if (!existingHook) {
|
|
224
|
+
throw new HookNotFoundError(data.correlationId);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const event = {
|
|
228
|
+
...data,
|
|
229
|
+
runId: effectiveRunId,
|
|
230
|
+
eventId,
|
|
231
|
+
createdAt: now,
|
|
232
|
+
specVersion: effectiveSpecVersion,
|
|
233
|
+
};
|
|
234
|
+
// Strip eventData from run_started — it belongs on run_created only.
|
|
235
|
+
if (data.eventType === 'run_started' && 'eventData' in event) {
|
|
236
|
+
delete event.eventData;
|
|
237
|
+
}
|
|
238
|
+
// Track entity created/updated for EventResult
|
|
239
|
+
let run;
|
|
240
|
+
let step;
|
|
241
|
+
let hook;
|
|
242
|
+
let wait;
|
|
243
|
+
// Create/update entity based on event type (event-sourced architecture)
|
|
244
|
+
// Run lifecycle events
|
|
245
|
+
if (data.eventType === 'run_created' && 'eventData' in data) {
|
|
246
|
+
const runData = data.eventData;
|
|
247
|
+
run = {
|
|
248
|
+
runId: effectiveRunId,
|
|
249
|
+
deploymentId: runData.deploymentId,
|
|
250
|
+
status: 'pending',
|
|
251
|
+
workflowName: runData.workflowName,
|
|
252
|
+
// Propagate specVersion from the event to the run entity
|
|
253
|
+
specVersion: effectiveSpecVersion,
|
|
254
|
+
executionContext: runData.executionContext,
|
|
255
|
+
input: runData.input,
|
|
256
|
+
output: undefined,
|
|
257
|
+
error: undefined,
|
|
258
|
+
startedAt: undefined,
|
|
259
|
+
completedAt: undefined,
|
|
260
|
+
createdAt: now,
|
|
261
|
+
updatedAt: now,
|
|
262
|
+
};
|
|
263
|
+
// Use writeExclusive (O_CREAT|O_EXCL) to atomically create the
|
|
264
|
+
// run entity file. This prevents a TOCTOU race with the resilient
|
|
265
|
+
// start path (run_started on non-existent run) that could result
|
|
266
|
+
// in duplicate run_created events in the event log.
|
|
267
|
+
const runPath = taggedPath(basedir, 'runs', effectiveRunId, tag);
|
|
268
|
+
const created = await writeExclusive(runPath, JSON.stringify(run, jsonReplacer, 2));
|
|
269
|
+
if (!created) {
|
|
270
|
+
throw new EntityConflictError(`Workflow run "${effectiveRunId}" already exists`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
else if (data.eventType === 'run_started') {
|
|
274
|
+
// Reuse currentRun from validation (already read above)
|
|
275
|
+
if (currentRun) {
|
|
276
|
+
// If already running, return the run without inserting a
|
|
277
|
+
// duplicate event. This makes run_started idempotent for
|
|
278
|
+
// concurrent invocations. We omit preloaded events here
|
|
279
|
+
// because this is a rare race-condition path — the runtime
|
|
280
|
+
// falls back to getAllWorkflowRunEvents().
|
|
281
|
+
if (currentRun.status === 'running') {
|
|
282
|
+
return { run: currentRun };
|
|
283
|
+
}
|
|
284
|
+
run = {
|
|
285
|
+
runId: currentRun.runId,
|
|
286
|
+
deploymentId: currentRun.deploymentId,
|
|
287
|
+
workflowName: currentRun.workflowName,
|
|
288
|
+
specVersion: currentRun.specVersion,
|
|
289
|
+
executionContext: currentRun.executionContext,
|
|
290
|
+
input: currentRun.input,
|
|
291
|
+
createdAt: currentRun.createdAt,
|
|
292
|
+
expiredAt: currentRun.expiredAt,
|
|
293
|
+
status: 'running',
|
|
294
|
+
output: undefined,
|
|
295
|
+
error: undefined,
|
|
296
|
+
completedAt: undefined,
|
|
297
|
+
startedAt: currentRun.startedAt ?? now,
|
|
298
|
+
updatedAt: now,
|
|
299
|
+
};
|
|
300
|
+
await writeJSON(taggedPath(basedir, 'runs', effectiveRunId, tag), run, { overwrite: true });
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
else if (data.eventType === 'run_completed' && 'eventData' in data) {
|
|
304
|
+
const completedData = data.eventData;
|
|
305
|
+
// Reuse currentRun from validation (already read above)
|
|
306
|
+
if (currentRun) {
|
|
307
|
+
run = {
|
|
308
|
+
runId: currentRun.runId,
|
|
309
|
+
deploymentId: currentRun.deploymentId,
|
|
310
|
+
workflowName: currentRun.workflowName,
|
|
311
|
+
specVersion: currentRun.specVersion,
|
|
312
|
+
executionContext: currentRun.executionContext,
|
|
313
|
+
input: currentRun.input,
|
|
314
|
+
createdAt: currentRun.createdAt,
|
|
315
|
+
expiredAt: currentRun.expiredAt,
|
|
316
|
+
startedAt: currentRun.startedAt,
|
|
317
|
+
status: 'completed',
|
|
318
|
+
output: completedData.output,
|
|
319
|
+
error: undefined,
|
|
320
|
+
completedAt: now,
|
|
321
|
+
updatedAt: now,
|
|
322
|
+
};
|
|
323
|
+
await writeJSON(taggedPath(basedir, 'runs', effectiveRunId, tag), run, { overwrite: true });
|
|
324
|
+
await Promise.all([
|
|
325
|
+
deleteAllHooksForRun(basedir, effectiveRunId),
|
|
326
|
+
deleteAllWaitsForRun(basedir, effectiveRunId),
|
|
327
|
+
]);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
else if (data.eventType === 'run_failed' && 'eventData' in data) {
|
|
331
|
+
const failedData = data.eventData;
|
|
332
|
+
// Reuse currentRun from validation (already read above)
|
|
333
|
+
if (currentRun) {
|
|
334
|
+
run = {
|
|
335
|
+
runId: currentRun.runId,
|
|
336
|
+
deploymentId: currentRun.deploymentId,
|
|
337
|
+
workflowName: currentRun.workflowName,
|
|
338
|
+
specVersion: currentRun.specVersion,
|
|
339
|
+
executionContext: currentRun.executionContext,
|
|
340
|
+
input: currentRun.input,
|
|
341
|
+
createdAt: currentRun.createdAt,
|
|
342
|
+
expiredAt: currentRun.expiredAt,
|
|
343
|
+
startedAt: currentRun.startedAt,
|
|
344
|
+
status: 'failed',
|
|
345
|
+
output: undefined,
|
|
346
|
+
error: {
|
|
347
|
+
message: typeof failedData.error === 'string'
|
|
348
|
+
? failedData.error
|
|
349
|
+
: (failedData.error?.message ?? 'Unknown error'),
|
|
350
|
+
stack: failedData.error?.stack,
|
|
351
|
+
code: failedData.errorCode,
|
|
352
|
+
},
|
|
353
|
+
completedAt: now,
|
|
354
|
+
updatedAt: now,
|
|
355
|
+
};
|
|
356
|
+
await writeJSON(taggedPath(basedir, 'runs', effectiveRunId, tag), run, { overwrite: true });
|
|
357
|
+
await Promise.all([
|
|
358
|
+
deleteAllHooksForRun(basedir, effectiveRunId),
|
|
359
|
+
deleteAllWaitsForRun(basedir, effectiveRunId),
|
|
360
|
+
]);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
else if (data.eventType === 'run_cancelled') {
|
|
364
|
+
// Reuse currentRun from validation (already read above)
|
|
365
|
+
if (currentRun) {
|
|
366
|
+
run = {
|
|
367
|
+
runId: currentRun.runId,
|
|
368
|
+
deploymentId: currentRun.deploymentId,
|
|
369
|
+
workflowName: currentRun.workflowName,
|
|
370
|
+
specVersion: currentRun.specVersion,
|
|
371
|
+
executionContext: currentRun.executionContext,
|
|
372
|
+
input: currentRun.input,
|
|
373
|
+
createdAt: currentRun.createdAt,
|
|
374
|
+
expiredAt: currentRun.expiredAt,
|
|
375
|
+
startedAt: currentRun.startedAt,
|
|
376
|
+
status: 'cancelled',
|
|
377
|
+
output: undefined,
|
|
378
|
+
error: undefined,
|
|
379
|
+
completedAt: now,
|
|
380
|
+
updatedAt: now,
|
|
381
|
+
};
|
|
382
|
+
await writeJSON(taggedPath(basedir, 'runs', effectiveRunId, tag), run, { overwrite: true });
|
|
383
|
+
await Promise.all([
|
|
384
|
+
deleteAllHooksForRun(basedir, effectiveRunId),
|
|
385
|
+
deleteAllWaitsForRun(basedir, effectiveRunId),
|
|
386
|
+
]);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
else if (
|
|
390
|
+
// Step lifecycle events
|
|
391
|
+
data.eventType === 'step_created' &&
|
|
392
|
+
'eventData' in data) {
|
|
393
|
+
// step_created: Creates step entity with status 'pending', attempt=0, createdAt set
|
|
394
|
+
const stepData = data.eventData;
|
|
395
|
+
step = {
|
|
396
|
+
runId: effectiveRunId,
|
|
397
|
+
stepId: data.correlationId,
|
|
398
|
+
stepName: stepData.stepName,
|
|
399
|
+
status: 'pending',
|
|
400
|
+
input: stepData.input,
|
|
401
|
+
output: undefined,
|
|
402
|
+
error: undefined,
|
|
403
|
+
attempt: 0,
|
|
404
|
+
startedAt: undefined,
|
|
405
|
+
completedAt: undefined,
|
|
406
|
+
createdAt: now,
|
|
407
|
+
updatedAt: now,
|
|
408
|
+
// Propagate specVersion from the event to the step entity
|
|
409
|
+
specVersion: effectiveSpecVersion,
|
|
410
|
+
};
|
|
411
|
+
const stepCompositeKey = `${effectiveRunId}-${data.correlationId}`;
|
|
412
|
+
await writeJSON(taggedPath(basedir, 'steps', stepCompositeKey, tag), step);
|
|
413
|
+
}
|
|
414
|
+
else if (data.eventType === 'step_started') {
|
|
415
|
+
// step_started: Increments attempt, sets status to 'running'
|
|
416
|
+
// Sets startedAt only on the first start (not updated on retries)
|
|
417
|
+
// Reuse validatedStep from validation (already read above)
|
|
418
|
+
if (validatedStep) {
|
|
419
|
+
// Check if retryAfter timestamp hasn't been reached yet
|
|
420
|
+
if (validatedStep.retryAfter &&
|
|
421
|
+
validatedStep.retryAfter.getTime() > Date.now()) {
|
|
422
|
+
throw new TooEarlyError(`Cannot start step "${data.correlationId}": retryAfter timestamp has not been reached yet`, {
|
|
423
|
+
retryAfter: Math.ceil((validatedStep.retryAfter.getTime() - Date.now()) / 1000),
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
// Best-effort guard: re-read the step entity to check if it
|
|
427
|
+
// reached terminal state between the validation read and now.
|
|
428
|
+
// This narrows the TOCTOU window but does not fully eliminate it
|
|
429
|
+
// (the local world is single-process / dev-only; the postgres
|
|
430
|
+
// world uses SQL-level atomic guards for production).
|
|
431
|
+
const stepCompositeKey = `${effectiveRunId}-${data.correlationId}`;
|
|
432
|
+
const freshStep = await readJSONWithFallback(basedir, 'steps', stepCompositeKey, StepSchema, tag);
|
|
433
|
+
if (freshStep && isStepTerminal(freshStep.status)) {
|
|
434
|
+
throw new EntityConflictError(`Cannot modify step in terminal state "${freshStep.status}"`);
|
|
435
|
+
}
|
|
436
|
+
step = {
|
|
437
|
+
...validatedStep,
|
|
438
|
+
status: 'running',
|
|
439
|
+
// Only set startedAt on the first start
|
|
440
|
+
startedAt: validatedStep.startedAt ?? now,
|
|
441
|
+
// Increment attempt counter on every start
|
|
442
|
+
attempt: validatedStep.attempt + 1,
|
|
443
|
+
// Clear retryAfter now that the step has started
|
|
444
|
+
retryAfter: undefined,
|
|
445
|
+
updatedAt: now,
|
|
446
|
+
};
|
|
447
|
+
await writeJSON(taggedPath(basedir, 'steps', stepCompositeKey, tag), step, { overwrite: true });
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
else if (data.eventType === 'step_completed' && 'eventData' in data) {
|
|
451
|
+
// step_completed: Terminal state with output
|
|
452
|
+
// Uses writeExclusive on a lock file to atomically prevent concurrent
|
|
453
|
+
// invocations from both completing the same step (TOCTOU race).
|
|
454
|
+
const completedData = data.eventData;
|
|
455
|
+
if (validatedStep) {
|
|
456
|
+
const stepCompositeKey = `${effectiveRunId}-${data.correlationId}`;
|
|
457
|
+
const lockName = tag
|
|
458
|
+
? `${stepCompositeKey}.terminal.${tag}`
|
|
459
|
+
: `${stepCompositeKey}.terminal`;
|
|
460
|
+
const terminalLockPath = path.join(basedir, '.locks', 'steps', lockName);
|
|
461
|
+
const claimed = await writeExclusive(terminalLockPath, '');
|
|
462
|
+
if (!claimed) {
|
|
463
|
+
throw new EntityConflictError('Cannot modify step in terminal state');
|
|
464
|
+
}
|
|
465
|
+
step = {
|
|
466
|
+
...validatedStep,
|
|
467
|
+
status: 'completed',
|
|
468
|
+
output: completedData.result,
|
|
469
|
+
completedAt: now,
|
|
470
|
+
updatedAt: now,
|
|
471
|
+
};
|
|
472
|
+
await writeJSON(taggedPath(basedir, 'steps', stepCompositeKey, tag), step, { overwrite: true });
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
else if (data.eventType === 'step_failed' && 'eventData' in data) {
|
|
476
|
+
// step_failed: Terminal state with error
|
|
477
|
+
// Uses writeExclusive on a lock file to atomically prevent concurrent
|
|
478
|
+
// invocations from both failing the same step (TOCTOU race).
|
|
479
|
+
const failedData = data.eventData;
|
|
480
|
+
if (validatedStep) {
|
|
481
|
+
const stepCompositeKey = `${effectiveRunId}-${data.correlationId}`;
|
|
482
|
+
const lockName = tag
|
|
483
|
+
? `${stepCompositeKey}.terminal.${tag}`
|
|
484
|
+
: `${stepCompositeKey}.terminal`;
|
|
485
|
+
const terminalLockPath = path.join(basedir, '.locks', 'steps', lockName);
|
|
486
|
+
const claimed = await writeExclusive(terminalLockPath, '');
|
|
487
|
+
if (!claimed) {
|
|
488
|
+
throw new EntityConflictError('Cannot modify step in terminal state');
|
|
489
|
+
}
|
|
490
|
+
const error = {
|
|
491
|
+
message: typeof failedData.error === 'string'
|
|
492
|
+
? failedData.error
|
|
493
|
+
: (failedData.error?.message ?? 'Unknown error'),
|
|
494
|
+
stack: failedData.stack,
|
|
495
|
+
};
|
|
496
|
+
step = {
|
|
497
|
+
...validatedStep,
|
|
498
|
+
status: 'failed',
|
|
499
|
+
error,
|
|
500
|
+
completedAt: now,
|
|
501
|
+
updatedAt: now,
|
|
502
|
+
};
|
|
503
|
+
await writeJSON(taggedPath(basedir, 'steps', stepCompositeKey, tag), step, { overwrite: true });
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
else if (data.eventType === 'step_retrying' && 'eventData' in data) {
|
|
507
|
+
// step_retrying: Sets status back to 'pending', records error
|
|
508
|
+
// Reuse validatedStep from validation (already read above)
|
|
509
|
+
const retryData = data.eventData;
|
|
510
|
+
if (validatedStep) {
|
|
511
|
+
const stepCompositeKey = `${effectiveRunId}-${data.correlationId}`;
|
|
512
|
+
step = {
|
|
513
|
+
...validatedStep,
|
|
514
|
+
status: 'pending',
|
|
515
|
+
error: {
|
|
516
|
+
message: typeof retryData.error === 'string'
|
|
517
|
+
? retryData.error
|
|
518
|
+
: (retryData.error?.message ?? 'Unknown error'),
|
|
519
|
+
stack: retryData.stack,
|
|
520
|
+
},
|
|
521
|
+
retryAfter: retryData.retryAfter,
|
|
522
|
+
updatedAt: now,
|
|
523
|
+
};
|
|
524
|
+
await writeJSON(taggedPath(basedir, 'steps', stepCompositeKey, tag), step, { overwrite: true });
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
else if (
|
|
528
|
+
// Hook lifecycle events
|
|
529
|
+
data.eventType === 'hook_created' &&
|
|
530
|
+
'eventData' in data) {
|
|
531
|
+
const hookData = data.eventData;
|
|
532
|
+
// Atomically claim the token using an exclusive-create constraint file.
|
|
533
|
+
// This avoids the TOCTOU race of the previous read-all-then-check approach.
|
|
534
|
+
const constraintPath = path.join(basedir, 'hooks', 'tokens', `${hashToken(hookData.token)}.json`);
|
|
535
|
+
const tokenClaimed = await writeExclusive(constraintPath, JSON.stringify({
|
|
536
|
+
token: hookData.token,
|
|
537
|
+
hookId: data.correlationId,
|
|
538
|
+
runId: effectiveRunId,
|
|
539
|
+
}));
|
|
540
|
+
if (!tokenClaimed) {
|
|
541
|
+
// Create hook_conflict event instead of hook_created
|
|
542
|
+
// This allows the workflow to continue and fail gracefully when the hook is awaited
|
|
543
|
+
const conflictEvent = {
|
|
544
|
+
eventType: 'hook_conflict',
|
|
545
|
+
correlationId: data.correlationId,
|
|
546
|
+
eventData: {
|
|
547
|
+
token: hookData.token,
|
|
548
|
+
},
|
|
549
|
+
runId: effectiveRunId,
|
|
550
|
+
eventId,
|
|
551
|
+
createdAt: now,
|
|
552
|
+
specVersion: effectiveSpecVersion,
|
|
553
|
+
};
|
|
554
|
+
// Store the conflict event
|
|
555
|
+
const compositeKey = `${effectiveRunId}-${eventId}`;
|
|
556
|
+
await writeJSON(taggedPath(basedir, 'events', compositeKey, tag), conflictEvent);
|
|
557
|
+
const resolveData = params?.resolveData ?? DEFAULT_RESOLVE_DATA_OPTION;
|
|
558
|
+
const filteredEvent = stripEventDataRefs(conflictEvent, resolveData);
|
|
559
|
+
// Return EventResult with conflict event (no hook entity created)
|
|
560
|
+
return {
|
|
561
|
+
event: filteredEvent,
|
|
562
|
+
run,
|
|
563
|
+
step,
|
|
564
|
+
hook: undefined,
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
hook = {
|
|
568
|
+
runId: effectiveRunId,
|
|
569
|
+
hookId: data.correlationId,
|
|
570
|
+
token: hookData.token,
|
|
571
|
+
metadata: hookData.metadata,
|
|
572
|
+
ownerId: 'local-owner',
|
|
573
|
+
projectId: 'local-project',
|
|
574
|
+
environment: 'local',
|
|
575
|
+
createdAt: now,
|
|
576
|
+
// Propagate specVersion from the event to the hook entity
|
|
577
|
+
specVersion: effectiveSpecVersion,
|
|
578
|
+
isWebhook: hookData.isWebhook ?? false,
|
|
579
|
+
};
|
|
580
|
+
await writeJSON(taggedPath(basedir, 'hooks', data.correlationId, tag), hook);
|
|
581
|
+
}
|
|
582
|
+
else if (data.eventType === 'hook_disposed') {
|
|
583
|
+
// hook_disposed: Deletes hook entity, rejects duplicates.
|
|
584
|
+
// Uses writeExclusive on a lock file to atomically prevent concurrent
|
|
585
|
+
// invocations from both disposing the same hook (TOCTOU race).
|
|
586
|
+
const hookLockName = tag
|
|
587
|
+
? `${data.correlationId}.disposed.${tag}`
|
|
588
|
+
: `${data.correlationId}.disposed`;
|
|
589
|
+
const lockPath = path.join(basedir, '.locks', 'hooks', hookLockName);
|
|
590
|
+
const claimed = await writeExclusive(lockPath, '');
|
|
591
|
+
if (!claimed) {
|
|
592
|
+
throw new EntityConflictError(`Hook "${data.correlationId}" already disposed`);
|
|
593
|
+
}
|
|
594
|
+
// Read the hook to get its token before deleting
|
|
595
|
+
const hookPath = taggedPath(basedir, 'hooks', data.correlationId, tag);
|
|
596
|
+
const existingHook = await readJSONWithFallback(basedir, 'hooks', data.correlationId, HookSchema, tag);
|
|
597
|
+
if (existingHook) {
|
|
598
|
+
// Delete the token constraint file to free up the token for reuse
|
|
599
|
+
const disposedConstraintPath = path.join(basedir, 'hooks', 'tokens', `${hashToken(existingHook.token)}.json`);
|
|
600
|
+
await deleteJSON(disposedConstraintPath);
|
|
601
|
+
}
|
|
602
|
+
await deleteJSON(hookPath);
|
|
603
|
+
}
|
|
604
|
+
else if (data.eventType === 'wait_created' && 'eventData' in data) {
|
|
605
|
+
// wait_created: Creates wait entity with status 'waiting'
|
|
606
|
+
const waitData = data.eventData;
|
|
607
|
+
const waitCompositeKey = `${effectiveRunId}-${data.correlationId}`;
|
|
608
|
+
const existingWait = await readJSONWithFallback(basedir, 'waits', waitCompositeKey, WaitSchema, tag);
|
|
609
|
+
if (existingWait) {
|
|
610
|
+
throw new EntityConflictError(`Wait "${data.correlationId}" already exists`);
|
|
611
|
+
}
|
|
612
|
+
wait = {
|
|
613
|
+
waitId: waitCompositeKey,
|
|
614
|
+
runId: effectiveRunId,
|
|
615
|
+
status: 'waiting',
|
|
616
|
+
resumeAt: waitData.resumeAt,
|
|
617
|
+
completedAt: undefined,
|
|
618
|
+
createdAt: now,
|
|
619
|
+
updatedAt: now,
|
|
620
|
+
specVersion: effectiveSpecVersion,
|
|
621
|
+
};
|
|
622
|
+
await writeJSON(taggedPath(basedir, 'waits', waitCompositeKey, tag), wait);
|
|
623
|
+
}
|
|
624
|
+
else if (data.eventType === 'wait_completed') {
|
|
625
|
+
// wait_completed: Transitions wait to 'completed', rejects duplicates.
|
|
626
|
+
// Uses writeExclusive on a lock file to atomically prevent concurrent
|
|
627
|
+
// invocations from both completing the same wait (TOCTOU race).
|
|
628
|
+
const waitCompositeKey = `${effectiveRunId}-${data.correlationId}`;
|
|
629
|
+
const waitLockName = tag
|
|
630
|
+
? `${waitCompositeKey}.completed.${tag}`
|
|
631
|
+
: `${waitCompositeKey}.completed`;
|
|
632
|
+
const lockPath = path.join(basedir, '.locks', 'waits', waitLockName);
|
|
633
|
+
const claimed = await writeExclusive(lockPath, '');
|
|
634
|
+
if (!claimed) {
|
|
635
|
+
throw new EntityConflictError(`Wait "${data.correlationId}" already completed`);
|
|
636
|
+
}
|
|
637
|
+
const existingWait = await readJSONWithFallback(basedir, 'waits', waitCompositeKey, WaitSchema, tag);
|
|
638
|
+
if (!existingWait) {
|
|
639
|
+
// Clean up the lock file we just claimed — the wait doesn't exist
|
|
640
|
+
await fs.unlink(lockPath).catch(() => { });
|
|
641
|
+
throw new WorkflowWorldError(`Wait "${data.correlationId}" not found`);
|
|
642
|
+
}
|
|
643
|
+
// The lock file (writeExclusive above) already prevents concurrent
|
|
644
|
+
// completions — no additional status check needed.
|
|
645
|
+
wait = {
|
|
646
|
+
...existingWait,
|
|
647
|
+
status: 'completed',
|
|
648
|
+
completedAt: now,
|
|
649
|
+
updatedAt: now,
|
|
650
|
+
};
|
|
651
|
+
await writeJSON(taggedPath(basedir, 'waits', waitCompositeKey, tag), wait, { overwrite: true });
|
|
652
|
+
}
|
|
653
|
+
// Note: hook_received events are stored in the event log but don't
|
|
654
|
+
// modify the Hook entity (which doesn't have a payload field)
|
|
655
|
+
// Store event using composite key {runId}-{eventId}
|
|
656
|
+
const compositeKey = `${effectiveRunId}-${eventId}`;
|
|
657
|
+
await writeJSON(taggedPath(basedir, 'events', compositeKey, tag), event);
|
|
658
|
+
const resolveData = params?.resolveData ?? DEFAULT_RESOLVE_DATA_OPTION;
|
|
659
|
+
const filteredEvent = stripEventDataRefs(event, resolveData);
|
|
660
|
+
// For run_started: include all events so the runtime can skip
|
|
661
|
+
// the initial events.list call and reduce TTFB.
|
|
662
|
+
let events;
|
|
663
|
+
if (data.eventType === 'run_started' && run) {
|
|
664
|
+
const allEvents = await paginatedFileSystemQuery({
|
|
665
|
+
directory: path.join(basedir, 'events'),
|
|
666
|
+
schema: EventSchema,
|
|
667
|
+
filePrefix: `${effectiveRunId}-`,
|
|
668
|
+
sortOrder: 'asc',
|
|
669
|
+
getCreatedAt: getObjectCreatedAt('evnt'),
|
|
670
|
+
getId: (e) => e.eventId,
|
|
671
|
+
});
|
|
672
|
+
events = allEvents.data;
|
|
673
|
+
}
|
|
674
|
+
// Return EventResult with event and any created/updated entity
|
|
675
|
+
return {
|
|
676
|
+
event: filteredEvent,
|
|
677
|
+
run,
|
|
678
|
+
step,
|
|
679
|
+
hook,
|
|
680
|
+
wait,
|
|
681
|
+
events,
|
|
682
|
+
};
|
|
683
|
+
},
|
|
684
|
+
async get(runId, eventId, params) {
|
|
685
|
+
const compositeKey = `${runId}-${eventId}`;
|
|
686
|
+
const event = await readJSONWithFallback(basedir, 'events', compositeKey, EventSchema, tag);
|
|
687
|
+
if (!event) {
|
|
688
|
+
throw new Error(`Event ${eventId} in run ${runId} not found`);
|
|
689
|
+
}
|
|
690
|
+
const resolveData = params?.resolveData ?? DEFAULT_RESOLVE_DATA_OPTION;
|
|
691
|
+
return stripEventDataRefs(event, resolveData);
|
|
692
|
+
},
|
|
693
|
+
async list(params) {
|
|
694
|
+
const { runId } = params;
|
|
695
|
+
const resolveData = params.resolveData ?? DEFAULT_RESOLVE_DATA_OPTION;
|
|
696
|
+
const result = await paginatedFileSystemQuery({
|
|
697
|
+
directory: path.join(basedir, 'events'),
|
|
698
|
+
schema: EventSchema,
|
|
699
|
+
filePrefix: `${runId}-`,
|
|
700
|
+
// Events in chronological order (oldest first) by default,
|
|
701
|
+
// different from the default for other list calls.
|
|
702
|
+
sortOrder: params.pagination?.sortOrder ?? 'asc',
|
|
703
|
+
limit: params.pagination?.limit,
|
|
704
|
+
cursor: params.pagination?.cursor,
|
|
705
|
+
getCreatedAt: getObjectCreatedAt('evnt'),
|
|
706
|
+
getId: (event) => event.eventId,
|
|
707
|
+
});
|
|
708
|
+
// If resolveData is "none", remove eventData from events
|
|
709
|
+
if (resolveData === 'none') {
|
|
710
|
+
return {
|
|
711
|
+
...result,
|
|
712
|
+
data: result.data.map((event) => stripEventDataRefs(event, resolveData)),
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
return result;
|
|
716
|
+
},
|
|
717
|
+
async listByCorrelationId(params) {
|
|
718
|
+
const correlationId = params.correlationId;
|
|
719
|
+
const resolveData = params.resolveData ?? DEFAULT_RESOLVE_DATA_OPTION;
|
|
720
|
+
const result = await paginatedFileSystemQuery({
|
|
721
|
+
directory: path.join(basedir, 'events'),
|
|
722
|
+
schema: EventSchema,
|
|
723
|
+
// No filePrefix - search all events
|
|
724
|
+
filter: (event) => event.correlationId === correlationId,
|
|
725
|
+
// Events in chronological order (oldest first) by default,
|
|
726
|
+
// different from the default for other list calls.
|
|
727
|
+
sortOrder: params.pagination?.sortOrder ?? 'asc',
|
|
728
|
+
limit: params.pagination?.limit,
|
|
729
|
+
cursor: params.pagination?.cursor,
|
|
730
|
+
getCreatedAt: getObjectCreatedAt('evnt'),
|
|
731
|
+
getId: (event) => event.eventId,
|
|
732
|
+
});
|
|
733
|
+
// If resolveData is "none", remove eventData from events
|
|
734
|
+
if (resolveData === 'none') {
|
|
735
|
+
return {
|
|
736
|
+
...result,
|
|
737
|
+
data: result.data.map((event) => stripEventDataRefs(event, resolveData)),
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
return result;
|
|
741
|
+
},
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
//# sourceMappingURL=events-storage.js.map
|