@octopi-agent/engine 0.97.15 → 0.102.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/config-schema/agent.d.ts +37 -5
- package/dist/config-schema/agent.js +24 -4
- package/dist/config-schema/context.d.ts +6 -36
- package/dist/config-schema/context.js +4 -28
- package/dist/config-schema/engine.d.ts +61 -160
- package/dist/config-schema/engine.js +1 -5
- package/dist/config-schema/governance.d.ts +10 -1
- package/dist/config-schema/governance.js +38 -1
- package/dist/config-schema/models.d.ts +2 -8
- package/dist/config-schema/models.js +0 -2
- package/dist/config-schema/run.d.ts +2 -2
- package/dist/config-schema/run.js +2 -2
- package/dist/config-schema/shared.d.ts +0 -3
- package/dist/config-schema/shared.js +0 -2
- package/dist/config-schema/substrate.d.ts +14 -88
- package/dist/config-schema/substrate.js +22 -107
- package/dist/config.d.ts +45 -113
- package/dist/config.js +8 -2
- package/dist/harness/activation/dispatcher.js +7 -0
- package/dist/harness/activation/inbox.d.ts +3 -3
- package/dist/harness/activation/inbox.js +3 -3
- package/dist/harness/activation/runtime.js +13 -5
- package/dist/harness/activation/types.d.ts +2 -2
- package/dist/harness/agent/builder.d.ts +34 -2
- package/dist/harness/agent/builder.js +189 -58
- package/dist/harness/agent/config-bridge.js +9 -2
- package/dist/harness/agent/config-integrity.d.ts +66 -0
- package/dist/harness/agent/config-integrity.js +108 -2
- package/dist/harness/agent/wire-agent-builder.d.ts +11 -3
- package/dist/harness/agent/wire-agent-builder.js +31 -7
- package/dist/harness/capabilities/document/factory.d.ts +5 -0
- package/dist/harness/capabilities/document/factory.js +1 -0
- 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/index.d.ts +2 -2
- package/dist/harness/collaboration/autonomous-subsystem/index.js +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/loader.js +4 -1
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.d.ts +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.js +21 -4
- 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/completion-guard.d.ts +40 -0
- package/dist/harness/collaboration/autonomous-subsystem/think/completion-guard.js +59 -0
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/think/index.d.ts +2 -0
- package/dist/harness/collaboration/autonomous-subsystem/think/index.js +1 -0
- package/dist/harness/collaboration/autonomous-subsystem/think/model-resolver.js +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/types.d.ts +12 -3
- package/dist/harness/collaboration/delegation/child-run.d.ts +1 -1
- package/dist/harness/collaboration/delegation/child-run.js +4 -4
- 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/service.d.ts +2 -0
- package/dist/harness/collaboration/delegation/service.js +2 -1
- package/dist/harness/collaboration/delegation/store.d.ts +2 -0
- package/dist/harness/collaboration/delegation/store.js +2 -1
- package/dist/harness/collaboration/delegation/types.d.ts +23 -2
- package/dist/harness/collaboration/delegation/types.js +22 -0
- package/dist/harness/collaboration/delegation/wiring.js +11 -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 +0 -6
- package/dist/harness/context/capabilities/compact/engine.js +169 -121
- 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 +5 -1
- package/dist/harness/context/capabilities/compact/index.js +4 -0
- 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 +76 -11
- 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/port.js +0 -1
- package/dist/harness/context/capabilities/summary/resolver.d.ts +1 -1
- package/dist/harness/context/capabilities/summary/resolver.js +4 -9
- package/dist/harness/context/capabilities/summary/tool-binding.d.ts +1 -1
- package/dist/harness/context/capabilities/summary/types.d.ts +4 -7
- package/dist/harness/context/context-size-ledger.d.ts +143 -0
- package/dist/harness/context/context-size-ledger.js +236 -0
- package/dist/harness/context/default-context-engine.d.ts +195 -119
- package/dist/harness/context/default-context-engine.js +1241 -743
- package/dist/harness/context/index.d.ts +2 -2
- package/dist/harness/context/index.js +1 -1
- package/dist/harness/context/layer-types.d.ts +15 -0
- package/dist/harness/context/layer-types.js +14 -1
- package/dist/harness/context/layers.d.ts +16 -19
- package/dist/harness/context/layers.js +42 -14
- package/dist/harness/context/message-selector.d.ts +10 -8
- package/dist/harness/context/message-selector.js +49 -27
- package/dist/harness/context/summarize.d.ts +3 -3
- package/dist/harness/context/summarize.js +11 -4
- package/dist/harness/context/system-prompt-assembler.d.ts +4 -0
- package/dist/harness/context/system-prompt-assembler.js +3 -1
- 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 +6 -1
- package/dist/harness/context/tool-result-aging.js +16 -5
- 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 +127 -117
- package/dist/harness/extension/execution-environment/effect-policy.d.ts +24 -2
- package/dist/harness/extension/execution-environment/effect-policy.js +30 -0
- package/dist/harness/extension/execution-environment/ops-audit.d.ts +7 -3
- package/dist/harness/extension/execution-environment/ops-audit.js +11 -12
- 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/undo-log.d.ts +6 -2
- package/dist/harness/extension/execution-environment/undo-log.js +31 -10
- package/dist/harness/extension/plugin-ecosystem/channel-types.d.ts +1 -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/plugins/api.d.ts +83 -249
- package/dist/harness/extension/plugin-ecosystem/plugins/api.js +88 -214
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.d.ts +43 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.js +61 -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 +50 -117
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +316 -285
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +23 -490
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +85 -353
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +41 -223
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +138 -19
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.d.ts +15 -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/builtin.js +17 -24
- package/dist/harness/extension/plugin-ecosystem/tools/delegation.js +18 -8
- package/dist/harness/extension/plugin-ecosystem/tools/document-tools.js +3 -9
- package/dist/harness/extension/plugin-ecosystem/tools/file-edit.js +3 -6
- package/dist/harness/extension/plugin-ecosystem/tools/file-ops.js +9 -5
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +9 -130
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +122 -14
- package/dist/harness/extension/plugin-ecosystem/tools/package-tools.js +4 -13
- package/dist/harness/extension/plugin-ecosystem/tools/platform.d.ts +25 -11
- package/dist/harness/extension/plugin-ecosystem/tools/platform.js +168 -62
- 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 +11 -2
- 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/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/security/capability-enforcer.js +1 -2
- package/dist/harness/governance/security/default-risk-policy.d.ts +1 -0
- package/dist/harness/governance/security/default-risk-policy.js +1 -0
- package/dist/harness/governance/security/default-security-guard.js +9 -0
- package/dist/harness/governance/security/index.d.ts +1 -0
- package/dist/harness/governance/security/index.js +2 -0
- package/dist/harness/governance/security/risk-evaluator.js +13 -0
- package/dist/harness/governance/security/risk-types.d.ts +12 -0
- package/dist/harness/governance/security/shell-parser-fallback.d.ts +29 -0
- package/dist/harness/governance/security/shell-parser-fallback.js +410 -0
- package/dist/harness/governance/security/shell-parser-shared.d.ts +83 -0
- package/dist/harness/governance/security/shell-parser-shared.js +188 -0
- package/dist/harness/governance/security/shell-parser-treesitter.d.ts +52 -0
- package/dist/harness/governance/security/shell-parser-treesitter.js +370 -0
- package/dist/harness/governance/security/shell-parser.d.ts +23 -10
- package/dist/harness/governance/security/shell-parser.js +37 -496
- package/dist/harness/governance/security/vendor/README.md +24 -0
- package/dist/harness/governance/security/vendor/tree-sitter-bash.LICENSE.txt +21 -0
- package/dist/harness/governance/security/vendor/tree-sitter-bash.wasm +0 -0
- package/dist/harness/index.d.ts +12 -12
- package/dist/harness/index.js +7 -7
- package/dist/harness/knowledge/boot-config.d.ts +55 -0
- package/dist/harness/knowledge/boot-config.js +50 -0
- package/dist/harness/knowledge/catalog-types.d.ts +2 -0
- package/dist/harness/knowledge/client.d.ts +24 -2
- package/dist/harness/knowledge/client.js +19 -5
- package/dist/harness/knowledge/db.js +5 -0
- 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 +6 -0
- package/dist/harness/knowledge/grounding.js +3 -1
- package/dist/harness/knowledge/http-app.js +35 -4
- 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 +3 -0
- package/dist/harness/knowledge/purge.js +5 -2
- package/dist/harness/knowledge/query-service.d.ts +13 -2
- package/dist/harness/knowledge/query-service.js +4 -2
- package/dist/harness/knowledge/query-worker-client.d.ts +9 -1
- package/dist/harness/knowledge/query-worker-client.js +4 -2
- package/dist/harness/knowledge/query-worker.js +4 -2
- package/dist/harness/knowledge/read-http.js +5 -1
- package/dist/harness/knowledge/serve.d.ts +8 -7
- package/dist/harness/knowledge/serve.js +9 -0
- package/dist/harness/knowledge/source-store.js +26 -3
- package/dist/harness/knowledge/start-service-process.d.ts +15 -7
- package/dist/harness/knowledge/start-service-process.js +21 -9
- package/dist/harness/knowledge/types.d.ts +7 -0
- package/dist/harness/knowledge/writer-service.d.ts +37 -9
- package/dist/harness/knowledge/writer-service.js +120 -14
- package/dist/harness/knowledge/writer-worker-client.d.ts +10 -9
- package/dist/harness/knowledge/writer-worker-client.js +20 -2
- package/dist/harness/knowledge/writer-worker.js +9 -1
- package/dist/harness/memory/index.d.ts +6 -2
- package/dist/harness/memory/index.js +4 -1
- package/dist/harness/memory/memory-events.d.ts +25 -0
- package/dist/harness/memory/memory-events.js +38 -0
- package/dist/harness/memory/sqlite/agent-db.js +15 -0
- package/dist/harness/memory/sqlite/wisdom-store.d.ts +7 -2
- package/dist/harness/memory/sqlite/wisdom-store.js +86 -12
- package/dist/harness/memory/steward-policy.d.ts +3 -0
- package/dist/harness/memory/steward-policy.js +1 -1
- package/dist/harness/memory/types.d.ts +47 -7
- package/dist/harness/memory/wisdom-attribution.d.ts +136 -0
- package/dist/harness/memory/wisdom-attribution.js +287 -0
- package/dist/harness/memory/wisdom-events.d.ts +37 -0
- package/dist/harness/memory/wisdom-events.js +61 -0
- package/dist/harness/memory/wisdom-formation.d.ts +31 -1
- package/dist/harness/memory/wisdom-formation.js +130 -33
- package/dist/harness/memory/wisdom-gates.d.ts +14 -0
- package/dist/harness/memory/wisdom-gates.js +59 -19
- package/dist/harness/memory/wisdom.d.ts +7 -2
- package/dist/harness/memory/wisdom.js +49 -2
- package/dist/harness/observability/observer/hub.js +25 -15
- 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 +66 -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/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 +10 -4
- package/dist/harness/run/reliability/run-agent.js +137 -53
- 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-scope.d.ts +41 -4
- package/dist/harness/run/runner.d.ts +71 -31
- package/dist/harness/run/runner.js +368 -106
- 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/transform-store.d.ts +136 -0
- package/dist/harness/session/transform-store.js +302 -0
- package/dist/harness/session/types.d.ts +2 -2
- package/dist/harness/shared/events/agent-event-map.d.ts +46 -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/paths/glob.d.ts +52 -0
- package/dist/harness/shared/paths/glob.js +218 -0
- package/dist/harness/shared/paths/index.d.ts +84 -0
- package/dist/harness/shared/paths/index.js +179 -0
- package/dist/harness/shared/types/agent-definition.d.ts +1 -2
- package/dist/index.d.ts +14 -17
- package/dist/index.js +7 -9
- 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.js +14 -1
- package/dist/integration/providers/openai.js +7 -1
- package/dist/integration/providers/usage.d.ts +1 -1
- package/dist/integration/providers/wire-dump.d.ts +1 -1
- package/dist/integration/storage/archive-manager.d.ts +6 -0
- package/dist/integration/storage/archive-manager.js +10 -0
- package/dist/integration/storage/jsonl.d.ts +1 -1
- package/dist/integration/storage/jsonl.js +18 -16
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +4 -2
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +12 -20
- package/dist/subsystems/memory-steward/backfill/config.yaml +4 -2
- package/dist/subsystems/memory-steward/backfill/handler.js +44 -52
- package/dist/subsystems/wisdom-steward/attribute/SUBSYSTEM.md +24 -0
- package/dist/subsystems/wisdom-steward/attribute/config.yaml +60 -0
- package/dist/subsystems/wisdom-steward/attribute/handler.d.ts +23 -0
- package/dist/subsystems/wisdom-steward/attribute/handler.js +167 -0
- package/dist/subsystems/wisdom-steward/evaluate/handler.js +3 -1
- package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +8 -1
- package/dist/subsystems/wisdom-steward/formation/config.yaml +13 -5
- package/dist/subsystems/wisdom-steward/formation/handler.js +39 -23
- package/dist/subsystems/wisdom-steward/govern/config.yaml +1 -1
- package/dist/subsystems/wisdom-steward/govern/handler.js +4 -0
- package/package.json +16 -14
- 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
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token 估算统一计数核心(P1-c:单实现 + 两 adapter)
|
|
3
|
+
*
|
|
4
|
+
* 「计数与入口形状无关」(arch/context-size-accounting.md §4.2):
|
|
5
|
+
* 入口形状(领域 `Message` / provider `LLMMessage`)只在 adapter 层归一为
|
|
6
|
+
* `Segment`,本模块是**唯一计数逻辑**。同内容两入口读数一致是测试红线(差 < 1%)。
|
|
7
|
+
*
|
|
8
|
+
* 比率策略:**先分类、再查表**({@link CHARS_PER_TOKEN_BY_KIND})。
|
|
9
|
+
* 取代旧 `estimateAdjustedChars` 的「nonLatin×3 膨胀」hack——那个 hack 是为
|
|
10
|
+
* text(4) 比率设计的,挪到 tool_result(2) 上会让全 CJK 结果变成 2 token/字(错一倍)。
|
|
11
|
+
*
|
|
12
|
+
* R4 红线:估算器不得返回常数兜底;未知内容块按长度计(告警半项归 P2-a)。
|
|
13
|
+
*/
|
|
14
|
+
/** 分类查表:chars/token(单源在 token-constants,此处只做语义命名) */
|
|
15
|
+
export declare const CHARS_PER_TOKEN_BY_KIND: {
|
|
16
|
+
/** 英文散文 */
|
|
17
|
+
readonly text_ascii: 4;
|
|
18
|
+
/** 中文等 CJK(1 token/字) */
|
|
19
|
+
readonly text_cjk: 1;
|
|
20
|
+
/** 工具结果:结构化输出更密集 */
|
|
21
|
+
readonly tool_result: 1.7;
|
|
22
|
+
/** JSON 负载(schema / arguments) */
|
|
23
|
+
readonly json: 2;
|
|
24
|
+
/** 代码 / 标识符 */
|
|
25
|
+
readonly code: 3;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* 归一化内容片段。
|
|
29
|
+
*
|
|
30
|
+
* `message_overhead` 为实现扩展(sketch 未列):消息结构开销(role/分隔符/边界标记)
|
|
31
|
+
* 不属于内容,但必须有唯一归属处——两个 adapter 在形状归一时按消息数生成。
|
|
32
|
+
*/
|
|
33
|
+
export type Segment = {
|
|
34
|
+
kind: 'text';
|
|
35
|
+
chars: number;
|
|
36
|
+
cjk: number;
|
|
37
|
+
} | {
|
|
38
|
+
kind: 'tool_use';
|
|
39
|
+
nameChars: number;
|
|
40
|
+
argChars: number;
|
|
41
|
+
} | {
|
|
42
|
+
kind: 'tool_result';
|
|
43
|
+
chars: number;
|
|
44
|
+
cjk: number;
|
|
45
|
+
isError: boolean;
|
|
46
|
+
} | {
|
|
47
|
+
kind: 'json';
|
|
48
|
+
chars: number;
|
|
49
|
+
} | {
|
|
50
|
+
kind: 'image' | 'audio' | 'video';
|
|
51
|
+
fixed: number;
|
|
52
|
+
} | {
|
|
53
|
+
kind: 'message_overhead';
|
|
54
|
+
count: number;
|
|
55
|
+
};
|
|
56
|
+
/** 消息结构开销(role + 分隔符 + 边界标记),单位 token(常量单源在 token-constants) */
|
|
57
|
+
export { MESSAGE_OVERHEAD_TOKENS } from './token-constants.js';
|
|
58
|
+
/**
|
|
59
|
+
* 字符分类:统计文本的码点数与其中的 CJK 数。
|
|
60
|
+
*
|
|
61
|
+
* 旧 `estimateAdjustedChars` 把 CJK 扩展 B 计成 ASCII(正则不匹配 surrogate),
|
|
62
|
+
* 此处并入 CJK(1 token/字)。其余非 BMP 码点(emoji 等)维持旧口径按 2 单元计,
|
|
63
|
+
* 不在本批静默调参。
|
|
64
|
+
*
|
|
65
|
+
* @param text 原始文本
|
|
66
|
+
* @returns chars=码点数;cjk=CJK 码点数(含扩展 B)
|
|
67
|
+
*/
|
|
68
|
+
export declare function classifyChars(text: string): {
|
|
69
|
+
chars: number;
|
|
70
|
+
cjk: number;
|
|
71
|
+
};
|
|
72
|
+
/** 文本 → text 段(adapter 便捷入口) */
|
|
73
|
+
export declare function textSegment(text: string): Segment;
|
|
74
|
+
/**
|
|
75
|
+
* 分段求和(不取整):两入口共用的浮点累加,供调用方在**总量**上统一取整——
|
|
76
|
+
* 逐消息取整会让读数随消息切分形态漂移(评审 #3)。
|
|
77
|
+
*
|
|
78
|
+
* @param segments 归一化内容片段
|
|
79
|
+
* @returns 估算 token 数(浮点)
|
|
80
|
+
*/
|
|
81
|
+
export declare function sumSegments(segments: Segment[]): number;
|
|
82
|
+
/**
|
|
83
|
+
* 唯一计数逻辑:Segment 列表 → token 估算(总量一次取整)。
|
|
84
|
+
*
|
|
85
|
+
* @param segments 归一化内容片段
|
|
86
|
+
* @returns 估算 token 数
|
|
87
|
+
*/
|
|
88
|
+
export declare function estimateSegments(segments: Segment[]): number;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token 估算统一计数核心(P1-c:单实现 + 两 adapter)
|
|
3
|
+
*
|
|
4
|
+
* 「计数与入口形状无关」(arch/context-size-accounting.md §4.2):
|
|
5
|
+
* 入口形状(领域 `Message` / provider `LLMMessage`)只在 adapter 层归一为
|
|
6
|
+
* `Segment`,本模块是**唯一计数逻辑**。同内容两入口读数一致是测试红线(差 < 1%)。
|
|
7
|
+
*
|
|
8
|
+
* 比率策略:**先分类、再查表**({@link CHARS_PER_TOKEN_BY_KIND})。
|
|
9
|
+
* 取代旧 `estimateAdjustedChars` 的「nonLatin×3 膨胀」hack——那个 hack 是为
|
|
10
|
+
* text(4) 比率设计的,挪到 tool_result(2) 上会让全 CJK 结果变成 2 token/字(错一倍)。
|
|
11
|
+
*
|
|
12
|
+
* R4 红线:估算器不得返回常数兜底;未知内容块按长度计(告警半项归 P2-a)。
|
|
13
|
+
*/
|
|
14
|
+
/** 分类查表:chars/token(单源在 token-constants,此处只做语义命名) */
|
|
15
|
+
export const CHARS_PER_TOKEN_BY_KIND = {
|
|
16
|
+
/** 英文散文 */
|
|
17
|
+
text_ascii: CHARS_PER_TOKEN,
|
|
18
|
+
/** 中文等 CJK(1 token/字) */
|
|
19
|
+
text_cjk: TEXT_CJK_CHARS_PER_TOKEN,
|
|
20
|
+
/** 工具结果:结构化输出更密集 */
|
|
21
|
+
tool_result: TOOL_RESULT_CHARS_PER_TOKEN,
|
|
22
|
+
/** JSON 负载(schema / arguments) */
|
|
23
|
+
json: JSON_CHARS_PER_TOKEN,
|
|
24
|
+
/** 代码 / 标识符 */
|
|
25
|
+
code: CODE_CHARS_PER_TOKEN,
|
|
26
|
+
};
|
|
27
|
+
/** 消息结构开销(role + 分隔符 + 边界标记),单位 token(常量单源在 token-constants) */
|
|
28
|
+
export { MESSAGE_OVERHEAD_TOKENS } from './token-constants.js';
|
|
29
|
+
import { MESSAGE_OVERHEAD_TOKENS, CHARS_PER_TOKEN, TOOL_RESULT_CHARS_PER_TOKEN, JSON_CHARS_PER_TOKEN, TEXT_CJK_CHARS_PER_TOKEN, CODE_CHARS_PER_TOKEN, } from './token-constants.js';
|
|
30
|
+
// ── CJK 分类 ──
|
|
31
|
+
// 非拉丁字符检测,覆盖完整东亚字符范围(BMP)
|
|
32
|
+
const NON_LATIN_RE = /[\u2E80-\u9FFF\uA000-\uA4FF\uAC00-\uD7AF\uF900-\uFAFF\uFF01-\uFF60\uFFE0-\uFFE6]/gu;
|
|
33
|
+
// CJK 扩展 B–H 的 surrogate pairs(U+20000–U+323AF):2 个 UTF-16 单元 = 1 个字符
|
|
34
|
+
const CJK_SURROGATE_HIGH_RE = /[\uD840-\uD888][\uDC00-\uDFFF]/g;
|
|
35
|
+
/**
|
|
36
|
+
* 字符分类:统计文本的码点数与其中的 CJK 数。
|
|
37
|
+
*
|
|
38
|
+
* 旧 `estimateAdjustedChars` 把 CJK 扩展 B 计成 ASCII(正则不匹配 surrogate),
|
|
39
|
+
* 此处并入 CJK(1 token/字)。其余非 BMP 码点(emoji 等)维持旧口径按 2 单元计,
|
|
40
|
+
* 不在本批静默调参。
|
|
41
|
+
*
|
|
42
|
+
* @param text 原始文本
|
|
43
|
+
* @returns chars=码点数;cjk=CJK 码点数(含扩展 B)
|
|
44
|
+
*/
|
|
45
|
+
export function classifyChars(text) {
|
|
46
|
+
if (text.length === 0)
|
|
47
|
+
return { chars: 0, cjk: 0 };
|
|
48
|
+
const surrogatePairs = (text.match(CJK_SURROGATE_HIGH_RE) ?? []).length;
|
|
49
|
+
const chars = text.length - surrogatePairs;
|
|
50
|
+
const cjk = (text.match(NON_LATIN_RE) ?? []).length + surrogatePairs;
|
|
51
|
+
return { chars, cjk };
|
|
52
|
+
}
|
|
53
|
+
/** 文本 → text 段(adapter 便捷入口) */
|
|
54
|
+
export function textSegment(text) {
|
|
55
|
+
const { chars, cjk } = classifyChars(text);
|
|
56
|
+
return { kind: 'text', chars, cjk };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 分段求和(不取整):两入口共用的浮点累加,供调用方在**总量**上统一取整——
|
|
60
|
+
* 逐消息取整会让读数随消息切分形态漂移(评审 #3)。
|
|
61
|
+
*
|
|
62
|
+
* @param segments 归一化内容片段
|
|
63
|
+
* @returns 估算 token 数(浮点)
|
|
64
|
+
*/
|
|
65
|
+
export function sumSegments(segments) {
|
|
66
|
+
let total = 0;
|
|
67
|
+
for (const seg of segments) {
|
|
68
|
+
switch (seg.kind) {
|
|
69
|
+
case 'text':
|
|
70
|
+
total += seg.cjk / CHARS_PER_TOKEN_BY_KIND.text_cjk + (seg.chars - seg.cjk) / CHARS_PER_TOKEN_BY_KIND.text_ascii;
|
|
71
|
+
break;
|
|
72
|
+
case 'tool_use':
|
|
73
|
+
total += seg.nameChars / CHARS_PER_TOKEN_BY_KIND.code + seg.argChars / CHARS_PER_TOKEN_BY_KIND.json;
|
|
74
|
+
break;
|
|
75
|
+
case 'tool_result':
|
|
76
|
+
// 分类计数:CJK 段按 1 token/字,结构化 ASCII 段按 TOOL_RESULT_CHARS_PER_TOKEN(1.7,2026-11-08 L1 调表)。
|
|
77
|
+
// isError 当前与正常结果同权(属性保留,调参留待有实测支撑时)。
|
|
78
|
+
total += seg.cjk / CHARS_PER_TOKEN_BY_KIND.text_cjk + (seg.chars - seg.cjk) / CHARS_PER_TOKEN_BY_KIND.tool_result;
|
|
79
|
+
break;
|
|
80
|
+
case 'json':
|
|
81
|
+
total += seg.chars / CHARS_PER_TOKEN_BY_KIND.json;
|
|
82
|
+
break;
|
|
83
|
+
case 'image':
|
|
84
|
+
case 'audio':
|
|
85
|
+
case 'video':
|
|
86
|
+
total += seg.fixed;
|
|
87
|
+
break;
|
|
88
|
+
case 'message_overhead':
|
|
89
|
+
total += seg.count * MESSAGE_OVERHEAD_TOKENS;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return total;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 唯一计数逻辑:Segment 列表 → token 估算(总量一次取整)。
|
|
97
|
+
*
|
|
98
|
+
* @param segments 归一化内容片段
|
|
99
|
+
* @returns 估算 token 数
|
|
100
|
+
*/
|
|
101
|
+
export function estimateSegments(segments) {
|
|
102
|
+
return Math.ceil(sumSegments(segments));
|
|
103
|
+
}
|
|
@@ -87,9 +87,14 @@ export declare function resolveToolResultAgingOptions(input?: ToolResultAgingCon
|
|
|
87
87
|
*
|
|
88
88
|
* @param messages - **完整**历史消息(按时间序;勿传裁剪后的装配视图)
|
|
89
89
|
* @param opts - 老化配置(已解析)
|
|
90
|
+
* @param override - overflow 收敛阶梯第 0 轮(2026-11-08):ignoreTurnBoundary=true 时
|
|
91
|
+
* 豁免 turn 首限定(不限 crossTurn、rawWindow 归 0)——守门兜底语境下「发得出去」
|
|
92
|
+
* 优先于「保护 turn 内工作数据/缓存前缀」;阈值(maxPointerChars/freezeBatch)不动
|
|
90
93
|
* @returns 老化计划(enabled=false 或无候选时 agedIds 为空)
|
|
91
94
|
*/
|
|
92
|
-
export declare function computeToolResultAgingPlan(messages: Message[], opts: ToolResultAgingOptions
|
|
95
|
+
export declare function computeToolResultAgingPlan(messages: Message[], opts: ToolResultAgingOptions, override?: {
|
|
96
|
+
ignoreTurnBoundary?: boolean;
|
|
97
|
+
}): ToolResultAgingPlan;
|
|
93
98
|
/**
|
|
94
99
|
* 把老化计划物化为消息视图副本(新建对象,不改原消息)
|
|
95
100
|
*
|
|
@@ -71,9 +71,12 @@ export function resolveToolResultAgingOptions(input) {
|
|
|
71
71
|
*
|
|
72
72
|
* @param messages - **完整**历史消息(按时间序;勿传裁剪后的装配视图)
|
|
73
73
|
* @param opts - 老化配置(已解析)
|
|
74
|
+
* @param override - overflow 收敛阶梯第 0 轮(2026-11-08):ignoreTurnBoundary=true 时
|
|
75
|
+
* 豁免 turn 首限定(不限 crossTurn、rawWindow 归 0)——守门兜底语境下「发得出去」
|
|
76
|
+
* 优先于「保护 turn 内工作数据/缓存前缀」;阈值(maxPointerChars/freezeBatch)不动
|
|
74
77
|
* @returns 老化计划(enabled=false 或无候选时 agedIds 为空)
|
|
75
78
|
*/
|
|
76
|
-
export function computeToolResultAgingPlan(messages, opts) {
|
|
79
|
+
export function computeToolResultAgingPlan(messages, opts, override) {
|
|
77
80
|
const argsById = new Map();
|
|
78
81
|
const ordered = [];
|
|
79
82
|
let lastGenuineUserIdx = -1;
|
|
@@ -98,10 +101,18 @@ export function computeToolResultAgingPlan(messages, opts) {
|
|
|
98
101
|
const agedIds = new Set();
|
|
99
102
|
if (!opts.enabled)
|
|
100
103
|
return { agedIds, argsById };
|
|
101
|
-
// 边界 = 完整历史最后一个真实 user(两遍判定:旧轮结果在遇到后续 user
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const
|
|
104
|
+
// 边界 = 完整历史最后一个真实 user(两遍判定:旧轮结果在遇到后续 user 前不可下结论)。
|
|
105
|
+
// overflow 第 0 轮豁免(ignoreTurnBoundary):全历史候选、rawWindow 归 0——
|
|
106
|
+
// turn 内工作数据的保护让位于「装得下去」(arch/overflow-redesign.md §3 第 0 轮细则)
|
|
107
|
+
const ignoreTurnBoundary = override?.ignoreTurnBoundary === true;
|
|
108
|
+
const crossTurn = ignoreTurnBoundary
|
|
109
|
+
? ordered
|
|
110
|
+
: ordered.filter((e) => e.msgIndex < lastGenuineUserIdx);
|
|
111
|
+
const rawWindow = ignoreTurnBoundary ? 0 : opts.rawWindow;
|
|
112
|
+
// freezeBatch(冻结批次防抖)同属 turn 内保护——守门语境豁免:发不出去了不等批次
|
|
113
|
+
const freezeBatch = ignoreTurnBoundary ? 1 : opts.freezeBatch;
|
|
114
|
+
const candidates = crossTurn.slice(0, Math.max(0, crossTurn.length - rawWindow));
|
|
115
|
+
const covered = candidates.length - (candidates.length % freezeBatch);
|
|
105
116
|
for (let i = 0; i < covered; i++) {
|
|
106
117
|
const cand = candidates[i];
|
|
107
118
|
if (cand.isError)
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* 消息内容写入后不可变(全库无对已存在消息 content/toolResults/toolCalls 的
|
|
5
5
|
* 原地修改;压缩器一律新建对象),同一 `tr` 对象可安全复用序列化结果。
|
|
6
6
|
* 这消除每轮 context 装配里对同一批 toolResults 反复 `JSON.stringify` 的开销。
|
|
7
|
+
* 注意:摘要投影(capabilities/compact 的会话文本化)也复用本缓存——原地改写
|
|
8
|
+
* toolResult 不只影响 token 计数,还会让摘要吃到陈旧正文;上述不可变性是硬前提。
|
|
7
9
|
*/
|
|
8
10
|
const cache = new WeakMap();
|
|
9
11
|
/**
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message anchors — stable coordinates for transform records.
|
|
3
|
+
*
|
|
4
|
+
* Messages in the session log are append-only and immutable (tool-result
|
|
5
|
+
* aging is view-level), so a content identity hash is a stable coordinate
|
|
6
|
+
* across restarts and session forks. `occurrence` disambiguates byte-identical
|
|
7
|
+
* duplicates in stream order; `indexHint` is only a resolution fast path.
|
|
8
|
+
*
|
|
9
|
+
* Ephemeral messages (never persisted) are never anchorable: a record whose
|
|
10
|
+
* boundary would fall on one is trimmed inward at creation (see
|
|
11
|
+
* {@link anchorRange}), so anchors always resolve against the persisted stream.
|
|
12
|
+
*
|
|
13
|
+
* @module harness/context/transform/anchors
|
|
14
|
+
*/
|
|
15
|
+
import type { Message } from '@octopi-agent/core/types.js';
|
|
16
|
+
import type { MessageAnchor, TransformRun } from './types.js';
|
|
17
|
+
/**
|
|
18
|
+
* Canonical identity hash of a message.
|
|
19
|
+
*
|
|
20
|
+
* Covers the persisted identity fields (role / content / toolCalls / toolResults
|
|
21
|
+
* / source / timestamp / agentId / metadata). Only `ephemeral` is excluded —
|
|
22
|
+
* it is in-run only, never persisted, and must not affect identity.
|
|
23
|
+
*
|
|
24
|
+
* @param m - message
|
|
25
|
+
* @returns sha256 hex of the canonical form
|
|
26
|
+
*/
|
|
27
|
+
export declare function canonicalMessageHash(m: Message): string;
|
|
28
|
+
/**
|
|
29
|
+
* Build the anchor for messages[index], with occurrence counted in stream order.
|
|
30
|
+
*
|
|
31
|
+
* @param messages - full message stream (may include ephemeral entries)
|
|
32
|
+
* @param index - raw index
|
|
33
|
+
* @returns anchor
|
|
34
|
+
*/
|
|
35
|
+
export declare function anchorFor(messages: Message[], index: number): MessageAnchor;
|
|
36
|
+
/**
|
|
37
|
+
* Resolve an anchor to a raw index in the current stream.
|
|
38
|
+
*
|
|
39
|
+
* Fast path: `indexHint` when the hash still matches there (append-only
|
|
40
|
+
* streams keep earlier indices stable). Slow path: the occurrence-th message
|
|
41
|
+
* with the anchor hash. Ephemeral messages are skipped during scanning — they
|
|
42
|
+
* cannot be anchors, and their presence/absence must not shift resolution.
|
|
43
|
+
*
|
|
44
|
+
* @param messages - current message stream
|
|
45
|
+
* @param anchor - anchor to resolve
|
|
46
|
+
* @returns raw index, or undefined when unresolvable (orphan)
|
|
47
|
+
*/
|
|
48
|
+
export declare function resolveAnchor(messages: Message[], anchor: MessageAnchor): number | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Anchor an inclusive index range `[start, end]`, trimming ephemeral
|
|
51
|
+
* boundaries inward so both anchors resolve against the persisted stream.
|
|
52
|
+
*
|
|
53
|
+
* @param messages - full message stream
|
|
54
|
+
* @param start - inclusive raw start index
|
|
55
|
+
* @param end - inclusive raw end index
|
|
56
|
+
* @returns run, or undefined when no non-ephemeral message remains in range
|
|
57
|
+
*/
|
|
58
|
+
export declare function anchorRange(messages: Message[], start: number, end: number): TransformRun | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Resolve a run to an inclusive raw index range.
|
|
61
|
+
*
|
|
62
|
+
* @param messages - current message stream
|
|
63
|
+
* @param run - run to resolve
|
|
64
|
+
* @returns `[start, end]` inclusive, or undefined when either anchor is orphan
|
|
65
|
+
*/
|
|
66
|
+
export declare function resolveRun(messages: Message[], run: TransformRun): [number, number] | undefined;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message anchors — stable coordinates for transform records.
|
|
3
|
+
*
|
|
4
|
+
* Messages in the session log are append-only and immutable (tool-result
|
|
5
|
+
* aging is view-level), so a content identity hash is a stable coordinate
|
|
6
|
+
* across restarts and session forks. `occurrence` disambiguates byte-identical
|
|
7
|
+
* duplicates in stream order; `indexHint` is only a resolution fast path.
|
|
8
|
+
*
|
|
9
|
+
* Ephemeral messages (never persisted) are never anchorable: a record whose
|
|
10
|
+
* boundary would fall on one is trimmed inward at creation (see
|
|
11
|
+
* {@link anchorRange}), so anchors always resolve against the persisted stream.
|
|
12
|
+
*
|
|
13
|
+
* @module harness/context/transform/anchors
|
|
14
|
+
*/
|
|
15
|
+
import { createHash } from 'node:crypto';
|
|
16
|
+
/**
|
|
17
|
+
* Canonical identity hash of a message.
|
|
18
|
+
*
|
|
19
|
+
* Covers the persisted identity fields (role / content / toolCalls / toolResults
|
|
20
|
+
* / source / timestamp / agentId / metadata). Only `ephemeral` is excluded —
|
|
21
|
+
* it is in-run only, never persisted, and must not affect identity.
|
|
22
|
+
*
|
|
23
|
+
* @param m - message
|
|
24
|
+
* @returns sha256 hex of the canonical form
|
|
25
|
+
*/
|
|
26
|
+
export function canonicalMessageHash(m) {
|
|
27
|
+
return createHash('sha256')
|
|
28
|
+
.update(canonicalJson({ role: m.role, content: m.content, toolCalls: m.toolCalls, toolResults: m.toolResults, source: m.source, timestamp: m.timestamp, agentId: m.agentId, metadata: m.metadata }), 'utf8')
|
|
29
|
+
.digest('hex');
|
|
30
|
+
}
|
|
31
|
+
/** Deterministic JSON: object keys sorted recursively. */
|
|
32
|
+
function canonicalJson(value) {
|
|
33
|
+
if (value === null || typeof value !== 'object')
|
|
34
|
+
return JSON.stringify(value) ?? 'undefined';
|
|
35
|
+
if (Array.isArray(value))
|
|
36
|
+
return `[${value.map(canonicalJson).join(',')}]`;
|
|
37
|
+
const entries = Object.entries(value)
|
|
38
|
+
.filter(([, v]) => v !== undefined)
|
|
39
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
40
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(',')}}`;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Build the anchor for messages[index], with occurrence counted in stream order.
|
|
44
|
+
*
|
|
45
|
+
* @param messages - full message stream (may include ephemeral entries)
|
|
46
|
+
* @param index - raw index
|
|
47
|
+
* @returns anchor
|
|
48
|
+
*/
|
|
49
|
+
export function anchorFor(messages, index) {
|
|
50
|
+
const hash = canonicalMessageHash(messages[index]);
|
|
51
|
+
let occurrence = 0;
|
|
52
|
+
for (let i = 0; i < index; i++) {
|
|
53
|
+
// 与 resolveAnchor 对称(评审 F8):ephemeral 不参与 occurrence 计数,
|
|
54
|
+
// 否则「创建时含 ephemeral 同形、解析时被跳过」会错位失锚
|
|
55
|
+
if (messages[i]?.ephemeral)
|
|
56
|
+
continue;
|
|
57
|
+
if (canonicalMessageHash(messages[i]) === hash)
|
|
58
|
+
occurrence += 1;
|
|
59
|
+
}
|
|
60
|
+
return { hash, occurrence, indexHint: index };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Resolve an anchor to a raw index in the current stream.
|
|
64
|
+
*
|
|
65
|
+
* Fast path: `indexHint` when the hash still matches there (append-only
|
|
66
|
+
* streams keep earlier indices stable). Slow path: the occurrence-th message
|
|
67
|
+
* with the anchor hash. Ephemeral messages are skipped during scanning — they
|
|
68
|
+
* cannot be anchors, and their presence/absence must not shift resolution.
|
|
69
|
+
*
|
|
70
|
+
* @param messages - current message stream
|
|
71
|
+
* @param anchor - anchor to resolve
|
|
72
|
+
* @returns raw index, or undefined when unresolvable (orphan)
|
|
73
|
+
*/
|
|
74
|
+
export function resolveAnchor(messages, anchor) {
|
|
75
|
+
const hinted = messages[anchor.indexHint];
|
|
76
|
+
if (hinted && !hinted.ephemeral && canonicalMessageHash(hinted) === anchor.hash) {
|
|
77
|
+
return anchor.indexHint;
|
|
78
|
+
}
|
|
79
|
+
let seen = 0;
|
|
80
|
+
for (let i = 0; i < messages.length; i++) {
|
|
81
|
+
const m = messages[i];
|
|
82
|
+
if (m.ephemeral)
|
|
83
|
+
continue;
|
|
84
|
+
if (canonicalMessageHash(m) !== anchor.hash)
|
|
85
|
+
continue;
|
|
86
|
+
if (seen === anchor.occurrence)
|
|
87
|
+
return i;
|
|
88
|
+
seen += 1;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Anchor an inclusive index range `[start, end]`, trimming ephemeral
|
|
94
|
+
* boundaries inward so both anchors resolve against the persisted stream.
|
|
95
|
+
*
|
|
96
|
+
* @param messages - full message stream
|
|
97
|
+
* @param start - inclusive raw start index
|
|
98
|
+
* @param end - inclusive raw end index
|
|
99
|
+
* @returns run, or undefined when no non-ephemeral message remains in range
|
|
100
|
+
*/
|
|
101
|
+
export function anchorRange(messages, start, end) {
|
|
102
|
+
let a = start;
|
|
103
|
+
let b = end;
|
|
104
|
+
while (a <= b && messages[a]?.ephemeral)
|
|
105
|
+
a += 1;
|
|
106
|
+
while (b >= a && messages[b]?.ephemeral)
|
|
107
|
+
b -= 1;
|
|
108
|
+
if (a > b)
|
|
109
|
+
return undefined;
|
|
110
|
+
return { from: anchorFor(messages, a), to: anchorFor(messages, b) };
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Resolve a run to an inclusive raw index range.
|
|
114
|
+
*
|
|
115
|
+
* @param messages - current message stream
|
|
116
|
+
* @param run - run to resolve
|
|
117
|
+
* @returns `[start, end]` inclusive, or undefined when either anchor is orphan
|
|
118
|
+
*/
|
|
119
|
+
export function resolveRun(messages, run) {
|
|
120
|
+
const a = resolveAnchor(messages, run.from);
|
|
121
|
+
const b = resolveAnchor(messages, run.to);
|
|
122
|
+
if (a === undefined || b === undefined || b < a)
|
|
123
|
+
return undefined;
|
|
124
|
+
return [a, b];
|
|
125
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic fold — the sole view implementation in the transform-record
|
|
3
|
+
* model: `view = originals with every effective record's runs replaced by its
|
|
4
|
+
* placeholder`.
|
|
5
|
+
*
|
|
6
|
+
* No cursors, no incremental rescue: any message not covered by an effective
|
|
7
|
+
* record is verbatim in the view. The same (messages, ledger) always yields
|
|
8
|
+
* the same view — that identity is the operational definition of the red line
|
|
9
|
+
* 「压缩产物跨装配永久生效」.
|
|
10
|
+
*
|
|
11
|
+
* Overlap composition: a later record wins the overlapping span **only when
|
|
12
|
+
* its text represents the subsumed content** — a fresh summary was merged from
|
|
13
|
+
* the previous summary at creation. A `degrade` record's mechanical text does
|
|
14
|
+
* NOT represent subsumed records, so the translation layer
|
|
15
|
+
* (`appendRecordFromProduct`) never lets degrade records cover placeholder
|
|
16
|
+
* spans; by the time spans reach this fold, later-seq-wins is safe.
|
|
17
|
+
*
|
|
18
|
+
* Orphan handling is **per run** (review F3): a record applies every run that
|
|
19
|
+
* still resolves; only a record with zero resolvable runs is fully orphaned.
|
|
20
|
+
* Edit-resend truncates the message stream — run-level application keeps the
|
|
21
|
+
* surviving coverage instead of refilling it.
|
|
22
|
+
*
|
|
23
|
+
* @module harness/context/transform/fold
|
|
24
|
+
*/
|
|
25
|
+
import type { Message } from '@octopi-agent/core/types.js';
|
|
26
|
+
import type { TransformLedger, TransformRecord } from './types.js';
|
|
27
|
+
export interface FoldOptions {
|
|
28
|
+
/** Called once per fully-orphaned record (no run resolves anymore) */
|
|
29
|
+
onOrphan?: (record: TransformRecord) => void;
|
|
30
|
+
/** Called per record whose runs were partially dropped (some runs no longer resolve) */
|
|
31
|
+
onPartialOrphan?: (record: TransformRecord, droppedRuns: number) => void;
|
|
32
|
+
}
|
|
33
|
+
export interface FoldRun {
|
|
34
|
+
record: TransformRecord;
|
|
35
|
+
/** resolved inclusive raw ranges (original index space) */
|
|
36
|
+
spans: Array<[number, number]>;
|
|
37
|
+
}
|
|
38
|
+
export interface FoldResult {
|
|
39
|
+
/** the deterministic assembled view (originals + placeholders) */
|
|
40
|
+
view: Message[];
|
|
41
|
+
/** effective records with resolved spans (for translation/events) */
|
|
42
|
+
applied: FoldRun[];
|
|
43
|
+
/** effective records with zero resolvable spans (skipped entirely) */
|
|
44
|
+
orphans: TransformRecord[];
|
|
45
|
+
/** rendered placeholder message → its record (product translation identity map) */
|
|
46
|
+
placeholderOf: Map<Message, FoldRun>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Apply effective records over the original messages.
|
|
50
|
+
*
|
|
51
|
+
* @param messages - full original stream (may include ephemeral entries)
|
|
52
|
+
* @param ledger - transform ledger (records ordered by seq)
|
|
53
|
+
* @param options - orphan sinks
|
|
54
|
+
* @returns fold result
|
|
55
|
+
*/
|
|
56
|
+
export declare function foldTransformView(messages: Message[], ledger: TransformLedger, options: FoldOptions): FoldResult;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic fold — the sole view implementation in the transform-record
|
|
3
|
+
* model: `view = originals with every effective record's runs replaced by its
|
|
4
|
+
* placeholder`.
|
|
5
|
+
*
|
|
6
|
+
* No cursors, no incremental rescue: any message not covered by an effective
|
|
7
|
+
* record is verbatim in the view. The same (messages, ledger) always yields
|
|
8
|
+
* the same view — that identity is the operational definition of the red line
|
|
9
|
+
* 「压缩产物跨装配永久生效」.
|
|
10
|
+
*
|
|
11
|
+
* Overlap composition: a later record wins the overlapping span **only when
|
|
12
|
+
* its text represents the subsumed content** — a fresh summary was merged from
|
|
13
|
+
* the previous summary at creation. A `degrade` record's mechanical text does
|
|
14
|
+
* NOT represent subsumed records, so the translation layer
|
|
15
|
+
* (`appendRecordFromProduct`) never lets degrade records cover placeholder
|
|
16
|
+
* spans; by the time spans reach this fold, later-seq-wins is safe.
|
|
17
|
+
*
|
|
18
|
+
* Orphan handling is **per run** (review F3): a record applies every run that
|
|
19
|
+
* still resolves; only a record with zero resolvable runs is fully orphaned.
|
|
20
|
+
* Edit-resend truncates the message stream — run-level application keeps the
|
|
21
|
+
* surviving coverage instead of refilling it.
|
|
22
|
+
*
|
|
23
|
+
* @module harness/context/transform/fold
|
|
24
|
+
*/
|
|
25
|
+
import { asSummaryMessage } from '../capabilities/compact/messages.js';
|
|
26
|
+
import { resolveRun } from './anchors.js';
|
|
27
|
+
/**
|
|
28
|
+
* Apply effective records over the original messages.
|
|
29
|
+
*
|
|
30
|
+
* @param messages - full original stream (may include ephemeral entries)
|
|
31
|
+
* @param ledger - transform ledger (records ordered by seq)
|
|
32
|
+
* @param options - orphan sinks
|
|
33
|
+
* @returns fold result
|
|
34
|
+
*/
|
|
35
|
+
export function foldTransformView(messages, ledger, options) {
|
|
36
|
+
const applied = [];
|
|
37
|
+
const orphans = [];
|
|
38
|
+
// span assignment in original index space; later seq overwrites (see module doc)
|
|
39
|
+
const coverAt = new Array(messages.length).fill(undefined);
|
|
40
|
+
// first/last covered index per record (placeholder position / coverage line)
|
|
41
|
+
const firstIndex = new Map();
|
|
42
|
+
const lastIndex = new Map();
|
|
43
|
+
for (const record of ledger.records) {
|
|
44
|
+
if (record.revoked)
|
|
45
|
+
continue;
|
|
46
|
+
// per-run resolution(F3):能解析的 run 生效;整记录零可解析才算孤儿
|
|
47
|
+
const spans = [];
|
|
48
|
+
let droppedRuns = 0;
|
|
49
|
+
for (const run of record.covers) {
|
|
50
|
+
const resolved = resolveRun(messages, run);
|
|
51
|
+
if (resolved === undefined) {
|
|
52
|
+
droppedRuns += 1;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
spans.push(resolved);
|
|
56
|
+
}
|
|
57
|
+
if (record.covers.length > 0 && spans.length === 0) {
|
|
58
|
+
orphans.push(record);
|
|
59
|
+
options.onOrphan?.(record);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (droppedRuns > 0) {
|
|
63
|
+
options.onPartialOrphan?.(record, droppedRuns);
|
|
64
|
+
}
|
|
65
|
+
applied.push({ record, spans });
|
|
66
|
+
for (const [a, b] of spans) {
|
|
67
|
+
for (let i = a; i <= b; i++) {
|
|
68
|
+
coverAt[i] = record;
|
|
69
|
+
const f = firstIndex.get(record);
|
|
70
|
+
const l = lastIndex.get(record);
|
|
71
|
+
if (f === undefined || i < f)
|
|
72
|
+
firstIndex.set(record, i);
|
|
73
|
+
if (l === undefined || i > l)
|
|
74
|
+
lastIndex.set(record, i);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// 零覆盖记录(covers:[],legacy 迁移的「覆盖未知」残缺快照):summary 型且有正文时
|
|
79
|
+
// 渲染在视图最前(旧粘滞视图把它插在 head 之后;fold 无 protectFirstN 概念,
|
|
80
|
+
// 位置取确定性的起点——信息等价、位置不同,仅此一形)。
|
|
81
|
+
const zeroCover = applied.filter((r) => r.spans.length === 0 && r.record.type === 'summary' && r.record.text);
|
|
82
|
+
const view = [];
|
|
83
|
+
const placeholderOf = new Map();
|
|
84
|
+
const rendered = new Set();
|
|
85
|
+
// zero-cover summary 占位映射到其记录(spans 空);翻译需要身份映射
|
|
86
|
+
for (const zc of zeroCover) {
|
|
87
|
+
rendered.add(zc.record);
|
|
88
|
+
// 无覆盖行(coveredEnd 省略,与 legacy 缺 coverEnd 的渲染同形)
|
|
89
|
+
const msg = asSummaryMessage(zc.record.text);
|
|
90
|
+
const placeholder = { ...msg, timestamp: messages[0]?.timestamp ?? 0 };
|
|
91
|
+
placeholderOf.set(placeholder, zc);
|
|
92
|
+
view.push(placeholder);
|
|
93
|
+
}
|
|
94
|
+
for (let i = 0; i < messages.length; i++) {
|
|
95
|
+
const record = coverAt[i];
|
|
96
|
+
if (record === undefined) {
|
|
97
|
+
view.push(messages[i]);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (rendered.has(record))
|
|
101
|
+
continue;
|
|
102
|
+
rendered.add(record);
|
|
103
|
+
const run = applied.find((r) => r.record === record);
|
|
104
|
+
const placeholder = renderPlaceholder(record, messages[firstIndex.get(record)], lastIndex.get(record));
|
|
105
|
+
placeholderOf.set(placeholder, run);
|
|
106
|
+
view.push(placeholder);
|
|
107
|
+
}
|
|
108
|
+
return { view, applied, orphans, placeholderOf };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Render the placeholder message for a record at its first covered index.
|
|
112
|
+
*
|
|
113
|
+
* Summary records render through the single-source wrapper (marker + dynamic
|
|
114
|
+
* `[Coverage]` line, F11); degrade records render their honest mechanical text
|
|
115
|
+
* as-is. Timestamp = first covered message's timestamp — deterministic (the
|
|
116
|
+
* timestamp is not part of the LLM request anyway).
|
|
117
|
+
*
|
|
118
|
+
* @param record - the record being rendered
|
|
119
|
+
* @param at - first covered message (timestamp source)
|
|
120
|
+
* @param lastCoveredIdx - last covered raw index (coverage line)
|
|
121
|
+
* @returns placeholder message
|
|
122
|
+
*/
|
|
123
|
+
function renderPlaceholder(record, at, lastCoveredIdx) {
|
|
124
|
+
const text = record.text ?? '';
|
|
125
|
+
if (record.type === 'summary') {
|
|
126
|
+
const msg = asSummaryMessage(text, lastCoveredIdx + 1);
|
|
127
|
+
return { ...msg, timestamp: at.timestamp };
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
role: record.placeholder.role,
|
|
131
|
+
content: text,
|
|
132
|
+
timestamp: at.timestamp,
|
|
133
|
+
...(record.placeholder.source ? { metadata: { source: record.placeholder.source } } : {}),
|
|
134
|
+
};
|
|
135
|
+
}
|