import { describe, expect, it } from "vitest"; import { SystemPromptGuard } from "./system-prompt-guard.js"; const payload = (system: string) => ({ system: [{ type: "text", text: "You are Claude Code." }, { type: "text", text: system }], messages: [{ role: "user", content: "hi" }] }); describe("SystemPromptGuard", () => { it("swaps pi's prompt for pi-pi's in a run that started without before_agent_start", () => { const guard = new SystemPromptGuard(); guard.prompted("BASE", "PI-PI"); guard.runStarted(); expect(guard.rewrite(payload("PI-PI"), "PI-PI")).toBeUndefined(); guard.runEnded(); guard.runStarted(); expect(guard.rewrite(payload("BASE"), "PI-PI")).toEqual(payload("PI-PI")); // pi rebuilt its base for a new tool set since. expect(guard.rewrite(payload("BASE 2"), "BASE 2")).toEqual(payload("PI-PI")); expect(guard.rewrite(payload("something else"), "BASE 2")).toBeUndefined(); }); it("leaves a prompted run alone, and does not reuse a one-off prompt for later runs", () => { const guard = new SystemPromptGuard(); guard.prompted("BASE", "PI-PI"); guard.prompted("BASE"); guard.runStarted(); expect(guard.rewrite(payload("Superseded."), "Superseded.")).toBeUndefined(); guard.runEnded(); guard.runStarted(); expect(guard.rewrite(payload("BASE"), "Superseded.")).toEqual(payload("PI-PI")); }); it("also swaps a base pi rebuilt during the last run, which keeps its opening", () => { const base = `You are pi. ${"x".repeat(300)}`; const guard = new SystemPromptGuard(); guard.prompted(base, "PI-PI"); guard.runStarted(); guard.runEnded(); guard.runStarted(); const rebuilt = payload(`${base}\nNew tool section.`); expect(guard.rewrite(rebuilt, "PI-PI")).toEqual(payload("PI-PI")); // Untouched parts are not copied. expect((guard.rewrite(rebuilt, "PI-PI") as any).messages).toBe(rebuilt.messages); }); it("rewrites only where providers put the system prompt, never conversation content that quotes it", () => { const base = `You are pi. ${"x".repeat(300)}`; const guard = new SystemPromptGuard(); guard.prompted(base, "PI-PI"); guard.runStarted(); guard.runEnded(); guard.runStarted(); const quoted = `${base}\nPlease explain this prompt`; const completions = { messages: [{ role: "developer", content: base }, { role: "user", content: quoted }, { role: "tool", content: base }] }; expect(guard.rewrite(completions, base)).toEqual({ messages: [{ role: "developer", content: "PI-PI" }, { role: "user", content: quoted }, { role: "tool", content: base }] }); expect(guard.rewrite({ instructions: base, input: [{ role: "user", content: base }] }, base)).toEqual({ instructions: "PI-PI", input: [{ role: "user", content: base }] }); expect(guard.rewrite({ contents: [], config: { systemInstruction: base, temperature: 1 } }, base)).toEqual({ contents: [], config: { systemInstruction: "PI-PI", temperature: 1 } }); expect(guard.rewrite({ model: "m", context: { systemPrompt: base, messages: [{ role: "user", content: base }] } }, base)).toEqual({ model: "m", context: { systemPrompt: "PI-PI", messages: [{ role: "user", content: base }] } }); expect(guard.rewrite({ messages: [{ role: "user", content: base }] }, base)).toBeUndefined(); }); it("does nothing before pi-pi has built a prompt", () => { const guard = new SystemPromptGuard(); guard.runStarted(); expect(guard.rewrite(payload("BASE"), "BASE")).toBeUndefined(); }); });