@zhushanwen/pi-subagent-workflow 8.5.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.
- package/package.json +13 -16
- package/relay/relay.mjs +1 -1
- package/src/host/__tests__/pi-host.test.ts +214 -0
- package/src/host/pi-host.ts +109 -0
- package/src/index.ts +142 -32
- package/src/injectors/__tests__/engine-awareness.test.ts +292 -0
- package/src/injectors/__tests__/engine-section-stability.test.ts +341 -0
- package/src/injectors/__tests__/subagent-list-injector.test.ts +5 -5
- package/src/injectors/__tests__/workflow-list-injector.test.ts +5 -5
- package/src/injectors/engine-awareness.ts +123 -0
- package/src/injectors/model-list-injector.ts +1 -1
- package/src/injectors/subagent-list-injector.ts +8 -8
- package/src/injectors/workflow-list-injector.ts +8 -8
- package/src/interface/__tests__/detectors.test.ts +3 -3
- package/src/interface/__tests__/subagent-tool-path-guard.test.ts +157 -0
- package/src/interface/__tests__/subagent-tool-prompt.test.ts +12 -0
- package/src/interface/__tests__/tool-workflow-script-generate.test.ts +2 -2
- package/src/interface/__tests__/tool-workflow-throw-paths.test.ts +1 -1
- package/src/interface/bg-notify-render.ts +34 -13
- package/src/interface/commands.ts +3 -3
- package/src/interface/format.ts +2 -2
- package/src/interface/helpers.ts +3 -3
- package/src/interface/list-component.ts +4 -4
- package/src/interface/list-shared.ts +2 -2
- package/src/interface/list-view.ts +2 -2
- package/src/interface/subagent-actions.ts +288 -19
- package/src/interface/subagent-tool-schema.ts +163 -0
- package/src/interface/subagent-tool.ts +65 -129
- package/src/interface/subagents.ts +5 -5
- package/src/interface/tool-render.ts +34 -2
- package/src/interface/tool-workflow-script.ts +4 -4
- package/src/interface/tool-workflow.ts +6 -6
- package/src/interface/views/WorkflowsView.ts +5 -5
- package/src/interface/views/__tests__/WorkflowsView-signature.test.ts +3 -3
- package/src/interface/views/__tests__/detail-content-session-file.test.ts +2 -2
- package/src/interface/views/detail-content.ts +4 -4
- package/src/interface/views/format.ts +4 -4
- package/src/{orchestration/jsonl-run-store.ts → jsonl-run-store.ts} +132 -18
- package/src/execution/__tests__/__fixtures__/notifier-golden-snapshots.json +0 -53
- package/src/execution/__tests__/__fixtures__/truncline.snapshot.json +0 -1
- package/src/execution/__tests__/agent-registry.test.ts +0 -363
- package/src/execution/__tests__/agent-result-mapper.test.ts +0 -150
- package/src/execution/__tests__/alive-store.test.ts +0 -147
- package/src/execution/__tests__/ask-user-transit-e2e.test.ts +0 -490
- package/src/execution/__tests__/before-agent-start-injection.test.ts +0 -132
- package/src/execution/__tests__/bg-notify-render.test.ts +0 -256
- package/src/execution/__tests__/channel-registry-handshake.test.ts +0 -243
- package/src/execution/__tests__/chat-engine-routing.test.ts +0 -597
- package/src/execution/__tests__/chatmode-first-round-closure-service.test.ts +0 -365
- package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +0 -190
- package/src/execution/__tests__/chatmode-round-notify-real-chain.test.ts +0 -215
- package/src/execution/__tests__/concurrency-pool.test.ts +0 -250
- package/src/execution/__tests__/config.test.ts +0 -110
- package/src/execution/__tests__/conversation-wiring.test.ts +0 -198
- package/src/execution/__tests__/crash-recovery.test.ts +0 -320
- package/src/execution/__tests__/delivery-methods.test.ts +0 -385
- package/src/execution/__tests__/dialog-queue.test.ts +0 -299
- package/src/execution/__tests__/epipe-fallback.test.ts +0 -241
- package/src/execution/__tests__/execute-and-await-worktree.test.ts +0 -243
- package/src/execution/__tests__/execute-nesting.test.ts +0 -323
- package/src/execution/__tests__/execute-options-mapper.test.ts +0 -189
- package/src/execution/__tests__/execution-record.test.ts +0 -1284
- package/src/execution/__tests__/finalize-record.test.ts +0 -368
- package/src/execution/__tests__/finalized-marker.test.ts +0 -82
- package/src/execution/__tests__/format-schema-instruction.test.ts +0 -138
- package/src/execution/__tests__/format.test.ts +0 -458
- package/src/execution/__tests__/gc-timer.test.ts +0 -184
- package/src/execution/__tests__/get-record-for-action-restart.test.ts +0 -254
- package/src/execution/__tests__/gui-mode-dispatch.test.ts +0 -59
- package/src/execution/__tests__/helpers/mock-extension-api.ts +0 -30
- package/src/execution/__tests__/helpers/spawn-mock.ts +0 -238
- package/src/execution/__tests__/host-mode.test.ts +0 -87
- package/src/execution/__tests__/index-session-start-identity.test.ts +0 -371
- package/src/execution/__tests__/index-session-start.test.ts +0 -593
- package/src/execution/__tests__/lifecycle-manager-lock.test.ts +0 -211
- package/src/execution/__tests__/lifecycle-manager.test.ts +0 -337
- package/src/execution/__tests__/lifecycle-predicates.test.ts +0 -116
- package/src/execution/__tests__/list-component.test.ts +0 -401
- package/src/execution/__tests__/list-fields.test.ts +0 -109
- package/src/execution/__tests__/manifest-parentid.test.ts +0 -117
- package/src/execution/__tests__/model-resolver.test.ts +0 -413
- package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +0 -287
- package/src/execution/__tests__/nested-visibility.test.ts +0 -325
- package/src/execution/__tests__/notifier-flush.test.ts +0 -574
- package/src/execution/__tests__/notifier-golden-snapshot.test.ts +0 -261
- package/src/execution/__tests__/one-shot-upgrade.test.ts +0 -205
- package/src/execution/__tests__/output-collector.test.ts +0 -61
- package/src/execution/__tests__/parent-child-matrix.test.ts +0 -336
- package/src/execution/__tests__/path-encoding.test.ts +0 -104
- package/src/execution/__tests__/pi-invocation.test.ts +0 -134
- package/src/execution/__tests__/record-store.test.ts +0 -1057
- package/src/execution/__tests__/records-cwd-isolation.test.ts +0 -91
- package/src/execution/__tests__/recursive-visibility-baseline.test.ts +0 -339
- package/src/execution/__tests__/recursive-visibility-env.test.ts +0 -273
- package/src/execution/__tests__/relay-agent.test.ts +0 -448
- package/src/execution/__tests__/relay-env.test.ts +0 -42
- package/src/execution/__tests__/resource-policy.test.ts +0 -109
- package/src/execution/__tests__/rpc-mode.test.ts +0 -89
- package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +0 -267
- package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +0 -253
- package/src/execution/__tests__/run-spawn-edges.test.ts +0 -644
- package/src/execution/__tests__/run-spawn-integration.test.ts +0 -933
- package/src/execution/__tests__/run-spawn-resume.test.ts +0 -322
- package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +0 -196
- package/src/execution/__tests__/sdk-contract.test.ts +0 -273
- package/src/execution/__tests__/session-context-resolver.test.ts +0 -167
- package/src/execution/__tests__/session-file-gc.test.ts +0 -293
- package/src/execution/__tests__/session-pending.test.ts +0 -197
- package/src/execution/__tests__/session-reconstructor.test.ts +0 -379
- package/src/execution/__tests__/session-runner-epipe.test.ts +0 -178
- package/src/execution/__tests__/session-runner-schema-env.test.ts +0 -311
- package/src/execution/__tests__/session-start-reaper.test.ts +0 -233
- package/src/execution/__tests__/spawn-args.test.ts +0 -434
- package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +0 -189
- package/src/execution/__tests__/spawn-event-adapter.test.ts +0 -167
- package/src/execution/__tests__/spawn-worktree-guidance.test.ts +0 -207
- package/src/execution/__tests__/spawned-children.test.ts +0 -92
- package/src/execution/__tests__/startup-config-declaration.test.ts +0 -35
- package/src/execution/__tests__/status-refactor.test.ts +0 -345
- package/src/execution/__tests__/stdin-writer.test.ts +0 -463
- package/src/execution/__tests__/stream-sink-retirement.test.ts +0 -261
- package/src/execution/__tests__/subagent-service-abort.test.ts +0 -60
- package/src/execution/__tests__/subagent-service-message-close.test.ts +0 -629
- package/src/execution/__tests__/subagent-service-parent-guard.test.ts +0 -180
- package/src/execution/__tests__/subagent-service.test.ts +0 -788
- package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +0 -310
- package/src/execution/__tests__/subprocess-agent-runner.test.ts +0 -506
- package/src/execution/__tests__/temp-prompt.test.ts +0 -53
- package/src/execution/__tests__/timeout-integration.test.ts +0 -397
- package/src/execution/__tests__/tombstone-store.test.ts +0 -73
- package/src/execution/__tests__/tool-action.test.ts +0 -385
- package/src/execution/__tests__/truncline-snapshot.test.ts +0 -81
- package/src/execution/__tests__/turn-limiter-semantics.test.ts +0 -194
- package/src/execution/__tests__/turn-limiter.test.ts +0 -65
- package/src/execution/__tests__/ui-channels.test.ts +0 -187
- package/src/execution/__tests__/ui-interaction-model.test.ts +0 -67
- package/src/execution/__tests__/ui-request-handler-factory.test.ts +0 -370
- package/src/execution/__tests__/ui-request-handler.test.ts +0 -204
- package/src/execution/__tests__/ui-request-observability.test.ts +0 -114
- package/src/execution/__tests__/ui-request-queue.test.ts +0 -133
- package/src/execution/__tests__/worktree-manager.test.ts +0 -633
- package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +0 -229
- package/src/execution/__tests__/worktree-reconcile.integration.test.ts +0 -181
- package/src/execution/__tests__/worktree-registry.test.ts +0 -199
- package/src/execution/agent-registry.ts +0 -193
- package/src/execution/agent-result-mapper.ts +0 -90
- package/src/execution/alive-store.ts +0 -92
- package/src/execution/argv-mirror.ts +0 -109
- package/src/execution/best-effort.ts +0 -38
- package/src/execution/channel-registry-access.ts +0 -144
- package/src/execution/concurrency-pool.ts +0 -116
- package/src/execution/config.ts +0 -96
- package/src/execution/dialog-queue.ts +0 -330
- package/src/execution/engine/__tests__/common/data-dir.test.ts +0 -53
- package/src/execution/engine/__tests__/common/errors.test.ts +0 -132
- package/src/execution/engine/__tests__/common/event-journal.test.ts +0 -177
- package/src/execution/engine/__tests__/common/kill-chain.test.ts +0 -192
- package/src/execution/engine/__tests__/common/nesting-guard.test.ts +0 -81
- package/src/execution/engine/__tests__/common/persona-router.test.ts +0 -123
- package/src/execution/engine/__tests__/common/pool-manager.test.ts +0 -154
- package/src/execution/engine/__tests__/common/schema-emulation.test.ts +0 -128
- package/src/execution/engine/__tests__/conformance/__fixtures__/pi-golden-events.json +0 -28
- package/src/execution/engine/__tests__/conformance/agent-event-invariants.ts +0 -141
- package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +0 -109
- package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +0 -101
- package/src/execution/engine/__tests__/conformance/contract.probe.test.ts +0 -77
- package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +0 -104
- package/src/execution/engine/__tests__/conformance/contract.relay.test.ts +0 -342
- package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +0 -201
- package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +0 -76
- package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +0 -79
- package/src/execution/engine/__tests__/engine-discovery.test.ts +0 -87
- package/src/execution/engine/__tests__/engines-declaration.test.ts +0 -36
- package/src/execution/engine/__tests__/model-prompt.test.ts +0 -85
- package/src/execution/engine/__tests__/paths.test.ts +0 -39
- package/src/execution/engine/__tests__/registry.test.ts +0 -120
- package/src/execution/engine/__tests__/routing.test.ts +0 -231
- package/src/execution/engine/common/data-dir.ts +0 -62
- package/src/execution/engine/common/errors.ts +0 -183
- package/src/execution/engine/common/event-journal.ts +0 -254
- package/src/execution/engine/common/journal-replay.ts +0 -62
- package/src/execution/engine/common/kill-chain.ts +0 -221
- package/src/execution/engine/common/nesting-guard.ts +0 -50
- package/src/execution/engine/common/persona-router.ts +0 -108
- package/src/execution/engine/common/pool-manager.ts +0 -226
- package/src/execution/engine/common/schema-emulation.ts +0 -189
- package/src/execution/engine/common/session-view-projection.ts +0 -51
- package/src/execution/engine/engine-discovery.ts +0 -65
- package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +0 -469
- package/src/execution/engine/engines/pi/__tests__/reader.test.ts +0 -155
- package/src/execution/engine/engines/pi/__tests__/task-spec-mapper.test.ts +0 -164
- package/src/execution/engine/engines/pi/pi-engine.ts +0 -415
- package/src/execution/engine/engines/pi/reader.ts +0 -48
- package/src/execution/engine/engines/pi/registration.ts +0 -35
- package/src/execution/engine/engines/pi/task-spec-mapper.ts +0 -100
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-spawn.json +0 -39
- package/src/execution/engine/engines/zcode/__tests__/launcher.test.ts +0 -150
- package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +0 -246
- package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +0 -228
- package/src/execution/engine/engines/zcode/__tests__/reader.test.ts +0 -210
- package/src/execution/engine/engines/zcode/__tests__/registration.test.ts +0 -64
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +0 -127
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.test.ts +0 -567
- package/src/execution/engine/engines/zcode/constants.ts +0 -43
- package/src/execution/engine/engines/zcode/golden-sample.ts +0 -39
- package/src/execution/engine/engines/zcode/launcher.ts +0 -161
- package/src/execution/engine/engines/zcode/parser.ts +0 -436
- package/src/execution/engine/engines/zcode/preparer.ts +0 -363
- package/src/execution/engine/engines/zcode/reader.ts +0 -381
- package/src/execution/engine/engines/zcode/registration.ts +0 -37
- package/src/execution/engine/engines/zcode/zcode-engine.ts +0 -648
- package/src/execution/engine/host-task-spec.ts +0 -47
- package/src/execution/engine/model-prompt.ts +0 -59
- package/src/execution/engine/paths.ts +0 -42
- package/src/execution/engine/port.ts +0 -153
- package/src/execution/engine/registry.ts +0 -123
- package/src/execution/engine/routing.ts +0 -218
- package/src/execution/engine/types.ts +0 -304
- package/src/execution/execute-options-mapper.ts +0 -111
- package/src/execution/execution-record.ts +0 -906
- package/src/execution/finalize-record.ts +0 -251
- package/src/execution/finalized-marker.ts +0 -51
- package/src/execution/get-state-handshake.ts +0 -104
- package/src/execution/host-mode.ts +0 -56
- package/src/execution/idle-gc.ts +0 -47
- package/src/execution/lifecycle-manager.ts +0 -491
- package/src/execution/lifecycle-predicates.ts +0 -65
- package/src/execution/manifest-store.ts +0 -256
- package/src/execution/model-config-service.ts +0 -204
- package/src/execution/model-resolver.ts +0 -276
- package/src/execution/notifier.ts +0 -260
- package/src/execution/output-collector.ts +0 -88
- package/src/execution/path-encoding.ts +0 -60
- package/src/execution/pi-invocation.ts +0 -120
- package/src/execution/record-entry.ts +0 -132
- package/src/execution/record-store.ts +0 -1234
- package/src/execution/relay-env.ts +0 -37
- package/src/execution/session-context-resolver.ts +0 -64
- package/src/execution/session-file-gc.ts +0 -120
- package/src/execution/session-pending.ts +0 -164
- package/src/execution/session-reconstructor.ts +0 -678
- package/src/execution/session-runner.ts +0 -1548
- package/src/execution/sessions-index.ts +0 -304
- package/src/execution/spawn-event-adapter.ts +0 -363
- package/src/execution/stdin-writer.ts +0 -198
- package/src/execution/stream-sink.ts +0 -126
- package/src/execution/subagent-service.ts +0 -2119
- package/src/execution/subprocess-agent-runner.ts +0 -303
- package/src/execution/temp-prompt.ts +0 -62
- package/src/execution/tombstone-store.ts +0 -72
- package/src/execution/turn-limiter.ts +0 -102
- package/src/execution/types.ts +0 -910
- package/src/execution/ui-channels.ts +0 -216
- package/src/execution/ui-interaction-model.ts +0 -48
- package/src/execution/ui-request-handler-factory.ts +0 -212
- package/src/execution/ui-request-observability.ts +0 -81
- package/src/execution/ui-request-queue.ts +0 -174
- package/src/execution/worktree-manager.ts +0 -688
- package/src/execution/worktree-registry.ts +0 -222
- package/src/orchestration/__tests__/__fixtures__/worker-template.snapshot.txt +0 -336
- package/src/orchestration/__tests__/agent-call-catch-fallback.test.ts +0 -208
- package/src/orchestration/__tests__/agent-call-stream.test.ts +0 -157
- package/src/orchestration/__tests__/args-validator.test.ts +0 -143
- package/src/orchestration/__tests__/concurrency-gate.test.ts +0 -125
- package/src/orchestration/__tests__/config-loader.test.ts +0 -503
- package/src/orchestration/__tests__/error-recovery-handlers.test.ts +0 -724
- package/src/orchestration/__tests__/error-recovery-postmessage-defense.test.ts +0 -405
- package/src/orchestration/__tests__/error-recovery-serialize-failed-result.test.ts +0 -56
- package/src/orchestration/__tests__/error-recovery-workflow-call.test.ts +0 -166
- package/src/orchestration/__tests__/execute-agent-call.test.ts +0 -314
- package/src/orchestration/__tests__/jsonl-run-store-loadall-sources.test.ts +0 -171
- package/src/orchestration/__tests__/jsonl-run-store-session-file.test.ts +0 -1030
- package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +0 -277
- package/src/orchestration/__tests__/lifecycle-runid-injection.test.ts +0 -96
- package/src/orchestration/__tests__/lifecycle.test.ts +0 -625
- package/src/orchestration/__tests__/review-fix-loop-e2e.test.ts +0 -1971
- package/src/orchestration/__tests__/script-lint.test.ts +0 -831
- package/src/orchestration/__tests__/skill-discovery.test.ts +0 -157
- package/src/orchestration/__tests__/test-mocks.ts +0 -197
- package/src/orchestration/__tests__/worker-host.test.ts +0 -120
- package/src/orchestration/__tests__/worker-returnmeta-passthrough.test.ts +0 -164
- package/src/orchestration/__tests__/worker-script-builder-runtime.test.ts +0 -520
- package/src/orchestration/__tests__/worker-script-builder.test.ts +0 -309
- package/src/orchestration/__tests__/worker-script-template-snapshot.test.ts +0 -110
- package/src/orchestration/__tests__/workflow-nesting-e2e.test.ts +0 -317
- package/src/orchestration/__tests__/workflow-script-lint-memo.test.ts +0 -110
- package/src/orchestration/__tests__/workflows-e2e.test.ts +0 -537
- package/src/orchestration/agent-opts-resolver.ts +0 -93
- package/src/orchestration/args-validator.ts +0 -127
- package/src/orchestration/concurrency-gate.ts +0 -69
- package/src/orchestration/config-loader.ts +0 -315
- package/src/orchestration/error-recovery.ts +0 -862
- package/src/orchestration/execute-agent-call.ts +0 -217
- package/src/orchestration/launcher.ts +0 -410
- package/src/orchestration/lifecycle.ts +0 -396
- package/src/orchestration/models/__tests__/budget.test.ts +0 -367
- package/src/orchestration/models/__tests__/trace.test.ts +0 -408
- package/src/orchestration/models/agent-call.ts +0 -83
- package/src/orchestration/models/budget.ts +0 -148
- package/src/orchestration/models/ports.ts +0 -164
- package/src/orchestration/models/run-runtime.ts +0 -89
- package/src/orchestration/models/run-spec.ts +0 -83
- package/src/orchestration/models/run-state.ts +0 -44
- package/src/orchestration/models/trace.ts +0 -183
- package/src/orchestration/models/types.ts +0 -292
- package/src/orchestration/models/workflow-run.ts +0 -254
- package/src/orchestration/models/workflow-script-registry.ts +0 -35
- package/src/orchestration/models/workflow-script.ts +0 -117
- package/src/orchestration/script-lint.ts +0 -766
- package/src/orchestration/skill-discovery.ts +0 -92
- package/src/orchestration/worker-handle.ts +0 -115
- package/src/orchestration/worker-host.ts +0 -98
- package/src/orchestration/worker-script-builder.ts +0 -408
- package/src/orchestration/workflow-files.ts +0 -85
- package/src/orchestration/workflow-script-registry-impl.ts +0 -133
- package/src/shared/__tests__/agent-ref.test.ts +0 -34
- package/src/shared/__tests__/meta-parser.test.ts +0 -304
- package/src/shared/__tests__/resource-discovery-manifest-cache.test.ts +0 -280
- package/src/shared/__tests__/resource-discovery.test.ts +0 -736
- package/src/shared/__tests__/resource-meta.test.ts +0 -51
- package/src/shared/__tests__/schema-jsonify.test.ts +0 -81
- package/src/shared/agent-event.ts +0 -13
- package/src/shared/agent-ref.ts +0 -57
- package/src/shared/meta-parser.ts +0 -261
- package/src/shared/resource-discovery.ts +0 -811
- package/src/shared/resource-meta.ts +0 -65
- package/src/shared/schema-jsonify.ts +0 -56
- package/src/shared/xml-injection.ts +0 -35
- package/workflows/README.md +0 -81
- package/workflows/_shared/agent-refs.cjs +0 -40
- package/workflows/chain.js +0 -131
- package/workflows/map-reduce.js +0 -174
- package/workflows/parallel.js +0 -148
- package/workflows/review-fix-loop-utils.cjs +0 -1307
- package/workflows/review-fix-loop.js +0 -1396
- package/workflows/scatter-gather.js +0 -178
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Workflow Extension — executeAgentCall(关键路径)
|
|
3
|
-
*
|
|
4
|
-
* 单次 agent 调用执行的 Engine free function(D-12)。显式 5 参数
|
|
5
|
-
* `(call, runner, budget, signal, trace)`,无依赖注入 bag(AC-2:消除散落的
|
|
6
|
-
* Context factory)。
|
|
7
|
-
*
|
|
8
|
-
* 职责:
|
|
9
|
-
* - 重试:3 次 + 指数退避(BACKOFF_MS = [1000, 2000, 4000])
|
|
10
|
-
* - 预算:超限不重试(直接 markDone failed)
|
|
11
|
-
* - stale-context:不重试(直接 markDone failed)
|
|
12
|
-
* - 成功:consume usage + incrementCallCount + markDone + trace.update(completed)
|
|
13
|
-
*
|
|
14
|
-
* 关键设计:
|
|
15
|
-
* - **usage 透传**:result.usage 直接交给 budget.consume,加权由 Budget 内部的权重常量
|
|
16
|
-
* 处理(见 budget.ts)。此函数不再做 usage 形状的改写。
|
|
17
|
-
* - **参数显式化**:runner 直接传入(而非 ctx.getRun(runId).pool),无 runId 查找 / pool 守卫。
|
|
18
|
-
* - **stale-state 检查**:signal.aborted 时早返回。WorkflowRun 状态由调用方 lifecycle
|
|
19
|
-
* 持有,executeAgentCall 只关心单次 call 生命周期。
|
|
20
|
-
*
|
|
21
|
-
* 层归属:Engine。零 infra 依赖(runner 是 AgentRunner port,budget/trace/call 是 Engine 模型)。
|
|
22
|
-
*
|
|
23
|
-
* 参考:domain-models.md §5 + §失败处理矩阵。
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import type { SubagentStream } from "../execution/stream-sink.ts";
|
|
27
|
-
import type { AgentEvent } from "../shared/agent-event.ts";
|
|
28
|
-
import type { AgentCall } from "./models/agent-call.ts";
|
|
29
|
-
import type { Budget } from "./models/budget.ts";
|
|
30
|
-
import type { AgentRunner } from "./models/ports.ts";
|
|
31
|
-
import type { Trace } from "./models/trace.ts";
|
|
32
|
-
import type { AgentResult } from "./models/types.ts";
|
|
33
|
-
|
|
34
|
-
// ── 常量 ─────────────────────────────────────────────────────
|
|
35
|
-
|
|
36
|
-
/** 指数退避基数(ms):第 n 次重试等待 BASE^n。 */
|
|
37
|
-
const BACKOFF_BASE_MS = 1000;
|
|
38
|
-
const BACKOFF_EXPONENT_BASE = 2;
|
|
39
|
-
|
|
40
|
-
/** 最大尝试次数(含首次):initial + 2 retries = 3。 */
|
|
41
|
-
const MAX_ATTEMPTS = 3;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Stale context 检测模式(P1-5;W4b 对齐 pi 0.84.x 真实文案)。
|
|
45
|
-
*
|
|
46
|
-
* pi session context 被 compact/cancel 时报告的模式。这种情况下重试无意义——
|
|
47
|
-
* 同样的 call 会再次失败。直接 markDone failed 终止单次调用。
|
|
48
|
-
*
|
|
49
|
-
* W4b:原 "stale context"/"stalecontext" 与 pi 真实文案零匹配(真实文案为
|
|
50
|
-
* "This extension ctx is stale after session replacement or reload. ..."——
|
|
51
|
-
* runner.ts:544(dist runner.js:352),词序是 "ctx is stale" 而非 "stale context"),stale 分诊对
|
|
52
|
-
* 真实文案失效。现对齐:
|
|
53
|
-
* - "ctx is stale":真实文案核心子串(词序修正)
|
|
54
|
-
* - "stale after session replacement":scheduler 已验证 marker(runtime.ts
|
|
55
|
-
* STALE_CTX_MARKER,同文案锚定)
|
|
56
|
-
* - "context canceled"/"aborted":保留——abort 族错误同样不重试(signal.aborted
|
|
57
|
-
* 分支的先行分诊,防边界竞态漏网),删除会放宽重试语义。
|
|
58
|
-
*/
|
|
59
|
-
export const STALE_CONTEXT_PATTERNS = [
|
|
60
|
-
"ctx is stale",
|
|
61
|
-
"stale after session replacement",
|
|
62
|
-
"context canceled",
|
|
63
|
-
"aborted",
|
|
64
|
-
] as const;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 判断错误信息是否表示 stale/canceled pi session context。
|
|
68
|
-
* 命中时不重试——重试只会再次失败(P1-5)。
|
|
69
|
-
*/
|
|
70
|
-
export function isStaleContextErrorMsg(msg: string | undefined): boolean {
|
|
71
|
-
if (!msg) return false;
|
|
72
|
-
const lower = msg.toLowerCase();
|
|
73
|
-
return STALE_CONTEXT_PATTERNS.some((p) => lower.includes(p));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ── 内部 helper ──────────────────────────────────────────────
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* 计算第 n 次重试前的退避时间(ms)。
|
|
80
|
-
* 第 1 次重试 → 1000ms,第 2 次 → 2000ms,第 3 次 → 4000ms(指数退避)。
|
|
81
|
-
*/
|
|
82
|
-
function backoffDelay(retryIndex: number): number {
|
|
83
|
-
return BACKOFF_BASE_MS * Math.pow(BACKOFF_EXPONENT_BASE, retryIndex - 1);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 终态化单次 call:markDone + trace.update。
|
|
88
|
-
*
|
|
89
|
-
* 成功:status="completed";失败:status="failed"。
|
|
90
|
-
* traceNode.stepIndex === call.id(D-10 单源,调用方保证)。
|
|
91
|
-
*
|
|
92
|
-
* 孤儿守卫(OB2,S7 残留):isOrphaned 谓词为 true 时跳过 trace.update——
|
|
93
|
-
* rebuild 竞态窗口中,重跑 dispatch 已 append 同 stepIndex 新节点,旧代际
|
|
94
|
-
* finalize 的 update 会命中新节点,TUI/中间快照短暂可见错误终态。正确性论证:
|
|
95
|
-
* 运行期 calls Map 写点仅 discardInFlightCalls 的 delete 与 dispatchAgentCall 的
|
|
96
|
-
* set 两族(error-recovery.ts isOrphanedCall 文档注释既定),故实例不等 ⟺ 本
|
|
97
|
-
* finalize 属于被丢弃/被替换的旧代际——与 dispatch 层 .then/.catch 守卫
|
|
98
|
-
* (S7-second 修复,8353f6b60)同一判定语义,本守卫只是把它前移到 trace.update
|
|
99
|
-
* 之前。markDone 与 sessionId/sessionFile 同步保留(markDone 在孤儿实例上无害,
|
|
100
|
-
* dispatch 层 catch 路径依赖 call.status 语义)。跳过时不记日志——本文件是纯
|
|
101
|
-
* 函数层无日志通道,dispatch 层 .then 守卫的 orphan completion dropped 日志已
|
|
102
|
-
* 覆盖同一事件的可观察性。
|
|
103
|
-
*/
|
|
104
|
-
function finalizeCall(
|
|
105
|
-
call: AgentCall,
|
|
106
|
-
result: AgentResult,
|
|
107
|
-
trace: Trace,
|
|
108
|
-
isOrphaned?: () => boolean,
|
|
109
|
-
): void {
|
|
110
|
-
call.markDone(result);
|
|
111
|
-
const status = result.error === undefined ? "completed" : "failed";
|
|
112
|
-
// 同步 AgentCall 的 sessionId/sessionFile(对齐 trace 节点,持久化 + reset 用)
|
|
113
|
-
if (result.sessionId !== undefined) call.setSessionId(result.sessionId);
|
|
114
|
-
if (result.sessionFile !== undefined) call.setSessionFile(result.sessionFile);
|
|
115
|
-
if (isOrphaned?.()) return;
|
|
116
|
-
trace.update(call.id, {
|
|
117
|
-
status,
|
|
118
|
-
result,
|
|
119
|
-
completedAt: new Date().toISOString(),
|
|
120
|
-
sessionId: result.sessionId,
|
|
121
|
-
sessionFile: result.sessionFile,
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* 延迟工具(testable —— 测试可通过 fake timers 推进)。
|
|
127
|
-
*/
|
|
128
|
-
function delay(ms: number): Promise<void> {
|
|
129
|
-
return new Promise((resolve) => {
|
|
130
|
-
const timer = setTimeout(resolve, ms);
|
|
131
|
-
timer.unref();
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// ── executeAgentCall ─────────────────────────────────────────
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* 执行单次 agent 调用,含重试 + 预算 + stale-context 检测。
|
|
139
|
-
*
|
|
140
|
-
* 流程:
|
|
141
|
-
* 1. markRunning(attempts++,含首次)
|
|
142
|
-
* 2. await runner.run(opts, signal)(AgentRunner port,infra 实现 spawn pi 子进程)
|
|
143
|
-
* 3. 若 result.usage 存在:consumeUsage(D.4 修复)
|
|
144
|
-
* 4. stale-context → finalizeCall failed,返回(不重试)
|
|
145
|
-
* 5. signal.aborted → 返回(调用方已终止,不重试)
|
|
146
|
-
* 6. budget.isExceeded → finalizeCall failed,返回(不重试)
|
|
147
|
-
* 7. 失败 && attempts < MAX → 退避后递归(下一次 markRunning)
|
|
148
|
-
* 8. 否则 finalizeCall(completed 或 failed)+ incrementCallCount
|
|
149
|
-
*
|
|
150
|
-
* @param call AgentCall 实体(markRunning/markDone 由本函数驱动)
|
|
151
|
-
* @param runner AgentRunner port(执行子进程)
|
|
152
|
-
* @param budget Budget 值对象(consume + isExceeded 检查)
|
|
153
|
-
* @param signal AbortSignal(runner.run 传播;abort 后不重试)
|
|
154
|
-
* @param trace Trace 值对象(finalizeCall 时 update)
|
|
155
|
-
* @param onEvent 实时事件回调(live record 更新用)
|
|
156
|
-
* @param stream streaming sink(透传 runner.run)
|
|
157
|
-
* @param isOrphaned 孤儿判定谓词(OB2,可选,默认恒 false):true 时 finalizeCall
|
|
158
|
-
* 跳过 trace.update(判定语义与正确性论证见 finalizeCall 文档注释)。递归重试
|
|
159
|
-
* 透传本谓词。
|
|
160
|
-
*/
|
|
161
|
-
export async function executeAgentCall(
|
|
162
|
-
call: AgentCall,
|
|
163
|
-
runner: AgentRunner,
|
|
164
|
-
budget: Budget,
|
|
165
|
-
signal: AbortSignal,
|
|
166
|
-
trace: Trace,
|
|
167
|
-
onEvent?: (event: AgentEvent) => void,
|
|
168
|
-
stream?: SubagentStream,
|
|
169
|
-
isOrphaned?: () => boolean,
|
|
170
|
-
): Promise<void> {
|
|
171
|
-
call.markRunning();
|
|
172
|
-
|
|
173
|
-
const result = await runner.run(call.opts, signal, onEvent, stream);
|
|
174
|
-
|
|
175
|
-
// 累加 usage(加权由 budget.consume 内部按权重常量处理,见 budget.ts)
|
|
176
|
-
if (result.usage) {
|
|
177
|
-
budget.consume(result.usage);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// stale-context:不重试(P1-5)
|
|
181
|
-
if (result.error !== undefined && isStaleContextErrorMsg(result.error)) {
|
|
182
|
-
finalizeCall(call, result, trace, isOrphaned);
|
|
183
|
-
budget.incrementCallCount();
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// signal 已 abort:调用方终止,不重试(避免无意义的递归)
|
|
188
|
-
if (signal.aborted) {
|
|
189
|
-
finalizeCall(call, result, trace, isOrphaned);
|
|
190
|
-
budget.incrementCallCount();
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// 预算超限:不重试(重试只会突破预算且无意义)
|
|
195
|
-
if (result.error !== undefined && budget.isExceeded()) {
|
|
196
|
-
finalizeCall(call, result, trace, isOrphaned);
|
|
197
|
-
budget.incrementCallCount();
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// 可重试失败:退避后递归
|
|
202
|
-
if (result.error !== undefined && call.attempts < MAX_ATTEMPTS) {
|
|
203
|
-
await delay(backoffDelay(call.attempts));
|
|
204
|
-
// 退避期间 signal 可能 abort
|
|
205
|
-
if (signal.aborted) {
|
|
206
|
-
finalizeCall(call, result, trace, isOrphaned);
|
|
207
|
-
budget.incrementCallCount();
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
await executeAgentCall(call, runner, budget, signal, trace, onEvent, stream, isOrphaned);
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// 终态(成功或达到重试上限的失败)
|
|
215
|
-
finalizeCall(call, result, trace, isOrphaned);
|
|
216
|
-
budget.incrementCallCount();
|
|
217
|
-
}
|
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Workflow Extension — launcher
|
|
3
|
-
*
|
|
4
|
-
* runAndWait free function(D-12)。跨扩展编程入口(pi.__workflowRun)——
|
|
5
|
-
* 阻塞至 run 到达 done 终态。
|
|
6
|
-
*
|
|
7
|
-
* **D-8 签名**:返回 WorkflowRunResult({status:"done", reason, ...})——
|
|
8
|
-
* status 恒为 "done",具体原因由 reason 区分
|
|
9
|
-
* (completed/failed/aborted/budget_limited/time_limited)。
|
|
10
|
-
*
|
|
11
|
-
* **C.7**:timeout → transition done,time_limited(仅返回 timeout 标记但不转终态
|
|
12
|
-
* 会让 workflow 仍 running,资源泄漏)。
|
|
13
|
-
*
|
|
14
|
-
* 流程:
|
|
15
|
-
* 1. registry.get(name) → WorkflowScript(未找到返回 failed)
|
|
16
|
-
* 2. script.validate(lint 检查)→ 失败抛错(不进 runWorkflow)
|
|
17
|
-
* 3. script.toExecutable → 可执行源
|
|
18
|
-
* 4. 构建 RunSpec + runWorkflow(spec, deps, signal)
|
|
19
|
-
* 5. 轮询至 done(间隔 STATUS_POLL_INTERVAL_MS)
|
|
20
|
-
* 6. timeout → abortRun + transition done,time_limited
|
|
21
|
-
* 7. signal.aborted → abortRun + reason=aborted
|
|
22
|
-
*
|
|
23
|
-
* 层归属:Engine。依赖 registry + runWorkflow/abortRun + LifecycleDeps。
|
|
24
|
-
*
|
|
25
|
-
* 参考:domain-models.md §D-8(WorkflowRunResult 签名)、clarification.md C.7。
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
import { ArgsValidationError } from "./args-validator.ts";
|
|
29
|
-
import { abortRun, runWorkflow } from "./lifecycle.ts";
|
|
30
|
-
import type { LifecycleDeps } from "./models/ports.ts";
|
|
31
|
-
import type { RunSpec } from "./models/run-spec.ts";
|
|
32
|
-
import type { DoneReason } from "./models/types.ts";
|
|
33
|
-
import type { WorkflowRun } from "./models/workflow-run.ts";
|
|
34
|
-
import type { WorkflowScriptRegistry } from "./models/workflow-script-registry.ts";
|
|
35
|
-
|
|
36
|
-
// ── 常量 ─────────────────────────────────────────────────────
|
|
37
|
-
|
|
38
|
-
/** 默认 runAndWait 超时(10 分钟)。 */
|
|
39
|
-
const DEFAULT_RUNANDWAIT_TIMEOUT_MS = 600_000;
|
|
40
|
-
|
|
41
|
-
/** 轮询间隔(500ms)。 */
|
|
42
|
-
const STATUS_POLL_INTERVAL_MS = 500;
|
|
43
|
-
|
|
44
|
-
// ── 类型 ─────────────────────────────────────────────────────
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* runAndWait 的返回(D-8 签名)。
|
|
48
|
-
*
|
|
49
|
-
* status 恒为 "done"(runAndWait 阻塞至 done 才返回);具体原因由 reason 区分
|
|
50
|
-
* (completed/failed/aborted/budget_limited/time_limited)。
|
|
51
|
-
*/
|
|
52
|
-
export interface WorkflowRunResult {
|
|
53
|
-
/** 恒为 "done"(runAndWait 阻塞至 done)。 */
|
|
54
|
-
status: "done";
|
|
55
|
-
/** 终态原因(completed/failed/aborted/budget_limited/time_limited)。 */
|
|
56
|
-
reason: DoneReason;
|
|
57
|
-
/** 脚本返回值(reason==="completed" 时有)。 */
|
|
58
|
-
scriptResult?: unknown;
|
|
59
|
-
/** 错误信息(reason!=="completed" 时可有)。 */
|
|
60
|
-
error?: string;
|
|
61
|
-
/** run 标识。 */
|
|
62
|
-
runId: string;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Launcher 依赖:LifecycleDeps + registry(脚本发现)。
|
|
67
|
-
*
|
|
68
|
-
* registry 是「发现依赖」(文件系统扫描),与 LifecycleDeps 的 3 个 port
|
|
69
|
-
* (执行依赖:子进程/线程/持久化)性质不同——故单独扩展,不进 LifecycleDeps。
|
|
70
|
-
*/
|
|
71
|
-
export interface LauncherDeps extends LifecycleDeps {
|
|
72
|
-
/** workflow 脚本仓库。 */
|
|
73
|
-
registry: WorkflowScriptRegistry;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ── 内部 helper ──────────────────────────────────────────────
|
|
77
|
-
|
|
78
|
-
/** 轮询间隔 Promise。 */
|
|
79
|
-
function pollInterval(): Promise<void> {
|
|
80
|
-
// IF10(#16):unref 使轮询等待 tick 不钉住事件循环(对齐 subagent-service
|
|
81
|
-
// gcTimer.unref?.() 先例的防御 duck-type 写法)。resolve 语义不变——unref
|
|
82
|
-
// 只影响进程退出判定,已注册 timer 仍按 500ms 触发。
|
|
83
|
-
return new Promise((resolve) => {
|
|
84
|
-
const timer = setTimeout(resolve, STATUS_POLL_INTERVAL_MS);
|
|
85
|
-
timer.unref?.();
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 从 WorkflowRun 构建 WorkflowRunResult(D-8)。
|
|
91
|
-
*
|
|
92
|
-
* reason 取 run.state.reason(done 时必有,WorkflowRun 不变式 I2 保证),
|
|
93
|
-
* 防御性 fallback "failed"(理论不可达——I2 保证 done 时 reason 已设)。
|
|
94
|
-
*/
|
|
95
|
-
function toResult(run: WorkflowRun): WorkflowRunResult {
|
|
96
|
-
return {
|
|
97
|
-
status: "done",
|
|
98
|
-
reason: run.state.reason ?? "failed",
|
|
99
|
-
scriptResult: run.state.scriptResult,
|
|
100
|
-
error: run.state.error,
|
|
101
|
-
runId: run.runId,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// ── pollRunToResult(runAndWait + executeNestedWorkflow 共用轮询) ────
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 轮询 run 至 done 终态并返回 WorkflowRunResult。
|
|
109
|
-
*
|
|
110
|
-
* runAndWait 与 executeNestedWorkflow 共用的轮询逻辑(D-12 后去重):
|
|
111
|
-
* - signal.aborted → 先查 run 是否已 done(避免二次 safeAbort 写不同 error 造成
|
|
112
|
-
* 非确定性),否则 safeAbort(aborted) + 返回 aborted 结果
|
|
113
|
-
* - run 丢失 → failed 结果
|
|
114
|
-
* - run done → toResult
|
|
115
|
-
* - deadline 到 → 先查 done,否则 safeAbort(time_limited) + 返回 timeout 结果
|
|
116
|
-
*
|
|
117
|
-
* @param abortReason signal abort 时写入 run.state.error 的原因串。runAndWait 传
|
|
118
|
-
* "Aborted by signal";executeNestedWorkflow 传 "Aborted by parent signal"。
|
|
119
|
-
*/
|
|
120
|
-
async function pollRunToResult(
|
|
121
|
-
runId: string,
|
|
122
|
-
deps: LauncherDeps,
|
|
123
|
-
signal: AbortSignal | undefined,
|
|
124
|
-
timeoutMs: number,
|
|
125
|
-
abortReason: string,
|
|
126
|
-
): Promise<WorkflowRunResult> {
|
|
127
|
-
const deadline = Date.now() + timeoutMs;
|
|
128
|
-
while (Date.now() < deadline) {
|
|
129
|
-
if (signal?.aborted) {
|
|
130
|
-
const runBeforeAbort = deps.runs.get(runId);
|
|
131
|
-
if (runBeforeAbort?.state.status === "done") return toResult(runBeforeAbort);
|
|
132
|
-
await safeAbort(runId, deps, abortReason, "aborted");
|
|
133
|
-
const run = deps.runs.get(runId);
|
|
134
|
-
return run
|
|
135
|
-
? toResult(run)
|
|
136
|
-
: { status: "done", reason: "aborted", error: abortReason, runId };
|
|
137
|
-
}
|
|
138
|
-
const run = deps.runs.get(runId);
|
|
139
|
-
if (!run) return { status: "done", reason: "failed", error: "Run not found", runId };
|
|
140
|
-
if (run.state.status === "done") return toResult(run);
|
|
141
|
-
await pollInterval();
|
|
142
|
-
}
|
|
143
|
-
const runBeforeTimeout = deps.runs.get(runId);
|
|
144
|
-
if (runBeforeTimeout?.state.status === "done") return toResult(runBeforeTimeout);
|
|
145
|
-
await safeAbort(runId, deps, `Workflow timed out after ${timeoutMs}ms`, "time_limited");
|
|
146
|
-
const finalRun = deps.runs.get(runId);
|
|
147
|
-
return finalRun
|
|
148
|
-
? toResult(finalRun)
|
|
149
|
-
: {
|
|
150
|
-
status: "done",
|
|
151
|
-
reason: "time_limited",
|
|
152
|
-
error: `Workflow timed out after ${timeoutMs}ms`,
|
|
153
|
-
runId,
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// ── runAndWait ───────────────────────────────────────────────
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 同步运行 workflow 至终态(跨扩展编程入口)。
|
|
161
|
-
*
|
|
162
|
-
* 阻塞至 run 到达 done,返回 WorkflowRunResult。用于 pi.__workflowRun 等
|
|
163
|
-
* 编程式调用——非交互场景(交互用 run + lifecycle tools)。
|
|
164
|
-
*
|
|
165
|
-
* **超时处理(C.7)**:timeout → abortRun + 返回 reason=time_limited。
|
|
166
|
-
* 旧代码返回 status:"timeout" 但 workflow 可能仍 running(资源泄漏);
|
|
167
|
-
* 本实现确保 timeout 转 done,time_limited 终态。
|
|
168
|
-
*
|
|
169
|
-
* **signal abort**:signal.aborted → abortRun + 返回 reason=aborted。
|
|
170
|
-
*
|
|
171
|
-
* **脚本未找到**:返回 reason=failed(不抛错——编程调用方据 reason 判断)。
|
|
172
|
-
*
|
|
173
|
-
* @param name workflow 脚本名(registry.get 查找)
|
|
174
|
-
* @param args 调用参数(worker 内 $ARGS 访问)
|
|
175
|
-
* @param deps LauncherDeps(LifecycleDeps + registry)
|
|
176
|
-
* @param signal 外部 abort signal(可选)
|
|
177
|
-
* @param timeoutMs 超时上限(默认 10 分钟)
|
|
178
|
-
* @returns WorkflowRunResult(status 恒 "done")
|
|
179
|
-
*/
|
|
180
|
-
export async function runAndWait(
|
|
181
|
-
name: string,
|
|
182
|
-
args: Record<string, unknown>,
|
|
183
|
-
deps: LauncherDeps,
|
|
184
|
-
signal?: AbortSignal,
|
|
185
|
-
timeoutMs: number = DEFAULT_RUNANDWAIT_TIMEOUT_MS,
|
|
186
|
-
): Promise<WorkflowRunResult> {
|
|
187
|
-
// 1. registry 查找脚本(workflowRef = 绝对路径,S2 路径统一)
|
|
188
|
-
const script = await deps.registry.getPath(name);
|
|
189
|
-
if (!script) {
|
|
190
|
-
return {
|
|
191
|
-
status: "done",
|
|
192
|
-
reason: "failed",
|
|
193
|
-
error: `Workflow '${name}' not found`,
|
|
194
|
-
runId: "",
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// 2. lint 校验(失败抛错——脚本本身有问题,不应静默吞)
|
|
199
|
-
const lintResult = script.validate();
|
|
200
|
-
if (!lintResult.valid) {
|
|
201
|
-
const errors = lintResult.findings
|
|
202
|
-
.filter((f) => f.severity === "error")
|
|
203
|
-
.map((f) => `L${f.line}: ${f.message}`)
|
|
204
|
-
.join("; ");
|
|
205
|
-
throw new Error(`Workflow script '${name}' has lint errors: ${errors}`);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// 3. 构建 RunSpec
|
|
209
|
-
// 不设 budgetTimeMs:runAndWait 自身用轮询 deadline(pollRunToResult 内 while + safeAbort)
|
|
210
|
-
// 实施 timeout,并产出「Workflow timed out after Xms」的具体错误信息。spec 级
|
|
211
|
-
// 时间预算(lifecycle.scheduleTimeBudget)服务于 fire-and-forget 的交互式 run
|
|
212
|
-
// (tool-workflow actionRun),若在此也设会与轮询 deadline 同时触发产生竞态。
|
|
213
|
-
const spec: RunSpec = {
|
|
214
|
-
scriptSource: script.toExecutable(),
|
|
215
|
-
args,
|
|
216
|
-
budgetTokens: undefined,
|
|
217
|
-
scriptName: script.name,
|
|
218
|
-
scriptPath: script.path,
|
|
219
|
-
description: script.meta.description,
|
|
220
|
-
parameters: script.meta.parameters,
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
// 4. 启动 workflow + 5. 轮询至 done(含 6. timeout → abortRun,C.7)
|
|
224
|
-
// pending-notification 的 register/unregister 由 runWorkflow(启动注册)+
|
|
225
|
-
// transition("done") 路径(完成注销)统一处理,runAndWait 不再重复 emit。
|
|
226
|
-
// m3:chokepoint 校验失败(ArgsValidationError)→ 返回 invalid_args 结果(run 从未
|
|
227
|
-
// 创建,runId 恒 ''),非 ArgsValidationError 保持传播。
|
|
228
|
-
let runId: string;
|
|
229
|
-
try {
|
|
230
|
-
runId = await runWorkflow(spec, deps, signal);
|
|
231
|
-
} catch (err) {
|
|
232
|
-
if (err instanceof ArgsValidationError) {
|
|
233
|
-
// 注:WorkflowRunResult.reason 是 pi.__workflowRun 的跨扩展公开类型——新增
|
|
234
|
-
// 'invalid_args' 成员是对外部消费方的契约变更(m3 exec-review m5);该 reason
|
|
235
|
-
// 永不进 run.state.reason(run 从未创建),仅存在于本合成返回值。
|
|
236
|
-
return {
|
|
237
|
-
status: "done",
|
|
238
|
-
reason: "invalid_args",
|
|
239
|
-
runId: "",
|
|
240
|
-
error: err.message,
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
throw err;
|
|
244
|
-
}
|
|
245
|
-
return pollRunToResult(runId, deps, signal, timeoutMs, "Aborted by signal");
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* 安全 abort——run 可能已终态(abortRun 对 done no-op,但防御兜底)。
|
|
250
|
-
*/
|
|
251
|
-
async function safeAbort(
|
|
252
|
-
runId: string,
|
|
253
|
-
deps: LauncherDeps,
|
|
254
|
-
reason: string,
|
|
255
|
-
doneReason: DoneReason,
|
|
256
|
-
): Promise<void> {
|
|
257
|
-
try {
|
|
258
|
-
await abortRun(runId, deps, reason, doneReason);
|
|
259
|
-
} catch (err) {
|
|
260
|
-
// run 可能已终态或不存在——忽略,调用方据 toResult 判断
|
|
261
|
-
void err;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// ── executeNestedWorkflow(workflow() 嵌套调用实现) ────────
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* workflow() 嵌套调用的 Engine 实现。
|
|
269
|
-
*
|
|
270
|
-
* Worker 脚本内调 workflow(name, args) 时,error-recovery.dispatchWorkflowCall 路由
|
|
271
|
-
* 到 deps.onWorkflowCall,后者(Interface 层 makeDeps 注入)委托本函数。
|
|
272
|
-
*
|
|
273
|
-
* 流程(6 步):
|
|
274
|
-
* 1. 循环检测——name 已在 parentWorkflowChain 中则拒绝(防 A→B→A 死循环)
|
|
275
|
-
* 2. signal 继承——子 run 响应父 run abort(parentController → childController)
|
|
276
|
-
* 3. registry.get + lint——失败返回 error result(不抛错,让脚本 soft-fail)
|
|
277
|
-
* 4. 构建 RunSpec(共享父 Budget 引用 + parentWorkflowChain 延长)+ runWorkflow
|
|
278
|
-
* 5. pollRunToResult 轮询至 done(复用 runAndWait 的轮询逻辑)
|
|
279
|
-
* 6. 结果转换(budget 已通过共享引用实时同步)
|
|
280
|
-
*
|
|
281
|
-
* 不走 runAndWait:runAndWait 内部构建 RunSpec 不支持 parentWorkflowChain 与 budget
|
|
282
|
-
* 共享引用,故直接构建 spec + runWorkflow + pollRunToResult。
|
|
283
|
-
*
|
|
284
|
-
* @param name 子 workflow 脚本名(registry.get 查找)
|
|
285
|
-
* @param args 调用参数(子 worker 内 $ARGS 访问)
|
|
286
|
-
* @param parentRun 发起嵌套调用的父 WorkflowRun(budget 共享 + 循环链源)
|
|
287
|
-
* @param deps LauncherDeps(与 runAndWait 同一组依赖 + registry)
|
|
288
|
-
* @returns { content, parsedOutput?, error? }——dispatchWorkflowCall 原样 postMessage 回 worker
|
|
289
|
-
*/
|
|
290
|
-
export async function executeNestedWorkflow(
|
|
291
|
-
name: string,
|
|
292
|
-
args: Record<string, unknown>,
|
|
293
|
-
parentRun: WorkflowRun,
|
|
294
|
-
deps: LauncherDeps,
|
|
295
|
-
): Promise<{ content: string; parsedOutput?: unknown; error?: string }> {
|
|
296
|
-
// Step 1: 循环检测——parentWorkflowChain 不存在时为 [](顶层 run)
|
|
297
|
-
const chain = [
|
|
298
|
-
...(parentRun.spec.parentWorkflowChain ?? []),
|
|
299
|
-
parentRun.spec.scriptName,
|
|
300
|
-
];
|
|
301
|
-
if (chain.includes(name)) {
|
|
302
|
-
return {
|
|
303
|
-
content: "",
|
|
304
|
-
error: `Circular workflow call detected: ${[...chain, name].join(" → ")}`,
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// Step 2: signal 继承——子 run 响应父 run abort
|
|
309
|
-
// [L-2] 提取命名 onParentAbort 以便 finally removeEventListener,防子 run 完成后
|
|
310
|
-
// parentSignal 上残留 listener(多次嵌套调用会累积)。
|
|
311
|
-
const childController = new AbortController();
|
|
312
|
-
const parentSignal = parentRun.runtime?.controller.signal;
|
|
313
|
-
const onParentAbort = (): void => childController.abort();
|
|
314
|
-
if (parentSignal) {
|
|
315
|
-
if (parentSignal.aborted) {
|
|
316
|
-
childController.abort();
|
|
317
|
-
} else {
|
|
318
|
-
parentSignal.addEventListener("abort", onParentAbort, { once: true });
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// Step 3+:registry 查找 + lint + RunSpec + runWorkflow + poll 全程 try(m3 E8——
|
|
323
|
-
// try 起点提到 Step 2 的 listener 注册之后,覆盖 Step 3-6。runWorkflow throw
|
|
324
|
-
// (含 chokepoint ArgsValidationError)与 not found/lint 早返回均走 finally 移除
|
|
325
|
-
// parentSignal listener——修复原 try 外 runWorkflow 的泄漏路径)。
|
|
326
|
-
try {
|
|
327
|
-
// Step 3: registry 查找 + lint(失败返回 error result,不抛错)
|
|
328
|
-
const script = await deps.registry.getPath(name);
|
|
329
|
-
if (!script) {
|
|
330
|
-
return { content: "", error: `Workflow '${name}' not found` };
|
|
331
|
-
}
|
|
332
|
-
const lintResult = script.validate();
|
|
333
|
-
if (!lintResult.valid) {
|
|
334
|
-
const errors = lintResult.findings
|
|
335
|
-
.filter((f) => f.severity === "error")
|
|
336
|
-
.map((f) => `L${f.line}: ${f.message}`)
|
|
337
|
-
.join("; ");
|
|
338
|
-
return {
|
|
339
|
-
content: "",
|
|
340
|
-
error: `Workflow script '${name}' has lint errors: ${errors}`,
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
// Step 4: 构建 RunSpec(共享父 Budget + 循环链)+ 启动子 workflow
|
|
345
|
-
// budget 共享(F-7 方案 B):子 run 直接复用父 Budget 引用(budgetRef),consume 实时
|
|
346
|
-
// 累加到父 Budget,消除并行嵌套下的超支窗口,无需 Step 6 的 sync-back。
|
|
347
|
-
const spec: RunSpec = {
|
|
348
|
-
scriptSource: script.toExecutable(),
|
|
349
|
-
args,
|
|
350
|
-
budgetRef: parentRun.state.budget,
|
|
351
|
-
// Run-level override 传播(与父 run 对齐):子 run 继承父 run 的 model/thinkingLevel,
|
|
352
|
-
// 否则嵌套 workflow 丢失父 run 的模型指定,回落主 agent 模型。
|
|
353
|
-
model: parentRun.spec.model,
|
|
354
|
-
thinkingLevel: parentRun.spec.thinkingLevel,
|
|
355
|
-
scriptName: script.name,
|
|
356
|
-
scriptPath: script.path,
|
|
357
|
-
description: script.meta.description,
|
|
358
|
-
parameters: script.meta.parameters,
|
|
359
|
-
parentWorkflowChain: chain,
|
|
360
|
-
};
|
|
361
|
-
const runId = await runWorkflow(spec, deps, childController.signal);
|
|
362
|
-
|
|
363
|
-
// Step 5: 轮询至 done(复用 runAndWait 的轮询逻辑)
|
|
364
|
-
// [H-1] 嵌套 workflow timeout 从父 run 继承:父 spec.budgetTimeMs 存在时取
|
|
365
|
-
// min(父 budget, DEFAULT),让子 run 不超出父 run 的剩余时间预算;否则用 DEFAULT。
|
|
366
|
-
// budgetRef(共享 Budget)已在 Step 4 透传给子 run 处理 token/cost 预算,
|
|
367
|
-
// 此处的 budgetTimeMs 只服务 pollRunToResult 的轮询 deadline(wall-clock 兜底)。
|
|
368
|
-
const nestedTimeoutMs = parentRun.spec.budgetTimeMs
|
|
369
|
-
? Math.min(parentRun.spec.budgetTimeMs, DEFAULT_RUNANDWAIT_TIMEOUT_MS)
|
|
370
|
-
: DEFAULT_RUNANDWAIT_TIMEOUT_MS;
|
|
371
|
-
|
|
372
|
-
const result = await pollRunToResult(
|
|
373
|
-
runId,
|
|
374
|
-
deps,
|
|
375
|
-
childController.signal,
|
|
376
|
-
nestedTimeoutMs,
|
|
377
|
-
"Aborted by parent signal",
|
|
378
|
-
);
|
|
379
|
-
|
|
380
|
-
// Step 6: 结果转换(budget 已通过共享 budgetRef 实时同步,无需 sync-back)
|
|
381
|
-
if (result.reason === "completed") {
|
|
382
|
-
const scriptResult = result.scriptResult;
|
|
383
|
-
return {
|
|
384
|
-
content:
|
|
385
|
-
typeof scriptResult === "string"
|
|
386
|
-
? scriptResult
|
|
387
|
-
: JSON.stringify(scriptResult ?? ""),
|
|
388
|
-
parsedOutput:
|
|
389
|
-
typeof scriptResult === "object" && scriptResult !== null
|
|
390
|
-
? scriptResult
|
|
391
|
-
: undefined,
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
return {
|
|
395
|
-
content: "",
|
|
396
|
-
error: result.error ?? `Workflow '${name}' ended: ${result.reason}`,
|
|
397
|
-
};
|
|
398
|
-
} catch (err) {
|
|
399
|
-
// m3:chokepoint 校验失败 → {error}(§5.3 指引文案),非 ArgsValidationError 保持传播
|
|
400
|
-
// (dispatchWorkflowCall 的 .catch 兜底转 postResult,worker 不崩)。
|
|
401
|
-
if (err instanceof ArgsValidationError) {
|
|
402
|
-
return { content: "", error: err.message };
|
|
403
|
-
}
|
|
404
|
-
throw err;
|
|
405
|
-
} finally {
|
|
406
|
-
// [L-2] 子 run done 后移除 parentSignal listener,避免累积({ once: true } 在
|
|
407
|
-
// 正常完成路径下不会自动触发,listener 残留;多次嵌套调用会泄漏到 parentSignal)。
|
|
408
|
-
parentSignal?.removeEventListener("abort", onParentAbort);
|
|
409
|
-
}
|
|
410
|
-
}
|