@abyss-project/runtime 3.4.0 → 3.6.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 (58) hide show
  1. package/.eslintrc.js +32 -32
  2. package/.prettierrc +5 -5
  3. package/dist/discord/discord.types.d.ts +167 -4
  4. package/dist/discord/discord.types.js +48 -1
  5. package/dist/discord/discord.types.js.map +1 -1
  6. package/dist/engine/engine-source.js +1 -1
  7. package/dist/interpreter/interpreter.js +18 -4
  8. package/dist/interpreter/interpreter.js.map +1 -1
  9. package/dist/interpreter/interpreter.types.d.ts +2 -0
  10. package/dist/interpreter/interpreter.types.js.map +1 -1
  11. package/dist/runner-protocol/execution-protocol.types.d.ts +11 -0
  12. package/dist/runner-protocol/execution-protocol.types.js +2 -1
  13. package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
  14. package/jest.config.ts +11 -11
  15. package/package.json +15 -10
  16. package/scripts/build-engine.cjs +70 -70
  17. package/src/__tests__/engine-bundle.test.ts +121 -121
  18. package/src/__tests__/surface.test.ts +5 -1
  19. package/src/access/__tests__/host-match.test.ts +24 -24
  20. package/src/access/access.types.ts +25 -25
  21. package/src/access/host-match.ts +13 -13
  22. package/src/cost/__tests__/execution-cost.test.ts +196 -196
  23. package/src/cost/execution-cost.ts +186 -186
  24. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  25. package/src/definition/definition.limits.ts +9 -9
  26. package/src/definition/definition.schema.ts +172 -172
  27. package/src/definition/definition.types.ts +171 -171
  28. package/src/definition/validate-definition.ts +206 -206
  29. package/src/definition/walk-steps.ts +44 -44
  30. package/src/discord/discord.types.ts +264 -5
  31. package/src/engine/__tests__/run-in-isolate.test.ts +468 -468
  32. package/src/engine/engine-entry.ts +3 -3
  33. package/src/engine/engine-global.ts +2 -2
  34. package/src/engine/run-in-isolate.ts +404 -404
  35. package/src/errors/runtime-error-code.enum.ts +37 -37
  36. package/src/index.ts +32 -32
  37. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  38. package/src/interpreter/__tests__/log-respond-resume.test.ts +270 -223
  39. package/src/interpreter/interpreter-error.ts +12 -12
  40. package/src/interpreter/interpreter.ts +482 -461
  41. package/src/interpreter/interpreter.types.ts +202 -192
  42. package/src/interpreter/sequence-path.ts +67 -67
  43. package/src/interpreter/test-host.ts +85 -85
  44. package/src/runner-protocol/evaluation.types.ts +21 -21
  45. package/src/runner-protocol/execution-protocol.types.ts +254 -231
  46. package/src/schedule/schedule.types.ts +100 -100
  47. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  48. package/src/server/code-runner-error.ts +12 -12
  49. package/src/server/code-runner-session.ts +310 -310
  50. package/src/server/index.ts +11 -11
  51. package/src/steps/__tests__/global-references.test.ts +133 -133
  52. package/src/steps/execute-step.ts +186 -186
  53. package/src/steps/global-references.ts +48 -48
  54. package/src/steps/step-access.ts +56 -56
  55. package/src/steps/step-fields.ts +87 -87
  56. package/src/steps/value-source.ts +69 -69
  57. package/tsconfig.check.json +15 -15
  58. package/tsconfig.json +35 -35
@@ -1,231 +1,254 @@
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-0243): 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
- }
70
-
71
- export interface ExecutionBudget {
72
- maxSteps: number;
73
- maxEffects: number;
74
- }
75
-
76
- /** Host → runner: the execution to run. One per connection. */
77
- export interface ExecuteMessage {
78
- type: ExecutionMessageType.EXECUTE;
79
- executionId: string;
80
- definition: RuntimeDefinition;
81
- input: unknown;
82
- /** Present to resume a suspended execution: the `state` its `finished` message returned. */
83
- state?: RunState;
84
- /** On resume: becomes the output of the step that suspended. Absent for a timer wait. */
85
- resumeInput?: unknown;
86
- /** Names of the host functions the execution may call; nothing else is exposed. */
87
- capabilities: string[];
88
- limits: ExecutionLimits;
89
- budget: ExecutionBudget;
90
- /**
91
- * A WAIT longer than this suspends the execution instead of calling `wait`. Absent: no wait
92
- * suspends on its own, and the host bounds every wait (Reflex keeps waits in the execution).
93
- */
94
- suspendWaitAboveMs?: number;
95
- /**
96
- * Read-only values the host provides to the client code: each top-level key becomes a global
97
- * (`secrets`, a workflow context...), deep-frozen and rebuilt in every evaluation. A key cannot
98
- * be one of `RESERVED_GLOBAL_NAMES`: the runner refuses the request with `INVALID_REQUEST`.
99
- * Never logged by the runner; the host masks what it stores. Dynamic access
100
- * (`secrets[name]`) is not resolvable by the host: it yields `undefined`.
101
- *
102
- * `config` is the conventional key of the non-secret per-environment variables
103
- * (`Record<string, string>`): `{{ config.NAME }}` in a template or expression and `config.NAME` in
104
- * client code, read-only like `secrets`, `undefined` for a name the host did not send. The host
105
- * sends the names `collectGlobalReferences(definition, 'config')` finds. Unlike secrets, these
106
- * values are not masked in traces.
107
- */
108
- globals?: Record<string, JSONValue>;
109
- }
110
-
111
- /** Runner → host: the step `stepId` calls the host function `capability`. */
112
- export interface ExecutionCallMessage {
113
- type: ExecutionMessageType.CALL;
114
- callId: string;
115
- stepId: string;
116
- capability: string;
117
- args: unknown;
118
- }
119
-
120
- export interface CallError {
121
- /** A `RuntimeErrorCode`; absent, the runner reports the capability's default code. */
122
- code?: RuntimeErrorCode;
123
- message: string;
124
- }
125
-
126
- /**
127
- * Host → runner: the answer to one `call`. `suspend` asks the runner to stop after this step
128
- * and finish SUSPENDED: a resume then gives the step its output through `resumeInput`.
129
- */
130
- export type ExecutionCallResultMessage =
131
- | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; value: unknown }
132
- | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; suspend: true }
133
- | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: false; error: CallError };
134
-
135
- export interface StepMetrics {
136
- cpuMs: number;
137
- /**
138
- * What the step itself grew the heap by: the highest reading during the step minus the reading
139
- * at its start, never below 0. An estimate that can err both ways (a late sample misses a short
140
- * peak, a delayed GC keeps freed memory), and blind to the isolate's base heap and to what earlier
141
- * steps left.
142
- */
143
- heapPeakBytes: number;
144
- wallMs: number;
145
- }
146
-
147
- export interface ExecutionMetrics {
148
- queueMs: number;
149
- wallMs: number;
150
- cpuMs: number;
151
- heapPeakBytes: number;
152
- memoryLimitBytes: number;
153
- }
154
-
155
- export enum StepPhase {
156
- STARTED = 'started',
157
- FINISHED = 'finished',
158
- }
159
-
160
- export enum StepStatus {
161
- SUCCEEDED = 'SUCCEEDED',
162
- FAILED = 'FAILED',
163
- /** The step's call was answered with `suspend`, or its wait is above `suspendWaitAboveMs`. */
164
- SUSPENDED = 'SUSPENDED',
165
- }
166
-
167
- /** Runner → host: a step started or finished. The base of the trace. */
168
- export interface ProgressMessage {
169
- type: ExecutionMessageType.PROGRESS;
170
- stepId: string;
171
- phase: StepPhase;
172
- /** On `finished`. */
173
- status?: StepStatus;
174
- /** On `finished`. */
175
- durationMs?: number;
176
- /** On `finished`, measured by the runner. */
177
- metrics?: StepMetrics;
178
- /**
179
- * On `finished` with SUCCEEDED: the step's output, or, past `MAX_PROGRESS_OUTPUT_CHARS`, the
180
- * beginning of its JSON with `outputTruncated`.
181
- */
182
- output?: unknown;
183
- outputTruncated?: boolean;
184
- /** On `finished` with FAILED. */
185
- errorCode?: RuntimeErrorCode;
186
- }
187
-
188
- export enum ExecutionStatus {
189
- SUCCEEDED = 'SUCCEEDED',
190
- FAILED = 'FAILED',
191
- SUSPENDED = 'SUSPENDED',
192
- }
193
-
194
- export interface ExecutionOutcome {
195
- status: ExecutionStatus;
196
- /** SUCCEEDED, or FAILED by a STOP step: the stop output. */
197
- output?: unknown;
198
- /** FAILED by an error; absent when a STOP step chose FAILED. */
199
- errorCode?: RuntimeErrorCode;
200
- errorMessage?: string;
201
- /** FAILED: the step that failed, when one did. */
202
- stepId?: string;
203
- /** SUSPENDED: what to send back as `state` to resume. */
204
- state?: RunState;
205
- }
206
-
207
- /** Runner → host: the execution ended. Closes the connection. */
208
- export interface FinishedMessage {
209
- type: ExecutionMessageType.FINISHED;
210
- outcome: ExecutionOutcome;
211
- metrics: ExecutionMetrics;
212
- }
213
-
214
- export type ExecutionRejectionCode =
215
- | RuntimeErrorCode.RUNNER_SATURATED
216
- | RuntimeErrorCode.INVALID_REQUEST
217
- | RuntimeErrorCode.STATE_VERSION_UNSUPPORTED;
218
-
219
- /** Runner → host: the execution was not run at all. */
220
- export interface ExecutionRejectedMessage {
221
- type: ExecutionMessageType.REJECTED;
222
- code: ExecutionRejectionCode;
223
- message: string;
224
- }
225
-
226
- export type ExecutionHostMessage = ExecuteMessage | ExecutionCallResultMessage;
227
- export type ExecutionRunnerMessage =
228
- | ExecutionCallMessage
229
- | ProgressMessage
230
- | FinishedMessage
231
- | ExecutionRejectedMessage;
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-0243): 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
+ /** Upper bound, in characters, on the `errorStack` of an outcome or a failed `progress`. */
18
+ export const MAX_ERROR_STACK_CHARS = 4_096;
19
+
20
+ /**
21
+ * Capabilities the core's own steps call. Every other name (Store, secrets, a product's
22
+ * actions) is declared by the host; the runner relays whatever the host lists.
23
+ */
24
+ export enum CoreCapability {
25
+ /** HTTP step, and `fetch()` in client code. Arguments: an `HttpRequest`. */
26
+ HTTP_FETCH = 'http.fetch',
27
+ /** WAIT step. Arguments: `{ durationMs }` or `{ until }`. */
28
+ WAIT = 'wait',
29
+ /** LOG step. Arguments: `{ level, message, data? }`. */
30
+ LOG_WRITE = 'log.write',
31
+ /** RESPOND step. Arguments: `{ status, headers, body? }`. */
32
+ HTTP_RESPOND = 'http.respond',
33
+ }
34
+
35
+ export enum ExecutionMessageType {
36
+ EXECUTE = 'execute',
37
+ CALL = 'call',
38
+ CALL_RESULT = 'callResult',
39
+ PROGRESS = 'progress',
40
+ FINISHED = 'finished',
41
+ REJECTED = 'rejected',
42
+ }
43
+
44
+ export type JSONValue =
45
+ | string
46
+ | number
47
+ | boolean
48
+ | null
49
+ | JSONValue[]
50
+ | { [key: string]: JSONValue };
51
+
52
+ /**
53
+ * Names `globals` cannot define: the evaluation context (`input`, `steps`, `vars`) and the
54
+ * client API (`fetch`, `abyss`, `console`).
55
+ */
56
+ export const RESERVED_GLOBAL_NAMES: readonly string[] = [
57
+ 'input',
58
+ 'steps',
59
+ 'vars',
60
+ 'fetch',
61
+ 'abyss',
62
+ 'console',
63
+ ];
64
+
65
+ export interface ExecutionLimits {
66
+ /** Heap of the execution's isolate: the memory of its resource profile. */
67
+ memoryMb: number;
68
+ /** CPU time of the whole execution (one segment when it suspends). */
69
+ cpuTimeMs: number;
70
+ /** Wall-clock time of the whole execution (one segment when it suspends). */
71
+ wallTimeMs: number;
72
+ }
73
+
74
+ export interface ExecutionBudget {
75
+ maxSteps: number;
76
+ maxEffects: number;
77
+ /**
78
+ * When given, `log.write` calls do not count in `maxEffects`: they are counted apart, and past
79
+ * this number the runner answers them without relaying them to the host.
80
+ */
81
+ maxLogLines?: number;
82
+ }
83
+
84
+ /** Where the client code of a step failed: line and column in the step's own code. */
85
+ export interface IExecutionErrorLocation {
86
+ stepId: string;
87
+ line: number;
88
+ column: number;
89
+ }
90
+
91
+ /** Host → runner: the execution to run. One per connection. */
92
+ export interface ExecuteMessage {
93
+ type: ExecutionMessageType.EXECUTE;
94
+ executionId: string;
95
+ definition: RuntimeDefinition;
96
+ input: unknown;
97
+ /** Present to resume a suspended execution: the `state` its `finished` message returned. */
98
+ state?: RunState;
99
+ /** On resume: becomes the output of the step that suspended. Absent for a timer wait. */
100
+ resumeInput?: unknown;
101
+ /** Names of the host functions the execution may call; nothing else is exposed. */
102
+ capabilities: string[];
103
+ limits: ExecutionLimits;
104
+ budget: ExecutionBudget;
105
+ /**
106
+ * A WAIT longer than this suspends the execution instead of calling `wait`. Absent: no wait
107
+ * suspends on its own, and the host bounds every wait (Reflex keeps waits in the execution).
108
+ */
109
+ suspendWaitAboveMs?: number;
110
+ /**
111
+ * Read-only values the host provides to the client code: each top-level key becomes a global
112
+ * (`secrets`, a workflow context...), deep-frozen and rebuilt in every evaluation. A key cannot
113
+ * be one of `RESERVED_GLOBAL_NAMES`: the runner refuses the request with `INVALID_REQUEST`.
114
+ * Never logged by the runner; the host masks what it stores. Dynamic access
115
+ * (`secrets[name]`) is not resolvable by the host: it yields `undefined`.
116
+ *
117
+ * `config` is the conventional key of the non-secret per-environment variables
118
+ * (`Record<string, string>`): `{{ config.NAME }}` in a template or expression and `config.NAME` in
119
+ * client code, read-only like `secrets`, `undefined` for a name the host did not send. The host
120
+ * sends the names `collectGlobalReferences(definition, 'config')` finds. Unlike secrets, these
121
+ * values are not masked in traces.
122
+ */
123
+ globals?: Record<string, JSONValue>;
124
+ }
125
+
126
+ /** Runner → host: the step `stepId` calls the host function `capability`. */
127
+ export interface ExecutionCallMessage {
128
+ type: ExecutionMessageType.CALL;
129
+ callId: string;
130
+ stepId: string;
131
+ capability: string;
132
+ args: unknown;
133
+ }
134
+
135
+ export interface CallError {
136
+ /** A `RuntimeErrorCode`; absent, the runner reports the capability's default code. */
137
+ code?: RuntimeErrorCode;
138
+ message: string;
139
+ }
140
+
141
+ /**
142
+ * Host → runner: the answer to one `call`. `suspend` asks the runner to stop after this step
143
+ * and finish SUSPENDED: a resume then gives the step its output through `resumeInput`.
144
+ */
145
+ export type ExecutionCallResultMessage =
146
+ | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; value: unknown }
147
+ | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; suspend: true }
148
+ | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: false; error: CallError };
149
+
150
+ export interface StepMetrics {
151
+ cpuMs: number;
152
+ /**
153
+ * What the step itself grew the heap by: the highest reading during the step minus the reading
154
+ * at its start, never below 0. An estimate that can err both ways (a late sample misses a short
155
+ * peak, a delayed GC keeps freed memory), and blind to the isolate's base heap and to what earlier
156
+ * steps left.
157
+ */
158
+ heapPeakBytes: number;
159
+ wallMs: number;
160
+ }
161
+
162
+ export interface ExecutionMetrics {
163
+ queueMs: number;
164
+ wallMs: number;
165
+ cpuMs: number;
166
+ heapPeakBytes: number;
167
+ memoryLimitBytes: number;
168
+ }
169
+
170
+ export enum StepPhase {
171
+ STARTED = 'started',
172
+ FINISHED = 'finished',
173
+ }
174
+
175
+ export enum StepStatus {
176
+ SUCCEEDED = 'SUCCEEDED',
177
+ FAILED = 'FAILED',
178
+ /** The step's call was answered with `suspend`, or its wait is above `suspendWaitAboveMs`. */
179
+ SUSPENDED = 'SUSPENDED',
180
+ }
181
+
182
+ /** Runner → host: a step started or finished. The base of the trace. */
183
+ export interface ProgressMessage {
184
+ type: ExecutionMessageType.PROGRESS;
185
+ stepId: string;
186
+ phase: StepPhase;
187
+ /** On `finished`. */
188
+ status?: StepStatus;
189
+ /** On `finished`. */
190
+ durationMs?: number;
191
+ /** On `finished`, measured by the runner. */
192
+ metrics?: StepMetrics;
193
+ /**
194
+ * On `finished` with SUCCEEDED: the step's output, or, past `MAX_PROGRESS_OUTPUT_CHARS`, the
195
+ * beginning of its JSON with `outputTruncated`.
196
+ */
197
+ output?: unknown;
198
+ outputTruncated?: boolean;
199
+ /** On `finished` with FAILED. */
200
+ errorCode?: RuntimeErrorCode;
201
+ /** On `finished` with FAILED by client code. */
202
+ errorLocation?: IExecutionErrorLocation;
203
+ /** On `finished` with FAILED by client code: at most `MAX_ERROR_STACK_CHARS` characters. */
204
+ errorStack?: string;
205
+ }
206
+
207
+ export enum ExecutionStatus {
208
+ SUCCEEDED = 'SUCCEEDED',
209
+ FAILED = 'FAILED',
210
+ SUSPENDED = 'SUSPENDED',
211
+ }
212
+
213
+ export interface ExecutionOutcome {
214
+ status: ExecutionStatus;
215
+ /** SUCCEEDED, or FAILED by a STOP step: the stop output. */
216
+ output?: unknown;
217
+ /** FAILED by an error; absent when a STOP step chose FAILED. */
218
+ errorCode?: RuntimeErrorCode;
219
+ errorMessage?: string;
220
+ /** FAILED: the step that failed, when one did. */
221
+ stepId?: string;
222
+ /** FAILED by client code: where it failed. */
223
+ errorLocation?: IExecutionErrorLocation;
224
+ /** FAILED by client code: at most `MAX_ERROR_STACK_CHARS` characters. */
225
+ errorStack?: string;
226
+ /** SUSPENDED: what to send back as `state` to resume. */
227
+ state?: RunState;
228
+ }
229
+
230
+ /** Runner → host: the execution ended. Closes the connection. */
231
+ export interface FinishedMessage {
232
+ type: ExecutionMessageType.FINISHED;
233
+ outcome: ExecutionOutcome;
234
+ metrics: ExecutionMetrics;
235
+ }
236
+
237
+ export type ExecutionRejectionCode =
238
+ | RuntimeErrorCode.RUNNER_SATURATED
239
+ | RuntimeErrorCode.INVALID_REQUEST
240
+ | RuntimeErrorCode.STATE_VERSION_UNSUPPORTED;
241
+
242
+ /** Runner → host: the execution was not run at all. */
243
+ export interface ExecutionRejectedMessage {
244
+ type: ExecutionMessageType.REJECTED;
245
+ code: ExecutionRejectionCode;
246
+ message: string;
247
+ }
248
+
249
+ export type ExecutionHostMessage = ExecuteMessage | ExecutionCallResultMessage;
250
+ export type ExecutionRunnerMessage =
251
+ | ExecutionCallMessage
252
+ | ProgressMessage
253
+ | FinishedMessage
254
+ | ExecutionRejectedMessage;