offpeak-host-faults.cjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // 仅由 E2E Host 启动器加载:在真实 CLI stdio 边界模拟已安装新票但确认失败。
  2. const { appendFile } = require("node:fs/promises");
  3. const { join } = require("node:path");
  4. const { syncBuiltinESMExports } = require("node:module");
  5. const { StringDecoder } = require("node:string_decoder");
  6. const childProcess = require("node:child_process");
  7. const logPath = join(process.env.ZCODE_E2E_HOST_FAULT_ROOT, `host-boundary-${process.pid}.jsonl`);
  8. let logWrites = Promise.resolve();
  9. const log = (kind, id) => {
  10. logWrites = logWrites.then(() => appendFile(logPath, JSON.stringify({ kind, id }) + "\n"));
  11. };
  12. const spawn = childProcess.spawn;
  13. childProcess.spawn = function (...args) {
  14. const child = spawn.apply(this, args);
  15. if (!child.stdin || !child.stdout) return child;
  16. const controls = new Map();
  17. const completed = [];
  18. let failed = false;
  19. let confirmed = false;
  20. let executionId;
  21. const write = child.stdin.write;
  22. child.stdin.write = function (chunk, ...rest) {
  23. for (const line of String(chunk).split("\n")) {
  24. let frame;
  25. try {
  26. frame = JSON.parse(line);
  27. } catch {
  28. continue;
  29. }
  30. if (
  31. frame.method !== "v4/conversation/offPeakControl" ||
  32. frame.params?.action !== "replace_ticket"
  33. )
  34. continue;
  35. executionId = frame.params.executionId;
  36. const id = `${executionId}/${frame.params.renewalId}/${frame.params.ticketId}`;
  37. controls.set(frame.id, id);
  38. log("ticket-control", id);
  39. }
  40. return write.call(this, chunk, ...rest);
  41. };
  42. const emit = child.stdout.emit;
  43. const decoder = new StringDecoder("utf8");
  44. let buffered = "";
  45. child.stdout.emit = function (event, chunk, ...rest) {
  46. if (event !== "data") return emit.call(this, event, chunk, ...rest);
  47. buffered += decoder.write(chunk);
  48. const lines = buffered.split("\n");
  49. buffered = lines.pop();
  50. for (const line of lines) {
  51. let frame;
  52. try {
  53. frame = JSON.parse(line);
  54. } catch {
  55. emit.call(this, "data", Buffer.from(line + "\n"));
  56. continue;
  57. }
  58. // 保留完成通知直到交接得到确认,确保真实同步周期有机会修复未知回执。
  59. if (
  60. !confirmed &&
  61. executionId &&
  62. frame.method === "v4/offPeak/lifecycle" &&
  63. frame.params?.executionId === executionId &&
  64. frame.params.type === "completed"
  65. ) {
  66. completed.push(line);
  67. continue;
  68. }
  69. const id = controls.get(frame.id);
  70. if (id && frame.result?.accepted) {
  71. if (!failed) {
  72. failed = true;
  73. log("ticket-ack-failed", id);
  74. emit.call(
  75. this,
  76. "data",
  77. Buffer.from(
  78. JSON.stringify({
  79. id: frame.id,
  80. error: { code: -32000, message: "E2E ticket ACK unavailable after installation" },
  81. }) + "\n",
  82. ),
  83. );
  84. continue;
  85. }
  86. confirmed = true;
  87. log("ticket-ack-confirmed", id);
  88. }
  89. emit.call(this, "data", Buffer.from(line + "\n"));
  90. if (confirmed)
  91. for (const held of completed.splice(0)) emit.call(this, "data", Buffer.from(held + "\n"));
  92. }
  93. return true;
  94. };
  95. return child;
  96. };
  97. syncBuiltinESMExports();
  98. log("ready");
  99. void import(require("node:url").pathToFileURL(process.argv[2]).href);