@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
|
@@ -8,8 +8,12 @@
|
|
|
8
8
|
import { getTextContent } from '@octopi-agent/core/types.js';
|
|
9
9
|
import { isCannedRefusalText } from '@octopi-agent/core/loop/canned-refusal.js';
|
|
10
10
|
import { HeuristicTokenEstimator } from '../../token-estimator.js';
|
|
11
|
+
import { toolResultBody } from '../../tool-result-body.js';
|
|
11
12
|
import { buildPolicyRegistry } from '../summary/resolver.js';
|
|
12
|
-
import {
|
|
13
|
+
import { buildHandoverPrompt, buildHandoverUserContent, clampSummaryProviderCap, } from './handover.js';
|
|
14
|
+
import { asSummaryMessage, MECHANICAL_NOTICE_STRUCTURAL } from './messages.js';
|
|
15
|
+
import { alignViewSplit } from './view-split.js';
|
|
16
|
+
import { mechanicalDegrade } from './mechanical.js';
|
|
13
17
|
import { estimateLLMMessages } from '../../token-estimator.js';
|
|
14
18
|
function defaultEstimator() {
|
|
15
19
|
const e = new HeuristicTokenEstimator();
|
|
@@ -26,59 +30,109 @@ const STRICT_RETRY_SUFFIX = '\n\nIMPORTANT: your previous attempt replayed input
|
|
|
26
30
|
* 角色行用行首语义(防正文误伤);工具调用语法 anywhere 判定——指令已不含这些
|
|
27
31
|
* 字面量(F15),输出里出现即高概率是续写污染。 */
|
|
28
32
|
const TRANSCRIPT_SHAPE = /<function[=_]|<\/?tool_call|<invoke[ \t]|<\/?tool_use|<parameter[ >]|antml:|Tool Calls:|\[(ASSISTANT|USER|TOOL|SYSTEM)\]|^\s*(Assistant|Human|User|System):/im;
|
|
29
|
-
/**
|
|
33
|
+
/** 单行最短长度:短于此的片段(标题、表头、空行)重复出现不构成复读证据 */
|
|
34
|
+
const ECHO_MIN_LINE_CHARS = 40;
|
|
35
|
+
/** 回声占比阈值:命中的输入字符占输出 ≥ 该比例才判复读。
|
|
36
|
+
*
|
|
37
|
+
* 修正前是「命中 ≥3 行」的计数分支,与四段式模板的「④ Evidence pointers」要求
|
|
38
|
+
* 直接冲突——模板要求写文件路径/commit id/测试名,而这些行几乎必然 ≥40 字符且
|
|
39
|
+
* 逐字出现在输入里。三条合法证据就触发整包重发(2026-11-13 实测:348k token、
|
|
40
|
+
* +59s)。判据改为纯比例:复读的本质是「输出基本就是输入的回声」,与命中几条
|
|
41
|
+
* 无关;引用三条短路径(占比极低)不应与复读整段正文等价。 */
|
|
42
|
+
const ECHO_RATIO_THRESHOLD = 0.6;
|
|
43
|
+
/**
|
|
44
|
+
* 判定 `text` 是否构成对 `sourceText` 的逐字复读。
|
|
45
|
+
*
|
|
46
|
+
* 判据是**长度占比**而非行数:复读的失效模式是「输出基本等于输入片段」,而不是
|
|
47
|
+
* 「引用了若干条证据」。四条以上互不相同的证据行如果只占输出很小比例,仍是合法
|
|
48
|
+
* 交接书(模板明确允许引用证据行)。
|
|
49
|
+
*
|
|
50
|
+
* @param text - 模型输出
|
|
51
|
+
* @param sourceText - 回声比对源
|
|
52
|
+
* @returns 命中量与判定
|
|
53
|
+
*/
|
|
54
|
+
export function evaluateEcho(text, sourceText) {
|
|
55
|
+
let hits = 0;
|
|
56
|
+
let hitChars = 0;
|
|
57
|
+
// 逐行比对:只统计 ≥ ECHO_MIN_LINE_CHARS 的行,且同一行只计一次(split 会产出重复行)
|
|
58
|
+
for (const line of new Set(sourceText.split('\n').map((l) => l.trim()))) {
|
|
59
|
+
if (line.length < ECHO_MIN_LINE_CHARS)
|
|
60
|
+
continue;
|
|
61
|
+
if (!text.includes(line))
|
|
62
|
+
continue;
|
|
63
|
+
hits += 1;
|
|
64
|
+
hitChars += line.length;
|
|
65
|
+
}
|
|
66
|
+
const textChars = text.length;
|
|
67
|
+
const contaminated = hitChars >= ECHO_MIN_LINE_CHARS && hitChars >= textChars * ECHO_RATIO_THRESHOLD;
|
|
68
|
+
return { hits, hitChars, textChars, contaminated };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 输出闸门:判定摘要输出是否污染(回声/对话体续写)。
|
|
30
72
|
*
|
|
31
73
|
* 失效模式(2026-10-28 实测):模型把对话样本当语料续写,产出「assistant 半句 + 逐字
|
|
32
|
-
* tool-call
|
|
33
|
-
*
|
|
74
|
+
* tool-call」的原文复读,使折叠掉的中段信息全丢。
|
|
75
|
+
*
|
|
76
|
+
* 三条规则,任一命中即污染:
|
|
77
|
+
* 1. `canned_refusal` —— 正文恰为内容过滤罐头拒答(整段精确匹配,长输出不可能命中)。
|
|
78
|
+
* 2. `transcript_shape` —— 出现对话体/工具调用语法字面量(指令已不含这些,出现即续写)。
|
|
79
|
+
* 3. `verbatim_echo` —— **按长度占比**判复读(见 {@link evaluateEcho})。
|
|
34
80
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
81
|
+
* 规则三的判据在 2026-11-13 由「命中 ≥3 行」改为「命中字符占比 ≥60%」:旧计数分支
|
|
82
|
+
* 与四段式模板的证据行要求结构性冲突——模板要求写文件路径/commit id/测试名,这些行
|
|
83
|
+
* 几乎必然 ≥40 字符且逐字出现在输入里,三条合法证据即触发整包重发。
|
|
37
84
|
*
|
|
38
85
|
* @param text - 模型输出
|
|
39
86
|
* @param sourceText - 「模型实际看到的会话文本」投影(回声比对源;不得含指令本身——
|
|
40
87
|
* 指令的四段式标题会合法出现在输出里)
|
|
41
|
-
* @returns
|
|
88
|
+
* @returns 判定结果(含回声明细,供可观测上报)
|
|
42
89
|
*/
|
|
43
|
-
export function
|
|
90
|
+
export function inspectSummaryOutput(text, sourceText) {
|
|
44
91
|
// 内容过滤罐头拒答:网关 200 内联拒答(正文被替换成固定英文罐头)不是摘要,
|
|
45
92
|
// 一律判污染——交由既有的「一次加严重试 → 二次抛错降级」链路处理
|
|
46
93
|
if (isCannedRefusalText(text)) {
|
|
47
|
-
return 'canned_refusal';
|
|
94
|
+
return { reason: 'canned_refusal' };
|
|
48
95
|
}
|
|
49
96
|
if (TRANSCRIPT_SHAPE.test(text)) {
|
|
50
|
-
return 'transcript_shape';
|
|
97
|
+
return { reason: 'transcript_shape' };
|
|
51
98
|
}
|
|
52
|
-
if (sourceText) {
|
|
53
|
-
|
|
54
|
-
let hitChars = 0;
|
|
55
|
-
for (const line of sourceText.split('\n')) {
|
|
56
|
-
const trimmed = line.trim();
|
|
57
|
-
if (trimmed.length >= 40 && text.includes(trimmed)) {
|
|
58
|
-
hits += 1;
|
|
59
|
-
hitChars += trimmed.length;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (hits >= 3 || (hitChars >= 40 && hitChars >= text.length * 0.6)) {
|
|
63
|
-
return 'verbatim_echo';
|
|
64
|
-
}
|
|
99
|
+
if (!sourceText) {
|
|
100
|
+
return { reason: null };
|
|
65
101
|
}
|
|
66
|
-
|
|
102
|
+
const echo = evaluateEcho(text, sourceText);
|
|
103
|
+
return echo.contaminated ? { reason: 'verbatim_echo', echo } : { reason: null, echo };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 输出闸门(字串版):保留旧签名供既有调用方与测试使用。
|
|
107
|
+
*
|
|
108
|
+
* @param text - 模型输出
|
|
109
|
+
* @param sourceText - 回声比对源
|
|
110
|
+
* @returns 命中原因;干净返回 null
|
|
111
|
+
*/
|
|
112
|
+
export function detectSummaryContamination(text, sourceText) {
|
|
113
|
+
return inspectSummaryOutput(text, sourceText).reason;
|
|
67
114
|
}
|
|
68
115
|
/** 带输出闸门的摘要执行:首次污染给一次加严重试,二次污染抛错交 onSummarizeFail 降级。
|
|
116
|
+
*
|
|
117
|
+
* 可观测(2026-11-13):每次闸门判定都通过 `onVerdict` 上报——事故里重发整包
|
|
118
|
+
* 348k token 请求却零事件,UI 只能显示「已等待 Ns」,无法区分「在跑」与「被判污染重试」。
|
|
69
119
|
*
|
|
70
120
|
* @param invoke - 摘要调用(strict=true 时需附加加严指令)
|
|
71
121
|
* @param sourceText - 回声比对源(喂给模型的输入文本)
|
|
122
|
+
* @param onVerdict - 判定回调(尝试序号 + 结论 + 明细)
|
|
72
123
|
* @returns 干净摘要文本
|
|
73
124
|
* @throws provider 错误,或 `contaminated_output:*`(二次污染)
|
|
74
125
|
*/
|
|
75
|
-
async function invokeGuardedSummarize(invoke, sourceText) {
|
|
126
|
+
async function invokeGuardedSummarize(invoke, sourceText, onVerdict) {
|
|
76
127
|
let text = await invoke(false);
|
|
77
|
-
|
|
128
|
+
const first = inspectSummaryOutput(text, sourceText);
|
|
129
|
+
onVerdict?.(1, first);
|
|
130
|
+
if (first.reason) {
|
|
78
131
|
text = await invoke(true);
|
|
79
|
-
const again =
|
|
80
|
-
|
|
81
|
-
|
|
132
|
+
const again = inspectSummaryOutput(text, sourceText);
|
|
133
|
+
onVerdict?.(2, again);
|
|
134
|
+
if (again.reason) {
|
|
135
|
+
throw new Error(`contaminated_output:${again.reason}`);
|
|
82
136
|
}
|
|
83
137
|
}
|
|
84
138
|
return text;
|
|
@@ -115,14 +169,7 @@ function formatMessagesAsText(messages) {
|
|
|
115
169
|
}
|
|
116
170
|
else if (msg.role === 'tool' && msg.toolResults?.length) {
|
|
117
171
|
const results = msg.toolResults
|
|
118
|
-
.map((tr) => {
|
|
119
|
-
const body = tr.error !== undefined
|
|
120
|
-
? JSON.stringify({ error: tr.error })
|
|
121
|
-
: typeof tr.result === 'string'
|
|
122
|
-
? tr.result
|
|
123
|
-
: JSON.stringify(tr.result ?? null);
|
|
124
|
-
return ` - ${tr.name}: ${body}`;
|
|
125
|
-
})
|
|
172
|
+
.map((tr) => ` - ${tr.name}: ${toolResultBody(tr)}`)
|
|
126
173
|
.join('\n');
|
|
127
174
|
parts.push(`[${role}]\nTool Results:\n${results}`);
|
|
128
175
|
}
|
|
@@ -132,24 +179,26 @@ function formatMessagesAsText(messages) {
|
|
|
132
179
|
}
|
|
133
180
|
return parts.join('\n\n---\n\n');
|
|
134
181
|
}
|
|
135
|
-
function asSummaryMessage(summary) {
|
|
136
|
-
const body = summary.includes('[Conversation Summary]')
|
|
137
|
-
? summary
|
|
138
|
-
: `[Conversation Summary]\n\n${summary}`;
|
|
139
|
-
return {
|
|
140
|
-
role: 'user',
|
|
141
|
-
content: body,
|
|
142
|
-
timestamp: Date.now(),
|
|
143
|
-
metadata: { source: 'contextSummary' },
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
182
|
/**
|
|
147
183
|
* 创建 CompactEngine
|
|
148
184
|
*
|
|
149
|
-
* @param options - 缺省 protect /
|
|
185
|
+
* @param options - 缺省 protect / estimator
|
|
150
186
|
* @returns CompactEngine
|
|
151
187
|
*/
|
|
188
|
+
/**
|
|
189
|
+
* v0.98.10 移除 summaryPort:残留传参必须 fail-loud——与 P0「配置了不生效须出声」同一原则,
|
|
190
|
+
* 禁止静默忽略后走 summarizeFn/截断降级(行为切换必须显式可见)。
|
|
191
|
+
*
|
|
192
|
+
* @param o - 待检配置对象(CompactOptions / CreateCompactEngineOptions)
|
|
193
|
+
* @param where - 报错定位(入口名)
|
|
194
|
+
*/
|
|
195
|
+
function rejectRemovedSummaryPort(o, where) {
|
|
196
|
+
if (o !== null && typeof o === 'object' && 'summaryPort' in o) {
|
|
197
|
+
throw new Error(`not_implemented: summaryPort was removed in v0.98.10 (${where}) — inject summarizeFn instead (see CHANGELOG breaking note)`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
152
200
|
export function createCompactEngine(options) {
|
|
201
|
+
rejectRemovedSummaryPort(options, 'createCompactEngine');
|
|
153
202
|
const defaults = {
|
|
154
203
|
// 能力层缺省与 DefaultContextEngine protectFirstN 同源(5,2026-10-28 拍板交接快照头 5)
|
|
155
204
|
protectHead: options?.defaultProtectHead ?? 5,
|
|
@@ -157,9 +206,9 @@ export function createCompactEngine(options) {
|
|
|
157
206
|
targetTokens: options?.defaultTargetTokens,
|
|
158
207
|
mode: (options?.defaultMode ?? 'structure_only'),
|
|
159
208
|
};
|
|
160
|
-
async function summarizeMiddle(middleText,
|
|
161
|
-
const port = opts.summaryPort ?? options?.summaryPort;
|
|
209
|
+
async function summarizeMiddle(middleText, opts, maxTokens) {
|
|
162
210
|
const summarizeFn = opts.summarizeFn;
|
|
211
|
+
let summaryOutputTokens;
|
|
163
212
|
const registry = buildPolicyRegistry();
|
|
164
213
|
let policy = registry['conversation_default'];
|
|
165
214
|
if (typeof opts.summarizePolicy === 'string' && registry[opts.summarizePolicy]) {
|
|
@@ -171,42 +220,11 @@ export function createCompactEngine(options) {
|
|
|
171
220
|
// 中段文本化为空(消息全无正文无工具):不发空摘要请求——空 user / 空 New content
|
|
172
221
|
// 过不了严格网关,且会让模型凭空产出;按摘要失败降级(onSummarizeFail 接管)
|
|
173
222
|
if (!middleText.trim()) {
|
|
174
|
-
return { error: 'empty_middle' };
|
|
175
|
-
}
|
|
176
|
-
if (port) {
|
|
177
|
-
const unit = {
|
|
178
|
-
text: middleText,
|
|
179
|
-
structured: middleMessages,
|
|
180
|
-
channel: 'pipeline',
|
|
181
|
-
source: {},
|
|
182
|
-
kind: 'conversation',
|
|
183
|
-
policy: opts.summarizePolicy,
|
|
184
|
-
task: HANDOVER_SUMMARY_TASK,
|
|
185
|
-
};
|
|
186
|
-
try {
|
|
187
|
-
// 输出闸门与 summarizeFn 路径对齐:污染给一次加严重试,仍污染按失败降级(不静默存坏摘要)
|
|
188
|
-
let r = await port.extract(unit, {
|
|
189
|
-
signal: opts.signal,
|
|
190
|
-
previousSummary: opts.previousSummary,
|
|
191
|
-
});
|
|
192
|
-
if (detectSummaryContamination(r.text, middleText)) {
|
|
193
|
-
r = await port.extract({ ...unit, task: `${HANDOVER_SUMMARY_TASK}${STRICT_RETRY_SUFFIX}` }, { signal: opts.signal, previousSummary: opts.previousSummary });
|
|
194
|
-
const again = detectSummaryContamination(r.text, middleText);
|
|
195
|
-
if (again) {
|
|
196
|
-
return { error: `contaminated_output:${again}` };
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return { summary: r.text, coverage: r.coverage };
|
|
200
|
-
}
|
|
201
|
-
catch (err) {
|
|
202
|
-
// 只吞 port.extract 的抛错(摘要器契约外失败:provider 报错/超时)——转 error
|
|
203
|
-
// 通道交给 onSummarizeFail 降级;try 内无其他可抛代码,无其他路径可至此
|
|
204
|
-
return { error: err instanceof Error ? err.message : String(err) };
|
|
205
|
-
}
|
|
223
|
+
return { error: 'empty_middle', summarizeAttempted: false };
|
|
206
224
|
}
|
|
207
225
|
if (summarizeFn) {
|
|
208
|
-
// 交接式契约单源(F8 模板统一):模板/指令/合并语义全走 handover.ts
|
|
209
|
-
//
|
|
226
|
+
// 交接式契约单源(F8 模板统一):模板/指令/合并语义全走 handover.ts,
|
|
227
|
+
// previousSummary 只有一种格式在迭代合并(P2 后 manual/proactive/overflow 唯一执行管道)
|
|
210
228
|
const prompt = buildHandoverPrompt({
|
|
211
229
|
maxTokens,
|
|
212
230
|
summarizePolicy: policy,
|
|
@@ -214,37 +232,50 @@ export function createCompactEngine(options) {
|
|
|
214
232
|
foldRange: opts.foldRange,
|
|
215
233
|
taskSnapshot: opts.taskSnapshot,
|
|
216
234
|
});
|
|
217
|
-
// 缓存优先:
|
|
235
|
+
// 缓存优先:requestView(会话同形 tools+system+history)+ 尾部 user 指令。
|
|
218
236
|
// 禁止把指令塞进 system 或重排历史——否则打穿前缀缓存。
|
|
219
237
|
const buildPrefixed = () => [
|
|
220
|
-
...opts.
|
|
238
|
+
...opts.requestView?.messages,
|
|
221
239
|
{ role: 'user', content: prompt.tailUser },
|
|
222
240
|
];
|
|
223
|
-
// 无
|
|
241
|
+
// 无 requestView 回退「policy system + 文本化中段」(旁路/旧调用):
|
|
224
242
|
// tool_call-only assistant / 缺 tool_call_id 的 tool 消息会被严格网关 400。
|
|
225
243
|
const buildTextify = () => [
|
|
226
244
|
{ role: 'system', content: prompt.instruction },
|
|
227
245
|
{ role: 'user', content: buildHandoverUserContent(opts, middleText) },
|
|
228
246
|
];
|
|
229
|
-
// 回声比对源 = 模型实际看到的会话文本投影(评审 F2
|
|
247
|
+
// 回声比对源 = 模型实际看到的会话文本投影(评审 F2):前缀分支含 head/middle/tail
|
|
230
248
|
// 全量历史,只比 middleText 会漏掉 head/tail 区回声;指令本身不得进比对源(四段式标题
|
|
231
249
|
// 会合法出现在输出里)。
|
|
232
|
-
const prefixEchoSource = () => (opts.
|
|
233
|
-
let usePrefix = Boolean(opts.
|
|
250
|
+
const prefixEchoSource = () => (opts.requestView?.messages ?? []).map((m) => String(m.content ?? '')).join('\n');
|
|
251
|
+
let usePrefix = Boolean(opts.requestView?.messages?.length);
|
|
234
252
|
let llmMessages = usePrefix ? buildPrefixed() : buildTextify();
|
|
235
|
-
|
|
253
|
+
// 回声源:仅前缀分支才用调用方覆盖(前缀含既有摘要占位时,占位正文不得参与判定);
|
|
254
|
+
// textify 分支的比对源必须是它自己发给模型的文本(middleText)
|
|
255
|
+
let echoSource = usePrefix ? (opts.echoSource ?? prefixEchoSource()) : middleText;
|
|
236
256
|
// 硬顶 = 软目标 4 倍保险丝(P1c 禁硬截断/静默半截),再按窗口钳制(评审 F5):
|
|
237
257
|
// provider 多按 input+max_tokens ≤ window 校验,压缩恰在会话最满时触发,保险丝可能把
|
|
238
258
|
// 请求顶出窗口——放不下就改走文本化中段旁路(cache-miss 如实降级),仍放不下则如实失败。
|
|
239
259
|
const hardCap = (opts.maxSummaryTokens ?? maxTokens) * 4;
|
|
240
|
-
|
|
260
|
+
// 工具面也是请求体的一部分(前缀 tools → system → messages):窗口钳制必须把它
|
|
261
|
+
// 算进输入成本,否则「放得下」的判定会漏掉整包 tool schema(几十 k tokens)。
|
|
262
|
+
// 只有 prefix 分支才发 tools——textify 分支 system 指令不同、缓存必 miss,
|
|
263
|
+
// 带工具面是纯成本,还会把旁路本身顶出窗口(2026-11-12 评审 MAJ-1)。
|
|
264
|
+
const prefixTools = opts.requestView?.tools;
|
|
265
|
+
const toolsTokensFor = (withTools) => {
|
|
266
|
+
if (!withTools || !prefixTools?.length)
|
|
267
|
+
return 0;
|
|
268
|
+
const est = opts.estimator;
|
|
269
|
+
return est?.estimateTools?.(prefixTools) ?? new HeuristicTokenEstimator().estimateTools(prefixTools);
|
|
270
|
+
};
|
|
271
|
+
const windowCapOf = (msgs, withTools) => clampSummaryProviderCap({
|
|
241
272
|
hardCap,
|
|
242
|
-
// estimateLLMMessages 口径含 tool_calls/content block
|
|
243
|
-
inputTokens: estimateLLMMessages(msgs),
|
|
273
|
+
// estimateLLMMessages 口径含 tool_calls/content block/消息开销(tool_calls 全量计入)
|
|
274
|
+
inputTokens: estimateLLMMessages(msgs) + toolsTokensFor(withTools),
|
|
244
275
|
contextWindow: opts.contextWindow,
|
|
245
276
|
maxOutputTokens: opts.maxOutputTokens,
|
|
246
277
|
});
|
|
247
|
-
let providerMaxTokens = windowCapOf(llmMessages);
|
|
278
|
+
let providerMaxTokens = windowCapOf(llmMessages, usePrefix);
|
|
248
279
|
// F22 可观测:无前缀/被迫旁路 = cache-miss,如实上报,不静默换请求形状
|
|
249
280
|
if (!usePrefix) {
|
|
250
281
|
opts.emit?.({ type: 'compact.summarize_fallback', reason: 'no_prefix', cached: false });
|
|
@@ -253,16 +284,19 @@ export function createCompactEngine(options) {
|
|
|
253
284
|
usePrefix = false;
|
|
254
285
|
llmMessages = buildTextify();
|
|
255
286
|
echoSource = middleText;
|
|
256
|
-
providerMaxTokens = windowCapOf(llmMessages);
|
|
287
|
+
providerMaxTokens = windowCapOf(llmMessages, false);
|
|
257
288
|
opts.emit?.({ type: 'compact.summarize_fallback', reason: 'window_guard', cached: false });
|
|
258
289
|
}
|
|
259
290
|
if (providerMaxTokens < 256) {
|
|
260
|
-
// F8R-06 可观测:最终失败也是 cache-miss
|
|
291
|
+
// F8R-06 可观测:最终失败也是 cache-miss 旁路结果,与旁路切换对称上报
|
|
261
292
|
opts.emit?.({ type: 'compact.summarize_fallback', reason: 'window_exhausted', cached: false });
|
|
262
|
-
return { error: 'window_exhausted:summary request cannot fit the model window' };
|
|
293
|
+
return { error: 'window_exhausted:summary request cannot fit the model window', summarizeAttempted: false };
|
|
263
294
|
}
|
|
264
295
|
try {
|
|
265
296
|
const invoke = (strict) => {
|
|
297
|
+
// 重试重置(评审 M1):strict 重试产生的是新响应——被丢弃响应的 outputTokens
|
|
298
|
+
// 不得串给最终摘要;每次 invoke 从零收集
|
|
299
|
+
summaryOutputTokens = undefined;
|
|
266
300
|
const msgs = strict
|
|
267
301
|
? [
|
|
268
302
|
...llmMessages.slice(0, -1),
|
|
@@ -275,18 +309,55 @@ export function createCompactEngine(options) {
|
|
|
275
309
|
return summarizeFn(msgs, {
|
|
276
310
|
previousSummary: opts.previousSummary,
|
|
277
311
|
maxTokens: providerMaxTokens,
|
|
312
|
+
// 工具面同形(2026-11-12):缓存前缀 = tools → system → messages,缺 tools 时
|
|
313
|
+
// 前缀从第一字节断开,整段历史退化为冷 prefill。
|
|
314
|
+
// 只在 prefix 分支发:textify 分支 system 不同、缓存必 miss,带工具面是纯成本
|
|
315
|
+
// 且会挤占窗口(评审 MAJ-1)。
|
|
316
|
+
//
|
|
317
|
+
// 不传 toolChoice(2026-11-13 实测):'none' 不是对所有 provider 中性的标记。
|
|
318
|
+
// DeepSeek 收到 tool_choice:'none' 会把 tools 整个从 prompt 渲染中剔除
|
|
319
|
+
//(实测 prompt 8117 → 6286 tokens),于是落入「无工具面」那个独立缓存空间,
|
|
320
|
+
// 与会话前缀零重叠(实测 3.7% 命中)。Anthropic 侧才是中性的。
|
|
321
|
+
// 「禁止调用」由指令 + summarize_tool_call_emitted fail-loud 守卫承担——
|
|
322
|
+
// 为了一个 provider-specific 的软提示牺牲整段前缀缓存不划算。
|
|
323
|
+
...(usePrefix && prefixTools && prefixTools.length > 0 ? { tools: prefixTools } : {}),
|
|
324
|
+
...(opts.signal ? { signal: opts.signal } : {}),
|
|
325
|
+
// outputTokens 真值收集(2026-11-08):摘要产物体量以 provider 实测为准
|
|
326
|
+
onUsage: (u) => {
|
|
327
|
+
summaryOutputTokens = u.outputTokens;
|
|
328
|
+
},
|
|
278
329
|
});
|
|
279
330
|
};
|
|
280
|
-
const text = await invokeGuardedSummarize(invoke, echoSource)
|
|
281
|
-
|
|
331
|
+
const text = await invokeGuardedSummarize(invoke, echoSource, (attempt, verdict) => {
|
|
332
|
+
// 闸门判定如实上报(fail-loud):ui 与日志都该看见「被判污染 + 重试」,
|
|
333
|
+
// 而不是只显示「已等待 Ns」
|
|
334
|
+
opts.emit?.({
|
|
335
|
+
type: 'compact.contamination',
|
|
336
|
+
verdict: verdict.reason ?? 'clean',
|
|
337
|
+
attempt,
|
|
338
|
+
...(verdict.echo
|
|
339
|
+
? {
|
|
340
|
+
echoHits: verdict.echo.hits,
|
|
341
|
+
echoHitChars: verdict.echo.hitChars,
|
|
342
|
+
textChars: verdict.echo.textChars,
|
|
343
|
+
}
|
|
344
|
+
: {}),
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
// 空白摘要(空串/纯空白)按失败降级(旧 LLMSummaryCompressor 的 trim 守卫在此收口):
|
|
348
|
+
// 空白冒充 fresh 摘要会把 previousSummary 写成空白并推进覆盖点——静默失忆红线
|
|
349
|
+
if (!text.trim()) {
|
|
350
|
+
return { error: 'empty_output', summarizeAttempted: true };
|
|
351
|
+
}
|
|
352
|
+
return { summary: text, coverage: 'full', summarizeAttempted: true, outputTokens: summaryOutputTokens };
|
|
282
353
|
}
|
|
283
354
|
catch (err) {
|
|
284
355
|
// 吞两类抛错并转 error 通道交 onSummarizeFail 降级:① provider 报错/超时;
|
|
285
356
|
// ② 闸门二次污染(contaminated_output:*)。try 内无其他可抛代码,无其他路径可至此
|
|
286
|
-
return { error: err instanceof Error ? err.message : String(err) };
|
|
357
|
+
return { error: err instanceof Error ? err.message : String(err), summarizeAttempted: true };
|
|
287
358
|
}
|
|
288
359
|
}
|
|
289
|
-
return { error: 'no_summarizer' };
|
|
360
|
+
return { error: 'no_summarizer', summarizeAttempted: false };
|
|
290
361
|
}
|
|
291
362
|
async function compactCore(items, opts) {
|
|
292
363
|
const isMessages = Array.isArray(items);
|
|
@@ -296,6 +367,12 @@ export function createCompactEngine(options) {
|
|
|
296
367
|
const tokensBefore = isMessages
|
|
297
368
|
? estimator.estimateMessages(messages)
|
|
298
369
|
: estimator.estimateText(text);
|
|
370
|
+
// 摘要段真值替换(2026-11-08 outputTokens 真值):摘要产物体量以 provider 实测输出
|
|
371
|
+
// tokens 为准,估算只作无回传兜底——压缩计量「放着真值用估算」是三数字核对暴露的
|
|
372
|
+
// 口径缺陷(问题 2 拍板:能用真实值的地方换真实值)。
|
|
373
|
+
const afterWithSummaryTruth = (baseTokens, summaryText, outputTokens) => outputTokens != null && summaryText
|
|
374
|
+
? baseTokens - estimator.estimateText(summaryText) + outputTokens
|
|
375
|
+
: baseTokens;
|
|
299
376
|
// 边界归一:负数无契约语义,按 0 处理(slice 对负数是另一套语义,会切出意外视图)
|
|
300
377
|
const protectHead = Math.max(0, opts.protectHead ?? defaults.protectHead);
|
|
301
378
|
const protectTail = Math.max(0, opts.protectTail ?? defaults.protectTail);
|
|
@@ -304,11 +381,11 @@ export function createCompactEngine(options) {
|
|
|
304
381
|
const onFail = opts.onSummarizeFail ?? 'truncate';
|
|
305
382
|
const unitCount = isMessages ? messages.length : 1;
|
|
306
383
|
if (unitCount === 0 || (isMessages && messages.length === 0) || (!isMessages && !text.trim())) {
|
|
307
|
-
return { ok: true, compacted: false, reason: 'empty', tokensBefore, view: items, summary: opts.previousSummary };
|
|
384
|
+
return { ok: true, compacted: false, reason: 'empty', tokensBefore, summarizeAttempted: false, view: items, summary: opts.previousSummary };
|
|
308
385
|
}
|
|
309
386
|
// 已在目标内且非 force 语义:mode auto 时跳过
|
|
310
387
|
if (mode === 'auto' && targetTokens != null && tokensBefore <= targetTokens) {
|
|
311
|
-
return { ok: true, compacted: false, reason: 'within_target', tokensBefore, view: items, summary: opts.previousSummary };
|
|
388
|
+
return { ok: true, compacted: false, reason: 'within_target', tokensBefore, summarizeAttempted: false, view: items, summary: opts.previousSummary };
|
|
312
389
|
}
|
|
313
390
|
opts.emit?.({ type: 'compact.start', reason: mode });
|
|
314
391
|
if (mode === 'head_tail_only') {
|
|
@@ -317,7 +394,7 @@ export function createCompactEngine(options) {
|
|
|
317
394
|
const tailChars = Math.floor(text.length * 0.2);
|
|
318
395
|
// 太短无可拆中段(len<5 时 20%×2 皆为 0):原样返回不丢正文(与消息路径 too_short 同语义)
|
|
319
396
|
if (headChars === 0 && tailChars === 0) {
|
|
320
|
-
return { ok: true, compacted: false, reason: 'too_short', tokensBefore, view: text };
|
|
397
|
+
return { ok: true, compacted: false, reason: 'too_short', tokensBefore, summarizeAttempted: false, view: text };
|
|
321
398
|
}
|
|
322
399
|
// slice(-0) === slice(0) 返回全量:tailChars=0 时尾段必须显式取空
|
|
323
400
|
const view = `${text.slice(0, headChars)}\n\n[middle removed]\n\n${tailChars > 0 ? text.slice(-tailChars) : ''}`;
|
|
@@ -327,18 +404,35 @@ export function createCompactEngine(options) {
|
|
|
327
404
|
reason: 'head_tail_only',
|
|
328
405
|
tokensBefore,
|
|
329
406
|
tokensAfter: estimator.estimateText(view),
|
|
407
|
+
summarizeAttempted: false,
|
|
330
408
|
view,
|
|
331
409
|
droppedSummary: 'middle removed without LLM',
|
|
332
410
|
};
|
|
333
411
|
}
|
|
334
412
|
if (messages.length <= protectHead + protectTail) {
|
|
335
|
-
return { ok: true, compacted: false, reason: 'too_short', tokensBefore, view: messages };
|
|
413
|
+
return { ok: true, compacted: false, reason: 'too_short', tokensBefore, summarizeAttempted: false, view: messages };
|
|
336
414
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
const
|
|
415
|
+
// #10 修复:边界对齐(不拆 tool_call/tool_result 对);protectN 为期望条数
|
|
416
|
+
const { headEnd, tailStart } = alignViewSplit(messages, protectHead, protectTail);
|
|
417
|
+
const head = messages.slice(0, headEnd);
|
|
418
|
+
const tail = messages.slice(tailStart);
|
|
340
419
|
const dropped = messages.length - head.length - tail.length;
|
|
341
|
-
|
|
420
|
+
// 对齐后无可丢弃(tail 退到 headEnd):不得插占位冒充压缩(tokensAfter 会反涨,评审 P3 F)
|
|
421
|
+
if (dropped <= 0) {
|
|
422
|
+
opts.emit?.({ type: 'compact.end', reason: 'too_short', cached: true });
|
|
423
|
+
return {
|
|
424
|
+
ok: true,
|
|
425
|
+
compacted: false,
|
|
426
|
+
reason: 'too_short',
|
|
427
|
+
tokensBefore,
|
|
428
|
+
tokensAfter: tokensBefore,
|
|
429
|
+
summary: opts.previousSummary,
|
|
430
|
+
fresh: false,
|
|
431
|
+
summarizeAttempted: false,
|
|
432
|
+
view: messages,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
const placeholder = asSummaryMessage(`${MECHANICAL_NOTICE_STRUCTURAL} dropped ${dropped} middle messages (head_tail_only)`);
|
|
342
436
|
const view = [...head, placeholder, ...tail];
|
|
343
437
|
return {
|
|
344
438
|
ok: true,
|
|
@@ -347,47 +441,56 @@ export function createCompactEngine(options) {
|
|
|
347
441
|
tokensBefore,
|
|
348
442
|
tokensAfter: estimator.estimateMessages(view),
|
|
349
443
|
summary: placeholder.content,
|
|
444
|
+
summarizeAttempted: false,
|
|
350
445
|
view,
|
|
351
446
|
droppedSummary: `${dropped} messages removed without LLM`,
|
|
352
447
|
};
|
|
353
448
|
}
|
|
354
449
|
if (mode === 'summary_only') {
|
|
355
450
|
const maxTokens = opts.maxSummaryTokens ?? summarySoftTargetTokens(tokensBefore, opts.summaryBudget);
|
|
356
|
-
const summarized = await summarizeMiddle(text,
|
|
451
|
+
const summarized = await summarizeMiddle(text, opts, maxTokens);
|
|
357
452
|
if (!summarized.summary) {
|
|
358
453
|
if (onFail === 'keep_partial' && opts.previousSummary && isMessages) {
|
|
359
|
-
//
|
|
360
|
-
const
|
|
361
|
-
|
|
454
|
+
// #10 修复:尾段对齐(tail 不得起于孤儿 tool 结果);protectTail=0 时尾段为空
|
|
455
|
+
const { tailStart } = alignViewSplit(messages, 0, protectTail);
|
|
456
|
+
const view = [asSummaryMessage(opts.previousSummary), ...messages.slice(tailStart)];
|
|
457
|
+
return { ok: true, compacted: true, reason: 'partial', tokensBefore, tokensAfter: estimator.estimateMessages(view), summary: opts.previousSummary, fresh: false, summarizeAttempted: summarized.summarizeAttempted, view };
|
|
362
458
|
}
|
|
363
459
|
if (isMessages) {
|
|
364
|
-
|
|
460
|
+
// 机械降级(单级 purpose 账本):旧摘要留 summary 字段
|
|
461
|
+
// (视图由 sticky 重建注入,覆盖点不推进——宁可胖不丢)。
|
|
462
|
+
// 账本已是最小形态:仍超预算时报 mechanical_over_budget,不再丢条目。
|
|
463
|
+
const degraded = mechanicalDegrade(messages, { targetTokens, estimator });
|
|
365
464
|
return {
|
|
366
465
|
ok: true,
|
|
367
466
|
compacted: true,
|
|
368
|
-
reason:
|
|
467
|
+
reason: degraded.overBudget
|
|
468
|
+
? `summarize_failed:${summarized.error ?? 'no_output'}:mechanical_over_budget`
|
|
469
|
+
: `summarize_failed:${summarized.error ?? 'no_output'}`,
|
|
369
470
|
tokensBefore,
|
|
370
|
-
tokensAfter:
|
|
371
|
-
// 摘要失败时旧摘要仍有效:summary
|
|
471
|
+
tokensAfter: degraded.tokensAfter,
|
|
472
|
+
// 摘要失败时旧摘要仍有效:summary 字段保留之(机械占位永不冒充摘要)
|
|
372
473
|
summary: opts.previousSummary,
|
|
373
474
|
fresh: false,
|
|
374
|
-
|
|
375
|
-
|
|
475
|
+
summarizeAttempted: summarized.summarizeAttempted,
|
|
476
|
+
view: degraded.result,
|
|
477
|
+
droppedSummary: degraded.droppedSummary ?? 'middle degraded; summarizer unavailable',
|
|
376
478
|
};
|
|
377
479
|
}
|
|
378
480
|
const view = text.slice(0, 500);
|
|
379
481
|
return {
|
|
380
482
|
ok: true,
|
|
381
483
|
compacted: true,
|
|
382
|
-
reason: '
|
|
484
|
+
reason: `summarize_failed:${summarized.error ?? 'no_output'}`,
|
|
383
485
|
tokensBefore,
|
|
384
486
|
summary: opts.previousSummary,
|
|
385
487
|
fresh: false,
|
|
488
|
+
summarizeAttempted: summarized.summarizeAttempted,
|
|
386
489
|
view,
|
|
387
490
|
};
|
|
388
491
|
}
|
|
389
492
|
// 摘要即完整更新(previousSummary 已随 prompt 交给 summarizeFn 合并;
|
|
390
|
-
// 与
|
|
493
|
+
// 与 summary executor 同一「迭代式更新」契约),不得再事后拼接
|
|
391
494
|
const summary = summarized.summary;
|
|
392
495
|
if (!isMessages) {
|
|
393
496
|
return {
|
|
@@ -395,9 +498,11 @@ export function createCompactEngine(options) {
|
|
|
395
498
|
compacted: true,
|
|
396
499
|
reason: 'summary_only',
|
|
397
500
|
tokensBefore,
|
|
398
|
-
tokensAfter: estimator.estimateText(summary),
|
|
501
|
+
tokensAfter: afterWithSummaryTruth(estimator.estimateText(summary), summary, summarized.outputTokens),
|
|
399
502
|
summary,
|
|
400
503
|
fresh: true,
|
|
504
|
+
summarizeAttempted: summarized.summarizeAttempted,
|
|
505
|
+
summaryOutputTokens: summarized.outputTokens,
|
|
401
506
|
coverage: summarized.coverage,
|
|
402
507
|
view: summary,
|
|
403
508
|
};
|
|
@@ -408,22 +513,25 @@ export function createCompactEngine(options) {
|
|
|
408
513
|
compacted: true,
|
|
409
514
|
reason: 'summary_only',
|
|
410
515
|
tokensBefore,
|
|
411
|
-
tokensAfter: estimator.estimateMessages(view),
|
|
516
|
+
tokensAfter: afterWithSummaryTruth(estimator.estimateMessages(view), summary, summarized.outputTokens),
|
|
412
517
|
summary,
|
|
413
518
|
fresh: true,
|
|
519
|
+
summarizeAttempted: summarized.summarizeAttempted,
|
|
520
|
+
summaryOutputTokens: summarized.outputTokens,
|
|
414
521
|
coverage: summarized.coverage,
|
|
415
522
|
view,
|
|
416
523
|
droppedSummary: `Conversation compressed from ${tokensBefore} tokens`,
|
|
417
524
|
};
|
|
418
525
|
}
|
|
419
|
-
// structure_only
|
|
526
|
+
// structure_only(默认)。'auto' 无独立执行体:仅在上方做 within_target 短路后
|
|
527
|
+
// 落到这里与 structure_only 同路径(新增 mode 分支时勿误改此隐式契约)
|
|
420
528
|
if (!isMessages) {
|
|
421
529
|
const maxTokens = opts.maxSummaryTokens ?? summarySoftTargetTokens(tokensBefore, opts.summaryBudget);
|
|
422
|
-
const summarized = await summarizeMiddle(text,
|
|
530
|
+
const summarized = await summarizeMiddle(text, opts, maxTokens);
|
|
423
531
|
if (!summarized.summary) {
|
|
424
532
|
const head = text.slice(0, Math.max(200, Math.floor(text.length * 0.15)));
|
|
425
533
|
const tail = text.slice(-Math.max(200, Math.floor(text.length * 0.15)));
|
|
426
|
-
const view = `${head}\n\n
|
|
534
|
+
const view = `${head}\n\n${MECHANICAL_NOTICE_STRUCTURAL}\n\n${tail}`;
|
|
427
535
|
return {
|
|
428
536
|
ok: true,
|
|
429
537
|
compacted: true,
|
|
@@ -433,6 +541,7 @@ export function createCompactEngine(options) {
|
|
|
433
541
|
// 摘要失败时旧摘要仍有效:summary 字段保留之(与 messages 失败兜底同款)
|
|
434
542
|
summary: opts.previousSummary,
|
|
435
543
|
fresh: false,
|
|
544
|
+
summarizeAttempted: summarized.summarizeAttempted,
|
|
436
545
|
view,
|
|
437
546
|
};
|
|
438
547
|
}
|
|
@@ -444,71 +553,98 @@ export function createCompactEngine(options) {
|
|
|
444
553
|
compacted: true,
|
|
445
554
|
reason: 'structure_only',
|
|
446
555
|
tokensBefore,
|
|
447
|
-
tokensAfter: estimator.estimateText(view),
|
|
556
|
+
tokensAfter: afterWithSummaryTruth(estimator.estimateText(view), summary, summarized.outputTokens),
|
|
448
557
|
summary,
|
|
449
558
|
fresh: true,
|
|
559
|
+
summarizeAttempted: summarized.summarizeAttempted,
|
|
560
|
+
summaryOutputTokens: summarized.outputTokens,
|
|
450
561
|
coverage: summarized.coverage,
|
|
451
562
|
view,
|
|
452
563
|
};
|
|
453
564
|
}
|
|
454
565
|
if (messages.length <= protectHead + protectTail) {
|
|
455
|
-
return { ok: true, compacted: false, reason: 'too_short', tokensBefore, view: messages, summary: opts.previousSummary };
|
|
566
|
+
return { ok: true, compacted: false, reason: 'too_short', tokensBefore, summarizeAttempted: false, view: messages, summary: opts.previousSummary };
|
|
456
567
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
const
|
|
568
|
+
// #10 修复:head/tail 边界对齐(不拆 tool_call/tool_result 对)——protectN 为期望条数,
|
|
569
|
+
// 对齐后实际条数可能不同(head 缩 / tail 扩);protectTail=0 时尾段自然为空
|
|
570
|
+
const { headEnd, tailStart } = alignViewSplit(messages, protectHead, protectTail);
|
|
571
|
+
const head = messages.slice(0, headEnd);
|
|
572
|
+
const middle = messages.slice(headEnd, tailStart);
|
|
573
|
+
const tail = messages.slice(tailStart);
|
|
461
574
|
const headTailTokens = estimator.estimateMessages(head) + estimator.estimateMessages(tail);
|
|
462
575
|
const maxSummaryTokens = opts.maxSummaryTokens ?? summarySoftTargetTokens(tokensBefore, opts.summaryBudget);
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
//
|
|
466
|
-
//
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
else {
|
|
479
|
-
const view = [...head, asSummaryMessage(placeholder), ...tail];
|
|
576
|
+
// 摘要素材(M-4):有 previousSummary + freshMessages 时只文本化未覆盖新消息——
|
|
577
|
+
// 合并契约保证完整性,已覆盖历史不以 raw middle 复发(textify 旁路曾重现 2.13MB
|
|
578
|
+
// 事故形状)。无摘要兜底 = 全量 middle(首次压缩无合并输入,完整性优先)。
|
|
579
|
+
// N2 配套(评审 del-e1ee43ef):空 fresh 段也回退全量 middle——宁可这一次请求胖,
|
|
580
|
+
// 不得丢内容;调用方已有 already_covered 守门,空段常态不达这里。
|
|
581
|
+
const summarizeSource = opts.freshMessages?.length && opts.previousSummary ? opts.freshMessages : middle;
|
|
582
|
+
const middleText = formatMessagesAsText(summarizeSource);
|
|
583
|
+
const summarized = await summarizeMiddle(middleText, opts, maxSummaryTokens);
|
|
584
|
+
// 摘要即完整更新(previousSummary 已随 prompt 交给 summarizeFn 合并)。
|
|
585
|
+
// 失败语义(B′ 机械降级阶梯):旧摘要兜底走 summary 字段/状态(视图由 sticky 重建
|
|
586
|
+
// 注入,覆盖点不推进——宁可胖不丢);keep_partial 显式要求旧摘要进视图;
|
|
587
|
+
// 其余走打码(不丢轮次)→ 账本截断,与自动路径旧 fallbackTruncate 形状一致。
|
|
588
|
+
if (!summarized.summary) {
|
|
589
|
+
if (onFail === 'keep_partial' && opts.previousSummary) {
|
|
590
|
+
const view = [...head, asSummaryMessage(opts.previousSummary), ...tail];
|
|
480
591
|
return {
|
|
481
592
|
ok: true,
|
|
482
593
|
compacted: true,
|
|
483
|
-
reason: '
|
|
594
|
+
reason: `summarize_failed:${summarized.error ?? 'no_output'}`,
|
|
484
595
|
tokensBefore,
|
|
485
596
|
tokensAfter: estimator.estimateMessages(view),
|
|
486
|
-
summary:
|
|
597
|
+
summary: opts.previousSummary,
|
|
487
598
|
fresh: false,
|
|
599
|
+
summarizeAttempted: summarized.summarizeAttempted,
|
|
488
600
|
view,
|
|
489
|
-
droppedSummary: `${middle.length} messages dropped; summarizer failed`,
|
|
490
601
|
};
|
|
491
602
|
}
|
|
603
|
+
const degraded = mechanicalDegrade(middle, { targetTokens, estimator });
|
|
604
|
+
const view = [...head, ...degraded.result, ...tail];
|
|
605
|
+
return {
|
|
606
|
+
ok: true,
|
|
607
|
+
compacted: true,
|
|
608
|
+
reason: degraded.overBudget
|
|
609
|
+
? `summarize_failed:${summarized.error ?? 'no_output'}:mechanical_over_budget`
|
|
610
|
+
: `summarize_failed:${summarized.error ?? 'no_output'}`,
|
|
611
|
+
tokensBefore,
|
|
612
|
+
tokensAfter: estimator.estimateMessages(view),
|
|
613
|
+
// 摘要失败时旧摘要仍有效:summary 字段保留之(机械占位永不冒充摘要)
|
|
614
|
+
summary: opts.previousSummary,
|
|
615
|
+
fresh: false,
|
|
616
|
+
summarizeAttempted: summarized.summarizeAttempted,
|
|
617
|
+
view,
|
|
618
|
+
droppedSummary: degraded.droppedSummary ?? `${middle.length} messages degraded; summarizer failed`,
|
|
619
|
+
};
|
|
492
620
|
}
|
|
621
|
+
const summary = summarized.summary;
|
|
493
622
|
const view = [...head, asSummaryMessage(summary), ...tail];
|
|
494
|
-
const tokensAfter = estimator.estimateMessages(view);
|
|
623
|
+
const tokensAfter = afterWithSummaryTruth(estimator.estimateMessages(view), summary, summarized.outputTokens);
|
|
495
624
|
opts.emit?.({ type: 'compact.end', reason: mode, cached: false });
|
|
496
625
|
return {
|
|
497
626
|
ok: true,
|
|
498
627
|
compacted: true,
|
|
499
|
-
|
|
500
|
-
reason: fresh ? 'structure_only' : `summarize_failed:${summarized.error ?? 'no_output'}`,
|
|
628
|
+
reason: 'structure_only',
|
|
501
629
|
tokensBefore,
|
|
502
630
|
tokensAfter,
|
|
503
631
|
summary,
|
|
504
|
-
fresh,
|
|
632
|
+
fresh: true,
|
|
633
|
+
summarizeAttempted: summarized.summarizeAttempted,
|
|
634
|
+
summaryOutputTokens: summarized.outputTokens,
|
|
505
635
|
coverage: summarized.coverage ?? (middle.length > 0 ? 'partial' : 'full'),
|
|
506
636
|
view,
|
|
507
637
|
droppedSummary: `Structural compact: head=${head.length} middle=${middle.length} tail=${tail.length}`,
|
|
508
638
|
};
|
|
509
639
|
}
|
|
510
640
|
return {
|
|
511
|
-
compactMessages: (messages, opts) =>
|
|
512
|
-
|
|
641
|
+
compactMessages: async (messages, opts) => {
|
|
642
|
+
rejectRemovedSummaryPort(opts, 'compactMessages');
|
|
643
|
+
return compactCore(messages, opts ?? {});
|
|
644
|
+
},
|
|
645
|
+
compactText: async (text, opts) => {
|
|
646
|
+
rejectRemovedSummaryPort(opts, 'compactText');
|
|
647
|
+
return compactCore(text, opts ?? {});
|
|
648
|
+
},
|
|
513
649
|
};
|
|
514
650
|
}
|