@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,186 +1,186 @@
1
- // Metered cost of an execution, shared by Flow and Reflex (ADR-0240). Prices are in
2
- // millicredits (thousandths of a credit). Every part is computed exactly, in integer
3
- // sub-millicredit units, and rounded half-up once, at the end.
4
-
5
- export enum ResourceProfile {
6
- S = 'S',
7
- M = 'M',
8
- L = 'L',
9
- XL = 'XL',
10
- XXL = 'XXL',
11
- }
12
-
13
- export interface ResourceProfileSpec {
14
- /** Memory of the execution's isolate. */
15
- memoryMb: number;
16
- /** Multiplies the CPU part. */
17
- cpuCoefficient: number;
18
- }
19
-
20
- export const RESOURCE_PROFILES: Readonly<Record<ResourceProfile, ResourceProfileSpec>> = {
21
- [ResourceProfile.S]: { memoryMb: 128, cpuCoefficient: 1 },
22
- [ResourceProfile.M]: { memoryMb: 256, cpuCoefficient: 1.05 },
23
- [ResourceProfile.L]: { memoryMb: 512, cpuCoefficient: 1.1 },
24
- [ResourceProfile.XL]: { memoryMb: 1024, cpuCoefficient: 1.2 },
25
- [ResourceProfile.XXL]: { memoryMb: 2048, cpuCoefficient: 1.35 },
26
- };
27
-
28
- /** The ADR-0240 price list, in millicredits. */
29
- export const COST_RATES = {
30
- /** Once per execution, whatever its number of segments. */
31
- executionMillicredits: 10,
32
- cpuMillicreditsPerSecond: 500,
33
- /** Per GB-second of ALLOCATED memory (the profile's), GB = 1024 MB. */
34
- memoryMillicreditsPerGbSecond: 100,
35
- /** Per MB sent out, MB = 1024 × 1024 bytes. */
36
- networkOutMillicreditsPerMb: 1000,
37
- httpCallMillicredits: 10,
38
- storeWriteMillicredits: 10,
39
- } as const;
40
-
41
- export enum CostedExecutionStatus {
42
- /** Ran (succeeded, failed or suspended): billed at the measure. */
43
- RAN = 'RAN',
44
- /** Stopped by a pod shutdown: free (ADR-0240 §6). */
45
- INTERRUPTED = 'INTERRUPTED',
46
- /** Refused before any step (401, 404, 429, paused, invalid input): free. */
47
- REFUSED = 'REFUSED',
48
- }
49
-
50
- /** What one step consumed. A refused host call is not counted in `httpCalls` nor `storeWrites`. */
51
- export interface StepUsage {
52
- cpuMs: number;
53
- wallMs: number;
54
- netOutBytes: number;
55
- httpCalls: number;
56
- storeWrites: number;
57
- }
58
-
59
- export interface ExecutionCostInput extends StepUsage {
60
- status: CostedExecutionStatus;
61
- profile: ResourceProfile;
62
- /**
63
- * True for the second and later segments of a suspended execution: they add their measure
64
- * but not the execution price, paid once by the first segment.
65
- */
66
- resumedSegment?: boolean;
67
- }
68
-
69
- export interface StepCostInput {
70
- /** The execution (segment) the steps belong to; its totals are what is billed. */
71
- execution: ExecutionCostInput;
72
- /** In execution order. */
73
- steps: StepUsage[];
74
- }
75
-
76
- // Exact unit: 1 / UNITS_PER_MILLICREDIT of a millicredit. Durations are taken in microseconds
77
- // (cpuMs and wallMs may carry fractions of a millisecond), coefficients in hundredths. Each part
78
- // is a product divided by a fixed denominator; UNITS_PER_MILLICREDIT is their least common
79
- // multiple, 2^20 × 5^8, so that every part is an exact integer of units.
80
- const MICROS_PER_SECOND = BigInt(1_000_000);
81
- const CPU_DENOMINATOR = MICROS_PER_SECOND * BigInt(100);
82
- const MEMORY_DENOMINATOR = BigInt(1024) * MICROS_PER_SECOND;
83
- const NETWORK_DENOMINATOR = BigInt(1024 * 1024);
84
- const UNITS_PER_MILLICREDIT = BigInt(1_048_576) * BigInt(390_625);
85
- const ZERO = BigInt(0);
86
- const ONE = BigInt(1);
87
- const TWO = BigInt(2);
88
-
89
- const assertUsage = (usage: StepUsage, subject: string): void => {
90
- (['cpuMs', 'wallMs', 'netOutBytes', 'httpCalls', 'storeWrites'] as const).forEach((key) => {
91
- const value = usage[key];
92
- if (!Number.isFinite(value) || value < 0) {
93
- throw new RangeError(`${subject}.${key} must be a finite number >= 0, got ${value}`);
94
- }
95
- });
96
- };
97
-
98
- const micros = (ms: number): bigint => BigInt(Math.round(ms * 1000));
99
- const whole = (count: number): bigint => BigInt(Math.round(count));
100
- /** Profile coefficients have at most two decimals (1.05): exact as integers. */
101
- const hundredths = (coefficient: number): bigint => BigInt(Math.round(coefficient * 100));
102
-
103
- /** Variable cost (everything but the execution price) of a usage, in exact units. */
104
- const variableUnits = (usage: StepUsage, execution: ExecutionCostInput): bigint => {
105
- const profile = RESOURCE_PROFILES[execution.profile];
106
- // cpuUs / 10^6 s × rate × coefficient% / 100
107
- const cpu =
108
- (micros(usage.cpuMs) *
109
- BigInt(COST_RATES.cpuMillicreditsPerSecond) *
110
- hundredths(profile.cpuCoefficient) *
111
- UNITS_PER_MILLICREDIT) /
112
- CPU_DENOMINATOR;
113
- // memoryMb / 1024 GB × wallUs / 10^6 s × rate
114
- const memory =
115
- (BigInt(profile.memoryMb) *
116
- micros(usage.wallMs) *
117
- BigInt(COST_RATES.memoryMillicreditsPerGbSecond) *
118
- UNITS_PER_MILLICREDIT) /
119
- MEMORY_DENOMINATOR;
120
- // bytes / (1024 × 1024) MB × rate
121
- const network =
122
- (whole(usage.netOutBytes) *
123
- BigInt(COST_RATES.networkOutMillicreditsPerMb) *
124
- UNITS_PER_MILLICREDIT) /
125
- NETWORK_DENOMINATOR;
126
- const effects =
127
- (whole(usage.httpCalls) * BigInt(COST_RATES.httpCallMillicredits) +
128
- whole(usage.storeWrites) * BigInt(COST_RATES.storeWriteMillicredits)) *
129
- UNITS_PER_MILLICREDIT;
130
- return cpu + memory + network + effects;
131
- };
132
-
133
- /** Floor division, also for a negative numerator. */
134
- const floorDiv = (numerator: bigint, denominator: bigint): bigint => {
135
- const quotient = numerator / denominator;
136
- return numerator % denominator !== ZERO && numerator < ZERO ? quotient - ONE : quotient;
137
- };
138
-
139
- /** Exact units to whole millicredits, half-up. */
140
- const roundHalfUp = (units: bigint): number =>
141
- Number(floorDiv(units * TWO + UNITS_PER_MILLICREDIT, UNITS_PER_MILLICREDIT * TWO));
142
-
143
- const isFree = (execution: ExecutionCostInput): boolean =>
144
- execution.status !== CostedExecutionStatus.RAN;
145
-
146
- const executionPrice = (execution: ExecutionCostInput): number =>
147
- isFree(execution) || execution.resumedSegment ? 0 : COST_RATES.executionMillicredits;
148
-
149
- /** Cost of an execution (or of one segment of it), in whole millicredits. */
150
- export const computeExecutionCost = (input: ExecutionCostInput): number => {
151
- assertUsage(input, 'execution');
152
- if (isFree(input)) {
153
- return 0;
154
- }
155
- return executionPrice(input) + roundHalfUp(variableUnits(input, input));
156
- };
157
-
158
- /**
159
- * Spreads the variable cost of an execution over its steps, one whole number of millicredits
160
- * per step, in the order given. Each step gets its CPU, its memory (allocated memory × its
161
- * duration), its network and its effects; what the execution measured outside any step (the
162
- * engine between two steps) goes to the last step. Rounding is cumulative, so that
163
- * `sum(steps) + execution price = computeExecutionCost(execution)` holds exactly. An execution
164
- * that ran no step has nothing to spread: the result is empty.
165
- */
166
- export const computeStepCost = (input: StepCostInput): number[] => {
167
- const { execution, steps } = input;
168
- assertUsage(execution, 'execution');
169
- steps.forEach((step, index) => assertUsage(step, `steps[${index}]`));
170
- if (steps.length === 0) {
171
- return [];
172
- }
173
- if (isFree(execution)) {
174
- return steps.map(() => 0);
175
- }
176
- const total = variableUnits(execution, execution);
177
- let cumulative = ZERO;
178
- let assigned = 0;
179
- return steps.map((step, index) => {
180
- cumulative = index === steps.length - 1 ? total : cumulative + variableUnits(step, execution);
181
- const rounded = roundHalfUp(cumulative);
182
- const cost = rounded - assigned;
183
- assigned = rounded;
184
- return cost;
185
- });
186
- };
1
+ // Metered cost of an execution, shared by Flow and Reflex (ADR-0244). Prices are in
2
+ // millicredits (thousandths of a credit). Every part is computed exactly, in integer
3
+ // sub-millicredit units, and rounded half-up once, at the end.
4
+
5
+ export enum ResourceProfile {
6
+ S = 'S',
7
+ M = 'M',
8
+ L = 'L',
9
+ XL = 'XL',
10
+ XXL = 'XXL',
11
+ }
12
+
13
+ export interface ResourceProfileSpec {
14
+ /** Memory of the execution's isolate. */
15
+ memoryMb: number;
16
+ /** Multiplies the CPU part. */
17
+ cpuCoefficient: number;
18
+ }
19
+
20
+ export const RESOURCE_PROFILES: Readonly<Record<ResourceProfile, ResourceProfileSpec>> = {
21
+ [ResourceProfile.S]: { memoryMb: 128, cpuCoefficient: 1 },
22
+ [ResourceProfile.M]: { memoryMb: 256, cpuCoefficient: 1.05 },
23
+ [ResourceProfile.L]: { memoryMb: 512, cpuCoefficient: 1.1 },
24
+ [ResourceProfile.XL]: { memoryMb: 1024, cpuCoefficient: 1.2 },
25
+ [ResourceProfile.XXL]: { memoryMb: 2048, cpuCoefficient: 1.35 },
26
+ };
27
+
28
+ /** The ADR-0244 price list, in millicredits. */
29
+ export const COST_RATES = {
30
+ /** Once per execution, whatever its number of segments. */
31
+ executionMillicredits: 10,
32
+ cpuMillicreditsPerSecond: 500,
33
+ /** Per GB-second of ALLOCATED memory (the profile's), GB = 1024 MB. */
34
+ memoryMillicreditsPerGbSecond: 100,
35
+ /** Per MB sent out, MB = 1024 × 1024 bytes. */
36
+ networkOutMillicreditsPerMb: 1000,
37
+ httpCallMillicredits: 10,
38
+ storeWriteMillicredits: 10,
39
+ } as const;
40
+
41
+ export enum CostedExecutionStatus {
42
+ /** Ran (succeeded, failed or suspended): billed at the measure. */
43
+ RAN = 'RAN',
44
+ /** Stopped by a pod shutdown: free (ADR-0244 §6). */
45
+ INTERRUPTED = 'INTERRUPTED',
46
+ /** Refused before any step (401, 404, 429, paused, invalid input): free. */
47
+ REFUSED = 'REFUSED',
48
+ }
49
+
50
+ /** What one step consumed. A refused host call is not counted in `httpCalls` nor `storeWrites`. */
51
+ export interface StepUsage {
52
+ cpuMs: number;
53
+ wallMs: number;
54
+ netOutBytes: number;
55
+ httpCalls: number;
56
+ storeWrites: number;
57
+ }
58
+
59
+ export interface ExecutionCostInput extends StepUsage {
60
+ status: CostedExecutionStatus;
61
+ profile: ResourceProfile;
62
+ /**
63
+ * True for the second and later segments of a suspended execution: they add their measure
64
+ * but not the execution price, paid once by the first segment.
65
+ */
66
+ resumedSegment?: boolean;
67
+ }
68
+
69
+ export interface StepCostInput {
70
+ /** The execution (segment) the steps belong to; its totals are what is billed. */
71
+ execution: ExecutionCostInput;
72
+ /** In execution order. */
73
+ steps: StepUsage[];
74
+ }
75
+
76
+ // Exact unit: 1 / UNITS_PER_MILLICREDIT of a millicredit. Durations are taken in microseconds
77
+ // (cpuMs and wallMs may carry fractions of a millisecond), coefficients in hundredths. Each part
78
+ // is a product divided by a fixed denominator; UNITS_PER_MILLICREDIT is their least common
79
+ // multiple, 2^20 × 5^8, so that every part is an exact integer of units.
80
+ const MICROS_PER_SECOND = BigInt(1_000_000);
81
+ const CPU_DENOMINATOR = MICROS_PER_SECOND * BigInt(100);
82
+ const MEMORY_DENOMINATOR = BigInt(1024) * MICROS_PER_SECOND;
83
+ const NETWORK_DENOMINATOR = BigInt(1024 * 1024);
84
+ const UNITS_PER_MILLICREDIT = BigInt(1_048_576) * BigInt(390_625);
85
+ const ZERO = BigInt(0);
86
+ const ONE = BigInt(1);
87
+ const TWO = BigInt(2);
88
+
89
+ const assertUsage = (usage: StepUsage, subject: string): void => {
90
+ (['cpuMs', 'wallMs', 'netOutBytes', 'httpCalls', 'storeWrites'] as const).forEach((key) => {
91
+ const value = usage[key];
92
+ if (!Number.isFinite(value) || value < 0) {
93
+ throw new RangeError(`${subject}.${key} must be a finite number >= 0, got ${value}`);
94
+ }
95
+ });
96
+ };
97
+
98
+ const micros = (ms: number): bigint => BigInt(Math.round(ms * 1000));
99
+ const whole = (count: number): bigint => BigInt(Math.round(count));
100
+ /** Profile coefficients have at most two decimals (1.05): exact as integers. */
101
+ const hundredths = (coefficient: number): bigint => BigInt(Math.round(coefficient * 100));
102
+
103
+ /** Variable cost (everything but the execution price) of a usage, in exact units. */
104
+ const variableUnits = (usage: StepUsage, execution: ExecutionCostInput): bigint => {
105
+ const profile = RESOURCE_PROFILES[execution.profile];
106
+ // cpuUs / 10^6 s × rate × coefficient% / 100
107
+ const cpu =
108
+ (micros(usage.cpuMs) *
109
+ BigInt(COST_RATES.cpuMillicreditsPerSecond) *
110
+ hundredths(profile.cpuCoefficient) *
111
+ UNITS_PER_MILLICREDIT) /
112
+ CPU_DENOMINATOR;
113
+ // memoryMb / 1024 GB × wallUs / 10^6 s × rate
114
+ const memory =
115
+ (BigInt(profile.memoryMb) *
116
+ micros(usage.wallMs) *
117
+ BigInt(COST_RATES.memoryMillicreditsPerGbSecond) *
118
+ UNITS_PER_MILLICREDIT) /
119
+ MEMORY_DENOMINATOR;
120
+ // bytes / (1024 × 1024) MB × rate
121
+ const network =
122
+ (whole(usage.netOutBytes) *
123
+ BigInt(COST_RATES.networkOutMillicreditsPerMb) *
124
+ UNITS_PER_MILLICREDIT) /
125
+ NETWORK_DENOMINATOR;
126
+ const effects =
127
+ (whole(usage.httpCalls) * BigInt(COST_RATES.httpCallMillicredits) +
128
+ whole(usage.storeWrites) * BigInt(COST_RATES.storeWriteMillicredits)) *
129
+ UNITS_PER_MILLICREDIT;
130
+ return cpu + memory + network + effects;
131
+ };
132
+
133
+ /** Floor division, also for a negative numerator. */
134
+ const floorDiv = (numerator: bigint, denominator: bigint): bigint => {
135
+ const quotient = numerator / denominator;
136
+ return numerator % denominator !== ZERO && numerator < ZERO ? quotient - ONE : quotient;
137
+ };
138
+
139
+ /** Exact units to whole millicredits, half-up. */
140
+ const roundHalfUp = (units: bigint): number =>
141
+ Number(floorDiv(units * TWO + UNITS_PER_MILLICREDIT, UNITS_PER_MILLICREDIT * TWO));
142
+
143
+ const isFree = (execution: ExecutionCostInput): boolean =>
144
+ execution.status !== CostedExecutionStatus.RAN;
145
+
146
+ const executionPrice = (execution: ExecutionCostInput): number =>
147
+ isFree(execution) || execution.resumedSegment ? 0 : COST_RATES.executionMillicredits;
148
+
149
+ /** Cost of an execution (or of one segment of it), in whole millicredits. */
150
+ export const computeExecutionCost = (input: ExecutionCostInput): number => {
151
+ assertUsage(input, 'execution');
152
+ if (isFree(input)) {
153
+ return 0;
154
+ }
155
+ return executionPrice(input) + roundHalfUp(variableUnits(input, input));
156
+ };
157
+
158
+ /**
159
+ * Spreads the variable cost of an execution over its steps, one whole number of millicredits
160
+ * per step, in the order given. Each step gets its CPU, its memory (allocated memory × its
161
+ * duration), its network and its effects; what the execution measured outside any step (the
162
+ * engine between two steps) goes to the last step. Rounding is cumulative, so that
163
+ * `sum(steps) + execution price = computeExecutionCost(execution)` holds exactly. An execution
164
+ * that ran no step has nothing to spread: the result is empty.
165
+ */
166
+ export const computeStepCost = (input: StepCostInput): number[] => {
167
+ const { execution, steps } = input;
168
+ assertUsage(execution, 'execution');
169
+ steps.forEach((step, index) => assertUsage(step, `steps[${index}]`));
170
+ if (steps.length === 0) {
171
+ return [];
172
+ }
173
+ if (isFree(execution)) {
174
+ return steps.map(() => 0);
175
+ }
176
+ const total = variableUnits(execution, execution);
177
+ let cumulative = ZERO;
178
+ let assigned = 0;
179
+ return steps.map((step, index) => {
180
+ cumulative = index === steps.length - 1 ? total : cumulative + variableUnits(step, execution);
181
+ const rounded = roundHalfUp(cumulative);
182
+ const cost = rounded - assigned;
183
+ assigned = rounded;
184
+ return cost;
185
+ });
186
+ };