@lelouchhe/agent-evals 0.2.0 → 0.3.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/README.md +25 -20
- package/dist/cli.js +39 -4
- package/dist/engine/check-runner.js +18 -3
- package/dist/engine/check-sandbox.js +9 -6
- package/dist/engine/env-source.js +82 -14
- package/dist/engine/files.js +2 -1
- package/dist/engine/mocks.js +72 -46
- package/dist/engine/registry.d.ts +2 -0
- package/dist/engine/run.js +3 -4
- package/dist/engine/skills.js +12 -5
- package/dist/engine/view.js +6 -3
- package/dist/engine/workspace.js +13 -28
- package/dist/platform/index.d.ts +31 -0
- package/dist/platform/index.js +88 -0
- package/dist/platform/linux/index.d.ts +2 -0
- package/dist/platform/linux/index.js +40 -0
- package/dist/platform/linux/launch.d.ts +8 -0
- package/dist/platform/linux/launch.js +25 -0
- package/dist/platform/linux/policy.d.ts +46 -0
- package/dist/platform/linux/policy.js +388 -0
- package/dist/platform/linux/supervise.d.ts +8 -0
- package/dist/platform/linux/supervise.js +124 -0
- package/dist/platform/macos/index.d.ts +2 -0
- package/dist/platform/macos/index.js +59 -0
- package/dist/platform/macos/launch.d.ts +2 -8
- package/dist/platform/macos/launch.js +5 -3
- package/dist/platform/macos/policy.d.ts +3 -21
- package/dist/platform/macos/policy.js +5 -2
- package/dist/platform/macos/supervise.d.ts +3 -25
- package/dist/platform/posix-forwarder-client.mjs +129 -0
- package/dist/platform/posix-forwarder.sh +9 -0
- package/dist/platform/posix-helpers.d.ts +22 -0
- package/dist/platform/posix-helpers.js +97 -0
- package/dist/platform/types.d.ts +118 -0
- package/dist/platform/types.js +1 -0
- package/dist/platform/windows/forwarder-client.mjs +165 -0
- package/dist/platform/windows/forwarder.cmd +3 -0
- package/dist/platform/windows/forwarder.ps1 +2 -0
- package/dist/platform/windows/forwarder.sh +2 -0
- package/dist/platform/windows/index.d.ts +2 -0
- package/dist/platform/windows/index.js +225 -0
- package/dist/platform/windows/launch.d.ts +3 -0
- package/dist/platform/windows/launch.js +30 -0
- package/dist/platform/windows/policy.d.ts +17 -0
- package/dist/platform/windows/policy.js +155 -0
- package/dist/platform/windows/setup.ps1 +112 -0
- package/dist/platform/windows/supervise.d.ts +2 -0
- package/dist/platform/windows/supervise.js +142 -0
- package/dist/platform/windows/win-sandbox.exe +0 -0
- package/dist/runners/acp/commands.js +2 -3
- package/dist/runners/acp/environment.d.ts +1 -7
- package/dist/runners/acp/environment.js +18 -48
- package/dist/runners/acp/index.js +1 -1
- package/dist/runners/acp/session.d.ts +1 -1
- package/dist/runners/pi/index.js +2 -2
- package/dist/runners/pi/session.d.ts +1 -1
- package/dist/runners/pi/session.js +27 -44
- package/dist/runners/pi/supervisor.js +1 -0
- package/package.json +5 -3
- package/dist/engine/forwarder.sh +0 -61
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
4
|
+
import { createServer } from "node:net";
|
|
5
|
+
import { join, resolve } from "node:path";
|
|
6
|
+
import { PassThrough } from "node:stream";
|
|
7
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
8
|
+
import { buildWindowsWorkloadEnv, resolveWindowsCommand, winSandboxCredentialsPath, winSandboxExecutablePath, } from "./policy.js";
|
|
9
|
+
export async function spawnSupervised(options) {
|
|
10
|
+
const helper = winSandboxExecutablePath();
|
|
11
|
+
const [program, ...args] = options.command;
|
|
12
|
+
await mkdir(options.policyDirectory, { recursive: true });
|
|
13
|
+
const controlPipe = `\\\\.\\pipe\\agent-evals-stop-${randomUUID()}`;
|
|
14
|
+
let controlSocket;
|
|
15
|
+
let stopRequested = false;
|
|
16
|
+
const controlServer = createServer((socket) => {
|
|
17
|
+
controlSocket = socket;
|
|
18
|
+
socket.on("error", () => { });
|
|
19
|
+
if (stopRequested) {
|
|
20
|
+
socket.end("stop\n");
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
controlServer.unref();
|
|
24
|
+
await new Promise((resolveListen, rejectListen) => {
|
|
25
|
+
controlServer.once("error", rejectListen);
|
|
26
|
+
controlServer.listen(controlPipe, () => {
|
|
27
|
+
controlServer.off("error", rejectListen);
|
|
28
|
+
resolveListen();
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
let report;
|
|
32
|
+
let reportPipe;
|
|
33
|
+
let reportServer;
|
|
34
|
+
if (options.report === true) {
|
|
35
|
+
report = new PassThrough();
|
|
36
|
+
reportPipe = `\\\\.\\pipe\\agent-evals-host-report-${randomUUID()}`;
|
|
37
|
+
const reportStream = report;
|
|
38
|
+
reportServer = createServer((socket) => {
|
|
39
|
+
socket.on("error", () => { });
|
|
40
|
+
socket.on("data", (chunk) => reportStream.write(chunk));
|
|
41
|
+
socket.on("end", () => reportStream.end());
|
|
42
|
+
});
|
|
43
|
+
reportServer.unref();
|
|
44
|
+
await new Promise((resolveListen, rejectListen) => {
|
|
45
|
+
reportServer.once("error", rejectListen);
|
|
46
|
+
reportServer.listen(reportPipe, () => {
|
|
47
|
+
reportServer.off("error", rejectListen);
|
|
48
|
+
resolveListen();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const workloadEnv = buildWindowsWorkloadEnv(options.env);
|
|
53
|
+
const resolvedCmd = resolveWindowsCommand(program, args, workloadEnv);
|
|
54
|
+
const configPath = join(options.policyDirectory, `win-sandbox-${randomUUID()}.json`);
|
|
55
|
+
const config = {
|
|
56
|
+
command: resolvedCmd.command,
|
|
57
|
+
args: [...resolvedCmd.args],
|
|
58
|
+
cwd: resolve(options.cwd),
|
|
59
|
+
env: workloadEnv,
|
|
60
|
+
writableRoots: options.policy.writableRoots,
|
|
61
|
+
readableRoots: options.policy.readableRoots,
|
|
62
|
+
writableFiles: options.policy.writableFiles ?? [],
|
|
63
|
+
readableFiles: options.policy.readableFiles ?? [],
|
|
64
|
+
allowNetwork: options.policy.allowNetwork,
|
|
65
|
+
credentialsPath: winSandboxCredentialsPath(),
|
|
66
|
+
controlPipe,
|
|
67
|
+
...(reportPipe === undefined ? {} : { reportPipe }),
|
|
68
|
+
};
|
|
69
|
+
await writeFile(configPath, JSON.stringify(config), "utf8");
|
|
70
|
+
const mode = options.interactive ? "inherit" : "pipe";
|
|
71
|
+
const child = spawn(helper, ["--config", configPath, "--delete-config"], {
|
|
72
|
+
cwd: options.cwd,
|
|
73
|
+
stdio: [mode, mode, mode],
|
|
74
|
+
windowsHide: !options.interactive,
|
|
75
|
+
});
|
|
76
|
+
const closeServers = () => {
|
|
77
|
+
controlSocket?.destroy();
|
|
78
|
+
controlServer.close();
|
|
79
|
+
reportServer?.close();
|
|
80
|
+
if (report !== undefined && !report.writableEnded) {
|
|
81
|
+
report.end();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
let exited = false;
|
|
85
|
+
const exit = new Promise((resolveExit, rejectExit) => {
|
|
86
|
+
child.once("exit", (code, signal) => {
|
|
87
|
+
exited = true;
|
|
88
|
+
closeServers();
|
|
89
|
+
resolveExit({ code, signal });
|
|
90
|
+
});
|
|
91
|
+
child.once("error", (error) => {
|
|
92
|
+
exited = true;
|
|
93
|
+
closeServers();
|
|
94
|
+
rejectExit(error);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
const completion = exit.then(({ code }) => {
|
|
98
|
+
if (code === null) {
|
|
99
|
+
throw new Error("sandbox supervisor exited without a workload completion record");
|
|
100
|
+
}
|
|
101
|
+
return code;
|
|
102
|
+
});
|
|
103
|
+
completion.catch(() => { });
|
|
104
|
+
const waitForExit = async (ms) => {
|
|
105
|
+
const deadline = new AbortController();
|
|
106
|
+
try {
|
|
107
|
+
return await Promise.race([exit.then(() => true), delay(ms, false, { signal: deadline.signal })]);
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
deadline.abort();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
let stopping;
|
|
114
|
+
const stop = () => (stopping ??= (async () => {
|
|
115
|
+
try {
|
|
116
|
+
if (!exited) {
|
|
117
|
+
stopRequested = true;
|
|
118
|
+
if (controlSocket !== undefined) {
|
|
119
|
+
controlSocket.end("stop\n");
|
|
120
|
+
}
|
|
121
|
+
if (!(await waitForExit(2_500))) {
|
|
122
|
+
child.kill();
|
|
123
|
+
const reaped = await waitForExit(2_000);
|
|
124
|
+
if (!reaped) {
|
|
125
|
+
throw new Error("Windows sandbox supervisor did not exit after kill");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
await exit;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
closeServers();
|
|
135
|
+
child.stdin?.destroy();
|
|
136
|
+
child.stdout?.destroy();
|
|
137
|
+
child.stderr?.destroy();
|
|
138
|
+
report?.destroy();
|
|
139
|
+
}
|
|
140
|
+
})());
|
|
141
|
+
return { child, completion, stop, ...(report === undefined ? {} : { report }) };
|
|
142
|
+
}
|
|
Binary file
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
2
|
import { mkdir, realpath, rm } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
3
|
import { join } from "node:path";
|
|
5
4
|
import { stringify } from "yaml";
|
|
6
5
|
import { loadImplementationParameters } from "../../engine/authoring.js";
|
|
7
6
|
import { ConfigError, errorMessage } from "../../engine/config.js";
|
|
8
7
|
import { createEnvSource } from "../../engine/env-source.js";
|
|
9
|
-
import { spawnSupervised } from "../../platform/
|
|
8
|
+
import { defaultTempParent, spawnSupervised } from "../../platform/index.js";
|
|
10
9
|
import { agentSchema } from "./agent.js";
|
|
11
10
|
import { harnessEnvironment, prepareHarnessHome, resolveHarnessHome, saveLogin } from "./environment.js";
|
|
12
11
|
import { withSession } from "./session.js";
|
|
@@ -14,7 +13,7 @@ import { withSession } from "./session.js";
|
|
|
14
13
|
export async function runAgentCommand(command, path, signal) {
|
|
15
14
|
// Placeholders need the directories' paths; they are created only after validation. The
|
|
16
15
|
// parent is canonical so the sandbox authorizes traversal through the `deps` link.
|
|
17
|
-
const directory = join(await realpath(
|
|
16
|
+
const directory = join(await realpath(defaultTempParent()), `ae-acp-${randomBytes(6).toString("hex")}`);
|
|
18
17
|
const dirs = {
|
|
19
18
|
home: join(directory, "home"),
|
|
20
19
|
workspace: join(directory, "workspace"),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { MockWiring } from "../../engine/registry.js";
|
|
2
2
|
import { type RunRoot } from "../../engine/workspace.js";
|
|
3
|
-
import { createSandboxPolicy } from "../../platform/
|
|
3
|
+
import { createSandboxPolicy } from "../../platform/index.js";
|
|
4
4
|
import type { AgentConfig } from "./agent.js";
|
|
5
5
|
/** Persistent login state: where it lives, which files compose it, and how to obtain it. */
|
|
6
6
|
export interface HarnessLogin {
|
|
@@ -63,9 +63,3 @@ export declare function harnessEnvironment(source: HarnessHome, dirs: {
|
|
|
63
63
|
}>;
|
|
64
64
|
/** Save declared login files after a successful native login. */
|
|
65
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[]>;
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import { constants } from "node:fs";
|
|
2
|
-
import { execFile } from "node:child_process";
|
|
3
|
-
import { promisify } from "node:util";
|
|
4
1
|
import { randomUUID } from "node:crypto";
|
|
5
|
-
import { lstat, mkdir,
|
|
6
|
-
import { basename, dirname, join, resolve } from "node:path";
|
|
2
|
+
import { lstat, mkdir, realpath, rename, rm, symlink, writeFile } from "node:fs/promises";
|
|
3
|
+
import { basename, delimiter, dirname, join, resolve } from "node:path";
|
|
7
4
|
import { ConfigError, errorMessage, expectPlacementDestination, expectRelativePath } from "../../engine/config.js";
|
|
8
5
|
import { copyPlacement, homePathOf, isHomeDestination, placedPaths, stageEntries } from "../../engine/files.js";
|
|
9
6
|
import { isMissing, isWithin, openRunRoot, resolveWorkspacePath } from "../../engine/workspace.js";
|
|
10
|
-
import {
|
|
7
|
+
import { assertSandboxAvailable, createSandboxPolicy, dirEnv, openRegularFileNoFollow, resolveDeniedExecutables, SYSTEM_PATH, } from "../../platform/index.js";
|
|
11
8
|
/** Validate the caller-owned software, HOME content and login state without creating run state. */
|
|
12
9
|
export async function resolveHarnessHome(agent, declaringDir) {
|
|
13
|
-
|
|
10
|
+
assertSandboxAvailable();
|
|
14
11
|
let deps;
|
|
15
12
|
try {
|
|
16
13
|
deps = (await openRunRoot(resolve(declaringDir, agent.deps))).path;
|
|
@@ -169,20 +166,29 @@ export async function harnessEnvironment(source, dirs, options) {
|
|
|
169
166
|
const mocks = options.mocks;
|
|
170
167
|
const depsLink = join(dirs.runRootDir, "deps");
|
|
171
168
|
const pathEntries = [...source.path.map((path) => join(depsLink, path)), ...SYSTEM_PATH];
|
|
169
|
+
let denyExecutables = [];
|
|
170
|
+
if (mocks !== undefined) {
|
|
171
|
+
denyExecutables = await resolveDeniedExecutables(mocks.denyTargets, pathEntries);
|
|
172
|
+
const runtimeNode = await realpath(process.execPath);
|
|
173
|
+
if (denyExecutables.includes(runtimeNode)) {
|
|
174
|
+
throw new ConfigError(`cannot deny ${runtimeNode} while mocks are active because mock forwarders execute with this Node interpreter`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
172
177
|
const policy = createSandboxPolicy({
|
|
173
178
|
writableRoots: [dirs.home, dirs.workspace, dirs.tmp],
|
|
174
179
|
// A mocked command's forwarder is readable and executable, never writable.
|
|
175
180
|
readableRoots: mocks === undefined ? [depsLink] : [depsLink, mocks.forwarderDir],
|
|
176
|
-
|
|
181
|
+
...(mocks?.readableFiles === undefined ? {} : { readableFiles: mocks.readableFiles }),
|
|
182
|
+
...(mocks === undefined ? {} : { writableFiles: [mocks.socketPath] }),
|
|
183
|
+
denyExecutables,
|
|
177
184
|
allowNetwork: true,
|
|
178
185
|
interactive: options.interactive,
|
|
179
186
|
});
|
|
180
187
|
const env = {
|
|
181
188
|
...source.environment,
|
|
182
189
|
...(process.env.TERM === undefined ? {} : { TERM: process.env.TERM }),
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
PATH: [...(mocks === undefined ? [] : [mocks.forwarderDir]), ...pathEntries].join(":"),
|
|
190
|
+
...dirEnv({ home: dirs.home, tmp: dirs.tmp }),
|
|
191
|
+
PATH: [...(mocks === undefined ? [] : [mocks.forwarderDir]), ...pathEntries].join(delimiter),
|
|
186
192
|
};
|
|
187
193
|
return { env, policy };
|
|
188
194
|
}
|
|
@@ -219,41 +225,6 @@ async function persistLogin(login, selected) {
|
|
|
219
225
|
}
|
|
220
226
|
}
|
|
221
227
|
}
|
|
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
228
|
/** Canonicalize existing ancestors too, without creating a first-login directory. */
|
|
258
229
|
async function canonicalPath(path) {
|
|
259
230
|
try {
|
|
@@ -267,8 +238,7 @@ async function canonicalPath(path) {
|
|
|
267
238
|
}
|
|
268
239
|
async function readRegular(root, file) {
|
|
269
240
|
const path = await resolveWorkspacePath(root, file);
|
|
270
|
-
|
|
271
|
-
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
241
|
+
const handle = await openRegularFileNoFollow(path);
|
|
272
242
|
try {
|
|
273
243
|
if (!(await handle.stat()).isFile())
|
|
274
244
|
throw new ConfigError(`login path is not a regular file: ${file}`);
|
|
@@ -2,7 +2,7 @@ import { methods } from "@agentclientprotocol/sdk";
|
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { loadImplementationParameters } from "../../engine/authoring.js";
|
|
4
4
|
import { resolveAuthoringAsset } from "../../engine/config.js";
|
|
5
|
-
import { spawnSupervised } from "../../platform/
|
|
5
|
+
import { spawnSupervised } from "../../platform/index.js";
|
|
6
6
|
import { agentSchema } from "./agent.js";
|
|
7
7
|
import { harnessEnvironment, prepareHarnessHome, resolveHarnessHome } from "./environment.js";
|
|
8
8
|
import { withSession } from "./session.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ClientContext, type InitializeResponse, type NewSessionResponse, type SessionUpdate } from "@agentclientprotocol/sdk";
|
|
2
|
-
import type { SupervisedProcess } from "../../platform/
|
|
2
|
+
import type { SupervisedProcess } from "../../platform/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Initialize the agent, create one session in `cwd` and run `use` while the
|
|
5
5
|
* connection is open. Fails when the agent exits or the connection breaks.
|
package/dist/runners/pi/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import { z } from "zod";
|
|
|
4
4
|
import { ConfigError, errorMessage, resolveAuthoringAsset } from "../../engine/config.js";
|
|
5
5
|
import { authoringCopyFilter } from "../../engine/files.js";
|
|
6
6
|
import { nonEmptyTrimmedString } from "../../engine/params.js";
|
|
7
|
-
import {
|
|
7
|
+
import { assertSandboxAvailable } from "../../platform/index.js";
|
|
8
8
|
import { thinkingSchema } from "./params.js";
|
|
9
9
|
import { invokePi, resolvePiExecutable } from "./session.js";
|
|
10
10
|
const piRunnerParamsSchema = z.strictObject({
|
|
@@ -21,7 +21,7 @@ export const piRunner = {
|
|
|
21
21
|
async create(params, context) {
|
|
22
22
|
await resolvePiExecutable(params.executable);
|
|
23
23
|
await assertSkillsAvailable(params.skills, context.authoringDir);
|
|
24
|
-
|
|
24
|
+
assertSandboxAvailable();
|
|
25
25
|
return new PiRunner(params, context.authoringDir);
|
|
26
26
|
},
|
|
27
27
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type SandboxPolicy } from "../../platform/
|
|
1
|
+
import { type SandboxPolicy } from "../../platform/index.js";
|
|
2
2
|
import { PiProcess } from "./rpc.js";
|
|
3
3
|
export declare const THINKING_LEVELS: readonly ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
4
4
|
export type ThinkingLevel = (typeof THINKING_LEVELS)[number];
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { basename, dirname, join, resolve } from "node:path";
|
|
1
|
+
import { mkdir, readFile, realpath, stat, writeFile } from "node:fs/promises";
|
|
2
|
+
import { basename, delimiter, dirname, join, resolve, sep } from "node:path";
|
|
4
3
|
import { fileURLToPath } from "node:url";
|
|
5
4
|
import { ConfigError, errorMessage } from "../../engine/config.js";
|
|
6
5
|
import { truncate } from "../../engine/truncate.js";
|
|
7
6
|
import { siblingRuntimeModule } from "../../engine/runtime-assets.js";
|
|
8
|
-
import { spawnSandboxed } from "../../platform/
|
|
9
|
-
import { writeSeatbeltPolicy } from "../../platform/macos/policy.js";
|
|
7
|
+
import { createSandboxPolicy, dirEnv, executableExtensions, isExecutableAccessible, spawnSandboxed, SYSTEM_PATH, usesPiShellWrapper, wrapExecutableCommand, } from "../../platform/index.js";
|
|
10
8
|
import { PiProcess, PiRpcError } from "./rpc.js";
|
|
11
9
|
export const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
12
10
|
export class PiSessionError extends Error {
|
|
@@ -22,7 +20,7 @@ export async function resolvePiExecutable(executable) {
|
|
|
22
20
|
if (executable.length === 0) {
|
|
23
21
|
throw new ConfigError("pi executable must be a non-empty string");
|
|
24
22
|
}
|
|
25
|
-
const candidate = executable.includes("/") ? resolve(executable) : await findOnPath(executable);
|
|
23
|
+
const candidate = executable.includes("/") || executable.includes(sep) ? resolve(executable) : await findOnPath(executable);
|
|
26
24
|
if (candidate === undefined) {
|
|
27
25
|
throw new ConfigError(`pi executable not found on PATH: ${executable}`);
|
|
28
26
|
}
|
|
@@ -102,7 +100,7 @@ export async function createPiProcess(config) {
|
|
|
102
100
|
await writeFile(supervisorPath, await readFile(supervisorSource, "utf8"), { mode: 0o700 });
|
|
103
101
|
await writeFile(safeGroupPath, await readFile(safeGroupSource, "utf8"), { mode: 0o700 });
|
|
104
102
|
let shellPath;
|
|
105
|
-
if (config.tools !== null) {
|
|
103
|
+
if (config.tools !== null && usesPiShellWrapper) {
|
|
106
104
|
shellPath = join(toolsDir, "shell-wrapper.sh");
|
|
107
105
|
await writeFile(shellPath, await readFile(new URL("./shell-wrapper.sh", import.meta.url), "utf8"), { mode: 0o700 });
|
|
108
106
|
await mkdir(shellStateDirectory(config), { recursive: true, mode: 0o700 });
|
|
@@ -112,35 +110,24 @@ export async function createPiProcess(config) {
|
|
|
112
110
|
await writeApiKeyConfig(config.dirs.configDir, config);
|
|
113
111
|
}
|
|
114
112
|
const policy = await buildPolicy(executable, config);
|
|
115
|
-
const policyPath = await writeSeatbeltPolicy(config.dirs.scratchDir, policy);
|
|
116
113
|
const args = buildPiArgs(config);
|
|
117
114
|
const env = buildEnvironment(config);
|
|
118
115
|
const nodeExecutable = await realpath(process.execPath);
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
116
|
+
const [wrappedExecutable, ...wrappedArgs] = wrapExecutableCommand(executable, args);
|
|
117
|
+
const child = await spawnSandboxed({
|
|
118
|
+
policy,
|
|
119
|
+
policyDirectory: config.dirs.scratchDir,
|
|
120
|
+
command: nodeExecutable,
|
|
121
|
+
args: [supervisorPath, wrappedExecutable, ...wrappedArgs],
|
|
122
|
+
cwd: config.dirs.workspaceDir,
|
|
123
|
+
env,
|
|
127
124
|
});
|
|
125
|
+
return new PiProcess({ spawn: () => child });
|
|
128
126
|
}
|
|
129
127
|
export async function buildPolicy(executable, config) {
|
|
130
128
|
const nodeExecutable = await realpath(process.execPath);
|
|
131
129
|
const nodeDirectory = dirname(nodeExecutable);
|
|
132
|
-
const readableRoots = new Set([
|
|
133
|
-
"/System",
|
|
134
|
-
"/usr",
|
|
135
|
-
"/bin",
|
|
136
|
-
"/sbin",
|
|
137
|
-
"/private/etc",
|
|
138
|
-
"/Library",
|
|
139
|
-
"/dev",
|
|
140
|
-
"/private/var/select",
|
|
141
|
-
"/private/var/db/timezone",
|
|
142
|
-
nodeDirectory,
|
|
143
|
-
]);
|
|
130
|
+
const readableRoots = new Set([nodeDirectory]);
|
|
144
131
|
if (basename(nodeDirectory) === "bin") {
|
|
145
132
|
readableRoots.add(dirname(nodeDirectory));
|
|
146
133
|
}
|
|
@@ -156,14 +143,13 @@ export async function buildPolicy(executable, config) {
|
|
|
156
143
|
readableRoots.add(nodeModulesRoot);
|
|
157
144
|
}
|
|
158
145
|
readableRoots.add(shellToolsDirectory(config));
|
|
159
|
-
return {
|
|
146
|
+
return createSandboxPolicy({
|
|
160
147
|
writableRoots: [config.dirs.workspaceDir, config.dirs.homeDir, config.dirs.configDir, config.dirs.scratchDir],
|
|
161
148
|
readableRoots: [...readableRoots],
|
|
162
|
-
|
|
163
|
-
// reads `/private/etc/resolv.conf`; without them a sandboxed Node fails DNS with ENOTFOUND.
|
|
164
|
-
readableFiles: [executable, "/etc", "/var"],
|
|
149
|
+
readableFiles: [executable],
|
|
165
150
|
allowNetwork: config.allowNetwork,
|
|
166
|
-
|
|
151
|
+
interactive: false,
|
|
152
|
+
});
|
|
167
153
|
}
|
|
168
154
|
function buildPiArgs(config) {
|
|
169
155
|
const args = [
|
|
@@ -189,9 +175,8 @@ function buildPiArgs(config) {
|
|
|
189
175
|
function buildEnvironment(config) {
|
|
190
176
|
const nodeDirectory = dirname(process.execPath);
|
|
191
177
|
const env = {
|
|
192
|
-
PATH: [nodeDirectory,
|
|
193
|
-
|
|
194
|
-
TMPDIR: config.dirs.scratchDir,
|
|
178
|
+
PATH: [nodeDirectory, ...SYSTEM_PATH].join(delimiter),
|
|
179
|
+
...dirEnv({ home: config.dirs.homeDir, tmp: config.dirs.scratchDir }),
|
|
195
180
|
TMP: config.dirs.scratchDir,
|
|
196
181
|
TEMP: config.dirs.scratchDir,
|
|
197
182
|
PI_CODING_AGENT_DIR: config.dirs.configDir,
|
|
@@ -228,17 +213,15 @@ async function writeIsolatedSettings(configDir, shellPath) {
|
|
|
228
213
|
}
|
|
229
214
|
async function findOnPath(name) {
|
|
230
215
|
const path = process.env.PATH ?? "";
|
|
231
|
-
for (const directory of path.split(
|
|
216
|
+
for (const directory of path.split(delimiter)) {
|
|
232
217
|
if (directory.length === 0) {
|
|
233
218
|
continue;
|
|
234
219
|
}
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
await
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
catch {
|
|
241
|
-
// Keep searching.
|
|
220
|
+
for (const ext of executableExtensions) {
|
|
221
|
+
const candidate = join(directory, `${name}${ext}`);
|
|
222
|
+
if (await isExecutableAccessible(candidate)) {
|
|
223
|
+
return candidate;
|
|
224
|
+
}
|
|
242
225
|
}
|
|
243
226
|
}
|
|
244
227
|
return undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lelouchhe/agent-evals",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A toolkit for evaluating AI agent systems.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"format": "prettier --write .",
|
|
28
28
|
"format:check": "prettier --check .",
|
|
29
29
|
"typecheck": "tsc --noEmit",
|
|
30
|
-
"test": "node
|
|
30
|
+
"test": "node scripts/test.mjs",
|
|
31
31
|
"test:package": "node --test --test-timeout=180000 tests/package-install.ts",
|
|
32
32
|
"pretest": "npm run build",
|
|
33
33
|
"check": "npm run lint && npm run format:check && npm run typecheck && npm test",
|
|
@@ -74,6 +74,8 @@
|
|
|
74
74
|
"access": "public"
|
|
75
75
|
},
|
|
76
76
|
"os": [
|
|
77
|
-
"darwin"
|
|
77
|
+
"darwin",
|
|
78
|
+
"linux",
|
|
79
|
+
"win32"
|
|
78
80
|
]
|
|
79
81
|
}
|
package/dist/engine/forwarder.sh
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
#!/bin/sh
|
|
2
|
-
self=$0
|
|
3
|
-
bindir=__BINDIR__
|
|
4
|
-
socket=__SOCKET__
|
|
5
|
-
fault=__FAULT__
|
|
6
|
-
encode() {
|
|
7
|
-
printf '%s' "$1" | /usr/bin/base64 | /usr/bin/tr -d '\n'
|
|
8
|
-
printf '\n'
|
|
9
|
-
}
|
|
10
|
-
# Canonicalise our own path so the host can tell which forwarder directory we came from.
|
|
11
|
-
folder=${self%/*}
|
|
12
|
-
[ "$folder" = "$self" ] && folder=.
|
|
13
|
-
self=$(cd "$folder" && pwd -P)/${self##*/}
|
|
14
|
-
reply=$(
|
|
15
|
-
{
|
|
16
|
-
encode "$self"
|
|
17
|
-
for argument in "$@"; do
|
|
18
|
-
encode "$argument"
|
|
19
|
-
done
|
|
20
|
-
} | /usr/bin/nc -U "$socket"
|
|
21
|
-
) || {
|
|
22
|
-
printf '%s: Service unavailable\n' "${self##*/}" >&2
|
|
23
|
-
printf '%s\n' "${self##*/}" >>"$fault" 2>/dev/null
|
|
24
|
-
exit 126
|
|
25
|
-
}
|
|
26
|
-
IFS=' ' read -r kind code out err <<EOF
|
|
27
|
-
$reply
|
|
28
|
-
EOF
|
|
29
|
-
if [ "$kind" = pass ]; then
|
|
30
|
-
name=${self##*/}
|
|
31
|
-
case $self in
|
|
32
|
-
"$bindir"/*)
|
|
33
|
-
oldifs=$IFS
|
|
34
|
-
IFS=:
|
|
35
|
-
set -f
|
|
36
|
-
for folder in $PATH; do
|
|
37
|
-
candidate=$folder/$name
|
|
38
|
-
[ -f "$candidate" ] || continue
|
|
39
|
-
[ -x "$candidate" ] || continue
|
|
40
|
-
# Skip our own entry even when PATH spells the directory differently.
|
|
41
|
-
if [ "$(cd "$folder" 2>/dev/null && pwd -P)/$name" = "$self" ]; then continue; fi
|
|
42
|
-
set +f
|
|
43
|
-
IFS=$oldifs
|
|
44
|
-
exec "$candidate" "$@"
|
|
45
|
-
done
|
|
46
|
-
set +f
|
|
47
|
-
IFS=$oldifs
|
|
48
|
-
;;
|
|
49
|
-
*)
|
|
50
|
-
real=${self%/*}/.${name}
|
|
51
|
-
if [ -x "$real" ]; then exec "$real" "$@"; fi
|
|
52
|
-
;;
|
|
53
|
-
esac
|
|
54
|
-
printf '%s: Operation not permitted\n' "$name" >&2
|
|
55
|
-
exit 126
|
|
56
|
-
fi
|
|
57
|
-
[ "$out" = - ] && out=""
|
|
58
|
-
[ "$err" = - ] && err=""
|
|
59
|
-
if [ -n "$out" ]; then printf '%s' "$out" | /usr/bin/base64 -D; fi
|
|
60
|
-
if [ -n "$err" ]; then printf '%s' "$err" | /usr/bin/base64 -D >&2; fi
|
|
61
|
-
exit "$code"
|