Restore preset-owned prompt/tool artifacts in wrapper path: add systemPromptPath artifact, write system-prompt.md, pass --tools and --append-system-prompt flags when present, preserve meta fields, and update tests

This commit is contained in:
pi
2026-04-12 13:26:58 +01:00
parent 2c61bd0753
commit 4826c103a2
5 changed files with 126 additions and 6 deletions

View File

@@ -107,12 +107,91 @@ test("wrapper marks anthropic child run as a subagent child", async () => {
assert.equal(captured.flags.PI_SUBAGENTS_CHILD, "1");
});
test("wrapper ignores stale tool and system prompt metadata", async () => {
test("wrapper respects tools metadata but ignores stale system prompt file", async () => {
const captured = await runWrapperWithFakePi("anthropic/claude-sonnet-4-5");
assert.equal(captured.flags.argv.includes("--tools"), false);
// tools metadata should be passed even if there are no tool binaries installed.
assert.equal(captured.flags.argv.includes("--tools"), true);
// system prompt file is not present in this test, so the flag should not be passed.
assert.equal(captured.flags.argv.includes("--append-system-prompt"), false);
});
test("wrapper passes --append-system-prompt when the prompt file exists", async () => {
const dir = await mkdtemp(join(tmpdir(), "pi-subagents-wrapper-"));
const metaPath = join(dir, "meta.json");
const resultPath = join(dir, "result.json");
const capturePath = join(dir, "capture.json");
const piPath = join(dir, "pi");
const promptPath = join(dir, "system-prompt.md");
await writeFile(
piPath,
[
`#!${process.execPath}`,
"const fs = require('fs');",
`const capturePath = ${JSON.stringify(capturePath)};`,
"const obj = {",
" PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR: process.env.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR || '',",
" PI_SUBAGENTS_CHILD: process.env.PI_SUBAGENTS_CHILD || '',",
" argv: process.argv.slice(2)",
"};",
"fs.writeFileSync(capturePath, JSON.stringify(obj), 'utf8');",
"console.log(JSON.stringify({type:'message_end',message:{role:'assistant',content:[{type:'text',text:'done'}],model:'github-copilot/gpt-4o',stopReason:'stop'}}));",
].join("\n"),
"utf8",
);
await chmod(piPath, 0o755);
// create the system prompt file so the wrapper will pass --append-system-prompt
await writeFile(promptPath, "System prompt here", "utf8");
await writeFile(
metaPath,
JSON.stringify(
{
runId: "run-1",
mode: "single",
task: "inspect auth",
cwd: dir,
requestedModel: "anthropic/claude-sonnet-4-5",
resolvedModel: "anthropic/claude-sonnet-4-5",
startedAt: "2026-04-09T00:00:00.000Z",
sessionPath: join(dir, "child-session.jsonl"),
eventsPath: join(dir, "events.jsonl"),
resultPath,
stdoutPath: join(dir, "stdout.log"),
stderrPath: join(dir, "stderr.log"),
transcriptPath: join(dir, "transcript.log"),
tools: ["read", "grep"],
systemPromptPath: promptPath,
},
null,
2,
),
"utf8",
);
const wrapperPath = join(dirname(fileURLToPath(import.meta.url)), "cli.mjs");
const child = spawn(process.execPath, [wrapperPath, metaPath], {
env: {
...process.env,
PATH: dir,
},
stdio: ["ignore", "pipe", "pipe"],
});
const exitCode = await waitForExit(child);
assert.equal(exitCode, 0);
const captureJson = JSON.parse(await readFile(capturePath, "utf8"));
const argv = captureJson.argv;
assert.equal(argv.includes("--tools"), true);
const toolsIndex = argv.indexOf("--tools");
assert.equal(argv[toolsIndex + 1], "read,grep");
assert.equal(argv.includes("--append-system-prompt"), true);
const idx = argv.indexOf("--append-system-prompt");
assert.equal(argv[idx + 1], promptPath);
});
test("wrapper marks github-copilot child runs as agent-initiated", async () => {
const captured = await runWrapperWithFakePi("github-copilot/gpt-4o");
assert.equal(captured.flags.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR, "agent");