/** * interrupt-marker 端到端:**真 pi(RPC 模式)+ 假 LLM**(fake-llm.ts),复现「发 prompt → 流到一半 / 工具跑到一半 * ESC 中止 → 立刻输入新指令」,断言第二次请求里模型到底收到了什么。anthropic 与 openai 两种线格式各跑一遍。 * * 隔离:`PI_CODING_AGENT_DIR` 指向临时目录(只放一个假 provider 的 models.json),`-ne -ns -np -nc --no-mcp * --no-themes` 关掉发现;只有 `-e` 显式给的扩展会加载。不碰用户真实的 `~/.pi/agent`。 * * 时序贴近 TUI:ESC 不 await `abort()`,所以这里发 `abort` 后不等应答,400ms 后就发新 prompt。 * * 找不到本机 pi 就整体 skip(不假装通过)。 * * node --test clients/pi/extensions/interrupt-marker/interrupt-marker.e2e.test.ts */ import assert from "node:assert/strict"; import { spawn, spawnSync } from "node:child_process"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { describe, test } from "node:test"; import { fileURLToPath } from "node:url"; import type { Api } from "./fake-llm.ts"; import { modelsJson, PROMPTS, startFakeLlm, summarize } from "./fake-llm.ts"; const EXT = path.join(path.dirname(fileURLToPath(import.meta.url)), "index.ts"); const skip = spawnSync("pi", ["--version"], { encoding: "utf8" }).status === 0 ? false : "找不到本机 pi(PATH 上没有 pi),端到端整体跳过"; const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); interface RunOptions { api: Api; scenario: "stream" | "tool" | "quiet"; extensions: string[]; env?: Record; } interface RunResult { requests: any[]; settled: boolean[]; stored: any[]; } async function runScenario(o: RunOptions): Promise { const llm = await startFakeLlm(o.api); const root = fs.mkdtempSync(path.join(os.tmpdir(), "interrupt-marker-e2e-")); const agentDir = path.join(root, "agent"); const projDir = path.join(root, "proj"); fs.mkdirSync(agentDir); fs.mkdirSync(projDir); fs.writeFileSync(path.join(agentDir, "models.json"), JSON.stringify(modelsJson(o.api, llm.baseUrl))); const child = spawn( "pi", [ "--mode", "rpc", "--no-session", "-ne", "-ns", "-np", "--no-themes", "-nc", "--no-mcp", "--offline", "-na", "--provider", "fake", "--model", "fake-1", ...(o.scenario === "stream" ? ["--no-tools"] : ["--tools", "bash"]), ...o.extensions.flatMap((e) => ["-e", e]), ], { cwd: projDir, env: { ...process.env, ...o.env, PI_CODING_AGENT_DIR: agentDir, PI_SKIP_VERSION_CHECK: "1", PI_OFFLINE: "1" }, stdio: ["pipe", "pipe", "pipe"], }, ); // RPC 是严格的 JSONL:只按 \n 切(不能用 readline,它还会按 U+2028 / U+2029 切) const events: any[] = []; let stderr = ""; let buffer = ""; child.stderr.setEncoding("utf8"); child.stderr.on("data", (d: string) => { stderr += d; }); child.stdout.setEncoding("utf8"); child.stdout.on("data", (d: string) => { buffer += d; for (let i = buffer.indexOf("\n"); i >= 0; i = buffer.indexOf("\n")) { const line = buffer.slice(0, i).replace(/\r$/, ""); buffer = buffer.slice(i + 1); if (!line.trim()) continue; try { events.push(JSON.parse(line)); } catch { // 不是 JSON 的行(不该有)直接忽略 } } }); const send = (command: object) => child.stdin.write(`${JSON.stringify(command)}\n`); const until = async (get: () => T | undefined | false, ms: number, label: string): Promise => { const deadline = Date.now() + ms; for (;;) { const value = get(); if (value) return value; if (Date.now() > deadline) throw new Error(`等待超时:${label}\n[pi stderr] ${stderr.slice(0, 800)}`); await sleep(25); } }; const response = (id: string) => events.find((e) => e.type === "response" && e.id === id); const settledEvents = () => events.filter((e) => e.type === "agent_settled"); try { send({ id: "ready", type: "get_state" }); await until(() => response("ready"), 30_000, "get_state 应答(pi 就绪)"); send({ id: "p1", type: "prompt", message: PROMPTS[o.scenario] }); if (o.scenario === "stream") { await until( () => events.filter((e) => e.type === "message_update" && e.assistantMessageEvent?.type === "text_delta").length >= 8, 30_000, "8 个 text_delta", ); } else if (o.scenario === "quiet") { // 工具(echo hi)秒级成功跑完;等模型收到工具结果的那次请求发出、并静默一会儿,再按 ESC await until(() => events.some((e) => e.type === "tool_execution_end"), 30_000, "tool_execution_end"); await until(() => llm.requests.length >= 2, 15_000, "工具结果回来后的那次请求"); await sleep(500); } else { await until(() => events.some((e) => e.type === "tool_execution_start"), 30_000, "tool_execution_start"); await sleep(1200); } send({ id: "abort", type: "abort" }); // 不等应答:TUI 里 ESC 不 await abort() await sleep(400); send({ id: "p2", type: "prompt", message: PROMPTS.next }); await until(() => settledEvents().length >= 2, 40_000, "两个 agent_settled"); send({ id: "msgs", type: "get_messages" }); const stored = (await until(() => response("msgs"), 10_000, "get_messages 应答")).data.messages; return { requests: llm.requests, settled: settledEvents().map((e) => e.aborted === true), stored }; } finally { child.kill("SIGTERM"); await llm.close(); fs.rmSync(root, { recursive: true, force: true }); } } /** 夹具:一个会在 agent_settled 里睡 3 秒的扩展(模拟慢处理器),写到临时目录。 */ function writeSlowSettleFixture(): string { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "interrupt-marker-slow-")); const file = path.join(dir, "slow-settle.ts"); fs.writeFileSync(file, 'export default function (pi) { pi.on("agent_settled", () => new Promise((r) => setTimeout(r, 3000))); }\n'); return file; } describe("interrupt-marker 端到端(真 pi + 假 LLM)", { concurrency: 3 }, () => { for (const api of ["anthropic", "openai"] as const) { test(`[${api}] 流式中止:第二次请求 = 老 prompt、半截回复、标记、新指令`, { skip, timeout: 60_000 }, async () => { const r = await runScenario({ api, scenario: "stream", extensions: [EXT] }); const s = summarize(r.requests[1], api); assert.equal(r.requests.length, 2); assert.equal(s.length, 4); assert.equal(s[0], `user: ${PROMPTS.stream}`); assert.match(s[1]!, /^assistant: step0 step1 /); assert.equal(s[2], "user: [Request interrupted by user]"); assert.equal(s[3], `user: ${PROMPTS.next}`); assert.deepEqual(r.settled, [true, false]); assert.ok(r.stored.some((m) => m.role === "assistant" && m.stopReason === "aborted"), "存储里的 aborted 消息不动"); }); test(`[${api}] 工具执行中止:标记是 for tool use,工具调用与结果都还在`, { skip, timeout: 60_000 }, async () => { const r = await runScenario({ api, scenario: "tool", extensions: [EXT] }); const s = summarize(r.requests[1], api); assert.ok(s.some((x) => x.includes(""))); assert.ok(s.some((x) => x.includes(""))); assert.equal(s.at(-2), "user: [Request interrupted by user for tool use]"); assert.equal(s.at(-1), `user: ${PROMPTS.next}`); }); test(`[${api}] 工具已成功跑完、模型静默思考时按 ESC:标记是普通文案,不是 for tool use`, { skip, timeout: 60_000 }, async () => { const r = await runScenario({ api, scenario: "quiet", extensions: [EXT] }); const s = summarize(r.requests.at(-1), api); assert.equal(r.requests.length, 3); assert.ok(s.some((x) => x.includes(""))); assert.ok(s.some((x) => x.includes(""))); assert.equal(s.at(-2), "user: [Request interrupted by user]"); assert.equal(s.at(-1), `user: ${PROMPTS.next}`); assert.deepEqual(r.settled, [true, false]); }); } test("慢的 agent_settled 处理器排在前面:新 prompt 仍然晚于标记", { skip, timeout: 60_000 }, async () => { const slow = writeSlowSettleFixture(); try { const r = await runScenario({ api: "anthropic", scenario: "stream", extensions: [slow, EXT] }); const s = summarize(r.requests[1], "anthropic"); assert.equal(s.at(-2), "user: [Request interrupted by user]"); assert.equal(s.at(-1), `user: ${PROMPTS.next}`); assert.match(s[1]!, /^assistant: step0 /); } finally { fs.rmSync(path.dirname(slow), { recursive: true, force: true }); } }); test("PI_INTERRUPT_MARKER=off:退回基线(无标记、无半截回复)", { skip, timeout: 60_000 }, async () => { const r = await runScenario({ api: "anthropic", scenario: "stream", extensions: [EXT], env: { PI_INTERRUPT_MARKER: "off" } }); assert.deepEqual(summarize(r.requests[1], "anthropic"), [`user: ${PROMPTS.stream}`, `user: ${PROMPTS.next}`]); }); });