@abyss-project/runtime 3.11.0 → 3.13.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.
Files changed (64) hide show
  1. package/.eslintrc.js +32 -32
  2. package/dist/definition/definition.schema.js +6 -0
  3. package/dist/definition/definition.schema.js.map +1 -1
  4. package/dist/definition/definition.types.d.ts +11 -0
  5. package/dist/definition/definition.types.js +6 -1
  6. package/dist/definition/definition.types.js.map +1 -1
  7. package/dist/engine/engine-source.js +1 -1
  8. package/dist/errors/runtime-error-code.enum.d.ts +1 -0
  9. package/dist/errors/runtime-error-code.enum.js +1 -0
  10. package/dist/errors/runtime-error-code.enum.js.map +1 -1
  11. package/dist/runner-protocol/evaluation.types.d.ts +1 -0
  12. package/dist/steps/global-references.js +7 -0
  13. package/dist/steps/global-references.js.map +1 -1
  14. package/dist/steps/step-fields.js +10 -0
  15. package/dist/steps/step-fields.js.map +1 -1
  16. package/jest.config.ts +11 -11
  17. package/package.json +1 -1
  18. package/scripts/build-engine.cjs +70 -70
  19. package/src/__tests__/engine-bundle.test.ts +121 -121
  20. package/src/__tests__/surface.test.ts +81 -81
  21. package/src/access/__tests__/host-match.test.ts +24 -24
  22. package/src/access/access.types.ts +25 -25
  23. package/src/access/host-match.ts +13 -13
  24. package/src/cost/__tests__/execution-cost.test.ts +249 -249
  25. package/src/cost/execution-cost.ts +203 -203
  26. package/src/definition/__tests__/code-step.test.ts +56 -0
  27. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  28. package/src/definition/definition.limits.ts +9 -9
  29. package/src/definition/definition.schema.ts +7 -0
  30. package/src/definition/definition.types.ts +21 -1
  31. package/src/definition/validate-definition.ts +206 -206
  32. package/src/definition/walk-steps.ts +44 -44
  33. package/src/discord/__tests__/discord-event-input.test.ts +90 -90
  34. package/src/discord/discord.types.ts +696 -696
  35. package/src/engine/__tests__/run-in-isolate.test.ts +519 -519
  36. package/src/engine/engine-entry.ts +3 -3
  37. package/src/engine/engine-global.ts +2 -2
  38. package/src/engine/run-in-isolate.ts +462 -462
  39. package/src/errors/runtime-error-code.enum.ts +5 -0
  40. package/src/index.ts +32 -32
  41. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  42. package/src/interpreter/__tests__/log-respond-resume.test.ts +271 -271
  43. package/src/interpreter/interpreter-error.ts +12 -12
  44. package/src/interpreter/interpreter.ts +483 -483
  45. package/src/interpreter/interpreter.types.ts +204 -204
  46. package/src/interpreter/sequence-path.ts +67 -67
  47. package/src/interpreter/test-host.ts +85 -85
  48. package/src/runner-protocol/__tests__/file-payload.test.ts +26 -26
  49. package/src/runner-protocol/evaluation.types.ts +2 -0
  50. package/src/runner-protocol/execution-protocol.types.ts +297 -297
  51. package/src/schedule/schedule.types.ts +100 -100
  52. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  53. package/src/server/code-runner-error.ts +12 -12
  54. package/src/server/code-runner-session.ts +310 -310
  55. package/src/server/index.ts +11 -11
  56. package/src/steps/__tests__/global-references.test.ts +140 -124
  57. package/src/steps/__tests__/step-fields.test.ts +58 -0
  58. package/src/steps/execute-step.ts +186 -186
  59. package/src/steps/global-references.ts +56 -48
  60. package/src/steps/step-access.ts +56 -56
  61. package/src/steps/step-fields.ts +10 -0
  62. package/src/steps/value-source.ts +69 -69
  63. package/tsconfig.check.json +15 -15
  64. package/tsconfig.json +35 -35
@@ -1,271 +1,271 @@
1
- import { CURRENT_FORMAT_VERSION, LogLevel, StepType } from '../../definition/definition.types';
2
- import type { RuntimeDefinition, Step } from '../../definition/definition.types';
3
- import { RuntimeErrorCode } from '../../errors/runtime-error-code.enum';
4
- import { EvaluationKind } from '../../runner-protocol/evaluation.types';
5
- import { InterpreterError } from '../interpreter-error';
6
- import { start, step } from '../interpreter';
7
- import { CURRENT_STATE_VERSION, EffectType, RunEventType, RunStatus } from '../interpreter.types';
8
- import type { RunOptions, RunState } from '../interpreter.types';
9
- import { runToCompletion } from '../test-host';
10
-
11
- const options = (overrides: Partial<RunOptions['budget']> = {}): RunOptions => ({
12
- catalog: new Map(),
13
- grants: [],
14
- budget: { maxSteps: 100, maxEffects: 100, maxWaitMs: 1_000_000, ...overrides },
15
- });
16
-
17
- const definitionOf = (steps: Step[]): RuntimeDefinition => ({
18
- formatVersion: CURRENT_FORMAT_VERSION,
19
- accesses: [],
20
- steps,
21
- });
22
-
23
- const thrownBy = (fn: () => unknown): unknown => {
24
- try {
25
- fn();
26
- } catch (error) {
27
- return error;
28
- }
29
- throw new Error('expected a throw');
30
- };
31
-
32
- describe('LOG step', () => {
33
- const log: Step = {
34
- id: 'log',
35
- type: StepType.LOG,
36
- level: LogLevel.WARN,
37
- message: 'order {{ input.id }}',
38
- data: { total: { $expression: 'input.total' }, fixed: 1 },
39
- };
40
-
41
- it('evaluates its message and data, then emits a LOG effect', () => {
42
- const definition = definitionOf([log]);
43
- const first = start(definition, { id: 7, total: 12 }, options());
44
- expect(first.effects).toEqual([
45
- expect.objectContaining({
46
- type: EffectType.EVALUATE,
47
- items: [
48
- { id: 'message', kind: EvaluationKind.TEMPLATE, source: 'order {{ input.id }}' },
49
- { id: 'data.total', kind: EvaluationKind.EXPRESSION, source: 'input.total' },
50
- ],
51
- }),
52
- ]);
53
- const second = step(
54
- definition,
55
- first.state,
56
- {
57
- type: RunEventType.EVALUATED,
58
- results: [
59
- { id: 'message', ok: true, value: 'order 7' },
60
- { id: 'data.total', ok: true, value: 12 },
61
- ],
62
- },
63
- options(),
64
- );
65
- expect(second.effects).toEqual([
66
- {
67
- type: EffectType.LOG,
68
- stepId: 'log',
69
- level: LogLevel.WARN,
70
- message: 'order 7',
71
- data: { total: 12, fixed: 1 },
72
- },
73
- ]);
74
- const done = step(definition, second.state, { type: RunEventType.LOG_WRITTEN }, options());
75
- expect(done.status).toBe(RunStatus.SUCCEEDED);
76
- expect(done.state.outputs.log).toEqual({ output: null });
77
- });
78
-
79
- it('writes a non-string message as its JSON and omits absent data', () => {
80
- const definition = definitionOf([
81
- { id: 'log', type: StepType.LOG, level: LogLevel.INFO, message: { $expression: 'input' } },
82
- ]);
83
- const first = start(definition, null, options());
84
- const second = step(
85
- definition,
86
- first.state,
87
- { type: RunEventType.EVALUATED, results: [{ id: 'message', ok: true, value: { a: 1 } }] },
88
- options(),
89
- );
90
- expect(second.effects).toEqual([
91
- { type: EffectType.LOG, stepId: 'log', level: LogLevel.INFO, message: '{"a":1}' },
92
- ]);
93
- });
94
-
95
- it('counts against the effect budget', () => {
96
- const definition = definitionOf([
97
- { id: 'a', type: StepType.LOG, level: LogLevel.INFO, message: 'a' },
98
- { id: 'b', type: StepType.LOG, level: LogLevel.INFO, message: 'b' },
99
- ]);
100
- const first = start(definition, null, options({ maxEffects: 1 }));
101
- const second = step(
102
- definition,
103
- first.state,
104
- { type: RunEventType.LOG_WRITTEN },
105
- options({ maxEffects: 1 }),
106
- );
107
- expect(second.status).toBe(RunStatus.FAILED);
108
- expect(second.state.outcome?.error).toEqual(
109
- expect.objectContaining({ code: RuntimeErrorCode.BUDGET_EXCEEDED, stepId: 'b' }),
110
- );
111
- });
112
-
113
- const threeLogs = definitionOf(
114
- ['a', 'b', 'c'].map((id) => ({
115
- id,
116
- type: StepType.LOG as const,
117
- level: LogLevel.INFO,
118
- message: id,
119
- })),
120
- );
121
-
122
- it('keeps LOG out of maxEffects when maxLogLines is given, and counts them apart', () => {
123
- const budget = { maxEffects: 0, maxLogLines: 5 };
124
- const first = start(threeLogs, null, options(budget));
125
- expect(first.status).toBe(RunStatus.WAITING);
126
- const second = step(
127
- threeLogs,
128
- first.state,
129
- { type: RunEventType.LOG_WRITTEN },
130
- options(budget),
131
- );
132
- const third = step(
133
- threeLogs,
134
- second.state,
135
- { type: RunEventType.LOG_WRITTEN },
136
- options(budget),
137
- );
138
- const done = step(threeLogs, third.state, { type: RunEventType.LOG_WRITTEN }, options(budget));
139
- expect(done.status).toBe(RunStatus.SUCCEEDED);
140
- expect(done.state.counters).toEqual({ steps: 3, effects: 0, logLines: 3 });
141
- });
142
-
143
- it('answers a LOG past maxLogLines without emitting it', () => {
144
- const budget = { maxLogLines: 1 };
145
- const first = start(threeLogs, null, options(budget));
146
- expect(first.effects).toEqual([expect.objectContaining({ type: EffectType.LOG, stepId: 'a' })]);
147
- const second = step(
148
- threeLogs,
149
- first.state,
150
- { type: RunEventType.LOG_WRITTEN },
151
- options(budget),
152
- );
153
- expect(second.status).toBe(RunStatus.SUCCEEDED);
154
- expect(second.effects).toEqual([]);
155
- expect(second.state.outputs.b).toEqual({ output: null });
156
- expect(second.state.outputs.c).toEqual({ output: null });
157
- expect(second.state.counters.logLines).toBe(1);
158
- expect(second.state.counters.droppedLogLines).toBe(2);
159
- });
160
- });
161
-
162
- describe('RESPOND step', () => {
163
- it('emits the response with string headers, and the run goes on after it', async () => {
164
- const definition = definitionOf([
165
- {
166
- id: 'reply',
167
- type: StepType.RESPOND,
168
- status: 201,
169
- headers: { 'X-Count': 3, 'Content-Type': 'application/json' },
170
- body: { ok: true },
171
- },
172
- { id: 'after', type: StepType.SET, variable: 'x', value: 1 },
173
- ]);
174
- const responses: unknown[] = [];
175
- const { state, effects } = await runToCompletion(definition, null, options(), {
176
- evaluate: () => [],
177
- respond: (effect) => {
178
- responses.push(effect.response);
179
- },
180
- });
181
- expect(effects.map((effect) => effect.type)).toEqual([EffectType.RESPOND]);
182
- expect(responses).toEqual([
183
- {
184
- status: 201,
185
- headers: { 'X-Count': '3', 'Content-Type': 'application/json' },
186
- body: { ok: true },
187
- },
188
- ]);
189
- expect(state.status).toBe(RunStatus.SUCCEEDED);
190
- expect(state.outputs.reply).toEqual({ output: null });
191
- expect(state.vars.x).toBe(1);
192
- });
193
-
194
- it('fails the step when the status does not resolve to an HTTP status', () => {
195
- const definition = definitionOf([
196
- { id: 'reply', type: StepType.RESPOND, status: { $expression: 'input.status' } },
197
- ]);
198
- const first = start(definition, null, options());
199
- const second = step(
200
- definition,
201
- first.state,
202
- { type: RunEventType.EVALUATED, results: [{ id: 'status', ok: true, value: 700 }] },
203
- options(),
204
- );
205
- expect(second.status).toBe(RunStatus.FAILED);
206
- expect(second.state.outcome?.error).toEqual(
207
- expect.objectContaining({ code: RuntimeErrorCode.EXPRESSION_ERROR, stepId: 'reply' }),
208
- );
209
- });
210
-
211
- it('refuses a RESPONSE_SENT event for another pending effect', () => {
212
- const definition = definitionOf([{ id: 'w', type: StepType.WAIT, durationMs: 10 }]);
213
- const first = start(definition, null, options());
214
- const error = thrownBy(() =>
215
- step(definition, first.state, { type: RunEventType.RESPONSE_SENT }, options()),
216
- );
217
- expect(error).toBeInstanceOf(InterpreterError);
218
- });
219
- });
220
-
221
- describe('resume and state version', () => {
222
- const definition = definitionOf([
223
- { id: 'approve', type: StepType.WAIT, durationMs: 500 },
224
- {
225
- id: 'after',
226
- type: StepType.SET,
227
- variable: 'v',
228
- value: { $expression: 'steps.approve.output' },
229
- },
230
- ]);
231
-
232
- it('stamps every new state with the current state version', () => {
233
- expect(CURRENT_STATE_VERSION).toBe(1);
234
- expect(start(definition, null, options()).state.stateVersion).toBe(CURRENT_STATE_VERSION);
235
- });
236
-
237
- it('gives a RESUMED output to the step that suspended', () => {
238
- const first = start(definition, null, options());
239
- const persisted = JSON.parse(JSON.stringify(first.state)) as RunState;
240
- const resumed = step(
241
- definition,
242
- persisted,
243
- { type: RunEventType.RESUMED, output: { approved: true } },
244
- options(),
245
- );
246
- expect(resumed.state.outputs.approve).toEqual({ output: { approved: true } });
247
- expect(resumed.effects).toEqual([
248
- expect.objectContaining({ type: EffectType.EVALUATE, stepId: 'after' }),
249
- ]);
250
- });
251
-
252
- it('refuses RESUMED while an evaluation is pending', () => {
253
- const evaluating = definitionOf([{ id: 'c', type: StepType.CODE, source: 'return 1;' }]);
254
- const first = start(evaluating, null, options());
255
- const error = thrownBy(() =>
256
- step(evaluating, first.state, { type: RunEventType.RESUMED, output: 1 }, options()),
257
- );
258
- expect(error).toBeInstanceOf(InterpreterError);
259
- expect((error as InterpreterError).code).toBe(RuntimeErrorCode.INVALID_REQUEST);
260
- });
261
-
262
- it('refuses a state of an unknown version', () => {
263
- const first = start(definition, null, options());
264
- const future = { ...first.state, stateVersion: CURRENT_STATE_VERSION + 1 };
265
- const error = thrownBy(() =>
266
- step(definition, future, { type: RunEventType.WAIT_ELAPSED }, options()),
267
- );
268
- expect(error).toBeInstanceOf(InterpreterError);
269
- expect((error as InterpreterError).code).toBe(RuntimeErrorCode.STATE_VERSION_UNSUPPORTED);
270
- });
271
- });
1
+ import { CURRENT_FORMAT_VERSION, LogLevel, StepType } from '../../definition/definition.types';
2
+ import type { RuntimeDefinition, Step } from '../../definition/definition.types';
3
+ import { RuntimeErrorCode } from '../../errors/runtime-error-code.enum';
4
+ import { EvaluationKind } from '../../runner-protocol/evaluation.types';
5
+ import { InterpreterError } from '../interpreter-error';
6
+ import { start, step } from '../interpreter';
7
+ import { CURRENT_STATE_VERSION, EffectType, RunEventType, RunStatus } from '../interpreter.types';
8
+ import type { RunOptions, RunState } from '../interpreter.types';
9
+ import { runToCompletion } from '../test-host';
10
+
11
+ const options = (overrides: Partial<RunOptions['budget']> = {}): RunOptions => ({
12
+ catalog: new Map(),
13
+ grants: [],
14
+ budget: { maxSteps: 100, maxEffects: 100, maxWaitMs: 1_000_000, ...overrides },
15
+ });
16
+
17
+ const definitionOf = (steps: Step[]): RuntimeDefinition => ({
18
+ formatVersion: CURRENT_FORMAT_VERSION,
19
+ accesses: [],
20
+ steps,
21
+ });
22
+
23
+ const thrownBy = (fn: () => unknown): unknown => {
24
+ try {
25
+ fn();
26
+ } catch (error) {
27
+ return error;
28
+ }
29
+ throw new Error('expected a throw');
30
+ };
31
+
32
+ describe('LOG step', () => {
33
+ const log: Step = {
34
+ id: 'log',
35
+ type: StepType.LOG,
36
+ level: LogLevel.WARN,
37
+ message: 'order {{ input.id }}',
38
+ data: { total: { $expression: 'input.total' }, fixed: 1 },
39
+ };
40
+
41
+ it('evaluates its message and data, then emits a LOG effect', () => {
42
+ const definition = definitionOf([log]);
43
+ const first = start(definition, { id: 7, total: 12 }, options());
44
+ expect(first.effects).toEqual([
45
+ expect.objectContaining({
46
+ type: EffectType.EVALUATE,
47
+ items: [
48
+ { id: 'message', kind: EvaluationKind.TEMPLATE, source: 'order {{ input.id }}' },
49
+ { id: 'data.total', kind: EvaluationKind.EXPRESSION, source: 'input.total' },
50
+ ],
51
+ }),
52
+ ]);
53
+ const second = step(
54
+ definition,
55
+ first.state,
56
+ {
57
+ type: RunEventType.EVALUATED,
58
+ results: [
59
+ { id: 'message', ok: true, value: 'order 7' },
60
+ { id: 'data.total', ok: true, value: 12 },
61
+ ],
62
+ },
63
+ options(),
64
+ );
65
+ expect(second.effects).toEqual([
66
+ {
67
+ type: EffectType.LOG,
68
+ stepId: 'log',
69
+ level: LogLevel.WARN,
70
+ message: 'order 7',
71
+ data: { total: 12, fixed: 1 },
72
+ },
73
+ ]);
74
+ const done = step(definition, second.state, { type: RunEventType.LOG_WRITTEN }, options());
75
+ expect(done.status).toBe(RunStatus.SUCCEEDED);
76
+ expect(done.state.outputs.log).toEqual({ output: null });
77
+ });
78
+
79
+ it('writes a non-string message as its JSON and omits absent data', () => {
80
+ const definition = definitionOf([
81
+ { id: 'log', type: StepType.LOG, level: LogLevel.INFO, message: { $expression: 'input' } },
82
+ ]);
83
+ const first = start(definition, null, options());
84
+ const second = step(
85
+ definition,
86
+ first.state,
87
+ { type: RunEventType.EVALUATED, results: [{ id: 'message', ok: true, value: { a: 1 } }] },
88
+ options(),
89
+ );
90
+ expect(second.effects).toEqual([
91
+ { type: EffectType.LOG, stepId: 'log', level: LogLevel.INFO, message: '{"a":1}' },
92
+ ]);
93
+ });
94
+
95
+ it('counts against the effect budget', () => {
96
+ const definition = definitionOf([
97
+ { id: 'a', type: StepType.LOG, level: LogLevel.INFO, message: 'a' },
98
+ { id: 'b', type: StepType.LOG, level: LogLevel.INFO, message: 'b' },
99
+ ]);
100
+ const first = start(definition, null, options({ maxEffects: 1 }));
101
+ const second = step(
102
+ definition,
103
+ first.state,
104
+ { type: RunEventType.LOG_WRITTEN },
105
+ options({ maxEffects: 1 }),
106
+ );
107
+ expect(second.status).toBe(RunStatus.FAILED);
108
+ expect(second.state.outcome?.error).toEqual(
109
+ expect.objectContaining({ code: RuntimeErrorCode.BUDGET_EXCEEDED, stepId: 'b' }),
110
+ );
111
+ });
112
+
113
+ const threeLogs = definitionOf(
114
+ ['a', 'b', 'c'].map((id) => ({
115
+ id,
116
+ type: StepType.LOG as const,
117
+ level: LogLevel.INFO,
118
+ message: id,
119
+ })),
120
+ );
121
+
122
+ it('keeps LOG out of maxEffects when maxLogLines is given, and counts them apart', () => {
123
+ const budget = { maxEffects: 0, maxLogLines: 5 };
124
+ const first = start(threeLogs, null, options(budget));
125
+ expect(first.status).toBe(RunStatus.WAITING);
126
+ const second = step(
127
+ threeLogs,
128
+ first.state,
129
+ { type: RunEventType.LOG_WRITTEN },
130
+ options(budget),
131
+ );
132
+ const third = step(
133
+ threeLogs,
134
+ second.state,
135
+ { type: RunEventType.LOG_WRITTEN },
136
+ options(budget),
137
+ );
138
+ const done = step(threeLogs, third.state, { type: RunEventType.LOG_WRITTEN }, options(budget));
139
+ expect(done.status).toBe(RunStatus.SUCCEEDED);
140
+ expect(done.state.counters).toEqual({ steps: 3, effects: 0, logLines: 3 });
141
+ });
142
+
143
+ it('answers a LOG past maxLogLines without emitting it', () => {
144
+ const budget = { maxLogLines: 1 };
145
+ const first = start(threeLogs, null, options(budget));
146
+ expect(first.effects).toEqual([expect.objectContaining({ type: EffectType.LOG, stepId: 'a' })]);
147
+ const second = step(
148
+ threeLogs,
149
+ first.state,
150
+ { type: RunEventType.LOG_WRITTEN },
151
+ options(budget),
152
+ );
153
+ expect(second.status).toBe(RunStatus.SUCCEEDED);
154
+ expect(second.effects).toEqual([]);
155
+ expect(second.state.outputs.b).toEqual({ output: null });
156
+ expect(second.state.outputs.c).toEqual({ output: null });
157
+ expect(second.state.counters.logLines).toBe(1);
158
+ expect(second.state.counters.droppedLogLines).toBe(2);
159
+ });
160
+ });
161
+
162
+ describe('RESPOND step', () => {
163
+ it('emits the response with string headers, and the run goes on after it', async () => {
164
+ const definition = definitionOf([
165
+ {
166
+ id: 'reply',
167
+ type: StepType.RESPOND,
168
+ status: 201,
169
+ headers: { 'X-Count': 3, 'Content-Type': 'application/json' },
170
+ body: { ok: true },
171
+ },
172
+ { id: 'after', type: StepType.SET, variable: 'x', value: 1 },
173
+ ]);
174
+ const responses: unknown[] = [];
175
+ const { state, effects } = await runToCompletion(definition, null, options(), {
176
+ evaluate: () => [],
177
+ respond: (effect) => {
178
+ responses.push(effect.response);
179
+ },
180
+ });
181
+ expect(effects.map((effect) => effect.type)).toEqual([EffectType.RESPOND]);
182
+ expect(responses).toEqual([
183
+ {
184
+ status: 201,
185
+ headers: { 'X-Count': '3', 'Content-Type': 'application/json' },
186
+ body: { ok: true },
187
+ },
188
+ ]);
189
+ expect(state.status).toBe(RunStatus.SUCCEEDED);
190
+ expect(state.outputs.reply).toEqual({ output: null });
191
+ expect(state.vars.x).toBe(1);
192
+ });
193
+
194
+ it('fails the step when the status does not resolve to an HTTP status', () => {
195
+ const definition = definitionOf([
196
+ { id: 'reply', type: StepType.RESPOND, status: { $expression: 'input.status' } },
197
+ ]);
198
+ const first = start(definition, null, options());
199
+ const second = step(
200
+ definition,
201
+ first.state,
202
+ { type: RunEventType.EVALUATED, results: [{ id: 'status', ok: true, value: 700 }] },
203
+ options(),
204
+ );
205
+ expect(second.status).toBe(RunStatus.FAILED);
206
+ expect(second.state.outcome?.error).toEqual(
207
+ expect.objectContaining({ code: RuntimeErrorCode.EXPRESSION_ERROR, stepId: 'reply' }),
208
+ );
209
+ });
210
+
211
+ it('refuses a RESPONSE_SENT event for another pending effect', () => {
212
+ const definition = definitionOf([{ id: 'w', type: StepType.WAIT, durationMs: 10 }]);
213
+ const first = start(definition, null, options());
214
+ const error = thrownBy(() =>
215
+ step(definition, first.state, { type: RunEventType.RESPONSE_SENT }, options()),
216
+ );
217
+ expect(error).toBeInstanceOf(InterpreterError);
218
+ });
219
+ });
220
+
221
+ describe('resume and state version', () => {
222
+ const definition = definitionOf([
223
+ { id: 'approve', type: StepType.WAIT, durationMs: 500 },
224
+ {
225
+ id: 'after',
226
+ type: StepType.SET,
227
+ variable: 'v',
228
+ value: { $expression: 'steps.approve.output' },
229
+ },
230
+ ]);
231
+
232
+ it('stamps every new state with the current state version', () => {
233
+ expect(CURRENT_STATE_VERSION).toBe(1);
234
+ expect(start(definition, null, options()).state.stateVersion).toBe(CURRENT_STATE_VERSION);
235
+ });
236
+
237
+ it('gives a RESUMED output to the step that suspended', () => {
238
+ const first = start(definition, null, options());
239
+ const persisted = JSON.parse(JSON.stringify(first.state)) as RunState;
240
+ const resumed = step(
241
+ definition,
242
+ persisted,
243
+ { type: RunEventType.RESUMED, output: { approved: true } },
244
+ options(),
245
+ );
246
+ expect(resumed.state.outputs.approve).toEqual({ output: { approved: true } });
247
+ expect(resumed.effects).toEqual([
248
+ expect.objectContaining({ type: EffectType.EVALUATE, stepId: 'after' }),
249
+ ]);
250
+ });
251
+
252
+ it('refuses RESUMED while an evaluation is pending', () => {
253
+ const evaluating = definitionOf([{ id: 'c', type: StepType.CODE, source: 'return 1;' }]);
254
+ const first = start(evaluating, null, options());
255
+ const error = thrownBy(() =>
256
+ step(evaluating, first.state, { type: RunEventType.RESUMED, output: 1 }, options()),
257
+ );
258
+ expect(error).toBeInstanceOf(InterpreterError);
259
+ expect((error as InterpreterError).code).toBe(RuntimeErrorCode.INVALID_REQUEST);
260
+ });
261
+
262
+ it('refuses a state of an unknown version', () => {
263
+ const first = start(definition, null, options());
264
+ const future = { ...first.state, stateVersion: CURRENT_STATE_VERSION + 1 };
265
+ const error = thrownBy(() =>
266
+ step(definition, future, { type: RunEventType.WAIT_ELAPSED }, options()),
267
+ );
268
+ expect(error).toBeInstanceOf(InterpreterError);
269
+ expect((error as InterpreterError).code).toBe(RuntimeErrorCode.STATE_VERSION_UNSUPPORTED);
270
+ });
271
+ });
@@ -1,12 +1,12 @@
1
- import { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
2
-
3
- /** Thrown for a misuse of the interpreter (unexpected event, corrupted state), never for a run failure. */
4
- export class InterpreterError extends Error {
5
- readonly code: RuntimeErrorCode;
6
-
7
- constructor(code: RuntimeErrorCode, message: string) {
8
- super(message);
9
- this.name = 'InterpreterError';
10
- this.code = code;
11
- }
12
- }
1
+ import { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
2
+
3
+ /** Thrown for a misuse of the interpreter (unexpected event, corrupted state), never for a run failure. */
4
+ export class InterpreterError extends Error {
5
+ readonly code: RuntimeErrorCode;
6
+
7
+ constructor(code: RuntimeErrorCode, message: string) {
8
+ super(message);
9
+ this.name = 'InterpreterError';
10
+ this.code = code;
11
+ }
12
+ }