@abyss-project/runtime 1.0.0 → 2.0.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.
- package/README.md +6 -3
- package/dist/cost/execution-cost.d.ts +45 -0
- package/dist/cost/execution-cost.js +114 -0
- package/dist/cost/execution-cost.js.map +1 -0
- package/dist/definition/definition.limits.d.ts +4 -0
- package/dist/definition/definition.limits.js +8 -0
- package/dist/definition/definition.limits.js.map +1 -0
- package/dist/definition/definition.schema.d.ts +2 -2
- package/dist/definition/definition.schema.js +20 -4
- package/dist/definition/definition.schema.js.map +1 -1
- package/dist/definition/definition.types.d.ts +20 -1
- package/dist/definition/definition.types.js +9 -1
- package/dist/definition/definition.types.js.map +1 -1
- package/dist/engine/engine-entry.d.ts +1 -0
- package/dist/engine/engine-entry.js +6 -0
- package/dist/engine/engine-entry.js.map +1 -0
- package/dist/engine/engine-global.d.ts +1 -0
- package/dist/engine/engine-global.js +5 -0
- package/dist/engine/engine-global.js.map +1 -0
- package/dist/engine/engine-source.d.ts +2 -0
- package/dist/engine/engine-source.js +3 -0
- package/dist/engine/run-in-isolate.d.ts +34 -0
- package/dist/engine/run-in-isolate.js +267 -0
- package/dist/engine/run-in-isolate.js.map +1 -0
- package/dist/errors/runtime-error-code.enum.d.ts +2 -2
- package/dist/errors/runtime-error-code.enum.js +1 -1
- package/dist/errors/runtime-error-code.enum.js.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.js +13 -4
- package/dist/index.js.map +1 -1
- package/dist/interpreter/interpreter.d.ts +27 -1
- package/dist/interpreter/interpreter.js +114 -42
- package/dist/interpreter/interpreter.js.map +1 -1
- package/dist/interpreter/interpreter.types.d.ts +35 -4
- package/dist/interpreter/interpreter.types.js +7 -1
- package/dist/interpreter/interpreter.types.js.map +1 -1
- package/dist/interpreter/test-host.d.ts +4 -2
- package/dist/interpreter/test-host.js +6 -0
- package/dist/interpreter/test-host.js.map +1 -1
- package/dist/runner-protocol/evaluation.types.d.ts +20 -0
- package/dist/runner-protocol/evaluation.types.js +10 -0
- package/dist/runner-protocol/evaluation.types.js.map +1 -0
- package/dist/runner-protocol/execution-protocol.types.d.ts +132 -0
- package/dist/runner-protocol/execution-protocol.types.js +48 -0
- package/dist/runner-protocol/execution-protocol.types.js.map +1 -0
- package/dist/server/code-runner-session.d.ts +28 -0
- package/dist/server/code-runner-session.js +208 -0
- package/dist/server/code-runner-session.js.map +1 -0
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.js +5 -3
- package/dist/server/index.js.map +1 -1
- package/dist/steps/execute-step.d.ts +12 -2
- package/dist/steps/execute-step.js +37 -0
- package/dist/steps/execute-step.js.map +1 -1
- package/dist/steps/global-references.d.ts +2 -0
- package/dist/steps/global-references.js +28 -0
- package/dist/steps/global-references.js.map +1 -0
- package/dist/steps/step-fields.d.ts +1 -1
- package/dist/steps/step-fields.js +10 -2
- package/dist/steps/step-fields.js.map +1 -1
- package/dist/steps/value-source.d.ts +1 -1
- package/dist/steps/value-source.js +3 -3
- package/dist/steps/value-source.js.map +1 -1
- package/package.json +10 -2
- package/scripts/build-engine.cjs +70 -0
- package/src/__tests__/engine-bundle.test.ts +121 -0
- package/src/cost/__tests__/execution-cost.test.ts +200 -0
- package/src/cost/execution-cost.ts +192 -0
- package/src/definition/__tests__/validate-definition.test.ts +354 -303
- package/src/definition/definition.limits.ts +9 -0
- package/src/definition/definition.schema.ts +172 -139
- package/src/definition/definition.types.ts +171 -143
- package/src/engine/__tests__/run-in-isolate.test.ts +468 -0
- package/src/engine/engine-entry.ts +3 -0
- package/src/engine/engine-global.ts +2 -0
- package/src/engine/run-in-isolate.ts +404 -0
- package/src/errors/runtime-error-code.enum.ts +37 -37
- package/src/index.ts +30 -18
- package/src/interpreter/__tests__/interpreter.test.ts +854 -854
- package/src/interpreter/__tests__/log-respond-resume.test.ts +223 -0
- package/src/interpreter/interpreter.ts +461 -342
- package/src/interpreter/interpreter.types.ts +192 -140
- package/src/interpreter/test-host.ts +85 -74
- package/src/runner-protocol/evaluation.types.ts +21 -0
- package/src/runner-protocol/execution-protocol.types.ts +221 -0
- package/src/server/__tests__/code-runner-session.test.ts +311 -0
- package/src/server/code-runner-error.ts +12 -12
- package/src/server/code-runner-session.ts +310 -0
- package/src/server/index.ts +11 -4
- package/src/steps/__tests__/global-references.test.ts +113 -0
- package/src/steps/execute-step.ts +186 -132
- package/src/steps/global-references.ts +48 -0
- package/src/steps/step-access.ts +56 -56
- package/src/steps/step-fields.ts +87 -79
- package/src/steps/value-source.ts +69 -69
- package/dist/runner-protocol/runner-protocol.types.d.ts +0 -92
- package/dist/runner-protocol/runner-protocol.types.js +0 -26
- package/dist/runner-protocol/runner-protocol.types.js.map +0 -1
- package/dist/server/code-runner-client.d.ts +0 -13
- package/dist/server/code-runner-client.js +0 -155
- package/dist/server/code-runner-client.js.map +0 -1
- package/src/runner-protocol/runner-protocol.types.ts +0 -117
- package/src/server/__tests__/code-runner-client.test.ts +0 -262
- package/src/server/code-runner-client.ts +0 -201
|
@@ -1,342 +1,461 @@
|
|
|
1
|
-
import { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
|
|
2
|
-
import type { RuntimeErrorDetail } from '../errors/runtime-error-code.enum';
|
|
3
|
-
import { MAX_WAIT_DURATION_MS } from '../definition/definition.
|
|
4
|
-
import { CURRENT_FORMAT_VERSION, StopStatus } from '../definition/definition.types';
|
|
5
|
-
import type { RuntimeDefinition, Step } from '../definition/definition.types';
|
|
6
|
-
import { StepActionKind, executeStep } from '../steps/execute-step';
|
|
7
|
-
import type { StepAction } from '../steps/execute-step';
|
|
8
|
-
import { collectStepItems, resolveStepFields } from '../steps/step-fields';
|
|
9
|
-
import { AccessVerdict, checkHostActionAccess, checkHttpAccess } from '../steps/step-access';
|
|
10
|
-
import { InterpreterError } from './interpreter-error';
|
|
11
|
-
import { EffectType, RunEventType, RunStatus } from './interpreter.types';
|
|
12
|
-
import type {
|
|
13
|
-
Effect,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
1
|
+
import { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
|
|
2
|
+
import type { RuntimeErrorDetail } from '../errors/runtime-error-code.enum';
|
|
3
|
+
import { MAX_WAIT_DURATION_MS } from '../definition/definition.limits';
|
|
4
|
+
import { CURRENT_FORMAT_VERSION, StopStatus } from '../definition/definition.types';
|
|
5
|
+
import type { RuntimeDefinition, Step } from '../definition/definition.types';
|
|
6
|
+
import { StepActionKind, executeStep } from '../steps/execute-step';
|
|
7
|
+
import type { StepAction } from '../steps/execute-step';
|
|
8
|
+
import { collectStepItems, resolveStepFields } from '../steps/step-fields';
|
|
9
|
+
import { AccessVerdict, checkHostActionAccess, checkHttpAccess } from '../steps/step-access';
|
|
10
|
+
import { InterpreterError } from './interpreter-error';
|
|
11
|
+
import { CURRENT_STATE_VERSION, EffectType, RunEventType, RunStatus } from './interpreter.types';
|
|
12
|
+
import type {
|
|
13
|
+
Effect,
|
|
14
|
+
LogEffect,
|
|
15
|
+
RunBudget,
|
|
16
|
+
RunEvent,
|
|
17
|
+
RunOptions,
|
|
18
|
+
RunState,
|
|
19
|
+
StepResult,
|
|
20
|
+
WaitEffect,
|
|
21
|
+
} from './interpreter.types';
|
|
22
|
+
import { ROOT_SEQUENCE_PATH, branchPath, resolveSequence, resolveStep } from './sequence-path';
|
|
23
|
+
|
|
24
|
+
const NO_RESULTS: ReadonlyMap<string, unknown> = new Map();
|
|
25
|
+
|
|
26
|
+
export type HostInputCheck = { input: Record<string, unknown> } | { error: RuntimeErrorDetail };
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* What the interpreter asks about a run. `start`/`step` build it from `RunOptions` (declarations,
|
|
30
|
+
* grants and catalog, on the host); the isolate engine builds it from the capabilities the host
|
|
31
|
+
* exposed (`engine/run-in-isolate.ts`).
|
|
32
|
+
*/
|
|
33
|
+
export interface RunPolicy {
|
|
34
|
+
budget: RunBudget;
|
|
35
|
+
httpAccess: (url: string) => AccessVerdict;
|
|
36
|
+
hostActionAccess: (action: string) => AccessVerdict;
|
|
37
|
+
/** Validates the resolved input of a host action; may normalize it. */
|
|
38
|
+
hostInput: (action: string, input: Record<string, unknown>) => HostInputCheck;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type StepFinish = { ok: true; output: unknown } | { ok: false; error: RuntimeErrorDetail };
|
|
42
|
+
|
|
43
|
+
/** Told when a step starts and when it ends; a step that emitted an effect ends with its answer. */
|
|
44
|
+
export interface StepObserver {
|
|
45
|
+
stepStarted: (stepId: string) => void;
|
|
46
|
+
stepFinished: (stepId: string, finish: StepFinish) => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const SILENT_OBSERVER: StepObserver = {
|
|
50
|
+
stepStarted: () => undefined,
|
|
51
|
+
stepFinished: () => undefined,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
interface RunContext {
|
|
55
|
+
definition: RuntimeDefinition;
|
|
56
|
+
policy: RunPolicy;
|
|
57
|
+
observer: StepObserver;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const invalidRequest = (message: string): InterpreterError =>
|
|
61
|
+
new InterpreterError(RuntimeErrorCode.INVALID_REQUEST, message);
|
|
62
|
+
|
|
63
|
+
/** A copy whose containers can be changed freely; the caller's state is never touched. */
|
|
64
|
+
const draftOf = (state: RunState): RunState => ({
|
|
65
|
+
...state,
|
|
66
|
+
stack: state.stack.map((frame) => ({ ...frame })),
|
|
67
|
+
outputs: { ...state.outputs },
|
|
68
|
+
vars: { ...state.vars },
|
|
69
|
+
counters: { ...state.counters },
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/** Own, enumerable data property: unlike `target[key] = value`, a key such as `__proto__` stays a key. */
|
|
73
|
+
const setOwn = <T>(target: Record<string, T>, key: string, value: T): void => {
|
|
74
|
+
Object.defineProperty(target, key, {
|
|
75
|
+
value,
|
|
76
|
+
writable: true,
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: true,
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const topFrame = (draft: RunState) => draft.stack[draft.stack.length - 1];
|
|
83
|
+
|
|
84
|
+
const advance = (draft: RunState): void => {
|
|
85
|
+
topFrame(draft).index += 1;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const settle = (draft: RunState, status: RunStatus): StepResult => {
|
|
89
|
+
draft.status = status;
|
|
90
|
+
draft.pending = null;
|
|
91
|
+
return { state: draft, effects: [], status };
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const failRun = (
|
|
95
|
+
context: RunContext,
|
|
96
|
+
draft: RunState,
|
|
97
|
+
error: RuntimeErrorDetail,
|
|
98
|
+
stepId: string,
|
|
99
|
+
): StepResult => {
|
|
100
|
+
draft.outcome = { status: StopStatus.FAILED, error: { ...error, stepId } };
|
|
101
|
+
context.observer.stepFinished(stepId, { ok: false, error });
|
|
102
|
+
return settle(draft, RunStatus.FAILED);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const emit = (context: RunContext, draft: RunState, effect: Effect): StepResult => {
|
|
106
|
+
if (draft.counters.effects + 1 > context.policy.budget.maxEffects) {
|
|
107
|
+
return failRun(
|
|
108
|
+
context,
|
|
109
|
+
draft,
|
|
110
|
+
{ code: RuntimeErrorCode.BUDGET_EXCEEDED, message: 'Effect budget exceeded' },
|
|
111
|
+
effect.stepId,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
draft.counters.effects += 1;
|
|
115
|
+
draft.pending = effect;
|
|
116
|
+
draft.status = RunStatus.WAITING;
|
|
117
|
+
return { state: draft, effects: [effect], status: RunStatus.WAITING };
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const accessDenied = (verdict: AccessVerdict, subject: string): RuntimeErrorDetail => ({
|
|
121
|
+
code: RuntimeErrorCode.ACCESS_DENIED,
|
|
122
|
+
message:
|
|
123
|
+
verdict === AccessVerdict.NOT_DECLARED
|
|
124
|
+
? `${subject} is not declared by the definition`
|
|
125
|
+
: `${subject} is not granted to the run`,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const validateHostInput = (
|
|
129
|
+
options: RunOptions,
|
|
130
|
+
action: string,
|
|
131
|
+
input: Record<string, unknown>,
|
|
132
|
+
): HostInputCheck => {
|
|
133
|
+
const definition = options.catalog.get(action);
|
|
134
|
+
if (!definition) {
|
|
135
|
+
return {
|
|
136
|
+
error: { code: RuntimeErrorCode.HOST_ERROR, message: 'Host action is not in the catalog' },
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const { error, value } = definition.inputSchema.validate(input, { abortEarly: false });
|
|
140
|
+
if (error) {
|
|
141
|
+
const reasons = error.details.map((detail) => detail.message).join('; ');
|
|
142
|
+
return {
|
|
143
|
+
error: {
|
|
144
|
+
code: RuntimeErrorCode.HOST_ERROR,
|
|
145
|
+
message: `Invalid host action input: ${reasons}`,
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return { input: value as Record<string, unknown> };
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/** Host-side policy: the definition's declarations AND the run's grants, the product's catalog. */
|
|
153
|
+
const hostPolicy = (definition: RuntimeDefinition, options: RunOptions): RunPolicy => ({
|
|
154
|
+
budget: options.budget,
|
|
155
|
+
httpAccess: (url) => checkHttpAccess(url, definition.accesses, options.grants),
|
|
156
|
+
hostActionAccess: (action) => checkHostActionAccess(action, definition.accesses, options.grants),
|
|
157
|
+
hostInput: (action, input) => validateHostInput(options, action, input),
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const applyAction = (
|
|
161
|
+
context: RunContext,
|
|
162
|
+
draft: RunState,
|
|
163
|
+
step: Step,
|
|
164
|
+
action: StepAction,
|
|
165
|
+
): StepResult | null => {
|
|
166
|
+
const { policy, observer } = context;
|
|
167
|
+
switch (action.kind) {
|
|
168
|
+
case StepActionKind.FAIL:
|
|
169
|
+
return failRun(context, draft, action.error, step.id);
|
|
170
|
+
case StepActionKind.STOP:
|
|
171
|
+
draft.outcome = { status: action.status, output: action.output };
|
|
172
|
+
observer.stepFinished(step.id, { ok: true, output: action.output });
|
|
173
|
+
return settle(
|
|
174
|
+
draft,
|
|
175
|
+
action.status === StopStatus.SUCCEEDED ? RunStatus.SUCCEEDED : RunStatus.FAILED,
|
|
176
|
+
);
|
|
177
|
+
case StepActionKind.DONE: {
|
|
178
|
+
setOwn(draft.outputs, step.id, { output: action.output });
|
|
179
|
+
observer.stepFinished(step.id, { ok: true, output: action.output });
|
|
180
|
+
if (action.variable) {
|
|
181
|
+
setOwn(draft.vars, action.variable.name, action.variable.value);
|
|
182
|
+
}
|
|
183
|
+
if (action.branch === undefined) {
|
|
184
|
+
advance(draft);
|
|
185
|
+
} else {
|
|
186
|
+
draft.stack.push({ sequencePath: branchPath(topFrame(draft), action.branch), index: 0 });
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
case StepActionKind.WAIT: {
|
|
191
|
+
// A host budget never loosens the bound the validation applies to a literal duration.
|
|
192
|
+
const maxWaitMs = Math.min(policy.budget.maxWaitMs, MAX_WAIT_DURATION_MS);
|
|
193
|
+
if (action.durationMs !== undefined && action.durationMs > maxWaitMs) {
|
|
194
|
+
return failRun(
|
|
195
|
+
context,
|
|
196
|
+
draft,
|
|
197
|
+
{
|
|
198
|
+
code: RuntimeErrorCode.BUDGET_EXCEEDED,
|
|
199
|
+
message: `Wait duration exceeds the budget of ${maxWaitMs} ms`,
|
|
200
|
+
},
|
|
201
|
+
step.id,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
const effect: WaitEffect = { type: EffectType.WAIT, stepId: step.id };
|
|
205
|
+
if (action.durationMs !== undefined) {
|
|
206
|
+
effect.durationMs = action.durationMs;
|
|
207
|
+
}
|
|
208
|
+
if (action.until !== undefined) {
|
|
209
|
+
effect.until = action.until;
|
|
210
|
+
}
|
|
211
|
+
return emit(context, draft, effect);
|
|
212
|
+
}
|
|
213
|
+
case StepActionKind.HTTP: {
|
|
214
|
+
const verdict = policy.httpAccess(action.request.url);
|
|
215
|
+
if (verdict !== AccessVerdict.ALLOWED) {
|
|
216
|
+
return failRun(context, draft, accessDenied(verdict, 'HTTP request host'), step.id);
|
|
217
|
+
}
|
|
218
|
+
return emit(context, draft, {
|
|
219
|
+
type: EffectType.HTTP,
|
|
220
|
+
stepId: step.id,
|
|
221
|
+
request: action.request,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
case StepActionKind.HOST: {
|
|
225
|
+
const verdict = policy.hostActionAccess(action.action);
|
|
226
|
+
if (verdict !== AccessVerdict.ALLOWED) {
|
|
227
|
+
return failRun(context, draft, accessDenied(verdict, 'Host action'), step.id);
|
|
228
|
+
}
|
|
229
|
+
const validated = policy.hostInput(action.action, action.input);
|
|
230
|
+
if ('error' in validated) {
|
|
231
|
+
return failRun(context, draft, validated.error, step.id);
|
|
232
|
+
}
|
|
233
|
+
return emit(context, draft, {
|
|
234
|
+
type: EffectType.HOST,
|
|
235
|
+
stepId: step.id,
|
|
236
|
+
action: action.action,
|
|
237
|
+
input: validated.input,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
// Logging and responding are granted to every run; the host bounds them (one response).
|
|
241
|
+
case StepActionKind.LOG: {
|
|
242
|
+
const effect: LogEffect = {
|
|
243
|
+
type: EffectType.LOG,
|
|
244
|
+
stepId: step.id,
|
|
245
|
+
level: action.level,
|
|
246
|
+
message: action.message,
|
|
247
|
+
};
|
|
248
|
+
if ('data' in action) {
|
|
249
|
+
effect.data = action.data;
|
|
250
|
+
}
|
|
251
|
+
return emit(context, draft, effect);
|
|
252
|
+
}
|
|
253
|
+
case StepActionKind.RESPOND:
|
|
254
|
+
return emit(context, draft, {
|
|
255
|
+
type: EffectType.RESPOND,
|
|
256
|
+
stepId: step.id,
|
|
257
|
+
response: action.response,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Runs the current step: evaluates it first when it has non-literal values (`results` undefined),
|
|
264
|
+
* then applies it. Returns null when the step finished and the run goes on.
|
|
265
|
+
*/
|
|
266
|
+
const runStep = (
|
|
267
|
+
context: RunContext,
|
|
268
|
+
draft: RunState,
|
|
269
|
+
step: Step,
|
|
270
|
+
results: ReadonlyMap<string, unknown> | undefined,
|
|
271
|
+
): StepResult | null => {
|
|
272
|
+
if (results === undefined) {
|
|
273
|
+
const items = collectStepItems(step);
|
|
274
|
+
if (items.length > 0) {
|
|
275
|
+
return emit(context, draft, {
|
|
276
|
+
type: EffectType.EVALUATE,
|
|
277
|
+
stepId: step.id,
|
|
278
|
+
items,
|
|
279
|
+
context: { input: draft.input, steps: draft.outputs, vars: draft.vars },
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
const fields = resolveStepFields(step, results ?? NO_RESULTS);
|
|
284
|
+
return applyAction(context, draft, step, executeStep(step, fields));
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
/** Enters steps one after the other until an effect is emitted or the run ends. */
|
|
288
|
+
const drive = (context: RunContext, draft: RunState): StepResult => {
|
|
289
|
+
for (;;) {
|
|
290
|
+
const frame = topFrame(draft);
|
|
291
|
+
if (!frame) {
|
|
292
|
+
draft.outcome = { status: StopStatus.SUCCEEDED };
|
|
293
|
+
return settle(draft, RunStatus.SUCCEEDED);
|
|
294
|
+
}
|
|
295
|
+
const sequence = resolveSequence(context.definition, frame.sequencePath);
|
|
296
|
+
if (frame.index >= sequence.length) {
|
|
297
|
+
draft.stack.pop();
|
|
298
|
+
if (draft.stack.length > 0) {
|
|
299
|
+
advance(draft);
|
|
300
|
+
}
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
const step = sequence[frame.index];
|
|
304
|
+
context.observer.stepStarted(step.id);
|
|
305
|
+
if (draft.counters.steps + 1 > context.policy.budget.maxSteps) {
|
|
306
|
+
return failRun(
|
|
307
|
+
context,
|
|
308
|
+
draft,
|
|
309
|
+
{ code: RuntimeErrorCode.BUDGET_EXCEEDED, message: 'Step budget exceeded' },
|
|
310
|
+
step.id,
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
draft.counters.steps += 1;
|
|
314
|
+
const result = runStep(context, draft, step, undefined);
|
|
315
|
+
if (result) {
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
/** `start` with an explicit policy and observer: the entry of the isolate engine. */
|
|
322
|
+
export const startRun = (
|
|
323
|
+
definition: RuntimeDefinition,
|
|
324
|
+
input: unknown,
|
|
325
|
+
policy: RunPolicy,
|
|
326
|
+
observer: StepObserver = SILENT_OBSERVER,
|
|
327
|
+
): StepResult =>
|
|
328
|
+
drive(
|
|
329
|
+
{ definition, policy, observer },
|
|
330
|
+
{
|
|
331
|
+
stateVersion: CURRENT_STATE_VERSION,
|
|
332
|
+
formatVersion: CURRENT_FORMAT_VERSION,
|
|
333
|
+
status: RunStatus.WAITING,
|
|
334
|
+
input,
|
|
335
|
+
stack: [{ sequencePath: ROOT_SEQUENCE_PATH, index: 0 }],
|
|
336
|
+
outputs: {},
|
|
337
|
+
vars: {},
|
|
338
|
+
pending: null,
|
|
339
|
+
counters: { steps: 0, effects: 0 },
|
|
340
|
+
},
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
export const start = (
|
|
344
|
+
definition: RuntimeDefinition,
|
|
345
|
+
input: unknown,
|
|
346
|
+
options: RunOptions,
|
|
347
|
+
): StepResult => startRun(definition, input, hostPolicy(definition, options));
|
|
348
|
+
|
|
349
|
+
const unexpected = (event: RunEvent, pending: Effect): InterpreterError =>
|
|
350
|
+
invalidRequest(`Event ${event.type} does not answer the pending ${pending.type} effect`);
|
|
351
|
+
|
|
352
|
+
/** Maps each item id to its value, or returns the first failing item's error. */
|
|
353
|
+
const readEvaluation = (
|
|
354
|
+
pending: Effect,
|
|
355
|
+
results: Extract<RunEvent, { type: RunEventType.EVALUATED }>['results'],
|
|
356
|
+
): ReadonlyMap<string, unknown> | RuntimeErrorDetail => {
|
|
357
|
+
if (pending.type !== EffectType.EVALUATE) {
|
|
358
|
+
throw invalidRequest(
|
|
359
|
+
`Event ${RunEventType.EVALUATED} does not answer the pending ${pending.type} effect`,
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
const values = new Map<string, unknown>();
|
|
363
|
+
for (const item of pending.items) {
|
|
364
|
+
const result = results.find((candidate) => candidate.id === item.id);
|
|
365
|
+
if (!result) {
|
|
366
|
+
throw invalidRequest('Evaluation results do not cover every item of the lot');
|
|
367
|
+
}
|
|
368
|
+
if (!result.ok) {
|
|
369
|
+
return result.error;
|
|
370
|
+
}
|
|
371
|
+
values.set(item.id, result.value);
|
|
372
|
+
}
|
|
373
|
+
return values;
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const complete = (
|
|
377
|
+
context: RunContext,
|
|
378
|
+
draft: RunState,
|
|
379
|
+
step: Step,
|
|
380
|
+
output: unknown,
|
|
381
|
+
): StepResult => {
|
|
382
|
+
setOwn(draft.outputs, step.id, { output });
|
|
383
|
+
context.observer.stepFinished(step.id, { ok: true, output });
|
|
384
|
+
advance(draft);
|
|
385
|
+
return drive(context, draft);
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
/** `step` with an explicit policy and observer: the entry of the isolate engine. */
|
|
389
|
+
export const stepRun = (
|
|
390
|
+
definition: RuntimeDefinition,
|
|
391
|
+
state: RunState,
|
|
392
|
+
event: RunEvent,
|
|
393
|
+
policy: RunPolicy,
|
|
394
|
+
observer: StepObserver = SILENT_OBSERVER,
|
|
395
|
+
): StepResult => {
|
|
396
|
+
if (state.stateVersion !== CURRENT_STATE_VERSION) {
|
|
397
|
+
throw new InterpreterError(
|
|
398
|
+
RuntimeErrorCode.STATE_VERSION_UNSUPPORTED,
|
|
399
|
+
`Run state version ${String(state.stateVersion)} is not supported ` +
|
|
400
|
+
`(this core reads version ${CURRENT_STATE_VERSION})`,
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
const pending = state.pending;
|
|
404
|
+
if (state.status !== RunStatus.WAITING || !pending) {
|
|
405
|
+
throw invalidRequest(`Event ${event.type} received by a run that waits for nothing`);
|
|
406
|
+
}
|
|
407
|
+
const context: RunContext = { definition, policy, observer };
|
|
408
|
+
const draft = draftOf(state);
|
|
409
|
+
draft.pending = null;
|
|
410
|
+
const current = resolveStep(definition, topFrame(draft));
|
|
411
|
+
|
|
412
|
+
switch (event.type) {
|
|
413
|
+
case RunEventType.EVALUATED: {
|
|
414
|
+
const values = readEvaluation(pending, event.results);
|
|
415
|
+
if (!(values instanceof Map)) {
|
|
416
|
+
return failRun(context, draft, values as RuntimeErrorDetail, current.id);
|
|
417
|
+
}
|
|
418
|
+
return runStep(context, draft, current, values) ?? drive(context, draft);
|
|
419
|
+
}
|
|
420
|
+
case RunEventType.HTTP_COMPLETED:
|
|
421
|
+
if (pending.type !== EffectType.HTTP) {
|
|
422
|
+
throw unexpected(event, pending);
|
|
423
|
+
}
|
|
424
|
+
return complete(context, draft, current, event.response);
|
|
425
|
+
case RunEventType.HOST_COMPLETED:
|
|
426
|
+
if (pending.type !== EffectType.HOST) {
|
|
427
|
+
throw unexpected(event, pending);
|
|
428
|
+
}
|
|
429
|
+
return complete(context, draft, current, event.output);
|
|
430
|
+
case RunEventType.WAIT_ELAPSED:
|
|
431
|
+
if (pending.type !== EffectType.WAIT) {
|
|
432
|
+
throw unexpected(event, pending);
|
|
433
|
+
}
|
|
434
|
+
return complete(context, draft, current, null);
|
|
435
|
+
case RunEventType.LOG_WRITTEN:
|
|
436
|
+
if (pending.type !== EffectType.LOG) {
|
|
437
|
+
throw unexpected(event, pending);
|
|
438
|
+
}
|
|
439
|
+
return complete(context, draft, current, null);
|
|
440
|
+
case RunEventType.RESPONSE_SENT:
|
|
441
|
+
if (pending.type !== EffectType.RESPOND) {
|
|
442
|
+
throw unexpected(event, pending);
|
|
443
|
+
}
|
|
444
|
+
return complete(context, draft, current, null);
|
|
445
|
+
case RunEventType.RESUMED:
|
|
446
|
+
// A run suspends between steps, never while an evaluation is in flight.
|
|
447
|
+
if (pending.type === EffectType.EVALUATE) {
|
|
448
|
+
throw unexpected(event, pending);
|
|
449
|
+
}
|
|
450
|
+
return complete(context, draft, current, event.output);
|
|
451
|
+
case RunEventType.EFFECT_FAILED:
|
|
452
|
+
return failRun(context, draft, event.error, current.id);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
export const step = (
|
|
457
|
+
definition: RuntimeDefinition,
|
|
458
|
+
state: RunState,
|
|
459
|
+
event: RunEvent,
|
|
460
|
+
options: RunOptions,
|
|
461
|
+
): StepResult => stepRun(definition, state, event, hostPolicy(definition, options));
|