@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,383 @@
|
|
|
1
|
+
// lifecycle-manager 单测 —— V2 §5.2 模块 1(进程生命周期管理)五项职责。
|
|
2
|
+
//
|
|
3
|
+
// 测试策略:
|
|
4
|
+
// - idle timer / ceiling LRU 用 vi.useFakeTimers() + advanceTimersByTime(同时 mock
|
|
5
|
+
// Date.now(),让 lastTouched 时间戳可控)。
|
|
6
|
+
// - activate 互斥锁是纯 Promise 链(无 timer),用 advanceTimersByTimeAsync(0)
|
|
7
|
+
// flush microtask 验证串行化。
|
|
8
|
+
// - 每个用例 beforeEach 调 _resetLifecycleState() 隔离模块级单例状态。
|
|
9
|
+
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
DEFAULT_IDLE_TIMEOUT_MS,
|
|
14
|
+
DEFAULT_MAX_ALIVE_PROCESSES,
|
|
15
|
+
_resetLifecycleState,
|
|
16
|
+
acquireActivateLock,
|
|
17
|
+
armIdleTimer,
|
|
18
|
+
disarmIdleTimer,
|
|
19
|
+
evictIfOverCeiling,
|
|
20
|
+
getActiveProcessCount,
|
|
21
|
+
hasIdleTimer,
|
|
22
|
+
reapAllAliveProcesses,
|
|
23
|
+
registerActiveProcess,
|
|
24
|
+
scanOrphanProcesses,
|
|
25
|
+
touchActiveProcess,
|
|
26
|
+
unregisterActiveProcess,
|
|
27
|
+
} from "../lifecycle-manager.ts";
|
|
28
|
+
|
|
29
|
+
describe("lifecycle-manager — V2 §5.2 模块 1", () => {
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
vi.useFakeTimers();
|
|
32
|
+
_resetLifecycleState();
|
|
33
|
+
// [F-4 同源修复] env 隔离:「默认超时」用例依赖 XYZ_SUBAGENT_IDLE_TIMEOUT_MS
|
|
34
|
+
// 未设基线(getEnvIdleTimeoutMs 会覆盖 DEFAULT),宿主 export 即假红。空串 = 未设。
|
|
35
|
+
vi.stubEnv("XYZ_SUBAGENT_IDLE_TIMEOUT_MS", "");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
_resetLifecycleState();
|
|
40
|
+
vi.useRealTimers();
|
|
41
|
+
vi.unstubAllEnvs();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// ============================================================
|
|
45
|
+
// 职责 1:idle timer
|
|
46
|
+
// ============================================================
|
|
47
|
+
describe("职责1 idle timer", () => {
|
|
48
|
+
it("arm 后到 timeoutMs 触发 onTimeout(边界:差 1ms 不触发)", () => {
|
|
49
|
+
const onTimeout = vi.fn();
|
|
50
|
+
armIdleTimer("sa-1", onTimeout, 1000);
|
|
51
|
+
|
|
52
|
+
expect(hasIdleTimer("sa-1")).toBe(true);
|
|
53
|
+
vi.advanceTimersByTime(999);
|
|
54
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
55
|
+
|
|
56
|
+
vi.advanceTimersByTime(1);
|
|
57
|
+
expect(onTimeout).toHaveBeenCalledTimes(1);
|
|
58
|
+
// 触发后自动从 Map 移除(不残留失效 entry)
|
|
59
|
+
expect(hasIdleTimer("sa-1")).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("disarm 后不再触发 onTimeout", () => {
|
|
63
|
+
const onTimeout = vi.fn();
|
|
64
|
+
armIdleTimer("sa-1", onTimeout, 1000);
|
|
65
|
+
disarmIdleTimer("sa-1");
|
|
66
|
+
|
|
67
|
+
expect(hasIdleTimer("sa-1")).toBe(false);
|
|
68
|
+
vi.advanceTimersByTime(5000);
|
|
69
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("disarm 不存在的 record 为 no-op(不抛错)", () => {
|
|
73
|
+
expect(() => disarmIdleTimer("never-armed")).not.toThrow();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("重复 arm 刷新 timer:旧 timer 作废、重新计时", () => {
|
|
77
|
+
const onTimeout = vi.fn();
|
|
78
|
+
armIdleTimer("sa-1", onTimeout, 1000);
|
|
79
|
+
vi.advanceTimersByTime(500); // 过了 500ms
|
|
80
|
+
|
|
81
|
+
armIdleTimer("sa-1", onTimeout, 1000); // 刷新:重新计 1000ms
|
|
82
|
+
vi.advanceTimersByTime(999);
|
|
83
|
+
expect(onTimeout).not.toHaveBeenCalled(); // 旧 timer 已作废,新的还没到
|
|
84
|
+
|
|
85
|
+
vi.advanceTimersByTime(1);
|
|
86
|
+
expect(onTimeout).toHaveBeenCalledTimes(1);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("默认超时 = DEFAULT_IDLE_TIMEOUT_MS", () => {
|
|
90
|
+
const onTimeout = vi.fn();
|
|
91
|
+
armIdleTimer("sa-1", onTimeout); // 不传 timeoutMs
|
|
92
|
+
|
|
93
|
+
vi.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT_MS - 1);
|
|
94
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
95
|
+
|
|
96
|
+
vi.advanceTimersByTime(1);
|
|
97
|
+
expect(onTimeout).toHaveBeenCalledTimes(1);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("显式禁用值(0/负数)→ 不挂 timer(预算语义对齐:idle GC 可显式关闭;旧实现 0 落成 setTimeout(0) 立即 kill)", () => {
|
|
101
|
+
const onTimeout = vi.fn();
|
|
102
|
+
armIdleTimer("sa-disable-0", onTimeout, 0);
|
|
103
|
+
expect(hasIdleTimer("sa-disable-0")).toBe(false);
|
|
104
|
+
armIdleTimer("sa-disable-neg", onTimeout, -1);
|
|
105
|
+
expect(hasIdleTimer("sa-disable-neg")).toBe(false);
|
|
106
|
+
|
|
107
|
+
vi.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT_MS * 2);
|
|
108
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("显式禁用值顺带 disarm 已有 armed timer(禁用不形同虚设)", () => {
|
|
112
|
+
const onTimeout = vi.fn();
|
|
113
|
+
armIdleTimer("sa-disable-late", onTimeout, 1000);
|
|
114
|
+
expect(hasIdleTimer("sa-disable-late")).toBe(true);
|
|
115
|
+
armIdleTimer("sa-disable-late", onTimeout, 0);
|
|
116
|
+
expect(hasIdleTimer("sa-disable-late")).toBe(false);
|
|
117
|
+
vi.advanceTimersByTime(2000);
|
|
118
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// [U1] setTimeout 2^31-1 溢出 fail-fast:溢出 delay 被 Node 置 1ms 立即触发
|
|
122
|
+
//(「长空闲保活」变「立即 kill」),arm 入口拦截且错误含上限值与恢复指引。
|
|
123
|
+
// 显式禁用通道(<=0)不受影响(上方用例已锁)。
|
|
124
|
+
it("idleTimeoutMs 溢出(>2^31-1)→ fail-fast throw,不挂 timer", () => {
|
|
125
|
+
const onTimeout = vi.fn();
|
|
126
|
+
expect(() => armIdleTimer("sa-overflow", onTimeout, 3_000_000_000)).toThrowError(/2147483647/);
|
|
127
|
+
expect(() => armIdleTimer("sa-overflow", onTimeout, Number.MAX_SAFE_INTEGER)).toThrowError(
|
|
128
|
+
/Recovery/,
|
|
129
|
+
);
|
|
130
|
+
expect(hasIdleTimer("sa-overflow")).toBe(false);
|
|
131
|
+
vi.advanceTimersByTime(3_000_000_000);
|
|
132
|
+
expect(onTimeout).not.toHaveBeenCalled();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("env XYZ_SUBAGENT_IDLE_TIMEOUT_MS 溢出 → fail-fast throw(arm 入口统一拦截)", () => {
|
|
136
|
+
vi.stubEnv("XYZ_SUBAGENT_IDLE_TIMEOUT_MS", "3000000000");
|
|
137
|
+
const onTimeout = vi.fn();
|
|
138
|
+
expect(() => armIdleTimer("sa-env-overflow", onTimeout)).toThrowError(/2147483647/);
|
|
139
|
+
expect(hasIdleTimer("sa-env-overflow")).toBe(false);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// ============================================================
|
|
144
|
+
// 职责 2:全局 ceiling(LRU 挤出)
|
|
145
|
+
// ============================================================
|
|
146
|
+
describe("职责2 全局 ceiling", () => {
|
|
147
|
+
it("register 超过上限时 evictIfOverCeiling 挤出最久空闲", () => {
|
|
148
|
+
registerActiveProcess("sa-1"); // 最早
|
|
149
|
+
vi.advanceTimersByTime(10);
|
|
150
|
+
registerActiveProcess("sa-2");
|
|
151
|
+
vi.advanceTimersByTime(10);
|
|
152
|
+
registerActiveProcess("sa-3"); // 超限(size=3 > ceiling=2)
|
|
153
|
+
expect(getActiveProcessCount()).toBe(3);
|
|
154
|
+
|
|
155
|
+
const evicted: string[] = [];
|
|
156
|
+
evictIfOverCeiling((id) => evicted.push(id), 2);
|
|
157
|
+
|
|
158
|
+
expect(evicted).toEqual(["sa-1"]); // lastTouched 最老的被挤出
|
|
159
|
+
expect(getActiveProcessCount()).toBe(2);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("不超过上限时 evictIfOverCeiling 不挤出", () => {
|
|
163
|
+
registerActiveProcess("sa-1");
|
|
164
|
+
registerActiveProcess("sa-2");
|
|
165
|
+
|
|
166
|
+
const evicted: string[] = [];
|
|
167
|
+
evictIfOverCeiling((id) => evicted.push(id), 5);
|
|
168
|
+
|
|
169
|
+
expect(evicted).toEqual([]);
|
|
170
|
+
expect(getActiveProcessCount()).toBe(2);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("touch 更新 LRU:被 touch 的不再是挤出候选", () => {
|
|
174
|
+
registerActiveProcess("sa-1");
|
|
175
|
+
vi.advanceTimersByTime(10);
|
|
176
|
+
registerActiveProcess("sa-2");
|
|
177
|
+
vi.advanceTimersByTime(10);
|
|
178
|
+
touchActiveProcess("sa-1"); // sa-1 变最新 → sa-2 变最老
|
|
179
|
+
|
|
180
|
+
const evicted: string[] = [];
|
|
181
|
+
evictIfOverCeiling((id) => evicted.push(id), 1);
|
|
182
|
+
|
|
183
|
+
expect(evicted).toEqual(["sa-2"]); // sa-2 最久未 touch,被挤出
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("touch 不存在的 record 为 no-op(不隐式创建)", () => {
|
|
187
|
+
expect(() => touchActiveProcess("never-registered")).not.toThrow();
|
|
188
|
+
expect(getActiveProcessCount()).toBe(0);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("unregister 清理活进程集合", () => {
|
|
192
|
+
registerActiveProcess("sa-1");
|
|
193
|
+
registerActiveProcess("sa-2");
|
|
194
|
+
unregisterActiveProcess("sa-1");
|
|
195
|
+
expect(getActiveProcessCount()).toBe(1);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("挤出时连带 disarm 被挤 record 的 idle timer", () => {
|
|
199
|
+
registerActiveProcess("sa-1");
|
|
200
|
+
armIdleTimer("sa-1", vi.fn(), 1000);
|
|
201
|
+
registerActiveProcess("sa-2");
|
|
202
|
+
vi.advanceTimersByTime(10);
|
|
203
|
+
registerActiveProcess("sa-3"); // sa-1 最老,将被挤出
|
|
204
|
+
|
|
205
|
+
evictIfOverCeiling(() => {
|
|
206
|
+
/* kill */
|
|
207
|
+
}, 2);
|
|
208
|
+
|
|
209
|
+
expect(hasIdleTimer("sa-1")).toBe(false); // 挤出 cascade disarm
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("unregister 连带 disarm idle timer(进程终态 timer 不残留)", () => {
|
|
213
|
+
registerActiveProcess("sa-1");
|
|
214
|
+
armIdleTimer("sa-1", vi.fn(), 1000);
|
|
215
|
+
unregisterActiveProcess("sa-1");
|
|
216
|
+
expect(hasIdleTimer("sa-1")).toBe(false);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("默认上限 = DEFAULT_MAX_ALIVE_PROCESSES(不传 maxAlive)", () => {
|
|
220
|
+
for (let i = 0; i < DEFAULT_MAX_ALIVE_PROCESSES; i++) {
|
|
221
|
+
registerActiveProcess(`sa-${i}`);
|
|
222
|
+
vi.advanceTimersByTime(1); // 保证 lastTouched 严格递增
|
|
223
|
+
}
|
|
224
|
+
registerActiveProcess("sa-over"); // 超限
|
|
225
|
+
const evicted: string[] = [];
|
|
226
|
+
evictIfOverCeiling((id) => evicted.push(id)); // 用默认上限
|
|
227
|
+
|
|
228
|
+
expect(evicted).toEqual(["sa-0"]); // 默认上限下挤出最早
|
|
229
|
+
expect(getActiveProcessCount()).toBe(DEFAULT_MAX_ALIVE_PROCESSES);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("大幅超限时 while 循环挤出多个直到 ≤ ceiling", () => {
|
|
233
|
+
for (let i = 0; i < 5; i++) {
|
|
234
|
+
registerActiveProcess(`sa-${i}`);
|
|
235
|
+
vi.advanceTimersByTime(1);
|
|
236
|
+
}
|
|
237
|
+
const evicted: string[] = [];
|
|
238
|
+
evictIfOverCeiling((id) => evicted.push(id), 2);
|
|
239
|
+
|
|
240
|
+
expect(evicted).toEqual(["sa-0", "sa-1", "sa-2"]); // 挤出 3 个(5-2),按 LRU
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// ============================================================
|
|
245
|
+
// 职责 3:shutdown 收割
|
|
246
|
+
// ============================================================
|
|
247
|
+
describe("职责3 shutdown 收割", () => {
|
|
248
|
+
it("reapAllAliveProcesses 遍历活进程调 killFn 并返回列表", () => {
|
|
249
|
+
registerActiveProcess("sa-1");
|
|
250
|
+
registerActiveProcess("sa-2");
|
|
251
|
+
|
|
252
|
+
const killed: string[] = [];
|
|
253
|
+
const reaped = reapAllAliveProcesses((id) => killed.push(id));
|
|
254
|
+
|
|
255
|
+
expect(killed).toHaveLength(2);
|
|
256
|
+
expect(killed).toEqual(expect.arrayContaining(["sa-1", "sa-2"]));
|
|
257
|
+
expect(reaped).toEqual(expect.arrayContaining(["sa-1", "sa-2"]));
|
|
258
|
+
expect(getActiveProcessCount()).toBe(0);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("reap 后 idle timer 已 disarm(advance 不再触发)", () => {
|
|
262
|
+
registerActiveProcess("sa-1");
|
|
263
|
+
const onTimeout = vi.fn();
|
|
264
|
+
armIdleTimer("sa-1", onTimeout, 1000);
|
|
265
|
+
|
|
266
|
+
reapAllAliveProcesses(() => {
|
|
267
|
+
/* kill */
|
|
268
|
+
});
|
|
269
|
+
vi.advanceTimersByTime(10000);
|
|
270
|
+
|
|
271
|
+
expect(onTimeout).not.toHaveBeenCalled(); // timer 已被 reap 清理
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("无活进程时 reapAll 返回空列表", () => {
|
|
275
|
+
const reaped = reapAllAliveProcesses(() => {
|
|
276
|
+
/* kill */
|
|
277
|
+
});
|
|
278
|
+
expect(reaped).toEqual([]);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
// ============================================================
|
|
283
|
+
// 职责 4:孤儿扫描
|
|
284
|
+
// ============================================================
|
|
285
|
+
describe("职责4 孤儿扫描", () => {
|
|
286
|
+
it("pid 存活 + 不在活进程集合 = 孤儿", () => {
|
|
287
|
+
registerActiveProcess("sa-active"); // 在集合内
|
|
288
|
+
const records = [
|
|
289
|
+
{ id: "sa-active", pid: 100 },
|
|
290
|
+
{ id: "sa-orphan", pid: 200 }, // pid 活但不在集合 → 孤儿
|
|
291
|
+
{ id: "sa-dead", pid: 300 }, // pid 不活 → 非孤儿
|
|
292
|
+
{ id: "sa-nopid" }, // 无 pid → 非孤儿
|
|
293
|
+
];
|
|
294
|
+
|
|
295
|
+
const orphans = scanOrphanProcesses(records, (pid) => pid === 100 || pid === 200);
|
|
296
|
+
|
|
297
|
+
expect(orphans).toEqual(["sa-orphan"]);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("pid 不存活 = 非孤儿", () => {
|
|
301
|
+
const records = [{ id: "sa-1", pid: 999 }];
|
|
302
|
+
const orphans = scanOrphanProcesses(records, () => false); // 全不活
|
|
303
|
+
expect(orphans).toEqual([]);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("在活进程集合内 = 非孤儿(即使 pid 活)", () => {
|
|
307
|
+
registerActiveProcess("sa-1");
|
|
308
|
+
const records = [{ id: "sa-1", pid: 100 }];
|
|
309
|
+
const orphans = scanOrphanProcesses(records, () => true);
|
|
310
|
+
expect(orphans).toEqual([]);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("无 pid 的 record 不视为孤儿", () => {
|
|
314
|
+
const records = [
|
|
315
|
+
{ id: "sa-1" },
|
|
316
|
+
{ id: "sa-2", sessionFile: "/x.jsonl" },
|
|
317
|
+
];
|
|
318
|
+
const orphans = scanOrphanProcesses(records, () => true);
|
|
319
|
+
expect(orphans).toEqual([]);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("扫描是只读(不改活进程集合)", () => {
|
|
323
|
+
registerActiveProcess("sa-1");
|
|
324
|
+
scanOrphanProcesses([{ id: "sa-2", pid: 200 }], () => true);
|
|
325
|
+
expect(getActiveProcessCount()).toBe(1); // 集合不变
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
// ============================================================
|
|
330
|
+
// 职责 5:activate 互斥锁
|
|
331
|
+
// ============================================================
|
|
332
|
+
describe("职责5 activate 互斥锁", () => {
|
|
333
|
+
it("首次 acquire 立即 resolve 返回 release 函数", async () => {
|
|
334
|
+
const release = await acquireActivateLock("sa-1");
|
|
335
|
+
expect(typeof release).toBe("function");
|
|
336
|
+
release();
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it("并发 acquire 同一 recordId 串行化:第二次等首次 release", async () => {
|
|
340
|
+
const release1 = await acquireActivateLock("sa-1");
|
|
341
|
+
|
|
342
|
+
let secondResolved = false;
|
|
343
|
+
const secondPromise = acquireActivateLock("sa-1").then((r) => {
|
|
344
|
+
secondResolved = true;
|
|
345
|
+
return r;
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
await vi.advanceTimersByTimeAsync(0); // flush microtask
|
|
349
|
+
expect(secondResolved).toBe(false); // 还在等首次 release
|
|
350
|
+
|
|
351
|
+
release1();
|
|
352
|
+
await vi.advanceTimersByTimeAsync(0); // flush microtask
|
|
353
|
+
expect(secondResolved).toBe(true);
|
|
354
|
+
|
|
355
|
+
const release2 = await secondPromise;
|
|
356
|
+
release2();
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("不同 recordId 不互斥(各自独立链)", async () => {
|
|
360
|
+
const release1 = await acquireActivateLock("sa-1");
|
|
361
|
+
|
|
362
|
+
let secondResolved = false;
|
|
363
|
+
acquireActivateLock("sa-2").then((r) => {
|
|
364
|
+
secondResolved = true;
|
|
365
|
+
r();
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
await vi.advanceTimersByTimeAsync(0); // flush microtask
|
|
369
|
+
expect(secondResolved).toBe(true); // sa-2 不被 sa-1 阻塞
|
|
370
|
+
|
|
371
|
+
release1();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("release 后同 recordId 可再次 acquire", async () => {
|
|
375
|
+
const r1 = await acquireActivateLock("sa-1");
|
|
376
|
+
r1();
|
|
377
|
+
|
|
378
|
+
const r2 = await acquireActivateLock("sa-1");
|
|
379
|
+
expect(typeof r2).toBe("function");
|
|
380
|
+
r2();
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// lifecycle-predicates 单测(v4 B-1)。
|
|
2
|
+
// 验证 isIdle/isResumable/hasLiveProcessHandle 在两态收敛后的判定逻辑。
|
|
3
|
+
// 依赖 lifecycle-manager 模块级 idleTimers 与 session-runner.spawnedChildren(单例 Map),
|
|
4
|
+
// beforeEach 重置隔离。
|
|
5
|
+
|
|
6
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
armIdleTimer,
|
|
10
|
+
disarmIdleTimer,
|
|
11
|
+
_resetLifecycleState,
|
|
12
|
+
} from "../lifecycle-manager.ts";
|
|
13
|
+
import { spawnedChildren } from "../session-runner.ts";
|
|
14
|
+
import type { ChildProcess } from "node:child_process";
|
|
15
|
+
import type { ExecutionRecord } from "../types.ts";
|
|
16
|
+
|
|
17
|
+
import { hasLiveProcessHandle, isIdle, isResumable } from "../lifecycle-predicates.ts";
|
|
18
|
+
|
|
19
|
+
/** 构造最小 ExecutionRecord(status 默认 running)。 */
|
|
20
|
+
function makeRecord(overrides: Partial<ExecutionRecord> = {}): ExecutionRecord {
|
|
21
|
+
return {
|
|
22
|
+
id: "sa-test",
|
|
23
|
+
agent: "general-purpose",
|
|
24
|
+
model: "test/model",
|
|
25
|
+
thinkingLevel: undefined,
|
|
26
|
+
mode: "background",
|
|
27
|
+
task: "test",
|
|
28
|
+
slug: "test",
|
|
29
|
+
startedAt: Date.now(),
|
|
30
|
+
rootSessionId: "root",
|
|
31
|
+
parentRecordId: undefined,
|
|
32
|
+
depth: 0,
|
|
33
|
+
status: "running",
|
|
34
|
+
turns: [],
|
|
35
|
+
turnCount: 0,
|
|
36
|
+
totalTokens: 0,
|
|
37
|
+
lastError: undefined,
|
|
38
|
+
round: 0,
|
|
39
|
+
endedAt: undefined,
|
|
40
|
+
result: undefined,
|
|
41
|
+
error: undefined,
|
|
42
|
+
agentResult: undefined,
|
|
43
|
+
controller: undefined,
|
|
44
|
+
...overrides,
|
|
45
|
+
} as ExecutionRecord;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 构造最小 ChildProcess fake(hasLiveProcessHandle 只读 .killed)。 */
|
|
49
|
+
function fakeChild(killed: boolean): ChildProcess {
|
|
50
|
+
// 测试 fake:被测代码仅读 .killed,其余字段不可达,用 unknown 中转满足 Map 类型。
|
|
51
|
+
return { killed } as unknown as ChildProcess;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe("lifecycle-predicates (v4 B-1)", () => {
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
_resetLifecycleState();
|
|
57
|
+
spawnedChildren.clear();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("isIdle (= hasIdleTimer)", () => {
|
|
61
|
+
it("armed idle timer → true", () => {
|
|
62
|
+
const rec = makeRecord();
|
|
63
|
+
armIdleTimer(rec.id, () => {});
|
|
64
|
+
expect(isIdle(rec)).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("no idle timer → false", () => {
|
|
68
|
+
expect(isIdle(makeRecord())).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("disarmed timer → false", () => {
|
|
72
|
+
const rec = makeRecord();
|
|
73
|
+
armIdleTimer(rec.id, () => {});
|
|
74
|
+
disarmIdleTimer(rec.id);
|
|
75
|
+
expect(isIdle(rec)).toBe(false);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("hasLiveProcessHandle", () => {
|
|
80
|
+
it("no child in map → false", () => {
|
|
81
|
+
expect(hasLiveProcessHandle("sa-test")).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("child present and not killed → true", () => {
|
|
85
|
+
spawnedChildren.set("sa-test", fakeChild(false));
|
|
86
|
+
expect(hasLiveProcessHandle("sa-test")).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("child present but killed → false", () => {
|
|
90
|
+
spawnedChildren.set("sa-test", fakeChild(true));
|
|
91
|
+
expect(hasLiveProcessHandle("sa-test")).toBe(false);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("isResumable (= running && !hasLiveProcessHandle)", () => {
|
|
96
|
+
it("running + no live process → true (Path B / 跨重启)", () => {
|
|
97
|
+
expect(isResumable(makeRecord({ status: "running" }))).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("running + live process → false (Path A 保活 / 正在执行)", () => {
|
|
101
|
+
const rec = makeRecord({ status: "running" });
|
|
102
|
+
spawnedChildren.set(rec.id, fakeChild(false));
|
|
103
|
+
expect(isResumable(rec)).toBe(false);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("closed + no live process → false (终态不可 resume)", () => {
|
|
107
|
+
expect(isResumable(makeRecord({ status: "closed" }))).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("closed + live process → false (终态优先)", () => {
|
|
111
|
+
const rec = makeRecord({ status: "closed" });
|
|
112
|
+
spawnedChildren.set(rec.id, fakeChild(false));
|
|
113
|
+
expect(isResumable(rec)).toBe(false);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// ManifestStore — parentRecordId 落盘测试(M3a)。
|
|
2
|
+
//
|
|
3
|
+
// 验证 M3a 三条契约:
|
|
4
|
+
// 1. 新 record 落盘含 parentRecordId(depth>=1 subagent 读回直接父 record id)
|
|
5
|
+
// 2. 旧 manifest JSON(无 parentRecordId 字段)readManifest 仍有效(向后兼容)
|
|
6
|
+
// 3. isValidManifest 校验不改(5 必填不变,parentRecordId optional)
|
|
7
|
+
//
|
|
8
|
+
// isValidManifest 私有未 export——通过 readManifest 等价验证:readManifest 内部
|
|
9
|
+
// `isValidManifest(parsed) ? parsed : null`,非 null ⟺ 校验通过。不 export 私有函数
|
|
10
|
+
// 避免扩大公共 API 表面(M3a C1 约束:不改 isValidManifest,仅 2 行代码改动)。
|
|
11
|
+
|
|
12
|
+
import * as fs from "node:fs";
|
|
13
|
+
import * as os from "node:os";
|
|
14
|
+
import * as path from "node:path";
|
|
15
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
16
|
+
|
|
17
|
+
import { ManifestStore, type ManifestRecord } from "../manifest-store.ts";
|
|
18
|
+
|
|
19
|
+
/** 构造最小合法 ManifestRecord(5 必填),optional 字段按 overrides 传入。 */
|
|
20
|
+
function makeBaseManifest(overrides: Partial<ManifestRecord> = {}): ManifestRecord {
|
|
21
|
+
return {
|
|
22
|
+
id: "rec-test",
|
|
23
|
+
rootSessionId: "session-main",
|
|
24
|
+
agentName: "worker",
|
|
25
|
+
status: "completed",
|
|
26
|
+
createdAt: 1000,
|
|
27
|
+
...overrides,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("ManifestStore — parentRecordId 落盘 (M3a)", () => {
|
|
32
|
+
let tmpDir: string;
|
|
33
|
+
let store: ManifestStore;
|
|
34
|
+
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "manifest-parentid-"));
|
|
37
|
+
store = new ManifestStore(tmpDir);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// ── TC-m3a-new-record-parentid ──
|
|
45
|
+
it("新 record 落盘含 parentRecordId(depth>=1 读回直接父 record id)", async () => {
|
|
46
|
+
const record = makeBaseManifest({
|
|
47
|
+
id: "rec-child",
|
|
48
|
+
parentRecordId: "sa-parent-1",
|
|
49
|
+
});
|
|
50
|
+
await store.writeManifest(record);
|
|
51
|
+
|
|
52
|
+
const readBack = await store.readManifest("rec-child");
|
|
53
|
+
expect(readBack).not.toBeNull();
|
|
54
|
+
expect(readBack?.parentRecordId).toBe("sa-parent-1");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("顶层 record(parentRecordId 缺失)落盘读回仍 undefined", async () => {
|
|
58
|
+
// 顶层 subagent parentRecordId=undefined(父是 main,main 无 record)
|
|
59
|
+
const record = makeBaseManifest({ id: "rec-top" });
|
|
60
|
+
await store.writeManifest(record);
|
|
61
|
+
|
|
62
|
+
const readBack = await store.readManifest("rec-top");
|
|
63
|
+
expect(readBack).not.toBeNull();
|
|
64
|
+
expect(readBack?.parentRecordId).toBeUndefined();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// ── TC-m3a-old-manifest-compat ──
|
|
68
|
+
it("旧 manifest JSON(无 parentRecordId)readManifest 返有效(isValidManifest 通过)", async () => {
|
|
69
|
+
// 手写旧版本格式 manifest(无 parentRecordId 字段),模拟旧 subagent-workflow 写的磁盘文件
|
|
70
|
+
const oldManifest = {
|
|
71
|
+
id: "rec-old",
|
|
72
|
+
rootSessionId: "session-main",
|
|
73
|
+
agentName: "worker",
|
|
74
|
+
status: "completed",
|
|
75
|
+
createdAt: 2000,
|
|
76
|
+
// 无 parentRecordId —— 旧版本写的
|
|
77
|
+
};
|
|
78
|
+
fs.writeFileSync(
|
|
79
|
+
path.join(tmpDir, "rec-old.json"),
|
|
80
|
+
JSON.stringify(oldManifest),
|
|
81
|
+
"utf-8",
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const readBack = await store.readManifest("rec-old");
|
|
85
|
+
// readManifest 内 isValidManifest(parsed) ? parsed : null —— 非 null 即 5 必填校验通过
|
|
86
|
+
expect(readBack).not.toBeNull();
|
|
87
|
+
expect(readBack?.id).toBe("rec-old");
|
|
88
|
+
expect(readBack?.parentRecordId).toBeUndefined();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// ── TC-m3a-isvalidmanifest-unchanged ──
|
|
92
|
+
it("isValidManifest 不改:含/不含 parentRecordId 均通过(5 必填不变)", async () => {
|
|
93
|
+
// isValidManifest 私有,通过 readManifest 等价验证(非 null ⟺ 校验通过)。
|
|
94
|
+
// 含 parentRecordId —— 新格式
|
|
95
|
+
const withParent = makeBaseManifest({
|
|
96
|
+
id: "rec-with-parent",
|
|
97
|
+
parentRecordId: "sa-x",
|
|
98
|
+
});
|
|
99
|
+
await store.writeManifest(withParent);
|
|
100
|
+
expect(await store.readManifest("rec-with-parent")).not.toBeNull();
|
|
101
|
+
|
|
102
|
+
// 不含 parentRecordId —— 仅 5 必填(旧格式 / 顶层 record)
|
|
103
|
+
const minimal = {
|
|
104
|
+
id: "rec-minimal",
|
|
105
|
+
rootSessionId: "session-main",
|
|
106
|
+
agentName: "worker",
|
|
107
|
+
status: "completed",
|
|
108
|
+
createdAt: 3000,
|
|
109
|
+
};
|
|
110
|
+
fs.writeFileSync(
|
|
111
|
+
path.join(tmpDir, "rec-minimal.json"),
|
|
112
|
+
JSON.stringify(minimal),
|
|
113
|
+
"utf-8",
|
|
114
|
+
);
|
|
115
|
+
expect(await store.readManifest("rec-minimal")).not.toBeNull();
|
|
116
|
+
});
|
|
117
|
+
});
|