@abyss-project/runtime 0.0.0-stage → 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/.eslintrc.js +32 -0
- package/.prettierrc +5 -0
- package/README.md +28 -3
- package/dist/access/access.types.d.ts +14 -0
- package/dist/access/access.types.js +9 -0
- package/dist/access/access.types.js.map +1 -0
- package/dist/access/host-match.d.ts +1 -0
- package/dist/access/host-match.js +14 -0
- package/dist/access/host-match.js.map +1 -0
- 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 +8 -0
- package/dist/definition/definition.schema.js +123 -0
- package/dist/definition/definition.schema.js.map +1 -0
- package/dist/definition/definition.types.d.ts +117 -0
- package/dist/definition/definition.types.js +37 -0
- package/dist/definition/definition.types.js.map +1 -0
- package/dist/definition/validate-definition.d.ts +9 -0
- package/dist/definition/validate-definition.js +163 -0
- package/dist/definition/validate-definition.js.map +1 -0
- package/dist/definition/walk-steps.d.ts +5 -0
- package/dist/definition/walk-steps.js +31 -0
- package/dist/definition/walk-steps.js.map +1 -0
- 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 +18 -0
- package/dist/errors/runtime-error-code.enum.js +19 -0
- package/dist/errors/runtime-error-code.enum.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/dist/interpreter/interpreter-error.d.ts +5 -0
- package/dist/interpreter/interpreter-error.js +12 -0
- package/dist/interpreter/interpreter-error.js.map +1 -0
- package/dist/interpreter/interpreter.d.ts +30 -0
- package/dist/interpreter/interpreter.js +313 -0
- package/dist/interpreter/interpreter.js.map +1 -0
- package/dist/interpreter/interpreter.types.d.ts +152 -0
- package/dist/interpreter/interpreter.types.js +31 -0
- package/dist/interpreter/interpreter.types.js.map +1 -0
- package/dist/interpreter/sequence-path.d.ts +6 -0
- package/dist/interpreter/sequence-path.js +53 -0
- package/dist/interpreter/sequence-path.js.map +1 -0
- package/dist/interpreter/test-host.d.ts +18 -0
- package/dist/interpreter/test-host.js +43 -0
- package/dist/interpreter/test-host.js.map +1 -0
- 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-error.d.ts +5 -0
- package/dist/server/code-runner-error.js +12 -0
- package/dist/server/code-runner-error.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 +3 -0
- package/dist/server/index.js +10 -0
- package/dist/server/index.js.map +1 -0
- package/dist/steps/execute-step.d.ts +50 -0
- package/dist/steps/execute-step.js +136 -0
- package/dist/steps/execute-step.js.map +1 -0
- 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-access.d.ts +8 -0
- package/dist/steps/step-access.js +39 -0
- package/dist/steps/step-access.js.map +1 -0
- package/dist/steps/step-fields.d.ts +5 -0
- package/dist/steps/step-fields.js +68 -0
- package/dist/steps/step-fields.js.map +1 -0
- package/dist/steps/value-source.d.ts +6 -0
- package/dist/steps/value-source.js +58 -0
- package/dist/steps/value-source.js.map +1 -0
- package/jest.config.ts +11 -0
- package/package.json +68 -3
- package/scripts/build-engine.cjs +70 -0
- package/src/__tests__/engine-bundle.test.ts +121 -0
- package/src/__tests__/surface.test.ts +53 -0
- package/src/access/__tests__/host-match.test.ts +24 -0
- package/src/access/access.types.ts +25 -0
- package/src/access/host-match.ts +13 -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 -0
- package/src/definition/definition.limits.ts +9 -0
- package/src/definition/definition.schema.ts +172 -0
- package/src/definition/definition.types.ts +171 -0
- package/src/definition/validate-definition.ts +206 -0
- package/src/definition/walk-steps.ts +44 -0
- 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 -0
- package/src/index.ts +30 -0
- package/src/interpreter/__tests__/interpreter.test.ts +854 -0
- package/src/interpreter/__tests__/log-respond-resume.test.ts +223 -0
- package/src/interpreter/interpreter-error.ts +12 -0
- package/src/interpreter/interpreter.ts +461 -0
- package/src/interpreter/interpreter.types.ts +192 -0
- package/src/interpreter/sequence-path.ts +67 -0
- package/src/interpreter/test-host.ts +85 -0
- 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 -0
- package/src/server/code-runner-session.ts +310 -0
- package/src/server/index.ts +11 -0
- package/src/steps/__tests__/global-references.test.ts +113 -0
- package/src/steps/execute-step.ts +186 -0
- package/src/steps/global-references.ts +48 -0
- package/src/steps/step-access.ts +56 -0
- package/src/steps/step-fields.ts +87 -0
- package/src/steps/value-source.ts +69 -0
- package/tsconfig.check.json +15 -0
- package/tsconfig.json +35 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { AccessGrant } from '../access/access.types';
|
|
2
|
+
import type {
|
|
3
|
+
HostActionCatalog,
|
|
4
|
+
HttpMethod,
|
|
5
|
+
LogLevel,
|
|
6
|
+
StopStatus,
|
|
7
|
+
} from '../definition/definition.types';
|
|
8
|
+
import type { RuntimeErrorDetail } from '../errors/runtime-error-code.enum';
|
|
9
|
+
import type { EvaluationItem, EvaluationResult } from '../runner-protocol/evaluation.types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Version of the `RunState` layout. A state persisted by a host (Flow, during a suspension) is
|
|
13
|
+
* resumed only by a core that reads its version; any change to the layout bumps it.
|
|
14
|
+
*/
|
|
15
|
+
export const CURRENT_STATE_VERSION = 1;
|
|
16
|
+
|
|
17
|
+
export enum RunStatus {
|
|
18
|
+
/** Waiting for the result of the effect it just emitted. */
|
|
19
|
+
WAITING = 'WAITING',
|
|
20
|
+
SUCCEEDED = 'SUCCEEDED',
|
|
21
|
+
FAILED = 'FAILED',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RunBudget {
|
|
25
|
+
maxSteps: number;
|
|
26
|
+
maxEffects: number;
|
|
27
|
+
/** Longest `durationMs` a WAIT may ask for; the host applies the same bound to `until`. */
|
|
28
|
+
maxWaitMs: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface RunOptions {
|
|
32
|
+
catalog: HostActionCatalog;
|
|
33
|
+
/** What this run is actually allowed to do; compared to the definition's declarations. */
|
|
34
|
+
grants: AccessGrant[];
|
|
35
|
+
budget: RunBudget;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Data an evaluation sees: `input`, `steps.<id>.output`, `vars`. `fn.*` is added by the runner. */
|
|
39
|
+
export interface EvaluationContext {
|
|
40
|
+
input: unknown;
|
|
41
|
+
steps: Record<string, { output: unknown }>;
|
|
42
|
+
vars: Record<string, unknown>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export enum EffectType {
|
|
46
|
+
EVALUATE = 'EVALUATE',
|
|
47
|
+
HTTP = 'HTTP',
|
|
48
|
+
HOST = 'HOST',
|
|
49
|
+
WAIT = 'WAIT',
|
|
50
|
+
LOG = 'LOG',
|
|
51
|
+
RESPOND = 'RESPOND',
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Every expression and template of one step, sent to the code runner as one lot. */
|
|
55
|
+
export interface EvaluateEffect {
|
|
56
|
+
type: EffectType.EVALUATE;
|
|
57
|
+
stepId: string;
|
|
58
|
+
items: EvaluationItem[];
|
|
59
|
+
context: EvaluationContext;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface HttpRequest {
|
|
63
|
+
method: HttpMethod;
|
|
64
|
+
url: string;
|
|
65
|
+
headers: Record<string, string>;
|
|
66
|
+
body?: unknown;
|
|
67
|
+
timeoutMs?: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface HttpEffect {
|
|
71
|
+
type: EffectType.HTTP;
|
|
72
|
+
stepId: string;
|
|
73
|
+
request: HttpRequest;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface HostEffect {
|
|
77
|
+
type: EffectType.HOST;
|
|
78
|
+
stepId: string;
|
|
79
|
+
action: string;
|
|
80
|
+
input: Record<string, unknown>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The interpreter has no clock: the host turns either field into a wake-up. It bounds
|
|
85
|
+
* `durationMs` against `RunBudget.maxWaitMs` itself, but cannot bound `until`: the host must
|
|
86
|
+
* refuse an `until` further from now than the same `maxWaitMs`.
|
|
87
|
+
*/
|
|
88
|
+
export interface WaitEffect {
|
|
89
|
+
type: EffectType.WAIT;
|
|
90
|
+
stepId: string;
|
|
91
|
+
durationMs?: number;
|
|
92
|
+
/** ISO 8601. */
|
|
93
|
+
until?: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface LogEffect {
|
|
97
|
+
type: EffectType.LOG;
|
|
98
|
+
stepId: string;
|
|
99
|
+
level: LogLevel;
|
|
100
|
+
message: string;
|
|
101
|
+
data?: unknown;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The HTTP response a RESPOND step sends to the caller of the run. */
|
|
105
|
+
export interface HttpResponseToSend {
|
|
106
|
+
status: number;
|
|
107
|
+
headers: Record<string, string>;
|
|
108
|
+
body?: unknown;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface RespondEffect {
|
|
112
|
+
type: EffectType.RESPOND;
|
|
113
|
+
stepId: string;
|
|
114
|
+
response: HttpResponseToSend;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export type Effect =
|
|
118
|
+
| EvaluateEffect
|
|
119
|
+
| HttpEffect
|
|
120
|
+
| HostEffect
|
|
121
|
+
| WaitEffect
|
|
122
|
+
| LogEffect
|
|
123
|
+
| RespondEffect;
|
|
124
|
+
|
|
125
|
+
export enum RunEventType {
|
|
126
|
+
EVALUATED = 'EVALUATED',
|
|
127
|
+
HTTP_COMPLETED = 'HTTP_COMPLETED',
|
|
128
|
+
HOST_COMPLETED = 'HOST_COMPLETED',
|
|
129
|
+
WAIT_ELAPSED = 'WAIT_ELAPSED',
|
|
130
|
+
LOG_WRITTEN = 'LOG_WRITTEN',
|
|
131
|
+
RESPONSE_SENT = 'RESPONSE_SENT',
|
|
132
|
+
/**
|
|
133
|
+
* Answers the pending effect of a resumed run (any effect but EVALUATE): `output` becomes the
|
|
134
|
+
* output of the step that suspended.
|
|
135
|
+
*/
|
|
136
|
+
RESUMED = 'RESUMED',
|
|
137
|
+
EFFECT_FAILED = 'EFFECT_FAILED',
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface HttpResponse {
|
|
141
|
+
status: number;
|
|
142
|
+
headers: Record<string, string>;
|
|
143
|
+
body: unknown;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type RunEvent =
|
|
147
|
+
| { type: RunEventType.EVALUATED; results: EvaluationResult[] }
|
|
148
|
+
| { type: RunEventType.HTTP_COMPLETED; response: HttpResponse }
|
|
149
|
+
| { type: RunEventType.HOST_COMPLETED; output: unknown }
|
|
150
|
+
| { type: RunEventType.WAIT_ELAPSED }
|
|
151
|
+
| { type: RunEventType.LOG_WRITTEN }
|
|
152
|
+
| { type: RunEventType.RESPONSE_SENT }
|
|
153
|
+
| { type: RunEventType.RESUMED; output: unknown }
|
|
154
|
+
| { type: RunEventType.EFFECT_FAILED; error: RuntimeErrorDetail };
|
|
155
|
+
|
|
156
|
+
/** Position in the step tree: indexes from the root sequence down to the current step. */
|
|
157
|
+
export interface Frame {
|
|
158
|
+
/** Path of the sequence, e.g. `steps`, `steps[2].then`, `steps[1].cases[0].steps`. */
|
|
159
|
+
sequencePath: string;
|
|
160
|
+
index: number;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Plain JSON: a host can persist it during a long wait (Flow) or keep it in memory (Reflex).
|
|
165
|
+
* Only the interpreter reads or writes its fields.
|
|
166
|
+
*/
|
|
167
|
+
export interface RunState {
|
|
168
|
+
/** Layout of this object; see `CURRENT_STATE_VERSION`. */
|
|
169
|
+
stateVersion: number;
|
|
170
|
+
formatVersion: number;
|
|
171
|
+
status: RunStatus;
|
|
172
|
+
input: unknown;
|
|
173
|
+
stack: Frame[];
|
|
174
|
+
outputs: Record<string, { output: unknown }>;
|
|
175
|
+
vars: Record<string, unknown>;
|
|
176
|
+
/** The effect the run waits for, if any. */
|
|
177
|
+
pending: Effect | null;
|
|
178
|
+
counters: { steps: number; effects: number };
|
|
179
|
+
/** Set once the run is SUCCEEDED (stop output) or FAILED. */
|
|
180
|
+
outcome?: {
|
|
181
|
+
status: StopStatus;
|
|
182
|
+
output?: unknown;
|
|
183
|
+
error?: RuntimeErrorDetail & { stepId?: string };
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface StepResult {
|
|
188
|
+
state: RunState;
|
|
189
|
+
/** Zero or one effect: the interpreter waits for one result at a time. */
|
|
190
|
+
effects: Effect[];
|
|
191
|
+
status: RunStatus;
|
|
192
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
|
|
2
|
+
import { StepType } from '../definition/definition.types';
|
|
3
|
+
import type { RuntimeDefinition, Step } from '../definition/definition.types';
|
|
4
|
+
import { InterpreterError } from './interpreter-error';
|
|
5
|
+
import type { Frame } from './interpreter.types';
|
|
6
|
+
|
|
7
|
+
const ROOT = 'steps';
|
|
8
|
+
const SEGMENT = /\[(\d+)\]\.(?:(then|else|default)|cases\[(\d+)\]\.steps)/y;
|
|
9
|
+
|
|
10
|
+
const corrupted = (path: string): InterpreterError =>
|
|
11
|
+
new InterpreterError(
|
|
12
|
+
RuntimeErrorCode.INVALID_REQUEST,
|
|
13
|
+
`Run state points outside the definition: ${path}`,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const branchOf = (
|
|
17
|
+
step: Step,
|
|
18
|
+
name: string | undefined,
|
|
19
|
+
caseIndex: string | undefined,
|
|
20
|
+
): Step[] | undefined => {
|
|
21
|
+
if (step.type === StepType.CONDITION) {
|
|
22
|
+
return name === 'then' ? step.then : name === 'else' ? step.else : undefined;
|
|
23
|
+
}
|
|
24
|
+
if (step.type === StepType.SWITCH) {
|
|
25
|
+
return name === 'default'
|
|
26
|
+
? step.default
|
|
27
|
+
: caseIndex === undefined
|
|
28
|
+
? undefined
|
|
29
|
+
: step.cases[Number(caseIndex)]?.steps;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** The sequence a frame iterates: the root one, or a branch reached through the path. */
|
|
35
|
+
export const resolveSequence = (definition: RuntimeDefinition, path: string): Step[] => {
|
|
36
|
+
if (!path.startsWith(ROOT)) {
|
|
37
|
+
throw corrupted(path);
|
|
38
|
+
}
|
|
39
|
+
let sequence = definition.steps;
|
|
40
|
+
let position = ROOT.length;
|
|
41
|
+
while (position < path.length) {
|
|
42
|
+
SEGMENT.lastIndex = position;
|
|
43
|
+
const match = SEGMENT.exec(path);
|
|
44
|
+
const next = match ? branchOf(sequence[Number(match[1])], match[2], match[3]) : undefined;
|
|
45
|
+
if (!match || !next) {
|
|
46
|
+
throw corrupted(path);
|
|
47
|
+
}
|
|
48
|
+
sequence = next;
|
|
49
|
+
position = SEGMENT.lastIndex;
|
|
50
|
+
}
|
|
51
|
+
return sequence;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** The step a frame points at. */
|
|
55
|
+
export const resolveStep = (definition: RuntimeDefinition, frame: Frame): Step => {
|
|
56
|
+
const step = resolveSequence(definition, frame.sequencePath)[frame.index];
|
|
57
|
+
if (!step) {
|
|
58
|
+
throw corrupted(frame.sequencePath);
|
|
59
|
+
}
|
|
60
|
+
return step;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** Path of a branch of the step the frame points at. */
|
|
64
|
+
export const branchPath = (frame: Frame, branch: string): string =>
|
|
65
|
+
`${frame.sequencePath}[${frame.index}].${branch}`;
|
|
66
|
+
|
|
67
|
+
export const ROOT_SEQUENCE_PATH = ROOT;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { RuntimeDefinition } from '../definition/definition.types';
|
|
2
|
+
import { start, step } from './interpreter';
|
|
3
|
+
import { EffectType, RunEventType } from './interpreter.types';
|
|
4
|
+
import type {
|
|
5
|
+
Effect,
|
|
6
|
+
EvaluateEffect,
|
|
7
|
+
HostEffect,
|
|
8
|
+
HttpEffect,
|
|
9
|
+
HttpResponse,
|
|
10
|
+
LogEffect,
|
|
11
|
+
RespondEffect,
|
|
12
|
+
RunEvent,
|
|
13
|
+
RunOptions,
|
|
14
|
+
RunState,
|
|
15
|
+
StepResult,
|
|
16
|
+
WaitEffect,
|
|
17
|
+
} from './interpreter.types';
|
|
18
|
+
import type { EvaluationResult } from '../runner-protocol/evaluation.types';
|
|
19
|
+
|
|
20
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* How a simulated host answers each effect. `evaluate` is mandatory: the package evaluates no
|
|
24
|
+
* JavaScript. The other handlers default to an empty answer; `wait` always elapses at once,
|
|
25
|
+
* `log` and `respond` default to accepting the line or the response.
|
|
26
|
+
*/
|
|
27
|
+
export interface TestHostHandlers {
|
|
28
|
+
evaluate: (effect: EvaluateEffect) => MaybePromise<EvaluationResult[]>;
|
|
29
|
+
http?: (effect: HttpEffect) => MaybePromise<HttpResponse>;
|
|
30
|
+
host?: (effect: HostEffect) => MaybePromise<unknown>;
|
|
31
|
+
wait?: (effect: WaitEffect) => MaybePromise<void>;
|
|
32
|
+
log?: (effect: LogEffect) => MaybePromise<void>;
|
|
33
|
+
respond?: (effect: RespondEffect) => MaybePromise<void>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface TestRunResult {
|
|
37
|
+
state: RunState;
|
|
38
|
+
/** Every effect the interpreter emitted, in order. */
|
|
39
|
+
effects: Effect[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const DEFAULT_HTTP_RESPONSE: HttpResponse = { status: 200, headers: {}, body: null };
|
|
43
|
+
|
|
44
|
+
const answer = async (effect: Effect, handlers: TestHostHandlers): Promise<RunEvent> => {
|
|
45
|
+
switch (effect.type) {
|
|
46
|
+
case EffectType.EVALUATE:
|
|
47
|
+
return { type: RunEventType.EVALUATED, results: await handlers.evaluate(effect) };
|
|
48
|
+
case EffectType.HTTP:
|
|
49
|
+
return {
|
|
50
|
+
type: RunEventType.HTTP_COMPLETED,
|
|
51
|
+
response: handlers.http ? await handlers.http(effect) : DEFAULT_HTTP_RESPONSE,
|
|
52
|
+
};
|
|
53
|
+
case EffectType.HOST:
|
|
54
|
+
return {
|
|
55
|
+
type: RunEventType.HOST_COMPLETED,
|
|
56
|
+
output: handlers.host ? await handlers.host(effect) : null,
|
|
57
|
+
};
|
|
58
|
+
case EffectType.WAIT:
|
|
59
|
+
await handlers.wait?.(effect);
|
|
60
|
+
return { type: RunEventType.WAIT_ELAPSED };
|
|
61
|
+
case EffectType.LOG:
|
|
62
|
+
await handlers.log?.(effect);
|
|
63
|
+
return { type: RunEventType.LOG_WRITTEN };
|
|
64
|
+
case EffectType.RESPOND:
|
|
65
|
+
await handlers.respond?.(effect);
|
|
66
|
+
return { type: RunEventType.RESPONSE_SENT };
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** Runs a definition to its end, answering every effect with `handlers` and executing nothing. */
|
|
71
|
+
export const runToCompletion = async (
|
|
72
|
+
definition: RuntimeDefinition,
|
|
73
|
+
input: unknown,
|
|
74
|
+
options: RunOptions,
|
|
75
|
+
handlers: TestHostHandlers,
|
|
76
|
+
): Promise<TestRunResult> => {
|
|
77
|
+
const effects: Effect[] = [];
|
|
78
|
+
let result: StepResult = start(definition, input, options);
|
|
79
|
+
while (result.effects.length > 0) {
|
|
80
|
+
const [effect] = result.effects;
|
|
81
|
+
effects.push(effect);
|
|
82
|
+
result = step(definition, result.state, await answer(effect, handlers), options);
|
|
83
|
+
}
|
|
84
|
+
return { state: result.state, effects };
|
|
85
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { RuntimeErrorDetail } from '../errors/runtime-error-code.enum';
|
|
2
|
+
|
|
3
|
+
export enum EvaluationKind {
|
|
4
|
+
/** A full JavaScript expression. */
|
|
5
|
+
EXPRESSION = 'EXPRESSION',
|
|
6
|
+
/** A string with `{{ … }}` placeholders; a lone placeholder keeps its value's type. */
|
|
7
|
+
TEMPLATE = 'TEMPLATE',
|
|
8
|
+
/** JavaScript statements; the value of their `return` is the result. */
|
|
9
|
+
CODE = 'CODE',
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface EvaluationItem {
|
|
13
|
+
/** Unique within the step. */
|
|
14
|
+
id: string;
|
|
15
|
+
kind: EvaluationKind;
|
|
16
|
+
source: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type EvaluationResult =
|
|
20
|
+
| { id: string; ok: true; value: unknown }
|
|
21
|
+
| { id: string; ok: false; error: RuntimeErrorDetail };
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import type { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
|
|
2
|
+
import type { RuntimeDefinition } from '../definition/definition.types';
|
|
3
|
+
import type { RunState } from '../interpreter/interpreter.types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* WebSocket sub-protocol of an execution (ADR-0238): one connection, one `execute`, one isolate
|
|
7
|
+
* running the whole definition. A new version is a new name, never a silent change.
|
|
8
|
+
*/
|
|
9
|
+
export const EXECUTION_PROTOCOL = 'abyss-runtime.v2';
|
|
10
|
+
|
|
11
|
+
/** Upper bound, in JSON bytes, on the arguments of one `call` and on the value of its result. */
|
|
12
|
+
export const MAX_CALL_PAYLOAD_BYTES = 1_048_576;
|
|
13
|
+
|
|
14
|
+
/** Upper bound, in characters of JSON, on the step output a `progress` message carries. */
|
|
15
|
+
export const MAX_PROGRESS_OUTPUT_CHARS = 4_096;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Capabilities the core's own steps call. Every other name (Store, secrets, a product's
|
|
19
|
+
* actions) is declared by the host; the runner relays whatever the host lists.
|
|
20
|
+
*/
|
|
21
|
+
export enum CoreCapability {
|
|
22
|
+
/** HTTP step, and `fetch()` in client code. Arguments: an `HttpRequest`. */
|
|
23
|
+
HTTP_FETCH = 'http.fetch',
|
|
24
|
+
/** WAIT step. Arguments: `{ durationMs }` or `{ until }`. */
|
|
25
|
+
WAIT = 'wait',
|
|
26
|
+
/** LOG step. Arguments: `{ level, message, data? }`. */
|
|
27
|
+
LOG_WRITE = 'log.write',
|
|
28
|
+
/** RESPOND step. Arguments: `{ status, headers, body? }`. */
|
|
29
|
+
HTTP_RESPOND = 'http.respond',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export enum ExecutionMessageType {
|
|
33
|
+
EXECUTE = 'execute',
|
|
34
|
+
CALL = 'call',
|
|
35
|
+
CALL_RESULT = 'callResult',
|
|
36
|
+
PROGRESS = 'progress',
|
|
37
|
+
FINISHED = 'finished',
|
|
38
|
+
REJECTED = 'rejected',
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type JSONValue =
|
|
42
|
+
| string
|
|
43
|
+
| number
|
|
44
|
+
| boolean
|
|
45
|
+
| null
|
|
46
|
+
| JSONValue[]
|
|
47
|
+
| { [key: string]: JSONValue };
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Names `globals` cannot define: the evaluation context (`input`, `steps`, `vars`) and the
|
|
51
|
+
* client API (`fetch`, `abyss`, `console`).
|
|
52
|
+
*/
|
|
53
|
+
export const RESERVED_GLOBAL_NAMES: readonly string[] = [
|
|
54
|
+
'input',
|
|
55
|
+
'steps',
|
|
56
|
+
'vars',
|
|
57
|
+
'fetch',
|
|
58
|
+
'abyss',
|
|
59
|
+
'console',
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
export interface ExecutionLimits {
|
|
63
|
+
/** Heap of the execution's isolate: the memory of its resource profile. */
|
|
64
|
+
memoryMb: number;
|
|
65
|
+
/** CPU time of the whole execution (one segment when it suspends). */
|
|
66
|
+
cpuTimeMs: number;
|
|
67
|
+
/** Wall-clock time of the whole execution (one segment when it suspends). */
|
|
68
|
+
wallTimeMs: number;
|
|
69
|
+
/** Whether the execution holds a core of the runner for itself. */
|
|
70
|
+
guaranteedCpu: boolean;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface ExecutionBudget {
|
|
74
|
+
maxSteps: number;
|
|
75
|
+
maxEffects: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Host → runner: the execution to run. One per connection. */
|
|
79
|
+
export interface ExecuteMessage {
|
|
80
|
+
type: ExecutionMessageType.EXECUTE;
|
|
81
|
+
executionId: string;
|
|
82
|
+
definition: RuntimeDefinition;
|
|
83
|
+
input: unknown;
|
|
84
|
+
/** Present to resume a suspended execution: the `state` its `finished` message returned. */
|
|
85
|
+
state?: RunState;
|
|
86
|
+
/** On resume: becomes the output of the step that suspended. Absent for a timer wait. */
|
|
87
|
+
resumeInput?: unknown;
|
|
88
|
+
/** Names of the host functions the execution may call; nothing else is exposed. */
|
|
89
|
+
capabilities: string[];
|
|
90
|
+
limits: ExecutionLimits;
|
|
91
|
+
budget: ExecutionBudget;
|
|
92
|
+
/**
|
|
93
|
+
* A WAIT longer than this suspends the execution instead of calling `wait`. Absent: no wait
|
|
94
|
+
* suspends on its own, and the host bounds every wait (Reflex keeps waits in the execution).
|
|
95
|
+
*/
|
|
96
|
+
suspendWaitAboveMs?: number;
|
|
97
|
+
/**
|
|
98
|
+
* Read-only values the host provides to the client code: each top-level key becomes a global
|
|
99
|
+
* (`secrets`, a workflow context...), deep-frozen and rebuilt in every evaluation. A key cannot
|
|
100
|
+
* be one of `RESERVED_GLOBAL_NAMES`: the runner refuses the request with `INVALID_REQUEST`.
|
|
101
|
+
* Never logged by the runner; the host masks what it stores. Dynamic access
|
|
102
|
+
* (`secrets[name]`) is not resolvable by the host: it yields `undefined`.
|
|
103
|
+
*/
|
|
104
|
+
globals?: Record<string, JSONValue>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Runner → host: the step `stepId` calls the host function `capability`. */
|
|
108
|
+
export interface ExecutionCallMessage {
|
|
109
|
+
type: ExecutionMessageType.CALL;
|
|
110
|
+
callId: string;
|
|
111
|
+
stepId: string;
|
|
112
|
+
capability: string;
|
|
113
|
+
args: unknown;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface CallError {
|
|
117
|
+
/** A `RuntimeErrorCode`; absent, the runner reports the capability's default code. */
|
|
118
|
+
code?: RuntimeErrorCode;
|
|
119
|
+
message: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Host → runner: the answer to one `call`. `suspend` asks the runner to stop after this step
|
|
124
|
+
* and finish SUSPENDED: a resume then gives the step its output through `resumeInput`.
|
|
125
|
+
*/
|
|
126
|
+
export type ExecutionCallResultMessage =
|
|
127
|
+
| { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; value: unknown }
|
|
128
|
+
| { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; suspend: true }
|
|
129
|
+
| { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: false; error: CallError };
|
|
130
|
+
|
|
131
|
+
export interface StepMetrics {
|
|
132
|
+
cpuMs: number;
|
|
133
|
+
heapPeakBytes: number;
|
|
134
|
+
wallMs: number;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface ExecutionMetrics {
|
|
138
|
+
queueMs: number;
|
|
139
|
+
wallMs: number;
|
|
140
|
+
cpuMs: number;
|
|
141
|
+
heapPeakBytes: number;
|
|
142
|
+
memoryLimitBytes: number;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export enum StepPhase {
|
|
146
|
+
STARTED = 'started',
|
|
147
|
+
FINISHED = 'finished',
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export enum StepStatus {
|
|
151
|
+
SUCCEEDED = 'SUCCEEDED',
|
|
152
|
+
FAILED = 'FAILED',
|
|
153
|
+
/** The step's call was answered with `suspend`, or its wait is above `suspendWaitAboveMs`. */
|
|
154
|
+
SUSPENDED = 'SUSPENDED',
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Runner → host: a step started or finished. The base of the trace. */
|
|
158
|
+
export interface ProgressMessage {
|
|
159
|
+
type: ExecutionMessageType.PROGRESS;
|
|
160
|
+
stepId: string;
|
|
161
|
+
phase: StepPhase;
|
|
162
|
+
/** On `finished`. */
|
|
163
|
+
status?: StepStatus;
|
|
164
|
+
/** On `finished`. */
|
|
165
|
+
durationMs?: number;
|
|
166
|
+
/** On `finished`, measured by the runner. */
|
|
167
|
+
metrics?: StepMetrics;
|
|
168
|
+
/**
|
|
169
|
+
* On `finished` with SUCCEEDED: the step's output, or, past `MAX_PROGRESS_OUTPUT_CHARS`, the
|
|
170
|
+
* beginning of its JSON with `outputTruncated`.
|
|
171
|
+
*/
|
|
172
|
+
output?: unknown;
|
|
173
|
+
outputTruncated?: boolean;
|
|
174
|
+
/** On `finished` with FAILED. */
|
|
175
|
+
errorCode?: RuntimeErrorCode;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export enum ExecutionStatus {
|
|
179
|
+
SUCCEEDED = 'SUCCEEDED',
|
|
180
|
+
FAILED = 'FAILED',
|
|
181
|
+
SUSPENDED = 'SUSPENDED',
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export interface ExecutionOutcome {
|
|
185
|
+
status: ExecutionStatus;
|
|
186
|
+
/** SUCCEEDED, or FAILED by a STOP step: the stop output. */
|
|
187
|
+
output?: unknown;
|
|
188
|
+
/** FAILED by an error; absent when a STOP step chose FAILED. */
|
|
189
|
+
errorCode?: RuntimeErrorCode;
|
|
190
|
+
errorMessage?: string;
|
|
191
|
+
/** FAILED: the step that failed, when one did. */
|
|
192
|
+
stepId?: string;
|
|
193
|
+
/** SUSPENDED: what to send back as `state` to resume. */
|
|
194
|
+
state?: RunState;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Runner → host: the execution ended. Closes the connection. */
|
|
198
|
+
export interface FinishedMessage {
|
|
199
|
+
type: ExecutionMessageType.FINISHED;
|
|
200
|
+
outcome: ExecutionOutcome;
|
|
201
|
+
metrics: ExecutionMetrics;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export type ExecutionRejectionCode =
|
|
205
|
+
| RuntimeErrorCode.RUNNER_SATURATED
|
|
206
|
+
| RuntimeErrorCode.INVALID_REQUEST
|
|
207
|
+
| RuntimeErrorCode.STATE_VERSION_UNSUPPORTED;
|
|
208
|
+
|
|
209
|
+
/** Runner → host: the execution was not run at all. */
|
|
210
|
+
export interface ExecutionRejectedMessage {
|
|
211
|
+
type: ExecutionMessageType.REJECTED;
|
|
212
|
+
code: ExecutionRejectionCode;
|
|
213
|
+
message: string;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export type ExecutionHostMessage = ExecuteMessage | ExecutionCallResultMessage;
|
|
217
|
+
export type ExecutionRunnerMessage =
|
|
218
|
+
| ExecutionCallMessage
|
|
219
|
+
| ProgressMessage
|
|
220
|
+
| FinishedMessage
|
|
221
|
+
| ExecutionRejectedMessage;
|