import { test, expect, describe, beforeEach, afterEach } from "bun:test"; import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { roundLogName, parseRoundFromLogName, latestRoundFile, nextRoundIndex, isServableLogName, roundOfLogFile, logFileKind, runnerLogNameFor, SESSION_LOG_PREFIX, EVAL_LOG_PREFIX, LEGACY_SESSION_LOG, LEGACY_EVAL_LOG, } from "@bbbug/shared/session_log"; import { digestSessionLog, renderPastRoundsBrief } from "@bbbug/shared/session_digest"; /** * 这一组守的是「多轮执行留得下痕迹」这件事本身。 * * 轮次化之前,一个任务的 N 批执行共用一个 `session.log`,每批开跑时 openSync("w") * 截断重写 —— 生产上 122/358 个 bug 任务跑过 ≥2 批,它们的早期批次全部不可考。 * 所以这里要钉死的是:写入按批分文件、读取仍然认得「最新一批」、老任务盘上的 * legacy 文件不会因为改名规则而读不到。 */ describe("会话日志的轮次命名", () => { test("写入按批分文件,读取解析出最新一批", () => { expect(roundLogName(SESSION_LOG_PREFIX, 3)).toBe("session-r3.log"); const names = ["session-r1.log", "session-r2.log", "session-r10.log", "FixReport.md"]; // 字典序会把 r10 排在 r2 前面 —— 必须按数值取最大,否则「最新一批」会取错。 expect(latestRoundFile(names, SESSION_LOG_PREFIX, LEGACY_SESSION_LOG)).toBe("session-r10.log"); }); test("没有轮次文件时回退到 legacy 名(老任务照样读得到)", () => { expect(latestRoundFile(["session.log"], SESSION_LOG_PREFIX, LEGACY_SESSION_LOG)).toBe("session.log"); expect(latestRoundFile([], SESSION_LOG_PREFIX, LEGACY_SESSION_LOG)).toBeNull(); }); test("legacy 文件的轮次是「未知」,不是第 1 批", () => { // 它极可能是某个多轮任务被覆盖后剩下的最后一批,当成第 1 批会把历史讲反。 expect(parseRoundFromLogName(SESSION_LOG_PREFIX, LEGACY_SESSION_LOG)).toBeNull(); expect(parseRoundFromLogName(SESSION_LOG_PREFIX, "session-r2.log")).toBe(2); expect(parseRoundFromLogName(SESSION_LOG_PREFIX, "eval-r2.log")).toBeNull(); }); test("评估日志的下一轮序号:legacy 占掉第 1 批的位置", () => { expect(nextRoundIndex([], EVAL_LOG_PREFIX)).toBe(1); expect(nextRoundIndex([LEGACY_EVAL_LOG], EVAL_LOG_PREFIX)).toBe(2); expect(nextRoundIndex(["eval-r1.log", "eval-r2.log"], EVAL_LOG_PREFIX)).toBe(3); }); }); /** * 运行事件日志(卫星自己那半边:派发、看门狗、进程收场)与会话日志成对存在。 * 名字由会话日志名推导,这样两个文件的轮次不可能对不上 —— 派发侧只记一次路径。 */ describe("运行事件日志的配对命名", () => { test("执行侧 / 评估侧各自成对,legacy 名也认", () => { expect(runnerLogNameFor("session-r3.log")).toBe("runner-r3.log"); expect(runnerLogNameFor("eval-r3.log")).toBe("runner-eval-r3.log"); expect(runnerLogNameFor("session.log")).toBe("runner.log"); expect(runnerLogNameFor("eval.log")).toBe("runner-eval.log"); }); test("推导出来的名字一定在白名单里,轮次也解得出来", () => { // 否则文件写在盘上却取不回来 —— 那等于没写。 for (const session of ["session-r3.log", "eval-r3.log", "session.log", "eval.log"]) { const name = runnerLogNameFor(session); expect(isServableLogName(name)).toBe(true); expect(logFileKind(name)).toBe("runner"); expect(roundOfLogFile(name)).toBe(roundOfLogFile(session)); } }); }); describe("logs/ 目录的对外白名单", () => { test("放行本来就带轮次、从不被覆盖的那几类", () => { for (const n of ["session-r2.log", "eval.log", "critic-3.log", "task-context-1.json"]) { expect(isServableLogName(n)).toBe(true); } expect(roundOfLogFile("critic-3.log")).toBe(3); expect(roundOfLogFile("task-context-1.json")).toBe(1); expect(logFileKind("session-r2.log")).toBe("session"); expect(logFileKind("task-context-1.json")).toBe("context"); }); test("挡住路径穿越和工作区里的其他文件", () => { for (const n of ["../../.env", "..%2Fsecret", "FixReport.md", ".hidden", "session-r2.log.bak", ""]) { expect(isServableLogName(n)).toBe(false); } }); }); /** * 摘要是唯一跨得过节点边界的那层事实(原始日志几 MB,只留在执行机上)。它必须只 * 承载执行留下的痕迹 —— 失败的命令、报错、动过的文件、有没有正常收尾 —— 而不是 * 模型自述的结论。 */ describe("会话摘要", () => { const log = [ JSON.stringify({ type: "system", subtype: "init", session_id: "s-1", model: "" }), JSON.stringify({ type: "assistant", message: { model: "claude-opus-5", content: [{ type: "tool_use", id: "t1", name: "Bash", input: { command: "./gradlew assembleDebug" } }], }, }), JSON.stringify({ type: "user", message: { content: [{ type: "tool_result", tool_use_id: "t1", is_error: true, content: "error: cannot find symbol" }] }, }), JSON.stringify({ type: "assistant", message: { content: [{ type: "tool_use", id: "t2", name: "Edit", input: { file_path: "/repo/Foo.kt" } }] }, }), "这一行不是 JSON,解析器应当跳过而不是炸掉", JSON.stringify({ type: "result", subtype: "success", num_turns: 42, duration_ms: 600_000, result: "…\nSTATUS: FIXED" }), ].join("\n"); test("压出失败命令、动过的文件和终止哨兵", () => { const d = digestSessionLog(log, 2); expect(d.round).toBe(2); expect(d.model).toBe("claude-opus-5"); // init 的 model 是空的,取第一条 assistant expect(d.status).toBe("STATUS: FIXED"); expect(d.completed).toBe(true); expect(d.turns).toBe(42); expect(d.failedCommands).toHaveLength(1); expect(d.failedCommands[0].command).toContain("gradlew"); expect(d.failedCommands[0].error).toContain("cannot find symbol"); expect(d.tools.find((t) => t.name === "Bash")).toMatchObject({ calls: 1, failed: 1 }); expect(d.touchedFiles).toEqual([{ path: "/repo/Foo.kt", edits: 1 }]); }); test("长命令留头也留尾 —— 真正失败的那一步通常在末尾", () => { const long = `cd ${"/Users/fei/bug_workspace/feature-e272c3ab-cc7c-4ed2-86ac-f13266f63371".repeat(4)} && xcodebuild -scheme Aweme test`; const one = JSON.stringify({ type: "assistant", message: { content: [{ type: "tool_use", id: "x", name: "Bash", input: { command: long } }] }, }); const two = JSON.stringify({ type: "user", message: { content: [{ type: "tool_result", tool_use_id: "x", is_error: true, content: "boom" }] }, }); const cmd = digestSessionLog([one, two].join("\n"), 1).failedCommands[0].command; expect(cmd).toStartWith("cd /Users/fei"); expect(cmd).toEndWith("xcodebuild -scheme Aweme test"); // 头部被 cd 占满时,尾部才是信息 }); test("task-context-N.json 必须绕开 stream-json 降噪(整份都不是逐行 JSON)", () => { // 降噪规则是「不以 { 开头的行一律丢掉」,套在缩进过的 JSON 上会过滤成空文件。 expect(logFileKind("task-context-1.json")).toBe("context"); expect(logFileKind("session-r1.log")).toBe("session"); }); test("没有 result 事件 = 那批没走到终点,这本身是要留下的信号", () => { const partial = log.split("\n").slice(0, 3).join("\n"); const d = digestSessionLog(partial, 1); expect(d.completed).toBe(false); expect(d.status).toBeNull(); expect(renderPastRoundsBrief([d])).toContain("没有打出终止哨兵"); }); test("历轮简报讲事实、不讲结论", () => { const brief = renderPastRoundsBrief([digestSessionLog(log, 1)]); expect(brief).toContain("第 1 批"); expect(brief).toContain("gradlew"); // 「做成了什么」不由这里下结论 —— 简报自己就得把这句话讲清楚。 expect(brief).toContain("以报告 / 已确认条件 / 复核意见为准"); }); test("空输入不产出简报(首批执行的提示词逐字节不变)", () => { expect(renderPastRoundsBrief([])).toBe(""); }); }); /** * workspace 层:真的按批写文件、真的能列出来、真的挑得出最新一批。 * 用临时目录跑,不碰任何真实工作区。 */ describe("工作区里的日志文件", () => { let dir = ""; beforeEach(() => { dir = mkdtempSync(path.join(tmpdir(), "streamline-logs-")); }); afterEach(() => { try { rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ } }); async function ws() { return await import(`../src/workspace.ts?logs-${Math.random()}`); } test("按批取路径、缺省取最新一批、列表带轮次", async () => { const { workspaceLogPath, listWorkspaceLogs, sessionLogRounds, nextEvalLogPath } = await ws(); mkdirSync(path.join(dir, "logs"), { recursive: true }); writeFileSync(path.join(dir, "logs", "session-r1.log"), "{}\n"); writeFileSync(path.join(dir, "logs", "session-r2.log"), "{}\n"); writeFileSync(path.join(dir, "logs", "critic-2.log"), "{}\n"); writeFileSync(path.join(dir, "logs", "FixReport.md"), "不该出现在清单里"); expect(workspaceLogPath(dir, 1)).toEndWith("session-r1.log"); expect(workspaceLogPath(dir)).toEndWith("session-r2.log"); expect(sessionLogRounds(dir)).toEqual([1, 2]); expect(nextEvalLogPath(dir)).toEndWith("eval-r1.log"); const names = listWorkspaceLogs(dir).map((f: { name: string }) => f.name); expect(names).toContain("session-r2.log"); expect(names).toContain("critic-2.log"); expect(names).not.toContain("FixReport.md"); }); test("中断任务后 logs/ 保留 —— 被中断的那批恰恰最需要留证", async () => { const { clearRuntimeArtifacts } = await ws(); mkdirSync(path.join(dir, "logs"), { recursive: true }); mkdirSync(path.join(dir, "evidence"), { recursive: true }); writeFileSync(path.join(dir, "logs", "session-r1.log"), "{}\n"); writeFileSync(path.join(dir, "evidence", "before-1-x.png"), "x"); writeFileSync(path.join(dir, "FixReport.md"), "stale"); clearRuntimeArtifacts(dir); expect(await Bun.file(path.join(dir, "logs", "session-r1.log")).exists()).toBe(true); // 证据和报告仍然要清:它们会被下一批当作输入读回去。 expect(await Bun.file(path.join(dir, "evidence", "before-1-x.png")).exists()).toBe(false); expect(await Bun.file(path.join(dir, "FixReport.md")).exists()).toBe(false); }); test("历轮简报只覆盖比当前批次早的批次", async () => { const { pastRoundsBrief } = await ws(); mkdirSync(path.join(dir, "logs"), { recursive: true }); const line = JSON.stringify({ type: "result", subtype: "success", result: "STATUS: FIX_FAILED" }); writeFileSync(path.join(dir, "logs", "session-r1.log"), line + "\n"); writeFileSync(path.join(dir, "logs", "session-r2.log"), line + "\n"); expect(pastRoundsBrief(dir, 1)).toBe(""); // 首批:没有历轮 expect(pastRoundsBrief(dir, 2)).toContain("第 1 批"); expect(pastRoundsBrief(dir, 3)).toContain("第 2 批"); expect(pastRoundsBrief(dir, 2)).not.toContain("第 2 批"); }); });