@lelouchhe/agent-evals 0.0.0-stage → 0.2.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/.env.example +27 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/dist/checkers/file/index.d.ts +26 -0
- package/dist/checkers/file/index.js +396 -0
- package/dist/checkers/judge/index.d.ts +20 -0
- package/dist/checkers/judge/index.js +474 -0
- package/dist/checkers/judge/prompt.d.ts +19 -0
- package/dist/checkers/judge/prompt.js +55 -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 +453 -0
- package/dist/engine/authoring.d.ts +24 -0
- package/dist/engine/authoring.js +356 -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 +171 -0
- package/dist/engine/config.js +368 -0
- package/dist/engine/env-source.d.ts +10 -0
- package/dist/engine/env-source.js +114 -0
- package/dist/engine/files.d.ts +84 -0
- package/dist/engine/files.js +304 -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 +18 -0
- package/dist/engine/params.js +96 -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 +243 -0
- package/dist/engine/registry.js +127 -0
- package/dist/engine/report.d.ts +17 -0
- package/dist/engine/report.js +420 -0
- package/dist/engine/results.d.ts +50 -0
- package/dist/engine/results.js +42 -0
- package/dist/engine/run.d.ts +50 -0
- package/dist/engine/run.js +710 -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 +150 -0
- package/dist/engine/workspace.d.ts +63 -0
- package/dist/engine/workspace.js +305 -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 +135 -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,17 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/** Set by ACP setup for every agent process; a declaration cannot replace them. */
|
|
3
|
+
export declare const RESERVED_VARIABLES: ReadonlySet<string>;
|
|
4
|
+
/** Shared by standalone helper input and the ACP runner's inline agent object. */
|
|
5
|
+
export declare const agentSchema: z.ZodObject<{
|
|
6
|
+
acpCommand: z.ZodTuple<[z.ZodString], z.ZodString>;
|
|
7
|
+
environment: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
8
|
+
deps: z.ZodString;
|
|
9
|
+
path: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
10
|
+
files: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
11
|
+
login: z.ZodOptional<z.ZodObject<{
|
|
12
|
+
command: z.ZodTuple<[z.ZodString], z.ZodString>;
|
|
13
|
+
dir: z.ZodString;
|
|
14
|
+
files: z.ZodArray<z.ZodString>;
|
|
15
|
+
}, z.core.$strict>>;
|
|
16
|
+
}, z.core.$strict>;
|
|
17
|
+
export type AgentConfig = z.output<typeof agentSchema>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ENV_NAME_PATTERN } from "../../engine/config.js";
|
|
3
|
+
import { runRelativePathSchema } from "../../engine/params.js";
|
|
4
|
+
const argument = z.string().min(1);
|
|
5
|
+
const command = z.tuple([argument]).rest(argument);
|
|
6
|
+
/** Set by ACP setup for every agent process; a declaration cannot replace them. */
|
|
7
|
+
export const RESERVED_VARIABLES = new Set(["HOME", "TMPDIR", "PATH", "TERM"]);
|
|
8
|
+
/** Persistent login state: one native command, one directory, the files that compose it. */
|
|
9
|
+
const loginSchema = z.strictObject({
|
|
10
|
+
command,
|
|
11
|
+
dir: argument,
|
|
12
|
+
files: z.array(runRelativePathSchema).min(1, "must list at least one login file"),
|
|
13
|
+
});
|
|
14
|
+
/** Shared by standalone helper input and the ACP runner's inline agent object. */
|
|
15
|
+
export const agentSchema = z.strictObject({
|
|
16
|
+
acpCommand: command,
|
|
17
|
+
/** Extra variables for both commands, after placeholder substitution. */
|
|
18
|
+
environment: z
|
|
19
|
+
.record(z.string(), z.string())
|
|
20
|
+
.superRefine((environment, context) => {
|
|
21
|
+
for (const name of Object.keys(environment)) {
|
|
22
|
+
if (!ENV_NAME_PATTERN.test(name)) {
|
|
23
|
+
context.addIssue({ code: "custom", path: [name], message: "must be a variable name" });
|
|
24
|
+
}
|
|
25
|
+
else if (RESERVED_VARIABLES.has(name)) {
|
|
26
|
+
context.addIssue({ code: "custom", path: [name], message: "is set by ACP setup" });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
.default({}),
|
|
31
|
+
/** Caller-prepared software, linked into the run as `deps`. */
|
|
32
|
+
deps: argument,
|
|
33
|
+
/** `deps`-relative command directories; the caller owns the software layout. */
|
|
34
|
+
path: z.array(runRelativePathSchema).default(["bin"]),
|
|
35
|
+
/** What the agent file places in HOME: `~/` destination to authoring source. */
|
|
36
|
+
files: z.record(z.string(), z.string()).optional(),
|
|
37
|
+
login: loginSchema.optional(),
|
|
38
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { mkdir, realpath, rm } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { stringify } from "yaml";
|
|
6
|
+
import { loadImplementationParameters } from "../../engine/authoring.js";
|
|
7
|
+
import { ConfigError, errorMessage } from "../../engine/config.js";
|
|
8
|
+
import { createEnvSource } from "../../engine/env-source.js";
|
|
9
|
+
import { spawnSupervised } from "../../platform/macos/supervise.js";
|
|
10
|
+
import { agentSchema } from "./agent.js";
|
|
11
|
+
import { harnessEnvironment, prepareHarnessHome, resolveHarnessHome, saveLogin } from "./environment.js";
|
|
12
|
+
import { withSession } from "./session.js";
|
|
13
|
+
/** Helpers share the same typed agent configuration and isolated lifecycle. */
|
|
14
|
+
export async function runAgentCommand(command, path, signal) {
|
|
15
|
+
// Placeholders need the directories' paths; they are created only after validation. The
|
|
16
|
+
// parent is canonical so the sandbox authorizes traversal through the `deps` link.
|
|
17
|
+
const directory = join(await realpath(tmpdir()), `ae-acp-${randomBytes(6).toString("hex")}`);
|
|
18
|
+
const dirs = {
|
|
19
|
+
home: join(directory, "home"),
|
|
20
|
+
workspace: join(directory, "workspace"),
|
|
21
|
+
tmp: join(directory, "tmp"),
|
|
22
|
+
engine: join(directory, "engine"),
|
|
23
|
+
};
|
|
24
|
+
const { params: agent, dir } = await loadImplementationParameters(path, agentSchema, {
|
|
25
|
+
env: createEnvSource(process.cwd(), signal),
|
|
26
|
+
run: { HOME: dirs.home, TMPDIR: dirs.tmp },
|
|
27
|
+
});
|
|
28
|
+
if (command === "login" && agent.login === undefined) {
|
|
29
|
+
throw new ConfigError("login requires login.command, login.dir and login.files");
|
|
30
|
+
}
|
|
31
|
+
signal.throwIfAborted();
|
|
32
|
+
const home = await resolveHarnessHome(agent, dir);
|
|
33
|
+
// Exclusive creation, like mkdtemp: an existing path is an error, not reused.
|
|
34
|
+
await mkdir(directory, { mode: 0o700 });
|
|
35
|
+
const failures = [];
|
|
36
|
+
let output = "";
|
|
37
|
+
let prepared;
|
|
38
|
+
let environment;
|
|
39
|
+
let supervised;
|
|
40
|
+
try {
|
|
41
|
+
await Promise.all(Object.values(dirs).map((path) => mkdir(path, { mode: 0o700 })));
|
|
42
|
+
prepared = await prepareHarnessHome(home, {
|
|
43
|
+
home: dirs.home,
|
|
44
|
+
runRootDir: directory,
|
|
45
|
+
engineDir: dirs.engine,
|
|
46
|
+
});
|
|
47
|
+
environment = await harnessEnvironment(home, { home: dirs.home, workspace: dirs.workspace, tmp: dirs.tmp, runRootDir: directory }, { interactive: command === "login" });
|
|
48
|
+
const operationSignal = command === "config" ? AbortSignal.any([signal, AbortSignal.timeout(30_000)]) : signal;
|
|
49
|
+
operationSignal.throwIfAborted();
|
|
50
|
+
const process = await spawnSupervised({
|
|
51
|
+
command: command === "config" ? agent.acpCommand : agent.login.command,
|
|
52
|
+
policy: environment.policy,
|
|
53
|
+
policyDirectory: dirs.engine,
|
|
54
|
+
cwd: dirs.workspace,
|
|
55
|
+
env: environment.env,
|
|
56
|
+
interactive: command === "login",
|
|
57
|
+
});
|
|
58
|
+
supervised = process;
|
|
59
|
+
let onAbort = () => { };
|
|
60
|
+
const cancelled = new Promise((_resolve, reject) => {
|
|
61
|
+
onAbort = () => reject(new Error(operationSignal.reason?.name === "TimeoutError"
|
|
62
|
+
? "ACP configuration query timed out after 30000ms"
|
|
63
|
+
: `${command} cancelled`));
|
|
64
|
+
operationSignal.addEventListener("abort", onAbort, { once: true });
|
|
65
|
+
if (operationSignal.aborted)
|
|
66
|
+
onAbort();
|
|
67
|
+
});
|
|
68
|
+
try {
|
|
69
|
+
const work = async () => {
|
|
70
|
+
if (command === "login") {
|
|
71
|
+
const code = await process.completion;
|
|
72
|
+
operationSignal.throwIfAborted();
|
|
73
|
+
if (code !== 0)
|
|
74
|
+
throw new Error(`login command exited with code ${code}`);
|
|
75
|
+
return "Login files saved.\n";
|
|
76
|
+
}
|
|
77
|
+
return withSession(process, dirs.workspace, () => { }, async (_agent, initialized, session) => stringify({ authMethods: initialized.authMethods ?? [], configOptions: session.configOptions ?? [] }));
|
|
78
|
+
};
|
|
79
|
+
output = await Promise.race([work(), cancelled]);
|
|
80
|
+
operationSignal.throwIfAborted();
|
|
81
|
+
if (command === "login")
|
|
82
|
+
await saveLogin(home, prepared.homeRoot, agent.login.files);
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
operationSignal.removeEventListener("abort", onAbort);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
failures.push(error);
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
if (supervised !== undefined) {
|
|
93
|
+
await supervised.stop();
|
|
94
|
+
await prepared.copyBack();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
failures.push(new Error(`cleanup failed: ${errorMessage(error)}`, { cause: error }));
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
await rm(directory, { recursive: true, force: true });
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
failures.push(new Error(`cleanup failed: ${errorMessage(error)}`, { cause: error }));
|
|
105
|
+
}
|
|
106
|
+
if (failures.length === 1)
|
|
107
|
+
throw failures[0];
|
|
108
|
+
if (failures.length > 1) {
|
|
109
|
+
throw new AggregateError(failures, failures.map(errorMessage).join("; "), { cause: failures[0] });
|
|
110
|
+
}
|
|
111
|
+
return output;
|
|
112
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { MockWiring } from "../../engine/registry.js";
|
|
2
|
+
import { type RunRoot } from "../../engine/workspace.js";
|
|
3
|
+
import { createSandboxPolicy } from "../../platform/macos/policy.js";
|
|
4
|
+
import type { AgentConfig } from "./agent.js";
|
|
5
|
+
/** Persistent login state: where it lives, which files compose it, and how to obtain it. */
|
|
6
|
+
export interface HarnessLogin {
|
|
7
|
+
/** Native program the login helper runs in the terminal. */
|
|
8
|
+
readonly command: readonly [string, ...string[]];
|
|
9
|
+
/** Canonical persistent directory; it may not exist before the first login. */
|
|
10
|
+
readonly dir: string;
|
|
11
|
+
/** HOME-relative files copied into the run and saved back after the sandbox stops. */
|
|
12
|
+
readonly files: readonly string[];
|
|
13
|
+
/** The directory as an opened run root, present only when it exists. */
|
|
14
|
+
readonly root?: RunRoot;
|
|
15
|
+
}
|
|
16
|
+
/** One resolved agent file: the caller-owned parts of a harness home. */
|
|
17
|
+
export interface HarnessHome {
|
|
18
|
+
readonly declaringDir: string;
|
|
19
|
+
readonly environment: Readonly<Record<string, string>>;
|
|
20
|
+
/** `deps`-relative command directories to put on `PATH`. */
|
|
21
|
+
readonly path: readonly string[];
|
|
22
|
+
/** Canonical caller-owned software directory, linked into the run as `deps`. */
|
|
23
|
+
readonly deps: string;
|
|
24
|
+
/** `~/` destinations and their authoring sources, validated but not placed. */
|
|
25
|
+
readonly files: Readonly<Record<string, string>>;
|
|
26
|
+
readonly login?: HarnessLogin;
|
|
27
|
+
}
|
|
28
|
+
/** Validate the caller-owned software, HOME content and login state without creating run state. */
|
|
29
|
+
export declare function resolveHarnessHome(agent: AgentConfig, declaringDir: string): Promise<HarnessHome>;
|
|
30
|
+
export interface HarnessHomeDirs {
|
|
31
|
+
/** The run HOME, already created by the engine. */
|
|
32
|
+
readonly home: string;
|
|
33
|
+
readonly runRootDir: string;
|
|
34
|
+
/** Engine-private directory for placement staging; unreadable to the agent. */
|
|
35
|
+
readonly engineDir: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Assemble the caller-owned HOME content in the run directory: link `deps` at the run root,
|
|
39
|
+
* copy the agent file's `files`, then copy the declared login state. The returned paths are
|
|
40
|
+
* login files the case may not touch.
|
|
41
|
+
*/
|
|
42
|
+
export declare function prepareHarnessHome(source: HarnessHome, dirs: HarnessHomeDirs, signal?: AbortSignal): Promise<{
|
|
43
|
+
readonly homeRoot: RunRoot;
|
|
44
|
+
readonly protectedPaths: readonly string[];
|
|
45
|
+
readonly copyBack: () => Promise<void>;
|
|
46
|
+
}>;
|
|
47
|
+
/**
|
|
48
|
+
* The process environment and sandbox policy for one harness run: `PATH` goes through the
|
|
49
|
+
* run-root `deps` link, writes stay in the run's writable directories, and `engine/` is absent
|
|
50
|
+
* from every readable root.
|
|
51
|
+
*/
|
|
52
|
+
export declare function harnessEnvironment(source: HarnessHome, dirs: {
|
|
53
|
+
readonly home: string;
|
|
54
|
+
readonly workspace: string;
|
|
55
|
+
readonly tmp: string;
|
|
56
|
+
readonly runRootDir: string;
|
|
57
|
+
}, options: {
|
|
58
|
+
readonly interactive: boolean;
|
|
59
|
+
readonly mocks?: MockWiring;
|
|
60
|
+
}): Promise<{
|
|
61
|
+
readonly env: NodeJS.ProcessEnv;
|
|
62
|
+
readonly policy: ReturnType<typeof createSandboxPolicy>;
|
|
63
|
+
}>;
|
|
64
|
+
/** Save declared login files after a successful native login. */
|
|
65
|
+
export declare function saveLogin(source: HarnessHome, homeRoot: RunRoot, selected: readonly string[]): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* The real binaries behind PATH targets whose rules never pass. Besides the first PATH match,
|
|
68
|
+
* the active developer directory is included because `/usr/bin/make` and `/usr/bin/git` are
|
|
69
|
+
* shims that start the toolchain's own copy.
|
|
70
|
+
*/
|
|
71
|
+
export declare function deniedBinaries(targets: readonly string[], pathEntries: readonly string[], developerDir: string | undefined): Promise<readonly string[]>;
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { lstat, mkdir, open, realpath, rename, rm, symlink, stat, writeFile } from "node:fs/promises";
|
|
6
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
7
|
+
import { ConfigError, errorMessage, expectPlacementDestination, expectRelativePath } from "../../engine/config.js";
|
|
8
|
+
import { copyPlacement, homePathOf, isHomeDestination, placedPaths, stageEntries } from "../../engine/files.js";
|
|
9
|
+
import { isMissing, isWithin, openRunRoot, resolveWorkspacePath } from "../../engine/workspace.js";
|
|
10
|
+
import { assertSeatbeltAvailable, createSandboxPolicy, SYSTEM_PATH } from "../../platform/macos/policy.js";
|
|
11
|
+
/** Validate the caller-owned software, HOME content and login state without creating run state. */
|
|
12
|
+
export async function resolveHarnessHome(agent, declaringDir) {
|
|
13
|
+
assertSeatbeltAvailable();
|
|
14
|
+
let deps;
|
|
15
|
+
try {
|
|
16
|
+
deps = (await openRunRoot(resolve(declaringDir, agent.deps))).path;
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
throw new ConfigError(`deps must be an accessible directory: ${errorMessage(error)}`);
|
|
20
|
+
}
|
|
21
|
+
const files = agent.files ?? {};
|
|
22
|
+
for (const [destination, source] of Object.entries(files)) {
|
|
23
|
+
expectPlacementDestination(destination, `agent files destination "${destination}"`);
|
|
24
|
+
if (!isHomeDestination(destination)) {
|
|
25
|
+
throw new ConfigError(`agent files destination "${destination}" must start with "~/"`);
|
|
26
|
+
}
|
|
27
|
+
expectRelativePath(source, `agent files["${destination}"]`);
|
|
28
|
+
if (homePathOf(destination) === "") {
|
|
29
|
+
throw new ConfigError(`agent files destination "${destination}" must name a path inside HOME, not HOME itself`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
let login;
|
|
33
|
+
if (agent.login !== undefined) {
|
|
34
|
+
let directory;
|
|
35
|
+
try {
|
|
36
|
+
directory = await canonicalPath(resolve(declaringDir, agent.login.dir));
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new ConfigError(`login.dir must be a directory: ${errorMessage(error)}`);
|
|
40
|
+
}
|
|
41
|
+
let loginRoot;
|
|
42
|
+
try {
|
|
43
|
+
loginRoot = await openRunRoot(directory);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
// Only a missing directory is valid for first login, not ENOTDIR.
|
|
47
|
+
if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) {
|
|
48
|
+
throw new ConfigError(`login.dir must be a directory: ${errorMessage(error)}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (isWithin(deps, directory) || isWithin(directory, deps)) {
|
|
52
|
+
throw new ConfigError("deps and login.dir must be separate, non-overlapping directories");
|
|
53
|
+
}
|
|
54
|
+
login = {
|
|
55
|
+
command: agent.login.command,
|
|
56
|
+
dir: directory,
|
|
57
|
+
files: agent.login.files,
|
|
58
|
+
...(loginRoot === undefined ? {} : { root: loginRoot }),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
// Every agent placement is read now, so a missing source or a directory whose contents name a
|
|
62
|
+
// login file is a configuration error before any run resource exists, not a failed run.
|
|
63
|
+
const entries = Object.entries(files).map(([destination, source]) => ({
|
|
64
|
+
destination,
|
|
65
|
+
target: homePathOf(destination),
|
|
66
|
+
source,
|
|
67
|
+
}));
|
|
68
|
+
let placed;
|
|
69
|
+
try {
|
|
70
|
+
placed = await placedPaths(entries, declaringDir, "agent files");
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
throw new ConfigError(`agent files: ${errorMessage(error)}`, { cause: error });
|
|
74
|
+
}
|
|
75
|
+
for (const file of login?.files ?? []) {
|
|
76
|
+
const collision = placed.find((target) => target === file || target.startsWith(`${file}/`));
|
|
77
|
+
if (collision !== undefined) {
|
|
78
|
+
throw new ConfigError(`agent files place ${collision}, which collides with login file ${file}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
declaringDir,
|
|
83
|
+
environment: agent.environment,
|
|
84
|
+
path: agent.path,
|
|
85
|
+
deps,
|
|
86
|
+
files,
|
|
87
|
+
...(login === undefined ? {} : { login }),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Assemble the caller-owned HOME content in the run directory: link `deps` at the run root,
|
|
92
|
+
* copy the agent file's `files`, then copy the declared login state. The returned paths are
|
|
93
|
+
* login files the case may not touch.
|
|
94
|
+
*/
|
|
95
|
+
export async function prepareHarnessHome(source, dirs, signal) {
|
|
96
|
+
const homeRoot = await openRunRoot(dirs.home);
|
|
97
|
+
if (isWithin(source.deps, homeRoot.path)) {
|
|
98
|
+
throw new ConfigError("deps must not contain the temporary run directory");
|
|
99
|
+
}
|
|
100
|
+
// One directory symlink: the caller's software, referenced in place. The target must be the
|
|
101
|
+
// canonical path so the sandbox authorizes traversal through the link.
|
|
102
|
+
await symlink(source.deps, join(dirs.runRootDir, "deps"), "dir");
|
|
103
|
+
const placements = Object.entries(source.files).map(([destination, file]) => ({
|
|
104
|
+
destination,
|
|
105
|
+
target: homePathOf(destination),
|
|
106
|
+
source: file,
|
|
107
|
+
}));
|
|
108
|
+
if (placements.length > 0) {
|
|
109
|
+
const staging = join(dirs.engineDir, "files-agent");
|
|
110
|
+
try {
|
|
111
|
+
await stageEntries(placements, source.declaringDir, staging, "agent files", signal);
|
|
112
|
+
await copyPlacement(staging, homeRoot.path, { label: "agent files", overwrite: false, signal });
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
await rm(staging, { recursive: true, force: true });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const originals = new Map();
|
|
119
|
+
const login = source.login;
|
|
120
|
+
if (login?.root !== undefined) {
|
|
121
|
+
for (const file of login.files) {
|
|
122
|
+
const destination = await resolveWorkspacePath(homeRoot, file);
|
|
123
|
+
let data;
|
|
124
|
+
try {
|
|
125
|
+
data = await readRegular(login.root, file);
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
if (isMissing(error))
|
|
129
|
+
continue;
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
await lstat(destination);
|
|
134
|
+
throw new ConfigError(`agent files and login contain the same file: ${file}`);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
if (!isMissing(error))
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
await mkdir(dirname(destination), { recursive: true, mode: 0o700 });
|
|
141
|
+
await writeFile(destination, data, { mode: 0o600, flag: "wx" });
|
|
142
|
+
originals.set(file, data);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const copyBack = async () => {
|
|
146
|
+
const changed = new Map();
|
|
147
|
+
for (const [file, before] of originals) {
|
|
148
|
+
try {
|
|
149
|
+
const after = await readRegular(homeRoot, file);
|
|
150
|
+
if (!before.equals(after))
|
|
151
|
+
changed.set(file, after);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
if (!isMissing(error))
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (changed.size > 0)
|
|
159
|
+
await persistLogin(login, changed);
|
|
160
|
+
};
|
|
161
|
+
return { homeRoot, protectedPaths: login?.files ?? [], copyBack };
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The process environment and sandbox policy for one harness run: `PATH` goes through the
|
|
165
|
+
* run-root `deps` link, writes stay in the run's writable directories, and `engine/` is absent
|
|
166
|
+
* from every readable root.
|
|
167
|
+
*/
|
|
168
|
+
export async function harnessEnvironment(source, dirs, options) {
|
|
169
|
+
const mocks = options.mocks;
|
|
170
|
+
const depsLink = join(dirs.runRootDir, "deps");
|
|
171
|
+
const pathEntries = [...source.path.map((path) => join(depsLink, path)), ...SYSTEM_PATH];
|
|
172
|
+
const policy = createSandboxPolicy({
|
|
173
|
+
writableRoots: [dirs.home, dirs.workspace, dirs.tmp],
|
|
174
|
+
// A mocked command's forwarder is readable and executable, never writable.
|
|
175
|
+
readableRoots: mocks === undefined ? [depsLink] : [depsLink, mocks.forwarderDir],
|
|
176
|
+
denyExecutables: mocks === undefined ? [] : await deniedBinaries(mocks.denyTargets, pathEntries, await activeDeveloperDir()),
|
|
177
|
+
allowNetwork: true,
|
|
178
|
+
interactive: options.interactive,
|
|
179
|
+
});
|
|
180
|
+
const env = {
|
|
181
|
+
...source.environment,
|
|
182
|
+
...(process.env.TERM === undefined ? {} : { TERM: process.env.TERM }),
|
|
183
|
+
HOME: dirs.home,
|
|
184
|
+
TMPDIR: dirs.tmp,
|
|
185
|
+
PATH: [...(mocks === undefined ? [] : [mocks.forwarderDir]), ...pathEntries].join(":"),
|
|
186
|
+
};
|
|
187
|
+
return { env, policy };
|
|
188
|
+
}
|
|
189
|
+
/** Save declared login files after a successful native login. */
|
|
190
|
+
export async function saveLogin(source, homeRoot, selected) {
|
|
191
|
+
const contents = new Map();
|
|
192
|
+
for (const file of selected) {
|
|
193
|
+
try {
|
|
194
|
+
contents.set(file, await readRegular(homeRoot, file));
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
// A declared file the harness did not create on this first login is simply absent.
|
|
198
|
+
if (!isMissing(error))
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
await persistLogin(source.login, contents);
|
|
203
|
+
}
|
|
204
|
+
async function persistLogin(login, selected) {
|
|
205
|
+
if (login === undefined)
|
|
206
|
+
throw new ConfigError("login requires a login declaration");
|
|
207
|
+
await mkdir(login.dir, { recursive: true, mode: 0o700 });
|
|
208
|
+
const targetRoot = await openRunRoot(login.dir);
|
|
209
|
+
for (const [file, data] of selected) {
|
|
210
|
+
const target = await resolveWorkspacePath(targetRoot, file);
|
|
211
|
+
await mkdir(dirname(target), { recursive: true, mode: 0o700 });
|
|
212
|
+
const temporary = join(dirname(target), `.agent-evals-${randomUUID()}`);
|
|
213
|
+
try {
|
|
214
|
+
await writeFile(temporary, data, { flag: "wx", mode: 0o600 });
|
|
215
|
+
await rename(temporary, target);
|
|
216
|
+
}
|
|
217
|
+
finally {
|
|
218
|
+
await rm(temporary, { force: true });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The real binaries behind PATH targets whose rules never pass. Besides the first PATH match,
|
|
224
|
+
* the active developer directory is included because `/usr/bin/make` and `/usr/bin/git` are
|
|
225
|
+
* shims that start the toolchain's own copy.
|
|
226
|
+
*/
|
|
227
|
+
export async function deniedBinaries(targets, pathEntries, developerDir) {
|
|
228
|
+
const denied = [];
|
|
229
|
+
for (const target of targets) {
|
|
230
|
+
const candidates = [
|
|
231
|
+
...pathEntries.map((entry) => join(entry, target)),
|
|
232
|
+
...(developerDir === undefined ? [] : [join(developerDir, "usr", "bin", target)]),
|
|
233
|
+
];
|
|
234
|
+
for (const candidate of candidates) {
|
|
235
|
+
try {
|
|
236
|
+
if ((await stat(candidate)).isFile())
|
|
237
|
+
denied.push(candidate, await realpath(candidate));
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
// Not installed here.
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return denied;
|
|
245
|
+
}
|
|
246
|
+
/** The directory the platform shims start tools from, when a developer directory is selected. */
|
|
247
|
+
async function activeDeveloperDir() {
|
|
248
|
+
try {
|
|
249
|
+
const { stdout } = await promisify(execFile)("/usr/bin/xcode-select", ["-p"]);
|
|
250
|
+
const path = stdout.trim();
|
|
251
|
+
return path.length === 0 ? undefined : path;
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/** Canonicalize existing ancestors too, without creating a first-login directory. */
|
|
258
|
+
async function canonicalPath(path) {
|
|
259
|
+
try {
|
|
260
|
+
return await realpath(path);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
if (!isMissing(error))
|
|
264
|
+
throw error;
|
|
265
|
+
return join(await canonicalPath(dirname(path)), basename(path));
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
async function readRegular(root, file) {
|
|
269
|
+
const path = await resolveWorkspacePath(root, file);
|
|
270
|
+
// NONBLOCK avoids opening a FIFO being substituted for a credential file.
|
|
271
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
272
|
+
try {
|
|
273
|
+
if (!(await handle.stat()).isFile())
|
|
274
|
+
throw new ConfigError(`login path is not a regular file: ${file}`);
|
|
275
|
+
return await handle.readFile();
|
|
276
|
+
}
|
|
277
|
+
finally {
|
|
278
|
+
await handle.close();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { RunnerDefinition } from "../../engine/registry.js";
|
|
3
|
+
declare const acpRunnerParamsSchema: z.ZodObject<{
|
|
4
|
+
agent: z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
|
|
5
|
+
acpCommand: z.ZodTuple<[z.ZodString], z.ZodString>;
|
|
6
|
+
environment: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
7
|
+
deps: z.ZodString;
|
|
8
|
+
path: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
9
|
+
files: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
10
|
+
login: z.ZodOptional<z.ZodObject<{
|
|
11
|
+
command: z.ZodTuple<[z.ZodString], z.ZodString>;
|
|
12
|
+
dir: z.ZodString;
|
|
13
|
+
files: z.ZodArray<z.ZodString>;
|
|
14
|
+
}, z.core.$strict>>;
|
|
15
|
+
}, z.core.$strict>]>;
|
|
16
|
+
config: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>>;
|
|
17
|
+
}, z.core.$strict>;
|
|
18
|
+
type AcpRunnerParams = z.output<typeof acpRunnerParamsSchema>;
|
|
19
|
+
/** Generic ACP runner: one prompt turn in a caller-prepared harness home under Seatbelt. */
|
|
20
|
+
export declare const acpRunner: RunnerDefinition<AcpRunnerParams>;
|
|
21
|
+
export {};
|