@workflow/world-postgres 5.0.0-beta.3 → 5.0.0-beta.5
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/README.md +3 -3
- package/dist/drizzle/schema.d.ts +38 -10
- package/dist/drizzle/schema.d.ts.map +1 -1
- package/dist/drizzle/schema.js +34 -4
- package/dist/drizzle/schema.js.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/queue.d.ts.map +1 -1
- package/dist/queue.js +10 -1
- package/dist/queue.js.map +1 -1
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +76 -74
- package/dist/storage.js.map +1 -1
- package/package.json +8 -8
- package/src/drizzle/migrations/0010_add_events_entity_creation_unique_index.sql +40 -0
- package/src/drizzle/migrations/0011_add_error_code.sql +1 -0
- package/src/drizzle/migrations/0012_add_is_system.sql +1 -0
- package/src/drizzle/migrations/meta/_journal.json +21 -0
package/dist/storage.js
CHANGED
|
@@ -5,50 +5,30 @@ import { monotonicFactory } from 'ulid';
|
|
|
5
5
|
import { Schema } from './drizzle/index.js';
|
|
6
6
|
import { compact } from './util.js';
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Read helper for the deprecated `error` text column (legacy: JSON-stringified
|
|
9
|
+
* `StructuredError`). In the current event-sourced model, the `error` field on
|
|
10
|
+
* entities is `SerializedData` (Uint8Array) produced by the new error
|
|
11
|
+
* serialization pipeline; legacy text-column records pre-date that pipeline
|
|
12
|
+
* and cannot be hydrated back into the original thrown value.
|
|
13
|
+
*
|
|
14
|
+
* Returns `null` unconditionally so downstream consumers treat legacy errors
|
|
15
|
+
* as absent rather than receiving a shape that `hydrateStepError` /
|
|
16
|
+
* `hydrateRunError` can't process. Callers that need to inspect the raw
|
|
17
|
+
* legacy payload should read the `errorJson` column directly.
|
|
10
18
|
*/
|
|
11
|
-
function parseErrorJson(
|
|
12
|
-
|
|
13
|
-
return null;
|
|
14
|
-
try {
|
|
15
|
-
const parsed = JSON.parse(errorJson);
|
|
16
|
-
if (typeof parsed === 'object' && parsed.message !== undefined) {
|
|
17
|
-
return {
|
|
18
|
-
message: parsed.message,
|
|
19
|
-
stack: parsed.stack,
|
|
20
|
-
code: parsed.code,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
// Not a structured error object, treat as plain string
|
|
24
|
-
return { message: String(parsed) };
|
|
25
|
-
}
|
|
26
|
-
catch {
|
|
27
|
-
// Not JSON, treat as plain string error message
|
|
28
|
-
return { message: errorJson };
|
|
29
|
-
}
|
|
19
|
+
function parseErrorJson(_errorJson) {
|
|
20
|
+
return null;
|
|
30
21
|
}
|
|
31
22
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
23
|
+
* Pass-through helper kept for backwards compatibility with the run read path.
|
|
24
|
+
* In the current event-sourced model, `error` is already `SerializedData`
|
|
25
|
+
* (Uint8Array) on the entity, and any legacy `errorStack` / `errorCode`
|
|
26
|
+
* fields are no longer populated by the current write path.
|
|
35
27
|
*/
|
|
36
28
|
function deserializeRunError(run) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return rest;
|
|
41
|
-
}
|
|
42
|
-
// Very old legacy: separate errorStack/errorCode fields
|
|
43
|
-
const existingError = rest.error;
|
|
44
|
-
return {
|
|
45
|
-
...rest,
|
|
46
|
-
error: {
|
|
47
|
-
message: existingError?.message || '',
|
|
48
|
-
stack: existingError?.stack || errorStack,
|
|
49
|
-
code: existingError?.code || errorCode,
|
|
50
|
-
},
|
|
51
|
-
};
|
|
29
|
+
// Drop any stale legacy-only fields we might still encounter on read.
|
|
30
|
+
const { errorStack: _errorStack, ...rest } = run;
|
|
31
|
+
return rest;
|
|
52
32
|
}
|
|
53
33
|
/**
|
|
54
34
|
* Deserialize step data, mapping DB columns to interface fields.
|
|
@@ -211,7 +191,7 @@ export function createEventsStorage(drizzle) {
|
|
|
211
191
|
.limit(1)
|
|
212
192
|
.prepare('events_get_step_for_validation');
|
|
213
193
|
const getHookByToken = drizzle
|
|
214
|
-
.select({ hookId: Schema.hooks.hookId })
|
|
194
|
+
.select({ hookId: Schema.hooks.hookId, runId: Schema.hooks.runId })
|
|
215
195
|
.from(Schema.hooks)
|
|
216
196
|
.where(eq(Schema.hooks.token, sql.placeholder('token')))
|
|
217
197
|
.limit(1)
|
|
@@ -477,7 +457,7 @@ export function createEventsStorage(drizzle) {
|
|
|
477
457
|
// for concurrent invocations: replay is deterministic, so letting
|
|
478
458
|
// multiple callers proceed with the same run is safe. We skip
|
|
479
459
|
// preloaded events here because this is a rare race-condition path
|
|
480
|
-
// — the runtime falls back to
|
|
460
|
+
// — the runtime falls back to loadWorkflowRunEvents().
|
|
481
461
|
if (currentRun?.status === 'running') {
|
|
482
462
|
const [fullRun] = await drizzle
|
|
483
463
|
.select()
|
|
@@ -547,18 +527,15 @@ export function createEventsStorage(drizzle) {
|
|
|
547
527
|
// Uses conditional UPDATE to prevent failing an already-terminal run.
|
|
548
528
|
if (data.eventType === 'run_failed') {
|
|
549
529
|
const eventData = data.eventData;
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
530
|
+
// The error field is SerializedData (Uint8Array) produced by
|
|
531
|
+
// dehydrateRunError. We store it verbatim in the error_cbor column;
|
|
532
|
+
// consumers hydrate via hydrateRunError.
|
|
553
533
|
const [runValue] = await drizzle
|
|
554
534
|
.update(Schema.runs)
|
|
555
535
|
.set({
|
|
556
536
|
status: 'failed',
|
|
557
|
-
error:
|
|
558
|
-
|
|
559
|
-
stack: eventData.error?.stack,
|
|
560
|
-
code: eventData.errorCode,
|
|
561
|
-
},
|
|
537
|
+
error: eventData.error,
|
|
538
|
+
errorCode: eventData.errorCode,
|
|
562
539
|
completedAt: now,
|
|
563
540
|
updatedAt: now,
|
|
564
541
|
})
|
|
@@ -728,17 +705,14 @@ export function createEventsStorage(drizzle) {
|
|
|
728
705
|
// Uses conditional UPDATE to prevent failing an already-terminal step.
|
|
729
706
|
if (data.eventType === 'step_failed') {
|
|
730
707
|
const eventData = data.eventData;
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
708
|
+
// The error field is SerializedData (Uint8Array) produced by
|
|
709
|
+
// dehydrateStepError. We store it verbatim in the error_cbor column;
|
|
710
|
+
// consumers hydrate via hydrateStepError.
|
|
734
711
|
const [stepValue] = await drizzle
|
|
735
712
|
.update(Schema.steps)
|
|
736
713
|
.set({
|
|
737
714
|
status: 'failed',
|
|
738
|
-
error:
|
|
739
|
-
message: errorMessage,
|
|
740
|
-
stack: eventData.stack,
|
|
741
|
-
},
|
|
715
|
+
error: eventData.error,
|
|
742
716
|
completedAt: now,
|
|
743
717
|
})
|
|
744
718
|
.where(and(eq(Schema.steps.runId, effectiveRunId), eq(Schema.steps.stepId, data.correlationId), notInArray(Schema.steps.status, terminalStepStatuses)))
|
|
@@ -764,17 +738,11 @@ export function createEventsStorage(drizzle) {
|
|
|
764
738
|
// Uses conditional UPDATE to prevent retrying an already-terminal step.
|
|
765
739
|
if (data.eventType === 'step_retrying') {
|
|
766
740
|
const eventData = data.eventData;
|
|
767
|
-
const errorMessage = typeof eventData.error === 'string'
|
|
768
|
-
? eventData.error
|
|
769
|
-
: (eventData.error?.message ?? 'Unknown error');
|
|
770
741
|
const [stepValue] = await drizzle
|
|
771
742
|
.update(Schema.steps)
|
|
772
743
|
.set({
|
|
773
744
|
status: 'pending',
|
|
774
|
-
error:
|
|
775
|
-
message: errorMessage,
|
|
776
|
-
stack: eventData.stack,
|
|
777
|
-
},
|
|
745
|
+
error: eventData.error,
|
|
778
746
|
retryAfter: eventData.retryAfter,
|
|
779
747
|
})
|
|
780
748
|
.where(and(eq(Schema.steps.runId, effectiveRunId), eq(Schema.steps.stepId, data.correlationId), notInArray(Schema.steps.status, terminalStepStatuses)))
|
|
@@ -809,6 +777,7 @@ export function createEventsStorage(drizzle) {
|
|
|
809
777
|
// This allows the workflow to continue and fail gracefully when the hook is awaited
|
|
810
778
|
const conflictEventData = {
|
|
811
779
|
token: eventData.token,
|
|
780
|
+
conflictingRunId: existingHook.runId,
|
|
812
781
|
};
|
|
813
782
|
const [conflictValue] = await drizzle
|
|
814
783
|
.insert(events)
|
|
@@ -854,6 +823,7 @@ export function createEventsStorage(drizzle) {
|
|
|
854
823
|
// Propagate specVersion from the event to the hook entity
|
|
855
824
|
specVersion: effectiveSpecVersion,
|
|
856
825
|
isWebhook: eventData.isWebhook,
|
|
826
|
+
isSystem: eventData.isSystem ?? false,
|
|
857
827
|
})
|
|
858
828
|
.onConflictDoNothing()
|
|
859
829
|
.returning();
|
|
@@ -948,17 +918,49 @@ export function createEventsStorage(drizzle) {
|
|
|
948
918
|
: 'eventData' in data
|
|
949
919
|
? data.eventData
|
|
950
920
|
: undefined;
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
921
|
+
let value;
|
|
922
|
+
try {
|
|
923
|
+
[value] = await drizzle
|
|
924
|
+
.insert(events)
|
|
925
|
+
.values({
|
|
926
|
+
runId: effectiveRunId,
|
|
927
|
+
eventId,
|
|
928
|
+
correlationId: data.correlationId,
|
|
929
|
+
eventType: data.eventType,
|
|
930
|
+
eventData: storedEventData,
|
|
931
|
+
specVersion: effectiveSpecVersion,
|
|
932
|
+
})
|
|
933
|
+
.returning({ createdAt: events.createdAt });
|
|
934
|
+
}
|
|
935
|
+
catch (err) {
|
|
936
|
+
// Translate unique-violation on the entity-creation partial index
|
|
937
|
+
// (workflow_events_entity_creation_unique) into EntityConflictError
|
|
938
|
+
// so the runtime's existing dedup catch path can handle it. Without
|
|
939
|
+
// this, two concurrent invocations producing identical
|
|
940
|
+
// correlationIds (e.g. snapshot runtime deterministic ULIDs) would
|
|
941
|
+
// surface as unhandled DB errors instead of dedup signals.
|
|
942
|
+
// Drizzle wraps the underlying pg error in DrizzleQueryError; the
|
|
943
|
+
// pg error (with .code === '23505') lives on .cause. We additionally
|
|
944
|
+
// gate on the violated constraint name so other 23505 violations on
|
|
945
|
+
// these event types (e.g. the events primary key, or any future
|
|
946
|
+
// unique constraint we might add) don't get misclassified as a
|
|
947
|
+
// correlationId conflict.
|
|
948
|
+
const isEntityCreatingEvent = data.eventType === 'step_created' ||
|
|
949
|
+
data.eventType === 'hook_created' ||
|
|
950
|
+
data.eventType === 'wait_created';
|
|
951
|
+
const pgErr = err.code
|
|
952
|
+
? err
|
|
953
|
+
: (err
|
|
954
|
+
.cause ?? {});
|
|
955
|
+
const pgCode = pgErr.code;
|
|
956
|
+
const pgConstraint = pgErr.constraint;
|
|
957
|
+
if (isEntityCreatingEvent &&
|
|
958
|
+
pgCode === '23505' &&
|
|
959
|
+
pgConstraint === 'workflow_events_entity_creation_unique') {
|
|
960
|
+
throw new EntityConflictError(`${data.eventType} for correlationId "${data.correlationId}" already exists in run "${effectiveRunId}"`);
|
|
961
|
+
}
|
|
962
|
+
throw err;
|
|
963
|
+
}
|
|
962
964
|
if (!value) {
|
|
963
965
|
throw new EntityConflictError(`Event ${eventId} could not be created`);
|
|
964
966
|
}
|