@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.
Files changed (55) hide show
  1. package/.eslintrc.js +32 -32
  2. package/.prettierrc +5 -5
  3. package/README.md +88 -88
  4. package/dist/engine/engine-source.js +1 -1
  5. package/dist/engine/run-in-isolate.js +32 -2
  6. package/dist/engine/run-in-isolate.js.map +1 -1
  7. package/dist/runner-protocol/execution-protocol.types.d.ts +2 -0
  8. package/dist/runner-protocol/execution-protocol.types.js +2 -1
  9. package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
  10. package/jest.config.ts +11 -11
  11. package/package.json +10 -15
  12. package/scripts/build-engine.cjs +70 -70
  13. package/src/__tests__/engine-bundle.test.ts +121 -121
  14. package/src/__tests__/surface.test.ts +12 -1
  15. package/src/access/__tests__/host-match.test.ts +24 -24
  16. package/src/access/access.types.ts +25 -25
  17. package/src/access/host-match.ts +13 -13
  18. package/src/cost/__tests__/execution-cost.test.ts +196 -196
  19. package/src/cost/execution-cost.ts +186 -186
  20. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  21. package/src/definition/definition.limits.ts +9 -9
  22. package/src/definition/definition.schema.ts +172 -172
  23. package/src/definition/definition.types.ts +171 -171
  24. package/src/definition/validate-definition.ts +206 -206
  25. package/src/definition/walk-steps.ts +44 -44
  26. package/src/discord/__tests__/discord-event-input.test.ts +89 -89
  27. package/src/discord/discord.types.ts +639 -639
  28. package/src/engine/__tests__/run-in-isolate.test.ts +56 -5
  29. package/src/engine/engine-entry.ts +3 -3
  30. package/src/engine/engine-global.ts +2 -2
  31. package/src/engine/run-in-isolate.ts +47 -4
  32. package/src/errors/runtime-error-code.enum.ts +37 -37
  33. package/src/index.ts +32 -32
  34. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  35. package/src/interpreter/__tests__/log-respond-resume.test.ts +271 -271
  36. package/src/interpreter/interpreter-error.ts +12 -12
  37. package/src/interpreter/interpreter.ts +483 -483
  38. package/src/interpreter/interpreter.types.ts +204 -204
  39. package/src/interpreter/sequence-path.ts +67 -67
  40. package/src/interpreter/test-host.ts +85 -85
  41. package/src/runner-protocol/evaluation.types.ts +21 -21
  42. package/src/runner-protocol/execution-protocol.types.ts +20 -3
  43. package/src/schedule/schedule.types.ts +100 -100
  44. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  45. package/src/server/code-runner-error.ts +12 -12
  46. package/src/server/code-runner-session.ts +310 -310
  47. package/src/server/index.ts +11 -11
  48. package/src/steps/__tests__/global-references.test.ts +133 -133
  49. package/src/steps/execute-step.ts +186 -186
  50. package/src/steps/global-references.ts +48 -48
  51. package/src/steps/step-access.ts +56 -56
  52. package/src/steps/step-fields.ts +87 -87
  53. package/src/steps/value-source.ts +69 -69
  54. package/tsconfig.check.json +15 -15
  55. package/tsconfig.json +35 -35
@@ -1,196 +1,196 @@
1
- import {
2
- COST_RATES,
3
- CostedExecutionStatus,
4
- RESOURCE_PROFILES,
5
- ResourceProfile,
6
- computeExecutionCost,
7
- computeStepCost,
8
- } from '../execution-cost';
9
- import type { ExecutionCostInput, StepUsage } from '../execution-cost';
10
-
11
- const NO_USAGE: StepUsage = { cpuMs: 0, wallMs: 0, netOutBytes: 0, httpCalls: 0, storeWrites: 0 };
12
-
13
- const execution = (overrides: Partial<ExecutionCostInput> = {}): ExecutionCostInput => ({
14
- ...NO_USAGE,
15
- status: CostedExecutionStatus.RAN,
16
- profile: ResourceProfile.S,
17
- ...overrides,
18
- });
19
-
20
- const usage = (overrides: Partial<StepUsage>): StepUsage => ({ ...NO_USAGE, ...overrides });
21
-
22
- const sum = (values: number[]): number => values.reduce((total, value) => total + value, 0);
23
-
24
- describe('resource profiles and rates', () => {
25
- it('follow ADR-0244', () => {
26
- expect(RESOURCE_PROFILES).toEqual({
27
- S: { memoryMb: 128, cpuCoefficient: 1 },
28
- M: { memoryMb: 256, cpuCoefficient: 1.05 },
29
- L: { memoryMb: 512, cpuCoefficient: 1.1 },
30
- XL: { memoryMb: 1024, cpuCoefficient: 1.2 },
31
- XXL: { memoryMb: 2048, cpuCoefficient: 1.35 },
32
- });
33
- expect(COST_RATES).toEqual({
34
- executionMillicredits: 10,
35
- cpuMillicreditsPerSecond: 500,
36
- memoryMillicreditsPerGbSecond: 100,
37
- networkOutMillicreditsPerMb: 1000,
38
- httpCallMillicredits: 10,
39
- storeWriteMillicredits: 10,
40
- });
41
- });
42
- });
43
-
44
- describe('computeExecutionCost', () => {
45
- it('prices a 3 s wait in profile S at about 0.05 credit', () => {
46
- // 10 + 0.125 GB × 3 s × 100 = 47.5, rounded half-up.
47
- const cost = computeExecutionCost(execution({ wallMs: 3_000 }));
48
- expect(cost).toBe(48);
49
- expect(cost / 1000).toBeCloseTo(0.05, 2);
50
- });
51
-
52
- it('prices 2 s of CPU in profile L at about 1.1 credits for the CPU part', () => {
53
- const computing = computeExecutionCost(
54
- execution({ profile: ResourceProfile.L, cpuMs: 2_000, wallMs: 2_000 }),
55
- );
56
- const waiting = computeExecutionCost(execution({ profile: ResourceProfile.L, wallMs: 2_000 }));
57
- expect(computing - waiting).toBe(1_100);
58
- // 10 + 1100 CPU + 0.5 GB × 2 s × 100 memory.
59
- expect(computing).toBe(1_210);
60
- });
61
-
62
- it('makes a computation cost more than a wait of the same duration', () => {
63
- const wall = { wallMs: 3_000 };
64
- expect(computeExecutionCost(execution({ ...wall, cpuMs: 3_000 }))).toBeGreaterThan(
65
- computeExecutionCost(execution(wall)),
66
- );
67
- });
68
-
69
- it('applies the profile coefficient to the CPU part', () => {
70
- const cpu = { cpuMs: 1_000 };
71
- expect(computeExecutionCost(execution(cpu))).toBe(10 + 500);
72
- expect(computeExecutionCost(execution({ ...cpu, profile: ResourceProfile.M }))).toBe(10 + 525);
73
- expect(computeExecutionCost(execution({ ...cpu, profile: ResourceProfile.XXL }))).toBe(
74
- 10 + 675,
75
- );
76
- // 500 × 1.05 × 0.001 = 0.525: rounded half-up once, at the end.
77
- expect(computeExecutionCost(execution({ cpuMs: 1, profile: ResourceProfile.M }))).toBe(10 + 1);
78
- });
79
-
80
- it('prices the allocated memory, the network out and the effects', () => {
81
- expect(computeExecutionCost(execution({ profile: ResourceProfile.XL, wallMs: 1_000 }))).toBe(
82
- 10 + 100,
83
- );
84
- expect(computeExecutionCost(execution({ netOutBytes: 1024 * 1024 }))).toBe(10 + 1_000);
85
- expect(computeExecutionCost(execution({ httpCalls: 2, storeWrites: 3 }))).toBe(10 + 50);
86
- });
87
-
88
- it('rounds half-up once, at the end', () => {
89
- // 128 MB × 40 ms = 0.5 millicredit.
90
- expect(computeExecutionCost(execution({ wallMs: 40 }))).toBe(11);
91
- // Fractions of a millisecond count.
92
- expect(computeExecutionCost(execution({ cpuMs: 0.999 }))).toBe(10);
93
- expect(computeExecutionCost(execution({ cpuMs: 1 }))).toBe(11);
94
- });
95
-
96
- it('makes an interrupted or refused execution free', () => {
97
- const heavy = { cpuMs: 5_000, wallMs: 9_000, netOutBytes: 10_000_000, httpCalls: 4 };
98
- expect(
99
- computeExecutionCost(execution({ ...heavy, status: CostedExecutionStatus.INTERRUPTED })),
100
- ).toBe(0);
101
- expect(
102
- computeExecutionCost(execution({ ...heavy, status: CostedExecutionStatus.REFUSED })),
103
- ).toBe(0);
104
- });
105
-
106
- it('charges the execution price once: a resumed segment adds only its measure', () => {
107
- expect(computeExecutionCost(execution({ wallMs: 3_000, resumedSegment: true }))).toBe(38);
108
- });
109
-
110
- it('refuses a negative or non-finite measure', () => {
111
- expect(() => computeExecutionCost(execution({ cpuMs: -1 }))).toThrow(RangeError);
112
- expect(() => computeExecutionCost(execution({ wallMs: Number.NaN }))).toThrow(RangeError);
113
- });
114
- });
115
-
116
- describe('computeStepCost', () => {
117
- it('gives each step its share, so that steps plus the execution price equal the total', () => {
118
- const steps = [
119
- usage({ cpuMs: 3.3, wallMs: 12.7 }),
120
- usage({ cpuMs: 120.25, wallMs: 450, httpCalls: 1, netOutBytes: 2_345 }),
121
- usage({ wallMs: 3_000 }),
122
- usage({ cpuMs: 7.77, wallMs: 9.1, storeWrites: 2 }),
123
- ];
124
- const total = execution({
125
- profile: ResourceProfile.M,
126
- // The engine between the steps adds CPU and time measured at the execution level only.
127
- cpuMs: 140,
128
- wallMs: 3_500,
129
- httpCalls: 1,
130
- storeWrites: 2,
131
- netOutBytes: 2_345,
132
- });
133
- const costs = computeStepCost({ execution: total, steps });
134
- expect(costs).toHaveLength(4);
135
- costs.forEach((cost) => expect(Number.isInteger(cost)).toBe(true));
136
- expect(sum(costs) + COST_RATES.executionMillicredits).toBe(computeExecutionCost(total));
137
- expect(costs[2]).toBeGreaterThan(costs[0]);
138
- });
139
-
140
- it('holds the invariant over many shapes', () => {
141
- for (let n = 1; n <= 40; n += 1) {
142
- const steps = Array.from({ length: n }, (_, i) =>
143
- usage({ cpuMs: (i * 7.3) % 11, wallMs: (i * 13.1) % 29, httpCalls: i % 3 === 0 ? 1 : 0 }),
144
- );
145
- const totals = steps.reduce(
146
- (acc, step) => ({
147
- cpuMs: acc.cpuMs + step.cpuMs,
148
- wallMs: acc.wallMs + step.wallMs,
149
- httpCalls: acc.httpCalls + step.httpCalls,
150
- }),
151
- { cpuMs: 0.37 * n, wallMs: 1.9 * n, httpCalls: 0 },
152
- );
153
- for (const profile of Object.values(ResourceProfile)) {
154
- const total = execution({ ...totals, profile });
155
- const costs = computeStepCost({ execution: total, steps });
156
- expect(sum(costs) + COST_RATES.executionMillicredits).toBe(computeExecutionCost(total));
157
- }
158
- }
159
- });
160
-
161
- it('rounds cumulatively instead of rounding each step', () => {
162
- // Three steps of 0.4 millicredit each (128 MB × 32 ms): 1.2 in total.
163
- const step = usage({ wallMs: 32 });
164
- const total = execution({ wallMs: 96 });
165
- expect(computeExecutionCost(total)).toBe(11);
166
- expect(computeStepCost({ execution: total, steps: [step, step, step] })).toEqual([0, 1, 0]);
167
- });
168
-
169
- it('carries no execution price on a resumed segment', () => {
170
- const total = execution({ wallMs: 3_000, resumedSegment: true });
171
- const costs = computeStepCost({ execution: total, steps: [usage({ wallMs: 3_000 })] });
172
- expect(sum(costs)).toBe(computeExecutionCost(total));
173
- });
174
-
175
- it('is zero for every step of an interrupted execution', () => {
176
- const total = execution({
177
- status: CostedExecutionStatus.INTERRUPTED,
178
- cpuMs: 900,
179
- wallMs: 2_000,
180
- });
181
- expect(
182
- computeStepCost({ execution: total, steps: [usage({ cpuMs: 400 }), usage({ cpuMs: 500 })] }),
183
- ).toEqual([0, 0]);
184
- expect(computeExecutionCost(total)).toBe(0);
185
- });
186
-
187
- it('returns nothing for an execution without step', () => {
188
- expect(computeStepCost({ execution: execution({ wallMs: 10 }), steps: [] })).toEqual([]);
189
- });
190
-
191
- it('refuses a negative step measure', () => {
192
- expect(() =>
193
- computeStepCost({ execution: execution(), steps: [usage({ httpCalls: -1 })] }),
194
- ).toThrow(RangeError);
195
- });
196
- });
1
+ import {
2
+ COST_RATES,
3
+ CostedExecutionStatus,
4
+ RESOURCE_PROFILES,
5
+ ResourceProfile,
6
+ computeExecutionCost,
7
+ computeStepCost,
8
+ } from '../execution-cost';
9
+ import type { ExecutionCostInput, StepUsage } from '../execution-cost';
10
+
11
+ const NO_USAGE: StepUsage = { cpuMs: 0, wallMs: 0, netOutBytes: 0, httpCalls: 0, storeWrites: 0 };
12
+
13
+ const execution = (overrides: Partial<ExecutionCostInput> = {}): ExecutionCostInput => ({
14
+ ...NO_USAGE,
15
+ status: CostedExecutionStatus.RAN,
16
+ profile: ResourceProfile.S,
17
+ ...overrides,
18
+ });
19
+
20
+ const usage = (overrides: Partial<StepUsage>): StepUsage => ({ ...NO_USAGE, ...overrides });
21
+
22
+ const sum = (values: number[]): number => values.reduce((total, value) => total + value, 0);
23
+
24
+ describe('resource profiles and rates', () => {
25
+ it('follow ADR-0244', () => {
26
+ expect(RESOURCE_PROFILES).toEqual({
27
+ S: { memoryMb: 128, cpuCoefficient: 1 },
28
+ M: { memoryMb: 256, cpuCoefficient: 1.05 },
29
+ L: { memoryMb: 512, cpuCoefficient: 1.1 },
30
+ XL: { memoryMb: 1024, cpuCoefficient: 1.2 },
31
+ XXL: { memoryMb: 2048, cpuCoefficient: 1.35 },
32
+ });
33
+ expect(COST_RATES).toEqual({
34
+ executionMillicredits: 10,
35
+ cpuMillicreditsPerSecond: 500,
36
+ memoryMillicreditsPerGbSecond: 100,
37
+ networkOutMillicreditsPerMb: 1000,
38
+ httpCallMillicredits: 10,
39
+ storeWriteMillicredits: 10,
40
+ });
41
+ });
42
+ });
43
+
44
+ describe('computeExecutionCost', () => {
45
+ it('prices a 3 s wait in profile S at about 0.05 credit', () => {
46
+ // 10 + 0.125 GB × 3 s × 100 = 47.5, rounded half-up.
47
+ const cost = computeExecutionCost(execution({ wallMs: 3_000 }));
48
+ expect(cost).toBe(48);
49
+ expect(cost / 1000).toBeCloseTo(0.05, 2);
50
+ });
51
+
52
+ it('prices 2 s of CPU in profile L at about 1.1 credits for the CPU part', () => {
53
+ const computing = computeExecutionCost(
54
+ execution({ profile: ResourceProfile.L, cpuMs: 2_000, wallMs: 2_000 }),
55
+ );
56
+ const waiting = computeExecutionCost(execution({ profile: ResourceProfile.L, wallMs: 2_000 }));
57
+ expect(computing - waiting).toBe(1_100);
58
+ // 10 + 1100 CPU + 0.5 GB × 2 s × 100 memory.
59
+ expect(computing).toBe(1_210);
60
+ });
61
+
62
+ it('makes a computation cost more than a wait of the same duration', () => {
63
+ const wall = { wallMs: 3_000 };
64
+ expect(computeExecutionCost(execution({ ...wall, cpuMs: 3_000 }))).toBeGreaterThan(
65
+ computeExecutionCost(execution(wall)),
66
+ );
67
+ });
68
+
69
+ it('applies the profile coefficient to the CPU part', () => {
70
+ const cpu = { cpuMs: 1_000 };
71
+ expect(computeExecutionCost(execution(cpu))).toBe(10 + 500);
72
+ expect(computeExecutionCost(execution({ ...cpu, profile: ResourceProfile.M }))).toBe(10 + 525);
73
+ expect(computeExecutionCost(execution({ ...cpu, profile: ResourceProfile.XXL }))).toBe(
74
+ 10 + 675,
75
+ );
76
+ // 500 × 1.05 × 0.001 = 0.525: rounded half-up once, at the end.
77
+ expect(computeExecutionCost(execution({ cpuMs: 1, profile: ResourceProfile.M }))).toBe(10 + 1);
78
+ });
79
+
80
+ it('prices the allocated memory, the network out and the effects', () => {
81
+ expect(computeExecutionCost(execution({ profile: ResourceProfile.XL, wallMs: 1_000 }))).toBe(
82
+ 10 + 100,
83
+ );
84
+ expect(computeExecutionCost(execution({ netOutBytes: 1024 * 1024 }))).toBe(10 + 1_000);
85
+ expect(computeExecutionCost(execution({ httpCalls: 2, storeWrites: 3 }))).toBe(10 + 50);
86
+ });
87
+
88
+ it('rounds half-up once, at the end', () => {
89
+ // 128 MB × 40 ms = 0.5 millicredit.
90
+ expect(computeExecutionCost(execution({ wallMs: 40 }))).toBe(11);
91
+ // Fractions of a millisecond count.
92
+ expect(computeExecutionCost(execution({ cpuMs: 0.999 }))).toBe(10);
93
+ expect(computeExecutionCost(execution({ cpuMs: 1 }))).toBe(11);
94
+ });
95
+
96
+ it('makes an interrupted or refused execution free', () => {
97
+ const heavy = { cpuMs: 5_000, wallMs: 9_000, netOutBytes: 10_000_000, httpCalls: 4 };
98
+ expect(
99
+ computeExecutionCost(execution({ ...heavy, status: CostedExecutionStatus.INTERRUPTED })),
100
+ ).toBe(0);
101
+ expect(
102
+ computeExecutionCost(execution({ ...heavy, status: CostedExecutionStatus.REFUSED })),
103
+ ).toBe(0);
104
+ });
105
+
106
+ it('charges the execution price once: a resumed segment adds only its measure', () => {
107
+ expect(computeExecutionCost(execution({ wallMs: 3_000, resumedSegment: true }))).toBe(38);
108
+ });
109
+
110
+ it('refuses a negative or non-finite measure', () => {
111
+ expect(() => computeExecutionCost(execution({ cpuMs: -1 }))).toThrow(RangeError);
112
+ expect(() => computeExecutionCost(execution({ wallMs: Number.NaN }))).toThrow(RangeError);
113
+ });
114
+ });
115
+
116
+ describe('computeStepCost', () => {
117
+ it('gives each step its share, so that steps plus the execution price equal the total', () => {
118
+ const steps = [
119
+ usage({ cpuMs: 3.3, wallMs: 12.7 }),
120
+ usage({ cpuMs: 120.25, wallMs: 450, httpCalls: 1, netOutBytes: 2_345 }),
121
+ usage({ wallMs: 3_000 }),
122
+ usage({ cpuMs: 7.77, wallMs: 9.1, storeWrites: 2 }),
123
+ ];
124
+ const total = execution({
125
+ profile: ResourceProfile.M,
126
+ // The engine between the steps adds CPU and time measured at the execution level only.
127
+ cpuMs: 140,
128
+ wallMs: 3_500,
129
+ httpCalls: 1,
130
+ storeWrites: 2,
131
+ netOutBytes: 2_345,
132
+ });
133
+ const costs = computeStepCost({ execution: total, steps });
134
+ expect(costs).toHaveLength(4);
135
+ costs.forEach((cost) => expect(Number.isInteger(cost)).toBe(true));
136
+ expect(sum(costs) + COST_RATES.executionMillicredits).toBe(computeExecutionCost(total));
137
+ expect(costs[2]).toBeGreaterThan(costs[0]);
138
+ });
139
+
140
+ it('holds the invariant over many shapes', () => {
141
+ for (let n = 1; n <= 40; n += 1) {
142
+ const steps = Array.from({ length: n }, (_, i) =>
143
+ usage({ cpuMs: (i * 7.3) % 11, wallMs: (i * 13.1) % 29, httpCalls: i % 3 === 0 ? 1 : 0 }),
144
+ );
145
+ const totals = steps.reduce(
146
+ (acc, step) => ({
147
+ cpuMs: acc.cpuMs + step.cpuMs,
148
+ wallMs: acc.wallMs + step.wallMs,
149
+ httpCalls: acc.httpCalls + step.httpCalls,
150
+ }),
151
+ { cpuMs: 0.37 * n, wallMs: 1.9 * n, httpCalls: 0 },
152
+ );
153
+ for (const profile of Object.values(ResourceProfile)) {
154
+ const total = execution({ ...totals, profile });
155
+ const costs = computeStepCost({ execution: total, steps });
156
+ expect(sum(costs) + COST_RATES.executionMillicredits).toBe(computeExecutionCost(total));
157
+ }
158
+ }
159
+ });
160
+
161
+ it('rounds cumulatively instead of rounding each step', () => {
162
+ // Three steps of 0.4 millicredit each (128 MB × 32 ms): 1.2 in total.
163
+ const step = usage({ wallMs: 32 });
164
+ const total = execution({ wallMs: 96 });
165
+ expect(computeExecutionCost(total)).toBe(11);
166
+ expect(computeStepCost({ execution: total, steps: [step, step, step] })).toEqual([0, 1, 0]);
167
+ });
168
+
169
+ it('carries no execution price on a resumed segment', () => {
170
+ const total = execution({ wallMs: 3_000, resumedSegment: true });
171
+ const costs = computeStepCost({ execution: total, steps: [usage({ wallMs: 3_000 })] });
172
+ expect(sum(costs)).toBe(computeExecutionCost(total));
173
+ });
174
+
175
+ it('is zero for every step of an interrupted execution', () => {
176
+ const total = execution({
177
+ status: CostedExecutionStatus.INTERRUPTED,
178
+ cpuMs: 900,
179
+ wallMs: 2_000,
180
+ });
181
+ expect(
182
+ computeStepCost({ execution: total, steps: [usage({ cpuMs: 400 }), usage({ cpuMs: 500 })] }),
183
+ ).toEqual([0, 0]);
184
+ expect(computeExecutionCost(total)).toBe(0);
185
+ });
186
+
187
+ it('returns nothing for an execution without step', () => {
188
+ expect(computeStepCost({ execution: execution({ wallMs: 10 }), steps: [] })).toEqual([]);
189
+ });
190
+
191
+ it('refuses a negative step measure', () => {
192
+ expect(() =>
193
+ computeStepCost({ execution: execution(), steps: [usage({ httpCalls: -1 })] }),
194
+ ).toThrow(RangeError);
195
+ });
196
+ });