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