@zhushanwen/subagent-core 0.2.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 +114 -0
- package/dist/chunk-3VOERJPJ.js +22 -0
- package/dist/chunk-A4IVZWVX.js +31 -0
- package/dist/execution/engine/engines/zcode/constants.cjs +54 -0
- package/dist/execution/engine/engines/zcode/constants.d.cts +31 -0
- package/dist/execution/engine/engines/zcode/constants.d.ts +31 -0
- package/dist/execution/engine/engines/zcode/constants.js +22 -0
- package/dist/execution/engine/engines/zcode/reader.cjs +276 -0
- package/dist/execution/engine/engines/zcode/reader.d.cts +20 -0
- package/dist/execution/engine/engines/zcode/reader.d.ts +20 -0
- package/dist/execution/engine/engines/zcode/reader.js +239 -0
- package/dist/execution/engine/paths.cjs +55 -0
- package/dist/execution/engine/paths.d.cts +8 -0
- package/dist/execution/engine/paths.d.ts +8 -0
- package/dist/execution/engine/paths.js +26 -0
- package/dist/execution/relay-env.cjs +62 -0
- package/dist/execution/relay-env.d.cts +33 -0
- package/dist/execution/relay-env.d.ts +33 -0
- package/dist/execution/relay-env.js +20 -0
- package/dist/index.cjs +1852 -0
- package/dist/index.d.cts +1184 -0
- package/dist/index.d.ts +1184 -0
- package/dist/index.js +1810 -0
- package/dist/types-BxyAidGf.d.cts +875 -0
- package/dist/types-BxyAidGf.d.ts +875 -0
- package/package.json +99 -0
- package/src/__tests__/agent-opts-resolver-schema-prompt.test.ts +142 -0
- package/src/__tests__/append-system-prompt-assembly.test.ts +242 -0
- package/src/__tests__/builtin-workflows-structure.test.ts +180 -0
- package/src/__tests__/derive-closed-display-parity.test.ts +93 -0
- package/src/__tests__/fr4-get-state-handshake.test.ts +125 -0
- package/src/__tests__/fr8-orphan-recovery.test.ts +259 -0
- package/src/__tests__/m2-append-content-probe.test.ts +73 -0
- package/src/__tests__/manifest-store.test.ts +368 -0
- package/src/__tests__/record-store-cache.test.ts +335 -0
- package/src/__tests__/record-store-index.test.ts +560 -0
- package/src/__tests__/record-store-last-line.test.ts +129 -0
- package/src/__tests__/review-fix-loop-script.test.ts +288 -0
- package/src/__tests__/review-fix-loop-utils.test.ts +2377 -0
- package/src/__tests__/robustness-low-batch1.test.ts +286 -0
- package/src/__tests__/robustness-medium-batch1.test.ts +69 -0
- package/src/__tests__/robustness-medium-batch2.test.ts +65 -0
- package/src/__tests__/robustness-medium-batch3.test.ts +30 -0
- package/src/__tests__/robustness-medium-batch4.test.ts +358 -0
- package/src/__tests__/session-runner.test.ts +69 -0
- package/src/__tests__/smoke.test.ts +9 -0
- package/src/__tests__/stream-sink.test.ts +203 -0
- package/src/core/__tests__/host-services.test.ts +144 -0
- package/src/core/__tests__/logger.test.ts +78 -0
- package/src/core/__tests__/notify-ports.test.ts +104 -0
- package/src/core/host-services.ts +97 -0
- package/src/core/logger.ts +47 -0
- package/src/core/notify-ports.ts +126 -0
- package/src/execution/__tests__/__fixtures__/notifier-golden-snapshots.json +53 -0
- package/src/execution/__tests__/__fixtures__/truncline.snapshot.json +1 -0
- package/src/execution/__tests__/agent-result-mapper.test.ts +150 -0
- package/src/execution/__tests__/alive-store.test.ts +147 -0
- package/src/execution/__tests__/ask-user-transit-e2e.test.ts +490 -0
- package/src/execution/__tests__/channel-registry-handshake.test.ts +242 -0
- package/src/execution/__tests__/chat-engine-routing.test.ts +610 -0
- package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +190 -0
- package/src/execution/__tests__/concurrency-pool.test.ts +250 -0
- package/src/execution/__tests__/config.test.ts +302 -0
- package/src/execution/__tests__/delivery-methods.test.ts +424 -0
- package/src/execution/__tests__/dialog-queue.test.ts +299 -0
- package/src/execution/__tests__/epipe-fallback.test.ts +241 -0
- package/src/execution/__tests__/execute-and-await-worktree.test.ts +243 -0
- package/src/execution/__tests__/execute-nesting.test.ts +323 -0
- package/src/execution/__tests__/execute-options-mapper.test.ts +200 -0
- package/src/execution/__tests__/execution-record.test.ts +1394 -0
- package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +171 -0
- package/src/execution/__tests__/finalize-record.test.ts +367 -0
- package/src/execution/__tests__/finalized-marker.test.ts +104 -0
- package/src/execution/__tests__/format-schema-instruction.test.ts +169 -0
- package/src/execution/__tests__/gc-timer.test.ts +184 -0
- package/src/execution/__tests__/get-record-for-action-restart.test.ts +254 -0
- package/src/execution/__tests__/helpers/mock-extension-api.ts +30 -0
- package/src/execution/__tests__/helpers/spawn-mock.ts +242 -0
- package/src/execution/__tests__/host-mode.test.ts +87 -0
- package/src/execution/__tests__/lifecycle-manager-lock.test.ts +211 -0
- package/src/execution/__tests__/lifecycle-manager.test.ts +383 -0
- package/src/execution/__tests__/lifecycle-predicates.test.ts +116 -0
- package/src/execution/__tests__/manifest-parentid.test.ts +117 -0
- package/src/execution/__tests__/model-resolver.test.ts +465 -0
- package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +287 -0
- package/src/execution/__tests__/nested-visibility.test.ts +325 -0
- package/src/execution/__tests__/output-collector.test.ts +358 -0
- package/src/execution/__tests__/path-encoding.test.ts +104 -0
- package/src/execution/__tests__/pi-invocation.test.ts +134 -0
- package/src/execution/__tests__/record-store.test.ts +1057 -0
- package/src/execution/__tests__/records-cwd-isolation.test.ts +91 -0
- package/src/execution/__tests__/recursive-visibility-baseline.test.ts +339 -0
- package/src/execution/__tests__/recursive-visibility-env.test.ts +273 -0
- package/src/execution/__tests__/relay-env.test.ts +42 -0
- package/src/execution/__tests__/resource-policy.test.ts +109 -0
- package/src/execution/__tests__/rpc-mode.test.ts +89 -0
- package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +267 -0
- package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +253 -0
- package/src/execution/__tests__/run-spawn-edges.test.ts +687 -0
- package/src/execution/__tests__/run-spawn-integration.test.ts +933 -0
- package/src/execution/__tests__/run-spawn-resume.test.ts +322 -0
- package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +196 -0
- package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +199 -0
- package/src/execution/__tests__/session-context-resolver.test.ts +167 -0
- package/src/execution/__tests__/session-file-gc.test.ts +293 -0
- package/src/execution/__tests__/session-reconstructor.test.ts +379 -0
- package/src/execution/__tests__/session-runner-epipe.test.ts +178 -0
- package/src/execution/__tests__/session-runner-schema-env.test.ts +350 -0
- package/src/execution/__tests__/spawn-args.test.ts +445 -0
- package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +189 -0
- package/src/execution/__tests__/spawn-event-adapter.test.ts +167 -0
- package/src/execution/__tests__/spawn-worktree-guidance.test.ts +207 -0
- package/src/execution/__tests__/spawned-children.test.ts +92 -0
- package/src/execution/__tests__/start-sync-model-guard.test.ts +150 -0
- package/src/execution/__tests__/stdin-writer.test.ts +462 -0
- package/src/execution/__tests__/stream-sink-retirement.test.ts +261 -0
- package/src/execution/__tests__/subagent-service-abort.test.ts +60 -0
- package/src/execution/__tests__/subagent-service-message-close.test.ts +629 -0
- package/src/execution/__tests__/subagent-service-parent-guard.test.ts +180 -0
- package/src/execution/__tests__/subagent-service.test.ts +788 -0
- package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +310 -0
- package/src/execution/__tests__/subprocess-agent-runner.test.ts +612 -0
- package/src/execution/__tests__/temp-prompt.test.ts +53 -0
- package/src/execution/__tests__/timeout-integration.test.ts +615 -0
- package/src/execution/__tests__/tombstone-store.test.ts +73 -0
- package/src/execution/__tests__/turn-limiter-semantics.test.ts +194 -0
- package/src/execution/__tests__/turn-limiter.test.ts +65 -0
- package/src/execution/__tests__/ui-channels.test.ts +187 -0
- package/src/execution/__tests__/ui-interaction-model.test.ts +67 -0
- package/src/execution/__tests__/ui-request-handler-factory.test.ts +369 -0
- package/src/execution/__tests__/ui-request-handler.test.ts +204 -0
- package/src/execution/__tests__/ui-request-observability.test.ts +113 -0
- package/src/execution/__tests__/ui-request-queue.test.ts +133 -0
- package/src/execution/__tests__/worktree-manager.test.ts +633 -0
- package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +229 -0
- package/src/execution/__tests__/worktree-reconcile.integration.test.ts +181 -0
- package/src/execution/__tests__/worktree-registry.test.ts +199 -0
- package/src/execution/agent-registry.ts +199 -0
- package/src/execution/agent-result-mapper.ts +90 -0
- package/src/execution/alive-store.ts +119 -0
- package/src/execution/argv-mirror.ts +113 -0
- package/src/execution/best-effort.ts +38 -0
- package/src/execution/channel-registry-access.ts +144 -0
- package/src/execution/concurrency-pool.ts +116 -0
- package/src/execution/config.ts +165 -0
- package/src/execution/dialog-queue.ts +330 -0
- package/src/execution/engine/__tests__/common/data-dir.test.ts +78 -0
- package/src/execution/engine/__tests__/common/errors.test.ts +132 -0
- package/src/execution/engine/__tests__/common/event-journal.test.ts +177 -0
- package/src/execution/engine/__tests__/common/kill-chain.test.ts +192 -0
- package/src/execution/engine/__tests__/common/nesting-guard.test.ts +81 -0
- package/src/execution/engine/__tests__/common/persona-router.test.ts +123 -0
- package/src/execution/engine/__tests__/common/pool-manager.test.ts +154 -0
- package/src/execution/engine/__tests__/common/schema-emulation.test.ts +128 -0
- package/src/execution/engine/__tests__/conformance/__fixtures__/pi-golden-events.json +28 -0
- package/src/execution/engine/__tests__/conformance/agent-event-invariants.ts +141 -0
- package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +109 -0
- package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +101 -0
- package/src/execution/engine/__tests__/conformance/contract.probe.test.ts +77 -0
- package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +104 -0
- package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +201 -0
- package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +76 -0
- package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +79 -0
- package/src/execution/engine/__tests__/engine-discovery.test.ts +87 -0
- package/src/execution/engine/__tests__/model-prompt.test.ts +197 -0
- package/src/execution/engine/__tests__/paths.test.ts +39 -0
- package/src/execution/engine/__tests__/registry.test.ts +120 -0
- package/src/execution/engine/__tests__/routing.test.ts +231 -0
- package/src/execution/engine/common/data-dir.ts +66 -0
- package/src/execution/engine/common/errors.ts +183 -0
- package/src/execution/engine/common/event-journal.ts +254 -0
- package/src/execution/engine/common/journal-replay.ts +62 -0
- package/src/execution/engine/common/kill-chain.ts +221 -0
- package/src/execution/engine/common/nesting-guard.ts +50 -0
- package/src/execution/engine/common/persona-router.ts +108 -0
- package/src/execution/engine/common/pool-manager.ts +226 -0
- package/src/execution/engine/common/schema-emulation.ts +189 -0
- package/src/execution/engine/common/session-view-projection.ts +51 -0
- package/src/execution/engine/engine-discovery.ts +65 -0
- package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +469 -0
- package/src/execution/engine/engines/pi/__tests__/reader.test.ts +155 -0
- package/src/execution/engine/engines/pi/__tests__/task-spec-mapper.test.ts +164 -0
- package/src/execution/engine/engines/pi/pi-engine.ts +415 -0
- package/src/execution/engine/engines/pi/reader.ts +48 -0
- package/src/execution/engine/engines/pi/registration.ts +35 -0
- package/src/execution/engine/engines/pi/task-spec-mapper.ts +100 -0
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-spawn.json +39 -0
- package/src/execution/engine/engines/zcode/__tests__/launcher.test.ts +150 -0
- package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +246 -0
- package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +228 -0
- package/src/execution/engine/engines/zcode/__tests__/reader.test.ts +210 -0
- package/src/execution/engine/engines/zcode/__tests__/registration.test.ts +71 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +127 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.test.ts +580 -0
- package/src/execution/engine/engines/zcode/constants.ts +43 -0
- package/src/execution/engine/engines/zcode/golden-sample.ts +39 -0
- package/src/execution/engine/engines/zcode/launcher.ts +161 -0
- package/src/execution/engine/engines/zcode/parser.ts +436 -0
- package/src/execution/engine/engines/zcode/preparer.ts +363 -0
- package/src/execution/engine/engines/zcode/reader.ts +381 -0
- package/src/execution/engine/engines/zcode/registration.ts +37 -0
- package/src/execution/engine/engines/zcode/zcode-engine.ts +658 -0
- package/src/execution/engine/host-task-spec.ts +47 -0
- package/src/execution/engine/model-prompt.ts +158 -0
- package/src/execution/engine/paths.ts +42 -0
- package/src/execution/engine/port.ts +153 -0
- package/src/execution/engine/registry.ts +133 -0
- package/src/execution/engine/routing.ts +218 -0
- package/src/execution/engine/types.ts +309 -0
- package/src/execution/execute-options-mapper.ts +115 -0
- package/src/execution/execution-record.ts +984 -0
- package/src/execution/finalize-record.ts +254 -0
- package/src/execution/finalized-marker.ts +70 -0
- package/src/execution/get-state-handshake.ts +104 -0
- package/src/execution/host-mode.ts +56 -0
- package/src/execution/idle-gc.ts +47 -0
- package/src/execution/lifecycle-manager.ts +513 -0
- package/src/execution/lifecycle-predicates.ts +65 -0
- package/src/execution/manifest-store.ts +256 -0
- package/src/execution/model-config-service.ts +250 -0
- package/src/execution/model-resolver.ts +248 -0
- package/src/execution/notifier.ts +372 -0
- package/src/execution/notify-ledger.ts +580 -0
- package/src/execution/output-collector.ts +228 -0
- package/src/execution/path-encoding.ts +60 -0
- package/src/execution/pi-invocation.ts +120 -0
- package/src/execution/record-entry.ts +132 -0
- package/src/execution/record-store.ts +1265 -0
- package/src/execution/relay-env.ts +37 -0
- package/src/execution/session-context-resolver.ts +64 -0
- package/src/execution/session-file-gc.ts +120 -0
- package/src/execution/session-pending.ts +170 -0
- package/src/execution/session-reconstructor.ts +678 -0
- package/src/execution/session-runner.ts +1789 -0
- package/src/execution/sessions-index.ts +304 -0
- package/src/execution/spawn-event-adapter.ts +363 -0
- package/src/execution/stdin-writer.ts +198 -0
- package/src/execution/stream-sink.ts +126 -0
- package/src/execution/subagent-service.ts +2268 -0
- package/src/execution/subprocess-agent-runner.ts +317 -0
- package/src/execution/temp-prompt.ts +62 -0
- package/src/execution/tombstone-store.ts +72 -0
- package/src/execution/turn-limiter.ts +102 -0
- package/src/execution/types.ts +1023 -0
- package/src/execution/ui-channels.ts +216 -0
- package/src/execution/ui-interaction-model.ts +48 -0
- package/src/execution/ui-request-handler-factory.ts +243 -0
- package/src/execution/ui-request-observability.ts +81 -0
- package/src/execution/ui-request-queue.ts +184 -0
- package/src/execution/worktree-manager.ts +688 -0
- package/src/execution/worktree-registry.ts +279 -0
- package/src/index.ts +87 -0
- package/src/orchestration/__tests__/__fixtures__/worker-template.snapshot.txt +349 -0
- package/src/orchestration/__tests__/agent-call-catch-fallback.test.ts +202 -0
- package/src/orchestration/__tests__/agent-call-stream.test.ts +152 -0
- package/src/orchestration/__tests__/args-validator.test.ts +143 -0
- package/src/orchestration/__tests__/config-loader.test.ts +503 -0
- package/src/orchestration/__tests__/error-recovery-handlers.test.ts +809 -0
- package/src/orchestration/__tests__/error-recovery-postmessage-defense.test.ts +404 -0
- package/src/orchestration/__tests__/error-recovery-serialize-failed-result.test.ts +56 -0
- package/src/orchestration/__tests__/error-recovery-workflow-call.test.ts +166 -0
- package/src/orchestration/__tests__/execute-agent-call.test.ts +451 -0
- package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +600 -0
- package/src/orchestration/__tests__/lifecycle-runid-injection.test.ts +96 -0
- package/src/orchestration/__tests__/lifecycle.test.ts +659 -0
- package/src/orchestration/__tests__/non-cloneable-return-e2e.test.ts +95 -0
- package/src/orchestration/__tests__/review-fix-loop-scriptpath-failfast.test.ts +183 -0
- package/src/orchestration/__tests__/script-lint.test.ts +831 -0
- package/src/orchestration/__tests__/skill-discovery.test.ts +210 -0
- package/src/orchestration/__tests__/test-mocks.ts +197 -0
- package/src/orchestration/__tests__/worker-exit-without-result.test.ts +368 -0
- package/src/orchestration/__tests__/worker-host.test.ts +120 -0
- package/src/orchestration/__tests__/worker-returnmeta-passthrough.test.ts +164 -0
- package/src/orchestration/__tests__/worker-script-builder-runtime.test.ts +563 -0
- package/src/orchestration/__tests__/worker-script-builder.test.ts +309 -0
- package/src/orchestration/__tests__/worker-script-template-snapshot.test.ts +129 -0
- package/src/orchestration/__tests__/workflow-nesting-e2e.test.ts +317 -0
- package/src/orchestration/__tests__/workflow-script-lint-memo.test.ts +110 -0
- package/src/orchestration/agent-opts-resolver.ts +174 -0
- package/src/orchestration/args-validator.ts +127 -0
- package/src/orchestration/config-loader.ts +316 -0
- package/src/orchestration/error-recovery.ts +1018 -0
- package/src/orchestration/execute-agent-call.ts +256 -0
- package/src/orchestration/launcher.ts +455 -0
- package/src/orchestration/lifecycle.ts +399 -0
- package/src/orchestration/models/__tests__/budget.test.ts +307 -0
- package/src/orchestration/models/agent-call.ts +83 -0
- package/src/orchestration/models/budget.ts +118 -0
- package/src/orchestration/models/ports.ts +164 -0
- package/src/orchestration/models/run-runtime.ts +104 -0
- package/src/orchestration/models/run-spec.ts +83 -0
- package/src/orchestration/models/run-state.ts +44 -0
- package/src/orchestration/models/trace.ts +183 -0
- package/src/orchestration/models/types.ts +301 -0
- package/src/orchestration/models/workflow-run.ts +254 -0
- package/src/orchestration/models/workflow-script-registry.ts +35 -0
- package/src/orchestration/models/workflow-script.ts +117 -0
- package/src/orchestration/script-lint.ts +766 -0
- package/src/orchestration/skill-discovery.ts +135 -0
- package/src/orchestration/worker-handle.ts +115 -0
- package/src/orchestration/worker-host.ts +98 -0
- package/src/orchestration/worker-script-builder.ts +421 -0
- package/src/orchestration/workflow-files.ts +85 -0
- package/src/orchestration/workflow-script-registry-impl.ts +133 -0
- package/src/shared/__tests__/agent-ref.test.ts +34 -0
- package/src/shared/__tests__/meta-parser.test.ts +304 -0
- package/src/shared/__tests__/model-ref.test.ts +306 -0
- package/src/shared/__tests__/resource-discovery-manifest-cache.test.ts +288 -0
- package/src/shared/__tests__/resource-discovery.test.ts +749 -0
- package/src/shared/__tests__/resource-meta.test.ts +51 -0
- package/src/shared/__tests__/schema-jsonify.test.ts +81 -0
- package/src/shared/__tests__/timer-delay.test.ts +61 -0
- package/src/shared/agent-event.ts +13 -0
- package/src/shared/agent-ref.ts +57 -0
- package/src/shared/meta-parser.ts +261 -0
- package/src/shared/model-ref.ts +286 -0
- package/src/shared/resource-discovery.ts +835 -0
- package/src/shared/resource-meta.ts +65 -0
- package/src/shared/schema-env.ts +44 -0
- package/src/shared/schema-jsonify.ts +58 -0
- package/src/shared/timer-delay.ts +54 -0
- package/src/shared/xml-injection.ts +35 -0
- package/workflows/README.md +81 -0
- package/workflows/_shared/agent-refs.cjs +40 -0
- package/workflows/chain.js +137 -0
- package/workflows/map-reduce.js +180 -0
- package/workflows/parallel.js +154 -0
- package/workflows/review-fix-loop-utils.cjs +1542 -0
- package/workflows/review-fix-loop.js +1605 -0
- package/workflows/scatter-gather.js +184 -0
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
// src/__tests__/output-collector.test.ts
|
|
2
|
+
//
|
|
3
|
+
// 锁定 extractParsedOutput 纯函数契约——它被 collectResult 直接消费,决定
|
|
4
|
+
// AgentResult.parsedOutput 字段。toUsageTotal / collectResponseText 已删除
|
|
5
|
+
// (usage 收口进 getTotalUsage,text 收口进 getFullText,均在 execution-record.test 测)。
|
|
6
|
+
import { describe, expect, it } from "vitest";
|
|
7
|
+
|
|
8
|
+
import { isStaleContextErrorMsg, DETERMINISTIC_SCHEMA_FAILURE_PREFIX, isDeterministicSchemaFailureMsg, STALE_CONTEXT_PATTERNS } from "../../orchestration/execute-agent-call.ts";
|
|
9
|
+
import { collectResult, describeMissingParsedOutput, extractParsedOutput, neutralizeStalePatterns } from "../output-collector.ts";
|
|
10
|
+
import type { ExecutionRecord, ToolCall } from "../types.ts";
|
|
11
|
+
|
|
12
|
+
// ============================================================
|
|
13
|
+
// extractParsedOutput
|
|
14
|
+
// ============================================================
|
|
15
|
+
|
|
16
|
+
describe("extractParsedOutput", () => {
|
|
17
|
+
it("returns undefined for empty toolCalls", () => {
|
|
18
|
+
expect(extractParsedOutput([])).toBeUndefined();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("returns undefined when no structured-output call exists", () => {
|
|
22
|
+
const calls: ToolCall[] = [
|
|
23
|
+
{ toolName: "bash", result: { details: "x" } },
|
|
24
|
+
{ toolName: "read", result: { details: "y" } },
|
|
25
|
+
];
|
|
26
|
+
expect(extractParsedOutput(calls)).toBeUndefined();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("returns undefined when structured-output has no result.details", () => {
|
|
30
|
+
const calls: ToolCall[] = [
|
|
31
|
+
{ toolName: "structured-output", result: { content: [] } },
|
|
32
|
+
{ toolName: "structured-output", result: {} },
|
|
33
|
+
{ toolName: "structured-output" },
|
|
34
|
+
];
|
|
35
|
+
expect(extractParsedOutput(calls)).toBeUndefined();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("returns details when exactly one structured-output call has details", () => {
|
|
39
|
+
const calls: ToolCall[] = [
|
|
40
|
+
{ toolName: "bash" },
|
|
41
|
+
{ toolName: "structured-output", result: { details: { answer: 42 } } },
|
|
42
|
+
];
|
|
43
|
+
expect(extractParsedOutput(calls)).toEqual({ answer: 42 });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("returns the LAST structured-output details (reverse iteration)", () => {
|
|
47
|
+
const calls: ToolCall[] = [
|
|
48
|
+
{ toolName: "structured-output", result: { details: "first" } },
|
|
49
|
+
{ toolName: "bash" },
|
|
50
|
+
{ toolName: "structured-output", result: { details: "second" } },
|
|
51
|
+
];
|
|
52
|
+
expect(extractParsedOutput(calls)).toBe("second");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("ignores isError structured-output calls without details, picks one with details", () => {
|
|
56
|
+
const calls: ToolCall[] = [
|
|
57
|
+
{ toolName: "structured-output", isError: true, result: { content: [{ type: "text", text: "bad" }] } },
|
|
58
|
+
{ toolName: "structured-output", result: { details: { ok: true } } },
|
|
59
|
+
];
|
|
60
|
+
expect(extractParsedOutput(calls)).toEqual({ ok: true });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// [F-1 失败吞没修复] 失败调用(pi 失败路径 details 默认 {})的 details 不是
|
|
64
|
+
// schema 校验产出——journal 实锤 tool_end isError=true details={} 曾被当
|
|
65
|
+
// parsedOutput,gate 终止/模型自弃的 run 以 parsedOutput={} 静默 completed。
|
|
66
|
+
it("skips failed structured-output call with details:{} (isError=true) — no parsedOutput", () => {
|
|
67
|
+
const calls: ToolCall[] = [
|
|
68
|
+
{ toolName: "bash", result: { content: [] } },
|
|
69
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text: "Schema validation failed: /target is required" }] } },
|
|
70
|
+
];
|
|
71
|
+
expect(extractParsedOutput(calls)).toBeUndefined();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("skips failed call with details, still picks later valid call (reverse order)", () => {
|
|
75
|
+
const calls: ToolCall[] = [
|
|
76
|
+
{ toolName: "structured-output", isError: true, result: { details: {} } },
|
|
77
|
+
{ toolName: "structured-output", result: { details: { fixed: true } } },
|
|
78
|
+
];
|
|
79
|
+
expect(extractParsedOutput(calls)).toEqual({ fixed: true });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("isError on non-structured-output calls does not affect extraction", () => {
|
|
83
|
+
const calls: ToolCall[] = [
|
|
84
|
+
{ toolName: "bash", isError: true, result: { details: "ignored" } },
|
|
85
|
+
{ toolName: "structured-output", result: { details: { answer: 1 } } },
|
|
86
|
+
];
|
|
87
|
+
expect(extractParsedOutput(calls)).toEqual({ answer: 1 });
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// ============================================================
|
|
92
|
+
// describeMissingParsedOutput(F-1 三态归因)
|
|
93
|
+
// ============================================================
|
|
94
|
+
|
|
95
|
+
describe("describeMissingParsedOutput", () => {
|
|
96
|
+
it("returns undefined when a valid parsedOutput exists", () => {
|
|
97
|
+
const calls: ToolCall[] = [
|
|
98
|
+
{ toolName: "structured-output", isError: true, result: { details: {} } },
|
|
99
|
+
{ toolName: "structured-output", result: { details: { ok: 1 } } },
|
|
100
|
+
];
|
|
101
|
+
expect(describeMissingParsedOutput(calls)).toBeUndefined();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("state 1: validation failure — isError calls present, error carries last summary", () => {
|
|
105
|
+
const calls: ToolCall[] = [
|
|
106
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text: "Schema validation failed: /target is required" }] } },
|
|
107
|
+
];
|
|
108
|
+
const msg = describeMissingParsedOutput(calls);
|
|
109
|
+
expect(msg).toContain("failed");
|
|
110
|
+
expect(msg).toContain("1 structured-output call(s) failed");
|
|
111
|
+
expect(msg).toContain("Schema validation failed: /target is required");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("state 2: structured-output never called — error hints to check extension install (C1 blind spot)", () => {
|
|
115
|
+
const calls: ToolCall[] = [
|
|
116
|
+
{ toolName: "bash", result: { content: [] } },
|
|
117
|
+
{ toolName: "read", result: { content: [] } },
|
|
118
|
+
];
|
|
119
|
+
const msg = describeMissingParsedOutput(calls);
|
|
120
|
+
expect(msg).toContain("structured-output tool was never called");
|
|
121
|
+
expect(msg).toContain("structured-output extension is installed");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("state 3: SO called successfully but no details", () => {
|
|
125
|
+
const calls: ToolCall[] = [
|
|
126
|
+
{ toolName: "structured-output", result: { content: [] } },
|
|
127
|
+
];
|
|
128
|
+
const msg = describeMissingParsedOutput(calls);
|
|
129
|
+
expect(msg).toContain("none of the successful calls carried result details");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("empty toolCalls → state 2 (never called)", () => {
|
|
133
|
+
expect(describeMissingParsedOutput([])).toContain("never called");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("summary truncates long error text (300 chars cap)", () => {
|
|
137
|
+
const longText = "x".repeat(500);
|
|
138
|
+
const calls: ToolCall[] = [
|
|
139
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text: longText }] } },
|
|
140
|
+
];
|
|
141
|
+
const msg = describeMissingParsedOutput(calls);
|
|
142
|
+
expect(msg).toContain("...");
|
|
143
|
+
expect(msg!.length).toBeLessThan(longText.length);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// 文案约束:不得命中 execute-agent-call 的 STALE_CONTEXT_PATTERNS("aborted" 等),
|
|
147
|
+
// 否则会被误诊为 stale-context 跳过重试。
|
|
148
|
+
it("messages avoid STALE_CONTEXT_PATTERNS substrings (no false stale-context triage)", () => {
|
|
149
|
+
const samples = [
|
|
150
|
+
describeMissingParsedOutput([])!,
|
|
151
|
+
describeMissingParsedOutput([{ toolName: "structured-output", isError: true, result: { details: {} } }])!,
|
|
152
|
+
describeMissingParsedOutput([{ toolName: "structured-output", result: { content: [] } }])!,
|
|
153
|
+
];
|
|
154
|
+
for (const msg of samples) {
|
|
155
|
+
const lower = msg.toLowerCase();
|
|
156
|
+
expect(lower).not.toContain("aborted");
|
|
157
|
+
expect(lower).not.toContain("ctx is stale");
|
|
158
|
+
expect(lower).not.toContain("context canceled");
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// ── [F-R1] 态2(isError 分支)错误摘要拼接段中和 ──
|
|
163
|
+
//
|
|
164
|
+
// 动态错误文本(模型/provider 原始错误)可能携带 "aborted"/"ctx is stale" 等
|
|
165
|
+
// STALE_CONTEXT_PATTERNS 词;不中和则归因 error 经 collectResult → result.error
|
|
166
|
+
// 流到 execute-agent-call 的 isStaleContextErrorMsg 分诊,被误诊为 stale-context
|
|
167
|
+
// 跳过重试。固定前缀静态无命中(上一用例锁定),中和只针对动态段。
|
|
168
|
+
describe("F-R1: 态2 错误摘要 STALE_CONTEXT_PATTERNS 中和", () => {
|
|
169
|
+
function failedSoCallsWith(text: string): ToolCall[] {
|
|
170
|
+
return [
|
|
171
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text }] } },
|
|
172
|
+
];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
it("失败 content 含 'aborted'/'ctx is stale' → 摘要被 [redacted],归因 error 不触发 isStaleContextErrorMsg", () => {
|
|
176
|
+
const msg = describeMissingParsedOutput(
|
|
177
|
+
failedSoCallsWith("Request aborted: context canceled — ctx is stale after session replacement"),
|
|
178
|
+
)!;
|
|
179
|
+
expect(msg).toContain("[redacted]");
|
|
180
|
+
const lower = msg.toLowerCase();
|
|
181
|
+
expect(lower).not.toContain("aborted");
|
|
182
|
+
expect(lower).not.toContain("ctx is stale");
|
|
183
|
+
expect(lower).not.toContain("context canceled");
|
|
184
|
+
// 消费点语义锁定:归因 error 进入 stale-context 分诊必须为 false(可重试)
|
|
185
|
+
expect(isStaleContextErrorMsg(msg)).toBe(false);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("大小写变体('ABORTED')同样被中和(分诊是大小写不敏感子串匹配)", () => {
|
|
189
|
+
const msg = describeMissingParsedOutput(failedSoCallsWith("ABORTED by provider"))!;
|
|
190
|
+
expect(msg).toContain("[redacted]");
|
|
191
|
+
expect(isStaleContextErrorMsg(msg)).toBe(false);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("无动态内容时整条归因也不命中(防御回归锁定)", () => {
|
|
195
|
+
const msg = describeMissingParsedOutput([
|
|
196
|
+
{ toolName: "structured-output", isError: true, result: { details: {} } },
|
|
197
|
+
])!;
|
|
198
|
+
expect(isStaleContextErrorMsg(msg)).toBe(false);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("neutralizeStalePatterns:多 pattern 同时命中 + 无命中原样透传", () => {
|
|
202
|
+
expect(neutralizeStalePatterns("aborted CTX IS STALE / context canceled")).toBe(
|
|
203
|
+
"[redacted] [redacted] / [redacted]",
|
|
204
|
+
);
|
|
205
|
+
expect(neutralizeStalePatterns("provider socket hang up")).toBe("provider socket hang up");
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// ── [F-R4] 态2 文案按最后错误内容分类,不再硬编码 "(schema validation)" ──
|
|
210
|
+
describe("F-R4: 态2 失败原因分类", () => {
|
|
211
|
+
it("最后错误含 'validation failed'(大小写不敏感)→ (schema validation)", () => {
|
|
212
|
+
const calls: ToolCall[] = [
|
|
213
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text: "Schema validation failed: /target is required" }] } },
|
|
214
|
+
];
|
|
215
|
+
const msg = describeMissingParsedOutput(calls)!;
|
|
216
|
+
expect(msg).toContain("(schema validation)");
|
|
217
|
+
expect(msg).not.toContain("(execution failure)");
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("最后错误为非校验失败(如 provider 错误)→ (execution failure)", () => {
|
|
221
|
+
const calls: ToolCall[] = [
|
|
222
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text: "provider socket hang up" }] } },
|
|
223
|
+
];
|
|
224
|
+
const msg = describeMissingParsedOutput(calls)!;
|
|
225
|
+
expect(msg).toContain("(execution failure)");
|
|
226
|
+
expect(msg).not.toContain("(schema validation)");
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// ============================================================
|
|
232
|
+
// MF-1: 三态可重试性矩阵(确定性失败标记)
|
|
233
|
+
// ============================================================
|
|
234
|
+
|
|
235
|
+
describe("MF-1: 三态可重试性矩阵(确定性失败标记)", () => {
|
|
236
|
+
// 矩阵(SSOT 注释在 execute-agent-call DETERMINISTIC_SCHEMA_FAILURE_PREFIX 与
|
|
237
|
+
// describeMissingParsedOutput JSDoc,本 describe 是执行面锁定):
|
|
238
|
+
// 态① 从未调用 SO → 带标记 → 不可重试(环境确定性,C1 安装盲区)
|
|
239
|
+
// 态② SO isError(gate 终止/不可满足 schema)→ 带标记 → 不可重试(同 schema 重试必同结果)
|
|
240
|
+
// 态③ 调用过但无 details → 无标记 → 可重试(可能瞬态,保留既有重试语义)
|
|
241
|
+
|
|
242
|
+
it("态① 从未调用 SO → error 以确定性标记开头(不可重试)", () => {
|
|
243
|
+
const msg = describeMissingParsedOutput([])!;
|
|
244
|
+
expect(msg.startsWith(DETERMINISTIC_SCHEMA_FAILURE_PREFIX)).toBe(true);
|
|
245
|
+
expect(isDeterministicSchemaFailureMsg(msg)).toBe(true);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("态② isError/schema validation 子类 → error 以确定性标记开头(不可重试)", () => {
|
|
249
|
+
const msg = describeMissingParsedOutput([
|
|
250
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text: "Schema validation failed: /target is required" }] } },
|
|
251
|
+
])!;
|
|
252
|
+
expect(msg.startsWith(DETERMINISTIC_SCHEMA_FAILURE_PREFIX)).toBe(true);
|
|
253
|
+
expect(isDeterministicSchemaFailureMsg(msg)).toBe(true);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("态② execution failure 子类(provider 瞬态)同样带标记(isError 态整体不重试)", () => {
|
|
257
|
+
const msg = describeMissingParsedOutput([
|
|
258
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text: "provider socket hang up" }] } },
|
|
259
|
+
])!;
|
|
260
|
+
expect(isDeterministicSchemaFailureMsg(msg)).toBe(true);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("态③ 调用过但无 details → 不带标记(可重试语义保留)", () => {
|
|
264
|
+
const msg = describeMissingParsedOutput([
|
|
265
|
+
{ toolName: "structured-output", result: { content: [] } },
|
|
266
|
+
])!;
|
|
267
|
+
expect(isDeterministicSchemaFailureMsg(msg)).toBe(false);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("有有效 parsedOutput → 无归因无标记(不适用矩阵)", () => {
|
|
271
|
+
expect(
|
|
272
|
+
describeMissingParsedOutput([{ toolName: "structured-output", result: { details: { ok: 1 } } }]),
|
|
273
|
+
).toBeUndefined();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// 验收④:标记词不命中 STALE_CONTEXT_PATTERNS——若命中,isStaleContextErrorMsg
|
|
277
|
+
// 分诊(判定在前)会抢先归因 stale-context,虽然同样不重试,但归因语义被污染
|
|
278
|
+
// (TUI/日志把 schema 失败误报为 stale)。
|
|
279
|
+
it("标记词与全部 STALE_CONTEXT_PATTERNS 零交集,isStaleContextErrorMsg 不抢先", () => {
|
|
280
|
+
const lower = DETERMINISTIC_SCHEMA_FAILURE_PREFIX.toLowerCase();
|
|
281
|
+
for (const pattern of STALE_CONTEXT_PATTERNS) {
|
|
282
|
+
expect(lower.includes(pattern)).toBe(false);
|
|
283
|
+
}
|
|
284
|
+
expect(isStaleContextErrorMsg(DETERMINISTIC_SCHEMA_FAILURE_PREFIX)).toBe(false);
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// ============================================================
|
|
289
|
+
// collectResult — F-1 集成行为(schemaExpected → 失败标注)
|
|
290
|
+
// ============================================================
|
|
291
|
+
|
|
292
|
+
describe("collectResult — F-1 schemaExpected 失败标注", () => {
|
|
293
|
+
/** 最小 ExecutionRecord stub(collectResult 只读 turns/toolCalls 派生面)。 */
|
|
294
|
+
function makeRecordWithCalls(calls: ToolCall[]): ExecutionRecord {
|
|
295
|
+
return {
|
|
296
|
+
id: "rec-1",
|
|
297
|
+
turns: [{ toolCalls: calls, text: "done", turnCount: 1, closed: true }],
|
|
298
|
+
turnCount: 1,
|
|
299
|
+
} as unknown as ExecutionRecord;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const baseArgs = {
|
|
303
|
+
startTime: Date.now(),
|
|
304
|
+
sessionId: "s-1",
|
|
305
|
+
sessionFile: undefined,
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
it("schemaExpected + failed SO call (details:{}) + success=true → success=false + error attributed", () => {
|
|
309
|
+
const record = makeRecordWithCalls([
|
|
310
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text: "validation boom" }] } },
|
|
311
|
+
]);
|
|
312
|
+
const result = collectResult(record, { ...baseArgs, success: true, error: undefined, schemaExpected: true });
|
|
313
|
+
expect(result.success).toBe(false);
|
|
314
|
+
expect(result.error).toContain("validation boom");
|
|
315
|
+
expect(result.parsedOutput).toBeUndefined();
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("schemaExpected + never called SO + success=true → error hints extension check", () => {
|
|
319
|
+
const record = makeRecordWithCalls([{ toolName: "bash", result: { content: [] } }]);
|
|
320
|
+
const result = collectResult(record, { ...baseArgs, success: true, error: undefined, schemaExpected: true });
|
|
321
|
+
expect(result.success).toBe(false);
|
|
322
|
+
expect(result.error).toContain("structured-output extension is installed");
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it("schemaExpected + valid details → success preserved (S3 e2e semantics no regression)", () => {
|
|
326
|
+
const record = makeRecordWithCalls([
|
|
327
|
+
{ toolName: "structured-output", result: { details: { answer: 42 } } },
|
|
328
|
+
]);
|
|
329
|
+
const result = collectResult(record, { ...baseArgs, success: true, error: undefined, schemaExpected: true });
|
|
330
|
+
expect(result.success).toBe(true);
|
|
331
|
+
expect(result.error).toBeUndefined();
|
|
332
|
+
expect(result.parsedOutput).toEqual({ answer: 42 });
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("no schemaExpected (plain mode) + no SO call → success unchanged", () => {
|
|
336
|
+
const record = makeRecordWithCalls([{ toolName: "bash", result: { content: [] } }]);
|
|
337
|
+
const result = collectResult(record, { ...baseArgs, success: true, error: undefined });
|
|
338
|
+
expect(result.success).toBe(true);
|
|
339
|
+
expect(result.parsedOutput).toBeUndefined();
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("success=false path: existing error not overwritten by F-1 attribution", () => {
|
|
343
|
+
const record = makeRecordWithCalls([]);
|
|
344
|
+
const result = collectResult(record, { ...baseArgs, success: false, error: "provider boom", schemaExpected: true });
|
|
345
|
+
expect(result.success).toBe(false);
|
|
346
|
+
expect(result.error).toBe("provider boom");
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("[MF-1] F-1 标注的 error 携带确定性标记(流到 executeAgentCall 即不可重试)", () => {
|
|
350
|
+
const record = makeRecordWithCalls([
|
|
351
|
+
{ toolName: "structured-output", isError: true, result: { details: {}, content: [{ type: "text", text: "Schema validation failed: /target is required" }] } },
|
|
352
|
+
]);
|
|
353
|
+
const result = collectResult(record, { ...baseArgs, success: true, error: undefined, schemaExpected: true });
|
|
354
|
+
expect(result.success).toBe(false);
|
|
355
|
+
expect(result.error).toBeDefined();
|
|
356
|
+
expect(isDeterministicSchemaFailureMsg(result.error)).toBe(true);
|
|
357
|
+
});
|
|
358
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// src/__tests__/path-encoding.test.ts
|
|
2
|
+
//
|
|
3
|
+
// 锁定 encodeCwd 契约:session-runner 与 session-file-gc 共用此编码,
|
|
4
|
+
// 漂移会导致同一 cwd 落到两个不同目录(见 path-encoding.ts 顶部注释)。
|
|
5
|
+
import * as path from "node:path";
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "vitest";
|
|
8
|
+
|
|
9
|
+
import { encodeCwd, getSubagentRecordsDir, getSubagentSessionDir } from "../path-encoding.ts";
|
|
10
|
+
|
|
11
|
+
describe("encodeCwd", () => {
|
|
12
|
+
it("encodes a normal unix absolute path", () => {
|
|
13
|
+
expect(encodeCwd("/Users/x/proj")).toBe("--Users-x-proj--");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("strips a single leading backslash", () => {
|
|
17
|
+
expect(encodeCwd("\\foo")).toBe("--foo--");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("encodes Windows drive letter (colon + backslash)", () => {
|
|
21
|
+
// C:\proj → 去掉无前导分隔符 → C:\proj → : 和 \ 都替换为 - → C--proj
|
|
22
|
+
expect(encodeCwd("C:\\proj")).toBe("--C--proj--");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("encodes empty string to bare delimiter pair", () => {
|
|
26
|
+
expect(encodeCwd("")).toBe("----");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("encodes relative path with no leading separator", () => {
|
|
30
|
+
expect(encodeCwd("relative/path")).toBe("--relative-path--");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("collapses consecutive separators (forward slash)", () => {
|
|
34
|
+
expect(encodeCwd("/a//b")).toBe("--a--b--");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("collapses consecutive separators (backslash)", () => {
|
|
38
|
+
expect(encodeCwd("a\\b\\c")).toBe("--a-b-c--");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("encodes mixed separators and colon", () => {
|
|
42
|
+
// /x:y\\z → 去前导 / → x:y\z → : \ → - - → x-y-z
|
|
43
|
+
expect(encodeCwd("/x:y\\z")).toBe("--x-y-z--");
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("getSubagentSessionDir", () => {
|
|
48
|
+
it("returns agentDir/subagents/<encodedCwd>/sessions", () => {
|
|
49
|
+
// [MF#1] 既有布局:subagents/<enc>/sessions/(不改,避免升级用户既有数据 orphan)
|
|
50
|
+
const result = getSubagentSessionDir("/home/user/.pi/agent", "/home/user/project");
|
|
51
|
+
expect(result).toBe(
|
|
52
|
+
path.join("/home/user/.pi/agent", "subagents", "--home-user-project--", "sessions")
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("uses mainCwd encoding (not effectiveCwd)", () => {
|
|
57
|
+
// D-004: 用主 cwd 编码,保证同一主 cwd 下所有 subagent 存同一目录
|
|
58
|
+
const mainCwd = "/Users/zhushanwen/Code/my-project";
|
|
59
|
+
const result = getSubagentSessionDir("~/.pi/agent", mainCwd);
|
|
60
|
+
const encoded = encodeCwd(mainCwd);
|
|
61
|
+
expect(result).toBe(path.join("~/.pi/agent", "subagents", encoded, "sessions"));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("produces consistent path for same inputs", () => {
|
|
65
|
+
const a = getSubagentSessionDir("/agent", "/cwd");
|
|
66
|
+
const b = getSubagentSessionDir("/agent", "/cwd");
|
|
67
|
+
expect(a).toBe(b);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("produces different path for different mainCwd", () => {
|
|
71
|
+
const a = getSubagentSessionDir("/agent", "/cwd1");
|
|
72
|
+
const b = getSubagentSessionDir("/agent", "/cwd2");
|
|
73
|
+
expect(a).not.toBe(b);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("getSubagentRecordsDir", () => {
|
|
78
|
+
it("returns agentDir/subagents/<encodedCwd>/records", () => {
|
|
79
|
+
const result = getSubagentRecordsDir("/home/user/.pi/agent", "/Users/x/proj");
|
|
80
|
+
expect(result).toBe(
|
|
81
|
+
path.join("/home/user/.pi/agent", "subagents", "--Users-x-proj--", "records")
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("shares the same <enc> segment with getSubagentSessionDir for same cwd", () => {
|
|
86
|
+
// F1:records 与 sessions 在同一 <enc> 段下,仅尾段不同(records vs sessions)。
|
|
87
|
+
// worktree 场景三者恒等的前提就靠这个共享 enc。
|
|
88
|
+
const agentDir = "/home/user/.pi/agent";
|
|
89
|
+
const cwd = "/Users/x/proj";
|
|
90
|
+
const sessions = getSubagentSessionDir(agentDir, cwd);
|
|
91
|
+
const records = getSubagentRecordsDir(agentDir, cwd);
|
|
92
|
+
// 共享 enc:去掉尾段后剩同一路径前缀
|
|
93
|
+
expect(path.dirname(sessions)).toBe(path.dirname(records));
|
|
94
|
+
// 尾段不同
|
|
95
|
+
expect(path.basename(sessions)).toBe("sessions");
|
|
96
|
+
expect(path.basename(records)).toBe("records");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("produces different path for different mainCwd", () => {
|
|
100
|
+
const a = getSubagentRecordsDir("/agent", "/cwd1");
|
|
101
|
+
const b = getSubagentRecordsDir("/agent", "/cwd2");
|
|
102
|
+
expect(a).not.toBe(b);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// src/__tests__/pi-invocation.test.ts
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as os from "node:os";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
|
|
6
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
7
|
+
|
|
8
|
+
import { getPiInvocation } from "../pi-invocation.ts";
|
|
9
|
+
import { RELAY_ENV_NODE, RELAY_ENV_SCRIPT, RELAY_ENV_SOCKET } from "../relay-env.ts";
|
|
10
|
+
|
|
11
|
+
describe("getPiInvocation", () => {
|
|
12
|
+
const originalArgv = process.argv;
|
|
13
|
+
const originalExecPath = process.execPath;
|
|
14
|
+
let tmpScript: string;
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
Object.defineProperty(process, "argv", { value: originalArgv, configurable: true });
|
|
18
|
+
Object.defineProperty(process, "execPath", { value: originalExecPath, configurable: true });
|
|
19
|
+
if (tmpScript && fs.existsSync(tmpScript)) fs.unlinkSync(tmpScript);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("真实脚本路径存在 → node <script> <userArgs>", () => {
|
|
23
|
+
// 创建真实临时脚本文件(避免 ESM spy 限制)
|
|
24
|
+
tmpScript = path.join(os.tmpdir(), `pi-inv-test-${Date.now()}.mjs`);
|
|
25
|
+
fs.writeFileSync(tmpScript, "// test");
|
|
26
|
+
Object.defineProperty(process, "argv", { value: ["node", tmpScript], configurable: true });
|
|
27
|
+
Object.defineProperty(process, "execPath", { value: "/usr/bin/node", configurable: true });
|
|
28
|
+
|
|
29
|
+
const result = getPiInvocation(["--mode", "json", "Task: x"]);
|
|
30
|
+
expect(result.command).toBe("/usr/bin/node");
|
|
31
|
+
expect(result.args).toEqual([tmpScript, "--mode", "json", "Task: x"]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("bun 虚拟脚本(/$bunfs/root/)→ 退化到 pi-in-PATH", () => {
|
|
35
|
+
tmpScript = "";
|
|
36
|
+
const virtualScript = "/$bunfs/root/pi";
|
|
37
|
+
Object.defineProperty(process, "argv", { value: ["bun", virtualScript], configurable: true });
|
|
38
|
+
Object.defineProperty(process, "execPath", { value: "/usr/bin/bun", configurable: true });
|
|
39
|
+
|
|
40
|
+
const result = getPiInvocation(["--mode", "json"]);
|
|
41
|
+
expect(result.command).toBe("pi");
|
|
42
|
+
expect(result.args).toEqual(["--mode", "json"]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("非通用 runtime(pi standalone binary)→ 直接 execPath", () => {
|
|
46
|
+
tmpScript = "";
|
|
47
|
+
Object.defineProperty(process, "argv", { value: ["/usr/bin/pi", "/nonexistent"], configurable: true });
|
|
48
|
+
Object.defineProperty(process, "execPath", { value: "/usr/local/bin/pi-binary", configurable: true });
|
|
49
|
+
|
|
50
|
+
const result = getPiInvocation(["--mode", "json"]);
|
|
51
|
+
expect(result.command).toBe("/usr/local/bin/pi-binary");
|
|
52
|
+
expect(result.args).toEqual(["--mode", "json"]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("node 通用 runtime + 脚本不存在 → pi-in-PATH", () => {
|
|
56
|
+
tmpScript = "";
|
|
57
|
+
Object.defineProperty(process, "argv", { value: ["node", "/nonexistent"], configurable: true });
|
|
58
|
+
Object.defineProperty(process, "execPath", { value: "/usr/bin/node", configurable: true });
|
|
59
|
+
|
|
60
|
+
const result = getPiInvocation(["--mode", "json"]);
|
|
61
|
+
expect(result.command).toBe("pi");
|
|
62
|
+
expect(result.args).toEqual(["--mode", "json"]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("空 userArgs 合法(仅 command + 空 args)", () => {
|
|
66
|
+
tmpScript = "";
|
|
67
|
+
Object.defineProperty(process, "argv", { value: ["node"], configurable: true });
|
|
68
|
+
Object.defineProperty(process, "execPath", { value: "/usr/bin/node", configurable: true });
|
|
69
|
+
|
|
70
|
+
const result = getPiInvocation([]);
|
|
71
|
+
expect(result.command).toBe("pi");
|
|
72
|
+
expect(result.args).toEqual([]);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("getPiInvocation relay 分支(E 方案 §5.1)", () => {
|
|
77
|
+
const originalArgv = process.argv;
|
|
78
|
+
const originalExecPath = process.execPath;
|
|
79
|
+
const RELAY_ENVS = [RELAY_ENV_SOCKET, RELAY_ENV_NODE, RELAY_ENV_SCRIPT] as const;
|
|
80
|
+
/** 三 env 齐备的基准值(改 argv/execPath 隔离直连分支,只测 relay 判定本身)。 */
|
|
81
|
+
const RELAY_FULL: Record<string, string> = {
|
|
82
|
+
[RELAY_ENV_SOCKET]: "/tmp/relay-test.sock",
|
|
83
|
+
[RELAY_ENV_NODE]: "/usr/bin/relay-node",
|
|
84
|
+
[RELAY_ENV_SCRIPT]: "/opt/relay/relay.mjs",
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
afterEach(() => {
|
|
88
|
+
Object.defineProperty(process, "argv", { value: originalArgv, configurable: true });
|
|
89
|
+
Object.defineProperty(process, "execPath", { value: originalExecPath, configurable: true });
|
|
90
|
+
for (const key of RELAY_ENVS) delete process.env[key];
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
/** 形态 1:三 env 齐备 + 默认 opts → relay invocation(command=NODE、args[0]=SCRIPT)。 */
|
|
94
|
+
it("三 env 齐备(无 opts)→ 代理形态 <NODE> <SCRIPT> <userArgs>", () => {
|
|
95
|
+
Object.assign(process.env, RELAY_FULL);
|
|
96
|
+
const result = getPiInvocation(["--mode", "rpc"]);
|
|
97
|
+
expect(result.command).toBe(RELAY_FULL[RELAY_ENV_NODE]);
|
|
98
|
+
expect(result.args).toEqual([RELAY_FULL[RELAY_ENV_SCRIPT], "--mode", "rpc"]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
/** 形态 1 变体:opts.relay === true 显式开启,行为与缺省一致。 */
|
|
102
|
+
it("三 env 齐备 + opts.relay=true → 同缺省(显式开启非必填)", () => {
|
|
103
|
+
Object.assign(process.env, RELAY_FULL);
|
|
104
|
+
const result = getPiInvocation(["--mode", "rpc"], { relay: true });
|
|
105
|
+
expect(result.command).toBe(RELAY_FULL[RELAY_ENV_NODE]);
|
|
106
|
+
expect(result.args[0]).toBe(RELAY_FULL[RELAY_ENV_SCRIPT]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
/** 形态 2:三缺一逐个枚举 → 回落现状直连(全有或全无,E-TUI 零回归)。 */
|
|
110
|
+
it("三缺一(逐个枚举)→ 现状直连分支", () => {
|
|
111
|
+
// 直连分支落到「node + 不存在脚本 → pi-in-PATH」可稳定断言(不依赖测试机 execPath 形态)
|
|
112
|
+
Object.defineProperty(process, "argv", { value: ["node", "/nonexistent"], configurable: true });
|
|
113
|
+
Object.defineProperty(process, "execPath", { value: "/usr/bin/node", configurable: true });
|
|
114
|
+
for (const missing of RELAY_ENVS) {
|
|
115
|
+
const partial = { ...RELAY_FULL };
|
|
116
|
+
delete partial[missing];
|
|
117
|
+
for (const key of RELAY_ENVS) delete process.env[key];
|
|
118
|
+
Object.assign(process.env, partial);
|
|
119
|
+
const result = getPiInvocation(["--mode", "rpc"]);
|
|
120
|
+
expect(result.command).toBe("pi"); // 分支 3:通用 runtime + 脚本不存在 → PATH
|
|
121
|
+
expect(result.args).toEqual(["--mode", "rpc"]);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
/** 形态 3:probe 排除——显式 relay:false 时即便三 env 齐备也直连。 */
|
|
126
|
+
it("relay:false 强制直连(probe 语义:探 pi 本体,不探 runtime 健康)", () => {
|
|
127
|
+
Object.assign(process.env, RELAY_FULL);
|
|
128
|
+
Object.defineProperty(process, "argv", { value: ["node", "/nonexistent"], configurable: true });
|
|
129
|
+
Object.defineProperty(process, "execPath", { value: "/usr/bin/node", configurable: true });
|
|
130
|
+
const result = getPiInvocation(["--version"], { relay: false });
|
|
131
|
+
expect(result.command).toBe("pi");
|
|
132
|
+
expect(result.args).toEqual(["--version"]);
|
|
133
|
+
});
|
|
134
|
+
});
|