Initial import of streamline

This commit is contained in:
2026-11-16 17:26:44 -08:00
commit 8e4aec1855
1221 changed files with 274256 additions and 0 deletions
+112
View File
@@ -0,0 +1,112 @@
/**
* 历轮简报的行为测试 —— 「一轮跑崩了,下一轮别从零开始」这条改动的落点。
*
* 盯三件事:
* 1. 「跑完了但没打出哨兵」必须被报出来。这是最常见的收场(抽样里 140 轮中 52 轮),
* 而原来的判断只看 `completed`(抽样里只有 1 轮为 false),等于几乎从不触发。
* 2. 最近一批没走到结论时,必须明说「给它的复核意见极可能一条都没落地」。
* 任务 ffff3280 跑了 19 批,同一条反馈被人追问四次,丢的不是反馈本身,
* 是「上一批其实没执行」这个事实。
* 3. 简报必须进得了提示词 —— 哪怕生产上跑的模板版本里没有 {{pastRounds}} 占位符。
*/
import { describe, expect, test } from "bun:test";
import { renderPastRoundsBrief, ensurePastRoundsInPrompt, digestSessionLog, stripControlChars, type SessionDigest } from "@bbbug/shared/session_digest";
const digest = (over: Partial<SessionDigest> = {}): SessionDigest => ({
round: 1, sessionId: "s", model: "m", events: 100, turns: 1, durationMs: 600_000,
status: "STATUS: FIXED", resultTail: null, completed: true,
tools: [{ name: "Bash", calls: 40, failed: 2 }], failedCommands: [], errors: [], touchedFiles: [],
...over,
});
describe("renderPastRoundsBrief", () => {
test("跑完了但没有终止哨兵 → 必须报出来", () => {
const out = renderPastRoundsBrief([digest({ completed: true, status: null })]);
expect(out).toContain("没有打出终止哨兵");
expect(out).toContain("按「没做完」对待");
});
test("正常收尾的批次不报警告", () => {
const out = renderPastRoundsBrief([digest({ status: "STATUS: FIXED" })]);
expect(out).not.toContain("没有打出终止哨兵");
expect(out).not.toContain("极可能一条都没有落地");
});
test("最近一批没走到结论 → 明说复核意见很可能没落地", () => {
const out = renderPastRoundsBrief([
digest({ round: 1, status: "STATUS: IMPLEMENTED" }),
digest({ round: 2, status: null }),
]);
expect(out).toContain("极可能一条都没有落地");
expect(out).toContain("逐条重新确认");
});
test("最近一批正常收尾 → 不加那句(否则每轮都喊狼来了)", () => {
const out = renderPastRoundsBrief([
digest({ round: 1, status: null }),
digest({ round: 2, status: "STATUS: IMPLEMENTED" }),
]);
expect(out).toContain("没有打出终止哨兵"); // 第 1 批仍然要报
expect(out).not.toContain("极可能一条都没有落地"); // 但不加总结那句
});
test("英文任务走英文文案", () => {
const out = renderPastRoundsBrief([digest({ status: null })], { lang: "en" });
expect(out).toContain("did not reach a conclusion");
expect(out).toContain("has most likely NOT been acted on");
});
test("失败过的命令原样带给下一批 —— 别在同一个坑里再摔一次", () => {
const out = renderPastRoundsBrief([digest({
failedCommands: [{ command: "airb cmd 'jojo build --bad-flag'", error: "unknown flag" }],
})]);
expect(out).toContain("jojo build --bad-flag");
expect(out).toContain("unknown flag");
});
});
describe("ensurePastRoundsInPrompt", () => {
const BRIEF = "## 此前各批执行的过程痕迹(只是事实,不是结论)\n### 第 1 批 · 无哨兵";
test("模板没有占位符 → 补到末尾", () => {
const out = ensurePastRoundsInPrompt("## 步骤 3 — 复现\n正文\n", BRIEF);
expect(out).toContain("## 此前各批执行的过程痕迹");
});
test("模板已渲染出这一段 → 不重复插", () => {
const already = `## 步骤 3\n正文\n\n${BRIEF}\n`;
expect(ensurePastRoundsInPrompt(already, BRIEF)).toBe(already);
});
test("首批执行(简报为空)→ 一个字都不加", () => {
const t = "## 步骤 3 — 复现\n";
expect(ensurePastRoundsInPrompt(t, "")).toBe(t);
});
});
describe("控制字符", () => {
// 任务 ae2a1a3a:失败命令的输出里夹着 16 个 NUL,下一批提示词因此被 spawn 拒绝,空转 16 批。
const NUL = "\u0000".repeat(16);
const sessionWithNul = [
JSON.stringify({ type: "assistant", message: { content: [{ type: "tool_use", id: "t1", name: "Bash", input: { command: `cat /tmp/frida.log${NUL}` } }] } }),
JSON.stringify({ type: "user", message: { content: [{ type: "tool_result", tool_use_id: "t1", is_error: true, content: `Failed to attach ${NUL}line 44: Terminated` }] } }),
JSON.stringify({ type: "result", num_turns: 3, result: `done${NUL} STATUS: IMPLEMENTED` }),
].join("\n");
test("摘要里不留 NUL", () => {
const d = digestSessionLog(sessionWithNul, 14);
expect(JSON.stringify(d)).not.toContain("\\u0000");
expect(d.failedCommands[0].error).toContain("Failed to attach");
expect(d.failedCommands[0].error).toContain("line 44: Terminated");
});
test("DB 里已存的旧摘要带着 NUL,渲染出的简报也不能有", () => {
const out = renderPastRoundsBrief([digest({ round: 14, failedCommands: [{ command: `cat x${NUL}`, error: `boom${NUL}` }] })]);
expect(out).not.toContain("\u0000");
expect(out).toContain("boom");
});
test("换行和制表符照常保留", () => {
expect(stripControlChars("a\tb\nc\u0000d\u001be")).toBe("a\tb\ncde");
});
});