@rivetkit/workflow-engine 3.0.6 → 3.1.0-rc.1
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/tsup/{chunk-JMOI2OHA.js → chunk-NIKZ2NOQ.js} +318 -125
- package/dist/tsup/chunk-NIKZ2NOQ.js.map +1 -0
- package/dist/tsup/{chunk-L4MA54VF.cjs → chunk-Z4F6P6M4.cjs} +318 -125
- package/dist/tsup/chunk-Z4F6P6M4.cjs.map +1 -0
- package/dist/tsup/index.cjs +2 -2
- package/dist/tsup/index.d.cts +15 -4
- package/dist/tsup/index.d.ts +15 -4
- package/dist/tsup/index.js +1 -1
- package/dist/tsup/testing.cjs +25 -25
- package/dist/tsup/testing.js +1 -1
- package/package.json +1 -1
- package/src/context.ts +305 -112
- package/src/storage.ts +117 -25
- package/src/types.ts +1 -1
- package/src/utils.ts +4 -0
- package/dist/tsup/chunk-JMOI2OHA.js.map +0 -1
- package/dist/tsup/chunk-L4MA54VF.cjs.map +0 -1
package/src/storage.ts
CHANGED
|
@@ -232,12 +232,64 @@ export interface PendingDeletions {
|
|
|
232
232
|
ranges: { start: Uint8Array; end: Uint8Array }[];
|
|
233
233
|
}
|
|
234
234
|
|
|
235
|
+
interface FlushQueue {
|
|
236
|
+
/** Settles when every save started or queued so far has finished. */
|
|
237
|
+
tail: Promise<void>;
|
|
238
|
+
/** The next save, while it has not started. Other callers can join it. */
|
|
239
|
+
waiting?: Promise<void>;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Saves of one workflow run one at a time, so an older snapshot never lands
|
|
243
|
+
// after a newer one.
|
|
244
|
+
const flushQueues = new WeakMap<Storage, FlushQueue>();
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Save workflow history in order, preserving mutations made during each write.
|
|
248
|
+
*
|
|
249
|
+
* A save that has not started reads storage when it starts, so it also saves
|
|
250
|
+
* the changes of callers that join it. They share its result. Saves with
|
|
251
|
+
* deletions do not share, so pruning runs in order with the writes around it.
|
|
252
|
+
*/
|
|
253
|
+
export function flush(
|
|
254
|
+
storage: Storage,
|
|
255
|
+
driver: EngineDriver,
|
|
256
|
+
onHistoryUpdated?: () => void,
|
|
257
|
+
pendingDeletions?: PendingDeletions,
|
|
258
|
+
): Promise<void> {
|
|
259
|
+
let queue = flushQueues.get(storage);
|
|
260
|
+
if (!queue) {
|
|
261
|
+
queue = { tail: Promise.resolve() };
|
|
262
|
+
flushQueues.set(storage, queue);
|
|
263
|
+
}
|
|
264
|
+
if (queue.waiting && !pendingDeletions) {
|
|
265
|
+
return queue.waiting;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const activeQueue = queue;
|
|
269
|
+
const next: Promise<void> = activeQueue.tail.then(() => {
|
|
270
|
+
if (activeQueue.waiting === next) {
|
|
271
|
+
activeQueue.waiting = undefined;
|
|
272
|
+
}
|
|
273
|
+
return flushSnapshot(
|
|
274
|
+
storage,
|
|
275
|
+
driver,
|
|
276
|
+
onHistoryUpdated,
|
|
277
|
+
pendingDeletions,
|
|
278
|
+
);
|
|
279
|
+
});
|
|
280
|
+
if (!pendingDeletions) {
|
|
281
|
+
activeQueue.waiting = next;
|
|
282
|
+
}
|
|
283
|
+
activeQueue.tail = next.catch(() => undefined);
|
|
284
|
+
return next;
|
|
285
|
+
}
|
|
286
|
+
|
|
235
287
|
/**
|
|
236
288
|
* Flush all dirty data to the driver. Optionally includes pending
|
|
237
289
|
* deletions so that history pruning happens alongside the
|
|
238
290
|
* state write.
|
|
239
291
|
*/
|
|
240
|
-
|
|
292
|
+
async function flushSnapshot(
|
|
241
293
|
storage: Storage,
|
|
242
294
|
driver: EngineDriver,
|
|
243
295
|
onHistoryUpdated?: () => void,
|
|
@@ -320,36 +372,51 @@ export async function flush(
|
|
|
320
372
|
});
|
|
321
373
|
}
|
|
322
374
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
// Apply pending deletions after the batch write. These are collected
|
|
334
|
-
// by collectLoopPruning so pruning happens alongside the state write.
|
|
335
|
-
if (pendingDeletions) {
|
|
336
|
-
const didChange = await runDeletes(driver, pendingDeletions);
|
|
337
|
-
if (didChange) {
|
|
338
|
-
historyUpdated = true;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// Update flushed tracking after successful write
|
|
375
|
+
// Record what this write contains before awaiting it. A concurrent branch
|
|
376
|
+
// can change storage during the write, and those changes must stay dirty.
|
|
377
|
+
const savedNameCount = storage.nameRegistry.length;
|
|
378
|
+
const savedState = storage.state;
|
|
379
|
+
const savedOutput = storage.output;
|
|
380
|
+
const savedError = storage.error;
|
|
343
381
|
for (const entry of dirtyEntries) {
|
|
344
382
|
entry.dirty = false;
|
|
345
383
|
}
|
|
346
384
|
for (const metadata of dirtyMetadata) {
|
|
347
385
|
metadata.dirty = false;
|
|
348
386
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
387
|
+
try {
|
|
388
|
+
if (writes.length > 0) {
|
|
389
|
+
if (driver.atomicBatch) {
|
|
390
|
+
await driver.batch(writes);
|
|
391
|
+
} else {
|
|
392
|
+
for (const chunk of splitBatchWrites(writes)) {
|
|
393
|
+
await driver.batch(chunk);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Apply pending deletions after the batch write. These are collected
|
|
399
|
+
// by collectLoopPruning so pruning happens alongside the state write.
|
|
400
|
+
if (pendingDeletions) {
|
|
401
|
+
const didChange = await runDeletes(driver, pendingDeletions);
|
|
402
|
+
if (didChange) {
|
|
403
|
+
historyUpdated = true;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
} catch (error) {
|
|
407
|
+
for (const entry of dirtyEntries) {
|
|
408
|
+
entry.dirty = true;
|
|
409
|
+
}
|
|
410
|
+
for (const metadata of dirtyMetadata) {
|
|
411
|
+
metadata.dirty = true;
|
|
412
|
+
}
|
|
413
|
+
throw error;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
storage.flushedNameCount = savedNameCount;
|
|
417
|
+
storage.flushedState = savedState;
|
|
418
|
+
storage.flushedOutput = savedOutput;
|
|
419
|
+
storage.flushedError = savedError;
|
|
353
420
|
|
|
354
421
|
if (historyUpdated && onHistoryUpdated) {
|
|
355
422
|
onHistoryUpdated();
|
|
@@ -436,6 +503,31 @@ async function runDeletes(
|
|
|
436
503
|
return metadataOps.length > 0 || historyOps.length > 0;
|
|
437
504
|
}
|
|
438
505
|
|
|
506
|
+
/**
|
|
507
|
+
* Remove the entries under a location prefix from memory, and return the
|
|
508
|
+
* deletions that remove them from the driver. Pass the result to `flush`, so
|
|
509
|
+
* the deletions run only after the writes that must land first.
|
|
510
|
+
*/
|
|
511
|
+
export function takeEntriesWithPrefix(
|
|
512
|
+
storage: Storage,
|
|
513
|
+
prefixLocation: Location,
|
|
514
|
+
): PendingDeletions {
|
|
515
|
+
const metadataKeys: Uint8Array[] = [];
|
|
516
|
+
for (const [key, entry] of [...storage.history.entries]) {
|
|
517
|
+
if (!isLocationPrefix(prefixLocation, entry.location)) {
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
metadataKeys.push(buildEntryMetadataKey(entry.id));
|
|
521
|
+
storage.entryMetadata.delete(entry.id);
|
|
522
|
+
storage.history.entries.delete(key);
|
|
523
|
+
}
|
|
524
|
+
return {
|
|
525
|
+
prefixes: [buildHistoryPrefix(prefixLocation)],
|
|
526
|
+
keys: metadataKeys,
|
|
527
|
+
ranges: [],
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
|
|
439
531
|
/**
|
|
440
532
|
* Delete entries with a given location prefix (used for loop forgetting).
|
|
441
533
|
* Also cleans up associated metadata from both memory and driver.
|
package/src/types.ts
CHANGED
|
@@ -410,7 +410,7 @@ export interface StepConfig<T> {
|
|
|
410
410
|
retryBackoffBase?: number;
|
|
411
411
|
/** Maximum delay in ms for exponential backoff (default: 30000). */
|
|
412
412
|
retryBackoffMax?: number;
|
|
413
|
-
/** Timeout in ms for step execution (default: 30000). Set to 0 to disable. */
|
|
413
|
+
/** Timeout in ms for step execution (default: 30000). Set to 0 or Infinity to disable. */
|
|
414
414
|
timeout?: number;
|
|
415
415
|
/** If true, step timeouts retry like any other error instead of failing immediately as critical. Default: false. */
|
|
416
416
|
retryOnTimeout?: boolean;
|
package/src/utils.ts
CHANGED
|
@@ -9,6 +9,10 @@ const TIMEOUT_MAX = 2147483647;
|
|
|
9
9
|
|
|
10
10
|
export type LongTimeoutHandle = { abort: () => void };
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Like setTimeout, but waits out delays over 2^31-1 ms (about 24.8 days).
|
|
14
|
+
* setTimeout fires such delays after 1 ms, so this chains shorter timers.
|
|
15
|
+
*/
|
|
12
16
|
export function setLongTimeout(
|
|
13
17
|
listener: () => void,
|
|
14
18
|
after: number,
|