@abyss-project/runtime 3.7.0 → 3.8.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 -32
- package/.prettierrc +5 -5
- package/README.md +88 -88
- package/dist/engine/engine-source.js +1 -1
- package/dist/engine/run-in-isolate.js +32 -2
- package/dist/engine/run-in-isolate.js.map +1 -1
- package/dist/runner-protocol/execution-protocol.types.d.ts +2 -0
- package/dist/runner-protocol/execution-protocol.types.js +2 -1
- package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
- package/jest.config.ts +11 -11
- package/package.json +10 -15
- package/scripts/build-engine.cjs +70 -70
- package/src/__tests__/engine-bundle.test.ts +121 -121
- package/src/__tests__/surface.test.ts +12 -1
- package/src/access/__tests__/host-match.test.ts +24 -24
- package/src/access/access.types.ts +25 -25
- package/src/access/host-match.ts +13 -13
- package/src/cost/__tests__/execution-cost.test.ts +196 -196
- package/src/cost/execution-cost.ts +186 -186
- package/src/definition/__tests__/validate-definition.test.ts +354 -354
- package/src/definition/definition.limits.ts +9 -9
- package/src/definition/definition.schema.ts +172 -172
- package/src/definition/definition.types.ts +171 -171
- package/src/definition/validate-definition.ts +206 -206
- package/src/definition/walk-steps.ts +44 -44
- package/src/discord/__tests__/discord-event-input.test.ts +89 -89
- package/src/discord/discord.types.ts +639 -639
- package/src/engine/__tests__/run-in-isolate.test.ts +56 -5
- package/src/engine/engine-entry.ts +3 -3
- package/src/engine/engine-global.ts +2 -2
- package/src/engine/run-in-isolate.ts +47 -4
- package/src/errors/runtime-error-code.enum.ts +37 -37
- package/src/index.ts +32 -32
- package/src/interpreter/__tests__/interpreter.test.ts +854 -854
- package/src/interpreter/__tests__/log-respond-resume.test.ts +271 -271
- package/src/interpreter/interpreter-error.ts +12 -12
- package/src/interpreter/interpreter.ts +483 -483
- package/src/interpreter/interpreter.types.ts +204 -204
- package/src/interpreter/sequence-path.ts +67 -67
- package/src/interpreter/test-host.ts +85 -85
- package/src/runner-protocol/evaluation.types.ts +21 -21
- package/src/runner-protocol/execution-protocol.types.ts +20 -3
- package/src/schedule/schedule.types.ts +100 -100
- package/src/server/__tests__/code-runner-session.test.ts +311 -311
- package/src/server/code-runner-error.ts +12 -12
- package/src/server/code-runner-session.ts +310 -310
- package/src/server/index.ts +11 -11
- package/src/steps/__tests__/global-references.test.ts +133 -133
- package/src/steps/execute-step.ts +186 -186
- package/src/steps/global-references.ts +48 -48
- package/src/steps/step-access.ts +56 -56
- package/src/steps/step-fields.ts +87 -87
- package/src/steps/value-source.ts +69 -69
- package/tsconfig.check.json +15 -15
- package/tsconfig.json +35 -35
|
@@ -14,6 +14,7 @@ import type { EvaluationItem, EvaluationResult } from '../../runner-protocol/eva
|
|
|
14
14
|
import {
|
|
15
15
|
CoreCapability,
|
|
16
16
|
ExecutionStatus,
|
|
17
|
+
MAX_PROGRESS_OUTPUT_BYTES,
|
|
17
18
|
MAX_PROGRESS_OUTPUT_CHARS,
|
|
18
19
|
StepPhase,
|
|
19
20
|
StepStatus,
|
|
@@ -178,9 +179,9 @@ describe('runInIsolate', () => {
|
|
|
178
179
|
expect(finishedName?.durationMs).toBeGreaterThan(0);
|
|
179
180
|
});
|
|
180
181
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const host = fakeHost(undefined, { 'input.name':
|
|
182
|
+
/** Runs the first step with `value` as its output and returns the progress of its end. */
|
|
183
|
+
const finishedProgressOf = async (value: unknown) => {
|
|
184
|
+
const host = fakeHost(undefined, { 'input.name': value });
|
|
184
185
|
const outcome = outcomeOf(
|
|
185
186
|
await runInIsolate(
|
|
186
187
|
request({ definition: { ...definition, steps: [definition.steps[0]] } }),
|
|
@@ -188,9 +189,59 @@ describe('runInIsolate', () => {
|
|
|
188
189
|
),
|
|
189
190
|
);
|
|
190
191
|
expect(outcome.status).toBe(ExecutionStatus.SUCCEEDED);
|
|
191
|
-
|
|
192
|
+
return host.progress.find((p) => p.phase === StepPhase.FINISHED);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// JSON of a string adds its two quotes: `jsonBytes` bytes of JSON hold `jsonBytes - 2` ASCII characters.
|
|
196
|
+
const asciiOfJsonBytes = (jsonBytes: number): string => 'x'.repeat(jsonBytes - 2);
|
|
197
|
+
|
|
198
|
+
it('keeps an output of exactly 64 KB of JSON whole', async () => {
|
|
199
|
+
const output = asciiOfJsonBytes(MAX_PROGRESS_OUTPUT_BYTES);
|
|
200
|
+
expect(Buffer.byteLength(JSON.stringify(output))).toBe(65_536);
|
|
201
|
+
const finished = await finishedProgressOf(output);
|
|
202
|
+
expect(finished?.output).toBe(output);
|
|
203
|
+
expect(finished?.outputTruncated).toBeUndefined();
|
|
204
|
+
expect(finished?.outputSizeBytes).toBeUndefined();
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('cuts an output of 64 KB + 1 byte to a 4 KB preview with its real size, never in the run', async () => {
|
|
208
|
+
const output = asciiOfJsonBytes(MAX_PROGRESS_OUTPUT_BYTES + 1);
|
|
209
|
+
const finished = await finishedProgressOf(output);
|
|
192
210
|
expect(finished?.outputTruncated).toBe(true);
|
|
193
|
-
expect(
|
|
211
|
+
expect(finished?.outputSizeBytes).toBe(65_537);
|
|
212
|
+
expect(finished?.output).toBe(JSON.stringify(output).slice(0, MAX_PROGRESS_OUTPUT_CHARS));
|
|
213
|
+
expect(Buffer.byteLength(finished?.output as string)).toBe(4_096);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('keeps a 4 KB to 64 KB output whole, where it used to be cut at 4096 characters', async () => {
|
|
217
|
+
const output = { rows: 'y'.repeat(30_000) };
|
|
218
|
+
const finished = await finishedProgressOf(output);
|
|
219
|
+
expect(finished?.output).toEqual(output);
|
|
220
|
+
expect(finished?.outputTruncated).toBeUndefined();
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('measures the preview and the real size in bytes, never cutting a multibyte character', async () => {
|
|
224
|
+
// Each "€" is 3 bytes: the 4 096-byte boundary falls inside one.
|
|
225
|
+
const output = `a${'€'.repeat(30_000)}`;
|
|
226
|
+
const json = JSON.stringify(output);
|
|
227
|
+
const finished = await finishedProgressOf(output);
|
|
228
|
+
expect(finished?.outputSizeBytes).toBe(Buffer.byteLength(json));
|
|
229
|
+
const preview = finished?.output as string;
|
|
230
|
+
expect(json.startsWith(preview)).toBe(true);
|
|
231
|
+
expect(preview).not.toContain('�');
|
|
232
|
+
// Quote + "a" + 1 364 whole "€" = 4 094 bytes; a 1 365th would end at 4 097.
|
|
233
|
+
expect(Buffer.byteLength(preview)).toBe(4_094);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('counts a character outside the BMP as 4 bytes and keeps its two units together', async () => {
|
|
237
|
+
const output = '😀'.repeat(20_000);
|
|
238
|
+
const json = JSON.stringify(output);
|
|
239
|
+
const finished = await finishedProgressOf(output);
|
|
240
|
+
expect(finished?.outputSizeBytes).toBe(Buffer.byteLength(json));
|
|
241
|
+
const preview = finished?.output as string;
|
|
242
|
+
expect(json.startsWith(preview)).toBe(true);
|
|
243
|
+
// Quote + 1 023 whole emoji = 4 093 bytes; a 1 024th would end at 4 097.
|
|
244
|
+
expect(Buffer.byteLength(preview)).toBe(4_093);
|
|
194
245
|
});
|
|
195
246
|
|
|
196
247
|
it('refuses a capability the host did not expose, without calling it', async () => {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
// Entry of the isolate bundle built by `scripts/build-engine.cjs` (IIFE): evaluating the script
|
|
2
|
-
// defines the global `ENGINE_GLOBAL_NAME` holding exactly these exports.
|
|
3
|
-
export { runInIsolate } from './run-in-isolate';
|
|
1
|
+
// Entry of the isolate bundle built by `scripts/build-engine.cjs` (IIFE): evaluating the script
|
|
2
|
+
// defines the global `ENGINE_GLOBAL_NAME` holding exactly these exports.
|
|
3
|
+
export { runInIsolate } from './run-in-isolate';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/** Global the engine script defines when evaluated: `{ runInIsolate }`. */
|
|
2
|
-
export const ENGINE_GLOBAL_NAME = 'AbyssRuntimeEngine';
|
|
1
|
+
/** Global the engine script defines when evaluated: `{ runInIsolate }`. */
|
|
2
|
+
export const ENGINE_GLOBAL_NAME = 'AbyssRuntimeEngine';
|
|
@@ -36,6 +36,7 @@ import type { EvaluationItem, EvaluationResult } from '../runner-protocol/evalua
|
|
|
36
36
|
import {
|
|
37
37
|
CoreCapability,
|
|
38
38
|
ExecutionStatus,
|
|
39
|
+
MAX_PROGRESS_OUTPUT_BYTES,
|
|
39
40
|
MAX_PROGRESS_OUTPUT_CHARS,
|
|
40
41
|
StepPhase,
|
|
41
42
|
StepStatus,
|
|
@@ -137,8 +138,49 @@ const isolatePolicy = (request: EngineRequest): RunPolicy => {
|
|
|
137
138
|
};
|
|
138
139
|
};
|
|
139
140
|
|
|
140
|
-
/**
|
|
141
|
-
const
|
|
141
|
+
/** UTF-8 width of the code point starting at `index`, and how many UTF-16 units it spans. */
|
|
142
|
+
const codePointAt = (text: string, index: number): { bytes: number; units: number } => {
|
|
143
|
+
const code = text.charCodeAt(index);
|
|
144
|
+
if (code < 0x80) {
|
|
145
|
+
return { bytes: 1, units: 1 };
|
|
146
|
+
}
|
|
147
|
+
if (code < 0x800) {
|
|
148
|
+
return { bytes: 2, units: 1 };
|
|
149
|
+
}
|
|
150
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
151
|
+
const next = text.charCodeAt(index + 1);
|
|
152
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
153
|
+
return { bytes: 4, units: 2 };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return { bytes: 3, units: 1 };
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Size of `text` in UTF-8 bytes (there is no `TextEncoder` in the isolate), and its longest prefix
|
|
161
|
+
* of at most `limit` bytes that ends on a character boundary.
|
|
162
|
+
*/
|
|
163
|
+
const utf8Measure = (text: string, limit: number): { bytes: number; prefix: string } => {
|
|
164
|
+
let bytes = 0;
|
|
165
|
+
let prefixEnd = -1;
|
|
166
|
+
for (let index = 0; index < text.length; ) {
|
|
167
|
+
const point = codePointAt(text, index);
|
|
168
|
+
if (prefixEnd < 0 && bytes + point.bytes > limit) {
|
|
169
|
+
prefixEnd = index;
|
|
170
|
+
}
|
|
171
|
+
bytes += point.bytes;
|
|
172
|
+
index += point.units;
|
|
173
|
+
}
|
|
174
|
+
return { bytes, prefix: text.slice(0, prefixEnd < 0 ? text.length : prefixEnd) };
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The progress view of an output: the value itself up to `MAX_PROGRESS_OUTPUT_BYTES` bytes of JSON;
|
|
179
|
+
* beyond, the first `MAX_PROGRESS_OUTPUT_CHARS` bytes of its JSON, its real size and the flag.
|
|
180
|
+
*/
|
|
181
|
+
const outputPreview = (
|
|
182
|
+
output: unknown,
|
|
183
|
+
): Pick<EngineProgress, 'output' | 'outputTruncated' | 'outputSizeBytes'> => {
|
|
142
184
|
let json: string | undefined;
|
|
143
185
|
try {
|
|
144
186
|
json = JSON.stringify(output);
|
|
@@ -148,10 +190,11 @@ const outputPreview = (output: unknown): Pick<EngineProgress, 'output' | 'output
|
|
|
148
190
|
if (json === undefined) {
|
|
149
191
|
return {};
|
|
150
192
|
}
|
|
151
|
-
|
|
193
|
+
const { bytes, prefix } = utf8Measure(json, MAX_PROGRESS_OUTPUT_CHARS);
|
|
194
|
+
if (bytes <= MAX_PROGRESS_OUTPUT_BYTES) {
|
|
152
195
|
return { output };
|
|
153
196
|
}
|
|
154
|
-
return { output:
|
|
197
|
+
return { output: prefix, outputTruncated: true, outputSizeBytes: bytes };
|
|
155
198
|
};
|
|
156
199
|
|
|
157
200
|
/** Turns interpreter step notifications into `progress`, timing each step with `host.now`. */
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Every failure a run, an evaluation or the code runner can report. The same codes travel in
|
|
3
|
-
* the interpreter's events, in the runner protocol and in the errors thrown by the Node client,
|
|
4
|
-
* so a caller maps one list to its own failure codes.
|
|
5
|
-
*/
|
|
6
|
-
export enum RuntimeErrorCode {
|
|
7
|
-
/** An expression or a template threw or did not parse. */
|
|
8
|
-
EXPRESSION_ERROR = 'EXPRESSION_ERROR',
|
|
9
|
-
/** A `code` item threw or did not parse. */
|
|
10
|
-
CODE_ERROR = 'CODE_ERROR',
|
|
11
|
-
/** The execution exceeded its wall-clock or CPU time limit. */
|
|
12
|
-
TIMEOUT = 'TIMEOUT',
|
|
13
|
-
/** The execution exceeded the memory limit of its isolate. */
|
|
14
|
-
MEMORY_LIMIT = 'MEMORY_LIMIT',
|
|
15
|
-
/** An effect is not covered by both the definition's declarations and the run's grants. */
|
|
16
|
-
ACCESS_DENIED = 'ACCESS_DENIED',
|
|
17
|
-
/** The run exceeded its step or effect budget. */
|
|
18
|
-
BUDGET_EXCEEDED = 'BUDGET_EXCEEDED',
|
|
19
|
-
/** A host action failed, or its resolved input does not match the action's schema. */
|
|
20
|
-
HOST_ERROR = 'HOST_ERROR',
|
|
21
|
-
/** The host could not perform an HTTP effect (network, SSRF guard, timeout). */
|
|
22
|
-
HTTP_ERROR = 'HTTP_ERROR',
|
|
23
|
-
/** The code runner could not be reached, or the connection dropped mid-execution. */
|
|
24
|
-
RUNNER_UNAVAILABLE = 'RUNNER_UNAVAILABLE',
|
|
25
|
-
/** The code runner refused the execution: its queue is full or the wait timed out. */
|
|
26
|
-
RUNNER_SATURATED = 'RUNNER_SATURATED',
|
|
27
|
-
/** A message or a definition does not match the contract. */
|
|
28
|
-
INVALID_REQUEST = 'INVALID_REQUEST',
|
|
29
|
-
/** A run state to resume carries a `stateVersion` this core cannot read. */
|
|
30
|
-
STATE_VERSION_UNSUPPORTED = 'STATE_VERSION_UNSUPPORTED',
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface RuntimeErrorDetail {
|
|
34
|
-
code: RuntimeErrorCode;
|
|
35
|
-
/** Human-readable cause. Never contains the client's code, context or values. */
|
|
36
|
-
message: string;
|
|
37
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Every failure a run, an evaluation or the code runner can report. The same codes travel in
|
|
3
|
+
* the interpreter's events, in the runner protocol and in the errors thrown by the Node client,
|
|
4
|
+
* so a caller maps one list to its own failure codes.
|
|
5
|
+
*/
|
|
6
|
+
export enum RuntimeErrorCode {
|
|
7
|
+
/** An expression or a template threw or did not parse. */
|
|
8
|
+
EXPRESSION_ERROR = 'EXPRESSION_ERROR',
|
|
9
|
+
/** A `code` item threw or did not parse. */
|
|
10
|
+
CODE_ERROR = 'CODE_ERROR',
|
|
11
|
+
/** The execution exceeded its wall-clock or CPU time limit. */
|
|
12
|
+
TIMEOUT = 'TIMEOUT',
|
|
13
|
+
/** The execution exceeded the memory limit of its isolate. */
|
|
14
|
+
MEMORY_LIMIT = 'MEMORY_LIMIT',
|
|
15
|
+
/** An effect is not covered by both the definition's declarations and the run's grants. */
|
|
16
|
+
ACCESS_DENIED = 'ACCESS_DENIED',
|
|
17
|
+
/** The run exceeded its step or effect budget. */
|
|
18
|
+
BUDGET_EXCEEDED = 'BUDGET_EXCEEDED',
|
|
19
|
+
/** A host action failed, or its resolved input does not match the action's schema. */
|
|
20
|
+
HOST_ERROR = 'HOST_ERROR',
|
|
21
|
+
/** The host could not perform an HTTP effect (network, SSRF guard, timeout). */
|
|
22
|
+
HTTP_ERROR = 'HTTP_ERROR',
|
|
23
|
+
/** The code runner could not be reached, or the connection dropped mid-execution. */
|
|
24
|
+
RUNNER_UNAVAILABLE = 'RUNNER_UNAVAILABLE',
|
|
25
|
+
/** The code runner refused the execution: its queue is full or the wait timed out. */
|
|
26
|
+
RUNNER_SATURATED = 'RUNNER_SATURATED',
|
|
27
|
+
/** A message or a definition does not match the contract. */
|
|
28
|
+
INVALID_REQUEST = 'INVALID_REQUEST',
|
|
29
|
+
/** A run state to resume carries a `stateVersion` this core cannot read. */
|
|
30
|
+
STATE_VERSION_UNSUPPORTED = 'STATE_VERSION_UNSUPPORTED',
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface RuntimeErrorDetail {
|
|
34
|
+
code: RuntimeErrorCode;
|
|
35
|
+
/** Human-readable cause. Never contains the client's code, context or values. */
|
|
36
|
+
message: string;
|
|
37
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
// Package root: browser-compatible. No Node import (`fs`, `net`, `http`, `ws`...) here nor in what
|
|
2
|
-
// this root re-exports; everything touching the network or the system lives under `src/server`
|
|
3
|
-
// (entry point `@abyss-project/runtime/server`).
|
|
4
|
-
export * from './errors/runtime-error-code.enum';
|
|
5
|
-
export * from './access/access.types';
|
|
6
|
-
export { hostMatches } from './access/host-match';
|
|
7
|
-
export * from './definition/definition.types';
|
|
8
|
-
export { validateDefinition } from './definition/validate-definition';
|
|
9
|
-
export type { ValidationResult } from './definition/validate-definition';
|
|
10
|
-
export { MAX_WAIT_DURATION_MS } from './definition/definition.limits';
|
|
11
|
-
export { walkSteps } from './definition/walk-steps';
|
|
12
|
-
export type { StepVisitor } from './definition/walk-steps';
|
|
13
|
-
export * from './interpreter/interpreter.types';
|
|
14
|
-
export { start, step } from './interpreter/interpreter';
|
|
15
|
-
export { InterpreterError } from './interpreter/interpreter-error';
|
|
16
|
-
export { runToCompletion } from './interpreter/test-host';
|
|
17
|
-
export type { TestHostHandlers, TestRunResult } from './interpreter/test-host';
|
|
18
|
-
export * from './discord/discord.types';
|
|
19
|
-
export * from './schedule/schedule.types';
|
|
20
|
-
export * from './runner-protocol/evaluation.types';
|
|
21
|
-
export * from './runner-protocol/execution-protocol.types';
|
|
22
|
-
export { runInIsolate, EngineResultKind } from './engine/run-in-isolate';
|
|
23
|
-
export type {
|
|
24
|
-
EngineHost,
|
|
25
|
-
EngineProgress,
|
|
26
|
-
EngineRequest,
|
|
27
|
-
EngineResult,
|
|
28
|
-
HostCallAnswer,
|
|
29
|
-
} from './engine/run-in-isolate';
|
|
30
|
-
export { ENGINE_GLOBAL_NAME } from './engine/engine-global';
|
|
31
|
-
export { collectGlobalReferences } from './steps/global-references';
|
|
32
|
-
export * from './cost/execution-cost';
|
|
1
|
+
// Package root: browser-compatible. No Node import (`fs`, `net`, `http`, `ws`...) here nor in what
|
|
2
|
+
// this root re-exports; everything touching the network or the system lives under `src/server`
|
|
3
|
+
// (entry point `@abyss-project/runtime/server`).
|
|
4
|
+
export * from './errors/runtime-error-code.enum';
|
|
5
|
+
export * from './access/access.types';
|
|
6
|
+
export { hostMatches } from './access/host-match';
|
|
7
|
+
export * from './definition/definition.types';
|
|
8
|
+
export { validateDefinition } from './definition/validate-definition';
|
|
9
|
+
export type { ValidationResult } from './definition/validate-definition';
|
|
10
|
+
export { MAX_WAIT_DURATION_MS } from './definition/definition.limits';
|
|
11
|
+
export { walkSteps } from './definition/walk-steps';
|
|
12
|
+
export type { StepVisitor } from './definition/walk-steps';
|
|
13
|
+
export * from './interpreter/interpreter.types';
|
|
14
|
+
export { start, step } from './interpreter/interpreter';
|
|
15
|
+
export { InterpreterError } from './interpreter/interpreter-error';
|
|
16
|
+
export { runToCompletion } from './interpreter/test-host';
|
|
17
|
+
export type { TestHostHandlers, TestRunResult } from './interpreter/test-host';
|
|
18
|
+
export * from './discord/discord.types';
|
|
19
|
+
export * from './schedule/schedule.types';
|
|
20
|
+
export * from './runner-protocol/evaluation.types';
|
|
21
|
+
export * from './runner-protocol/execution-protocol.types';
|
|
22
|
+
export { runInIsolate, EngineResultKind } from './engine/run-in-isolate';
|
|
23
|
+
export type {
|
|
24
|
+
EngineHost,
|
|
25
|
+
EngineProgress,
|
|
26
|
+
EngineRequest,
|
|
27
|
+
EngineResult,
|
|
28
|
+
HostCallAnswer,
|
|
29
|
+
} from './engine/run-in-isolate';
|
|
30
|
+
export { ENGINE_GLOBAL_NAME } from './engine/engine-global';
|
|
31
|
+
export { collectGlobalReferences } from './steps/global-references';
|
|
32
|
+
export * from './cost/execution-cost';
|