@octopi-agent/engine 0.98.4 → 0.121.10
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/builtin-model-info.js +28 -7
- package/dist/config-admin/agent-presets.d.ts +32 -0
- package/dist/config-admin/agent-presets.js +133 -0
- package/dist/config-admin/discover.d.ts +33 -4
- package/dist/config-admin/discover.js +101 -6
- package/dist/config-admin/env.d.ts +11 -0
- package/dist/config-admin/env.js +41 -0
- package/dist/config-admin/error-copy.d.ts +106 -0
- package/dist/config-admin/error-copy.js +287 -0
- package/dist/config-admin/i18n.d.ts +21 -0
- package/dist/config-admin/i18n.js +19 -0
- package/dist/config-admin/index.d.ts +6 -2
- package/dist/config-admin/index.js +3 -1
- package/dist/config-admin/model-presets.d.ts +14 -0
- package/dist/config-admin/model-presets.js +595 -70
- package/dist/config-admin/onboarding-apply.d.ts +71 -0
- package/dist/config-admin/onboarding-apply.js +235 -0
- package/dist/config-admin/onboarding.d.ts +154 -0
- package/dist/config-admin/onboarding.js +270 -0
- package/dist/config-admin/redact.d.ts +42 -9
- package/dist/config-admin/redact.js +69 -5
- package/dist/config-schema/agent.d.ts +40 -6
- package/dist/config-schema/agent.js +44 -6
- package/dist/config-schema/browser.d.ts +62 -0
- package/dist/config-schema/browser.js +89 -0
- package/dist/config-schema/context.d.ts +12 -37
- package/dist/config-schema/context.js +10 -31
- package/dist/config-schema/engine.d.ts +140 -164
- package/dist/config-schema/engine.js +10 -7
- package/dist/config-schema/governance.d.ts +20 -2
- package/dist/config-schema/governance.js +18 -3
- package/dist/config-schema/models.d.ts +10 -12
- package/dist/config-schema/models.js +16 -3
- package/dist/config-schema/run.d.ts +6 -2
- package/dist/config-schema/run.js +12 -2
- package/dist/config-schema/shared.d.ts +1 -3
- package/dist/config-schema/shared.js +4 -4
- package/dist/config-schema/substrate.d.ts +19 -88
- package/dist/config-schema/substrate.js +30 -107
- package/dist/config.d.ts +161 -123
- package/dist/config.js +109 -7
- package/dist/file-mode.d.ts +67 -0
- package/dist/file-mode.js +182 -0
- package/dist/fork-child.d.ts +82 -0
- package/dist/fork-child.js +123 -0
- package/dist/harness/activation/delegated-run.d.ts +42 -0
- package/dist/harness/activation/delegated-run.js +71 -0
- package/dist/harness/activation/dispatcher.js +11 -0
- package/dist/harness/activation/inbox.d.ts +3 -3
- package/dist/harness/activation/inbox.js +3 -3
- package/dist/harness/activation/index.d.ts +1 -0
- package/dist/harness/activation/run-backend.d.ts +92 -0
- package/dist/harness/activation/run-backend.js +16 -0
- package/dist/harness/activation/run-protocol.d.ts +392 -0
- package/dist/harness/activation/run-protocol.js +19 -0
- package/dist/harness/activation/runtime.d.ts +33 -0
- package/dist/harness/activation/runtime.js +130 -31
- package/dist/harness/activation/types.d.ts +20 -3
- package/dist/harness/agent/builder.d.ts +36 -3
- package/dist/harness/agent/builder.js +206 -46
- package/dist/harness/agent/config-bridge.d.ts +10 -1
- package/dist/harness/agent/config-bridge.js +24 -5
- package/dist/harness/agent/config-integrity.js +19 -2
- package/dist/harness/agent/registered-tool-adapter.d.ts +27 -0
- package/dist/harness/agent/registered-tool-adapter.js +67 -0
- package/dist/harness/agent/wire-agent-builder.d.ts +13 -3
- package/dist/harness/agent/wire-agent-builder.js +35 -9
- package/dist/harness/capabilities/document/backends/plain.js +2 -1
- package/dist/harness/capabilities/document/factory.d.ts +5 -0
- package/dist/harness/capabilities/document/factory.js +1 -0
- package/dist/harness/capabilities/document/legacy/factory.js +3 -2
- package/dist/harness/capabilities/document/port.js +6 -3
- package/dist/harness/capabilities/document/types.d.ts +5 -0
- package/dist/harness/collaboration/autonomous-subsystem/audit/reader.d.ts +17 -0
- package/dist/harness/collaboration/autonomous-subsystem/audit/reader.js +66 -36
- package/dist/harness/collaboration/autonomous-subsystem/audit/writer.d.ts +6 -2
- package/dist/harness/collaboration/autonomous-subsystem/audit/writer.js +27 -7
- package/dist/harness/collaboration/autonomous-subsystem/boundary/validator.js +13 -0
- package/dist/harness/collaboration/autonomous-subsystem/loader.js +2 -1
- package/dist/harness/collaboration/autonomous-subsystem/runtime.d.ts +11 -0
- package/dist/harness/collaboration/autonomous-subsystem/runtime.js +17 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.d.ts +14 -4
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.js +50 -41
- package/dist/harness/collaboration/autonomous-subsystem/sense/input-builder.js +4 -4
- package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.d.ts +2 -2
- package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.js +2 -2
- package/dist/harness/collaboration/autonomous-subsystem/signal/bus.d.ts +6 -0
- package/dist/harness/collaboration/autonomous-subsystem/signal/bus.js +25 -4
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +4 -1
- package/dist/harness/collaboration/autonomous-subsystem/types.d.ts +7 -3
- package/dist/harness/collaboration/delegation/child-run.d.ts +10 -1
- package/dist/harness/collaboration/delegation/child-run.js +38 -6
- package/dist/harness/collaboration/delegation/index.d.ts +1 -1
- package/dist/harness/collaboration/delegation/index.js +1 -1
- package/dist/harness/collaboration/delegation/notifier.d.ts +6 -1
- package/dist/harness/collaboration/delegation/notifier.js +8 -1
- package/dist/harness/collaboration/delegation/registry-types.d.ts +2 -0
- package/dist/harness/collaboration/delegation/service.d.ts +11 -4
- package/dist/harness/collaboration/delegation/service.js +35 -13
- package/dist/harness/collaboration/delegation/store.d.ts +4 -0
- package/dist/harness/collaboration/delegation/store.js +3 -1
- package/dist/harness/collaboration/delegation/types.d.ts +34 -2
- package/dist/harness/collaboration/delegation/types.js +22 -0
- package/dist/harness/collaboration/delegation/wiring.d.ts +2 -0
- package/dist/harness/collaboration/delegation/wiring.js +12 -1
- package/dist/harness/context/budget-allocator.d.ts +26 -18
- package/dist/harness/context/budget-allocator.js +67 -32
- package/dist/harness/context/capabilities/compact/engine.d.ts +44 -11
- package/dist/harness/context/capabilities/compact/engine.js +296 -160
- package/dist/harness/context/capabilities/compact/handover.d.ts +5 -3
- package/dist/harness/context/capabilities/compact/handover.js +7 -5
- package/dist/harness/context/capabilities/compact/index.d.ts +7 -2
- package/dist/harness/context/capabilities/compact/index.js +5 -1
- package/dist/harness/context/capabilities/compact/mechanical.d.ts +130 -0
- package/dist/harness/context/capabilities/compact/mechanical.js +268 -0
- package/dist/harness/context/capabilities/compact/messages.d.ts +60 -0
- package/dist/harness/context/capabilities/compact/messages.js +91 -0
- package/dist/harness/context/capabilities/compact/types.d.ts +134 -19
- package/dist/harness/context/capabilities/compact/types.js +11 -1
- package/dist/harness/context/capabilities/compact/view-split.d.ts +36 -0
- package/dist/harness/context/capabilities/compact/view-split.js +72 -0
- package/dist/harness/context/capabilities/summary/gate.d.ts +19 -2
- package/dist/harness/context/capabilities/summary/gate.js +53 -6
- package/dist/harness/context/capabilities/summary/port.js +0 -1
- package/dist/harness/context/capabilities/summary/registry.js +20 -0
- package/dist/harness/context/capabilities/summary/resolver.d.ts +1 -1
- package/dist/harness/context/capabilities/summary/resolver.js +1 -6
- package/dist/harness/context/capabilities/summary/tool-binding.d.ts +1 -1
- package/dist/harness/context/capabilities/summary/tool-binding.js +16 -12
- package/dist/harness/context/capabilities/summary/tool-bindings.d.ts +13 -0
- package/dist/harness/context/capabilities/summary/tool-bindings.js +22 -0
- package/dist/harness/context/capabilities/summary/types.d.ts +19 -6
- package/dist/harness/context/constitution/default-agents.md +85 -0
- package/dist/harness/context/context-size-ledger.d.ts +143 -0
- package/dist/harness/context/context-size-ledger.js +243 -0
- package/dist/harness/context/default-context-engine.d.ts +290 -120
- package/dist/harness/context/default-context-engine.js +1555 -764
- package/dist/harness/context/index.d.ts +3 -2
- package/dist/harness/context/index.js +2 -1
- package/dist/harness/context/layer-health.d.ts +4 -0
- package/dist/harness/context/layer-health.js +4 -0
- package/dist/harness/context/layer-types.d.ts +72 -0
- package/dist/harness/context/layer-types.js +87 -2
- package/dist/harness/context/layers.d.ts +17 -15
- package/dist/harness/context/layers.js +54 -17
- package/dist/harness/context/message-selector.d.ts +10 -8
- package/dist/harness/context/message-selector.js +49 -27
- package/dist/harness/context/prefix-fingerprint.d.ts +46 -0
- package/dist/harness/context/prefix-fingerprint.js +52 -0
- package/dist/harness/context/summarize.d.ts +2 -2
- package/dist/harness/context/summarize.js +27 -2
- package/dist/harness/context/system-prompt-assembler.d.ts +4 -0
- package/dist/harness/context/system-prompt-assembler.js +7 -3
- package/dist/harness/context/token-constants.d.ts +8 -6
- package/dist/harness/context/token-constants.js +8 -7
- package/dist/harness/context/token-estimate-fns.d.ts +37 -27
- package/dist/harness/context/token-estimate-fns.js +85 -66
- package/dist/harness/context/token-estimator.d.ts +27 -12
- package/dist/harness/context/token-estimator.js +120 -66
- package/dist/harness/context/token-segments.d.ts +88 -0
- package/dist/harness/context/token-segments.js +103 -0
- package/dist/harness/context/tool-result-aging.d.ts +109 -16
- package/dist/harness/context/tool-result-aging.js +220 -55
- package/dist/harness/context/tool-result-body.js +2 -0
- package/dist/harness/context/transform/anchors.d.ts +66 -0
- package/dist/harness/context/transform/anchors.js +125 -0
- package/dist/harness/context/transform/fold.d.ts +56 -0
- package/dist/harness/context/transform/fold.js +135 -0
- package/dist/harness/context/transform/migrate.d.ts +41 -0
- package/dist/harness/context/transform/migrate.js +69 -0
- package/dist/harness/context/transform/types.d.ts +129 -0
- package/dist/harness/context/transform/types.js +19 -0
- package/dist/harness/context/turn-envelope.d.ts +6 -3
- package/dist/harness/context/types.d.ts +196 -119
- package/dist/harness/context/types.js +25 -1
- package/dist/harness/extension/execution-environment/effect-policy.d.ts +4 -3
- package/dist/harness/extension/execution-environment/effect-policy.js +2 -2
- package/dist/harness/extension/execution-environment/ops-audit.d.ts +1 -1
- package/dist/harness/extension/execution-environment/privileged-exec.js +4 -1
- package/dist/harness/extension/execution-environment/resource-limits.d.ts +1 -1
- package/dist/harness/extension/execution-environment/resource-limits.js +1 -1
- package/dist/harness/extension/execution-environment/workspace.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/channel-types.d.ts +28 -2
- package/dist/harness/extension/plugin-ecosystem/channel-types.js +11 -2
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +12 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +25 -0
- package/dist/harness/extension/plugin-ecosystem/commands/builtin.js +28 -1
- package/dist/harness/extension/plugin-ecosystem/commands/index.d.ts +3 -1
- package/dist/harness/extension/plugin-ecosystem/commands/index.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/commands/plugin-bridge.d.ts +12 -1
- package/dist/harness/extension/plugin-ecosystem/commands/plugin-bridge.js +14 -4
- package/dist/harness/extension/plugin-ecosystem/commands/router.d.ts +5 -4
- package/dist/harness/extension/plugin-ecosystem/commands/router.js +23 -14
- package/dist/harness/extension/plugin-ecosystem/commands/types.d.ts +8 -1
- package/dist/harness/extension/plugin-ecosystem/commands/types.js +2 -1
- package/dist/harness/extension/plugin-ecosystem/hooks/adapter.d.ts +54 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/adapter.js +397 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/compaction-integration.d.ts +16 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/compaction-integration.js +66 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/index.d.ts +4 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/index.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/iteration-snapshot.d.ts +11 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/iteration-snapshot.js +54 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/journal.d.ts +9 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/journal.js +22 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/policy-error.d.ts +12 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/policy-error.js +19 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/run-integration.d.ts +36 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/run-integration.js +338 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/services.d.ts +24 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/services.js +139 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/types.d.ts +229 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/types.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/index.d.ts +1 -0
- package/dist/harness/extension/plugin-ecosystem/index.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/api.d.ts +223 -222
- package/dist/harness/extension/plugin-ecosystem/plugins/api.js +218 -203
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.d.ts +95 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.js +170 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.d.ts +178 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.js +314 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.d.ts +78 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.js +80 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/engine-compatibility.d.ts +4 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/engine-compatibility.js +18 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/entry.d.ts +7 -85
- package/dist/harness/extension/plugin-ecosystem/plugins/entry.js +8 -50
- package/dist/harness/extension/plugin-ecosystem/plugins/index.d.ts +6 -23
- package/dist/harness/extension/plugin-ecosystem/plugins/index.js +3 -23
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.d.ts +57 -114
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +279 -282
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +46 -484
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +123 -345
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +50 -223
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +144 -20
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.d.ts +17 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.js +15 -0
- package/dist/harness/extension/plugin-ecosystem/skills/manager.d.ts +6 -1
- package/dist/harness/extension/plugin-ecosystem/skills/manager.js +17 -1
- package/dist/harness/extension/plugin-ecosystem/tools/ask-user.d.ts +3 -0
- package/dist/harness/extension/plugin-ecosystem/tools/ask-user.js +46 -21
- package/dist/harness/extension/plugin-ecosystem/tools/browser-types.d.ts +333 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser-types.js +16 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser.d.ts +64 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser.js +692 -0
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +0 -37
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +108 -4
- package/dist/harness/extension/plugin-ecosystem/tools/delegation.js +36 -8
- package/dist/harness/extension/plugin-ecosystem/tools/file-edit.js +34 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.d.ts +0 -6
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +57 -4
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +81 -13
- package/dist/harness/extension/plugin-ecosystem/tools/http.js +5 -0
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +13 -7
- package/dist/harness/extension/plugin-ecosystem/tools/package-tools.js +126 -14
- package/dist/harness/extension/plugin-ecosystem/tools/platform.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/platform.js +44 -5
- package/dist/harness/extension/plugin-ecosystem/tools/plugin-tool.d.ts +11 -0
- package/dist/harness/extension/plugin-ecosystem/tools/plugin-tool.js +39 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.js +19 -24
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.d.ts +7 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.js +4 -0
- package/dist/harness/governance/accounting/usage-ledger.d.ts +13 -1
- package/dist/harness/governance/accounting/usage-ledger.js +23 -1
- package/dist/harness/governance/credentials/db.d.ts +9 -0
- package/dist/harness/governance/credentials/db.js +122 -31
- package/dist/harness/governance/credentials/store.d.ts +68 -7
- package/dist/harness/governance/credentials/store.js +177 -64
- package/dist/harness/governance/credentials/types.d.ts +13 -2
- package/dist/harness/governance/human-in-the-loop/decision-cache.d.ts +3 -3
- package/dist/harness/governance/human-in-the-loop/decision-cache.js +2 -2
- package/dist/harness/governance/index.d.ts +1 -0
- package/dist/harness/governance/index.js +1 -0
- package/dist/harness/governance/registry/db.d.ts +32 -0
- package/dist/harness/governance/registry/db.js +142 -0
- package/dist/harness/governance/registry/index.d.ts +9 -0
- package/dist/harness/governance/registry/index.js +9 -0
- package/dist/harness/governance/registry/service.d.ts +381 -0
- package/dist/harness/governance/registry/service.js +825 -0
- package/dist/harness/governance/registry/types.d.ts +77 -0
- package/dist/harness/governance/registry/types.js +7 -0
- package/dist/harness/governance/security/capability-enforcer.js +1 -2
- package/dist/harness/governance/security/default-security-guard.d.ts +3 -0
- package/dist/harness/governance/security/default-security-guard.js +12 -1
- package/dist/harness/governance/security/risk-evaluator.d.ts +13 -3
- package/dist/harness/governance/security/risk-evaluator.js +307 -21
- package/dist/harness/governance/security/shell-parser-treesitter.js +12 -4
- package/dist/harness/governance/session-acl/tool-rights.d.ts +1 -0
- package/dist/harness/index.d.ts +17 -15
- package/dist/harness/index.js +9 -9
- package/dist/harness/knowledge/boot-config.d.ts +57 -0
- package/dist/harness/knowledge/boot-config.js +52 -0
- package/dist/harness/knowledge/catalog-types.d.ts +2 -0
- package/dist/harness/knowledge/client.d.ts +77 -9
- package/dist/harness/knowledge/client.js +95 -24
- package/dist/harness/knowledge/db.d.ts +16 -0
- package/dist/harness/knowledge/db.js +112 -41
- package/dist/harness/knowledge/describe-sample.d.ts +23 -0
- package/dist/harness/knowledge/describe-sample.js +58 -0
- package/dist/harness/knowledge/describe.d.ts +8 -0
- package/dist/harness/knowledge/describe.js +18 -2
- package/dist/harness/knowledge/engine-thread.js +11 -7
- package/dist/harness/knowledge/grounding.js +7 -2
- package/dist/harness/knowledge/http-app.d.ts +15 -0
- package/dist/harness/knowledge/http-app.js +117 -25
- package/dist/harness/knowledge/http-bridge.d.ts +15 -0
- package/dist/harness/knowledge/http-bridge.js +19 -0
- package/dist/harness/knowledge/http-params.d.ts +18 -0
- package/dist/harness/knowledge/http-params.js +24 -0
- package/dist/harness/knowledge/index.d.ts +2 -0
- package/dist/harness/knowledge/index.js +1 -0
- package/dist/harness/knowledge/knowledge-serve-child.js +5 -3
- package/dist/harness/knowledge/purge.js +5 -2
- package/dist/harness/knowledge/query-service.d.ts +25 -4
- package/dist/harness/knowledge/query-service.js +79 -43
- package/dist/harness/knowledge/query-worker-client.d.ts +14 -2
- package/dist/harness/knowledge/query-worker-client.js +8 -3
- package/dist/harness/knowledge/query-worker.js +9 -5
- package/dist/harness/knowledge/read-http.d.ts +2 -0
- package/dist/harness/knowledge/read-http.js +14 -3
- package/dist/harness/knowledge/retriever.d.ts +1 -1
- package/dist/harness/knowledge/retriever.js +1 -2
- package/dist/harness/knowledge/serve.d.ts +14 -8
- package/dist/harness/knowledge/serve.js +14 -6
- package/dist/harness/knowledge/source-store.d.ts +101 -5
- package/dist/harness/knowledge/source-store.js +323 -74
- package/dist/harness/knowledge/start-service-process.d.ts +41 -14
- package/dist/harness/knowledge/start-service-process.js +67 -96
- package/dist/harness/knowledge/tenant-gate.d.ts +81 -0
- package/dist/harness/knowledge/tenant-gate.js +167 -0
- package/dist/harness/knowledge/types.d.ts +7 -0
- package/dist/harness/knowledge/writer-service.d.ts +55 -11
- package/dist/harness/knowledge/writer-service.js +200 -30
- package/dist/harness/knowledge/writer-worker-client.d.ts +19 -11
- package/dist/harness/knowledge/writer-worker-client.js +40 -5
- package/dist/harness/knowledge/writer-worker.js +20 -5
- package/dist/harness/memory/backfill-trigger.d.ts +10 -3
- package/dist/harness/memory/backfill-trigger.js +19 -19
- package/dist/harness/memory/cognition-seeds.d.ts +45 -0
- package/dist/harness/memory/cognition-seeds.js +67 -0
- package/dist/harness/memory/cognition.d.ts +12 -3
- package/dist/harness/memory/cognition.js +55 -35
- package/dist/harness/memory/conceptualizer.d.ts +11 -1
- package/dist/harness/memory/conceptualizer.js +10 -1
- package/dist/harness/memory/context-slice.d.ts +42 -0
- package/dist/harness/memory/context-slice.js +197 -0
- package/dist/harness/memory/health-probe.d.ts +8 -2
- package/dist/harness/memory/health-probe.js +10 -13
- package/dist/harness/memory/index.d.ts +5 -1
- package/dist/harness/memory/index.js +3 -1
- package/dist/harness/memory/sqlite/agent-db.d.ts +1 -0
- package/dist/harness/memory/sqlite/agent-db.js +9 -8
- package/dist/harness/memory/sqlite/cognition-store.d.ts +26 -3
- package/dist/harness/memory/sqlite/cognition-store.js +103 -45
- package/dist/harness/memory/sqlite/memory-store.js +4 -0
- package/dist/harness/memory/sqlite/sqlite-vec.d.ts +24 -1
- package/dist/harness/memory/sqlite/sqlite-vec.js +37 -1
- package/dist/harness/memory/sqlite/wisdom-store.js +35 -10
- package/dist/harness/memory/types.d.ts +24 -6
- package/dist/harness/memory/wisdom-formation.d.ts +14 -3
- package/dist/harness/memory/wisdom-formation.js +85 -22
- package/dist/harness/memory/wisdom-gates.d.ts +14 -0
- package/dist/harness/memory/wisdom-gates.js +59 -19
- package/dist/harness/memory/wisdom.js +9 -0
- package/dist/harness/observability/observer/hub.d.ts +10 -0
- package/dist/harness/observability/observer/hub.js +120 -17
- package/dist/harness/observability/observer/types.d.ts +10 -6
- package/dist/harness/run/agent/agent.d.ts +40 -21
- package/dist/harness/run/agent/agent.js +67 -13
- package/dist/harness/run/agent/index.d.ts +1 -1
- package/dist/harness/run/budget/budget.d.ts +28 -10
- package/dist/harness/run/budget/budget.js +38 -15
- package/dist/harness/run/concurrency/tool-validator.js +4 -3
- package/dist/harness/run/model/index.d.ts +3 -1
- package/dist/harness/run/model/index.js +2 -1
- package/dist/harness/run/model/resolver.d.ts +4 -1
- package/dist/harness/run/model/resolver.js +36 -2
- package/dist/harness/run/model/run-scope.d.ts +2 -0
- package/dist/harness/run/model/run-scope.js +7 -0
- package/dist/harness/run/model/types.d.ts +22 -5
- package/dist/harness/run/model/types.js +3 -2
- package/dist/harness/run/model/window-truth.d.ts +37 -0
- package/dist/harness/run/model/window-truth.js +39 -0
- package/dist/harness/run/reliability/context-length-feedback.d.ts +77 -0
- package/dist/harness/run/reliability/context-length-feedback.js +128 -0
- package/dist/harness/run/reliability/harness-events.d.ts +3 -3
- package/dist/harness/run/reliability/model-error-policy.js +4 -0
- package/dist/harness/run/reliability/run-agent.d.ts +32 -4
- package/dist/harness/run/reliability/run-agent.js +216 -63
- package/dist/harness/run/reliability/run-metrics-collector.d.ts +19 -8
- package/dist/harness/run/reliability/run-metrics-collector.js +38 -23
- package/dist/harness/run/run-guard/default-run-guard.js +8 -8
- package/dist/harness/run/run-host.d.ts +302 -0
- package/dist/harness/run/run-host.js +978 -0
- package/dist/harness/run/run-scope.d.ts +48 -4
- package/dist/harness/run/run-scope.js +19 -1
- package/dist/harness/run/runner.d.ts +181 -39
- package/dist/harness/run/runner.js +654 -119
- package/dist/harness/session/attachments/index.d.ts +1 -0
- package/dist/harness/session/attachments/index.js +1 -0
- package/dist/harness/session/attachments/inject.d.ts +21 -10
- package/dist/harness/session/attachments/inject.js +76 -24
- package/dist/harness/session/attachments/recall-index.d.ts +70 -0
- package/dist/harness/session/attachments/recall-index.js +219 -0
- package/dist/harness/session/attachments/service.d.ts +21 -0
- package/dist/harness/session/attachments/service.js +258 -139
- package/dist/harness/session/attachments/types.d.ts +6 -0
- package/dist/harness/session/in-memory-store.js +13 -1
- package/dist/harness/session/schedules/service.d.ts +10 -2
- package/dist/harness/session/schedules/service.js +13 -2
- package/dist/harness/session/tasks/tools.js +12 -0
- package/dist/harness/session/transform-store.d.ts +136 -0
- package/dist/harness/session/transform-store.js +302 -0
- package/dist/harness/session/types.d.ts +11 -15
- package/dist/harness/shared/events/agent-event-map.d.ts +75 -13
- package/dist/harness/shared/events/agent-event-map.js +3 -2
- package/dist/harness/shared/events/scenario-events.d.ts +6 -6
- package/dist/harness/shared/types/agent-definition.d.ts +7 -2
- package/dist/index.d.ts +16 -18
- package/dist/index.js +9 -10
- package/dist/integration/agent-runtime/channel-message-source.d.ts +3 -1
- package/dist/integration/agent-runtime/channel-message-source.js +4 -3
- package/dist/integration/browser/camoufox.d.ts +40 -0
- package/dist/integration/browser/camoufox.js +80 -0
- package/dist/integration/browser/index.d.ts +91 -0
- package/dist/integration/browser/index.js +110 -0
- package/dist/integration/browser/install.d.ts +47 -0
- package/dist/integration/browser/install.js +214 -0
- package/dist/integration/browser/playwright.d.ts +224 -0
- package/dist/integration/browser/playwright.js +925 -0
- package/dist/integration/browser/service.d.ts +41 -0
- package/dist/integration/browser/service.js +181 -0
- package/dist/integration/browser/stealth.d.ts +42 -0
- package/dist/integration/browser/stealth.js +104 -0
- package/dist/integration/observability/metrics.d.ts +6 -6
- package/dist/integration/observability/metrics.js +13 -13
- package/dist/integration/observability/observer-bridge.js +2 -2
- package/dist/integration/observability/trace-collector.d.ts +5 -5
- package/dist/integration/observability/trace-collector.js +34 -35
- package/dist/integration/observability/trace-events.d.ts +3 -3
- package/dist/integration/observability/trace-events.js +3 -3
- package/dist/integration/providers/anthropic-message-normalize.d.ts +20 -0
- package/dist/integration/providers/anthropic-message-normalize.js +48 -0
- package/dist/integration/providers/anthropic.js +41 -3
- package/dist/integration/providers/fetch-timeout.d.ts +17 -0
- package/dist/integration/providers/fetch-timeout.js +24 -0
- package/dist/integration/providers/openai.d.ts +8 -0
- package/dist/integration/providers/openai.js +48 -2
- package/dist/integration/providers/usage.d.ts +1 -1
- package/dist/integration/providers/wire-dump-diff.js +2 -1
- package/dist/integration/providers/wire-dump.d.ts +57 -3
- package/dist/integration/providers/wire-dump.js +119 -6
- package/dist/integration/storage/archive-manager.d.ts +20 -2
- package/dist/integration/storage/archive-manager.js +34 -8
- package/dist/integration/storage/atomic-tmp.d.ts +12 -0
- package/dist/integration/storage/atomic-tmp.js +36 -0
- package/dist/integration/storage/jsonl.d.ts +10 -1
- package/dist/integration/storage/jsonl.js +155 -54
- package/dist/integration/storage/session-index.d.ts +5 -0
- package/dist/integration/storage/session-index.js +25 -4
- package/dist/integration/tools/assemble.d.ts +50 -0
- package/dist/integration/tools/assemble.js +137 -0
- package/dist/paths.d.ts +18 -0
- package/dist/paths.js +36 -0
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +3 -1
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +74 -5
- package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +3 -2
- package/dist/subsystems/cognition-steward/consolidate/config.yaml +11 -2
- package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +6 -3
- package/dist/subsystems/cognition-steward/consolidate/handler.js +266 -28
- package/dist/subsystems/memory-steward/backfill/handler.js +3 -2
- package/dist/subsystems/memory-steward/govern/config.yaml +6 -1
- package/dist/subsystems/wisdom-steward/attribute/config.yaml +5 -1
- package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +8 -1
- package/dist/subsystems/wisdom-steward/formation/config.yaml +4 -0
- package/dist/subsystems/wisdom-steward/formation/handler.js +27 -5
- package/dist/subsystems/wisdom-steward/govern/config.yaml +6 -1
- package/package.json +14 -15
- package/dist/harness/context/hybrid-compressor.d.ts +0 -41
- package/dist/harness/context/hybrid-compressor.js +0 -159
- package/dist/harness/context/llm-summarizer.d.ts +0 -46
- package/dist/harness/context/llm-summarizer.js +0 -249
- package/dist/harness/context/smart-router.d.ts +0 -75
- package/dist/harness/context/smart-router.js +0 -118
- package/dist/harness/context/truncate-compressor.d.ts +0 -28
- package/dist/harness/context/truncate-compressor.js +0 -107
- package/dist/harness/extension/plugin-ecosystem/plugins/capability.d.ts +0 -97
- package/dist/harness/extension/plugin-ecosystem/plugins/capability.js +0 -155
- package/dist/harness/extension/plugin-ecosystem/plugins/hooks.d.ts +0 -7
- package/dist/harness/extension/plugin-ecosystem/plugins/hooks.js +0 -7
- package/dist/harness/extension/plugin-ecosystem/plugins/lifecycle.d.ts +0 -86
- package/dist/harness/extension/plugin-ecosystem/plugins/lifecycle.js +0 -139
|
@@ -3,628 +3,1188 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Harness 层的默认实现,整合所有组件:
|
|
5
5
|
* - MessageSelector:消息选择
|
|
6
|
-
* -
|
|
6
|
+
* - capabilities/compact:压缩执行(交接摘要 + 机械降级阶梯;本引擎只做触发/状态)
|
|
7
7
|
* - BudgetAllocator:预算分配
|
|
8
8
|
* - TokenEstimator:Token 估算
|
|
9
9
|
*
|
|
10
10
|
* 特性:
|
|
11
|
-
* -
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* - **持久转换记录**(arch/context-size-accounting.md §10.18):压缩产物是事实,
|
|
12
|
+
* 不是临时视图。装配 = 原文 + 有效记录的确定性 fold(transform/fold)——没有
|
|
13
|
+
* 游标、没有增量抢救;没被记录覆盖的消息逐字在视图里。回填只在显式撤销时。
|
|
14
|
+
* - fresh 门控只决定**新记录的类型**(summary / degrade),不再决定旧记录是否生效。
|
|
15
|
+
* - 四区域划分(头部保护 + 中间可压缩 + 尾部保护)退为压缩时刻的选段策略:
|
|
16
|
+
* 保护窗决定记录覆盖哪些消息(写进锚点),装配不再看它们。
|
|
17
|
+
* - 边界对齐(不拆分 tool_call/tool_result 对)——同上,压缩时刻生效。
|
|
14
18
|
* - 迭代式摘要(传入上次摘要,更新而非重建)
|
|
15
19
|
* - 实际 token 数回写
|
|
16
20
|
*/
|
|
17
21
|
import { getTextContent } from '@octopi-agent/core/types.js';
|
|
22
|
+
import { contextSizeSnapshot } from './types.js';
|
|
18
23
|
import { HeuristicTokenEstimator, estimateLLMMessages } from './token-estimator.js';
|
|
24
|
+
import { ContextSizeLedger } from './context-size-ledger.js';
|
|
19
25
|
import { toolResultBody } from './tool-result-body.js';
|
|
20
|
-
import {
|
|
21
|
-
import { agedToolResultPointer, agedToolArgDigest, applyAgingToMessages, computeToolResultAgingPlan, resolveToolResultAgingOptions, } from './tool-result-aging.js';
|
|
26
|
+
import { agedToolArgDigest, ageOneToolResult, shouldAgeArgs, applyAgingToMessages, computeToolResultAgingPlan, resolveToolResultAgingOptions, } from './tool-result-aging.js';
|
|
22
27
|
import { DefaultMessageSelector } from './message-selector.js';
|
|
23
|
-
import { HybridCompressor } from './hybrid-compressor.js';
|
|
24
28
|
import { DefaultBudgetAllocator } from './budget-allocator.js';
|
|
25
|
-
import { SmartRouter } from './smart-router.js';
|
|
26
29
|
import { compactStateKey } from './compact-key.js';
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
30
|
+
import { HookedCompactEngine } from '../extension/plugin-ecosystem/hooks/compaction-integration.js';
|
|
31
|
+
import { isTurnBoundaryAnchor, isScheduleNotice } from './layer-types.js';
|
|
32
|
+
import { fingerprintRequestView, diffPrefixFingerprint } from './prefix-fingerprint.js';
|
|
29
33
|
import { createCompactEngine } from './capabilities/compact/index.js';
|
|
34
|
+
import { alignViewSplit, isMechanicalNotice, toolExecutionLedgerTrim, semanticHistory, } from './capabilities/compact/index.js';
|
|
35
|
+
import { anchorRange } from './transform/anchors.js';
|
|
36
|
+
import { foldTransformView } from './transform/fold.js';
|
|
37
|
+
import { legacySnapshotToRecords } from './transform/migrate.js';
|
|
38
|
+
import { InMemoryTransformStore } from '../session/transform-store.js';
|
|
30
39
|
// ── 引擎实现 ──
|
|
31
|
-
/**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
/** LLM message content → 纯文本投影(回声比对源用)。
|
|
41
|
+
*
|
|
42
|
+
* string 原样返回;content block 数组取各 text block 拼接(缺 text 的块按占位跳过,
|
|
43
|
+
* 绝不退化成 "[object Object]");其他形态走 String 兜底。
|
|
44
|
+
*
|
|
45
|
+
* @param content - LLM 消息 content(string | block[] | null)
|
|
46
|
+
* @returns 正文文本
|
|
47
|
+
*/
|
|
48
|
+
function llmContentToText(content) {
|
|
49
|
+
if (typeof content === 'string')
|
|
50
|
+
return content;
|
|
51
|
+
if (Array.isArray(content)) {
|
|
52
|
+
const parts = [];
|
|
53
|
+
for (const block of content) {
|
|
54
|
+
if (block === null || typeof block !== 'object')
|
|
55
|
+
continue;
|
|
56
|
+
const text = block.text;
|
|
57
|
+
if (typeof text === 'string' && text)
|
|
58
|
+
parts.push(text);
|
|
59
|
+
}
|
|
60
|
+
return parts.join('\n');
|
|
61
|
+
}
|
|
62
|
+
if (content === null || content === undefined)
|
|
63
|
+
return '';
|
|
64
|
+
return String(content);
|
|
37
65
|
}
|
|
38
|
-
/** 供调用方/测试引用 */
|
|
39
|
-
export { wrapContextSummary };
|
|
40
66
|
export class DefaultContextEngine {
|
|
41
67
|
info = {
|
|
42
68
|
id: 'default',
|
|
43
69
|
name: 'Default Context Engine',
|
|
44
70
|
};
|
|
45
71
|
config;
|
|
46
|
-
router;
|
|
47
72
|
states = new Map();
|
|
73
|
+
/** 转换记录账本缓存(每 (sessionId, agentId) 加载一次;append 同步进缓存) */
|
|
74
|
+
ledgers = new Map();
|
|
75
|
+
/** 孤儿记录告警去重(键:session|agent|seq[|partial]) */
|
|
76
|
+
orphanWarned = new Set();
|
|
77
|
+
/** P1-a 校准台账:ratio 按 model 分键,observe/calibrate 唯一出入口 */
|
|
78
|
+
sizeLedger = new ContextSizeLedger();
|
|
79
|
+
/**
|
|
80
|
+
* 最近一次会话请求的工具面前缀指纹(按 sessionId)。
|
|
81
|
+
*
|
|
82
|
+
* 只为可观测:摘要请求的前缀必须与它同形才能命中缓存。tools 在一个会话生命周期内
|
|
83
|
+
* 近乎不变,一旦摘要请求的 toolsHash 与之不同,就是真问题(2026-11-12 事故形状)。
|
|
84
|
+
* 只告警不断言——摘要前缀与「当前」请求本就可能不同(折叠阶梯)。
|
|
85
|
+
*/
|
|
86
|
+
/** 最近一次工具面哈希(按数组引用缓存,避免每 step 重复 sha1) */
|
|
87
|
+
lastToolsHash;
|
|
88
|
+
/**
|
|
89
|
+
* 最近一次**会话请求**的前缀指纹(tools + system + messages)
|
|
90
|
+
*
|
|
91
|
+
* 用途:摘要请求发出前比对——任一项不同,前缀缓存必 miss。
|
|
92
|
+
* 2026-11-13 事故正是 tools/system 相同而 messages 半区(老化形态)分叉,
|
|
93
|
+
* 旧实现只存 tools 哈希,看不见这类差异。
|
|
94
|
+
*/
|
|
95
|
+
lastRequestPrefix = new Map();
|
|
48
96
|
constructor(config) {
|
|
49
97
|
const tokenEstimator = config?.tokenEstimator ?? new HeuristicTokenEstimator();
|
|
50
98
|
this.config = {
|
|
51
99
|
messageSelector: config?.messageSelector ?? new DefaultMessageSelector(),
|
|
52
|
-
compressor: config?.compressor ?? new HybridCompressor(),
|
|
53
100
|
budgetAllocator: config?.budgetAllocator ?? new DefaultBudgetAllocator(),
|
|
54
101
|
tokenEstimator,
|
|
55
|
-
router: config?.router ?? {},
|
|
56
102
|
protectFirstN: config?.protectFirstN ?? 5,
|
|
57
103
|
protectLastN: config?.protectLastN ?? 20,
|
|
58
104
|
summaryBudget: config?.summaryBudget ?? {},
|
|
59
105
|
proactiveCompactRatio: config?.proactiveCompactRatio ?? 0.6,
|
|
60
106
|
proactiveCooldownMs: config?.proactiveCooldownMs ?? 30_000,
|
|
107
|
+
summarizeTimeoutMs: config?.summarizeTimeoutMs ?? 300_000,
|
|
61
108
|
historyAging: resolveToolResultAgingOptions(config?.historyAging),
|
|
62
109
|
targetTokens: config?.targetTokens ?? 200_000,
|
|
63
110
|
// <1 或非法值回落缺省——0 会把目标线压成 0(压缩风暴),不是「禁用」语义
|
|
64
111
|
compactFallbackRatio: config?.compactFallbackRatio != null && config.compactFallbackRatio >= 1
|
|
65
112
|
? config.compactFallbackRatio
|
|
66
113
|
: 3,
|
|
67
|
-
|
|
68
|
-
|
|
114
|
+
// 非法/0 值回落缺省 50(0 会退化成 protectLastN,不是「关闭」语义——评审 m3);
|
|
115
|
+
// 有效值再与 protectLastN 取 max(活跃窗口不得窄于常态保护尾)
|
|
116
|
+
midTurnProtectLastN: config?.midTurnProtectLastN != null && config.midTurnProtectLastN >= 1
|
|
117
|
+
? config.midTurnProtectLastN
|
|
118
|
+
: 50,
|
|
119
|
+
transformStore: config?.transformStore ?? new InMemoryTransformStore(),
|
|
120
|
+
compactEngine: (() => {
|
|
121
|
+
const engine = config?.compactEngine ?? createCompactEngine({
|
|
69
122
|
defaultProtectHead: config?.protectFirstN ?? 5,
|
|
70
123
|
defaultProtectTail: config?.protectLastN ?? 20,
|
|
71
124
|
estimator: {
|
|
72
125
|
estimateMessages: (m) => tokenEstimator.estimateMessages(m),
|
|
73
126
|
estimateText: (t) => tokenEstimator.estimateText(t),
|
|
74
127
|
},
|
|
75
|
-
})
|
|
128
|
+
});
|
|
129
|
+
if (!config?.extensionHooks)
|
|
130
|
+
return engine;
|
|
131
|
+
return new HookedCompactEngine(engine, config.extensionHooks, config.extensionIdentity ?? { sessionId: 'default' });
|
|
132
|
+
})(),
|
|
76
133
|
};
|
|
77
|
-
this.router = new SmartRouter({
|
|
78
|
-
...this.config.router,
|
|
79
|
-
tokenEstimator,
|
|
80
|
-
});
|
|
81
134
|
}
|
|
82
135
|
/**
|
|
83
136
|
* 组装上下文
|
|
84
137
|
*
|
|
85
|
-
*
|
|
138
|
+
* 核心方法。装配 = 原文 + 有效转换记录的确定性 fold;需要收缩时由
|
|
139
|
+
* proactive/overflow 路径产出新记录(记录先落盘、后发视图)。
|
|
86
140
|
*
|
|
87
|
-
*
|
|
141
|
+
* 路由(Q2 B′ 后两路由):
|
|
88
142
|
* - fits: 不需要压缩
|
|
89
|
-
* -
|
|
90
|
-
* - compact_only: 只做 LLM 摘要
|
|
91
|
-
* - compact_then_truncate: 先 LLM 摘要,再截断
|
|
143
|
+
* - compact: 走 compact 能力(有 summarize 出真摘要;无 summarize / 失败走机械降级阶梯)
|
|
92
144
|
*/
|
|
93
145
|
async assemble(params) {
|
|
94
|
-
const { sessionId, agentId, messages, systemPrompt, tools, contextWindow, maxOutputTokens, compactTargetTokens,
|
|
95
|
-
// E4
|
|
146
|
+
const { sessionId, agentId, messages, systemPrompt, tools, toolAgers, toolArgAgers, contextWindow, maxOutputTokens, compactTargetTokens, tokenEstimator, summarize, taskSnapshot, emit, loadCompactState, } = params;
|
|
147
|
+
// E4:引擎内部状态键 = (sessionId, agentId)
|
|
96
148
|
const stateKey = compactStateKey(sessionId, agentId);
|
|
97
|
-
|
|
98
|
-
|
|
149
|
+
const ledger = await this.loadLedger(sessionId, agentId, messages, loadCompactState);
|
|
150
|
+
this.restoreGatesFromLedger(stateKey, ledger);
|
|
151
|
+
// F1-B2:老化计划基于**完整历史**(append-only)一次算好、随视图复用。
|
|
99
152
|
// 估算(estimateLLMMessages)作用于老化后的 LLM 视图,尺子与实际请求一致。
|
|
100
|
-
const agingPlan = computeToolResultAgingPlan(messages, this.config.historyAging);
|
|
153
|
+
const agingPlan = computeToolResultAgingPlan(messages, this.config.historyAging, { toolAgers, toolArgAgers });
|
|
101
154
|
// 窗口未知且无显式 compactTargetTokens:跳过基于预算的自动压缩/截断
|
|
102
155
|
const budgetCap = params.tokenBudget ?? contextWindow ?? compactTargetTokens;
|
|
103
156
|
const windowKnown = contextWindow != null && contextWindow > 0;
|
|
104
157
|
const targetKnown = compactTargetTokens != null && compactTargetTokens > 0;
|
|
105
|
-
|
|
106
|
-
//
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const restored = await loadCompactState(sessionId);
|
|
113
|
-
if (restored?.summary || restored?.lastProactiveMessageCount != null) {
|
|
114
|
-
this.states.set(stateKey, {
|
|
115
|
-
...existing,
|
|
116
|
-
previousSummary: restored.summary ?? existing?.previousSummary,
|
|
117
|
-
lastProactiveMessageCount: restored.lastProactiveMessageCount ?? existing?.lastProactiveMessageCount,
|
|
118
|
-
lastProactiveCoverEnd: restored.lastProactiveCoverEnd ?? existing?.lastProactiveCoverEnd,
|
|
119
|
-
lastProactiveTokens: restored.lastProactiveTokens ?? existing?.lastProactiveTokens,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
158
|
+
// P1-a(R1):决策读数一律校准——estimator 参数流读到的都是校准值;原始估算只供
|
|
159
|
+
// 校准分母(state.lastEstimatedTokens,「system+tools+视图」请求口径)。
|
|
160
|
+
const rawEstimator = tokenEstimator ?? this.config.tokenEstimator;
|
|
161
|
+
const modelKey = params.modelKey ?? 'unknown';
|
|
162
|
+
const estimator = this.sizeLedger.calibratedEstimator(this.calKey(stateKey, modelKey), rawEstimator);
|
|
163
|
+
// 装配唯一实现:fold(原文 + 有效记录的确定性应用)
|
|
164
|
+
let folded = this.foldView(messages, ledger);
|
|
124
165
|
// 未知窗口:无 token 预算 → 不按 budget 截消息。
|
|
125
|
-
// F1-B3:targetTokens>0
|
|
126
|
-
// targetTokens=0:只挂窗口比例线(proactiveCompactRatio);该模式窗口未知时跳过 proactive。
|
|
127
|
-
// 已有结构压缩摘要时,用 head + summary + tail 视图
|
|
166
|
+
// F1-B3:targetTokens>0 时兜底压缩与窗口无关,窗口未知照样执行(工作目标解耦)。
|
|
128
167
|
if (!windowKnown && !targetKnown && budgetCap == null) {
|
|
129
168
|
const proactive = this.config.targetTokens > 0
|
|
130
169
|
? await this.applyProactiveCompact({
|
|
131
170
|
sessionId,
|
|
171
|
+
agentId,
|
|
132
172
|
stateKey,
|
|
133
173
|
messages,
|
|
174
|
+
ledger,
|
|
134
175
|
messagesBudget: 0,
|
|
135
176
|
agingPlan,
|
|
136
177
|
summarize,
|
|
178
|
+
taskSnapshot,
|
|
137
179
|
estimator,
|
|
180
|
+
rawEstimator,
|
|
181
|
+
calKey: this.calKey(stateKey, modelKey),
|
|
138
182
|
emit,
|
|
139
183
|
systemPrompt,
|
|
140
184
|
tools,
|
|
141
185
|
contextWindow,
|
|
142
186
|
maxOutputTokens,
|
|
187
|
+
signal: params.signal,
|
|
143
188
|
})
|
|
144
|
-
:
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
189
|
+
: undefined;
|
|
190
|
+
if (proactive)
|
|
191
|
+
folded = proactive.fold;
|
|
192
|
+
// 结构回放(复用轮):记录生效但本轮零新压缩——cached 对不入会话流(既有语义)
|
|
193
|
+
if (!proactive && folded.applied.some((r) => r.spans.length > 0)) {
|
|
194
|
+
emit?.({ type: 'context.compact.start', sessionId, reason: 'proactive' });
|
|
195
|
+
emit?.({
|
|
196
|
+
type: 'context.compact.end',
|
|
197
|
+
sessionId,
|
|
198
|
+
reason: 'proactive',
|
|
199
|
+
tokensBefore: 0,
|
|
200
|
+
tokensAfter: 0,
|
|
201
|
+
cached: true,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
const view = {
|
|
205
|
+
messages: this.buildLlmMessages(folded.view, systemPrompt, agingPlan),
|
|
206
|
+
tools,
|
|
207
|
+
};
|
|
208
|
+
// 会话请求基线(摘要路径以它为比对对象;messages 半区含老化形态)
|
|
209
|
+
this.recordSessionRequest(stateKey, view);
|
|
210
|
+
const llmMessages = view.messages;
|
|
211
|
+
const rawEstimatedTokens = estimateLLMMessages(llmMessages) + rawEstimator.estimateTools(tools);
|
|
212
|
+
const decision = this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), rawEstimatedTokens, this.states.get(stateKey)?.lastUsageAnchor);
|
|
213
|
+
this.freezeUsagePair(stateKey, rawEstimatedTokens, modelKey);
|
|
154
214
|
return {
|
|
155
215
|
messages: llmMessages,
|
|
156
|
-
estimatedTokens:
|
|
216
|
+
estimatedTokens: decision.tokens,
|
|
217
|
+
readingSource: decision.source,
|
|
157
218
|
systemPrompt,
|
|
158
|
-
droppedSummary:
|
|
159
|
-
|
|
160
|
-
|
|
219
|
+
droppedSummary: proactive?.droppedSummary ??
|
|
220
|
+
(folded.applied.some((r) => r.spans.length > 0)
|
|
221
|
+
? 'Structural compact view (contextWindow unknown)'
|
|
222
|
+
: undefined),
|
|
223
|
+
tools,
|
|
224
|
+
summary: this.deriveSummary(ledger),
|
|
161
225
|
};
|
|
162
226
|
}
|
|
163
227
|
// 1. 计算可用预算(有窗口或 compactTargetTokens 时)
|
|
228
|
+
// 不变量:inputLimit + outputReserve ≤ effectiveLimit;强制点一律用 messagesLimit。
|
|
164
229
|
const tokenBudgetForAlloc = budgetCap;
|
|
230
|
+
const systemPromptTokensForAlloc = estimator.estimateText(systemPrompt);
|
|
231
|
+
const toolTokensForAlloc = estimator.estimateTools(tools);
|
|
165
232
|
const budget = this.config.budgetAllocator.allocate({
|
|
166
233
|
tokenBudget: tokenBudgetForAlloc,
|
|
167
234
|
contextWindow,
|
|
168
|
-
|
|
169
|
-
|
|
235
|
+
maxOutputTokens: params.maxOutputTokens,
|
|
236
|
+
systemPromptTokens: systemPromptTokensForAlloc,
|
|
237
|
+
toolTokens: toolTokensForAlloc,
|
|
170
238
|
});
|
|
239
|
+
// 不变量不可满足(窗口连 system + tools + 输出预留都放不下):
|
|
240
|
+
// 这是配置问题,不在这里 fail-loud——8k 窗口 + 大 persona 是真实存在的部署,
|
|
241
|
+
// 直接抛错会让每次 turn 都崩。改为用最小可运行预算继续,并由 `infeasible`
|
|
242
|
+
// 如实标记(自定义 allocator / 上层可据此告警);真实越界仍由 provider 拒绝暴露。
|
|
243
|
+
const effectiveMessagesLimit = budget.infeasible ? 1000 : budget.messagesLimit;
|
|
244
|
+
// P3 红线 7 族:T3 保护段超限 = D 类结构性超预算(配置病)——不管视图大小、
|
|
245
|
+
// 不依赖是否进入收敛阶梯(早退路径也必须可见),每次触发打告警,不当日常。
|
|
246
|
+
if (budget.infeasible) {
|
|
247
|
+
console.warn(`[overflow] contextWindow 配置病:保护段超限(窗口连 system+tools+输出预留都放不下)——换 model 或修配置`);
|
|
248
|
+
emit?.({
|
|
249
|
+
type: 'context.overflow.alert',
|
|
250
|
+
sessionId,
|
|
251
|
+
symptom: 'structural_over_budget',
|
|
252
|
+
message: `[overflow] contextWindow 配置病:保护段超限(窗口连 system+tools+输出预留都放不下)——换 model 或修配置`,
|
|
253
|
+
contextWindow,
|
|
254
|
+
trigger: 'T3',
|
|
255
|
+
});
|
|
256
|
+
}
|
|
171
257
|
// 1b. 主动摘要:仅在有预算时启用(基于窗口/显式 target 的自动压缩)
|
|
172
258
|
const proactive = await this.applyProactiveCompact({
|
|
173
259
|
sessionId,
|
|
260
|
+
agentId,
|
|
174
261
|
stateKey,
|
|
175
262
|
messages,
|
|
176
|
-
|
|
263
|
+
ledger,
|
|
264
|
+
// 决策尺同源(与守门 fits 同一 effectiveMessagesLimit):budget.messagesBudget 在
|
|
265
|
+
// infeasible 时为 0,会被窗口比例线读成「未知预算→Infinity→永不触发」——小预算
|
|
266
|
+
// (最该语义压缩的场景)反而只走 overflow 硬砍。unbounded 时 effectiveMessagesLimit
|
|
267
|
+
// 为 Infinity,比例线禁用语义不变。
|
|
268
|
+
// 决策尺同源(评审 R6):与守门的 effectiveMessagesLimit 同一口径;但 infeasible
|
|
269
|
+
// (固定开销+输出预留就超窗)时比例线无意义——摘要请求同样装不下(Q1 同一物理理由),
|
|
270
|
+
// 保持禁用(0 → Infinity 线)交给 overflow 纯机械收敛,不唤醒注定失败的 LLM 摘要。
|
|
271
|
+
messagesBudget: budget.infeasible ? 0 : effectiveMessagesLimit,
|
|
177
272
|
agingPlan,
|
|
178
273
|
summarize,
|
|
274
|
+
taskSnapshot,
|
|
179
275
|
estimator,
|
|
276
|
+
rawEstimator,
|
|
277
|
+
calKey: this.calKey(stateKey, modelKey),
|
|
180
278
|
emit,
|
|
181
279
|
systemPrompt,
|
|
182
280
|
tools,
|
|
183
281
|
contextWindow,
|
|
184
282
|
maxOutputTokens,
|
|
283
|
+
signal: params.signal,
|
|
185
284
|
});
|
|
186
|
-
|
|
285
|
+
folded = proactive.fold;
|
|
286
|
+
let view = proactive.view;
|
|
187
287
|
const proactiveDroppedSummary = proactive.droppedSummary;
|
|
188
288
|
// 2. 四区域消息选择(MessageSelector)
|
|
189
|
-
|
|
190
|
-
|
|
289
|
+
// 强制点用 messagesLimit(硬)。尺子与作用域同源:切分按视图,度量按老化视图。
|
|
290
|
+
const measureMessages = applyAgingToMessages(view, agingPlan, this.config.historyAging);
|
|
291
|
+
const selection = this.config.messageSelector.select(view, {
|
|
292
|
+
maxTokens: effectiveMessagesLimit,
|
|
191
293
|
protectFirstN: this.config.protectFirstN,
|
|
192
294
|
protectLastN: this.config.protectLastN,
|
|
295
|
+
measureMessages: measureMessages === view ? undefined : measureMessages,
|
|
193
296
|
}, estimator);
|
|
194
|
-
// 3.
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
297
|
+
// 3. 守门人判据 = 「装得下」(Q2 无目标线:与 selector 的溢出段概念无关):
|
|
298
|
+
// 发送视图(fold+overflow aging)估算 ≤ 决策预算即无需压缩。
|
|
299
|
+
const overflowAgingOpts = resolveToolResultAgingOptions(this.config.historyAging);
|
|
300
|
+
const overflowAgingPlan = computeToolResultAgingPlan(messages, overflowAgingOpts, {
|
|
301
|
+
toolAgers,
|
|
302
|
+
toolArgAgers,
|
|
303
|
+
ignoreTurnBoundary: true,
|
|
304
|
+
});
|
|
305
|
+
// overflow 阶梯的统一出口读数:每条分支都只算一次 view + 一次估算 + 一次决策。
|
|
306
|
+
// 此前同一表达式在 4 个分支内被手抄 4 次(每分支 3–4 遍),是「改一边忘另一边」的形状。
|
|
307
|
+
const overflowExit = (ms) => {
|
|
308
|
+
const requestView = {
|
|
309
|
+
messages: this.buildLlmMessages(ms, systemPrompt, overflowAgingPlan),
|
|
310
|
+
tools,
|
|
311
|
+
};
|
|
312
|
+
const rawTokens = estimateLLMMessages(requestView.messages) + rawEstimator.estimateTools(tools);
|
|
313
|
+
const decision = this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), rawTokens, this.states.get(stateKey)?.lastUsageAnchor);
|
|
314
|
+
return {
|
|
315
|
+
messages: requestView.messages,
|
|
316
|
+
estimatedTokens: decision.tokens,
|
|
317
|
+
readingSource: decision.source,
|
|
318
|
+
requestView,
|
|
319
|
+
};
|
|
320
|
+
};
|
|
321
|
+
const fits = (ms) => estimator.estimateMessages(applyAgingToMessages(ms, overflowAgingPlan, overflowAgingOpts)) <=
|
|
322
|
+
effectiveMessagesLimit;
|
|
323
|
+
if (selection.overflow.length === 0 && fits(view)) {
|
|
324
|
+
const sessionView = {
|
|
325
|
+
messages: this.buildLlmMessages(view, systemPrompt, agingPlan),
|
|
326
|
+
tools,
|
|
327
|
+
};
|
|
328
|
+
// 会话请求基线:摘要路径以它为比对对象(messages 半区含老化形态)
|
|
329
|
+
this.recordSessionRequest(stateKey, sessionView);
|
|
330
|
+
const llmMessages = sessionView.messages;
|
|
331
|
+
const rawEstimatedTokens = estimateLLMMessages(llmMessages) + rawEstimator.estimateTools(tools);
|
|
332
|
+
const decision = this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), rawEstimatedTokens, this.states.get(stateKey)?.lastUsageAnchor);
|
|
333
|
+
this.freezeUsagePair(stateKey, rawEstimatedTokens, modelKey);
|
|
203
334
|
return {
|
|
204
335
|
messages: llmMessages,
|
|
205
|
-
estimatedTokens,
|
|
336
|
+
estimatedTokens: decision.tokens,
|
|
337
|
+
readingSource: decision.source,
|
|
206
338
|
systemPrompt,
|
|
207
339
|
droppedSummary: proactiveDroppedSummary,
|
|
208
|
-
|
|
209
|
-
|
|
340
|
+
tools,
|
|
341
|
+
summary: this.deriveSummary(ledger),
|
|
210
342
|
};
|
|
211
343
|
}
|
|
212
|
-
// 4.
|
|
213
|
-
//
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
const routing = this.router.evaluate(selection.overflow, 0, !!summarize);
|
|
217
|
-
// 5. 压缩溢出消息
|
|
344
|
+
// 4. 纯机械收敛阶梯(Q1–Q3,arch/overflow-redesign.md §3):零 LLM、无目标线、
|
|
345
|
+
// 无冷却/名额(Q1:超窗口时 provider 无法做摘要,物理不可行)。丢弃次序原则
|
|
346
|
+
// (从最可再生到最不可再生):数据细节(长值)→ 执行记录(工具执行→purpose 账本)
|
|
347
|
+
// → 工作窗口(tail 保护档 50→20→5)→ 放弃(L3 报错)。
|
|
218
348
|
const state = this.states.get(stateKey);
|
|
219
|
-
const previousSummary = state?.previousSummary;
|
|
220
|
-
let compressedOverflow;
|
|
221
349
|
let droppedSummary;
|
|
222
|
-
let
|
|
223
|
-
//
|
|
224
|
-
//
|
|
225
|
-
//
|
|
226
|
-
|
|
227
|
-
|
|
350
|
+
let lastDroppedCount = 0; // MN4:droppedSummary 报实际账本化条数
|
|
351
|
+
// P3 红线 7(arch/overflow-redesign.md §2.3):守门人触发 = 罕见异常信号,每次
|
|
352
|
+
// 触发打告警、不当日常。前馈溢出(视图超已知窗口)= A 类症状——proactive 本该
|
|
353
|
+
// 在到达窗口前压缩而没做到。T3(infeasible)告警已在预算判定点发出,不重复。
|
|
354
|
+
if (!budget.infeasible) {
|
|
355
|
+
console.warn(`[overflow] 前馈溢出触发收敛阶梯:proactive 失效(视图超已知窗口而未被提前压缩)——本信号罕见,出现即异常`);
|
|
356
|
+
emit?.({
|
|
357
|
+
type: 'context.overflow.alert',
|
|
358
|
+
sessionId,
|
|
359
|
+
symptom: 'proactive_failed',
|
|
360
|
+
message: `[overflow] 前馈溢出触发收敛阶梯:proactive 失效(视图超已知窗口而未被提前压缩)——本信号罕见,出现即异常`,
|
|
361
|
+
estimatedTokens: rawEstimator.estimateMessages(view),
|
|
362
|
+
contextWindow,
|
|
363
|
+
trigger: 'T1',
|
|
364
|
+
});
|
|
365
|
+
}
|
|
228
366
|
const overflowStart = Date.now();
|
|
229
|
-
|
|
367
|
+
// 事件计量口径(M3 统一,报告尺 reportRequestTokens):compact 对 = 全请求估算
|
|
368
|
+
//(fold+老化+system+tools,未校准)、同一集合前后对——与 manual before/after 同式。
|
|
369
|
+
const compactTokensBefore = this.reportRequestTokens(view, systemPrompt, tools, overflowAgingPlan, rawEstimator);
|
|
230
370
|
emit?.({
|
|
231
371
|
type: 'context.compact.start',
|
|
232
372
|
sessionId,
|
|
233
373
|
reason: 'overflow',
|
|
234
|
-
tokensBefore:
|
|
374
|
+
tokensBefore: compactTokensBefore,
|
|
235
375
|
});
|
|
376
|
+
// M-1:上游已中止 → 跳过本轮全部收敛动作、零写入;中止收尾必须发终态事件,
|
|
377
|
+
// 否则 store 的 compact.active 永久悬挂(UI 卡「进行中」)。
|
|
378
|
+
if (params.signal?.aborted) {
|
|
379
|
+
emit?.({
|
|
380
|
+
type: 'context.compact.end',
|
|
381
|
+
sessionId,
|
|
382
|
+
reason: 'overflow',
|
|
383
|
+
durationMs: Date.now() - overflowStart,
|
|
384
|
+
cancelled: true,
|
|
385
|
+
});
|
|
386
|
+
const exit = overflowExit(view);
|
|
387
|
+
this.freezeUsagePair(stateKey, estimateLLMMessages(exit.messages) + rawEstimator.estimateTools(tools), modelKey); // 装配出口=配对冻结点(早退轮也冻,防丢样)
|
|
388
|
+
return {
|
|
389
|
+
messages: exit.messages, // MJ1:与 fits 同源(overflow 豁免 aging)
|
|
390
|
+
estimatedTokens: exit.estimatedTokens,
|
|
391
|
+
readingSource: exit.readingSource,
|
|
392
|
+
systemPrompt,
|
|
393
|
+
droppedSummary: proactiveDroppedSummary,
|
|
394
|
+
tools,
|
|
395
|
+
summary: this.deriveSummary(ledger),
|
|
396
|
+
};
|
|
397
|
+
}
|
|
236
398
|
try {
|
|
237
|
-
//
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
summarize,
|
|
258
|
-
tokenEstimator: estimator,
|
|
259
|
-
llmPrefix: overflowLlmPrefix,
|
|
260
|
-
// F8 统一契约:预算/折叠区间/窗口/事件与 manual、proactive 同源
|
|
261
|
-
summaryBudget: this.config.summaryBudget,
|
|
262
|
-
foldRange: {
|
|
263
|
-
replacedCount: selection.overflow.length,
|
|
264
|
-
total: workingMessages.length,
|
|
265
|
-
preservedTail: selection.tail.length,
|
|
266
|
-
},
|
|
267
|
-
contextWindow: params.contextWindow,
|
|
268
|
-
maxOutputTokens: params.maxOutputTokens,
|
|
269
|
-
emit: emit
|
|
270
|
-
? (e) => emit({
|
|
271
|
-
type: 'context.compact.summarize_fallback',
|
|
272
|
-
sessionId,
|
|
273
|
-
reason: 'overflow',
|
|
274
|
-
fallback: e.reason,
|
|
275
|
-
cached: e.cached,
|
|
276
|
-
})
|
|
277
|
-
: undefined,
|
|
278
|
-
});
|
|
279
|
-
overflowFresh = compressed.fresh === true;
|
|
280
|
-
compressedOverflow = compressed.result;
|
|
281
|
-
droppedSummary = compressed.droppedSummary;
|
|
282
|
-
break;
|
|
283
|
-
}
|
|
284
|
-
default: {
|
|
285
|
-
// 兜底:保守降级(head + 工具账本 + tail),禁止只留 4 条
|
|
286
|
-
compressedOverflow = await this.conservativeTruncate(selection.overflow, estimator);
|
|
287
|
-
break;
|
|
399
|
+
// 第 0 轮 overflow aging:长值降级(与 historyAging 同算法同阈值;豁免 turn 首窗与
|
|
400
|
+
// freezeBatch 批次——全历史候选;视图级、原文不动、不进记录)。fits 判据已同源
|
|
401
|
+
// 应用 aging,aging 后装得下 = 本就不该砍(零记录、保持 usage 锚)。
|
|
402
|
+
let productView = view;
|
|
403
|
+
let overflowRound = 0; // 0 = aging 后即收敛(无账本化)
|
|
404
|
+
if (!fits(view)) {
|
|
405
|
+
// 第 1–3 轮:工具执行账本化(user/assistant 文本/合成占位跳过保留——用户输入
|
|
406
|
+
// 最不可再生;占位已是索引/交接形态不可再降),tail 保护档 50→20→5。**每档对
|
|
407
|
+
// 原 view 独立**账本化(不累积截断——累积会把前轮账本当普通消息吞掉,purpose
|
|
408
|
+
// 索引失忆)。产物与 foldTransformView 的「多 covers 单占位」同构(跳过保留项
|
|
409
|
+
// fold 原样保留):守门量的与发送的是同一个产物。
|
|
410
|
+
// 终止性=有限三档(档间可能 no-op,**不是**「每步严格缩小」——勿据此改 while-fits)。
|
|
411
|
+
for (const tailKeep of [50, 20, 5]) {
|
|
412
|
+
overflowRound++;
|
|
413
|
+
const trimmed = toolExecutionLedgerTrim(view, { tailKeep });
|
|
414
|
+
lastDroppedCount = trimmed.droppedCount;
|
|
415
|
+
productView = trimmed.result;
|
|
416
|
+
console.warn(`[overflow] session=${sessionId} converge round=${overflowRound} tailKeep=${tailKeep} ledgerDropped=${trimmed.droppedCount}`);
|
|
417
|
+
if (trimmed.droppedCount > 0 && fits(productView))
|
|
418
|
+
break;
|
|
288
419
|
}
|
|
289
420
|
}
|
|
421
|
+
const overflowConverged = fits(productView);
|
|
422
|
+
// droppedSummary 只说实际发生的事(档位 + 实际账本化条数,不虚报)
|
|
423
|
+
droppedSummary =
|
|
424
|
+
overflowConverged && overflowRound > 0
|
|
425
|
+
? `Context overflow converged at round ${overflowRound} (tailKeep=${[50, 20, 5][overflowRound - 1]}, ledgerDropped=${lastDroppedCount})`
|
|
426
|
+
: undefined;
|
|
427
|
+
// M-2:落记录前再查中止(M-1 与本点之间无 await、实际不可达——保留为防御性检查)。
|
|
428
|
+
// 语义:中止轮零写入,残缺产物(degrade/半截 summary)不得落进历史。
|
|
429
|
+
if (params.signal?.aborted) {
|
|
430
|
+
emit?.({
|
|
431
|
+
type: 'context.compact.end',
|
|
432
|
+
sessionId,
|
|
433
|
+
reason: 'overflow',
|
|
434
|
+
durationMs: Date.now() - overflowStart,
|
|
435
|
+
cancelled: true,
|
|
436
|
+
});
|
|
437
|
+
const exit = overflowExit(view);
|
|
438
|
+
this.freezeUsagePair(stateKey, estimateLLMMessages(exit.messages) + rawEstimator.estimateTools(tools), modelKey); // 装配出口=配对冻结点(早退轮也冻,防丢样)
|
|
439
|
+
return {
|
|
440
|
+
messages: exit.messages,
|
|
441
|
+
estimatedTokens: exit.estimatedTokens,
|
|
442
|
+
readingSource: exit.readingSource,
|
|
443
|
+
systemPrompt,
|
|
444
|
+
droppedSummary: proactiveDroppedSummary,
|
|
445
|
+
tools,
|
|
446
|
+
summary: this.deriveSummary(ledger),
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
// L3 终局:tail 5 之外的工具执行已全部账本化仍装不下——剩余为用户文本/在飞工作
|
|
450
|
+
//(不可再生不可砍)。fail-loud 提示换 session/model;可失败性由 provider 承担
|
|
451
|
+
//(P2 接 400 反馈),本轮照常返回超预算视图。不落记录、不伪装 compacted。
|
|
452
|
+
if (!overflowConverged) {
|
|
453
|
+
// L3 也发配对(请求照发、实测回来是最该学习的样本——不发则 afterStep 当「usage without pair」丢掉)
|
|
454
|
+
this.freezeUsagePair(stateKey, rawEstimator.estimateMessages(view), modelKey);
|
|
455
|
+
emit?.({
|
|
456
|
+
type: 'context.compact.error',
|
|
457
|
+
sessionId,
|
|
458
|
+
reason: 'overflow',
|
|
459
|
+
error: 'context_overflow_unrecoverable: tool executions beyond tail 5 are fully ledgerized yet the request still does not fit. ' +
|
|
460
|
+
'This session is no longer suitable for continued work, or the current model does not fit the task scale — start a new session or switch to a model with a larger context window.',
|
|
461
|
+
durationMs: Date.now() - overflowStart,
|
|
462
|
+
});
|
|
463
|
+
const exit = overflowExit(view);
|
|
464
|
+
this.freezeUsagePair(stateKey, estimateLLMMessages(exit.messages) + rawEstimator.estimateTools(tools), modelKey); // 装配出口=配对冻结点(早退轮也冻,防丢样)
|
|
465
|
+
return {
|
|
466
|
+
messages: exit.messages,
|
|
467
|
+
estimatedTokens: exit.estimatedTokens,
|
|
468
|
+
readingSource: exit.readingSource,
|
|
469
|
+
systemPrompt,
|
|
470
|
+
droppedSummary: proactiveDroppedSummary,
|
|
471
|
+
tools,
|
|
472
|
+
summary: this.deriveSummary(ledger),
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
// 5. 产物 → 持久转换记录(记录先落盘,视图后发)。收敛产物 = 机械降级
|
|
476
|
+
//(type=degrade):covers=被账本化的工具执行区间(跳过保留项不在 covers 内)。
|
|
477
|
+
const overflowRecord = await this.appendRecordFromProduct({
|
|
478
|
+
sessionId,
|
|
479
|
+
agentId,
|
|
480
|
+
messages,
|
|
481
|
+
input: view,
|
|
482
|
+
productView,
|
|
483
|
+
fold: folded,
|
|
484
|
+
agingPlan,
|
|
485
|
+
fresh: false,
|
|
486
|
+
summarizeAttempted: false,
|
|
487
|
+
reason: 'overflow',
|
|
488
|
+
rawEstimator,
|
|
489
|
+
});
|
|
490
|
+
// 6. 确定性重装配:fold 是唯一视图实现
|
|
491
|
+
folded = this.foldView(messages, ledger);
|
|
492
|
+
view = folded.view;
|
|
493
|
+
// 锚卫生(评审 m4):只有真产出转换记录(请求形状已变)才清锚,
|
|
494
|
+
// 什么都没变的轮清锚只会打回裸估算。
|
|
495
|
+
this.states.set(stateKey, {
|
|
496
|
+
...state,
|
|
497
|
+
lastUsageAnchor: overflowRecord === undefined ? state?.lastUsageAnchor : undefined,
|
|
498
|
+
});
|
|
499
|
+
// 估算一次、两处引用(tokensAfter 与 contextSize 同值同源)
|
|
500
|
+
const tokensAfter = this.reportRequestTokens(view, systemPrompt, tools, overflowAgingPlan, rawEstimator);
|
|
501
|
+
emit?.({
|
|
502
|
+
type: 'context.compact.end',
|
|
503
|
+
sessionId,
|
|
504
|
+
reason: 'overflow',
|
|
505
|
+
tokensBefore: compactTokensBefore,
|
|
506
|
+
tokensAfter,
|
|
507
|
+
// contextSize 显示槽:仅真压缩(有砍除记录)才携带——零覆盖轮不动显示(评审 F-1:
|
|
508
|
+
// 估算不得在没砍东西时覆盖上一次实测);0/0 与 cancelled 同理不带
|
|
509
|
+
contextSize: overflowRecord !== undefined ? contextSizeSnapshot('compact-est', tokensAfter) : undefined,
|
|
510
|
+
durationMs: Date.now() - overflowStart,
|
|
511
|
+
// 无记录 = 未砍(第 0 轮 aging 后即收敛):空转缓存语义
|
|
512
|
+
cached: overflowRecord === undefined,
|
|
513
|
+
});
|
|
290
514
|
}
|
|
291
515
|
catch (err) {
|
|
516
|
+
// 守门人必须永远成功:收敛动作异常不得炸本轮;start 必须有终态事件
|
|
517
|
+
this.freezeUsagePair(stateKey, rawEstimator.estimateMessages(view), modelKey); // 异常轮请求仍会发出——配对照发
|
|
518
|
+
//(否则 store 的 compact.active 悬挂 + 诊断丢失)。降级返回原视图继续。
|
|
292
519
|
emit?.({
|
|
293
520
|
type: 'context.compact.error',
|
|
294
521
|
sessionId,
|
|
295
522
|
reason: 'overflow',
|
|
296
|
-
error: err instanceof Error ? err.message : String(err)
|
|
523
|
+
error: `overflow_convergence_failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
297
524
|
durationMs: Date.now() - overflowStart,
|
|
298
525
|
});
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
526
|
+
const exit = overflowExit(view);
|
|
527
|
+
this.freezeUsagePair(stateKey, estimateLLMMessages(exit.messages) + rawEstimator.estimateTools(tools), modelKey); // 装配出口=配对冻结点(早退轮也冻,防丢样)
|
|
528
|
+
return {
|
|
529
|
+
messages: exit.messages,
|
|
530
|
+
estimatedTokens: exit.estimatedTokens,
|
|
531
|
+
readingSource: exit.readingSource,
|
|
532
|
+
systemPrompt,
|
|
533
|
+
droppedSummary: proactiveDroppedSummary,
|
|
534
|
+
tools,
|
|
535
|
+
summary: this.deriveSummary(ledger),
|
|
536
|
+
};
|
|
302
537
|
}
|
|
303
|
-
//
|
|
304
|
-
//
|
|
305
|
-
|
|
306
|
-
const
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
const protectHead = keptBody.slice(0, Math.min(headCount, keptBody.length));
|
|
310
|
-
const selectedMiddle = keptBody.slice(protectHead.length);
|
|
311
|
-
const reassembled = [
|
|
312
|
-
...protectHead,
|
|
313
|
-
...compressedOverflow,
|
|
314
|
-
...selectedMiddle,
|
|
315
|
-
...selection.tail,
|
|
316
|
-
];
|
|
317
|
-
// 7. 更新状态
|
|
318
|
-
const compressedTokens = estimator.estimateMessages(reassembled);
|
|
319
|
-
emit?.({
|
|
320
|
-
type: 'context.compact.end',
|
|
321
|
-
sessionId,
|
|
322
|
-
reason: 'overflow',
|
|
323
|
-
tokensBefore: overflowTokensBefore,
|
|
324
|
-
tokensAfter: compressedTokens,
|
|
325
|
-
durationMs: Date.now() - overflowStart,
|
|
326
|
-
cached: false,
|
|
327
|
-
});
|
|
328
|
-
const overflowState = this.states.get(stateKey);
|
|
329
|
-
// fresh 门控(评审 F8-F1):机械降级/失败产出不得冒充新摘要推进 previousSummary
|
|
330
|
-
this.states.set(stateKey, {
|
|
331
|
-
...overflowState,
|
|
332
|
-
// 存摘要正文(而非 dropped 描述),供下次迭代更新
|
|
333
|
-
previousSummary: overflowFresh
|
|
334
|
-
? this.extractSummaryText(compressedOverflow) ?? overflowState?.previousSummary
|
|
335
|
-
: overflowState?.previousSummary,
|
|
336
|
-
});
|
|
337
|
-
// 8. 构建 LLM 消息(校准基 = 老化后 LLM 视图估算——尺子必须量实际请求,
|
|
338
|
-
// 不能像旧代码存 raw Message 估算,否则 calibrationRatio 系统性漂移)
|
|
339
|
-
const llmMessages = this.buildLlmMessages(reassembled, systemPrompt, tools, agingPlan);
|
|
340
|
-
const rawEstimatedTokens = estimateLLMMessages(llmMessages);
|
|
341
|
-
this.states.set(stateKey, {
|
|
342
|
-
...this.states.get(stateKey),
|
|
343
|
-
lastEstimatedTokens: rawEstimatedTokens,
|
|
344
|
-
});
|
|
345
|
-
const estimatedTokens = this.calibrateTokens(sessionId, rawEstimatedTokens, stateKey);
|
|
538
|
+
// 8. 构建 LLM 消息(发送视图带本轮 overflow aging——豁免版只影响本次请求,
|
|
539
|
+
// 下一 turn 正常规则照常(视图级、原文不动、天然可撤销)
|
|
540
|
+
const llmMessages = this.buildLlmMessages(view, systemPrompt, overflowAgingPlan);
|
|
541
|
+
const rawEstimatedTokens = estimateLLMMessages(llmMessages) + rawEstimator.estimateTools(tools);
|
|
542
|
+
const decision = this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), rawEstimatedTokens, this.states.get(stateKey)?.lastUsageAnchor);
|
|
543
|
+
this.freezeUsagePair(stateKey, rawEstimatedTokens, modelKey);
|
|
346
544
|
return {
|
|
347
545
|
messages: llmMessages,
|
|
348
|
-
estimatedTokens,
|
|
546
|
+
estimatedTokens: decision.tokens,
|
|
547
|
+
readingSource: decision.source,
|
|
349
548
|
systemPrompt,
|
|
350
549
|
droppedSummary: droppedSummary ?? proactiveDroppedSummary,
|
|
351
|
-
|
|
352
|
-
|
|
550
|
+
tools,
|
|
551
|
+
summary: this.deriveSummary(ledger),
|
|
353
552
|
};
|
|
354
553
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
554
|
+
// ── 转换记录账本 ──
|
|
555
|
+
/**
|
|
556
|
+
* 加载(并缓存)(sessionId, agentId) 的转换记录账本;账本为空且有旧压缩快照时
|
|
557
|
+
* 一次性迁移(幂等:账本非空即跳过)。
|
|
558
|
+
*
|
|
559
|
+
* @param sessionId - 会话 id
|
|
560
|
+
* @param agentId - agent id
|
|
561
|
+
* @param messages - 全量原始消息(迁移锚点需要)
|
|
562
|
+
* @param loadLegacy - 旧 ContextCompactSnapshot 读口(迁移输入;缺省不迁移)
|
|
563
|
+
* @returns 账本
|
|
564
|
+
*/
|
|
565
|
+
async loadLedger(sessionId, agentId, messages, loadLegacy) {
|
|
566
|
+
const stateKey = compactStateKey(sessionId, agentId);
|
|
567
|
+
const cached = this.ledgers.get(stateKey);
|
|
568
|
+
if (cached)
|
|
569
|
+
return cached;
|
|
570
|
+
const agent = agentId ?? '';
|
|
571
|
+
let ledger = await this.config.transformStore.load(sessionId, agent);
|
|
572
|
+
// 迁移幂等门 = 账本完全无条目(F7:行在 blob 全丢不算空——不得重放旧快照迁移)
|
|
573
|
+
if (ledger.records.length === 0 && !ledger.hadEntries && loadLegacy) {
|
|
574
|
+
const snapshot = await loadLegacy(sessionId);
|
|
575
|
+
if (snapshot) {
|
|
576
|
+
const records = legacySnapshotToRecords({
|
|
577
|
+
sessionId,
|
|
578
|
+
agentId: agent,
|
|
579
|
+
snapshot,
|
|
580
|
+
messages,
|
|
581
|
+
protectFirstN: this.config.protectFirstN,
|
|
582
|
+
protectLastN: this.config.protectLastN,
|
|
583
|
+
});
|
|
584
|
+
if (records && records.length > 0) {
|
|
585
|
+
await this.config.transformStore.append(sessionId, agent, records);
|
|
586
|
+
ledger = await this.config.transformStore.load(sessionId, agent);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
this.ledgers.set(stateKey, ledger);
|
|
591
|
+
return ledger;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* 摘要调用预算(P3):外部 abort 与单次摘要超时合并成一个 signal。
|
|
595
|
+
*
|
|
596
|
+
* 为什么必须合并:run 中止只到 convertToLlm 边界,摘要调用在 `provider.chat` 内部
|
|
597
|
+
* 等待 HTTP;没有 signal 时 abort 只能等 provider 自己超时(实测 900s,UI 全程假死)。
|
|
598
|
+
*
|
|
599
|
+
* @param external - 上游(run)abort 信号
|
|
600
|
+
* @returns 合并信号与「是否超时」判定(超时归失败降级,外部中止归取消不写记录)
|
|
601
|
+
*/
|
|
602
|
+
createSummarizeSignal(external) {
|
|
603
|
+
const timeoutMs = this.config.summarizeTimeoutMs;
|
|
604
|
+
const timeoutSignal = timeoutMs > 0 ? AbortSignal.timeout(timeoutMs) : undefined;
|
|
605
|
+
if (!external) {
|
|
606
|
+
return {
|
|
607
|
+
signal: timeoutSignal,
|
|
608
|
+
timedOut: () => timeoutSignal?.aborted === true,
|
|
609
|
+
};
|
|
360
610
|
}
|
|
611
|
+
if (!timeoutSignal)
|
|
612
|
+
return { signal: external, timedOut: () => false };
|
|
361
613
|
return {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
614
|
+
signal: AbortSignal.any([external, timeoutSignal]),
|
|
615
|
+
timedOut: () => timeoutSignal.aborted,
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* 请求口径读数:**fold 视图 + 老化** = 实际发给 LLM 的 messages 部分。
|
|
620
|
+
*
|
|
621
|
+
* 压缩决策(触发线、增长门、记录 meta)只许用这个口径:量原文会让「已压缩但视图很小」
|
|
622
|
+
* 的会话被反复判超线(2026-11-07 事故:视图 118k、原文 621k → 每次增长 25% 必然重压)。
|
|
623
|
+
*
|
|
624
|
+
* @param folded - 当前 fold 结果
|
|
625
|
+
* @param agingPlan - 老化计划(基于完整历史,按消息身份匹配)
|
|
626
|
+
* @param estimator - 校准后的估算器
|
|
627
|
+
* @returns messages 口径 token 估算
|
|
628
|
+
*/
|
|
629
|
+
measureRequestViewTokens(folded, agingPlan, estimator) {
|
|
630
|
+
return estimator.estimateMessages(applyAgingToMessages(folded.view, agingPlan, this.config.historyAging));
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* 报告尺(rawRequestViewTokens,M3 统一):**请求形状 messages(fold+老化)+ system + tools**
|
|
634
|
+
* 的未校准全请求估算。manual/proactive/overflow 的 compact 对 tokensBefore/After 与
|
|
635
|
+
* contextSize 快照同式(一处公式,禁止手抄分叉——旧 auto 口径 messages-only 缺
|
|
636
|
+
* system/tools,报出来比顶栏永远少一截,M3)。
|
|
637
|
+
*
|
|
638
|
+
* @param view - fold 视图(未老化原文形态)
|
|
639
|
+
* @param systemPrompt - 同形 system(缺省按空串)
|
|
640
|
+
* @param tools - 请求工具面(缺省按空)
|
|
641
|
+
* @param agingPlan - 老化计划(与真实请求同式)
|
|
642
|
+
* @param raw - 未校准估算器(报告尺单位,不乘 ratio)
|
|
643
|
+
* @returns 全请求口径 token 估算
|
|
644
|
+
*/
|
|
645
|
+
reportRequestTokens(view, systemPrompt, tools, agingPlan, raw) {
|
|
646
|
+
return (estimateLLMMessages(this.buildLlmMessages(view, systemPrompt ?? '', agingPlan)) +
|
|
647
|
+
raw.estimateTools(tools ?? []));
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* 摘要请求的回声比对源(P2):前缀去掉既有压缩占位。
|
|
651
|
+
*
|
|
652
|
+
* 前缀已是 fold 视图时,占位正文就是「上一份交接书」——模型续写/复用它是正常迭代,
|
|
653
|
+
* 不是复读原文;若把占位计入比对源,只要新增内容不多就会误判 verbatim_echo 并降级。
|
|
654
|
+
*
|
|
655
|
+
* @param view - 摘要前缀所用的消息(fold 视图)
|
|
656
|
+
* @param systemPrompt - 同形 system
|
|
657
|
+
* @param agingPlan - 老化计划(与真实请求同式)
|
|
658
|
+
* @returns 换行拼接的文本投影
|
|
659
|
+
*/
|
|
660
|
+
buildEchoSource(view, systemPrompt, agingPlan) {
|
|
661
|
+
const conversation = view.filter((m) => m.metadata?.source !== 'contextSummary' && m.metadata?.source !== 'contextLedger');
|
|
662
|
+
return this.buildLlmMessages(conversation, systemPrompt, agingPlan)
|
|
663
|
+
// 正文投影必须覆盖 block 形态(2026-11-13 实测):
|
|
664
|
+
// LLM message 的 content 多为 content block 数组(本次真实摘要请求 1059 条中
|
|
665
|
+
// 1037 条是 list),`String(content)` 对数组返回 "[object Object]"——这些消息
|
|
666
|
+
// 对回声比对贡献为零,闸门对 97.9% 的正文完全失明(假阴性)。
|
|
667
|
+
.map((m) => llmContentToText(m.content))
|
|
668
|
+
.join('\n');
|
|
669
|
+
}
|
|
670
|
+
/** 重 fold(append 后调用);孤儿告警去重键 = session|agent|seq(F9:跨会话不串键) */
|
|
671
|
+
foldView(messages, ledger) {
|
|
672
|
+
return foldTransformView(messages, ledger, {
|
|
673
|
+
onOrphan: (record) => {
|
|
674
|
+
const key = `${record.sessionId}|${record.agentId}|${record.seq}`;
|
|
675
|
+
if (this.orphanWarned.has(key))
|
|
676
|
+
return;
|
|
677
|
+
this.orphanWarned.add(key);
|
|
678
|
+
console.warn(`[transform] session=${record.sessionId} agent=${record.agentId} record seq=${record.seq} orphaned (anchor unresolved); raw text stays visible`);
|
|
679
|
+
},
|
|
680
|
+
onPartialOrphan: (record, droppedRuns) => {
|
|
681
|
+
const key = `${record.sessionId}|${record.agentId}|${record.seq}|partial`;
|
|
682
|
+
if (this.orphanWarned.has(key))
|
|
683
|
+
return;
|
|
684
|
+
this.orphanWarned.add(key);
|
|
685
|
+
console.warn(`[transform] session=${record.sessionId} agent=${record.agentId} record seq=${record.seq}: ${droppedRuns} run(s) orphaned (stream truncated?); surviving runs stay effective`);
|
|
686
|
+
},
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* 显式撤销一条转换记录(回填的唯一通道;记录模型拍板:「回填只在显式撤销时」)。
|
|
691
|
+
*
|
|
692
|
+
* 撤销是 append-only 附加行,永久生效面翻转为 revoked;被覆盖的原文在下一次
|
|
693
|
+
* 装配回到视图。宿主(UI/工具)据此实现「撤销压缩」。
|
|
694
|
+
*
|
|
695
|
+
* @param sessionId - 会话 id
|
|
696
|
+
* @param agentId - agent id
|
|
697
|
+
* @param seq - 记录 seq(账本 load 可得)
|
|
698
|
+
* @returns void
|
|
699
|
+
*/
|
|
700
|
+
async revokeTransformRecord(sessionId, agentId, seq) {
|
|
701
|
+
const agent = agentId ?? '';
|
|
702
|
+
await this.config.transformStore.append(sessionId, agent, [], [seq]);
|
|
703
|
+
// 缓存同步:revocation 立即生效(无需重载)
|
|
704
|
+
const ledger = this.ledgers.get(compactStateKey(sessionId, agentId));
|
|
705
|
+
const target = ledger?.records.find((r) => r.seq === seq);
|
|
706
|
+
if (target)
|
|
707
|
+
target.revoked = true;
|
|
708
|
+
}
|
|
709
|
+
/** 当前有效语义摘要正文(最新 summary 型记录的文本;机械占位永不冒充) */
|
|
710
|
+
deriveSummary(ledger) {
|
|
711
|
+
for (let i = ledger.records.length - 1; i >= 0; i--) {
|
|
712
|
+
const record = ledger.records[i];
|
|
713
|
+
if (record.revoked || record.type !== 'summary')
|
|
714
|
+
continue;
|
|
715
|
+
if (record.text)
|
|
716
|
+
return record.text;
|
|
717
|
+
}
|
|
718
|
+
return undefined;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* previousSummary 的覆盖终点(下标,不含)——与 deriveSummary 同一选择口径(最新、
|
|
722
|
+
* type==='summary'、有正文)的记录,返回其**解析后**覆盖端(M-4/C1,评审 del-e1ee43ef)。
|
|
723
|
+
*
|
|
724
|
+
* 摘要素材起点必须与 previousSummary 同源:derivedCoverEnd 含 degrade 多 run 的最大覆盖
|
|
725
|
+
* 端 E(E > S 是常态),以 E 为起点会把 [S, E)「既不在旧摘要、也不在新素材」的原文排除,
|
|
726
|
+
* 而新记录 covers 吞掉 [headEnd, tailStart) 全区间 → 静默丢数据。返回 undefined = 覆盖端
|
|
727
|
+
* 解析不出(如零覆盖 legacy 记录)→ 素材回退全区间(宁可胖不丢,方向安全)。
|
|
728
|
+
*/
|
|
729
|
+
deriveSummaryCoverEnd(folded) {
|
|
730
|
+
for (let i = folded.applied.length - 1; i >= 0; i--) {
|
|
731
|
+
const run = folded.applied[i];
|
|
732
|
+
const record = run.record;
|
|
733
|
+
if (record.revoked || record.type !== 'summary')
|
|
734
|
+
continue;
|
|
735
|
+
if (!record.text)
|
|
736
|
+
continue;
|
|
737
|
+
let end;
|
|
738
|
+
for (const [, b] of run.spans) {
|
|
739
|
+
if (end === undefined || b + 1 > end)
|
|
740
|
+
end = b + 1;
|
|
741
|
+
}
|
|
742
|
+
return end;
|
|
743
|
+
}
|
|
744
|
+
return undefined;
|
|
745
|
+
}
|
|
746
|
+
/** 有效记录覆盖终点(下标,不含)——传输口径 coveredMessageCount 用 */
|
|
747
|
+
derivedCoverEnd(folded) {
|
|
748
|
+
let end;
|
|
749
|
+
for (const run of folded.applied) {
|
|
750
|
+
for (const [, b] of run.spans) {
|
|
751
|
+
if (end === undefined || b + 1 > end)
|
|
752
|
+
end = b + 1;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
return end;
|
|
756
|
+
}
|
|
757
|
+
/** 重启恢复:增长门/冷却基数从账本 meta 初始化(仅内存态缺失时;F2 对称) */
|
|
758
|
+
restoreGatesFromLedger(stateKey, ledger) {
|
|
759
|
+
if (this.states.has(stateKey))
|
|
760
|
+
return;
|
|
761
|
+
const effective = ledger.records.filter((r) => !r.revoked);
|
|
762
|
+
// 门基只认非 overflow 记录——overflow 在进程内从不推进增长门(防饿死真摘要),
|
|
763
|
+
// 重启恢复必须同口径(评审 F2:否则每次重启后增长门静默失效/被饿死)
|
|
764
|
+
const gateBase = [...effective].reverse().find((r) => r.meta.reason !== 'overflow');
|
|
765
|
+
const lastLlm = [...effective].reverse().find((r) => r.meta.summarizeAttempted);
|
|
766
|
+
if (!gateBase && !lastLlm)
|
|
767
|
+
return;
|
|
768
|
+
// 口径版本守卫:旧记录的 viewTokens 是另一套口径/单位(未老化、非请求形状,或
|
|
769
|
+
// 'request' 校准存量),拿它当增长门基数会与当前尺互比 → 永远判「没长够」而永久
|
|
770
|
+
// 压制压缩。只认 'request-raw'(请求形状 + raw 单位);无该标记 = 无基数,
|
|
771
|
+
// 允许下一次压缩自愈(代价至多多压一次)。
|
|
772
|
+
const gateTokens = gateBase?.meta.viewTokensSource === 'request-raw' ? gateBase.meta.viewTokens : undefined;
|
|
773
|
+
this.states.set(stateKey, {
|
|
774
|
+
...(gateBase
|
|
775
|
+
? {
|
|
776
|
+
lastProactiveMessageCount: gateBase.meta.messageCount,
|
|
777
|
+
// 门基 raw 单位(M-3);lastProactiveTokens 展示值随后续装配刷新,不从 raw 存量回填
|
|
778
|
+
...(gateTokens !== undefined ? { lastCompactGateTokens: gateTokens } : {}),
|
|
779
|
+
}
|
|
780
|
+
: {}),
|
|
781
|
+
lastProactiveLlmAt: lastLlm?.createdAt,
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* 压缩产物 → 持久转换记录(三条压缩路径的唯一翻译口)。
|
|
786
|
+
*
|
|
787
|
+
* 翻译 = 对照压缩输入与产物视图做身份差分:产物里消失的原文/占位所代表的原始
|
|
788
|
+
* 区间即记录覆盖区间(contiguous 合并)。fresh 且为真摘要 → summary 型;否则
|
|
789
|
+
* degrade 型(机械占位如实分类,伪摘要永不冒充 summary)。
|
|
790
|
+
*
|
|
791
|
+
* @param params - 输入集合、产物视图、fresh 判定等
|
|
792
|
+
* @returns 生成的记录(无丢弃时为 undefined,不产记录)
|
|
793
|
+
*/
|
|
794
|
+
async appendRecordFromProduct(params) {
|
|
795
|
+
const kept = new Set(params.productView);
|
|
796
|
+
const rawIndex = new Map();
|
|
797
|
+
for (let i = 0; i < params.messages.length; i++)
|
|
798
|
+
rawIndex.set(params.messages[i], i);
|
|
799
|
+
// 产物新增的合成消息(摘要/账本占位)——降级记录的文本来源
|
|
800
|
+
const inputSet = new Set(params.input);
|
|
801
|
+
const synthetic = params.productView.filter((m) => !rawIndex.has(m) && !inputSet.has(m));
|
|
802
|
+
// F1(评审):类型判定前置——degrade 记录的机械文本**不包含**被吞占位的语义内容,
|
|
803
|
+
// 因此不得覆盖既有记录的区间(否则摘要从视图里静默消失);只有 fresh 语义摘要
|
|
804
|
+
// (经 previousSummary 合并)才允许接管既有记录的区间。
|
|
805
|
+
const freshOk = params.fresh && Boolean(params.summaryText) && !isMechanicalNotice(params.summaryText);
|
|
806
|
+
// C-1(2026-11-07 评审):degrade 不得吞既有记录区间——不仅跳过占位项(见下),
|
|
807
|
+
// **raw 项同样要查**:proactive 的 input 是原 messages 中段(已覆盖区间以 raw 形式
|
|
808
|
+
// 出现),此前只护占位项,degrade 的机械 span 会整段覆盖旧 summary 记录的区间
|
|
809
|
+
// (fold 后写覆盖)→ 交接书从视图静默消失。fold 模块契约「degrade 永不覆盖
|
|
810
|
+
// 占位 span」对 raw 项同样成立。
|
|
811
|
+
const coveredByRecord = [];
|
|
812
|
+
if (!freshOk) {
|
|
813
|
+
for (const run of params.fold.applied)
|
|
814
|
+
coveredByRecord.push(...run.spans);
|
|
815
|
+
}
|
|
816
|
+
const isCovered = (idx) => coveredByRecord.some(([a, b]) => idx >= a && idx <= b);
|
|
817
|
+
const spans = [];
|
|
818
|
+
for (const item of params.input) {
|
|
819
|
+
if (kept.has(item))
|
|
820
|
+
continue;
|
|
821
|
+
const idx = rawIndex.get(item);
|
|
822
|
+
if (idx !== undefined) {
|
|
823
|
+
// degrade:既有记录覆盖的 raw 不接管(宁可少覆盖,不得吞摘要)
|
|
824
|
+
if (!freshOk && isCovered(idx))
|
|
825
|
+
continue;
|
|
826
|
+
spans.push([idx, idx]);
|
|
827
|
+
continue;
|
|
828
|
+
}
|
|
829
|
+
// 占位项 = 既有记录的区间。degrade 不接管(见上);summary 接管(合并语义)
|
|
830
|
+
if (!freshOk)
|
|
831
|
+
continue;
|
|
832
|
+
const run = params.fold.placeholderOf.get(item);
|
|
833
|
+
if (run)
|
|
834
|
+
spans.push(...run.spans.map((s) => [s[0], s[1]]));
|
|
835
|
+
}
|
|
836
|
+
if (spans.length === 0)
|
|
837
|
+
return undefined;
|
|
838
|
+
// 合并连续/重叠区间 → 覆盖 run 集
|
|
839
|
+
spans.sort((a, b) => a[0] - b[0]);
|
|
840
|
+
const merged = [];
|
|
841
|
+
for (const span of spans) {
|
|
842
|
+
const last = merged[merged.length - 1];
|
|
843
|
+
if (last && span[0] <= last[1] + 1) {
|
|
844
|
+
last[1] = Math.max(last[1], span[1]);
|
|
845
|
+
}
|
|
846
|
+
else {
|
|
847
|
+
merged.push([span[0], span[1]]);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
const covers = [];
|
|
851
|
+
for (const [a, b] of merged) {
|
|
852
|
+
const run = anchorRange(params.messages, a, b);
|
|
853
|
+
if (run)
|
|
854
|
+
covers.push(run);
|
|
855
|
+
}
|
|
856
|
+
if (covers.length === 0)
|
|
857
|
+
return undefined;
|
|
858
|
+
const syntheticText = synthetic
|
|
859
|
+
.map((m) => getTextContent(m.content))
|
|
860
|
+
.filter((t) => t.trim().length > 0)
|
|
861
|
+
.join('\n\n');
|
|
862
|
+
const text = freshOk
|
|
863
|
+
? params.summaryText
|
|
864
|
+
: syntheticText || `[Context truncated: ${merged.length} range(s) mechanically dropped]`;
|
|
865
|
+
const syntheticSource = synthetic.find((m) => typeof m.metadata?.source === 'string')?.metadata
|
|
866
|
+
?.source;
|
|
867
|
+
const record = {
|
|
868
|
+
sessionId: params.sessionId,
|
|
869
|
+
agentId: params.agentId ?? '',
|
|
870
|
+
type: freshOk ? 'summary' : 'degrade',
|
|
871
|
+
createdAt: Date.now(),
|
|
872
|
+
covers,
|
|
873
|
+
text,
|
|
874
|
+
placeholder: freshOk
|
|
875
|
+
? { role: 'user', source: 'contextSummary' }
|
|
876
|
+
: { role: 'user', source: syntheticSource ?? 'contextLedger' },
|
|
877
|
+
meta: {
|
|
878
|
+
reason: params.reason,
|
|
879
|
+
fresh: freshOk,
|
|
880
|
+
summarizeAttempted: params.summarizeAttempted,
|
|
881
|
+
messageCount: params.messages.length,
|
|
882
|
+
},
|
|
366
883
|
};
|
|
884
|
+
// 记录 meta 的视图估算:以「记录生效后的 fold」度量(度量=装配),
|
|
885
|
+
// 且与真实请求同口径(老化 + fold)——增长门基数只认这个读数。
|
|
886
|
+
// 单位 raw(M-3):跨时间存量必须用时间稳定的单位,比较点折算当前 ratio。
|
|
887
|
+
const virtual = {
|
|
888
|
+
records: [
|
|
889
|
+
...this.ledgers.get(compactStateKey(params.sessionId, params.agentId)).records,
|
|
890
|
+
{ ...record, seq: -1, blobRef: undefined, revoked: false },
|
|
891
|
+
],
|
|
892
|
+
};
|
|
893
|
+
const virtualFold = foldTransformView(params.messages, virtual, {});
|
|
894
|
+
record.meta.viewTokens = this.measureRequestViewTokens(virtualFold, params.agingPlan, params.rawEstimator);
|
|
895
|
+
// 口径标记升版(M-3):'request-raw' = 请求形状 + raw 单位。恢复端只认新标记——
|
|
896
|
+
// 旧 'request'(校准存量)缺写入时 ratio,无法换算 raw,按无基数处理(一次自愈)。
|
|
897
|
+
record.meta.viewTokensSource = 'request-raw';
|
|
898
|
+
const [persisted] = await this.config.transformStore.append(params.sessionId, params.agentId ?? '', [record]);
|
|
899
|
+
const ledger = this.ledgers.get(compactStateKey(params.sessionId, params.agentId));
|
|
900
|
+
if (ledger && persisted)
|
|
901
|
+
ledger.records.push(persisted);
|
|
902
|
+
return persisted;
|
|
367
903
|
}
|
|
368
904
|
/**
|
|
369
905
|
* 主动压缩(路径 A):消息 token 超过触发线时,在硬溢出前收缩
|
|
370
906
|
*
|
|
371
|
-
* 触发线取两条线的 min
|
|
907
|
+
* 触发线取两条线的 min:
|
|
372
908
|
* - **目标线** targetTokens × compactFallbackRatio——与窗口解耦,防长会话失忆
|
|
373
|
-
* - **窗口比例线** messagesBudget × proactiveCompactRatio
|
|
909
|
+
* - **窗口比例线** messagesBudget × proactiveCompactRatio——防小窗口到顶未触发
|
|
374
910
|
*
|
|
375
|
-
*
|
|
376
|
-
* -
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
* -
|
|
380
|
-
* **不**丢弃已有压缩视图。
|
|
381
|
-
* - 策略:
|
|
382
|
-
* - ratio=0:无压缩态原样返回;有压缩态仅粘滞重建
|
|
383
|
-
* - 未超线且无压缩态:原样返回
|
|
384
|
-
* - 超线且需要新一轮压缩(上次压缩后新增消息够多):LLM 压缩中间段并产出语义摘要
|
|
385
|
-
* - 已有压缩态且增量小或冷却中或禁新 LLM:零 LLM 重建视图
|
|
911
|
+
* 语义(记录模型):
|
|
912
|
+
* - 装配 = fold(原文 + 有效记录);已有压缩态天然粘滞,不因估算回落打回全量
|
|
913
|
+
* - proactiveCompactRatio=0:禁止发起**新的**压缩;已有记录照常生效
|
|
914
|
+
* - 超线且需要新一轮压缩(增长门 + 冷却):LLM 压缩中间段 → 产出 summary 记录
|
|
915
|
+
* - 其余情形:零 LLM,直接返回 fold 视图
|
|
386
916
|
*/
|
|
387
917
|
async applyProactiveCompact(params) {
|
|
388
|
-
const { sessionId, messages, messagesBudget, agingPlan, summarize, estimator, emit, systemPrompt, tools, } = params;
|
|
918
|
+
const { sessionId, messages, ledger, messagesBudget, agingPlan, summarize, estimator, rawEstimator, calKey, emit, systemPrompt, tools, } = params;
|
|
389
919
|
const stateKey = params.stateKey ?? sessionId;
|
|
390
920
|
const ratio = this.config.proactiveCompactRatio;
|
|
391
921
|
const state = this.states.get(stateKey);
|
|
922
|
+
let folded = this.foldView(messages, ledger);
|
|
392
923
|
if (messages.length === 0) {
|
|
393
|
-
return {
|
|
924
|
+
return { view: folded.view, fold: folded };
|
|
394
925
|
}
|
|
395
926
|
const protectFirstN = this.config.protectFirstN;
|
|
396
927
|
const protectLastN = this.config.protectLastN;
|
|
397
|
-
//
|
|
398
|
-
const hasCompactView =
|
|
399
|
-
// 边界对齐(不拆 tool_call/tool_result 对)——两形状都会被严格网关 400
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
const
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
const summaryMsg = this.contextSummaryMessage(state.previousSummary, state.lastProactiveCoverEnd);
|
|
421
|
-
// 摘要覆盖到 lastProactiveCoverEnd(tailStart);之后的消息是增量,必须保留
|
|
422
|
-
const coverUpTo = state.lastProactiveCoverEnd ?? 0;
|
|
423
|
-
const sinceIdx = Math.max(coverUpTo, headEnd);
|
|
424
|
-
const sinceCompact = this.semanticHistory(messages.slice(sinceIdx, tailStart));
|
|
425
|
-
let extra = sinceCompact;
|
|
426
|
-
if (sinceCompact.length > 0) {
|
|
427
|
-
const extraTokens = estimator.estimateMessages(sinceCompact);
|
|
428
|
-
if (extraTokens > targetMiddleTokens) {
|
|
429
|
-
try {
|
|
430
|
-
const compressedExtra = await this.config.compressor.compress({
|
|
431
|
-
messages: sinceCompact,
|
|
432
|
-
targetTokens: targetMiddleTokens,
|
|
433
|
-
tokenEstimator: estimator,
|
|
434
|
-
});
|
|
435
|
-
extra = compressedExtra.result;
|
|
436
|
-
}
|
|
437
|
-
catch {
|
|
438
|
-
// 机械压缩失败:保留增量原文(宁可胖,不可丢)
|
|
439
|
-
extra = sinceCompact;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
return { messages: [...head, summaryMsg, ...extra, ...tail] };
|
|
444
|
-
}
|
|
445
|
-
if (middle.length === 0) {
|
|
446
|
-
return null;
|
|
447
|
-
}
|
|
448
|
-
try {
|
|
449
|
-
const compressed = await this.config.compressor.compress({
|
|
450
|
-
messages: middle,
|
|
451
|
-
targetTokens: targetMiddleTokens,
|
|
452
|
-
tokenEstimator: estimator,
|
|
453
|
-
});
|
|
454
|
-
return { messages: [...head, ...compressed.result, ...tail] };
|
|
455
|
-
}
|
|
456
|
-
catch {
|
|
457
|
-
return null;
|
|
458
|
-
}
|
|
459
|
-
};
|
|
460
|
-
// F1-B3:尺子量**老化视图**(实际请求的保守上界)——raw 估算会把 B2 稳态误判超线(双机制打架)。
|
|
928
|
+
// 压缩态标记:有有效记录即「已压缩」(含 gate-anchor / degrade)
|
|
929
|
+
const hasCompactView = ledger.records.some((r) => !r.revoked);
|
|
930
|
+
// 边界对齐(不拆 tool_call/tool_result 对)——两形状都会被严格网关 400。
|
|
931
|
+
// 保护窗只决定**压缩选段**(记录覆盖哪些原文),装配由 fold 负责。
|
|
932
|
+
// 方案 E 修订(2026-11-07):turn 内不再禁止 LLM 摘要,改为活跃窗口保护——
|
|
933
|
+
// tail 扩大到 midTurnProtectLastN 条(进行中的工具步骤/证据/推理链不进选段),
|
|
934
|
+
// 窗口外的执行痕迹正常语义压缩;长任务 turn 内目标线由此真正生效。
|
|
935
|
+
const midTurn = isMidTurn(messages);
|
|
936
|
+
const tailCount = midTurn
|
|
937
|
+
? Math.max(protectLastN, this.config.midTurnProtectLastN)
|
|
938
|
+
: protectLastN;
|
|
939
|
+
const { headEnd, tailStart } = alignViewSplit(messages, protectFirstN, tailCount);
|
|
940
|
+
// 锚点硬保护(评审 M2):当前 turn 的真实 user 消息(含 turn_context/指令约束)
|
|
941
|
+
// 永不进选段——turn 内压缩不得吞掉任务的原始指令,模型续跑只能靠摘要转述风险太大。
|
|
942
|
+
// 老 turn 的锚点已由历史 summary 记录覆盖语义,这里只保最后一条(当前 turn)。
|
|
943
|
+
const anchorIdx = lastGenuineUserIndex(messages);
|
|
944
|
+
const middleRaw = messages
|
|
945
|
+
.slice(headEnd, tailStart)
|
|
946
|
+
.filter((_m, i) => headEnd + i !== anchorIdx);
|
|
947
|
+
const middle = semanticHistory(middleRaw); // 仅供 summarize 素材,禁止回装进视图
|
|
948
|
+
// 请求口径(F1-B3 + 2026-11-07 修正):读数 = **fold 视图 + 老化** = 实际发给 LLM 的内容。
|
|
949
|
+
// 旧实现量「老化后的全量历史」:压缩把视图压小,却压不动读数,于是已压缩会话
|
|
950
|
+
// (实测视图 118k / 原文 621k)只要历史再涨 25% 就必然再压一次 —— 越压越发全量。
|
|
461
951
|
// 压缩输入仍用原 messages(摘要质量需要原文)。
|
|
462
|
-
const
|
|
952
|
+
const agedView = applyAgingToMessages(folded.view, agingPlan, this.config.historyAging);
|
|
953
|
+
const currentTokens = estimator.estimateMessages(agedView);
|
|
954
|
+
// 头/尾保护窗在**视图坐标**上的条数(fold 只覆盖中段,首尾原样保留)
|
|
955
|
+
const viewCount = folded.view.length;
|
|
956
|
+
const headCountInView = Math.min(headEnd, viewCount);
|
|
957
|
+
const tailCountInView = Math.min(Math.max(0, messages.length - tailStart), viewCount - headCountInView);
|
|
958
|
+
const agingHead = agedView.slice(0, headCountInView);
|
|
959
|
+
const agingTail = tailCountInView > 0 ? agedView.slice(viewCount - tailCountInView) : [];
|
|
960
|
+
const headTailTokens = estimator.estimateMessages(agingHead) + estimator.estimateMessages(agingTail);
|
|
463
961
|
// 两条触发线(都保留;刻意不用 legacy 命名,防被当成死代码清掉):
|
|
464
|
-
// - 目标线 targetLine = targetTokens × compactFallbackRatio:与窗口解耦(F1-B3),
|
|
465
|
-
// 防长会话失忆;1M 窗口只挂比例线会永不触发。
|
|
466
|
-
// - 窗口比例线 windowRatioLine = messagesBudget × proactiveCompactRatio:
|
|
467
|
-
// 防小窗口(256k 等)在摸到目标线之前就顶满。
|
|
468
|
-
// 窗口未知(messagesBudget=0)时比例线为 +∞,只用目标线。
|
|
469
962
|
const targetLine = Math.ceil(this.config.targetTokens * this.config.compactFallbackRatio);
|
|
470
963
|
const windowRatioLine = messagesBudget > 0 ? Math.floor(messagesBudget * ratio) : Number.POSITIVE_INFINITY;
|
|
471
|
-
// ratio=0:禁止发起新的路径 A 压缩(不只禁 LLM
|
|
964
|
+
// ratio=0:禁止发起新的路径 A 压缩(不只禁 LLM)。已有记录照常生效(fold)。
|
|
472
965
|
if (ratio <= 0) {
|
|
473
|
-
if (
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
const stickyMiddleTokens = Math.max(200, Math.floor(stickyBase * 0.55) - headTailTokens);
|
|
479
|
-
const sticky = await rebuildStickyView(stickyMiddleTokens);
|
|
480
|
-
if (!sticky) {
|
|
481
|
-
return { messages };
|
|
966
|
+
if (hasCompactView) {
|
|
967
|
+
this.states.set(stateKey, {
|
|
968
|
+
...state,
|
|
969
|
+
lastProactiveTokens: this.measureRequestViewTokens(folded, agingPlan, estimator),
|
|
970
|
+
});
|
|
482
971
|
}
|
|
483
|
-
|
|
484
|
-
this.states.set(stateKey, {
|
|
485
|
-
...state,
|
|
486
|
-
lastProactiveTokens: reducedTokens,
|
|
487
|
-
lastEstimatedTokens: reducedTokens,
|
|
488
|
-
});
|
|
489
|
-
return { messages: sticky.messages };
|
|
972
|
+
return { view: folded.view, fold: folded };
|
|
490
973
|
}
|
|
491
|
-
// 触发线:目标线与窗口比例线取 min
|
|
492
|
-
//
|
|
974
|
+
// 触发线:目标线与窗口比例线取 min。单位:比较双方都是**校准后** token 读数。
|
|
975
|
+
// 数值:threshold = min(targetLine, windowRatioLine)(arch/context-size-accounting.md §5.2)。
|
|
493
976
|
const threshold = this.config.targetTokens > 0 ? Math.min(targetLine, windowRatioLine) : windowRatioLine;
|
|
494
|
-
|
|
495
|
-
//
|
|
496
|
-
//
|
|
497
|
-
//
|
|
498
|
-
|
|
499
|
-
//
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
//
|
|
977
|
+
// 失败降级的机械账本目标(structure_only 下该值只作用于 summarize 失败后的
|
|
978
|
+
// mechanicalDegrade,不影响摘要大小——摘要走 summaryBudget clamp)。
|
|
979
|
+
// 地板 2000(评审 m2):tail 活跃窗口扩大后 headTailTokens 变大,200 地板会把
|
|
980
|
+
// 失败降级压成极瘦账本(比降级前狠得多),宁可账本胖一点也别丢太多。
|
|
981
|
+
const targetMiddleTokens = Math.max(2000, Math.floor(threshold * 0.55) - headTailTokens);
|
|
982
|
+
// 新 LLM 压缩闸门(方案 E 修订,2026-11-07):只看 summarize 可用性。
|
|
983
|
+
// 旧版 turn 内禁止新 LLM 摘要(保护活动工作记录)——但 agent 式长 turn 让该禁令
|
|
984
|
+
// 全程生效,目标线失效、长任务只能落 overflow 削尖。保护改由活跃窗口承担
|
|
985
|
+
// (切割处 turn 内 tail 扩到 midTurnProtectLastN 条),窗口外执行痕迹正常压缩。
|
|
986
|
+
const allowNewLlmCompact = !!summarize;
|
|
987
|
+
// 未超线:fold 视图(记录天然粘滞)
|
|
503
988
|
if (currentTokens <= threshold) {
|
|
504
|
-
if (
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
return { messages };
|
|
989
|
+
if (hasCompactView) {
|
|
990
|
+
this.states.set(stateKey, {
|
|
991
|
+
...state,
|
|
992
|
+
lastProactiveTokens: this.measureRequestViewTokens(folded, agingPlan, estimator),
|
|
993
|
+
});
|
|
510
994
|
}
|
|
511
|
-
|
|
512
|
-
this.states.set(stateKey, {
|
|
513
|
-
...state,
|
|
514
|
-
lastProactiveTokens: reducedTokens,
|
|
515
|
-
lastEstimatedTokens: reducedTokens,
|
|
516
|
-
});
|
|
517
|
-
return { messages: sticky.messages };
|
|
995
|
+
return { view: folded.view, fold: folded };
|
|
518
996
|
}
|
|
519
997
|
if (messages.length <= protectFirstN + protectLastN) {
|
|
520
|
-
return {
|
|
998
|
+
return { view: folded.view, fold: folded };
|
|
521
999
|
}
|
|
522
1000
|
// 判据:是否开启新一轮压缩 —— 只看「自上次压缩以来的新增条数」与冷却。
|
|
523
|
-
// 注意:只比「自上次压缩以来新增条数」,不要拿全量 token 和压缩后视图比;
|
|
524
|
-
// 也不要把「有无 previousSummary」当判据:压缩可能是机械截断(无摘要),
|
|
525
|
-
// 拿摘要存在与否当判据会让无摘要会话每轮重裁、复用分支永远进不去。
|
|
526
1001
|
const lastCount = state?.lastProactiveMessageCount;
|
|
527
1002
|
const newSinceLast = lastCount === undefined ? messages.length : messages.length - lastCount;
|
|
528
1003
|
const minNewForResummarize = Math.max(3, Math.floor(protectLastN * 0.25));
|
|
529
|
-
// 增长门(target
|
|
530
|
-
//
|
|
531
|
-
//
|
|
532
|
-
//
|
|
533
|
-
|
|
1004
|
+
// 增长门(target 模式;防「偶发兜底」变「周期压缩」):只有**同一请求口径**读数较上次
|
|
1005
|
+
// **压缩完成时**实质增长(≥ 25% 阈值)才允许再打 LLM。基数只认 lastCompactGateTokens——
|
|
1006
|
+
// 不能用 lastProactiveTokens(每轮装配都刷新)当基数,否则基数永远追上当前读数、
|
|
1007
|
+
// grownEnough 恒 false,压缩永久饿死。
|
|
1008
|
+
// 基数单位 raw(M-3):门基跨时间比对,比较时用**当前** ratio 折到校准单位(与
|
|
1009
|
+
// currentTokens 同尺)——ratio 漂移/重启换尺不再让两端异尺(饿死/抢跑)。
|
|
1010
|
+
const lastViewRaw = state?.lastCompactGateTokens;
|
|
1011
|
+
const lastViewTokens = lastViewRaw === undefined ? undefined : this.sizeLedger.calibrate(calKey, lastViewRaw);
|
|
534
1012
|
const grownEnough = this.config.targetTokens <= 0 ||
|
|
535
|
-
|
|
536
|
-
currentTokens >=
|
|
1013
|
+
lastViewTokens === undefined ||
|
|
1014
|
+
currentTokens >= lastViewTokens + Math.max(1, Math.floor(threshold * 0.25));
|
|
537
1015
|
const needNewCompaction = newSinceLast >= minNewForResummarize && grownEnough;
|
|
538
|
-
// 冷却:刚做过 LLM
|
|
1016
|
+
// 冷却:刚做过 LLM 摘要则优先零 LLM 复用,降低单 turn 双摘要概率
|
|
539
1017
|
const cooldownMs = this.config.proactiveCooldownMs;
|
|
540
1018
|
const inCooldown = cooldownMs > 0 &&
|
|
541
1019
|
state?.lastProactiveLlmAt != null &&
|
|
542
1020
|
Date.now() - state.lastProactiveLlmAt < cooldownMs;
|
|
543
1021
|
// 复用重建(零 LLM):已压缩过,且增量小或在冷却期或禁止新 LLM。
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
reason: 'proactive',
|
|
569
|
-
tokensBefore: currentTokens,
|
|
570
|
-
tokensAfter: reducedTokens,
|
|
571
|
-
durationMs: 0,
|
|
572
|
-
cached: true,
|
|
573
|
-
});
|
|
574
|
-
return { messages: sticky.messages };
|
|
575
|
-
}
|
|
1022
|
+
// fold 即重建——记录是持久事实,复用不改任何状态(增长门基数不动)。
|
|
1023
|
+
if (hasCompactView && (!allowNewLlmCompact || !needNewCompaction || inCooldown)) {
|
|
1024
|
+
this.states.set(stateKey, {
|
|
1025
|
+
...state,
|
|
1026
|
+
lastProactiveTokens: this.measureRequestViewTokens(folded, agingPlan, estimator),
|
|
1027
|
+
});
|
|
1028
|
+
// 复用重建 ≠ 压缩(2026-11-06 事故的 1ms「compacted」误报家族):cached=true
|
|
1029
|
+
// 使 adapter 不入会话流(既有语义);计量对 0/0(无新压缩计量)
|
|
1030
|
+
emit?.({
|
|
1031
|
+
type: 'context.compact.start',
|
|
1032
|
+
sessionId,
|
|
1033
|
+
reason: 'proactive',
|
|
1034
|
+
threshold,
|
|
1035
|
+
});
|
|
1036
|
+
emit?.({
|
|
1037
|
+
type: 'context.compact.end',
|
|
1038
|
+
sessionId,
|
|
1039
|
+
reason: 'proactive',
|
|
1040
|
+
tokensBefore: 0,
|
|
1041
|
+
tokensAfter: 0,
|
|
1042
|
+
durationMs: 0,
|
|
1043
|
+
cached: true,
|
|
1044
|
+
});
|
|
1045
|
+
return { view: folded.view, fold: folded };
|
|
576
1046
|
}
|
|
577
|
-
//
|
|
1047
|
+
// 无 summarize:超线也不打摘要 LLM(fold 机械分级照常)
|
|
578
1048
|
if (!allowNewLlmCompact) {
|
|
579
|
-
|
|
580
|
-
return sticky ? { messages: sticky.messages } : { messages };
|
|
1049
|
+
return { view: folded.view, fold: folded };
|
|
581
1050
|
}
|
|
582
1051
|
// 需要新一轮压缩:压缩 head/tail 之间的中间段(语义历史,不含 grounding)。
|
|
583
|
-
// requireSummary:主交付是语义摘要(缓解长会话失忆),机械截断达标也必须补摘要——
|
|
584
|
-
// 否则 previousSummary 永远为空、复用分支永远进不去,每个 turn 都会重裁+发通知。
|
|
585
1052
|
if (middle.length === 0) {
|
|
586
|
-
return {
|
|
1053
|
+
return { view: folded.view, fold: folded };
|
|
1054
|
+
}
|
|
1055
|
+
// 待并入摘要的新消息 = 「上一份摘要覆盖终点之后、保护尾之前」。
|
|
1056
|
+
// 视图超线但这里为 0(中段全被既有摘要覆盖):重压只会原样复述上一份交接书——
|
|
1057
|
+
// P2 让重压请求变便宜后,若不拦住就会退化成每轮一次「摘要风暴」。零 LLM 复用。
|
|
1058
|
+
// M-4/C1(评审 del-e1ee43ef):覆盖终点与 previousSummary 同源(summary 记录覆盖端 S)。
|
|
1059
|
+
// derivedCoverEnd 含 degrade 多 run 的最大端 E(E>S 常态):以 E 判零新会饿死真摘要,
|
|
1060
|
+
// 以 E 当素材起点会把 [S,E) 未摘要原文排除出素材而被新记录吞掉(静默丢数据)。
|
|
1061
|
+
const summaryEnd = this.deriveSummaryCoverEnd(folded);
|
|
1062
|
+
const freshStart = Math.max(summaryEnd ?? headEnd, headEnd);
|
|
1063
|
+
const coveredCount = freshStart - headEnd;
|
|
1064
|
+
const newFoldCount = Math.max(0, tailStart - freshStart);
|
|
1065
|
+
if (coveredCount > 0 && newFoldCount === 0) {
|
|
1066
|
+
if (hasCompactView) {
|
|
1067
|
+
this.states.set(stateKey, {
|
|
1068
|
+
...state,
|
|
1069
|
+
lastProactiveTokens: this.measureRequestViewTokens(folded, agingPlan, estimator),
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
return { view: folded.view, fold: folded };
|
|
587
1073
|
}
|
|
588
1074
|
const proactiveStart = Date.now();
|
|
1075
|
+
// P3:摘要调用的预算信号(run abort ∪ 单次摘要超时);取消/超时都从这里出来
|
|
1076
|
+
const summarizeBudget = this.createSummarizeSignal(params.signal);
|
|
1077
|
+
if (params.signal?.aborted) {
|
|
1078
|
+
// 上游已中止:不发起压缩、不写记录(本轮已经作废,留下记录只会污染历史)
|
|
1079
|
+
return { view: folded.view, fold: folded };
|
|
1080
|
+
}
|
|
1081
|
+
// 事件计量口径同 overflow(M3 统一,报告尺 reportRequestTokens):全请求估算、同集合前后对
|
|
1082
|
+
const compactTokensBefore = this.reportRequestTokens(folded.view, systemPrompt, tools, agingPlan, rawEstimator);
|
|
589
1083
|
emit?.({
|
|
590
1084
|
type: 'context.compact.start',
|
|
591
1085
|
sessionId,
|
|
592
1086
|
reason: 'proactive',
|
|
593
|
-
tokensBefore:
|
|
1087
|
+
tokensBefore: compactTokensBefore,
|
|
594
1088
|
threshold,
|
|
595
1089
|
});
|
|
596
|
-
let
|
|
1090
|
+
let outcome;
|
|
597
1091
|
try {
|
|
598
|
-
//
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
1092
|
+
// P2(2026-11-07):前缀 = **折叠视图**,与主会话请求逐字节同形 → 吃前缀缓存。
|
|
1093
|
+
// 旧实现用原 messages(全部历史):折叠生效后两者形状不同 → 缓存 0 命中,
|
|
1094
|
+
// 且每次重压都把已压缩过的中段原封不动再发一遍(实测 2.13MB 请求、provider 不返回)。
|
|
1095
|
+
// 请求视图(原子产物):messages 与 tools 同批解析,摘要请求原样复用这份前缀。
|
|
1096
|
+
// 旧实现只取 messages 半区、tools 由调用方另传——2026-11-12 前缀缓存事故的形状。
|
|
1097
|
+
// recordBaseline:false —— 摘要请求不覆盖会话基线
|
|
1098
|
+
const view = this.buildRequestView({
|
|
1099
|
+
messages: folded.view,
|
|
1100
|
+
systemPrompt,
|
|
1101
|
+
tools,
|
|
1102
|
+
aging: agingPlan,
|
|
1103
|
+
recordBaseline: false,
|
|
1104
|
+
});
|
|
1105
|
+
const llmPrefix = view.messages;
|
|
1106
|
+
this.checkSummaryPrefix(stateKey, view);
|
|
1107
|
+
// 回声源排除既有摘要占位:模型续写上一份交接书不是「复读原文」
|
|
1108
|
+
const echoSource = this.buildEchoSource(folded.view, systemPrompt, agingPlan);
|
|
1109
|
+
// P3 可观测:摘要请求即将发出——如实上报请求体量与预算,UI 不再只显示「进行中」。
|
|
1110
|
+
// 体量含工具面:tools 与 messages 一起构成真实 wire 请求(前缀 tools → system → messages)。
|
|
1111
|
+
emit?.({
|
|
1112
|
+
type: 'context.compact.progress',
|
|
1113
|
+
sessionId,
|
|
1114
|
+
reason: 'proactive',
|
|
1115
|
+
phase: 'summarize',
|
|
1116
|
+
requestChars: JSON.stringify(llmPrefix).length + (tools.length > 0 ? JSON.stringify(tools).length : 0),
|
|
1117
|
+
// token 口径(2026-11-13):字符数与顶栏 token 并排显示会误导(实测 966K 字符
|
|
1118
|
+
// ≈ 348k token,看起来像"请求比上下文大 3 倍")。面板显示 token,字符仅留内部诊断。
|
|
1119
|
+
// 尺子唯一(2026-11-14):messages 与 tools 必须在**同一把尺**上相加。旧实现
|
|
1120
|
+
// `estimateLLMMessages(llmPrefix)` 走裸估算器、`estimator.estimateTools()` 走校准
|
|
1121
|
+
// 估算器,把「未校准 messages 187k + 校准 tools 13k」加成一个不存在的量
|
|
1122
|
+
// (面板 200.7k,而 provider 实测 154.2k、顶栏 146.7k)。两边先用 raw 估算,
|
|
1123
|
+
// 再统一乘一次 ratio——与 assemble 的 rawEstimatedTokens 同式,口径不可再分叉。
|
|
1124
|
+
requestTokens: Math.ceil(this.sizeLedger.calibrate(calKey, estimateLLMMessages(llmPrefix) + rawEstimator.estimateTools(tools))),
|
|
1125
|
+
requestMessages: llmPrefix.length,
|
|
1126
|
+
timeoutMs: this.config.summarizeTimeoutMs || undefined,
|
|
1127
|
+
});
|
|
1128
|
+
// requireSummary 语义 → mode 'structure_only':机械截断达标也必须尝试摘要级
|
|
1129
|
+
outcome = await this.config.compactEngine.compactMessages(middle, {
|
|
1130
|
+
protectHead: 0,
|
|
1131
|
+
protectTail: 0,
|
|
602
1132
|
targetTokens: targetMiddleTokens,
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
1133
|
+
mode: 'structure_only',
|
|
1134
|
+
previousSummary: this.deriveSummary(ledger),
|
|
1135
|
+
// M-4:textify 旁路只文本化未覆盖新消息(合并契约:previousSummary + 新消息 =
|
|
1136
|
+
// 完整更新),已覆盖历史不以 raw middle 复发;锚排除与 middleRaw 同式。
|
|
1137
|
+
// C1(评审 del-e1ee43ef):起点 = previousSummary 覆盖端(同源)——[S,E) 原文必须
|
|
1138
|
+
// 进素材,否则新记录 covers 吞掉全区间而模型从未见过它(静默丢数据)
|
|
1139
|
+
freshMessages: semanticHistory(messages.slice(freshStart, tailStart).filter((_m, i) => freshStart + i !== anchorIdx)),
|
|
1140
|
+
summarizeFn: summarize,
|
|
1141
|
+
taskSnapshot: params.taskSnapshot,
|
|
1142
|
+
estimator,
|
|
608
1143
|
// F8 统一契约:预算/折叠区间与手动路径同源
|
|
609
1144
|
summaryBudget: this.config.summaryBudget,
|
|
610
1145
|
foldRange: {
|
|
611
|
-
//
|
|
612
|
-
//
|
|
613
|
-
replacedCount:
|
|
1146
|
+
// 视图口径:前缀已含上一份摘要,本次只需并入它之后的新消息;
|
|
1147
|
+
// coveredCount>0 时指令改述为「继续合并」,与模型实际所见一致
|
|
1148
|
+
replacedCount: newFoldCount,
|
|
614
1149
|
total: messages.length,
|
|
615
|
-
preservedTail:
|
|
1150
|
+
preservedTail: messages.length - tailStart,
|
|
1151
|
+
...(coveredCount > 0 ? { coveredCount } : {}),
|
|
616
1152
|
},
|
|
617
1153
|
contextWindow: params.contextWindow,
|
|
618
1154
|
maxOutputTokens: params.maxOutputTokens,
|
|
1155
|
+
onSummarizeFail: 'truncate',
|
|
1156
|
+
// 原子前缀(messages + tools):一个入参,漏一半在类型层不成立。
|
|
1157
|
+
// 缓存前缀 = tools → system → messages,二者必须同批产出(2026-11-12 事故)。
|
|
1158
|
+
requestView: view,
|
|
1159
|
+
echoSource,
|
|
1160
|
+
signal: summarizeBudget.signal,
|
|
619
1161
|
// F22:cache-miss 旁路透传可观测事件
|
|
620
1162
|
emit: emit
|
|
621
|
-
? (e) =>
|
|
622
|
-
type
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
1163
|
+
? (e) => {
|
|
1164
|
+
if (e.type === 'compact.summarize_fallback') {
|
|
1165
|
+
emit({
|
|
1166
|
+
type: 'context.compact.summarize_fallback',
|
|
1167
|
+
sessionId,
|
|
1168
|
+
reason: 'proactive',
|
|
1169
|
+
fallback: e.reason,
|
|
1170
|
+
cached: e.cached,
|
|
1171
|
+
});
|
|
1172
|
+
}
|
|
1173
|
+
else if (e.type === 'compact.contamination') {
|
|
1174
|
+
// 闸门判定必须可见:被判污染→丢弃输出→重发整包前缀(348k token 级别)
|
|
1175
|
+
emit({
|
|
1176
|
+
type: 'context.compact.contamination',
|
|
1177
|
+
sessionId,
|
|
1178
|
+
reason: 'proactive',
|
|
1179
|
+
verdict: e.verdict,
|
|
1180
|
+
attempt: e.attempt,
|
|
1181
|
+
retryTriggered: e.verdict !== 'clean',
|
|
1182
|
+
...(e.echoHits !== undefined ? { echoHits: e.echoHits } : {}),
|
|
1183
|
+
...(e.echoHitChars !== undefined ? { echoHitChars: e.echoHitChars } : {}),
|
|
1184
|
+
...(e.textChars !== undefined ? { textChars: e.textChars } : {}),
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
628
1188
|
: undefined,
|
|
629
1189
|
});
|
|
630
1190
|
}
|
|
@@ -636,426 +1196,508 @@ export class DefaultContextEngine {
|
|
|
636
1196
|
error: err instanceof Error ? err.message : String(err),
|
|
637
1197
|
durationMs: Date.now() - proactiveStart,
|
|
638
1198
|
});
|
|
639
|
-
//
|
|
640
|
-
|
|
641
|
-
|
|
1199
|
+
// 主动摘要失败:不产记录,视图保持 fold(已有记录照常生效)
|
|
1200
|
+
return { view: folded.view, fold: folded };
|
|
1201
|
+
}
|
|
1202
|
+
// P3:上游中止(用户 abort)——本轮已作废,不写记录也不推进增长门基数,
|
|
1203
|
+
// 否则会用一次残缺压缩污染会话历史。M-1:收尾必须发终态事件(cancelled),
|
|
1204
|
+
// 否则 store 的 compact.active 永久悬挂(UI 卡「摘要进行中」)。
|
|
1205
|
+
if (params.signal?.aborted) {
|
|
1206
|
+
emit?.({
|
|
1207
|
+
type: 'context.compact.end',
|
|
1208
|
+
sessionId,
|
|
1209
|
+
reason: 'proactive',
|
|
1210
|
+
durationMs: Date.now() - proactiveStart,
|
|
1211
|
+
cancelled: true,
|
|
1212
|
+
});
|
|
1213
|
+
return { view: folded.view, fold: folded };
|
|
642
1214
|
}
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
1215
|
+
// P3:摘要超时 → 如实上报(kind=failed),按既有 onSummarizeFail 语义降级为机械记录
|
|
1216
|
+
if (summarizeBudget.timedOut() && outcome.reason?.startsWith('summarize_failed')) {
|
|
1217
|
+
emit?.({
|
|
1218
|
+
type: 'context.compact.error',
|
|
1219
|
+
sessionId,
|
|
1220
|
+
reason: 'proactive',
|
|
1221
|
+
error: `summarize_timeout:${this.config.summarizeTimeoutMs}ms (${outcome.reason})`,
|
|
1222
|
+
kind: 'failed',
|
|
1223
|
+
durationMs: Date.now() - proactiveStart,
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
if (outcome.reason?.startsWith('denied:')) {
|
|
1227
|
+
emit?.({
|
|
1228
|
+
type: 'context.compact.error', sessionId, reason: 'proactive', error: outcome.reason,
|
|
1229
|
+
kind: 'denied',
|
|
1230
|
+
});
|
|
1231
|
+
return { view: folded.view, fold: folded, droppedSummary: outcome.reason };
|
|
1232
|
+
}
|
|
1233
|
+
// fresh(评审 F8-F1)= 本次产出**新的**语义摘要——只决定新记录的类型:
|
|
1234
|
+
// summary vs degrade。旧记录是否生效与门控无关(记录是持久事实)。
|
|
1235
|
+
const fresh = outcome.fresh === true &&
|
|
1236
|
+
Boolean(outcome.summary) &&
|
|
1237
|
+
!isMechanicalNotice(outcome.summary);
|
|
1238
|
+
const proactiveRecord = await this.appendRecordFromProduct({
|
|
1239
|
+
sessionId,
|
|
1240
|
+
agentId: params.agentId,
|
|
1241
|
+
messages,
|
|
1242
|
+
input: middleRaw,
|
|
1243
|
+
productView: Array.isArray(outcome.view) ? outcome.view : middleRaw,
|
|
1244
|
+
fold: folded,
|
|
1245
|
+
agingPlan,
|
|
1246
|
+
fresh,
|
|
1247
|
+
summarizeAttempted: outcome.summarizeAttempted === true,
|
|
1248
|
+
reason: 'proactive',
|
|
1249
|
+
summaryText: outcome.summary,
|
|
1250
|
+
rawEstimator,
|
|
1251
|
+
});
|
|
1252
|
+
folded = this.foldView(messages, ledger);
|
|
1253
|
+
const reducedTokens = this.measureRequestViewTokens(folded, agingPlan, estimator);
|
|
1254
|
+
// 增长门基数单位 raw(M-3):raw 是时间稳定单位(ratio 漂移/重启不改写历史存量),
|
|
1255
|
+
// 比较点再用当前 ratio 折算到校准单位(grownEnough 处)
|
|
1256
|
+
const gateBaseRaw = this.measureRequestViewTokens(folded, agingPlan, rawEstimator);
|
|
1257
|
+
// 增长门计数随任何**完成的压缩轮**推进(含机械降级)——它是「自上次压缩新增条数」
|
|
1258
|
+
// 的基数;只随 fresh 推进会让无摘要会话每轮被判全量新增、复用分支永远进不去。
|
|
650
1259
|
this.states.set(stateKey, {
|
|
651
1260
|
...state,
|
|
652
|
-
previousSummary: fresh ? actualSummary : state?.previousSummary,
|
|
653
|
-
lastEstimatedTokens: reducedTokens,
|
|
654
|
-
// 增长门计数随任何**完成的压缩轮**推进(含机械降级)——它是「自上次压缩新增条数」的
|
|
655
|
-
// 基数与压缩态粘滞标记;若只随 fresh 推进,无摘要会话每轮被判全量新增、复用分支
|
|
656
|
-
// 永远进不去(每 turn 重打 LLM)。「有计数无摘要」是生产状态形状(重启恢复快照
|
|
657
|
-
// 即此形状)。覆盖终点语义不同:只随 fresh 推进(被丢的中段由 sticky 增量兜住)。
|
|
658
1261
|
lastProactiveMessageCount: messages.length,
|
|
659
|
-
...(fresh ? { lastProactiveCoverEnd: tailStart } : {}),
|
|
660
1262
|
lastProactiveTokens: reducedTokens,
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
//
|
|
664
|
-
|
|
1263
|
+
// 增长门基数只在**压缩完成**时推进(raw 口径,与比较点折算配对)
|
|
1264
|
+
lastCompactGateTokens: gateBaseRaw,
|
|
1265
|
+
// 锚卫生:压缩改变请求形状,旧锚的 (actual,raw) 对不再对应任何未来请求 → 即刻失效
|
|
1266
|
+
lastUsageAnchor: undefined,
|
|
1267
|
+
// 只在 LLM 摘要调用真正发起过时推进冷却时间戳(成功/失败均算发起,防打爆)
|
|
1268
|
+
lastProactiveLlmAt: outcome.summarizeAttempted ? Date.now() : state?.lastProactiveLlmAt,
|
|
665
1269
|
});
|
|
1270
|
+
// 估算一次、两处引用(tokensAfter 与 contextSize 同值同源)
|
|
1271
|
+
const tokensAfter = this.reportRequestTokens(folded.view, systemPrompt, tools, agingPlan, rawEstimator);
|
|
666
1272
|
emit?.({
|
|
667
1273
|
type: 'context.compact.end',
|
|
668
1274
|
sessionId,
|
|
669
1275
|
reason: 'proactive',
|
|
670
|
-
tokensBefore:
|
|
671
|
-
tokensAfter
|
|
1276
|
+
tokensBefore: compactTokensBefore,
|
|
1277
|
+
tokensAfter,
|
|
1278
|
+
// contextSize 显示槽:仅真压缩(有砍除记录)才携带——零覆盖轮不动显示(评审 F-1)
|
|
1279
|
+
contextSize: proactiveRecord !== undefined ? contextSizeSnapshot('compact-est', tokensAfter) : undefined,
|
|
672
1280
|
durationMs: Date.now() - proactiveStart,
|
|
673
1281
|
cached: false,
|
|
674
1282
|
});
|
|
675
1283
|
return {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
}
|
|
680
|
-
/**
|
|
681
|
-
* 保守降级(overflow **middle 替换体**):工具账本说明 + 近尾
|
|
682
|
-
* 外层 head/tail 由 assemble 重组拼接,此处不得重复携带。
|
|
683
|
-
*
|
|
684
|
-
* @param messages - 待降级消息(overflow 段)
|
|
685
|
-
* @param _estimator - token 估算器(保留签名对称)
|
|
686
|
-
* @returns 降级后的消息列表
|
|
687
|
-
*/
|
|
688
|
-
async conservativeTruncate(messages, _estimator) {
|
|
689
|
-
const keepCount = Math.min(Math.max(6, Math.ceil(messages.length * 0.15)), messages.length);
|
|
690
|
-
const kept = messages.slice(-keepCount);
|
|
691
|
-
const dropped = messages.slice(0, messages.length - keepCount);
|
|
692
|
-
// 无可丢弃:原样返回(禁止「0 removed」假通知膨胀)
|
|
693
|
-
if (dropped.length === 0) {
|
|
694
|
-
return [...messages];
|
|
695
|
-
}
|
|
696
|
-
const body = buildToolCallLedger(dropped);
|
|
697
|
-
const notice = {
|
|
698
|
-
role: 'user',
|
|
699
|
-
content: body
|
|
700
|
-
? `[Context truncated: ${dropped.length} earlier messages removed to fit context window]\n[Tool call ledger — do NOT redo these unless explicitly asked]\n${body}`
|
|
701
|
-
: `[Context truncated: ${dropped.length} earlier messages removed to fit context window]`,
|
|
702
|
-
timestamp: Date.now(),
|
|
703
|
-
metadata: { source: 'contextSummary' },
|
|
704
|
-
};
|
|
705
|
-
return [notice, ...kept];
|
|
706
|
-
}
|
|
707
|
-
/**
|
|
708
|
-
* 从压缩结果中提取语义摘要正文(contextSummary 标记或 [Conversation Summary] 前缀)。
|
|
709
|
-
* 机械降级的占位说明([Context truncated / [Context compressed / [Structural compact])
|
|
710
|
-
* 不算语义摘要:把它们当摘要存入 previousSummary 会污染增量合并输入,
|
|
711
|
-
* 并让复用分支拿到假摘要、掩盖「从未真正摘要」的事实。
|
|
712
|
-
*/
|
|
713
|
-
extractSummaryText(compressed) {
|
|
714
|
-
for (const m of compressed) {
|
|
715
|
-
if (m.metadata?.source === 'contextSummary' &&
|
|
716
|
-
typeof m.content === 'string' &&
|
|
717
|
-
m.content.trim() &&
|
|
718
|
-
!this.isMechanicalNotice(m.content)) {
|
|
719
|
-
return m.content;
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
for (const m of compressed) {
|
|
723
|
-
if (typeof m.content === 'string' &&
|
|
724
|
-
m.content.includes('[Conversation Summary]') &&
|
|
725
|
-
!this.isMechanicalNotice(m.content)) {
|
|
726
|
-
return m.content;
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
return undefined;
|
|
730
|
-
}
|
|
731
|
-
/**
|
|
732
|
-
* 机械降级占位说明判别(非语义摘要):首条实质行以占位标记开头即认定。
|
|
733
|
-
* 只看首行而非全文 includes——真实摘要引用这些字符串(如记录「曾发生截断」)不得被误杀。
|
|
734
|
-
*
|
|
735
|
-
* @param text - 候选摘要正文
|
|
736
|
-
* @returns 是否为机械占位说明
|
|
737
|
-
*/
|
|
738
|
-
isMechanicalNotice(text) {
|
|
739
|
-
for (const raw of text.split('\n')) {
|
|
740
|
-
const line = raw.trim();
|
|
741
|
-
if (!line || line === '[Conversation Summary]')
|
|
742
|
-
continue;
|
|
743
|
-
return (line.startsWith('[Context truncated:') ||
|
|
744
|
-
line.startsWith('[Context compressed:') ||
|
|
745
|
-
line.startsWith('[Structural compact]'));
|
|
746
|
-
}
|
|
747
|
-
return false;
|
|
748
|
-
}
|
|
749
|
-
/**
|
|
750
|
-
* 判断是否工具结果消息(视图边界对齐用)
|
|
751
|
-
*
|
|
752
|
-
* @param msg - 待判定消息
|
|
753
|
-
* @returns 是否为工具结果消息
|
|
754
|
-
*/
|
|
755
|
-
isToolResultMessage(msg) {
|
|
756
|
-
return msg.role === 'tool' || (msg.toolResults?.length ?? 0) > 0;
|
|
757
|
-
}
|
|
758
|
-
/**
|
|
759
|
-
* 视图切分边界对齐(不拆 tool_call/tool_result 对)
|
|
760
|
-
*
|
|
761
|
-
* head 不得停在带 toolCalls 的 assistant 上(其结果落入中间段,视图里会成悬空调用);
|
|
762
|
-
* tail 不得起于 tool 结果(其调用在中间段,会成孤儿结果)——两种形状都会被严格网关 400。
|
|
763
|
-
*
|
|
764
|
-
* @param messages - 全量消息
|
|
765
|
-
* @param headCount - 期望头部条数
|
|
766
|
-
* @param tailCount - 期望尾部条数
|
|
767
|
-
* @returns 对齐后的 head 终点(不含)与 tail 起点(含)
|
|
768
|
-
*/
|
|
769
|
-
alignViewSplit(messages, headCount, tailCount) {
|
|
770
|
-
let headEnd = Math.min(headCount, messages.length);
|
|
771
|
-
while (headEnd > 0 && (messages[headEnd - 1]?.toolCalls?.length ?? 0) > 0) {
|
|
772
|
-
headEnd--;
|
|
773
|
-
}
|
|
774
|
-
let tailStart = Math.max(0, messages.length - tailCount);
|
|
775
|
-
while (tailStart > headEnd && this.isToolResultMessage(messages[tailStart])) {
|
|
776
|
-
tailStart--;
|
|
777
|
-
}
|
|
778
|
-
return { headEnd, tailStart };
|
|
779
|
-
}
|
|
780
|
-
/**
|
|
781
|
-
* 压缩/摘要的语义输入:丢掉托管 system 与 turn 信封元数据
|
|
782
|
-
* (turn_context picks 不进 previousSummary——防陈旧择取固化进摘要);
|
|
783
|
-
* turn_start 只保留 <user_message> 正文。
|
|
784
|
-
*/
|
|
785
|
-
semanticHistory(messages) {
|
|
786
|
-
const out = [];
|
|
787
|
-
for (const m of messages) {
|
|
788
|
-
if (m.role === 'system' && m.metadata?.source === 'systemPrompt')
|
|
789
|
-
continue;
|
|
790
|
-
if (m.metadata?.source === 'turnEnd')
|
|
791
|
-
continue;
|
|
792
|
-
if (m.metadata?.source === 'knowledgeGrounding')
|
|
793
|
-
continue;
|
|
794
|
-
if (m.metadata?.source === 'runtimeSnapshot')
|
|
795
|
-
continue;
|
|
796
|
-
if (m.metadata?.source === 'sessionAttachment')
|
|
797
|
-
continue;
|
|
798
|
-
if (isTurnStartContent(m.content)) {
|
|
799
|
-
out.push({ ...m, content: extractUserMessageText(m.content) });
|
|
800
|
-
continue;
|
|
801
|
-
}
|
|
802
|
-
out.push(m);
|
|
803
|
-
}
|
|
804
|
-
return out;
|
|
805
|
-
}
|
|
806
|
-
/**
|
|
807
|
-
* 摘要消息构造(装配/度量共用)——禁止在别处手抄此形状,防口径漂移
|
|
808
|
-
*/
|
|
809
|
-
contextSummaryMessage(summary, coveredEnd) {
|
|
810
|
-
// 覆盖边界行(评审 F11):按覆盖点动态渲染,不写进 previousSummary——
|
|
811
|
-
// 写进摘要文本会把过期数字烘死、随失败累积、还会被当 Previous summary 回喂模型。
|
|
812
|
-
const coverage = coveredEnd && coveredEnd > 0
|
|
813
|
-
? `\n\n[Coverage] This handoff covers the conversation up to message ${coveredEnd}; later messages appear verbatim and take precedence.`
|
|
814
|
-
: '';
|
|
815
|
-
return {
|
|
816
|
-
role: 'user',
|
|
817
|
-
content: wrapContextSummary(`${summary}${coverage}`),
|
|
818
|
-
timestamp: Date.now(),
|
|
819
|
-
metadata: { source: 'contextSummary' },
|
|
820
|
-
};
|
|
821
|
-
}
|
|
822
|
-
/**
|
|
823
|
-
* 粘性视图布局 = head + 摘要 + tail(压缩后请求视图的唯一实现,装配与度量共用)
|
|
824
|
-
*
|
|
825
|
-
* @param messages - 全量原始消息
|
|
826
|
-
* @param protectFirstN - 头部保护条数
|
|
827
|
-
* @param protectLastN - 尾部保护条数
|
|
828
|
-
* @param summary - 语义摘要正文
|
|
829
|
-
* @returns 视图消息列表;总长 ≤ 头尾保护无需压缩时返回 null
|
|
830
|
-
*/
|
|
831
|
-
stickyViewMessages(messages, protectFirstN, protectLastN, summary, coveredEnd, extra) {
|
|
832
|
-
if (messages.length <= protectFirstN + protectLastN) {
|
|
833
|
-
return null;
|
|
834
|
-
}
|
|
835
|
-
// 边界对齐(不拆 tool_call/tool_result 对):未对齐的 head/tail 会被严格网关 400
|
|
836
|
-
const { headEnd, tailStart } = this.alignViewSplit(messages, protectFirstN, protectLastN);
|
|
837
|
-
return [
|
|
838
|
-
...messages.slice(0, headEnd),
|
|
839
|
-
this.contextSummaryMessage(summary, coveredEnd),
|
|
840
|
-
// 增量位:覆盖点之后、尾部保护区之前的「摘要没覆盖、tailStart 又前移」区间,
|
|
841
|
-
// 不显式保留会静默消失(评审 F12)。压缩时刻为空(coverEnd == tailStart),度量=装配不受影响
|
|
842
|
-
...(extra ?? []),
|
|
843
|
-
...messages.slice(tailStart),
|
|
844
|
-
];
|
|
845
|
-
}
|
|
846
|
-
/**
|
|
847
|
-
* 结构视图:有 previousSummary 时 head + summary + tail;否则原样
|
|
848
|
-
* 用于 contextWindow 未知时的 assemble / 手动压缩后的回放
|
|
849
|
-
*/
|
|
850
|
-
buildStructuralView(sessionId, messages, emit, stateKey) {
|
|
851
|
-
const state = this.states.get(stateKey ?? sessionId);
|
|
852
|
-
const summary = state?.previousSummary;
|
|
853
|
-
if (!summary || messages.length === 0) {
|
|
854
|
-
return { messages };
|
|
855
|
-
}
|
|
856
|
-
// 压缩时刻布局的唯一实现 stickyViewMessages(与 structuralCompact 的度量共用:度量=装配)。
|
|
857
|
-
// 本函数服务「压缩后回放」:覆盖点(lastProactiveCoverEnd)到当前尾部保护区之间是**增量**
|
|
858
|
-
// ——摘要没覆盖它们、tailStart 又随新消息前移,不显式保留会静默消失(评审 F12)。
|
|
859
|
-
// rebuildStickyView 是带增量压缩的异步版布局;这里同步保留增量原文(宁可胖,不可丢)。
|
|
860
|
-
const coverUpTo = state.lastProactiveCoverEnd ?? 0;
|
|
861
|
-
const { headEnd, tailStart: viewTailStart } = this.alignViewSplit(messages, this.config.protectFirstN, this.config.protectLastN);
|
|
862
|
-
const increment = this.semanticHistory(messages.slice(Math.max(coverUpTo, headEnd), viewTailStart));
|
|
863
|
-
const viewMsgs = this.stickyViewMessages(messages, this.config.protectFirstN, this.config.protectLastN, summary, state.lastProactiveCoverEnd, increment);
|
|
864
|
-
if (!viewMsgs) {
|
|
865
|
-
return { messages };
|
|
866
|
-
}
|
|
867
|
-
emit?.({
|
|
868
|
-
type: 'context.compact.start',
|
|
869
|
-
sessionId,
|
|
870
|
-
reason: 'proactive',
|
|
871
|
-
});
|
|
872
|
-
emit?.({
|
|
873
|
-
type: 'context.compact.end',
|
|
874
|
-
sessionId,
|
|
875
|
-
reason: 'proactive',
|
|
876
|
-
tokensBefore: 0,
|
|
877
|
-
tokensAfter: 0,
|
|
878
|
-
cached: true,
|
|
879
|
-
});
|
|
880
|
-
return {
|
|
881
|
-
messages: viewMsgs,
|
|
882
|
-
droppedSummary: 'Structural compact view (contextWindow unknown)',
|
|
1284
|
+
view: folded.view,
|
|
1285
|
+
fold: folded,
|
|
1286
|
+
droppedSummary: outcome.droppedSummary ?? 'Older turns proactively summarized',
|
|
883
1287
|
};
|
|
884
1288
|
}
|
|
885
1289
|
/**
|
|
886
1290
|
* 结构压缩:头尾保护 + 中间段摘要(不依赖 contextWindow)
|
|
887
|
-
* 算法委托 capabilities/compact
|
|
1291
|
+
* 算法委托 capabilities/compact;本引擎把产物翻译成转换记录。
|
|
888
1292
|
*/
|
|
889
1293
|
async structuralCompact(params) {
|
|
890
1294
|
const { sessionId, messages, summarize, estimator } = params;
|
|
1295
|
+
const stateKey = params.stateKey ?? compactStateKey(sessionId, params.agentId);
|
|
1296
|
+
const ledger = await this.loadLedger(sessionId, params.agentId, messages, params.loadCompactState);
|
|
1297
|
+
let folded = this.foldView(messages, ledger);
|
|
1298
|
+
const raw = this.config.tokenEstimator;
|
|
891
1299
|
// 同形前缀单源:优先调用方现成 llmPrefix;否则引擎内建。
|
|
892
1300
|
// system 字节取自会话托管 system 消息(source==='systemPrompt')——它就是上一轮请求的逐字
|
|
893
1301
|
// system;重走 assembler 重建会有字节漂移(打穿缓存)。托管缺失时回落调用方 systemPrompt。
|
|
894
|
-
// aging 计划与 assemble
|
|
1302
|
+
// aging 计划与 assemble 同式——禁止手抄视图。
|
|
895
1303
|
const managedSystem = messages.find((m) => m.role === 'system' && m.metadata?.source === 'systemPrompt');
|
|
896
1304
|
const systemText = (managedSystem ? getTextContent(managedSystem.content) : '') || params.systemPrompt;
|
|
897
|
-
const agingPlan = computeToolResultAgingPlan(messages, this.config.historyAging
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
1305
|
+
const agingPlan = computeToolResultAgingPlan(messages, this.config.historyAging, {
|
|
1306
|
+
toolAgers: params.toolAgers,
|
|
1307
|
+
toolArgAgers: params.toolArgAgers,
|
|
1308
|
+
});
|
|
1309
|
+
// 报告尺唯一公式在 reportRequestTokens(M3 统一):fold+老化+system+tools 的未校准
|
|
1310
|
+
// 全请求估算(与 assemble 的 rawEstimatedTokens/配对槽分母同式)。注意与「决策尺」分工:
|
|
1311
|
+
// proactive/overflow 触发走 measureRequestViewTokens(校准后 messages-only);增长门
|
|
1312
|
+
// **基数**存 raw 同形读数、比较点按当前 ratio 折算(M-3,评审 del-e1ee43ef)——report
|
|
1313
|
+
// vs decision 两把尺各有其职,勿互换(互换会搅乱增长门基数,见 arch/context-size-accounting.md §10.19)。
|
|
1314
|
+
const rawRequestViewTokens = (fold) => this.reportRequestTokens(fold.view, systemText, params.tools, agingPlan, raw);
|
|
1315
|
+
const tokensBefore = rawRequestViewTokens(folded);
|
|
1316
|
+
// P2:前缀 = 折叠视图(与主会话请求同形)。
|
|
1317
|
+
// 只能由 buildRequestView 产出(messages+tools 原子)——不接受调用方单独塞
|
|
1318
|
+
// messages:那正是「两半各走一路」的入口(评审 del-b5ffc13a MIN-4)。
|
|
1319
|
+
// recordBaseline:false —— 摘要请求不覆盖会话基线,否则护栏永远一致(形同虚设)
|
|
1320
|
+
const requestView = systemText
|
|
1321
|
+
? this.buildRequestView({
|
|
1322
|
+
messages: folded.view,
|
|
1323
|
+
systemPrompt: systemText,
|
|
1324
|
+
tools: params.tools ?? [],
|
|
1325
|
+
aging: agingPlan,
|
|
1326
|
+
recordBaseline: false,
|
|
1327
|
+
})
|
|
1328
|
+
: undefined;
|
|
1329
|
+
const llmPrefix = requestView?.messages;
|
|
1330
|
+
const manualStateKey = compactStateKey(sessionId, params.agentId);
|
|
1331
|
+
if (requestView) {
|
|
1332
|
+
this.checkSummaryPrefix(manualStateKey, requestView);
|
|
1333
|
+
}
|
|
1334
|
+
else {
|
|
1335
|
+
// 无 system 文本 → 视图由调用方提供,形状不可知;退回只比 tools(旧行为)
|
|
1336
|
+
this.checkSummaryToolFace(manualStateKey, params.tools ?? []);
|
|
1337
|
+
}
|
|
1338
|
+
// 回声源只用引擎自建的前缀形状(前缀=调用方视图时形状不可知)
|
|
1339
|
+
const echoSource = llmPrefix
|
|
1340
|
+
? this.buildEchoSource(folded.view, systemText ?? '', agingPlan)
|
|
1341
|
+
: undefined;
|
|
901
1342
|
const protectFirstN = this.config.protectFirstN;
|
|
902
1343
|
const protectLastN = this.config.protectLastN;
|
|
903
|
-
const tokensBefore = estimator.estimateMessages(messages);
|
|
904
|
-
const state = this.states.get(stateKey);
|
|
905
1344
|
if (messages.length <= protectFirstN + protectLastN) {
|
|
906
1345
|
return {
|
|
907
|
-
|
|
1346
|
+
ok: true,
|
|
1347
|
+
summary: this.deriveSummary(ledger),
|
|
908
1348
|
compacted: false,
|
|
1349
|
+
reason: 'too_short',
|
|
909
1350
|
tokensBefore,
|
|
910
1351
|
tokensAfter: tokensBefore,
|
|
911
|
-
coveredMessageCount:
|
|
1352
|
+
coveredMessageCount: this.derivedCoverEnd(folded),
|
|
1353
|
+
fresh: false,
|
|
1354
|
+
summarizeAttempted: false,
|
|
1355
|
+
view: folded.view,
|
|
912
1356
|
};
|
|
913
1357
|
}
|
|
914
|
-
//
|
|
915
|
-
//
|
|
916
|
-
//
|
|
917
|
-
const { headEnd, tailStart } =
|
|
918
|
-
const middle =
|
|
1358
|
+
// 覆盖坐标:原始 messages 上 alignViewSplit → middle 语义历史 → 摘要;记录覆盖
|
|
1359
|
+
// [headEnd, tailStart)。禁止对 semanticHistory(全量) 切 protectHead/Tail(过滤数组
|
|
1360
|
+
// 下标 ≠ 原始下标,会丢消息)。
|
|
1361
|
+
const { headEnd, tailStart } = alignViewSplit(messages, protectFirstN, protectLastN);
|
|
1362
|
+
const middle = semanticHistory(messages.slice(headEnd, tailStart));
|
|
1363
|
+
// P2 视图口径:前缀已含上一份摘要,本次只需并入它之后、保护尾之前的新消息
|
|
1364
|
+
const summaryEnd = this.deriveSummaryCoverEnd(folded);
|
|
1365
|
+
const freshStart = Math.max(summaryEnd ?? headEnd, headEnd);
|
|
1366
|
+
const coveredCount = freshStart - headEnd;
|
|
1367
|
+
const newFoldCount = Math.max(0, tailStart - freshStart);
|
|
1368
|
+
// N2(评审 del-e1ee43ef):已全被摘要覆盖 = 无需压缩(语义成功,不是失败)——
|
|
1369
|
+
// 不发 progress、不打 LLM,直接以 already_covered 收口(否则退化为 empty_middle 失败)
|
|
1370
|
+
if (coveredCount > 0 && newFoldCount === 0) {
|
|
1371
|
+
return {
|
|
1372
|
+
ok: true,
|
|
1373
|
+
summary: this.deriveSummary(ledger),
|
|
1374
|
+
compacted: false,
|
|
1375
|
+
reason: 'already_covered',
|
|
1376
|
+
tokensBefore,
|
|
1377
|
+
tokensAfter: tokensBefore,
|
|
1378
|
+
coveredMessageCount: this.derivedCoverEnd(folded),
|
|
1379
|
+
fresh: false,
|
|
1380
|
+
summarizeAttempted: false,
|
|
1381
|
+
view: folded.view,
|
|
1382
|
+
};
|
|
1383
|
+
}
|
|
1384
|
+
// P3:手动压缩同样受摘要预算约束(无 run signal 时只有超时;超时 → 失败不产记录)
|
|
1385
|
+
const manualBudget = this.createSummarizeSignal(params.signal);
|
|
1386
|
+
if (llmPrefix) {
|
|
1387
|
+
params.emit?.({
|
|
1388
|
+
type: 'context.compact.progress',
|
|
1389
|
+
sessionId: params.sessionId,
|
|
1390
|
+
// m-2 顺修(评审 Nit):manual 显式带 reason——缺省会被 store 沿用上一次
|
|
1391
|
+
// proactive/overflow 的标签
|
|
1392
|
+
reason: 'manual',
|
|
1393
|
+
phase: 'summarize',
|
|
1394
|
+
requestChars: JSON.stringify(llmPrefix).length +
|
|
1395
|
+
(params.tools && params.tools.length > 0 ? JSON.stringify(params.tools).length : 0),
|
|
1396
|
+
// token 口径同上:与顶栏同一把尺子,避免字符/token 量纲混排
|
|
1397
|
+
// 尺子唯一(2026-11-14):messages 与 tools 必须同尺相加。旧实现 messages 走裸估算
|
|
1398
|
+
// (estimateLLMMessages)、tools 走校准估算(estimator.estimateTools),相加得到不存在的量。
|
|
1399
|
+
// 此处先用 raw 估算、最后乘一次 ratio,与同函数里 rawRequestViewTokens(tokensBefore
|
|
1400
|
+
// 的报告尺)同式,两处读数不可再分叉。
|
|
1401
|
+
requestTokens: Math.ceil(this.sizeLedger.calibrate(params.calKey, estimateLLMMessages(llmPrefix) + raw.estimateTools(params.tools ?? []))),
|
|
1402
|
+
requestMessages: llmPrefix.length,
|
|
1403
|
+
timeoutMs: this.config.summarizeTimeoutMs || undefined,
|
|
1404
|
+
});
|
|
1405
|
+
}
|
|
919
1406
|
const outcome = await this.config.compactEngine.compactMessages(middle, {
|
|
1407
|
+
sessionId: params.sessionId,
|
|
1408
|
+
...(params.agentId === undefined ? {} : { agentId: params.agentId }),
|
|
920
1409
|
protectHead: 0,
|
|
921
1410
|
protectTail: 0,
|
|
922
1411
|
targetTokens: params.compactTargetTokens,
|
|
923
1412
|
mode: 'structure_only',
|
|
924
|
-
previousSummary:
|
|
1413
|
+
previousSummary: this.deriveSummary(ledger),
|
|
1414
|
+
// M-4:textify 旁路只文本化未覆盖新消息(合并契约完整性),不重发已覆盖 raw 历史。
|
|
1415
|
+
// 起点 = previousSummary 覆盖端(同源,C1)——[S,E) 原文必须进素材,否则被新记录吞掉
|
|
1416
|
+
freshMessages: semanticHistory(messages.slice(Math.min(freshStart, tailStart), tailStart)),
|
|
925
1417
|
summarizeFn: summarize,
|
|
926
1418
|
estimator,
|
|
927
1419
|
// 软目标预算(P1c clamp(2%,2k,8k))交 compactCore 公式;可配置 ratio/min/max 经 summaryBudget
|
|
928
1420
|
summaryBudget: this.config.summaryBudget,
|
|
929
1421
|
taskSnapshot: params.taskSnapshot,
|
|
930
|
-
// 折叠区间(评审 F7
|
|
1422
|
+
// 折叠区间(评审 F7 / P2):摘要并入「上一份摘要之后、保护尾之前」的新消息
|
|
931
1423
|
foldRange: {
|
|
932
|
-
replacedCount:
|
|
1424
|
+
replacedCount: newFoldCount,
|
|
933
1425
|
total: messages.length,
|
|
934
1426
|
preservedTail: messages.length - tailStart,
|
|
1427
|
+
...(coveredCount > 0 ? { coveredCount } : {}),
|
|
935
1428
|
},
|
|
936
1429
|
contextWindow: params.contextWindow,
|
|
937
1430
|
maxOutputTokens: params.maxOutputTokens,
|
|
938
1431
|
onSummarizeFail: 'truncate',
|
|
939
|
-
|
|
1432
|
+
requestView,
|
|
1433
|
+
echoSource,
|
|
1434
|
+
signal: manualBudget.signal,
|
|
940
1435
|
// F8R-06:manual 路 cache-miss 旁路同样可观测(no_prefix/window_guard/window_exhausted)
|
|
941
1436
|
emit: params.emit
|
|
942
|
-
? (e) =>
|
|
943
|
-
type
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
1437
|
+
? (e) => {
|
|
1438
|
+
if (e.type === 'compact.summarize_fallback') {
|
|
1439
|
+
params.emit({
|
|
1440
|
+
type: 'context.compact.summarize_fallback',
|
|
1441
|
+
sessionId: params.sessionId,
|
|
1442
|
+
reason: 'manual',
|
|
1443
|
+
fallback: e.reason,
|
|
1444
|
+
cached: e.cached,
|
|
1445
|
+
});
|
|
1446
|
+
}
|
|
1447
|
+
else if (e.type === 'compact.contamination') {
|
|
1448
|
+
params.emit({
|
|
1449
|
+
type: 'context.compact.contamination',
|
|
1450
|
+
sessionId: params.sessionId,
|
|
1451
|
+
reason: 'manual',
|
|
1452
|
+
verdict: e.verdict,
|
|
1453
|
+
attempt: e.attempt,
|
|
1454
|
+
retryTriggered: e.verdict !== 'clean',
|
|
1455
|
+
...(e.echoHits !== undefined ? { echoHits: e.echoHits } : {}),
|
|
1456
|
+
...(e.echoHitChars !== undefined ? { echoHitChars: e.echoHitChars } : {}),
|
|
1457
|
+
...(e.textChars !== undefined ? { textChars: e.textChars } : {}),
|
|
1458
|
+
});
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
948
1461
|
: undefined,
|
|
949
1462
|
});
|
|
950
1463
|
// fresh(评审 F1)= 本次产出**新的**语义摘要。false 时 outcome.summary 只是旧摘要兜底:
|
|
951
|
-
//
|
|
952
|
-
//
|
|
1464
|
+
// 不得产出 summary 型记录。**manual 失败轮不产任何记录**——失败语义(F9/F10)是
|
|
1465
|
+
// 「中段原文仍在视图」(宁可胖不丢),产物被整体丢弃,没有裁剪就没有记录。
|
|
953
1466
|
const fresh = outcome.fresh === true &&
|
|
954
1467
|
Boolean(outcome.summary) &&
|
|
955
|
-
!
|
|
1468
|
+
!isMechanicalNotice(outcome.summary);
|
|
956
1469
|
if (!fresh) {
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
1470
|
+
const failReason = outcome.reason?.startsWith('summarize_failed') || outcome.reason?.startsWith('denied:')
|
|
1471
|
+
? outcome.reason
|
|
1472
|
+
: `summarize_failed:${outcome.reason ?? 'no_output'}`;
|
|
1473
|
+
// M1(评审 del-e1ee43ef):manual 终端事件——progress 已置 store active,失败若不发
|
|
1474
|
+
// error/end 会让「摘要进行中」永久悬挂(progress 不是 start,没有别的路径收敛它)
|
|
1475
|
+
if (llmPrefix) {
|
|
1476
|
+
params.emit?.({
|
|
1477
|
+
type: 'context.compact.error',
|
|
1478
|
+
sessionId: params.sessionId,
|
|
1479
|
+
reason: 'manual',
|
|
1480
|
+
error: failReason,
|
|
1481
|
+
kind: failReason.startsWith('denied:') ? 'denied' : 'failed',
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
960
1484
|
return {
|
|
961
|
-
|
|
1485
|
+
ok: true,
|
|
1486
|
+
summary: this.deriveSummary(ledger),
|
|
962
1487
|
compacted: false,
|
|
963
|
-
reason:
|
|
964
|
-
? outcome.reason
|
|
965
|
-
: `summarize_failed:${outcome.reason ?? 'no_output'}`,
|
|
1488
|
+
reason: failReason,
|
|
966
1489
|
tokensBefore,
|
|
967
1490
|
tokensAfter: tokensBefore,
|
|
968
|
-
coveredMessageCount:
|
|
1491
|
+
coveredMessageCount: this.derivedCoverEnd(folded),
|
|
1492
|
+
fresh: false,
|
|
1493
|
+
summarizeAttempted: outcome.summarizeAttempted,
|
|
1494
|
+
// 宁可胖不丢:中段原文仍在视图(未裁剪,无记录)
|
|
1495
|
+
view: folded.view,
|
|
969
1496
|
};
|
|
970
1497
|
}
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
1498
|
+
// fresh:产物 → summary 记录(覆盖 [headEnd, tailStart)),fold 出视图
|
|
1499
|
+
const record = await this.appendRecordFromProduct({
|
|
1500
|
+
sessionId,
|
|
1501
|
+
agentId: params.agentId,
|
|
1502
|
+
messages,
|
|
1503
|
+
input: messages.slice(headEnd, tailStart),
|
|
1504
|
+
productView: Array.isArray(outcome.view) ? outcome.view : [],
|
|
1505
|
+
fold: folded,
|
|
1506
|
+
agingPlan,
|
|
1507
|
+
fresh: true,
|
|
1508
|
+
summarizeAttempted: outcome.summarizeAttempted === true,
|
|
1509
|
+
reason: 'manual',
|
|
1510
|
+
summaryText: outcome.summary,
|
|
1511
|
+
rawEstimator: raw,
|
|
1512
|
+
});
|
|
1513
|
+
folded = this.foldView(messages, ledger);
|
|
1514
|
+
// after 同口径(报告尺);摘要段用 provider 实测输出 tokens 真值替换估算(2026-11-08)。
|
|
1515
|
+
// 只在记录真正落账(record 存在)时替换——record 缺失 = 产物没进视图,摘要不在请求里;
|
|
1516
|
+
// outputTokens 须 >0(0 是「未回传」的常见形态,防把 after 减到近 0)。
|
|
1517
|
+
let tokensAfter = rawRequestViewTokens(folded);
|
|
1518
|
+
if (record && outcome.summary && (outcome.summaryOutputTokens ?? 0) > 0) {
|
|
1519
|
+
tokensAfter =
|
|
1520
|
+
tokensAfter - raw.estimateText(outcome.summary) + outcome.summaryOutputTokens;
|
|
1521
|
+
}
|
|
1522
|
+
const afterViewTokens = this.measureRequestViewTokens(folded, agingPlan, estimator);
|
|
1523
|
+
// 增长门基数单位 raw(M-3):raw 存量 + 比较点当前 ratio 折算,漂移/重启不换尺
|
|
1524
|
+
const afterGateRaw = this.measureRequestViewTokens(folded, agingPlan, this.config.tokenEstimator);
|
|
978
1525
|
this.states.set(stateKey, {
|
|
979
|
-
...
|
|
980
|
-
previousSummary: summary,
|
|
1526
|
+
...this.states.get(stateKey),
|
|
981
1527
|
lastProactiveMessageCount: messages.length,
|
|
982
|
-
|
|
983
|
-
lastProactiveTokens:
|
|
984
|
-
|
|
985
|
-
|
|
1528
|
+
// 展示读数(校准)与增长门基数(raw)单位分工,见字段注释(M-3)
|
|
1529
|
+
lastProactiveTokens: afterViewTokens,
|
|
1530
|
+
lastCompactGateTokens: afterGateRaw,
|
|
1531
|
+
// 锚卫生:压缩改变请求形状,旧锚即刻失效
|
|
1532
|
+
lastUsageAnchor: undefined,
|
|
1533
|
+
lastProactiveLlmAt: outcome.summarizeAttempted ? Date.now() : this.states.get(stateKey)?.lastProactiveLlmAt,
|
|
986
1534
|
});
|
|
1535
|
+
// M1(评审 del-e1ee43ef):manual 终端事件(成功收口)——与 progress 配对收敛 store。
|
|
1536
|
+
// 显示槽快照一次构造、事件与 outcome 两处引用(同值同源同 asOf——F5 顺序无关的前提);
|
|
1537
|
+
// 仅真压缩(record!==undefined ⟺ compacted)才挂——零覆盖轮不动显示(评审 F-1)
|
|
1538
|
+
const afterSize = record !== undefined ? contextSizeSnapshot('compact-est', tokensAfter) : undefined;
|
|
1539
|
+
if (llmPrefix) {
|
|
1540
|
+
params.emit?.({
|
|
1541
|
+
type: 'context.compact.end',
|
|
1542
|
+
sessionId: params.sessionId,
|
|
1543
|
+
reason: 'manual',
|
|
1544
|
+
tokensBefore,
|
|
1545
|
+
tokensAfter,
|
|
1546
|
+
// contextSize 显示槽:压缩后估算随事件携带(as-of);REST 返回侧由 outcome 同源携带
|
|
1547
|
+
contextSize: afterSize,
|
|
1548
|
+
...(outcome.summarizeAttempted === true ? {} : { cached: true }),
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
987
1551
|
return {
|
|
988
|
-
|
|
989
|
-
|
|
1552
|
+
ok: true,
|
|
1553
|
+
summary: outcome.summary,
|
|
1554
|
+
compacted: record !== undefined,
|
|
990
1555
|
tokensBefore,
|
|
991
1556
|
tokensAfter,
|
|
992
|
-
|
|
1557
|
+
// 显示槽同源(与事件同一快照):runner 据此持久化 session.metadata.contextSize
|
|
1558
|
+
contextSize: afterSize,
|
|
1559
|
+
coveredMessageCount: record ? tailStart : this.derivedCoverEnd(folded),
|
|
1560
|
+
fresh: true,
|
|
1561
|
+
summarizeAttempted: outcome.summarizeAttempted,
|
|
1562
|
+
summaryOutputTokens: outcome.summaryOutputTokens,
|
|
1563
|
+
coverage: outcome.coverage,
|
|
1564
|
+
// 度量=装配严格同形:返还 fold 视图(与 tokensAfter 同源)
|
|
1565
|
+
view: folded.view,
|
|
993
1566
|
};
|
|
994
1567
|
}
|
|
995
1568
|
/**
|
|
996
|
-
*
|
|
1569
|
+
* 结构压缩入口(手动)— 可无 contextWindow
|
|
997
1570
|
*
|
|
998
1571
|
* @param input - sessionId + 全量 messages + 可选 summarize
|
|
999
1572
|
* @returns 压缩结果
|
|
1000
1573
|
*/
|
|
1001
1574
|
async compactStructural(input) {
|
|
1002
|
-
const estimator = this.config.tokenEstimator;
|
|
1003
1575
|
const stateKey = compactStateKey(input.sessionId, input.agentId);
|
|
1576
|
+
// R1(评审 #5):manual 压缩同样是决策路径(目标裁剪 / tokensBefore|After 读数),
|
|
1577
|
+
// 须与 assemble 同走校准口径;modelKey 复用会话最近一次 assemble 的分键。
|
|
1578
|
+
const calKey = this.calKey(stateKey, this.states.get(stateKey)?.lastModelKey ?? 'unknown');
|
|
1579
|
+
const estimator = this.sizeLedger.calibratedEstimator(calKey, this.config.tokenEstimator);
|
|
1580
|
+
// 整体透传 + 只覆盖需变换的三个字段:手抄 15 项参数表是「改一边忘另一边」的温床
|
|
1581
|
+
// (2026-11-13 前缀分叉就是这么来的)。新增字段自动随 input 流下,无需同步维护。
|
|
1004
1582
|
const result = await this.structuralCompact({
|
|
1005
|
-
|
|
1583
|
+
...input,
|
|
1584
|
+
...(input.agentId === undefined ? {} : { agentId: input.agentId }),
|
|
1006
1585
|
stateKey,
|
|
1007
|
-
|
|
1008
|
-
summarize: input.summarize,
|
|
1586
|
+
calKey,
|
|
1009
1587
|
estimator,
|
|
1010
|
-
compactTargetTokens: input.compactTargetTokens,
|
|
1011
|
-
llmPrefix: input.llmPrefix,
|
|
1012
|
-
systemPrompt: input.systemPrompt,
|
|
1013
|
-
tools: input.tools,
|
|
1014
|
-
taskSnapshot: input.taskSnapshot,
|
|
1015
|
-
contextWindow: input.contextWindow,
|
|
1016
|
-
maxOutputTokens: input.maxOutputTokens,
|
|
1017
|
-
emit: input.emit,
|
|
1018
1588
|
});
|
|
1019
1589
|
return {
|
|
1590
|
+
...result,
|
|
1020
1591
|
ok: true,
|
|
1021
|
-
|
|
1022
|
-
//
|
|
1592
|
+
// 失败语义如实透传(评审 F10):summarize_failed:* 不得被 'structural' 盖成成功。
|
|
1593
|
+
// 有意决定(P3 评审 F3):manual 对外词汇固定为 'structural'(历史 API 词汇,
|
|
1594
|
+
// UI/测试沿用);内层 reason(structure_only 等)不透传,避免对外词汇漂移
|
|
1023
1595
|
reason: result.reason ?? 'structural',
|
|
1024
|
-
tokensBefore: result.tokensBefore,
|
|
1025
|
-
tokensAfter: result.tokensAfter,
|
|
1026
|
-
summary: result.summary,
|
|
1027
|
-
coveredMessageCount: result.coveredMessageCount,
|
|
1028
1596
|
};
|
|
1029
1597
|
}
|
|
1598
|
+
/**
|
|
1599
|
+
* 冻结请求级配对槽(callId 同源配对的唯一写入口,2026-11-08)。
|
|
1600
|
+
*
|
|
1601
|
+
* 每次冻结携带单调 seq(pairSeq)作请求身份;覆盖未消费的旧槽即作废并告警——
|
|
1602
|
+
* 「上一请求 usage 未到就再次装配」正是错位对(0.085/0.258 家族)的嫌疑通道,
|
|
1603
|
+
* 静默覆盖会让旧估算去配陌生 usage。覆盖语义=只认最新发送(串行下与先到先得等价,
|
|
1604
|
+
* 非串行下更安全:旧请求可能已被重试/替代)。
|
|
1605
|
+
*
|
|
1606
|
+
* @param stateKey 会话状态键
|
|
1607
|
+
* @param rawEstimatedTokens 本请求「system+tools+视图」口径未校准估算
|
|
1608
|
+
* @param modelKey 校准分键
|
|
1609
|
+
*/
|
|
1610
|
+
/**
|
|
1611
|
+
* 校准分键(取舍③落地 2026-11-08):ratio/锚按 (session, modelKey) 复合键隔离——
|
|
1612
|
+
* 同 modelKey 的多 session 内容形态各异,混学 EMA 互相毒化(生产实证:跨形态比率
|
|
1613
|
+
* 0.185 vs 1.263 共存)。stateKey 即 session 维度(E4 compact key component)。
|
|
1614
|
+
*/
|
|
1615
|
+
calKey(stateKey, modelKey) {
|
|
1616
|
+
return `${stateKey}::${modelKey}`;
|
|
1617
|
+
}
|
|
1618
|
+
freezeUsagePair(stateKey, rawEstimatedTokens, modelKey) {
|
|
1619
|
+
const pairKey = this.calKey(stateKey, modelKey); // 配对/锚/EMA 同键隔离(取舍③);lastModelKey 保持裸键
|
|
1620
|
+
const state = this.states.get(stateKey) ?? {};
|
|
1621
|
+
if (state.pendingUsagePair) {
|
|
1622
|
+
console.warn(`[ctx-pair] overwrite unconsumed pair: state=${stateKey} seq=${state.pendingUsagePair.seq} rawEst=${state.pendingUsagePair.rawEstimatedTokens}(旧 pair 作废;随后到达的 usage 归属不确定,下一个 usage 同样丢样)`);
|
|
1623
|
+
}
|
|
1624
|
+
const seq = (state.pairSeq ?? 0) + 1;
|
|
1625
|
+
this.states.set(stateKey, {
|
|
1626
|
+
...state,
|
|
1627
|
+
// 覆盖丢样(评审 M8,2026-11-08):覆盖说明存在并发装配/未发送请求,随后到达的
|
|
1628
|
+
// usage 可能属于被覆盖的旧请求——与其错配进 EMA 成毒样本,不如丢掉这一个样本。
|
|
1629
|
+
// 只在**实际发生覆盖**时标 orphaned;下一轮正常冻结(无覆盖不标)恢复收集。
|
|
1630
|
+
pendingUsagePair: {
|
|
1631
|
+
rawEstimatedTokens,
|
|
1632
|
+
modelKey: pairKey,
|
|
1633
|
+
seq,
|
|
1634
|
+
...(state.pendingUsagePair ? { orphaned: true } : {}),
|
|
1635
|
+
},
|
|
1636
|
+
pairSeq: seq,
|
|
1637
|
+
lastModelKey: modelKey,
|
|
1638
|
+
});
|
|
1639
|
+
}
|
|
1030
1640
|
/**
|
|
1031
1641
|
* 每轮结束后更新状态
|
|
1032
1642
|
*
|
|
1033
|
-
* 使用 LLM 返回的真实 usage 校准 token
|
|
1034
|
-
*
|
|
1035
|
-
*
|
|
1036
|
-
*
|
|
1643
|
+
* 使用 LLM 返回的真实 usage 校准 token 估算:把(实测 prompt, 请求口径估算)样本
|
|
1644
|
+
* 送入 ContextSizeLedger(P1-a,ratio 按 model 分键)。
|
|
1645
|
+
*
|
|
1646
|
+
* 配对纪律(2026-11-07):分母只能是**本请求** assemble 冻结的 pendingUsagePair,
|
|
1647
|
+
* 消费即清;缺对丢样,绝不拿旧估算/旧实测凑对(错位对是毒 ratio 样本与毒锚的源头)。
|
|
1037
1648
|
*/
|
|
1038
|
-
async
|
|
1039
|
-
const { sessionId, agentId, usage
|
|
1040
|
-
if (!usage)
|
|
1041
|
-
return;
|
|
1649
|
+
async afterStep(params) {
|
|
1650
|
+
const { sessionId, agentId, usage } = params;
|
|
1042
1651
|
const stateKey = compactStateKey(sessionId, agentId);
|
|
1043
1652
|
const state = this.states.get(stateKey) ?? {};
|
|
1044
|
-
//
|
|
1653
|
+
// 消费配对槽(读后即清):无论本步有无 usage,槽只属于产生它的那个请求
|
|
1654
|
+
const pair = state.pendingUsagePair;
|
|
1655
|
+
if (pair) {
|
|
1656
|
+
const { pendingUsagePair: _consumed, ...rest } = state;
|
|
1657
|
+
this.states.set(stateKey, rest);
|
|
1658
|
+
}
|
|
1659
|
+
if (!usage)
|
|
1660
|
+
return;
|
|
1661
|
+
// 真实 prompt tokens(provider 报告优先,分项和兜底——与展示口径同源)
|
|
1045
1662
|
const promptActual = usage.inputReportedTokens ??
|
|
1046
1663
|
usage.inputUncachedTokens + usage.inputCachedTokens + usage.inputCacheWriteTokens;
|
|
1047
|
-
|
|
1048
|
-
//
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1664
|
+
// 缺对丢样:本请求没有冻结估算(或已被消费)时不喂样本——错配样本比少一个样本更糟。
|
|
1665
|
+
// 有 usage 却无对 = 外来 usage(摘要/子请求冒名)或漏写槽的信号,如实告警留破案线索。
|
|
1666
|
+
if (!pair || !(pair.rawEstimatedTokens > 0) || !(promptActual > 0)) {
|
|
1667
|
+
if (promptActual > 0) {
|
|
1668
|
+
console.warn(`[ctx-pair] usage without pair: session=${sessionId} actual=${promptActual}(丢样——疑似外来 usage 或漏写槽)`);
|
|
1669
|
+
}
|
|
1670
|
+
return;
|
|
1671
|
+
}
|
|
1672
|
+
// 覆盖丢样(评审 M8):pair 曾覆盖未消费的旧槽——本 usage 可能属于被覆盖的旧请求,
|
|
1673
|
+
// 归属不确定即丢样(宁缺不错配);下一轮正常冻结恢复收集
|
|
1674
|
+
if (pair.orphaned) {
|
|
1675
|
+
console.warn(`[ctx-pair] drop orphaned pair: session=${sessionId} seq=${pair.seq} actual=${promptActual}(覆盖后首个 usage 归属不确定,丢样)`);
|
|
1676
|
+
return;
|
|
1677
|
+
}
|
|
1678
|
+
// 全样本审计(2026-11-08):正常样本也打——错位家族(0.085/0.258)破案的盲区
|
|
1679
|
+
// 就是 accepted 样本零日志,出事时只能看到被门拒的残影。
|
|
1680
|
+
console.log(`[ctx-pair] session=${sessionId} seq=${pair.seq} model=${pair.modelKey} raw=${pair.rawEstimatedTokens} actual=${promptActual} ratio=${(promptActual / pair.rawEstimatedTokens).toFixed(3)}`);
|
|
1681
|
+
// P1-a:校准样本入台账——分母 = 本请求冻结的请求口径未校准估算,分子 = provider 实测。
|
|
1682
|
+
// ratio 按 model 分键跨 session 共享。
|
|
1683
|
+
const verdict = this.sizeLedger.observe(pair.modelKey, promptActual, pair.rawEstimatedTokens, {
|
|
1684
|
+
sessionId,
|
|
1685
|
+
});
|
|
1686
|
+
// 降级 B 锚(arch §10.16,评审 F3):{actual, raw} 同请求同源冻结成对写入——
|
|
1687
|
+
// 冻结门=「在门内入账」的对(accepted/clamped):clamped 是合法极端内容的锚
|
|
1688
|
+
// (F1 冷锚定语义:极端样本不进 EMA 决策,但同请求实测对可锚定外推);
|
|
1689
|
+
// 被拒/重锚样本(证据流敌对或错位对)不得冻结。配对槽已保证同请求同源,
|
|
1690
|
+
// 毒对从结构上进不来——消费侧不再按比率带拒锚(会误杀 ratio 远离 1 的合法内容)。
|
|
1691
|
+
if (verdict === 'accepted' || verdict === 'clamped') {
|
|
1692
|
+
this.states.set(stateKey, {
|
|
1693
|
+
...this.states.get(stateKey),
|
|
1694
|
+
lastUsageAnchor: {
|
|
1695
|
+
actual: promptActual,
|
|
1696
|
+
raw: pair.rawEstimatedTokens,
|
|
1697
|
+
modelKey: pair.modelKey,
|
|
1698
|
+
},
|
|
1699
|
+
});
|
|
1057
1700
|
}
|
|
1058
|
-
this.states.set(stateKey, state);
|
|
1059
1701
|
}
|
|
1060
1702
|
/**
|
|
1061
1703
|
* 存储消息(可选)
|
|
@@ -1063,30 +1705,141 @@ export class DefaultContextEngine {
|
|
|
1063
1705
|
* DefaultContextEngine 不需要单独存储消息,
|
|
1064
1706
|
* 因为消息历史由调用方管理。
|
|
1065
1707
|
*/
|
|
1708
|
+
/**
|
|
1709
|
+
* 校准状态快照(持久化用):per-key 样本计数 + 锚(含 stateKey 归属)。
|
|
1710
|
+
* 与 importCalibration 配对——重启后恢复,消除校准冷启动过渡(跑几轮才准)。
|
|
1711
|
+
*/
|
|
1712
|
+
exportCalibration() {
|
|
1713
|
+
const anchors = {};
|
|
1714
|
+
for (const [stateKey, st] of this.states) {
|
|
1715
|
+
if (st.lastUsageAnchor)
|
|
1716
|
+
anchors[stateKey] = st.lastUsageAnchor;
|
|
1717
|
+
}
|
|
1718
|
+
return { samples: this.sizeLedger.exportState(), anchors };
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* 校准状态恢复(restore 用):锚/样本计数回填——restore 后第一轮即 anchored/calibrated。
|
|
1722
|
+
*/
|
|
1723
|
+
importCalibration(data) {
|
|
1724
|
+
if (data?.samples)
|
|
1725
|
+
this.sizeLedger.importState(data.samples);
|
|
1726
|
+
for (const [stateKey, anchor] of Object.entries(data?.anchors ?? {})) {
|
|
1727
|
+
this.states.set(stateKey, { ...this.states.get(stateKey), lastUsageAnchor: anchor });
|
|
1728
|
+
}
|
|
1729
|
+
}
|
|
1066
1730
|
async ingest(_params) {
|
|
1067
1731
|
// No-op: 消息历史由调用方管理
|
|
1068
1732
|
}
|
|
1069
1733
|
/**
|
|
1070
|
-
*
|
|
1734
|
+
* 工具面指纹(按数组引用记忆,NIT-9)。
|
|
1735
|
+
*
|
|
1736
|
+
* sha1(JSON.stringify(tools)) 在每个 LLM step 的 assemble 出口都会跑一次;工具面
|
|
1737
|
+
* 在一步之内几乎总是不变的同一实例,按引用复用可把常态成本降到一次比较。
|
|
1738
|
+
*
|
|
1739
|
+
* @param tools - 工具面
|
|
1740
|
+
* @returns 哈希(16 位)
|
|
1741
|
+
*/
|
|
1742
|
+
toolsHashOf(tools) {
|
|
1743
|
+
const cached = this.lastToolsHash;
|
|
1744
|
+
if (cached && cached.ref === tools)
|
|
1745
|
+
return cached.hash;
|
|
1746
|
+
const hash = fingerprintRequestView({ messages: [], tools }).toolsHash;
|
|
1747
|
+
this.lastToolsHash = { ref: tools, hash };
|
|
1748
|
+
return hash;
|
|
1749
|
+
}
|
|
1750
|
+
/**
|
|
1751
|
+
* 摘要前缀工具面校验(可观测护栏,2026-11-12)。
|
|
1752
|
+
*
|
|
1753
|
+
* tools 在会话生命周期内近乎不变;摘要请求的 toolsHash 与最近一次会话请求不同,
|
|
1754
|
+
* 就说明前缀从第一字节断开,缓存必然 miss。这里只告警不断言——摘要前缀与「当前」
|
|
1755
|
+
* 请求本就可能不同(折叠阶梯是逐轮追加的),但工具面不该变。
|
|
1756
|
+
*
|
|
1757
|
+
* @param stateKey - 引擎状态键(sessionId + agentId,与 compactStateKey 同源)
|
|
1758
|
+
* @param tools - 摘要请求即将使用的工具面
|
|
1759
|
+
* @returns 无(异常情况打印告警)
|
|
1760
|
+
*/
|
|
1761
|
+
/**
|
|
1762
|
+
* 记录「最近一次会话请求」的前缀基线(唯一实现)
|
|
1763
|
+
*
|
|
1764
|
+
* 三处调用点(两个 assemble 出口 + buildRequestView 的默认分支)都经此,
|
|
1765
|
+
* 禁止各自手写 set 体:2026-11-13 一轮里同一段 set 体曾出现三份,
|
|
1766
|
+
* 正是「改一边忘另一边」的形状(CON E8)。
|
|
1767
|
+
*
|
|
1768
|
+
* 摘要路径**不得**调用:它会把自己的指纹写成基线,护栏随即永远一致。
|
|
1071
1769
|
*
|
|
1072
|
-
*
|
|
1770
|
+
* @param stateKey - 引擎状态键(与 compactStateKey 同源)
|
|
1771
|
+
* @param view - 会话请求前缀(messages 必须已是老化后的 LLM 形态)
|
|
1073
1772
|
*/
|
|
1773
|
+
recordSessionRequest(stateKey, view) {
|
|
1774
|
+
this.lastRequestPrefix.set(stateKey, {
|
|
1775
|
+
fingerprint: fingerprintRequestView(view),
|
|
1776
|
+
toolCount: view.tools.length,
|
|
1777
|
+
messageCount: view.messages.length,
|
|
1778
|
+
});
|
|
1779
|
+
}
|
|
1780
|
+
checkSummaryToolFace(stateKey, tools) {
|
|
1781
|
+
const last = this.lastRequestPrefix.get(stateKey);
|
|
1782
|
+
if (!last)
|
|
1783
|
+
return;
|
|
1784
|
+
const hash = this.toolsHashOf(tools);
|
|
1785
|
+
if (hash === last.fingerprint.toolsHash)
|
|
1786
|
+
return;
|
|
1787
|
+
console.warn(`[ctx] summary tool face differs from last session request ` +
|
|
1788
|
+
`(key=${stateKey}, summary=${tools.length} tools, session=${last.toolCount}) — ` +
|
|
1789
|
+
`prefix cache will miss; check tool-face resolution`);
|
|
1790
|
+
}
|
|
1791
|
+
checkSummaryPrefix(stateKey, view) {
|
|
1792
|
+
const last = this.lastRequestPrefix.get(stateKey);
|
|
1793
|
+
if (!last)
|
|
1794
|
+
return; // 无基线(如 manual 压缩发生在任何 assemble 之前)——不虚构告警
|
|
1795
|
+
const mine = fingerprintRequestView(view);
|
|
1796
|
+
const diffs = diffPrefixFingerprint(last.fingerprint, mine);
|
|
1797
|
+
if (diffs.length === 0)
|
|
1798
|
+
return;
|
|
1799
|
+
// 差异项具体到 tools / system / messages:只报「不同」无法定位是哪条路径漏了参数。
|
|
1800
|
+
// messages 不同最常见于老化形态分叉(摘要用默认指针而会话用 per-tool ager)。
|
|
1801
|
+
const detail = diffs.includes('messages') && !last.fingerprint.prefixCount
|
|
1802
|
+
? ''
|
|
1803
|
+
: `, summary=${mine.prefixCount} msgs/${mine.toolsHash.slice(0, 8)}, ` +
|
|
1804
|
+
`session=${last.messageCount} msgs/${last.fingerprint.toolsHash.slice(0, 8)}`;
|
|
1805
|
+
console.warn(`[ctx] summary prefix differs from last session request ` +
|
|
1806
|
+
`(key=${stateKey}, differs=${diffs.join('+')}${detail}) — ` +
|
|
1807
|
+
`prefix cache will miss; check tool-face resolution AND aging parity`);
|
|
1808
|
+
}
|
|
1074
1809
|
/**
|
|
1075
|
-
*
|
|
1810
|
+
* 构建**请求视图**:前缀的 messages 与 tools 同批产出(缓存前缀的原子单位)。
|
|
1076
1811
|
*
|
|
1077
|
-
*
|
|
1078
|
-
*
|
|
1812
|
+
* 会话请求与摘要请求(proactive / manual)都经此产出前缀。这样"改装配逻辑忘了一边"
|
|
1813
|
+
* 在结构上不可能发生:没有哪条路径能只拿到 messages 或只拿到 tools。
|
|
1814
|
+
*
|
|
1815
|
+
* @param input.messages - 已折叠的视图(领域消息)
|
|
1816
|
+
* @param input.systemPrompt - 会话托管 system 文本(逐字取自上一轮请求,不重建)
|
|
1817
|
+
* @param input.tools - 已解析的工具面(调用方与 run 同源解析,禁自行拼装)
|
|
1818
|
+
* @param input.aging - 老化计划(上游决策;此处不重新推导,避免静默推进)
|
|
1819
|
+
* @returns 请求视图
|
|
1079
1820
|
*/
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1821
|
+
buildRequestView(input) {
|
|
1822
|
+
// 只产出前缀两半;尾部摘要指令由 compact 引擎独占追加(单一追加点,
|
|
1823
|
+
// 否则重压/旁路会出现两条尾部指令——评审 del-b5ffc13a NIT-8)。
|
|
1824
|
+
const view = {
|
|
1825
|
+
messages: this.buildLlmMessages(input.messages, input.systemPrompt, input.aging),
|
|
1826
|
+
tools: input.tools,
|
|
1827
|
+
};
|
|
1828
|
+
if (input.recordBaseline !== false && input.stateKey) {
|
|
1829
|
+
this.recordSessionRequest(input.stateKey, view);
|
|
1084
1830
|
}
|
|
1085
|
-
|
|
1086
|
-
const clampedRatio = Math.max(0.5, Math.min(2.0, state.calibrationRatio));
|
|
1087
|
-
return Math.ceil(rawEstimated * clampedRatio);
|
|
1831
|
+
return view;
|
|
1088
1832
|
}
|
|
1089
|
-
|
|
1833
|
+
/**
|
|
1834
|
+
* 构建 LLM 消息格式(前缀的 messages 半区)
|
|
1835
|
+
*
|
|
1836
|
+
* 注意:这里刻意**不接受 tools**。缓存前缀的原子产物是 `RequestView`
|
|
1837
|
+
* (messages + tools 同批产出,见 core `RequestView`);一个收了 tools 却不用的参数
|
|
1838
|
+
* 会让读者误以为「视图=system+messages+tools」,2026-11-12 的前缀缓存事故正是这样
|
|
1839
|
+
* 发生的(摘要请求看着"同形",实际整包 tools 缺失)。
|
|
1840
|
+
* 需要成对产出时请用 {@link buildRequestView}。
|
|
1841
|
+
*/
|
|
1842
|
+
buildLlmMessages(messages, systemPrompt, aging) {
|
|
1090
1843
|
const result = [];
|
|
1091
1844
|
// 系统提示词(唯一权威来源)。
|
|
1092
1845
|
// Loop 会把同一内容以 metadata.source==='systemPrompt' unshift 进 messages,
|
|
@@ -1106,7 +1859,14 @@ export class DefaultContextEngine {
|
|
|
1106
1859
|
if (msg.metadata?.source === 'runStopped') {
|
|
1107
1860
|
continue;
|
|
1108
1861
|
}
|
|
1109
|
-
|
|
1862
|
+
// 反向围栏(B0):非托管 system 一律降级为 engineHint 载体(user 形态,内容原样),
|
|
1863
|
+
// 不丢弃(不静默失忆)也绝不进 provider system 字段——system 槽位只归托管
|
|
1864
|
+
// systemPrompt 一条;外来 system 进字段会让前缀从第 0 字节失效
|
|
1865
|
+
// (context-size-accounting.md §1),且 provider 适配层按「system ≤ 1」断言。
|
|
1866
|
+
const llmMsg = msg.role === 'system'
|
|
1867
|
+
? { ...msg, role: 'user', metadata: { ...msg.metadata, source: 'engineHint' } }
|
|
1868
|
+
: msg;
|
|
1869
|
+
result.push(...this.convertMessage(llmMsg, aging));
|
|
1110
1870
|
}
|
|
1111
1871
|
return result;
|
|
1112
1872
|
}
|
|
@@ -1132,7 +1892,7 @@ export class DefaultContextEngine {
|
|
|
1132
1892
|
for (const tr of msg.toolResults ?? []) {
|
|
1133
1893
|
// F1-B2:老化条目降级为结构化指针(错误文案保真在指针内;tool_call 配对不动)
|
|
1134
1894
|
const content = aging && aging.agedIds.has(tr.toolCallId)
|
|
1135
|
-
?
|
|
1895
|
+
? ageOneToolResult(tr, aging.argsById.get(tr.toolCallId), aging, this.config.historyAging)
|
|
1136
1896
|
: toolResultBody(tr);
|
|
1137
1897
|
result.push({
|
|
1138
1898
|
role: 'tool',
|
|
@@ -1149,7 +1909,7 @@ export class DefaultContextEngine {
|
|
|
1149
1909
|
role: 'assistant',
|
|
1150
1910
|
content: getTextContent(msg.content) || null,
|
|
1151
1911
|
tool_calls: msg.toolCalls.map(tc => {
|
|
1152
|
-
const args = aging && this.config.historyAging
|
|
1912
|
+
const args = aging && shouldAgeArgs(tc.id, aging, this.config.historyAging)
|
|
1153
1913
|
? agedToolArgDigest(tc.name, tc.arguments, this.config.historyAging)
|
|
1154
1914
|
: tc.arguments;
|
|
1155
1915
|
return {
|
|
@@ -1226,6 +1986,26 @@ export class DefaultContextEngine {
|
|
|
1226
1986
|
content: msg.content,
|
|
1227
1987
|
});
|
|
1228
1988
|
}
|
|
1989
|
+
// schedule notify 便签:借 user 通道落库的机器留言,不得在 LLM 视图冒充
|
|
1990
|
+
// 人话——视图级包 <schedule_notice> 信封(宪法 Confusables;持久层/UI 保
|
|
1991
|
+
// 原文。信封是消息的纯函数,回放逐字节稳定,不破 E8 前缀缓存)。
|
|
1992
|
+
if (isScheduleNotice(msg)) {
|
|
1993
|
+
for (const lm of result) {
|
|
1994
|
+
// 转义闭标签防伪造突破信封;空便签不包(不把空消息变成非空)
|
|
1995
|
+
const wrapText = (text) => text.trim()
|
|
1996
|
+
? `<schedule_notice>${text.replaceAll('</schedule_notice', '</schedule_notice')}</schedule_notice>`
|
|
1997
|
+
: text;
|
|
1998
|
+
if (typeof lm.content === 'string') {
|
|
1999
|
+
lm.content = wrapText(lm.content);
|
|
2000
|
+
}
|
|
2001
|
+
else if (Array.isArray(lm.content)) {
|
|
2002
|
+
const blocks = lm.content;
|
|
2003
|
+
lm.content = blocks.map((block) => block.type === 'text' && typeof block.text === 'string'
|
|
2004
|
+
? { ...block, text: wrapText(block.text) }
|
|
2005
|
+
: block);
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
1229
2009
|
return result;
|
|
1230
2010
|
}
|
|
1231
2011
|
/**
|
|
@@ -1267,17 +2047,7 @@ export class DefaultContextEngine {
|
|
|
1267
2047
|
* @returns 是否 turn 内
|
|
1268
2048
|
*/
|
|
1269
2049
|
export function isMidTurn(messages) {
|
|
1270
|
-
|
|
1271
|
-
for (let i = 0; i < messages.length; i++) {
|
|
1272
|
-
const msg = messages[i];
|
|
1273
|
-
// 「真实 user」收紧:agentId 归因的 user(appendCommandDiscourse)不重置边界,
|
|
1274
|
-
// 载体过滤走唯一谓词 isTurnAnchor
|
|
1275
|
-
if (msg.agentId)
|
|
1276
|
-
continue;
|
|
1277
|
-
if (!isTurnAnchor(msg))
|
|
1278
|
-
continue;
|
|
1279
|
-
lastGenuineUserIdx = i;
|
|
1280
|
-
}
|
|
2050
|
+
const lastGenuineUserIdx = lastGenuineUserIndex(messages);
|
|
1281
2051
|
if (lastGenuineUserIdx < 0)
|
|
1282
2052
|
return false;
|
|
1283
2053
|
for (let i = lastGenuineUserIdx + 1; i < messages.length; i++) {
|
|
@@ -1290,3 +2060,24 @@ export function isMidTurn(messages) {
|
|
|
1290
2060
|
}
|
|
1291
2061
|
return false;
|
|
1292
2062
|
}
|
|
2063
|
+
/**
|
|
2064
|
+
* 最后一条「真实 user 消息」的下标(turn 起点标识)。
|
|
2065
|
+
*
|
|
2066
|
+
* 「真实 user」= 无 agentId 归因,且非引擎载体(含 `engineHint` reliability 注入)。
|
|
2067
|
+
* 历史 append-only,该下标在同 turn 内稳定,可作 turn key(overflow 每 turn 限一次用)。
|
|
2068
|
+
*
|
|
2069
|
+
* @param messages - 全量消息
|
|
2070
|
+
* @returns 下标;无真实 user 时返回 -1
|
|
2071
|
+
*/
|
|
2072
|
+
export function lastGenuineUserIndex(messages) {
|
|
2073
|
+
let lastGenuineUserIdx = -1;
|
|
2074
|
+
for (let i = 0; i < messages.length; i++) {
|
|
2075
|
+
const msg = messages[i];
|
|
2076
|
+
// 边界锚点走唯一谓词 isTurnBoundaryAnchor:agentId 归因的 user
|
|
2077
|
+
// (appendCommandDiscourse)与引擎载体都不重置边界
|
|
2078
|
+
if (!isTurnBoundaryAnchor(msg))
|
|
2079
|
+
continue;
|
|
2080
|
+
lastGenuineUserIdx = i;
|
|
2081
|
+
}
|
|
2082
|
+
return lastGenuineUserIdx;
|
|
2083
|
+
}
|