subagent-host-faults.cjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // 仅由 E2E 给第二个 Electron UtilityProcess 预加载;生产入口不引用此文件。
  2. // 替换文件边界,不替换缓存、配置服务、RPC handler 或 CLI 执行对象。
  3. const fs = require("node:fs");
  4. const fsp = require("node:fs/promises");
  5. const { join } = require("node:path");
  6. const { syncBuiltinESMExports } = require("node:module");
  7. const root = process.env.ZCODE_E2E_HOST_FAULT_ROOT;
  8. const controlPath = join(root, "host-fault-control.json");
  9. const logPath = join(root, `host-boundary-${process.pid}.jsonl`);
  10. const control = () => JSON.parse(fs.readFileSync(controlPath, "utf8"));
  11. const log = (event) =>
  12. fs.appendFileSync(logPath, JSON.stringify({ pid: process.pid, ...event }) + "\n");
  13. const consumed = new Set();
  14. const readCounts = new Map();
  15. function released(id) {
  16. return control().released?.includes(id);
  17. }
  18. function waitForRelease(id) {
  19. return new Promise((resolve) => {
  20. const watcher = fs.watch(root, () => {
  21. if (released(id)) {
  22. watcher.close();
  23. resolve();
  24. }
  25. });
  26. if (released(id)) {
  27. watcher.close();
  28. resolve();
  29. }
  30. });
  31. }
  32. for (const operation of ["readFile", "readdir", "writeFile"]) {
  33. const original = fsp[operation];
  34. fsp[operation] = async function (path, ...args) {
  35. if (control().tracePaths?.includes(String(path))) {
  36. if (operation === "readFile")
  37. readCounts.set(String(path), (readCounts.get(String(path)) ?? 0) + 1);
  38. log({
  39. kind: "file-called",
  40. operation,
  41. path: String(path),
  42. paths: [String(path)],
  43. stack: new Error().stack,
  44. });
  45. }
  46. // 目录扫描故障只拦配置读取;watcher 自身的枚举另有监听异常语义,不能一起阻断事件源。
  47. const watcherScan = operation === "readdir" && /ReaddirpStream/.test(new Error().stack ?? "");
  48. const rule = control().files?.find(
  49. (r) =>
  50. !watcherScan &&
  51. r.operation === operation &&
  52. String(path) === r.path &&
  53. (!r.once || !consumed.has(r.id)),
  54. );
  55. if (!rule) return original.call(this, path, ...args);
  56. consumed.add(rule.id);
  57. if (rule.action === "fail") {
  58. log({ kind: "file-failed", id: rule.id, operation, path: String(path) });
  59. throw Object.assign(new Error(`E2E ${operation} failed`), { code: "EACCES" });
  60. }
  61. const result = await original.call(this, path, ...args);
  62. log({ kind: "file-held", id: rule.id, operation, path: String(path) });
  63. await waitForRelease(rule.id);
  64. log({ kind: "file-released", id: rule.id });
  65. if (rule.rejectAfterRelease) throw new Error("E2E stale read rejected");
  66. return result;
  67. };
  68. }
  69. syncBuiltinESMExports();
  70. // 只观察既有 Host 日志通道,将文件读取序号与随后完成的批次关联;不改写消息或缓存。
  71. const post = process.parentPort.postMessage.bind(process.parentPort);
  72. process.parentPort.postMessage = (...args) => {
  73. const [frame] = args;
  74. if (
  75. frame?.source === "host" &&
  76. typeof frame.message === "string" &&
  77. frame.message.includes("Subagent 配置文件更新完成")
  78. ) {
  79. // 与 Host 日志的 JSON 转义保持一致,否则 Windows 路径会漏记已完成的更新。
  80. const paths = (control().tracePaths ?? []).filter((path) =>
  81. frame.message.includes(JSON.stringify(path).slice(1, -1)),
  82. );
  83. if (paths.length)
  84. log({ kind: "snapshot-published", paths, readCounts: Object.fromEntries(readCounts) });
  85. }
  86. return post(...args);
  87. };
  88. log({ kind: "ready" });
  89. // UtilityProcess 不执行 Node --require 参数:测试启动器加载边界后进入原 Host 模块。
  90. void import(require("node:url").pathToFileURL(process.argv[2]).href);