219 lines
11 KiB
TypeScript
219 lines
11 KiB
TypeScript
// MUST be first: sets DATABASE_URL before db_client is evaluated.
|
|
import "./setup_env";
|
|
|
|
import { test, expect, describe } from "bun:test";
|
|
import { readFileSync, existsSync } from "node:fs";
|
|
import path from "node:path";
|
|
import { PROMPT_STAGES, builtinTemplateFor } from "../src/agent_prompt_db";
|
|
import {
|
|
PROMPT_NODES, promptEdges, promptNode, dependentsOf, impactOf, dependenciesOf,
|
|
} from "@bbbug/shared/prompt_registry";
|
|
|
|
/**
|
|
* 提示词注册表的防漂移测试。
|
|
*
|
|
* 注册表存在的唯一理由是回答「改这块提示词会波及谁」。一张漏了边的图比没有图更
|
|
* 糟糕 —— 它会自信地给出错误答案,而且没人会怀疑它。所以这里把注册表钉死在真实
|
|
* 代码上:文件必须存在、符号必须能在文件里找到、而且**反向**扫描代码里所有提示词
|
|
* 构造函数,一个没登记就红。
|
|
*
|
|
* 加了 fragment 忘了登记 → CI 红,而不是几个月后有人照着一张骗人的图去改 prompt。
|
|
*/
|
|
const REPO = path.resolve(import.meta.dir, "../..");
|
|
const read = (rel: string) => readFileSync(path.join(REPO, rel), "utf8");
|
|
|
|
describe("提示词注册表 · 与代码的一致性", () => {
|
|
test("id 唯一", () => {
|
|
const ids = PROMPT_NODES.map((n) => n.id);
|
|
expect(new Set(ids).size).toBe(ids.length);
|
|
});
|
|
|
|
test("每个节点的 source 文件真实存在", () => {
|
|
for (const n of PROMPT_NODES) {
|
|
expect(existsSync(path.join(REPO, n.source)), `${n.id} → ${n.source}`).toBe(true);
|
|
}
|
|
});
|
|
|
|
test("每个节点的 symbol 能在它的 source 里找到", () => {
|
|
// 符号被重命名而注册表没跟上,是这张图最容易出现、也最难察觉的一种谎话。
|
|
for (const n of PROMPT_NODES) {
|
|
const src = read(n.source);
|
|
const declared =
|
|
src.includes(`const ${n.symbol}`) ||
|
|
src.includes(`function ${n.symbol}`) ||
|
|
src.includes(`${n.symbol}:`);
|
|
expect(declared, `${n.id}: 在 ${n.source} 里找不到 ${n.symbol}`).toBe(true);
|
|
}
|
|
});
|
|
|
|
test("includes / derivesFrom 指向真实存在的节点,且类型正确", () => {
|
|
for (const n of PROMPT_NODES) {
|
|
for (const dep of n.includes ?? []) {
|
|
const target = promptNode(dep);
|
|
expect(target, `${n.id} includes 了不存在的 ${dep}`).not.toBeNull();
|
|
// 模板可以组合片段;组合另一个整份模板说明建模错了。
|
|
expect(target!.kind, `${n.id} includes 的 ${dep} 不是 fragment`).toBe("fragment");
|
|
}
|
|
if (n.derivesFrom) {
|
|
const base = promptNode(n.derivesFrom);
|
|
expect(base, `${n.id} 派生自不存在的 ${n.derivesFrom}`).not.toBeNull();
|
|
expect(base!.kind).toBe("template");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("图里没有环", () => {
|
|
// impactOf / dependenciesOf 都是 BFS,有环会让 UI 的影响面高亮死循环或算错。
|
|
for (const n of PROMPT_NODES) {
|
|
expect(dependenciesOf(n.id), `${n.id} 出现在自己的依赖闭包里`).not.toContain(n.id);
|
|
expect(impactOf(n.id), `${n.id} 出现在自己的影响闭包里`).not.toContain(n.id);
|
|
}
|
|
});
|
|
|
|
test("每个 fragment 至少被一个节点引用", () => {
|
|
// 无人引用的 fragment 要么是死代码,要么是有人删了引用却没删登记 —— 两种都该查。
|
|
for (const n of PROMPT_NODES.filter((x) => x.kind === "fragment")) {
|
|
expect(dependentsOf(n.id).length, `${n.id} 没有任何引用者`).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
|
|
test("边由节点派生,不会与节点脱节", () => {
|
|
const ids = new Set(PROMPT_NODES.map((n) => n.id));
|
|
for (const e of promptEdges()) {
|
|
expect(ids.has(e.from)).toBe(true);
|
|
expect(ids.has(e.to)).toBe(true);
|
|
}
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 反向扫描:代码里每一个提示词构造函数都必须在注册表里。
|
|
*
|
|
* 下面这几个是**转发器**而不是提示词本体 —— 正文在别的地方(已登记),它们只负责
|
|
* 取模板并填充变量。每一条都写清理由;想加新豁免就得先说服自己写得出理由。
|
|
*/
|
|
const WRAPPERS: Record<string, string> = {
|
|
"satellite/src/evaluator.ts:buildEvalPrompt":
|
|
"转发器:正文是 shared/src/eval_prompt.ts 的 DEFAULT_EVAL_PROMPT_TEMPLATE(已登记为 bug.eval),这里只做 renderEvalPrompt 变量填充",
|
|
"satellite/src/evaluator.ts:buildRegenPrompt":
|
|
"转发器:正文是 DEFAULT_REGEN_PROMPT_TEMPLATE(已登记为 bug.regen)",
|
|
"satellite/src/readable_report.ts:buildPrompt":
|
|
"转发器:正文是 shared/src/readable_prompt.ts 的模板(已登记为 report.readable),这里只做 renderReadablePrompt 变量填充",
|
|
// P3 之后,执行期这几份的正文也搬到了 shared/src/run_prompt.ts,卫星侧只剩「把分支
|
|
// 算成文本再填进模板」。它们各自的模板常量已按 bug.run / feature.run.first / … 登记。
|
|
"satellite/src/runner.ts:buildSinglePrompt":
|
|
"转发器:正文是 DEFAULT_BUG_RUN_TEMPLATE(已登记为 bug.run)",
|
|
"satellite/src/runner.ts:buildCriticPrompt":
|
|
"转发器:正文是 DEFAULT_BUG_CRITIC_TEMPLATE(已登记为 bug.critic)",
|
|
"satellite/src/feature_runner.ts:buildFeatureFirstPrompt":
|
|
"转发器:正文是 DEFAULT_FEATURE_RUN_FIRST_TEMPLATE(已登记为 feature.run.first)",
|
|
"satellite/src/feature_runner.ts:buildFeatureSecondPrompt":
|
|
"转发器:正文是 DEFAULT_FEATURE_RUN_SECOND_TEMPLATE(已登记为 feature.run.second)",
|
|
"satellite/src/security_runner.ts:buildSecurityFirstPrompt":
|
|
"转发器:正文是 DEFAULT_SECURITY_RUN_FIRST_TEMPLATE(已登记为 security.run.first)",
|
|
"satellite/src/security_runner.ts:buildSecuritySecondPrompt":
|
|
"转发器:正文是 DEFAULT_SECURITY_RUN_SECOND_TEMPLATE(已登记为 security.run.second)",
|
|
"satellite/src/prototype_runner.ts:buildPrototypeFirstPrompt":
|
|
"转发器:正文是 DEFAULT_PROTOTYPE_RUN_FIRST_TEMPLATE(已登记为 prototype.run.first)",
|
|
"satellite/src/prototype_runner.ts:buildPrototypeSecondPrompt":
|
|
"转发器:正文是 DEFAULT_PROTOTYPE_RUN_SECOND_TEMPLATE(已登记为 prototype.run.second)",
|
|
"satellite/src/verify_runner.ts:buildVerificationFirstPrompt":
|
|
"转发器:按规约格式取 DEFAULT_VERIFY_RUN_FIRST_TEMPLATE / DEFAULT_VERIFY_CASE_FIRST_TEMPLATE(已登记为 verify.run.first / verify.case.first)",
|
|
"satellite/src/verify_runner.ts:buildVerificationSecondPrompt":
|
|
"转发器:按规约格式取 DEFAULT_VERIFY_RUN_SECOND_TEMPLATE / DEFAULT_VERIFY_CASE_SECOND_TEMPLATE(已登记为 verify.run.second / verify.case.second)",
|
|
"satellite/src/verify_runner.ts:buildVerificationPrompt":
|
|
"转发器:上面两个的共同实现,按 verificationPromptStage 挑四份已登记模板之一",
|
|
"satellite/src/asking.ts:buildPreamble":
|
|
"转发器:正文是 DEFAULT_ASKING_PREAMBLE_TEMPLATE(已登记为 review.asking)",
|
|
"satellite/src/feature_evaluator.ts:buildFeatureEvalPrompt":
|
|
"转发器:正文是 DEFAULT_FEATURE_EVAL_TEMPLATE(已登记为 feature.eval)",
|
|
"satellite/src/feature_evaluator.ts:buildFeatureRegenPrompt":
|
|
"转发器:正文是 DEFAULT_FEATURE_REGEN_TEMPLATE(已登记为 feature.regen)",
|
|
"satellite/src/security_evaluator.ts:buildSecurityEvalPrompt":
|
|
"转发器:正文是 DEFAULT_SECURITY_EVAL_TEMPLATE(已登记为 security.eval)",
|
|
};
|
|
|
|
describe("提示词注册表 · 反向覆盖", () => {
|
|
const SCAN_DIRS = ["satellite/src", "shared/src"];
|
|
|
|
/** 代码里所有形如 `function buildXxxPrompt` / `buildXxxPreamble` 的定义。 */
|
|
function scanBuilders(): { file: string; symbol: string }[] {
|
|
const out: { file: string; symbol: string }[] = [];
|
|
for (const dir of SCAN_DIRS) {
|
|
const glob = new Bun.Glob("*.ts");
|
|
for (const rel of glob.scanSync({ cwd: path.join(REPO, dir) })) {
|
|
const file = `${dir}/${rel}`;
|
|
for (const m of read(file).matchAll(/function (build[A-Za-z]*(?:Prompt|Preamble))\b/g)) {
|
|
out.push({ file, symbol: m[1] });
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
test("每个提示词构造函数要么已登记,要么是写明理由的转发器", () => {
|
|
const registered = new Set(PROMPT_NODES.map((n) => `${n.source}:${n.symbol}`));
|
|
const unregistered = scanBuilders()
|
|
.map((b) => `${b.file}:${b.symbol}`)
|
|
.filter((key) => !registered.has(key) && !(key in WRAPPERS));
|
|
expect(
|
|
unregistered,
|
|
`以下提示词构造函数没有登记进 shared/src/prompt_registry.ts —— ` +
|
|
`要么加进 PROMPT_NODES,要么(若它只是转发器)加进本测试的 WRAPPERS 并写明理由`,
|
|
).toEqual([]);
|
|
});
|
|
|
|
test("WRAPPERS 里的豁免项都还真实存在", () => {
|
|
// 函数被删/改名后豁免却留着,下次真有人加同名函数就会被静默放行。
|
|
for (const key of Object.keys(WRAPPERS)) {
|
|
const [file, symbol] = key.split(":");
|
|
expect(existsSync(path.join(REPO, file)), `豁免项指向不存在的文件 ${file}`).toBe(true);
|
|
expect(read(file).includes(`function ${symbol}`), `豁免项 ${key} 已不存在,请删掉`).toBe(true);
|
|
}
|
|
});
|
|
|
|
test("shared 里的提示词模板常量都已登记", () => {
|
|
const registered = new Set(PROMPT_NODES.map((n) => n.symbol));
|
|
const glob = new Bun.Glob("*.ts");
|
|
const missing: string[] = [];
|
|
for (const rel of glob.scanSync({ cwd: path.join(REPO, "shared/src") })) {
|
|
// `: Record<…>` 声明的是**索引**(stage → 正文的查找表),不是正文本身;
|
|
// 把它当成一份没登记的提示词,只会逼着大家往注册表里塞一个假节点。
|
|
for (const m of read(`shared/src/${rel}`).matchAll(/export const ([A-Z_]*TEMPLATE[A-Z_]*)\b(\s*:\s*Record<)?/g)) {
|
|
if (m[2]) continue;
|
|
if (!registered.has(m[1])) missing.push(`shared/src/${rel}:${m[1]}`);
|
|
}
|
|
}
|
|
expect(missing, "以下模板常量没有登记进注册表").toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("提示词注册表 · 影响面查询", () => {
|
|
test("执行条件记录块的影响面覆盖三条流水线的执行提示词", () => {
|
|
// 这是整张图存在的理由:改这一块,到底波及谁。
|
|
const hit = impactOf("frag.conditions");
|
|
expect(hit).toContain("bug.run");
|
|
expect(hit).toContain("feature.run.first");
|
|
expect(hit).toContain("feature.run.second");
|
|
expect(hit).toContain("security.run.first");
|
|
expect(hit).toContain("security.run.second");
|
|
});
|
|
|
|
test("派发 stage 和控制面能下发的 stage 一一对应", () => {
|
|
// 注册表里每个带 stage 的节点,控制面都得有那份代码正文可发;反过来,控制面会发的
|
|
// 每个 stage 都该在图谱上有个节点 —— 否则「改这份会波及谁」对它就答不上来。
|
|
const staged = PROMPT_NODES.filter((n) => n.stage).map((n) => n.stage!);
|
|
expect(new Set(staged).size).toBe(staged.length);
|
|
expect([...staged].sort()).toEqual([...PROMPT_STAGES].sort());
|
|
for (const st of PROMPT_STAGES) expect(builtinTemplateFor(st).length).toBeGreaterThan(200);
|
|
});
|
|
|
|
test("外链阅读块是引用面最广的片段", () => {
|
|
const byUsers = PROMPT_NODES
|
|
.filter((n) => n.kind === "fragment")
|
|
.map((n) => ({ id: n.id, n: dependentsOf(n.id).length }))
|
|
.sort((a, b) => b.n - a.n);
|
|
expect(byUsers[0].id).toBe("frag.external_links");
|
|
});
|
|
});
|