@zhushanwen/pi-subagent-workflow 8.6.0 → 8.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 (347) hide show
  1. package/package.json +9 -13
  2. package/relay/relay.mjs +1 -1
  3. package/src/host/__tests__/pi-host.test.ts +214 -0
  4. package/src/host/pi-host.ts +109 -0
  5. package/src/index.ts +89 -32
  6. package/src/injectors/__tests__/engine-awareness.test.ts +292 -0
  7. package/src/injectors/__tests__/engine-section-stability.test.ts +341 -0
  8. package/src/injectors/__tests__/subagent-list-injector.test.ts +5 -5
  9. package/src/injectors/__tests__/workflow-list-injector.test.ts +5 -5
  10. package/src/injectors/engine-awareness.ts +123 -0
  11. package/src/injectors/model-list-injector.ts +1 -1
  12. package/src/injectors/subagent-list-injector.ts +8 -8
  13. package/src/injectors/workflow-list-injector.ts +8 -8
  14. package/src/interface/__tests__/detectors.test.ts +3 -3
  15. package/src/interface/__tests__/subagent-tool-path-guard.test.ts +2 -2
  16. package/src/interface/__tests__/tool-workflow-script-generate.test.ts +2 -2
  17. package/src/interface/__tests__/tool-workflow-throw-paths.test.ts +1 -1
  18. package/src/interface/bg-notify-render.ts +4 -4
  19. package/src/interface/commands.ts +3 -3
  20. package/src/interface/format.ts +2 -2
  21. package/src/interface/helpers.ts +1 -1
  22. package/src/interface/list-component.ts +4 -4
  23. package/src/interface/list-shared.ts +2 -2
  24. package/src/interface/list-view.ts +2 -2
  25. package/src/interface/subagent-actions.ts +263 -11
  26. package/src/interface/subagent-tool-schema.ts +20 -13
  27. package/src/interface/subagent-tool.ts +11 -6
  28. package/src/interface/subagents.ts +3 -3
  29. package/src/interface/tool-render.ts +34 -2
  30. package/src/interface/tool-workflow-script.ts +4 -4
  31. package/src/interface/tool-workflow.ts +6 -6
  32. package/src/interface/views/WorkflowsView.ts +5 -5
  33. package/src/interface/views/__tests__/WorkflowsView-signature.test.ts +3 -3
  34. package/src/interface/views/__tests__/detail-content-session-file.test.ts +2 -2
  35. package/src/interface/views/detail-content.ts +4 -4
  36. package/src/interface/views/format.ts +4 -4
  37. package/src/{orchestration/jsonl-run-store.ts → jsonl-run-store.ts} +11 -11
  38. package/src/execution/__tests__/__fixtures__/notifier-golden-snapshots.json +0 -53
  39. package/src/execution/__tests__/__fixtures__/truncline.snapshot.json +0 -1
  40. package/src/execution/__tests__/agent-registry.test.ts +0 -363
  41. package/src/execution/__tests__/agent-result-mapper.test.ts +0 -150
  42. package/src/execution/__tests__/alive-store.test.ts +0 -147
  43. package/src/execution/__tests__/ask-user-transit-e2e.test.ts +0 -490
  44. package/src/execution/__tests__/before-agent-start-injection.test.ts +0 -132
  45. package/src/execution/__tests__/bg-notify-render.test.ts +0 -329
  46. package/src/execution/__tests__/channel-registry-handshake.test.ts +0 -243
  47. package/src/execution/__tests__/chat-engine-routing.test.ts +0 -601
  48. package/src/execution/__tests__/chatmode-first-round-closure-service.test.ts +0 -365
  49. package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +0 -190
  50. package/src/execution/__tests__/chatmode-round-notify-real-chain.test.ts +0 -215
  51. package/src/execution/__tests__/concurrency-pool.test.ts +0 -250
  52. package/src/execution/__tests__/config.test.ts +0 -110
  53. package/src/execution/__tests__/conversation-wiring.test.ts +0 -198
  54. package/src/execution/__tests__/crash-recovery.test.ts +0 -320
  55. package/src/execution/__tests__/delivery-methods.test.ts +0 -422
  56. package/src/execution/__tests__/dialog-queue.test.ts +0 -299
  57. package/src/execution/__tests__/epipe-fallback.test.ts +0 -241
  58. package/src/execution/__tests__/execute-and-await-worktree.test.ts +0 -243
  59. package/src/execution/__tests__/execute-nesting.test.ts +0 -323
  60. package/src/execution/__tests__/execute-options-mapper.test.ts +0 -200
  61. package/src/execution/__tests__/execution-record.test.ts +0 -1394
  62. package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +0 -171
  63. package/src/execution/__tests__/finalize-record.test.ts +0 -368
  64. package/src/execution/__tests__/finalized-marker.test.ts +0 -82
  65. package/src/execution/__tests__/format-schema-instruction.test.ts +0 -169
  66. package/src/execution/__tests__/format.test.ts +0 -458
  67. package/src/execution/__tests__/gc-timer.test.ts +0 -184
  68. package/src/execution/__tests__/get-record-for-action-restart.test.ts +0 -254
  69. package/src/execution/__tests__/gui-mode-dispatch.test.ts +0 -59
  70. package/src/execution/__tests__/helpers/mock-extension-api.ts +0 -30
  71. package/src/execution/__tests__/helpers/spawn-mock.ts +0 -242
  72. package/src/execution/__tests__/host-mode.test.ts +0 -87
  73. package/src/execution/__tests__/index-session-start-identity.test.ts +0 -371
  74. package/src/execution/__tests__/index-session-start.test.ts +0 -672
  75. package/src/execution/__tests__/lifecycle-manager-lock.test.ts +0 -211
  76. package/src/execution/__tests__/lifecycle-manager.test.ts +0 -383
  77. package/src/execution/__tests__/lifecycle-predicates.test.ts +0 -116
  78. package/src/execution/__tests__/list-component.test.ts +0 -401
  79. package/src/execution/__tests__/list-fields.test.ts +0 -140
  80. package/src/execution/__tests__/manifest-parentid.test.ts +0 -117
  81. package/src/execution/__tests__/model-resolver.test.ts +0 -465
  82. package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +0 -287
  83. package/src/execution/__tests__/nested-visibility.test.ts +0 -325
  84. package/src/execution/__tests__/notifier-flush.test.ts +0 -612
  85. package/src/execution/__tests__/notifier-golden-snapshot.test.ts +0 -261
  86. package/src/execution/__tests__/notify-ledger.test.ts +0 -826
  87. package/src/execution/__tests__/one-shot-upgrade.test.ts +0 -205
  88. package/src/execution/__tests__/output-collector.test.ts +0 -358
  89. package/src/execution/__tests__/parent-child-matrix.test.ts +0 -336
  90. package/src/execution/__tests__/path-encoding.test.ts +0 -104
  91. package/src/execution/__tests__/pi-invocation.test.ts +0 -134
  92. package/src/execution/__tests__/record-store.test.ts +0 -1057
  93. package/src/execution/__tests__/records-cwd-isolation.test.ts +0 -91
  94. package/src/execution/__tests__/recursive-visibility-baseline.test.ts +0 -339
  95. package/src/execution/__tests__/recursive-visibility-env.test.ts +0 -273
  96. package/src/execution/__tests__/relay-agent.test.ts +0 -448
  97. package/src/execution/__tests__/relay-env.test.ts +0 -42
  98. package/src/execution/__tests__/resource-policy.test.ts +0 -109
  99. package/src/execution/__tests__/rpc-mode.test.ts +0 -89
  100. package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +0 -267
  101. package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +0 -253
  102. package/src/execution/__tests__/run-spawn-edges.test.ts +0 -687
  103. package/src/execution/__tests__/run-spawn-integration.test.ts +0 -933
  104. package/src/execution/__tests__/run-spawn-resume.test.ts +0 -322
  105. package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +0 -196
  106. package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +0 -199
  107. package/src/execution/__tests__/sdk-contract.test.ts +0 -273
  108. package/src/execution/__tests__/session-context-resolver.test.ts +0 -167
  109. package/src/execution/__tests__/session-file-gc.test.ts +0 -293
  110. package/src/execution/__tests__/session-pending.test.ts +0 -197
  111. package/src/execution/__tests__/session-reconstructor.test.ts +0 -379
  112. package/src/execution/__tests__/session-runner-epipe.test.ts +0 -178
  113. package/src/execution/__tests__/session-runner-schema-env.test.ts +0 -350
  114. package/src/execution/__tests__/session-start-reaper.test.ts +0 -233
  115. package/src/execution/__tests__/spawn-args.test.ts +0 -445
  116. package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +0 -189
  117. package/src/execution/__tests__/spawn-event-adapter.test.ts +0 -167
  118. package/src/execution/__tests__/spawn-worktree-guidance.test.ts +0 -207
  119. package/src/execution/__tests__/spawned-children.test.ts +0 -92
  120. package/src/execution/__tests__/start-sync-model-guard.test.ts +0 -150
  121. package/src/execution/__tests__/startup-config-declaration.test.ts +0 -35
  122. package/src/execution/__tests__/status-refactor.test.ts +0 -345
  123. package/src/execution/__tests__/stdin-writer.test.ts +0 -463
  124. package/src/execution/__tests__/stream-sink-retirement.test.ts +0 -261
  125. package/src/execution/__tests__/subagent-service-abort.test.ts +0 -60
  126. package/src/execution/__tests__/subagent-service-message-close.test.ts +0 -629
  127. package/src/execution/__tests__/subagent-service-parent-guard.test.ts +0 -180
  128. package/src/execution/__tests__/subagent-service.test.ts +0 -788
  129. package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +0 -310
  130. package/src/execution/__tests__/subprocess-agent-runner.test.ts +0 -599
  131. package/src/execution/__tests__/temp-prompt.test.ts +0 -53
  132. package/src/execution/__tests__/timeout-integration.test.ts +0 -615
  133. package/src/execution/__tests__/tombstone-store.test.ts +0 -73
  134. package/src/execution/__tests__/tool-action.test.ts +0 -476
  135. package/src/execution/__tests__/truncline-snapshot.test.ts +0 -81
  136. package/src/execution/__tests__/turn-limiter-semantics.test.ts +0 -194
  137. package/src/execution/__tests__/turn-limiter.test.ts +0 -65
  138. package/src/execution/__tests__/ui-channels.test.ts +0 -187
  139. package/src/execution/__tests__/ui-interaction-model.test.ts +0 -67
  140. package/src/execution/__tests__/ui-request-handler-factory.test.ts +0 -370
  141. package/src/execution/__tests__/ui-request-handler.test.ts +0 -204
  142. package/src/execution/__tests__/ui-request-observability.test.ts +0 -114
  143. package/src/execution/__tests__/ui-request-queue.test.ts +0 -133
  144. package/src/execution/__tests__/worktree-manager.test.ts +0 -633
  145. package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +0 -229
  146. package/src/execution/__tests__/worktree-reconcile.integration.test.ts +0 -181
  147. package/src/execution/__tests__/worktree-registry.test.ts +0 -199
  148. package/src/execution/agent-registry.ts +0 -199
  149. package/src/execution/agent-result-mapper.ts +0 -90
  150. package/src/execution/alive-store.ts +0 -92
  151. package/src/execution/argv-mirror.ts +0 -113
  152. package/src/execution/best-effort.ts +0 -38
  153. package/src/execution/channel-registry-access.ts +0 -144
  154. package/src/execution/concurrency-pool.ts +0 -116
  155. package/src/execution/config.ts +0 -96
  156. package/src/execution/dialog-queue.ts +0 -330
  157. package/src/execution/engine/__tests__/common/data-dir.test.ts +0 -53
  158. package/src/execution/engine/__tests__/common/errors.test.ts +0 -132
  159. package/src/execution/engine/__tests__/common/event-journal.test.ts +0 -177
  160. package/src/execution/engine/__tests__/common/kill-chain.test.ts +0 -192
  161. package/src/execution/engine/__tests__/common/nesting-guard.test.ts +0 -81
  162. package/src/execution/engine/__tests__/common/persona-router.test.ts +0 -123
  163. package/src/execution/engine/__tests__/common/pool-manager.test.ts +0 -154
  164. package/src/execution/engine/__tests__/common/schema-emulation.test.ts +0 -128
  165. package/src/execution/engine/__tests__/conformance/__fixtures__/pi-golden-events.json +0 -28
  166. package/src/execution/engine/__tests__/conformance/agent-event-invariants.ts +0 -141
  167. package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +0 -109
  168. package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +0 -101
  169. package/src/execution/engine/__tests__/conformance/contract.probe.test.ts +0 -77
  170. package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +0 -104
  171. package/src/execution/engine/__tests__/conformance/contract.relay.test.ts +0 -342
  172. package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +0 -201
  173. package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +0 -76
  174. package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +0 -79
  175. package/src/execution/engine/__tests__/engine-discovery.test.ts +0 -87
  176. package/src/execution/engine/__tests__/engines-declaration.test.ts +0 -36
  177. package/src/execution/engine/__tests__/model-prompt.test.ts +0 -85
  178. package/src/execution/engine/__tests__/paths.test.ts +0 -39
  179. package/src/execution/engine/__tests__/registry.test.ts +0 -120
  180. package/src/execution/engine/__tests__/routing.test.ts +0 -231
  181. package/src/execution/engine/common/data-dir.ts +0 -62
  182. package/src/execution/engine/common/errors.ts +0 -183
  183. package/src/execution/engine/common/event-journal.ts +0 -254
  184. package/src/execution/engine/common/journal-replay.ts +0 -62
  185. package/src/execution/engine/common/kill-chain.ts +0 -221
  186. package/src/execution/engine/common/nesting-guard.ts +0 -50
  187. package/src/execution/engine/common/persona-router.ts +0 -108
  188. package/src/execution/engine/common/pool-manager.ts +0 -226
  189. package/src/execution/engine/common/schema-emulation.ts +0 -189
  190. package/src/execution/engine/common/session-view-projection.ts +0 -51
  191. package/src/execution/engine/engine-discovery.ts +0 -65
  192. package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +0 -469
  193. package/src/execution/engine/engines/pi/__tests__/reader.test.ts +0 -155
  194. package/src/execution/engine/engines/pi/__tests__/task-spec-mapper.test.ts +0 -164
  195. package/src/execution/engine/engines/pi/pi-engine.ts +0 -415
  196. package/src/execution/engine/engines/pi/reader.ts +0 -48
  197. package/src/execution/engine/engines/pi/registration.ts +0 -35
  198. package/src/execution/engine/engines/pi/task-spec-mapper.ts +0 -100
  199. package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-spawn.json +0 -39
  200. package/src/execution/engine/engines/zcode/__tests__/launcher.test.ts +0 -150
  201. package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +0 -246
  202. package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +0 -228
  203. package/src/execution/engine/engines/zcode/__tests__/reader.test.ts +0 -210
  204. package/src/execution/engine/engines/zcode/__tests__/registration.test.ts +0 -64
  205. package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +0 -127
  206. package/src/execution/engine/engines/zcode/__tests__/zcode-engine.test.ts +0 -580
  207. package/src/execution/engine/engines/zcode/constants.ts +0 -43
  208. package/src/execution/engine/engines/zcode/golden-sample.ts +0 -39
  209. package/src/execution/engine/engines/zcode/launcher.ts +0 -161
  210. package/src/execution/engine/engines/zcode/parser.ts +0 -436
  211. package/src/execution/engine/engines/zcode/preparer.ts +0 -363
  212. package/src/execution/engine/engines/zcode/reader.ts +0 -381
  213. package/src/execution/engine/engines/zcode/registration.ts +0 -37
  214. package/src/execution/engine/engines/zcode/zcode-engine.ts +0 -658
  215. package/src/execution/engine/host-task-spec.ts +0 -47
  216. package/src/execution/engine/model-prompt.ts +0 -59
  217. package/src/execution/engine/paths.ts +0 -42
  218. package/src/execution/engine/port.ts +0 -153
  219. package/src/execution/engine/registry.ts +0 -123
  220. package/src/execution/engine/routing.ts +0 -218
  221. package/src/execution/engine/types.ts +0 -309
  222. package/src/execution/execute-options-mapper.ts +0 -112
  223. package/src/execution/execution-record.ts +0 -965
  224. package/src/execution/finalize-record.ts +0 -251
  225. package/src/execution/finalized-marker.ts +0 -51
  226. package/src/execution/get-state-handshake.ts +0 -104
  227. package/src/execution/host-mode.ts +0 -56
  228. package/src/execution/idle-gc.ts +0 -47
  229. package/src/execution/lifecycle-manager.ts +0 -513
  230. package/src/execution/lifecycle-predicates.ts +0 -65
  231. package/src/execution/manifest-store.ts +0 -256
  232. package/src/execution/model-config-service.ts +0 -219
  233. package/src/execution/model-resolver.ts +0 -248
  234. package/src/execution/notifier.ts +0 -330
  235. package/src/execution/notify-ledger.ts +0 -580
  236. package/src/execution/output-collector.ts +0 -228
  237. package/src/execution/path-encoding.ts +0 -60
  238. package/src/execution/pi-invocation.ts +0 -120
  239. package/src/execution/record-entry.ts +0 -132
  240. package/src/execution/record-store.ts +0 -1234
  241. package/src/execution/relay-env.ts +0 -37
  242. package/src/execution/session-context-resolver.ts +0 -64
  243. package/src/execution/session-file-gc.ts +0 -120
  244. package/src/execution/session-pending.ts +0 -164
  245. package/src/execution/session-reconstructor.ts +0 -678
  246. package/src/execution/session-runner.ts +0 -1781
  247. package/src/execution/sessions-index.ts +0 -304
  248. package/src/execution/spawn-event-adapter.ts +0 -363
  249. package/src/execution/stdin-writer.ts +0 -198
  250. package/src/execution/stream-sink.ts +0 -126
  251. package/src/execution/subagent-service.ts +0 -2141
  252. package/src/execution/subprocess-agent-runner.ts +0 -317
  253. package/src/execution/temp-prompt.ts +0 -62
  254. package/src/execution/tombstone-store.ts +0 -72
  255. package/src/execution/turn-limiter.ts +0 -102
  256. package/src/execution/types.ts +0 -973
  257. package/src/execution/ui-channels.ts +0 -216
  258. package/src/execution/ui-interaction-model.ts +0 -48
  259. package/src/execution/ui-request-handler-factory.ts +0 -212
  260. package/src/execution/ui-request-observability.ts +0 -81
  261. package/src/execution/ui-request-queue.ts +0 -184
  262. package/src/execution/worktree-manager.ts +0 -688
  263. package/src/execution/worktree-registry.ts +0 -222
  264. package/src/orchestration/__tests__/__fixtures__/worker-template.snapshot.txt +0 -349
  265. package/src/orchestration/__tests__/agent-call-catch-fallback.test.ts +0 -202
  266. package/src/orchestration/__tests__/agent-call-stream.test.ts +0 -152
  267. package/src/orchestration/__tests__/args-validator.test.ts +0 -143
  268. package/src/orchestration/__tests__/config-loader.test.ts +0 -503
  269. package/src/orchestration/__tests__/error-recovery-handlers.test.ts +0 -809
  270. package/src/orchestration/__tests__/error-recovery-postmessage-defense.test.ts +0 -405
  271. package/src/orchestration/__tests__/error-recovery-serialize-failed-result.test.ts +0 -56
  272. package/src/orchestration/__tests__/error-recovery-workflow-call.test.ts +0 -166
  273. package/src/orchestration/__tests__/execute-agent-call.test.ts +0 -451
  274. package/src/orchestration/__tests__/jsonl-run-store-corrupt-entry.test.ts +0 -150
  275. package/src/orchestration/__tests__/jsonl-run-store-loadall-sources.test.ts +0 -171
  276. package/src/orchestration/__tests__/jsonl-run-store-retention.test.ts +0 -202
  277. package/src/orchestration/__tests__/jsonl-run-store-session-file.test.ts +0 -1030
  278. package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +0 -600
  279. package/src/orchestration/__tests__/lifecycle-runid-injection.test.ts +0 -96
  280. package/src/orchestration/__tests__/lifecycle.test.ts +0 -659
  281. package/src/orchestration/__tests__/non-cloneable-return-e2e.test.ts +0 -95
  282. package/src/orchestration/__tests__/review-fix-loop-e2e.test.ts +0 -2025
  283. package/src/orchestration/__tests__/script-lint.test.ts +0 -831
  284. package/src/orchestration/__tests__/skill-discovery.test.ts +0 -201
  285. package/src/orchestration/__tests__/test-mocks.ts +0 -197
  286. package/src/orchestration/__tests__/worker-exit-without-result.test.ts +0 -368
  287. package/src/orchestration/__tests__/worker-host.test.ts +0 -120
  288. package/src/orchestration/__tests__/worker-returnmeta-passthrough.test.ts +0 -164
  289. package/src/orchestration/__tests__/worker-script-builder-runtime.test.ts +0 -563
  290. package/src/orchestration/__tests__/worker-script-builder.test.ts +0 -309
  291. package/src/orchestration/__tests__/worker-script-template-snapshot.test.ts +0 -129
  292. package/src/orchestration/__tests__/workflow-nesting-e2e.test.ts +0 -317
  293. package/src/orchestration/__tests__/workflow-script-lint-memo.test.ts +0 -110
  294. package/src/orchestration/__tests__/workflows-e2e.test.ts +0 -537
  295. package/src/orchestration/agent-opts-resolver.ts +0 -174
  296. package/src/orchestration/args-validator.ts +0 -127
  297. package/src/orchestration/config-loader.ts +0 -315
  298. package/src/orchestration/error-recovery.ts +0 -1018
  299. package/src/orchestration/execute-agent-call.ts +0 -256
  300. package/src/orchestration/launcher.ts +0 -455
  301. package/src/orchestration/lifecycle.ts +0 -399
  302. package/src/orchestration/models/__tests__/budget.test.ts +0 -307
  303. package/src/orchestration/models/__tests__/trace.test.ts +0 -408
  304. package/src/orchestration/models/agent-call.ts +0 -83
  305. package/src/orchestration/models/budget.ts +0 -118
  306. package/src/orchestration/models/ports.ts +0 -164
  307. package/src/orchestration/models/run-runtime.ts +0 -104
  308. package/src/orchestration/models/run-spec.ts +0 -83
  309. package/src/orchestration/models/run-state.ts +0 -44
  310. package/src/orchestration/models/trace.ts +0 -183
  311. package/src/orchestration/models/types.ts +0 -301
  312. package/src/orchestration/models/workflow-run.ts +0 -254
  313. package/src/orchestration/models/workflow-script-registry.ts +0 -35
  314. package/src/orchestration/models/workflow-script.ts +0 -117
  315. package/src/orchestration/script-lint.ts +0 -766
  316. package/src/orchestration/skill-discovery.ts +0 -115
  317. package/src/orchestration/worker-handle.ts +0 -115
  318. package/src/orchestration/worker-host.ts +0 -98
  319. package/src/orchestration/worker-script-builder.ts +0 -421
  320. package/src/orchestration/workflow-files.ts +0 -85
  321. package/src/orchestration/workflow-script-registry-impl.ts +0 -133
  322. package/src/shared/__tests__/agent-ref.test.ts +0 -34
  323. package/src/shared/__tests__/meta-parser.test.ts +0 -304
  324. package/src/shared/__tests__/model-ref.test.ts +0 -306
  325. package/src/shared/__tests__/resource-discovery-manifest-cache.test.ts +0 -280
  326. package/src/shared/__tests__/resource-discovery.test.ts +0 -736
  327. package/src/shared/__tests__/resource-meta.test.ts +0 -51
  328. package/src/shared/__tests__/schema-jsonify.test.ts +0 -81
  329. package/src/shared/__tests__/timer-delay.test.ts +0 -61
  330. package/src/shared/agent-event.ts +0 -13
  331. package/src/shared/agent-ref.ts +0 -57
  332. package/src/shared/meta-parser.ts +0 -261
  333. package/src/shared/model-ref.ts +0 -286
  334. package/src/shared/resource-discovery.ts +0 -811
  335. package/src/shared/resource-meta.ts +0 -65
  336. package/src/shared/schema-env.ts +0 -44
  337. package/src/shared/schema-jsonify.ts +0 -58
  338. package/src/shared/timer-delay.ts +0 -54
  339. package/src/shared/xml-injection.ts +0 -35
  340. package/workflows/README.md +0 -81
  341. package/workflows/_shared/agent-refs.cjs +0 -40
  342. package/workflows/chain.js +0 -131
  343. package/workflows/map-reduce.js +0 -174
  344. package/workflows/parallel.js +0 -148
  345. package/workflows/review-fix-loop-utils.cjs +0 -1309
  346. package/workflows/review-fix-loop.js +0 -1404
  347. package/workflows/scatter-gather.js +0 -178
@@ -1,399 +0,0 @@
1
- /**
2
- * Workflow Extension — lifecycle
3
- *
4
- * Workflow run 生命周期 free functions(D-12)。
5
- *
6
- * 5 个导出函数:
7
- * - runWorkflow(spec, deps, signal?) → Promise<runId>
8
- * - abortRun(runId, deps, reason?, doneReason?) → Promise<void>(done no-op)
9
- * - terminateRunningRuns(deps, reason) → Promise<void>(session 切换/关闭终止)
10
- * - evictDoneRunsBeyondCap(runs, keepDone) → number(done run 内存淘汰)
11
- * - scheduleTimeBudget(runId, deps, budgetTimeMs) → timer(C.7 时间预算)
12
- *
13
- * 私有 makeHandlers(run, deps) → WorkerHandlers:
14
- * - onMessage → handleWorkerMessage(run, raw, deps, handlers)
15
- * - onError → handleWorkerError(run, err, deps, handlers) + workerErrorCount++
16
- * - onExit(code, handle) → handleWorkerExit(run, code, handle, deps, handlers)
17
- * (G-025:handle.isCurrent 检查内化在 handleWorkerExit 内)
18
- *
19
- * **A4 原子性**:abort/terminate 内部 transition 先 releaseRuntime(cleanup before
20
- * mutate),失败时 status 不变。transition("done") 在 WorkflowRun.transition 内已实现
21
- * 「releaseRuntime → 改 status」原子顺序。
22
- *
23
- * **G3-001**(run 一次性生命周期):AbortController 一次性无法复用,runtime 释放后
24
- * 只有两类重建——rebuildRuntime(error-recovery,崩溃重试路径,run 保持 running、
25
- * replaceRuntime 原子换新)与 abort/terminate 的终态释放(transition("done") 内
26
- * releaseRuntime,run 不再恢复)。
27
- * (旧并发门闩 gate 抽象已删——no-op 无生产语义,实际并发由 SubagentService
28
- * ConcurrencyPool 管理;原 D-13 maxConcurrency=4 无消费方。)
29
- *
30
- * 层归属:Engine。依赖 LifecycleDeps + WorkerHost via port +
31
- * WorkflowRun + handleWorker* 函数。
32
- *
33
- * 参考:domain-models.md §1(聚合根状态机)。
34
- */
35
-
36
- import { getLogger } from "@zhushanwen/pi-extension-logger";
37
-
38
- import { assertSafeTimerDelay } from "../shared/timer-delay.ts";
39
- import { validateRunArgs } from "./args-validator.ts";
40
- import {
41
- handleWorkerError,
42
- handleWorkerExit,
43
- handleWorkerMessage,
44
- } from "./error-recovery.ts";
45
- import { Budget } from "./models/budget.ts";
46
- import type { LifecycleDeps, WorkerHandlers } from "./models/ports.ts";
47
- import { RunRuntime } from "./models/run-runtime.ts";
48
- import type { RunSpec } from "./models/run-spec.ts";
49
- import { Trace } from "./models/trace.ts";
50
- import type { DoneReason } from "./models/types.ts";
51
- import { WorkflowRun } from "./models/workflow-run.ts";
52
- import type { WorkerHandle } from "./worker-handle.ts";
53
-
54
- const logger = getLogger("subagents");
55
-
56
- // ── 常量 ─────────────────────────────────────────────────────
57
-
58
- /** runId 生成:wf-<timestamp>-<base36 random 6 chars>。 */
59
- const RUNID_RADIX = 36;
60
- const RUNID_SLICE_START = 2;
61
- const RUNID_SLICE_END = 8;
62
-
63
- /**
64
- * done run 内存保留窗口(K=20)。
65
- *
66
- * 本淘汰是 done run 内存有界性的唯一来源:calls.result 不裁、单聚合大小不随 wave1
67
- * 裁剪缩小,故内存上限 = K × 实际聚合大小。同时定义 actionStatus 可查刚完成 run
68
- * 的窗口(超出窗口的 done run 不再出现在列表中——已接受的用户可见变化)。
69
- */
70
- export const MAX_RETAINED_DONE_RUNS = 20;
71
-
72
- function generateRunId(): string {
73
- return `wf-${Date.now()}-${Math.random().toString(RUNID_RADIX).slice(RUNID_SLICE_START, RUNID_SLICE_END)}`;
74
- }
75
-
76
- // ── makeHandlers(路由 worker 事件到 error-recovery handle* 函数) ──────
77
-
78
- /**
79
- * 构造 WorkerHandlers——将 worker 的 onMessage/onError/onExit 事件路由到
80
- * error-recovery 的 handleWorker* 函数。
81
- *
82
- * 闭包捕获 run + deps。runtime 重建(replaceRuntime)后 run 实例不变、deps 不变,
83
- * 故 handlers 对新 worker 仍有效(lifecycle 与 error-recovery 共用 handlers)。
84
- *
85
- * **onExit G-025**:handleWorkerExit 内部检查 handle.isCurrent(stale exit 丢弃)。
86
- * 本函数不在 onExit 里重复检查——error-recovery.handleWorkerExit 是单一守卫点。
87
- *
88
- * **workerErrorCount**:onError 触发时递增(C.5 跨 runtime 存活的重试计数载体)。
89
- * 注意 handleWorkerError 内部也会递增——这里 onError 递增是 worker 事件层面的
90
- * 「error 事件到达」计数,handleWorkerError 内的是「错误处理决策」计数。
91
- * 实际 handleWorkerError 会做最终计数(含重试上限判断),onError 不重复递增。
92
- */
93
- function makeHandlers(run: WorkflowRun, deps: LifecycleDeps): WorkerHandlers {
94
- // 自引用——error-recovery rebuildRuntime 需要 handlers 参数(handlers 引用自身)
95
- const handlers: WorkerHandlers = {
96
- async onMessage(raw: unknown): Promise<void> {
97
- await handleWorkerMessage(run, raw, deps, handlers);
98
- },
99
- async onError(err: Error): Promise<void> {
100
- await handleWorkerError(run, err, deps, handlers);
101
- },
102
- async onExit(code: number, handle: WorkerHandle): Promise<void> {
103
- // H-2:用 worker-host 传入的 handle(即真正触发 exit 的那个 handle),而非
104
- // run.runtime?.worker——重试竞态下 runtime.worker 可能已被 replaceRuntime 替换
105
- // 为新 handle,导致 handleWorkerExit 内的 isCurrent 检查误判(漏判 stale exit 或
106
- // 误杀新 worker)。G-025 检查仍在 handleWorkerExit 内(handle.isCurrent)。
107
- await handleWorkerExit(run, code, handle, deps, handlers);
108
- },
109
- };
110
- return handlers;
111
- }
112
-
113
- // ── scheduleTimeBudget(C.7 Run 级时间预算调度) ──────────
114
-
115
- /**
116
- * 启动 run 级墙钟时间预算计时器:到期后 abortRun(doneReason="time_limited")。
117
- *
118
- * 恢复旧 orchestrator-budget.ts 的 scheduleTimeBudgetCheck 语义——runWorkflow 启动
119
- * 一个 setTimeout(maxTimeMs),到期若 run 仍未终态则转 done,time_limited。
120
- * 计时器存入 RunRuntime.timeBudgetTimer,release(abort/replaceRuntime)时
121
- * 自动清理,避免孤儿触发。worker/script 错误重试经 rebuildRuntime 重排新计时器。
122
- *
123
- * @returns 计时器句柄(未设预算时 undefined)
124
- * @throws budgetTimeMs 超出 Node setTimeout 上限(2^31-1)——溢出值会被 Node 置 1ms
125
- * 立即触发(「不限时预算」变「立即超时」),fail-fast 不静默 clamp(U1)。
126
- */
127
- export function scheduleTimeBudget(
128
- runId: string,
129
- deps: LifecycleDeps,
130
- budgetTimeMs: number,
131
- ): ReturnType<typeof setTimeout> {
132
- // [U1] arm 入口:值流入 setTimeout 前校验安全域(>2^31-1 会变 1ms 立即触发)。
133
- assertSafeTimerDelay(budgetTimeMs, "budgetTimeMs");
134
- const timer = setTimeout(() => {
135
- void abortRun(runId, deps, "Time budget exceeded", "time_limited").catch(
136
- (err: unknown) => {
137
- const msg = err instanceof Error ? err.message : String(err);
138
- logger.error(`[workflow] time budget abort failed: ${msg}`);
139
- },
140
- );
141
- }, budgetTimeMs);
142
- // unref:不阻止 Node 退出(workflow 是后台任务,不应因计时器持有事件循环)。
143
- timer.unref();
144
- return timer;
145
- }
146
-
147
- // ── runWorkflow ──────────────────────────────────────────────
148
-
149
- /**
150
- * 启动一个 workflow run。
151
- *
152
- * 流程:创建 WorkflowRun(running,I1 构造期跳过)+ makeHandlers + 构建 RunRuntime
153
- * (worker+gate+controller)+ assignRuntime(注入 runtime,恢复 I1)+ 注册到
154
- * deps.runs + store.save。
155
- *
156
- * @param spec RunSpec(scriptSource 只读;args 会被原地注入 _runId——rfl C2 契约,
157
- * worker 启动与崩溃重建共用同一 args 对象)
158
- * @param deps LifecycleDeps(store/workerHost/runner/runs)
159
- * @param signal 外部 abort signal(可选;abort 时调 abortRun)
160
- * @returns runId(wf-<timestamp>-<random>)
161
- * @throws signal 已 abort(pre-abort fail fast)
162
- */
163
- export async function runWorkflow(
164
- spec: RunSpec,
165
- deps: LifecycleDeps,
166
- signal?: AbortSignal,
167
- ): Promise<string> {
168
- // m3 E9:参数校验单一 chokepoint,钉在所有副作用前(generateRunId/log/signal
169
- // listener/runs.set/workerHost.start/store.save/pending:register)。校验失败时
170
- // zero side effects。coerceTypes 原地规范化 spec.args——worker 启动与崩溃重建
171
- // 共用同一对象(run.spec === spec),恢复路径参数一致。
172
- validateRunArgs(spec);
173
-
174
- const runId = generateRunId();
175
- // rfl 仪表(tier-1 §7.1):注入稳定 _runId。runAndWait 与 executeNestedWorkflow
176
- // 两个 args 入口都经本 choke point;rebuildRuntime 复用 run.spec.args 同一对象
177
- // (error-recovery.ts),worker rebuild 后脚本侧 $ARGS._runId 不漂移——修复
178
- // 「rebuild 回退 run-<Date.now()> 导致同一逻辑 run 碎裂到多个 state 目录」。
179
- // 注入在 validateRunArgs 之后,不参与脚本参数 schema 校验(引擎内部字段)。
180
- if (spec.args && typeof spec.args === "object") {
181
- spec.args._runId = runId;
182
- }
183
- deps.log?.("debug", "workflow:lifecycle", "runWorkflow start", { runId, scriptName: spec.scriptName });
184
-
185
- // P1-2: pre-aborted signal → fail fast
186
- if (signal?.aborted) {
187
- throw new Error("Workflow run aborted before start");
188
- }
189
-
190
- const run = new WorkflowRun(
191
- runId,
192
- spec,
193
- {
194
- status: "running",
195
- budget: spec.budgetRef ?? new Budget({
196
- maxTokens: spec.budgetTokens,
197
- maxTimeMs: spec.budgetTimeMs,
198
- }),
199
- calls: new Map(),
200
- trace: new Trace(),
201
- errorLogs: [],
202
- },
203
- { startedAt: new Date().toISOString() },
204
- );
205
-
206
- // signal abort → abortRun(一次性监听)
207
- if (signal) {
208
- signal.addEventListener(
209
- "abort",
210
- () => {
211
- void abortRun(runId, deps, "External signal aborted").catch((err: unknown) => {
212
- const msg = err instanceof Error ? err.message : String(err);
213
- logger.error(`[workflow] abortRun on signal failed: ${msg}`);
214
- });
215
- },
216
- { once: true },
217
- );
218
- }
219
-
220
- // 构造 handlers + runtime(worker + controller)
221
- const handlers = makeHandlers(run, deps);
222
- const controller = new AbortController();
223
- const worker = deps.workerHost.start(spec, spec.args, handlers);
224
- // C.7:run 级时间预算计时器(spec.budgetTimeMs > 0 时启用,到期 abortRun time_limited)。
225
- const timeBudgetTimer =
226
- spec.budgetTimeMs && spec.budgetTimeMs > 0
227
- ? scheduleTimeBudget(runId, deps, spec.budgetTimeMs)
228
- : undefined;
229
- const runtime = new RunRuntime(worker, controller, timeBudgetTimer);
230
-
231
- // assignRuntime(注入 runtime,恢复 I1:running ⟺ runtime!==undefined)
232
- run.assignRuntime(runtime);
233
-
234
- // 注册到 deps.runs(assignRuntime 之后——构造到 assignRuntime 之间 run 处于
235
- // I1 跳过窗口(running 而 runtime undefined),后移保证窗口对外不可见;
236
- // worker.start 抛错时 run 未注册,无孤儿 run 残留)
237
- deps.runs.set(runId, run);
238
-
239
- await deps.store.save(run);
240
- deps.log?.("debug", "workflow:lifecycle", "run saved", { runId, status: run.state.status });
241
-
242
- // pending-notifications: run 启动 → 注册(所有 workflow 启动路径的单一汇聚点:
243
- // runAndWait / actionRun / 未来入口全覆盖)
244
- deps.log?.("debug", "workflow:lifecycle", "emit pending:register", { runId });
245
- deps.eventBus?.emit("pending:register", {
246
- id: runId,
247
- type: "workflow",
248
- name: spec.slug || spec.scriptName || runId,
249
- });
250
- deps.log?.("debug", "workflow:lifecycle", "emit pending:register done", { runId });
251
-
252
- return runId;
253
- }
254
-
255
- // ── abortRun ─────────────────────────────────────────────────
256
-
257
- /**
258
- * 中止 workflow(running)。
259
- *
260
- * **done 状态 no-op**:已终态的 run 不重复 abort。
261
- * **A4 原子性**:transition("done", doneReason) 内部先 releaseRuntime。
262
- *
263
- * @param runId
264
- * @param deps
265
- * @param reason 可选中止原因(存 run.state.error)
266
- * @param doneReason 终态原因(默认 "aborted";超时场景传 "time_limited",C.7)
267
- * @throws runId 不存在
268
- */
269
- export async function abortRun(
270
- runId: string,
271
- deps: LifecycleDeps,
272
- reason?: string,
273
- doneReason: DoneReason = "aborted",
274
- ): Promise<void> {
275
- const run = deps.runs.get(runId);
276
- if (!run) {
277
- throw new Error(`Workflow '${runId}' not found`);
278
- }
279
-
280
- deps.log?.("debug", "workflow:lifecycle", "abortRun", { runId, status: run.state.status, reason, doneReason });
281
-
282
- // done 状态 no-op
283
- if (run.state.status === "done") {
284
- deps.log?.("debug", "workflow:lifecycle", "abortRun no-op: already done", { runId });
285
- return;
286
- }
287
-
288
- // 记录中止原因
289
- if (reason) {
290
- run.state.error = reason;
291
- }
292
- // A4: transition 内部 releaseRuntime(cleanup before mutate)
293
- run.transition("done", doneReason);
294
- await deps.store.save(run);
295
- deps.log?.("debug", "workflow:lifecycle", "abortRun transition done", { runId, reason: run.state.reason });
296
- // C-4: run 到达 done 终态 → 注销 pending-notification + 通知 Interface 层
297
- deps.log?.("debug", "workflow:lifecycle", "emit pending:unregister", { runId, reason: run.state.reason });
298
- deps.eventBus?.emit("pending:unregister", { id: run.runId, reason: run.state.reason ?? "completed" });
299
- deps.log?.("debug", "workflow:lifecycle", "emit pending:unregister done", { runId });
300
- deps.onRunDone?.(run);
301
- }
302
-
303
- // ── terminateRunningRuns(session 切换/关闭:终止全部 running run) ────────
304
-
305
- /**
306
- * 终止 deps.runs 中全部 running run(session 切换 / session 关闭时调用)。
307
- *
308
- * 一次性生命周期(D-2):session 离开当刻,running run 的 token 投入作废,转
309
- * done,failed 持久化落盘——重启后 kill-9 恢复不误判,也不再存在「挂起待恢复」
310
- * 的中间态。
311
- *
312
- * per-run 行为:`state.error = reason` → `transition("done","failed")`(内部先
313
- * releaseRuntime,A4)→ `await store.save(run)` → `eventBus.emit("pending:unregister",
314
- * {reason:"failed"})`。
315
- *
316
- * **不调 deps.onRunDone**:对齐 session_start 恢复先例(index.ts kill-9 恢复只发
317
- * unregister、不发 onRunDone)——session 切换/关闭语境下主 agent 已离开本 session,
318
- * 注入完成通知只会把消息发给已离开的 session。
319
- *
320
- * **不调 discardInFlightCalls**:run 已转终态不再 replay(无恢复路径),在飞 call
321
- * 缓存清不清都不影响结果;该清理仅 rebuildRuntime 需要(崩溃重试会重放脚本,
322
- * 假失败结果会污染重跑输出)。
323
- *
324
- * 单 run 失败(try/catch + log 带 runId/reason)不中断其余 run——终止是批量收尾,
325
- * 一个 run 落盘失败不应放走其余 run 的 failed 状态。
326
- *
327
- * @param deps LifecycleDeps(runs/store/eventBus/log)
328
- * @param reason 终止原因(写入 run.state.error,如 "Session switched: run terminated")
329
- */
330
- export async function terminateRunningRuns(
331
- deps: LifecycleDeps,
332
- reason: string,
333
- ): Promise<void> {
334
- for (const run of deps.runs.values()) {
335
- if (run.state.status !== "running") continue;
336
- try {
337
- deps.log?.("debug", "workflow:lifecycle", "terminateRunningRuns", { runId: run.runId, reason });
338
- run.state.error = reason;
339
- // A4: transition 内部先 releaseRuntime(cleanup before mutate)
340
- run.transition("done", "failed");
341
- await deps.store.save(run);
342
- // C-4: run 到达 done 终态 → 注销 pending-notification(reason 固定 "failed")
343
- deps.eventBus?.emit("pending:unregister", { id: run.runId, reason: "failed" });
344
- deps.log?.("debug", "workflow:lifecycle", "run terminated", { runId: run.runId, reason: run.state.reason });
345
- } catch (err) {
346
- const msg = err instanceof Error ? err.message : String(err);
347
- logger.error(
348
- `[workflow] terminateRunningRuns failed for run ${run.runId}: ${msg} (reason: ${reason})`,
349
- );
350
- }
351
- }
352
- }
353
-
354
- // ── evictDoneRunsBeyondCap(done run 内存淘汰,原地裁剪函数) ────────
355
-
356
- /**
357
- * 淘汰 runs Map 中超出保留窗口的 done run,返回本次淘汰数量。
358
- *
359
- * 规则(契约 W3C1):
360
- * 1. **状态白名单**:仅 `state.status === "done"` 可淘汰(RunStatus 封闭两态,
361
- * 显式白名单而非「非 running」——未来新增状态不落淘汰端)。running
362
- * (活跃执行,isScriptRunning 遍历依赖)永不淘汰,即使 completedAt 缺失也
363
- * 绝不参与排序淘汰。
364
- * 2. **排序**:done 项按 `meta.completedAt` ISO 字符串字典序升序(toISOString 恒
365
- * UTC 毫秒格式,字典序=时间序)。completedAt 缺失(防御旧格式/异常快照)fallback
366
- * 排序键为空串——字典序最小=最旧,先被淘汰。
367
- * 3. **tie 稳定排序**:比较器三态返回(相等返回 0),Array#sort 稳定性(Node≥12)
368
- * 保持元素原序——原序 = Map 插入序 = 创建序,tie 组内先创建者视为更旧先被淘汰
369
- * (kill-9 批量恢复同 ms completedAt 场景的确定性保证)。
370
- * 4. **淘汰执行**:超限数 excess = doneCount - keepDone(<=0 时 no-op 返回 0),
371
- * 对升序前 excess 项逐个 `runs.delete(runId)`。
372
- * 5. **边界不变式**:禁止按 Map 插入序直接淘汰——嵌套 workflow 父 run 创建最早、
373
- * 完成最晚,插入序淘汰会在其自身 onRunDone 同步裁剪中淘汰它,runAndWait 轮询
374
- * 窗口内 get 不到 → 误返 "Run not found"。
375
- * 6. **副作用边界**:只清内存 runs Map,不动磁盘 state 文件、不删
376
- * workflow-state-link 指针条目、不发任何事件。
377
- *
378
- * @param runs per-session 的 run 注册表(原地裁剪)
379
- * @param keepDone done run 保留数(生产传 MAX_RETAINED_DONE_RUNS)
380
- * @returns 本次淘汰的 run 数量
381
- */
382
- export function evictDoneRunsBeyondCap(
383
- runs: Map<string, WorkflowRun>,
384
- keepDone: number,
385
- ): number {
386
- // 显式白名单:仅 done 参与淘汰(running 误删即功能破坏)
387
- const done = Array.from(runs.values()).filter((r) => r.state.status === "done");
388
- const excess = done.length - keepDone;
389
- if (excess <= 0) return 0;
390
- // ISO 字典序=时间序;缺失 fallback 空串(ISO 串恒以 '2' 开头非空,空串严格最小=最旧)
391
- const keyOf = (r: WorkflowRun): string => r.meta.completedAt ?? "";
392
- // 三态比较器 + sort 稳定性:tie 保持 Array.from 的 Map 插入序(=创建序)——
393
- // 先插入者更旧先淘汰,禁止按插入序直接 slice 淘汰(边界不变式 5)
394
- done.sort((a, b) => (keyOf(a) < keyOf(b) ? -1 : keyOf(a) > keyOf(b) ? 1 : 0));
395
- for (const r of done.slice(0, excess)) {
396
- runs.delete(r.runId);
397
- }
398
- return excess;
399
- }