import { test, expect, describe } from "bun:test"; import path from "node:path"; /** * 「拉起 agent 的那条命令,到底指向谁」。 * * 这三个 wrapper 里只有 claude-pty 是每次执行都要跑的引擎,而它曾经是从**节点自己的 * git 检出**里读的 —— 热部署只换 satellite/dist/satellite.js,永远碰不到它。于是一个 * wrapper 修复可以被合并、被部署、被脚本报告成「已推送」,然后在任何节点上都没运行, * 而面板上每个节点都显示最新(节点只上报 dist/VERSION)。折进 bundle 之后,引擎和拉起 * 它的代码在同一个产物里,这类漂移不再可能。 * * 这条测试钉的就是那个分叉:`SATELLITE_ENTRY_PATH` 有值(= 以 bundle 身份运行)时, * 命令必须是「重新进入这个产物 + 子命令」;没有值(源码跑)时仍指向 bin/ 下的脚本。 * 两种形态都得测,因为回归的方式恰恰是「其中一种悄悄退回旧路径」。 * * 用子进程而不是直接 import:claude_launch 的这些常量是**导入时**按环境变量算死的, * 同一个进程里改 env 再 import 第二次拿到的是模块缓存。 */ const MOD = path.resolve(import.meta.dir, "../src/claude_launch.ts"); function launchWith(env: Record): string[] { const code = `const m = await import(${JSON.stringify(MOD)}); console.log(JSON.stringify({ launch: m.CLAUDE_LAUNCH, evidence: m.RECORD_EVIDENCE_CMD }));`; const r = Bun.spawnSync({ cmd: [process.execPath, "-e", code], env: { ...process.env, STREAMLINE_AGENT_MODE: "pty", ...env }, stdout: "pipe", stderr: "pipe", }); const out = r.stdout.toString().trim(); if (!out) throw new Error(`no output: ${r.stderr.toString()}`); return JSON.parse(out.split("\n").pop()!).launch; } function readOnlyWith(env: Record): { cmd: string[]; model: string } { const code = `const m = await import(${JSON.stringify(MOD)}); console.log(JSON.stringify(m.readOnlyAgentCommand("atomize me")));`; const r = Bun.spawnSync({ cmd: [process.execPath, "-e", code], env: { ...process.env, ...env }, stdout: "pipe", stderr: "pipe", }); const out = r.stdout.toString().trim(); if (!out) throw new Error(`no output: ${r.stderr.toString()}`); return JSON.parse(out.split("\n").pop()!); } function modeWith(env: Record): { mode: string; cursorBin?: string; codexBin?: string } { const code = `const m = await import(${JSON.stringify(MOD)}); const out={mode:m.AGENT_MODE,cursorBin:process.env.CURSOR_BIN}; if(m.AGENT_MODE==="codex") out.codexBin=m.CODEX_BIN; console.log(JSON.stringify(out));`; const r = Bun.spawnSync({ cmd: [process.execPath, "-e", code], env: { ...process.env, ...env }, stdout: "pipe", stderr: "pipe", }); const out = r.stdout.toString().trim(); if (!out) throw new Error(`no output: ${r.stderr.toString()}`); return JSON.parse(out.split("\n").pop()!); } const BUNDLE = "/opt/streamline/satellite.js"; describe("PTY 引擎的解析", () => { test("以 bundle 身份运行时,重新进入这个产物并带上 claude-pty 子命令", () => { const launch = launchWith({ SATELLITE_ENTRY_PATH: BUNDLE, CLAUDE_PTY_SCRIPT: "" }); expect(launch).toEqual([process.execPath, BUNDLE, "claude-pty"]); }); test("源码跑(没有 bundle)时仍指向 bin/claude-pty.ts,且不带子命令", () => { const launch = launchWith({ SATELLITE_ENTRY_PATH: "", CLAUDE_PTY_SCRIPT: "" }); expect(launch).toHaveLength(2); expect(launch[0]).toBe(process.execPath); expect(launch[1].endsWith("/bin/claude-pty.ts")).toBe(true); }); test("显式 CLAUDE_PTY_SCRIPT 压过 bundle —— 它指的是一个脚本路径,不该被追加子命令", () => { // 这是逃生口:某台节点要临时挂一个改过的 wrapper 时用。追加了子命令, // 那个脚本会把 "claude-pty" 当成 prompt 的一部分。 const launch = launchWith({ SATELLITE_ENTRY_PATH: BUNDLE, CLAUDE_PTY_SCRIPT: "/tmp/patched-pty.ts" }); expect(launch).toEqual([process.execPath, "/tmp/patched-pty.ts"]); }); }); describe("只读一次性 Agent", () => { test("CLAUDE_BIN=agent 时使用 Cursor CLI 自己的参数", () => { const launch = readOnlyWith({ CLAUDE_BIN: "agent", STREAMLINE_AGENT_MODE: "pty" }); expect(launch.model).toBe("cursor-agent"); expect(launch.cmd).toEqual([ "agent", "--print", "--output-format", "stream-json", "--stream-partial-output", "--mode", "ask", "--trust", "atomize me", ]); }); test("显式 cursor 模式的一次性只读任务绕过通用兼容层和 MCP 自动批准", () => { const launch = readOnlyWith({ CLAUDE_BIN: "claude", STREAMLINE_AGENT_MODE: "cursor", CURSOR_BIN: "agent" }); expect(launch.model).toBe("cursor-agent"); expect(launch.cmd[0]).toBe("agent"); expect(launch.cmd).toContain("--stream-partial-output"); expect(launch.cmd).toContain("ask"); expect(launch.cmd).not.toContain("--approve-mcps"); expect(launch.cmd.join(" ")).not.toContain("claude-cursor"); }); test("只设 CLAUDE_BIN=agent 时,完整流水线自动使用 Cursor 兼容层", () => { expect(modeWith({ CLAUDE_BIN: "agent", STREAMLINE_AGENT_MODE: "", CURSOR_BIN: "" })) .toEqual({ mode: "cursor", cursorBin: "agent" }); }); }); describe("Codex 引擎", () => { test("显式 codex 模式从 bundle 重进 codex-exec 子命令", () => { const launch = launchWith({ SATELLITE_ENTRY_PATH: BUNDLE, STREAMLINE_AGENT_MODE: "codex", CODEX_BIN: "codex" }); expect(launch).toEqual([process.execPath, BUNDLE, "codex-exec"]); }); test("CLAUDE_BIN=codex 的旧写法自动切到 Codex adapter", () => { expect(modeWith({ CLAUDE_BIN: "/tmp/codex", STREAMLINE_AGENT_MODE: "", CODEX_BIN: "" })) .toEqual({ mode: "codex", codexBin: "/tmp/codex" }); }); test("Codex 只读任务也经过 adapter,保持下游 stream-json 契约", () => { const launch = readOnlyWith({ STREAMLINE_AGENT_MODE: "codex", CODEX_BIN: "codex" }); expect(launch.model).toBe("codex"); expect(launch.cmd[0]).toBe(process.execPath); expect(launch.cmd[1]).toContain("claude-codex"); expect(launch.cmd).toContain("--streamline-read-only"); }); }); /** * STREAMLINE_EVALUATE_AGENT_MODE 是给 evaluate 阶段单独选引擎的旋钮:未配置时 * 沿用全局 STREAMLINE_AGENT_MODE(run 老行为不动),配置了就单独切换。这里同时也 * 钉住 EVALUATE_AGENT_PROVIDER 与之一致 —— security_evaluator.ts 落库和 telemetry * 用的是这个 provider 值,如果它跟真实拉起的引擎不匹配,飞书卡片和数据面就会说 * 谎(claude 跑的活儿被记成 cursor 之类)。 */ function evaluateWith(env: Record): { mode: string; provider: string; launch: string[]; runMode: string; runProvider: string; runLaunch: string[]; } { const code = `const m = await import(${JSON.stringify(MOD)}); console.log(JSON.stringify({ mode: m.EVALUATE_AGENT_MODE, provider: m.EVALUATE_AGENT_PROVIDER, launch: m.EVALUATE_CLAUDE_LAUNCH, runMode: m.AGENT_MODE, runProvider: m.AGENT_PROVIDER, runLaunch: m.CLAUDE_LAUNCH, }));`; const r = Bun.spawnSync({ cmd: [process.execPath, "-e", code], env: { ...process.env, ...env }, stdout: "pipe", stderr: "pipe", }); const out = r.stdout.toString().trim(); if (!out) throw new Error(`no output: ${r.stderr.toString()}`); return JSON.parse(out.split("\n").pop()!); } describe("EVALUATE_AGENT: evaluate 阶段独立引擎", () => { test("未配置时沿用全局 —— run/eval 走同一个引擎", () => { const r = evaluateWith({ STREAMLINE_AGENT_MODE: "pty", STREAMLINE_EVALUATE_AGENT_MODE: "" }); expect(r.mode).toBe("pty"); expect(r.runMode).toBe("pty"); expect(r.provider).toBe("claude"); expect(r.runProvider).toBe("claude"); // 未配置时 EVALUATE_CLAUDE_LAUNCH 与 CLAUDE_LAUNCH 应完全相等 —— 不该是一个 // 内容相同但对象不同的 clone,否则真出了热部署等边角问题排查会绕远路。 expect(r.launch).toEqual(r.runLaunch); }); test("显式设 codex 时只有 evaluate 切走,run 不动", () => { const r = evaluateWith({ STREAMLINE_AGENT_MODE: "pty", STREAMLINE_EVALUATE_AGENT_MODE: "codex", CODEX_BIN: "codex" }); expect(r.mode).toBe("codex"); expect(r.provider).toBe("codex"); expect(r.launch[1]).toContain("claude-codex"); // run 侧仍是 pty:没被这次覆盖污染,是这个特性的核心保证。 expect(r.runMode).toBe("pty"); expect(r.runProvider).toBe("claude"); }); test("显式设 cursor 时 provider 也跟上", () => { const r = evaluateWith({ STREAMLINE_AGENT_MODE: "pty", STREAMLINE_EVALUATE_AGENT_MODE: "cursor", CURSOR_BIN: "cursor-agent" }); expect(r.mode).toBe("cursor"); expect(r.provider).toBe("cursor"); expect(r.launch[1]).toContain("claude-cursor"); expect(r.runMode).toBe("pty"); }); test("值不在白名单时静默回落到全局 —— 不 crash 也不切换", () => { const r = evaluateWith({ STREAMLINE_AGENT_MODE: "pty", STREAMLINE_EVALUATE_AGENT_MODE: "garbage" }); expect(r.mode).toBe("pty"); expect(r.runMode).toBe("pty"); expect(r.launch).toEqual(r.runLaunch); }); });