conversation-session-bash-direct-output.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. import {
  2. copyFile,
  3. mkdir,
  4. open,
  5. readFile,
  6. readdir,
  7. rm,
  8. stat,
  9. statfs,
  10. writeFile,
  11. } from "node:fs/promises";
  12. import { join } from "node:path";
  13. import { clearAppData, DEFAULT_WORKSPACE, getE2EAppDataPaths } from "../helpers/desktop-app.js";
  14. import { resolveE2ERuntimePath } from "../helpers/e2e-runtime-paths.js";
  15. import {
  16. getLatestUpstreamToolResultByToolCallId,
  17. countUpstreamRequests,
  18. } from "../helpers/conversation-session-network.js";
  19. import {
  20. listToolCallBlocks,
  21. waitForToolCallBlockByToolCallId,
  22. } from "../helpers/conversation-session-tool.js";
  23. import {
  24. clickV4Stop,
  25. getV4PaneSnapshot,
  26. getV4ConfigProjection,
  27. prepareV4ConversationE2E,
  28. sendV4Prompt,
  29. switchV4Mode,
  30. selectV4TaskById,
  31. waitForV4AssistantMessageContaining,
  32. waitForV4ConversationState,
  33. } from "../helpers/v4-conversation.js";
  34. import { startDraftFromProject } from "../helpers/project-sidebar.js";
  35. const ROOT = resolveE2ERuntimePath("bash-direct-output", "中文 空格");
  36. const LIMIT = 5 * 1024 ** 3;
  37. const RELEASES = [
  38. "A.release",
  39. "B.release",
  40. "B.finish",
  41. "query.grow",
  42. "query.release",
  43. "foreground.release",
  44. "foreground.finish",
  45. "background.release",
  46. "background.finish",
  47. "read-failure.release",
  48. "realtime.1.release",
  49. "realtime.2.release",
  50. "realtime.3.release",
  51. "realtime.4.release",
  52. "realtime.5.release",
  53. ];
  54. const outputs = new Set<string>();
  55. async function release(name: string) {
  56. await writeFile(join(ROOT, name), "release");
  57. }
  58. async function waitFile(name: string) {
  59. await browser.waitUntil(async () => (await stat(join(ROOT, name)).catch(() => null)) !== null, {
  60. timeout: 90000,
  61. timeoutMsg: `producer 没有到达 ${name}`,
  62. });
  63. }
  64. async function idle() {
  65. await waitForV4ConversationState((s) => s.state === "idle", "BDO 会话没有回到 idle", 30000);
  66. }
  67. async function prompt(marker: string, newTask = true) {
  68. if (newTask) {
  69. // 合并适配:全局新任务落到独立聊天目录,Bash fixture 必须从 ZCodeProject 发起。
  70. await startDraftFromProject(DEFAULT_WORKSPACE);
  71. // 项目新草稿恢复 build;显式选择 yolo,避免把模式切换动画中的旧文案当成已选中。
  72. await switchV4Mode("yolo");
  73. await browser.waitUntil(async () => (await getV4ConfigProjection()).mode === "yolo", {
  74. timeout: 15000,
  75. timeoutMsg: "项目草稿未应用 yolo 模式",
  76. });
  77. }
  78. await sendV4Prompt(`${marker}: Execute the exact Bash output regression fixture.`);
  79. }
  80. async function done(marker: string) {
  81. await waitForV4AssistantMessageContaining(`${marker}_DONE`, 90000);
  82. await idle();
  83. }
  84. async function result(id: string) {
  85. const value = await getLatestUpstreamToolResultByToolCallId(id);
  86. expect(value).not.toBeNull();
  87. return value!;
  88. }
  89. async function outputPath(id: string) {
  90. const sessionId = (await getV4PaneSnapshot()).sessionId;
  91. expect(sessionId).not.toBe("draft");
  92. const path = join(
  93. getE2EAppDataPaths().storageRoot,
  94. "cli",
  95. "exec",
  96. sessionId!,
  97. `${id}-stdout.log`,
  98. );
  99. outputs.add(path);
  100. return path;
  101. }
  102. async function snippet(path: string, tail = false) {
  103. const handle = await open(path, "r");
  104. try {
  105. const size = (await handle.stat()).size;
  106. const buffer = Buffer.alloc(Math.min(size, 128));
  107. await handle.read(buffer, 0, buffer.length, tail ? size - buffer.length : 0);
  108. return buffer.toString();
  109. } finally {
  110. await handle.close();
  111. }
  112. }
  113. async function setExpanded(id: string, expanded: boolean) {
  114. await waitForToolCallBlockByToolCallId(id);
  115. const trigger = await $(`[data-testid="tool-summary-trigger-${id}"]`);
  116. if ((await trigger.getAttribute("aria-expanded")) !== String(expanded)) await trigger.click();
  117. }
  118. async function assertOutputHidden(id: string) {
  119. await browser.waitUntil(
  120. async () =>
  121. browser.execute((toolId) => {
  122. const block = document.querySelector(`[data-testid="chat-tool-call-block-${toolId}"]`);
  123. return (
  124. Boolean(block) &&
  125. !block!.querySelector(
  126. '[data-testid^="bash-output-preview-"], [data-testid="bash-result-output"], [data-testid="bash-output-notice"], [data-testid="bash-output-file"]',
  127. )
  128. );
  129. }, id),
  130. { timeout: 5000, timeoutMsg: `收起后的 Bash 仍渲染输出:${id}` },
  131. );
  132. }
  133. describe("Bash 直接文件输出补充 E2E", () => {
  134. before(async function () {
  135. this.timeout(120000);
  136. await mkdir(ROOT, { recursive: true });
  137. await copyFile(
  138. new URL("../fixtures/fs/conversation-session/bash-direct-output/writer.cjs", import.meta.url),
  139. join(ROOT, "writer.cjs"),
  140. );
  141. await prepareV4ConversationE2E();
  142. await switchV4Mode("yolo");
  143. await browser.waitUntil(async () => (await getV4ConfigProjection()).mode === "yolo", {
  144. timeout: 15000,
  145. timeoutMsg: "项目草稿未应用 yolo 模式",
  146. });
  147. });
  148. beforeEach(async () => {
  149. for (const name of await readdir(ROOT)) if (name.endsWith(".pid")) await rm(join(ROOT, name));
  150. for (const name of [
  151. ...RELEASES,
  152. "query.ready",
  153. "query.grown",
  154. "foreground.ready",
  155. "background.ready",
  156. "read-failure.ready",
  157. "realtime.1.ready",
  158. "realtime.2.ready",
  159. "realtime.3.ready",
  160. "realtime.4.ready",
  161. "realtime.5.ready",
  162. ]) {
  163. await rm(join(ROOT, name), { force: true });
  164. }
  165. });
  166. afterEach(async () => {
  167. // 失败同样释放本 case 的子进程,避免下一条测试或 coverage teardown 被遗留任务影响。
  168. await Promise.all(RELEASES.map(release));
  169. if ((await getV4PaneSnapshot()).canStop) await clickV4Stop();
  170. const pids = await Promise.all(
  171. (await readdir(ROOT))
  172. .filter((name) => name.endsWith(".pid"))
  173. .map(async (name) => Number(await readFile(join(ROOT, name), "utf8"))),
  174. );
  175. await browser.waitUntil(
  176. async () =>
  177. pids.every((pid) => {
  178. try {
  179. process.kill(pid, 0);
  180. return false;
  181. } catch {
  182. return true;
  183. }
  184. }),
  185. { timeout: 10000, timeoutMsg: "本条 Bash producer 没有清理完成" },
  186. );
  187. for (const path of outputs) await rm(path, { force: true, recursive: true });
  188. outputs.clear();
  189. });
  190. after(async () => {
  191. await browser.electron.restoreAllMocks();
  192. await clearAppData();
  193. });
  194. it("BDO01: 中文空格路径、两流无换行与非零空输出保持正确结果", async function () {
  195. this.timeout(120000);
  196. await prompt("E2E_BDO_SMALL");
  197. await done("E2E_BDO_SMALL");
  198. expect((await result("toolu_bdo_small")).content).toBe("BDO_中文_STDOUT|中文_STDERR");
  199. expect((await result("toolu_bdo_empty_error")).content).toContain("Exit code 7");
  200. expect(await stat(await outputPath("toolu_bdo_small")).catch(() => null)).toBeNull();
  201. });
  202. it("BDO02: 超过 64 MiB 的前台输出保留完整文件,详情只展示正文", async function () {
  203. this.timeout(120000);
  204. await prompt("E2E_BDO_LARGE");
  205. await done("E2E_BDO_LARGE");
  206. const path = await outputPath("toolu_bdo_large");
  207. expect((await stat(path)).size).toBe(
  208. 65 * 1024 ** 2 + Buffer.byteLength("BDO_HEAD\n\nBDO_TAIL\n"),
  209. );
  210. expect(await snippet(path)).toContain("BDO_HEAD");
  211. expect(await snippet(path, true)).toContain("BDO_TAIL");
  212. expect((await result("toolu_bdo_large")).content).toContain(path);
  213. await setExpanded("toolu_bdo_large", false);
  214. await assertOutputHidden("toolu_bdo_large");
  215. await setExpanded("toolu_bdo_large", true);
  216. const body = await $('[data-testid="bash-result-output"]');
  217. await body.waitForDisplayed();
  218. await browser.waitUntil(async () => (await body.getText()).includes("BDO_HEAD"), {
  219. timeout: 10000,
  220. timeoutMsg: "展开后的 Bash 头部摘要没有显示",
  221. });
  222. expect(await body.getText()).toContain("BDO_HEAD");
  223. expect(await body.getText()).not.toContain("BDO_TAIL");
  224. expect(Buffer.byteLength(await body.getText())).toBe(30000);
  225. expect(await $('[data-testid="bash-output-notice"]').isExisting()).toBe(false);
  226. expect(await $('[data-testid="bash-output-file"]').isExisting()).toBe(false);
  227. await setExpanded("toolu_bdo_large", false);
  228. await assertOutputHidden("toolu_bdo_large");
  229. });
  230. it("BDO03: 并发前台 Bash 共享进度期间一个退出不影响另一个", async function () {
  231. this.timeout(120000);
  232. const previews = () =>
  233. browser.execute(() =>
  234. Array.from(document.querySelectorAll('[data-testid="bash-output-preview-full"]')).map(
  235. (el) => el.textContent ?? "",
  236. ),
  237. );
  238. const waitPreview = (text: string) =>
  239. browser.waitUntil(async () => (await previews()).some((s) => s.includes(text)), {
  240. timeout: 30000,
  241. timeoutMsg: `Bash 没有更新进度:${text}`,
  242. });
  243. // 同一批 mutating Bash 会串行执行;两个独立会话才能形成真实并发。
  244. await prompt("E2E_BDO_PROGRESS_A");
  245. await setExpanded("toolu_bdo_progress_a", true);
  246. await waitPreview("A-STAGE1");
  247. const fullPreview = await $('[data-testid="bash-output-preview-full"]');
  248. await fullPreview.waitForDisplayed();
  249. // 展开动画期间元素已有尺寸但可见文本仍可能为空;等待实际预览再检查行数和字节数。
  250. await browser.waitUntil(async () => (await fullPreview.getText()).includes("A-STAGE1"), {
  251. timeout: 10000,
  252. timeoutMsg: "展开后的 Bash 进度预览没有显示",
  253. });
  254. const fullText = await fullPreview.getText();
  255. expect(fullText).toContain("A-STAGE1");
  256. expect(fullText.split("\n").length).toBeGreaterThan(5);
  257. expect(fullText.split("\n").length).toBeLessThanOrEqual(100);
  258. expect(Buffer.byteLength(fullText)).toBeLessThanOrEqual(4096);
  259. await setExpanded("toolu_bdo_progress_a", false);
  260. await assertOutputHidden("toolu_bdo_progress_a");
  261. const sessionA = (await getV4PaneSnapshot()).sessionId!;
  262. await outputPath("toolu_bdo_progress_a");
  263. await prompt("E2E_BDO_PROGRESS_B");
  264. await setExpanded("toolu_bdo_progress_b", true);
  265. await waitPreview("B-STAGE1");
  266. const sessionB = (await getV4PaneSnapshot()).sessionId!;
  267. await outputPath("toolu_bdo_progress_b");
  268. for (const text of await previews()) expect(text.length).toBeLessThanOrEqual(4096);
  269. await selectV4TaskById(sessionA);
  270. // 切回会话后明确展开,再检查进度;不隐含验证卡片展开状态的恢复。
  271. await setExpanded("toolu_bdo_progress_a", true);
  272. await waitPreview("A-STAGE1");
  273. await release("A.release");
  274. await done("E2E_BDO_PROGRESS_A");
  275. await selectV4TaskById(sessionB);
  276. await setExpanded("toolu_bdo_progress_b", true);
  277. await release("B.release");
  278. await browser.waitUntil(async () => (await previews()).some((s) => s.includes("B-STAGE2")), {
  279. timeout: 10000,
  280. timeoutMsg: "第一个 Bash 完成后另一个 Bash 的进度停止更新",
  281. });
  282. await release("B.finish");
  283. await done("E2E_BDO_PROGRESS_B");
  284. expect(await previews()).toEqual([]);
  285. });
  286. it("BDO07: 展开时实时刷新,收起不渲染输出,再展开显示最新内容", async function () {
  287. this.timeout(60000);
  288. const id = "toolu_bdo_realtime";
  289. const marker = "E2E_BDO_REALTIME";
  290. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  291. if (!artifactDir) throw new Error("E2E artifact directory is required for refresh timings");
  292. const observations: {
  293. stage: number;
  294. fileVisibleMs: number;
  295. previewVisibleMs: number;
  296. height: number;
  297. lineHeight: number;
  298. bottomGap: number;
  299. }[] = [];
  300. await prompt(marker);
  301. await waitFile("realtime.1.ready");
  302. const path = await outputPath(id);
  303. const pid = Number(await readFile(join(ROOT, "realtime.pid"), "utf8"));
  304. await setExpanded(id, true);
  305. let expectedOutput = "";
  306. let frozenOutput = "";
  307. const scrollMetrics = () =>
  308. browser.execute(() => {
  309. const el = document.querySelector<HTMLElement>('[data-testid="bash-output-scroll"]')!;
  310. return {
  311. height: el.clientHeight,
  312. lineHeight: Number.parseFloat(getComputedStyle(el.querySelector("pre")!).lineHeight),
  313. bottomGap: el.scrollHeight - el.scrollTop - el.clientHeight,
  314. };
  315. });
  316. for (const stage of [1, 2, 3, 4, 5]) {
  317. if (stage > 1) await waitFile(`realtime.${stage}.ready`);
  318. const writtenAt = Number(await readFile(join(ROOT, `realtime.${stage}.ready`), "utf8"));
  319. const fragment = `实时-${stage}-stdout|实时-${stage}-stderr`;
  320. expectedOutput += (stage > 1 ? `line-${stage} abcdefghijklmnop\n`.repeat(80) : "") + fragment;
  321. // ready 由 producer 在两流写入后创建;release 前直接读原文件,不能用最终结果冒充实时输出。
  322. expect(await readFile(path, "utf8")).toBe(expectedOutput);
  323. const fileVisibleMs = Date.now() - writtenAt;
  324. if (stage === 3) {
  325. // 收起期间跨过一次 1 秒进度轮询;新输出仍不能重新渲染正文。
  326. await browser.pause(1200);
  327. await assertOutputHidden(id);
  328. await setExpanded(id, true);
  329. }
  330. const preview = await $('[data-testid="bash-output-preview-full"]');
  331. if (stage === 4) {
  332. // 用户上滚后即使 CLI 已发布新一段,正在阅读的有界窗口也不能被替换。
  333. await browser.pause(1500);
  334. expect(await preview.getText()).toBe(frozenOutput);
  335. await browser.execute(() => {
  336. const el = document.querySelector<HTMLElement>('[data-testid="bash-output-scroll"]')!;
  337. el.scrollTop = el.scrollHeight;
  338. el.dispatchEvent(new Event("scroll", { bubbles: true }));
  339. });
  340. }
  341. await browser.waitUntil(
  342. async () => (await preview.isExisting()) && (await preview.getText()).includes(fragment),
  343. {
  344. timeout: stage === 1 ? 8000 : 5000,
  345. timeoutMsg: `第 ${stage} 段 Bash 输出未在退出前刷新`,
  346. },
  347. );
  348. const previewVisibleMs = Date.now() - writtenAt;
  349. expect(previewVisibleMs).toBeLessThanOrEqual(stage === 1 ? 8000 : 5000);
  350. expect(process.kill(pid, 0)).toBe(true);
  351. expect((await listToolCallBlocks()).find((block) => block.toolCallId === id)?.status).toBe(
  352. "in_progress",
  353. );
  354. expect(await getLatestUpstreamToolResultByToolCallId(id)).toBeNull();
  355. const metrics = await scrollMetrics();
  356. observations.push({ stage, fileVisibleMs, previewVisibleMs, ...metrics });
  357. console.log("BASH_REALTIME_VIEWPORT", JSON.stringify(observations.at(-1)));
  358. await writeFile(
  359. join(artifactDir, "bash-realtime-refresh.json"),
  360. JSON.stringify(observations, null, 2),
  361. );
  362. expect(metrics.lineHeight).toBeGreaterThan(0);
  363. expect(metrics.height).toBe(metrics.lineHeight * (stage === 1 ? 1 : 5));
  364. expect(metrics.bottomGap).toBeLessThanOrEqual(1);
  365. const scrollViewport = await $('[data-testid="bash-output-scroll"]');
  366. await browser.waitUntil(
  367. async () =>
  368. (await scrollViewport.getAttribute("data-scroll-mask")) ===
  369. (stage === 1 ? "none" : "top"),
  370. { timeout: 5000, timeoutMsg: "Bash 吸底后渐隐方向不正确" },
  371. );
  372. expect(
  373. await scrollViewport.getCSSProperty("mask-image").then((value) => value.value === "none"),
  374. ).toBe(stage === 1);
  375. expect(await $('[data-testid="bash-output-resume"]').isExisting()).toBe(false);
  376. if (stage === 2) {
  377. await setExpanded(id, false);
  378. await assertOutputHidden(id);
  379. }
  380. if (stage === 3 || stage === 5) {
  381. frozenOutput = await preview.getText();
  382. await browser.execute(() => {
  383. const el = document.querySelector<HTMLElement>('[data-testid="bash-output-scroll"]')!;
  384. el.scrollTop = (el.scrollHeight - el.clientHeight) / 2;
  385. el.dispatchEvent(new Event("scroll", { bubbles: true }));
  386. });
  387. await browser.waitUntil(
  388. async () => (await scrollViewport.getAttribute("data-scroll-mask")) === "both",
  389. {
  390. timeout: 5000,
  391. timeoutMsg: "Bash 中间位置没有上下渐隐",
  392. },
  393. );
  394. await browser.execute(() => {
  395. const el = document.querySelector<HTMLElement>('[data-testid="bash-output-scroll"]')!;
  396. el.scrollTop = 0;
  397. el.dispatchEvent(new Event("scroll", { bubbles: true }));
  398. });
  399. await browser.waitUntil(
  400. async () =>
  401. (await $('[data-testid="bash-output-scroll"]').getAttribute("data-following")) ===
  402. "false",
  403. { timeout: 5000, timeoutMsg: "用户上滚后没有暂停跟随" },
  404. );
  405. expect(await $('[data-testid="bash-output-resume"]').isExisting()).toBe(false);
  406. expect(await scrollViewport.getAttribute("data-scroll-mask")).toBe("bottom");
  407. }
  408. await release(`realtime.${stage}.release`);
  409. }
  410. await done(marker);
  411. // assistant 收尾会把工具移入可折叠历史;重新定位展开,不把父级卸载当成输出视口丢失。
  412. await setExpanded(id, false);
  413. await assertOutputHidden(id);
  414. await setExpanded(id, true);
  415. // 详情有展开动画;等待真实可见文本,不能把动画首帧的空 getText 当成丢输出。
  416. const finalOutput = await $('[data-testid="bash-result-output"]');
  417. await browser.waitUntil(async () => (await finalOutput.getText()) === expectedOutput, {
  418. timeout: 10000,
  419. timeoutMsg: "重新展开后没有显示最终 Bash 输出",
  420. });
  421. const finalMetrics = await scrollMetrics();
  422. expect(finalMetrics.height).toBe(finalMetrics.lineHeight * 5);
  423. expect(await $('[data-testid="bash-output-resume"]').isExisting()).toBe(false);
  424. expect((await result(id)).content).toBe(expectedOutput);
  425. expect((await waitForToolCallBlockByToolCallId(id)).status).toBe("completed");
  426. expect(await $('[data-testid="bash-output-preview-short"]').isExisting()).toBe(false);
  427. expect(await $('[data-testid="bash-output-preview-full"]').isExisting()).toBe(false);
  428. await setExpanded(id, false);
  429. await assertOutputHidden(id);
  430. expect(
  431. await countUpstreamRequests({ includes: [marker], excludes: ["Generate a concise title"] }),
  432. ).toBe(2);
  433. });
  434. it("BDO04: TaskOutput 运行时重复头读、完成后尾读", async function () {
  435. this.timeout(120000);
  436. await prompt("E2E_BDO_QUERY");
  437. await done("E2E_BDO_QUERY");
  438. await waitFile("query.ready");
  439. const path = await outputPath("toolu_bdo_query");
  440. await prompt("E2E_BDO_QUERY_FIRST", false);
  441. await done("E2E_BDO_QUERY_FIRST");
  442. const first = (await result("toolu_bdo_query_first")).content;
  443. expect(first).toContain("BDO_QUERY_HEAD");
  444. expect(first).not.toContain("BDO_QUERY_FIRST_TAIL");
  445. await release("query.grow");
  446. await waitFile("query.grown");
  447. await prompt("E2E_BDO_QUERY_SECOND", false);
  448. await done("E2E_BDO_QUERY_SECOND");
  449. const second = (await result("toolu_bdo_query_second")).content;
  450. const firstOutput = first.match(/<output>([\s\S]*?)<\/output>/)?.[1];
  451. expect(firstOutput).toBeDefined();
  452. expect(second.match(/<output>([\s\S]*?)<\/output>/)?.[1]).toBe(firstOutput);
  453. expect(second).not.toContain("BDO_QUERY_SECOND_TAIL");
  454. await release("query.release");
  455. await waitForV4AssistantMessageContaining("E2E_BDO_QUERY_NOTIFIED", 30000);
  456. await prompt("E2E_BDO_QUERY_FINAL", false);
  457. await done("E2E_BDO_QUERY_FINAL");
  458. const final = (await result("toolu_bdo_query_final")).content;
  459. expect(final).toContain("BDO_QUERY_FINAL_TAIL");
  460. expect(final).not.toContain("BDO_QUERY_HEAD");
  461. expect((await stat(path)).size).toBeGreaterThan(600000);
  462. });
  463. for (const mode of ["foreground", "background"] as const) {
  464. it(`BDO05: ${mode} 真实 5 GiB 边界与 5 秒 watchdog`, async function () {
  465. this.timeout(150000);
  466. const disk = await statfs(ROOT, { bigint: true });
  467. if (disk.bavail * disk.bsize < BigInt(12 * 1024 ** 3))
  468. throw new Error("5 GiB 实写需要至少 12 GiB 可用空间,未执行不能算通过");
  469. const marker = `E2E_BDO_LIMIT_${mode.toUpperCase()}`;
  470. await prompt(marker);
  471. await waitFile(`${mode}.ready`);
  472. const path = await outputPath(`toolu_bdo_limit_${mode}`);
  473. expect((await stat(path)).size).toBe(LIMIT);
  474. // 跨过完整检查周期后,等于阈值的命令必须仍存活。
  475. await browser.pause(5500);
  476. const pid = Number(await readFile(join(ROOT, `${mode}.pid`), "utf8"));
  477. expect(() => process.kill(pid, 0)).not.toThrow();
  478. await release(`${mode}.release`);
  479. await waitFile(`${mode}.exceeded`);
  480. await waitForV4AssistantMessageContaining(`${marker}_KILLED`, 20000);
  481. await idle();
  482. if (mode === "foreground") {
  483. const output = await result("toolu_bdo_limit_foreground");
  484. expect(output.isError).toBe(true);
  485. // 取消结果超过模型预算时沿用 artifact 包装,诊断在完整结果中而非 2 KiB 预览里。
  486. const persisted = output.content.match(/Full output saved to: ([^\n]+)/)?.[1];
  487. const full = persisted ? await readFile(persisted, "utf8") : output.content;
  488. expect(full).toContain("Command killed: output file exceeded 5GB");
  489. expect(await stat(path).catch(() => null)).toBeNull();
  490. } else {
  491. expect((await stat(path)).size).toBe(LIMIT + 1);
  492. expect(
  493. await countUpstreamRequests({
  494. includes: [marker, "<task-notification>", "<status>killed</status>"],
  495. }),
  496. ).toBe(1);
  497. }
  498. await browser.waitUntil(
  499. async () => {
  500. try {
  501. process.kill(pid, 0);
  502. return false;
  503. } catch {
  504. return true;
  505. }
  506. },
  507. { timeout: 10000, timeoutMsg: "超限 producer 没有退出" },
  508. );
  509. });
  510. }
  511. it("BDO06: 真实输出读取/打开失败有诊断,后续 Bash 仍能执行", async function () {
  512. this.timeout(120000);
  513. await prompt("E2E_BDO_READ_FAILURE");
  514. await waitFile("read-failure.ready");
  515. const path = await outputPath("toolu_bdo_read_failure");
  516. await rm(path);
  517. await release("read-failure.release");
  518. await done("E2E_BDO_READ_FAILURE");
  519. expect((await result("toolu_bdo_read_failure")).content).toContain("bash output unavailable");
  520. const blocked = await outputPath("toolu_bdo_open_failure");
  521. await mkdir(blocked, { recursive: true });
  522. await prompt("E2E_BDO_OPEN_FAILURE", false);
  523. await done("E2E_BDO_OPEN_FAILURE");
  524. // 启动失败没有进程退出码;验证实际错误诊断,不套用命令非零退出的 is_error 契约。
  525. expect((await result("toolu_bdo_open_failure")).content).toContain("EISDIR");
  526. expect((await stat(blocked)).isDirectory()).toBe(true);
  527. await prompt("E2E_BDO_RECOVERY", false);
  528. await done("E2E_BDO_RECOVERY");
  529. expect((await result("toolu_bdo_recovery")).content).toBe("BDO_中文_STDOUT|中文_STDERR");
  530. });
  531. });