@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,273 @@
|
|
|
1
|
+
// src/__tests__/recursive-visibility-env.test.ts
|
|
2
|
+
//
|
|
3
|
+
// 递归 subagent 跨层可见性:env 身份贯穿验证(设计 docs/design/recursive-subagent-visibility.md 场景 1b)。
|
|
4
|
+
//
|
|
5
|
+
// 验证 runSpawn 构造的 childEnv 含 4 个 PI_SUBAGENT_* 身份 env,值 = ctx.sessionRootId /
|
|
6
|
+
// record.id / String(record.depth) / ctx.rootCwd([MF-3] 第 4 个:ROOT cwd,落盘目录编码键)。覆盖 opts.fork=true 与 opts.fork=false 两种(决策 2 无条件注入)。
|
|
7
|
+
//
|
|
8
|
+
// 这是场景 1(端到端三层嵌套全树可见)的「env 传递机制」确定性验证——不依赖 LLM 配合,
|
|
9
|
+
// mock spawn 拦截 childEnv 直接断言。端到端可见性由场景 1(真实 pi CLI + recursive-worker agent)覆盖。
|
|
10
|
+
|
|
11
|
+
import type { PassThrough } from "node:stream";
|
|
12
|
+
|
|
13
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
14
|
+
|
|
15
|
+
import { getSubagentSessionDir } from "../path-encoding.ts";
|
|
16
|
+
|
|
17
|
+
// ── mock modules(同 session-runner-schema-env.test.ts 模式)──
|
|
18
|
+
|
|
19
|
+
vi.mock("node:child_process", async () => {
|
|
20
|
+
const { EventEmitter } = await import("node:events");
|
|
21
|
+
const { PassThrough } = await import("node:stream");
|
|
22
|
+
|
|
23
|
+
class FakeChild extends EventEmitter {
|
|
24
|
+
pid = 12345;
|
|
25
|
+
stdout = new PassThrough();
|
|
26
|
+
stdin = new PassThrough(); // runSpawn 注册 stdin EPIPE handler(session-runner),FakeChild 必须提供
|
|
27
|
+
stderr = new PassThrough();
|
|
28
|
+
killed = false;
|
|
29
|
+
killSignal: string | undefined;
|
|
30
|
+
kill(sig?: string): boolean {
|
|
31
|
+
this.killed = true;
|
|
32
|
+
this.killSignal = sig;
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
spawn: vi.fn(() => new FakeChild()),
|
|
39
|
+
// buildEnvBlock 用 execFile 异步取 git branch:默认 err-first 兜底(catch → branch=""),
|
|
40
|
+
// 形态同 worktree-manager.test.ts 的 setupExecFile
|
|
41
|
+
execFile: vi.fn(
|
|
42
|
+
(
|
|
43
|
+
_cmd: string,
|
|
44
|
+
_args: readonly string[],
|
|
45
|
+
_opts: unknown,
|
|
46
|
+
cb: (err: Error | null, stdout?: string, stderr?: string) => void,
|
|
47
|
+
) => cb(new Error("execFile not configured in this test")),
|
|
48
|
+
),
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
vi.mock("node:fs", async () => {
|
|
53
|
+
const actual = await import("node:fs");
|
|
54
|
+
return {
|
|
55
|
+
default: {
|
|
56
|
+
...actual,
|
|
57
|
+
mkdirSync: vi.fn(),
|
|
58
|
+
existsSync: vi.fn(() => false),
|
|
59
|
+
appendFileSync: vi.fn(),
|
|
60
|
+
writeFileSync: vi.fn(),
|
|
61
|
+
readdirSync: vi.fn(() => []),
|
|
62
|
+
},
|
|
63
|
+
mkdirSync: vi.fn(),
|
|
64
|
+
existsSync: vi.fn(() => false),
|
|
65
|
+
appendFileSync: vi.fn(),
|
|
66
|
+
writeFileSync: vi.fn(),
|
|
67
|
+
readdirSync: vi.fn(() => []),
|
|
68
|
+
promises: actual.promises,
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
vi.mock("../alive-store.ts", () => ({
|
|
73
|
+
writeAliveMarker: vi.fn(),
|
|
74
|
+
}));
|
|
75
|
+
|
|
76
|
+
vi.mock("../temp-prompt.ts", () => ({
|
|
77
|
+
writePromptToTempFile: vi.fn(async (agent: string) => {
|
|
78
|
+
const safeName = agent.replace(/[^\w.-]+/g, "_");
|
|
79
|
+
return { dir: `/tmp/fake-${safeName}`, filePath: `/tmp/fake-${safeName}/prompt-${safeName}.md` };
|
|
80
|
+
}),
|
|
81
|
+
cleanupTempPrompt: vi.fn(async () => {}),
|
|
82
|
+
}));
|
|
83
|
+
|
|
84
|
+
import { spawn } from "node:child_process";
|
|
85
|
+
import * as fs from "node:fs";
|
|
86
|
+
|
|
87
|
+
import { waitForSpawn } from "./helpers/spawn-mock.ts";
|
|
88
|
+
import { createRecord } from "../execution-record.ts";
|
|
89
|
+
import { type RunOptions, runSpawn, type SessionRunnerContext } from "../session-runner.ts";
|
|
90
|
+
|
|
91
|
+
const mockSpawn = vi.mocked(spawn);
|
|
92
|
+
const mockExistsSync = vi.mocked(fs.existsSync);
|
|
93
|
+
|
|
94
|
+
interface FakeChild {
|
|
95
|
+
pid: number;
|
|
96
|
+
stdout: PassThrough;
|
|
97
|
+
stderr: PassThrough;
|
|
98
|
+
killed: boolean;
|
|
99
|
+
killSignal: string | undefined;
|
|
100
|
+
kill(sig?: string): boolean;
|
|
101
|
+
emit(event: string, ...args: unknown[]): boolean;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function getLastSpawnedChild(): FakeChild {
|
|
105
|
+
const result = mockSpawn.mock.results.at(-1);
|
|
106
|
+
if (!result) throw new Error("spawn was not called yet");
|
|
107
|
+
return result.value as FakeChild;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function getLastSpawnEnv(): Record<string, string | undefined> {
|
|
111
|
+
return (mockSpawn.mock.calls.at(-1)?.[2]?.env as Record<string, string | undefined>) ?? {};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ── fixture ──
|
|
115
|
+
|
|
116
|
+
function makeRecord(overrides: { id?: string; depth?: number } = {}) {
|
|
117
|
+
return createRecord(overrides.id ?? "sa-test-record", {
|
|
118
|
+
agent: "general-purpose",
|
|
119
|
+
model: "test/model",
|
|
120
|
+
mode: "background",
|
|
121
|
+
task: "test task",
|
|
122
|
+
startedAt: Date.now(),
|
|
123
|
+
rootSessionId: "should-be-overridden-by-sessionRootId-source",
|
|
124
|
+
parentRecordId: undefined,
|
|
125
|
+
depth: overrides.depth ?? 0,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function makeRunOpts(overrides: Partial<RunOptions> = {}): RunOptions {
|
|
130
|
+
return {
|
|
131
|
+
resolved: { model: { provider: "test", id: "model" }, thinkingLevel: undefined },
|
|
132
|
+
agentConfig: undefined,
|
|
133
|
+
appendSystemPrompt: undefined,
|
|
134
|
+
skillPath: undefined,
|
|
135
|
+
schema: undefined,
|
|
136
|
+
maxTurns: undefined,
|
|
137
|
+
graceTurns: undefined,
|
|
138
|
+
signal: undefined,
|
|
139
|
+
onEvent: undefined,
|
|
140
|
+
...overrides,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function makeCtx(overrides: Partial<SessionRunnerContext> = {}): SessionRunnerContext {
|
|
145
|
+
return {
|
|
146
|
+
cwd: "/fake/cwd",
|
|
147
|
+
agentDir: "/fake/agent",
|
|
148
|
+
skillDirs: [],
|
|
149
|
+
mainCwd: "/fake/cwd",
|
|
150
|
+
sessionRootId: "root-main-session",
|
|
151
|
+
rootCwd: "/fake/cwd",
|
|
152
|
+
...overrides,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ── runSpawn childEnv 身份 env 注入(场景 1b)──
|
|
157
|
+
|
|
158
|
+
describe("runSpawn 跨进程身份 env 注入(递归可见性场景 1b)", () => {
|
|
159
|
+
beforeEach(() => {
|
|
160
|
+
vi.clearAllMocks();
|
|
161
|
+
mockExistsSync.mockReturnValue(false);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
afterEach(() => {
|
|
165
|
+
vi.restoreAllMocks();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("非 fork(fork=false/undefined):无条件注入 4 个身份 env(决策 2)", async () => {
|
|
169
|
+
const record = makeRecord({ id: "sa-aaa", depth: 0 });
|
|
170
|
+
const ctx = makeCtx({ sessionRootId: "root-main" });
|
|
171
|
+
const opts = makeRunOpts({ fork: false });
|
|
172
|
+
|
|
173
|
+
const resultPromise = runSpawn(record, "test task", opts, ctx);
|
|
174
|
+
await waitForSpawn(mockSpawn);
|
|
175
|
+
const childEnv = getLastSpawnEnv();
|
|
176
|
+
|
|
177
|
+
expect(childEnv.PI_SUBAGENT_ROOT_SESSION_ID).toBe("root-main");
|
|
178
|
+
expect(childEnv.PI_SUBAGENT_SELF_RECORD_ID).toBe("sa-aaa");
|
|
179
|
+
expect(childEnv.PI_SUBAGENT_DEPTH).toBe("0");
|
|
180
|
+
// [MF-3] 第 4 个贯穿 env:ROOT cwd(子进程落盘目录编码键)
|
|
181
|
+
expect(childEnv.PI_SUBAGENT_ROOT_CWD).toBe("/fake/cwd");
|
|
182
|
+
// fork=false 不注入 fork depth env(与既有行为一致,本测试不改变它)
|
|
183
|
+
expect(childEnv.PI_SUBAGENT_FORK_DEPTH).toBeUndefined();
|
|
184
|
+
|
|
185
|
+
const child = getLastSpawnedChild();
|
|
186
|
+
child.emit("close", 0);
|
|
187
|
+
await resultPromise;
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("fork=true:4 个身份 env 与 fork depth env 共存(决策 2 无条件注入不依赖 fork)", async () => {
|
|
191
|
+
const record = makeRecord({ id: "sa-bbb", depth: 2 });
|
|
192
|
+
const ctx = makeCtx({ sessionRootId: "root-main" });
|
|
193
|
+
const opts = makeRunOpts({ fork: true, parentForkDepth: 1 });
|
|
194
|
+
|
|
195
|
+
const resultPromise = runSpawn(record, "test task", opts, ctx);
|
|
196
|
+
await waitForSpawn(mockSpawn);
|
|
197
|
+
const childEnv = getLastSpawnEnv();
|
|
198
|
+
|
|
199
|
+
// 身份 env 无条件存在(决策 2)
|
|
200
|
+
expect(childEnv.PI_SUBAGENT_ROOT_SESSION_ID).toBe("root-main");
|
|
201
|
+
expect(childEnv.PI_SUBAGENT_SELF_RECORD_ID).toBe("sa-bbb");
|
|
202
|
+
expect(childEnv.PI_SUBAGENT_DEPTH).toBe("2");
|
|
203
|
+
expect(childEnv.PI_SUBAGENT_ROOT_CWD).toBe("/fake/cwd");
|
|
204
|
+
// fork depth env 同时存在(fork=true + parentForkDepth=1 → 2)
|
|
205
|
+
expect(childEnv.PI_SUBAGENT_FORK_DEPTH).toBe("2");
|
|
206
|
+
|
|
207
|
+
const child = getLastSpawnedChild();
|
|
208
|
+
child.emit("close", 0);
|
|
209
|
+
await resultPromise;
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("深层 record(depth=3):DEPTH env = String(record.depth),正确贯穿嵌套层级", async () => {
|
|
213
|
+
const record = makeRecord({ id: "sa-deep", depth: 3 });
|
|
214
|
+
const ctx = makeCtx({ sessionRootId: "root-topmost" });
|
|
215
|
+
|
|
216
|
+
const resultPromise = runSpawn(record, "test task", makeRunOpts(), ctx);
|
|
217
|
+
await waitForSpawn(mockSpawn);
|
|
218
|
+
const childEnv = getLastSpawnEnv();
|
|
219
|
+
|
|
220
|
+
expect(childEnv.PI_SUBAGENT_DEPTH).toBe("3");
|
|
221
|
+
expect(childEnv.PI_SUBAGENT_SELF_RECORD_ID).toBe("sa-deep");
|
|
222
|
+
expect(childEnv.PI_SUBAGENT_ROOT_SESSION_ID).toBe("root-topmost");
|
|
223
|
+
|
|
224
|
+
const child = getLastSpawnedChild();
|
|
225
|
+
child.emit("close", 0);
|
|
226
|
+
await resultPromise;
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("ROOT_SESSION_ID 恒等于 ctx.sessionRootId(贯穿真 ROOT,非 record.rootSessionId)", async () => {
|
|
230
|
+
// record.rootSessionId 是 createRecord 时写入的值(可能来自旧逻辑),但 env 注入用的是
|
|
231
|
+
// ctx.sessionRootId(经 buildSessionRunnerContext 从 this.sessionRootId 透传,贯穿真 ROOT)。
|
|
232
|
+
// 这保证深层 subagent 的子进程仍归顶层 ROOT(设计决策 1/3)。
|
|
233
|
+
const record = makeRecord({ id: "sa-ccc" }); // record.rootSessionId = fixture 默认值
|
|
234
|
+
const ctx = makeCtx({ sessionRootId: "real-root-session" });
|
|
235
|
+
|
|
236
|
+
const resultPromise = runSpawn(record, "test task", makeRunOpts(), ctx);
|
|
237
|
+
await waitForSpawn(mockSpawn);
|
|
238
|
+
const childEnv = getLastSpawnEnv();
|
|
239
|
+
|
|
240
|
+
// env 用 ctx.sessionRootId,不是 record.rootSessionId
|
|
241
|
+
expect(childEnv.PI_SUBAGENT_ROOT_SESSION_ID).toBe("real-root-session");
|
|
242
|
+
expect(childEnv.PI_SUBAGENT_ROOT_SESSION_ID).not.toBe(record.rootSessionId);
|
|
243
|
+
|
|
244
|
+
const child = getLastSpawnedChild();
|
|
245
|
+
child.emit("close", 0);
|
|
246
|
+
await resultPromise;
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("[MF-3 回归] worktree 模式(mainCwd=checkout ≠ rootCwd):sessionDir 用 ROOT cwd 编码,深层 record 落盘到 ROOT 可扫描段", async () => {
|
|
250
|
+
// 模拟 B(worktree 子进程)spawn C:ctx.cwd/mainCwd = checkout 路径,rootCwd = 真 ROOT cwd。
|
|
251
|
+
// 旧实现 sessionDir 用 ctx.mainCwd 编码 → enc(checkout) 段,ROOT 磁盘重建扫不到(MF-3)。
|
|
252
|
+
const rootCwd = "/root/project";
|
|
253
|
+
const checkoutPath = "/var/folders/worktree/pi-subagents/--root-project--/branch";
|
|
254
|
+
const agentDir = "/fake/agent";
|
|
255
|
+
const record = makeRecord({ id: "sa-deep", depth: 2 });
|
|
256
|
+
const ctx = makeCtx({ cwd: checkoutPath, mainCwd: checkoutPath, rootCwd, agentDir });
|
|
257
|
+
|
|
258
|
+
const resultPromise = runSpawn(record, "test task", makeRunOpts(), ctx);
|
|
259
|
+
await waitForSpawn(mockSpawn);
|
|
260
|
+
const childEnv = getLastSpawnEnv();
|
|
261
|
+
const spawnArgs = mockSpawn.mock.calls.at(-1)?.[1] as string[];
|
|
262
|
+
|
|
263
|
+
// 第 4 个 env 贯穿 ROOT cwd
|
|
264
|
+
expect(childEnv.PI_SUBAGENT_ROOT_CWD).toBe(rootCwd);
|
|
265
|
+
// spawn --session-dir 指向 enc(ROOT cwd)(非 enc(checkout))
|
|
266
|
+
expect(spawnArgs).toContain(getSubagentSessionDir(agentDir, rootCwd));
|
|
267
|
+
expect(spawnArgs).not.toContain(getSubagentSessionDir(agentDir, checkoutPath));
|
|
268
|
+
|
|
269
|
+
const child = getLastSpawnedChild();
|
|
270
|
+
child.emit("close", 0);
|
|
271
|
+
await resultPromise;
|
|
272
|
+
});
|
|
273
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
RELAY_ENV_NODE,
|
|
5
|
+
RELAY_ENV_RECORD_ID,
|
|
6
|
+
RELAY_ENV_SCRIPT,
|
|
7
|
+
RELAY_ENV_SESSION_ID,
|
|
8
|
+
RELAY_ENV_SOCKET,
|
|
9
|
+
RELAY_EXIT_CODES,
|
|
10
|
+
RELAY_PROTOCOL_VERSION,
|
|
11
|
+
isRelayActive,
|
|
12
|
+
} from '../relay-env.js';
|
|
13
|
+
|
|
14
|
+
describe('relay env SSOT', () => {
|
|
15
|
+
it('激活判定:三 env 同时非空', () => {
|
|
16
|
+
expect(isRelayActive({ [RELAY_ENV_SOCKET]: '/x.sock', [RELAY_ENV_NODE]: '/node', [RELAY_ENV_SCRIPT]: '/relay.mjs' })).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('任一缺失即不激活(全有或全无)', () => {
|
|
20
|
+
const full = {
|
|
21
|
+
[RELAY_ENV_SOCKET]: '/x.sock',
|
|
22
|
+
[RELAY_ENV_NODE]: '/node',
|
|
23
|
+
[RELAY_ENV_SCRIPT]: '/relay.mjs',
|
|
24
|
+
};
|
|
25
|
+
expect(isRelayActive({ ...full, [RELAY_ENV_SOCKET]: '' })).toBe(false);
|
|
26
|
+
expect(isRelayActive({ ...full, [RELAY_ENV_NODE]: undefined })).toBe(false);
|
|
27
|
+
expect(isRelayActive({})).toBe(false);
|
|
28
|
+
expect(isRelayActive(process.env)).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('env 名与退出码稳定(relay.mjs 镜像一致性由 conformance relay 断言锁定)', () => {
|
|
32
|
+
expect(RELAY_ENV_SESSION_ID).toBe('XYZ_SUBAGENT_RELAY_SESSION_ID');
|
|
33
|
+
expect(RELAY_ENV_RECORD_ID).toBe('XYZ_SUBAGENT_RELAY_RECORD_ID');
|
|
34
|
+
expect(RELAY_PROTOCOL_VERSION).toBe(1);
|
|
35
|
+
expect(RELAY_EXIT_CODES).toEqual({
|
|
36
|
+
VERSION_MISMATCH: 10,
|
|
37
|
+
SOCKET_UNREACHABLE: 11,
|
|
38
|
+
SOCKET_CLOSED: 12,
|
|
39
|
+
MISSING_IDENTITY: 13,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// resource-policy 单测 —— SP-6 资源策略配置化(idleTimeoutMs + env 覆盖)。
|
|
2
|
+
//
|
|
3
|
+
// 测试策略:
|
|
4
|
+
// - 验证 armIdleTimer 的三层优先级:参数 > env XYZ_SUBAGENT_IDLE_TIMEOUT_MS > 默认 300000ms。
|
|
5
|
+
// - 用 vi.useFakeTimers() 控制时间流逝。
|
|
6
|
+
// - 每个用例 beforeEach/afterEach 清理 env + reset lifecycle state。
|
|
7
|
+
|
|
8
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
DEFAULT_IDLE_TIMEOUT_MS,
|
|
12
|
+
_resetLifecycleState,
|
|
13
|
+
armIdleTimer,
|
|
14
|
+
hasIdleTimer,
|
|
15
|
+
} from "../lifecycle-manager.ts";
|
|
16
|
+
|
|
17
|
+
describe("resource-policy — SP-6 idleTimeoutMs 配置化", () => {
|
|
18
|
+
const ENV_KEY = "XYZ_SUBAGENT_IDLE_TIMEOUT_MS";
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
vi.useFakeTimers();
|
|
22
|
+
_resetLifecycleState();
|
|
23
|
+
// 清理 env,确保用例间隔离。
|
|
24
|
+
delete process.env[ENV_KEY];
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
_resetLifecycleState();
|
|
29
|
+
vi.useRealTimers();
|
|
30
|
+
delete process.env[ENV_KEY];
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// TC-1: idleTimeoutMs 参数透传生效
|
|
34
|
+
it("TC-1: explicit timeoutMs 参数直接生效(不看 env)", () => {
|
|
35
|
+
const onTimeout = vi.fn();
|
|
36
|
+
armIdleTimer("sa-tc1", onTimeout, 1234);
|
|
37
|
+
|
|
38
|
+
expect(hasIdleTimer("sa-tc1")).toBe(true);
|
|
39
|
+
|
|
40
|
+
// 差 1ms 不触发
|
|
41
|
+
vi.advanceTimersByTime(1233);
|
|
42
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
43
|
+
|
|
44
|
+
// 到点触发
|
|
45
|
+
vi.advanceTimersByTime(1);
|
|
46
|
+
expect(onTimeout).toHaveBeenCalledOnce();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// TC-2: env XYZ_SUBAGENT_IDLE_TIMEOUT_MS 覆盖默认值
|
|
50
|
+
it("TC-2: env XYZ_SUBAGENT_IDLE_TIMEOUT_MS 覆盖默认值(不传参数时)", () => {
|
|
51
|
+
process.env[ENV_KEY] = "5000"; // 5 秒
|
|
52
|
+
|
|
53
|
+
const onTimeout = vi.fn();
|
|
54
|
+
armIdleTimer("sa-tc2", onTimeout); // 不传 timeoutMs
|
|
55
|
+
|
|
56
|
+
// 4.9s 不触发
|
|
57
|
+
vi.advanceTimersByTime(4999);
|
|
58
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
59
|
+
|
|
60
|
+
// 5s 触发(env 值,不是默认 300s)
|
|
61
|
+
vi.advanceTimersByTime(1);
|
|
62
|
+
expect(onTimeout).toHaveBeenCalledOnce();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// TC-3: 参数优先于 env
|
|
66
|
+
it("TC-3: explicit timeoutMs 参数优先于 env", () => {
|
|
67
|
+
process.env[ENV_KEY] = "99999"; // env 设大值
|
|
68
|
+
|
|
69
|
+
const onTimeout = vi.fn();
|
|
70
|
+
armIdleTimer("sa-tc3", onTimeout, 2000); // 参数设小值
|
|
71
|
+
|
|
72
|
+
// 2s 触发(参数优先,不等 env 的 99s)
|
|
73
|
+
vi.advanceTimersByTime(1999);
|
|
74
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
75
|
+
|
|
76
|
+
vi.advanceTimersByTime(1);
|
|
77
|
+
expect(onTimeout).toHaveBeenCalledOnce();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// TC-4: 都不设时默认 5min (300000ms)
|
|
81
|
+
it("TC-4: 无参数无 env 时默认 300000ms (5min)", () => {
|
|
82
|
+
const onTimeout = vi.fn();
|
|
83
|
+
armIdleTimer("sa-tc4", onTimeout); // 不传 timeoutMs,env 也未设
|
|
84
|
+
|
|
85
|
+
// 差 1ms 不触发
|
|
86
|
+
vi.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT_MS - 1);
|
|
87
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
88
|
+
|
|
89
|
+
// 到点触发
|
|
90
|
+
vi.advanceTimersByTime(1);
|
|
91
|
+
expect(onTimeout).toHaveBeenCalledOnce();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// TC-5 (额外): env 非法值回落默认
|
|
95
|
+
it("TC-5: env 非法值(负数/非数字)回落默认 300000ms", () => {
|
|
96
|
+
process.env[ENV_KEY] = "not-a-number";
|
|
97
|
+
|
|
98
|
+
const onTimeout = vi.fn();
|
|
99
|
+
armIdleTimer("sa-tc5", onTimeout); // 不传 timeoutMs
|
|
100
|
+
|
|
101
|
+
// 非法 env → 回落默认 300s。299s 不触发。
|
|
102
|
+
vi.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT_MS - 1);
|
|
103
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
104
|
+
|
|
105
|
+
// 到点触发
|
|
106
|
+
vi.advanceTimersByTime(1);
|
|
107
|
+
expect(onTimeout).toHaveBeenCalledOnce();
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/__tests__/rpc-mode.test.ts
|
|
2
|
+
//
|
|
3
|
+
// RPC 模式相关功能
|
|
4
|
+
// 1. buildSpawnArgs 返回 --mode rpc(而非 json)
|
|
5
|
+
// 2. parseSpawnLine 正确解析 Pi 原生 RpcResponse(type:response + command + success + data|error)
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "vitest";
|
|
8
|
+
|
|
9
|
+
import { buildSpawnArgs } from "../session-runner.ts";
|
|
10
|
+
import { parseSpawnLine } from "../spawn-event-adapter.ts";
|
|
11
|
+
|
|
12
|
+
describe("buildSpawnArgs - RPC mode", () => {
|
|
13
|
+
const baseParams = {
|
|
14
|
+
modelRef: { provider: "openai", id: "gpt-4" },
|
|
15
|
+
thinkingLevel: undefined,
|
|
16
|
+
agentTools: undefined,
|
|
17
|
+
appendSystemPromptPath: undefined,
|
|
18
|
+
sessionDir: "/tmp/sessions",
|
|
19
|
+
forkSource: undefined,
|
|
20
|
+
skillPaths: undefined,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
it("生成 --mode rpc 参数(非 json)", () => {
|
|
24
|
+
const args = buildSpawnArgs(baseParams);
|
|
25
|
+
// 应包含 --mode rpc
|
|
26
|
+
expect(args).toContain("--mode");
|
|
27
|
+
const modeIdx = args.indexOf("--mode");
|
|
28
|
+
expect(args[modeIdx + 1]).toBe("rpc");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("不包含 --mode json", () => {
|
|
32
|
+
const args = buildSpawnArgs(baseParams);
|
|
33
|
+
const modeIdx = args.indexOf("--mode");
|
|
34
|
+
expect(args[modeIdx + 1]).not.toBe("json");
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("parseSpawnLine - RpcResponse", () => {
|
|
39
|
+
// Pi 原生 RpcResponse 格式(rpc-types.ts):{type:"response", id?, command, success, data?, error?}
|
|
40
|
+
// W1 修复后 isRpcResponse 判 type:response + command:string + success:boolean(SR-1 根因 1b)
|
|
41
|
+
|
|
42
|
+
it("解析 Pi 原生 success response(kind=response)", () => {
|
|
43
|
+
const rpcResponse = JSON.stringify({
|
|
44
|
+
type: "response",
|
|
45
|
+
id: "req-123",
|
|
46
|
+
command: "run",
|
|
47
|
+
success: true,
|
|
48
|
+
data: { answers: [{ question: "Q1", answer: "A1" }] },
|
|
49
|
+
});
|
|
50
|
+
const result = parseSpawnLine(rpcResponse);
|
|
51
|
+
expect(result?.kind).toBe("response");
|
|
52
|
+
if (result?.kind === "response") {
|
|
53
|
+
expect(result.id).toBe("req-123");
|
|
54
|
+
expect(result.command).toBe("run");
|
|
55
|
+
expect(result.success).toBe(true);
|
|
56
|
+
expect(result.data).toEqual({ answers: [{ question: "Q1", answer: "A1" }] });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("解析 Pi 原生 error response(kind=response)", () => {
|
|
61
|
+
const rpcError = JSON.stringify({
|
|
62
|
+
type: "response",
|
|
63
|
+
id: "req-456",
|
|
64
|
+
command: "run",
|
|
65
|
+
success: false,
|
|
66
|
+
error: "task failed: timeout",
|
|
67
|
+
});
|
|
68
|
+
const result = parseSpawnLine(rpcError);
|
|
69
|
+
expect(result?.kind).toBe("response");
|
|
70
|
+
if (result?.kind === "response") {
|
|
71
|
+
expect(result.id).toBe("req-456");
|
|
72
|
+
expect(result.success).toBe(false);
|
|
73
|
+
expect(result.error).toBe("task failed: timeout");
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("旧 JSON-RPC 2.0 格式不再归类为 response(W1 废弃)", () => {
|
|
78
|
+
// SR-1: 旧 {jsonrpc:"2.0", id, result} 无 type:response 字段,应落入 invalid
|
|
79
|
+
const oldRpc = JSON.stringify({ jsonrpc: "2.0", id: "req-old", result: { x: 1 } });
|
|
80
|
+
const result = parseSpawnLine(oldRpc);
|
|
81
|
+
expect(result?.kind).not.toBe("response");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("非 response 类型的 JSON 不归类为 response", () => {
|
|
85
|
+
const normalEvent = JSON.stringify({ type: "tool_execution_start", toolName: "bash" });
|
|
86
|
+
const result = parseSpawnLine(normalEvent);
|
|
87
|
+
expect(result?.kind).not.toBe("response");
|
|
88
|
+
});
|
|
89
|
+
});
|