@workflow/web-shared 5.0.5 → 5.0.7
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/components/event-list-view.d.ts.map +1 -1
- package/dist/components/event-list-view.js +14 -4
- package/dist/components/sidebar/attribute-panel.d.ts.map +1 -1
- package/dist/components/sidebar/attribute-panel.js +16 -2
- package/dist/components/workflow-traces/trace-span-construction.d.ts.map +1 -1
- package/dist/components/workflow-traces/trace-span-construction.js +8 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/lib/duplicate-events.d.ts +6 -6
- package/dist/lib/duplicate-events.d.ts.map +1 -1
- package/dist/lib/duplicate-events.js +18 -18
- package/dist/lib/event-materialization.d.ts +3 -1
- package/dist/lib/event-materialization.d.ts.map +1 -1
- package/dist/lib/event-materialization.js +11 -4
- package/dist/lib/hydration.d.ts.map +1 -1
- package/dist/lib/hydration.js +11 -1
- package/dist/lib/run-limit-guidance.d.ts +43 -0
- package/dist/lib/run-limit-guidance.d.ts.map +1 -0
- package/dist/lib/run-limit-guidance.js +72 -0
- package/package.json +4 -4
- package/src/components/event-list-view.test.ts +51 -0
- package/src/components/event-list-view.tsx +16 -3
- package/src/components/sidebar/attribute-panel.tsx +17 -1
- package/src/components/workflow-traces/trace-span-construction.test.ts +28 -0
- package/src/components/workflow-traces/trace-span-construction.ts +10 -3
- package/src/index.ts +7 -0
- package/src/lib/duplicate-events.test.ts +8 -11
- package/src/lib/duplicate-events.ts +18 -16
- package/src/lib/event-materialization.test.ts +19 -0
- package/src/lib/event-materialization.ts +11 -3
- package/src/lib/hydration.ts +12 -0
- package/src/lib/run-limit-guidance.test.ts +90 -0
- package/src/lib/run-limit-guidance.ts +111 -0
|
@@ -172,9 +172,13 @@ export const stepEventsToStepEntity = (
|
|
|
172
172
|
(event) => event.eventType === 'step_created'
|
|
173
173
|
);
|
|
174
174
|
|
|
175
|
-
// V1 runs don't emit step_created events
|
|
176
|
-
//
|
|
175
|
+
// V1 runs don't emit step_created events, and a World may record an inline
|
|
176
|
+
// step's creation on its `step_started` alone. Fall back to the earliest
|
|
177
|
+
// event in the group so we can still build a step span.
|
|
177
178
|
const anchorEvent = createdEvent ?? events[0];
|
|
179
|
+
const firstStartedEvent = events.find(
|
|
180
|
+
(event) => event.eventType === 'step_started'
|
|
181
|
+
);
|
|
178
182
|
if (!anchorEvent) {
|
|
179
183
|
return null;
|
|
180
184
|
}
|
|
@@ -218,7 +222,10 @@ export const stepEventsToStepEntity = (
|
|
|
218
222
|
return {
|
|
219
223
|
stepId: anchorEvent.correlationId ?? '',
|
|
220
224
|
runId: anchorEvent.runId,
|
|
221
|
-
stepName:
|
|
225
|
+
stepName:
|
|
226
|
+
createdEvent?.eventData?.stepName ??
|
|
227
|
+
firstStartedEvent?.eventData?.stepName ??
|
|
228
|
+
'',
|
|
222
229
|
status,
|
|
223
230
|
attempt,
|
|
224
231
|
createdAt: getEventTimestamp(anchorEvent) ?? anchorEvent.createdAt,
|
package/src/index.ts
CHANGED
|
@@ -59,6 +59,13 @@ export {
|
|
|
59
59
|
STREAM_REF_TYPE,
|
|
60
60
|
truncateId,
|
|
61
61
|
} from './lib/hydration';
|
|
62
|
+
export {
|
|
63
|
+
getRunLimitGuidance,
|
|
64
|
+
isQuickJSSnapshotRun,
|
|
65
|
+
type RunLimitGuidance,
|
|
66
|
+
type RunLimitGuidanceInput,
|
|
67
|
+
type RunLimitGuidanceKind,
|
|
68
|
+
} from './lib/run-limit-guidance';
|
|
62
69
|
export {
|
|
63
70
|
isSealedNoopEvent,
|
|
64
71
|
SEALED_EVENT_MESSAGE,
|
|
@@ -80,17 +80,18 @@ describe('findDuplicateEventIds', () => {
|
|
|
80
80
|
).toEqual(new Set([createdAgain.eventId, startedAgain.eventId]));
|
|
81
81
|
});
|
|
82
82
|
|
|
83
|
-
it('
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
// over, and the UI must not present it as a settled repeat.
|
|
83
|
+
it('reads past a start after the outcome even if it repeats nothing', () => {
|
|
84
|
+
// Terminal-inert: once the step's outcome is in the log, every later
|
|
85
|
+
// event for the step is read past, the same as the runtime does.
|
|
87
86
|
const events = [
|
|
88
87
|
event('step_created', { correlationId: 'step_a' }),
|
|
89
88
|
event('step_completed', { correlationId: 'step_a' }),
|
|
90
89
|
event('step_started', { correlationId: 'step_a' }),
|
|
91
90
|
];
|
|
92
91
|
|
|
93
|
-
expect(findDuplicateEventIds(events, COMPLETE)).toEqual(
|
|
92
|
+
expect(findDuplicateEventIds(events, COMPLETE)).toEqual(
|
|
93
|
+
new Set([events[2].eventId])
|
|
94
|
+
);
|
|
94
95
|
});
|
|
95
96
|
|
|
96
97
|
it('treats completed and failed as one terminal class', () => {
|
|
@@ -266,11 +267,7 @@ describe('findDuplicateEventIds', () => {
|
|
|
266
267
|
).toEqual(new Set());
|
|
267
268
|
});
|
|
268
269
|
|
|
269
|
-
it('
|
|
270
|
-
// The step finished without a step_started, so the runtime reports
|
|
271
|
-
// divergence on the first trailing start and exits. The second start and
|
|
272
|
-
// the wait's repeat after it went unread, and neither is a repeat the run
|
|
273
|
-
// passed over. The wait's repeat before it still is.
|
|
270
|
+
it('reads past every event of an entity after it closed', () => {
|
|
274
271
|
const events = [
|
|
275
272
|
event('wait_created', { correlationId: 'wait_a' }),
|
|
276
273
|
event('wait_completed', { correlationId: 'wait_a' }),
|
|
@@ -283,7 +280,7 @@ describe('findDuplicateEventIds', () => {
|
|
|
283
280
|
];
|
|
284
281
|
|
|
285
282
|
expect(findDuplicateEventIds(events, COMPLETE)).toEqual(
|
|
286
|
-
new Set([events[
|
|
283
|
+
new Set([2, 5, 6, 7].map((index) => events[index].eventId))
|
|
287
284
|
);
|
|
288
285
|
});
|
|
289
286
|
|
|
@@ -21,9 +21,9 @@ import {
|
|
|
21
21
|
* registered callback has declined it, and a callback registered for a
|
|
22
22
|
* still-open entity legitimately claims a repeat (each retry of a step writes
|
|
23
23
|
* another `step_started`, and a live step consumer absorbs a second
|
|
24
|
-
* `step_created`). So
|
|
24
|
+
* `step_created`). So an event counts here only once a terminal event for the
|
|
25
25
|
* same entity sits earlier in the log, which is the point past which no
|
|
26
|
-
* consumer remains.
|
|
26
|
+
* consumer remains, and from then on every event for that entity counts.
|
|
27
27
|
*/
|
|
28
28
|
|
|
29
29
|
/** Classes with no entity to close first: the log records one per run. */
|
|
@@ -107,6 +107,15 @@ function foldDuplicates(ordered: readonly Event[]): Set<string> {
|
|
|
107
107
|
const closedEntities = new Set<string>();
|
|
108
108
|
|
|
109
109
|
for (const event of ordered) {
|
|
110
|
+
// Terminal-inert: once an entity's terminal event is in the log, its
|
|
111
|
+
// consumer is gone and every later event under the same correlation id
|
|
112
|
+
// is read past, whatever its class (a start or a retry after the step's
|
|
113
|
+
// outcome included). This is the rule the runtime applies too.
|
|
114
|
+
if (event.correlationId && closedEntities.has(event.correlationId)) {
|
|
115
|
+
duplicates.add(event.eventId);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
110
119
|
// Shared with the runtime's own duplicate detection, deliberately: an
|
|
111
120
|
// event it tracks under no class is one it never reads past, so naming it
|
|
112
121
|
// here would grey out an event the run acted on.
|
|
@@ -116,26 +125,19 @@ function foldDuplicates(ordered: readonly Event[]): Set<string> {
|
|
|
116
125
|
const { eventClass, entity } = classification;
|
|
117
126
|
const classKey = `${eventClass}:${entity}`;
|
|
118
127
|
const repeatsClass = seenClasses.has(classKey);
|
|
119
|
-
const entityWasClosed = closedEntities.has(entity);
|
|
120
128
|
|
|
121
129
|
if (TERMINAL_EVENT_CLASSES.has(eventClass)) {
|
|
122
130
|
closedEntities.add(entity);
|
|
123
131
|
}
|
|
124
132
|
|
|
125
133
|
if (!repeatsClass) {
|
|
126
|
-
// First of its class, but the entity already finished: no consumer is
|
|
127
|
-
// left to take it and it repeats nothing, so the runtime reports
|
|
128
|
-
// divergence here and exits. Everything past this point went unread, so
|
|
129
|
-
// the fold stops with it rather than recording the class and presenting
|
|
130
|
-
// a later event of it as a repeat the run passed over.
|
|
131
|
-
if (entityWasClosed) break;
|
|
132
134
|
seenClasses.add(classKey);
|
|
133
135
|
continue;
|
|
134
136
|
}
|
|
135
137
|
|
|
136
138
|
// The entity is still open, so a consumer is registered for it and takes
|
|
137
139
|
// this event: another attempt, not a repeat read past.
|
|
138
|
-
if (
|
|
140
|
+
if (SINGLETON_EVENT_CLASSES.has(eventClass)) {
|
|
139
141
|
duplicates.add(event.eventId);
|
|
140
142
|
}
|
|
141
143
|
}
|
|
@@ -144,8 +146,9 @@ function foldDuplicates(ordered: readonly Event[]): Set<string> {
|
|
|
144
146
|
}
|
|
145
147
|
|
|
146
148
|
/**
|
|
147
|
-
* The IDs of the events in `events`
|
|
148
|
-
*
|
|
149
|
+
* The IDs of the events in `events` the run reads past: every event recorded
|
|
150
|
+
* for an entity after that entity's terminal event, and a repeat of a class
|
|
151
|
+
* that records once per run.
|
|
149
152
|
*
|
|
150
153
|
* `isCompleteHistory` must be false whenever the caller holds a subset of the
|
|
151
154
|
* run's log: one page of a paginated list, or the result of a search. Which
|
|
@@ -153,10 +156,9 @@ function foldDuplicates(ordered: readonly Event[]): Set<string> {
|
|
|
153
156
|
* subset the earlier event may be missing, and the fold would report
|
|
154
157
|
* the surviving one. Nothing is classified in that case.
|
|
155
158
|
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* diverged, since the run exited there and read no further.
|
|
159
|
+
* A log whose order cannot be recovered from the events (see
|
|
160
|
+
* {@link hasKnowableLogOrder}) also makes the answer unknowable and yields the
|
|
161
|
+
* same empty result.
|
|
160
162
|
*/
|
|
161
163
|
export function findDuplicateEventIds(
|
|
162
164
|
events: readonly Event[],
|
|
@@ -39,6 +39,25 @@ describe('materializeSteps', () => {
|
|
|
39
39
|
expect(step.completedAt?.getTime()).toBe(BASE_TIME + 5000);
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
+
it('materializes a step whose creation is recorded on its step_started', () => {
|
|
43
|
+
const started = event('step_started', { correlationId: 'step_a', at: 2 });
|
|
44
|
+
const events = [
|
|
45
|
+
{ ...started, eventData: { stepName: 'doWork' } } as Event,
|
|
46
|
+
event('step_completed', { correlationId: 'step_a', at: 5 }),
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
const [step] = materializeSteps(events);
|
|
50
|
+
|
|
51
|
+
expect(step).toMatchObject({
|
|
52
|
+
stepId: 'step_a',
|
|
53
|
+
stepName: 'doWork',
|
|
54
|
+
status: 'completed',
|
|
55
|
+
attempt: 1,
|
|
56
|
+
});
|
|
57
|
+
expect(step.createdAt.getTime()).toBe(BASE_TIME + 2000);
|
|
58
|
+
expect(step.startedAt?.getTime()).toBe(BASE_TIME + 2000);
|
|
59
|
+
});
|
|
60
|
+
|
|
42
61
|
it('counts one attempt per start across retries', () => {
|
|
43
62
|
const events = [
|
|
44
63
|
event('step_created', { correlationId: 'step_a', at: 1 }),
|
|
@@ -105,7 +105,9 @@ function getEventTimestamp(event: Event | undefined): Date | undefined {
|
|
|
105
105
|
* Group step_* events by correlationId and build Step-like entities.
|
|
106
106
|
*
|
|
107
107
|
* Handles partial event lists gracefully: a step may only have a
|
|
108
|
-
* step_created event with no completion yet.
|
|
108
|
+
* step_created event with no completion yet. A step whose creation the World
|
|
109
|
+
* recorded on its first `step_started` (no `step_created` in the log) is
|
|
110
|
+
* anchored on that start instead.
|
|
109
111
|
*
|
|
110
112
|
* The derived status and timestamps come from the events the run acted on. A
|
|
111
113
|
* repeat of a class the log already records is read past by every replay, so
|
|
@@ -127,7 +129,12 @@ export function materializeSteps(
|
|
|
127
129
|
const steps: MaterializedStep[] = [];
|
|
128
130
|
|
|
129
131
|
for (const [correlationId, stepEvents] of groups) {
|
|
130
|
-
|
|
132
|
+
// A World may record an inline step's creation on its `step_started`
|
|
133
|
+
// alone, with no `step_created` in the log. That start is then the step's
|
|
134
|
+
// creation event.
|
|
135
|
+
const created =
|
|
136
|
+
stepEvents.find((e) => e.eventType === 'step_created') ??
|
|
137
|
+
stepEvents.find((e) => e.eventType === 'step_started');
|
|
131
138
|
if (!created) continue;
|
|
132
139
|
|
|
133
140
|
let status: StepStatus = 'pending';
|
|
@@ -168,7 +175,8 @@ export function materializeSteps(
|
|
|
168
175
|
stepId: correlationId,
|
|
169
176
|
runId: created.runId,
|
|
170
177
|
stepName:
|
|
171
|
-
created.eventType === 'step_created'
|
|
178
|
+
created.eventType === 'step_created' ||
|
|
179
|
+
created.eventType === 'step_started'
|
|
172
180
|
? (created.eventData?.stepName ?? correlationId)
|
|
173
181
|
: correlationId,
|
|
174
182
|
status,
|
package/src/lib/hydration.ts
CHANGED
|
@@ -220,6 +220,18 @@ export function getWebRevivers(): Revivers {
|
|
|
220
220
|
if (value.stack !== undefined) error.stack = value.stack;
|
|
221
221
|
return error;
|
|
222
222
|
},
|
|
223
|
+
WorkflowStartError: (value) => {
|
|
224
|
+
const opts = 'cause' in value ? { cause: value.cause } : undefined;
|
|
225
|
+
const error = new Error(value.message, opts) as Error & {
|
|
226
|
+
runId?: string;
|
|
227
|
+
stage?: string;
|
|
228
|
+
};
|
|
229
|
+
error.name = 'WorkflowStartError';
|
|
230
|
+
error.runId = value.runId;
|
|
231
|
+
error.stage = value.stage;
|
|
232
|
+
if (value.stack !== undefined) error.stack = value.stack;
|
|
233
|
+
return error;
|
|
234
|
+
},
|
|
223
235
|
RetryableError: (value) => {
|
|
224
236
|
const opts = 'cause' in value ? { cause: value.cause } : undefined;
|
|
225
237
|
const error = new Error(value.message, opts) as Error & {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
getRunLimitGuidance,
|
|
4
|
+
isQuickJSSnapshotRun,
|
|
5
|
+
} from './run-limit-guidance';
|
|
6
|
+
|
|
7
|
+
const failed = (errorCode: string, extra: Record<string, unknown> = {}) => ({
|
|
8
|
+
status: 'failed',
|
|
9
|
+
errorCode,
|
|
10
|
+
...extra,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('getRunLimitGuidance', () => {
|
|
14
|
+
it('returns null for missing, non-failed, and unrelated runs', () => {
|
|
15
|
+
expect(getRunLimitGuidance(undefined)).toBeNull();
|
|
16
|
+
expect(getRunLimitGuidance(null)).toBeNull();
|
|
17
|
+
expect(
|
|
18
|
+
getRunLimitGuidance({
|
|
19
|
+
status: 'running',
|
|
20
|
+
errorCode: 'MAX_EVENTS_EXCEEDED',
|
|
21
|
+
})
|
|
22
|
+
).toBeNull();
|
|
23
|
+
expect(getRunLimitGuidance(failed('USER_ERROR'))).toBeNull();
|
|
24
|
+
expect(getRunLimitGuidance({ status: 'failed' })).toBeNull();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('recommends WORKFLOW_VM=quickjs for node runs that hit the event limit', () => {
|
|
28
|
+
const guidance = getRunLimitGuidance(failed('MAX_EVENTS_EXCEEDED'));
|
|
29
|
+
expect(guidance).toMatchObject({
|
|
30
|
+
kind: 'max-events-exceeded',
|
|
31
|
+
envVar: 'WORKFLOW_VM=quickjs',
|
|
32
|
+
docsUrl: 'https://workflow-sdk.dev/err/max-events-exceeded',
|
|
33
|
+
});
|
|
34
|
+
expect(
|
|
35
|
+
getRunLimitGuidance(failed('MAX_EVENTS_EXCEEDED', { workflowVm: 'node' }))
|
|
36
|
+
?.envVar
|
|
37
|
+
).toBe('WORKFLOW_VM=quickjs');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('recommends QuickJS for replay timeouts', () => {
|
|
41
|
+
expect(getRunLimitGuidance(failed('REPLAY_TIMEOUT'))).toMatchObject({
|
|
42
|
+
kind: 'replay-timeout',
|
|
43
|
+
envVar: 'WORKFLOW_VM=quickjs',
|
|
44
|
+
docsUrl: 'https://workflow-sdk.dev/err/replay-timeout',
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('suggests a snapshot threshold for QuickJS runs without one', () => {
|
|
49
|
+
for (const extra of [
|
|
50
|
+
{ workflowVm: 'quickjs' },
|
|
51
|
+
{ workflowVm: 'quickjs', snapshotThreshold: 0 },
|
|
52
|
+
]) {
|
|
53
|
+
const guidance = getRunLimitGuidance(
|
|
54
|
+
failed('MAX_EVENTS_EXCEEDED', extra)
|
|
55
|
+
);
|
|
56
|
+
expect(guidance?.envVar).toBe('WORKFLOW_SNAPSHOT_THRESHOLD=1000');
|
|
57
|
+
expect(guidance?.description).toContain('QuickJS');
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('returns null for runs already on QuickJS with snapshotting', () => {
|
|
62
|
+
const hydrated = { workflowVm: 'quickjs', snapshotThreshold: 1000 };
|
|
63
|
+
expect(
|
|
64
|
+
getRunLimitGuidance(failed('MAX_EVENTS_EXCEEDED', hydrated))
|
|
65
|
+
).toBeNull();
|
|
66
|
+
expect(getRunLimitGuidance(failed('REPLAY_TIMEOUT', hydrated))).toBeNull();
|
|
67
|
+
// Raw (unhydrated) runs carry the policy in executionContext.
|
|
68
|
+
expect(
|
|
69
|
+
getRunLimitGuidance(
|
|
70
|
+
failed('MAX_EVENTS_EXCEEDED', { executionContext: hydrated })
|
|
71
|
+
)
|
|
72
|
+
).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('isQuickJSSnapshotRun', () => {
|
|
77
|
+
it('requires the QuickJS VM and a positive safe-integer threshold', () => {
|
|
78
|
+
expect(
|
|
79
|
+
isQuickJSSnapshotRun({ workflowVm: 'quickjs', snapshotThreshold: 1 })
|
|
80
|
+
).toBe(true);
|
|
81
|
+
expect(isQuickJSSnapshotRun({ workflowVm: 'quickjs' })).toBe(false);
|
|
82
|
+
expect(
|
|
83
|
+
isQuickJSSnapshotRun({ workflowVm: 'quickjs', snapshotThreshold: 1.5 })
|
|
84
|
+
).toBe(false);
|
|
85
|
+
expect(
|
|
86
|
+
isQuickJSSnapshotRun({ workflowVm: 'node', snapshotThreshold: 1000 })
|
|
87
|
+
).toBe(false);
|
|
88
|
+
expect(isQuickJSSnapshotRun({})).toBe(false);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Guidance for runs that failed because they outgrew the default per-run
|
|
3
|
+
* event limit or replay budget. Pure (no React) so every observability UI
|
|
4
|
+
* (the self-hosted web UI and the Vercel dashboard) can share the decision
|
|
5
|
+
* and the copy while rendering it with its own components.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Error codes this guidance applies to (`RUN_ERROR_CODES` values). */
|
|
9
|
+
const MAX_EVENTS_EXCEEDED = 'MAX_EVENTS_EXCEEDED';
|
|
10
|
+
const REPLAY_TIMEOUT = 'REPLAY_TIMEOUT';
|
|
11
|
+
|
|
12
|
+
const DOCS_BASE_URL = 'https://workflow-sdk.dev/err';
|
|
13
|
+
|
|
14
|
+
export type RunLimitGuidanceKind = 'max-events-exceeded' | 'replay-timeout';
|
|
15
|
+
|
|
16
|
+
export interface RunLimitGuidance {
|
|
17
|
+
kind: RunLimitGuidanceKind;
|
|
18
|
+
/** Short heading, e.g. for an alert title. */
|
|
19
|
+
title: string;
|
|
20
|
+
/** One or two sentences explaining what to do. Plain text. */
|
|
21
|
+
description: string;
|
|
22
|
+
/** The env var assignment to suggest, for rendering as code. */
|
|
23
|
+
envVar: string;
|
|
24
|
+
/** Docs page for the error code. */
|
|
25
|
+
docsUrl: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The run fields this guidance reads. Accepts both a hydrated run (where
|
|
30
|
+
* `hydrateResourceIO` lifts `workflowVm` / `snapshotThreshold` out of the
|
|
31
|
+
* stripped `executionContext`) and a raw run (with `executionContext`).
|
|
32
|
+
*/
|
|
33
|
+
export interface RunLimitGuidanceInput {
|
|
34
|
+
status?: string;
|
|
35
|
+
errorCode?: string | null;
|
|
36
|
+
workflowVm?: unknown;
|
|
37
|
+
snapshotThreshold?: unknown;
|
|
38
|
+
executionContext?: unknown;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function readVmPolicy(run: RunLimitGuidanceInput): {
|
|
42
|
+
workflowVm: unknown;
|
|
43
|
+
snapshotThreshold: unknown;
|
|
44
|
+
} {
|
|
45
|
+
const ctx =
|
|
46
|
+
run.executionContext && typeof run.executionContext === 'object'
|
|
47
|
+
? (run.executionContext as Record<string, unknown>)
|
|
48
|
+
: undefined;
|
|
49
|
+
return {
|
|
50
|
+
workflowVm: run.workflowVm ?? ctx?.workflowVm,
|
|
51
|
+
snapshotThreshold: run.snapshotThreshold ?? ctx?.snapshotThreshold,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Whether the run was started on the QuickJS VM with snapshotting enabled,
|
|
57
|
+
* which exempts it from the default per-run event limit. Mirrors the World
|
|
58
|
+
* policy (`isEventLimitExemptRun` in `@workflow/world`).
|
|
59
|
+
*/
|
|
60
|
+
export function isQuickJSSnapshotRun(run: RunLimitGuidanceInput): boolean {
|
|
61
|
+
const { workflowVm, snapshotThreshold } = readVmPolicy(run);
|
|
62
|
+
return (
|
|
63
|
+
workflowVm === 'quickjs' &&
|
|
64
|
+
typeof snapshotThreshold === 'number' &&
|
|
65
|
+
Number.isSafeInteger(snapshotThreshold) &&
|
|
66
|
+
snapshotThreshold > 0
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns guidance to show for a failed run whose error code is
|
|
72
|
+
* `MAX_EVENTS_EXCEEDED` or `REPLAY_TIMEOUT` and that wasn't already running
|
|
73
|
+
* on the QuickJS VM with snapshotting. Returns `null` otherwise.
|
|
74
|
+
*/
|
|
75
|
+
export function getRunLimitGuidance(
|
|
76
|
+
run: RunLimitGuidanceInput | null | undefined
|
|
77
|
+
): RunLimitGuidance | null {
|
|
78
|
+
if (!run || run.status !== 'failed') return null;
|
|
79
|
+
const code = run.errorCode;
|
|
80
|
+
if (code !== MAX_EVENTS_EXCEEDED && code !== REPLAY_TIMEOUT) return null;
|
|
81
|
+
// Already on the recommended configuration; switching wouldn't help.
|
|
82
|
+
if (isQuickJSSnapshotRun(run)) return null;
|
|
83
|
+
|
|
84
|
+
const { workflowVm } = readVmPolicy(run);
|
|
85
|
+
const onQuickJS = workflowVm === 'quickjs';
|
|
86
|
+
|
|
87
|
+
const remediation = onQuickJS
|
|
88
|
+
? "This run used the QuickJS VM, but a snapshot threshold wasn't recorded when it started (snapshotting was disabled, or the run was started by an older SDK), so the default event limit applied. Set a positive WORKFLOW_SNAPSHOT_THRESHOLD in the environment that starts runs, or upgrade the workflow package, so new runs are exempt."
|
|
89
|
+
: 'Run long-running workflows on the QuickJS VM with snapshotting. Snapshotted runs resume without replaying the full event log and are exempt from the default per-run event limit. Set this in the environment that starts runs; only new runs are affected.';
|
|
90
|
+
|
|
91
|
+
if (code === MAX_EVENTS_EXCEEDED) {
|
|
92
|
+
return {
|
|
93
|
+
kind: 'max-events-exceeded',
|
|
94
|
+
title: 'This run reached the per-run event limit',
|
|
95
|
+
description: remediation,
|
|
96
|
+
envVar: onQuickJS
|
|
97
|
+
? 'WORKFLOW_SNAPSHOT_THRESHOLD=1000'
|
|
98
|
+
: 'WORKFLOW_VM=quickjs',
|
|
99
|
+
docsUrl: `${DOCS_BASE_URL}/max-events-exceeded`,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
kind: 'replay-timeout',
|
|
104
|
+
title: 'This run exceeded the workflow replay time limit',
|
|
105
|
+
description: `Replay time grows with the size of the event log. ${remediation}`,
|
|
106
|
+
envVar: onQuickJS
|
|
107
|
+
? 'WORKFLOW_SNAPSHOT_THRESHOLD=1000'
|
|
108
|
+
: 'WORKFLOW_VM=quickjs',
|
|
109
|
+
docsUrl: `${DOCS_BASE_URL}/replay-timeout`,
|
|
110
|
+
};
|
|
111
|
+
}
|