@zhushanwen/subagent-core 0.2.0 → 0.5.1

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 (347) hide show
  1. package/README.md +45 -2
  2. package/agents/analyst.md +60 -0
  3. package/agents/coder.md +69 -0
  4. package/agents/debugger.md +66 -0
  5. package/agents/doc-reviewer.md +49 -0
  6. package/agents/explorer.md +63 -0
  7. package/agents/general-purpose.md +32 -0
  8. package/agents/orchestrator.md +61 -0
  9. package/agents/planner.md +53 -0
  10. package/agents/researcher.md +67 -0
  11. package/agents/reviewer.md +73 -0
  12. package/dist/chunk-43ONBFZX.js +240 -0
  13. package/dist/chunk-APZY4IME.js +27 -0
  14. package/dist/chunk-V3VHZ2VX.js +59 -0
  15. package/dist/execution/engine/engines/zcode/constants.cjs +62 -9
  16. package/dist/execution/engine/engines/zcode/constants.d.cts +116 -12
  17. package/dist/execution/engine/engines/zcode/constants.d.ts +116 -12
  18. package/dist/execution/engine/engines/zcode/constants.js +39 -7
  19. package/dist/execution/engine/engines/zcode/reader.d.cts +2 -1
  20. package/dist/execution/engine/engines/zcode/reader.d.ts +2 -1
  21. package/dist/execution/engine/engines/zcode/reader.js +4 -234
  22. package/dist/execution/engine/paths.js +7 -19
  23. package/dist/index.cjs +18234 -1277
  24. package/dist/index.d.cts +5646 -673
  25. package/dist/index.d.ts +5646 -673
  26. package/dist/index.js +17828 -1277
  27. package/dist/{types-BxyAidGf.d.cts → types-Dv4QhSJ_.d.cts} +678 -112
  28. package/dist/{types-BxyAidGf.d.ts → types-Dv4QhSJ_.d.ts} +678 -112
  29. package/package.json +7 -4
  30. package/src/__tests__/agent-opts-resolver-schema-prompt.test.ts +0 -1
  31. package/src/__tests__/append-system-prompt-assembly.test.ts +8 -7
  32. package/src/__tests__/fr4-get-state-handshake.test.ts +2 -2
  33. package/src/__tests__/m2-append-content-probe.test.ts +0 -1
  34. package/src/__tests__/manifest-store.test.ts +10 -9
  35. package/src/__tests__/record-store-cache.test.ts +0 -2
  36. package/src/__tests__/record-store-index.test.ts +1 -2
  37. package/src/__tests__/review-fix-loop-script.test.ts +93 -3
  38. package/src/__tests__/review-fix-loop-utils.test.ts +31 -10
  39. package/src/__tests__/robustness-low-batch1.test.ts +6 -5
  40. package/src/__tests__/robustness-medium-batch1.test.ts +2 -2
  41. package/src/__tests__/robustness-medium-batch2.test.ts +17 -6
  42. package/src/__tests__/robustness-medium-batch4.test.ts +2 -2
  43. package/src/__tests__/session-runner.test.ts +1 -1
  44. package/src/__tests__/smoke.test.ts +9 -2
  45. package/src/core/__tests__/host-services.test.ts +2 -2
  46. package/src/core/__tests__/logger.test.ts +1 -1
  47. package/src/core/error-message.ts +9 -0
  48. package/src/core/host-services.ts +21 -5
  49. package/src/core/notify-ports.ts +18 -5
  50. package/src/execution/__tests__/agent-profile.test.ts +232 -0
  51. package/src/execution/__tests__/agent-registry.test.ts +370 -0
  52. package/src/execution/__tests__/agent-result-mapper.test.ts +18 -2
  53. package/src/execution/__tests__/agents-assembly.test.ts +219 -0
  54. package/src/execution/__tests__/ask-user-transit-e2e.test.ts +6 -6
  55. package/src/execution/__tests__/chat-engine-routing.test.ts +136 -30
  56. package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +5 -4
  57. package/src/execution/__tests__/cold-resurrect.test.ts +283 -0
  58. package/src/execution/__tests__/create-concurrency-pool.test.ts +194 -0
  59. package/src/execution/__tests__/delivery-methods.test.ts +77 -62
  60. package/src/execution/__tests__/descendant-sweep-guards.test.ts +220 -0
  61. package/src/execution/__tests__/descendant-sweep.test.ts +269 -0
  62. package/src/execution/__tests__/dialog-queue.test.ts +199 -1
  63. package/src/execution/__tests__/engine-model-validation.test.ts +741 -0
  64. package/src/execution/__tests__/epipe-fallback.test.ts +31 -24
  65. package/src/execution/__tests__/execute-and-await-worktree.test.ts +21 -91
  66. package/src/execution/__tests__/execute-nesting.test.ts +34 -188
  67. package/src/execution/__tests__/execution-record.test.ts +217 -60
  68. package/src/execution/__tests__/execution-runtime-face.test.ts +272 -0
  69. package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +3 -3
  70. package/src/execution/__tests__/finalize-record.test.ts +278 -16
  71. package/src/execution/__tests__/gc-timer.test.ts +57 -2
  72. package/src/execution/__tests__/get-record-for-action-restart.test.ts +14 -12
  73. package/src/execution/__tests__/get-state-handshake.test.ts +127 -0
  74. package/src/execution/__tests__/helpers/mock-extension-api.ts +1 -1
  75. package/src/execution/__tests__/helpers/session-runner-mocks.ts +113 -0
  76. package/src/execution/__tests__/helpers/spawn-mock.ts +150 -3
  77. package/src/execution/__tests__/helpers/subagent-service-mocks.ts +137 -0
  78. package/src/execution/__tests__/keep-alive-no-progress.test.ts +280 -0
  79. package/src/execution/__tests__/kill-all-escalation.test.ts +196 -0
  80. package/src/execution/__tests__/lifecycle-manager-idle-timer-identity.test.ts +117 -0
  81. package/src/execution/__tests__/lifecycle-manager.test.ts +39 -0
  82. package/src/execution/__tests__/lifecycle-predicates.test.ts +1 -2
  83. package/src/execution/__tests__/manifest-parentid.test.ts +4 -4
  84. package/src/execution/__tests__/manifest-store-tmp-recovery.test.ts +99 -0
  85. package/src/execution/__tests__/max-turns-to-watchdog-ms.test.ts +113 -0
  86. package/src/execution/__tests__/model-resolver.test.ts +2 -2
  87. package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +23 -121
  88. package/src/execution/__tests__/nested-visibility.test.ts +2 -0
  89. package/src/execution/__tests__/notify-ledger.test.ts +1249 -0
  90. package/src/execution/__tests__/output-collector.test.ts +117 -6
  91. package/src/execution/__tests__/pi-invocation.test.ts +1 -1
  92. package/src/execution/__tests__/record-store-orphan-revive.test.ts +159 -0
  93. package/src/execution/__tests__/record-store.test.ts +89 -5
  94. package/src/execution/__tests__/records-cwd-isolation.test.ts +2 -2
  95. package/src/execution/__tests__/recursive-visibility-baseline.test.ts +29 -132
  96. package/src/execution/__tests__/recursive-visibility-env.test.ts +55 -179
  97. package/src/execution/__tests__/rpc-mode.test.ts +2 -2
  98. package/src/execution/__tests__/run-and-finalize-anchoring.test.ts +387 -0
  99. package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +19 -9
  100. package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +5 -4
  101. package/src/execution/__tests__/run-spawn-edges.test.ts +418 -26
  102. package/src/execution/__tests__/run-spawn-integration.test.ts +3 -3
  103. package/src/execution/__tests__/run-spawn-resume.test.ts +2 -2
  104. package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +2 -2
  105. package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +16 -8
  106. package/src/execution/__tests__/service-kill-escalation.test.ts +91 -0
  107. package/src/execution/__tests__/session-context-resolver.test.ts +0 -2
  108. package/src/execution/__tests__/session-file-gc.test.ts +35 -0
  109. package/src/execution/__tests__/session-pending.test.ts +471 -0
  110. package/src/execution/__tests__/session-reconstructor.test.ts +366 -15
  111. package/src/execution/__tests__/session-runner-branch-cache-lru.test.ts +110 -0
  112. package/src/execution/__tests__/session-runner-close-prune.test.ts +181 -0
  113. package/src/execution/__tests__/session-runner-dispatch.test.ts +489 -0
  114. package/src/execution/__tests__/session-runner-epipe.test.ts +4 -3
  115. package/src/execution/__tests__/session-runner-heartbeat-idle-fallback.test.ts +227 -0
  116. package/src/execution/__tests__/session-runner-lifecycle-helpers.test.ts +290 -0
  117. package/src/execution/__tests__/session-runner-schema-env.test.ts +7 -5
  118. package/src/execution/__tests__/settled-watchdog.test.ts +524 -0
  119. package/src/execution/__tests__/spawn-args.test.ts +11 -10
  120. package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +26 -23
  121. package/src/execution/__tests__/spawn-event-adapter.test.ts +1 -1
  122. package/src/execution/__tests__/spawn-worktree-guidance.test.ts +5 -4
  123. package/src/execution/__tests__/spawned-children.test.ts +155 -11
  124. package/src/execution/__tests__/start-sync-model-guard.test.ts +4 -4
  125. package/src/execution/__tests__/stdin-writer.test.ts +6 -4
  126. package/src/execution/__tests__/stream-sink-retirement.test.ts +6 -6
  127. package/src/execution/__tests__/subagent-actions-core.test.ts +1017 -0
  128. package/src/execution/__tests__/subagent-service-message-close.test.ts +99 -35
  129. package/src/execution/__tests__/subagent-service-multiproc-guard.test.ts +174 -0
  130. package/src/execution/__tests__/subagent-service-notify-gate.test.ts +271 -0
  131. package/src/execution/__tests__/subagent-service-parent-guard.test.ts +33 -29
  132. package/src/execution/__tests__/subagent-service-recovery-bounds.test.ts +341 -0
  133. package/src/execution/__tests__/subagent-service.test.ts +37 -30
  134. package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +88 -12
  135. package/src/execution/__tests__/subprocess-agent-runner-timeout.test.ts +75 -0
  136. package/src/execution/__tests__/subprocess-agent-runner.test.ts +22 -20
  137. package/src/execution/__tests__/temp-prompt.test.ts +1 -1
  138. package/src/execution/__tests__/timeout-integration.test.ts +13 -12
  139. package/src/execution/__tests__/turn-limiter-semantics.test.ts +1 -1
  140. package/src/execution/__tests__/turn-limiter.test.ts +1 -1
  141. package/src/execution/__tests__/ui-request-handler-factory.test.ts +107 -7
  142. package/src/execution/__tests__/ui-request-handler.test.ts +4 -3
  143. package/src/execution/__tests__/ui-request-queue.test.ts +167 -2
  144. package/src/execution/__tests__/worktree-git-ops.test.ts +571 -0
  145. package/src/execution/__tests__/worktree-manager.test.ts +14 -5
  146. package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +4 -3
  147. package/src/execution/__tests__/worktree-reconcile-aging.test.ts +204 -0
  148. package/src/execution/agent-registry.ts +298 -30
  149. package/src/execution/agent-result-mapper.ts +3 -0
  150. package/src/execution/agents-assembly.ts +88 -0
  151. package/src/execution/cold-resurrect.ts +199 -0
  152. package/src/execution/concurrency-pool.ts +71 -9
  153. package/src/execution/dialog-queue.ts +101 -3
  154. package/src/execution/engine/__tests__/common/capability-gate.test.ts +91 -0
  155. package/src/execution/engine/__tests__/common/data-dir.test.ts +1 -1
  156. package/src/execution/engine/__tests__/common/journal-wiring.test.ts +84 -0
  157. package/src/execution/engine/__tests__/common/kill-chain.test.ts +34 -7
  158. package/src/execution/engine/__tests__/common/persona-router.test.ts +4 -5
  159. package/src/execution/engine/__tests__/common/pool-manager.test.ts +211 -51
  160. package/src/execution/engine/__tests__/common/session-view-service-zcode-dbpath.test.ts +149 -0
  161. package/src/execution/engine/__tests__/common/session-view-service.test.ts +499 -0
  162. package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +56 -99
  163. package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +88 -15
  164. package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +33 -7
  165. package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +1 -1
  166. package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +75 -54
  167. package/src/execution/engine/__tests__/conformance/zcode-appserver-harness.ts +128 -0
  168. package/src/execution/engine/__tests__/engine-discovery.test.ts +15 -3
  169. package/src/execution/engine/__tests__/model-prompt.test.ts +15 -3
  170. package/src/execution/engine/__tests__/registry.test.ts +94 -3
  171. package/src/execution/engine/__tests__/routing.test.ts +115 -6
  172. package/src/execution/engine/common/capability-gate.ts +92 -0
  173. package/src/execution/engine/common/journal-wiring.ts +97 -0
  174. package/src/execution/engine/common/kill-chain.ts +42 -10
  175. package/src/execution/engine/common/nesting-guard.ts +66 -0
  176. package/src/execution/engine/common/persona-router.ts +18 -8
  177. package/src/execution/engine/common/pool-manager.ts +332 -78
  178. package/src/execution/engine/common/session-view-service.ts +506 -0
  179. package/src/execution/engine/common/session-view-types.ts +125 -0
  180. package/src/execution/engine/engine-discovery.ts +12 -16
  181. package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +174 -28
  182. package/src/execution/engine/engines/pi/__tests__/spawn-opts-direct.test.ts +257 -0
  183. package/src/execution/{argv-mirror.ts → engine/engines/pi/argv-mirror.ts} +43 -21
  184. package/src/execution/{get-state-handshake.ts → engine/engines/pi/get-state-handshake.ts} +86 -11
  185. package/src/execution/{output-collector.ts → engine/engines/pi/output-collector.ts} +108 -19
  186. package/src/execution/engine/engines/pi/pi-engine.ts +434 -38
  187. package/src/execution/{pi-invocation.ts → engine/engines/pi/pi-invocation.ts} +2 -2
  188. package/src/execution/engine/engines/pi/reader.ts +11 -0
  189. package/src/execution/engine/engines/pi/registration.ts +4 -2
  190. package/src/execution/engine/engines/pi/session-runner.ts +2840 -0
  191. package/src/execution/{spawn-event-adapter.ts → engine/engines/pi/spawn-event-adapter.ts} +75 -34
  192. package/src/execution/{stdin-writer.ts → engine/engines/pi/stdin-writer.ts} +10 -9
  193. package/src/execution/{temp-prompt.ts → engine/engines/pi/temp-prompt.ts} +2 -2
  194. package/src/execution/{turn-limiter.ts → engine/engines/pi/turn-limiter.ts} +1 -1
  195. package/src/execution/engine/engines/zcode/__tests__/__fixtures__/fake-appserver.mjs +301 -0
  196. package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-appserver.json +36 -0
  197. package/src/execution/engine/engines/zcode/__tests__/appserver-launcher.test.ts +321 -0
  198. package/src/execution/engine/engines/zcode/__tests__/connection.test.ts +473 -0
  199. package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +7 -210
  200. package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +6 -110
  201. package/src/execution/engine/engines/zcode/__tests__/session-channel-dispose-harvest.test.ts +247 -0
  202. package/src/execution/engine/engines/zcode/__tests__/session-channel-turn-timers.test.ts +451 -0
  203. package/src/execution/engine/engines/zcode/__tests__/session-channel.test.ts +792 -0
  204. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-appserver.test.ts +782 -0
  205. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-degrade.test.ts +168 -0
  206. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-dispose.test.ts +210 -0
  207. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-retry.test.ts +353 -0
  208. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-status.test.ts +282 -0
  209. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-timeout.test.ts +372 -0
  210. package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +65 -47
  211. package/src/execution/engine/engines/zcode/appserver-launcher.ts +167 -0
  212. package/src/execution/engine/engines/zcode/connection.ts +598 -0
  213. package/src/execution/engine/engines/zcode/constants.ts +147 -14
  214. package/src/execution/engine/engines/zcode/golden-sample.ts +42 -34
  215. package/src/execution/engine/engines/zcode/parser.ts +12 -346
  216. package/src/execution/engine/engines/zcode/preparer.ts +22 -150
  217. package/src/execution/engine/engines/zcode/session-channel.ts +968 -0
  218. package/src/execution/engine/engines/zcode/zcode-engine.ts +1023 -250
  219. package/src/execution/engine/host-task-spec.ts +32 -36
  220. package/src/execution/engine/model-validation.ts +177 -0
  221. package/src/execution/engine/port.ts +65 -7
  222. package/src/execution/engine/registry.ts +53 -0
  223. package/src/execution/engine/routing.ts +75 -1
  224. package/src/execution/engine/types.ts +30 -103
  225. package/src/execution/execution-record.ts +222 -144
  226. package/src/execution/finalize-record.ts +181 -85
  227. package/src/execution/idle-gc.ts +28 -0
  228. package/src/execution/lifecycle-manager.ts +27 -3
  229. package/src/execution/lifecycle-predicates.ts +1 -1
  230. package/src/execution/manifest-store.ts +53 -62
  231. package/src/execution/notifier.ts +10 -10
  232. package/src/execution/notify-host.ts +212 -0
  233. package/src/execution/notify-ledger.ts +117 -16
  234. package/src/execution/record-entry.ts +8 -2
  235. package/src/execution/record-store.ts +148 -88
  236. package/src/execution/round-settlement.ts +93 -0
  237. package/src/execution/session-file-gc.ts +96 -62
  238. package/src/execution/session-pending.ts +213 -62
  239. package/src/execution/session-reconstructor.ts +256 -143
  240. package/src/execution/sessions-index.ts +127 -102
  241. package/src/execution/settled-watchdog.ts +311 -0
  242. package/src/execution/subagent-actions-core.ts +686 -0
  243. package/src/execution/subagent-service.ts +1106 -907
  244. package/src/execution/subprocess-agent-runner.ts +144 -97
  245. package/src/execution/types.ts +9 -0
  246. package/src/execution/ui-request-handler-factory.ts +126 -75
  247. package/src/execution/ui-request-queue.ts +50 -19
  248. package/src/execution/worktree-git-ops.ts +397 -0
  249. package/src/execution/worktree-manager.ts +72 -10
  250. package/src/execution/worktree-registry.ts +6 -12
  251. package/src/index.ts +542 -9
  252. package/src/orchestration/__tests__/__fixtures__/worker-template.snapshot.txt +42 -3
  253. package/src/orchestration/__tests__/agent-call-catch-fallback.test.ts +3 -5
  254. package/src/orchestration/__tests__/agent-call-stream.test.ts +2 -2
  255. package/src/orchestration/__tests__/args-meta.test.ts +358 -0
  256. package/src/orchestration/__tests__/args-validator.test.ts +14 -0
  257. package/src/orchestration/__tests__/error-recovery-rebuild-failure.test.ts +312 -0
  258. package/src/orchestration/__tests__/error-recovery-terminal-hardening.test.ts +384 -0
  259. package/src/orchestration/__tests__/execute-agent-call.test.ts +151 -11
  260. package/src/orchestration/__tests__/file-run-store-prune.test.ts +168 -0
  261. package/src/orchestration/__tests__/file-run-store-throttle.test.ts +168 -0
  262. package/src/orchestration/__tests__/file-run-store.test.ts +390 -0
  263. package/src/orchestration/__tests__/helpers/flush-microtasks.ts +13 -0
  264. package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +34 -0
  265. package/src/orchestration/__tests__/lifecycle-abort-broadcast-signal.test.ts +357 -0
  266. package/src/orchestration/__tests__/lifecycle-recover-crashed.test.ts +289 -0
  267. package/src/orchestration/__tests__/lifecycle-runid-injection.test.ts +3 -3
  268. package/src/orchestration/__tests__/lifecycle.test.ts +84 -5
  269. package/src/orchestration/__tests__/non-cloneable-return-e2e.test.ts +1 -1
  270. package/src/orchestration/__tests__/run-snapshot.test.ts +356 -0
  271. package/src/orchestration/__tests__/script-generate.test.ts +314 -0
  272. package/src/orchestration/__tests__/script-lint.test.ts +17 -0
  273. package/src/orchestration/__tests__/test-mocks.ts +2 -0
  274. package/src/orchestration/__tests__/worker-exit-without-result.test.ts +7 -6
  275. package/src/orchestration/__tests__/worker-message-pump-finalize-run.test.ts +229 -0
  276. package/src/orchestration/__tests__/{error-recovery-handlers.test.ts → worker-message-pump-handlers.test.ts} +28 -27
  277. package/src/orchestration/__tests__/{error-recovery-postmessage-defense.test.ts → worker-message-pump-postmessage-defense.test.ts} +8 -8
  278. package/src/orchestration/__tests__/{error-recovery-serialize-failed-result.test.ts → worker-message-pump-serialize-failed-result.test.ts} +2 -2
  279. package/src/orchestration/__tests__/{error-recovery-workflow-call.test.ts → worker-message-pump-workflow-call.test.ts} +1 -1
  280. package/src/orchestration/__tests__/worker-pending-timeout-abort.test.ts +275 -0
  281. package/src/orchestration/__tests__/worker-script-builder-runtime.test.ts +78 -1
  282. package/src/orchestration/__tests__/worker-script-builder.test.ts +1 -1
  283. package/src/orchestration/__tests__/workflow-files.test.ts +186 -0
  284. package/src/orchestration/__tests__/workflow-nesting-e2e.test.ts +1 -1
  285. package/src/orchestration/__tests__/workflow-run-summary.test.ts +119 -0
  286. package/src/orchestration/__tests__/workflow-script-registry-impl.test.ts +124 -0
  287. package/src/orchestration/agent-opts-resolver.ts +8 -8
  288. package/src/orchestration/args-meta.ts +198 -0
  289. package/src/orchestration/args-validator.ts +53 -31
  290. package/src/orchestration/execute-agent-call.ts +25 -79
  291. package/src/orchestration/file-run-store.ts +327 -0
  292. package/src/orchestration/launcher.ts +160 -104
  293. package/src/orchestration/lifecycle.ts +371 -97
  294. package/src/orchestration/models/agent-call.ts +7 -7
  295. package/src/orchestration/models/budget.ts +5 -5
  296. package/src/orchestration/models/ports.ts +4 -4
  297. package/src/orchestration/models/run-runtime.ts +15 -15
  298. package/src/orchestration/models/trace.ts +9 -9
  299. package/src/orchestration/models/types.ts +111 -22
  300. package/src/orchestration/models/workflow-run.ts +28 -28
  301. package/src/orchestration/models/workflow-script.ts +4 -4
  302. package/src/orchestration/run-snapshot.ts +266 -0
  303. package/src/orchestration/script-generate.ts +154 -0
  304. package/src/orchestration/script-lint.ts +123 -90
  305. package/src/orchestration/worker-handle.ts +10 -10
  306. package/src/orchestration/worker-host.ts +10 -10
  307. package/src/orchestration/{error-recovery.ts → worker-message-pump.ts} +498 -221
  308. package/src/orchestration/worker-script-builder.ts +401 -346
  309. package/src/orchestration/workflow-files.ts +37 -11
  310. package/src/orchestration/workflow-run-summary.ts +69 -0
  311. package/src/orchestration/workflow-script-registry-impl.ts +31 -9
  312. package/src/shared/__tests__/agent-ref.test.ts +209 -2
  313. package/src/shared/__tests__/atomic-write.test.ts +267 -0
  314. package/src/shared/__tests__/bounded-serialize.test.ts +236 -0
  315. package/src/shared/__tests__/injection-render.test.ts +518 -0
  316. package/src/shared/__tests__/meta-parser.test.ts +151 -0
  317. package/src/shared/__tests__/resource-discovery-host-roots.test.ts +474 -0
  318. package/src/shared/__tests__/resource-discovery.test.ts +261 -217
  319. package/src/shared/agent-ref.ts +143 -2
  320. package/src/shared/atomic-write.ts +323 -0
  321. package/src/shared/bounded-serialize.ts +154 -0
  322. package/src/shared/injection-render.ts +279 -0
  323. package/src/shared/meta-parser.ts +182 -68
  324. package/src/shared/model-ref.ts +15 -2
  325. package/src/shared/resource-discovery.ts +97 -204
  326. package/src/shared/resource-meta.ts +14 -0
  327. package/src/shared/schema-jsonify.ts +1 -1
  328. package/src/shared/xml-injection.ts +9 -9
  329. package/workflows/README.md +9 -9
  330. package/workflows/chain.js +4 -2
  331. package/workflows/map-reduce.js +5 -3
  332. package/workflows/parallel.js +5 -3
  333. package/workflows/review-fix-loop-utils.cjs +165 -92
  334. package/workflows/review-fix-loop-utils.d.cts +287 -0
  335. package/workflows/review-fix-loop.js +18 -7
  336. package/workflows/scatter-gather.js +4 -2
  337. package/dist/chunk-3VOERJPJ.js +0 -22
  338. package/src/execution/__tests__/execute-options-mapper.test.ts +0 -200
  339. package/src/execution/engine/engines/pi/__tests__/task-spec-mapper.test.ts +0 -164
  340. package/src/execution/engine/engines/pi/task-spec-mapper.ts +0 -100
  341. package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-spawn.json +0 -39
  342. package/src/execution/engine/engines/zcode/__tests__/launcher.test.ts +0 -150
  343. package/src/execution/engine/engines/zcode/__tests__/zcode-engine.test.ts +0 -580
  344. package/src/execution/engine/engines/zcode/launcher.ts +0 -161
  345. package/src/execution/execute-options-mapper.ts +0 -115
  346. package/src/execution/session-runner.ts +0 -1789
  347. package/src/shared/__tests__/resource-discovery-manifest-cache.test.ts +0 -288
@@ -46,7 +46,7 @@ export class Budget {
46
46
  readonly maxTimeMs?: number;
47
47
  usedTokens = 0;
48
48
  usedCost = 0;
49
- /** 总调用计数(持久化/诊断用;execute-agent-call 每次 dispatch 后 increment)。 */
49
+ /** 总调用计数(持久化/诊断用;execute-agent-call 每次 dispatch 后 increment)。 */
50
50
  totalCallCount = 0;
51
51
 
52
52
  constructor(opts: {
@@ -65,7 +65,7 @@ export class Budget {
65
65
  this.totalCallCount = opts.totalCallCount ?? 0;
66
66
  }
67
67
 
68
- /**
68
+ /**
69
69
  * 累加一次 agent 调用的 usage(加权口径)。
70
70
  *
71
71
  * 四项 token 按各自权重(INPUT/CACHE_READ/CACHE_WRITE/OUTPUT_WEIGHT)折算后求和,
@@ -84,12 +84,12 @@ export class Budget {
84
84
  this.usedCost += numOrZero(usage.cost);
85
85
  }
86
86
 
87
- /** 累加调用计数(每次 agent dispatch 后调用;持久化快照同步)。 */
87
+ /** 累加调用计数(每次 agent dispatch 后调用;持久化快照同步)。 */
88
88
  incrementCallCount(): void {
89
89
  this.totalCallCount += 1;
90
90
  }
91
91
 
92
- /**
92
+ /**
93
93
  * 是否超 token / cost 预算(FR-3)。
94
94
  *
95
95
  * maxTokens===0 或 undefined 视为不限制(守卫);
@@ -105,7 +105,7 @@ export class Budget {
105
105
  return this.maxCost !== undefined && this.maxCost > 0 && this.usedCost >= this.maxCost;
106
106
  }
107
107
 
108
- /**
108
+ /**
109
109
  * 剩余 token 预算。maxTokens 未设或 ≤0 时返回 undefined(视为不限制)。
110
110
  *
111
111
  * 嵌套 workflow() 调用时由 executeNestedWorkflow 消费:子 run 的 budgetTokens
@@ -5,7 +5,7 @@
5
5
  * 是真需要 mock 测试的依赖(子进程/文件系统/线程)。
6
6
  *
7
7
  * 编排层共享类型(WorkerHandlers / LifecycleDeps)——打破 lifecycle ↔
8
- * error-recovery 循环依赖:2 个 engine 函数文件各自独立,共用同一组
8
+ * worker-message-pump 循环依赖:2 个 engine 函数文件各自独立,共用同一组
9
9
  * 依赖签名(D-12)。
10
10
  *
11
11
  * 层归属:Engine。零 infra 依赖(AC-1)。
@@ -71,7 +71,7 @@ export interface WorkerHost {
71
71
 
72
72
  /**
73
73
  * Worker 线程事件回调集合——WorkerHost.start 的入参,由 lifecycle
74
- * 构造并注入。2 个 engine 文件(lifecycle / error-recovery)共用此签名,
74
+ * 构造并注入。2 个 engine 文件(lifecycle / worker-message-pump)共用此签名,
75
75
  * 避免各自定义形状不一致的 handler bag(打破循环依赖)。
76
76
  *
77
77
  * 所有回调返回 Promise——允许 engine 层在回调内做 await persistState 等异步操作。
@@ -88,7 +88,7 @@ export interface WorkerHandlers {
88
88
  // ── 编排层共享类型 2: LifecycleDeps ────────────────────────────
89
89
 
90
90
  /**
91
- * lifecycle / error-recovery 2 个 engine 函数文件的共同依赖 bag。
91
+ * lifecycle / worker-message-pump 2 个 engine 函数文件的共同依赖 bag。
92
92
  *
93
93
  * 取代旧 4 个 Context factory(errorHandlerContext / agentCallContext /
94
94
  * budgetCallbacks / 旧 terminate bag,AC-2 目标)。函数签名 `(deps: LifecycleDeps, ...)`
@@ -142,7 +142,7 @@ export interface LifecycleDeps {
142
142
  * workflow() 嵌套调用回调(可选)。Worker 脚本内调 workflow(name, args) 时触发。
143
143
  *
144
144
  * 由 Interface 层 makeDeps 注入(闭包捕获 registry + deps)。Engine 层的
145
- * error-recovery.handleWorkerMessage 收到 workflow-call 消息后调本回调,
145
+ * worker-message-pump.handleWorkerMessage 收到 workflow-call 消息后调本回调,
146
146
  * 拿到子 workflow 执行结果后 postMessage(workflow-result) 回 worker。
147
147
  *
148
148
  * 不注入时 workflow() 返回 error result(向后兼容,不影响非嵌套场景)。
@@ -7,11 +7,11 @@
7
7
  * 职责:封装一次 running-segment 的所有技术资源(worker 线程 +
8
8
  * abort controller),统一 release 入口(AC-2:单 release 替代多 boolean flag)。
9
9
  * (旧并发门闩 gate 抽象已删——no-op,实际并发由 SubagentService ConcurrencyPool 管理;
10
- * 原 withSlot 的 pre-abort 检查内联到 error-recovery dispatchAgentCall。)
10
+ * 原 withSlot 的 pre-abort 检查内联到 worker-message-pump dispatchAgentCall。)
11
11
  *
12
12
  * 一次性生命周期(G3-001):runtime 释放后不再复用——AbortController 一次性
13
13
  * 语义决定 controller 无法跨释放复用,所以整个 RunRuntime 重建。唯一注入路径:
14
- * assignRuntime(runWorkflow 创建)与 replaceRuntime(error-recovery 崩溃重试)。
14
+ * assignRuntime(runWorkflow 创建)与 replaceRuntime(worker-message-pump 崩溃重试)。
15
15
  *
16
16
  * 参考:domain-models.md §10、clarification.md G3-001。
17
17
  */
@@ -30,15 +30,15 @@ export type ReleaseMode = "terminal";
30
30
  // ── RunRuntime ───────────────────────────────────────────────
31
31
 
32
32
  export class RunRuntime {
33
- /** Worker 线程句柄。 */
33
+ /** Worker 线程句柄。 */
34
34
  readonly worker: WorkerHandle;
35
- /** per-running-segment AbortController(一次性,无法复用——G3-001)。 */
35
+ /** per-running-segment AbortController(一次性,无法复用——G3-001)。 */
36
36
  readonly controller: AbortController;
37
- /** Run 级墙钟时间预算计时器(spec.budgetTimeMs > 0 时由 lifecycle 调度,
37
+ /** Run 级墙钟时间预算计时器(spec.budgetTimeMs > 0 时由 lifecycle 调度,
38
38
  * 到期 abortRun time_limited)。release 时清理,避免 abort/replaceRuntime
39
39
  * 后孤儿计时器仍触发(rebuildRuntime 会重排一个全新的计时器,旧的不应残留)。 */
40
40
  readonly timeBudgetTimer?: ReturnType<typeof setTimeout>;
41
- /**
41
+ /**
42
42
  * 本 runtime 代际是否已收到 worker 的终态消息(return / error)。
43
43
  *
44
44
  * [F1] worker exit(0) 且本标记为 false = worker 静默退出、未交付任何终态——最常见根因
@@ -57,7 +57,7 @@ export class RunRuntime {
57
57
  * rebuildRuntime 构造新 RunRuntime 自然重置。
58
58
  */
59
59
  receivedTerminalMessage = false;
60
- /** 防止 release 重复执行(幂等)。 */
60
+ /** 防止 release 重复执行(幂等)。 */
61
61
  private released = false;
62
62
 
63
63
  constructor(
@@ -70,7 +70,7 @@ export class RunRuntime {
70
70
  this.timeBudgetTimer = timeBudgetTimer;
71
71
  }
72
72
 
73
- /**
73
+ /**
74
74
  * 释放所有资源:terminate worker + abort controller。
75
75
  *
76
76
  * 幂等——重复调用安全(第二次起 no-op,released flag 守卫)。
@@ -86,18 +86,18 @@ export class RunRuntime {
86
86
  release(_mode: ReleaseMode): void {
87
87
  if (this.released) return;
88
88
  this.released = true;
89
- // 清理 run 级时间预算计时器——abort/terminate/replaceRuntime 后它不应再触发
90
- // (rebuildRuntime 会重排全新计时器;孤儿触发会把已终态的 run 误转 done)。
89
+ // 清理 run 级时间预算计时器——abort/terminate/replaceRuntime 后它不应再触发
90
+ // (rebuildRuntime 会重排全新计时器;孤儿触发会把已终态的 run 误转 done)。
91
91
  if (this.timeBudgetTimer) clearTimeout(this.timeBudgetTimer);
92
- // worker.terminate 异步但幂等——不 await(release 是同步签名,调用方
93
- // 不应被底层线程关闭阻塞;worker 收到 terminate 后自行清理)。
92
+ // worker.terminate 异步但幂等——不 await(release 是同步签名,调用方
93
+ // 不应被底层线程关闭阻塞;worker 收到 terminate 后自行清理)。
94
94
  void this.worker.terminate();
95
- // controller.abort 触发 listener(kill agent subprocess、中止在飞调用)。
96
- // 一次性语义——已 aborted 的 controller 重复 abort 无副作用。
95
+ // controller.abort 触发 listener(kill agent subprocess、中止在飞调用)。
96
+ // 一次性语义——已 aborted 的 controller 重复 abort 无副作用。
97
97
  this.controller.abort();
98
98
  }
99
99
 
100
- /** 是否已 release(测试 + 诊断用)。 */
100
+ /** 是否已 release(测试 + 诊断用)。 */
101
101
  get isReleased(): boolean {
102
102
  return this.released;
103
103
  }
@@ -75,7 +75,7 @@ export class Trace {
75
75
  /** stepIndex 倒排索引(查询加速 O(1);值与 nodes 元素引用共享)。 */
76
76
  private readonly byIndex = new Map<number, ExecutionTraceNode>();
77
77
 
78
- /**
78
+ /**
79
79
  * 从已有节点数组重建 Trace(用于 RunStore 反序列化重水合)。
80
80
  *
81
81
  * 防御性拷贝——传入数组不被持有,外部 mutation 不影响 Trace。
@@ -96,7 +96,7 @@ export class Trace {
96
96
  return trace;
97
97
  }
98
98
 
99
- /**
99
+ /**
100
100
  * Append a trace node(append-only,不改已有节点)。
101
101
  *
102
102
  * 入口裁剪:超长 result.content 先 mutate 入参节点的 result 字段,
@@ -109,7 +109,7 @@ export class Trace {
109
109
  this.byIndex.set(node.stepIndex, node);
110
110
  }
111
111
 
112
- /**
112
+ /**
113
113
  * Update a trace node by stepIndex (callId) with a partial patch.
114
114
  *
115
115
  * 只改 patch 中提供的字段(status/result/error/completedAt/sessionId)。
@@ -129,7 +129,7 @@ export class Trace {
129
129
  if (patch.sessionFile !== undefined) node.sessionFile = patch.sessionFile;
130
130
  }
131
131
 
132
- /**
132
+ /**
133
133
  * 查找指定 stepIndex 的节点(byIndex O(1),trace 中 stepIndex 应唯一)。
134
134
  *
135
135
  * 语义差异声明(旧线性扫 first-match → Map last-wins):仅在破坏
@@ -145,15 +145,15 @@ export class Trace {
145
145
  return this.byIndex.get(stepIndex);
146
146
  }
147
147
 
148
- /** 按节点引用删除(仅用于测试或 run 重建场景;正常运行不调用)。 */
148
+ /** 按节点引用删除(仅用于测试或 run 重建场景;正常运行不调用)。 */
149
149
  find(stepIndex: number): ExecutionTraceNode | undefined {
150
150
  return this.findByStepIndex(stepIndex);
151
151
  }
152
152
 
153
- /**
153
+ /**
154
154
  * 按 stepIndex 移除节点(崩溃重建清理在飞 call 用)。
155
155
  *
156
- * 正常运行不调用(append-only 不变式)。仅 error-recovery 的 discardInFlightCalls
156
+ * 正常运行不调用(append-only 不变式)。仅 worker-message-pump 的 discardInFlightCalls
157
157
  * (rebuildRuntime 内,F2)清理被旧 runtime abort 的在飞 call 时用——移除其 trace
158
158
  * 节点,让重跑重发 agent-call 时 append 全新节点走全新执行路径(避免 stale
159
159
  * "running" 节点残留 + trace.update 命中旧节点导致新节点 orphan)。
@@ -167,7 +167,7 @@ export class Trace {
167
167
  this.byIndex.delete(stepIndex);
168
168
  }
169
169
 
170
- /**
170
+ /**
171
171
  * readonly 视图——返回内部 nodes 数组引用(仅类型级 readonly,运行时无
172
172
  * 防御)。消费方禁止结构化 mutate(push/splice/重排/覆盖元素):byIndex
173
173
  * 引入后外部结构化 mutate 会使 nodes 与倒排索引 desync。字段级变更走 update()。
@@ -176,7 +176,7 @@ export class Trace {
176
176
  return this.nodes;
177
177
  }
178
178
 
179
- /** 当前节点数。 */
179
+ /** 当前节点数。 */
180
180
  get length(): number {
181
181
  return this.nodes.length;
182
182
  }
@@ -13,7 +13,7 @@
13
13
  * 层归属:Engine(数据结构 + 不变式守卫)。
14
14
  */
15
15
 
16
- import type { ExecutionRecord } from "../../execution/types.ts";
16
+ import type { ExecutionRecord, WorktreeHandle } from "../../execution/types.ts";
17
17
 
18
18
  // ── 状态机 ────────────────────────────────────────────────────
19
19
 
@@ -65,7 +65,37 @@ export function canRunTransition(from: RunStatus, to: RunStatus): boolean {
65
65
  // ── Agent 调用 ────────────────────────────────────────────────
66
66
 
67
67
  /**
68
- * 单次 agent 调用的输入选项。
68
+ * slug 最大长度(D6 合流迁入本文件,原权威定义在已删除的 execution/execute-options-mapper.ts)。
69
+ * 历史值 20 偏紧——描述性 slug 如 "audit-structured-output"(23)/ "fix-subagent-wf-tools"(21)
70
+ * 会撞上限,放宽到 35 兼顾「短到能塞进 TUI 标题行」与「容纳合理描述性 kebab-case 名」。
71
+ * 放本文件的原因:约束对象是 AgentCallOpts.description(slug 的源字段,见下方 slug 派生说明),
72
+ * 与字段同文件;worker-message-pump(live record slug 截断)与壳侧 tool schema maxLength 共享引用。
73
+ */
74
+ export const SLUG_MAX_LENGTH = 35;
75
+
76
+ /**
77
+ * 单次 agent 调用的任务声明(D6 任务形状合流后的单一形状)。
78
+ *
79
+ * [D6 合流裁决] 本类型 = 原 AgentCallOpts(workflow 调用方声明,18 字段)与原
80
+ * AgentTaskSpec(engine 中立任务声明,已删除)的合流形态,从模型脚本 agent() API
81
+ * 到 EnginePort.run 直达 pi 边界一次映射——SAR 链路上的 ExecuteOptions/AgentTaskSpec
82
+ * 中间态消除(设计 docs/design/subagent-dual-track-convergence.md §3.3 D6 / 终态四)。
83
+ *
84
+ * 终态命名按变化轴裁定为 AgentCallOpts,理由:
85
+ * - 字段演进的首要驱动轴是「调用方要表达的任务语义」(agent() API 是唯一生产写入方,
86
+ * 合流字段中 prompt/description/skill/skillPath/schemaEnv/thinkingLevel 等多数派
87
+ * 已是调用方命名);
88
+ * - 原 AgentTaskSpec 的中立重命名层(task/slug/effort/persona)与调用方命名是
89
+ * 形式同构、语义同构的假差异(prompt≡task、slug=description 截断、effort≡thinkingLevel、
90
+ * persona≡skillPath+appendSystemPrompt 平铺),按「消假差异」原则并入调用方命名;
91
+ * - 持久化兼容反向锁定 prompt 形态:jsonl run 快照的 AgentCall.opts 与 worker 消息
92
+ * opts 均以 prompt 字段落盘,改名会破坏旧快照重水合。
93
+ *
94
+ * 引擎中立字段的并入方式(可选字段,原 AgentTaskSpec 独有字段去向):
95
+ * - graceTurns / conversation / idleTimeoutMs / denyTools / permissionMode:可选并入;
96
+ * - persona.agentRef:裁撤(无生产写入方、无消费者——pi 走 agent 字段解析身份);
97
+ * - requires:裁撤(P4 形状预留、无生产写入方;且并入需 import EngineCapabilities
98
+ * 形成 orchestration↔engine 类型环)。将来能力依赖声明下钻时在本形状上加回。
69
99
  *
70
100
  * D-12 仅重组执行编排,AgentCallOpts 形状保持兼容。
71
101
  */
@@ -101,14 +131,20 @@ export interface AgentCallOpts {
101
131
  */
102
132
  timeoutMs?: number;
103
133
  /**
104
- * Turn 上限(turn limiter 用)。
105
- *
106
- * [预算语义对齐] 未传或 <=0 = 不限 turn;此时也不按 turns 估算 spawn watchdog——
107
- * 仅当 env XYZ_SUBAGENT_SPAWN_WATCHDOG_MS 设置时才按绝对时限挂 watchdog(见
108
- * session-runner.resolveSpawnWatchdogMs)。mapToExecuteOptions 原样透传到
109
- * ExecuteOptions.maxTurns 引擎 task-spec → runSpawn。
110
- */
134
+ * Turn 上限(turn limiter 用)。
135
+ *
136
+ * [预算语义对齐] 未传或 <=0 = 不限 turn;此时也不按 turns 估算 spawn watchdog——
137
+ * 仅当 env XYZ_SUBAGENT_SPAWN_WATCHDOG_MS 设置时才按绝对时限挂 watchdog(见
138
+ * session-runner.resolveSpawnWatchdogMs)。pi 边界直出为 ExecuteOptions.maxTurns
139
+ * → runSpawn(D6 合流后无中间映射层)。
140
+ */
111
141
  maxTurns?: number;
142
+ /**
143
+ * Turn limiter 的宽限轮数(原 AgentTaskSpec.graceTurns 并入):超 maxTurns 后
144
+ * 允许继续的轮数(等待在途工具收尾)。pi 引擎消费;workflow agent() 无写入方,
145
+ * chat 域(ExecuteOptions.graceTurns 同名透传)经 host-task-spec 填充。
146
+ */
147
+ graceTurns?: number;
112
148
  /**
113
149
  * Skill name to load (e.g. "code-review"). Resolved to SKILL.md path
114
150
  * and injected via --skill flag in the subprocess.
@@ -129,18 +165,18 @@ export interface AgentCallOpts {
129
165
  */
130
166
  agent?: string;
131
167
  /**
132
- * System prompt injection CONTENT (not file paths).
133
- * Set by agent-opts-resolver: schema structured-output instruction string.
134
- * Agent systemPrompt is NOT included here (handled by resolveIdentity/agentConfig).
135
- * mapToExecuteOptions passes this through to ExecuteOptions.appendSystemPrompt
136
- * (same name/semantics, transparent passthrough).
137
- */
168
+ * System prompt injection CONTENT (not file paths).
169
+ * Set by agent-opts-resolver: schema structured-output instruction string.
170
+ * Agent systemPrompt is NOT included here (handled by resolveIdentity/agentConfig).
171
+ * pi 边界直出为 ExecuteOptions.appendSystemPrompt(同名同义透传,D6 合流后无中间映射层)。
172
+ */
138
173
  appendSystemPrompt?: string[];
139
174
  /**
140
- * Schema JSON for PI_WORKFLOW_SCHEMA env var.
141
- * Set by agent-opts-resolver when opts.schema is present; passed as env var
142
- * to activate the structured-output tool + hook.
143
- */
175
+ * Schema JSON for PI_WORKFLOW_SCHEMA env var.
176
+ * Set by agent-opts-resolver when opts.schema is present (值 = stringifySchemaCached
177
+ * compact,与 schema 派生等值); passed as env var to activate the structured-output
178
+ * tool + hook. pi 边界直出时 schema 派生优先、本字段兜底(解耦形态通道,生产不可达)。
179
+ */
144
180
  schemaEnv?: string;
145
181
  /**
146
182
  * Per-call 工作目录(ADR-029 决策 1)。传给 child_process.spawn 的 cwd option。
@@ -159,11 +195,36 @@ export interface AgentCallOpts {
159
195
  * agent() → execute-agent-call → SAR 路由层。
160
196
  */
161
197
  engine?: string;
162
- /** Filesystem isolation: when true, creates a new git worktree for the agent. Independent of fork. */
163
- worktree?: boolean;
198
+ /** Filesystem isolation: when true, creates a new git worktree for the agent. Independent of fork.
199
+ * [D6 合流] 类型扩展为 boolean | WorktreeHandle(原 AgentTaskSpec.worktree 的超集)——
200
+ * WorktreeHandle 形态仅在 chat 域(复用外部已创建的 worktree)出现,workflow agent()
201
+ * 恒传 boolean。 */
202
+ worktree?: boolean | WorktreeHandle;
164
203
  /** When true, agent() resolves {value, sessionFile, worktreePath, error} instead of a bare value.
165
- * Worker-layer flag only — not forwarded to ExecuteOptions (mapToExecuteOptions drops it). */
204
+ * Worker-layer flag only — not consumed by any engine (dropped at the pi boundary). */
166
205
  returnMeta?: boolean;
206
+ /**
207
+ * 可持续对话模式(原 AgentTaskSpec.conversation 并入):true = record 标记 chatMode,
208
+ * 轮次完成进 idle 态等待 message 续聊。chat 域(ExecuteOptions.conversation 同名)经
209
+ * host-task-spec 填充;workflow agent() 无写入方。
210
+ */
211
+ conversation?: boolean;
212
+ /**
213
+ * 空闲超时毫秒数(原 AgentTaskSpec.idleTimeoutMs 并入,仅 conversation 模式有意义)。
214
+ * 优先级:参数 > env XYZ_SUBAGENT_IDLE_TIMEOUT_MS > 默认 300000ms;显式 0/负 = 禁用
215
+ * idle GC。chat 域经 host-task-spec 填充。
216
+ */
217
+ idleTimeoutMs?: number;
218
+ /**
219
+ * 工具 denylist(原 AgentTaskSpec.denyTools 并入,中立新增面):各引擎做语法映射
220
+ * (zcode buildZcodeArgv 消费;pi 链路暂无对应面)。无 workflow 写入方,预留形状。
221
+ */
222
+ denyTools?: string[];
223
+ /**
224
+ * 中立权限模式(原 AgentTaskSpec.permissionMode 并入,预留形状):映射按各引擎
225
+ * capabilities.permissionMode。无生产写入方/消费者。
226
+ */
227
+ permissionMode?: string;
167
228
  }
168
229
 
169
230
  /**
@@ -189,6 +250,24 @@ export interface ToolCallEntry {
189
250
  input: string;
190
251
  }
191
252
 
253
+ /**
254
+ * 失败分诊结构化标签(D5-③,r1 MF4 钉正语义)。
255
+ *
256
+ * - stale_context:pi session context 被 compact/cancel/替换——重试无意义(同 call
257
+ * 再次失败),不重试。
258
+ * - schema_deterministic:确定性 schema 失败(SO tool 从未调用 / gate 终止 /
259
+ * 不可满足 schema)——同 schema 重试必同结果,不重试。
260
+ * - unknown:其余一切失败(瞬态 provider 错误、spawn 失败等)——**默认可重试**。
261
+ *
262
+ * **语义守恒(最高优先约束)**:unknown(含字段缺省)= 可重试——保持收敛前的
263
+ * 默认重试语义;仅 stale_context 与 schema_deterministic 两态维持不重试特判。
264
+ * 词表归属(产出侧单点识别):stale_context / schema_deterministic 的识别词表
265
+ * (stale 词表与确定性 schema 失败标记前缀)保留在产出侧
266
+ * execution/engine/engines/pi/output-collector.ts 包内——词表漂移的失效模式是
267
+ * failureKind=unknown → 保守重试(安全默认),不再是静默漏诊。
268
+ */
269
+ export type AgentFailureKind = "stale_context" | "schema_deterministic" | "unknown";
270
+
192
271
  /**
193
272
  * 单次 agent 调用的结果(统一形态)。
194
273
  *
@@ -199,6 +278,16 @@ export interface AgentResult {
199
278
  /** Raw text output from the agent. */
200
279
  content: string;
201
280
  /**
281
+ * [D5-③] 失败分诊结构化标签(AgentFailureKind)。产出侧唯一识别点 =
282
+ * execution/engine/engines/pi/output-collector.ts(collectResult 对最终 error
283
+ * 分类后写入,经 agent-result-mapper / AgentOutcome 透传到本形态);消费侧
284
+ * execute-agent-call 读本字段分诊,不再扫 error 文案子串。
285
+ *
286
+ * 仅在 error !== undefined 时有意义(成功时缺省);缺省视为 unknown = 可重试
287
+ * (语义守恒,见 AgentFailureKind)。
288
+ */
289
+ failureKind?: AgentFailureKind;
290
+ /**
202
291
  * Parsed structured output.
203
292
  * Present when `schema` was provided and the output was valid JSON.
204
293
  * Source: tool_execution_end.result.details(validated data object)。
@@ -39,7 +39,7 @@ import { canRunTransition } from "./types.ts";
39
39
  /**
40
40
  * 聚合根级 meta(非 RunState 的一部分,不随 trace 持久化到 worker JSONL)。
41
41
  *
42
- * workerErrorCount/scriptErrorCount 跨 runtime 存活(C.5:error-recovery 重试计数载体),
42
+ * workerErrorCount/scriptErrorCount 跨 runtime 存活(C.5:worker-message-pump 重试计数载体),
43
43
  * 因为 retry 会 replaceRuntime,但计数是 run 级而非 runtime 级。
44
44
  */
45
45
  export interface WorkflowRunMeta {
@@ -62,7 +62,7 @@ export class WorkflowRun {
62
62
  runtime?: RunRuntime;
63
63
  meta: WorkflowRunMeta;
64
64
 
65
- /**
65
+ /**
66
66
  * 创建聚合根。初始状态 "running"(一次性生命周期:run 从创建起即在执行,
67
67
  * runtime 由紧随其后的 assignRuntime 注入)。也可传入 done 状态用于重水合
68
68
  * 已完成的 run(loadAll 后的只读聚合)。
@@ -82,15 +82,15 @@ export class WorkflowRun {
82
82
  this.spec = spec;
83
83
  this.state = state;
84
84
  this.meta = meta;
85
- // runtime 在构造时始终为 undefined——run 创建时无活 worker,loadAll 重水合
86
- // 时也不恢复 runtime(worker 必须由 lifecycle 重新 start)。
85
+ // runtime 在构造时始终为 undefined——run 创建时无活 worker,loadAll 重水合
86
+ // 时也不恢复 runtime(worker 必须由 lifecycle 重新 start)。
87
87
  this.runtime = undefined;
88
- // 构造期仅校验 I2(I1 跳过,见方法 doc);I1 由 assignRuntime 末尾
89
- // validateInvariants 恢复。
88
+ // 构造期仅校验 I2(I1 跳过,见方法 doc);I1 由 assignRuntime 末尾
89
+ // validateInvariants 恢复。
90
90
  this.validateInvariantI2();
91
91
  }
92
92
 
93
- /**
93
+ /**
94
94
  * 从持久化快照重水合聚合根。与构造函数同语义(构造期跳过 I1——持久化的
95
95
  * running 状态没有 worker,进程被杀后 worker 不可能还活着)。保留独立工厂
96
96
  * 标注重水合意图;调用方(D-4 kill-9 恢复)负责在 session_start 时把残留
@@ -102,15 +102,15 @@ export class WorkflowRun {
102
102
  return new WorkflowRun(runId, spec, state, meta);
103
103
  }
104
104
 
105
- // ── 不变式校验 ─────────────────────────────────────────────
105
+ // ── 不变式校验 ─────────────────────────────────────────────
106
106
 
107
- /**
107
+ /**
108
108
  * 校验不变式 I1 + I2。违反抛错(聚合根自我保护,fail-fast)。
109
109
  * 在每个 mutation 方法末尾调用(防御式编程 + 测试可断言)。
110
110
  */
111
111
  private validateInvariants(): void {
112
112
  this.validateInvariantI2();
113
- // I1: status==="running" ⟺ runtime!==undefined
113
+ // I1: status==="running" ⟺ runtime!==undefined
114
114
  if (this.state.status === "running" && this.runtime === undefined) {
115
115
  throw new Error(
116
116
  `WorkflowRun invariant I1 violated: status==="running" but runtime is undefined (runId=${this.runId})`,
@@ -123,7 +123,7 @@ export class WorkflowRun {
123
123
  }
124
124
  }
125
125
 
126
- /**
126
+ /**
127
127
  * 仅校验不变式 I2(done ⟹ reason)。构造期用——「创建即 running」与重水合的
128
128
  * running 快照都无 runtime(I1 构造期跳过),但 I2 必须保证(done 缺 reason 是真 bug)。
129
129
  */
@@ -135,9 +135,9 @@ export class WorkflowRun {
135
135
  }
136
136
  }
137
137
 
138
- // ── 状态机转换 ─────────────────────────────────────────────
138
+ // ── 状态机转换 ─────────────────────────────────────────────
139
139
 
140
- /**
140
+ /**
141
141
  * 状态机转换。合法转换:running→done。
142
142
  *
143
143
  * running 的进入不走 transition——构造即 running,replaceRuntime 保持 running。
@@ -151,7 +151,7 @@ export class WorkflowRun {
151
151
  * @throws 非法转换 / done 缺 reason / target==="running"
152
152
  */
153
153
  transition(target: RunStatus, reason?: DoneReason): void {
154
- // "running" 必须经 assignRuntime(需 runtime 参数,transition 无法提供)
154
+ // "running" 必须经 assignRuntime(需 runtime 参数,transition 无法提供)
155
155
  if (target === "running") {
156
156
  throw new Error(
157
157
  `WorkflowRun.transition: cannot transition to "running" directly — use assignRuntime() (runId=${this.runId})`,
@@ -164,15 +164,15 @@ export class WorkflowRun {
164
164
  );
165
165
  }
166
166
 
167
- // →done 需 reason(不变式 I2)
167
+ // →done 需 reason(不变式 I2)
168
168
  if (target === "done" && reason === undefined) {
169
169
  throw new Error(
170
170
  `WorkflowRun.transition: transition to "done" requires a reason (runId=${this.runId})`,
171
171
  );
172
172
  }
173
173
 
174
- // 副作用:先清理 runtime(releaseRuntime 守不变式 I1),再改 status
175
- // (canRunTransition 已排除 target==="running",此处 target 恒为 "done")
174
+ // 副作用:先清理 runtime(releaseRuntime 守不变式 I1),再改 status
175
+ // (canRunTransition 已排除 target==="running",此处 target 恒为 "done")
176
176
  this.releaseRuntime();
177
177
  this.state.status = target;
178
178
  this.state.reason = reason;
@@ -181,9 +181,9 @@ export class WorkflowRun {
181
181
  this.validateInvariants();
182
182
  }
183
183
 
184
- // ── Runtime 生命周期 ───────────────────────────────────────
184
+ // ── Runtime 生命周期 ───────────────────────────────────────
185
185
 
186
- /**
186
+ /**
187
187
  * 绑定 runtime(run 创建后注入执行资源)。
188
188
  *
189
189
  * 前置:status==="running" && runtime===undefined(runWorkflow 创建路径——
@@ -204,14 +204,14 @@ export class WorkflowRun {
204
204
  `WorkflowRun.assignRuntime: requires status==="running" (current: ${this.state.status}, runId=${this.runId})`,
205
205
  );
206
206
  }
207
- // 原子绑定:构造期 I1 处于跳过窗口(running 而 runtime undefined),设 runtime
208
- // 后末尾 validateInvariants 恢复 I1。调用方在 assignRuntime 后才对外注册
209
- // (lifecycle.runWorkflow 的 runs.set 后移),窗口外部不可见。
207
+ // 原子绑定:构造期 I1 处于跳过窗口(running 而 runtime undefined),设 runtime
208
+ // 后末尾 validateInvariants 恢复 I1。调用方在 assignRuntime 后才对外注册
209
+ // (lifecycle.runWorkflow 的 runs.set 后移),窗口外部不可见。
210
210
  this.runtime = rt;
211
211
  this.validateInvariants();
212
212
  }
213
213
 
214
- /**
214
+ /**
215
215
  * 解绑 runtime(done 时由 transition 调用,也可独立调用)。
216
216
  *
217
217
  * 前置:无(runtime===undefined 时 no-op,幂等)。
@@ -221,11 +221,11 @@ export class WorkflowRun {
221
221
  if (this.runtime === undefined) return;
222
222
  this.runtime.release("terminal");
223
223
  this.runtime = undefined;
224
- // 不改 status——调用方(transition)负责。独立调用时调用方需自行确保
225
- // status 一致(如 worker-error-retry 用 replaceRuntime 而非 release+assign)。
224
+ // 不改 status——调用方(transition)负责。独立调用时调用方需自行确保
225
+ // status 一致(如 worker-error-retry 用 replaceRuntime 而非 release+assign)。
226
226
  }
227
227
 
228
- /**
228
+ /**
229
229
  * 原地替换 runtime(G5-001:worker-error-retry)。
230
230
  *
231
231
  * 前置:status==="running"(G6-001:终态 run 拒绝重建)。
@@ -243,8 +243,8 @@ export class WorkflowRun {
243
243
  `WorkflowRun.replaceRuntime: requires status==="running" (current: ${this.state.status}, runId=${this.runId})`,
244
244
  );
245
245
  }
246
- // 原子替换:旧 runtime 释放(terminate+abort),新 runtime 绑定。
247
- // status 保持 "running",runtime 全程 !== undefined,I1 不违反。
246
+ // 原子替换:旧 runtime 释放(terminate+abort),新 runtime 绑定。
247
+ // status 保持 "running",runtime 全程 !== undefined,I1 不违反。
248
248
  if (this.runtime !== undefined) {
249
249
  this.runtime.release("terminal");
250
250
  }
@@ -60,10 +60,10 @@ export class WorkflowScript {
60
60
  readonly name: string;
61
61
  readonly source: WorkflowSource;
62
62
  readonly path: string;
63
- /** 原始文件内容(可编辑)。toExecutable 返回 strip 后的副本,不改本字段。 */
63
+ /** 原始文件内容(可编辑)。toExecutable 返回 strip 后的副本,不改本字段。 */
64
64
  sourceCode: string;
65
65
  readonly meta: WorkflowMeta;
66
- /** false 当 meta 提取失败(loader 不抛错,标记不可用但仍列出)。 */
66
+ /** false 当 meta 提取失败(loader 不抛错,标记不可用但仍列出)。 */
67
67
  available: boolean;
68
68
 
69
69
  constructor(opts: {
@@ -82,7 +82,7 @@ export class WorkflowScript {
82
82
  this.available = opts.available;
83
83
  }
84
84
 
85
- /**
85
+ /**
86
86
  * 静态检查脚本合法性。
87
87
  *
88
88
  * 委托 engine/script-lint.ts 的 lintScript——检查项含:
@@ -104,7 +104,7 @@ export class WorkflowScript {
104
104
  return result;
105
105
  }
106
106
 
107
- /**
107
+ /**
108
108
  * 返回可执行源。
109
109
  *
110
110
  * m2:不再 strip `export const meta`——meta 现为 @pi-meta 块注释(合法 JS,