@abyss-project/runtime 1.0.0 → 2.0.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/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
|
@@ -0,0 +1,223 @@
|
|
|
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
|
+
|
|
114
|
+
describe('RESPOND step', () => {
|
|
115
|
+
it('emits the response with string headers, and the run goes on after it', async () => {
|
|
116
|
+
const definition = definitionOf([
|
|
117
|
+
{
|
|
118
|
+
id: 'reply',
|
|
119
|
+
type: StepType.RESPOND,
|
|
120
|
+
status: 201,
|
|
121
|
+
headers: { 'X-Count': 3, 'Content-Type': 'application/json' },
|
|
122
|
+
body: { ok: true },
|
|
123
|
+
},
|
|
124
|
+
{ id: 'after', type: StepType.SET, variable: 'x', value: 1 },
|
|
125
|
+
]);
|
|
126
|
+
const responses: unknown[] = [];
|
|
127
|
+
const { state, effects } = await runToCompletion(definition, null, options(), {
|
|
128
|
+
evaluate: () => [],
|
|
129
|
+
respond: (effect) => {
|
|
130
|
+
responses.push(effect.response);
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
expect(effects.map((effect) => effect.type)).toEqual([EffectType.RESPOND]);
|
|
134
|
+
expect(responses).toEqual([
|
|
135
|
+
{
|
|
136
|
+
status: 201,
|
|
137
|
+
headers: { 'X-Count': '3', 'Content-Type': 'application/json' },
|
|
138
|
+
body: { ok: true },
|
|
139
|
+
},
|
|
140
|
+
]);
|
|
141
|
+
expect(state.status).toBe(RunStatus.SUCCEEDED);
|
|
142
|
+
expect(state.outputs.reply).toEqual({ output: null });
|
|
143
|
+
expect(state.vars.x).toBe(1);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('fails the step when the status does not resolve to an HTTP status', () => {
|
|
147
|
+
const definition = definitionOf([
|
|
148
|
+
{ id: 'reply', type: StepType.RESPOND, status: { $expression: 'input.status' } },
|
|
149
|
+
]);
|
|
150
|
+
const first = start(definition, null, options());
|
|
151
|
+
const second = step(
|
|
152
|
+
definition,
|
|
153
|
+
first.state,
|
|
154
|
+
{ type: RunEventType.EVALUATED, results: [{ id: 'status', ok: true, value: 700 }] },
|
|
155
|
+
options(),
|
|
156
|
+
);
|
|
157
|
+
expect(second.status).toBe(RunStatus.FAILED);
|
|
158
|
+
expect(second.state.outcome?.error).toEqual(
|
|
159
|
+
expect.objectContaining({ code: RuntimeErrorCode.EXPRESSION_ERROR, stepId: 'reply' }),
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('refuses a RESPONSE_SENT event for another pending effect', () => {
|
|
164
|
+
const definition = definitionOf([{ id: 'w', type: StepType.WAIT, durationMs: 10 }]);
|
|
165
|
+
const first = start(definition, null, options());
|
|
166
|
+
const error = thrownBy(() =>
|
|
167
|
+
step(definition, first.state, { type: RunEventType.RESPONSE_SENT }, options()),
|
|
168
|
+
);
|
|
169
|
+
expect(error).toBeInstanceOf(InterpreterError);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe('resume and state version', () => {
|
|
174
|
+
const definition = definitionOf([
|
|
175
|
+
{ id: 'approve', type: StepType.WAIT, durationMs: 500 },
|
|
176
|
+
{
|
|
177
|
+
id: 'after',
|
|
178
|
+
type: StepType.SET,
|
|
179
|
+
variable: 'v',
|
|
180
|
+
value: { $expression: 'steps.approve.output' },
|
|
181
|
+
},
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
it('stamps every new state with the current state version', () => {
|
|
185
|
+
expect(CURRENT_STATE_VERSION).toBe(1);
|
|
186
|
+
expect(start(definition, null, options()).state.stateVersion).toBe(CURRENT_STATE_VERSION);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('gives a RESUMED output to the step that suspended', () => {
|
|
190
|
+
const first = start(definition, null, options());
|
|
191
|
+
const persisted = JSON.parse(JSON.stringify(first.state)) as RunState;
|
|
192
|
+
const resumed = step(
|
|
193
|
+
definition,
|
|
194
|
+
persisted,
|
|
195
|
+
{ type: RunEventType.RESUMED, output: { approved: true } },
|
|
196
|
+
options(),
|
|
197
|
+
);
|
|
198
|
+
expect(resumed.state.outputs.approve).toEqual({ output: { approved: true } });
|
|
199
|
+
expect(resumed.effects).toEqual([
|
|
200
|
+
expect.objectContaining({ type: EffectType.EVALUATE, stepId: 'after' }),
|
|
201
|
+
]);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('refuses RESUMED while an evaluation is pending', () => {
|
|
205
|
+
const evaluating = definitionOf([{ id: 'c', type: StepType.CODE, source: 'return 1;' }]);
|
|
206
|
+
const first = start(evaluating, null, options());
|
|
207
|
+
const error = thrownBy(() =>
|
|
208
|
+
step(evaluating, first.state, { type: RunEventType.RESUMED, output: 1 }, options()),
|
|
209
|
+
);
|
|
210
|
+
expect(error).toBeInstanceOf(InterpreterError);
|
|
211
|
+
expect((error as InterpreterError).code).toBe(RuntimeErrorCode.INVALID_REQUEST);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('refuses a state of an unknown version', () => {
|
|
215
|
+
const first = start(definition, null, options());
|
|
216
|
+
const future = { ...first.state, stateVersion: CURRENT_STATE_VERSION + 1 };
|
|
217
|
+
const error = thrownBy(() =>
|
|
218
|
+
step(definition, future, { type: RunEventType.WAIT_ELAPSED }, options()),
|
|
219
|
+
);
|
|
220
|
+
expect(error).toBeInstanceOf(InterpreterError);
|
|
221
|
+
expect((error as InterpreterError).code).toBe(RuntimeErrorCode.STATE_VERSION_UNSUPPORTED);
|
|
222
|
+
});
|
|
223
|
+
});
|