refactor: remove role metadata from subagent artifacts

This commit is contained in:
pi
2026-04-12 06:57:18 +01:00
parent 4a0f78f9fb
commit c8859b626b
8 changed files with 16 additions and 43 deletions

View File

@@ -11,11 +11,12 @@ test("createRunArtifacts writes metadata and reserves stable artifact paths", as
const artifacts = await createRunArtifacts(cwd, { const artifacts = await createRunArtifacts(cwd, {
runId: "run-1", runId: "run-1",
task: "inspect auth", task: "inspect auth",
systemPrompt: "You are scout",
}); });
assert.equal(artifacts.runId, "run-1"); assert.equal(artifacts.runId, "run-1");
assert.match(artifacts.dir, /\.pi\/subagents\/runs\/run-1$/); assert.match(artifacts.dir, /\.pi\/subagents\/runs\/run-1$/);
assert.equal(JSON.parse(await readFile(artifacts.metaPath, "utf8")).task, "inspect auth"); const meta = JSON.parse(await readFile(artifacts.metaPath, "utf8"));
assert.equal(await readFile(artifacts.systemPromptPath, "utf8"), "You are scout"); assert.equal(meta.task, "inspect auth");
assert.equal("systemPromptPath" in meta, false);
await assert.rejects(readFile(join(artifacts.dir, "system-prompt.md"), "utf8"));
}); });

View File

@@ -12,12 +12,11 @@ export interface RunArtifacts {
stderrPath: string; stderrPath: string;
transcriptPath: string; transcriptPath: string;
sessionPath: string; sessionPath: string;
systemPromptPath: string;
} }
export async function createRunArtifacts( export async function createRunArtifacts(
cwd: string, cwd: string,
meta: Record<string, unknown> & { runId?: string; systemPrompt?: string }, meta: Record<string, unknown> & { runId?: string },
): Promise<RunArtifacts> { ): Promise<RunArtifacts> {
const runId = meta.runId ?? randomUUID(); const runId = meta.runId ?? randomUUID();
const dir = resolve(cwd, ".pi", "subagents", "runs", runId); const dir = resolve(cwd, ".pi", "subagents", "runs", runId);
@@ -33,7 +32,6 @@ export async function createRunArtifacts(
stderrPath: join(dir, "stderr.log"), stderrPath: join(dir, "stderr.log"),
transcriptPath: join(dir, "transcript.log"), transcriptPath: join(dir, "transcript.log"),
sessionPath: join(dir, "child-session.jsonl"), sessionPath: join(dir, "child-session.jsonl"),
systemPromptPath: join(dir, "system-prompt.md"),
}; };
await writeFile( await writeFile(
@@ -48,7 +46,6 @@ export async function createRunArtifacts(
stdoutPath: artifacts.stdoutPath, stdoutPath: artifacts.stdoutPath,
stderrPath: artifacts.stderrPath, stderrPath: artifacts.stderrPath,
transcriptPath: artifacts.transcriptPath, transcriptPath: artifacts.transcriptPath,
systemPromptPath: artifacts.systemPromptPath,
}, },
null, null,
2, 2,
@@ -56,7 +53,6 @@ export async function createRunArtifacts(
"utf8", "utf8",
); );
await writeFile(artifacts.systemPromptPath, typeof meta.systemPrompt === "string" ? meta.systemPrompt : "", "utf8");
await writeFile(artifacts.eventsPath, "", "utf8"); await writeFile(artifacts.eventsPath, "", "utf8");
await writeFile(artifacts.stdoutPath, "", "utf8"); await writeFile(artifacts.stdoutPath, "", "utf8");
await writeFile(artifacts.stderrPath, "", "utf8"); await writeFile(artifacts.stderrPath, "", "utf8");

View File

@@ -30,8 +30,6 @@ test("createProcessSingleRunner launches wrapper without tmux and returns monito
{ {
runId: meta.runId, runId: meta.runId,
mode: meta.mode, mode: meta.mode,
agent: meta.agent,
agentSource: meta.agentSource,
task: meta.task, task: meta.task,
requestedModel: meta.requestedModel, requestedModel: meta.requestedModel,
resolvedModel: meta.resolvedModel, resolvedModel: meta.resolvedModel,
@@ -60,8 +58,6 @@ test("createProcessSingleRunner launches wrapper without tmux and returns monito
cwd, cwd,
meta: { meta: {
mode: "single", mode: "single",
agent: "scout",
agentSource: "builtin",
task: "inspect auth", task: "inspect auth",
requestedModel: "openai/gpt-5", requestedModel: "openai/gpt-5",
resolvedModel: "openai/gpt-5", resolvedModel: "openai/gpt-5",
@@ -95,8 +91,6 @@ test("createProcessSingleRunner writes error result.json when wrapper launch fai
cwd, cwd,
meta: { meta: {
mode: "single", mode: "single",
agent: "scout",
agentSource: "builtin",
task: "inspect auth", task: "inspect auth",
requestedModel: "openai/gpt-5", requestedModel: "openai/gpt-5",
resolvedModel: "openai/gpt-5", resolvedModel: "openai/gpt-5",
@@ -109,5 +103,6 @@ test("createProcessSingleRunner writes error result.json when wrapper launch fai
const saved = JSON.parse(await readFile(result.resultPath!, "utf8")); const saved = JSON.parse(await readFile(result.resultPath!, "utf8"));
assert.equal(saved.exitCode, 1); assert.equal(saved.exitCode, 1);
assert.equal("agent" in saved, false);
assert.match(saved.errorMessage ?? "", /spawn boom/); assert.match(saved.errorMessage ?? "", /spawn boom/);
}); });

View File

@@ -11,8 +11,6 @@ function makeLaunchFailureResult(artifacts: any, meta: Record<string, unknown>,
mode: meta.mode, mode: meta.mode,
taskIndex: meta.taskIndex, taskIndex: meta.taskIndex,
step: meta.step, step: meta.step,
agent: meta.agent,
agentSource: meta.agentSource,
task: meta.task, task: meta.task,
cwd, cwd,
requestedModel: meta.requestedModel, requestedModel: meta.requestedModel,

View File

@@ -34,8 +34,6 @@ function makeResult(meta, startedAt, input = {}) {
mode: meta.mode, mode: meta.mode,
taskIndex: meta.taskIndex, taskIndex: meta.taskIndex,
step: meta.step, step: meta.step,
agent: meta.agent,
agentSource: meta.agentSource,
task: meta.task, task: meta.task,
cwd: meta.cwd, cwd: meta.cwd,
requestedModel: meta.requestedModel, requestedModel: meta.requestedModel,
@@ -69,8 +67,6 @@ async function runWrapper(meta, startedAt) {
const args = ["--mode", "json", "--session", meta.sessionPath]; const args = ["--mode", "json", "--session", meta.sessionPath];
if (effectiveModel) args.push("--model", effectiveModel); if (effectiveModel) args.push("--model", effectiveModel);
if (Array.isArray(meta.tools) && meta.tools.length > 0) args.push("--tools", meta.tools.join(","));
if (meta.systemPromptPath) args.push("--append-system-prompt", meta.systemPromptPath);
args.push(meta.task); args.push(meta.task);
let finalText = ""; let finalText = "";

View File

@@ -32,8 +32,6 @@ async function runWrapperWithFakePi(requestedModel: string, resolvedModel?: stri
const capturePath = join(dir, "capture.json"); const capturePath = join(dir, "capture.json");
const piPath = join(dir, "pi"); const piPath = join(dir, "pi");
// The fake `pi` is a small Node script that writes a JSON capture file
// including relevant PI_* environment variables and the argv it received.
const resolved = typeof resolvedModel === "string" ? resolvedModel : requestedModel; const resolved = typeof resolvedModel === "string" ? resolvedModel : requestedModel;
await writeFile( await writeFile(
piPath, piPath,
@@ -59,8 +57,6 @@ async function runWrapperWithFakePi(requestedModel: string, resolvedModel?: stri
{ {
runId: "run-1", runId: "run-1",
mode: "single", mode: "single",
agent: "scout",
agentSource: "builtin",
task: "inspect auth", task: "inspect auth",
cwd: dir, cwd: dir,
requestedModel, requestedModel,
@@ -72,6 +68,7 @@ async function runWrapperWithFakePi(requestedModel: string, resolvedModel?: stri
stdoutPath: join(dir, "stdout.log"), stdoutPath: join(dir, "stdout.log"),
stderrPath: join(dir, "stderr.log"), stderrPath: join(dir, "stderr.log"),
transcriptPath: join(dir, "transcript.log"), transcriptPath: join(dir, "transcript.log"),
tools: ["read", "grep"],
systemPromptPath: join(dir, "system-prompt.md"), systemPromptPath: join(dir, "system-prompt.md"),
}, },
null, null,
@@ -96,7 +93,6 @@ async function runWrapperWithFakePi(requestedModel: string, resolvedModel?: stri
return { flags: captureJson }; return { flags: captureJson };
} }
// Dedicated tests: every child run must have PI_SUBAGENTS_CHILD=1
test("wrapper marks github-copilot child run as a subagent child", async () => { test("wrapper marks github-copilot child run as a subagent child", async () => {
const captured = await runWrapperWithFakePi("github-copilot/gpt-4o"); const captured = await runWrapperWithFakePi("github-copilot/gpt-4o");
assert.equal(captured.flags.PI_SUBAGENTS_CHILD, "1"); assert.equal(captured.flags.PI_SUBAGENTS_CHILD, "1");
@@ -107,6 +103,12 @@ test("wrapper marks anthropic child run as a subagent child", async () => {
assert.equal(captured.flags.PI_SUBAGENTS_CHILD, "1"); assert.equal(captured.flags.PI_SUBAGENTS_CHILD, "1");
}); });
test("wrapper ignores stale tool and system prompt metadata", async () => {
const captured = await runWrapperWithFakePi("anthropic/claude-sonnet-4-5");
assert.equal(captured.flags.argv.includes("--tools"), false);
assert.equal(captured.flags.argv.includes("--append-system-prompt"), false);
});
test("wrapper marks github-copilot child runs as agent-initiated", async () => { test("wrapper marks github-copilot child runs as agent-initiated", async () => {
const captured = await runWrapperWithFakePi("github-copilot/gpt-4o"); const captured = await runWrapperWithFakePi("github-copilot/gpt-4o");
assert.equal(captured.flags.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR, "agent"); assert.equal(captured.flags.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR, "agent");
@@ -115,25 +117,19 @@ test("wrapper marks github-copilot child runs as agent-initiated", async () => {
test("wrapper leaves non-copilot child runs unchanged", async () => { test("wrapper leaves non-copilot child runs unchanged", async () => {
const captured = await runWrapperWithFakePi("anthropic/claude-sonnet-4-5"); const captured = await runWrapperWithFakePi("anthropic/claude-sonnet-4-5");
// The wrapper should not inject the copilot initiator for non-copilot models.
assert.equal(captured.flags.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR, ""); assert.equal(captured.flags.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR, "");
assert.equal(captured.flags.PI_SUBAGENTS_CHILD, "1"); assert.equal(captured.flags.PI_SUBAGENTS_CHILD, "1");
}); });
// Regression test: ensure when requestedModel and resolvedModel differ, the
// wrapper uses the same effective model for the child --model arg and the
// copilot initiator env flag.
test("wrapper uses effective model for both argv and env when requested/resolved differ", async () => { test("wrapper uses effective model for both argv and env when requested/resolved differ", async () => {
const requested = "anthropic/claude-sonnet-4-5"; const requested = "anthropic/claude-sonnet-4-5";
const resolved = "github-copilot/gpt-4o"; const resolved = "github-copilot/gpt-4o";
const captured = await runWrapperWithFakePi(requested, resolved); const captured = await runWrapperWithFakePi(requested, resolved);
// The effective model should be the resolved model in this case.
assert.equal(captured.flags.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR, "agent"); assert.equal(captured.flags.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR, "agent");
assert.equal(captured.flags.PI_SUBAGENTS_CHILD, "1"); assert.equal(captured.flags.PI_SUBAGENTS_CHILD, "1");
// Verify the child argv contains the effective model after a --model flag.
const argv = captured.flags.argv; const argv = captured.flags.argv;
const modelIndex = argv.indexOf("--model"); const modelIndex = argv.indexOf("--model");
assert.ok(modelIndex >= 0, "expected --model in argv"); assert.ok(modelIndex >= 0, "expected --model in argv");
@@ -151,8 +147,6 @@ test("wrapper exits and writes result.json when the pi child cannot be spawned",
{ {
runId: "run-1", runId: "run-1",
mode: "single", mode: "single",
agent: "scout",
agentSource: "builtin",
task: "inspect auth", task: "inspect auth",
cwd: dir, cwd: dir,
requestedModel: "anthropic/claude-sonnet-4-5", requestedModel: "anthropic/claude-sonnet-4-5",
@@ -186,7 +180,8 @@ test("wrapper exits and writes result.json when the pi child cannot be spawned",
const result = JSON.parse(await readFile(resultPath, "utf8")); const result = JSON.parse(await readFile(resultPath, "utf8"));
assert.equal(result.runId, "run-1"); assert.equal(result.runId, "run-1");
assert.equal(result.agent, "scout"); assert.equal(result.task, "inspect auth");
assert.equal("agent" in result, false);
assert.equal(result.exitCode, 1); assert.equal(result.exitCode, 1);
assert.match(result.errorMessage ?? "", /ENOENT|not found|spawn pi/i); assert.match(result.errorMessage ?? "", /ENOENT|not found|spawn pi/i);
}); });
@@ -217,8 +212,6 @@ test("wrapper does not exit early on non-terminal toolUse assistant messages", a
{ {
runId: "run-1", runId: "run-1",
mode: "single", mode: "single",
agent: "scout",
agentSource: "builtin",
task: "inspect auth", task: "inspect auth",
cwd: dir, cwd: dir,
requestedModel: "openai/gpt-5", requestedModel: "openai/gpt-5",
@@ -280,8 +273,6 @@ test("wrapper exits and writes result.json after terminal output even if the pi
{ {
runId: "run-1", runId: "run-1",
mode: "single", mode: "single",
agent: "scout",
agentSource: "builtin",
task: "inspect auth", task: "inspect auth",
cwd: dir, cwd: dir,
requestedModel: "openai/gpt-5", requestedModel: "openai/gpt-5",
@@ -341,8 +332,6 @@ test("wrapper still writes result.json when transcript/stdout artifact writes fa
{ {
runId: "run-1", runId: "run-1",
mode: "single", mode: "single",
agent: "scout",
agentSource: "builtin",
task: "inspect auth", task: "inspect auth",
cwd: dir, cwd: dir,
requestedModel: "openai/gpt-5", requestedModel: "openai/gpt-5",

View File

@@ -5,7 +5,6 @@ function shortenCommand(command) {
export function renderHeader(meta) { export function renderHeader(meta) {
return [ return [
"=== subagent ===", "=== subagent ===",
`Agent: ${meta.agent}`,
`Task: ${meta.task}`, `Task: ${meta.task}`,
`CWD: ${meta.cwd}`, `CWD: ${meta.cwd}`,
`Requested model: ${meta.requestedModel ?? "(default)"}`, `Requested model: ${meta.requestedModel ?? "(default)"}`,

View File

@@ -4,7 +4,6 @@ import { renderHeader, renderEventLine } from "./render.mjs";
test("renderHeader prints generic subagent metadata", () => { test("renderHeader prints generic subagent metadata", () => {
const header = renderHeader({ const header = renderHeader({
agent: "scout",
task: "Inspect authentication code", task: "Inspect authentication code",
cwd: "/repo", cwd: "/repo",
requestedModel: "anthropic/claude-sonnet-4-5", requestedModel: "anthropic/claude-sonnet-4-5",
@@ -13,7 +12,7 @@ test("renderHeader prints generic subagent metadata", () => {
}); });
assert.match(header, /^=== subagent ===/m); assert.match(header, /^=== subagent ===/m);
assert.match(header, /Agent: scout/); assert.doesNotMatch(header, /Agent:/);
assert.match(header, /Task: Inspect authentication code/); assert.match(header, /Task: Inspect authentication code/);
assert.match(header, /Session: \/repo\/\.pi\/subagents\/runs\/run-1\/child-session\.jsonl/); assert.match(header, /Session: \/repo\/\.pi\/subagents\/runs\/run-1\/child-session\.jsonl/);
}); });