@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,241 +0,0 @@
|
|
|
1
|
-
// src/execution/__tests__/epipe-fallback.test.ts
|
|
2
|
-
//
|
|
3
|
-
// deliverMessage EPIPE 兜底集成测试——热路径 stdin 写入 EPIPE 时自动转冷路径 resume + 消息重放。
|
|
4
|
-
//
|
|
5
|
-
// 场景覆盖:
|
|
6
|
-
// 1. 首次 EPIPE → 自动转冷路径 resumeRound(runSpawn 收到 resume 参数 + 原消息)
|
|
7
|
-
// 2. 连续 2 次 EPIPE → throw 含恢复指引(防无限循环)
|
|
8
|
-
// 3. 热路径成功写入后清零 EPIPE 计数(正常路径不受影响)
|
|
9
|
-
|
|
10
|
-
import * as fs from "node:fs";
|
|
11
|
-
import * as os from "node:os";
|
|
12
|
-
import * as path from "node:path";
|
|
13
|
-
import type { ChildProcess } from "node:child_process";
|
|
14
|
-
import { PassThrough } from "node:stream";
|
|
15
|
-
|
|
16
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
17
|
-
|
|
18
|
-
const { loggerMock } = vi.hoisted(() => ({
|
|
19
|
-
loggerMock: { debug: vi.fn(), warn: vi.fn(), error: vi.fn(), info: vi.fn() },
|
|
20
|
-
}));
|
|
21
|
-
vi.mock("@zhushanwen/pi-extension-logger", () => ({ getLogger: () => loggerMock }));
|
|
22
|
-
|
|
23
|
-
// mock session-runner:runSpawn 受控 + killAllSpawnedChildren 空实现;
|
|
24
|
-
// getChildByRecord/spawnedChildren 提供真实 Map 语义。
|
|
25
|
-
vi.mock("../session-runner.ts", () => {
|
|
26
|
-
const spawnedChildren = new Map<string, unknown>();
|
|
27
|
-
return {
|
|
28
|
-
runSpawn: vi.fn(),
|
|
29
|
-
killAllSpawnedChildren: vi.fn(),
|
|
30
|
-
spawnedChildren,
|
|
31
|
-
getChildByRecord: (id: string): unknown => spawnedChildren.get(id),
|
|
32
|
-
};
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
import { runSpawn, spawnedChildren } from "../session-runner.ts";
|
|
36
|
-
import * as lifecycle from "../lifecycle-manager.ts";
|
|
37
|
-
import { createRecord } from "../execution-record.ts";
|
|
38
|
-
import { ModelConfigService } from "../model-config-service.ts";
|
|
39
|
-
import type { ModelInfo } from "../model-resolver.ts";
|
|
40
|
-
import { SubagentService } from "../subagent-service.ts";
|
|
41
|
-
import type { PiLike } from "../subagent-service.ts";
|
|
42
|
-
import type { AgentResult, ExecutionRecord } from "../types.ts";
|
|
43
|
-
|
|
44
|
-
const mockRunSpawn = vi.mocked(runSpawn);
|
|
45
|
-
|
|
46
|
-
function makeTmpAgentDir(): string {
|
|
47
|
-
return fs.mkdtempSync(path.join(os.tmpdir(), "epipe-test-"));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function makePi(): PiLike & {
|
|
51
|
-
appendEntry: ReturnType<typeof vi.fn>;
|
|
52
|
-
events: { emit: ReturnType<typeof vi.fn> };
|
|
53
|
-
sendMessage: ReturnType<typeof vi.fn>;
|
|
54
|
-
} {
|
|
55
|
-
return { appendEntry: vi.fn(), events: { emit: vi.fn() }, sendMessage: vi.fn() };
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function makeResult(success: boolean): AgentResult {
|
|
59
|
-
return {
|
|
60
|
-
text: success ? "done" : "err",
|
|
61
|
-
turns: 1,
|
|
62
|
-
durationMs: 100,
|
|
63
|
-
success,
|
|
64
|
-
error: success ? undefined : "boom",
|
|
65
|
-
sessionId: "sess-1",
|
|
66
|
-
toolCalls: [],
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** chatMode running-resumable record(第一轮已完成,等待续聊;v4 B-1 旧 idle 折入 running)。 */
|
|
71
|
-
function makeIdleRecord(id = "sa-epipe"): ExecutionRecord {
|
|
72
|
-
const record = createRecord(id, {
|
|
73
|
-
agent: "general-purpose",
|
|
74
|
-
model: "test/test-model",
|
|
75
|
-
thinkingLevel: "low",
|
|
76
|
-
mode: "background",
|
|
77
|
-
task: "initial task",
|
|
78
|
-
slug: "epipe-chat",
|
|
79
|
-
startedAt: 1000,
|
|
80
|
-
rootSessionId: "root-session",
|
|
81
|
-
chatMode: true,
|
|
82
|
-
});
|
|
83
|
-
record.status = "running";
|
|
84
|
-
record.round = 1;
|
|
85
|
-
record.controller = new AbortController();
|
|
86
|
-
return record;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 构造 EPIPE child:stdin.write 抛 {code:'EPIPE'},模拟子进程已退出但 close 事件尚未到达的窗口。
|
|
91
|
-
*/
|
|
92
|
-
function makeEpipeChild(): ChildProcess {
|
|
93
|
-
const fakeStdin = {
|
|
94
|
-
write: vi.fn(() => {
|
|
95
|
-
const err = Object.assign(new Error("write after end"), { code: "EPIPE" });
|
|
96
|
-
throw err;
|
|
97
|
-
}),
|
|
98
|
-
destroyed: false,
|
|
99
|
-
} as unknown as PassThrough;
|
|
100
|
-
return { stdin: fakeStdin, pid: 99999 } as unknown as ChildProcess;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 构造正常 child:PassThrough stdin(可正常写入)。
|
|
105
|
-
*/
|
|
106
|
-
function makeNormalChild(): ChildProcess {
|
|
107
|
-
return { stdin: new PassThrough(), pid: 88888 } as unknown as ChildProcess;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// ============================================================
|
|
111
|
-
// deliverMessage EPIPE 兜底
|
|
112
|
-
// ============================================================
|
|
113
|
-
|
|
114
|
-
describe("deliverMessage EPIPE 兜底(热路径 stdin EPIPE → 冷路径 resume)", () => {
|
|
115
|
-
let agentDir: string;
|
|
116
|
-
let service: SubagentService;
|
|
117
|
-
let record: ExecutionRecord;
|
|
118
|
-
|
|
119
|
-
beforeEach(() => {
|
|
120
|
-
agentDir = makeTmpAgentDir();
|
|
121
|
-
const modelService = new ModelConfigService({ agentDir });
|
|
122
|
-
service = new SubagentService({ cwd: agentDir, modelService });
|
|
123
|
-
service.initSession({ pi: makePi(), sessionId: "root-session" });
|
|
124
|
-
record = makeIdleRecord();
|
|
125
|
-
record.sessionFile = path.join(agentDir, "fake-session.jsonl");
|
|
126
|
-
spawnedChildren.clear();
|
|
127
|
-
mockRunSpawn.mockReset();
|
|
128
|
-
lifecycle._resetLifecycleState();
|
|
129
|
-
loggerMock.warn.mockClear();
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
afterEach(() => {
|
|
133
|
-
service.dispose();
|
|
134
|
-
spawnedChildren.clear();
|
|
135
|
-
lifecycle._resetLifecycleState();
|
|
136
|
-
fs.rmSync(agentDir, { recursive: true, force: true });
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it("首次 EPIPE → 自动转冷路径 resumeRound + 原消息重放", async () => {
|
|
140
|
-
mockRunSpawn.mockResolvedValueOnce(makeResult(true));
|
|
141
|
-
// 注册 EPIPE child(热路径会命中但 stdin.write 抛 EPIPE)
|
|
142
|
-
spawnedChildren.set(record.id, makeEpipeChild());
|
|
143
|
-
|
|
144
|
-
// deliverMessage 不 throw(首次 EPIPE 走 resume 兜底)
|
|
145
|
-
await expect(service.deliverMessage(record, "msg after epipe", false)).resolves.toBeUndefined();
|
|
146
|
-
|
|
147
|
-
// spawnedChildren 中的死进程条目已清理(EPIPE catch 块 delete)
|
|
148
|
-
expect(spawnedChildren.has(record.id)).toBe(false);
|
|
149
|
-
|
|
150
|
-
// resumeRound 被触发(冷路径),runSpawn 收到 resume 参数 + 原消息
|
|
151
|
-
await vi.waitFor(() => expect(mockRunSpawn).toHaveBeenCalledTimes(1));
|
|
152
|
-
const call = mockRunSpawn.mock.calls[0]!;
|
|
153
|
-
expect(call[1]).toBe("msg after epipe");
|
|
154
|
-
expect(call[4]).toEqual({
|
|
155
|
-
sessionFile: record.sessionFile,
|
|
156
|
-
model: record.model,
|
|
157
|
-
thinkingLevel: record.thinkingLevel,
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
// warn 日志包含 EPIPE 关键词
|
|
161
|
-
expect(loggerMock.warn).toHaveBeenCalled();
|
|
162
|
-
const warnCalls = loggerMock.warn.mock.calls;
|
|
163
|
-
const epipeWarn = warnCalls.find((c: unknown[]) =>
|
|
164
|
-
typeof c[0] === "string" && c[0].includes("EPIPE"),
|
|
165
|
-
);
|
|
166
|
-
expect(epipeWarn).toBeDefined();
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it("连续 2 次 EPIPE → throw 含恢复指引(防无限循环)", async () => {
|
|
170
|
-
mockRunSpawn.mockResolvedValue(makeResult(true));
|
|
171
|
-
// 注册 EPIPE child
|
|
172
|
-
spawnedChildren.set(record.id, makeEpipeChild());
|
|
173
|
-
|
|
174
|
-
// 第 1 次:EPIPE → resume(不 throw)
|
|
175
|
-
await service.deliverMessage(record, "first epipe", false);
|
|
176
|
-
await vi.waitFor(() => expect(mockRunSpawn).toHaveBeenCalledTimes(1));
|
|
177
|
-
|
|
178
|
-
// resume 完成后 record 回 running(v4 B-1:finalizeRoundToIdle 设 running,旧 idle 折入)
|
|
179
|
-
await vi.waitFor(() => expect(record.status).toBe("running"));
|
|
180
|
-
|
|
181
|
-
// 第 2 次:再次注入 EPIPE child + EPIPE
|
|
182
|
-
spawnedChildren.set(record.id, makeEpipeChild());
|
|
183
|
-
|
|
184
|
-
// 第 2 次 EPIPE → throw 含恢复指引(连续 2 次触发 exhaustion)
|
|
185
|
-
// 注意:第二 EPIPE 时 count 已为 2,catch 块立即 throw(不经过 resumeRound)。
|
|
186
|
-
let thrown: unknown;
|
|
187
|
-
try {
|
|
188
|
-
await service.deliverMessage(record, "second epipe", false);
|
|
189
|
-
} catch (err) {
|
|
190
|
-
thrown = err;
|
|
191
|
-
}
|
|
192
|
-
expect(thrown).toBeInstanceOf(Error);
|
|
193
|
-
expect((thrown as Error).message).toContain("EPIPE fallback exhausted");
|
|
194
|
-
expect((thrown as Error).message).toContain("2 consecutive EPIPE");
|
|
195
|
-
expect((thrown as Error).message).toContain("action:'close'");
|
|
196
|
-
expect((thrown as Error).message).toContain("action:'start'");
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it("热路径成功写入后清零 EPIPE 计数(正常路径不受 EPIPE 计数影响)", async () => {
|
|
200
|
-
mockRunSpawn.mockResolvedValue(makeResult(true));
|
|
201
|
-
|
|
202
|
-
// 第 1 轮:EPIPE → resume
|
|
203
|
-
spawnedChildren.set(record.id, makeEpipeChild());
|
|
204
|
-
await service.deliverMessage(record, "epipe msg", false);
|
|
205
|
-
await vi.waitFor(() => expect(mockRunSpawn).toHaveBeenCalledTimes(1));
|
|
206
|
-
await vi.waitFor(() => expect(record.status).toBe("running"));
|
|
207
|
-
|
|
208
|
-
// 第 2 轮:正常 child,成功写入(清零 EPIPE 计数)
|
|
209
|
-
spawnedChildren.set(record.id, makeNormalChild());
|
|
210
|
-
await service.deliverMessage(record, "normal msg", true);
|
|
211
|
-
// 不 throw,status=running(热路径成功)
|
|
212
|
-
expect(record.status).toBe("running");
|
|
213
|
-
|
|
214
|
-
// 第 3 轮:再次 EPIPE → 但因为第 2 轮清零了计数,这次是首次 EPIPE(不 throw)
|
|
215
|
-
// 需要先让 record 回 running(等待续聊态)
|
|
216
|
-
record.status = "running";
|
|
217
|
-
spawnedChildren.set(record.id, makeEpipeChild());
|
|
218
|
-
await expect(service.deliverMessage(record, "after reset", false)).resolves.toBeUndefined();
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it("EPIPE 兜底与冷路径共用 in-flight 守卫:resume 在途时 EPIPE 兜底不再二次 spawn(review round2 MF1)", async () => {
|
|
222
|
-
// 冷路径发起 resume,runSpawn 挂起(spawn 异步窗口内)
|
|
223
|
-
let releaseFirst!: (r: AgentResult) => void;
|
|
224
|
-
mockRunSpawn.mockImplementationOnce(
|
|
225
|
-
() => new Promise<AgentResult>((res) => { releaseFirst = res; }),
|
|
226
|
-
);
|
|
227
|
-
await service.deliverMessage(record, "cold path first", false);
|
|
228
|
-
await vi.waitFor(() => expect(mockRunSpawn).toHaveBeenCalledTimes(1));
|
|
229
|
-
|
|
230
|
-
// 注册 EPIPE child → deliverMessage 走热路径 → stdin EPIPE → 兜底调 resumeRound
|
|
231
|
-
// → in-flight 命中 → throw(不再走第二次 spawn。EPIPE 计数仍 +1 但未达阈值 2,
|
|
232
|
-
// exhaustion 分支不触发;throw 由兜底路径的 resumeRound 守卫抛出)
|
|
233
|
-
spawnedChildren.set(record.id, makeEpipeChild());
|
|
234
|
-
await expect(service.deliverMessage(record, "epipe during resume", false)).rejects.toThrow(
|
|
235
|
-
/already starting a new round/,
|
|
236
|
-
);
|
|
237
|
-
expect(mockRunSpawn).toHaveBeenCalledTimes(1);
|
|
238
|
-
|
|
239
|
-
releaseFirst(makeResult(true));
|
|
240
|
-
});
|
|
241
|
-
});
|
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
// src/__tests__/execute-and-await-worktree.test.ts
|
|
2
|
-
//
|
|
3
|
-
// executeAndAwait 的 worktree 失败收尾测试(W1 code review 修复回归锁)。
|
|
4
|
-
//
|
|
5
|
-
// 覆盖:worktreeManager.create 抛错时 record 被 finalizeFailed(status→failed)且原错外抛。
|
|
6
|
-
// (worktree 与 fork 解耦后,worktree:true+fork:false 的解耦验证见 subagent-service.test.ts。)
|
|
7
|
-
//
|
|
8
|
-
// ── mock 策略 ──
|
|
9
|
-
//
|
|
10
|
-
// 复用 execute-nesting.test.ts 的 spawn / node:fs / manifest-store / temp-prompt / alive-store
|
|
11
|
-
// / finalized-marker mock 范式(见该文件头部详细注释)。本文件 **不驱动 FakeChild 完成**——
|
|
12
|
-
// 被测的两个分支都在 runSpawn 之前抛/收尾(worktree create 在步骤 2.5,runSpawn 在步骤 5),
|
|
13
|
-
// 因此 spawn 即使被调也无人驱动,测试在抛错后立即断言即可结束。
|
|
14
|
-
//
|
|
15
|
-
// worktreeManager 是 SubagentService 构造时 new 出的私有字段(WorktreeManager 实例,非模块)。
|
|
16
|
-
// 测试 2 用 vi.spyOn(Reflect.get(service, "worktreeManager"), "create") 注入抛错,无需模块级 mock。
|
|
17
|
-
|
|
18
|
-
import { PassThrough } from "node:stream";
|
|
19
|
-
|
|
20
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
21
|
-
|
|
22
|
-
// ── mock modules(与 execute-nesting.test.ts 同范式)──
|
|
23
|
-
|
|
24
|
-
vi.mock("node:child_process", async () => {
|
|
25
|
-
const { EventEmitter } = await import("node:events");
|
|
26
|
-
const { PassThrough } = await import("node:stream");
|
|
27
|
-
|
|
28
|
-
class FakeChild extends EventEmitter {
|
|
29
|
-
pid = 12345;
|
|
30
|
-
stdout = new PassThrough();
|
|
31
|
-
stderr = new PassThrough();
|
|
32
|
-
killed = false;
|
|
33
|
-
killSignal: string | undefined;
|
|
34
|
-
kill(sig?: string): boolean {
|
|
35
|
-
this.killed = true;
|
|
36
|
-
this.killSignal = sig;
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
spawn: vi.fn(() => new FakeChild()),
|
|
43
|
-
// buildEnvBlock 的 git branch 调用(execFile 异步):默认 err-first 兜底 → catch → branch=""
|
|
44
|
-
execFile: vi.fn(
|
|
45
|
-
(
|
|
46
|
-
_cmd: string,
|
|
47
|
-
_args: readonly string[],
|
|
48
|
-
_opts: unknown,
|
|
49
|
-
cb: (err: Error | null, stdout?: string, stderr?: string) => void,
|
|
50
|
-
) => cb(new Error("execFile not configured in this test")),
|
|
51
|
-
),
|
|
52
|
-
};
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
vi.mock("node:fs", async () => {
|
|
56
|
-
const actual = await import("node:fs");
|
|
57
|
-
return {
|
|
58
|
-
default: {
|
|
59
|
-
...actual,
|
|
60
|
-
mkdirSync: vi.fn(),
|
|
61
|
-
existsSync: vi.fn(() => false),
|
|
62
|
-
appendFileSync: vi.fn(),
|
|
63
|
-
writeFileSync: vi.fn(),
|
|
64
|
-
readdirSync: vi.fn(() => []),
|
|
65
|
-
},
|
|
66
|
-
mkdirSync: vi.fn(),
|
|
67
|
-
existsSync: vi.fn(() => false),
|
|
68
|
-
appendFileSync: vi.fn(),
|
|
69
|
-
writeFileSync: vi.fn(),
|
|
70
|
-
readdirSync: vi.fn(() => []),
|
|
71
|
-
promises: actual.promises,
|
|
72
|
-
};
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
vi.mock("../alive-store.ts", async (importOriginal) => {
|
|
76
|
-
const actual = await importOriginal<typeof import("../alive-store.ts")>();
|
|
77
|
-
return {
|
|
78
|
-
...actual,
|
|
79
|
-
writeAliveMarker: vi.fn(),
|
|
80
|
-
removeAliveMarker: vi.fn(),
|
|
81
|
-
};
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
vi.mock("../finalized-marker.ts", () => ({
|
|
85
|
-
writeFinalized: vi.fn(),
|
|
86
|
-
readFinalized: vi.fn(() => false),
|
|
87
|
-
}));
|
|
88
|
-
|
|
89
|
-
vi.mock("../manifest-store.ts", () => {
|
|
90
|
-
class FakeManifestStore {
|
|
91
|
-
writeManifest = vi.fn(async () => {});
|
|
92
|
-
readManifest = vi.fn(async () => null);
|
|
93
|
-
listAllSync = vi.fn(() => []);
|
|
94
|
-
recoverTmpFiles = vi.fn(async () => []);
|
|
95
|
-
}
|
|
96
|
-
return { ManifestStore: FakeManifestStore };
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
vi.mock("../temp-prompt.ts", () => ({
|
|
100
|
-
writePromptToTempFile: vi.fn(async (agent: string) => {
|
|
101
|
-
const safeName = agent.replace(/[^\w.-]+/g, "_");
|
|
102
|
-
return { dir: `/tmp/fake-${safeName}`, filePath: `/tmp/fake-${safeName}/prompt-${safeName}.md` };
|
|
103
|
-
}),
|
|
104
|
-
cleanupTempPrompt: vi.fn(async () => {}),
|
|
105
|
-
}));
|
|
106
|
-
|
|
107
|
-
import { ModelConfigService } from "../model-config-service.ts";
|
|
108
|
-
import type { ModelInfo, ModelRegistryLike } from "../model-resolver.ts";
|
|
109
|
-
import type { RecordStore } from "../record-store.ts";
|
|
110
|
-
import type { WorktreeManager } from "../worktree-manager.ts";
|
|
111
|
-
import { SubagentService } from "../subagent-service.ts";
|
|
112
|
-
|
|
113
|
-
// ── 辅助:service 构造(与 execute-nesting.test.ts setup 等价)──
|
|
114
|
-
|
|
115
|
-
function makeEmptyRegistry(): ModelRegistryLike {
|
|
116
|
-
return { getAvailable: () => [], find: () => undefined, hasConfiguredAuth: () => true };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function makePi() {
|
|
120
|
-
return { sendMessage: vi.fn(), appendEntry: vi.fn(), events: { emit: vi.fn() } };
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
interface SetupResult {
|
|
124
|
-
service: SubagentService;
|
|
125
|
-
worktreeManager: WorktreeManager;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function setup(): SetupResult {
|
|
129
|
-
const agentDir = "/tmp/exec-await-worktree-it"; // fs 已 mock,路径不需真实存在
|
|
130
|
-
const modelService = new ModelConfigService({ agentDir });
|
|
131
|
-
modelService.initModel({
|
|
132
|
-
modelRegistry: makeEmptyRegistry(),
|
|
133
|
-
sessionId: "exec-await-worktree-it",
|
|
134
|
-
ctxModel: { id: "m", name: "M", provider: "p", reasoning: false },
|
|
135
|
-
});
|
|
136
|
-
const service = new SubagentService({
|
|
137
|
-
cwd: agentDir,
|
|
138
|
-
modelService,
|
|
139
|
-
getMainSessionFile: () => "/mock/main-session.jsonl",
|
|
140
|
-
});
|
|
141
|
-
service.initSession({ pi: makePi(), sessionId: "exec-await-worktree-it" });
|
|
142
|
-
// worktreeManager 是 SubagentService 构造时 new 的 private 字段(无外部注入入口),
|
|
143
|
-
// 测试经 Reflect.get 访问后 cast 到生产导出类型 WorktreeManager(已在文件顶部 import),
|
|
144
|
-
// 让字段/方法签名与生产类型契约绑定。
|
|
145
|
-
const worktreeManager = Reflect.get(service, "worktreeManager") as WorktreeManager;
|
|
146
|
-
return { service, worktreeManager };
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const ctxModel: ModelInfo = { id: "m", name: "M", provider: "p", reasoning: false };
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* 从 service 取出 private store(断言 record 终态用)。
|
|
153
|
-
*
|
|
154
|
-
* worktreeManager 与 store 都是 SubagentService 构造时 new 出的 private 字段——
|
|
155
|
-
* 无外部注入入口,测试只能经 Reflect.get 访问。这里 cast 到生产导出类型
|
|
156
|
-
* (RecordStore / WorktreeManager)而非内联匿名 shape,让测试与生产类型契约绑定:
|
|
157
|
-
* 字段改名/签名变更时 tsc 立即报错(而非静默漂移)。
|
|
158
|
-
*/
|
|
159
|
-
function getStore(service: SubagentService): RecordStore {
|
|
160
|
-
return Reflect.get(service, "store") as RecordStore;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
describe("executeAndAwait worktree 失败收尾", () => {
|
|
164
|
-
afterEach(() => {
|
|
165
|
-
vi.restoreAllMocks();
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
// ============================================================
|
|
169
|
-
// worktreeManager.create 抛错 → record 收尾为 failed + 原错外抛
|
|
170
|
-
// ============================================================
|
|
171
|
-
it("worktreeManager.create 失败时 finalizeFailed 收尾 record 并抛原错", async () => {
|
|
172
|
-
const { service, worktreeManager } = setup();
|
|
173
|
-
|
|
174
|
-
const createErr = new Error("worktree create boom");
|
|
175
|
-
vi.spyOn(worktreeManager, "create").mockImplementation(() => {
|
|
176
|
-
throw createErr;
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// spy store.archive:finalizeFailed 真实收尾链(CAS→completeRecord→archive)的最末一步。
|
|
180
|
-
// 捕获传入 archive 的 record,断言其 status 已被推向 "failed"(证明 finalizeFailed 完整执行,
|
|
181
|
-
// 而非仅 tryTransition 中途返回)。archive 真实执行(不 mockImplementation)以保留移出 running map 的语义。
|
|
182
|
-
const store = getStore(service);
|
|
183
|
-
const archiveSpy = vi.spyOn(store, "archive");
|
|
184
|
-
|
|
185
|
-
await expect(
|
|
186
|
-
service.executeAndAwait({
|
|
187
|
-
task: "worktree create will fail",
|
|
188
|
-
slug: "worktree-create-fail",
|
|
189
|
-
worktree: true,
|
|
190
|
-
fork: true,
|
|
191
|
-
ctxModel,
|
|
192
|
-
}),
|
|
193
|
-
).rejects.toBe(createErr);
|
|
194
|
-
|
|
195
|
-
// finalizeFailed 完整执行:record 经 CAS→completeRecord 推到 failed 终态后 archive。
|
|
196
|
-
expect(archiveSpy).toHaveBeenCalledTimes(1);
|
|
197
|
-
// store 现已强类型为 RecordStore → archive 入参为 ExecutionRecord,无需 `as` 断言。
|
|
198
|
-
const archivedRecord = archiveSpy.mock.calls[0]![0];
|
|
199
|
-
expect(archivedRecord.status).toBe("closed");
|
|
200
|
-
// archive 后 record 已移出 running map → listRunning 空、getMutable 取不到。
|
|
201
|
-
expect(store.listRunning()).toHaveLength(0);
|
|
202
|
-
expect(store.getMutable(archivedRecord.id)).toBeUndefined();
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
// ============================================================
|
|
206
|
-
// create-await 竞态守卫(Phase 2):create await 窗口内 dispose 抢先把
|
|
207
|
-
// record CAS 成 closed → 守卫 cleanup + throw cancelled(失败 throw 语义,
|
|
208
|
-
// SAR.run 的 catch 会转 AgentResult.error)。守卫 throw 不落在 try 内
|
|
209
|
-
// (否则被上方 catch 当 create 失败再走 finalizeFailed,对已 closed record 语义未定义)。
|
|
210
|
-
// ============================================================
|
|
211
|
-
it("守卫:create await 窗口内 dispose 抢先 → cleanup 被调 + throw cancelled(不进 runAndFinalize)", async () => {
|
|
212
|
-
const { service, worktreeManager } = setup();
|
|
213
|
-
|
|
214
|
-
const handle = Object.freeze({
|
|
215
|
-
path: "/tmp/wt-guard2",
|
|
216
|
-
branch: "pi-sub-guard2",
|
|
217
|
-
baseCommit: "abc123",
|
|
218
|
-
mainCwd: "/repo",
|
|
219
|
-
}) as Parameters<WorktreeManager["cleanup"]>[0];
|
|
220
|
-
let resolveCreate!: (h: unknown) => void;
|
|
221
|
-
vi.spyOn(worktreeManager, "create").mockImplementation(
|
|
222
|
-
() => new Promise((r) => { resolveCreate = r; }) as ReturnType<WorktreeManager["create"]>,
|
|
223
|
-
);
|
|
224
|
-
const cleanupSpy = vi.spyOn(worktreeManager, "cleanup").mockResolvedValue(undefined);
|
|
225
|
-
|
|
226
|
-
const execP = service.executeAndAwait({
|
|
227
|
-
task: "guard test",
|
|
228
|
-
slug: "guard-test",
|
|
229
|
-
worktree: true,
|
|
230
|
-
fork: true,
|
|
231
|
-
ctxModel,
|
|
232
|
-
});
|
|
233
|
-
// 微任务推进:record 已建 + executeAndAwait 挂在 pending create 上
|
|
234
|
-
await new Promise((r) => { setTimeout(r, 0); });
|
|
235
|
-
// dispose 抢先:CAS running → closed(此刻 worktreeHandle 仍 undefined,
|
|
236
|
-
// dispose 的 fire-and-forget cleanup 跳过)——守卫是唯一清理点
|
|
237
|
-
service.disposeAllRecords("parent-shutdown");
|
|
238
|
-
resolveCreate(handle);
|
|
239
|
-
|
|
240
|
-
await expect(execP).rejects.toThrow("cancelled during worktree creation");
|
|
241
|
-
expect(cleanupSpy).toHaveBeenCalledWith(handle);
|
|
242
|
-
});
|
|
243
|
-
});
|