Files
my-pi/pi-hermes-memory/tests/prompt-injection.test.ts
云服务部-叶林立andDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com> f9a7f743dd feat: 接入 Pi 内置 codemode,并修复全面审查发现的问题
- codemode:Tool Search 常驻(alwaysEnabled)、权限 allow(嵌套调用仍逐个过权限门)、
  tools.jsonc 登记使用规则;RTK 跳过 parentToolCallId 嵌套调用,避免丢失 bash
  structuredContent;Tool Search 目录过滤 hidden / codemode(script-only)exposure;
  扩展加载测试加载 builtin:codemode 并断言其常驻激活
- 搜索 key 不再经命令行参数传递:search-auth.mjs 改为 --set-stdin(先读校验再加锁),
  configure.sh 交互输入经管道、非交互用 --<provider> -
- CodeGraph 客户端监听子进程 stdin error,避免 EPIPE 未处理导致崩溃
- 移除 PI_TOOL_SEARCH/HERMES_MEMORY/RTK_CONFIG_PATH 环境变量入口,测试改用工厂 configPath 注入
- pi-subagents 固定 vitest / coverage-istanbul 4.1.10,修复 npm ls 版本冲突
- 补 pi-web-search / pi-worktree UPSTREAM.md 与 pi-minimal-footer AGENTS.md,
  files 白名单补齐并加测试;.codegraph/.gitignore 移出跟踪
- detect_kitty 优先识别官方安装目录

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 23:18:34 -07:00

196 lines
6.9 KiB
TypeScript

/**
* `before_agent_start` prompt injection (#251).
*
* pi 0.86+ passes a normalized *clone* of the prompt options and re-renders the
* prompt from it, so writing the memory context into `sections.memory_policy`
* keeps the prompt a set of named sections for later handlers and section-aware
* providers; returning `systemPrompt` forces the rendered text instead. The
* bundle's single system-prompt owner (extensions/system-prompt.ts) orders the
* section via `config/prompt/sections.jsonc`.
*
* 0.80.6-0.85.x put `systemPromptOptions` on the event too, but they pass the raw
* long-lived base options and only apply a returned `systemPrompt` — a mutation
* there reaches no prompt and leaks into `ctx.getSystemPromptOptions()`. The two
* shapes below are the ones `agent-session.js` actually builds on each side of
* that boundary, so the branch is tested against the real inputs rather than a
* hand-built `{}` that no SDK version produces.
*/
import assert from "node:assert/strict";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { after, beforeEach, describe, it } from "node:test";
// Bind the entry point to a disposable agent root, never the user's memory.
const root = await fs.mkdtemp(path.join(os.tmpdir(), "pi-prompt-injection-"));
const previousRoot = process.env.PI_CODING_AGENT_DIR;
process.env.PI_CODING_AGENT_DIR = root;
const configPath = path.join(root, "hermes-memory-config.json");
const { createHermesMemoryExtension } = await import("../src/index.js");
const registerExtension = createHermesMemoryExtension({ configPath });
const globalDir = path.join(root, "pi-hermes-memory");
const cwd = path.join(root, "workspace");
/** Options as 0.86+ normalizes them: `sections` and `forceSystemPrompt` always present. */
function structuredOptions(overrides: Record<string, unknown> = {}) {
return {
customPrompt: undefined,
forceSystemPrompt: undefined,
selectedTools: ["read"],
toolSnippets: {},
toolGuidelines: {},
promptGuidelines: [],
appendSystemPrompt: "",
sections: {},
cwd,
contextFiles: [],
skills: [],
...overrides,
};
}
/** Options as 0.80.x-0.85.x build them: the raw base object, no `sections`. */
function legacyOptions(overrides: Record<string, unknown> = {}) {
return {
cwd,
skills: [],
contextFiles: [],
customPrompt: undefined,
appendSystemPrompt: "",
selectedTools: ["read"],
toolSnippets: {},
promptGuidelines: [],
...overrides,
};
}
let handlers: Record<string, Array<(event: any, ctx: any) => any>>;
let ctx: any;
async function configure(overrides: Record<string, unknown> = {}) {
await fs.writeFile(configPath, JSON.stringify({
memoryMode: "policy-only",
memoryPolicyStyle: "full",
lazyInitialization: true,
reviewEnabled: false,
correctionDetection: false,
flushOnCompact: false,
flushOnShutdown: false,
...overrides,
}));
}
function register() {
registerExtension({
on(event: string, handler: any) { (handlers[event] ??= []).push(handler); },
registerTool() {},
registerCommand() {},
} as any);
}
async function emit(event: string, data: any = {}) {
let result: any;
for (const handler of handlers[event] ?? []) result = (await handler(data, ctx)) ?? result;
return result;
}
async function registerWith(overrides: Record<string, unknown> = {}) {
handlers = {};
await configure(overrides);
register();
await emit("session_start");
}
beforeEach(async () => {
await fs.mkdir(globalDir, { recursive: true });
await fs.mkdir(cwd, { recursive: true });
ctx = {
cwd,
hasUI: false,
sessionManager: { getBranch: () => [], getSessionFile: () => undefined },
ui: { notify: () => {} },
};
});
after(async () => {
if (previousRoot === undefined) delete process.env.PI_CODING_AGENT_DIR;
else process.env.PI_CODING_AGENT_DIR = previousRoot;
try {
await fs.rm(root, { recursive: true, force: true });
} catch {
// Windows may keep the directory busy briefly; the temp dir is disposable.
}
});
describe("before_agent_start prompt injection", () => {
it("contributes a named section through structured prompt options instead of forcing the prompt", async () => {
await registerWith();
const event = { systemPrompt: "base prompt", systemPromptOptions: structuredOptions() };
const result = await emit("before_agent_start", event);
assert.equal(result, undefined, "structured events must not force a full prompt replacement");
assert.match(event.systemPromptOptions.sections.memory_policy, /memory-policy/);
assert.equal(
event.systemPromptOptions.appendSystemPrompt,
"",
"the policy is a named section, not appended text",
);
});
it("keeps earlier sections while adding memory_policy", async () => {
await registerWith();
const event = {
systemPrompt: "base prompt",
systemPromptOptions: structuredOptions({
appendSystemPrompt: "earlier handler text",
sections: { other_section: "earlier content" },
}),
};
await emit("before_agent_start", event);
assert.equal(event.systemPromptOptions.sections.other_section, "earlier content");
assert.match(event.systemPromptOptions.sections.memory_policy, /memory-policy/);
assert.equal(
event.systemPromptOptions.appendSystemPrompt,
"earlier handler text",
);
});
it("reaches the 0.80.x session through the returned prompt, not the options object", async () => {
await registerWith();
const options = legacyOptions();
const event = { systemPrompt: "base prompt", systemPromptOptions: options };
const result = await emit("before_agent_start", event);
// 0.80.x-0.85.x apply `result.systemPrompt` or reset to the base prompt
// (agent-session.js); the options object is never rendered, and it is the one
// `ctx.getSystemPromptOptions()` hands to other extensions.
const applied = result?.systemPrompt ?? event.systemPrompt;
assert.match(applied, /memory-policy/, "the policy must reach a prompt the session applies");
assert.equal(options.appendSystemPrompt, "", "a legacy event must not accumulate into the exposed options");
});
it("keeps the replacement path when the event carries no options at all", async () => {
await registerWith();
const result = await emit("before_agent_start", { systemPrompt: "base prompt" });
assert.match(result.systemPrompt, /base prompt/);
assert.match(result.systemPrompt, /memory-policy/);
});
it("leaves the options untouched when there is no context to inject", async () => {
await registerWith({ memoryPolicyStyle: "none" });
const options = structuredOptions();
const snapshot = structuredClone(options);
const event = { systemPrompt: "base prompt", systemPromptOptions: options };
const result = await emit("before_agent_start", event);
assert.equal(result, undefined);
assert.deepEqual(event.systemPromptOptions, snapshot);
});
});