@octopi-agent/engine 0.102.10 → 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 +5 -1
- package/dist/config-schema/agent.js +20 -2
- package/dist/config-schema/browser.d.ts +62 -0
- package/dist/config-schema/browser.js +89 -0
- package/dist/config-schema/context.d.ts +7 -2
- package/dist/config-schema/context.js +6 -3
- package/dist/config-schema/engine.d.ts +86 -7
- package/dist/config-schema/engine.js +9 -2
- package/dist/config-schema/governance.d.ts +12 -1
- package/dist/config-schema/governance.js +10 -2
- package/dist/config-schema/models.d.ts +12 -8
- package/dist/config-schema/models.js +16 -1
- package/dist/config-schema/run.d.ts +4 -0
- package/dist/config-schema/run.js +10 -0
- package/dist/config-schema/shared.d.ts +1 -0
- package/dist/config-schema/shared.js +4 -2
- package/dist/config-schema/substrate.d.ts +5 -0
- package/dist/config-schema/substrate.js +8 -0
- package/dist/config.d.ts +116 -10
- package/dist/config.js +109 -13
- 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 +4 -0
- 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 +119 -28
- package/dist/harness/activation/types.d.ts +19 -2
- package/dist/harness/agent/builder.d.ts +12 -0
- package/dist/harness/agent/builder.js +78 -22
- package/dist/harness/agent/config-bridge.d.ts +10 -1
- package/dist/harness/agent/config-bridge.js +15 -3
- 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 +2 -0
- package/dist/harness/agent/wire-agent-builder.js +9 -2
- package/dist/harness/capabilities/document/backends/plain.js +2 -1
- package/dist/harness/capabilities/document/legacy/factory.js +3 -2
- 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 +1 -0
- 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 +13 -3
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.js +48 -39
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +3 -0
- package/dist/harness/collaboration/autonomous-subsystem/types.d.ts +4 -0
- package/dist/harness/collaboration/delegation/child-run.d.ts +9 -0
- package/dist/harness/collaboration/delegation/child-run.js +34 -2
- package/dist/harness/collaboration/delegation/registry-types.d.ts +2 -0
- package/dist/harness/collaboration/delegation/service.d.ts +9 -4
- package/dist/harness/collaboration/delegation/service.js +33 -12
- package/dist/harness/collaboration/delegation/store.d.ts +2 -0
- package/dist/harness/collaboration/delegation/store.js +1 -0
- package/dist/harness/collaboration/delegation/types.d.ts +11 -0
- package/dist/harness/collaboration/delegation/wiring.d.ts +2 -0
- package/dist/harness/collaboration/delegation/wiring.js +1 -0
- package/dist/harness/context/capabilities/compact/engine.d.ts +44 -5
- package/dist/harness/context/capabilities/compact/engine.js +127 -39
- package/dist/harness/context/capabilities/compact/index.d.ts +2 -1
- package/dist/harness/context/capabilities/compact/index.js +1 -1
- package/dist/harness/context/capabilities/compact/types.d.ts +59 -9
- 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/registry.js +20 -0
- 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 +16 -0
- package/dist/harness/context/constitution/default-agents.md +85 -0
- package/dist/harness/context/context-size-ledger.js +8 -1
- package/dist/harness/context/default-context-engine.d.ts +100 -6
- package/dist/harness/context/default-context-engine.js +382 -89
- package/dist/harness/context/index.d.ts +2 -1
- package/dist/harness/context/index.js +1 -0
- 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 +57 -0
- package/dist/harness/context/layer-types.js +73 -1
- package/dist/harness/context/layers.d.ts +3 -0
- package/dist/harness/context/layers.js +13 -6
- package/dist/harness/context/prefix-fingerprint.d.ts +46 -0
- package/dist/harness/context/prefix-fingerprint.js +52 -0
- package/dist/harness/context/summarize.js +18 -0
- package/dist/harness/context/system-prompt-assembler.d.ts +2 -0
- package/dist/harness/context/system-prompt-assembler.js +5 -2
- package/dist/harness/context/tool-result-aging.d.ts +104 -16
- package/dist/harness/context/tool-result-aging.js +211 -57
- package/dist/harness/context/types.d.ts +71 -4
- 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/privileged-exec.js +4 -1
- package/dist/harness/extension/execution-environment/workspace.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/channel-types.d.ts +27 -1
- 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/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 +168 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/api.js +156 -15
- 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 +40 -5
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.js +45 -26
- package/dist/harness/extension/plugin-ecosystem/plugins/index.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.d.ts +11 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +42 -76
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +30 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +46 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +9 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +19 -14
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.d.ts +3 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.js +1 -1
- 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 +18 -0
- 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/tool-bus.js +10 -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/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/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/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 +5 -3
- package/dist/harness/index.js +2 -2
- package/dist/harness/knowledge/boot-config.d.ts +2 -0
- package/dist/harness/knowledge/boot-config.js +2 -0
- package/dist/harness/knowledge/client.d.ts +53 -7
- package/dist/harness/knowledge/client.js +77 -20
- package/dist/harness/knowledge/db.d.ts +16 -0
- package/dist/harness/knowledge/db.js +107 -41
- package/dist/harness/knowledge/engine-thread.js +5 -7
- package/dist/harness/knowledge/grounding.js +4 -1
- package/dist/harness/knowledge/http-app.d.ts +15 -0
- package/dist/harness/knowledge/http-app.js +82 -21
- package/dist/harness/knowledge/http-bridge.d.ts +15 -0
- package/dist/harness/knowledge/http-bridge.js +19 -0
- package/dist/harness/knowledge/knowledge-serve-child.js +2 -3
- package/dist/harness/knowledge/query-service.d.ts +12 -2
- package/dist/harness/knowledge/query-service.js +75 -41
- package/dist/harness/knowledge/query-worker-client.d.ts +5 -1
- package/dist/harness/knowledge/query-worker-client.js +4 -1
- package/dist/harness/knowledge/query-worker.js +5 -3
- package/dist/harness/knowledge/read-http.d.ts +2 -0
- package/dist/harness/knowledge/read-http.js +9 -2
- package/dist/harness/knowledge/retriever.d.ts +1 -1
- package/dist/harness/knowledge/retriever.js +1 -2
- package/dist/harness/knowledge/serve.d.ts +6 -1
- package/dist/harness/knowledge/serve.js +5 -6
- package/dist/harness/knowledge/source-store.d.ts +101 -5
- package/dist/harness/knowledge/source-store.js +297 -71
- package/dist/harness/knowledge/start-service-process.d.ts +27 -8
- package/dist/harness/knowledge/start-service-process.js +49 -90
- package/dist/harness/knowledge/tenant-gate.d.ts +81 -0
- package/dist/harness/knowledge/tenant-gate.js +167 -0
- package/dist/harness/knowledge/writer-service.d.ts +18 -2
- package/dist/harness/knowledge/writer-service.js +80 -16
- package/dist/harness/knowledge/writer-worker-client.d.ts +9 -2
- package/dist/harness/knowledge/writer-worker-client.js +20 -3
- package/dist/harness/knowledge/writer-worker.js +11 -4
- 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/types.d.ts +18 -2
- package/dist/harness/observability/observer/hub.d.ts +10 -0
- package/dist/harness/observability/observer/hub.js +95 -2
- package/dist/harness/run/agent/agent.js +3 -2
- package/dist/harness/run/concurrency/tool-validator.js +4 -3
- package/dist/harness/run/reliability/run-agent.d.ts +22 -0
- package/dist/harness/run/reliability/run-agent.js +79 -10
- 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 +11 -0
- package/dist/harness/run/run-scope.js +19 -1
- package/dist/harness/run/runner.d.ts +149 -22
- package/dist/harness/run/runner.js +352 -35
- 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/types.d.ts +10 -14
- package/dist/harness/shared/events/agent-event-map.d.ts +29 -0
- package/dist/harness/shared/types/agent-definition.d.ts +6 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- 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/providers/anthropic-message-normalize.d.ts +20 -0
- package/dist/integration/providers/anthropic-message-normalize.js +48 -0
- package/dist/integration/providers/anthropic.js +27 -2
- 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 +41 -1
- package/dist/integration/providers/wire-dump-diff.js +2 -1
- package/dist/integration/providers/wire-dump.d.ts +56 -2
- package/dist/integration/providers/wire-dump.js +119 -6
- package/dist/integration/storage/archive-manager.d.ts +14 -2
- package/dist/integration/storage/archive-manager.js +24 -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 +9 -0
- package/dist/integration/storage/jsonl.js +137 -38
- 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/config.yaml +3 -0
- package/dist/subsystems/wisdom-steward/govern/config.yaml +6 -1
- package/package.json +5 -3
|
@@ -19,15 +19,17 @@
|
|
|
19
19
|
* - 实际 token 数回写
|
|
20
20
|
*/
|
|
21
21
|
import { getTextContent } from '@octopi-agent/core/types.js';
|
|
22
|
+
import { contextSizeSnapshot } from './types.js';
|
|
22
23
|
import { HeuristicTokenEstimator, estimateLLMMessages } from './token-estimator.js';
|
|
23
24
|
import { ContextSizeLedger } from './context-size-ledger.js';
|
|
24
25
|
import { toolResultBody } from './tool-result-body.js';
|
|
25
|
-
import {
|
|
26
|
+
import { agedToolArgDigest, ageOneToolResult, shouldAgeArgs, applyAgingToMessages, computeToolResultAgingPlan, resolveToolResultAgingOptions, } from './tool-result-aging.js';
|
|
26
27
|
import { DefaultMessageSelector } from './message-selector.js';
|
|
27
28
|
import { DefaultBudgetAllocator } from './budget-allocator.js';
|
|
28
29
|
import { compactStateKey } from './compact-key.js';
|
|
29
30
|
import { HookedCompactEngine } from '../extension/plugin-ecosystem/hooks/compaction-integration.js';
|
|
30
|
-
import {
|
|
31
|
+
import { isTurnBoundaryAnchor, isScheduleNotice } from './layer-types.js';
|
|
32
|
+
import { fingerprintRequestView, diffPrefixFingerprint } from './prefix-fingerprint.js';
|
|
31
33
|
import { createCompactEngine } from './capabilities/compact/index.js';
|
|
32
34
|
import { alignViewSplit, isMechanicalNotice, toolExecutionLedgerTrim, semanticHistory, } from './capabilities/compact/index.js';
|
|
33
35
|
import { anchorRange } from './transform/anchors.js';
|
|
@@ -35,6 +37,32 @@ import { foldTransformView } from './transform/fold.js';
|
|
|
35
37
|
import { legacySnapshotToRecords } from './transform/migrate.js';
|
|
36
38
|
import { InMemoryTransformStore } from '../session/transform-store.js';
|
|
37
39
|
// ── 引擎实现 ──
|
|
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);
|
|
65
|
+
}
|
|
38
66
|
export class DefaultContextEngine {
|
|
39
67
|
info = {
|
|
40
68
|
id: 'default',
|
|
@@ -48,6 +76,23 @@ export class DefaultContextEngine {
|
|
|
48
76
|
orphanWarned = new Set();
|
|
49
77
|
/** P1-a 校准台账:ratio 按 model 分键,observe/calibrate 唯一出入口 */
|
|
50
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();
|
|
51
96
|
constructor(config) {
|
|
52
97
|
const tokenEstimator = config?.tokenEstimator ?? new HeuristicTokenEstimator();
|
|
53
98
|
this.config = {
|
|
@@ -98,14 +143,14 @@ export class DefaultContextEngine {
|
|
|
98
143
|
* - compact: 走 compact 能力(有 summarize 出真摘要;无 summarize / 失败走机械降级阶梯)
|
|
99
144
|
*/
|
|
100
145
|
async assemble(params) {
|
|
101
|
-
const { sessionId, agentId, messages, systemPrompt, tools, contextWindow, maxOutputTokens, compactTargetTokens, tokenEstimator, summarize, taskSnapshot, emit, loadCompactState, } = params;
|
|
146
|
+
const { sessionId, agentId, messages, systemPrompt, tools, toolAgers, toolArgAgers, contextWindow, maxOutputTokens, compactTargetTokens, tokenEstimator, summarize, taskSnapshot, emit, loadCompactState, } = params;
|
|
102
147
|
// E4:引擎内部状态键 = (sessionId, agentId)
|
|
103
148
|
const stateKey = compactStateKey(sessionId, agentId);
|
|
104
149
|
const ledger = await this.loadLedger(sessionId, agentId, messages, loadCompactState);
|
|
105
150
|
this.restoreGatesFromLedger(stateKey, ledger);
|
|
106
151
|
// F1-B2:老化计划基于**完整历史**(append-only)一次算好、随视图复用。
|
|
107
152
|
// 估算(estimateLLMMessages)作用于老化后的 LLM 视图,尺子与实际请求一致。
|
|
108
|
-
const agingPlan = computeToolResultAgingPlan(messages, this.config.historyAging);
|
|
153
|
+
const agingPlan = computeToolResultAgingPlan(messages, this.config.historyAging, { toolAgers, toolArgAgers });
|
|
109
154
|
// 窗口未知且无显式 compactTargetTokens:跳过基于预算的自动压缩/截断
|
|
110
155
|
const budgetCap = params.tokenBudget ?? contextWindow ?? compactTargetTokens;
|
|
111
156
|
const windowKnown = contextWindow != null && contextWindow > 0;
|
|
@@ -156,7 +201,13 @@ export class DefaultContextEngine {
|
|
|
156
201
|
cached: true,
|
|
157
202
|
});
|
|
158
203
|
}
|
|
159
|
-
const
|
|
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;
|
|
160
211
|
const rawEstimatedTokens = estimateLLMMessages(llmMessages) + rawEstimator.estimateTools(tools);
|
|
161
212
|
const decision = this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), rawEstimatedTokens, this.states.get(stateKey)?.lastUsageAnchor);
|
|
162
213
|
this.freezeUsagePair(stateKey, rawEstimatedTokens, modelKey);
|
|
@@ -169,6 +220,7 @@ export class DefaultContextEngine {
|
|
|
169
220
|
(folded.applied.some((r) => r.spans.length > 0)
|
|
170
221
|
? 'Structural compact view (contextWindow unknown)'
|
|
171
222
|
: undefined),
|
|
223
|
+
tools,
|
|
172
224
|
summary: this.deriveSummary(ledger),
|
|
173
225
|
};
|
|
174
226
|
}
|
|
@@ -246,12 +298,36 @@ export class DefaultContextEngine {
|
|
|
246
298
|
// 发送视图(fold+overflow aging)估算 ≤ 决策预算即无需压缩。
|
|
247
299
|
const overflowAgingOpts = resolveToolResultAgingOptions(this.config.historyAging);
|
|
248
300
|
const overflowAgingPlan = computeToolResultAgingPlan(messages, overflowAgingOpts, {
|
|
301
|
+
toolAgers,
|
|
302
|
+
toolArgAgers,
|
|
249
303
|
ignoreTurnBoundary: true,
|
|
250
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
|
+
};
|
|
251
321
|
const fits = (ms) => estimator.estimateMessages(applyAgingToMessages(ms, overflowAgingPlan, overflowAgingOpts)) <=
|
|
252
322
|
effectiveMessagesLimit;
|
|
253
323
|
if (selection.overflow.length === 0 && fits(view)) {
|
|
254
|
-
const
|
|
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;
|
|
255
331
|
const rawEstimatedTokens = estimateLLMMessages(llmMessages) + rawEstimator.estimateTools(tools);
|
|
256
332
|
const decision = this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), rawEstimatedTokens, this.states.get(stateKey)?.lastUsageAnchor);
|
|
257
333
|
this.freezeUsagePair(stateKey, rawEstimatedTokens, modelKey);
|
|
@@ -261,6 +337,7 @@ export class DefaultContextEngine {
|
|
|
261
337
|
readingSource: decision.source,
|
|
262
338
|
systemPrompt,
|
|
263
339
|
droppedSummary: proactiveDroppedSummary,
|
|
340
|
+
tools,
|
|
264
341
|
summary: this.deriveSummary(ledger),
|
|
265
342
|
};
|
|
266
343
|
}
|
|
@@ -287,8 +364,9 @@ export class DefaultContextEngine {
|
|
|
287
364
|
});
|
|
288
365
|
}
|
|
289
366
|
const overflowStart = Date.now();
|
|
290
|
-
// 事件计量口径(
|
|
291
|
-
|
|
367
|
+
// 事件计量口径(M3 统一,报告尺 reportRequestTokens):compact 对 = 全请求估算
|
|
368
|
+
//(fold+老化+system+tools,未校准)、同一集合前后对——与 manual before/after 同式。
|
|
369
|
+
const compactTokensBefore = this.reportRequestTokens(view, systemPrompt, tools, overflowAgingPlan, rawEstimator);
|
|
292
370
|
emit?.({
|
|
293
371
|
type: 'context.compact.start',
|
|
294
372
|
sessionId,
|
|
@@ -305,14 +383,15 @@ export class DefaultContextEngine {
|
|
|
305
383
|
durationMs: Date.now() - overflowStart,
|
|
306
384
|
cancelled: true,
|
|
307
385
|
});
|
|
308
|
-
|
|
386
|
+
const exit = overflowExit(view);
|
|
387
|
+
this.freezeUsagePair(stateKey, estimateLLMMessages(exit.messages) + rawEstimator.estimateTools(tools), modelKey); // 装配出口=配对冻结点(早退轮也冻,防丢样)
|
|
309
388
|
return {
|
|
310
|
-
messages:
|
|
311
|
-
estimatedTokens:
|
|
312
|
-
|
|
313
|
-
readingSource: this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), estimateLLMMessages(this.buildLlmMessages(view, systemPrompt, tools, overflowAgingPlan)) + rawEstimator.estimateTools(tools), this.states.get(stateKey)?.lastUsageAnchor).source, // source 与 tokens 同源(M2 残留 label 修正)
|
|
389
|
+
messages: exit.messages, // MJ1:与 fits 同源(overflow 豁免 aging)
|
|
390
|
+
estimatedTokens: exit.estimatedTokens,
|
|
391
|
+
readingSource: exit.readingSource,
|
|
314
392
|
systemPrompt,
|
|
315
393
|
droppedSummary: proactiveDroppedSummary,
|
|
394
|
+
tools,
|
|
316
395
|
summary: this.deriveSummary(ledger),
|
|
317
396
|
};
|
|
318
397
|
}
|
|
@@ -355,14 +434,15 @@ export class DefaultContextEngine {
|
|
|
355
434
|
durationMs: Date.now() - overflowStart,
|
|
356
435
|
cancelled: true,
|
|
357
436
|
});
|
|
358
|
-
|
|
437
|
+
const exit = overflowExit(view);
|
|
438
|
+
this.freezeUsagePair(stateKey, estimateLLMMessages(exit.messages) + rawEstimator.estimateTools(tools), modelKey); // 装配出口=配对冻结点(早退轮也冻,防丢样)
|
|
359
439
|
return {
|
|
360
|
-
messages:
|
|
361
|
-
estimatedTokens:
|
|
362
|
-
|
|
363
|
-
readingSource: this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), estimateLLMMessages(this.buildLlmMessages(view, systemPrompt, tools, overflowAgingPlan)) + rawEstimator.estimateTools(tools), this.states.get(stateKey)?.lastUsageAnchor).source, // source 与 tokens 同源(M2 残留 label 修正)
|
|
440
|
+
messages: exit.messages,
|
|
441
|
+
estimatedTokens: exit.estimatedTokens,
|
|
442
|
+
readingSource: exit.readingSource,
|
|
364
443
|
systemPrompt,
|
|
365
444
|
droppedSummary: proactiveDroppedSummary,
|
|
445
|
+
tools,
|
|
366
446
|
summary: this.deriveSummary(ledger),
|
|
367
447
|
};
|
|
368
448
|
}
|
|
@@ -380,14 +460,15 @@ export class DefaultContextEngine {
|
|
|
380
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.',
|
|
381
461
|
durationMs: Date.now() - overflowStart,
|
|
382
462
|
});
|
|
383
|
-
|
|
463
|
+
const exit = overflowExit(view);
|
|
464
|
+
this.freezeUsagePair(stateKey, estimateLLMMessages(exit.messages) + rawEstimator.estimateTools(tools), modelKey); // 装配出口=配对冻结点(早退轮也冻,防丢样)
|
|
384
465
|
return {
|
|
385
|
-
messages:
|
|
386
|
-
estimatedTokens:
|
|
387
|
-
|
|
388
|
-
readingSource: this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), estimateLLMMessages(this.buildLlmMessages(view, systemPrompt, tools, overflowAgingPlan)) + rawEstimator.estimateTools(tools), this.states.get(stateKey)?.lastUsageAnchor).source, // source 与 tokens 同源(M2 残留 label 修正)
|
|
466
|
+
messages: exit.messages,
|
|
467
|
+
estimatedTokens: exit.estimatedTokens,
|
|
468
|
+
readingSource: exit.readingSource,
|
|
389
469
|
systemPrompt,
|
|
390
470
|
droppedSummary: proactiveDroppedSummary,
|
|
471
|
+
tools,
|
|
391
472
|
summary: this.deriveSummary(ledger),
|
|
392
473
|
};
|
|
393
474
|
}
|
|
@@ -415,12 +496,17 @@ export class DefaultContextEngine {
|
|
|
415
496
|
...state,
|
|
416
497
|
lastUsageAnchor: overflowRecord === undefined ? state?.lastUsageAnchor : undefined,
|
|
417
498
|
});
|
|
499
|
+
// 估算一次、两处引用(tokensAfter 与 contextSize 同值同源)
|
|
500
|
+
const tokensAfter = this.reportRequestTokens(view, systemPrompt, tools, overflowAgingPlan, rawEstimator);
|
|
418
501
|
emit?.({
|
|
419
502
|
type: 'context.compact.end',
|
|
420
503
|
sessionId,
|
|
421
504
|
reason: 'overflow',
|
|
422
505
|
tokensBefore: compactTokensBefore,
|
|
423
|
-
tokensAfter
|
|
506
|
+
tokensAfter,
|
|
507
|
+
// contextSize 显示槽:仅真压缩(有砍除记录)才携带——零覆盖轮不动显示(评审 F-1:
|
|
508
|
+
// 估算不得在没砍东西时覆盖上一次实测);0/0 与 cancelled 同理不带
|
|
509
|
+
contextSize: overflowRecord !== undefined ? contextSizeSnapshot('compact-est', tokensAfter) : undefined,
|
|
424
510
|
durationMs: Date.now() - overflowStart,
|
|
425
511
|
// 无记录 = 未砍(第 0 轮 aging 后即收敛):空转缓存语义
|
|
426
512
|
cached: overflowRecord === undefined,
|
|
@@ -437,20 +523,21 @@ export class DefaultContextEngine {
|
|
|
437
523
|
error: `overflow_convergence_failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
438
524
|
durationMs: Date.now() - overflowStart,
|
|
439
525
|
});
|
|
440
|
-
|
|
526
|
+
const exit = overflowExit(view);
|
|
527
|
+
this.freezeUsagePair(stateKey, estimateLLMMessages(exit.messages) + rawEstimator.estimateTools(tools), modelKey); // 装配出口=配对冻结点(早退轮也冻,防丢样)
|
|
441
528
|
return {
|
|
442
|
-
messages:
|
|
443
|
-
estimatedTokens:
|
|
444
|
-
|
|
445
|
-
readingSource: this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), estimateLLMMessages(this.buildLlmMessages(view, systemPrompt, tools, overflowAgingPlan)) + rawEstimator.estimateTools(tools), this.states.get(stateKey)?.lastUsageAnchor).source, // source 与 tokens 同源(M2 残留 label 修正)
|
|
529
|
+
messages: exit.messages,
|
|
530
|
+
estimatedTokens: exit.estimatedTokens,
|
|
531
|
+
readingSource: exit.readingSource,
|
|
446
532
|
systemPrompt,
|
|
447
533
|
droppedSummary: proactiveDroppedSummary,
|
|
534
|
+
tools,
|
|
448
535
|
summary: this.deriveSummary(ledger),
|
|
449
536
|
};
|
|
450
537
|
}
|
|
451
538
|
// 8. 构建 LLM 消息(发送视图带本轮 overflow aging——豁免版只影响本次请求,
|
|
452
539
|
// 下一 turn 正常规则照常(视图级、原文不动、天然可撤销)
|
|
453
|
-
const llmMessages = this.buildLlmMessages(view, systemPrompt,
|
|
540
|
+
const llmMessages = this.buildLlmMessages(view, systemPrompt, overflowAgingPlan);
|
|
454
541
|
const rawEstimatedTokens = estimateLLMMessages(llmMessages) + rawEstimator.estimateTools(tools);
|
|
455
542
|
const decision = this.sizeLedger.decisionTokensWithSource(this.calKey(stateKey, modelKey), rawEstimatedTokens, this.states.get(stateKey)?.lastUsageAnchor);
|
|
456
543
|
this.freezeUsagePair(stateKey, rawEstimatedTokens, modelKey);
|
|
@@ -460,6 +547,7 @@ export class DefaultContextEngine {
|
|
|
460
547
|
readingSource: decision.source,
|
|
461
548
|
systemPrompt,
|
|
462
549
|
droppedSummary: droppedSummary ?? proactiveDroppedSummary,
|
|
550
|
+
tools,
|
|
463
551
|
summary: this.deriveSummary(ledger),
|
|
464
552
|
};
|
|
465
553
|
}
|
|
@@ -541,6 +629,23 @@ export class DefaultContextEngine {
|
|
|
541
629
|
measureRequestViewTokens(folded, agingPlan, estimator) {
|
|
542
630
|
return estimator.estimateMessages(applyAgingToMessages(folded.view, agingPlan, this.config.historyAging));
|
|
543
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
|
+
}
|
|
544
649
|
/**
|
|
545
650
|
* 摘要请求的回声比对源(P2):前缀去掉既有压缩占位。
|
|
546
651
|
*
|
|
@@ -554,8 +659,12 @@ export class DefaultContextEngine {
|
|
|
554
659
|
*/
|
|
555
660
|
buildEchoSource(view, systemPrompt, agingPlan) {
|
|
556
661
|
const conversation = view.filter((m) => m.metadata?.source !== 'contextSummary' && m.metadata?.source !== 'contextLedger');
|
|
557
|
-
return this.buildLlmMessages(conversation, systemPrompt,
|
|
558
|
-
|
|
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))
|
|
559
668
|
.join('\n');
|
|
560
669
|
}
|
|
561
670
|
/** 重 fold(append 后调用);孤儿告警去重键 = session|agent|seq(F9:跨会话不串键) */
|
|
@@ -969,8 +1078,8 @@ export class DefaultContextEngine {
|
|
|
969
1078
|
// 上游已中止:不发起压缩、不写记录(本轮已经作废,留下记录只会污染历史)
|
|
970
1079
|
return { view: folded.view, fold: folded };
|
|
971
1080
|
}
|
|
972
|
-
// 事件计量口径同 overflow(
|
|
973
|
-
const compactTokensBefore =
|
|
1081
|
+
// 事件计量口径同 overflow(M3 统一,报告尺 reportRequestTokens):全请求估算、同集合前后对
|
|
1082
|
+
const compactTokensBefore = this.reportRequestTokens(folded.view, systemPrompt, tools, agingPlan, rawEstimator);
|
|
974
1083
|
emit?.({
|
|
975
1084
|
type: 'context.compact.start',
|
|
976
1085
|
sessionId,
|
|
@@ -983,16 +1092,36 @@ export class DefaultContextEngine {
|
|
|
983
1092
|
// P2(2026-11-07):前缀 = **折叠视图**,与主会话请求逐字节同形 → 吃前缀缓存。
|
|
984
1093
|
// 旧实现用原 messages(全部历史):折叠生效后两者形状不同 → 缓存 0 命中,
|
|
985
1094
|
// 且每次重压都把已压缩过的中段原封不动再发一遍(实测 2.13MB 请求、provider 不返回)。
|
|
986
|
-
|
|
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);
|
|
987
1107
|
// 回声源排除既有摘要占位:模型续写上一份交接书不是「复读原文」
|
|
988
1108
|
const echoSource = this.buildEchoSource(folded.view, systemPrompt, agingPlan);
|
|
989
|
-
// P3 可观测:摘要请求即将发出——如实上报请求体量与预算,UI
|
|
1109
|
+
// P3 可观测:摘要请求即将发出——如实上报请求体量与预算,UI 不再只显示「进行中」。
|
|
1110
|
+
// 体量含工具面:tools 与 messages 一起构成真实 wire 请求(前缀 tools → system → messages)。
|
|
990
1111
|
emit?.({
|
|
991
1112
|
type: 'context.compact.progress',
|
|
992
1113
|
sessionId,
|
|
993
1114
|
reason: 'proactive',
|
|
994
1115
|
phase: 'summarize',
|
|
995
|
-
requestChars: JSON.stringify(llmPrefix).length,
|
|
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))),
|
|
996
1125
|
requestMessages: llmPrefix.length,
|
|
997
1126
|
timeoutMs: this.config.summarizeTimeoutMs || undefined,
|
|
998
1127
|
});
|
|
@@ -1024,7 +1153,9 @@ export class DefaultContextEngine {
|
|
|
1024
1153
|
contextWindow: params.contextWindow,
|
|
1025
1154
|
maxOutputTokens: params.maxOutputTokens,
|
|
1026
1155
|
onSummarizeFail: 'truncate',
|
|
1027
|
-
|
|
1156
|
+
// 原子前缀(messages + tools):一个入参,漏一半在类型层不成立。
|
|
1157
|
+
// 缓存前缀 = tools → system → messages,二者必须同批产出(2026-11-12 事故)。
|
|
1158
|
+
requestView: view,
|
|
1028
1159
|
echoSource,
|
|
1029
1160
|
signal: summarizeBudget.signal,
|
|
1030
1161
|
// F22:cache-miss 旁路透传可观测事件
|
|
@@ -1039,6 +1170,20 @@ export class DefaultContextEngine {
|
|
|
1039
1170
|
cached: e.cached,
|
|
1040
1171
|
});
|
|
1041
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
|
+
}
|
|
1042
1187
|
}
|
|
1043
1188
|
: undefined,
|
|
1044
1189
|
});
|
|
@@ -1090,7 +1235,7 @@ export class DefaultContextEngine {
|
|
|
1090
1235
|
const fresh = outcome.fresh === true &&
|
|
1091
1236
|
Boolean(outcome.summary) &&
|
|
1092
1237
|
!isMechanicalNotice(outcome.summary);
|
|
1093
|
-
await this.appendRecordFromProduct({
|
|
1238
|
+
const proactiveRecord = await this.appendRecordFromProduct({
|
|
1094
1239
|
sessionId,
|
|
1095
1240
|
agentId: params.agentId,
|
|
1096
1241
|
messages,
|
|
@@ -1122,12 +1267,16 @@ export class DefaultContextEngine {
|
|
|
1122
1267
|
// 只在 LLM 摘要调用真正发起过时推进冷却时间戳(成功/失败均算发起,防打爆)
|
|
1123
1268
|
lastProactiveLlmAt: outcome.summarizeAttempted ? Date.now() : state?.lastProactiveLlmAt,
|
|
1124
1269
|
});
|
|
1270
|
+
// 估算一次、两处引用(tokensAfter 与 contextSize 同值同源)
|
|
1271
|
+
const tokensAfter = this.reportRequestTokens(folded.view, systemPrompt, tools, agingPlan, rawEstimator);
|
|
1125
1272
|
emit?.({
|
|
1126
1273
|
type: 'context.compact.end',
|
|
1127
1274
|
sessionId,
|
|
1128
1275
|
reason: 'proactive',
|
|
1129
1276
|
tokensBefore: compactTokensBefore,
|
|
1130
|
-
tokensAfter
|
|
1277
|
+
tokensAfter,
|
|
1278
|
+
// contextSize 显示槽:仅真压缩(有砍除记录)才携带——零覆盖轮不动显示(评审 F-1)
|
|
1279
|
+
contextSize: proactiveRecord !== undefined ? contextSizeSnapshot('compact-est', tokensAfter) : undefined,
|
|
1131
1280
|
durationMs: Date.now() - proactiveStart,
|
|
1132
1281
|
cached: false,
|
|
1133
1282
|
});
|
|
@@ -1153,24 +1302,41 @@ export class DefaultContextEngine {
|
|
|
1153
1302
|
// aging 计划与 assemble 同式——禁止手抄视图。
|
|
1154
1303
|
const managedSystem = messages.find((m) => m.role === 'system' && m.metadata?.source === 'systemPrompt');
|
|
1155
1304
|
const systemText = (managedSystem ? getTextContent(managedSystem.content) : '') || params.systemPrompt;
|
|
1156
|
-
const agingPlan = computeToolResultAgingPlan(messages, this.config.historyAging
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
//
|
|
1161
|
-
//
|
|
1162
|
-
//
|
|
1163
|
-
//
|
|
1164
|
-
//
|
|
1165
|
-
const rawRequestViewTokens = (fold) =>
|
|
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);
|
|
1166
1315
|
const tokensBefore = rawRequestViewTokens(folded);
|
|
1167
|
-
// P2:前缀 =
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
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
|
|
1174
1340
|
? this.buildEchoSource(folded.view, systemText ?? '', agingPlan)
|
|
1175
1341
|
: undefined;
|
|
1176
1342
|
const protectFirstN = this.config.protectFirstN;
|
|
@@ -1225,7 +1391,14 @@ export class DefaultContextEngine {
|
|
|
1225
1391
|
// proactive/overflow 的标签
|
|
1226
1392
|
reason: 'manual',
|
|
1227
1393
|
phase: 'summarize',
|
|
1228
|
-
requestChars: JSON.stringify(llmPrefix).length
|
|
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 ?? []))),
|
|
1229
1402
|
requestMessages: llmPrefix.length,
|
|
1230
1403
|
timeoutMs: this.config.summarizeTimeoutMs || undefined,
|
|
1231
1404
|
});
|
|
@@ -1256,17 +1429,35 @@ export class DefaultContextEngine {
|
|
|
1256
1429
|
contextWindow: params.contextWindow,
|
|
1257
1430
|
maxOutputTokens: params.maxOutputTokens,
|
|
1258
1431
|
onSummarizeFail: 'truncate',
|
|
1259
|
-
|
|
1432
|
+
requestView,
|
|
1260
1433
|
echoSource,
|
|
1261
1434
|
signal: manualBudget.signal,
|
|
1262
1435
|
// F8R-06:manual 路 cache-miss 旁路同样可观测(no_prefix/window_guard/window_exhausted)
|
|
1263
1436
|
emit: params.emit
|
|
1264
|
-
? (e) =>
|
|
1265
|
-
type
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
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
|
+
}
|
|
1270
1461
|
: undefined,
|
|
1271
1462
|
});
|
|
1272
1463
|
// fresh(评审 F1)= 本次产出**新的**语义摘要。false 时 outcome.summary 只是旧摘要兜底:
|
|
@@ -1341,7 +1532,10 @@ export class DefaultContextEngine {
|
|
|
1341
1532
|
lastUsageAnchor: undefined,
|
|
1342
1533
|
lastProactiveLlmAt: outcome.summarizeAttempted ? Date.now() : this.states.get(stateKey)?.lastProactiveLlmAt,
|
|
1343
1534
|
});
|
|
1344
|
-
// M1(评审 del-e1ee43ef):manual 终端事件(成功收口)——与 progress 配对收敛 store
|
|
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;
|
|
1345
1539
|
if (llmPrefix) {
|
|
1346
1540
|
params.emit?.({
|
|
1347
1541
|
type: 'context.compact.end',
|
|
@@ -1349,6 +1543,8 @@ export class DefaultContextEngine {
|
|
|
1349
1543
|
reason: 'manual',
|
|
1350
1544
|
tokensBefore,
|
|
1351
1545
|
tokensAfter,
|
|
1546
|
+
// contextSize 显示槽:压缩后估算随事件携带(as-of);REST 返回侧由 outcome 同源携带
|
|
1547
|
+
contextSize: afterSize,
|
|
1352
1548
|
...(outcome.summarizeAttempted === true ? {} : { cached: true }),
|
|
1353
1549
|
});
|
|
1354
1550
|
}
|
|
@@ -1358,6 +1554,8 @@ export class DefaultContextEngine {
|
|
|
1358
1554
|
compacted: record !== undefined,
|
|
1359
1555
|
tokensBefore,
|
|
1360
1556
|
tokensAfter,
|
|
1557
|
+
// 显示槽同源(与事件同一快照):runner 据此持久化 session.metadata.contextSize
|
|
1558
|
+
contextSize: afterSize,
|
|
1361
1559
|
coveredMessageCount: record ? tailStart : this.derivedCoverEnd(folded),
|
|
1362
1560
|
fresh: true,
|
|
1363
1561
|
summarizeAttempted: outcome.summarizeAttempted,
|
|
@@ -1377,23 +1575,16 @@ export class DefaultContextEngine {
|
|
|
1377
1575
|
const stateKey = compactStateKey(input.sessionId, input.agentId);
|
|
1378
1576
|
// R1(评审 #5):manual 压缩同样是决策路径(目标裁剪 / tokensBefore|After 读数),
|
|
1379
1577
|
// 须与 assemble 同走校准口径;modelKey 复用会话最近一次 assemble 的分键。
|
|
1380
|
-
const
|
|
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 流下,无需同步维护。
|
|
1381
1582
|
const result = await this.structuralCompact({
|
|
1382
|
-
|
|
1583
|
+
...input,
|
|
1383
1584
|
...(input.agentId === undefined ? {} : { agentId: input.agentId }),
|
|
1384
1585
|
stateKey,
|
|
1385
|
-
|
|
1386
|
-
summarize: input.summarize,
|
|
1586
|
+
calKey,
|
|
1387
1587
|
estimator,
|
|
1388
|
-
compactTargetTokens: input.compactTargetTokens,
|
|
1389
|
-
llmPrefix: input.llmPrefix,
|
|
1390
|
-
systemPrompt: input.systemPrompt,
|
|
1391
|
-
tools: input.tools,
|
|
1392
|
-
taskSnapshot: input.taskSnapshot,
|
|
1393
|
-
contextWindow: input.contextWindow,
|
|
1394
|
-
maxOutputTokens: input.maxOutputTokens,
|
|
1395
|
-
emit: input.emit,
|
|
1396
|
-
loadCompactState: input.loadCompactState,
|
|
1397
1588
|
});
|
|
1398
1589
|
return {
|
|
1399
1590
|
...result,
|
|
@@ -1540,11 +1731,115 @@ export class DefaultContextEngine {
|
|
|
1540
1731
|
// No-op: 消息历史由调用方管理
|
|
1541
1732
|
}
|
|
1542
1733
|
/**
|
|
1543
|
-
*
|
|
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
|
+
* 摘要路径**不得**调用:它会把自己的指纹写成基线,护栏随即永远一致。
|
|
1544
1769
|
*
|
|
1545
|
-
*
|
|
1770
|
+
* @param stateKey - 引擎状态键(与 compactStateKey 同源)
|
|
1771
|
+
* @param view - 会话请求前缀(messages 必须已是老化后的 LLM 形态)
|
|
1546
1772
|
*/
|
|
1547
|
-
|
|
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
|
+
}
|
|
1809
|
+
/**
|
|
1810
|
+
* 构建**请求视图**:前缀的 messages 与 tools 同批产出(缓存前缀的原子单位)。
|
|
1811
|
+
*
|
|
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 请求视图
|
|
1820
|
+
*/
|
|
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);
|
|
1830
|
+
}
|
|
1831
|
+
return view;
|
|
1832
|
+
}
|
|
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) {
|
|
1548
1843
|
const result = [];
|
|
1549
1844
|
// 系统提示词(唯一权威来源)。
|
|
1550
1845
|
// Loop 会把同一内容以 metadata.source==='systemPrompt' unshift 进 messages,
|
|
@@ -1597,7 +1892,7 @@ export class DefaultContextEngine {
|
|
|
1597
1892
|
for (const tr of msg.toolResults ?? []) {
|
|
1598
1893
|
// F1-B2:老化条目降级为结构化指针(错误文案保真在指针内;tool_call 配对不动)
|
|
1599
1894
|
const content = aging && aging.agedIds.has(tr.toolCallId)
|
|
1600
|
-
?
|
|
1895
|
+
? ageOneToolResult(tr, aging.argsById.get(tr.toolCallId), aging, this.config.historyAging)
|
|
1601
1896
|
: toolResultBody(tr);
|
|
1602
1897
|
result.push({
|
|
1603
1898
|
role: 'tool',
|
|
@@ -1614,7 +1909,7 @@ export class DefaultContextEngine {
|
|
|
1614
1909
|
role: 'assistant',
|
|
1615
1910
|
content: getTextContent(msg.content) || null,
|
|
1616
1911
|
tool_calls: msg.toolCalls.map(tc => {
|
|
1617
|
-
const args = aging && this.config.historyAging
|
|
1912
|
+
const args = aging && shouldAgeArgs(tc.id, aging, this.config.historyAging)
|
|
1618
1913
|
? agedToolArgDigest(tc.name, tc.arguments, this.config.historyAging)
|
|
1619
1914
|
: tc.arguments;
|
|
1620
1915
|
return {
|
|
@@ -1778,11 +2073,9 @@ export function lastGenuineUserIndex(messages) {
|
|
|
1778
2073
|
let lastGenuineUserIdx = -1;
|
|
1779
2074
|
for (let i = 0; i < messages.length; i++) {
|
|
1780
2075
|
const msg = messages[i];
|
|
1781
|
-
//
|
|
1782
|
-
//
|
|
1783
|
-
if (msg
|
|
1784
|
-
continue;
|
|
1785
|
-
if (!isTurnAnchor(msg))
|
|
2076
|
+
// 边界锚点走唯一谓词 isTurnBoundaryAnchor:agentId 归因的 user
|
|
2077
|
+
// (appendCommandDiscourse)与引擎载体都不重置边界
|
|
2078
|
+
if (!isTurnBoundaryAnchor(msg))
|
|
1786
2079
|
continue;
|
|
1787
2080
|
lastGenuineUserIdx = i;
|
|
1788
2081
|
}
|