@zhushanwen/subagent-core 0.5.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/chunk-KXVBIVOU.js +16 -0
- package/dist/chunk-T2SYBW3I.js +24 -0
- package/dist/chunk-VURUAGMM.js +2305 -0
- package/dist/{types-Dv4QhSJ_.d.cts → engine-discovery-scan-ocpM8FmI.d.cts} +835 -668
- package/dist/{types-Dv4QhSJ_.d.ts → engine-discovery-scan-ocpM8FmI.d.ts} +835 -668
- package/dist/execution/engine/engine-discovery-scan.cjs +2210 -0
- package/dist/execution/engine/engine-discovery-scan.d.cts +4 -0
- package/dist/execution/engine/engine-discovery-scan.d.ts +4 -0
- package/dist/execution/engine/engine-discovery-scan.js +20 -0
- package/dist/execution/engine/paths.cjs +6 -23
- package/dist/execution/engine/paths.d.cts +1 -8
- package/dist/execution/engine/paths.d.ts +1 -8
- package/dist/execution/engine/paths.js +1 -1
- package/dist/execution/relay-env.cjs +12 -28
- package/dist/execution/relay-env.d.cts +1 -33
- package/dist/execution/relay-env.d.ts +1 -33
- package/dist/execution/relay-env.js +5 -3
- package/dist/index.cjs +14123 -15467
- package/dist/index.d.cts +1179 -1540
- package/dist/index.d.ts +1179 -1540
- package/dist/index.js +7694 -10986
- package/dist.bundle/index.cjs +33485 -0
- package/package.json +20 -26
- package/src/__tests__/record-store-last-line.test.ts +16 -7
- package/src/core/error-message.ts +7 -6
- package/src/core/host-services.ts +8 -2
- package/src/core/notify-ports.ts +17 -2
- package/src/execution/__tests__/agent-registry.test.ts +1 -1
- package/src/execution/__tests__/alive-store.test.ts +1 -1
- package/src/execution/__tests__/chat-engine-routing.test.ts +62 -28
- package/src/execution/__tests__/chat-round-first-round-watchdog.test.ts +255 -0
- package/src/execution/__tests__/collect-budget.test.ts +291 -0
- package/src/execution/__tests__/collect-coordinator-service.test.ts +316 -0
- package/src/execution/__tests__/collect-coordinator.test.ts +371 -0
- package/src/execution/__tests__/collect-mixed-dispatch.test.ts +229 -0
- package/src/execution/__tests__/config-collect-sync.test.ts +120 -0
- package/src/execution/__tests__/config.test.ts +1 -1
- package/src/execution/__tests__/delivery-methods.test.ts +179 -237
- package/src/execution/__tests__/execute-and-await-worktree.test.ts +0 -2
- package/src/execution/__tests__/execute-nesting.test.ts +20 -13
- package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +21 -34
- package/src/execution/__tests__/finalized-marker.test.ts +1 -1
- package/src/execution/__tests__/gc-timer.test.ts +8 -23
- package/src/execution/__tests__/get-record-for-action-restart.test.ts +20 -10
- package/src/execution/__tests__/helpers/fake-engine-port.ts +187 -0
- package/src/execution/__tests__/helpers/spawn-mock.ts +36 -9
- package/src/execution/__tests__/helpers/subagent-service-mocks.ts +1 -1
- package/src/execution/__tests__/idle-gc.test.ts +200 -0
- package/src/execution/__tests__/lifecycle-predicates.test.ts +26 -21
- package/src/execution/__tests__/manifest-store-tmp-recovery.test.ts +1 -1
- package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +5 -2
- package/src/execution/__tests__/notify-batch.test.ts +500 -0
- package/src/execution/__tests__/notify-ledger.test.ts +27 -0
- package/src/execution/__tests__/reconcile-sweep.test.ts +200 -0
- package/src/execution/__tests__/record-entry-collect.test.ts +102 -0
- package/src/execution/__tests__/record-store.test.ts +60 -6
- package/src/execution/__tests__/recursive-visibility-baseline.test.ts +20 -13
- package/src/execution/__tests__/relay-env.test.ts +12 -1
- package/src/execution/__tests__/round-supervisor.test.ts +293 -0
- package/src/execution/__tests__/session-file-gc.test.ts +1 -1
- package/src/execution/__tests__/session-pending.test.ts +119 -17
- package/src/execution/__tests__/start-collect-guard.test.ts +255 -0
- package/src/execution/__tests__/stream-sink-retirement.test.ts +10 -10
- package/src/execution/__tests__/subagent-actions-core.test.ts +6 -0
- package/src/execution/__tests__/subagent-service-multiproc-guard.test.ts +14 -11
- package/src/execution/__tests__/subagent-service-notify-gate.test.ts +33 -30
- package/src/execution/__tests__/subagent-service-parent-guard.test.ts +2 -9
- package/src/execution/__tests__/subagent-service-recovery-bounds.test.ts +59 -129
- package/src/execution/__tests__/subagent-service.test.ts +2 -2
- package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +109 -7
- package/src/execution/__tests__/subprocess-agent-runner.test.ts +108 -36
- package/src/execution/__tests__/sync-collect-recovery.test.ts +1102 -0
- package/src/execution/__tests__/tombstone-store.test.ts +1 -1
- package/src/execution/__tests__/workflow-state-root.test.ts +232 -0
- package/src/execution/__tests__/worktree-git-ops.test.ts +1 -1
- package/src/execution/__tests__/worktree-reconcile-aging.test.ts +2 -2
- package/src/execution/__tests__/worktree-reconcile.integration.test.ts +1 -1
- package/src/execution/__tests__/worktree-registry.test.ts +1 -1
- package/src/execution/agent-registry.ts +6 -2
- package/src/execution/collect-coordinator.ts +200 -0
- package/src/execution/config.ts +77 -5
- package/src/execution/dialog-queue.ts +6 -73
- package/src/execution/engine/__tests__/common/capability-gate.test.ts +115 -15
- package/src/execution/engine/__tests__/common/errors.test.ts +11 -18
- package/src/execution/engine/__tests__/common/event-journal.test.ts +1 -1
- package/src/execution/engine/__tests__/common/kill-chain.test.ts +7 -0
- package/src/execution/engine/__tests__/common/nesting-guard.test.ts +9 -3
- package/src/execution/engine/__tests__/common/pool-manager.test.ts +2 -2
- package/src/execution/engine/__tests__/common/session-view-service.test.ts +90 -3
- package/src/execution/engine/__tests__/conformance/H9-test-disposition.md +116 -0
- package/src/execution/engine/__tests__/conformance/__fixtures__/engine-protocol/fake-engine-protocol.mjs +377 -0
- package/src/execution/engine/__tests__/conformance/__fixtures__/engine-protocol/recorded.fixture.json +331 -0
- package/src/execution/engine/__tests__/conformance/__fixtures__/engine-protocol/smoke-run.fixture.json +213 -0
- package/src/execution/engine/__tests__/conformance/chat-round-protocol.test.ts +230 -0
- package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +69 -42
- package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +4 -81
- package/src/execution/engine/__tests__/conformance/contract.probe.test.ts +67 -44
- package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +82 -55
- package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +133 -70
- package/src/execution/engine/__tests__/conformance/engine-crash.test.ts +143 -0
- package/src/execution/engine/__tests__/conformance/fake-engine-capabilities.ts +19 -0
- package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +8 -3
- package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +6 -5
- package/src/execution/engine/__tests__/conformance/protocol-blackbox.test.ts +398 -0
- package/src/execution/engine/__tests__/conformance/registry-fork-filter.test.ts +221 -0
- package/src/execution/engine/__tests__/conformance/round-liveness-supervisor.test.ts +302 -0
- package/src/execution/engine/__tests__/conformance/static-spawn-guard.test.ts +58 -0
- package/src/execution/engine/__tests__/engine-discovery-scan.test.ts +558 -0
- package/src/execution/engine/__tests__/host-task-spec.test.ts +45 -0
- package/src/execution/engine/__tests__/model-ref-split.test.ts +74 -0
- package/src/execution/engine/__tests__/registry.test.ts +206 -1
- package/src/execution/engine/__tests__/routing.test.ts +97 -0
- package/src/execution/engine/__tests__/run-failure-worktree-cleanup.test.ts +97 -0
- package/src/execution/engine/client/__tests__/__fixtures__/fake-engine.mjs +294 -0
- package/src/execution/engine/client/__tests__/engine-client.test.ts +516 -0
- package/src/execution/engine/client/__tests__/mirror.test.ts +67 -0
- package/src/execution/engine/client/__tests__/pid-file.test.ts +257 -0
- package/src/execution/engine/client/__tests__/protocol-closure.test.ts +95 -0
- package/src/execution/engine/client/__tests__/remote-engine.test.ts +457 -0
- package/src/execution/engine/client/client-options.ts +103 -0
- package/src/execution/engine/client/engine-client.ts +687 -0
- package/src/execution/engine/client/mirror.ts +167 -0
- package/src/execution/engine/client/pid-file.ts +392 -0
- package/src/execution/engine/client/reaper.ts +95 -0
- package/src/execution/engine/client/remote-engine.ts +441 -0
- package/src/execution/engine/client/reverse-router.ts +255 -0
- package/src/execution/engine/common/capability-gate.ts +109 -22
- package/src/execution/engine/common/errors.ts +9 -25
- package/src/execution/engine/common/event-journal.ts +4 -50
- package/src/execution/engine/common/journal-replay.ts +3 -1
- package/src/execution/engine/common/kill-chain.ts +18 -251
- package/src/execution/engine/common/nesting-guard.ts +18 -114
- package/src/execution/engine/common/pool-manager.ts +4 -0
- package/src/execution/engine/common/session-view-service.ts +56 -84
- package/src/execution/engine/config.ts +151 -0
- package/src/execution/engine/d8-compat.ts +346 -0
- package/src/execution/engine/engine-discovery-roots.ts +107 -0
- package/src/execution/engine/engine-discovery-scan.ts +269 -0
- package/src/execution/engine/engine-discovery.ts +30 -7
- package/src/execution/engine/engine-inspect-package.ts +282 -0
- package/src/execution/engine/engine-manifest.ts +244 -0
- package/src/execution/engine/host/__tests__/host-askuser-endpoint.test.ts +123 -0
- package/src/execution/engine/host/__tests__/host-bridge.test.ts +135 -0
- package/src/execution/engine/host/__tests__/spawned-children.test.ts +73 -0
- package/src/execution/engine/host/host-bridge.ts +150 -0
- package/src/execution/engine/host/host-ui-endpoint.ts +47 -0
- package/src/execution/engine/host/pi-host-binding.ts +91 -0
- package/src/execution/engine/host/spawned-children.ts +139 -0
- package/src/execution/engine/host-task-spec.ts +29 -2
- package/src/execution/engine/model-validation.ts +55 -2
- package/src/execution/engine/paths.ts +16 -41
- package/src/execution/engine/port.ts +48 -2
- package/src/execution/engine/registry.ts +153 -34
- package/src/execution/engine/routing.ts +225 -52
- package/src/execution/engine/types.ts +25 -200
- package/src/execution/execution-record.ts +7 -0
- package/src/execution/finalize-record.ts +40 -11
- package/src/execution/idle-gc.ts +89 -14
- package/src/execution/lifecycle-manager.ts +10 -7
- package/src/execution/lifecycle-predicates.ts +10 -8
- package/src/execution/notifier.ts +234 -25
- package/src/execution/notify-host.ts +21 -1
- package/src/execution/record-entry.ts +16 -0
- package/src/execution/record-store.ts +189 -31
- package/src/execution/relay-env.ts +19 -36
- package/src/execution/round-supervisor/domain.ts +89 -0
- package/src/execution/round-supervisor/index.ts +33 -0
- package/src/execution/round-supervisor/notify-accounting.ts +87 -0
- package/src/execution/round-supervisor/reconcile-sweep.ts +235 -0
- package/src/execution/round-supervisor/service-binding.test.ts +661 -0
- package/src/execution/round-supervisor/service-binding.ts +273 -0
- package/src/execution/round-supervisor/supervisor.ts +426 -0
- package/src/execution/session-pending.ts +68 -74
- package/src/execution/settled-watchdog.ts +39 -0
- package/src/execution/subagent-actions-core.ts +62 -6
- package/src/execution/subagent-service.ts +1161 -406
- package/src/execution/subprocess-agent-runner.ts +16 -8
- package/src/execution/sync-rebuild.ts +87 -0
- package/src/execution/types.ts +104 -117
- package/src/execution/ui-channels.ts +13 -214
- package/src/execution/ui-request-handler-factory.ts +7 -4
- package/src/execution/ui-request-observability.ts +2 -2
- package/src/execution/workflow-state-root.ts +67 -0
- package/src/execution/worktree-git-ops.ts +5 -0
- package/src/execution/worktree-manager.ts +11 -1
- package/src/index.ts +55 -26
- package/src/orchestration/__tests__/config-loader.test.ts +1 -1
- package/src/orchestration/__tests__/execute-agent-call.test.ts +1 -1
- package/src/orchestration/__tests__/file-run-store-prune.test.ts +1 -1
- package/src/orchestration/__tests__/file-run-store-throttle.test.ts +1 -1
- package/src/orchestration/__tests__/review-fix-loop-scriptpath-failfast.test.ts +1 -1
- package/src/orchestration/__tests__/script-generate.test.ts +5 -5
- package/src/orchestration/__tests__/skill-discovery.test.ts +2 -2
- package/src/orchestration/__tests__/workflow-files.test.ts +10 -10
- package/src/orchestration/__tests__/workflow-script-registry-impl.test.ts +1 -1
- package/src/orchestration/execute-agent-call.ts +1 -1
- package/src/orchestration/file-run-store.ts +144 -59
- package/src/orchestration/models/types.ts +43 -42
- package/src/shared/__tests__/atomic-write.test.ts +1 -1
- package/src/shared/__tests__/resource-discovery-host-roots.test.ts +5 -5
- package/src/shared/zcode-model-ref.ts +31 -0
- package/dist/chunk-43ONBFZX.js +0 -240
- package/dist/chunk-A4IVZWVX.js +0 -31
- package/dist/chunk-APZY4IME.js +0 -27
- package/dist/chunk-V3VHZ2VX.js +0 -59
- package/dist/execution/engine/engines/zcode/constants.cjs +0 -107
- package/dist/execution/engine/engines/zcode/constants.d.cts +0 -135
- package/dist/execution/engine/engines/zcode/constants.d.ts +0 -135
- package/dist/execution/engine/engines/zcode/constants.js +0 -54
- package/dist/execution/engine/engines/zcode/reader.cjs +0 -276
- package/dist/execution/engine/engines/zcode/reader.d.cts +0 -21
- package/dist/execution/engine/engines/zcode/reader.d.ts +0 -21
- package/dist/execution/engine/engines/zcode/reader.js +0 -9
- package/src/__tests__/append-system-prompt-assembly.test.ts +0 -243
- package/src/__tests__/fr4-get-state-handshake.test.ts +0 -125
- package/src/__tests__/session-runner.test.ts +0 -69
- package/src/execution/__tests__/ask-user-transit-e2e.test.ts +0 -490
- package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +0 -191
- package/src/execution/__tests__/descendant-sweep-guards.test.ts +0 -220
- package/src/execution/__tests__/descendant-sweep.test.ts +0 -269
- package/src/execution/__tests__/engine-model-validation.test.ts +0 -741
- package/src/execution/__tests__/epipe-fallback.test.ts +0 -248
- package/src/execution/__tests__/execution-runtime-face.test.ts +0 -272
- package/src/execution/__tests__/get-state-handshake.test.ts +0 -127
- package/src/execution/__tests__/helpers/session-runner-mocks.ts +0 -113
- package/src/execution/__tests__/keep-alive-no-progress.test.ts +0 -280
- package/src/execution/__tests__/kill-all-escalation.test.ts +0 -196
- package/src/execution/__tests__/max-turns-to-watchdog-ms.test.ts +0 -113
- package/src/execution/__tests__/output-collector.test.ts +0 -469
- package/src/execution/__tests__/pi-invocation.test.ts +0 -134
- package/src/execution/__tests__/recursive-visibility-env.test.ts +0 -149
- package/src/execution/__tests__/rpc-mode.test.ts +0 -89
- package/src/execution/__tests__/run-and-finalize-anchoring.test.ts +0 -387
- package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +0 -277
- package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +0 -254
- package/src/execution/__tests__/run-spawn-edges.test.ts +0 -1079
- package/src/execution/__tests__/run-spawn-integration.test.ts +0 -933
- package/src/execution/__tests__/run-spawn-resume.test.ts +0 -322
- package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +0 -196
- package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +0 -207
- package/src/execution/__tests__/service-kill-escalation.test.ts +0 -91
- package/src/execution/__tests__/session-runner-branch-cache-lru.test.ts +0 -110
- package/src/execution/__tests__/session-runner-close-prune.test.ts +0 -181
- package/src/execution/__tests__/session-runner-dispatch.test.ts +0 -489
- package/src/execution/__tests__/session-runner-epipe.test.ts +0 -179
- package/src/execution/__tests__/session-runner-heartbeat-idle-fallback.test.ts +0 -227
- package/src/execution/__tests__/session-runner-lifecycle-helpers.test.ts +0 -290
- package/src/execution/__tests__/session-runner-schema-env.test.ts +0 -352
- package/src/execution/__tests__/settled-watchdog.test.ts +0 -524
- package/src/execution/__tests__/spawn-args.test.ts +0 -446
- package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +0 -192
- package/src/execution/__tests__/spawn-event-adapter.test.ts +0 -167
- package/src/execution/__tests__/spawn-worktree-guidance.test.ts +0 -208
- package/src/execution/__tests__/spawned-children.test.ts +0 -236
- package/src/execution/__tests__/start-sync-model-guard.test.ts +0 -150
- package/src/execution/__tests__/stdin-writer.test.ts +0 -464
- package/src/execution/__tests__/subagent-service-message-close.test.ts +0 -693
- package/src/execution/__tests__/temp-prompt.test.ts +0 -53
- package/src/execution/__tests__/timeout-integration.test.ts +0 -616
- package/src/execution/__tests__/turn-limiter-semantics.test.ts +0 -194
- package/src/execution/__tests__/turn-limiter.test.ts +0 -65
- package/src/execution/__tests__/ui-request-handler.test.ts +0 -205
- package/src/execution/__tests__/ui-request-queue.test.ts +0 -298
- package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +0 -230
- package/src/execution/engine/__tests__/common/schema-emulation.test.ts +0 -128
- package/src/execution/engine/__tests__/common/session-view-service-zcode-dbpath.test.ts +0 -149
- package/src/execution/engine/__tests__/conformance/__fixtures__/pi-golden-events.json +0 -28
- package/src/execution/engine/__tests__/conformance/zcode-appserver-harness.ts +0 -128
- package/src/execution/engine/__tests__/paths.test.ts +0 -39
- package/src/execution/engine/common/schema-emulation.ts +0 -189
- package/src/execution/engine/common/session-view-projection.ts +0 -51
- package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +0 -615
- package/src/execution/engine/engines/pi/__tests__/reader.test.ts +0 -155
- package/src/execution/engine/engines/pi/__tests__/spawn-opts-direct.test.ts +0 -257
- package/src/execution/engine/engines/pi/argv-mirror.ts +0 -135
- package/src/execution/engine/engines/pi/get-state-handshake.ts +0 -179
- package/src/execution/engine/engines/pi/output-collector.ts +0 -317
- package/src/execution/engine/engines/pi/pi-engine.ts +0 -811
- package/src/execution/engine/engines/pi/pi-invocation.ts +0 -120
- package/src/execution/engine/engines/pi/reader.ts +0 -59
- package/src/execution/engine/engines/pi/registration.ts +0 -37
- package/src/execution/engine/engines/pi/session-runner.ts +0 -2840
- package/src/execution/engine/engines/pi/spawn-event-adapter.ts +0 -404
- package/src/execution/engine/engines/pi/stdin-writer.ts +0 -199
- package/src/execution/engine/engines/pi/temp-prompt.ts +0 -62
- package/src/execution/engine/engines/pi/turn-limiter.ts +0 -102
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/fake-appserver.mjs +0 -301
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-appserver.json +0 -36
- package/src/execution/engine/engines/zcode/__tests__/appserver-launcher.test.ts +0 -321
- package/src/execution/engine/engines/zcode/__tests__/connection.test.ts +0 -473
- package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +0 -43
- package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +0 -124
- package/src/execution/engine/engines/zcode/__tests__/reader.test.ts +0 -210
- package/src/execution/engine/engines/zcode/__tests__/registration.test.ts +0 -71
- package/src/execution/engine/engines/zcode/__tests__/session-channel-dispose-harvest.test.ts +0 -247
- package/src/execution/engine/engines/zcode/__tests__/session-channel-turn-timers.test.ts +0 -451
- package/src/execution/engine/engines/zcode/__tests__/session-channel.test.ts +0 -792
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-appserver.test.ts +0 -782
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-degrade.test.ts +0 -168
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-dispose.test.ts +0 -210
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-retry.test.ts +0 -353
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-status.test.ts +0 -282
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-timeout.test.ts +0 -372
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +0 -145
- package/src/execution/engine/engines/zcode/appserver-launcher.ts +0 -167
- package/src/execution/engine/engines/zcode/connection.ts +0 -598
- package/src/execution/engine/engines/zcode/constants.ts +0 -176
- package/src/execution/engine/engines/zcode/golden-sample.ts +0 -47
- package/src/execution/engine/engines/zcode/parser.ts +0 -102
- package/src/execution/engine/engines/zcode/preparer.ts +0 -235
- package/src/execution/engine/engines/zcode/reader.ts +0 -381
- package/src/execution/engine/engines/zcode/registration.ts +0 -37
- package/src/execution/engine/engines/zcode/session-channel.ts +0 -968
- package/src/execution/engine/engines/zcode/zcode-engine.ts +0 -1431
- package/src/execution/round-settlement.ts +0 -93
- package/src/execution/ui-request-queue.ts +0 -215
- package/src/shared/schema-env.ts +0 -44
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
// src/execution/engine/engines/zcode/constants.ts
|
|
2
|
-
//
|
|
3
|
-
// ZcodeEngine 共享常量(P3)。独立成模块的原因:reader.ts 是双端复用的无状态模块
|
|
4
|
-
// (设计 §3.3.7:runtime 侧也会 import),它需要的常量不能连带其他模块的模块级
|
|
5
|
-
// 副作用(如 logger 初始化)一起加载——constants.ts 保持零 import 纯常量。
|
|
6
|
-
//
|
|
7
|
-
// 2026-09 重构(共享宿主 HOME):删除 CLI spawn 降级链与 HOME 池化全部常量
|
|
8
|
-
// (home-appserver 池/锁/pidfile/派生上限/凭据引导/probe 冒烟/漂移降级码)。
|
|
9
|
-
// 引擎只走 app-server RPC,spawn env 不覆写 HOME(共享宿主 ~/.zcode/)。
|
|
10
|
-
|
|
11
|
-
/** zcode 引擎的 registry key。 */
|
|
12
|
-
export const ZCODE_ENGINE_ID = "zcode";
|
|
13
|
-
|
|
14
|
-
/** zcode 适配器版本(handle.adapterVersion 数据源——golden 样本对齐排查锚点)。 */
|
|
15
|
-
export const ZCODE_ADAPTER_VERSION = "2.0.0";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* zcode CLI 缺省路径(12.4MB node bundle,不在 PATH——2026-08-25 实测)。
|
|
19
|
-
* 组合根可用 deps.cliPath 覆盖(测试注入 / 未来安装形态变化)。
|
|
20
|
-
*/
|
|
21
|
-
export const ZCODE_CLI_DEFAULT_PATH = "/Applications/ZCode.app/Contents/Resources/glm/zcode.cjs";
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 唯一凭据源 config(桌面端登录态,zsub V2_CONFIG_PATH 同构)。
|
|
25
|
-
* 为什么绝不读 ~/.zcode/cli/config.json 作为引擎侧凭据校验源:它不在 ZCode GUI
|
|
26
|
-
* 管理面,可能残留历史验证配置(2026-08-25 事故:8/24 zsub 开发残留把默认模型
|
|
27
|
-
* 劫持到失效 router 端点,turn 0 即 401);桌面端登录凭据落在 v2/config.json,
|
|
28
|
-
* 是 zsub 生产验证过的凭据源。共享宿主 HOME 后引擎侧只读它做模型解析校验;
|
|
29
|
-
* app-server 进程侧 CLI 只认 ~/.zcode/cli/config.json(GUI 从不写该文件),由
|
|
30
|
-
* appserver-launcher 的 fs 拦截 wrapper 把对该文件的读取重定向为「真实文件 +
|
|
31
|
-
* v2 provider 注入」内存合并(同 id 时 v2 整条优先)——凭据实际供数仍以 v2 为
|
|
32
|
-
* 权威源,并非直接消费宿主 HOME 的原始 cli config。
|
|
33
|
-
*/
|
|
34
|
-
export const ZCODE_V2_CONFIG_PATH_SUFFIX = [".zcode", "v2", "config.json"] as const;
|
|
35
|
-
|
|
36
|
-
/** zsub 同构的兜底缺省模型(v2 config 无 model.main 且 task 未指定时)。 */
|
|
37
|
-
export const ZCODE_FALLBACK_DEFAULT_MODEL = "builtin:bigmodel-coding-plan/GLM-5.3";
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 宿主 HOME 下 zcode 会话 db 的相对位置(共享 HOME 形态的 handle.sessionRef.dbPath
|
|
41
|
-
* 锚点——绝对路径 = join(os.homedir(), ...suffix);runtime 读侧用它做精确白名单,
|
|
42
|
-
* 旧池时代 records 的相对 dbPath 仍按 poolKey 锚定兼容)。
|
|
43
|
-
*/
|
|
44
|
-
export const ZCODE_HOST_DB_SUFFIX = [".zcode", "cli", "db", "db.sqlite"] as const;
|
|
45
|
-
|
|
46
|
-
/** 杀链 grace 窗口:SIGTERM 后等这么久再 SIGKILL(zsub 同构 5s;实测 SIGTERM→exit 仅 103ms)。 */
|
|
47
|
-
export const ZCODE_KILL_GRACE_MS = 5_000;
|
|
48
|
-
|
|
49
|
-
/** 错误信息里保留的 stdout 尾部长度(engine_run_failed 规格:够诊断、不刷屏)。 */
|
|
50
|
-
export const ZCODE_ERROR_TAIL_CHARS = 2000;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* [R2] app-server 控制面请求默认超时(ms)。依据:旧 zsw 实现 REQUEST_TIMEOUT_MS=15s
|
|
54
|
-
* (60+ fake-server 用例 + 真机 e2e 验证值)。
|
|
55
|
-
*/
|
|
56
|
-
export const ZCODE_APPSERVER_REQUEST_TIMEOUT_MS = 15_000;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* [R2] 连接崩溃错误信息附带的 stderr 尾部长度(字符)。设计 zcode-engine-appserver-
|
|
60
|
-
* resident.md §3.1 失败路径 2 / §3.3 错误规格表「进程意外退出」行:stderr 尾 400 字符。
|
|
61
|
-
*/
|
|
62
|
-
export const ZCODE_APPSERVER_STDERR_TAIL_CHARS = 400;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* [R2] stderr 滚动缓冲上限(字符):只保最近内容供崩溃诊断。旧 zsw 实现同值 2048。
|
|
66
|
-
*/
|
|
67
|
-
export const ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS = 2048;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* [R3] 终态判定后 session/read 兜底拉取的超时(ms)。read 是全文权威来源(不变量 1
|
|
71
|
-
* 的双来源之二),失败不抛——降级收尾帧/delta 聚合。旧 zsw 实现 READ_TIMEOUT_MS=5000。
|
|
72
|
-
*/
|
|
73
|
-
export const ZCODE_APPSERVER_TURN_READ_TIMEOUT_MS = 5_000;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* [R3] 任务收尾 session/close 的控制面超时(ms,best-effort——失败只 warn 不抛)。
|
|
77
|
-
* 旧 zsw 实现 RELEASE_CLOSE_TIMEOUT_MS=1500(wave2 D2,同值同语义)。
|
|
78
|
-
*/
|
|
79
|
-
export const ZCODE_APPSERVER_TURN_CLOSE_TIMEOUT_MS = 1_500;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* [R3 → superseded by P0-1] 旧「一轮终态等待」固定墙钟缺省值(ms)。**已被
|
|
83
|
-
* `ZCODE_TURN_IDLE_TIMEOUT_MS`(idle 主判定)+ `ZCODE_TURN_MAX_TIMEOUT_MS`
|
|
84
|
-
* (总上界兜底)两 timer 语义替换,session-channel 不再消费本值**——固定墙钟
|
|
85
|
-
* 「到点=不可推进」判定被 2026-09 T001 深诊击穿(21% 活跃任务被误杀,见设计
|
|
86
|
-
* timeout-zcode-turn-and-settled-watchdog.md §3.1/§4)。符号保留:audit/设计
|
|
87
|
-
* 文档(timeout-audit-2026-09.md 等)以本名记录事故成因,删除即产生悬空引用。
|
|
88
|
-
*/
|
|
89
|
-
export const ZCODE_APPSERVER_TURN_DEFAULT_TIMEOUT_MS = 300_000;
|
|
90
|
-
|
|
91
|
-
// ============================================================
|
|
92
|
-
// [P0-1 U1] turn 等待两 timer(idle 主判定 + 总上界回收兜底)
|
|
93
|
-
// 设计权威源:docs/design/timeout-zcode-turn-and-settled-watchdog.md §6 D1/D2
|
|
94
|
-
// ============================================================
|
|
95
|
-
|
|
96
|
-
/** turn idle 主判定的 env 覆盖通道(>0 覆盖、≤0 关闭、非法值 warn+回落默认)。 */
|
|
97
|
-
export const ZCODE_TURN_IDLE_TIMEOUT_ENV = "XYZ_ZCODE_TURN_IDLE_TIMEOUT_MS";
|
|
98
|
-
|
|
99
|
-
/** turn 总上界的 env 覆盖通道(语义同上;0=关闭后 chatty-wedge 无自动回收)。 */
|
|
100
|
-
export const ZCODE_TURN_MAX_TIMEOUT_ENV = "XYZ_ZCODE_TURN_MAX_TIMEOUT_MS";
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* [P0-1 D1] turn idle 主判定缺省阈值(ms):该 turn 任何事件到达刷新计时,连续
|
|
104
|
-
* 静默达此值判「执行已不可推进」。活跃事件流零误杀(ADR-0047 逆否面)。默认
|
|
105
|
-
* 30min 为先验值,⛔P-Z1 门(事件流 inter-event gap 分布)标定前用此默认。
|
|
106
|
-
*/
|
|
107
|
-
export const ZCODE_TURN_IDLE_TIMEOUT_MS = 1_800_000;
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* [P0-1 D1] turn 总上界缺省值(ms):从 openTurn 挂载起固定不刷新,兜 idle 覆盖
|
|
111
|
-
* 不了的 chatty-wedge(事件持续但终态永不到达)。对超上界的合法极长任务是显式
|
|
112
|
-
* 接受的残余误杀面(env 可调/可关 + 错误文案附自救指引)。默认 60min 为先验值
|
|
113
|
-
* (T001 34 任务最长 541s,先验远离 6.6×),⛔P-Z0 门(任务总时长分布)标定前
|
|
114
|
-
* 用此默认。
|
|
115
|
-
*/
|
|
116
|
-
export const ZCODE_TURN_MAX_TIMEOUT_MS = 3_600_000;
|
|
117
|
-
|
|
118
|
-
/** `parseZcodeTurnTimeoutEnv` 的解析结果(可判别联合——valid 分支 ms 必有)。 */
|
|
119
|
-
export type ZcodeTurnTimeoutEnvParse =
|
|
120
|
-
| { state: "unset" }
|
|
121
|
-
| { state: "valid"; ms: number }
|
|
122
|
-
| { state: "invalid" };
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* 解析 turn 阈值 env 原始值(空串视同未设置,对齐 lifecycle-manager 先例的
|
|
126
|
-
* `if (!raw)` 口径)。**与 `XYZ_SUBAGENT_IDLE_TIMEOUT_MS` 先例的刻意分歧(设计
|
|
127
|
-
* D2/r3 SG-5 显式登记)**:先例 ≤0=非法回落且禁用后不认 env;本通道 ≤0=显式
|
|
128
|
-
* 关闭该 timer(规则 19 的 opt-out 出路),非法(非数字)才回落默认——调用方
|
|
129
|
-
* 必须对 invalid 与 ≤0 关闭分别 warn 留痕(生效行为可见,A10① 断言依据)。
|
|
130
|
-
*/
|
|
131
|
-
export function parseZcodeTurnTimeoutEnv(
|
|
132
|
-
raw: string | undefined
|
|
133
|
-
): ZcodeTurnTimeoutEnvParse {
|
|
134
|
-
if (raw === undefined || raw === "") return { state: "unset" };
|
|
135
|
-
const parsed = Number(raw);
|
|
136
|
-
if (!Number.isFinite(parsed)) return { state: "invalid" };
|
|
137
|
-
return { state: "valid", ms: parsed };
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 共享宿主 HOME 语义下的 journal 分组 key(与 pi 引擎 PI_POOL_KEY='shared' 同构):
|
|
142
|
-
* journal 落 engineDataDir/engines/zcode/shared/journal-<taskId>.jsonl。无隔离池、
|
|
143
|
-
* 无派生目录——key 是固定字面量,仅作 pool-manager 通用契约的分组锚点。
|
|
144
|
-
*/
|
|
145
|
-
export const ZCODE_SHARED_POOL_KEY = "shared";
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* [R4 D3] abort 链第一级:session/stop 的控制面超时(ms)。stop 失败/超时即落
|
|
149
|
-
* killChain(协议此时已不可信)。
|
|
150
|
-
*/
|
|
151
|
-
export const ZCODE_APPSERVER_STOP_TIMEOUT_MS = 3_000;
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* [R4 D3] abort 链第二级:stop 送达后的终态确认 grace 窗口(ms)。窗口内终态
|
|
155
|
-
* 到达 → 不杀共享进程;超时 → killChain 连坐。
|
|
156
|
-
*/
|
|
157
|
-
export const ZCODE_APPSERVER_ABORT_GRACE_MS = 3_000;
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* [R4] app-server 内部错误码中「模型配置缺失」的归类判据(A.3:-32603 内部错误族,
|
|
161
|
-
* 消息含 "Model config is missing"——错误规格表第 2 行:报 engine_credential_missing)。
|
|
162
|
-
*/
|
|
163
|
-
export const ZCODE_APPSERVER_ERR_MODEL_CONFIG_MISSING = -32603;
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* [R5] app-server 内部错误码「会话忙」:send 时该会话已有轮在跑(busy 不排队不打断)。
|
|
167
|
-
* 单会话一任务是结构保证,运行中命中即 bug(错误文案引导上报 sessionId 与 state 流水)。
|
|
168
|
-
*/
|
|
169
|
-
export const ZCODE_APPSERVER_ERR_BUSY_SESSION = -32010;
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* [R5] 崩溃收割确认的兜底窗口(ms):killChain 在 `exit` 事件 resolve,而连接
|
|
173
|
-
* finalize(onClose → 在途 turn 崩溃收割)挂 `close` 事件——channel 退订前等 onClose
|
|
174
|
-
* 触发,本窗口兜底防 `close` 永不到达时 dispose 挂死。
|
|
175
|
-
*/
|
|
176
|
-
export const ZCODE_APPSERVER_HARVEST_GRACE_MS = 1_000;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
// src/execution/engine/engines/zcode/golden-sample.ts
|
|
2
|
-
//
|
|
3
|
-
// golden 语料内嵌副本(2026-09 收缩:CLI spawn 链删除后只剩 app-server 通道语料——
|
|
4
|
-
// spawn 单 JSON 样本(ZCODE_GOLDEN_STDOUT)随 probe 干跑/spawn 回归一并删除):
|
|
5
|
-
//
|
|
6
|
-
// app-server NDJSON 帧序列(R3,D8-②):常驻通道的 golden 帧序列语料,四类样本
|
|
7
|
-
// (create 应答 / 推送流 / 终态帧 / read 应答,附录 A.2 任务生命周期帧序)。
|
|
8
|
-
// 来源:按 A.2 协议权威 + 旧 zsw 实现真机抓包形态合成(非实录,fixture _meta
|
|
9
|
-
// 如实标注);真机冒烟后替换。fixture __tests__/__fixtures__/zcode-golden-appserver.json
|
|
10
|
-
// 是采集 SSOT,此处为运行时副本,更新样本需同步改两处(session-channel 测试
|
|
11
|
-
// diff 校验)。
|
|
12
|
-
|
|
13
|
-
// ============================================================
|
|
14
|
-
// [②app-server] NDJSON 帧序列语料(R3,附录 A.2 帧序逐字)
|
|
15
|
-
// ============================================================
|
|
16
|
-
|
|
17
|
-
/** 四类样本的结构(与 zcode-golden-appserver.json 的 frames 字段一一对应)。 */
|
|
18
|
-
export interface AppServerGoldenFrames {
|
|
19
|
-
/** ① session/create 应答帧(NDJSON 单行;projection.sessionId 恒 "unknown" 勿用)。 */
|
|
20
|
-
createResponse: string;
|
|
21
|
-
/** ④ 推送流帧(NDJSON 逐行:state.updated / stream.chunk 无文本 / session/event delta 有文本)。 */
|
|
22
|
-
pushStream: string[];
|
|
23
|
-
/** ⑤ 终态帧(NDJSON 逐行:v4 telemetry turn.terminal 权威 + session/event 收尾帧全文/usage)。 */
|
|
24
|
-
terminal: string[];
|
|
25
|
-
/** ⑥ session/read 应答帧(NDJSON 单行:messages[].parts[],text 全文 + step-finish tokens)。 */
|
|
26
|
-
readResponse: string;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** [②app-server] golden 帧序列内嵌副本(采集 SSOT = zcode-golden-appserver.json,双副本 diff 防漂移)。 */
|
|
30
|
-
export const ZCODE_APPSERVER_GOLDEN: AppServerGoldenFrames = {
|
|
31
|
-
createResponse:
|
|
32
|
-
'{"session":{"sessionId":"sess_golden_r3_01"},"projection":{"sessionId":"unknown","status":"idle","turnCount":0,"totalTokenCount":0,"contextUsed":0,"contextWindow":1000000}}',
|
|
33
|
-
pushStream: [
|
|
34
|
-
'{"method":"state.updated","params":{"sessionId":"sess_golden_r3_01","patch":{"status":"running"},"reason":"prompt_started"}}',
|
|
35
|
-
'{"method":"v4/telemetry/event","params":{"kind":"stream.chunk","channel":"text","chunkLength":6,"firstChunk":true,"assistantMessageId":"msg_golden_r3_01"}}',
|
|
36
|
-
'{"method":"session/event","params":{"sessionId":"sess_golden_r3_01","payload":{"delta":"你好","assistantMessageId":"msg_golden_r3_01"}}}',
|
|
37
|
-
'{"method":"v4/telemetry/event","params":{"kind":"stream.chunk","channel":"text","chunkLength":1,"firstChunk":false,"assistantMessageId":"msg_golden_r3_01"}}',
|
|
38
|
-
'{"method":"session/event","params":{"sessionId":"sess_golden_r3_01","payload":{"delta":",","assistantMessageId":"msg_golden_r3_01"}}}',
|
|
39
|
-
'{"method":"session/event","params":{"sessionId":"sess_golden_r3_01","payload":{"delta":"任务完成","assistantMessageId":"msg_golden_r3_01"}}}',
|
|
40
|
-
],
|
|
41
|
-
terminal: [
|
|
42
|
-
'{"method":"v4/telemetry/event","params":{"kind":"turn.terminal","status":"success"}}',
|
|
43
|
-
'{"method":"session/event","params":{"sessionId":"sess_golden_r3_01","payload":{"response":"你好,任务完成","usage":{"inputTokens":12599,"outputTokens":17}}}}',
|
|
44
|
-
],
|
|
45
|
-
readResponse:
|
|
46
|
-
'{"messages":[{"info":{"role":"user"},"parts":[{"type":"text","text":"做点什么"}]},{"info":{"role":"assistant"},"parts":[{"type":"text","text":"你好"},{"type":"text","text":",任务完成"},{"type":"step-finish","tokens":{"input":12599,"output":17,"reasoning":0,"cache":512,"total":12616}}]}]}',
|
|
47
|
-
};
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
// src/execution/engine/engines/zcode/parser.ts
|
|
2
|
-
//
|
|
3
|
-
// ZcodeEngine usage 映射与 coarse 事件件(2026-09 收缩):CLI stdout 有界收集/
|
|
4
|
-
// 终 JSON 解析/buildRunFailedMessage 随 spawn 链删除(app-server 路径的终态来自
|
|
5
|
-
// session-channel 的 read 兜底帧,不经 stdout 解析)。保留件:
|
|
6
|
-
// - ZcodeRawUsage / mapZcodeUsage / mapZcodeOutcomeUsage:原生 usage 形状 →
|
|
7
|
-
// 事件层(execution)与终态层(orchestration)AgentUsage(spawn 时代实测形状,
|
|
8
|
-
// app-server 收尾帧 usage 同形——session-channel 消费);
|
|
9
|
-
// - ZcodeTerminalPayload:app-server 轮成功态的载荷形态(turnResultToPayload 组装);
|
|
10
|
-
// - synthesizeCoarseEvents:终态合成最小事件序列(不变量:turn_end 最后、其前
|
|
11
|
-
// 至少一个 message_end)。
|
|
12
|
-
//
|
|
13
|
-
// 事件产出不变量(设计 §3.3.7,coarse 口径):turn_end 前至少一个 message_end;
|
|
14
|
-
// message_end.usage 出现时为完整 AgentUsage 形状(缺数据给显式 0,不给残缺对象)。
|
|
15
|
-
|
|
16
|
-
import type { AgentUsage as ExecutionAgentUsage } from "../../../types.ts";
|
|
17
|
-
import type { AgentUsage as OutcomeAgentUsage } from "../../../../orchestration/models/types.ts";
|
|
18
|
-
import type { AgentEvent } from "../../types.ts";
|
|
19
|
-
|
|
20
|
-
// ============================================================
|
|
21
|
-
// usage 映射(含运行时 guard——禁 any)
|
|
22
|
-
// ============================================================
|
|
23
|
-
|
|
24
|
-
/** 原生 usage 形状(2026-08-25 实测 0.16.5,字段名带 Tokens 后缀;app-server 收尾帧同形)。 */
|
|
25
|
-
export interface ZcodeRawUsage {
|
|
26
|
-
source?: unknown;
|
|
27
|
-
modelRequestCount?: unknown;
|
|
28
|
-
inputTokens?: unknown;
|
|
29
|
-
outputTokens?: unknown;
|
|
30
|
-
totalTokens?: unknown;
|
|
31
|
-
cacheReadTokens?: unknown;
|
|
32
|
-
cacheWriteTokens?: unknown;
|
|
33
|
-
reasoningTokens?: unknown;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function finiteOr(v: unknown, fallback: number): number {
|
|
37
|
-
const n = typeof v === "number" ? v : Number(v);
|
|
38
|
-
return Number.isFinite(n) ? n : fallback;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** 原生 usage → 事件层 AgentUsage(execution 版:四项 token;cost 无来源缺省)。 */
|
|
42
|
-
export function mapZcodeUsage(raw: unknown): ExecutionAgentUsage | undefined {
|
|
43
|
-
if (typeof raw !== "object" || raw === null) return undefined;
|
|
44
|
-
const r = raw as ZcodeRawUsage;
|
|
45
|
-
if (r.inputTokens === undefined && r.outputTokens === undefined) return undefined;
|
|
46
|
-
return {
|
|
47
|
-
input: finiteOr(r.inputTokens, 0),
|
|
48
|
-
output: finiteOr(r.outputTokens, 0),
|
|
49
|
-
cacheRead: finiteOr(r.cacheReadTokens, 0),
|
|
50
|
-
cacheWrite: finiteOr(r.cacheWriteTokens, 0),
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 原生 usage + projection → 终态层 AgentUsage(orchestration 版:cost/contextTokens/
|
|
56
|
-
* turns 为必填)。zcode 不回传 cost(调研附录 A「cost 回传 ❌」)——显式 0(消费方按
|
|
57
|
-
* 「显示降级」处理,不给残缺);contextTokens 取 projection.contextUsed(当前上下文
|
|
58
|
-
* 占用),turns 取 projection.turnCount。
|
|
59
|
-
*/
|
|
60
|
-
export function mapZcodeOutcomeUsage(rawUsage: unknown, projection: unknown): OutcomeAgentUsage | undefined {
|
|
61
|
-
const base = mapZcodeUsage(rawUsage);
|
|
62
|
-
if (base === undefined) return undefined;
|
|
63
|
-
const p =
|
|
64
|
-
typeof projection === "object" && projection !== null ? (projection as Record<string, unknown>) : {};
|
|
65
|
-
const r = typeof rawUsage === "object" && rawUsage !== null ? (rawUsage as ZcodeRawUsage) : {};
|
|
66
|
-
const contextTokens = firstFinite(p["contextUsed"], r.totalTokens, 0);
|
|
67
|
-
const turns = firstFinite(p["turnCount"], 1);
|
|
68
|
-
return { ...base, cost: 0, contextTokens, turns };
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function firstFinite(...vals: unknown[]): number {
|
|
72
|
-
for (const v of vals) {
|
|
73
|
-
const n = typeof v === "number" ? v : Number(v);
|
|
74
|
-
if (Number.isFinite(n)) return n;
|
|
75
|
-
}
|
|
76
|
-
return 0;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** 轮成功态的载荷形态(app-server 路径由 turnResultToPayload 组装:response 必 string)。 */
|
|
80
|
-
export interface ZcodeTerminalPayload {
|
|
81
|
-
sessionId?: string;
|
|
82
|
-
response: string;
|
|
83
|
-
/** 事件层 usage(execution 版 AgentUsage——message_end 合成用)。 */
|
|
84
|
-
usage?: ExecutionAgentUsage;
|
|
85
|
-
/** 终态层 usage(orchestration 版 AgentUsage——AgentOutcome.usage 用)。 */
|
|
86
|
-
outcomeUsage?: OutcomeAgentUsage;
|
|
87
|
-
/** projection.turnCount(gui/record 的轮数参考;解析不出则缺省)。 */
|
|
88
|
-
turnCount?: number;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// ============================================================
|
|
92
|
-
// coarse 事件合成(不变量:turn_end 最后、其前至少一个 message_end)
|
|
93
|
-
// ============================================================
|
|
94
|
-
|
|
95
|
-
/** 终态成功时合成的最小事件序列(coarse 引擎只有终态级信息——设计 D3 eventGranularity)。 */
|
|
96
|
-
export function synthesizeCoarseEvents(response: string, usage?: ExecutionAgentUsage): AgentEvent[] {
|
|
97
|
-
return [
|
|
98
|
-
// usage 给不出完整形状时显式缺省整个字段,不给残缺对象(不变量 2)
|
|
99
|
-
{ type: "message_end", ...(usage !== undefined ? { usage } : {}) },
|
|
100
|
-
{ type: "turn_end" },
|
|
101
|
-
] as AgentEvent[];
|
|
102
|
-
}
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
// src/execution/engine/engines/zcode/preparer.ts
|
|
2
|
-
//
|
|
3
|
-
// ZcodeEngine prepare 期模型解析件(P3 起源,2026-09 收缩):隔离 HOME 池化与
|
|
4
|
-
// 池内 config 引导随 CLI spawn 链删除(共享宿主 HOME——app-server 的凭据经
|
|
5
|
-
// appserver-launcher fs 拦截注入 v2 provider,本文件只剩 v2 单源的模型引用
|
|
6
|
-
// 解析/校验/清单:
|
|
7
|
-
// - **凭据源 = v2 config 单源(2026-08-25 用户拍板)**:只读 `~/.zcode/v2/config.json`
|
|
8
|
-
// (ZCode 桌面登录态落点,GUI 管理面)。曾泛化过「v2 + cli config 双源合并」,
|
|
9
|
-
// 但 `~/.zcode/cli/config.json` 不在 GUI 管理面、可能残留历史验证配置
|
|
10
|
-
// (8/24 zsub 开发残留把默认模型劫持到失效 router 端点的 401 事故),撤掉依赖;
|
|
11
|
-
// - 凭据缺失/模型不可解析抛结构化 ZcodePrepareError(错误码对齐设计 §3.3.3:
|
|
12
|
-
// engine_credential_missing / model_not_available),一律先于进程创建。
|
|
13
|
-
|
|
14
|
-
import * as fs from "node:fs";
|
|
15
|
-
import * as os from "node:os";
|
|
16
|
-
import * as path from "node:path";
|
|
17
|
-
|
|
18
|
-
import { ZCODE_FALLBACK_DEFAULT_MODEL, ZCODE_V2_CONFIG_PATH_SUFFIX } from "./constants.ts";
|
|
19
|
-
|
|
20
|
-
// ============================================================
|
|
21
|
-
// 结构化错误(prepare 期——进程创建前 reject 的载体)
|
|
22
|
-
// ============================================================
|
|
23
|
-
|
|
24
|
-
export type ZcodePrepareErrorCode =
|
|
25
|
-
| "engine_credential_missing"
|
|
26
|
-
| "model_not_available"
|
|
27
|
-
| "engine_capability_unsupported";
|
|
28
|
-
|
|
29
|
-
/** prepare 期错误:code 对齐设计 §3.3.3 错误规格表,message 自带恢复指引。 */
|
|
30
|
-
export class ZcodePrepareError extends Error {
|
|
31
|
-
readonly code: ZcodePrepareErrorCode;
|
|
32
|
-
|
|
33
|
-
constructor(code: ZcodePrepareErrorCode, message: string) {
|
|
34
|
-
super(`[${code}] ${message}`);
|
|
35
|
-
this.name = "ZcodePrepareError";
|
|
36
|
-
this.code = code;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// ============================================================
|
|
41
|
-
// 源 config 读取(运行时 guard,禁 any)
|
|
42
|
-
// ============================================================
|
|
43
|
-
|
|
44
|
-
/** provider 注册表条目的最小消费面(凭据 + 模型清单校验)。 */
|
|
45
|
-
export interface ZcodeProviderEntry {
|
|
46
|
-
options?: { apiKey?: unknown };
|
|
47
|
-
models?: Record<string, unknown>;
|
|
48
|
-
[k: string]: unknown;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
interface SourceConfig {
|
|
52
|
-
/** provider 注册表(来自 v2 config,逐条运行时 guard)。 */
|
|
53
|
-
providers: Map<string, ZcodeProviderEntry>;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** unknown 的 Record 窄化 guard(替代 as 全可选断言——taste/no-unsafe-cast)。 */
|
|
57
|
-
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
58
|
-
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** provider 条目一律按 ZcodeProviderEntry 消费(索引签名形态,无需逐键校验)。 */
|
|
62
|
-
function isProviderEntry(v: unknown): v is ZcodeProviderEntry {
|
|
63
|
-
return isRecord(v);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function readSourceConfig(absPath: string): SourceConfig {
|
|
67
|
-
const empty: SourceConfig = { providers: new Map() };
|
|
68
|
-
let raw: string;
|
|
69
|
-
try {
|
|
70
|
-
raw = fs.readFileSync(absPath, "utf8");
|
|
71
|
-
} catch {
|
|
72
|
-
return empty;
|
|
73
|
-
}
|
|
74
|
-
let parsed: unknown;
|
|
75
|
-
try {
|
|
76
|
-
parsed = JSON.parse(raw);
|
|
77
|
-
} catch {
|
|
78
|
-
return empty;
|
|
79
|
-
}
|
|
80
|
-
if (!isRecord(parsed)) return empty;
|
|
81
|
-
const conf: SourceConfig = { providers: new Map() };
|
|
82
|
-
const provider = parsed["provider"];
|
|
83
|
-
if (isRecord(provider)) {
|
|
84
|
-
for (const [id, entry] of Object.entries(provider)) {
|
|
85
|
-
if (isProviderEntry(entry)) conf.providers.set(id, entry);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return conf;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// ============================================================
|
|
92
|
-
// 模型解析(v2 单源,zsub resolve 同构)
|
|
93
|
-
// ============================================================
|
|
94
|
-
|
|
95
|
-
function modelShort(ref: string): string {
|
|
96
|
-
return ref.slice(ref.lastIndexOf("/") + 1);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function providerOf(ref: string): string {
|
|
100
|
-
return ref.slice(0, ref.lastIndexOf("/"));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** [R4] 规范化全名 provider/model → create 参数的 per-session model 拆分(A.2 ① strict 对象)。 */
|
|
104
|
-
export function splitZcodeModelRef(modelRef: string): { providerId: string; modelId: string } {
|
|
105
|
-
return { providerId: providerOf(modelRef), modelId: modelShort(modelRef) };
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function hasApiKey(entry: ZcodeProviderEntry): boolean {
|
|
109
|
-
const key = entry.options?.apiKey;
|
|
110
|
-
return typeof key === "string" && key !== "";
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export interface ZcodeSourcePaths {
|
|
114
|
-
/** 桌面登录态 config(唯一凭据源)。缺省 ~/.zcode/v2/config.json。 */
|
|
115
|
-
v2ConfigPath?: string;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function defaultV2ConfigPath(): string {
|
|
119
|
-
return path.join(os.homedir(), ...ZCODE_V2_CONFIG_PATH_SUFFIX);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* 短名(无 provider 前缀)解析的默认 provider(zsub DEFAULT_PROVIDER_ID 同构)。
|
|
124
|
-
* 导出(sink 设计 U1 模型切分四件之一):barrel re-export 供第三宿主模型路由消费,
|
|
125
|
-
* 实现体内聚本文件不挪。
|
|
126
|
-
*/
|
|
127
|
-
export const DEFAULT_PROVIDER_ID = "builtin:bigmodel-coding-plan";
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* 短名模型(如 "GLM-5.3")的默认 provider 决策。让位条件(对齐点⑦):显式默认引擎
|
|
131
|
-
* 模型配置引入时(config.json 出现 per-engine model 映射 / engineRouting 级模型缺省),
|
|
132
|
-
* 本函数的「内置常量优先」须让位为「配置值优先」——引擎路由层(engine/routing.ts 的
|
|
133
|
-
* taskModel 判定处)是引入该配置时应同步调整的决策点,避免出现「路由按配置模型选了
|
|
134
|
-
* 引擎、prepare 却按内置常量落池」的分裂。首期无该配置,常量即权威。
|
|
135
|
-
*/
|
|
136
|
-
function defaultProviderForShortName(
|
|
137
|
-
merged: Map<string, ZcodeProviderEntry>,
|
|
138
|
-
withKey: Array<[string, ZcodeProviderEntry]>,
|
|
139
|
-
): string {
|
|
140
|
-
if (merged.has(DEFAULT_PROVIDER_ID)) return DEFAULT_PROVIDER_ID;
|
|
141
|
-
return withKey[0]![0];
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* 解析并校验模型引用 → 规范化全名 `provider/model`。
|
|
146
|
-
*
|
|
147
|
-
* 解析链(requested > 默认链):
|
|
148
|
-
* 1. 显式 requested(task.model);
|
|
149
|
-
* 2. ZCODE_FALLBACK_DEFAULT_MODEL(zsub 同构兜底)。
|
|
150
|
-
*
|
|
151
|
-
* 校验对「带 apiKey 的 provider 注册表」做(没配凭据的 provider 写进池也跑不起来,
|
|
152
|
-
* resolve 期报错比运行时挂掉可诊断——zsub 经验)。provider 存在但无 apiKey →
|
|
153
|
-
* engine_credential_missing;provider/模型不存在 → model_not_available(列可用清单)。
|
|
154
|
-
*/
|
|
155
|
-
export function resolveZcodeModelRef(requested: string | undefined, sources?: ZcodeSourcePaths): string {
|
|
156
|
-
const v2 = readSourceConfig(sources?.v2ConfigPath ?? defaultV2ConfigPath());
|
|
157
|
-
const merged = v2.providers;
|
|
158
|
-
const withKey = [...merged.entries()].filter(([, e]) => hasApiKey(e));
|
|
159
|
-
const wanted = requested?.trim() || undefined;
|
|
160
|
-
const target = wanted ?? ZCODE_FALLBACK_DEFAULT_MODEL;
|
|
161
|
-
|
|
162
|
-
if (withKey.length === 0) {
|
|
163
|
-
const hint = wanted
|
|
164
|
-
? `model="${wanted}" 不能校验`
|
|
165
|
-
: `默认模型 "${target}" 不能校验`;
|
|
166
|
-
throw new ZcodePrepareError(
|
|
167
|
-
"engine_credential_missing",
|
|
168
|
-
`zcode 引擎找不到任何带 apiKey 的 provider(${hint})。已读源:${sources?.v2ConfigPath ?? defaultV2ConfigPath()}。` +
|
|
169
|
-
`恢复指引:先在 ZCode 桌面端登录并配置 provider(v2 config 内存在含 apiKey 的条目)后重试;` +
|
|
170
|
-
`凭据配置说明见 docs/research/agent-engine-zcode.md。`,
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const provider = target.includes("/")
|
|
175
|
-
? providerOf(target)
|
|
176
|
-
: defaultProviderForShortName(merged, withKey);
|
|
177
|
-
const short = modelShort(target);
|
|
178
|
-
const entry = merged.get(provider);
|
|
179
|
-
if (!entry) {
|
|
180
|
-
const known = withKey.map(([id]) => id).join(", ");
|
|
181
|
-
throw new ZcodePrepareError(
|
|
182
|
-
"model_not_available",
|
|
183
|
-
`未知 provider "${provider}"(带凭据的 provider: ${known})。` +
|
|
184
|
-
`恢复指引:改用上述 provider 之一(全名 provider/model),或先在 ZCode 桌面端配置该 provider 后重试。`,
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
if (!hasApiKey(entry)) {
|
|
188
|
-
throw new ZcodePrepareError(
|
|
189
|
-
"engine_credential_missing",
|
|
190
|
-
`provider "${provider}" 存在但未配置 apiKey。` +
|
|
191
|
-
`恢复指引:在 ZCode 桌面端为该 provider 登录配置凭据,或改用已配凭据的 provider(${withKey.map(([id]) => id).join(", ")})后重试。`,
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
const models = Object.keys(entry.models ?? {});
|
|
195
|
-
if (models.length > 0 && !models.includes(short)) {
|
|
196
|
-
throw new ZcodePrepareError(
|
|
197
|
-
"model_not_available",
|
|
198
|
-
`未知模型 "${short}"(provider ${provider} 下可用: ${models.join(", ")})。` +
|
|
199
|
-
`恢复指引:改用该 provider 下的模型,或先在 ZCode 桌面端启用目标模型后重试。`,
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
return `${provider}/${short}`;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// ============================================================
|
|
206
|
-
// 模型清单(U7 可发现性——v2 单源聚合,与 resolveZcodeModelRef 的凭据校验同判据)
|
|
207
|
-
// ============================================================
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* 列出当前环境 zcode 引擎实际可用的模型(v2 config 内带 apiKey 的 provider × 其
|
|
211
|
-
* models 清单)。消费方:EnginePort.listModels(system prompt 引擎段 / GUI)。
|
|
212
|
-
* 失败安全:v2 config 不可读 → 空清单(可发现性降级不阻塞主流程)。
|
|
213
|
-
*/
|
|
214
|
-
export function listZcodeModels(sources?: ZcodeSourcePaths): Array<{ id: string; name?: string }> {
|
|
215
|
-
const v2 = readSourceConfig(sources?.v2ConfigPath ?? defaultV2ConfigPath());
|
|
216
|
-
const out: Array<{ id: string; name?: string }> = [];
|
|
217
|
-
for (const [pid, entry] of v2.providers) {
|
|
218
|
-
if (!hasApiKey(entry)) continue;
|
|
219
|
-
const providerName =
|
|
220
|
-
typeof entry["name"] === "string" && entry["name"].trim() !== "" ? entry["name"].trim() : undefined;
|
|
221
|
-
const models = Object.keys(entry.models ?? {});
|
|
222
|
-
for (const model of models) {
|
|
223
|
-
out.push({
|
|
224
|
-
id: `${pid}/${model}`,
|
|
225
|
-
...(providerName !== undefined ? { name: `${providerName} · ${model}` } : {}),
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return out;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// ============================================================
|
|
233
|
-
// 池 key 与目录(2026-09 删除:HOME 池化随 CLI spawn 链一并退役——共享宿主 HOME
|
|
234
|
-
// 后无隔离池、无 config 引导,本文件只剩 v2 单源的模型解析/清单校验件)
|
|
235
|
-
// ============================================================
|