@lelouchhe/agent-evals 0.0.0-stage → 0.1.1
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/.env.example +27 -0
- package/LICENSE +21 -0
- package/README.md +117 -2
- package/dist/checkers/file/index.d.ts +26 -0
- package/dist/checkers/file/index.js +228 -0
- package/dist/checkers/judge/index.d.ts +20 -0
- package/dist/checkers/judge/index.js +262 -0
- package/dist/checkers/judge/prompt.d.ts +18 -0
- package/dist/checkers/judge/prompt.js +36 -0
- package/dist/checkers/judge/trace.d.ts +6 -0
- package/dist/checkers/judge/trace.js +41 -0
- package/dist/checkers/mock/index.d.ts +18 -0
- package/dist/checkers/mock/index.js +127 -0
- package/dist/checkers/shell/index.d.ts +10 -0
- package/dist/checkers/shell/index.js +61 -0
- package/dist/checkers/trace/index.d.ts +22 -0
- package/dist/checkers/trace/index.js +183 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +352 -0
- package/dist/engine/authoring.d.ts +24 -0
- package/dist/engine/authoring.js +261 -0
- package/dist/engine/check-runner.d.ts +1 -0
- package/dist/engine/check-runner.js +127 -0
- package/dist/engine/check-sandbox.d.ts +18 -0
- package/dist/engine/check-sandbox.js +171 -0
- package/dist/engine/config.d.ts +142 -0
- package/dist/engine/config.js +362 -0
- package/dist/engine/env-source.d.ts +10 -0
- package/dist/engine/env-source.js +114 -0
- package/dist/engine/files.d.ts +87 -0
- package/dist/engine/files.js +307 -0
- package/dist/engine/forwarder.sh +61 -0
- package/dist/engine/ignore.d.ts +29 -0
- package/dist/engine/ignore.js +67 -0
- package/dist/engine/messages.d.ts +8 -0
- package/dist/engine/messages.js +9 -0
- package/dist/engine/mock-match.d.ts +44 -0
- package/dist/engine/mock-match.js +104 -0
- package/dist/engine/mocks.d.ts +35 -0
- package/dist/engine/mocks.js +336 -0
- package/dist/engine/params.d.ts +16 -0
- package/dist/engine/params.js +86 -0
- package/dist/engine/placement.d.ts +26 -0
- package/dist/engine/placement.js +107 -0
- package/dist/engine/regex.d.ts +10 -0
- package/dist/engine/regex.js +16 -0
- package/dist/engine/registry.d.ts +234 -0
- package/dist/engine/registry.js +127 -0
- package/dist/engine/report.d.ts +17 -0
- package/dist/engine/report.js +385 -0
- package/dist/engine/results.d.ts +43 -0
- package/dist/engine/results.js +39 -0
- package/dist/engine/run.d.ts +47 -0
- package/dist/engine/run.js +566 -0
- package/dist/engine/runtime-assets.d.ts +6 -0
- package/dist/engine/runtime-assets.js +11 -0
- package/dist/engine/skills.d.ts +49 -0
- package/dist/engine/skills.js +270 -0
- package/dist/engine/truncate.d.ts +3 -0
- package/dist/engine/truncate.js +7 -0
- package/dist/engine/values.d.ts +18 -0
- package/dist/engine/values.js +75 -0
- package/dist/engine/view.d.ts +10 -0
- package/dist/engine/view.js +38 -0
- package/dist/engine/workspace.d.ts +56 -0
- package/dist/engine/workspace.js +280 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +7 -0
- package/dist/platform/macos/launch.d.ts +13 -0
- package/dist/platform/macos/launch.js +14 -0
- package/dist/platform/macos/policy.d.ts +33 -0
- package/dist/platform/macos/policy.js +131 -0
- package/dist/platform/macos/supervise.d.ts +26 -0
- package/dist/platform/macos/supervise.js +98 -0
- package/dist/platform/macos/supervisor.sh +13 -0
- package/dist/runners/acp/agent.d.ts +17 -0
- package/dist/runners/acp/agent.js +38 -0
- package/dist/runners/acp/commands.d.ts +2 -0
- package/dist/runners/acp/commands.js +112 -0
- package/dist/runners/acp/environment.d.ts +71 -0
- package/dist/runners/acp/environment.js +280 -0
- package/dist/runners/acp/index.d.ts +21 -0
- package/dist/runners/acp/index.js +131 -0
- package/dist/runners/acp/session.d.ts +7 -0
- package/dist/runners/acp/session.js +48 -0
- package/dist/runners/acp/trace.d.ts +23 -0
- package/dist/runners/acp/trace.js +44 -0
- package/dist/runners/acp/usage.d.ts +12 -0
- package/dist/runners/acp/usage.js +56 -0
- package/dist/runners/noop/index.d.ts +12 -0
- package/dist/runners/noop/index.js +22 -0
- package/dist/runners/pi/index.d.ts +21 -0
- package/dist/runners/pi/index.js +116 -0
- package/dist/runners/pi/params.d.ts +10 -0
- package/dist/runners/pi/params.js +3 -0
- package/dist/runners/pi/rpc.d.ts +30 -0
- package/dist/runners/pi/rpc.js +341 -0
- package/dist/runners/pi/safe-process-group.d.ts +7 -0
- package/dist/runners/pi/safe-process-group.js +13 -0
- package/dist/runners/pi/session.d.ts +56 -0
- package/dist/runners/pi/session.js +298 -0
- package/dist/runners/pi/shell-wrapper.sh +46 -0
- package/dist/runners/pi/supervisor.d.ts +1 -0
- package/dist/runners/pi/supervisor.js +101 -0
- package/package.json +77 -4
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/** A configuration problem detected before any runtime resource is created. */
|
|
2
|
+
export declare class ConfigError extends Error {
|
|
3
|
+
constructor(message: string, options?: ErrorOptions);
|
|
4
|
+
}
|
|
5
|
+
/** Implementation-owned parameters. Only `$type` has engine-defined meaning. */
|
|
6
|
+
export type ImplementationParams = Readonly<Record<string, unknown>>;
|
|
7
|
+
export interface ImplementationRecord extends ImplementationParams {
|
|
8
|
+
readonly $type: string;
|
|
9
|
+
}
|
|
10
|
+
/** What the case's checks are expected to do: pass, or fail as a vacancy or negative baseline. */
|
|
11
|
+
export type CaseExpectation = "pass" | "fail";
|
|
12
|
+
/** One complete, executable case after composition. */
|
|
13
|
+
export interface CaseConfig {
|
|
14
|
+
readonly input: string;
|
|
15
|
+
readonly fixture?: string;
|
|
16
|
+
/** Authoring asset placed at a workspace-relative destination path. */
|
|
17
|
+
readonly files?: Readonly<Record<string, string>>;
|
|
18
|
+
readonly timeoutMs?: number;
|
|
19
|
+
readonly mocks?: MocksConfig;
|
|
20
|
+
/** Absent means `pass`: a case is a test unless it declares itself a negative one. */
|
|
21
|
+
readonly expect?: CaseExpectation;
|
|
22
|
+
readonly checks: readonly ImplementationRecord[];
|
|
23
|
+
readonly runner: ImplementationRecord;
|
|
24
|
+
}
|
|
25
|
+
/** A fixed result returned to a called command. */
|
|
26
|
+
export interface MockResult {
|
|
27
|
+
readonly exitCode: number;
|
|
28
|
+
readonly stdout: string;
|
|
29
|
+
readonly stderr: string;
|
|
30
|
+
}
|
|
31
|
+
/** A literal text, or a RE2 pattern written as /…/; the caller decides what it is matched against. */
|
|
32
|
+
export interface PatternSyntax {
|
|
33
|
+
/** The literal text, or the pattern source without its slashes. */
|
|
34
|
+
readonly source: string;
|
|
35
|
+
readonly regex: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** A literal token, or a RE2 pattern written as /…/ and matched against the whole token. */
|
|
38
|
+
export type MockPattern = PatternSyntax;
|
|
39
|
+
/** One entry of a rule's option map: required, forbidden, or required with a value. */
|
|
40
|
+
export type MockArgument = {
|
|
41
|
+
readonly key: MockPattern;
|
|
42
|
+
readonly kind: "required";
|
|
43
|
+
} | {
|
|
44
|
+
readonly key: MockPattern;
|
|
45
|
+
readonly kind: "forbidden";
|
|
46
|
+
} | {
|
|
47
|
+
readonly key: MockPattern;
|
|
48
|
+
readonly kind: "value";
|
|
49
|
+
readonly value: MockPattern;
|
|
50
|
+
};
|
|
51
|
+
/** A rule's optional argument condition: a positional prefix plus one unordered option map. */
|
|
52
|
+
export interface MockArgs {
|
|
53
|
+
readonly positional: readonly MockPattern[];
|
|
54
|
+
readonly options: readonly MockArgument[];
|
|
55
|
+
}
|
|
56
|
+
/** What a matching rule does with the call. */
|
|
57
|
+
export type MockAction = {
|
|
58
|
+
readonly kind: "return";
|
|
59
|
+
readonly result: MockResult;
|
|
60
|
+
} | {
|
|
61
|
+
readonly kind: "run";
|
|
62
|
+
readonly command: readonly string[];
|
|
63
|
+
readonly pass: boolean;
|
|
64
|
+
} | {
|
|
65
|
+
readonly kind: "pass";
|
|
66
|
+
} | {
|
|
67
|
+
readonly kind: "refuse";
|
|
68
|
+
};
|
|
69
|
+
/** One ordered rule; a rule without `args` is the fallback. */
|
|
70
|
+
export interface MockRule {
|
|
71
|
+
readonly args?: MockArgs;
|
|
72
|
+
readonly action: MockAction;
|
|
73
|
+
}
|
|
74
|
+
/** One target's ordered rules; the first match wins. */
|
|
75
|
+
export type ShellMock = readonly MockRule[];
|
|
76
|
+
/** One ordered rule list per bare `PATH` command name or workspace-relative script path. */
|
|
77
|
+
export type ShellMocks = Readonly<Record<string, ShellMock>>;
|
|
78
|
+
export interface MocksConfig {
|
|
79
|
+
readonly shell: ShellMocks;
|
|
80
|
+
}
|
|
81
|
+
/** Where one final top-level case field was declared. Nested values share this origin. */
|
|
82
|
+
export type FieldOrigin = {
|
|
83
|
+
readonly file: string;
|
|
84
|
+
readonly line: number;
|
|
85
|
+
readonly kind: "common";
|
|
86
|
+
} | {
|
|
87
|
+
readonly file: string;
|
|
88
|
+
readonly line: number;
|
|
89
|
+
readonly kind: "case";
|
|
90
|
+
readonly caseName: string;
|
|
91
|
+
};
|
|
92
|
+
/** Basic environment-variable name grammar used by placeholders and declared variables. */
|
|
93
|
+
export declare const ENV_NAME_PATTERN: RegExp;
|
|
94
|
+
/** One source file resolved to a single case, or to common fields plus a selected case. */
|
|
95
|
+
export interface ResolvedCase {
|
|
96
|
+
readonly path: string;
|
|
97
|
+
readonly dir: string;
|
|
98
|
+
/** The selected named case, or absent for an ordinary single-case document. */
|
|
99
|
+
readonly name?: string;
|
|
100
|
+
readonly config: CaseConfig;
|
|
101
|
+
/** One annotation per final top-level field in `config`. */
|
|
102
|
+
readonly origins: Readonly<Record<string, FieldOrigin>>;
|
|
103
|
+
}
|
|
104
|
+
/** Top-level fields allowed in a complete resolved case. */
|
|
105
|
+
export declare const CASE_FIELD_KEYS: ReadonlySet<string>;
|
|
106
|
+
/** The document-level authoring control for named cases. */
|
|
107
|
+
export declare const CASES_KEY = "cases";
|
|
108
|
+
/** Resolve an authoring asset relative to the file that declares it. */
|
|
109
|
+
export declare function resolveAuthoringAsset(declaringDir: string, relativePath: string, label: string): string;
|
|
110
|
+
/** Validate a complete resolved case record and return its typed configuration. */
|
|
111
|
+
export declare function expectCaseConfig(value: unknown, label: string): CaseConfig;
|
|
112
|
+
export declare function expectRelativePath(value: unknown, label: string): string;
|
|
113
|
+
/** A placement destination is workspace-relative, or starts with `~/` to name the run HOME. */
|
|
114
|
+
export declare function expectPlacementDestination(destination: string, label: string): void;
|
|
115
|
+
export declare function expectWorkspaceDestination(destination: string, label: string): void;
|
|
116
|
+
/** The declared spelling two targets name the same thing by, for duplicates and checks. */
|
|
117
|
+
export declare function mockTargetIdentity(target: string): string;
|
|
118
|
+
/** The workspace-relative file a target names, without `.` or empty segments. */
|
|
119
|
+
export declare function workspaceTargetPath(target: string): string;
|
|
120
|
+
/** A bare command name is placed on `PATH`; a path containing `/` is workspace-relative. */
|
|
121
|
+
export declare function expectMockTarget(target: string, label: string): string;
|
|
122
|
+
export declare function expectMockArgs(value: unknown, label: string): MockArgs;
|
|
123
|
+
/**
|
|
124
|
+
* Read one pattern scalar, shared by mock rules and file checks. A finite number is compared as
|
|
125
|
+
* its text; a non-empty string that starts and ends with `/` is a RE2 pattern with the outer
|
|
126
|
+
* slashes removed; any other string is a literal. Callers own the labels, the wording of each
|
|
127
|
+
* error, and whether an empty `/…/` source is allowed.
|
|
128
|
+
*/
|
|
129
|
+
export declare function parsePatternScalar(value: unknown): PatternScalarResult;
|
|
130
|
+
/** A parsed pattern scalar, or why the value is not one. */
|
|
131
|
+
export type PatternScalarResult = {
|
|
132
|
+
readonly kind: "pattern";
|
|
133
|
+
readonly pattern: PatternSyntax;
|
|
134
|
+
} | {
|
|
135
|
+
readonly kind: "invalid";
|
|
136
|
+
readonly reason: "empty" | "unsupported";
|
|
137
|
+
} | {
|
|
138
|
+
readonly kind: "invalid";
|
|
139
|
+
readonly reason: "regex";
|
|
140
|
+
readonly message: string;
|
|
141
|
+
};
|
|
142
|
+
export declare function errorMessage(error: unknown): string;
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { compilePattern } from "./regex.js";
|
|
3
|
+
/** A configuration problem detected before any runtime resource is created. */
|
|
4
|
+
export class ConfigError extends Error {
|
|
5
|
+
constructor(message, options) {
|
|
6
|
+
super(message, options);
|
|
7
|
+
this.name = "ConfigError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/** Basic environment-variable name grammar used by placeholders and declared variables. */
|
|
11
|
+
export const ENV_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
12
|
+
/** Top-level fields allowed in a complete resolved case. */
|
|
13
|
+
export const CASE_FIELD_KEYS = new Set([
|
|
14
|
+
"input",
|
|
15
|
+
"fixture",
|
|
16
|
+
"files",
|
|
17
|
+
"timeoutMs",
|
|
18
|
+
"mocks",
|
|
19
|
+
"expect",
|
|
20
|
+
"checks",
|
|
21
|
+
"runner",
|
|
22
|
+
]);
|
|
23
|
+
/** The document-level authoring control for named cases. */
|
|
24
|
+
export const CASES_KEY = "cases";
|
|
25
|
+
const MOCK_KEYS = new Set(["shell"]);
|
|
26
|
+
const MOCK_RULE_KEYS = new Set(["args", "return", "run", "pass"]);
|
|
27
|
+
const MOCK_RESULT_KEYS = new Set(["exitCode", "stdout", "stderr"]);
|
|
28
|
+
/** Resolve an authoring asset relative to the file that declares it. */
|
|
29
|
+
export function resolveAuthoringAsset(declaringDir, relativePath, label) {
|
|
30
|
+
assertRelativePath(relativePath, label);
|
|
31
|
+
return resolve(declaringDir, relativePath);
|
|
32
|
+
}
|
|
33
|
+
/** Validate a complete resolved case record and return its typed configuration. */
|
|
34
|
+
export function expectCaseConfig(value, label) {
|
|
35
|
+
const record = expectRecord(value, label);
|
|
36
|
+
rejectUnknownKeys(record, CASE_FIELD_KEYS, label);
|
|
37
|
+
const input = expectNonEmptyString(record.input, `${label}.input`);
|
|
38
|
+
const fixture = record.fixture === undefined ? undefined : expectRelativePath(record.fixture, `${label}.fixture`);
|
|
39
|
+
const files = record.files === undefined ? undefined : expectFiles(record.files, label);
|
|
40
|
+
const timeoutMs = record.timeoutMs === undefined ? undefined : expectTimeout(record.timeoutMs, `${label}.timeoutMs`);
|
|
41
|
+
const mocks = record.mocks === undefined ? undefined : expectMocks(record.mocks, `${label}.mocks`);
|
|
42
|
+
const expect = record.expect === undefined ? undefined : expectExpectation(record.expect, `${label}.expect`);
|
|
43
|
+
if (!Array.isArray(record.checks) || record.checks.length === 0) {
|
|
44
|
+
throw new ConfigError(`${label}.checks must be a non-empty list`);
|
|
45
|
+
}
|
|
46
|
+
const checks = record.checks.map((check, index) => expectImplementationRecord(check, `${label}.checks[${index}]`));
|
|
47
|
+
const runner = expectImplementationRecord(record.runner, `${label}.runner`);
|
|
48
|
+
return {
|
|
49
|
+
input,
|
|
50
|
+
...(fixture === undefined ? {} : { fixture }),
|
|
51
|
+
...(files === undefined ? {} : { files }),
|
|
52
|
+
...(timeoutMs === undefined ? {} : { timeoutMs }),
|
|
53
|
+
...(mocks === undefined ? {} : { mocks }),
|
|
54
|
+
...(expect === undefined ? {} : { expect }),
|
|
55
|
+
checks,
|
|
56
|
+
runner,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function expectExpectation(value, label) {
|
|
60
|
+
if (value === "pass" || value === "fail") {
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
throw new ConfigError(`${label} must be "pass" or "fail"`);
|
|
64
|
+
}
|
|
65
|
+
function expectRecord(value, label) {
|
|
66
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
67
|
+
throw new ConfigError(`${label} must be a YAML mapping`);
|
|
68
|
+
}
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
function expectImplementationRecord(value, label) {
|
|
72
|
+
const record = expectRecord(value, label);
|
|
73
|
+
const type = record.$type;
|
|
74
|
+
if (typeof type !== "string" || type.trim().length === 0) {
|
|
75
|
+
throw new ConfigError(`${label}.$type must be a non-empty string`);
|
|
76
|
+
}
|
|
77
|
+
return record;
|
|
78
|
+
}
|
|
79
|
+
function rejectUnknownKeys(record, allowed, label) {
|
|
80
|
+
const unknown = Object.keys(record).filter((key) => !allowed.has(key));
|
|
81
|
+
if (unknown.length > 0) {
|
|
82
|
+
throw new ConfigError(`${label} has unknown field${unknown.length === 1 ? "" : "s"}: ${unknown.join(", ")}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function expectNonEmptyString(value, label) {
|
|
86
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
87
|
+
throw new ConfigError(`${label} must be a non-empty string`);
|
|
88
|
+
}
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
export function expectRelativePath(value, label) {
|
|
92
|
+
const path = expectNonEmptyString(value, label);
|
|
93
|
+
assertRelativePath(path, label);
|
|
94
|
+
return path;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Read the case's file placements. Keys are workspace-relative destinations, or `~/…`
|
|
98
|
+
* destinations in the run HOME, so unlike authoring sources they may not leave their root.
|
|
99
|
+
*/
|
|
100
|
+
function expectFiles(value, label) {
|
|
101
|
+
const record = expectRecord(value, `${label}.files`);
|
|
102
|
+
return Object.fromEntries(Object.entries(record).map(([destination, source]) => {
|
|
103
|
+
expectPlacementDestination(destination, `${label}.files destination "${destination}"`);
|
|
104
|
+
return [destination, expectRelativePath(source, `${label}.files["${destination}"]`)];
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
/** A placement destination is workspace-relative, or starts with `~/` to name the run HOME. */
|
|
108
|
+
export function expectPlacementDestination(destination, label) {
|
|
109
|
+
expectNonEmptyString(destination, label);
|
|
110
|
+
if (destination.includes("\0")) {
|
|
111
|
+
throw new ConfigError(`${label} contains a NUL byte`);
|
|
112
|
+
}
|
|
113
|
+
if (destination.startsWith("~/")) {
|
|
114
|
+
const homePath = destination.slice(2);
|
|
115
|
+
// `~/` alone names the HOME root; anything after it follows the workspace rules.
|
|
116
|
+
if (homePath.length === 0) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
expectWorkspaceDestination(homePath, label);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
expectWorkspaceDestination(destination, label);
|
|
123
|
+
}
|
|
124
|
+
export function expectWorkspaceDestination(destination, label) {
|
|
125
|
+
expectNonEmptyString(destination, label);
|
|
126
|
+
if (destination.includes("\0")) {
|
|
127
|
+
throw new ConfigError(`${label} contains a NUL byte`);
|
|
128
|
+
}
|
|
129
|
+
if (isAbsoluteLike(destination)) {
|
|
130
|
+
throw new ConfigError(`${label} must be relative to the workspace root`);
|
|
131
|
+
}
|
|
132
|
+
if (destination.split(/[\\/]+/).includes("..")) {
|
|
133
|
+
throw new ConfigError(`${label} must not contain ".."`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function assertRelativePath(relativePath, label) {
|
|
137
|
+
if (relativePath.includes("\0")) {
|
|
138
|
+
throw new ConfigError(`${label} contains a NUL byte`);
|
|
139
|
+
}
|
|
140
|
+
if (isAbsoluteLike(relativePath)) {
|
|
141
|
+
throw new ConfigError(`${label} must be relative to its configuration file`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function isAbsoluteLike(path) {
|
|
145
|
+
return path.startsWith("/") || path.startsWith("\\") || /^[A-Za-z]:[\\/]/.test(path);
|
|
146
|
+
}
|
|
147
|
+
function expectTimeout(value, label) {
|
|
148
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) {
|
|
149
|
+
throw new ConfigError(`${label} must be a positive integer number of milliseconds`);
|
|
150
|
+
}
|
|
151
|
+
return value;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Read the declared shell mocks: one target per bare command name or workspace-relative script
|
|
155
|
+
* path, each with one rule or an ordered list. Every pattern compiles at load time, so an
|
|
156
|
+
* unsupported RE2 construct or an unsupported shape is a configuration error rather than a
|
|
157
|
+
* silent fallback to the real command.
|
|
158
|
+
*/
|
|
159
|
+
function expectMocks(value, label) {
|
|
160
|
+
const record = expectRecord(value, label);
|
|
161
|
+
rejectUnknownKeys(record, MOCK_KEYS, label);
|
|
162
|
+
if (record.shell === undefined) {
|
|
163
|
+
throw new ConfigError(`${label} must declare shell targets`);
|
|
164
|
+
}
|
|
165
|
+
const shell = expectRecord(record.shell, `${label}.shell`);
|
|
166
|
+
const entries = Object.entries(shell).map(([target, rules]) => [expectMockTarget(target, `${label}.shell`), expectMockRules(rules, `${label}.shell["${target}"]`)]);
|
|
167
|
+
// `./scripts/x`, `scripts/.//x` and `scripts/x` name one workspace file, and a bare name and
|
|
168
|
+
// `./name` name different things: a PATH command and a workspace-root script.
|
|
169
|
+
const seen = new Map();
|
|
170
|
+
for (const [target] of entries) {
|
|
171
|
+
const identity = mockTargetIdentity(target);
|
|
172
|
+
const previous = seen.get(identity);
|
|
173
|
+
if (previous !== undefined) {
|
|
174
|
+
throw new ConfigError(`${label}.shell declares "${previous}" and "${target}" as the same target`);
|
|
175
|
+
}
|
|
176
|
+
seen.set(identity, target);
|
|
177
|
+
}
|
|
178
|
+
return { shell: Object.fromEntries(entries) };
|
|
179
|
+
}
|
|
180
|
+
/** The declared spelling two targets name the same thing by, for duplicates and checks. */
|
|
181
|
+
export function mockTargetIdentity(target) {
|
|
182
|
+
return target.includes("/") ? `workspace:${workspaceTargetPath(target)}` : `path:${target}`;
|
|
183
|
+
}
|
|
184
|
+
/** The workspace-relative file a target names, without `.` or empty segments. */
|
|
185
|
+
export function workspaceTargetPath(target) {
|
|
186
|
+
return target
|
|
187
|
+
.split("/")
|
|
188
|
+
.filter((segment) => segment.length > 0 && segment !== ".")
|
|
189
|
+
.join("/");
|
|
190
|
+
}
|
|
191
|
+
/** A bare command name is placed on `PATH`; a path containing `/` is workspace-relative. */
|
|
192
|
+
export function expectMockTarget(target, label) {
|
|
193
|
+
if (target.trim().length === 0) {
|
|
194
|
+
throw new ConfigError(`${label} has an empty target name`);
|
|
195
|
+
}
|
|
196
|
+
if (target.includes("\0")) {
|
|
197
|
+
throw new ConfigError(`${label} target "${target}" contains a NUL byte`);
|
|
198
|
+
}
|
|
199
|
+
if (target.includes("/")) {
|
|
200
|
+
expectWorkspaceDestination(target, `${label} target "${target}"`);
|
|
201
|
+
if (target.endsWith("/") || target.split("/").at(-1) === "") {
|
|
202
|
+
throw new ConfigError(`${label} target "${target}" must name a file, not a directory`);
|
|
203
|
+
}
|
|
204
|
+
return target;
|
|
205
|
+
}
|
|
206
|
+
if (target === "." || target === "..") {
|
|
207
|
+
throw new ConfigError(`${label} target "${target}" is not a command name`);
|
|
208
|
+
}
|
|
209
|
+
return target;
|
|
210
|
+
}
|
|
211
|
+
function expectMockRules(value, label) {
|
|
212
|
+
const rules = Array.isArray(value) ? value : [value];
|
|
213
|
+
if (rules.length === 0) {
|
|
214
|
+
throw new ConfigError(`${label} must declare at least one rule`);
|
|
215
|
+
}
|
|
216
|
+
return rules.map((rule, index) => expectMockRule(rule, Array.isArray(value) ? `${label}[${index}]` : label));
|
|
217
|
+
}
|
|
218
|
+
function expectMockRule(value, label) {
|
|
219
|
+
const rule = expectRecord(value, label);
|
|
220
|
+
rejectUnknownKeys(rule, MOCK_RULE_KEYS, label);
|
|
221
|
+
const args = rule.args === undefined ? undefined : expectMockArgs(rule.args, `${label}.args`);
|
|
222
|
+
const action = expectMockAction(rule, label);
|
|
223
|
+
return { ...(args === undefined ? {} : { args }), action };
|
|
224
|
+
}
|
|
225
|
+
function expectMockAction(rule, label) {
|
|
226
|
+
const declared = ["return", "run", "pass"].filter((key) => rule[key] !== undefined);
|
|
227
|
+
if (declared.length === 0) {
|
|
228
|
+
throw new ConfigError(`${label} must declare one of return, run or pass`);
|
|
229
|
+
}
|
|
230
|
+
if (rule.return !== undefined) {
|
|
231
|
+
if (declared.length > 1) {
|
|
232
|
+
throw new ConfigError(`${label} cannot combine return with ${declared.filter((key) => key !== "return").join(" and ")}`);
|
|
233
|
+
}
|
|
234
|
+
return { kind: "return", result: expectMockResult(rule.return, `${label}.return`) };
|
|
235
|
+
}
|
|
236
|
+
if (rule.run !== undefined) {
|
|
237
|
+
const pass = rule.pass === undefined ? false : expectMockPass(rule.pass, `${label}.pass`);
|
|
238
|
+
return { kind: "run", command: expectMockCommand(rule.run, `${label}.run`), pass };
|
|
239
|
+
}
|
|
240
|
+
return expectMockPass(rule.pass, `${label}.pass`) ? { kind: "pass" } : { kind: "refuse" };
|
|
241
|
+
}
|
|
242
|
+
/** `true` hands the call to the real command; `false` refuses it, or keeps `run` from passing. */
|
|
243
|
+
function expectMockPass(value, label) {
|
|
244
|
+
if (typeof value !== "boolean") {
|
|
245
|
+
throw new ConfigError(`${label} must be true or false`);
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
function expectMockCommand(value, label) {
|
|
250
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
251
|
+
throw new ConfigError(`${label} must be a non-empty list of program and arguments`);
|
|
252
|
+
}
|
|
253
|
+
return value.map((entry, index) => {
|
|
254
|
+
if (typeof entry !== "string" || entry.length === 0) {
|
|
255
|
+
throw new ConfigError(`${label}[${index}] must be a non-empty string`);
|
|
256
|
+
}
|
|
257
|
+
return entry;
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
export function expectMockArgs(value, label) {
|
|
261
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
262
|
+
return { positional: [], options: expectMockOptions(value, label) };
|
|
263
|
+
}
|
|
264
|
+
if (!Array.isArray(value)) {
|
|
265
|
+
throw new ConfigError(`${label} must be a list or a map`);
|
|
266
|
+
}
|
|
267
|
+
const positional = [];
|
|
268
|
+
for (const [index, entry] of value.entries()) {
|
|
269
|
+
if (typeof entry === "object" && entry !== null && !Array.isArray(entry)) {
|
|
270
|
+
if (index !== value.length - 1) {
|
|
271
|
+
throw new ConfigError(`${label} may hold an option map only as its last element`);
|
|
272
|
+
}
|
|
273
|
+
return { positional, options: expectMockOptions(entry, label) };
|
|
274
|
+
}
|
|
275
|
+
positional.push(expectMockPattern(entry, `${label}[${index}]`));
|
|
276
|
+
}
|
|
277
|
+
return { positional, options: [] };
|
|
278
|
+
}
|
|
279
|
+
function expectMockOptions(value, label) {
|
|
280
|
+
return Object.entries(value).map(([key, entry]) => {
|
|
281
|
+
if (key.length === 0) {
|
|
282
|
+
throw new ConfigError(`${label} has an empty option name`);
|
|
283
|
+
}
|
|
284
|
+
const pattern = expectMockPattern(key, `${label} option "${key}"`, true);
|
|
285
|
+
if (entry === true)
|
|
286
|
+
return { key: pattern, kind: "required" };
|
|
287
|
+
if (entry === false)
|
|
288
|
+
return { key: pattern, kind: "forbidden" };
|
|
289
|
+
if (typeof entry === "boolean") {
|
|
290
|
+
throw new ConfigError(`${label} option "${key}" must be true, false or a value`);
|
|
291
|
+
}
|
|
292
|
+
return { key: pattern, kind: "value", value: expectMockPattern(entry, `${label}["${key}"]`) };
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Read one pattern scalar, shared by mock rules and file checks. A finite number is compared as
|
|
297
|
+
* its text; a non-empty string that starts and ends with `/` is a RE2 pattern with the outer
|
|
298
|
+
* slashes removed; any other string is a literal. Callers own the labels, the wording of each
|
|
299
|
+
* error, and whether an empty `/…/` source is allowed.
|
|
300
|
+
*/
|
|
301
|
+
export function parsePatternScalar(value) {
|
|
302
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
303
|
+
return { kind: "pattern", pattern: { source: String(value), regex: false } };
|
|
304
|
+
}
|
|
305
|
+
if (typeof value === "string" && value.length === 0) {
|
|
306
|
+
return { kind: "invalid", reason: "empty" };
|
|
307
|
+
}
|
|
308
|
+
if (typeof value !== "string") {
|
|
309
|
+
return { kind: "invalid", reason: "unsupported" };
|
|
310
|
+
}
|
|
311
|
+
if (value.length < 2 || !value.startsWith("/") || !value.endsWith("/")) {
|
|
312
|
+
return { kind: "pattern", pattern: { source: value, regex: false } };
|
|
313
|
+
}
|
|
314
|
+
const source = value.slice(1, -1);
|
|
315
|
+
try {
|
|
316
|
+
compilePattern(source);
|
|
317
|
+
}
|
|
318
|
+
catch (error) {
|
|
319
|
+
return { kind: "invalid", reason: "regex", message: errorMessage(error) };
|
|
320
|
+
}
|
|
321
|
+
return { kind: "pattern", pattern: { source, regex: true } };
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Read one literal or /…/ pattern. A YAML number is compared as its text; an unquoted boolean
|
|
325
|
+
* is rejected because it would read as a required or forbidden option.
|
|
326
|
+
*/
|
|
327
|
+
function expectMockPattern(value, label, optionName = false) {
|
|
328
|
+
const parsed = parsePatternScalar(value);
|
|
329
|
+
if (parsed.kind === "pattern") {
|
|
330
|
+
return parsed.pattern;
|
|
331
|
+
}
|
|
332
|
+
if (parsed.reason === "regex") {
|
|
333
|
+
throw new ConfigError(`${label} is not a valid RE2 pattern: ${parsed.message}`);
|
|
334
|
+
}
|
|
335
|
+
throw new ConfigError(optionName
|
|
336
|
+
? `${label} must be a non-empty string`
|
|
337
|
+
: `${label} must be a non-empty string, number, or /…/ pattern; quote a boolean literal`);
|
|
338
|
+
}
|
|
339
|
+
function expectMockResult(value, label) {
|
|
340
|
+
const result = expectRecord(value, label);
|
|
341
|
+
rejectUnknownKeys(result, MOCK_RESULT_KEYS, label);
|
|
342
|
+
return {
|
|
343
|
+
exitCode: result.exitCode === undefined ? 0 : expectExitCode(result.exitCode, `${label}.exitCode`),
|
|
344
|
+
stdout: result.stdout === undefined ? "" : expectText(result.stdout, `${label}.stdout`),
|
|
345
|
+
stderr: result.stderr === undefined ? "" : expectText(result.stderr, `${label}.stderr`),
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
function expectExitCode(value, label) {
|
|
349
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || value > 255) {
|
|
350
|
+
throw new ConfigError(`${label} must be an integer from 0 to 255`);
|
|
351
|
+
}
|
|
352
|
+
return value;
|
|
353
|
+
}
|
|
354
|
+
function expectText(value, label) {
|
|
355
|
+
if (typeof value !== "string") {
|
|
356
|
+
throw new ConfigError(`${label} must be a string`);
|
|
357
|
+
}
|
|
358
|
+
return value;
|
|
359
|
+
}
|
|
360
|
+
export function errorMessage(error) {
|
|
361
|
+
return error instanceof Error ? error.message : String(error);
|
|
362
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Run-scoped access to `${env:NAME}` values from the invocation `.env`. */
|
|
2
|
+
export interface EnvSource {
|
|
3
|
+
/** Resolve one named value from the invocation `.env`, caching it per run. */
|
|
4
|
+
require(name: string): Promise<string>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Create a lazy environment-value source rooted at the invocation working directory.
|
|
8
|
+
* The `.env` file is read only when a value is actually requested.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createEnvSource(directory: string, signal?: AbortSignal): EnvSource;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { parseEnv } from "node:util";
|
|
5
|
+
import { ConfigError, errorMessage } from "./config.js";
|
|
6
|
+
const COMMAND_TIMEOUT_MS = 5_000;
|
|
7
|
+
const COMMAND_MAX_OUTPUT_BYTES = 64 * 1024;
|
|
8
|
+
/**
|
|
9
|
+
* Create a lazy environment-value source rooted at the invocation working directory.
|
|
10
|
+
* The `.env` file is read only when a value is actually requested.
|
|
11
|
+
*/
|
|
12
|
+
export function createEnvSource(directory, signal) {
|
|
13
|
+
const file = join(directory, ".env");
|
|
14
|
+
const cache = new Map();
|
|
15
|
+
let valuesPromise;
|
|
16
|
+
const values = () => (valuesPromise ??= (async () => {
|
|
17
|
+
try {
|
|
18
|
+
return parseEnv(await readFile(file, "utf8"));
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw new ConfigError(`could not load environment file ${file}: ${errorMessage(error)}`);
|
|
22
|
+
}
|
|
23
|
+
})());
|
|
24
|
+
return {
|
|
25
|
+
require(name) {
|
|
26
|
+
const cached = cache.get(name);
|
|
27
|
+
if (cached !== undefined) {
|
|
28
|
+
return cached;
|
|
29
|
+
}
|
|
30
|
+
const pending = (async () => {
|
|
31
|
+
if (signal?.aborted === true) {
|
|
32
|
+
throw new ConfigError(`environment lookup for ${name} was cancelled`);
|
|
33
|
+
}
|
|
34
|
+
let parsed;
|
|
35
|
+
try {
|
|
36
|
+
parsed = await values();
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new ConfigError(`environment value ${name} is unavailable: ${errorMessage(error)}`);
|
|
40
|
+
}
|
|
41
|
+
if (!Object.hasOwn(parsed, name)) {
|
|
42
|
+
throw new ConfigError(`environment value ${name} is not defined in ${file}`);
|
|
43
|
+
}
|
|
44
|
+
const value = parsed[name] ?? "";
|
|
45
|
+
if (value.length === 0) {
|
|
46
|
+
throw new ConfigError(`environment value ${name} is empty in ${file}`);
|
|
47
|
+
}
|
|
48
|
+
if (!value.startsWith("!")) {
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
const command = value.slice(1).trim();
|
|
52
|
+
if (command.length === 0) {
|
|
53
|
+
throw new ConfigError(`environment value ${name} has an empty lookup command in ${file}`);
|
|
54
|
+
}
|
|
55
|
+
return runLookupCommand(name, command, directory, signal);
|
|
56
|
+
})();
|
|
57
|
+
cache.set(name, pending);
|
|
58
|
+
return pending;
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
async function runLookupCommand(name, command, cwd, signal) {
|
|
63
|
+
if (signal?.aborted === true) {
|
|
64
|
+
throw new ConfigError(`environment lookup for ${name} was cancelled`);
|
|
65
|
+
}
|
|
66
|
+
let stdout;
|
|
67
|
+
try {
|
|
68
|
+
stdout = await new Promise((resolve, reject) => {
|
|
69
|
+
execFile("/bin/sh", ["-c", command], {
|
|
70
|
+
cwd,
|
|
71
|
+
env: process.env,
|
|
72
|
+
encoding: "utf8",
|
|
73
|
+
timeout: COMMAND_TIMEOUT_MS,
|
|
74
|
+
maxBuffer: COMMAND_MAX_OUTPUT_BYTES,
|
|
75
|
+
signal,
|
|
76
|
+
killSignal: "SIGKILL",
|
|
77
|
+
}, (error, output) => {
|
|
78
|
+
if (error === null) {
|
|
79
|
+
resolve(output);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
reject(error);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
throw classifyCommandError(name, error, signal);
|
|
89
|
+
}
|
|
90
|
+
const value = stdout.trim();
|
|
91
|
+
if (value.length === 0) {
|
|
92
|
+
throw new ConfigError(`environment lookup for ${name} produced no value`);
|
|
93
|
+
}
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
function classifyCommandError(name, error, signal) {
|
|
97
|
+
const record = typeof error === "object" && error !== null ? error : {};
|
|
98
|
+
if (signal?.aborted === true || record.code === "ABORT_ERR" || record.name === "AbortError") {
|
|
99
|
+
return new ConfigError(`environment lookup for ${name} was cancelled`);
|
|
100
|
+
}
|
|
101
|
+
if (record.code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER") {
|
|
102
|
+
return new ConfigError(`environment lookup for ${name} exceeded ${COMMAND_MAX_OUTPUT_BYTES} bytes`);
|
|
103
|
+
}
|
|
104
|
+
if (record.killed === true) {
|
|
105
|
+
return new ConfigError(`environment lookup for ${name} timed out after ${COMMAND_TIMEOUT_MS}ms`);
|
|
106
|
+
}
|
|
107
|
+
if (typeof record.code === "number") {
|
|
108
|
+
return new ConfigError(`environment lookup for ${name} exited with code ${String(record.code)}`);
|
|
109
|
+
}
|
|
110
|
+
if (typeof record.signal === "string") {
|
|
111
|
+
return new ConfigError(`environment lookup for ${name} exited by signal ${record.signal}`);
|
|
112
|
+
}
|
|
113
|
+
return new ConfigError(`could not run environment lookup for ${name}${typeof record.code === "string" ? ` (${record.code})` : ""}`);
|
|
114
|
+
}
|