@abyss-project/runtime 3.0.0 → 3.1.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 (41) hide show
  1. package/README.md +27 -0
  2. package/dist/discord/discord.types.d.ts +123 -0
  3. package/dist/discord/discord.types.js +53 -0
  4. package/dist/discord/discord.types.js.map +1 -0
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
  9. package/package.json +1 -1
  10. package/scripts/build-engine.cjs +70 -70
  11. package/src/__tests__/engine-bundle.test.ts +121 -121
  12. package/src/cost/__tests__/execution-cost.test.ts +196 -196
  13. package/src/cost/execution-cost.ts +186 -186
  14. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  15. package/src/definition/definition.limits.ts +9 -9
  16. package/src/definition/definition.schema.ts +172 -172
  17. package/src/definition/definition.types.ts +171 -171
  18. package/src/discord/discord.types.ts +155 -0
  19. package/src/engine/__tests__/run-in-isolate.test.ts +468 -468
  20. package/src/engine/engine-entry.ts +3 -3
  21. package/src/engine/engine-global.ts +2 -2
  22. package/src/engine/run-in-isolate.ts +404 -404
  23. package/src/errors/runtime-error-code.enum.ts +37 -37
  24. package/src/index.ts +31 -30
  25. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  26. package/src/interpreter/__tests__/log-respond-resume.test.ts +223 -223
  27. package/src/interpreter/interpreter.ts +461 -461
  28. package/src/interpreter/interpreter.types.ts +192 -192
  29. package/src/interpreter/test-host.ts +85 -85
  30. package/src/runner-protocol/evaluation.types.ts +21 -21
  31. package/src/runner-protocol/execution-protocol.types.ts +231 -225
  32. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  33. package/src/server/code-runner-error.ts +12 -12
  34. package/src/server/code-runner-session.ts +310 -310
  35. package/src/server/index.ts +11 -11
  36. package/src/steps/__tests__/global-references.test.ts +133 -113
  37. package/src/steps/execute-step.ts +186 -186
  38. package/src/steps/global-references.ts +48 -48
  39. package/src/steps/step-access.ts +56 -56
  40. package/src/steps/step-fields.ts +87 -87
  41. package/src/steps/value-source.ts +69 -69
@@ -1,225 +1,231 @@
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-0239): 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
- globals?: Record<string, JSONValue>;
103
- }
104
-
105
- /** Runner → host: the step `stepId` calls the host function `capability`. */
106
- export interface ExecutionCallMessage {
107
- type: ExecutionMessageType.CALL;
108
- callId: string;
109
- stepId: string;
110
- capability: string;
111
- args: unknown;
112
- }
113
-
114
- export interface CallError {
115
- /** A `RuntimeErrorCode`; absent, the runner reports the capability's default code. */
116
- code?: RuntimeErrorCode;
117
- message: string;
118
- }
119
-
120
- /**
121
- * Host → runner: the answer to one `call`. `suspend` asks the runner to stop after this step
122
- * and finish SUSPENDED: a resume then gives the step its output through `resumeInput`.
123
- */
124
- export type ExecutionCallResultMessage =
125
- | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; value: unknown }
126
- | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: true; suspend: true }
127
- | { type: ExecutionMessageType.CALL_RESULT; callId: string; ok: false; error: CallError };
128
-
129
- export interface StepMetrics {
130
- cpuMs: number;
131
- /**
132
- * What the step itself grew the heap by: the highest reading during the step minus the reading
133
- * at its start, never below 0. An estimate that can err both ways (a late sample misses a short
134
- * peak, a delayed GC keeps freed memory), and blind to the isolate's base heap and to what earlier
135
- * steps left.
136
- */
137
- heapPeakBytes: number;
138
- wallMs: number;
139
- }
140
-
141
- export interface ExecutionMetrics {
142
- queueMs: number;
143
- wallMs: number;
144
- cpuMs: number;
145
- heapPeakBytes: number;
146
- memoryLimitBytes: number;
147
- }
148
-
149
- export enum StepPhase {
150
- STARTED = 'started',
151
- FINISHED = 'finished',
152
- }
153
-
154
- export enum StepStatus {
155
- SUCCEEDED = 'SUCCEEDED',
156
- FAILED = 'FAILED',
157
- /** The step's call was answered with `suspend`, or its wait is above `suspendWaitAboveMs`. */
158
- SUSPENDED = 'SUSPENDED',
159
- }
160
-
161
- /** Runner → host: a step started or finished. The base of the trace. */
162
- export interface ProgressMessage {
163
- type: ExecutionMessageType.PROGRESS;
164
- stepId: string;
165
- phase: StepPhase;
166
- /** On `finished`. */
167
- status?: StepStatus;
168
- /** On `finished`. */
169
- durationMs?: number;
170
- /** On `finished`, measured by the runner. */
171
- metrics?: StepMetrics;
172
- /**
173
- * On `finished` with SUCCEEDED: the step's output, or, past `MAX_PROGRESS_OUTPUT_CHARS`, the
174
- * beginning of its JSON with `outputTruncated`.
175
- */
176
- output?: unknown;
177
- outputTruncated?: boolean;
178
- /** On `finished` with FAILED. */
179
- errorCode?: RuntimeErrorCode;
180
- }
181
-
182
- export enum ExecutionStatus {
183
- SUCCEEDED = 'SUCCEEDED',
184
- FAILED = 'FAILED',
185
- SUSPENDED = 'SUSPENDED',
186
- }
187
-
188
- export interface ExecutionOutcome {
189
- status: ExecutionStatus;
190
- /** SUCCEEDED, or FAILED by a STOP step: the stop output. */
191
- output?: unknown;
192
- /** FAILED by an error; absent when a STOP step chose FAILED. */
193
- errorCode?: RuntimeErrorCode;
194
- errorMessage?: string;
195
- /** FAILED: the step that failed, when one did. */
196
- stepId?: string;
197
- /** SUSPENDED: what to send back as `state` to resume. */
198
- state?: RunState;
199
- }
200
-
201
- /** Runner → host: the execution ended. Closes the connection. */
202
- export interface FinishedMessage {
203
- type: ExecutionMessageType.FINISHED;
204
- outcome: ExecutionOutcome;
205
- metrics: ExecutionMetrics;
206
- }
207
-
208
- export type ExecutionRejectionCode =
209
- | RuntimeErrorCode.RUNNER_SATURATED
210
- | RuntimeErrorCode.INVALID_REQUEST
211
- | RuntimeErrorCode.STATE_VERSION_UNSUPPORTED;
212
-
213
- /** Runner → host: the execution was not run at all. */
214
- export interface ExecutionRejectedMessage {
215
- type: ExecutionMessageType.REJECTED;
216
- code: ExecutionRejectionCode;
217
- message: string;
218
- }
219
-
220
- export type ExecutionHostMessage = ExecuteMessage | ExecutionCallResultMessage;
221
- export type ExecutionRunnerMessage =
222
- | ExecutionCallMessage
223
- | ProgressMessage
224
- | FinishedMessage
225
- | 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
+ /**
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;