@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,135 @@
|
|
|
1
|
+
import { methods } from "@agentclientprotocol/sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { loadImplementationParameters } from "../../engine/authoring.js";
|
|
4
|
+
import { resolveAuthoringAsset } from "../../engine/config.js";
|
|
5
|
+
import { spawnSupervised } from "../../platform/macos/supervise.js";
|
|
6
|
+
import { agentSchema } from "./agent.js";
|
|
7
|
+
import { harnessEnvironment, prepareHarnessHome, resolveHarnessHome } from "./environment.js";
|
|
8
|
+
import { withSession } from "./session.js";
|
|
9
|
+
import { AcpTrace } from "./trace.js";
|
|
10
|
+
import { AcpUsage } from "./usage.js";
|
|
11
|
+
const acpRunnerParamsSchema = z.strictObject({
|
|
12
|
+
/** Inline agent startup object, or a path to a standalone file holding the same object. */
|
|
13
|
+
agent: z.union([z.string().min(1), agentSchema]),
|
|
14
|
+
/** Session config values keyed by option category, otherwise option ID; applied in order. */
|
|
15
|
+
config: z.record(z.string(), z.union([z.string(), z.boolean()])).default({}),
|
|
16
|
+
});
|
|
17
|
+
/** Generic ACP runner: one prompt turn in a caller-prepared harness home under Seatbelt. */
|
|
18
|
+
export const acpRunner = {
|
|
19
|
+
type: "acp",
|
|
20
|
+
params: acpRunnerParamsSchema,
|
|
21
|
+
mocks: true,
|
|
22
|
+
home: true,
|
|
23
|
+
async create(params, context) {
|
|
24
|
+
const { params: agent, dir } = typeof params.agent === "string"
|
|
25
|
+
? await loadImplementationParameters(resolveAuthoringAsset(context.authoringDir, params.agent, "acp runner agent"), agentSchema, context.values)
|
|
26
|
+
: { params: params.agent, dir: context.authoringDir };
|
|
27
|
+
return new AcpRunner(agent.acpCommand, await resolveHarnessHome(agent, dir), params.config);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
class AcpRunner {
|
|
31
|
+
#command;
|
|
32
|
+
#home;
|
|
33
|
+
homeFiles;
|
|
34
|
+
concurrencyKey;
|
|
35
|
+
#config;
|
|
36
|
+
#process;
|
|
37
|
+
#copyBack;
|
|
38
|
+
#stopped;
|
|
39
|
+
#trace = new AcpTrace();
|
|
40
|
+
#usage = new AcpUsage();
|
|
41
|
+
constructor(command, home, config) {
|
|
42
|
+
this.#command = command;
|
|
43
|
+
this.#home = home;
|
|
44
|
+
this.homeFiles = home.login?.files ?? [];
|
|
45
|
+
if (home.login !== undefined) {
|
|
46
|
+
this.concurrencyKey = home.login.dir;
|
|
47
|
+
}
|
|
48
|
+
this.#config = config;
|
|
49
|
+
}
|
|
50
|
+
/** Place the agent file's HOME content and login state before the case's own `~/` entries. */
|
|
51
|
+
prepareHome = async (context) => {
|
|
52
|
+
const prepared = await prepareHarnessHome(this.#home, { home: context.homeDir, runRootDir: context.runRootDir, engineDir: context.engineDir }, context.signal);
|
|
53
|
+
this.#copyBack = prepared.copyBack;
|
|
54
|
+
return prepared.protectedPaths;
|
|
55
|
+
};
|
|
56
|
+
run = async (context) => {
|
|
57
|
+
const environment = await harnessEnvironment(this.#home, {
|
|
58
|
+
home: context.homeDir,
|
|
59
|
+
workspace: context.workspaceDir,
|
|
60
|
+
tmp: context.tmpDir,
|
|
61
|
+
runRootDir: context.runRootDir,
|
|
62
|
+
}, {
|
|
63
|
+
interactive: false,
|
|
64
|
+
...(context.mocks === undefined ? {} : { mocks: context.mocks }),
|
|
65
|
+
});
|
|
66
|
+
this.#process = await spawnSupervised({
|
|
67
|
+
command: this.#command,
|
|
68
|
+
policy: environment.policy,
|
|
69
|
+
// Outside everything the sandbox can read.
|
|
70
|
+
policyDirectory: context.engineDir,
|
|
71
|
+
cwd: context.workspaceDir,
|
|
72
|
+
env: environment.env,
|
|
73
|
+
interactive: false,
|
|
74
|
+
});
|
|
75
|
+
// Stopping the sandbox closes the connection, which fails the pending request.
|
|
76
|
+
const onAbort = () => void this.stop().catch(() => { });
|
|
77
|
+
context.signal.addEventListener("abort", onAbort, { once: true });
|
|
78
|
+
try {
|
|
79
|
+
context.signal.throwIfAborted();
|
|
80
|
+
await withSession(this.#process, context.workspaceDir, (update) => {
|
|
81
|
+
this.#trace.add(update);
|
|
82
|
+
this.#usage.add(update);
|
|
83
|
+
}, async (agent, _initialized, session) => {
|
|
84
|
+
const { sessionId } = session;
|
|
85
|
+
let options = session.configOptions ?? [];
|
|
86
|
+
for (const [key, value] of Object.entries(this.#config)) {
|
|
87
|
+
const configId = offeredOption(options, key, value).id;
|
|
88
|
+
const request = typeof value === "boolean" ? { type: "boolean", value } : { value };
|
|
89
|
+
options = (await agent.request(methods.agent.session.setConfigOption, { sessionId, configId, ...request }))
|
|
90
|
+
.configOptions;
|
|
91
|
+
}
|
|
92
|
+
const response = await agent.request(methods.agent.session.prompt, {
|
|
93
|
+
sessionId,
|
|
94
|
+
prompt: [{ type: "text", text: context.input }],
|
|
95
|
+
});
|
|
96
|
+
this.#usage.setPromptUsage(response);
|
|
97
|
+
if (response.stopReason !== "end_turn") {
|
|
98
|
+
throw new Error(`ACP turn stopped with ${response.stopReason}`);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
return { finalText: this.#trace.finalText(), diagnostics: [] };
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
context.signal.removeEventListener("abort", onAbort);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
trace = () => this.#trace.entries;
|
|
108
|
+
usage = () => this.#usage.value;
|
|
109
|
+
/** Stop the whole sandbox, then keep login changes the harness made to its original files. */
|
|
110
|
+
stop = async () => {
|
|
111
|
+
const process = this.#process;
|
|
112
|
+
if (process === undefined)
|
|
113
|
+
return;
|
|
114
|
+
await (this.#stopped ??= (async () => {
|
|
115
|
+
await process.stop();
|
|
116
|
+
await this.#copyBack?.();
|
|
117
|
+
})());
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/** Find the option a config key names and require that the agent offers the value. */
|
|
121
|
+
function offeredOption(options, key, value) {
|
|
122
|
+
const inCategory = options.filter((option) => option.category === key);
|
|
123
|
+
const option = inCategory.length === 1 ? inCategory[0] : options.find((candidate) => candidate.id === key);
|
|
124
|
+
if (option === undefined) {
|
|
125
|
+
const available = options.map((candidate) => candidate.category ?? candidate.id).join(", ") || "none";
|
|
126
|
+
throw new Error(`ACP config "${key}" matches no single option category or ID (available: ${available})`);
|
|
127
|
+
}
|
|
128
|
+
const offered = option.type === "boolean"
|
|
129
|
+
? [true, false]
|
|
130
|
+
: option.options.flatMap((entry) => ("options" in entry ? entry.options : [entry])).map((entry) => entry.value);
|
|
131
|
+
if (!offered.includes(value)) {
|
|
132
|
+
throw new Error(`ACP config "${key}" does not offer ${JSON.stringify(value)} (available: ${offered.join(", ")})`);
|
|
133
|
+
}
|
|
134
|
+
return option;
|
|
135
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ClientContext, type InitializeResponse, type NewSessionResponse, type SessionUpdate } from "@agentclientprotocol/sdk";
|
|
2
|
+
import type { SupervisedProcess } from "../../platform/macos/supervise.js";
|
|
3
|
+
/**
|
|
4
|
+
* Initialize the agent, create one session in `cwd` and run `use` while the
|
|
5
|
+
* connection is open. Fails when the agent exits or the connection breaks.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withSession<T>(process: SupervisedProcess, cwd: string, onUpdate: (update: SessionUpdate) => void, use: (agent: ClientContext, initialized: InitializeResponse, session: NewSessionResponse) => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { client, methods, ndJsonStream, PROTOCOL_VERSION, RequestError, } from "@agentclientprotocol/sdk";
|
|
2
|
+
import { Readable, Writable } from "node:stream";
|
|
3
|
+
import { errorMessage } from "../../engine/config.js";
|
|
4
|
+
/**
|
|
5
|
+
* Initialize the agent, create one session in `cwd` and run `use` while the
|
|
6
|
+
* connection is open. Fails when the agent exits or the connection breaks.
|
|
7
|
+
*/
|
|
8
|
+
export async function withSession(process, cwd, onUpdate, use) {
|
|
9
|
+
let stderr = "";
|
|
10
|
+
process.child.stderr.on("data", (chunk) => {
|
|
11
|
+
stderr = (stderr + chunk.toString("utf8")).slice(-4096);
|
|
12
|
+
});
|
|
13
|
+
const app = client()
|
|
14
|
+
// The sandbox is the permission boundary, so unattended runs approve what the agent asks for.
|
|
15
|
+
.onRequest(methods.client.session.requestPermission, async ({ params: { options } }) => {
|
|
16
|
+
const option = options.find((candidate) => candidate.kind === "allow_once") ??
|
|
17
|
+
options.find((candidate) => candidate.kind === "allow_always");
|
|
18
|
+
return {
|
|
19
|
+
outcome: option === undefined ? { outcome: "cancelled" } : { outcome: "selected", optionId: option.optionId },
|
|
20
|
+
};
|
|
21
|
+
})
|
|
22
|
+
.onNotification(methods.client.session.update, async ({ params }) => onUpdate(params.update));
|
|
23
|
+
const exited = process.completion.then((code) => {
|
|
24
|
+
throw new Error(`ACP agent exited with code ${code}`);
|
|
25
|
+
});
|
|
26
|
+
try {
|
|
27
|
+
return await Promise.race([
|
|
28
|
+
exited,
|
|
29
|
+
app.connectWith(ndJsonStream(Writable.toWeb(process.child.stdin), Readable.toWeb(process.child.stdout)), async (agent) => {
|
|
30
|
+
const initialized = await agent.request(methods.agent.initialize, {
|
|
31
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
32
|
+
clientCapabilities: {},
|
|
33
|
+
});
|
|
34
|
+
const session = await agent.request(methods.agent.session.new, { cwd, mcpServers: [] });
|
|
35
|
+
return use(agent, initialized, session);
|
|
36
|
+
}),
|
|
37
|
+
]);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof RequestError && error.code === -32000) {
|
|
41
|
+
throw new Error("ACP authentication required; run login with this agent configuration first", { cause: error });
|
|
42
|
+
}
|
|
43
|
+
if (stderr.trim()) {
|
|
44
|
+
throw new Error(`ACP agent failed: ${errorMessage(error)}; stderr: ${stderr.trim()}`, { cause: error });
|
|
45
|
+
}
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { SessionUpdate } from "@agentclientprotocol/sdk";
|
|
2
|
+
type TextEntry = {
|
|
3
|
+
type: "message" | "thought";
|
|
4
|
+
text: string;
|
|
5
|
+
};
|
|
6
|
+
type ToolEntry = {
|
|
7
|
+
type: "tool";
|
|
8
|
+
toolCallId: string;
|
|
9
|
+
[field: string]: unknown;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Assembles ACP session updates into trace entries. Consecutive text chunks of one kind are
|
|
13
|
+
* joined. ACP updates replace the fields they carry, so every update for a `toolCallId` is
|
|
14
|
+
* merged into one entry that keeps only the call's final state.
|
|
15
|
+
*/
|
|
16
|
+
export declare class AcpTrace {
|
|
17
|
+
#private;
|
|
18
|
+
readonly entries: (TextEntry | ToolEntry)[];
|
|
19
|
+
add(update: SessionUpdate): void;
|
|
20
|
+
/** The agent's text after its last tool call. */
|
|
21
|
+
finalText(): string | null;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Assembles ACP session updates into trace entries. Consecutive text chunks of one kind are
|
|
3
|
+
* joined. ACP updates replace the fields they carry, so every update for a `toolCallId` is
|
|
4
|
+
* merged into one entry that keeps only the call's final state.
|
|
5
|
+
*/
|
|
6
|
+
export class AcpTrace {
|
|
7
|
+
entries = [];
|
|
8
|
+
#tools = new Map();
|
|
9
|
+
add(update) {
|
|
10
|
+
if (update.sessionUpdate === "agent_message_chunk" || update.sessionUpdate === "agent_thought_chunk") {
|
|
11
|
+
if (update.content.type !== "text")
|
|
12
|
+
return;
|
|
13
|
+
const type = update.sessionUpdate === "agent_message_chunk" ? "message" : "thought";
|
|
14
|
+
const last = this.entries.at(-1);
|
|
15
|
+
if (last?.type === type)
|
|
16
|
+
last.text += update.content.text;
|
|
17
|
+
else
|
|
18
|
+
this.entries.push({ type, text: update.content.text });
|
|
19
|
+
}
|
|
20
|
+
else if (update.sessionUpdate === "tool_call" || update.sessionUpdate === "tool_call_update") {
|
|
21
|
+
const { sessionUpdate: _, toolCallId, ...fields } = update;
|
|
22
|
+
let entry = this.#tools.get(toolCallId);
|
|
23
|
+
if (entry === undefined) {
|
|
24
|
+
entry = { type: "tool", toolCallId };
|
|
25
|
+
this.#tools.set(toolCallId, entry);
|
|
26
|
+
this.entries.push(entry);
|
|
27
|
+
}
|
|
28
|
+
// Absent and null fields leave the previous value unchanged.
|
|
29
|
+
for (const [field, value] of Object.entries(fields)) {
|
|
30
|
+
if (value !== undefined && value !== null)
|
|
31
|
+
entry[field] = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** The agent's text after its last tool call. */
|
|
36
|
+
finalText() {
|
|
37
|
+
const last = this.entries.findLastIndex((entry) => entry.type === "tool");
|
|
38
|
+
const text = this.entries
|
|
39
|
+
.slice(last + 1)
|
|
40
|
+
.flatMap((entry) => (entry.type === "message" ? [entry.text] : []))
|
|
41
|
+
.join("");
|
|
42
|
+
return text === "" ? null : text;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PromptResponse, SessionUpdate } from "@agentclientprotocol/sdk";
|
|
2
|
+
import type { RunnerUsage } from "../../engine/registry.js";
|
|
3
|
+
/** Accumulates what an ACP harness reports: context and cost from updates, tokens from the turn. */
|
|
4
|
+
export declare class AcpUsage {
|
|
5
|
+
#private;
|
|
6
|
+
/** Apply one session update. A `usage_update` replaces the context and, when it carries one, the cost. */
|
|
7
|
+
add(update: SessionUpdate): void;
|
|
8
|
+
/** Read the unstable usage from a prompt response; a malformed field is dropped. */
|
|
9
|
+
setPromptUsage(response: PromptResponse): void;
|
|
10
|
+
/** What the harness reported so far; absent when it reported nothing. */
|
|
11
|
+
get value(): RunnerUsage | undefined;
|
|
12
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* ACP's unstable prompt-response usage. The stable client passes the field through untyped, so
|
|
4
|
+
* this schema is the only check of its shape; a mismatch drops it instead of failing the run.
|
|
5
|
+
* `_meta` and any other extra field are ignored.
|
|
6
|
+
*/
|
|
7
|
+
const tokenCount = z.number().int().min(0);
|
|
8
|
+
const tokenUsageSchema = z.object({
|
|
9
|
+
totalTokens: tokenCount,
|
|
10
|
+
inputTokens: tokenCount,
|
|
11
|
+
outputTokens: tokenCount,
|
|
12
|
+
thoughtTokens: tokenCount.nullable().optional(),
|
|
13
|
+
cachedReadTokens: tokenCount.nullable().optional(),
|
|
14
|
+
cachedWriteTokens: tokenCount.nullable().optional(),
|
|
15
|
+
});
|
|
16
|
+
/** Accumulates what an ACP harness reports: context and cost from updates, tokens from the turn. */
|
|
17
|
+
export class AcpUsage {
|
|
18
|
+
#context;
|
|
19
|
+
#cost;
|
|
20
|
+
#tokens;
|
|
21
|
+
/** Apply one session update. A `usage_update` replaces the context and, when it carries one, the cost. */
|
|
22
|
+
add(update) {
|
|
23
|
+
if (update.sessionUpdate !== "usage_update")
|
|
24
|
+
return;
|
|
25
|
+
this.#context = { used: update.used, size: update.size };
|
|
26
|
+
// An omitted cost leaves the last one; null clears it; a value replaces it.
|
|
27
|
+
if (update.cost === undefined)
|
|
28
|
+
return;
|
|
29
|
+
this.#cost = update.cost === null ? undefined : { amount: update.cost.amount, currency: update.cost.currency };
|
|
30
|
+
}
|
|
31
|
+
/** Read the unstable usage from a prompt response; a malformed field is dropped. */
|
|
32
|
+
setPromptUsage(response) {
|
|
33
|
+
const usage = tokenUsageSchema.safeParse(response.usage);
|
|
34
|
+
if (!usage.success)
|
|
35
|
+
return;
|
|
36
|
+
const { totalTokens, inputTokens, outputTokens, thoughtTokens, cachedReadTokens, cachedWriteTokens } = usage.data;
|
|
37
|
+
this.#tokens = {
|
|
38
|
+
totalTokens,
|
|
39
|
+
inputTokens,
|
|
40
|
+
outputTokens,
|
|
41
|
+
...(thoughtTokens === null || thoughtTokens === undefined ? {} : { thoughtTokens }),
|
|
42
|
+
...(cachedReadTokens === null || cachedReadTokens === undefined ? {} : { cachedReadTokens }),
|
|
43
|
+
...(cachedWriteTokens === null || cachedWriteTokens === undefined ? {} : { cachedWriteTokens }),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** What the harness reported so far; absent when it reported nothing. */
|
|
47
|
+
get value() {
|
|
48
|
+
if (this.#context === undefined && this.#cost === undefined && this.#tokens === undefined)
|
|
49
|
+
return undefined;
|
|
50
|
+
return {
|
|
51
|
+
...(this.#context === undefined ? {} : { context: this.#context }),
|
|
52
|
+
...(this.#cost === undefined ? {} : { cost: this.#cost }),
|
|
53
|
+
...(this.#tokens === undefined ? {} : { tokens: this.#tokens }),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { RunnerDefinition } from "../../engine/registry.js";
|
|
3
|
+
declare const noopRunnerParamsSchema: z.ZodObject<{}, z.core.$strict>;
|
|
4
|
+
type NoopRunnerParams = z.output<typeof noopRunnerParamsSchema>;
|
|
5
|
+
/**
|
|
6
|
+
* The baseline runner: start nothing, call nothing, and record an empty trace so a judge that
|
|
7
|
+
* asks for one is evaluated. A case that still passes here does not depend on the agent. It
|
|
8
|
+
* declares HOME support because it starts no harness: the case's `~/` entries are placed and
|
|
9
|
+
* snapshotted like any other run, so a `~/` restraint is proved vacuous or not.
|
|
10
|
+
*/
|
|
11
|
+
export declare const noopRunner: RunnerDefinition<NoopRunnerParams>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const noopRunnerParamsSchema = z.strictObject({});
|
|
3
|
+
/**
|
|
4
|
+
* The baseline runner: start nothing, call nothing, and record an empty trace so a judge that
|
|
5
|
+
* asks for one is evaluated. A case that still passes here does not depend on the agent. It
|
|
6
|
+
* declares HOME support because it starts no harness: the case's `~/` entries are placed and
|
|
7
|
+
* snapshotted like any other run, so a `~/` restraint is proved vacuous or not.
|
|
8
|
+
*/
|
|
9
|
+
export const noopRunner = {
|
|
10
|
+
type: "noop",
|
|
11
|
+
params: noopRunnerParamsSchema,
|
|
12
|
+
mocks: true,
|
|
13
|
+
home: true,
|
|
14
|
+
create() {
|
|
15
|
+
return new NoopRunner();
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
class NoopRunner {
|
|
19
|
+
run = async (_context) => ({ finalText: null, diagnostics: [] });
|
|
20
|
+
stop = async () => { };
|
|
21
|
+
trace = () => [];
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { RunnerDefinition } from "../../engine/registry.js";
|
|
3
|
+
declare const piRunnerParamsSchema: z.ZodObject<{
|
|
4
|
+
model: z.ZodString;
|
|
5
|
+
thinking: z.ZodDefault<z.ZodEnum<{
|
|
6
|
+
off: "off";
|
|
7
|
+
minimal: "minimal";
|
|
8
|
+
low: "low";
|
|
9
|
+
medium: "medium";
|
|
10
|
+
high: "high";
|
|
11
|
+
xhigh: "xhigh";
|
|
12
|
+
max: "max";
|
|
13
|
+
}>>;
|
|
14
|
+
skills: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
15
|
+
executable: z.ZodDefault<z.ZodString>;
|
|
16
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
17
|
+
}, z.core.$strict>;
|
|
18
|
+
type PiRunnerParams = z.output<typeof piRunnerParamsSchema>;
|
|
19
|
+
/** Pi native RPC runner: one user message, autonomous tool turns, Seatbelt isolation. */
|
|
20
|
+
export declare const piRunner: RunnerDefinition<PiRunnerParams>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { cp, copyFile, mkdir, stat } from "node:fs/promises";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { ConfigError, errorMessage, resolveAuthoringAsset } from "../../engine/config.js";
|
|
5
|
+
import { authoringCopyFilter } from "../../engine/files.js";
|
|
6
|
+
import { nonEmptyTrimmedString } from "../../engine/params.js";
|
|
7
|
+
import { assertSeatbeltAvailable } from "../../platform/macos/policy.js";
|
|
8
|
+
import { thinkingSchema } from "./params.js";
|
|
9
|
+
import { invokePi, resolvePiExecutable } from "./session.js";
|
|
10
|
+
const piRunnerParamsSchema = z.strictObject({
|
|
11
|
+
model: nonEmptyTrimmedString,
|
|
12
|
+
thinking: thinkingSchema.default("medium"),
|
|
13
|
+
skills: z.array(z.string().min(1, "must be a non-empty relative path")).default([]),
|
|
14
|
+
executable: nonEmptyTrimmedString.default("pi"),
|
|
15
|
+
apiKey: nonEmptyTrimmedString.optional(),
|
|
16
|
+
});
|
|
17
|
+
/** Pi native RPC runner: one user message, autonomous tool turns, Seatbelt isolation. */
|
|
18
|
+
export const piRunner = {
|
|
19
|
+
type: "pi",
|
|
20
|
+
params: piRunnerParamsSchema,
|
|
21
|
+
async create(params, context) {
|
|
22
|
+
await resolvePiExecutable(params.executable);
|
|
23
|
+
await assertSkillsAvailable(params.skills, context.authoringDir);
|
|
24
|
+
assertSeatbeltAvailable();
|
|
25
|
+
return new PiRunner(params, context.authoringDir);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
class PiRunner {
|
|
29
|
+
#params;
|
|
30
|
+
#authoringDir;
|
|
31
|
+
#stop;
|
|
32
|
+
constructor(params, authoringDir) {
|
|
33
|
+
this.#params = params;
|
|
34
|
+
this.#authoringDir = authoringDir;
|
|
35
|
+
}
|
|
36
|
+
run = async (context) => {
|
|
37
|
+
const params = this.#params;
|
|
38
|
+
const skills = await stageSkills(params.skills, this.#authoringDir, join(context.configDir, "skills"));
|
|
39
|
+
const result = await invokePi({
|
|
40
|
+
executable: params.executable,
|
|
41
|
+
runRootDir: context.runRootDir,
|
|
42
|
+
model: params.model,
|
|
43
|
+
thinking: params.thinking,
|
|
44
|
+
dirs: {
|
|
45
|
+
workspaceDir: context.workspaceDir,
|
|
46
|
+
homeDir: context.homeDir,
|
|
47
|
+
configDir: context.configDir,
|
|
48
|
+
scratchDir: context.tmpDir,
|
|
49
|
+
},
|
|
50
|
+
skills,
|
|
51
|
+
tools: ["read", "write", "edit", "bash"],
|
|
52
|
+
...(params.apiKey === undefined ? {} : { apiKey: params.apiKey }),
|
|
53
|
+
allowNetwork: true,
|
|
54
|
+
}, context.input, {
|
|
55
|
+
timeoutMs: context.timeoutMs,
|
|
56
|
+
signal: context.signal,
|
|
57
|
+
onProcess: (stop) => {
|
|
58
|
+
this.#stop = stop;
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
return { finalText: result.finalText, diagnostics: result.diagnostics };
|
|
62
|
+
};
|
|
63
|
+
stop = async () => {
|
|
64
|
+
await this.#stop?.();
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
async function inspectSkills(skills, authoringDir) {
|
|
68
|
+
const inspected = [];
|
|
69
|
+
for (const skill of skills) {
|
|
70
|
+
const source = resolveAuthoringAsset(authoringDir, skill, "pi runner skill");
|
|
71
|
+
let info;
|
|
72
|
+
try {
|
|
73
|
+
info = await stat(source);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
throw new ConfigError(`pi runner skill ${skill} is not accessible: ${errorMessage(error)}`);
|
|
77
|
+
}
|
|
78
|
+
if (info.isDirectory()) {
|
|
79
|
+
try {
|
|
80
|
+
await stat(join(source, "SKILL.md"));
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
throw new ConfigError(`pi runner skill directory ${skill} has no SKILL.md`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else if (!info.isFile()) {
|
|
87
|
+
throw new ConfigError(`pi runner skill ${skill} must be a directory or a Markdown file`);
|
|
88
|
+
}
|
|
89
|
+
inspected.push({ skill, source, info });
|
|
90
|
+
}
|
|
91
|
+
return inspected;
|
|
92
|
+
}
|
|
93
|
+
async function assertSkillsAvailable(skills, authoringDir) {
|
|
94
|
+
await inspectSkills(skills, authoringDir);
|
|
95
|
+
}
|
|
96
|
+
async function stageSkills(skills, authoringDir, destination) {
|
|
97
|
+
await mkdir(destination, { recursive: true, mode: 0o700 });
|
|
98
|
+
const staged = [];
|
|
99
|
+
const names = new Set();
|
|
100
|
+
for (const { source, info } of await inspectSkills(skills, authoringDir)) {
|
|
101
|
+
const name = basename(source);
|
|
102
|
+
if (names.has(name)) {
|
|
103
|
+
throw new ConfigError(`pi runner skills contain duplicate name: ${name}`);
|
|
104
|
+
}
|
|
105
|
+
names.add(name);
|
|
106
|
+
const target = join(destination, name);
|
|
107
|
+
if (info.isDirectory()) {
|
|
108
|
+
await cp(source, target, { recursive: true, filter: authoringCopyFilter(source) });
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
await copyFile(source, target);
|
|
112
|
+
}
|
|
113
|
+
staged.push(target);
|
|
114
|
+
}
|
|
115
|
+
return staged;
|
|
116
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
export interface PiEvent {
|
|
3
|
+
readonly type: string;
|
|
4
|
+
readonly [key: string]: unknown;
|
|
5
|
+
}
|
|
6
|
+
export declare class PiRpcError extends Error {
|
|
7
|
+
readonly diagnostics: readonly string[];
|
|
8
|
+
constructor(message: string, diagnostics?: readonly string[]);
|
|
9
|
+
}
|
|
10
|
+
export interface PiProcessOptions {
|
|
11
|
+
readonly spawn: () => ChildProcessWithoutNullStreams;
|
|
12
|
+
readonly commandTimeoutMs?: number;
|
|
13
|
+
readonly stopGraceMs?: number;
|
|
14
|
+
}
|
|
15
|
+
/** Minimal strict-JSONL client for Pi RPC mode. */
|
|
16
|
+
export declare class PiProcess {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(options: PiProcessOptions);
|
|
19
|
+
get stderrTail(): string;
|
|
20
|
+
get protocolErrors(): readonly string[];
|
|
21
|
+
get errorEvents(): readonly string[];
|
|
22
|
+
get exited(): boolean;
|
|
23
|
+
get pid(): number | undefined;
|
|
24
|
+
/** Send a command and resolve its matching successful response. */
|
|
25
|
+
command(type: string, payload?: Record<string, unknown>, timeoutMs?: number): Promise<unknown>;
|
|
26
|
+
/** Register a waiter before issuing the command that triggers the event. */
|
|
27
|
+
waitForEvent(type: string, timeoutMs: number, signal?: AbortSignal): Promise<PiEvent>;
|
|
28
|
+
/** Close stdin, then escalate through SIGTERM and SIGKILL for the process group. */
|
|
29
|
+
stop(): Promise<void>;
|
|
30
|
+
}
|