示例工程 ch08-system-prompt-context
ch08-system-prompt-context/02-context-files/index.ts
// 02-context-files —— 上下文文件(AGENTS.md)的发现顺序、覆盖规则与关闭方式。
//
// 运行:node --experimental-strip-types index.ts
//
// 目录结构(全部在系统临时目录里创建):
// <agentDir>/AGENTS.md 全局上下文
// <root>/AGENTS.md 祖先上下文
// <root>/child/AGENTS.md 离 cwd 最近的一层
// <root>/child/AGENTS.override.md 同目录下覆盖 AGENTS.md
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { DefaultResourceLoader, createAgentSession, SessionManager } from "@earendil-works/pi-coding-agent";
import { createStubRuntime } from "./stub-provider.ts";
const root = mkdtempSync(join(tmpdir(), "pi-ch08-root-"));
const child = join(root, "child");
mkdirSync(child, { recursive: true });
const agentDir = mkdtempSync(join(tmpdir(), "pi-ch08-agent-"));
writeFileSync(join(agentDir, "AGENTS.md"), "# 全局\n\nGLOBAL-MARKER\n");
writeFileSync(join(root, "AGENTS.md"), "# 仓库\n\nROOT-MARKER\n");
writeFileSync(join(child, "AGENTS.md"), "# 子目录\n\nCHILD-OLD-MARKER\n");
writeFileSync(join(child, "AGENTS.override.md"), "# 子目录覆盖\n\nCHILD-OVERRIDE-MARKER\n");
const stub = await createStubRuntime({ steps: [{ text: "x" }], agentDir });
async function makeSession(noContextFiles: boolean) {
const loader = new DefaultResourceLoader({
cwd: child,
agentDir,
noSkills: true,
noContextFiles,
});
await loader.reload();
const discovered = loader.getAgentsFiles().agentsFiles.map((file) => file.path.replace(root, "<root>").replace(agentDir, "<agentDir>"));
const { session } = await createAgentSession({
model: stub.model,
modelRuntime: stub.modelRuntime,
agentDir,
sessionManager: SessionManager.inMemory(child),
resourceLoader: loader,
cwd: child,
tools: [],
});
return { session, discovered };
}
const withFiles = await makeSession(false);
const prompt = withFiles.session.systemPrompt;
const without = await makeSession(true);
const promptWithout = without.session.systemPrompt;
const indexOf = (marker: string) => prompt.indexOf(marker);
const order = ["GLOBAL-MARKER", "ROOT-MARKER", "CHILD-OVERRIDE-MARKER"].map(indexOf);
console.log("发现的上下文文件:", withFiles.discovered.join(" , "));
console.log("三个标记在提示中的位置:", order.join(" < "));
console.log("同目录 AGENTS.md 是否被 override 顶掉:", !prompt.includes("CHILD-OLD-MARKER"));
console.log("project_context 段是否存在:", prompt.includes("<project_context>"), "/ 关闭后:", promptWithout.includes("<project_context>"));
const ok = (label: string, pass: boolean): void => {
console.log(`${pass ? "✓" : "✗"} ${label}`);
if (!pass) process.exitCode = 1;
};
ok("发现了 3 个上下文文件(全局 + 祖先 + cwd)", withFiles.discovered.length === 3);
ok("全局文件排在祖先之前,祖先排在 cwd 之前", order.every((value, i) => i === 0 || (value > (order[i - 1] ?? -1))));
ok("同目录的 AGENTS.override.md 覆盖 AGENTS.md", prompt.includes("CHILD-OVERRIDE-MARKER") && !prompt.includes("CHILD-OLD-MARKER"));
ok("三个标记都进了 project_context", prompt.includes("<project_context>") && order.every((value) => value > 0));
ok("project_context 只有一个(没有被重复注入)", prompt.split("<project_context>").length === 2);
const markers = ["GLOBAL-MARKER", "ROOT-MARKER", "CHILD-OVERRIDE-MARKER"];
ok("noContextFiles 后没有任何标记", markers.every((marker) => promptWithout.includes(marker) === false));
ok("noContextFiles 后没有 project_context 段", !promptWithout.includes("<project_context>"));
ok("关闭上下文文件不影响 cwd 段", promptWithout.includes("<cwd>"));
ok("关闭后提示更短", promptWithout.length < prompt.length);
withFiles.session.dispose();
without.session.dispose();
console.log("✓ OK");