import { test, expect, describe } from "bun:test"; import { RUN_PROMPT_STAGES, isRunPromptStage, defaultRunTemplate, renderRunPrompt, DEFAULT_RUN_TEMPLATES, DEFAULT_BUG_RUN_TEMPLATE, } from "@bbbug/shared/run_prompt"; import { TRACK_EVAL_PROMPT_STAGES, isTrackEvalPromptStage, defaultTrackEvalTemplate, DEFAULT_TRACK_EVAL_TEMPLATES, } from "@bbbug/shared/eval_prompt"; /** * 执行期提示词模板的契约。 * * 这一层现在在关键路径上:控制面可以随任务下发一份不同的正文,卫星拿到什么就跑什么。 * 所以真正要钉死的不是「渲染得对不对」,而是**所有出错的路上都退回内置默认** —— * 库是空的、指针指向不存在的行、控制面根本没发这个字段,三种情况下行为都必须和改造 * 之前完全一样。生产上 agent_prompts 目前正是空的,这条性质就是这次改动能安全上线的 * 全部理由。 */ describe("run prompt · 回落契约", () => { test("每个 stage 都有内置默认,且非空", () => { for (const st of RUN_PROMPT_STAGES) { const t = defaultRunTemplate(st); expect(typeof t, st).toBe("string"); expect(t.length, st).toBeGreaterThan(200); } }); test("下发为空 / null / 空白时用内置默认", () => { // 卫星侧统一写成 `template?.trim() || DEFAULT_X`,这里钉的就是那三种输入。 for (const empty of [undefined, null, "", " ", "\n\t "]) { const chosen = (empty as string | null | undefined)?.trim() || DEFAULT_BUG_RUN_TEMPLATE; expect(chosen).toBe(DEFAULT_BUG_RUN_TEMPLATE); } }); test("stage 判别拒绝眼生的字符串", () => { expect(isRunPromptStage("bug.run")).toBe(true); expect(isRunPromptStage("eval")).toBe(false); expect(isRunPromptStage("bug.run.")).toBe(false); expect(isRunPromptStage(null)).toBe(false); expect(isRunPromptStage(123)).toBe(false); }); test("查找表覆盖且只覆盖已声明的 stage", () => { expect(Object.keys(DEFAULT_RUN_TEMPLATES).sort()).toEqual([...RUN_PROMPT_STAGES].sort()); }); }); describe("renderRunPrompt", () => { test("替换掉所有出现的占位符", () => { const out = renderRunPrompt("a{{x}}b{{x}}c{{y}}", { x: "1", y: "2" }); expect(out).toBe("a1b1c2"); }); test("数值会被字符串化", () => { expect(renderRunPrompt("{{n}}", { n: 5 })).toBe("5"); }); test("变量值里的 $& / $1 不被当成替换模式", () => { // 提示词正文里真的有 `$`(shell 片段、正则示例)。用字符串做 replaceAll 的 // 替换值时,`$&` 会展开成"被匹配的整段",于是产出里凭空多出一个 {{v}}。 expect(renderRunPrompt("[{{v}}]", { v: "$&" })).toBe("[$&]"); expect(renderRunPrompt("[{{v}}]", { v: "$1$'" })).toBe("[$1$']"); }); test("没给值的占位符原样留下,而不是变成空串", () => { // 留下一个显眼的 {{missing}} 会在第一次运行时被人看到;静默变成空串则会让 // 一整段指令凭空消失,而且没有任何痕迹。 expect(renderRunPrompt("a{{missing}}b", { other: "x" })).toBe("a{{missing}}b"); }); test("空模板与空变量表都不炸", () => { expect(renderRunPrompt("", {})).toBe(""); expect(renderRunPrompt("plain", {})).toBe("plain"); }); test("内置模板里没有遗留的 ${...} 插值残迹", () => { // 模板是从 TS 模板字符串里机械搬出来的;漏掉一个 ${} 会让它原样出现在发给模型的 // 提示词里,读起来像是提示词自己坏了。 for (const st of RUN_PROMPT_STAGES) { expect(defaultRunTemplate(st).includes("${"), st).toBe(false); } }); }); describe("run prompt · 变更方法覆盖", () => { const stages = ["bug.run", "feature.run.first", "feature.run.second"] as const; test("用 Frida 方法级 hook,多次执行取并集,禁止源码插桩", () => { const bullets = ["**禁止**", "**多次执行取并集**", "**三态,不要混**", "**Frida 同进程只能一次 attach**", "**平台**", "**落盘**", "**统计**"]; for (const st of stages) { const t = defaultRunTemplate(st); expect(t, st).toContain("Frida"); expect(t, st).toContain("并集"); expect(t, st).toContain("没记录到"); expect(t, st).not.toContain("最稳的做法是插一行"); expect(t, st).not.toContain("调试插桩"); expect(t, st).not.toContain("行覆盖插桩"); for (const b of bullets) expect(t, `${st} ${b}`).toContain(b); } }); test("同一进程的覆盖率 hook 必须和其他 Frida 脚本共用一次 session", () => { const t = defaultRunTemplate("bug.run"); expect(t).toContain("同一次 session"); expect(t).toContain("-l cov.js"); }); }); describe("security run · runner-owned status", () => { test("only security run stops asking the agent for a STATUS sentinel", () => { expect(defaultRunTemplate("security.run.first")).not.toContain("STATUS:"); expect(defaultRunTemplate("security.run.second")).not.toContain("STATUS:"); expect(defaultRunTemplate("bug.run")).toContain("STATUS:"); expect(defaultRunTemplate("feature.run.first")).toContain("STATUS:"); expect(defaultRunTemplate("feature.run.second")).toContain("STATUS:"); }); }); describe("feature / security 评估模板", () => { test("每个 stage 都有内置默认,且非空", () => { for (const st of TRACK_EVAL_PROMPT_STAGES) { expect(defaultTrackEvalTemplate(st).length, st).toBeGreaterThan(200); } }); test("查找表覆盖且只覆盖已声明的 stage", () => { expect(Object.keys(DEFAULT_TRACK_EVAL_TEMPLATES).sort()).toEqual([...TRACK_EVAL_PROMPT_STAGES].sort()); }); test("security 没有 regen —— 它的状态机里没有「按批注修订」这一步", () => { expect(TRACK_EVAL_PROMPT_STAGES).toContain("feature.regen"); expect(TRACK_EVAL_PROMPT_STAGES).not.toContain("security.regen"); expect(isTrackEvalPromptStage("security.regen")).toBe(false); }); test("stage 判别不与 bug 侧的 eval / regen 混淆", () => { // agent_prompts 用一个扁平的 stage 字符串做键;"eval" 与 "feature.eval" 混起来 // 就会把 Feature 的措辞发给 Bug 的评估器。 expect(isTrackEvalPromptStage("feature.eval")).toBe(true); expect(isTrackEvalPromptStage("eval")).toBe(false); expect(isTrackEvalPromptStage("regen")).toBe(false); }); test("模板里没有遗留的 ${...} 插值残迹", () => { for (const st of TRACK_EVAL_PROMPT_STAGES) { expect(defaultTrackEvalTemplate(st).includes("${"), st).toBe(false); } }); test("输出契约的哨兵还在(下游靠它们解析)", () => { for (const st of TRACK_EVAL_PROMPT_STAGES) { const t = defaultTrackEvalTemplate(st); expect(t, st).toContain("ASSESSMENT:"); // security.eval 不再用 sentinel —— evaluate agent 把 SecuritySpec // 直接写进 workspace 的 TestSpec.md,DB 里的 executionPlan 从文件读回来。 // 只有 feature 两个 stage 仍走 抠取。 if (st !== "security.eval") { expect(t, st).toContain(""); expect(t, st).toContain(""); } else { expect(t, st).toContain("TestSpec.md"); } } }); test("Security Evaluate 只描述方案,不再要求 agent 克隆或构建", () => { const prompt = defaultTrackEvalTemplate("security.eval"); expect(prompt).toContain("{{needsInputOpen}}"); expect(prompt).not.toContain("NEEDS_INPUT"); expect(prompt).not.toContain("git clone"); expect(prompt).not.toContain("waitBlock"); }); });