@abyss-project/runtime 3.5.0 → 3.7.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 +5 -2
- package/dist/engine/run-in-isolate.js.map +1 -1
- package/dist/interpreter/interpreter.js +19 -4
- package/dist/interpreter/interpreter.js.map +1 -1
- package/dist/interpreter/interpreter.types.d.ts +3 -0
- package/dist/interpreter/interpreter.types.js.map +1 -1
- package/dist/runner-protocol/execution-protocol.types.d.ts +12 -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 +15 -10
- package/scripts/build-engine.cjs +70 -70
- package/src/__tests__/engine-bundle.test.ts +121 -121
- package/src/__tests__/surface.test.ts +70 -66
- 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 +468 -468
- package/src/engine/engine-entry.ts +3 -3
- package/src/engine/engine-global.ts +2 -2
- package/src/engine/run-in-isolate.ts +419 -404
- 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 -223
- package/src/interpreter/interpreter-error.ts +12 -12
- package/src/interpreter/interpreter.ts +483 -461
- package/src/interpreter/interpreter.types.ts +204 -192
- 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 +260 -231
- 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
|
@@ -1,66 +1,70 @@
|
|
|
1
|
-
import { builtinModules } from 'module';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
|
|
5
|
-
import { DiscordEventType, isDiscordEventInput } from '../index';
|
|
6
|
-
const SRC_DIR = path.resolve(__dirname, '..');
|
|
7
|
-
const SERVER_DIR = path.join(SRC_DIR, 'server');
|
|
8
|
-
const TESTS_DIR = path.join(SRC_DIR, '__tests__');
|
|
9
|
-
|
|
10
|
-
// `ws` and every Node built-in module (`fs`, `net`, `http`, `node:*`...) are forbidden outside
|
|
11
|
-
// `src/server`, so that the package root stays usable in a browser.
|
|
12
|
-
const FORBIDDEN = new Set<string>(['ws', ...builtinModules]);
|
|
13
|
-
|
|
14
|
-
const walkTsFiles = (dir: string): string[] =>
|
|
15
|
-
fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
16
|
-
const full = path.join(dir, entry.name);
|
|
17
|
-
if (entry.isDirectory()) {
|
|
18
|
-
return full === SERVER_DIR || full === TESTS_DIR ? [] : walkTsFiles(full);
|
|
19
|
-
}
|
|
20
|
-
return entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts') ? [full] : [];
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
const IMPORT_PATTERNS = [
|
|
24
|
-
/\bfrom\s*['"]([^'"]+)['"]/g,
|
|
25
|
-
/\bimport\s*['"]([^'"]+)['"]/g,
|
|
26
|
-
/\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
27
|
-
/\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
const importedSpecifiers = (text: string): string[] =>
|
|
31
|
-
IMPORT_PATTERNS.flatMap((pattern) => [...text.matchAll(pattern)].map((match) => match[1]));
|
|
32
|
-
|
|
33
|
-
const isForbidden = (specifier: string): boolean => {
|
|
34
|
-
const bare = specifier.startsWith('node:') ? specifier.slice('node:'.length) : specifier;
|
|
35
|
-
return FORBIDDEN.has(bare) || FORBIDDEN.has(bare.split('/')[0]);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
describe('browser-compatible package root', () => {
|
|
39
|
-
it('imports no Node built-in nor ws outside src/server', () => {
|
|
40
|
-
const offenders = walkTsFiles(SRC_DIR).flatMap((file) =>
|
|
41
|
-
importedSpecifiers(fs.readFileSync(file, 'utf8'))
|
|
42
|
-
.filter(isForbidden)
|
|
43
|
-
.map((specifier) => `${path.relative(SRC_DIR, file)} imports ${specifier}`),
|
|
44
|
-
);
|
|
45
|
-
expect(offenders).toEqual([]);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('detects forbidden imports (guard sanity)', () => {
|
|
49
|
-
const source = "import { WebSocket } from 'ws';\nconst n = require('node:net');";
|
|
50
|
-
expect(importedSpecifiers(source)).toEqual(['ws', 'node:net']);
|
|
51
|
-
expect(['ws', 'node:net', 'fs/promises', 'http'].every(isForbidden)).toBe(true);
|
|
52
|
-
expect(['joi', './steps', '../access'].some(isForbidden)).toBe(false);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
describe('public surface of the package root', () => {
|
|
57
|
-
it('exports the Discord event input contract', () => {
|
|
58
|
-
expect(Object.values(DiscordEventType)).toEqual([
|
|
59
|
-
'GUILD_MEMBER_ADD',
|
|
60
|
-
'GUILD_MEMBER_REMOVE',
|
|
61
|
-
'MESSAGE_CREATE',
|
|
62
|
-
'MESSAGE_REACTION_ADD',
|
|
63
|
-
]);
|
|
64
|
-
expect(typeof isDiscordEventInput).toBe('function');
|
|
65
|
-
});
|
|
66
|
-
|
|
1
|
+
import { builtinModules } from 'module';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
|
|
5
|
+
import { DiscordEventType, MAX_ERROR_STACK_CHARS, isDiscordEventInput } from '../index';
|
|
6
|
+
const SRC_DIR = path.resolve(__dirname, '..');
|
|
7
|
+
const SERVER_DIR = path.join(SRC_DIR, 'server');
|
|
8
|
+
const TESTS_DIR = path.join(SRC_DIR, '__tests__');
|
|
9
|
+
|
|
10
|
+
// `ws` and every Node built-in module (`fs`, `net`, `http`, `node:*`...) are forbidden outside
|
|
11
|
+
// `src/server`, so that the package root stays usable in a browser.
|
|
12
|
+
const FORBIDDEN = new Set<string>(['ws', ...builtinModules]);
|
|
13
|
+
|
|
14
|
+
const walkTsFiles = (dir: string): string[] =>
|
|
15
|
+
fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
16
|
+
const full = path.join(dir, entry.name);
|
|
17
|
+
if (entry.isDirectory()) {
|
|
18
|
+
return full === SERVER_DIR || full === TESTS_DIR ? [] : walkTsFiles(full);
|
|
19
|
+
}
|
|
20
|
+
return entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts') ? [full] : [];
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const IMPORT_PATTERNS = [
|
|
24
|
+
/\bfrom\s*['"]([^'"]+)['"]/g,
|
|
25
|
+
/\bimport\s*['"]([^'"]+)['"]/g,
|
|
26
|
+
/\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
27
|
+
/\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const importedSpecifiers = (text: string): string[] =>
|
|
31
|
+
IMPORT_PATTERNS.flatMap((pattern) => [...text.matchAll(pattern)].map((match) => match[1]));
|
|
32
|
+
|
|
33
|
+
const isForbidden = (specifier: string): boolean => {
|
|
34
|
+
const bare = specifier.startsWith('node:') ? specifier.slice('node:'.length) : specifier;
|
|
35
|
+
return FORBIDDEN.has(bare) || FORBIDDEN.has(bare.split('/')[0]);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
describe('browser-compatible package root', () => {
|
|
39
|
+
it('imports no Node built-in nor ws outside src/server', () => {
|
|
40
|
+
const offenders = walkTsFiles(SRC_DIR).flatMap((file) =>
|
|
41
|
+
importedSpecifiers(fs.readFileSync(file, 'utf8'))
|
|
42
|
+
.filter(isForbidden)
|
|
43
|
+
.map((specifier) => `${path.relative(SRC_DIR, file)} imports ${specifier}`),
|
|
44
|
+
);
|
|
45
|
+
expect(offenders).toEqual([]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('detects forbidden imports (guard sanity)', () => {
|
|
49
|
+
const source = "import { WebSocket } from 'ws';\nconst n = require('node:net');";
|
|
50
|
+
expect(importedSpecifiers(source)).toEqual(['ws', 'node:net']);
|
|
51
|
+
expect(['ws', 'node:net', 'fs/promises', 'http'].every(isForbidden)).toBe(true);
|
|
52
|
+
expect(['joi', './steps', '../access'].some(isForbidden)).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('public surface of the package root', () => {
|
|
57
|
+
it('exports the Discord event input contract', () => {
|
|
58
|
+
expect(Object.values(DiscordEventType)).toEqual([
|
|
59
|
+
'GUILD_MEMBER_ADD',
|
|
60
|
+
'GUILD_MEMBER_REMOVE',
|
|
61
|
+
'MESSAGE_CREATE',
|
|
62
|
+
'MESSAGE_REACTION_ADD',
|
|
63
|
+
]);
|
|
64
|
+
expect(typeof isDiscordEventInput).toBe('function');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('exports the error stack bound of the execution protocol', () => {
|
|
68
|
+
expect(MAX_ERROR_STACK_CHARS).toBe(4096);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { hostMatches } from '../host-match';
|
|
2
|
-
|
|
3
|
-
describe('hostMatches', () => {
|
|
4
|
-
it('matches an exact host', () => {
|
|
5
|
-
expect(hostMatches('api.example.com', 'api.example.com')).toBe(true);
|
|
6
|
-
expect(hostMatches('api.example.com', 'example.com')).toBe(false);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('lets a wildcard cover any sub-domain', () => {
|
|
10
|
-
expect(hostMatches('api.example.com', '*.example.com')).toBe(true);
|
|
11
|
-
expect(hostMatches('a.b.example.com', '*.example.com')).toBe(true);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('does not let a wildcard cover the apex domain', () => {
|
|
15
|
-
expect(hostMatches('example.com', '*.example.com')).toBe(false);
|
|
16
|
-
expect(hostMatches('.example.com', '*.example.com')).toBe(false);
|
|
17
|
-
expect(hostMatches('badexample.com', '*.example.com')).toBe(false);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('ignores case', () => {
|
|
21
|
-
expect(hostMatches('API.Example.COM', 'api.example.com')).toBe(true);
|
|
22
|
-
expect(hostMatches('api.example.com', '*.EXAMPLE.com')).toBe(true);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
1
|
+
import { hostMatches } from '../host-match';
|
|
2
|
+
|
|
3
|
+
describe('hostMatches', () => {
|
|
4
|
+
it('matches an exact host', () => {
|
|
5
|
+
expect(hostMatches('api.example.com', 'api.example.com')).toBe(true);
|
|
6
|
+
expect(hostMatches('api.example.com', 'example.com')).toBe(false);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('lets a wildcard cover any sub-domain', () => {
|
|
10
|
+
expect(hostMatches('api.example.com', '*.example.com')).toBe(true);
|
|
11
|
+
expect(hostMatches('a.b.example.com', '*.example.com')).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('does not let a wildcard cover the apex domain', () => {
|
|
15
|
+
expect(hostMatches('example.com', '*.example.com')).toBe(false);
|
|
16
|
+
expect(hostMatches('.example.com', '*.example.com')).toBe(false);
|
|
17
|
+
expect(hostMatches('badexample.com', '*.example.com')).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('ignores case', () => {
|
|
21
|
+
expect(hostMatches('API.Example.COM', 'api.example.com')).toBe(true);
|
|
22
|
+
expect(hostMatches('api.example.com', '*.EXAMPLE.com')).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
export enum AccessKind {
|
|
2
|
-
/** Outbound HTTP to a list of host names (exact, or `*.domain` for any sub-domain). */
|
|
3
|
-
HTTP = 'HTTP',
|
|
4
|
-
/** One action of the host catalog, by name. */
|
|
5
|
-
HOST_ACTION = 'HOST_ACTION',
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface HttpAccess {
|
|
9
|
-
kind: AccessKind.HTTP;
|
|
10
|
-
hosts: string[];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface HostActionAccess {
|
|
14
|
-
kind: AccessKind.HOST_ACTION;
|
|
15
|
-
action: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* What a definition declares it needs (like the `permissions:` block of GitHub Actions), and,
|
|
20
|
-
* with the same shape, what a run is actually granted. An effect is emitted only when both
|
|
21
|
-
* cover it.
|
|
22
|
-
*/
|
|
23
|
-
export type AccessDeclaration = HttpAccess | HostActionAccess;
|
|
24
|
-
|
|
25
|
-
export type AccessGrant = AccessDeclaration;
|
|
1
|
+
export enum AccessKind {
|
|
2
|
+
/** Outbound HTTP to a list of host names (exact, or `*.domain` for any sub-domain). */
|
|
3
|
+
HTTP = 'HTTP',
|
|
4
|
+
/** One action of the host catalog, by name. */
|
|
5
|
+
HOST_ACTION = 'HOST_ACTION',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface HttpAccess {
|
|
9
|
+
kind: AccessKind.HTTP;
|
|
10
|
+
hosts: string[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HostActionAccess {
|
|
14
|
+
kind: AccessKind.HOST_ACTION;
|
|
15
|
+
action: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What a definition declares it needs (like the `permissions:` block of GitHub Actions), and,
|
|
20
|
+
* with the same shape, what a run is actually granted. An effect is emitted only when both
|
|
21
|
+
* cover it.
|
|
22
|
+
*/
|
|
23
|
+
export type AccessDeclaration = HttpAccess | HostActionAccess;
|
|
24
|
+
|
|
25
|
+
export type AccessGrant = AccessDeclaration;
|
package/src/access/host-match.ts
CHANGED
|
@@ -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,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
|
+
});
|