@zhushanwen/subagent-core 0.6.0 → 0.7.0

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 (278) hide show
  1. package/README.md +1 -1
  2. package/dist/chunk-KXVBIVOU.js +16 -0
  3. package/dist/chunk-T2SYBW3I.js +24 -0
  4. package/dist/chunk-VURUAGMM.js +2305 -0
  5. package/dist/{types-C3fE3R9x.d.cts → engine-discovery-scan-ocpM8FmI.d.cts} +770 -668
  6. package/dist/{types-C3fE3R9x.d.ts → engine-discovery-scan-ocpM8FmI.d.ts} +770 -668
  7. package/dist/execution/engine/engine-discovery-scan.cjs +2210 -0
  8. package/dist/execution/engine/engine-discovery-scan.d.cts +4 -0
  9. package/dist/execution/engine/engine-discovery-scan.d.ts +4 -0
  10. package/dist/execution/engine/engine-discovery-scan.js +20 -0
  11. package/dist/execution/engine/paths.cjs +6 -23
  12. package/dist/execution/engine/paths.d.cts +1 -8
  13. package/dist/execution/engine/paths.d.ts +1 -8
  14. package/dist/execution/engine/paths.js +1 -1
  15. package/dist/execution/relay-env.cjs +12 -28
  16. package/dist/execution/relay-env.d.cts +1 -33
  17. package/dist/execution/relay-env.d.ts +1 -33
  18. package/dist/execution/relay-env.js +5 -3
  19. package/dist/index.cjs +14045 -16059
  20. package/dist/index.d.cts +1035 -1579
  21. package/dist/index.d.ts +1035 -1579
  22. package/dist/index.js +9838 -13800
  23. package/dist.bundle/index.cjs +28875 -30323
  24. package/package.json +20 -26
  25. package/src/__tests__/record-store-last-line.test.ts +16 -7
  26. package/src/core/error-message.ts +7 -6
  27. package/src/core/host-services.ts +8 -2
  28. package/src/core/notify-ports.ts +17 -2
  29. package/src/execution/__tests__/chat-engine-routing.test.ts +62 -28
  30. package/src/execution/__tests__/chat-round-first-round-watchdog.test.ts +255 -0
  31. package/src/execution/__tests__/collect-coordinator-service.test.ts +36 -47
  32. package/src/execution/__tests__/collect-mixed-dispatch.test.ts +20 -53
  33. package/src/execution/__tests__/delivery-methods.test.ts +179 -237
  34. package/src/execution/__tests__/execute-and-await-worktree.test.ts +0 -2
  35. package/src/execution/__tests__/execute-nesting.test.ts +20 -13
  36. package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +20 -33
  37. package/src/execution/__tests__/gc-timer.test.ts +7 -22
  38. package/src/execution/__tests__/get-record-for-action-restart.test.ts +20 -10
  39. package/src/execution/__tests__/helpers/fake-engine-port.ts +187 -0
  40. package/src/execution/__tests__/helpers/spawn-mock.ts +36 -9
  41. package/src/execution/__tests__/helpers/subagent-service-mocks.ts +1 -1
  42. package/src/execution/__tests__/idle-gc.test.ts +200 -0
  43. package/src/execution/__tests__/lifecycle-predicates.test.ts +26 -21
  44. package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +5 -2
  45. package/src/execution/__tests__/reconcile-sweep.test.ts +200 -0
  46. package/src/execution/__tests__/record-store.test.ts +60 -6
  47. package/src/execution/__tests__/recursive-visibility-baseline.test.ts +20 -13
  48. package/src/execution/__tests__/round-supervisor.test.ts +293 -0
  49. package/src/execution/__tests__/session-pending.test.ts +119 -17
  50. package/src/execution/__tests__/stream-sink-retirement.test.ts +10 -10
  51. package/src/execution/__tests__/subagent-service-multiproc-guard.test.ts +1 -11
  52. package/src/execution/__tests__/subagent-service-notify-gate.test.ts +33 -30
  53. package/src/execution/__tests__/subagent-service-parent-guard.test.ts +2 -9
  54. package/src/execution/__tests__/subagent-service-recovery-bounds.test.ts +59 -129
  55. package/src/execution/__tests__/subagent-service.test.ts +2 -2
  56. package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +109 -7
  57. package/src/execution/__tests__/subprocess-agent-runner.test.ts +108 -36
  58. package/src/execution/__tests__/sync-collect-recovery.test.ts +21 -36
  59. package/src/execution/__tests__/workflow-state-root.test.ts +232 -0
  60. package/src/execution/agent-registry.ts +6 -2
  61. package/src/execution/dialog-queue.ts +6 -73
  62. package/src/execution/engine/__tests__/common/capability-gate.test.ts +115 -15
  63. package/src/execution/engine/__tests__/common/errors.test.ts +11 -18
  64. package/src/execution/engine/__tests__/common/kill-chain.test.ts +7 -0
  65. package/src/execution/engine/__tests__/common/nesting-guard.test.ts +9 -3
  66. package/src/execution/engine/__tests__/common/session-view-service.test.ts +90 -3
  67. package/src/execution/engine/__tests__/conformance/H9-test-disposition.md +116 -0
  68. package/src/execution/engine/__tests__/conformance/__fixtures__/engine-protocol/fake-engine-protocol.mjs +377 -0
  69. package/src/execution/engine/__tests__/conformance/__fixtures__/engine-protocol/recorded.fixture.json +331 -0
  70. package/src/execution/engine/__tests__/conformance/__fixtures__/engine-protocol/smoke-run.fixture.json +213 -0
  71. package/src/execution/engine/__tests__/conformance/chat-round-protocol.test.ts +230 -0
  72. package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +69 -42
  73. package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +4 -81
  74. package/src/execution/engine/__tests__/conformance/contract.probe.test.ts +67 -44
  75. package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +81 -54
  76. package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +133 -70
  77. package/src/execution/engine/__tests__/conformance/engine-crash.test.ts +143 -0
  78. package/src/execution/engine/__tests__/conformance/fake-engine-capabilities.ts +19 -0
  79. package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +7 -2
  80. package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +6 -5
  81. package/src/execution/engine/__tests__/conformance/protocol-blackbox.test.ts +398 -0
  82. package/src/execution/engine/__tests__/conformance/registry-fork-filter.test.ts +221 -0
  83. package/src/execution/engine/__tests__/conformance/round-liveness-supervisor.test.ts +302 -0
  84. package/src/execution/engine/__tests__/conformance/static-spawn-guard.test.ts +58 -0
  85. package/src/execution/engine/__tests__/engine-discovery-scan.test.ts +558 -0
  86. package/src/execution/engine/__tests__/host-task-spec.test.ts +45 -0
  87. package/src/execution/engine/__tests__/model-ref-split.test.ts +74 -0
  88. package/src/execution/engine/__tests__/registry.test.ts +206 -1
  89. package/src/execution/engine/__tests__/routing.test.ts +97 -0
  90. package/src/execution/engine/__tests__/run-failure-worktree-cleanup.test.ts +97 -0
  91. package/src/execution/engine/client/__tests__/__fixtures__/fake-engine.mjs +294 -0
  92. package/src/execution/engine/client/__tests__/engine-client.test.ts +516 -0
  93. package/src/execution/engine/client/__tests__/mirror.test.ts +67 -0
  94. package/src/execution/engine/client/__tests__/pid-file.test.ts +257 -0
  95. package/src/execution/engine/client/__tests__/protocol-closure.test.ts +95 -0
  96. package/src/execution/engine/client/__tests__/remote-engine.test.ts +457 -0
  97. package/src/execution/engine/client/client-options.ts +103 -0
  98. package/src/execution/engine/client/engine-client.ts +687 -0
  99. package/src/execution/engine/client/mirror.ts +167 -0
  100. package/src/execution/engine/client/pid-file.ts +392 -0
  101. package/src/execution/engine/client/reaper.ts +95 -0
  102. package/src/execution/engine/client/remote-engine.ts +441 -0
  103. package/src/execution/engine/client/reverse-router.ts +255 -0
  104. package/src/execution/engine/common/capability-gate.ts +109 -22
  105. package/src/execution/engine/common/errors.ts +9 -25
  106. package/src/execution/engine/common/event-journal.ts +4 -50
  107. package/src/execution/engine/common/journal-replay.ts +3 -1
  108. package/src/execution/engine/common/kill-chain.ts +18 -251
  109. package/src/execution/engine/common/nesting-guard.ts +18 -114
  110. package/src/execution/engine/common/pool-manager.ts +4 -0
  111. package/src/execution/engine/common/session-view-service.ts +56 -84
  112. package/src/execution/engine/config.ts +151 -0
  113. package/src/execution/engine/d8-compat.ts +346 -0
  114. package/src/execution/engine/engine-discovery-roots.ts +107 -0
  115. package/src/execution/engine/engine-discovery-scan.ts +269 -0
  116. package/src/execution/engine/engine-discovery.ts +30 -7
  117. package/src/execution/engine/engine-inspect-package.ts +282 -0
  118. package/src/execution/engine/engine-manifest.ts +244 -0
  119. package/src/execution/engine/host/__tests__/host-askuser-endpoint.test.ts +123 -0
  120. package/src/execution/engine/host/__tests__/host-bridge.test.ts +135 -0
  121. package/src/execution/engine/host/__tests__/spawned-children.test.ts +73 -0
  122. package/src/execution/engine/host/host-bridge.ts +150 -0
  123. package/src/execution/engine/host/host-ui-endpoint.ts +47 -0
  124. package/src/execution/engine/host/pi-host-binding.ts +91 -0
  125. package/src/execution/engine/host/spawned-children.ts +139 -0
  126. package/src/execution/engine/host-task-spec.ts +29 -2
  127. package/src/execution/engine/model-validation.ts +55 -2
  128. package/src/execution/engine/paths.ts +16 -41
  129. package/src/execution/engine/port.ts +48 -2
  130. package/src/execution/engine/registry.ts +153 -34
  131. package/src/execution/engine/routing.ts +225 -52
  132. package/src/execution/engine/types.ts +25 -200
  133. package/src/execution/finalize-record.ts +25 -0
  134. package/src/execution/idle-gc.ts +89 -14
  135. package/src/execution/lifecycle-manager.ts +10 -7
  136. package/src/execution/lifecycle-predicates.ts +10 -8
  137. package/src/execution/record-store.ts +29 -2
  138. package/src/execution/relay-env.ts +19 -36
  139. package/src/execution/round-supervisor/domain.ts +89 -0
  140. package/src/execution/round-supervisor/index.ts +33 -0
  141. package/src/execution/round-supervisor/notify-accounting.ts +87 -0
  142. package/src/execution/round-supervisor/reconcile-sweep.ts +235 -0
  143. package/src/execution/round-supervisor/service-binding.test.ts +661 -0
  144. package/src/execution/round-supervisor/service-binding.ts +273 -0
  145. package/src/execution/round-supervisor/supervisor.ts +426 -0
  146. package/src/execution/session-pending.ts +68 -74
  147. package/src/execution/settled-watchdog.ts +39 -0
  148. package/src/execution/subagent-service.ts +746 -400
  149. package/src/execution/subprocess-agent-runner.ts +16 -8
  150. package/src/execution/types.ts +41 -117
  151. package/src/execution/ui-channels.ts +13 -214
  152. package/src/execution/ui-request-handler-factory.ts +7 -4
  153. package/src/execution/ui-request-observability.ts +2 -2
  154. package/src/execution/workflow-state-root.ts +67 -0
  155. package/src/execution/worktree-git-ops.ts +5 -0
  156. package/src/execution/worktree-manager.ts +11 -1
  157. package/src/index.ts +55 -26
  158. package/src/orchestration/__tests__/execute-agent-call.test.ts +1 -1
  159. package/src/orchestration/execute-agent-call.ts +1 -1
  160. package/src/orchestration/file-run-store.ts +144 -59
  161. package/src/orchestration/models/types.ts +43 -42
  162. package/src/shared/zcode-model-ref.ts +31 -0
  163. package/dist/chunk-43ONBFZX.js +0 -240
  164. package/dist/chunk-A4IVZWVX.js +0 -31
  165. package/dist/chunk-APZY4IME.js +0 -27
  166. package/dist/chunk-V3VHZ2VX.js +0 -59
  167. package/dist/execution/engine/engines/zcode/constants.cjs +0 -107
  168. package/dist/execution/engine/engines/zcode/constants.d.cts +0 -135
  169. package/dist/execution/engine/engines/zcode/constants.d.ts +0 -135
  170. package/dist/execution/engine/engines/zcode/constants.js +0 -54
  171. package/dist/execution/engine/engines/zcode/reader.cjs +0 -276
  172. package/dist/execution/engine/engines/zcode/reader.d.cts +0 -21
  173. package/dist/execution/engine/engines/zcode/reader.d.ts +0 -21
  174. package/dist/execution/engine/engines/zcode/reader.js +0 -9
  175. package/src/__tests__/append-system-prompt-assembly.test.ts +0 -243
  176. package/src/__tests__/fr4-get-state-handshake.test.ts +0 -125
  177. package/src/__tests__/session-runner.test.ts +0 -69
  178. package/src/execution/__tests__/ask-user-transit-e2e.test.ts +0 -490
  179. package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +0 -191
  180. package/src/execution/__tests__/descendant-sweep-guards.test.ts +0 -220
  181. package/src/execution/__tests__/descendant-sweep.test.ts +0 -269
  182. package/src/execution/__tests__/engine-model-validation.test.ts +0 -741
  183. package/src/execution/__tests__/epipe-fallback.test.ts +0 -248
  184. package/src/execution/__tests__/execution-runtime-face.test.ts +0 -272
  185. package/src/execution/__tests__/get-state-handshake.test.ts +0 -127
  186. package/src/execution/__tests__/helpers/session-runner-mocks.ts +0 -113
  187. package/src/execution/__tests__/keep-alive-no-progress.test.ts +0 -280
  188. package/src/execution/__tests__/kill-all-escalation.test.ts +0 -196
  189. package/src/execution/__tests__/max-turns-to-watchdog-ms.test.ts +0 -113
  190. package/src/execution/__tests__/output-collector.test.ts +0 -469
  191. package/src/execution/__tests__/pi-invocation.test.ts +0 -166
  192. package/src/execution/__tests__/recursive-visibility-env.test.ts +0 -149
  193. package/src/execution/__tests__/rpc-mode.test.ts +0 -89
  194. package/src/execution/__tests__/run-and-finalize-anchoring.test.ts +0 -387
  195. package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +0 -277
  196. package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +0 -254
  197. package/src/execution/__tests__/run-spawn-edges.test.ts +0 -1079
  198. package/src/execution/__tests__/run-spawn-integration.test.ts +0 -933
  199. package/src/execution/__tests__/run-spawn-resume.test.ts +0 -322
  200. package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +0 -196
  201. package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +0 -207
  202. package/src/execution/__tests__/service-kill-escalation.test.ts +0 -91
  203. package/src/execution/__tests__/session-runner-branch-cache-lru.test.ts +0 -110
  204. package/src/execution/__tests__/session-runner-close-prune.test.ts +0 -181
  205. package/src/execution/__tests__/session-runner-dispatch.test.ts +0 -489
  206. package/src/execution/__tests__/session-runner-epipe.test.ts +0 -179
  207. package/src/execution/__tests__/session-runner-heartbeat-idle-fallback.test.ts +0 -227
  208. package/src/execution/__tests__/session-runner-lifecycle-helpers.test.ts +0 -290
  209. package/src/execution/__tests__/session-runner-schema-env.test.ts +0 -352
  210. package/src/execution/__tests__/settled-watchdog.test.ts +0 -524
  211. package/src/execution/__tests__/spawn-args.test.ts +0 -446
  212. package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +0 -192
  213. package/src/execution/__tests__/spawn-event-adapter.test.ts +0 -167
  214. package/src/execution/__tests__/spawn-worktree-guidance.test.ts +0 -208
  215. package/src/execution/__tests__/spawned-children.test.ts +0 -236
  216. package/src/execution/__tests__/start-sync-model-guard.test.ts +0 -150
  217. package/src/execution/__tests__/stdin-writer.test.ts +0 -464
  218. package/src/execution/__tests__/subagent-service-message-close.test.ts +0 -693
  219. package/src/execution/__tests__/temp-prompt.test.ts +0 -53
  220. package/src/execution/__tests__/timeout-integration.test.ts +0 -616
  221. package/src/execution/__tests__/turn-limiter-semantics.test.ts +0 -194
  222. package/src/execution/__tests__/turn-limiter.test.ts +0 -65
  223. package/src/execution/__tests__/ui-request-handler.test.ts +0 -205
  224. package/src/execution/__tests__/ui-request-queue.test.ts +0 -298
  225. package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +0 -230
  226. package/src/execution/engine/__tests__/common/schema-emulation.test.ts +0 -128
  227. package/src/execution/engine/__tests__/common/session-view-service-zcode-dbpath.test.ts +0 -149
  228. package/src/execution/engine/__tests__/conformance/__fixtures__/pi-golden-events.json +0 -28
  229. package/src/execution/engine/__tests__/conformance/zcode-appserver-harness.ts +0 -128
  230. package/src/execution/engine/__tests__/paths.test.ts +0 -39
  231. package/src/execution/engine/common/schema-emulation.ts +0 -189
  232. package/src/execution/engine/common/session-view-projection.ts +0 -51
  233. package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +0 -615
  234. package/src/execution/engine/engines/pi/__tests__/reader.test.ts +0 -155
  235. package/src/execution/engine/engines/pi/__tests__/spawn-opts-direct.test.ts +0 -257
  236. package/src/execution/engine/engines/pi/argv-mirror.ts +0 -135
  237. package/src/execution/engine/engines/pi/get-state-handshake.ts +0 -179
  238. package/src/execution/engine/engines/pi/output-collector.ts +0 -317
  239. package/src/execution/engine/engines/pi/pi-engine.ts +0 -811
  240. package/src/execution/engine/engines/pi/pi-invocation.ts +0 -120
  241. package/src/execution/engine/engines/pi/reader.ts +0 -59
  242. package/src/execution/engine/engines/pi/registration.ts +0 -37
  243. package/src/execution/engine/engines/pi/session-runner.ts +0 -2840
  244. package/src/execution/engine/engines/pi/spawn-event-adapter.ts +0 -404
  245. package/src/execution/engine/engines/pi/stdin-writer.ts +0 -199
  246. package/src/execution/engine/engines/pi/temp-prompt.ts +0 -62
  247. package/src/execution/engine/engines/pi/turn-limiter.ts +0 -102
  248. package/src/execution/engine/engines/zcode/__tests__/__fixtures__/fake-appserver.mjs +0 -301
  249. package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-appserver.json +0 -36
  250. package/src/execution/engine/engines/zcode/__tests__/appserver-launcher.test.ts +0 -321
  251. package/src/execution/engine/engines/zcode/__tests__/connection.test.ts +0 -473
  252. package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +0 -43
  253. package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +0 -124
  254. package/src/execution/engine/engines/zcode/__tests__/reader.test.ts +0 -210
  255. package/src/execution/engine/engines/zcode/__tests__/registration.test.ts +0 -71
  256. package/src/execution/engine/engines/zcode/__tests__/session-channel-dispose-harvest.test.ts +0 -247
  257. package/src/execution/engine/engines/zcode/__tests__/session-channel-turn-timers.test.ts +0 -451
  258. package/src/execution/engine/engines/zcode/__tests__/session-channel.test.ts +0 -792
  259. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-appserver.test.ts +0 -782
  260. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-degrade.test.ts +0 -168
  261. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-dispose.test.ts +0 -210
  262. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-retry.test.ts +0 -353
  263. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-status.test.ts +0 -282
  264. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-timeout.test.ts +0 -372
  265. package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +0 -145
  266. package/src/execution/engine/engines/zcode/appserver-launcher.ts +0 -167
  267. package/src/execution/engine/engines/zcode/connection.ts +0 -598
  268. package/src/execution/engine/engines/zcode/constants.ts +0 -176
  269. package/src/execution/engine/engines/zcode/golden-sample.ts +0 -47
  270. package/src/execution/engine/engines/zcode/parser.ts +0 -102
  271. package/src/execution/engine/engines/zcode/preparer.ts +0 -235
  272. package/src/execution/engine/engines/zcode/reader.ts +0 -381
  273. package/src/execution/engine/engines/zcode/registration.ts +0 -37
  274. package/src/execution/engine/engines/zcode/session-channel.ts +0 -968
  275. package/src/execution/engine/engines/zcode/zcode-engine.ts +0 -1431
  276. package/src/execution/round-settlement.ts +0 -93
  277. package/src/execution/ui-request-queue.ts +0 -215
  278. package/src/shared/schema-env.ts +0 -44
@@ -23,6 +23,11 @@
23
23
 
24
24
  import type { ChildProcess } from "node:child_process";
25
25
 
26
+ import type {
27
+ HostRoundLifecycleParams,
28
+ ResumeAnchor,
29
+ } from "@zhushanwen/subagent-engine-sdk";
30
+
26
31
  import type { AgentCallOpts } from "../../orchestration/models/types.ts";
27
32
  import type { ModelInfo } from "../model-resolver.ts";
28
33
  import type { SubagentStream } from "../stream-sink.ts";
@@ -120,6 +125,34 @@ export interface RunContext {
120
125
  * SIGTERM / 单任务 abort 误杀共享进程)。
121
126
  */
122
127
  onChildSpawned?: (child: ChildProcess) => void;
128
+ /**
129
+ * [W3 v1.x] chat 会话形态参数(协议 run.params.chat 的 RunContext 承载位):
130
+ * - recordId:core 预建 record 的关联键(引擎据此上报首轮 runId 键之外的反向
131
+ * 载荷与 interact 定位)——task.conversation === true 的 chat 轮必传;
132
+ * - resume:冷续锚点(重开已 idle 的 session 续聊;pi 消费 sessionRef.sessionFile
133
+ * ——对照协议化设计前 SpawnResumeOpts.sessionFile 的锚点面)。
134
+ * 类型权威 = SDK RunChatParams(remote-engine 直传,结构互证由 implements 关系
135
+ * 在 typecheck 期承载)。非 chat 轮不传,wire 上不出现该键。
136
+ */
137
+ chat?: { recordId: string; resume?: ResumeAnchor };
138
+ /**
139
+ * [W3 v1.x] host/roundLifecycle 轮次生命周期消费口(第 9 反向通道,settled/idle/
140
+ * failed 三相位;关联键 runId|recordId 互斥)。首轮(run 会话形态)经 run 作用域
141
+ * 路由到达(runId 键);续聊轮(interact)无 runId——经 EnginePort
142
+ * registerChatRoundRoute 的 recordId 键路由到达(见下)。消费语义(arm/disarm/
143
+ * settled 交棒)归宿主编排层(settled-watchdog 协议事件面接线,W4 三入口)。
144
+ */
145
+ onRoundLifecycle?: (phase: HostRoundLifecycleParams) => void;
146
+ }
147
+
148
+ /**
149
+ * [W3 v1.x] chat 轮次反向通道路由(recordId 键)——interact 续聊轮的 streamDelta /
150
+ * roundLifecycle 分发目标(协议关联键裁定 D1-A:续聊轮无独立 runId)。与 run 作用域
151
+ * RunRoute 同构的薄消费面;注册/注销时机归宿主 chat 编排(轮开始注册、record 终态注销)。
152
+ */
153
+ export interface ChatRoundRoute {
154
+ onStreamDelta?: (delta: string) => void | Promise<void>;
155
+ onRoundLifecycle?: (phase: HostRoundLifecycleParams) => void | Promise<void>;
123
156
  }
124
157
 
125
158
  // ============================================================
@@ -173,6 +206,17 @@ export interface EnginePort {
173
206
  /** D6 三级降级链:①引擎原生读取 → ②宿主 event journal(P2)→ ③outcome-only。 */
174
207
  read(handle: EngineHandle): Promise<SessionView>;
175
208
 
209
+ /**
210
+ * [W3 v1.x] 可选面:chat 轮次反向通道路由注册(recordId 键)。interact 续聊轮的
211
+ * streamDelta / roundLifecycle 按 recordId 关联(无 runId,D1-A),引擎进程发射后
212
+ * 经协议客户端分发到本路由。与 validateModel/listModels 同款的 additively-optional
213
+ * 演进:宿主 feature-detect(`typeof registerChatRoundRoute === "function"`),
214
+ * 未实现的引擎(不支持 chat 域或旧客户端)续聊轮 delta/生命周期静默不可达——
215
+ * conversation gate 已在派发前拦住无 chat 能力的引擎,本缺省只在「能力声明与
216
+ * 客户端实现错位」的诊断形态出现。返回注销函数(record 终态时宿主调用)。
217
+ */
218
+ registerChatRoundRoute?(recordId: string, route: ChatRoundRoute): () => void;
219
+
176
220
  /**
177
221
  * [U7] 可选面:模型可发现性——引擎自带 provider/model 体系时(如 zcode 的 v2 桌面
178
222
  * 登录态),列出当前环境实际可用的模型清单(带凭据校验),供 system prompt 引擎段
@@ -190,8 +234,10 @@ export interface EnginePort {
190
234
  * 「引擎与模型不配套」错误,见 engine/model-validation.ts)。
191
235
  *
192
236
  * modelRef undefined = 查询引擎缺省模型(D2-1:主 agent 的 pi id 不透传给非 pi 引擎,
193
- * 缺省语义归引擎——zcode 落 ZCODE_FALLBACK_DEFAULT_MODEL)。返回 canonical 全名供
194
- * record.model 留痕。
237
+ * 缺省语义归引擎——zcode 落 ZCODE_FALLBACK_DEFAULT_MODEL)。返回 canonical ref 供
238
+ * record.model 留痕。[W3 契约变更④] 协议化后 canonicalRef 允许**无斜杠**形态
239
+ * (引擎原样返回的 ref)——core 侧按 provider=""/id=ref/整串进 name 拆分留痕
240
+ * (splitEngineModelRef 单一权威),不落 "<ref>/" 畸形。
195
241
  *
196
242
  * 未实现:model 透传,引擎自身 prepare 期校验兜底(现状语义);pi 不实现(pi 链走
197
243
  * 既有三层解析 + assertCanonicalModelRef 裁决,搬迁是大重构,设计 D2-2 被否②)。
@@ -1,19 +1,31 @@
1
1
  // src/execution/engine/registry.ts
2
2
  //
3
- // 引擎注册表(P1)。设计权威源:docs/architecture/subagent-engine-abstraction.md
4
- // §3.3.1 分层总图([引擎注册表 engine registry](id → factory))与 §3.3.3 错误规格
5
- // 1 行(engine_not_found 指向注册表清单 + 配置文件路径)。
3
+ // 引擎注册表(P1;W3 协议化改造)。设计权威源(现行):
4
+ // docs/design/subagent-engine-protocolization.md §3.4 发现与注册 / §3.8 D1(EngineDescriptor
5
+ // 双模)+ D4(缺省引擎与 fallback 目标);实现级规格 impl-plan §2.3。历史权威源
6
+ // docs/architecture/subagent-engine-abstraction.md §3.3.1/§3.3.3(engine_not_found 错误
7
+ // 规格第 1 行)仍然有效。
6
8
  //
7
- // 为什么需要注册表:引擎身份是「spawn 细节的归属边界」(设计 §2.3 根因一)——上层
8
- // (配置路由/agent 解析,P4 接线)按 id 取引擎,不感知实现类;新引擎接入 = 新增一个
9
- // 适配器模块 + 注册表登记一行,不改上层与既有引擎(目标 5)。
9
+ // 为什么需要注册表:引擎身份是「spawn 细节的归属边界」——上层(配置路由/agent 解析)
10
+ // id 取引擎,不感知实现类;新引擎接入 = 装一个引擎包(manifest 自注册,W4 发现器
11
+ // 装载)或登记一行 inproc 工厂(过渡期),不改上层与既有引擎。
10
12
  //
11
- // 依赖方向(设计 §3.3.1 贯穿纪律④):registry 只依赖 port.ts 的类型,不 import 任何
12
- // 具体引擎——注册由组合根(index.ts)或引擎自己的 registration 模块完成,防循环依赖。
13
- // (core/logger facade 基础设施非引擎,dispose best-effort 记日志需要它,不违反本纪律。)
13
+ // 双模 descriptor(D1):`{kind:"inproc", factory}`(过渡期——内建 pi/zcode,DoD#5
14
+ // inproc 分支删除)与 `{kind:"cli", command, args, capabilities, portFactory}`(目标——
15
+ // 独立引擎 CLI 包,port W2 RemoteEngine 承担)。上层 getEngine(id) 返回代理,两形态
16
+ // 透明:cli descriptor 首次使用才经 portFactory 解析(构造同步、缺包/坏包不在构造期
17
+ // throw,§3.5.3 代理形态),失败形态 = 首次协议调用的结构化错误。
18
+ //
19
+ // 依赖方向(贯穿纪律,协议化后不变):registry 只依赖 port/types 的类型与 SDK 的
20
+ // modelCatalog 条目类型,不 import 任何具体引擎或协议客户端(client/)——cli 形态的
21
+ // port 装配(RemoteEngine + EngineClient + 宿主侧参数)归 portFactory 注入方(W4 发现器
22
+ // 装载 descriptor 时装配),registry 只持类型与惰性代理。(core/logger 是 facade 基础
23
+ // 设施非引擎,dispose best-effort 记日志需要它,不违反本纪律。)
14
24
 
15
25
  import { getLogger } from "../../core/logger.ts";
26
+ import type { ModelCatalogEntry } from "@zhushanwen/subagent-engine-sdk";
16
27
 
28
+ import type { EngineCapabilities } from "./types.ts";
17
29
  import type { EnginePort } from "./port.ts";
18
30
 
19
31
  // core log facade(execution 层统一 "subagents" component,模块顶层缓存惯例)。
@@ -23,8 +35,60 @@ const logger = getLogger("subagents");
23
35
  export type EngineFactory = () => EnginePort;
24
36
 
25
37
  /**
26
- * 缺省引擎 id(D9:缺省引擎 = 'pi',回填期零风险默认)。
27
- * P1 per-agent/调用级 engine 字段(P4 配置路由引入三层优先级),一切执行恒走缺省。
38
+ * 引擎包 manifest 注册期快照(session_start 扫描所得,非握手缓存——设计 §3.3「同步
39
+ * 成员清单」单一同步源原则)。descriptor 携带,cli 形态 port(RemoteEngine)的同步
40
+ * 成员(capabilities/listModels/validateModel)直读本快照。
41
+ *
42
+ * 与 client/remote-engine.ts 的 RemoteEngineManifestSnapshot 是结构闭包(后者多出
43
+ * core 中立类型包装;漂移由 protocol-closure 同族结构互证断言守卫)。
44
+ */
45
+ export interface EngineManifestSnapshot {
46
+ /** manifest `capabilities`(同步能力位权威,注册期读,无缓存)。 */
47
+ capabilities: EngineCapabilities;
48
+ /**
49
+ * manifest `modelCatalog` 三态(W4 §2.4:缺省 = 不注入保持 undefined;null 合法等价
50
+ * 省略;`models: []` 仅作者显式声明)。解析器不得把省略填成 `[]`——否则「无枚举面」
51
+ * 语义不可达。
52
+ */
53
+ modelCatalog?: { dynamic: boolean; models: ModelCatalogEntry[] } | null;
54
+ /** manifest `displayName`(可选,缺省 = id;D4 缺省引擎回落序的排序键)。 */
55
+ displayName?: string;
56
+ }
57
+
58
+ /** cli 形态 descriptor(目标形态):引擎 CLI 启动参数 + manifest 快照 + port 装配器。 */
59
+ export interface CliEngineDescriptor {
60
+ kind: "cli";
61
+ /** 引擎 CLI 命令(发现器解析后的绝对路径/可执行名;不依赖 PATH)。 */
62
+ command: string;
63
+ /** 引擎 CLI 参数(不含 command 本身)。 */
64
+ args: readonly string[];
65
+ /** manifest `capabilities`(同步能力位权威——gate 同步消费,无缓存)。 */
66
+ capabilities: EngineCapabilities;
67
+ /**
68
+ * cli 形态 EnginePort 实例装配器(core 侧接线通道,非 manifest 面)。生产 = W4
69
+ * 发现器装载 descriptor 时装配(W2 `RemoteEngine` + `EngineClient` + 宿主侧参数
70
+ * dataDir/hostKind/engineConfig 等——registry 不越权猜宿主形态)。必须构造同步、
71
+ * 不 throw(§3.5.3 代理形态:缺包/坏包的失败推迟到首次协议调用)。
72
+ */
73
+ portFactory: EngineFactory;
74
+ /** manifest 快照余项(modelCatalog/displayName;capabilities 已提升为直读字段)。 */
75
+ manifest?: Omit<EngineManifestSnapshot, "capabilities">;
76
+ }
77
+
78
+ /** inproc 形态 descriptor(过渡期):内建引擎工厂。DoD#5 后随 inproc 分支删除。 */
79
+ export interface InprocEngineDescriptor {
80
+ kind: "inproc";
81
+ factory: EngineFactory;
82
+ }
83
+
84
+ /** D1 双模引擎描述符:inproc(过渡期)/ cli(目标)。上层经 getEngine 拿代理,两形态透明。 */
85
+ export type EngineDescriptor = InprocEngineDescriptor | CliEngineDescriptor;
86
+
87
+ /**
88
+ * 缺省引擎 id(D4 重定义:**配置的缺省引擎 id**——config defaultEngine 未配置时的
89
+ * 归一名,不再表达「内置 pi 永久兜底」)。配置值不在已发现清单 → warn + 回落第一个
90
+ * 可用引擎(manifest displayName 稳定序,D4);全不可用 → 派发期 engine_not_found
91
+ * (「未发现任何引擎包」+ 安装指引)。
28
92
  */
29
93
  export const DEFAULT_ENGINE_ID = "pi";
30
94
 
@@ -39,12 +103,15 @@ export function normalizeEngineId(engine: string | undefined): string {
39
103
  }
40
104
 
41
105
  /**
42
- * engine_not_found(错误规格表第 1 行):agent frontmatter 写了未注册 engine id。
106
+ * engine_not_found(错误规格表第 1 行):请求的引擎 id 无对应注册/发现结果。
43
107
  * 错误文案契约:含已注册引擎清单 + 配置文件指引——配置错误前置暴露(agent 解析期),
44
- * 不留到运行时神秘失败(设计目标 4)。
108
+ * 不留到运行时神秘失败。
109
+ *
110
+ * D4 全不可用文案:清单为空(未发现任何引擎包)时追加安装指引——此时问题不是 id
111
+ * 写错,而是环境里没有任何引擎包可派发,恢复动作 = 装引擎包/配发现根,不是改 typo。
45
112
  */
46
113
  export class EngineNotFoundError extends Error {
47
- /** 结构化错误码(§3.3.3 错误规格表的 code 列,供调用方程序化分流)。 */
114
+ /** 结构化错误码(错误规格表的 code 列,供调用方程序化分流)。 */
48
115
  readonly code = "engine_not_found";
49
116
  /** 请求的(未注册的)引擎 id。 */
50
117
  readonly engineId: string;
@@ -57,9 +124,13 @@ export class EngineNotFoundError extends Error {
57
124
  super(
58
125
  `engine_not_found: engine '${engineId}' is not registered. ` +
59
126
  `Registered engines: ${registered.length > 0 ? registered.join(", ") : "(none)"}. ` +
60
- `Recovery: check the engine id in the agent .md frontmatter (engine: field) or the global ` +
61
- `default engine setting, fix the typo, or install/register the engine first ` +
62
- `(registered engines are listed above).` +
127
+ (registered.length === 0
128
+ ? `No engine packages were discovered. ` +
129
+ `Recovery: install an engine package (e.g. @zhushanwen/pi-subagent-cli / @zhushanwen/zcode-subagent-cli), ` +
130
+ `or add its location to XYZ_AGENT_ENGINE_ROOTS / subagents/config.json engines section.`
131
+ : `Recovery: check the engine id in the agent .md frontmatter (engine: field) or the global ` +
132
+ `default engine setting, fix the typo, or install/register the engine first ` +
133
+ `(registered engines are listed above).`) +
63
134
  (source !== undefined ? ` Source: ${source}.` : ""),
64
135
  );
65
136
  this.name = "EngineNotFoundError";
@@ -70,7 +141,7 @@ export class EngineNotFoundError extends Error {
70
141
  }
71
142
 
72
143
  /**
73
- * id → factory(注册表本体)+ id → 惰性单例(getEngine 首次取用创建;registerEngine
144
+ * id → descriptor(注册表本体)+ id → 惰性单例(getEngine 首次取用创建;重复注册
74
145
  * 覆盖时丢弃旧实例)。进程级单例状态,用 globalThis[Symbol.for] 持有防 jiti 双路径
75
146
  * 加载分裂(development-guide §7.5),不用模块级 const。
76
147
  */
@@ -78,7 +149,7 @@ const ENGINE_REGISTRY_SLOT_KEY = Symbol.for("@zhushanwen/pi-subagent-workflow.en
78
149
 
79
150
  /** 注册表槽位形状(同文件唯一写入点,运行时保证)。 */
80
151
  interface EngineRegistrySlot {
81
- factories: Map<string, EngineFactory>;
152
+ descriptors: Map<string, EngineDescriptor>;
82
153
  singletons: Map<string, EnginePort>;
83
154
  }
84
155
 
@@ -87,12 +158,17 @@ function getRegistrySlot(): EngineRegistrySlot {
87
158
  // 避免双重断言(同 model-config-service.ts 先例)。
88
159
  let slot = Reflect.get(globalThis, ENGINE_REGISTRY_SLOT_KEY) as EngineRegistrySlot | undefined;
89
160
  if (!slot) {
90
- slot = { factories: new Map(), singletons: new Map() };
161
+ slot = { descriptors: new Map(), singletons: new Map() };
91
162
  Reflect.set(globalThis, ENGINE_REGISTRY_SLOT_KEY, slot);
92
163
  }
93
164
  return slot;
94
165
  }
95
166
 
167
+ /** descriptor → port 装配器(两形态透明收敛点:inproc = factory,cli = portFactory)。 */
168
+ function portFactoryOf(descriptor: EngineDescriptor): EngineFactory {
169
+ return descriptor.kind === "inproc" ? descriptor.factory : descriptor.portFactory;
170
+ }
171
+
96
172
  /**
97
173
  * [R1 D6] 触发引擎 dispose:同步调用拿 Promise 不 await(「触发不等待」,D6①——
98
174
  * dispose 的同步面〔fire close 帧 + 同步 SIGTERM〕由引擎实现保证在返回 Promise 前
@@ -120,18 +196,27 @@ function triggerEngineDispose(engine: EnginePort, source: string): void {
120
196
  }
121
197
 
122
198
  /**
123
- * 登记引擎工厂。重复注册同一 id = 覆盖(组合根可能多次执行,如每次 session_start 重跑
124
- * registerPiEngine——幂等覆盖保证不炸也不堆积),覆盖时丢弃缓存的旧单例,让下一次
125
- * getEngine 用新工厂重建。
199
+ * 登记引擎工厂(inproc 快捷形态,等价 registerEngineDescriptor(id, {kind:"inproc", factory}))。
200
+ * 重复注册同一 id = 覆盖(组合根可能多次执行,如每次 session_start 重跑 registerPiEngine
201
+ * ——幂等覆盖保证不炸也不堆积),覆盖时丢弃缓存的旧单例,让下一次 getEngine 用新
202
+ * descriptor 重建。
126
203
  *
127
204
  * [R1 D6②] 覆盖前对已实例化的旧单例触发 dispose(防泄漏——旧实例可能持有常驻进程/
128
205
  * 长连接);触发不等待 + 失败不阻断(见 triggerEngineDispose),幂等覆盖语义不变。
129
206
  */
130
207
  export function registerEngine(id: string, factory: EngineFactory): void {
208
+ registerEngineDescriptor(id, { kind: "inproc", factory });
209
+ }
210
+
211
+ /**
212
+ * 登记引擎 descriptor(D1 双模注册入口)。inproc(过渡期内建引擎)与 cli(引擎包,
213
+ * portFactory 由发现器装配)两形态;覆盖语义与 dispose 触发同 registerEngine。
214
+ */
215
+ export function registerEngineDescriptor(id: string, descriptor: EngineDescriptor): void {
131
216
  const slot = getRegistrySlot();
132
217
  const previous = slot.singletons.get(id);
133
- if (previous) triggerEngineDispose(previous, `registerEngine('${id}') overwrite`);
134
- slot.factories.set(id, factory);
218
+ if (previous) triggerEngineDispose(previous, `registerEngineDescriptor('${id}') overwrite`);
219
+ slot.descriptors.set(id, descriptor);
135
220
  slot.singletons.delete(id);
136
221
  }
137
222
 
@@ -151,36 +236,70 @@ export function disposeEngines(): void {
151
236
  }
152
237
  }
153
238
 
154
- /** 未注册 id 抛 EngineNotFoundError(含已注册清单与配置指引)。 */
239
+ /**
240
+ * 按 id 取引擎代理(两形态透明)。惰性单例:descriptor 首次使用才解析(inproc 工厂
241
+ * / cli portFactory 都是此刻才调用——构造同步、缺包/坏包不在本函数期 throw,§3.5.3
242
+ * 代理形态;cli 形态实例 = W2 RemoteEngine,失败形态是首次协议调用的结构化错误)。
243
+ */
155
244
  export function getEngine(id: string): EnginePort {
156
245
  const slot = getRegistrySlot();
157
246
  const cached = slot.singletons.get(id);
158
247
  if (cached) return cached;
159
- const factory = slot.factories.get(id);
160
- if (!factory) {
248
+ const descriptor = slot.descriptors.get(id);
249
+ if (!descriptor) {
161
250
  throw new EngineNotFoundError(id, listEngines());
162
251
  }
163
- const engine = factory();
252
+ const engine = portFactoryOf(descriptor)();
164
253
  slot.singletons.set(id, engine);
165
254
  return engine;
166
255
  }
167
256
 
168
- /** id 是否已注册(agent 解析期的配置校验入口,D9——不取实例、不触发工厂副作用)。 */
257
+ /** id 是否已注册(agent 解析期的配置校验入口——不取实例、不触发 port 装配副作用)。 */
169
258
  export function hasEngine(id: string): boolean {
170
- return getRegistrySlot().factories.has(id);
259
+ return getRegistrySlot().descriptors.has(id);
171
260
  }
172
261
 
173
262
  /** 已注册引擎 id 清单(稳定序 = 注册序;错误文案与 GUI 引擎选择器共用)。 */
174
263
  export function listEngines(): string[] {
175
- return [...getRegistrySlot().factories.keys()];
264
+ return [...getRegistrySlot().descriptors.keys()];
265
+ }
266
+
267
+ /**
268
+ * 已发现引擎按 manifest `displayName` 稳定序排序(D4 缺省引擎回落序的单一权威)。
269
+ * displayName 缺省 = id(inproc 形态无 manifest 恒用 id);排序 = 码点序(不用
270
+ * localeCompare——跨环境字节稳定,同 model-prompt.ts 字节稳定先例);displayName
271
+ * 相同按 id 决胜,保证全序确定。
272
+ */
273
+ export function listEnginesByDisplayName(): string[] {
274
+ const slot = getRegistrySlot();
275
+ const keyed = [...slot.descriptors.entries()].map(([id, descriptor]) => {
276
+ const displayName =
277
+ descriptor.kind === "cli" ? (descriptor.manifest?.displayName ?? id) : id;
278
+ return { id, displayName };
279
+ });
280
+ keyed.sort((a, b) =>
281
+ a.displayName !== b.displayName
282
+ ? a.displayName < b.displayName ? -1 : 1
283
+ : a.id < b.id ? -1 : a.id > b.id ? 1 : 0,
284
+ );
285
+ return keyed.map((k) => k.id);
286
+ }
287
+
288
+ /**
289
+ * 第一个可用引擎 id(D4 回落目标:缺省引擎不在清单 / fallback 目标的「首个可用」)。
290
+ * 空注册表返回 undefined——调用方按 D4 转 engine_not_found(「未发现任何引擎包」+
291
+ * 安装指引),不静默。
292
+ */
293
+ export function firstAvailableEngineId(): string | undefined {
294
+ return listEnginesByDisplayName()[0];
176
295
  }
177
296
 
178
297
  /**
179
- * 清空注册表(测试隔离专用:防止用例间工厂/单例泄漏串扰)。
298
+ * 清空注册表(测试隔离专用:防止用例间 descriptor/单例泄漏串扰)。
180
299
  * 生产代码禁用——进程内注册表是全局状态,清空会让已获取的引擎句柄与新注册表脱钩。
181
300
  */
182
301
  export function clearEngines(): void {
183
302
  const slot = getRegistrySlot();
184
- slot.factories.clear();
303
+ slot.descriptors.clear();
185
304
  slot.singletons.clear();
186
305
  }