From fff3dd9ecb4bc74f5bbbba560a50735c2a9dfc95 Mon Sep 17 00:00:00 2001 From: Enver Date: Tue, 24 Nov 2026 05:24:45 -0400 Subject: [PATCH] feat(pi-extension): keep host system prompt parts separate Pi hosts pass the system prompt as an array of parts and place provider cache breakpoints between them. Joining the parts into one string moved the whole prompt whenever any part changed. Append the guardrail prompt as its own part for arrays and keep the joined string for string or missing prompts. --- CHANGELOG.md | 9 +++++++++ src/pi-extension.ts | 15 ++++++++++++--- tests/node/pi-extension.test.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9a8bb71..d3fd8a91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,15 @@ Releases before 1.1.3 predate this file; their notes live on the ## [Unreleased] +### Changed + +- The native Pi extension now appends its operating guidance as a separate + system prompt part when the host passes the system prompt as an array of + parts, instead of joining every part into one string. Hosts place provider + prompt-cache breakpoints between parts, so the host's stable parts keep their + cached prefix. A string or missing system prompt is still joined with the + guidance as before. + ## [2.9.1] - 2026-11-04 ### Security diff --git a/src/pi-extension.ts b/src/pi-extension.ts index 0637e09a..58dc9ad7 100644 --- a/src/pi-extension.ts +++ b/src/pi-extension.ts @@ -1001,9 +1001,18 @@ export function createPiExtension(options: any = {}) { renderResult: summaryRenderResult, }); - pi.on("before_agent_start", (event) => ({ - systemPrompt: `${String(event.systemPrompt || "")}\n\n${agentSystemPrompt(options.guardrails)}`, - })); + pi.on("before_agent_start", (event) => { + const guardrailPrompt = agentSystemPrompt(options.guardrails); + // Newer hosts pass the system prompt as separate parts and place provider + // cache breakpoints between them. Append our text as its own part so the + // host's stable parts keep their cached prefix when another part changes. + if (Array.isArray(event.systemPrompt)) { + return { systemPrompt: [...event.systemPrompt, guardrailPrompt] }; + } + return { + systemPrompt: `${String(event.systemPrompt || "")}\n\n${guardrailPrompt}`, + }; + }); pi.on("agent_end", () => { const sendMessage = pi.sendMessage; diff --git a/tests/node/pi-extension.test.ts b/tests/node/pi-extension.test.ts index bea9d513..4ee72b4c 100644 --- a/tests/node/pi-extension.test.ts +++ b/tests/node/pi-extension.test.ts @@ -235,6 +235,36 @@ test("native Pi extension registers persistent tools and records its supplied st } }); +test("native Pi extension appends guardrails as a separate part when the host passes prompt parts", () => { + const pi = new FakePi(); + createPiExtension({ browser: new FakeBrowser() })(pi); + const handler = pi.handlers.get("before_agent_start"); + + const parts = ["stable host policy", "recalled memories"]; + const result = handler({ systemPrompt: parts }); + + assert.ok(Array.isArray(result.systemPrompt)); + assert.equal(result.systemPrompt.length, 3); + assert.deepEqual(result.systemPrompt.slice(0, 2), ["stable host policy", "recalled memories"]); + assert.match(result.systemPrompt[2], /^# Operating the browser/); + assert.deepEqual(parts, ["stable host policy", "recalled memories"]); +}); + +test("native Pi extension keeps joining a string or missing system prompt", () => { + const pi = new FakePi(); + createPiExtension({ browser: new FakeBrowser() })(pi); + const handler = pi.handlers.get("before_agent_start"); + + const fromString = handler({ systemPrompt: "base" }); + assert.equal(Array.isArray(fromString.systemPrompt), false); + assert.match(fromString.systemPrompt, /^base\n\n# Operating the browser/); + + const fromUndefined = handler({}); + assert.equal(Array.isArray(fromUndefined.systemPrompt), false); + assert.match(fromUndefined.systemPrompt, /^\n\n# Operating the browser/); +}); + + test("native Pi extension grounds required checklist items in proof frames", async () => { const { dir, screenshot } = await fixture(); const browser = new FakeBrowser({ screenshot });