@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
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
|
|
11
11
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
12
12
|
|
|
13
|
-
import type { DiscoveredResource } from "
|
|
13
|
+
import type { DiscoveredResource } from "@zhushanwen/subagent-core/shared/resource-discovery.ts";
|
|
14
14
|
// 共享 mock 基建(vi.mock 工厂 / mock pi / mock ctx):helpers/injector-test-mocks.ts
|
|
15
15
|
import { createDiscoveryModuleMock, createLoggerModuleMock, createMockCtx, createMockPi, type CapturedHandlers } from "./helpers/injector-test-mocks.ts";
|
|
16
16
|
|
|
17
17
|
// ── 稳定 spy(vi.hoisted 保证 resetModules 后引用不变,见 subagent 测试同款注释) ──
|
|
18
18
|
const spies = vi.hoisted(() => ({ discoverResources: vi.fn(), getCachedFileContent: vi.fn() }));
|
|
19
19
|
|
|
20
|
-
vi.mock("
|
|
20
|
+
vi.mock("@zhushanwen/subagent-core/shared/resource-discovery.ts", () => createDiscoveryModuleMock(spies));
|
|
21
21
|
|
|
22
22
|
// 工厂必须写成箭头惰性形式(vi.mock 提升后直接传引用会 TDZ,见 helper 文件头注释)
|
|
23
23
|
vi.mock("@zhushanwen/pi-extension-logger", () => createLoggerModuleMock());
|
|
@@ -293,7 +293,7 @@ describe("discoverAllWorkflows 顺序契约(KV-cache)", () => {
|
|
|
293
293
|
spies.getCachedFileContent.mockImplementation((p: string) => byPath[p] ?? null);
|
|
294
294
|
|
|
295
295
|
const { discoverAllWorkflows } = await import("../workflow-list-injector");
|
|
296
|
-
const workflows = await discoverAllWorkflows("/ws"
|
|
296
|
+
const workflows = await discoverAllWorkflows("/ws");
|
|
297
297
|
expect(workflows.map((w) => w.name)).toEqual(["alpha", "chain", "zeta"]);
|
|
298
298
|
});
|
|
299
299
|
|
|
@@ -314,8 +314,8 @@ describe("discoverAllWorkflows 顺序契约(KV-cache)", () => {
|
|
|
314
314
|
spies.getCachedFileContent.mockImplementation((p: string) => byPath[p] ?? null);
|
|
315
315
|
|
|
316
316
|
const { discoverAllWorkflows, formatWorkflowList } = await import("../workflow-list-injector");
|
|
317
|
-
const first = await discoverAllWorkflows("/ws"
|
|
318
|
-
const second = await discoverAllWorkflows("/ws"
|
|
317
|
+
const first = await discoverAllWorkflows("/ws");
|
|
318
|
+
const second = await discoverAllWorkflows("/ws");
|
|
319
319
|
expect(second).toEqual(first);
|
|
320
320
|
expect(formatWorkflowList(second)).toBe(formatWorkflowList(first));
|
|
321
321
|
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// src/injectors/engine-awareness.ts
|
|
2
|
+
//
|
|
3
|
+
// [engine-awareness U3] 引擎感知检测编排。设计权威源:
|
|
4
|
+
// docs/design/subagent-engine-awareness-injection.md §2.3 物理数据流 /
|
|
5
|
+
// §3.3 D1(per-turn poll)、D1b(lastEngine 初始化与静默基线化)、D2(reload 先行)、
|
|
6
|
+
// D3(通知通道)、D5(三态读取语义)。
|
|
7
|
+
//
|
|
8
|
+
// 职责:per-turn(before_agent_start)三态 poll 全局 config → 与 per-session
|
|
9
|
+
// lastEngine diff → 变更时按硬约束顺序编排:提交读取结果到路由缓存(D2,先于一切——
|
|
10
|
+
// 只改注入不改路由会出现「prompt 说引擎 B、实际派发跑引擎 A」,权威信息源说谎)
|
|
11
|
+
// → sendMessage 对话流通知(D3/D4,短文案不含任何模型清单)→ 更新 lastEngine。
|
|
12
|
+
//
|
|
13
|
+
// 本模块是无状态纯编排(依赖注入式,可测);状态(lastEngine)与宿主能力
|
|
14
|
+
// (readConfig / applyRead / sendMessage)由 index.ts engine handler 装配注入。
|
|
15
|
+
|
|
16
|
+
import { getLogger } from "@zhushanwen/pi-extension-logger";
|
|
17
|
+
|
|
18
|
+
import type { GlobalConfigReadResult } from "@zhushanwen/subagent-core/execution/config.ts";
|
|
19
|
+
import { DEFAULT_ENGINE_ID, normalizeEngineId } from "@zhushanwen/subagent-core/execution/engine/registry.ts";
|
|
20
|
+
|
|
21
|
+
const logger = getLogger("subagents");
|
|
22
|
+
|
|
23
|
+
/** 引擎变更通知 customType(D3;renderer/测试按此识别引擎变更消息)。 */
|
|
24
|
+
export const ENGINE_CHANGE_CUSTOM_TYPE = "subagent-engine-changed";
|
|
25
|
+
|
|
26
|
+
// normalizeEngineId 单一权威源在 registry.ts,此处再导出保持既有导入面(index.ts
|
|
27
|
+
// 与测试经本模块导入)不断裂。
|
|
28
|
+
export { normalizeEngineId };
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 引擎变更通知文案(设计 §3.1 英文基准,三行结构逐字骨架;D4:不含任何模型清单,
|
|
32
|
+
* 清单只活在 system prompt 现值一份)。指路段按目标引擎参数化,与
|
|
33
|
+
* <current_subagent_engine> 恒在段的分界语义一致:目标 pi → 指向核心段
|
|
34
|
+
* <available_provider_models>;非 pi → 指向该引擎清单段 <available_<engine>_models>。
|
|
35
|
+
*/
|
|
36
|
+
export function buildEngineChangeNotice(from: string, to: string): string {
|
|
37
|
+
const modelsLine =
|
|
38
|
+
to === DEFAULT_ENGINE_ID
|
|
39
|
+
? "Use pi-registry ids from <available_provider_models> for explicit models;"
|
|
40
|
+
: `Use ids from <available_${to}_models> for explicit models;`;
|
|
41
|
+
return [
|
|
42
|
+
`Subagent default engine changed: ${from} → ${to} (effective this turn).`,
|
|
43
|
+
modelsLine,
|
|
44
|
+
"omit `model` to inherit. The <current_subagent_engine> section reflects the current state.",
|
|
45
|
+
].join("\n");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 检测编排依赖(注入式)。生产装配在 index.ts engine handler;测试用 stub。 */
|
|
49
|
+
export interface EngineAwarenessDeps {
|
|
50
|
+
/** 三态读取全局 config(D5;生产 = () => readGlobalConfig(getAgentDir()))。 */
|
|
51
|
+
readConfig(): GlobalConfigReadResult;
|
|
52
|
+
/**
|
|
53
|
+
* D2:将本 turn 三态读取结果提交路由缓存(生产 = ModelConfigService.applyGlobalConfig,
|
|
54
|
+
* 纯赋值幂等;failed 已由编排层早退,到达此调用的必为 ok/absent)。
|
|
55
|
+
*/
|
|
56
|
+
applyRead(read: GlobalConfigReadResult): void;
|
|
57
|
+
/** 对话流通知投递(D3;生产 = pi.sendMessage(message, {}),不设 triggerTurn)。 */
|
|
58
|
+
sendMessage(message: {
|
|
59
|
+
customType: string;
|
|
60
|
+
content: string;
|
|
61
|
+
display: boolean;
|
|
62
|
+
details?: unknown;
|
|
63
|
+
}): void;
|
|
64
|
+
/** per-session lastEngine 读取(undefined = 未基线化,D1b)。 */
|
|
65
|
+
getLastEngine(): string | undefined;
|
|
66
|
+
/** per-session lastEngine 写入(仅合法引擎 id,不写 undefined)。 */
|
|
67
|
+
setLastEngine(engine: string): void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 单次 per-turn 检测的编排结果(测试断言面 + 诊断留痕消费)。 */
|
|
71
|
+
export type EngineAwarenessOutcome =
|
|
72
|
+
| { outcome: "changed"; from: string; to: string }
|
|
73
|
+
| { outcome: "unchanged"; engine: string }
|
|
74
|
+
| { outcome: "baseline"; engine: string }
|
|
75
|
+
| { outcome: "read-failed"; reason: string };
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 单个 session 的 per-turn 引擎检测编排(§2.3 数据流 ①② 步;③ 渲染归 index.ts)。
|
|
79
|
+
*
|
|
80
|
+
* 分派(D5 三态 × D1b 基线):
|
|
81
|
+
* - read failed → 保持 lastEngine 不动、不提交缓存、不通知(防 torn write 瞬间伪通知);
|
|
82
|
+
* read-failure warn 日志由 readGlobalConfig 内部落,此处只返回结果。
|
|
83
|
+
* - lastEngine === undefined(session_start 初始化读失败的兜底形态)→ 静默基线化为
|
|
84
|
+
* 当前目标值:不算变更、不发通知(防「changed: undefined → zcode」首 turn 伪通知)。
|
|
85
|
+
* - last === target(absent 归一后同值也在内)→ 无事。
|
|
86
|
+
* - last ≠ target(含 ENOENT=合法缺省)→ 提交缓存先行 → 通知 → 记账,三步同 turn。
|
|
87
|
+
*
|
|
88
|
+
* 抛错面:不主动 try-catch——readGlobalConfig 三态返回不抛,applyRead/sendMessage 的
|
|
89
|
+
* 生产实现各自吞错;handler 顶层 catch 兜底(fail-safe 不阻塞 agent loop)。
|
|
90
|
+
*/
|
|
91
|
+
export function runEngineAwarenessTurn(deps: EngineAwarenessDeps): EngineAwarenessOutcome {
|
|
92
|
+
const read = deps.readConfig();
|
|
93
|
+
if (read.status === "failed") {
|
|
94
|
+
return { outcome: "read-failed", reason: read.reason };
|
|
95
|
+
}
|
|
96
|
+
const target = normalizeEngineId(read.config.defaultEngine);
|
|
97
|
+
const last = deps.getLastEngine();
|
|
98
|
+
if (last === undefined) {
|
|
99
|
+
// D1b(修订):基线化前把本 turn 读取结果提交到单例缓存。baseline 形态可能来自
|
|
100
|
+
// session_start 读取失败(lastEngine 未设置 + Service 缓存回落缺省)而文件此后被
|
|
101
|
+
// 修好——若只记账不提交,缓存/路由/状态段永停旧值且永不通知(「改 config 不生效」
|
|
102
|
+
// 以更隐蔽形态复活)。apply 纯赋值——检测值与缓存值构造性同源,重复提交无害。
|
|
103
|
+
deps.applyRead(read);
|
|
104
|
+
deps.setLastEngine(target);
|
|
105
|
+
return { outcome: "baseline", engine: target };
|
|
106
|
+
}
|
|
107
|
+
if (last === target) {
|
|
108
|
+
return { outcome: "unchanged", engine: target };
|
|
109
|
+
}
|
|
110
|
+
// D2 顺序硬约束:先提交读取结果到路由缓存(本 turn 路由 + 渲染同源生效)→ 再通知 → 再记账。
|
|
111
|
+
deps.applyRead(read);
|
|
112
|
+
deps.sendMessage({
|
|
113
|
+
customType: ENGINE_CHANGE_CUSTOM_TYPE,
|
|
114
|
+
content: buildEngineChangeNotice(last, target),
|
|
115
|
+
display: true,
|
|
116
|
+
details: { from: last, to: target },
|
|
117
|
+
});
|
|
118
|
+
deps.setLastEngine(target);
|
|
119
|
+
logger.debug(
|
|
120
|
+
`[engine-awareness] default engine changed: ${last} -> ${target} (route reloaded, notice sent)`,
|
|
121
|
+
);
|
|
122
|
+
return { outcome: "changed", from: last, to: target };
|
|
123
|
+
}
|
|
@@ -35,7 +35,7 @@ import type {
|
|
|
35
35
|
} from "@earendil-works/pi-coding-agent";
|
|
36
36
|
import { getLogger } from "@zhushanwen/pi-extension-logger";
|
|
37
37
|
|
|
38
|
-
import { escapeXml, renderXmlSection } from "
|
|
38
|
+
import { escapeXml, renderXmlSection } from "@zhushanwen/subagent-core/shared/xml-injection.ts";
|
|
39
39
|
|
|
40
40
|
const logger = getLogger("injector");
|
|
41
41
|
|
|
@@ -28,17 +28,18 @@ import type {
|
|
|
28
28
|
SessionShutdownEvent,
|
|
29
29
|
SessionStartEvent,
|
|
30
30
|
} from "@earendil-works/pi-coding-agent";
|
|
31
|
-
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
32
31
|
import { getLogger } from "@zhushanwen/pi-extension-logger";
|
|
33
32
|
|
|
33
|
+
import { getHostServices } from "@zhushanwen/subagent-core/core/host-services.ts";
|
|
34
|
+
|
|
34
35
|
import {
|
|
35
36
|
discoverResources,
|
|
36
37
|
findWorkspaceRoot,
|
|
37
38
|
getCachedFileContent,
|
|
38
39
|
getCachedParsed,
|
|
39
|
-
} from "
|
|
40
|
-
import { parseResourceMeta } from "
|
|
41
|
-
import { escapeXml, renderXmlSection } from "
|
|
40
|
+
} from "@zhushanwen/subagent-core/shared/resource-discovery.ts";
|
|
41
|
+
import { parseResourceMeta } from "@zhushanwen/subagent-core/shared/meta-parser.ts";
|
|
42
|
+
import { escapeXml, renderXmlSection } from "@zhushanwen/subagent-core/shared/xml-injection.ts";
|
|
42
43
|
|
|
43
44
|
const logger = getLogger("injector");
|
|
44
45
|
|
|
@@ -112,12 +113,13 @@ export function parseAgentFrontmatter(content: string): AgentEntry | null {
|
|
|
112
113
|
*/
|
|
113
114
|
export async function discoverAllAgents(
|
|
114
115
|
workspaceRoot: string,
|
|
115
|
-
agentDir: string,
|
|
116
116
|
): Promise<AgentEntry[]> {
|
|
117
117
|
const resources = await discoverResources({
|
|
118
118
|
kind: "agents",
|
|
119
119
|
workspaceRoot,
|
|
120
|
-
|
|
120
|
+
// 宿主注入根现取(pi 壳 discoveryRoots 每次现取 getAgentDir,实例隔离);
|
|
121
|
+
// agentDir 形参已删——其唯一用途就是喂 ScanConfig(u0-data-discovery 偏差 #7)
|
|
122
|
+
hostRoots: getHostServices().discoveryRoots?.()?.agents ?? [],
|
|
121
123
|
});
|
|
122
124
|
|
|
123
125
|
const agentMap = new Map<string, AgentEntry>();
|
|
@@ -214,7 +216,6 @@ export function setupSubagentListInjector(pi: ExtensionAPI): void {
|
|
|
214
216
|
setAgentCache(
|
|
215
217
|
await discoverAllAgents(
|
|
216
218
|
findWorkspaceRoot(ctx.cwd),
|
|
217
|
-
getAgentDir(),
|
|
218
219
|
),
|
|
219
220
|
);
|
|
220
221
|
} catch (err) {
|
|
@@ -238,7 +239,6 @@ export function setupSubagentListInjector(pi: ExtensionAPI): void {
|
|
|
238
239
|
setAgentCache(
|
|
239
240
|
await discoverAllAgents(
|
|
240
241
|
findWorkspaceRoot(ctx.cwd),
|
|
241
|
-
getAgentDir(),
|
|
242
242
|
),
|
|
243
243
|
);
|
|
244
244
|
}
|
|
@@ -25,16 +25,17 @@ import type {
|
|
|
25
25
|
SessionShutdownEvent,
|
|
26
26
|
SessionStartEvent,
|
|
27
27
|
} from "@earendil-works/pi-coding-agent";
|
|
28
|
-
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
29
28
|
import { getLogger } from "@zhushanwen/pi-extension-logger";
|
|
30
29
|
|
|
30
|
+
import { getHostServices } from "@zhushanwen/subagent-core/core/host-services.ts";
|
|
31
|
+
|
|
31
32
|
import {
|
|
32
33
|
discoverResources,
|
|
33
34
|
findWorkspaceRoot,
|
|
34
35
|
getCachedParsed,
|
|
35
|
-
} from "
|
|
36
|
-
import { parseResourceMeta } from "
|
|
37
|
-
import { escapeXml, renderXmlSection } from "
|
|
36
|
+
} from "@zhushanwen/subagent-core/shared/resource-discovery.ts";
|
|
37
|
+
import { parseResourceMeta } from "@zhushanwen/subagent-core/shared/meta-parser.ts";
|
|
38
|
+
import { escapeXml, renderXmlSection } from "@zhushanwen/subagent-core/shared/xml-injection.ts";
|
|
38
39
|
|
|
39
40
|
const logger = getLogger("injector");
|
|
40
41
|
|
|
@@ -114,12 +115,13 @@ export function parseWorkflowMeta(content: string): WorkflowEntry | null {
|
|
|
114
115
|
*/
|
|
115
116
|
export async function discoverAllWorkflows(
|
|
116
117
|
workspaceRoot: string,
|
|
117
|
-
agentDir: string,
|
|
118
118
|
): Promise<WorkflowEntry[]> {
|
|
119
119
|
const resources = await discoverResources({
|
|
120
120
|
kind: "workflows",
|
|
121
121
|
workspaceRoot,
|
|
122
|
-
|
|
122
|
+
// 宿主注入根现取(pi 壳 discoveryRoots 每次现取 getAgentDir,实例隔离);
|
|
123
|
+
// agentDir 形参已删——其唯一用途就是喂 ScanConfig(u0-data-discovery 偏差 #7)
|
|
124
|
+
hostRoots: getHostServices().discoveryRoots?.()?.workflows ?? [],
|
|
123
125
|
includeTmp: true,
|
|
124
126
|
});
|
|
125
127
|
|
|
@@ -178,7 +180,6 @@ export function setupWorkflowListInjector(pi: ExtensionAPI): void {
|
|
|
178
180
|
setWorkflowCache(
|
|
179
181
|
await discoverAllWorkflows(
|
|
180
182
|
findWorkspaceRoot(ctx.cwd),
|
|
181
|
-
getAgentDir(),
|
|
182
183
|
),
|
|
183
184
|
);
|
|
184
185
|
} catch (err) {
|
|
@@ -202,7 +203,6 @@ export function setupWorkflowListInjector(pi: ExtensionAPI): void {
|
|
|
202
203
|
setWorkflowCache(
|
|
203
204
|
await discoverAllWorkflows(
|
|
204
205
|
findWorkspaceRoot(ctx.cwd),
|
|
205
|
-
getAgentDir(),
|
|
206
206
|
),
|
|
207
207
|
);
|
|
208
208
|
}
|
|
@@ -18,17 +18,17 @@ import { readFileSync } from "node:fs";
|
|
|
18
18
|
import { join } from "node:path";
|
|
19
19
|
|
|
20
20
|
import { argKeysFromMeta, findFlattenedArgKeys } from "../tool-workflow";
|
|
21
|
-
import { parseResourceMeta } from "
|
|
21
|
+
import { parseResourceMeta } from "@zhushanwen/subagent-core/shared/meta-parser.ts";
|
|
22
22
|
|
|
23
23
|
// 真实参数集(argKeysFromMeta 产物——m6 动态数据源,与 TC2 同源防漂移)
|
|
24
24
|
function rflKeys() {
|
|
25
|
-
const src = readFileSync(join(__dirname, "../../../workflows/review-fix-loop.js"), "utf-8");
|
|
25
|
+
const src = readFileSync(join(__dirname, "../../../node_modules/@zhushanwen/subagent-core/workflows/review-fix-loop.js"), "utf-8");
|
|
26
26
|
const meta = parseResourceMeta(src, "workflow");
|
|
27
27
|
if (!meta || meta.kind !== "workflow" || !meta.parameters) throw new Error("rfl meta");
|
|
28
28
|
return argKeysFromMeta(meta.parameters);
|
|
29
29
|
}
|
|
30
30
|
function chainKeys() {
|
|
31
|
-
const src = readFileSync(join(__dirname, "../../../workflows/chain.js"), "utf-8");
|
|
31
|
+
const src = readFileSync(join(__dirname, "../../../node_modules/@zhushanwen/subagent-core/workflows/chain.js"), "utf-8");
|
|
32
32
|
const meta = parseResourceMeta(src, "workflow");
|
|
33
33
|
if (!meta || meta.kind !== "workflow" || !meta.parameters) throw new Error("chain meta");
|
|
34
34
|
return argKeysFromMeta(meta.parameters);
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// subagent tool 路径类参数守卫测试(三通道对称审查修复 + MF-13)。
|
|
2
|
+
//
|
|
3
|
+
// 修复背景:subagent tool 的 skillPath 参数此前零校验直传 session-runner 拼
|
|
4
|
+
// `--skill <path>`;cwd 仅 description 声明 "Must be an absolute path" 无运行时
|
|
5
|
+
// 闸。schema pattern(^/)经 pi agent-loop 运行时强校验已是强制(PS-20:
|
|
6
|
+
// agent-loop.js:403-404 → validation.js:247-273),工具层守卫定位 = defense-in-depth
|
|
7
|
+
// + schema 表达力缺口——`..` 穿越语义超出 pattern 能力(^/ 放行 "/a/../b"),
|
|
8
|
+
// 守卫在 executeSubagent start 分支 immediate throw(与 action 枚举守卫同风格)。
|
|
9
|
+
//
|
|
10
|
+
// 本文件锁住:
|
|
11
|
+
// 1. skillPath / cwd 含 `..` 穿越段 → 同步 reject,service.execute 零触达
|
|
12
|
+
// 2. skillPath / cwd 相对路径 → 同步 reject
|
|
13
|
+
// 3. 合法绝对路径放行 → 参数原样透传 service.execute(守卫零误伤对照)
|
|
14
|
+
//
|
|
15
|
+
// harness 复用 sdk-contract.test.ts 的 mock 链(pi-ai/typebox/subagent-service),
|
|
16
|
+
// registerSubagentTool 后捕获 execute 回调直接调用。
|
|
17
|
+
|
|
18
|
+
import { describe, expect, it, vi, beforeEach } from "vitest";
|
|
19
|
+
|
|
20
|
+
vi.mock("@earendil-works/pi-ai", () => ({
|
|
21
|
+
StringEnum: (values: string[]) => ({ type: "string", enum: values }),
|
|
22
|
+
}));
|
|
23
|
+
vi.mock("typebox", () => ({
|
|
24
|
+
Type: {
|
|
25
|
+
Object: (props: Record<string, unknown>) => ({ type: "object", properties: props }),
|
|
26
|
+
Optional: (schema: unknown) => ({ ...(schema as object), optional: true }),
|
|
27
|
+
String: (opts?: Record<string, unknown>) => ({ type: "string", ...opts }),
|
|
28
|
+
Boolean: () => ({ type: "boolean" }),
|
|
29
|
+
Number: (opts?: Record<string, unknown>) => ({ type: "number", ...opts }),
|
|
30
|
+
Array: (items: unknown) => ({ type: "array", items }),
|
|
31
|
+
Record: (key: unknown, value: unknown) => ({ type: "object", additionalProperties: value, key }),
|
|
32
|
+
Unknown: () => ({ type: "unknown" }),
|
|
33
|
+
Union: (members: unknown[]) => ({ type: "union", members }),
|
|
34
|
+
Literal: (value: unknown) => ({ type: "literal", value }),
|
|
35
|
+
},
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
// Mock getSubagentService:断言「守卫拒绝时 service.execute 零触达」的承重证据。
|
|
39
|
+
const { mockServiceExecute } = vi.hoisted(() => ({
|
|
40
|
+
mockServiceExecute: vi.fn(),
|
|
41
|
+
}));
|
|
42
|
+
vi.mock("@zhushanwen/subagent-core/execution/subagent-service.ts", () => ({
|
|
43
|
+
getSubagentService: () => ({ execute: mockServiceExecute }),
|
|
44
|
+
}));
|
|
45
|
+
|
|
46
|
+
import { registerSubagentTool } from "../../interface/subagent-tool.ts";
|
|
47
|
+
import { mockExtensionApi } from "@zhushanwen/subagent-core/execution/__tests__/helpers/mock-extension-api.ts";
|
|
48
|
+
|
|
49
|
+
type ExecuteCb = (...args: unknown[]) => Promise<unknown>;
|
|
50
|
+
|
|
51
|
+
/** 注册 tool 并捕获 execute 回调(sdk-contract 同款)。 */
|
|
52
|
+
function captureExecute(): ExecuteCb {
|
|
53
|
+
let captured: ExecuteCb | undefined;
|
|
54
|
+
const pi = mockExtensionApi({
|
|
55
|
+
registerTool: (tool: unknown) => {
|
|
56
|
+
captured = (tool as { execute: ExecuteCb }).execute;
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
registerSubagentTool(pi);
|
|
60
|
+
if (!captured) throw new Error("subagent tool not registered");
|
|
61
|
+
return captured;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 合法 start 入参基线(守卫测试只变动 skillPath/cwd 字段)。 */
|
|
65
|
+
function baseParams(over: Record<string, unknown> = {}): Record<string, unknown> {
|
|
66
|
+
return { action: "start", task: "guard task", slug: "path-guard", ...over };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 合法 execute 返回值 stub(放行路径 adapter 包装用)。 */
|
|
70
|
+
function stubHandle() {
|
|
71
|
+
return {
|
|
72
|
+
mode: "background",
|
|
73
|
+
subagentId: "sa-path-guard",
|
|
74
|
+
sessionFile: "/tmp/session.jsonl",
|
|
75
|
+
details: { slug: "path-guard", model: "test/model" },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
describe("subagent tool 路径守卫(skillPath/cwd:绝对路径 + 禁 .. 穿越)", () => {
|
|
80
|
+
let execute: ExecuteCb;
|
|
81
|
+
|
|
82
|
+
beforeEach(() => {
|
|
83
|
+
vi.clearAllMocks();
|
|
84
|
+
mockServiceExecute.mockResolvedValue(stubHandle());
|
|
85
|
+
execute = captureExecute();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// ── `..` 穿越段拒绝 ──
|
|
89
|
+
|
|
90
|
+
it("skillPath 含 .. 穿越段 → immediate reject,service.execute 零触达", async () => {
|
|
91
|
+
await expect(
|
|
92
|
+
execute("call-1", baseParams({ skillPath: "../../etc/passwd" }), undefined, undefined, undefined),
|
|
93
|
+
).rejects.toThrow(/skillPath must not contain '\.\.' path segments/);
|
|
94
|
+
|
|
95
|
+
expect(mockServiceExecute).not.toHaveBeenCalled();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("cwd 含 .. 穿越段 → immediate reject,service.execute 零触达", async () => {
|
|
99
|
+
await expect(
|
|
100
|
+
execute("call-2", baseParams({ cwd: "/safe/root/../../unsafe" }), undefined, undefined, undefined),
|
|
101
|
+
).rejects.toThrow(/cwd must not contain '\.\.' path segments/);
|
|
102
|
+
|
|
103
|
+
expect(mockServiceExecute).not.toHaveBeenCalled();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// ── 相对路径拒绝(`~` 缩写不是绝对路径,一并拒)──
|
|
107
|
+
|
|
108
|
+
it("skillPath 相对路径 → immediate reject(报错指引展开为绝对路径)", async () => {
|
|
109
|
+
await expect(
|
|
110
|
+
execute("call-3", baseParams({ skillPath: ".agents/skills/my-skill" }), undefined, undefined, undefined),
|
|
111
|
+
).rejects.toThrow(/skillPath must be an absolute path.*Expand '~' yourself/s);
|
|
112
|
+
|
|
113
|
+
expect(mockServiceExecute).not.toHaveBeenCalled();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("cwd 相对路径 → immediate reject", async () => {
|
|
117
|
+
await expect(
|
|
118
|
+
execute("call-4", baseParams({ cwd: "relative/dir" }), undefined, undefined, undefined),
|
|
119
|
+
).rejects.toThrow(/cwd must be an absolute path/);
|
|
120
|
+
|
|
121
|
+
expect(mockServiceExecute).not.toHaveBeenCalled();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("cwd `~` 缩写 → immediate reject(下游 spawn cwd 不展开 ~)", async () => {
|
|
125
|
+
await expect(
|
|
126
|
+
execute("call-5", baseParams({ cwd: "~/project" }), undefined, undefined, undefined),
|
|
127
|
+
).rejects.toThrow(/cwd must be an absolute path/);
|
|
128
|
+
|
|
129
|
+
expect(mockServiceExecute).not.toHaveBeenCalled();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ── 放行对照(守卫零误伤)──
|
|
133
|
+
|
|
134
|
+
it("合法绝对路径 skillPath/cwd 放行,参数原样透传 service.execute", async () => {
|
|
135
|
+
await execute(
|
|
136
|
+
"call-ok",
|
|
137
|
+
baseParams({ skillPath: "/work/project/.agents/skills/my-skill", cwd: "/work/project" }),
|
|
138
|
+
undefined,
|
|
139
|
+
undefined,
|
|
140
|
+
undefined,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
expect(mockServiceExecute).toHaveBeenCalledTimes(1);
|
|
144
|
+
expect(mockServiceExecute).toHaveBeenCalledWith(
|
|
145
|
+
expect.objectContaining({
|
|
146
|
+
skillPath: "/work/project/.agents/skills/my-skill",
|
|
147
|
+
cwd: "/work/project",
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("不传 skillPath/cwd → 守卫不介入(undefined 合法缺省)", async () => {
|
|
153
|
+
await execute("call-none", baseParams(), undefined, undefined, undefined);
|
|
154
|
+
|
|
155
|
+
expect(mockServiceExecute).toHaveBeenCalledTimes(1);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
@@ -114,6 +114,18 @@ describe("subagent tool description — 行为约束器(非功能说明书)"
|
|
|
114
114
|
expect(DESCRIPTION).not.toContain('"sa_');
|
|
115
115
|
});
|
|
116
116
|
|
|
117
|
+
it("Examples 示例 agent 值必须是绝对路径 .md 形态(显式 ref 硬守卫拒绝裸名)", () => {
|
|
118
|
+
// 显式 agent ref 走硬守卫:getRequiredAgentConfig → loadByPath(ref, true),
|
|
119
|
+
// 裸名(normalizeRef 非绝对路径 → null)同步 throw `Invalid agent ref` +
|
|
120
|
+
// <available_subagents> 恢复指引(explicit-agent-ref-guard.test.ts 锁定拒绝路径)。
|
|
121
|
+
// 示例若教裸名(历史漂移:"agent":"coder"),弱模型照抄即必败调用——浪费一轮
|
|
122
|
+
// 并系统性教唆反模式。与 subagentId 格式测试同风格:示例必须与实际契约一致。
|
|
123
|
+
// ① 零裸名:任何 "agent":"<非/>" 形态都禁止
|
|
124
|
+
expect(DESCRIPTION).not.toMatch(/"agent":"(?!\/)[^"]*"/);
|
|
125
|
+
// ② 正例在位且为绝对路径 .md 形态(<available_subagents> 注入的 <location> 形态)
|
|
126
|
+
expect(DESCRIPTION).toMatch(/"agent":"\/[^"]*\.md"/);
|
|
127
|
+
});
|
|
128
|
+
|
|
117
129
|
it("agent 字段 description 不写死枚举,指向 <available_subagents>(通用化防漂移)", () => {
|
|
118
130
|
// 原实现把 9 个内置 agent 名写死在 schema field description(防漏),
|
|
119
131
|
// 但枚举与包内 agents/*.md 存在漂移风险(新增/删除 agent 要手改两处)。
|
|
@@ -11,14 +11,14 @@ import { mkdirSync, writeFileSync } from "node:fs";
|
|
|
11
11
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
12
12
|
|
|
13
13
|
import { actionGenerate, type ScriptParams, type TextContent, registerWorkflowScriptTool } from "../tool-workflow-script.ts";
|
|
14
|
-
import { deleteWorkflow, saveWorkflow } from "
|
|
14
|
+
import { deleteWorkflow, saveWorkflow } from "@zhushanwen/subagent-core/orchestration/workflow-files.ts";
|
|
15
15
|
|
|
16
16
|
vi.mock("node:fs", () => ({
|
|
17
17
|
mkdirSync: vi.fn(),
|
|
18
18
|
writeFileSync: vi.fn(),
|
|
19
19
|
}));
|
|
20
20
|
|
|
21
|
-
vi.mock("
|
|
21
|
+
vi.mock("@zhushanwen/subagent-core/orchestration/workflow-files.ts", () => ({
|
|
22
22
|
saveWorkflow: vi.fn(),
|
|
23
23
|
deleteWorkflow: vi.fn(),
|
|
24
24
|
}));
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import { describe, expect, it, vi } from "vitest";
|
|
17
17
|
|
|
18
|
-
import { SLUG_MAX_LENGTH } from "
|
|
18
|
+
import { SLUG_MAX_LENGTH } from "@zhushanwen/subagent-core/execution/execute-options-mapper.ts";
|
|
19
19
|
import { REENTRY_BUSY_MESSAGE, type ReentryGuardRef } from "../reentry-guard.ts";
|
|
20
20
|
import { registerWorkflowTool } from "../tool-workflow.ts";
|
|
21
21
|
|
|
@@ -22,7 +22,10 @@
|
|
|
22
22
|
import type { Component } from "@earendil-works/pi-tui";
|
|
23
23
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
24
24
|
|
|
25
|
-
import { displayAgentName } from "
|
|
25
|
+
import { displayAgentName } from "@zhushanwen/subagent-core/shared/agent-ref.ts";
|
|
26
|
+
import { deriveOutcome } from "@zhushanwen/subagent-core/execution/execution-record.ts";
|
|
27
|
+
import { CLOSED_REASONS } from "@zhushanwen/subagent-core/execution/types.ts";
|
|
28
|
+
import type { ClosedReason, ExecutionOutcome } from "@zhushanwen/subagent-core/execution/types.ts";
|
|
26
29
|
import {
|
|
27
30
|
firstLine,
|
|
28
31
|
padToVisible,
|
|
@@ -55,8 +58,10 @@ interface BgNotifyRecord {
|
|
|
55
58
|
id: string;
|
|
56
59
|
/** v4 B-1: closed(终态,含 cancelled)或 running(对话模式轮次完成,旧 idle)。 */
|
|
57
60
|
status: "running" | "closed";
|
|
58
|
-
/** L2
|
|
59
|
-
closedReason?:
|
|
61
|
+
/** L2 关闭原因子枚举(内部诊断 + outcome 兑底派生输入;经 toClosedReason 防御性收窄)。 */
|
|
62
|
+
closedReason?: ClosedReason;
|
|
63
|
+
/** 终态三态对外语义(U3 C-outcome)。缺失(升级前旧消息重放)时按 deriveOutcome 兑底。 */
|
|
64
|
+
outcome?: ExecutionOutcome;
|
|
60
65
|
agent: string;
|
|
61
66
|
model?: string;
|
|
62
67
|
result?: string;
|
|
@@ -218,12 +223,15 @@ function renderRecordLines(record: BgNotifyRecord, t: ThemeLike): string[] {
|
|
|
218
223
|
const modelPart = record.model
|
|
219
224
|
? ` ${t.fg("dim", "·")} ${t.fg("accent", truncLine(record.model, MODEL_MAX_WIDTH))}`
|
|
220
225
|
: "";
|
|
221
|
-
//
|
|
222
|
-
|
|
226
|
+
// U3 C-outcome:verb 与正文分流只读 outcome——单一权威派生(升级前旧消息重放等
|
|
227
|
+
// details 缺 outcome 的存量形态经 deriveOutcome(closedReason, error) 兑底,非同构
|
|
228
|
+
// 重写)。判定先于 patchFile:failed 分支不展示 patch/result(失败轮也会写
|
|
229
|
+
// patchFile,历史 bug 存档见 deriveOutcome 注释)。
|
|
230
|
+
const outcome = record.outcome ?? deriveOutcome(record.closedReason, record.error);
|
|
223
231
|
let verb: string;
|
|
224
|
-
if (
|
|
232
|
+
if (outcome === "cancelled") {
|
|
225
233
|
verb = "cancelled";
|
|
226
|
-
} else if (
|
|
234
|
+
} else if (outcome === "failed") {
|
|
227
235
|
verb = "failed";
|
|
228
236
|
} else {
|
|
229
237
|
verb = "finished";
|
|
@@ -233,13 +241,12 @@ function renderRecordLines(record: BgNotifyRecord, t: ThemeLike): string[] {
|
|
|
233
241
|
|
|
234
242
|
switch (record.status) {
|
|
235
243
|
case "closed": {
|
|
236
|
-
//
|
|
237
|
-
|
|
238
|
-
if (r === "cancelled") {
|
|
244
|
+
// U3 C-outcome:cancelled 无正文;failed 显示错误;否则结果/patch(同上,分流只读 outcome)。
|
|
245
|
+
if (outcome === "cancelled") {
|
|
239
246
|
return [head];
|
|
240
247
|
}
|
|
241
|
-
if (
|
|
242
|
-
return [head, t.fg("dim", truncLine(`Error: ${firstLineSanitized(record.error)}`, BODY_MAX_WIDTH))];
|
|
248
|
+
if (outcome === "failed") {
|
|
249
|
+
return [head, t.fg("dim", truncLine(`Error: ${firstLineSanitized(record.error ?? "")}`, BODY_MAX_WIDTH))];
|
|
243
250
|
}
|
|
244
251
|
if (!record.result && !record.patchFile) return [head];
|
|
245
252
|
const lines: string[] = [];
|
|
@@ -275,6 +282,19 @@ function extractBatch(details: unknown): BgNotifyRecord[] | undefined {
|
|
|
275
282
|
return records.length > 0 ? records : undefined;
|
|
276
283
|
}
|
|
277
284
|
|
|
285
|
+
/**
|
|
286
|
+
* details.closedReason 防御性收窄:任意字符串 → ClosedReason | undefined。
|
|
287
|
+
* 旧数据/外部构造的非法值按缺失处理,交由 deriveOutcome 兑底(消费方不崩溃)。
|
|
288
|
+
*/
|
|
289
|
+
function toClosedReason(value: unknown): ClosedReason | undefined {
|
|
290
|
+
return CLOSED_REASONS.find((reason) => reason === value);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** details.outcome 防御性收窄:仅接受三态枚举值,其余按缺失处理。 */
|
|
294
|
+
function toOutcome(value: unknown): ExecutionOutcome | undefined {
|
|
295
|
+
return value === "completed" || value === "failed" || value === "cancelled" ? value : undefined;
|
|
296
|
+
}
|
|
297
|
+
|
|
278
298
|
/**
|
|
279
299
|
* 从 message.details 防御性提取 BgNotifyRecord。
|
|
280
300
|
* 结构不全(缺 status / agent)返回 undefined。
|
|
@@ -297,7 +317,8 @@ function extractBgNotifyRecord(details: unknown): BgNotifyRecord | undefined {
|
|
|
297
317
|
model: typeof d.model === "string" ? d.model : undefined,
|
|
298
318
|
result: typeof d.result === "string" ? d.result : undefined,
|
|
299
319
|
error: typeof d.error === "string" ? d.error : undefined,
|
|
300
|
-
closedReason:
|
|
320
|
+
closedReason: toClosedReason(d.closedReason),
|
|
321
|
+
outcome: toOutcome(d.outcome),
|
|
301
322
|
round: typeof d.round === "number" ? d.round : undefined,
|
|
302
323
|
// [MF#1] 提取 patchFile(worktree background 完成通知携带)。
|
|
303
324
|
patchFile: typeof d.patchFile === "string" ? d.patchFile : undefined,
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
|
|
22
22
|
import type { ExtensionAPI, ExtensionCommandContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
23
23
|
|
|
24
|
-
import type { LauncherDeps } from "
|
|
25
|
-
import { abortRun } from "
|
|
26
|
-
import type { WorkflowRun } from "
|
|
24
|
+
import type { LauncherDeps } from "@zhushanwen/subagent-core/orchestration/launcher.ts";
|
|
25
|
+
import { abortRun } from "@zhushanwen/subagent-core/orchestration/lifecycle.ts";
|
|
26
|
+
import type { WorkflowRun } from "@zhushanwen/subagent-core/orchestration/models/workflow-run.ts";
|
|
27
27
|
import { parseWorkflowRpcCommand } from "./command-actions.ts";
|
|
28
28
|
import { createWorkflowsView, type ViewActions } from "./views/WorkflowsView.ts";
|
|
29
29
|
|
package/src/interface/format.ts
CHANGED
|
@@ -14,8 +14,8 @@ import os from "node:os";
|
|
|
14
14
|
|
|
15
15
|
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
16
16
|
|
|
17
|
-
import type { AgentEventLogEntry, DisplayItem, ExecutionStatus } from "
|
|
18
|
-
import { DEFAULT_AGENT_NAME } from "
|
|
17
|
+
import type { AgentEventLogEntry, DisplayItem, ExecutionStatus } from "@zhushanwen/subagent-core/execution/types.ts";
|
|
18
|
+
import { DEFAULT_AGENT_NAME } from "@zhushanwen/subagent-core/execution/types.ts";
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* ThemeLike:TUI 语义 token 着色接口(duck-typed,兼容 Pi Theme).
|
package/src/interface/helpers.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
12
12
|
|
|
13
|
-
import type { WorkflowRun } from "
|
|
13
|
+
import type { WorkflowRun } from "@zhushanwen/subagent-core/orchestration/models/workflow-run.ts";
|
|
14
14
|
import {
|
|
15
15
|
guiComponent,
|
|
16
16
|
type GuiContext,
|
|
@@ -238,7 +238,7 @@ export function notifyDone(
|
|
|
238
238
|
|
|
239
239
|
const content = parts.join("\n");
|
|
240
240
|
|
|
241
|
-
// deliverAs:"steer" + triggerTurn:true —— workflow 完成作为 steering
|
|
241
|
+
// deliverAs:"steer" + triggerTurn:true —— workflow 完成作为 steering 消息注入(g4-allow: 存量待迁移——结果语义通知,账本化迁移登记 pi-boundary-reliability 附录 B 待办)
|
|
242
242
|
// 并立即唤醒 parent agent 处理结果(与 subagent 的 followUp+triggerTurn 对称)
|
|
243
243
|
const details: WorkflowNotifyDetails = {
|
|
244
244
|
runId,
|
|
@@ -275,7 +275,7 @@ export function notifyDone(
|
|
|
275
275
|
display: true,
|
|
276
276
|
details,
|
|
277
277
|
},
|
|
278
|
-
{ triggerTurn: true, deliverAs: "steer" },
|
|
278
|
+
{ triggerTurn: true, deliverAs: "steer" }, // g4-allow: 存量待迁移——workflow 完成通知属结果语义,迁移切片复用 U2 账本设施(附录 B 待办)
|
|
279
279
|
);
|
|
280
280
|
}
|
|
281
281
|
|