@zhushanwen/subagent-core 0.4.0 → 0.6.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/dist/{chunk-4KN4TTG7.js → chunk-43ONBFZX.js} +1 -1
- package/dist/{chunk-X7SZ5HLQ.js → chunk-V3VHZ2VX.js} +20 -27
- package/dist/execution/engine/engines/zcode/constants.cjs +29 -41
- package/dist/execution/engine/engines/zcode/constants.d.cts +65 -53
- package/dist/execution/engine/engines/zcode/constants.d.ts +65 -53
- package/dist/execution/engine/engines/zcode/constants.js +17 -27
- package/dist/execution/engine/engines/zcode/reader.d.cts +2 -1
- package/dist/execution/engine/engines/zcode/reader.d.ts +2 -1
- package/dist/execution/engine/engines/zcode/reader.js +2 -2
- package/dist/index.cjs +17100 -14082
- package/dist/index.d.cts +5336 -4033
- package/dist/index.d.ts +5336 -4033
- package/dist/index.js +17020 -14055
- package/dist/{types-DpUO16pj.d.cts → types-C3fE3R9x.d.cts} +280 -112
- package/dist/{types-DpUO16pj.d.ts → types-C3fE3R9x.d.ts} +280 -112
- package/dist.bundle/index.cjs +34933 -0
- package/package.json +3 -3
- package/src/__tests__/agent-opts-resolver-schema-prompt.test.ts +0 -1
- package/src/__tests__/append-system-prompt-assembly.test.ts +8 -7
- package/src/__tests__/fr4-get-state-handshake.test.ts +2 -2
- package/src/__tests__/m2-append-content-probe.test.ts +0 -1
- package/src/__tests__/manifest-store.test.ts +10 -9
- package/src/__tests__/record-store-cache.test.ts +0 -2
- package/src/__tests__/record-store-index.test.ts +1 -2
- package/src/__tests__/review-fix-loop-script.test.ts +4 -2
- package/src/__tests__/review-fix-loop-utils.test.ts +10 -10
- package/src/__tests__/robustness-low-batch1.test.ts +6 -5
- package/src/__tests__/robustness-medium-batch1.test.ts +2 -2
- package/src/__tests__/robustness-medium-batch2.test.ts +17 -6
- package/src/__tests__/robustness-medium-batch4.test.ts +2 -2
- package/src/__tests__/session-runner.test.ts +1 -1
- package/src/core/__tests__/host-services.test.ts +2 -2
- package/src/core/__tests__/logger.test.ts +1 -1
- package/src/core/error-message.ts +9 -0
- package/src/core/host-services.ts +21 -5
- package/src/core/notify-ports.ts +18 -5
- package/src/execution/__tests__/agent-profile.test.ts +4 -4
- package/src/execution/__tests__/agent-registry.test.ts +370 -0
- package/src/execution/__tests__/agent-result-mapper.test.ts +18 -2
- package/src/execution/__tests__/agents-assembly.test.ts +2 -2
- package/src/execution/__tests__/alive-store.test.ts +1 -1
- package/src/execution/__tests__/ask-user-transit-e2e.test.ts +6 -6
- package/src/execution/__tests__/chat-engine-routing.test.ts +68 -33
- package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +4 -4
- package/src/execution/__tests__/cold-resurrect.test.ts +283 -0
- package/src/execution/__tests__/collect-budget.test.ts +291 -0
- package/src/execution/__tests__/collect-coordinator-service.test.ts +327 -0
- package/src/execution/__tests__/collect-coordinator.test.ts +371 -0
- package/src/execution/__tests__/collect-mixed-dispatch.test.ts +262 -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 +75 -62
- package/src/execution/__tests__/descendant-sweep-guards.test.ts +220 -0
- package/src/execution/__tests__/descendant-sweep.test.ts +6 -6
- package/src/execution/__tests__/dialog-queue.test.ts +2 -2
- package/src/execution/__tests__/engine-model-validation.test.ts +741 -0
- package/src/execution/__tests__/epipe-fallback.test.ts +29 -24
- package/src/execution/__tests__/execute-and-await-worktree.test.ts +21 -91
- package/src/execution/__tests__/execute-nesting.test.ts +34 -188
- package/src/execution/__tests__/execution-record.test.ts +217 -60
- package/src/execution/__tests__/execution-runtime-face.test.ts +5 -5
- package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +4 -4
- package/src/execution/__tests__/finalize-record.test.ts +173 -17
- package/src/execution/__tests__/finalized-marker.test.ts +1 -1
- package/src/execution/__tests__/gc-timer.test.ts +56 -3
- package/src/execution/__tests__/get-record-for-action-restart.test.ts +12 -12
- package/src/execution/__tests__/get-state-handshake.test.ts +1 -1
- package/src/execution/__tests__/helpers/mock-extension-api.ts +1 -1
- package/src/execution/__tests__/helpers/session-runner-mocks.ts +113 -0
- package/src/execution/__tests__/helpers/spawn-mock.ts +150 -3
- package/src/execution/__tests__/helpers/subagent-service-mocks.ts +137 -0
- package/src/execution/__tests__/keep-alive-no-progress.test.ts +50 -148
- package/src/execution/__tests__/kill-all-escalation.test.ts +2 -2
- package/src/execution/__tests__/lifecycle-predicates.test.ts +1 -2
- package/src/execution/__tests__/manifest-parentid.test.ts +4 -4
- package/src/execution/__tests__/manifest-store-tmp-recovery.test.ts +1 -1
- package/src/execution/__tests__/max-turns-to-watchdog-ms.test.ts +1 -1
- package/src/execution/__tests__/model-resolver.test.ts +2 -2
- package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +23 -121
- package/src/execution/__tests__/nested-visibility.test.ts +2 -0
- package/src/execution/__tests__/notify-batch.test.ts +500 -0
- package/src/execution/__tests__/notify-ledger.test.ts +1000 -18
- package/src/execution/__tests__/output-collector.test.ts +117 -6
- package/src/execution/__tests__/pi-invocation.test.ts +36 -4
- package/src/execution/__tests__/record-entry-collect.test.ts +102 -0
- package/src/execution/__tests__/record-store.test.ts +10 -9
- package/src/execution/__tests__/records-cwd-isolation.test.ts +2 -2
- package/src/execution/__tests__/recursive-visibility-baseline.test.ts +29 -132
- package/src/execution/__tests__/recursive-visibility-env.test.ts +55 -179
- package/src/execution/__tests__/relay-env.test.ts +12 -1
- package/src/execution/__tests__/rpc-mode.test.ts +2 -2
- package/src/execution/__tests__/run-and-finalize-anchoring.test.ts +387 -0
- package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +15 -9
- package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +4 -4
- package/src/execution/__tests__/run-spawn-edges.test.ts +17 -15
- package/src/execution/__tests__/run-spawn-integration.test.ts +2 -2
- package/src/execution/__tests__/run-spawn-resume.test.ts +2 -2
- package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +2 -2
- package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +4 -4
- package/src/execution/__tests__/service-kill-escalation.test.ts +7 -5
- package/src/execution/__tests__/session-context-resolver.test.ts +0 -2
- package/src/execution/__tests__/session-file-gc.test.ts +36 -1
- package/src/execution/__tests__/session-pending.test.ts +257 -14
- package/src/execution/__tests__/session-reconstructor.test.ts +366 -15
- package/src/execution/__tests__/session-runner-branch-cache-lru.test.ts +1 -1
- package/src/execution/__tests__/session-runner-close-prune.test.ts +2 -2
- package/src/execution/__tests__/session-runner-dispatch.test.ts +489 -0
- package/src/execution/__tests__/session-runner-epipe.test.ts +3 -3
- package/src/execution/__tests__/session-runner-heartbeat-idle-fallback.test.ts +5 -4
- package/src/execution/__tests__/session-runner-lifecycle-helpers.test.ts +290 -0
- package/src/execution/__tests__/session-runner-schema-env.test.ts +7 -5
- package/src/execution/__tests__/settled-watchdog.test.ts +282 -47
- package/src/execution/__tests__/spawn-args.test.ts +11 -10
- package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +26 -23
- package/src/execution/__tests__/spawn-event-adapter.test.ts +3 -3
- package/src/execution/__tests__/spawn-worktree-guidance.test.ts +5 -4
- package/src/execution/__tests__/spawned-children.test.ts +2 -1
- package/src/execution/__tests__/start-collect-guard.test.ts +255 -0
- package/src/execution/__tests__/start-sync-model-guard.test.ts +5 -5
- package/src/execution/__tests__/stdin-writer.test.ts +6 -4
- package/src/execution/__tests__/stream-sink-retirement.test.ts +6 -6
- package/src/execution/__tests__/subagent-actions-core.test.ts +48 -22
- package/src/execution/__tests__/subagent-service-message-close.test.ts +29 -26
- package/src/execution/__tests__/subagent-service-multiproc-guard.test.ts +21 -8
- package/src/execution/__tests__/subagent-service-notify-gate.test.ts +27 -22
- package/src/execution/__tests__/subagent-service-parent-guard.test.ts +30 -29
- package/src/execution/__tests__/subagent-service-recovery-bounds.test.ts +45 -20
- package/src/execution/__tests__/subagent-service.test.ts +37 -30
- package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +88 -12
- package/src/execution/__tests__/subprocess-agent-runner-timeout.test.ts +75 -0
- package/src/execution/__tests__/subprocess-agent-runner.test.ts +22 -20
- package/src/execution/__tests__/sync-collect-recovery.test.ts +1117 -0
- package/src/execution/__tests__/temp-prompt.test.ts +1 -1
- package/src/execution/__tests__/timeout-integration.test.ts +6 -5
- package/src/execution/__tests__/tombstone-store.test.ts +1 -1
- package/src/execution/__tests__/turn-limiter-semantics.test.ts +1 -1
- package/src/execution/__tests__/turn-limiter.test.ts +1 -1
- package/src/execution/__tests__/ui-request-handler-factory.test.ts +12 -6
- package/src/execution/__tests__/ui-request-handler.test.ts +4 -3
- package/src/execution/__tests__/ui-request-queue.test.ts +142 -2
- package/src/execution/__tests__/worktree-git-ops.test.ts +1 -1
- package/src/execution/__tests__/worktree-manager.test.ts +14 -5
- package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +4 -3
- 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 +74 -30
- package/src/execution/agent-result-mapper.ts +3 -0
- package/src/execution/cold-resurrect.ts +199 -0
- package/src/execution/collect-coordinator.ts +200 -0
- package/src/execution/config.ts +77 -5
- package/src/execution/engine/__tests__/common/capability-gate.test.ts +91 -0
- package/src/execution/engine/__tests__/common/data-dir.test.ts +1 -1
- package/src/execution/engine/__tests__/common/event-journal.test.ts +1 -1
- package/src/execution/engine/__tests__/common/journal-wiring.test.ts +84 -0
- package/src/execution/engine/__tests__/common/kill-chain.test.ts +34 -7
- package/src/execution/engine/__tests__/common/persona-router.test.ts +4 -5
- package/src/execution/engine/__tests__/common/pool-manager.test.ts +213 -53
- package/src/execution/engine/__tests__/common/session-view-service-zcode-dbpath.test.ts +149 -0
- package/src/execution/engine/__tests__/common/session-view-service.test.ts +499 -0
- package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +22 -112
- package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +15 -28
- package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +1 -1
- package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +12 -12
- package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +2 -2
- package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +21 -82
- package/src/execution/engine/__tests__/conformance/zcode-appserver-harness.ts +4 -7
- package/src/execution/engine/__tests__/engine-discovery.test.ts +15 -3
- package/src/execution/engine/__tests__/model-prompt.test.ts +15 -3
- package/src/execution/engine/__tests__/registry.test.ts +4 -2
- package/src/execution/engine/__tests__/routing.test.ts +115 -6
- package/src/execution/engine/common/capability-gate.ts +92 -0
- package/src/execution/engine/common/journal-wiring.ts +97 -0
- package/src/execution/engine/common/kill-chain.ts +42 -10
- package/src/execution/engine/common/nesting-guard.ts +66 -0
- package/src/execution/engine/common/persona-router.ts +18 -8
- package/src/execution/engine/common/pool-manager.ts +332 -78
- package/src/execution/engine/common/session-view-service.ts +506 -0
- package/src/execution/engine/common/session-view-types.ts +125 -0
- package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +174 -28
- package/src/execution/engine/engines/pi/__tests__/spawn-opts-direct.test.ts +257 -0
- package/src/execution/{argv-mirror.ts → engine/engines/pi/argv-mirror.ts} +43 -21
- package/src/execution/{get-state-handshake.ts → engine/engines/pi/get-state-handshake.ts} +1 -1
- package/src/execution/{output-collector.ts → engine/engines/pi/output-collector.ts} +108 -19
- package/src/execution/engine/engines/pi/pi-engine.ts +434 -38
- package/src/execution/{pi-invocation.ts → engine/engines/pi/pi-invocation.ts} +2 -2
- package/src/execution/engine/engines/pi/reader.ts +11 -0
- package/src/execution/engine/engines/pi/registration.ts +4 -2
- package/src/execution/{session-runner.ts → engine/engines/pi/session-runner.ts} +617 -412
- package/src/execution/{spawn-event-adapter.ts → engine/engines/pi/spawn-event-adapter.ts} +75 -34
- package/src/execution/{stdin-writer.ts → engine/engines/pi/stdin-writer.ts} +10 -9
- package/src/execution/{temp-prompt.ts → engine/engines/pi/temp-prompt.ts} +2 -2
- package/src/execution/{turn-limiter.ts → engine/engines/pi/turn-limiter.ts} +1 -1
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/fake-appserver.mjs +54 -27
- package/src/execution/engine/engines/zcode/__tests__/appserver-launcher.test.ts +321 -0
- package/src/execution/engine/engines/zcode/__tests__/connection.test.ts +13 -12
- package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +7 -210
- package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +6 -110
- package/src/execution/engine/engines/zcode/__tests__/session-channel-dispose-harvest.test.ts +247 -0
- package/src/execution/engine/engines/zcode/__tests__/session-channel-turn-timers.test.ts +451 -0
- package/src/execution/engine/engines/zcode/__tests__/session-channel.test.ts +20 -8
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-appserver.test.ts +77 -110
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-degrade.test.ts +26 -320
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-dispose.test.ts +210 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-retry.test.ts +353 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-status.test.ts +282 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-timeout.test.ts +372 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +48 -148
- package/src/execution/engine/engines/zcode/appserver-launcher.ts +167 -0
- package/src/execution/engine/engines/zcode/connection.ts +24 -11
- package/src/execution/engine/engines/zcode/constants.ts +75 -71
- package/src/execution/engine/engines/zcode/golden-sample.ts +8 -46
- package/src/execution/engine/engines/zcode/parser.ts +12 -346
- package/src/execution/engine/engines/zcode/preparer.ts +12 -143
- package/src/execution/engine/engines/zcode/session-channel.ts +351 -39
- package/src/execution/engine/engines/zcode/zcode-engine.ts +665 -760
- package/src/execution/engine/host-task-spec.ts +32 -36
- package/src/execution/engine/model-validation.ts +177 -0
- package/src/execution/engine/port.ts +24 -6
- package/src/execution/engine/routing.ts +75 -1
- package/src/execution/engine/types.ts +30 -103
- package/src/execution/execution-record.ts +229 -144
- package/src/execution/finalize-record.ts +141 -99
- package/src/execution/idle-gc.ts +28 -0
- package/src/execution/lifecycle-predicates.ts +1 -1
- package/src/execution/notifier.ts +234 -25
- package/src/execution/notify-host.ts +232 -0
- package/src/execution/record-entry.ts +16 -0
- package/src/execution/record-store.ts +280 -103
- package/src/execution/round-settlement.ts +93 -0
- package/src/execution/session-file-gc.ts +96 -62
- package/src/execution/session-reconstructor.ts +256 -143
- package/src/execution/sessions-index.ts +117 -47
- package/src/execution/settled-watchdog.ts +264 -52
- package/src/execution/subagent-actions-core.ts +84 -28
- package/src/execution/subagent-service.ts +1225 -964
- package/src/execution/subprocess-agent-runner.ts +144 -97
- package/src/execution/sync-rebuild.ts +87 -0
- package/src/execution/types.ts +72 -0
- package/src/execution/ui-request-handler-factory.ts +102 -74
- package/src/execution/ui-request-queue.ts +50 -19
- package/src/index.ts +442 -316
- package/src/orchestration/__tests__/agent-call-catch-fallback.test.ts +1 -1
- package/src/orchestration/__tests__/agent-call-stream.test.ts +2 -2
- package/src/orchestration/__tests__/args-validator.test.ts +14 -0
- package/src/orchestration/__tests__/config-loader.test.ts +1 -1
- package/src/orchestration/__tests__/error-recovery-rebuild-failure.test.ts +1 -1
- package/src/orchestration/__tests__/error-recovery-terminal-hardening.test.ts +11 -9
- package/src/orchestration/__tests__/execute-agent-call.test.ts +151 -11
- 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__/file-run-store.test.ts +7 -6
- package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +2 -2
- package/src/orchestration/__tests__/lifecycle-abort-broadcast-signal.test.ts +1 -1
- package/src/orchestration/__tests__/lifecycle-runid-injection.test.ts +3 -3
- package/src/orchestration/__tests__/lifecycle.test.ts +7 -5
- package/src/orchestration/__tests__/non-cloneable-return-e2e.test.ts +1 -1
- package/src/orchestration/__tests__/review-fix-loop-scriptpath-failfast.test.ts +1 -1
- package/src/orchestration/__tests__/run-snapshot.test.ts +8 -5
- package/src/orchestration/__tests__/script-generate.test.ts +5 -5
- package/src/orchestration/__tests__/script-lint.test.ts +17 -0
- package/src/orchestration/__tests__/skill-discovery.test.ts +2 -2
- package/src/orchestration/__tests__/test-mocks.ts +2 -0
- package/src/orchestration/__tests__/worker-exit-without-result.test.ts +7 -6
- package/src/orchestration/__tests__/worker-message-pump-finalize-run.test.ts +229 -0
- package/src/orchestration/__tests__/{error-recovery-handlers.test.ts → worker-message-pump-handlers.test.ts} +27 -19
- package/src/orchestration/__tests__/{error-recovery-postmessage-defense.test.ts → worker-message-pump-postmessage-defense.test.ts} +8 -8
- package/src/orchestration/__tests__/{error-recovery-serialize-failed-result.test.ts → worker-message-pump-serialize-failed-result.test.ts} +2 -2
- package/src/orchestration/__tests__/{error-recovery-workflow-call.test.ts → worker-message-pump-workflow-call.test.ts} +1 -1
- package/src/orchestration/__tests__/worker-pending-timeout-abort.test.ts +1 -1
- package/src/orchestration/__tests__/worker-script-builder.test.ts +1 -1
- package/src/orchestration/__tests__/workflow-files.test.ts +10 -10
- package/src/orchestration/__tests__/workflow-nesting-e2e.test.ts +1 -1
- package/src/orchestration/__tests__/workflow-script-registry-impl.test.ts +1 -1
- package/src/orchestration/agent-opts-resolver.ts +1 -1
- package/src/orchestration/args-validator.ts +53 -31
- package/src/orchestration/execute-agent-call.ts +19 -73
- package/src/orchestration/launcher.ts +128 -81
- package/src/orchestration/lifecycle.ts +43 -45
- package/src/orchestration/models/ports.ts +4 -4
- package/src/orchestration/models/run-runtime.ts +2 -2
- package/src/orchestration/models/trace.ts +1 -1
- package/src/orchestration/models/types.ts +111 -22
- package/src/orchestration/models/workflow-run.ts +1 -1
- package/src/orchestration/script-lint.ts +91 -58
- package/src/orchestration/{error-recovery.ts → worker-message-pump.ts} +157 -150
- package/src/shared/__tests__/atomic-write.test.ts +1 -1
- package/src/shared/__tests__/meta-parser.test.ts +151 -0
- package/src/shared/__tests__/resource-discovery-host-roots.test.ts +5 -5
- package/src/shared/__tests__/resource-discovery.test.ts +258 -215
- package/src/shared/atomic-write.ts +12 -9
- package/src/shared/meta-parser.ts +158 -84
- package/src/shared/model-ref.ts +15 -2
- package/src/shared/resource-discovery.ts +9 -183
- package/src/shared/schema-jsonify.ts +1 -1
- package/workflows/review-fix-loop-utils.cjs +151 -89
- package/workflows/review-fix-loop-utils.d.cts +287 -0
- package/src/execution/__tests__/execute-options-mapper.test.ts +0 -200
- package/src/execution/engine/engines/pi/__tests__/task-spec-mapper.test.ts +0 -164
- package/src/execution/engine/engines/pi/task-spec-mapper.ts +0 -100
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-spawn.json +0 -39
- package/src/execution/engine/engines/zcode/__tests__/launcher.test.ts +0 -150
- package/src/execution/engine/engines/zcode/__tests__/preparer-appserver.test.ts +0 -387
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.test.ts +0 -710
- package/src/execution/engine/engines/zcode/appserver-home.ts +0 -442
- package/src/execution/engine/engines/zcode/appserver-probe.ts +0 -141
- package/src/execution/engine/engines/zcode/launcher.ts +0 -161
- package/src/execution/execute-options-mapper.ts +0 -115
- package/src/shared/__tests__/resource-discovery-manifest-cache.test.ts +0 -288
|
@@ -104,26 +104,34 @@ function truncateLabel(label: string): string {
|
|
|
104
104
|
return label.length > TOOL_LABEL_MAX ? label.slice(0, TOOL_LABEL_MAX) : label;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
+
/** usage 单字段求和(undefined 视为 0——与旧 `(a ?? 0) + (b ?? 0)` 内联式逐字等价)。 */
|
|
108
|
+
function sumUsageField(a: number | undefined, b: number | undefined): number {
|
|
109
|
+
return (a ?? 0) + (b ?? 0);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** prev 为空时 next 的规范化拷贝(cost 保留原值,可能 undefined——与旧首条分支逐字等价)。 */
|
|
113
|
+
function usageFromNext(next: AgentUsage): AgentUsage {
|
|
114
|
+
return {
|
|
115
|
+
input: next.input ?? 0,
|
|
116
|
+
output: next.output ?? 0,
|
|
117
|
+
cacheRead: next.cacheRead ?? 0,
|
|
118
|
+
cacheWrite: next.cacheWrite ?? 0,
|
|
119
|
+
cost: next.cost,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
107
123
|
/**
|
|
108
124
|
* 累加两个 AgentUsage(field-wise)。prev 为空时返回 next 的拷贝。
|
|
109
125
|
* 供 message_end 把 usage 增量并入 turn.usageDelta。
|
|
110
126
|
*/
|
|
111
127
|
function addUsage(prev: AgentUsage | undefined, next: AgentUsage): AgentUsage {
|
|
112
|
-
if (prev === undefined)
|
|
113
|
-
return {
|
|
114
|
-
input: next.input ?? 0,
|
|
115
|
-
output: next.output ?? 0,
|
|
116
|
-
cacheRead: next.cacheRead ?? 0,
|
|
117
|
-
cacheWrite: next.cacheWrite ?? 0,
|
|
118
|
-
cost: next.cost,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
128
|
+
if (prev === undefined) return usageFromNext(next);
|
|
121
129
|
return {
|
|
122
|
-
input: (prev.input
|
|
123
|
-
output: (prev.output
|
|
124
|
-
cacheRead: (prev.cacheRead
|
|
125
|
-
cacheWrite: (prev.cacheWrite
|
|
126
|
-
cost: (prev.cost
|
|
130
|
+
input: sumUsageField(prev.input, next.input),
|
|
131
|
+
output: sumUsageField(prev.output, next.output),
|
|
132
|
+
cacheRead: sumUsageField(prev.cacheRead, next.cacheRead),
|
|
133
|
+
cacheWrite: sumUsageField(prev.cacheWrite, next.cacheWrite),
|
|
134
|
+
cost: sumUsageField(prev.cost, next.cost),
|
|
127
135
|
};
|
|
128
136
|
}
|
|
129
137
|
|
|
@@ -167,6 +175,12 @@ export function createRecord(
|
|
|
167
175
|
engine?: string;
|
|
168
176
|
/** 引擎 fallback 留痕(probe 失败路由回默认引擎)。GUI 警告条数据源。 */
|
|
169
177
|
engineFallback?: { from: string; reason: string };
|
|
178
|
+
/**
|
|
179
|
+
* 同步收集模式标记(subagent-sync-collect U1 foundation)。undefined = async
|
|
180
|
+
* (缺省语义,旧记录零迁移)。U2 接线点:service.createRecordForMode 从
|
|
181
|
+
* ExecuteOptions.collect 读入(opts.collect === "sync" ? "sync" : undefined)。
|
|
182
|
+
*/
|
|
183
|
+
collectMode?: "sync";
|
|
170
184
|
controller?: AbortController;
|
|
171
185
|
},
|
|
172
186
|
): ExecutionRecord {
|
|
@@ -186,6 +200,7 @@ export function createRecord(
|
|
|
186
200
|
idleTimeoutMs: identity.idleTimeoutMs,
|
|
187
201
|
engine: identity.engine,
|
|
188
202
|
engineFallback: identity.engineFallback,
|
|
203
|
+
collectMode: identity.collectMode,
|
|
189
204
|
|
|
190
205
|
// 状态(实时更新)
|
|
191
206
|
status: "running",
|
|
@@ -274,6 +289,141 @@ function indexToolStart(record: ExecutionRecord, turn: Turn, toolName: string):
|
|
|
274
289
|
}
|
|
275
290
|
}
|
|
276
291
|
|
|
292
|
+
// ── 各事件处理器(updateFromEvent 按 case 分发,每个处理器单一职责)──
|
|
293
|
+
|
|
294
|
+
/** text_delta:流式累积进当前 turn 的 text(完整内容,非切片)。 */
|
|
295
|
+
function applyTextDelta(
|
|
296
|
+
record: ExecutionRecord,
|
|
297
|
+
event: Extract<AgentEvent, { type: "text_delta" }>,
|
|
298
|
+
): void {
|
|
299
|
+
currentTurn(record).text += event.delta;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** thinking_delta:流式累积进当前 turn 的 thinking(完整内容)。 */
|
|
303
|
+
function applyThinkingDelta(
|
|
304
|
+
record: ExecutionRecord,
|
|
305
|
+
event: Extract<AgentEvent, { type: "thinking_delta" }>,
|
|
306
|
+
): void {
|
|
307
|
+
currentTurn(record).thinking += event.delta;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** tool_start:push 一个 running 的 InternalToolCall(带 startedTs)+ 弹尾索引入册。 */
|
|
311
|
+
function applyToolStart(
|
|
312
|
+
record: ExecutionRecord,
|
|
313
|
+
event: Extract<AgentEvent, { type: "tool_start" }>,
|
|
314
|
+
): void {
|
|
315
|
+
const tc: InternalToolCall = {
|
|
316
|
+
toolName: event.toolName,
|
|
317
|
+
args: event.args,
|
|
318
|
+
result: undefined,
|
|
319
|
+
isError: false,
|
|
320
|
+
_status: "running",
|
|
321
|
+
startedTs: Date.now(),
|
|
322
|
+
};
|
|
323
|
+
const turn = currentTurn(record);
|
|
324
|
+
turn.toolCalls.push(tc);
|
|
325
|
+
indexToolStart(record, turn, event.toolName);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* tool_end 定位:索引弹尾 O(1) 命中 running 同名 toolCall;索引 miss(无记录 / 槽位
|
|
330
|
+
* 已非 running)回退 findRunningToolCall 跨 turn 倒序全扫兜底。
|
|
331
|
+
* 返回 [turn, index];两路都 miss 返回 undefined。
|
|
332
|
+
*/
|
|
333
|
+
function matchRunningToolCall(
|
|
334
|
+
record: ExecutionRecord,
|
|
335
|
+
toolName: string,
|
|
336
|
+
): readonly [Turn, number] | undefined {
|
|
337
|
+
const byName = runningToolIndex.get(record);
|
|
338
|
+
const arr = byName?.get(toolName);
|
|
339
|
+
if (arr !== undefined && arr.length > 0) {
|
|
340
|
+
const item = arr[arr.length - 1];
|
|
341
|
+
arr.pop();
|
|
342
|
+
const tc = item.turn.toolCalls[item.idx];
|
|
343
|
+
if (tc !== undefined && tc._status === "running") {
|
|
344
|
+
return [item.turn, item.idx] as const;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
// 兜底:重建 record 的历史 running toolCall(索引未覆盖)、索引项被外部路径
|
|
348
|
+
// 置非 running 等场景——保持与旧实现一致的跨 turn 倒序全扫。
|
|
349
|
+
return findRunningToolCall(record, toolName);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* tool_end:命中则回填 result/isError/_status;未命中(SDK 发了 tool_end 但无对应
|
|
354
|
+
* tool_start,如外部注入的工具)直接 push 一个已完成的 InternalToolCall,避免数据丢失。
|
|
355
|
+
*/
|
|
356
|
+
function applyToolEnd(
|
|
357
|
+
record: ExecutionRecord,
|
|
358
|
+
event: Extract<AgentEvent, { type: "tool_end" }>,
|
|
359
|
+
): void {
|
|
360
|
+
const matched = matchRunningToolCall(record, event.toolName);
|
|
361
|
+
if (matched !== undefined) {
|
|
362
|
+
const [turn, i] = matched;
|
|
363
|
+
const tc = turn.toolCalls[i]!;
|
|
364
|
+
tc.args = event.args ?? tc.args;
|
|
365
|
+
tc.result = event.result;
|
|
366
|
+
tc.isError = event.isError ?? false;
|
|
367
|
+
tc._status = event.isError ? "failed" : "done";
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
currentTurn(record).toolCalls.push({
|
|
371
|
+
toolName: event.toolName,
|
|
372
|
+
args: event.args,
|
|
373
|
+
result: event.result,
|
|
374
|
+
isError: event.isError ?? false,
|
|
375
|
+
_status: event.isError ? "failed" : "done",
|
|
376
|
+
startedTs: Date.now(),
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* turn_end:闭合当前 turn,记 closedTs(真实墙钟),turnCount++,清 lastError。
|
|
382
|
+
* 正常闭合清 lastError:瞬态 error 恢复后不应误判 success=false
|
|
383
|
+
* (若 turn_end 后 message_end 报 error,会在 message_end 处理器重新写回)。
|
|
384
|
+
*/
|
|
385
|
+
function applyTurnEnd(record: ExecutionRecord): void {
|
|
386
|
+
const turn = currentTurn(record);
|
|
387
|
+
turn.closed = true;
|
|
388
|
+
turn.closedTs = Date.now();
|
|
389
|
+
record.turnCount += 1;
|
|
390
|
+
record.lastError = undefined;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* message_end:usage 增量存进末 turn.usageDelta(直接写末 turn,不开新 turn);
|
|
395
|
+
* totalTokens 累加;error(stopReason=error)记进 lastError。
|
|
396
|
+
*
|
|
397
|
+
* usageDelta 按 message_end **累加**(非覆盖)——同一 turn 内若多次 message_end
|
|
398
|
+
* 到达(或 turn_end 后的滞后 message_end 落到 currentTurn 开的新 turn),
|
|
399
|
+
* 累加保证不丢 usage。getTotalUsage 扁平求和所有 turn,归属 turn 的精确性
|
|
400
|
+
* 不影响最终 total(无消费方读单 turn usage)。
|
|
401
|
+
*/
|
|
402
|
+
function applyMessageEnd(
|
|
403
|
+
record: ExecutionRecord,
|
|
404
|
+
event: Extract<AgentEvent, { type: "message_end" }>,
|
|
405
|
+
): void {
|
|
406
|
+
if (event.usage) {
|
|
407
|
+
const turn = currentTurn(record);
|
|
408
|
+
turn.usageDelta = addUsage(turn.usageDelta, event.usage);
|
|
409
|
+
// totalTokens 累加四项之和(保留旧语义,投影直接读)
|
|
410
|
+
record.totalTokens +=
|
|
411
|
+
(event.usage.input ?? 0) + (event.usage.output ?? 0) +
|
|
412
|
+
(event.usage.cacheRead ?? 0) + (event.usage.cacheWrite ?? 0);
|
|
413
|
+
}
|
|
414
|
+
if (event.error) {
|
|
415
|
+
record.lastError = event.error;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** error:存 record.lastError(getEventLog 派生 error 条目用)。 */
|
|
420
|
+
function applyErrorEvent(
|
|
421
|
+
record: ExecutionRecord,
|
|
422
|
+
event: Extract<AgentEvent, { type: "error" }>,
|
|
423
|
+
): void {
|
|
424
|
+
record.lastError = event.message;
|
|
425
|
+
}
|
|
426
|
+
|
|
277
427
|
/**
|
|
278
428
|
* 从 AgentEvent 更新 record。所有数据收口进 record.turns[]。
|
|
279
429
|
* - text/thinking:流式累积进 currentTurn()(完整内容,非切片)
|
|
@@ -293,116 +443,32 @@ function indexToolStart(record: ExecutionRecord, turn: Turn, toolName: string):
|
|
|
293
443
|
export function updateFromEvent(record: ExecutionRecord, event: AgentEvent): void {
|
|
294
444
|
switch (event.type) {
|
|
295
445
|
// ── text / thinking:流式累积进当前 turn ──
|
|
296
|
-
case "text_delta":
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
case "thinking_delta": {
|
|
301
|
-
currentTurn(record).thinking += event.delta;
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// ── tool_start:push 一个 running 的 InternalToolCall(带 startedTs)──
|
|
306
|
-
case "tool_start": {
|
|
307
|
-
const tc: InternalToolCall = {
|
|
308
|
-
toolName: event.toolName,
|
|
309
|
-
args: event.args,
|
|
310
|
-
result: undefined,
|
|
311
|
-
isError: false,
|
|
312
|
-
_status: "running",
|
|
313
|
-
startedTs: Date.now(),
|
|
314
|
-
};
|
|
315
|
-
const turn = currentTurn(record);
|
|
316
|
-
turn.toolCalls.push(tc);
|
|
317
|
-
indexToolStart(record, turn, event.toolName);
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
446
|
+
case "text_delta":
|
|
447
|
+
return applyTextDelta(record, event);
|
|
448
|
+
case "thinking_delta":
|
|
449
|
+
return applyThinkingDelta(record, event);
|
|
320
450
|
|
|
321
|
-
// ──
|
|
322
|
-
case "
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if (arr !== undefined && arr.length > 0) {
|
|
327
|
-
const item = arr[arr.length - 1];
|
|
328
|
-
arr.pop();
|
|
329
|
-
const tc = item.turn.toolCalls[item.idx];
|
|
330
|
-
if (tc !== undefined && tc._status === "running") {
|
|
331
|
-
matched = [item.turn, item.idx] as const;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
if (matched === undefined) {
|
|
335
|
-
// 兜底:重建 record 的历史 running toolCall(索引未覆盖)、索引项被外部路径
|
|
336
|
-
// 置非 running 等场景——保持与旧实现一致的跨 turn 倒序全扫。
|
|
337
|
-
matched = findRunningToolCall(record, event.toolName);
|
|
338
|
-
}
|
|
339
|
-
if (matched !== undefined) {
|
|
340
|
-
const [turn, i] = matched;
|
|
341
|
-
const tc = turn.toolCalls[i]!;
|
|
342
|
-
tc.args = event.args ?? tc.args;
|
|
343
|
-
tc.result = event.result;
|
|
344
|
-
tc.isError = event.isError ?? false;
|
|
345
|
-
tc._status = event.isError ? "failed" : "done";
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
// 匹配失败(SDK 发了 tool_end 但无对应 tool_start,如外部注入的工具):
|
|
349
|
-
// 直接 push 一个已完成的 InternalToolCall,避免数据丢失。
|
|
350
|
-
currentTurn(record).toolCalls.push({
|
|
351
|
-
toolName: event.toolName,
|
|
352
|
-
args: event.args,
|
|
353
|
-
result: event.result,
|
|
354
|
-
isError: event.isError ?? false,
|
|
355
|
-
_status: event.isError ? "failed" : "done",
|
|
356
|
-
startedTs: Date.now(),
|
|
357
|
-
});
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
451
|
+
// ── tool_start/end:push 进 currentTurn().toolCalls(含完整 result)──
|
|
452
|
+
case "tool_start":
|
|
453
|
+
return applyToolStart(record, event);
|
|
454
|
+
case "tool_end":
|
|
455
|
+
return applyToolEnd(record, event);
|
|
360
456
|
|
|
361
|
-
// ── turn_end:闭合当前 turn
|
|
362
|
-
case "turn_end":
|
|
363
|
-
|
|
364
|
-
turn.closed = true;
|
|
365
|
-
turn.closedTs = Date.now();
|
|
366
|
-
record.turnCount += 1;
|
|
367
|
-
// turn 正常闭合意味着本段执行成功——清掉运行期可能记录的瞬态 error,
|
|
368
|
-
// 避免瞬态 error 恢复后 session-runner 仍据 lastError 误判 success=false。
|
|
369
|
-
// (若 turn_end 后 message_end 报 error,会在 message_end 分支重新写回 lastError。)
|
|
370
|
-
record.lastError = undefined;
|
|
371
|
-
return;
|
|
372
|
-
}
|
|
457
|
+
// ── turn_end:闭合当前 turn ──
|
|
458
|
+
case "turn_end":
|
|
459
|
+
return applyTurnEnd(record);
|
|
373
460
|
|
|
374
|
-
// ── message_end:usage
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
// 到达(或 turn_end 后的滞后 message_end 落到 currentTurn 开的新 turn),
|
|
378
|
-
// 累加保证不丢 usage。getTotalUsage 扁平求和所有 turn,归属 turn 的精确性
|
|
379
|
-
// 不影响最终 total(无消费方读单 turn usage)。
|
|
380
|
-
case "message_end": {
|
|
381
|
-
if (event.usage) {
|
|
382
|
-
const turn = currentTurn(record);
|
|
383
|
-
turn.usageDelta = addUsage(turn.usageDelta, event.usage);
|
|
384
|
-
// totalTokens 累加四项之和(保留旧语义,投影直接读)
|
|
385
|
-
record.totalTokens +=
|
|
386
|
-
(event.usage.input ?? 0) + (event.usage.output ?? 0) +
|
|
387
|
-
(event.usage.cacheRead ?? 0) + (event.usage.cacheWrite ?? 0);
|
|
388
|
-
}
|
|
389
|
-
// message_end 的 error(stopReason=error)也记进 lastError
|
|
390
|
-
if (event.error) {
|
|
391
|
-
record.lastError = event.error;
|
|
392
|
-
}
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
461
|
+
// ── message_end:usage 增量累加 + totalTokens 累加 ──
|
|
462
|
+
case "message_end":
|
|
463
|
+
return applyMessageEnd(record, event);
|
|
395
464
|
|
|
396
|
-
// ── error:存 record.lastError
|
|
397
|
-
case "error":
|
|
398
|
-
record
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
465
|
+
// ── error:存 record.lastError ──
|
|
466
|
+
case "error":
|
|
467
|
+
return applyErrorEvent(record, event);
|
|
401
468
|
|
|
402
469
|
// ── compaction:不产生数据(不变)──
|
|
403
|
-
case "compaction":
|
|
470
|
+
case "compaction":
|
|
404
471
|
return;
|
|
405
|
-
}
|
|
406
472
|
|
|
407
473
|
default: {
|
|
408
474
|
// 穷尽性检查:新增 AgentEvent variant 时编译期报错
|
|
@@ -881,6 +947,46 @@ export function snapshot(record: ExecutionRecord): RecordSnapshot {
|
|
|
881
947
|
/** subprocess JSONL 事件(JSON.parse 结果)。duck-typed,对应 SDK SdkEvent。 */
|
|
882
948
|
type JsonlEvent = Record<string, unknown>;
|
|
883
949
|
|
|
950
|
+
// ── 各 case 翻译器(jsonlToAgentEvent 按 case 分发,每个翻译器单一职责)──
|
|
951
|
+
|
|
952
|
+
/** tool_execution_start → tool_start。toolName 非字符串归一空串(与原实现一致)。 */
|
|
953
|
+
function translateToolExecutionStart(raw: JsonlEvent): AgentEvent[] {
|
|
954
|
+
const toolName = typeof raw.toolName === "string" ? raw.toolName : "";
|
|
955
|
+
return [{ type: "tool_start", toolName, args: raw.args }];
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
/** tool_execution_end → tool_end。isError 仅在 === true 时成立;result 原样透传。 */
|
|
959
|
+
function translateToolExecutionEnd(raw: JsonlEvent): AgentEvent[] {
|
|
960
|
+
const toolName = typeof raw.toolName === "string" ? raw.toolName : "";
|
|
961
|
+
const isError = raw.isError === true;
|
|
962
|
+
return [{
|
|
963
|
+
type: "tool_end",
|
|
964
|
+
toolName,
|
|
965
|
+
args: raw.args,
|
|
966
|
+
result: raw.result as ToolCallResult | undefined,
|
|
967
|
+
isError,
|
|
968
|
+
}];
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
/**
|
|
972
|
+
* message_update → thinking_delta / text_delta。
|
|
973
|
+
* ame.type === "thinking_delta"(delta 非字符串归一空串)
|
|
974
|
+
* 其余 ame(delta 有值)→ text_delta(delta 非字符串 String() 归一)
|
|
975
|
+
* ame 缺失 / delta 缺失 → 不产出。
|
|
976
|
+
*/
|
|
977
|
+
function translateMessageUpdate(raw: JsonlEvent): AgentEvent[] {
|
|
978
|
+
const ame = raw.assistantMessageEvent as Record<string, unknown> | undefined;
|
|
979
|
+
if (ame?.type === "thinking_delta") {
|
|
980
|
+
const delta = typeof ame.delta === "string" ? ame.delta : "";
|
|
981
|
+
return [{ type: "thinking_delta", delta }];
|
|
982
|
+
}
|
|
983
|
+
if (ame !== undefined && ame.delta !== undefined) {
|
|
984
|
+
const delta = typeof ame.delta === "string" ? ame.delta : String(ame.delta);
|
|
985
|
+
return [{ type: "text_delta", delta }];
|
|
986
|
+
}
|
|
987
|
+
return [];
|
|
988
|
+
}
|
|
989
|
+
|
|
884
990
|
/**
|
|
885
991
|
* 把一条 JSONL 事件翻译成 AgentEvent。
|
|
886
992
|
*
|
|
@@ -900,35 +1006,14 @@ export function jsonlToAgentEvent(raw: JsonlEvent): AgentEvent[] {
|
|
|
900
1006
|
case "tool_execution_update":
|
|
901
1007
|
return [];
|
|
902
1008
|
|
|
903
|
-
case "tool_execution_start":
|
|
904
|
-
|
|
905
|
-
return [{ type: "tool_start", toolName, args: raw.args }];
|
|
906
|
-
}
|
|
1009
|
+
case "tool_execution_start":
|
|
1010
|
+
return translateToolExecutionStart(raw);
|
|
907
1011
|
|
|
908
|
-
case "tool_execution_end":
|
|
909
|
-
|
|
910
|
-
const isError = raw.isError === true;
|
|
911
|
-
return [{
|
|
912
|
-
type: "tool_end",
|
|
913
|
-
toolName,
|
|
914
|
-
args: raw.args,
|
|
915
|
-
result: raw.result as ToolCallResult | undefined,
|
|
916
|
-
isError,
|
|
917
|
-
}];
|
|
918
|
-
}
|
|
1012
|
+
case "tool_execution_end":
|
|
1013
|
+
return translateToolExecutionEnd(raw);
|
|
919
1014
|
|
|
920
|
-
case "message_update":
|
|
921
|
-
|
|
922
|
-
if (ame?.type === "thinking_delta") {
|
|
923
|
-
const delta = typeof ame.delta === "string" ? ame.delta : "";
|
|
924
|
-
return [{ type: "thinking_delta", delta }];
|
|
925
|
-
}
|
|
926
|
-
if (ame !== undefined && ame.delta !== undefined) {
|
|
927
|
-
const delta = typeof ame.delta === "string" ? ame.delta : String(ame.delta);
|
|
928
|
-
return [{ type: "text_delta", delta }];
|
|
929
|
-
}
|
|
930
|
-
return [];
|
|
931
|
-
}
|
|
1015
|
+
case "message_update":
|
|
1016
|
+
return translateMessageUpdate(raw);
|
|
932
1017
|
|
|
933
1018
|
case "turn_end": {
|
|
934
1019
|
return [{ type: "turn_end" }];
|