@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,256 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as fsPromises from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
|
|
5
|
+
import { bestEffort } from "./best-effort.ts";
|
|
6
|
+
|
|
7
|
+
export interface ManifestRecord {
|
|
8
|
+
id: string;
|
|
9
|
+
rootSessionId: string;
|
|
10
|
+
/** 直接父 subagent record ID(层级树构建用)。顶层 record 缺失(undefined)。M3a 补字段。 */
|
|
11
|
+
parentRecordId?: string;
|
|
12
|
+
agentName: string;
|
|
13
|
+
/**
|
|
14
|
+
* 终态枚举:finalizeRecord 写 running/closed/cancelled 三态。
|
|
15
|
+
* SP-1 重构:旧 completed/failed 合并为 closed(L1 统一终态)。
|
|
16
|
+
* cancelled 保持独立(用户取消语义)。crashed 不进 manifest——
|
|
17
|
+
* crashed 是重启重建时靠 sidecar 四分支推断的派生态(见 record-store.ts reconstructAll)。
|
|
18
|
+
* 历史 "error"/"completed"/"failed" 值由读侧 mapManifestStatus 向后兼容映射。
|
|
19
|
+
*/
|
|
20
|
+
status: "running" | "closed" | "cancelled";
|
|
21
|
+
createdAt: number;
|
|
22
|
+
completedAt?: number;
|
|
23
|
+
sessionFile?: string;
|
|
24
|
+
/** FR-7 补字段:manifest 写入时从 ExecutionRecord 抓取,供 manifestToSubagent 投影真实值。 */
|
|
25
|
+
task?: string;
|
|
26
|
+
slug?: string;
|
|
27
|
+
model?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** JSON.stringify 缩进空格数(no-magic-numbers 合规)。 */
|
|
31
|
+
const MANIFEST_INDENT_SPACES = 2;
|
|
32
|
+
|
|
33
|
+
/** [perf] 缓存校验戳(与 record-store.ts Stamp 同构;manifest 是小文件,mtime+size 足够)。 */
|
|
34
|
+
interface Stamp {
|
|
35
|
+
mtimeMs: number;
|
|
36
|
+
size: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function statStamp(p: string): Stamp | null {
|
|
40
|
+
try {
|
|
41
|
+
const s = fs.statSync(p);
|
|
42
|
+
return { mtimeMs: s.mtimeMs, size: s.size };
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 合法 manifest status 集合(3 态;运行时守卫用,磁盘文件可能陈旧/损坏)。
|
|
49
|
+
* SP-1:completed/failed 合并为 closed。读侧 mapManifestStatus 向后兼容旧值。
|
|
50
|
+
* crashed 不在其中。 */
|
|
51
|
+
const VALID_MANIFEST_STATUSES: ReadonlySet<string> = new Set([
|
|
52
|
+
"running",
|
|
53
|
+
"closed",
|
|
54
|
+
"cancelled",
|
|
55
|
+
"completed", // 向后兼容旧 manifest 数据
|
|
56
|
+
"failed", // 向后兼容旧 manifest 数据
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 校验 JSON.parse 产物是否为合法 ManifestRecord。
|
|
61
|
+
* 关键字段类型检查——不合法返回 false,调用方据此过滤(防损坏/陈旧文件污染投影)。
|
|
62
|
+
*/
|
|
63
|
+
function isValidManifest(value: unknown): value is ManifestRecord {
|
|
64
|
+
if (typeof value !== "object" || value === null) return false;
|
|
65
|
+
const v = value as Record<string, unknown>;
|
|
66
|
+
return (
|
|
67
|
+
typeof v.id === "string" &&
|
|
68
|
+
typeof v.rootSessionId === "string" &&
|
|
69
|
+
typeof v.agentName === "string" &&
|
|
70
|
+
typeof v.createdAt === "number" &&
|
|
71
|
+
typeof v.status === "string" &&
|
|
72
|
+
VALID_MANIFEST_STATUSES.has(v.status)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class ManifestStore {
|
|
77
|
+
private readonly dir: string;
|
|
78
|
+
|
|
79
|
+
/** [perf] per-file 缓存:file → { stamp, record }。record=null 表示「已解析但非法」(缓存
|
|
80
|
+
* 负结果避免反复 parse 损坏文件)。stat 戳变化(writeManifest tmp→rename 后 mtime/size 变)
|
|
81
|
+
* 自动失效;删除的文件在下次扫描时修剪。 */
|
|
82
|
+
private readonly cache = new Map<string, { stamp: Stamp; record: ManifestRecord | null }>();
|
|
83
|
+
|
|
84
|
+
constructor(dir: string) {
|
|
85
|
+
this.dir = dir;
|
|
86
|
+
if (!fs.existsSync(dir)) {
|
|
87
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 原子写:tmp → fsync → rename → fsync dir。真异步(fs.promises,不阻塞 event loop)。
|
|
93
|
+
*
|
|
94
|
+
* rename 失败时 best-effort 清理残留 tmp(用 renamed 标志在 catch 中决定是否 unlink),
|
|
95
|
+
* 不掩盖原错误。失败向上抛——调用方(finalizeRecord)决定降级策略。
|
|
96
|
+
*/
|
|
97
|
+
async writeManifest(record: ManifestRecord): Promise<void> {
|
|
98
|
+
const filePath = path.join(this.dir, `${record.id}.json`);
|
|
99
|
+
const tmpPath = `${filePath}.tmp.${process.pid}`;
|
|
100
|
+
const content = JSON.stringify(record, null, MANIFEST_INDENT_SPACES);
|
|
101
|
+
|
|
102
|
+
let renamed = false;
|
|
103
|
+
try {
|
|
104
|
+
// 1. 写 tmp → fsync 文件
|
|
105
|
+
const fh = await fsPromises.open(tmpPath, "w");
|
|
106
|
+
try {
|
|
107
|
+
await fh.writeFile(content, "utf-8");
|
|
108
|
+
await fh.sync();
|
|
109
|
+
} finally {
|
|
110
|
+
await fh.close();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 2. rename tmp → final(放入 try:失败时 catch 清理 tmp)
|
|
114
|
+
await fsPromises.rename(tmpPath, filePath);
|
|
115
|
+
renamed = true;
|
|
116
|
+
|
|
117
|
+
// 3. fsync 目录(best-effort:POSIX 不要求,失败不否定已成功的 rename)
|
|
118
|
+
try {
|
|
119
|
+
const dirFh = await fsPromises.open(this.dir, "r");
|
|
120
|
+
try {
|
|
121
|
+
await dirFh.sync();
|
|
122
|
+
} finally {
|
|
123
|
+
await dirFh.close();
|
|
124
|
+
}
|
|
125
|
+
} catch (dirSyncErr) {
|
|
126
|
+
bestEffort(dirSyncErr, "fsync dir (writeManifest)");
|
|
127
|
+
}
|
|
128
|
+
} catch (err) {
|
|
129
|
+
// rename 未成功 → 清理残留 tmp(best-effort,不掩盖原错误)
|
|
130
|
+
if (!renamed) {
|
|
131
|
+
try {
|
|
132
|
+
await fsPromises.unlink(tmpPath);
|
|
133
|
+
} catch (cleanupErr) {
|
|
134
|
+
// best-effort:tmp 可能已被 rename 消费或从未创建。不影响主错误(下面 re-throw err)
|
|
135
|
+
bestEffort(cleanupErr, "unlink tmp (writeManifest)");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
throw err;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 按 id 读 manifest。文件不存在/JSON 损坏/schema 不合法均返回 null。
|
|
144
|
+
* 调用方需处理 null。
|
|
145
|
+
*/
|
|
146
|
+
async readManifest(id: string): Promise<ManifestRecord | null> {
|
|
147
|
+
const filePath = path.join(this.dir, `${id}.json`);
|
|
148
|
+
try {
|
|
149
|
+
const content = await fsPromises.readFile(filePath, "utf-8");
|
|
150
|
+
const parsed: unknown = JSON.parse(content);
|
|
151
|
+
return isValidManifest(parsed) ? parsed : null;
|
|
152
|
+
} catch {
|
|
153
|
+
// 文件缺失(ENOENT)或 JSON 损坏(SyntaxError)均降级为 null
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 同步读取所有 manifest 记录(best-effort,损坏/非法文件跳过)。
|
|
160
|
+
* 供 RecordStore.collectRecords 投影 orphan 记录使用——替代对私有 dir 的反射访问。
|
|
161
|
+
* 仅返回通过 isValidManifest 校验的记录。
|
|
162
|
+
*
|
|
163
|
+
* [perf] per-file 缓存 + stat 戳校验:collectRecords 每次渲染都调本方法,旧实现每次
|
|
164
|
+
* 全量 readFileSync + JSON.parse 千级 manifest(实测 ~300ms/次)。命中缓存的文件零读取。
|
|
165
|
+
*/
|
|
166
|
+
listAllSync(): readonly ManifestRecord[] {
|
|
167
|
+
let files: string[];
|
|
168
|
+
try {
|
|
169
|
+
files = fs.readdirSync(this.dir);
|
|
170
|
+
} catch {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
const names = files.filter((f) => f.endsWith(".json") && !f.includes(".tmp."));
|
|
174
|
+
const disk = new Set(names);
|
|
175
|
+
|
|
176
|
+
// 修剪已删除文件
|
|
177
|
+
for (const f of this.cache.keys()) {
|
|
178
|
+
if (!disk.has(f)) this.cache.delete(f);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const results: ManifestRecord[] = [];
|
|
182
|
+
for (const file of names) {
|
|
183
|
+
const filePath = path.join(this.dir, file);
|
|
184
|
+
const stamp = statStamp(filePath);
|
|
185
|
+
if (!stamp) {
|
|
186
|
+
this.cache.delete(file);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const cached = this.cache.get(file);
|
|
190
|
+
if (cached && cached.stamp.mtimeMs === stamp.mtimeMs && cached.stamp.size === stamp.size) {
|
|
191
|
+
if (cached.record) results.push(cached.record);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
196
|
+
const parsed: unknown = JSON.parse(content);
|
|
197
|
+
const record = isValidManifest(parsed) ? parsed : null;
|
|
198
|
+
this.cache.set(file, { stamp, record });
|
|
199
|
+
if (record) results.push(record);
|
|
200
|
+
} catch (fileErr) {
|
|
201
|
+
// best-effort:损坏/非法文件跳过(debug 记录便于排查)。缓存负结果防反复 parse。
|
|
202
|
+
this.cache.set(file, { stamp, record: null });
|
|
203
|
+
bestEffort(fileErr, `read manifest ${file} (listAllSync)`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return results;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* 启动时恢复 tmp 文件。
|
|
211
|
+
* 3 分支逻辑:
|
|
212
|
+
* 1. manifest 已存在 → 删 tmp(陈旧)
|
|
213
|
+
* 2. tmp 合法 + manifest 缺失 → rename tmp 为 manifest
|
|
214
|
+
* 3. tmp 非法 + manifest 缺失 → 删 tmp
|
|
215
|
+
*/
|
|
216
|
+
async recoverTmpFiles(): Promise<{ deleted: number; recovered: number }> {
|
|
217
|
+
let deleted = 0;
|
|
218
|
+
let recovered = 0;
|
|
219
|
+
|
|
220
|
+
const files = fs.readdirSync(this.dir);
|
|
221
|
+
const tmpFiles = files.filter((f) => f.includes(".json.tmp."));
|
|
222
|
+
|
|
223
|
+
for (const tmpFile of tmpFiles) {
|
|
224
|
+
const tmpPath = path.join(this.dir, tmpFile);
|
|
225
|
+
const manifestId = tmpFile.split(".json.tmp.")[0];
|
|
226
|
+
const manifestPath = path.join(this.dir, `${manifestId}.json`);
|
|
227
|
+
|
|
228
|
+
if (fs.existsSync(manifestPath)) {
|
|
229
|
+
// 分支 1: manifest 已存在,删 tmp
|
|
230
|
+
fs.unlinkSync(tmpPath);
|
|
231
|
+
deleted++;
|
|
232
|
+
} else {
|
|
233
|
+
// 试解析 tmp
|
|
234
|
+
try {
|
|
235
|
+
const content = fs.readFileSync(tmpPath, "utf-8");
|
|
236
|
+
const parsed: unknown = JSON.parse(content);
|
|
237
|
+
if (isValidManifest(parsed)) {
|
|
238
|
+
// 分支 2: tmp 是合法 manifest,rename 为正式文件
|
|
239
|
+
fs.renameSync(tmpPath, manifestPath);
|
|
240
|
+
recovered++;
|
|
241
|
+
} else {
|
|
242
|
+
// 分支 3b: 合法 JSON 但非合法 manifest(缺必填字段),删
|
|
243
|
+
fs.unlinkSync(tmpPath);
|
|
244
|
+
deleted++;
|
|
245
|
+
}
|
|
246
|
+
} catch {
|
|
247
|
+
// 分支 3a: JSON.parse 失败,删
|
|
248
|
+
fs.unlinkSync(tmpPath);
|
|
249
|
+
deleted++;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return { deleted, recovered };
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// src/runtime/model-config-service.ts
|
|
2
|
+
//
|
|
3
|
+
// 配置 + 模型解析领域 Service。"给定 agent 名 + 用户参数 + 主 agent 模型,用哪个模型?"
|
|
4
|
+
//
|
|
5
|
+
// 与 SubagentService(执行/记录/通知域)正交——本 Service 不碰 pool/store/notifier。
|
|
6
|
+
// 上游:SubagentService.execute 内部调 resolveModel。
|
|
7
|
+
// session_start 时经 initModel 注入 modelRegistry。
|
|
8
|
+
|
|
9
|
+
import { AgentRegistry } from "./agent-registry.ts";
|
|
10
|
+
import {
|
|
11
|
+
loadGlobalConfig,
|
|
12
|
+
readGlobalConfig,
|
|
13
|
+
type GlobalConfigReadResult,
|
|
14
|
+
} from "./config.ts";
|
|
15
|
+
import {
|
|
16
|
+
type AgentConfig,
|
|
17
|
+
type ModelInfo,
|
|
18
|
+
type ModelRegistryLike,
|
|
19
|
+
type ResolvedModel,
|
|
20
|
+
resolveModel,
|
|
21
|
+
} from "./model-resolver.ts";
|
|
22
|
+
import type { SubagentsGlobalConfig } from "./types.ts";
|
|
23
|
+
|
|
24
|
+
// ============================================================
|
|
25
|
+
// 类型
|
|
26
|
+
// ============================================================
|
|
27
|
+
|
|
28
|
+
/** Service 构造参数(进程级,跨 session 不变)。 */
|
|
29
|
+
export interface ModelConfigServiceInit {
|
|
30
|
+
agentDir: string;
|
|
31
|
+
/** 项目根目录(ctx.cwd,用于推导 workspaceRoot 扫描 project 级资源)。 */
|
|
32
|
+
cwd: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** session_start 注入参数(session 级,每次重建)。 */
|
|
36
|
+
export interface ModelServiceSessionInit {
|
|
37
|
+
/** 模型注册表(鉴权 + 发现)。null 立即抛错(fail-fast)。 */
|
|
38
|
+
modelRegistry: ModelRegistryLike | null;
|
|
39
|
+
/** 当前 session ID。 */
|
|
40
|
+
sessionId: string;
|
|
41
|
+
/**
|
|
42
|
+
* 主 agent 当前 model(session_start 时注入,model_select 时刷新)。
|
|
43
|
+
*
|
|
44
|
+
* renderCall 阶段的 ToolRenderContext 不含 model 字段(SDK 限制),无法直接拿到
|
|
45
|
+
* 主 agent model。缓存后 renderCall 的 resolveModel 能命中第三层(ctxModel),
|
|
46
|
+
* 让标题行恢复显示 model——即使未显式传 model 也能展示默认 model。
|
|
47
|
+
*
|
|
48
|
+
* [HISTORICAL] 99f20da1e 引入三层 fallback 后,renderCall 因拿不到 ctxModel
|
|
49
|
+
* 而 resolveModel 拗错→降级不显示 model。此缓存修复该降级。
|
|
50
|
+
*/
|
|
51
|
+
ctxModel?: ModelInfo;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ============================================================
|
|
55
|
+
// ModelConfigService
|
|
56
|
+
// ============================================================
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 配置 + 模型解析 Service。进程级单例。
|
|
60
|
+
*
|
|
61
|
+
* ┌──────────────────────────────────────────────────────┐
|
|
62
|
+
* │ globalConfig(~/.pi/.../config.json,仅 maxConcurrent)│
|
|
63
|
+
* │ agentRegistry(agent .md 发现 + frontmatter) │
|
|
64
|
+
* │ modelRegistry(SDK 注入的可用模型) │
|
|
65
|
+
* │ │
|
|
66
|
+
* │ resolveModel: override → agentConfig → 主 agent model │
|
|
67
|
+
* └──────────────────────────────────────────────────────┘
|
|
68
|
+
*/
|
|
69
|
+
export class ModelConfigService {
|
|
70
|
+
private globalConfig: SubagentsGlobalConfig;
|
|
71
|
+
private readonly agentRegistry: AgentRegistry;
|
|
72
|
+
private readonly agentRegistryDir: string;
|
|
73
|
+
private modelRegistry: ModelRegistryLike | null = null;
|
|
74
|
+
private _sessionId: string | undefined;
|
|
75
|
+
/** 主 agent 当前 model 缓存(session_start 注入,model_select 刷新)。 */
|
|
76
|
+
private _ctxModel: ModelInfo | undefined;
|
|
77
|
+
|
|
78
|
+
constructor(init: ModelConfigServiceInit) {
|
|
79
|
+
this.agentRegistryDir = init.agentDir;
|
|
80
|
+
this.globalConfig = loadGlobalConfig(init.agentDir);
|
|
81
|
+
this.agentRegistry = new AgentRegistry();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ── 生命周期(index.ts 调)──────────────────────────────
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* session_start 注入。封装 3 步固定时序:
|
|
88
|
+
* 1. reloadGlobalConfig(复用时拿最新 config)
|
|
89
|
+
* 2. injectModelRegistry(fail-fast:null 抛错)
|
|
90
|
+
* 3. setSessionId
|
|
91
|
+
*/
|
|
92
|
+
initModel(init: ModelServiceSessionInit): void {
|
|
93
|
+
// 1. 重载配置(agent 按需 loadByPath,无预热扫描)
|
|
94
|
+
this.reloadGlobalConfig();
|
|
95
|
+
|
|
96
|
+
// 2. modelRegistry(fail-fast)
|
|
97
|
+
if (init.modelRegistry === null) {
|
|
98
|
+
throw new Error("modelRegistry is required but got null");
|
|
99
|
+
}
|
|
100
|
+
this.modelRegistry = init.modelRegistry;
|
|
101
|
+
|
|
102
|
+
// 3. sessionId + ctxModel 缓存(model_select 后续调 setCtxModel 刷新)
|
|
103
|
+
this._sessionId = init.sessionId;
|
|
104
|
+
this._ctxModel = init.ctxModel;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 将一次三态读取结果提交到路由缓存(纯赋值幂等)。
|
|
109
|
+
*
|
|
110
|
+
* ok/absent 覆盖缓存、failed 保持缓存不动(坏 JSON 不能把好缓存打回缺省);
|
|
111
|
+
* 返回入参便于链式消费。用途 = 构造性同源:session_start 初始化与 per-turn 引擎
|
|
112
|
+
* 检测各只读一次文件,同一读取结果既刷新路由缓存又充当检测基准,消灭两次独立
|
|
113
|
+
* 读取之间的分叉窗口(两次读值不一致时检测走 unchanged,状态段/路由永停旧值)。
|
|
114
|
+
*/
|
|
115
|
+
applyGlobalConfig(read: GlobalConfigReadResult): GlobalConfigReadResult {
|
|
116
|
+
if (read.status !== "failed") {
|
|
117
|
+
this.globalConfig = read.config;
|
|
118
|
+
}
|
|
119
|
+
return read;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 三态重读全局配置并提交缓存(幂等可重入),返回本次读取结果供调用方感知。
|
|
124
|
+
*
|
|
125
|
+
* 从 initModel 提取(设计 D2):引擎感知检测器 per-turn poll 发现 config 变更时
|
|
126
|
+
* 调用本方法,使「system prompt 现值、路由缓存、变更通知」同 turn 对齐——只改注入
|
|
127
|
+
* 不刷新路由缓存,会出现 prompt 说引擎 B、实际派发跑引擎 A(权威信息源说谎)。
|
|
128
|
+
* 幂等性:只做「读文件 → 按三态提交缓存」单向赋值,无时序状态,重复调用收敛到
|
|
129
|
+
* 同一结果。三态语义(failed 保持缓存、静默回落 DEFAULT 是旧缺陷——读失败曾把
|
|
130
|
+
* 好缓存打回缺省且调用方无法感知):ok/absent 覆盖、failed 保持并携带原因。
|
|
131
|
+
*/
|
|
132
|
+
reloadGlobalConfig(): GlobalConfigReadResult {
|
|
133
|
+
return this.applyGlobalConfig(readGlobalConfig(this.agentRegistryDir));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 刷新主 agent model 缓存。model_select 事件时调用。
|
|
138
|
+
* renderCall 的 resolveModel 读此缓存以显示标题行 model。
|
|
139
|
+
*/
|
|
140
|
+
setCtxModel(model: ModelInfo | undefined): void {
|
|
141
|
+
this._ctxModel = model;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ── 模型解析(SubagentService.execute 内部调)──────────────
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 解析 agent 的模型(三层:override → agentConfig → 主 agent model)。
|
|
148
|
+
*
|
|
149
|
+
* @param agentRef agent 引用(.md 绝对路径;查 agentConfig 的 model override)
|
|
150
|
+
* @param override 调用方显式 override(最高优先级)
|
|
151
|
+
* @param ctxModel 主 agent 当前模型(兜底,直接透传)
|
|
152
|
+
*/
|
|
153
|
+
resolveModel(
|
|
154
|
+
agentRef: string,
|
|
155
|
+
override?: { model?: string; thinkingLevel?: string },
|
|
156
|
+
ctxModel?: ModelInfo,
|
|
157
|
+
/** 已解析的 agent 配置(调用方已加载时复用,避免同一 agentRef 二次 loadByPath)。 */
|
|
158
|
+
agentConfig?: AgentConfig,
|
|
159
|
+
): ResolvedModel {
|
|
160
|
+
this.assertReady();
|
|
161
|
+
const config = agentConfig ?? (agentRef ? this.agentRegistry.loadByPath(agentRef) : undefined);
|
|
162
|
+
// ctxModel 优先用显式传入(execute 路径),其次用 session 缓存(renderCall 路径)
|
|
163
|
+
return resolveModel(config, this.modelRegistry!, override, ctxModel ?? this._ctxModel);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 查询 agent 配置(SubagentService 内部判定 defaultBackground 用)。
|
|
167
|
+
* undefined = 合法缺省语义(未点名 / 默认 general-purpose 形态)。 */
|
|
168
|
+
getAgentConfig(agentRef?: string): AgentConfig | undefined {
|
|
169
|
+
return agentRef ? this.agentRegistry.loadByPath(agentRef) : undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 查询 agent 配置——显式 ref 失败即 throw(SubagentService.resolveIdentity 用)。
|
|
174
|
+
*
|
|
175
|
+
* 与 getAgentConfig 的语义分界(「用户显式点名」vs「默认 general-purpose」):
|
|
176
|
+
* 用户显式点名的 agentRef(工具 agent 参数 / workflow agent({agent}) opts)解析
|
|
177
|
+
* 失败 = 配置错误,必须显式报错——错误文案含 <available_subagents> 恢复指引
|
|
178
|
+
* (对齐 workflow name not found 反馈风格),不允许静默降级为无配置
|
|
179
|
+
* general-purpose 形态(systemPrompt/工具白名单全丢且零反馈)。默认形态
|
|
180
|
+
* (不传 agent)走 getAgentConfig:undefined = 合法缺省,走 override → ctxModel 兑底。
|
|
181
|
+
*/
|
|
182
|
+
getRequiredAgentConfig(agentRef: string): AgentConfig {
|
|
183
|
+
return this.agentRegistry.loadByPath(agentRef, true);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ── 配置读取(subagent-service 调)────────────────────────
|
|
187
|
+
|
|
188
|
+
/** 全局配置深拷贝(调用方拿到副本,改不影响 Service 内部)。 */
|
|
189
|
+
getGlobalConfig(): SubagentsGlobalConfig {
|
|
190
|
+
return structuredClone(this.globalConfig);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 内部:session id 缓存(initModel 注入;当前无消费者,保留供未来 session 作用域需求)。 */
|
|
194
|
+
get sessionId(): string | undefined {
|
|
195
|
+
return this._sessionId;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** agent 配置目录(SubagentService 构造 store/SessionRunnerContext 时读)。 */
|
|
199
|
+
getAgentDir(): string {
|
|
200
|
+
return this.agentRegistryDir;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** modelRegistry(SubagentService 构造 factoryCtx 时读)。已注入保证非 null。 */
|
|
204
|
+
getModelRegistry(): ModelRegistryLike {
|
|
205
|
+
if (this.modelRegistry === null) {
|
|
206
|
+
throw new Error("modelRegistry not injected (initModel not called?)");
|
|
207
|
+
}
|
|
208
|
+
return this.modelRegistry;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// ── 内部 ────────────────────────────────────────────────
|
|
212
|
+
|
|
213
|
+
/** 校验 modelRegistry 已注入。 */
|
|
214
|
+
private assertReady(): void {
|
|
215
|
+
if (this.modelRegistry === null) {
|
|
216
|
+
throw new Error("modelRegistry not injected (initModel not called?)");
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ============================================================
|
|
222
|
+
// 进程单例访问器
|
|
223
|
+
// ============================================================
|
|
224
|
+
|
|
225
|
+
// 用 globalThis[Symbol.for] 持有进程单例,避免 jiti 因路径字符串不同加载多份模块
|
|
226
|
+
// 导致单例分裂(详见 docs/standards.md §7.5)。
|
|
227
|
+
const MODEL_SERVICE_SLOT_KEY = Symbol.for("@zhushanwen/pi-subagents.model-service");
|
|
228
|
+
|
|
229
|
+
type ModelServiceSlot = { current: ModelConfigService | null };
|
|
230
|
+
|
|
231
|
+
function getModelServiceSlot(): ModelServiceSlot {
|
|
232
|
+
// globalThis 无 symbol 索引签名,但运行时支持 symbol 键——用 Reflect 安全读写,
|
|
233
|
+
// 避免双重断言。ModelServiceSlot 是运行时保证的固定形状(同文件唯一写入点)。
|
|
234
|
+
let slot = Reflect.get(globalThis, MODEL_SERVICE_SLOT_KEY) as ModelServiceSlot | undefined;
|
|
235
|
+
if (!slot) {
|
|
236
|
+
slot = { current: null };
|
|
237
|
+
Reflect.set(globalThis, MODEL_SERVICE_SLOT_KEY, slot);
|
|
238
|
+
}
|
|
239
|
+
return slot;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** 获取进程单例。session_start 前为 null。 */
|
|
243
|
+
export function getModelConfigService(): ModelConfigService | null {
|
|
244
|
+
return getModelServiceSlot().current;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** 设置进程单例(session_start 首次创建时)。 */
|
|
248
|
+
export function setModelConfigService(service: ModelConfigService): void {
|
|
249
|
+
getModelServiceSlot().current = service;
|
|
250
|
+
}
|