@abyss-project/runtime 3.12.0 → 3.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/.eslintrc.js +32 -32
  2. package/.prettierrc +5 -5
  3. package/README.md +88 -88
  4. package/dist/definition/definition.schema.js +6 -0
  5. package/dist/definition/definition.schema.js.map +1 -1
  6. package/dist/definition/definition.types.d.ts +11 -0
  7. package/dist/definition/definition.types.js +6 -1
  8. package/dist/definition/definition.types.js.map +1 -1
  9. package/dist/discord/discord.types.d.ts +2 -34
  10. package/dist/discord/discord.types.js +1 -9
  11. package/dist/discord/discord.types.js.map +1 -1
  12. package/dist/engine/engine-source.js +1 -1
  13. package/dist/errors/runtime-error-code.enum.d.ts +1 -0
  14. package/dist/errors/runtime-error-code.enum.js +1 -0
  15. package/dist/errors/runtime-error-code.enum.js.map +1 -1
  16. package/dist/runner-protocol/evaluation.types.d.ts +1 -0
  17. package/dist/steps/global-references.js +7 -0
  18. package/dist/steps/global-references.js.map +1 -1
  19. package/dist/steps/step-fields.js +10 -0
  20. package/dist/steps/step-fields.js.map +1 -1
  21. package/jest.config.ts +11 -11
  22. package/package.json +1 -1
  23. package/scripts/build-engine.cjs +70 -70
  24. package/src/__tests__/engine-bundle.test.ts +121 -121
  25. package/src/__tests__/surface.test.ts +81 -88
  26. package/src/access/__tests__/host-match.test.ts +24 -24
  27. package/src/access/access.types.ts +25 -25
  28. package/src/access/host-match.ts +13 -13
  29. package/src/cost/__tests__/execution-cost.test.ts +249 -249
  30. package/src/cost/execution-cost.ts +203 -203
  31. package/src/definition/__tests__/code-step.test.ts +56 -0
  32. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  33. package/src/definition/definition.limits.ts +9 -9
  34. package/src/definition/definition.schema.ts +7 -0
  35. package/src/definition/definition.types.ts +21 -1
  36. package/src/definition/validate-definition.ts +206 -206
  37. package/src/definition/walk-steps.ts +44 -44
  38. package/src/discord/__tests__/discord-event-input.test.ts +90 -108
  39. package/src/discord/discord.types.ts +696 -738
  40. package/src/engine/__tests__/run-in-isolate.test.ts +519 -519
  41. package/src/engine/engine-entry.ts +3 -3
  42. package/src/engine/engine-global.ts +2 -2
  43. package/src/engine/run-in-isolate.ts +462 -462
  44. package/src/errors/runtime-error-code.enum.ts +5 -0
  45. package/src/index.ts +32 -32
  46. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  47. package/src/interpreter/__tests__/log-respond-resume.test.ts +271 -271
  48. package/src/interpreter/interpreter-error.ts +12 -12
  49. package/src/interpreter/interpreter.ts +483 -483
  50. package/src/interpreter/interpreter.types.ts +204 -204
  51. package/src/interpreter/sequence-path.ts +67 -67
  52. package/src/interpreter/test-host.ts +85 -85
  53. package/src/runner-protocol/__tests__/file-payload.test.ts +26 -26
  54. package/src/runner-protocol/evaluation.types.ts +2 -0
  55. package/src/runner-protocol/execution-protocol.types.ts +297 -297
  56. package/src/schedule/schedule.types.ts +100 -100
  57. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  58. package/src/server/code-runner-error.ts +12 -12
  59. package/src/server/code-runner-session.ts +310 -310
  60. package/src/server/index.ts +11 -11
  61. package/src/steps/__tests__/global-references.test.ts +140 -124
  62. package/src/steps/__tests__/step-fields.test.ts +58 -0
  63. package/src/steps/execute-step.ts +186 -186
  64. package/src/steps/global-references.ts +56 -48
  65. package/src/steps/step-access.ts +56 -56
  66. package/src/steps/step-fields.ts +10 -0
  67. package/src/steps/value-source.ts +69 -69
  68. package/tsconfig.check.json +15 -15
  69. package/tsconfig.json +35 -35
@@ -1,297 +1,297 @@
1
- import { REFLEX_ATTACHMENT_MAX_BYTES_BOUND } from '../discord/discord.types';
2
- import type { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
3
- import type { RuntimeDefinition } from '../definition/definition.types';
4
- import type { RunState } from '../interpreter/interpreter.types';
5
-
6
- /**
7
- * WebSocket sub-protocol of an execution (ADR-0243): one connection, one `execute`, one isolate
8
- * running the whole definition. A new version is a new name, never a silent change.
9
- */
10
- export const EXECUTION_PROTOCOL = 'abyss-runtime.v2';
11
-
12
- /** Upper bound, in JSON bytes, on the arguments of one `call` and on the value of its result. */
13
- export const MAX_CALL_PAYLOAD_BYTES = 1_048_576;
14
-
15
- /**
16
- * `budget.maxFilePayloadBytes` for attachments of at most `maxAttachmentBytes`: their base64
17
- * (4 bytes for 3) plus 64 KiB of envelope.
18
- */
19
- export const filePayloadBytesFor = (maxAttachmentBytes: number): number =>
20
- Math.ceil((maxAttachmentBytes * 4) / 3) + 64 * 1024;
21
-
22
- /**
23
- * Highest `budget.maxFilePayloadBytes` a host may grant: `filePayloadBytesFor` the largest
24
- * attachment a bot may configure, below the 16 MiB WebSocket frame of the runner.
25
- */
26
- export const MAX_FILE_CALL_PAYLOAD_BYTES = filePayloadBytesFor(REFLEX_ATTACHMENT_MAX_BYTES_BOUND);
27
-
28
- /**
29
- * Upper bound, in UTF-8 bytes of JSON, on the step output a `progress` message carries whole. Past
30
- * it the message carries a preview of `MAX_PROGRESS_OUTPUT_CHARS` bytes and the real size.
31
- */
32
- export const MAX_PROGRESS_OUTPUT_BYTES = 65_536;
33
-
34
- /**
35
- * Size, in UTF-8 bytes of JSON, of the preview a `progress` message carries for a step output over
36
- * `MAX_PROGRESS_OUTPUT_BYTES` (cut on a character boundary, so never more than this many bytes).
37
- * The name predates the byte unit and is kept: renaming a published export needs a major version.
38
- */
39
- export const MAX_PROGRESS_OUTPUT_CHARS = 4_096;
40
-
41
- /** Upper bound, in characters, on the `errorStack` of an outcome or a failed `progress`. */
42
- export const MAX_ERROR_STACK_CHARS = 4_096;
43
-
44
- /**
45
- * Capabilities the core's own steps call. Every other name (Store, secrets, a product's
46
- * actions) is declared by the host; the runner relays whatever the host lists.
47
- */
48
- export enum CoreCapability {
49
- /** HTTP step, and `fetch()` in client code. Arguments: an `HttpRequest`. */
50
- HTTP_FETCH = 'http.fetch',
51
- /** WAIT step. Arguments: `{ durationMs }` or `{ until }`. */
52
- WAIT = 'wait',
53
- /** LOG step. Arguments: `{ level, message, data? }`. */
54
- LOG_WRITE = 'log.write',
55
- /** RESPOND step. Arguments: `{ status, headers, body? }`. */
56
- HTTP_RESPOND = 'http.respond',
57
- }
58
-
59
- export enum ExecutionMessageType {
60
- EXECUTE = 'execute',
61
- CALL = 'call',
62
- CALL_RESULT = 'callResult',
63
- PROGRESS = 'progress',
64
- FINISHED = 'finished',
65
- REJECTED = 'rejected',
66
- }
67
-
68
- export type JSONValue =
69
- | string
70
- | number
71
- | boolean
72
- | null
73
- | JSONValue[]
74
- | { [key: string]: JSONValue };
75
-
76
- /**
77
- * Names `globals` cannot define: the evaluation context (`input`, `steps`, `vars`) and the
78
- * client API (`fetch`, `abyss`, `console`).
79
- */
80
- export const RESERVED_GLOBAL_NAMES: readonly string[] = [
81
- 'input',
82
- 'steps',
83
- 'vars',
84
- 'fetch',
85
- 'abyss',
86
- 'console',
87
- ];
88
-
89
- export interface ExecutionLimits {
90
- /** Heap of the execution's isolate: the memory of its resource profile. */
91
- memoryMb: number;
92
- /** CPU time of the whole execution (one segment when it suspends). */
93
- cpuTimeMs: number;
94
- /** Wall-clock time of the whole execution (one segment when it suspends). */
95
- wallTimeMs: number;
96
- }
97
-
98
- export interface ExecutionBudget {
99
- maxSteps: number;
100
- maxEffects: number;
101
- /**
102
- * When given, `log.write` calls do not count in `maxEffects`: they are counted apart, and past
103
- * this number the runner answers them without relaying them to the host.
104
- */
105
- maxLogLines?: number;
106
- /**
107
- * When given, a call to a `discord.*` capability whose arguments carry a non-empty `files`
108
- * array may weigh up to this many JSON bytes (instead of `MAX_CALL_PAYLOAD_BYTES`), and so
109
- * may the result of `discord.readAttachment`. Every other call keeps the default bound.
110
- */
111
- maxFilePayloadBytes?: number;
112
- }
113
-
114
- /** Where the client code of a step failed: line and column in the step's own code. */
115
- export interface IExecutionErrorLocation {
116
- stepId: string;
117
- line: number;
118
- column: number;
119
- }
120
-
121
- /** Host → runner: the execution to run. One per connection. */
122
- export interface ExecuteMessage {
123
- type: ExecutionMessageType.EXECUTE;
124
- executionId: string;
125
- definition: RuntimeDefinition;
126
- input: unknown;
127
- /** Present to resume a suspended execution: the `state` its `finished` message returned. */
128
- state?: RunState;
129
- /** On resume: becomes the output of the step that suspended. Absent for a timer wait. */
130
- resumeInput?: unknown;
131
- /** Names of the host functions the execution may call; nothing else is exposed. */
132
- capabilities: string[];
133
- limits: ExecutionLimits;
134
- budget: ExecutionBudget;
135
- /**
136
- * A WAIT longer than this suspends the execution instead of calling `wait`. Absent: no wait
137
- * suspends on its own, and the host bounds every wait (Reflex keeps waits in the execution).
138
- */
139
- suspendWaitAboveMs?: number;
140
- /**
141
- * Read-only values the host provides to the client code: each top-level key becomes a global
142
- * (`secrets`, a workflow context...), deep-frozen and rebuilt in every evaluation. A key cannot
143
- * be one of `RESERVED_GLOBAL_NAMES`: the runner refuses the request with `INVALID_REQUEST`.
144
- * Never logged by the runner; the host masks what it stores. Dynamic access
145
- * (`secrets[name]`) is not resolvable by the host: it yields `undefined`.
146
- *
147
- * `config` is the conventional key of the non-secret per-environment variables
148
- * (`Record<string, string>`): `{{ config.NAME }}` in a template or expression and `config.NAME` in
149
- * client code, read-only like `secrets`, `undefined` for a name the host did not send. The host
150
- * sends the names `collectGlobalReferences(definition, 'config')` finds. Unlike secrets, these
151
- * values are not masked in traces.
152
- */
153
- globals?: Record<string, JSONValue>;
154
- }
155
-
156
- /** Runner → host: the step `stepId` calls the host function `capability`. */
157
- export interface ExecutionCallMessage {
158
- type: ExecutionMessageType.CALL;
159
- callId: string;
160
- stepId: string;
161
- capability: string;
162
- args: unknown;
163
- }
164
-
165
- export interface CallError {
166
- /** A `RuntimeErrorCode`; absent, the runner reports the capability's default code. */
167
- code?: RuntimeErrorCode;
168
- message: string;
169
- }
170
-
171
- /**
172
- * Host → runner: the answer to one `call`. `suspend` asks the runner to stop after this step
173
- * and finish SUSPENDED: a resume then gives the step its output through `resumeInput`.
174
- */
175
- export type ExecutionCallResultMessage =
176
- | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; value: unknown }
177
- | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; suspend: true }
178
- | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: false; error: CallError };
179
-
180
- export interface StepMetrics {
181
- cpuMs: number;
182
- /**
183
- * What the step itself grew the heap by: the highest reading during the step minus the reading
184
- * at its start, never below 0. An estimate that can err both ways (a late sample misses a short
185
- * peak, a delayed GC keeps freed memory), and blind to the isolate's base heap and to what earlier
186
- * steps left.
187
- */
188
- heapPeakBytes: number;
189
- wallMs: number;
190
- }
191
-
192
- export interface ExecutionMetrics {
193
- queueMs: number;
194
- wallMs: number;
195
- cpuMs: number;
196
- heapPeakBytes: number;
197
- memoryLimitBytes: number;
198
- }
199
-
200
- export enum StepPhase {
201
- STARTED = 'started',
202
- FINISHED = 'finished',
203
- }
204
-
205
- export enum StepStatus {
206
- SUCCEEDED = 'SUCCEEDED',
207
- FAILED = 'FAILED',
208
- /** The step's call was answered with `suspend`, or its wait is above `suspendWaitAboveMs`. */
209
- SUSPENDED = 'SUSPENDED',
210
- }
211
-
212
- /** Runner → host: a step started or finished. The base of the trace. */
213
- export interface ProgressMessage {
214
- type: ExecutionMessageType.PROGRESS;
215
- stepId: string;
216
- phase: StepPhase;
217
- /** On `finished`. */
218
- status?: StepStatus;
219
- /** On `finished`. */
220
- durationMs?: number;
221
- /** On `finished`, measured by the runner. */
222
- metrics?: StepMetrics;
223
- /**
224
- * On `finished` with SUCCEEDED: the step's output whole when its JSON is at most
225
- * `MAX_PROGRESS_OUTPUT_BYTES` bytes of UTF-8; past that, the first `MAX_PROGRESS_OUTPUT_CHARS`
226
- * bytes of its JSON (a string, cut on a character boundary) with `outputTruncated` and
227
- * `outputSizeBytes`.
228
- */
229
- output?: unknown;
230
- outputTruncated?: boolean;
231
- /**
232
- * With `outputTruncated`: the real size of the output's JSON, in UTF-8 bytes. Absent when the
233
- * output could not be serialized (no preview either).
234
- */
235
- outputSizeBytes?: number;
236
- /** On `finished` with FAILED. */
237
- errorCode?: RuntimeErrorCode;
238
- /** On `finished` with FAILED by client code. */
239
- errorLocation?: IExecutionErrorLocation;
240
- /** On `finished` with FAILED by client code: at most `MAX_ERROR_STACK_CHARS` characters. */
241
- errorStack?: string;
242
- }
243
-
244
- export enum ExecutionStatus {
245
- SUCCEEDED = 'SUCCEEDED',
246
- FAILED = 'FAILED',
247
- SUSPENDED = 'SUSPENDED',
248
- }
249
-
250
- export interface ExecutionOutcome {
251
- status: ExecutionStatus;
252
- /** SUCCEEDED, or FAILED by a STOP step: the stop output. */
253
- output?: unknown;
254
- /** FAILED by an error; absent when a STOP step chose FAILED. */
255
- errorCode?: RuntimeErrorCode;
256
- errorMessage?: string;
257
- /** FAILED: the step that failed, when one did. */
258
- stepId?: string;
259
- /** FAILED by client code: where it failed. */
260
- errorLocation?: IExecutionErrorLocation;
261
- /** FAILED by client code: at most `MAX_ERROR_STACK_CHARS` characters. */
262
- errorStack?: string;
263
- /**
264
- * Lines not relayed to the host because of `budget.maxLogLines`, the single total: the LOG steps
265
- * the interpreter skipped past the cap plus the `log.write` calls the runner answered without
266
- * relaying (client code and engine). Counts this execution segment only. Absent when none.
267
- */
268
- droppedLogLines?: number;
269
- /** SUSPENDED: what to send back as `state` to resume. */
270
- state?: RunState;
271
- }
272
-
273
- /** Runner → host: the execution ended. Closes the connection. */
274
- export interface FinishedMessage {
275
- type: ExecutionMessageType.FINISHED;
276
- outcome: ExecutionOutcome;
277
- metrics: ExecutionMetrics;
278
- }
279
-
280
- export type ExecutionRejectionCode =
281
- | RuntimeErrorCode.RUNNER_SATURATED
282
- | RuntimeErrorCode.INVALID_REQUEST
283
- | RuntimeErrorCode.STATE_VERSION_UNSUPPORTED;
284
-
285
- /** Runner → host: the execution was not run at all. */
286
- export interface ExecutionRejectedMessage {
287
- type: ExecutionMessageType.REJECTED;
288
- code: ExecutionRejectionCode;
289
- message: string;
290
- }
291
-
292
- export type ExecutionHostMessage = ExecuteMessage | ExecutionCallResultMessage;
293
- export type ExecutionRunnerMessage =
294
- | ExecutionCallMessage
295
- | ProgressMessage
296
- | FinishedMessage
297
- | ExecutionRejectedMessage;
1
+ import { REFLEX_ATTACHMENT_MAX_BYTES_BOUND } from '../discord/discord.types';
2
+ import type { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
3
+ import type { RuntimeDefinition } from '../definition/definition.types';
4
+ import type { RunState } from '../interpreter/interpreter.types';
5
+
6
+ /**
7
+ * WebSocket sub-protocol of an execution (ADR-0243): one connection, one `execute`, one isolate
8
+ * running the whole definition. A new version is a new name, never a silent change.
9
+ */
10
+ export const EXECUTION_PROTOCOL = 'abyss-runtime.v2';
11
+
12
+ /** Upper bound, in JSON bytes, on the arguments of one `call` and on the value of its result. */
13
+ export const MAX_CALL_PAYLOAD_BYTES = 1_048_576;
14
+
15
+ /**
16
+ * `budget.maxFilePayloadBytes` for attachments of at most `maxAttachmentBytes`: their base64
17
+ * (4 bytes for 3) plus 64 KiB of envelope.
18
+ */
19
+ export const filePayloadBytesFor = (maxAttachmentBytes: number): number =>
20
+ Math.ceil((maxAttachmentBytes * 4) / 3) + 64 * 1024;
21
+
22
+ /**
23
+ * Highest `budget.maxFilePayloadBytes` a host may grant: `filePayloadBytesFor` the largest
24
+ * attachment a bot may configure, below the 16 MiB WebSocket frame of the runner.
25
+ */
26
+ export const MAX_FILE_CALL_PAYLOAD_BYTES = filePayloadBytesFor(REFLEX_ATTACHMENT_MAX_BYTES_BOUND);
27
+
28
+ /**
29
+ * Upper bound, in UTF-8 bytes of JSON, on the step output a `progress` message carries whole. Past
30
+ * it the message carries a preview of `MAX_PROGRESS_OUTPUT_CHARS` bytes and the real size.
31
+ */
32
+ export const MAX_PROGRESS_OUTPUT_BYTES = 65_536;
33
+
34
+ /**
35
+ * Size, in UTF-8 bytes of JSON, of the preview a `progress` message carries for a step output over
36
+ * `MAX_PROGRESS_OUTPUT_BYTES` (cut on a character boundary, so never more than this many bytes).
37
+ * The name predates the byte unit and is kept: renaming a published export needs a major version.
38
+ */
39
+ export const MAX_PROGRESS_OUTPUT_CHARS = 4_096;
40
+
41
+ /** Upper bound, in characters, on the `errorStack` of an outcome or a failed `progress`. */
42
+ export const MAX_ERROR_STACK_CHARS = 4_096;
43
+
44
+ /**
45
+ * Capabilities the core's own steps call. Every other name (Store, secrets, a product's
46
+ * actions) is declared by the host; the runner relays whatever the host lists.
47
+ */
48
+ export enum CoreCapability {
49
+ /** HTTP step, and `fetch()` in client code. Arguments: an `HttpRequest`. */
50
+ HTTP_FETCH = 'http.fetch',
51
+ /** WAIT step. Arguments: `{ durationMs }` or `{ until }`. */
52
+ WAIT = 'wait',
53
+ /** LOG step. Arguments: `{ level, message, data? }`. */
54
+ LOG_WRITE = 'log.write',
55
+ /** RESPOND step. Arguments: `{ status, headers, body? }`. */
56
+ HTTP_RESPOND = 'http.respond',
57
+ }
58
+
59
+ export enum ExecutionMessageType {
60
+ EXECUTE = 'execute',
61
+ CALL = 'call',
62
+ CALL_RESULT = 'callResult',
63
+ PROGRESS = 'progress',
64
+ FINISHED = 'finished',
65
+ REJECTED = 'rejected',
66
+ }
67
+
68
+ export type JSONValue =
69
+ | string
70
+ | number
71
+ | boolean
72
+ | null
73
+ | JSONValue[]
74
+ | { [key: string]: JSONValue };
75
+
76
+ /**
77
+ * Names `globals` cannot define: the evaluation context (`input`, `steps`, `vars`) and the
78
+ * client API (`fetch`, `abyss`, `console`).
79
+ */
80
+ export const RESERVED_GLOBAL_NAMES: readonly string[] = [
81
+ 'input',
82
+ 'steps',
83
+ 'vars',
84
+ 'fetch',
85
+ 'abyss',
86
+ 'console',
87
+ ];
88
+
89
+ export interface ExecutionLimits {
90
+ /** Heap of the execution's isolate: the memory of its resource profile. */
91
+ memoryMb: number;
92
+ /** CPU time of the whole execution (one segment when it suspends). */
93
+ cpuTimeMs: number;
94
+ /** Wall-clock time of the whole execution (one segment when it suspends). */
95
+ wallTimeMs: number;
96
+ }
97
+
98
+ export interface ExecutionBudget {
99
+ maxSteps: number;
100
+ maxEffects: number;
101
+ /**
102
+ * When given, `log.write` calls do not count in `maxEffects`: they are counted apart, and past
103
+ * this number the runner answers them without relaying them to the host.
104
+ */
105
+ maxLogLines?: number;
106
+ /**
107
+ * When given, a call to a `discord.*` capability whose arguments carry a non-empty `files`
108
+ * array may weigh up to this many JSON bytes (instead of `MAX_CALL_PAYLOAD_BYTES`), and so
109
+ * may the result of `discord.readAttachment`. Every other call keeps the default bound.
110
+ */
111
+ maxFilePayloadBytes?: number;
112
+ }
113
+
114
+ /** Where the client code of a step failed: line and column in the step's own code. */
115
+ export interface IExecutionErrorLocation {
116
+ stepId: string;
117
+ line: number;
118
+ column: number;
119
+ }
120
+
121
+ /** Host → runner: the execution to run. One per connection. */
122
+ export interface ExecuteMessage {
123
+ type: ExecutionMessageType.EXECUTE;
124
+ executionId: string;
125
+ definition: RuntimeDefinition;
126
+ input: unknown;
127
+ /** Present to resume a suspended execution: the `state` its `finished` message returned. */
128
+ state?: RunState;
129
+ /** On resume: becomes the output of the step that suspended. Absent for a timer wait. */
130
+ resumeInput?: unknown;
131
+ /** Names of the host functions the execution may call; nothing else is exposed. */
132
+ capabilities: string[];
133
+ limits: ExecutionLimits;
134
+ budget: ExecutionBudget;
135
+ /**
136
+ * A WAIT longer than this suspends the execution instead of calling `wait`. Absent: no wait
137
+ * suspends on its own, and the host bounds every wait (Reflex keeps waits in the execution).
138
+ */
139
+ suspendWaitAboveMs?: number;
140
+ /**
141
+ * Read-only values the host provides to the client code: each top-level key becomes a global
142
+ * (`secrets`, a workflow context...), deep-frozen and rebuilt in every evaluation. A key cannot
143
+ * be one of `RESERVED_GLOBAL_NAMES`: the runner refuses the request with `INVALID_REQUEST`.
144
+ * Never logged by the runner; the host masks what it stores. Dynamic access
145
+ * (`secrets[name]`) is not resolvable by the host: it yields `undefined`.
146
+ *
147
+ * `config` is the conventional key of the non-secret per-environment variables
148
+ * (`Record<string, string>`): `{{ config.NAME }}` in a template or expression and `config.NAME` in
149
+ * client code, read-only like `secrets`, `undefined` for a name the host did not send. The host
150
+ * sends the names `collectGlobalReferences(definition, 'config')` finds. Unlike secrets, these
151
+ * values are not masked in traces.
152
+ */
153
+ globals?: Record<string, JSONValue>;
154
+ }
155
+
156
+ /** Runner → host: the step `stepId` calls the host function `capability`. */
157
+ export interface ExecutionCallMessage {
158
+ type: ExecutionMessageType.CALL;
159
+ callId: string;
160
+ stepId: string;
161
+ capability: string;
162
+ args: unknown;
163
+ }
164
+
165
+ export interface CallError {
166
+ /** A `RuntimeErrorCode`; absent, the runner reports the capability's default code. */
167
+ code?: RuntimeErrorCode;
168
+ message: string;
169
+ }
170
+
171
+ /**
172
+ * Host → runner: the answer to one `call`. `suspend` asks the runner to stop after this step
173
+ * and finish SUSPENDED: a resume then gives the step its output through `resumeInput`.
174
+ */
175
+ export type ExecutionCallResultMessage =
176
+ | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; value: unknown }
177
+ | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; suspend: true }
178
+ | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: false; error: CallError };
179
+
180
+ export interface StepMetrics {
181
+ cpuMs: number;
182
+ /**
183
+ * What the step itself grew the heap by: the highest reading during the step minus the reading
184
+ * at its start, never below 0. An estimate that can err both ways (a late sample misses a short
185
+ * peak, a delayed GC keeps freed memory), and blind to the isolate's base heap and to what earlier
186
+ * steps left.
187
+ */
188
+ heapPeakBytes: number;
189
+ wallMs: number;
190
+ }
191
+
192
+ export interface ExecutionMetrics {
193
+ queueMs: number;
194
+ wallMs: number;
195
+ cpuMs: number;
196
+ heapPeakBytes: number;
197
+ memoryLimitBytes: number;
198
+ }
199
+
200
+ export enum StepPhase {
201
+ STARTED = 'started',
202
+ FINISHED = 'finished',
203
+ }
204
+
205
+ export enum StepStatus {
206
+ SUCCEEDED = 'SUCCEEDED',
207
+ FAILED = 'FAILED',
208
+ /** The step's call was answered with `suspend`, or its wait is above `suspendWaitAboveMs`. */
209
+ SUSPENDED = 'SUSPENDED',
210
+ }
211
+
212
+ /** Runner → host: a step started or finished. The base of the trace. */
213
+ export interface ProgressMessage {
214
+ type: ExecutionMessageType.PROGRESS;
215
+ stepId: string;
216
+ phase: StepPhase;
217
+ /** On `finished`. */
218
+ status?: StepStatus;
219
+ /** On `finished`. */
220
+ durationMs?: number;
221
+ /** On `finished`, measured by the runner. */
222
+ metrics?: StepMetrics;
223
+ /**
224
+ * On `finished` with SUCCEEDED: the step's output whole when its JSON is at most
225
+ * `MAX_PROGRESS_OUTPUT_BYTES` bytes of UTF-8; past that, the first `MAX_PROGRESS_OUTPUT_CHARS`
226
+ * bytes of its JSON (a string, cut on a character boundary) with `outputTruncated` and
227
+ * `outputSizeBytes`.
228
+ */
229
+ output?: unknown;
230
+ outputTruncated?: boolean;
231
+ /**
232
+ * With `outputTruncated`: the real size of the output's JSON, in UTF-8 bytes. Absent when the
233
+ * output could not be serialized (no preview either).
234
+ */
235
+ outputSizeBytes?: number;
236
+ /** On `finished` with FAILED. */
237
+ errorCode?: RuntimeErrorCode;
238
+ /** On `finished` with FAILED by client code. */
239
+ errorLocation?: IExecutionErrorLocation;
240
+ /** On `finished` with FAILED by client code: at most `MAX_ERROR_STACK_CHARS` characters. */
241
+ errorStack?: string;
242
+ }
243
+
244
+ export enum ExecutionStatus {
245
+ SUCCEEDED = 'SUCCEEDED',
246
+ FAILED = 'FAILED',
247
+ SUSPENDED = 'SUSPENDED',
248
+ }
249
+
250
+ export interface ExecutionOutcome {
251
+ status: ExecutionStatus;
252
+ /** SUCCEEDED, or FAILED by a STOP step: the stop output. */
253
+ output?: unknown;
254
+ /** FAILED by an error; absent when a STOP step chose FAILED. */
255
+ errorCode?: RuntimeErrorCode;
256
+ errorMessage?: string;
257
+ /** FAILED: the step that failed, when one did. */
258
+ stepId?: string;
259
+ /** FAILED by client code: where it failed. */
260
+ errorLocation?: IExecutionErrorLocation;
261
+ /** FAILED by client code: at most `MAX_ERROR_STACK_CHARS` characters. */
262
+ errorStack?: string;
263
+ /**
264
+ * Lines not relayed to the host because of `budget.maxLogLines`, the single total: the LOG steps
265
+ * the interpreter skipped past the cap plus the `log.write` calls the runner answered without
266
+ * relaying (client code and engine). Counts this execution segment only. Absent when none.
267
+ */
268
+ droppedLogLines?: number;
269
+ /** SUSPENDED: what to send back as `state` to resume. */
270
+ state?: RunState;
271
+ }
272
+
273
+ /** Runner → host: the execution ended. Closes the connection. */
274
+ export interface FinishedMessage {
275
+ type: ExecutionMessageType.FINISHED;
276
+ outcome: ExecutionOutcome;
277
+ metrics: ExecutionMetrics;
278
+ }
279
+
280
+ export type ExecutionRejectionCode =
281
+ | RuntimeErrorCode.RUNNER_SATURATED
282
+ | RuntimeErrorCode.INVALID_REQUEST
283
+ | RuntimeErrorCode.STATE_VERSION_UNSUPPORTED;
284
+
285
+ /** Runner → host: the execution was not run at all. */
286
+ export interface ExecutionRejectedMessage {
287
+ type: ExecutionMessageType.REJECTED;
288
+ code: ExecutionRejectionCode;
289
+ message: string;
290
+ }
291
+
292
+ export type ExecutionHostMessage = ExecuteMessage | ExecutionCallResultMessage;
293
+ export type ExecutionRunnerMessage =
294
+ | ExecutionCallMessage
295
+ | ProgressMessage
296
+ | FinishedMessage
297
+ | ExecutionRejectedMessage;