@mininglamp-oss/octo-runtime 1.1.8 → 1.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/README.md +1 -1
  2. package/apps/cli/README.md +3 -1
  3. package/{node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/runtime-configuration.d.ts → apps/cli/dist/adapter-configuration.d.ts} +1 -0
  4. package/{node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/runtime-configuration.js → apps/cli/dist/adapter-configuration.js} +1 -0
  5. package/apps/cli/dist/adapter-configurations.d.ts +1 -1
  6. package/apps/cli/dist/adapter-configurations.js +1 -1
  7. package/apps/cli/dist/adapters.d.ts +7 -0
  8. package/apps/cli/dist/adapters.js +51 -0
  9. package/apps/cli/dist/build-info.json +2 -2
  10. package/apps/cli/dist/command-tree.js +3 -1
  11. package/apps/cli/dist/help.js +1 -1
  12. package/apps/cli/dist/index.js +4 -0
  13. package/apps/cli/dist/main.d.ts +2 -0
  14. package/apps/cli/dist/main.js +10 -12
  15. package/apps/cli/dist/manifest.json +0 -5
  16. package/apps/cli/dist/output.js +7 -9
  17. package/apps/cli/dist/playboard-interrupt.d.ts +6 -0
  18. package/apps/cli/dist/playboard-interrupt.js +86 -0
  19. package/apps/cli/dist/playboard.d.ts +2 -0
  20. package/apps/cli/dist/playboard.js +20 -12
  21. package/dist/public/executor-kernel.d.ts +2 -3
  22. package/dist/public/executor-kernel.js +1 -2
  23. package/dist/public/protocol.d.ts +1 -1
  24. package/dist/public/protocol.js +1 -1
  25. package/node_modules/@mininglamp-oss/octo-runtime-acp-client/package.json +3 -3
  26. package/node_modules/@mininglamp-oss/octo-runtime-adapter-acp/package.json +4 -4
  27. package/node_modules/@mininglamp-oss/octo-runtime-adapter-antigravity/package.json +3 -3
  28. package/node_modules/@mininglamp-oss/octo-runtime-adapter-claude-code/package.json +3 -3
  29. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codebuddy/package.json +4 -4
  30. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/app-server.d.ts +1 -0
  31. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/app-server.js +200 -106
  32. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/async-questions.d.ts +27 -0
  33. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/async-questions.js +205 -0
  34. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/stdin-writer.d.ts +30 -0
  35. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/stdin-writer.js +223 -0
  36. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/package.json +3 -3
  37. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/dist/{chunk-V5W6ZTYC.js → chunk-FGFEM7JP.js} +1 -1
  38. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/dist/{chunk-CFKMMCO6.js → chunk-R3PDZLX5.js} +3 -3
  39. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/dist/{chunk-LO34KB7D.js → chunk-TSRZVLIG.js} +1 -1
  40. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/dist/dsh-sdk-NOTICE.txt +2 -2
  41. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/dist/errors.js +1 -1
  42. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/dist/index.js +5 -5
  43. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/dist/launch.js +2 -2
  44. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/dist/trajectory.js +2 -2
  45. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/package.json +3 -3
  46. package/node_modules/@mininglamp-oss/octo-runtime-adapter-grok-build/package.json +5 -5
  47. package/node_modules/@mininglamp-oss/octo-runtime-adapter-hermes/package.json +2 -2
  48. package/node_modules/@mininglamp-oss/octo-runtime-adapter-kimi-code/package.json +2 -2
  49. package/node_modules/@mininglamp-oss/octo-runtime-adapter-mcode/package.json +2 -2
  50. package/node_modules/@mininglamp-oss/octo-runtime-adapter-openclaw/dist/gateway.d.ts +3 -0
  51. package/node_modules/@mininglamp-oss/octo-runtime-adapter-openclaw/dist/gateway.js +57 -13
  52. package/node_modules/@mininglamp-oss/octo-runtime-adapter-openclaw/dist/index.d.ts +3 -2
  53. package/node_modules/@mininglamp-oss/octo-runtime-adapter-openclaw/dist/index.js +4 -2
  54. package/node_modules/@mininglamp-oss/octo-runtime-adapter-openclaw/package.json +3 -3
  55. package/node_modules/@mininglamp-oss/octo-runtime-adapter-opencode/package.json +4 -4
  56. package/node_modules/@mininglamp-oss/octo-runtime-adapter-qodercli/package.json +2 -2
  57. package/node_modules/@mininglamp-oss/octo-runtime-adapter-qoderclicn/package.json +2 -2
  58. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/approval.js +2 -0
  59. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/reasoning.js +1 -2
  60. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/service-status.d.ts +1 -1
  61. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/service-status.js +12 -30
  62. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/package.json +2 -2
  63. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/adapter-configuration.d.ts +3 -0
  64. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/adapter-configuration.js +125 -0
  65. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/adapter-configurations.d.ts +1 -1
  66. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/adapter-configurations.js +1 -1
  67. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/adapters.d.ts +7 -0
  68. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/adapters.js +51 -0
  69. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/build-info.json +2 -2
  70. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/command-tree.js +3 -1
  71. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/help.js +1 -1
  72. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/index.js +4 -0
  73. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/main.d.ts +2 -0
  74. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/main.js +10 -12
  75. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/manifest.json +0 -5
  76. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/output.js +7 -9
  77. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/playboard-interrupt.d.ts +6 -0
  78. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/playboard-interrupt.js +86 -0
  79. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/playboard.d.ts +2 -0
  80. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/playboard.js +20 -12
  81. package/node_modules/@mininglamp-oss/octo-runtime-cli/package.json +23 -24
  82. package/node_modules/@mininglamp-oss/octo-runtime-cross-service/dist/launchd.d.ts +2 -2
  83. package/node_modules/@mininglamp-oss/octo-runtime-cross-service/dist/launchd.js +12 -11
  84. package/node_modules/@mininglamp-oss/octo-runtime-cross-service/package.json +1 -1
  85. package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/experience.js +2 -1
  86. package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/index.d.ts +8 -7
  87. package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/index.js +33 -11
  88. package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/invoke-input.js +9 -2
  89. package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/watchdog.js +6 -3
  90. package/node_modules/@mininglamp-oss/octo-runtime-executor-core/package.json +3 -3
  91. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/async-question-reply.d.ts +30 -0
  92. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/async-question-reply.js +199 -0
  93. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/client.d.ts +2 -0
  94. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/client.js +2 -0
  95. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/execution-source.d.ts +17 -0
  96. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/execution-source.js +21 -0
  97. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/host-run-service.d.ts +9 -9
  98. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/host-run-service.js +3 -3
  99. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/index.d.ts +2 -6
  100. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/index.js +3 -6
  101. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/invocation-manifest.d.ts +14 -13
  102. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/invocation-manifest.js +84 -187
  103. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/server.d.ts +5 -8
  104. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/server.js +77 -53
  105. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/service.d.ts +5 -1
  106. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/service.js +15 -3
  107. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/package.json +4 -12
  108. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/application/im-channel.js +6 -2
  109. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/contracts.d.ts +2 -0
  110. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/execution/task.js +14 -0
  111. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/transport/channel.js +4 -0
  112. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/transport/interaction-card.js +2 -1
  113. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/transport/session-command.d.ts +9 -0
  114. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/transport/session-command.js +71 -0
  115. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/package.json +6 -6
  116. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/dist/application/im-channel.js +14 -0
  117. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/package.json +5 -5
  118. package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/dist/web/assets/{index-B127MhT-.js → index-BLMf2Etg.js} +2 -4
  119. package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/dist/web/index.html +1 -1
  120. package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/package.json +2 -2
  121. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/context-loader.js +1 -1
  122. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/package.json +4 -4
  123. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/browser.d.ts +1 -0
  124. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/browser.js +1 -0
  125. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/context.d.ts +2 -0
  126. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/context.js +4 -1
  127. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/failure.d.ts +3 -0
  128. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/failure.js +63 -12
  129. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/generated/protocol-types.d.ts +110 -20
  130. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/index.d.ts +7 -4
  131. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/index.js +28 -6
  132. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/invoke-input.js +2 -0
  133. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/questions.d.ts +9 -0
  134. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/questions.js +84 -0
  135. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/run-protocol.d.ts +1 -1
  136. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/run-protocol.js +11 -5
  137. package/node_modules/@mininglamp-oss/octo-runtime-protocol/package.json +1 -1
  138. package/package.json +26 -28
  139. package/packages/octo-runtime-protocol/schema/protocol.schema.json +157 -41
  140. package/apps/cli/dist/worker.d.ts +0 -1
  141. package/apps/cli/dist/worker.js +0 -18
  142. package/node_modules/@mininglamp-oss/octo-runtime-adapter-zcode-gui/dist/app-server.d.ts +0 -63
  143. package/node_modules/@mininglamp-oss/octo-runtime-adapter-zcode-gui/dist/app-server.js +0 -846
  144. package/node_modules/@mininglamp-oss/octo-runtime-adapter-zcode-gui/dist/index.d.ts +0 -54
  145. package/node_modules/@mininglamp-oss/octo-runtime-adapter-zcode-gui/dist/index.js +0 -154
  146. package/node_modules/@mininglamp-oss/octo-runtime-adapter-zcode-gui/package.json +0 -15
  147. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/worker.d.ts +0 -1
  148. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/worker.js +0 -18
  149. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/heartbeat.d.ts +0 -16
  150. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/heartbeat.js +0 -59
  151. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/manifest-adapters.d.ts +0 -6
  152. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/manifest-adapters.js +0 -88
  153. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/module-specifier.d.ts +0 -1
  154. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/module-specifier.js +0 -2
  155. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/process.d.ts +0 -42
  156. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/process.js +0 -292
  157. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/rpc.d.ts +0 -62
  158. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/rpc.js +0 -661
  159. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/runner.d.ts +0 -2
  160. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/runner.js +0 -5
  161. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/serve.d.ts +0 -10
  162. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/serve.js +0 -96
  163. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/supervisor.d.ts +0 -15
  164. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/supervisor.js +0 -279
@@ -1,96 +0,0 @@
1
- import { AgentExecutor } from "@mininglamp-oss/octo-runtime-executor-core";
2
- import { WorkerRpcServer } from "./rpc.js";
3
- import { RuntimeLogging } from "@mininglamp-oss/octo-runtime-adapter-sdk";
4
- /**
5
- * 在当前独立 worker 子进程中提供 Core 执行服务。调用方已经决定加载集合:
6
- * CLI 可从自己的 JSON 动态加载,SDK 可直接 import;这里不扫描或补齐任何 Adapter。
7
- * 该函数只在 worker 入口调用;宿主 App 和 Kernel 监督进程通过客户端接入,不直接调用。
8
- */
9
- export function serveWorker(adapters, options = {}) {
10
- const logging = new RuntimeLogging(options.logging ??
11
- (process.env.OCTO_RUNTIME_LOGS_DIRECTORY
12
- ? { directory: process.env.OCTO_RUNTIME_LOGS_DIRECTORY }
13
- : undefined));
14
- process.on("uncaughtExceptionMonitor", () => logging.kernel("error", "host.uncaught", { code: "HOST_UNCAUGHT_EXCEPTION" }));
15
- const host = new AgentExecutor({ log: logging.kernel });
16
- for (const adapter of adapters) {
17
- // 注册阶段保留协议校验和重复类型检查,失败时不建立就绪的 RPC 服务。
18
- host.register(adapter);
19
- logging.kernel("info", "adapter.registered", { adapter_type: adapter.metadata.type });
20
- }
21
- // 仅服务已注册集合;Kernel 监督器通过首次 list 握手确认 worker 就绪。
22
- // 注册不代表 Provider 已安装或认证,实际可用性由 probe 和调用结果说明。
23
- new WorkerRpcServer({
24
- host,
25
- output: process.stdout,
26
- log: logging.kernel
27
- }).listen(process.stdin);
28
- logging.kernel("info", "host.ready");
29
- // 父 Kernel 断开或停止时等待 Core 取消和清理;需要替换时由 Kernel 监督器重启相同入口。
30
- let stopping = false;
31
- let drainedExitCode;
32
- const stop = (holdForParent = false) => {
33
- if (stopping)
34
- return;
35
- stopping = true;
36
- // Let the launcher reserve the full bounded drain only for a worker that handles EOF.
37
- if (holdForParent && process.connected)
38
- process.send?.({ type: "worker.stopping" }, () => { });
39
- const finish = async (code) => {
40
- await logging.close();
41
- drainedExitCode = code;
42
- if (holdForParent && process.connected) {
43
- // Windows 的树清理需要 leader 仍存活。通知只表示排空完成,不代表后代已回收。
44
- // IPC 保持引用直到 launcher 执行 taskkill;owner 断连仍走下方既有退出收尾。
45
- process.channel?.ref();
46
- process.send?.({ type: "worker.drained", cleanup: code === 0 ? "complete" : "incomplete" }, (error) => {
47
- if (error)
48
- process.exit(code || 1);
49
- });
50
- }
51
- else
52
- process.exit(code);
53
- };
54
- void host.shutdown().then(async (result) => {
55
- if (result.cleanup === "incomplete") {
56
- logging.kernel("error", "host.cleanup_incomplete", {
57
- code: "EXECUTOR_CLEANUP_INCOMPLETE",
58
- unresolved_count: result.unresolvedInvocationIds.length
59
- });
60
- }
61
- await finish(result.cleanup === "complete" ? 0 : 1);
62
- }, async () => {
63
- await finish(1);
64
- });
65
- };
66
- const onSignal = () => {
67
- // Windows 控制台事件也会送达 worker;父 Kernel 须先经 RPC 取消并消费终态,再以 EOF 关闭。
68
- // 抢先退出会丢失终态及树回收所需的 leader。父 IPC 已断开或独立运行时仍自行收尾。
69
- if (process.platform !== "win32" || !process.connected)
70
- stop();
71
- };
72
- // 保持监听到父进程关闭,避免重复控制台事件恢复默认的立即退出行为。
73
- process.on("SIGINT", onSignal);
74
- process.on("SIGTERM", onSignal);
75
- process.once("SIGHUP", () => stop());
76
- process.stdin.once("end", () => stop(process.platform === "win32"));
77
- process.stdin.once("close", () => stop(process.platform === "win32"));
78
- // 心跳必须由执行进程主事件循环处理;独立线程应答会掩盖执行线程阻塞。
79
- process.on("message", (message) => {
80
- if (message &&
81
- typeof message === "object" &&
82
- "type" in message &&
83
- message.type === "kernel.ping" &&
84
- "probe" in message &&
85
- Number.isSafeInteger(message.probe) &&
86
- process.connected) {
87
- process.send?.({ type: "worker.pong", probe: message.probe });
88
- }
89
- });
90
- process.once("disconnect", () => {
91
- if (drainedExitCode !== undefined)
92
- process.exit(drainedExitCode);
93
- else
94
- stop();
95
- });
96
- }
@@ -1,15 +0,0 @@
1
- import { type SpawnWorkerOptions } from "./process.js";
2
- import { type RuntimeLog } from "@mininglamp-oss/octo-runtime-adapter-sdk";
3
- import type { KernelStatus } from "./types.js";
4
- /** 监督所属 worker;清理阻塞持续重试,只恢复新请求,不重新执行已丢失的 Run。 */
5
- export declare class WorkerSupervisor {
6
- #private;
7
- private readonly options;
8
- private readonly recordWorker?;
9
- private readonly log;
10
- constructor(options: SpawnWorkerOptions, recordWorker?: ((pid: number | undefined) => Promise<void>) | undefined, log?: RuntimeLog);
11
- get status(): KernelStatus["worker"];
12
- get client(): import("./rpc.js").WorkerRpcClient;
13
- start(signal?: AbortSignal): Promise<void>;
14
- close(until?: number): Promise<void>;
15
- }
@@ -1,279 +0,0 @@
1
- import { closeWorker, spawnWorker, WorkerCleanupError } from "./process.js";
2
- import { noopRuntimeLog } from "@mininglamp-oss/octo-runtime-adapter-sdk";
3
- import { KernelError } from "./types.js";
4
- import { WorkerHeartbeat } from "./heartbeat.js";
5
- /** 监督所属 worker;清理阻塞持续重试,只恢复新请求,不重新执行已丢失的 Run。 */
6
- export class WorkerSupervisor {
7
- options;
8
- recordWorker;
9
- log;
10
- #worker;
11
- #generation = 0;
12
- #attempt = 0;
13
- #state = "starting";
14
- #closing = false;
15
- #recovery;
16
- #timer;
17
- #heartbeat;
18
- #activeTime = 0;
19
- #attempts = [];
20
- #slowRecovery = false;
21
- #abort = new AbortController();
22
- #cleanupFailure;
23
- #closeUntil;
24
- #diagnostic;
25
- constructor(options, recordWorker, log = noopRuntimeLog) {
26
- this.options = options;
27
- this.recordWorker = recordWorker;
28
- this.log = log;
29
- }
30
- get status() {
31
- return {
32
- pid: this.#worker?.child.pid,
33
- generation: this.#generation,
34
- state: this.#state,
35
- restart_attempt: this.#attempt,
36
- ...(this.#diagnostic ? { recovery: { ...this.#diagnostic } } : {})
37
- };
38
- }
39
- get client() {
40
- if (this.#state !== "ready" || !this.#worker)
41
- throw new KernelError("KERNEL_UNAVAILABLE");
42
- return this.#worker.client;
43
- }
44
- async start(signal) {
45
- signal?.throwIfAborted();
46
- const abort = () => this.#abort.abort();
47
- signal?.addEventListener("abort", abort, { once: true });
48
- try {
49
- await this.#spawn();
50
- signal?.throwIfAborted();
51
- }
52
- finally {
53
- signal?.removeEventListener("abort", abort);
54
- }
55
- }
56
- #emit(event, data = {}) {
57
- // 宿主日志异常不得终止监督循环,错误正文和 worker stderr 不进入状态日志。
58
- try {
59
- this.log(event === "worker.ready" ? "info" : "warn", event, {
60
- pid: this.#worker?.child.pid,
61
- generation: this.#generation,
62
- attempt: this.#attempt,
63
- ...data
64
- });
65
- }
66
- catch {
67
- /* 日志不是恢复前置条件。 */
68
- }
69
- }
70
- async #spawn() {
71
- let worker;
72
- try {
73
- worker = await spawnWorker({
74
- ...this.options,
75
- signal: this.#abort.signal,
76
- onSpawn: async (child) => {
77
- await this.recordWorker?.(child.pid);
78
- }
79
- });
80
- }
81
- catch (error) {
82
- if (error instanceof WorkerCleanupError)
83
- this.#cleanupFailure = error;
84
- throw error;
85
- }
86
- if (this.#closing) {
87
- try {
88
- await closeWorker(worker, this.#closeUntil ?? Date.now() + 5000, true);
89
- }
90
- catch (error) {
91
- throw (this.#cleanupFailure = new AggregateError([error], "WORKER_CLEANUP_INCOMPLETE"));
92
- }
93
- return;
94
- }
95
- this.#worker = worker;
96
- this.#generation++;
97
- this.#state = "ready";
98
- this.#diagnostic = undefined;
99
- this.#emit("worker.ready");
100
- const heartbeat = new WorkerHeartbeat();
101
- this.#heartbeat = heartbeat;
102
- let elapsed = 0;
103
- worker.child.on("message", (message) => {
104
- if (this.#worker === worker &&
105
- message &&
106
- typeof message === "object" &&
107
- "type" in message &&
108
- message.type === "worker.pong")
109
- heartbeat.pong("probe" in message ? message.probe : undefined);
110
- });
111
- worker.child.once("exit", () => this.#failed(worker, "exit"));
112
- worker.child.once("error", () => this.#failed(worker, "error"));
113
- this.#timer = setInterval(() => {
114
- if (this.#closing || this.#worker !== worker || this.#state !== "ready")
115
- return;
116
- const probe = heartbeat.tick();
117
- this.#activeTime += heartbeat.activeMs - elapsed;
118
- elapsed = heartbeat.activeMs;
119
- if (probe.type === "expired") {
120
- this.#failed(worker, "heartbeat");
121
- return;
122
- }
123
- if (probe.type === "ping" && worker.child.connected)
124
- worker.child.send({ type: "kernel.ping", probe: probe.id }, (error) => {
125
- if (error)
126
- this.#failed(worker, "ipc");
127
- });
128
- }, 1000);
129
- this.#timer.unref();
130
- if (worker.child.exitCode !== null || worker.child.signalCode !== null)
131
- this.#failed(worker, "exit");
132
- }
133
- #failed(worker, trigger) {
134
- if (this.#closing || this.#worker !== worker || this.#state !== "ready")
135
- return;
136
- clearInterval(this.#timer);
137
- this.#state = "recovering";
138
- this.#diagnostic = {
139
- trigger,
140
- stage: "cleanup",
141
- code: "WORKER_LOST",
142
- occurred_at: new Date().toISOString(),
143
- ...(worker.child.exitCode !== null ? { exit_code: worker.child.exitCode } : {}),
144
- ...(worker.child.signalCode !== null ? { signal: worker.child.signalCode } : {})
145
- };
146
- this.#emit("worker.lost", this.#diagnostic);
147
- worker.client.close(new KernelError("WORKER_LOST"));
148
- // 连续健康运行才重置快速恢复预算;睡眠或仅仅等待不能洗掉连续故障。
149
- if ((this.#heartbeat?.healthyMs ?? 0) >= 60_000) {
150
- this.#attempts = [];
151
- this.#attempt = 0;
152
- this.#slowRecovery = false;
153
- }
154
- this.#recovery = this.#recover(worker).catch((error) => {
155
- this.#cleanupFailure = new AggregateError([error], "WORKER_RECOVERY_BLOCKED");
156
- if (!this.#closing)
157
- this.#state = "exhausted";
158
- this.#emit("worker.recovery_blocked", { code: failureCode(error) });
159
- });
160
- }
161
- async #wait(delay) {
162
- if (this.#abort.signal.aborted)
163
- return;
164
- await new Promise((resolve) => {
165
- const finish = () => {
166
- clearTimeout(timer);
167
- this.#abort.signal.removeEventListener("abort", finish);
168
- resolve();
169
- };
170
- const timer = setTimeout(finish, delay);
171
- this.#abort.signal.addEventListener("abort", finish, { once: true });
172
- });
173
- }
174
- /** 阻塞时单路按 1/2/5 秒重新确认;失败不洗掉记录,关闭会取消等待并保留未清理事实。 */
175
- async #retry(stage, operation) {
176
- let retry = operation;
177
- let attempt = 0;
178
- while (!this.#closing) {
179
- try {
180
- await retry();
181
- this.#cleanupFailure = undefined;
182
- this.#state = "recovering";
183
- return true;
184
- }
185
- catch (error) {
186
- if (error instanceof WorkerCleanupError)
187
- retry = () => error.retryCleanup(Date.now() + 4800);
188
- this.#cleanupFailure =
189
- error instanceof AggregateError
190
- ? error
191
- : new AggregateError([error], "WORKER_RECOVERY_BLOCKED");
192
- if (this.#closing)
193
- return false;
194
- const delay = [1000, 2000, 5000][Math.min(attempt++, 2)];
195
- this.#state = "exhausted";
196
- this.#diagnostic = {
197
- ...this.#diagnostic,
198
- stage,
199
- code: failureCode(error),
200
- retry_at: new Date(Date.now() + delay).toISOString()
201
- };
202
- this.#emit("worker.recovery_blocked", { ...this.#diagnostic, delay_ms: delay });
203
- await this.#wait(delay);
204
- }
205
- }
206
- return false;
207
- }
208
- async #recover(worker) {
209
- // 只在真实回收和清单写入都成功后放行替代;任何旧 Run 都不会重新 invoke。
210
- // Windows 在首次清理前已退出时沿用 skipped 路径;仍存活的故障 worker 不等待正常排空。
211
- // 已发生的清理失败继续由原 close Promise 和重试入口保留,不能因稍后退出而放行。
212
- if (!(await this.#retry("cleanup", () => closeWorker(worker, Date.now() + 4800, worker.child.exitCode === null && worker.child.signalCode === null))))
213
- return;
214
- if (!(await this.#retry("manifest", () => this.recordWorker?.(undefined) ?? Promise.resolve())))
215
- return;
216
- this.#worker = undefined;
217
- while (!this.#closing) {
218
- this.#attempts = this.#attempts.filter((at) => this.#activeTime - at < 60_000);
219
- if (this.#attempts.length >= 3)
220
- this.#slowRecovery = true;
221
- const delay = this.#slowRecovery ? 15_000 : [250, 1000, 4000][this.#attempts.length];
222
- this.#diagnostic = {
223
- ...this.#diagnostic,
224
- stage: "backoff",
225
- retry_at: new Date(Date.now() + delay).toISOString()
226
- };
227
- this.#emit("worker.retry_scheduled", { ...this.#diagnostic, delay_ms: delay });
228
- await this.#wait(delay);
229
- if (this.#closing)
230
- return;
231
- if (!this.#slowRecovery)
232
- this.#attempts.push(this.#activeTime);
233
- this.#attempt = Math.min(Number.MAX_SAFE_INTEGER, this.#attempt + 1);
234
- this.#diagnostic = { ...this.#diagnostic, stage: "starting" };
235
- delete this.#diagnostic.retry_at;
236
- try {
237
- await this.#spawn();
238
- return;
239
- }
240
- catch (error) {
241
- this.#diagnostic = { ...this.#diagnostic, code: failureCode(error) };
242
- this.#emit("worker.start_failed", {
243
- ...this.#diagnostic,
244
- ...(error instanceof WorkerCleanupError ? { pid: error.pid } : {})
245
- });
246
- if (error instanceof WorkerCleanupError) {
247
- if (!(await this.#retry("cleanup", () => Promise.reject(error))))
248
- return;
249
- if (!(await this.#retry("manifest", () => this.recordWorker?.(undefined) ?? Promise.resolve())))
250
- return;
251
- }
252
- }
253
- }
254
- }
255
- async close(until = Date.now() + 5000) {
256
- this.#closing = true;
257
- this.#closeUntil ??= until;
258
- this.#abort.abort();
259
- clearInterval(this.#timer);
260
- await this.#recovery;
261
- if (this.#worker)
262
- await closeWorker(this.#worker, this.#closeUntil);
263
- if (this.#cleanupFailure)
264
- throw this.#cleanupFailure;
265
- await this.recordWorker?.(undefined);
266
- this.#state = "stopped";
267
- }
268
- }
269
- function failureCode(error) {
270
- // 聚合回收错误取其真实 cause;只输出稳定错误码,禁止输出异常正文或 stderr。
271
- for (let depth = 0; depth < 5 && error instanceof Error; depth++, error = error.cause) {
272
- const code = "code" in error ? error.code : undefined;
273
- if (typeof code === "string" && /^[A-Z][A-Z0-9_]{0,95}$/.test(code))
274
- return code;
275
- if (!error.cause && /^[A-Z][A-Z0-9_]{0,95}$/.test(error.message))
276
- return error.message;
277
- }
278
- return "WORKER_RECOVERY_FAILED";
279
- }