@zhushanwen/pi-subagent-workflow 8.6.0 → 8.8.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/README.md +5 -11
- package/package.json +13 -20
- package/relay/relay.mjs +1 -1
- package/skills/workflow-script-format/SKILL.md +32 -13
- package/src/host/__tests__/pi-host.test.ts +235 -0
- package/src/host/pi-host.ts +163 -0
- package/src/index.ts +141 -138
- package/src/injectors/__tests__/engine-awareness.test.ts +292 -0
- package/src/injectors/__tests__/engine-section-stability.test.ts +343 -0
- package/src/injectors/__tests__/model-list-injector.test.ts +18 -21
- package/src/injectors/__tests__/subagent-list-injector.test.ts +59 -19
- package/src/injectors/__tests__/workflow-list-injector.test.ts +31 -17
- package/src/injectors/engine-awareness.ts +119 -0
- package/src/injectors/model-list-injector.ts +15 -58
- package/src/injectors/subagent-list-injector.ts +56 -114
- package/src/injectors/workflow-list-injector.ts +37 -70
- package/src/interface/__tests__/detectors.test.ts +48 -37
- package/src/interface/__tests__/subagent-tool-path-guard.test.ts +2 -2
- package/src/interface/__tests__/subagent-tool-prompt.test.ts +6 -3
- package/src/interface/__tests__/tool-workflow-run-builtin-name.test.ts +216 -0
- package/src/interface/__tests__/tool-workflow-script-generate.test.ts +17 -6
- package/src/interface/__tests__/tool-workflow-throw-paths.test.ts +38 -2
- package/src/interface/bg-notify-render.ts +7 -17
- package/src/interface/command-actions.ts +5 -15
- package/src/interface/commands.ts +4 -4
- package/src/interface/format.ts +17 -6
- package/src/interface/gui-mappers.ts +18 -22
- package/src/interface/helpers.ts +9 -130
- package/src/interface/list-component.ts +5 -5
- package/src/interface/list-shared.ts +3 -3
- package/src/interface/list-view.ts +3 -3
- package/src/interface/subagent-actions.ts +66 -439
- package/src/interface/subagent-tool-schema.ts +21 -17
- package/src/interface/subagent-tool.ts +12 -7
- package/src/interface/subagents.ts +4 -4
- package/src/interface/tool-render.ts +37 -15
- package/src/interface/tool-workflow-script.ts +34 -76
- package/src/interface/tool-workflow.ts +57 -94
- package/src/interface/views/WorkflowsView.ts +8 -16
- 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 +23 -62
- package/src/{orchestration/jsonl-run-store.ts → jsonl-run-store.ts} +137 -225
- package/agents/analyst.md +0 -61
- package/agents/coder.md +0 -70
- package/agents/debugger.md +0 -67
- package/agents/doc-reviewer.md +0 -50
- package/agents/explorer.md +0 -64
- package/agents/general-purpose.md +0 -32
- package/agents/orchestrator.md +0 -63
- package/agents/planner.md +0 -54
- package/agents/researcher.md +0 -65
- package/agents/reviewer.md +0 -74
- 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 -329
- package/src/execution/__tests__/channel-registry-handshake.test.ts +0 -243
- package/src/execution/__tests__/chat-engine-routing.test.ts +0 -601
- 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 -422
- 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 -200
- package/src/execution/__tests__/execution-record.test.ts +0 -1394
- package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +0 -171
- 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 -169
- 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 -242
- 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 -672
- package/src/execution/__tests__/lifecycle-manager-lock.test.ts +0 -211
- package/src/execution/__tests__/lifecycle-manager.test.ts +0 -383
- 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 -140
- package/src/execution/__tests__/manifest-parentid.test.ts +0 -117
- package/src/execution/__tests__/model-resolver.test.ts +0 -465
- 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 -612
- package/src/execution/__tests__/notifier-golden-snapshot.test.ts +0 -261
- package/src/execution/__tests__/notify-ledger.test.ts +0 -826
- package/src/execution/__tests__/one-shot-upgrade.test.ts +0 -205
- package/src/execution/__tests__/output-collector.test.ts +0 -358
- 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 -687
- 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__/run-spawn-stdout-callback-throw.test.ts +0 -199
- 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 -350
- package/src/execution/__tests__/session-start-reaper.test.ts +0 -233
- package/src/execution/__tests__/spawn-args.test.ts +0 -445
- 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__/start-sync-model-guard.test.ts +0 -150
- 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 -599
- package/src/execution/__tests__/temp-prompt.test.ts +0 -53
- package/src/execution/__tests__/timeout-integration.test.ts +0 -615
- package/src/execution/__tests__/tombstone-store.test.ts +0 -73
- package/src/execution/__tests__/tool-action.test.ts +0 -476
- 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 -199
- package/src/execution/agent-result-mapper.ts +0 -90
- package/src/execution/alive-store.ts +0 -92
- package/src/execution/argv-mirror.ts +0 -113
- 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 -580
- 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 -658
- 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 -309
- package/src/execution/execute-options-mapper.ts +0 -112
- package/src/execution/execution-record.ts +0 -965
- 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 -513
- package/src/execution/lifecycle-predicates.ts +0 -65
- package/src/execution/manifest-store.ts +0 -256
- package/src/execution/model-config-service.ts +0 -219
- package/src/execution/model-resolver.ts +0 -248
- package/src/execution/notifier.ts +0 -330
- package/src/execution/notify-ledger.ts +0 -580
- package/src/execution/output-collector.ts +0 -228
- 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 -1781
- 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 -2141
- package/src/execution/subprocess-agent-runner.ts +0 -317
- 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 -973
- 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 -184
- 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 -349
- package/src/orchestration/__tests__/agent-call-catch-fallback.test.ts +0 -202
- package/src/orchestration/__tests__/agent-call-stream.test.ts +0 -152
- package/src/orchestration/__tests__/args-validator.test.ts +0 -143
- package/src/orchestration/__tests__/config-loader.test.ts +0 -503
- package/src/orchestration/__tests__/error-recovery-handlers.test.ts +0 -809
- 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 -451
- package/src/orchestration/__tests__/jsonl-run-store-corrupt-entry.test.ts +0 -150
- package/src/orchestration/__tests__/jsonl-run-store-loadall-sources.test.ts +0 -171
- package/src/orchestration/__tests__/jsonl-run-store-retention.test.ts +0 -202
- package/src/orchestration/__tests__/jsonl-run-store-session-file.test.ts +0 -1030
- package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +0 -600
- package/src/orchestration/__tests__/lifecycle-runid-injection.test.ts +0 -96
- package/src/orchestration/__tests__/lifecycle.test.ts +0 -659
- package/src/orchestration/__tests__/non-cloneable-return-e2e.test.ts +0 -95
- package/src/orchestration/__tests__/review-fix-loop-e2e.test.ts +0 -2025
- package/src/orchestration/__tests__/script-lint.test.ts +0 -831
- package/src/orchestration/__tests__/skill-discovery.test.ts +0 -201
- package/src/orchestration/__tests__/test-mocks.ts +0 -197
- package/src/orchestration/__tests__/worker-exit-without-result.test.ts +0 -368
- 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 -563
- package/src/orchestration/__tests__/worker-script-builder.test.ts +0 -309
- package/src/orchestration/__tests__/worker-script-template-snapshot.test.ts +0 -129
- 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 -174
- package/src/orchestration/args-validator.ts +0 -127
- package/src/orchestration/config-loader.ts +0 -315
- package/src/orchestration/error-recovery.ts +0 -1018
- package/src/orchestration/execute-agent-call.ts +0 -256
- package/src/orchestration/launcher.ts +0 -455
- package/src/orchestration/lifecycle.ts +0 -399
- package/src/orchestration/models/__tests__/budget.test.ts +0 -307
- 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 -118
- package/src/orchestration/models/ports.ts +0 -164
- package/src/orchestration/models/run-runtime.ts +0 -104
- 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 -301
- 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 -115
- package/src/orchestration/worker-handle.ts +0 -115
- package/src/orchestration/worker-host.ts +0 -98
- package/src/orchestration/worker-script-builder.ts +0 -421
- 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__/model-ref.test.ts +0 -306
- 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/__tests__/timer-delay.test.ts +0 -61
- 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/model-ref.ts +0 -286
- package/src/shared/resource-discovery.ts +0 -811
- package/src/shared/resource-meta.ts +0 -65
- package/src/shared/schema-env.ts +0 -44
- package/src/shared/schema-jsonify.ts +0 -58
- package/src/shared/timer-delay.ts +0 -54
- 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 -1309
- package/workflows/review-fix-loop.js +0 -1404
- package/workflows/scatter-gather.js +0 -178
|
@@ -1,448 +0,0 @@
|
|
|
1
|
-
// src/execution/__tests__/relay-agent.test.ts
|
|
2
|
-
//
|
|
3
|
-
// relay.mjs 代理 CLI 协议行为集成测试(E-1 验收)——本地环回 socket + 伪 runtime
|
|
4
|
-
// (net.createServer 模拟 runtime 侧行为),spawn 真实代理进程逐条断言:
|
|
5
|
-
// 1. 启动自检:归属 env(SESSION_ID/RECORD_ID)缺失 / socket env 缺失 → 退出码 13
|
|
6
|
-
// 2. socket 不可达(重试一次后仍失败)→ 退出码 11 + stderr 含路径与恢复指引
|
|
7
|
-
// 3. 握手帧字段完整(v/kind/mainSessionId/recordId/argv/env/cwd)
|
|
8
|
-
// 4. down/up/up-stderr 字节往返保真(中文/换行/空字节的二进制 buffer)
|
|
9
|
-
// 5. exit 帧退出码传播 / 退出前 stdout 已 flush
|
|
10
|
-
// 6. exit 帧 signal 传播(spawn 方观察到 (code=null, signal))
|
|
11
|
-
// 7. socket 断 → 退出码 12(崩溃矩阵②的 extension 侧感知机制)
|
|
12
|
-
// 8. reject version → 退出码 10 + 重装指引;reject 其他 reason → 12
|
|
13
|
-
// 9. 背压:1MB 随机字节经代理双向环回(stdin→down→server→up→stdout)收齐无丢字节
|
|
14
|
-
|
|
15
|
-
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
|
|
16
|
-
import * as crypto from "node:crypto";
|
|
17
|
-
import * as fs from "node:fs";
|
|
18
|
-
import * as net from "node:net";
|
|
19
|
-
import * as os from "node:os";
|
|
20
|
-
import * as path from "node:path";
|
|
21
|
-
import { fileURLToPath } from "node:url";
|
|
22
|
-
|
|
23
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
24
|
-
|
|
25
|
-
import {
|
|
26
|
-
RELAY_ENV_NODE,
|
|
27
|
-
RELAY_ENV_RECORD_ID,
|
|
28
|
-
RELAY_ENV_SCRIPT,
|
|
29
|
-
RELAY_ENV_SESSION_ID,
|
|
30
|
-
RELAY_ENV_SOCKET,
|
|
31
|
-
RELAY_EXIT_CODES,
|
|
32
|
-
RELAY_PROTOCOL_VERSION,
|
|
33
|
-
} from "../relay-env.ts";
|
|
34
|
-
|
|
35
|
-
/** 被测脚本:包根 relay/relay.mjs(与 src/ 平行的零依赖脚本,不参与 TS 编译)。 */
|
|
36
|
-
const RELAY_SCRIPT_PATH = path.resolve(
|
|
37
|
-
path.dirname(fileURLToPath(import.meta.url)),
|
|
38
|
-
"../../../relay/relay.mjs",
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
/** 协议帧(代理 ↔ runtime JSONL)。字段全部可选除 kind——按 kind 分支后逐字段断言。 */
|
|
42
|
-
interface RelayFrame {
|
|
43
|
-
kind: string;
|
|
44
|
-
v?: unknown;
|
|
45
|
-
dir?: unknown;
|
|
46
|
-
b64?: unknown;
|
|
47
|
-
code?: unknown;
|
|
48
|
-
signal?: unknown;
|
|
49
|
-
reason?: unknown;
|
|
50
|
-
supported?: unknown;
|
|
51
|
-
mainSessionId?: unknown;
|
|
52
|
-
recordId?: unknown;
|
|
53
|
-
argv?: unknown;
|
|
54
|
-
env?: unknown;
|
|
55
|
-
cwd?: unknown;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** JSON.parse + 运行时守卫(禁裸 as):非对象或无 string kind 视为不可解析。 */
|
|
59
|
-
function parseFrame(line: string): RelayFrame | null {
|
|
60
|
-
let parsed: unknown;
|
|
61
|
-
try {
|
|
62
|
-
parsed = JSON.parse(line);
|
|
63
|
-
} catch {
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
if (typeof parsed !== "object" || parsed === null) return null;
|
|
67
|
-
const kind = (parsed as Record<string, unknown>).kind;
|
|
68
|
-
if (typeof kind !== "string") return null;
|
|
69
|
-
return { ...(parsed as Record<string, unknown>), kind };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
interface FakeRuntime {
|
|
73
|
-
socketPath: string;
|
|
74
|
-
server: net.Server;
|
|
75
|
-
conn: net.Socket | null;
|
|
76
|
-
/** 第一帧 handshake(默认 respond 逻辑 accept 后 resolve;自定义 respond 也 resolve)。 */
|
|
77
|
-
handshakePromise: Promise<RelayFrame>;
|
|
78
|
-
/** 收到的全部 down 帧解码后的字节块。 */
|
|
79
|
-
downChunks: Buffer[];
|
|
80
|
-
downTotal: () => number;
|
|
81
|
-
send: (frame: Record<string, unknown>) => boolean;
|
|
82
|
-
destroyConn: () => void;
|
|
83
|
-
close: () => Promise<void>;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** 默认 runtime 行为:收 handshake → 回 accept。 */
|
|
87
|
-
function defaultRespond(_handshake: RelayFrame, rt: FakeRuntime): void {
|
|
88
|
-
rt.send({ v: RELAY_PROTOCOL_VERSION, kind: "accept" });
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function startFakeRuntime(
|
|
92
|
-
socketPath: string,
|
|
93
|
-
respond: (handshake: RelayFrame, rt: FakeRuntime) => void = defaultRespond,
|
|
94
|
-
): Promise<FakeRuntime> {
|
|
95
|
-
// handshake 的 settle 状态(resolve 回调 + 已收到帧),供「仅首个 handshake 帧响应」判定
|
|
96
|
-
const handshakeState: { resolve: ((f: RelayFrame) => void) | null; frame: RelayFrame | null } = {
|
|
97
|
-
resolve: null,
|
|
98
|
-
frame: null,
|
|
99
|
-
};
|
|
100
|
-
const handshakePromise = new Promise<RelayFrame>((resolve) => {
|
|
101
|
-
handshakeState.resolve = resolve;
|
|
102
|
-
});
|
|
103
|
-
let connRef: net.Socket | null = null;
|
|
104
|
-
const downChunks: Buffer[] = [];
|
|
105
|
-
const server = net.createServer((conn) => {
|
|
106
|
-
connRef = conn;
|
|
107
|
-
let lineBuf = "";
|
|
108
|
-
conn.setEncoding("utf8");
|
|
109
|
-
conn.on("data", (chunk: string) => {
|
|
110
|
-
lineBuf += chunk;
|
|
111
|
-
let nl: number;
|
|
112
|
-
while ((nl = lineBuf.indexOf("\n")) >= 0) {
|
|
113
|
-
const line = lineBuf.slice(0, nl);
|
|
114
|
-
lineBuf = lineBuf.slice(nl + 1);
|
|
115
|
-
if (!line.trim()) continue;
|
|
116
|
-
const frame = parseFrame(line);
|
|
117
|
-
if (!frame) continue;
|
|
118
|
-
if (frame.kind === "handshake") {
|
|
119
|
-
// 仅首个 handshake 帧响应 + settle(单连接单握手是协议形态)
|
|
120
|
-
if (handshakeState.frame === null) {
|
|
121
|
-
handshakeState.frame = frame;
|
|
122
|
-
respond(frame, rt);
|
|
123
|
-
handshakeState.resolve?.(frame);
|
|
124
|
-
}
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
if (frame.kind === "data" && frame.dir === "down" && typeof frame.b64 === "string") {
|
|
128
|
-
downChunks.push(Buffer.from(frame.b64, "base64"));
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
const rt: FakeRuntime = {
|
|
134
|
-
socketPath,
|
|
135
|
-
server,
|
|
136
|
-
get conn() {
|
|
137
|
-
return connRef;
|
|
138
|
-
},
|
|
139
|
-
downChunks,
|
|
140
|
-
downTotal: () => downChunks.reduce((sum, c) => sum + c.length, 0),
|
|
141
|
-
send: (frame) => {
|
|
142
|
-
if (!connRef || connRef.destroyed) throw new Error("fake runtime: no live connection");
|
|
143
|
-
return connRef.write(JSON.stringify(frame) + "\n");
|
|
144
|
-
},
|
|
145
|
-
destroyConn: () => {
|
|
146
|
-
connRef?.destroy();
|
|
147
|
-
},
|
|
148
|
-
handshakePromise,
|
|
149
|
-
close: () =>
|
|
150
|
-
new Promise<void>((resolve) => {
|
|
151
|
-
server.close(() => resolve());
|
|
152
|
-
}),
|
|
153
|
-
};
|
|
154
|
-
return new Promise<FakeRuntime>((resolve, reject) => {
|
|
155
|
-
server.once("error", reject);
|
|
156
|
-
server.listen(socketPath, () => {
|
|
157
|
-
activeServers.push(server);
|
|
158
|
-
resolve(rt);
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
interface SpawnOpts {
|
|
164
|
-
/** undefined = 不注入该 env(测缺失路径)。 */
|
|
165
|
-
socketPath?: string;
|
|
166
|
-
sessionId?: string;
|
|
167
|
-
recordId?: string;
|
|
168
|
-
argv?: string[];
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* spawn 代理进程(node 直跑 relay.mjs,env 精确控制 5 个 relay 变量)。
|
|
173
|
-
* stdout/stderr 收集器随 spawn 立即挂上——对齐真实契约(extension 的 stdout pump
|
|
174
|
-
* 从 spawn 起消费;若测试侧不读,代理的 stdout 背压会正确地暂停 socket,测试卡死)。
|
|
175
|
-
*/
|
|
176
|
-
interface CollectedChild {
|
|
177
|
-
child: ChildProcessWithoutNullStreams;
|
|
178
|
-
stdoutChunks: Buffer[];
|
|
179
|
-
stderrChunks: Buffer[];
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function spawnRelay(opts: SpawnOpts): CollectedChild {
|
|
183
|
-
const env: NodeJS.ProcessEnv = { ...process.env };
|
|
184
|
-
delete env[RELAY_ENV_SOCKET];
|
|
185
|
-
delete env[RELAY_ENV_NODE];
|
|
186
|
-
delete env[RELAY_ENV_SCRIPT];
|
|
187
|
-
delete env[RELAY_ENV_SESSION_ID];
|
|
188
|
-
delete env[RELAY_ENV_RECORD_ID];
|
|
189
|
-
if (opts.socketPath !== undefined) env[RELAY_ENV_SOCKET] = opts.socketPath;
|
|
190
|
-
if (opts.sessionId !== undefined) env[RELAY_ENV_SESSION_ID] = opts.sessionId;
|
|
191
|
-
if (opts.recordId !== undefined) env[RELAY_ENV_RECORD_ID] = opts.recordId;
|
|
192
|
-
const child = spawn(process.execPath, [RELAY_SCRIPT_PATH, ...(opts.argv ?? ["--mode", "rpc"])], {
|
|
193
|
-
env,
|
|
194
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
195
|
-
});
|
|
196
|
-
const collected: CollectedChild = {
|
|
197
|
-
child,
|
|
198
|
-
stdoutChunks: [],
|
|
199
|
-
stderrChunks: [],
|
|
200
|
-
};
|
|
201
|
-
child.stdout.on("data", (c: Buffer) => collected.stdoutChunks.push(c));
|
|
202
|
-
child.stderr.on("data", (c: Buffer) => collected.stderrChunks.push(c));
|
|
203
|
-
activeChildren.push(child);
|
|
204
|
-
return collected;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
interface ExitResult {
|
|
208
|
-
code: number | null;
|
|
209
|
-
signal: NodeJS.Signals | null;
|
|
210
|
-
stderr: string;
|
|
211
|
-
stdout: Buffer;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** 等 child close,输出取自 spawn 时挂起的收集器(close 时数据已齐)。 */
|
|
215
|
-
function waitForExit({ child, stdoutChunks, stderrChunks }: CollectedChild): Promise<ExitResult> {
|
|
216
|
-
return new Promise((resolve, reject) => {
|
|
217
|
-
child.on("error", reject);
|
|
218
|
-
child.on("close", (code, signal) => {
|
|
219
|
-
resolve({
|
|
220
|
-
code,
|
|
221
|
-
signal,
|
|
222
|
-
stdout: Buffer.concat(stdoutChunks),
|
|
223
|
-
stderr: Buffer.concat(stderrChunks).toString("utf8"),
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/** 轮询等待条件成立(25ms 间隔;超时抛错)。 */
|
|
230
|
-
async function pollUntil(predicate: () => boolean, timeoutMs: number, what: string): Promise<void> {
|
|
231
|
-
const deadline = Date.now() + timeoutMs;
|
|
232
|
-
while (!predicate()) {
|
|
233
|
-
if (Date.now() > deadline) throw new Error(`timeout waiting for ${what}`);
|
|
234
|
-
await new Promise((r) => setTimeout(r, 25));
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/** 分块发送大 up 帧(64KB/帧 + socket drain 等待,驱动代理侧 socket→stdout 背压路径)。 */
|
|
239
|
-
async function sendLargeUp(rt: FakeRuntime, data: Buffer): Promise<void> {
|
|
240
|
-
const CHUNK = 64 * 1024;
|
|
241
|
-
for (let i = 0; i < data.length; i += CHUNK) {
|
|
242
|
-
const frame = {
|
|
243
|
-
v: RELAY_PROTOCOL_VERSION,
|
|
244
|
-
kind: "data",
|
|
245
|
-
dir: "up",
|
|
246
|
-
b64: data.subarray(i, i + CHUNK).toString("base64"),
|
|
247
|
-
};
|
|
248
|
-
if (!rt.send(frame)) {
|
|
249
|
-
await new Promise<void>((resolve) => {
|
|
250
|
-
const c = rt.conn;
|
|
251
|
-
if (!c) return resolve();
|
|
252
|
-
c.once("drain", () => resolve());
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const activeServers: net.Server[] = [];
|
|
259
|
-
const activeChildren: ChildProcessWithoutNullStreams[] = [];
|
|
260
|
-
const activeTmpDirs: string[] = [];
|
|
261
|
-
|
|
262
|
-
function makeSocketPath(): string {
|
|
263
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "relay-agent-test-"));
|
|
264
|
-
activeTmpDirs.push(dir);
|
|
265
|
-
return path.join(dir, "relay.sock");
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
afterEach(async () => {
|
|
269
|
-
for (const child of activeChildren) {
|
|
270
|
-
if (!child.killed) child.kill("SIGKILL");
|
|
271
|
-
}
|
|
272
|
-
activeChildren.length = 0;
|
|
273
|
-
await Promise.all(
|
|
274
|
-
activeServers.map(
|
|
275
|
-
(s) => new Promise<void>((resolve) => s.close(() => resolve())),
|
|
276
|
-
),
|
|
277
|
-
);
|
|
278
|
-
activeServers.length = 0;
|
|
279
|
-
for (const dir of activeTmpDirs) fs.rmSync(dir, { recursive: true, force: true });
|
|
280
|
-
activeTmpDirs.length = 0;
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
describe("relay agent CLI (relay.mjs)", () => {
|
|
284
|
-
it("identity env missing → exit 13 + stderr names the env vars", { timeout: 10_000 }, async () => {
|
|
285
|
-
const child = spawnRelay({});
|
|
286
|
-
const res = await waitForExit(child);
|
|
287
|
-
expect(res.code).toBe(RELAY_EXIT_CODES.MISSING_IDENTITY);
|
|
288
|
-
expect(res.stderr).toContain(RELAY_ENV_SESSION_ID);
|
|
289
|
-
expect(res.stderr).toContain(RELAY_ENV_RECORD_ID);
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
it("socket env missing (identity present) → exit 13", { timeout: 10_000 }, async () => {
|
|
293
|
-
const child = spawnRelay({ sessionId: "sess-1", recordId: "rec-1" });
|
|
294
|
-
const res = await waitForExit(child);
|
|
295
|
-
expect(res.code).toBe(RELAY_EXIT_CODES.MISSING_IDENTITY);
|
|
296
|
-
expect(res.stderr).toContain(RELAY_ENV_SOCKET);
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
it("socket unreachable → retry once then exit 11 with path + recovery hint", { timeout: 15_000 }, async () => {
|
|
300
|
-
const socketPath = path.join(makeSocketPath(), "never-listening.sock");
|
|
301
|
-
const child = spawnRelay({ socketPath, sessionId: "sess-1", recordId: "rec-1" });
|
|
302
|
-
const res = await waitForExit(child);
|
|
303
|
-
expect(res.code).toBe(RELAY_EXIT_CODES.SOCKET_UNREACHABLE);
|
|
304
|
-
expect(res.stderr).toContain(socketPath);
|
|
305
|
-
expect(res.stderr).toContain("重试任务");
|
|
306
|
-
expect(res.stderr).toContain("重启 xyz-agent");
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
it("handshake frame carries full spawn spec (v/kind/mainSessionId/recordId/argv/env/cwd)", { timeout: 15_000 }, async () => {
|
|
310
|
-
const socketPath = makeSocketPath();
|
|
311
|
-
const rt = await startFakeRuntime(socketPath);
|
|
312
|
-
const argv = ["--mode", "rpc", "--model", "test/model"];
|
|
313
|
-
const child = spawnRelay({ socketPath, sessionId: "sess-42", recordId: "rec-99", argv });
|
|
314
|
-
const hs = await rt.handshakePromise;
|
|
315
|
-
expect(hs.kind).toBe("handshake");
|
|
316
|
-
expect(hs.v).toBe(RELAY_PROTOCOL_VERSION);
|
|
317
|
-
expect(hs.mainSessionId).toBe("sess-42");
|
|
318
|
-
expect(hs.recordId).toBe("rec-99");
|
|
319
|
-
expect(hs.argv).toEqual(argv);
|
|
320
|
-
expect(hs.cwd).toBe(process.cwd());
|
|
321
|
-
// env 全量转发:含注入的归属 env 与 socket env(runtime spawn 真实 pi 原样使用)
|
|
322
|
-
expect(hs.env).toBeTypeOf("object");
|
|
323
|
-
expect(hs.env).toHaveProperty(RELAY_ENV_SESSION_ID, "sess-42");
|
|
324
|
-
expect(hs.env).toHaveProperty(RELAY_ENV_RECORD_ID, "rec-99");
|
|
325
|
-
expect(hs.env).toHaveProperty(RELAY_ENV_SOCKET, socketPath);
|
|
326
|
-
// 收尾:默认 respond 已 accept,发 exit 让子进程退出
|
|
327
|
-
rt.send({ kind: "exit", code: 0 });
|
|
328
|
-
const res = await waitForExit(child);
|
|
329
|
-
expect(res.code).toBe(0);
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
it("byte roundtrip fidelity: down/up/up-stderr with CJK, newlines, NUL bytes", { timeout: 15_000 }, async () => {
|
|
333
|
-
const socketPath = makeSocketPath();
|
|
334
|
-
const rt = await startFakeRuntime(socketPath);
|
|
335
|
-
const child = spawnRelay({ socketPath, sessionId: "s", recordId: "r" });
|
|
336
|
-
await rt.handshakePromise;
|
|
337
|
-
|
|
338
|
-
// 下行字节:JSON 命令行 + 中文 + 换行(挑战 JSONL 行协议)+ 空字节(挑战文本假设)
|
|
339
|
-
const downBytes = Buffer.concat([
|
|
340
|
-
Buffer.from('{"type":"prompt","message":"你好,世界\n', "utf8"),
|
|
341
|
-
Buffer.from([0x00, 0x01, 0xff, 0xfe]),
|
|
342
|
-
Buffer.from('第二行"}\n', "utf8"),
|
|
343
|
-
]);
|
|
344
|
-
child.child.stdin.write(downBytes);
|
|
345
|
-
await pollUntil(() => rt.downTotal() === downBytes.length, 5_000, "down bytes");
|
|
346
|
-
expect(Buffer.concat(rt.downChunks)).toEqual(downBytes);
|
|
347
|
-
|
|
348
|
-
// 上行 stdout / stderr 字节保真
|
|
349
|
-
const upBytes = Buffer.concat([
|
|
350
|
-
Buffer.from("stdout 流式输出\nline2\0", "utf8"),
|
|
351
|
-
crypto.randomBytes(64),
|
|
352
|
-
]);
|
|
353
|
-
const upStderrBytes = Buffer.concat([
|
|
354
|
-
Buffer.from("stderr 诊断\n\0字节", "utf8"),
|
|
355
|
-
crypto.randomBytes(32),
|
|
356
|
-
]);
|
|
357
|
-
rt.send({ v: RELAY_PROTOCOL_VERSION, kind: "data", dir: "up", b64: upBytes.toString("base64") });
|
|
358
|
-
rt.send({
|
|
359
|
-
v: RELAY_PROTOCOL_VERSION,
|
|
360
|
-
kind: "data",
|
|
361
|
-
dir: "up-stderr",
|
|
362
|
-
b64: upStderrBytes.toString("base64"),
|
|
363
|
-
});
|
|
364
|
-
rt.send({ kind: "exit", code: 3 });
|
|
365
|
-
const res = await waitForExit(child);
|
|
366
|
-
expect(res.stdout).toEqual(upBytes);
|
|
367
|
-
expect(res.stderr).toEqual(upStderrBytes.toString("utf8"));
|
|
368
|
-
expect(res.code).toBe(3);
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
it("exit frame code propagation flushes stdout first", { timeout: 10_000 }, async () => {
|
|
372
|
-
const socketPath = makeSocketPath();
|
|
373
|
-
const rt = await startFakeRuntime(socketPath);
|
|
374
|
-
const child = spawnRelay({ socketPath, sessionId: "s", recordId: "r" });
|
|
375
|
-
await rt.handshakePromise;
|
|
376
|
-
const upBytes = Buffer.from("final bytes before exit\n", "utf8");
|
|
377
|
-
rt.send({ v: RELAY_PROTOCOL_VERSION, kind: "data", dir: "up", b64: upBytes.toString("base64") });
|
|
378
|
-
rt.send({ kind: "exit", code: 7 });
|
|
379
|
-
const res = await waitForExit(child);
|
|
380
|
-
expect(res.code).toBe(7);
|
|
381
|
-
expect(res.stdout).toEqual(upBytes);
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
it("exit frame signal propagation → close(null, SIGTERM)", { timeout: 10_000 }, async () => {
|
|
385
|
-
const socketPath = makeSocketPath();
|
|
386
|
-
const rt = await startFakeRuntime(socketPath);
|
|
387
|
-
const child = spawnRelay({ socketPath, sessionId: "s", recordId: "r" });
|
|
388
|
-
await rt.handshakePromise;
|
|
389
|
-
rt.send({ kind: "exit", signal: "SIGTERM" });
|
|
390
|
-
const res = await waitForExit(child);
|
|
391
|
-
expect(res.signal).toBe("SIGTERM");
|
|
392
|
-
expect(res.code).toBeNull();
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
it("socket closed by runtime → exit 12", { timeout: 10_000 }, async () => {
|
|
396
|
-
const socketPath = makeSocketPath();
|
|
397
|
-
const rt = await startFakeRuntime(socketPath);
|
|
398
|
-
const child = spawnRelay({ socketPath, sessionId: "s", recordId: "r" });
|
|
399
|
-
await rt.handshakePromise;
|
|
400
|
-
rt.destroyConn();
|
|
401
|
-
const res = await waitForExit(child);
|
|
402
|
-
expect(res.code).toBe(RELAY_EXIT_CODES.SOCKET_CLOSED);
|
|
403
|
-
expect(res.stderr).toContain("closed");
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
it("reject reason=version → exit 10 + reinstall recovery hint", { timeout: 10_000 }, async () => {
|
|
407
|
-
const socketPath = makeSocketPath();
|
|
408
|
-
const rt = await startFakeRuntime(socketPath, (_hs, r) => {
|
|
409
|
-
r.send({ kind: "reject", reason: "version", supported: [RELAY_PROTOCOL_VERSION] });
|
|
410
|
-
});
|
|
411
|
-
const child = spawnRelay({ socketPath, sessionId: "s", recordId: "r" });
|
|
412
|
-
const res = await waitForExit(child);
|
|
413
|
-
expect(res.code).toBe(RELAY_EXIT_CODES.VERSION_MISMATCH);
|
|
414
|
-
expect(res.stderr).toContain("version mismatch");
|
|
415
|
-
expect(res.stderr).toContain("重装应用");
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
it("reject with non-version reason → exit 12", { timeout: 10_000 }, async () => {
|
|
419
|
-
const socketPath = makeSocketPath();
|
|
420
|
-
const rt = await startFakeRuntime(socketPath, (_hs, r) => {
|
|
421
|
-
r.send({ kind: "reject", reason: "handshake_invalid" });
|
|
422
|
-
});
|
|
423
|
-
const child = spawnRelay({ socketPath, sessionId: "s", recordId: "r" });
|
|
424
|
-
const res = await waitForExit(child);
|
|
425
|
-
expect(res.code).toBe(RELAY_EXIT_CODES.SOCKET_CLOSED);
|
|
426
|
-
expect(res.stderr).toContain("handshake_invalid");
|
|
427
|
-
});
|
|
428
|
-
|
|
429
|
-
it("backpressure: 1MB random bytes survive full down→up roundtrip", { timeout: 60_000 }, async () => {
|
|
430
|
-
const socketPath = makeSocketPath();
|
|
431
|
-
const rt = await startFakeRuntime(socketPath);
|
|
432
|
-
const child = spawnRelay({ socketPath, sessionId: "s", recordId: "r" });
|
|
433
|
-
await rt.handshakePromise;
|
|
434
|
-
|
|
435
|
-
const payload = crypto.randomBytes(1024 * 1024);
|
|
436
|
-
child.child.stdin.write(payload);
|
|
437
|
-
await pollUntil(() => rt.downTotal() === payload.length, 30_000, "1MB down bytes");
|
|
438
|
-
const receivedDown = Buffer.concat(rt.downChunks);
|
|
439
|
-
expect(receivedDown).toEqual(payload);
|
|
440
|
-
|
|
441
|
-
// 同一 payload 分 64KB up 帧回发(驱动代理 socket→stdout 背压 + 行缓冲路径)
|
|
442
|
-
await sendLargeUp(rt, receivedDown);
|
|
443
|
-
rt.send({ kind: "exit", code: 0 });
|
|
444
|
-
const res = await waitForExit(child);
|
|
445
|
-
expect(res.code).toBe(0);
|
|
446
|
-
expect(res.stdout).toEqual(payload);
|
|
447
|
-
});
|
|
448
|
-
});
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
RELAY_ENV_NODE,
|
|
5
|
-
RELAY_ENV_RECORD_ID,
|
|
6
|
-
RELAY_ENV_SCRIPT,
|
|
7
|
-
RELAY_ENV_SESSION_ID,
|
|
8
|
-
RELAY_ENV_SOCKET,
|
|
9
|
-
RELAY_EXIT_CODES,
|
|
10
|
-
RELAY_PROTOCOL_VERSION,
|
|
11
|
-
isRelayActive,
|
|
12
|
-
} from '../relay-env.js';
|
|
13
|
-
|
|
14
|
-
describe('relay env SSOT', () => {
|
|
15
|
-
it('激活判定:三 env 同时非空', () => {
|
|
16
|
-
expect(isRelayActive({ [RELAY_ENV_SOCKET]: '/x.sock', [RELAY_ENV_NODE]: '/node', [RELAY_ENV_SCRIPT]: '/relay.mjs' })).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('任一缺失即不激活(全有或全无)', () => {
|
|
20
|
-
const full = {
|
|
21
|
-
[RELAY_ENV_SOCKET]: '/x.sock',
|
|
22
|
-
[RELAY_ENV_NODE]: '/node',
|
|
23
|
-
[RELAY_ENV_SCRIPT]: '/relay.mjs',
|
|
24
|
-
};
|
|
25
|
-
expect(isRelayActive({ ...full, [RELAY_ENV_SOCKET]: '' })).toBe(false);
|
|
26
|
-
expect(isRelayActive({ ...full, [RELAY_ENV_NODE]: undefined })).toBe(false);
|
|
27
|
-
expect(isRelayActive({})).toBe(false);
|
|
28
|
-
expect(isRelayActive(process.env)).toBe(false);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('env 名与退出码稳定(relay.mjs 镜像一致性由 conformance relay 断言锁定)', () => {
|
|
32
|
-
expect(RELAY_ENV_SESSION_ID).toBe('XYZ_SUBAGENT_RELAY_SESSION_ID');
|
|
33
|
-
expect(RELAY_ENV_RECORD_ID).toBe('XYZ_SUBAGENT_RELAY_RECORD_ID');
|
|
34
|
-
expect(RELAY_PROTOCOL_VERSION).toBe(1);
|
|
35
|
-
expect(RELAY_EXIT_CODES).toEqual({
|
|
36
|
-
VERSION_MISMATCH: 10,
|
|
37
|
-
SOCKET_UNREACHABLE: 11,
|
|
38
|
-
SOCKET_CLOSED: 12,
|
|
39
|
-
MISSING_IDENTITY: 13,
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
});
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
// resource-policy 单测 —— SP-6 资源策略配置化(idleTimeoutMs + env 覆盖)。
|
|
2
|
-
//
|
|
3
|
-
// 测试策略:
|
|
4
|
-
// - 验证 armIdleTimer 的三层优先级:参数 > env XYZ_SUBAGENT_IDLE_TIMEOUT_MS > 默认 300000ms。
|
|
5
|
-
// - 用 vi.useFakeTimers() 控制时间流逝。
|
|
6
|
-
// - 每个用例 beforeEach/afterEach 清理 env + reset lifecycle state。
|
|
7
|
-
|
|
8
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
DEFAULT_IDLE_TIMEOUT_MS,
|
|
12
|
-
_resetLifecycleState,
|
|
13
|
-
armIdleTimer,
|
|
14
|
-
hasIdleTimer,
|
|
15
|
-
} from "../lifecycle-manager.ts";
|
|
16
|
-
|
|
17
|
-
describe("resource-policy — SP-6 idleTimeoutMs 配置化", () => {
|
|
18
|
-
const ENV_KEY = "XYZ_SUBAGENT_IDLE_TIMEOUT_MS";
|
|
19
|
-
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
vi.useFakeTimers();
|
|
22
|
-
_resetLifecycleState();
|
|
23
|
-
// 清理 env,确保用例间隔离。
|
|
24
|
-
delete process.env[ENV_KEY];
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
afterEach(() => {
|
|
28
|
-
_resetLifecycleState();
|
|
29
|
-
vi.useRealTimers();
|
|
30
|
-
delete process.env[ENV_KEY];
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// TC-1: idleTimeoutMs 参数透传生效
|
|
34
|
-
it("TC-1: explicit timeoutMs 参数直接生效(不看 env)", () => {
|
|
35
|
-
const onTimeout = vi.fn();
|
|
36
|
-
armIdleTimer("sa-tc1", onTimeout, 1234);
|
|
37
|
-
|
|
38
|
-
expect(hasIdleTimer("sa-tc1")).toBe(true);
|
|
39
|
-
|
|
40
|
-
// 差 1ms 不触发
|
|
41
|
-
vi.advanceTimersByTime(1233);
|
|
42
|
-
expect(onTimeout).not.toHaveBeenCalled();
|
|
43
|
-
|
|
44
|
-
// 到点触发
|
|
45
|
-
vi.advanceTimersByTime(1);
|
|
46
|
-
expect(onTimeout).toHaveBeenCalledOnce();
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// TC-2: env XYZ_SUBAGENT_IDLE_TIMEOUT_MS 覆盖默认值
|
|
50
|
-
it("TC-2: env XYZ_SUBAGENT_IDLE_TIMEOUT_MS 覆盖默认值(不传参数时)", () => {
|
|
51
|
-
process.env[ENV_KEY] = "5000"; // 5 秒
|
|
52
|
-
|
|
53
|
-
const onTimeout = vi.fn();
|
|
54
|
-
armIdleTimer("sa-tc2", onTimeout); // 不传 timeoutMs
|
|
55
|
-
|
|
56
|
-
// 4.9s 不触发
|
|
57
|
-
vi.advanceTimersByTime(4999);
|
|
58
|
-
expect(onTimeout).not.toHaveBeenCalled();
|
|
59
|
-
|
|
60
|
-
// 5s 触发(env 值,不是默认 300s)
|
|
61
|
-
vi.advanceTimersByTime(1);
|
|
62
|
-
expect(onTimeout).toHaveBeenCalledOnce();
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// TC-3: 参数优先于 env
|
|
66
|
-
it("TC-3: explicit timeoutMs 参数优先于 env", () => {
|
|
67
|
-
process.env[ENV_KEY] = "99999"; // env 设大值
|
|
68
|
-
|
|
69
|
-
const onTimeout = vi.fn();
|
|
70
|
-
armIdleTimer("sa-tc3", onTimeout, 2000); // 参数设小值
|
|
71
|
-
|
|
72
|
-
// 2s 触发(参数优先,不等 env 的 99s)
|
|
73
|
-
vi.advanceTimersByTime(1999);
|
|
74
|
-
expect(onTimeout).not.toHaveBeenCalled();
|
|
75
|
-
|
|
76
|
-
vi.advanceTimersByTime(1);
|
|
77
|
-
expect(onTimeout).toHaveBeenCalledOnce();
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// TC-4: 都不设时默认 5min (300000ms)
|
|
81
|
-
it("TC-4: 无参数无 env 时默认 300000ms (5min)", () => {
|
|
82
|
-
const onTimeout = vi.fn();
|
|
83
|
-
armIdleTimer("sa-tc4", onTimeout); // 不传 timeoutMs,env 也未设
|
|
84
|
-
|
|
85
|
-
// 差 1ms 不触发
|
|
86
|
-
vi.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT_MS - 1);
|
|
87
|
-
expect(onTimeout).not.toHaveBeenCalled();
|
|
88
|
-
|
|
89
|
-
// 到点触发
|
|
90
|
-
vi.advanceTimersByTime(1);
|
|
91
|
-
expect(onTimeout).toHaveBeenCalledOnce();
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
// TC-5 (额外): env 非法值回落默认
|
|
95
|
-
it("TC-5: env 非法值(负数/非数字)回落默认 300000ms", () => {
|
|
96
|
-
process.env[ENV_KEY] = "not-a-number";
|
|
97
|
-
|
|
98
|
-
const onTimeout = vi.fn();
|
|
99
|
-
armIdleTimer("sa-tc5", onTimeout); // 不传 timeoutMs
|
|
100
|
-
|
|
101
|
-
// 非法 env → 回落默认 300s。299s 不触发。
|
|
102
|
-
vi.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT_MS - 1);
|
|
103
|
-
expect(onTimeout).not.toHaveBeenCalled();
|
|
104
|
-
|
|
105
|
-
// 到点触发
|
|
106
|
-
vi.advanceTimersByTime(1);
|
|
107
|
-
expect(onTimeout).toHaveBeenCalledOnce();
|
|
108
|
-
});
|
|
109
|
-
});
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
// src/__tests__/rpc-mode.test.ts
|
|
2
|
-
//
|
|
3
|
-
// RPC 模式相关功能
|
|
4
|
-
// 1. buildSpawnArgs 返回 --mode rpc(而非 json)
|
|
5
|
-
// 2. parseSpawnLine 正确解析 Pi 原生 RpcResponse(type:response + command + success + data|error)
|
|
6
|
-
|
|
7
|
-
import { describe, expect, it } from "vitest";
|
|
8
|
-
|
|
9
|
-
import { buildSpawnArgs } from "../session-runner.ts";
|
|
10
|
-
import { parseSpawnLine } from "../spawn-event-adapter.ts";
|
|
11
|
-
|
|
12
|
-
describe("buildSpawnArgs - RPC mode", () => {
|
|
13
|
-
const baseParams = {
|
|
14
|
-
modelRef: { provider: "openai", id: "gpt-4" },
|
|
15
|
-
thinkingLevel: undefined,
|
|
16
|
-
agentTools: undefined,
|
|
17
|
-
appendSystemPromptPath: undefined,
|
|
18
|
-
sessionDir: "/tmp/sessions",
|
|
19
|
-
forkSource: undefined,
|
|
20
|
-
skillPaths: undefined,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
it("生成 --mode rpc 参数(非 json)", () => {
|
|
24
|
-
const args = buildSpawnArgs(baseParams);
|
|
25
|
-
// 应包含 --mode rpc
|
|
26
|
-
expect(args).toContain("--mode");
|
|
27
|
-
const modeIdx = args.indexOf("--mode");
|
|
28
|
-
expect(args[modeIdx + 1]).toBe("rpc");
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("不包含 --mode json", () => {
|
|
32
|
-
const args = buildSpawnArgs(baseParams);
|
|
33
|
-
const modeIdx = args.indexOf("--mode");
|
|
34
|
-
expect(args[modeIdx + 1]).not.toBe("json");
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
describe("parseSpawnLine - RpcResponse", () => {
|
|
39
|
-
// Pi 原生 RpcResponse 格式(rpc-types.ts):{type:"response", id?, command, success, data?, error?}
|
|
40
|
-
// W1 修复后 isRpcResponse 判 type:response + command:string + success:boolean(SR-1 根因 1b)
|
|
41
|
-
|
|
42
|
-
it("解析 Pi 原生 success response(kind=response)", () => {
|
|
43
|
-
const rpcResponse = JSON.stringify({
|
|
44
|
-
type: "response",
|
|
45
|
-
id: "req-123",
|
|
46
|
-
command: "run",
|
|
47
|
-
success: true,
|
|
48
|
-
data: { answers: [{ question: "Q1", answer: "A1" }] },
|
|
49
|
-
});
|
|
50
|
-
const result = parseSpawnLine(rpcResponse);
|
|
51
|
-
expect(result?.kind).toBe("response");
|
|
52
|
-
if (result?.kind === "response") {
|
|
53
|
-
expect(result.id).toBe("req-123");
|
|
54
|
-
expect(result.command).toBe("run");
|
|
55
|
-
expect(result.success).toBe(true);
|
|
56
|
-
expect(result.data).toEqual({ answers: [{ question: "Q1", answer: "A1" }] });
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("解析 Pi 原生 error response(kind=response)", () => {
|
|
61
|
-
const rpcError = JSON.stringify({
|
|
62
|
-
type: "response",
|
|
63
|
-
id: "req-456",
|
|
64
|
-
command: "run",
|
|
65
|
-
success: false,
|
|
66
|
-
error: "task failed: timeout",
|
|
67
|
-
});
|
|
68
|
-
const result = parseSpawnLine(rpcError);
|
|
69
|
-
expect(result?.kind).toBe("response");
|
|
70
|
-
if (result?.kind === "response") {
|
|
71
|
-
expect(result.id).toBe("req-456");
|
|
72
|
-
expect(result.success).toBe(false);
|
|
73
|
-
expect(result.error).toBe("task failed: timeout");
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("旧 JSON-RPC 2.0 格式不再归类为 response(W1 废弃)", () => {
|
|
78
|
-
// SR-1: 旧 {jsonrpc:"2.0", id, result} 无 type:response 字段,应落入 invalid
|
|
79
|
-
const oldRpc = JSON.stringify({ jsonrpc: "2.0", id: "req-old", result: { x: 1 } });
|
|
80
|
-
const result = parseSpawnLine(oldRpc);
|
|
81
|
-
expect(result?.kind).not.toBe("response");
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("非 response 类型的 JSON 不归类为 response", () => {
|
|
85
|
-
const normalEvent = JSON.stringify({ type: "tool_execution_start", toolName: "bash" });
|
|
86
|
-
const result = parseSpawnLine(normalEvent);
|
|
87
|
-
expect(result?.kind).not.toBe("response");
|
|
88
|
-
});
|
|
89
|
-
});
|