@psiclawops/hypermem 0.8.5 → 0.9.1
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/ARCHITECTURE.md +2 -2
- package/CHANGELOG.md +36 -1
- package/INSTALL.md +132 -9
- package/README.md +119 -272
- package/bench/README.md +42 -0
- package/bench/data-access-bench.mjs +380 -0
- package/bin/hypermem-bench.mjs +2 -0
- package/bin/hypermem-doctor.mjs +412 -0
- package/bin/hypermem-model-audit.mjs +339 -0
- package/bin/hypermem-status.mjs +491 -70
- package/docs/DIAGNOSTICS.md +205 -0
- package/docs/INTEGRATION_VALIDATION.md +186 -0
- package/docs/MIGRATION.md +9 -6
- package/docs/MIGRATION_GUIDE.md +125 -101
- package/docs/ROADMAP.md +247 -20
- package/docs/TUNING.md +19 -5
- package/install.sh +152 -401
- package/memory-plugin/LICENSE +190 -0
- package/memory-plugin/README.md +20 -0
- package/memory-plugin/package.json +3 -3
- package/package.json +19 -4
- package/plugin/LICENSE +190 -0
- package/plugin/README.md +20 -0
- package/plugin/package.json +4 -4
- package/scripts/install-runtime.mjs +12 -1
- package/dist/background-indexer.d.ts +0 -161
- package/dist/background-indexer.d.ts.map +0 -1
- package/dist/background-indexer.js +0 -1263
- package/dist/budget-policy.d.ts +0 -22
- package/dist/budget-policy.d.ts.map +0 -1
- package/dist/budget-policy.js +0 -27
- package/dist/cache.d.ts +0 -147
- package/dist/cache.d.ts.map +0 -1
- package/dist/cache.js +0 -569
- package/dist/compaction-fence.d.ts +0 -97
- package/dist/compaction-fence.d.ts.map +0 -1
- package/dist/compaction-fence.js +0 -174
- package/dist/compositor-utils.d.ts +0 -31
- package/dist/compositor-utils.d.ts.map +0 -1
- package/dist/compositor-utils.js +0 -47
- package/dist/compositor.d.ts +0 -394
- package/dist/compositor.d.ts.map +0 -1
- package/dist/compositor.js +0 -3632
- package/dist/content-hash.d.ts +0 -43
- package/dist/content-hash.d.ts.map +0 -1
- package/dist/content-hash.js +0 -75
- package/dist/content-type-classifier.d.ts +0 -41
- package/dist/content-type-classifier.d.ts.map +0 -1
- package/dist/content-type-classifier.js +0 -181
- package/dist/context-backfill.d.ts +0 -46
- package/dist/context-backfill.d.ts.map +0 -1
- package/dist/context-backfill.js +0 -113
- package/dist/context-store.d.ts +0 -131
- package/dist/context-store.d.ts.map +0 -1
- package/dist/context-store.js +0 -279
- package/dist/contradiction-audit-store.d.ts +0 -54
- package/dist/contradiction-audit-store.d.ts.map +0 -1
- package/dist/contradiction-audit-store.js +0 -88
- package/dist/contradiction-detector.d.ts +0 -78
- package/dist/contradiction-detector.d.ts.map +0 -1
- package/dist/contradiction-detector.js +0 -362
- package/dist/contradiction-resolution-policy.d.ts +0 -21
- package/dist/contradiction-resolution-policy.d.ts.map +0 -1
- package/dist/contradiction-resolution-policy.js +0 -17
- package/dist/cross-agent.d.ts +0 -74
- package/dist/cross-agent.d.ts.map +0 -1
- package/dist/cross-agent.js +0 -271
- package/dist/db.d.ts +0 -131
- package/dist/db.d.ts.map +0 -1
- package/dist/db.js +0 -410
- package/dist/degradation.d.ts +0 -102
- package/dist/degradation.d.ts.map +0 -1
- package/dist/degradation.js +0 -141
- package/dist/desired-state-store.d.ts +0 -100
- package/dist/desired-state-store.d.ts.map +0 -1
- package/dist/desired-state-store.js +0 -222
- package/dist/doc-chunk-store.d.ts +0 -140
- package/dist/doc-chunk-store.d.ts.map +0 -1
- package/dist/doc-chunk-store.js +0 -391
- package/dist/doc-chunker.d.ts +0 -99
- package/dist/doc-chunker.d.ts.map +0 -1
- package/dist/doc-chunker.js +0 -324
- package/dist/dreaming-promoter.d.ts +0 -124
- package/dist/dreaming-promoter.d.ts.map +0 -1
- package/dist/dreaming-promoter.js +0 -447
- package/dist/episode-store.d.ts +0 -49
- package/dist/episode-store.d.ts.map +0 -1
- package/dist/episode-store.js +0 -135
- package/dist/expertise-store.d.ts +0 -129
- package/dist/expertise-store.d.ts.map +0 -1
- package/dist/expertise-store.js +0 -342
- package/dist/fact-store.d.ts +0 -90
- package/dist/fact-store.d.ts.map +0 -1
- package/dist/fact-store.js +0 -283
- package/dist/fleet-store.d.ts +0 -144
- package/dist/fleet-store.d.ts.map +0 -1
- package/dist/fleet-store.js +0 -276
- package/dist/fos-mod.d.ts +0 -178
- package/dist/fos-mod.d.ts.map +0 -1
- package/dist/fos-mod.js +0 -416
- package/dist/hybrid-retrieval.d.ts +0 -64
- package/dist/hybrid-retrieval.d.ts.map +0 -1
- package/dist/hybrid-retrieval.js +0 -344
- package/dist/image-eviction.d.ts +0 -49
- package/dist/image-eviction.d.ts.map +0 -1
- package/dist/image-eviction.js +0 -251
- package/dist/index.d.ts +0 -718
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -1469
- package/dist/keystone-scorer.d.ts +0 -51
- package/dist/keystone-scorer.d.ts.map +0 -1
- package/dist/keystone-scorer.js +0 -52
- package/dist/knowledge-graph.d.ts +0 -110
- package/dist/knowledge-graph.d.ts.map +0 -1
- package/dist/knowledge-graph.js +0 -305
- package/dist/knowledge-lint.d.ts +0 -31
- package/dist/knowledge-lint.d.ts.map +0 -1
- package/dist/knowledge-lint.js +0 -155
- package/dist/knowledge-store.d.ts +0 -72
- package/dist/knowledge-store.d.ts.map +0 -1
- package/dist/knowledge-store.js +0 -247
- package/dist/library-schema.d.ts +0 -27
- package/dist/library-schema.d.ts.map +0 -1
- package/dist/library-schema.js +0 -1343
- package/dist/message-store.d.ts +0 -181
- package/dist/message-store.d.ts.map +0 -1
- package/dist/message-store.js +0 -573
- package/dist/metrics-dashboard.d.ts +0 -114
- package/dist/metrics-dashboard.d.ts.map +0 -1
- package/dist/metrics-dashboard.js +0 -260
- package/dist/obsidian-exporter.d.ts +0 -57
- package/dist/obsidian-exporter.d.ts.map +0 -1
- package/dist/obsidian-exporter.js +0 -274
- package/dist/obsidian-watcher.d.ts +0 -147
- package/dist/obsidian-watcher.d.ts.map +0 -1
- package/dist/obsidian-watcher.js +0 -403
- package/dist/open-domain.d.ts +0 -46
- package/dist/open-domain.d.ts.map +0 -1
- package/dist/open-domain.js +0 -125
- package/dist/preference-store.d.ts +0 -54
- package/dist/preference-store.d.ts.map +0 -1
- package/dist/preference-store.js +0 -109
- package/dist/preservation-gate.d.ts +0 -82
- package/dist/preservation-gate.d.ts.map +0 -1
- package/dist/preservation-gate.js +0 -150
- package/dist/proactive-pass.d.ts +0 -63
- package/dist/proactive-pass.d.ts.map +0 -1
- package/dist/proactive-pass.js +0 -293
- package/dist/profiles.d.ts +0 -46
- package/dist/profiles.d.ts.map +0 -1
- package/dist/profiles.js +0 -262
- package/dist/provider-translator.d.ts +0 -50
- package/dist/provider-translator.d.ts.map +0 -1
- package/dist/provider-translator.js +0 -403
- package/dist/rate-limiter.d.ts +0 -76
- package/dist/rate-limiter.d.ts.map +0 -1
- package/dist/rate-limiter.js +0 -179
- package/dist/repair-tool-pairs.d.ts +0 -38
- package/dist/repair-tool-pairs.d.ts.map +0 -1
- package/dist/repair-tool-pairs.js +0 -209
- package/dist/replay-recovery.d.ts +0 -29
- package/dist/replay-recovery.d.ts.map +0 -1
- package/dist/replay-recovery.js +0 -82
- package/dist/reranker.d.ts +0 -95
- package/dist/reranker.d.ts.map +0 -1
- package/dist/reranker.js +0 -308
- package/dist/retrieval-policy.d.ts +0 -51
- package/dist/retrieval-policy.d.ts.map +0 -1
- package/dist/retrieval-policy.js +0 -77
- package/dist/schema.d.ts +0 -15
- package/dist/schema.d.ts.map +0 -1
- package/dist/schema.js +0 -300
- package/dist/secret-scanner.d.ts +0 -51
- package/dist/secret-scanner.d.ts.map +0 -1
- package/dist/secret-scanner.js +0 -248
- package/dist/seed.d.ts +0 -108
- package/dist/seed.d.ts.map +0 -1
- package/dist/seed.js +0 -177
- package/dist/session-flusher.d.ts +0 -53
- package/dist/session-flusher.d.ts.map +0 -1
- package/dist/session-flusher.js +0 -69
- package/dist/session-topic-map.d.ts +0 -41
- package/dist/session-topic-map.d.ts.map +0 -1
- package/dist/session-topic-map.js +0 -77
- package/dist/spawn-context.d.ts +0 -54
- package/dist/spawn-context.d.ts.map +0 -1
- package/dist/spawn-context.js +0 -159
- package/dist/system-store.d.ts +0 -73
- package/dist/system-store.d.ts.map +0 -1
- package/dist/system-store.js +0 -182
- package/dist/temporal-store.d.ts +0 -81
- package/dist/temporal-store.d.ts.map +0 -1
- package/dist/temporal-store.js +0 -149
- package/dist/tool-artifact-store.d.ts +0 -98
- package/dist/tool-artifact-store.d.ts.map +0 -1
- package/dist/tool-artifact-store.js +0 -244
- package/dist/topic-detector.d.ts +0 -35
- package/dist/topic-detector.d.ts.map +0 -1
- package/dist/topic-detector.js +0 -249
- package/dist/topic-store.d.ts +0 -51
- package/dist/topic-store.d.ts.map +0 -1
- package/dist/topic-store.js +0 -175
- package/dist/topic-synthesizer.d.ts +0 -51
- package/dist/topic-synthesizer.d.ts.map +0 -1
- package/dist/topic-synthesizer.js +0 -316
- package/dist/trigger-registry.d.ts +0 -63
- package/dist/trigger-registry.d.ts.map +0 -1
- package/dist/trigger-registry.js +0 -163
- package/dist/types.d.ts +0 -815
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -9
- package/dist/vector-store.d.ts +0 -180
- package/dist/vector-store.d.ts.map +0 -1
- package/dist/vector-store.js +0 -1041
- package/dist/version.d.ts +0 -34
- package/dist/version.d.ts.map +0 -1
- package/dist/version.js +0 -34
- package/dist/wiki-page-emitter.d.ts +0 -65
- package/dist/wiki-page-emitter.d.ts.map +0 -1
- package/dist/wiki-page-emitter.js +0 -258
- package/dist/work-store.d.ts +0 -112
- package/dist/work-store.d.ts.map +0 -1
- package/dist/work-store.js +0 -273
- package/memory-plugin/dist/index.d.ts +0 -24
- package/memory-plugin/dist/index.d.ts.map +0 -1
- package/memory-plugin/dist/index.js +0 -300
- package/memory-plugin/dist/index.js.map +0 -1
- package/node_modules/sqlite-vec/README.md +0 -1
- package/node_modules/sqlite-vec/index.cjs +0 -46
- package/node_modules/sqlite-vec/index.d.ts +0 -17
- package/node_modules/sqlite-vec/index.mjs +0 -47
- package/node_modules/sqlite-vec/package.json +0 -1
- package/node_modules/sqlite-vec-linux-x64/README.md +0 -1
- package/node_modules/sqlite-vec-linux-x64/package.json +0 -1
- package/node_modules/sqlite-vec-linux-x64/vec0.so +0 -0
- package/node_modules/zod/LICENSE +0 -21
- package/node_modules/zod/README.md +0 -208
- package/node_modules/zod/index.cjs +0 -33
- package/node_modules/zod/index.d.cts +0 -4
- package/node_modules/zod/index.d.ts +0 -4
- package/node_modules/zod/index.js +0 -4
- package/node_modules/zod/locales/index.cjs +0 -17
- package/node_modules/zod/locales/index.d.cts +0 -1
- package/node_modules/zod/locales/index.d.ts +0 -1
- package/node_modules/zod/locales/index.js +0 -1
- package/node_modules/zod/locales/package.json +0 -6
- package/node_modules/zod/mini/index.cjs +0 -32
- package/node_modules/zod/mini/index.d.cts +0 -3
- package/node_modules/zod/mini/index.d.ts +0 -3
- package/node_modules/zod/mini/index.js +0 -3
- package/node_modules/zod/mini/package.json +0 -6
- package/node_modules/zod/package.json +0 -135
- package/node_modules/zod/src/index.ts +0 -4
- package/node_modules/zod/src/locales/index.ts +0 -1
- package/node_modules/zod/src/mini/index.ts +0 -3
- package/node_modules/zod/src/v3/ZodError.ts +0 -330
- package/node_modules/zod/src/v3/benchmarks/datetime.ts +0 -58
- package/node_modules/zod/src/v3/benchmarks/discriminatedUnion.ts +0 -80
- package/node_modules/zod/src/v3/benchmarks/index.ts +0 -59
- package/node_modules/zod/src/v3/benchmarks/ipv4.ts +0 -57
- package/node_modules/zod/src/v3/benchmarks/object.ts +0 -69
- package/node_modules/zod/src/v3/benchmarks/primitives.ts +0 -162
- package/node_modules/zod/src/v3/benchmarks/realworld.ts +0 -63
- package/node_modules/zod/src/v3/benchmarks/string.ts +0 -55
- package/node_modules/zod/src/v3/benchmarks/union.ts +0 -80
- package/node_modules/zod/src/v3/errors.ts +0 -13
- package/node_modules/zod/src/v3/external.ts +0 -6
- package/node_modules/zod/src/v3/helpers/enumUtil.ts +0 -17
- package/node_modules/zod/src/v3/helpers/errorUtil.ts +0 -8
- package/node_modules/zod/src/v3/helpers/parseUtil.ts +0 -176
- package/node_modules/zod/src/v3/helpers/partialUtil.ts +0 -34
- package/node_modules/zod/src/v3/helpers/typeAliases.ts +0 -2
- package/node_modules/zod/src/v3/helpers/util.ts +0 -224
- package/node_modules/zod/src/v3/index.ts +0 -4
- package/node_modules/zod/src/v3/locales/en.ts +0 -124
- package/node_modules/zod/src/v3/standard-schema.ts +0 -113
- package/node_modules/zod/src/v3/tests/Mocker.ts +0 -54
- package/node_modules/zod/src/v3/tests/all-errors.test.ts +0 -157
- package/node_modules/zod/src/v3/tests/anyunknown.test.ts +0 -28
- package/node_modules/zod/src/v3/tests/array.test.ts +0 -71
- package/node_modules/zod/src/v3/tests/async-parsing.test.ts +0 -388
- package/node_modules/zod/src/v3/tests/async-refinements.test.ts +0 -46
- package/node_modules/zod/src/v3/tests/base.test.ts +0 -29
- package/node_modules/zod/src/v3/tests/bigint.test.ts +0 -55
- package/node_modules/zod/src/v3/tests/branded.test.ts +0 -53
- package/node_modules/zod/src/v3/tests/catch.test.ts +0 -220
- package/node_modules/zod/src/v3/tests/coerce.test.ts +0 -133
- package/node_modules/zod/src/v3/tests/complex.test.ts +0 -70
- package/node_modules/zod/src/v3/tests/custom.test.ts +0 -31
- package/node_modules/zod/src/v3/tests/date.test.ts +0 -32
- package/node_modules/zod/src/v3/tests/deepmasking.test.ts +0 -186
- package/node_modules/zod/src/v3/tests/default.test.ts +0 -112
- package/node_modules/zod/src/v3/tests/description.test.ts +0 -33
- package/node_modules/zod/src/v3/tests/discriminated-unions.test.ts +0 -315
- package/node_modules/zod/src/v3/tests/enum.test.ts +0 -80
- package/node_modules/zod/src/v3/tests/error.test.ts +0 -551
- package/node_modules/zod/src/v3/tests/firstparty.test.ts +0 -87
- package/node_modules/zod/src/v3/tests/firstpartyschematypes.test.ts +0 -21
- package/node_modules/zod/src/v3/tests/function.test.ts +0 -261
- package/node_modules/zod/src/v3/tests/generics.test.ts +0 -48
- package/node_modules/zod/src/v3/tests/instanceof.test.ts +0 -37
- package/node_modules/zod/src/v3/tests/intersection.test.ts +0 -110
- package/node_modules/zod/src/v3/tests/language-server.source.ts +0 -76
- package/node_modules/zod/src/v3/tests/language-server.test.ts +0 -207
- package/node_modules/zod/src/v3/tests/literal.test.ts +0 -36
- package/node_modules/zod/src/v3/tests/map.test.ts +0 -110
- package/node_modules/zod/src/v3/tests/masking.test.ts +0 -4
- package/node_modules/zod/src/v3/tests/mocker.test.ts +0 -19
- package/node_modules/zod/src/v3/tests/nan.test.ts +0 -24
- package/node_modules/zod/src/v3/tests/nativeEnum.test.ts +0 -87
- package/node_modules/zod/src/v3/tests/nullable.test.ts +0 -42
- package/node_modules/zod/src/v3/tests/number.test.ts +0 -176
- package/node_modules/zod/src/v3/tests/object-augmentation.test.ts +0 -29
- package/node_modules/zod/src/v3/tests/object-in-es5-env.test.ts +0 -29
- package/node_modules/zod/src/v3/tests/object.test.ts +0 -434
- package/node_modules/zod/src/v3/tests/optional.test.ts +0 -42
- package/node_modules/zod/src/v3/tests/parseUtil.test.ts +0 -23
- package/node_modules/zod/src/v3/tests/parser.test.ts +0 -41
- package/node_modules/zod/src/v3/tests/partials.test.ts +0 -243
- package/node_modules/zod/src/v3/tests/pickomit.test.ts +0 -111
- package/node_modules/zod/src/v3/tests/pipeline.test.ts +0 -29
- package/node_modules/zod/src/v3/tests/preprocess.test.ts +0 -186
- package/node_modules/zod/src/v3/tests/primitive.test.ts +0 -440
- package/node_modules/zod/src/v3/tests/promise.test.ts +0 -90
- package/node_modules/zod/src/v3/tests/readonly.test.ts +0 -194
- package/node_modules/zod/src/v3/tests/record.test.ts +0 -171
- package/node_modules/zod/src/v3/tests/recursive.test.ts +0 -197
- package/node_modules/zod/src/v3/tests/refine.test.ts +0 -313
- package/node_modules/zod/src/v3/tests/safeparse.test.ts +0 -27
- package/node_modules/zod/src/v3/tests/set.test.ts +0 -142
- package/node_modules/zod/src/v3/tests/standard-schema.test.ts +0 -83
- package/node_modules/zod/src/v3/tests/string.test.ts +0 -916
- package/node_modules/zod/src/v3/tests/transformer.test.ts +0 -233
- package/node_modules/zod/src/v3/tests/tuple.test.ts +0 -90
- package/node_modules/zod/src/v3/tests/unions.test.ts +0 -57
- package/node_modules/zod/src/v3/tests/validations.test.ts +0 -133
- package/node_modules/zod/src/v3/tests/void.test.ts +0 -15
- package/node_modules/zod/src/v3/types.ts +0 -5138
- package/node_modules/zod/src/v4/classic/checks.ts +0 -32
- package/node_modules/zod/src/v4/classic/coerce.ts +0 -27
- package/node_modules/zod/src/v4/classic/compat.ts +0 -70
- package/node_modules/zod/src/v4/classic/errors.ts +0 -82
- package/node_modules/zod/src/v4/classic/external.ts +0 -51
- package/node_modules/zod/src/v4/classic/from-json-schema.ts +0 -643
- package/node_modules/zod/src/v4/classic/index.ts +0 -5
- package/node_modules/zod/src/v4/classic/iso.ts +0 -90
- package/node_modules/zod/src/v4/classic/parse.ts +0 -82
- package/node_modules/zod/src/v4/classic/schemas.ts +0 -2409
- package/node_modules/zod/src/v4/classic/tests/anyunknown.test.ts +0 -26
- package/node_modules/zod/src/v4/classic/tests/apply.test.ts +0 -59
- package/node_modules/zod/src/v4/classic/tests/array.test.ts +0 -264
- package/node_modules/zod/src/v4/classic/tests/assignability.test.ts +0 -210
- package/node_modules/zod/src/v4/classic/tests/async-parsing.test.ts +0 -381
- package/node_modules/zod/src/v4/classic/tests/async-refinements.test.ts +0 -68
- package/node_modules/zod/src/v4/classic/tests/base.test.ts +0 -7
- package/node_modules/zod/src/v4/classic/tests/bigint.test.ts +0 -54
- package/node_modules/zod/src/v4/classic/tests/brand.test.ts +0 -106
- package/node_modules/zod/src/v4/classic/tests/catch.test.ts +0 -276
- package/node_modules/zod/src/v4/classic/tests/coalesce.test.ts +0 -20
- package/node_modules/zod/src/v4/classic/tests/codec-examples.test.ts +0 -573
- package/node_modules/zod/src/v4/classic/tests/codec.test.ts +0 -562
- package/node_modules/zod/src/v4/classic/tests/coerce.test.ts +0 -160
- package/node_modules/zod/src/v4/classic/tests/continuability.test.ts +0 -374
- package/node_modules/zod/src/v4/classic/tests/custom.test.ts +0 -40
- package/node_modules/zod/src/v4/classic/tests/date.test.ts +0 -62
- package/node_modules/zod/src/v4/classic/tests/datetime.test.ts +0 -302
- package/node_modules/zod/src/v4/classic/tests/default.test.ts +0 -365
- package/node_modules/zod/src/v4/classic/tests/describe-meta-checks.test.ts +0 -27
- package/node_modules/zod/src/v4/classic/tests/description.test.ts +0 -32
- package/node_modules/zod/src/v4/classic/tests/discriminated-unions.test.ts +0 -661
- package/node_modules/zod/src/v4/classic/tests/enum.test.ts +0 -285
- package/node_modules/zod/src/v4/classic/tests/error-utils.test.ts +0 -595
- package/node_modules/zod/src/v4/classic/tests/error.test.ts +0 -711
- package/node_modules/zod/src/v4/classic/tests/file.test.ts +0 -96
- package/node_modules/zod/src/v4/classic/tests/firstparty.test.ts +0 -179
- package/node_modules/zod/src/v4/classic/tests/fix-json-issue.test.ts +0 -26
- package/node_modules/zod/src/v4/classic/tests/from-json-schema.test.ts +0 -734
- package/node_modules/zod/src/v4/classic/tests/function.test.ts +0 -360
- package/node_modules/zod/src/v4/classic/tests/generics.test.ts +0 -72
- package/node_modules/zod/src/v4/classic/tests/hash.test.ts +0 -68
- package/node_modules/zod/src/v4/classic/tests/index.test.ts +0 -939
- package/node_modules/zod/src/v4/classic/tests/instanceof.test.ts +0 -60
- package/node_modules/zod/src/v4/classic/tests/intersection.test.ts +0 -198
- package/node_modules/zod/src/v4/classic/tests/json.test.ts +0 -109
- package/node_modules/zod/src/v4/classic/tests/lazy.test.ts +0 -227
- package/node_modules/zod/src/v4/classic/tests/literal.test.ts +0 -117
- package/node_modules/zod/src/v4/classic/tests/map.test.ts +0 -330
- package/node_modules/zod/src/v4/classic/tests/nan.test.ts +0 -21
- package/node_modules/zod/src/v4/classic/tests/nested-refine.test.ts +0 -168
- package/node_modules/zod/src/v4/classic/tests/nonoptional.test.ts +0 -101
- package/node_modules/zod/src/v4/classic/tests/nullable.test.ts +0 -22
- package/node_modules/zod/src/v4/classic/tests/number.test.ts +0 -270
- package/node_modules/zod/src/v4/classic/tests/object.test.ts +0 -640
- package/node_modules/zod/src/v4/classic/tests/optional.test.ts +0 -223
- package/node_modules/zod/src/v4/classic/tests/partial.test.ts +0 -427
- package/node_modules/zod/src/v4/classic/tests/pickomit.test.ts +0 -211
- package/node_modules/zod/src/v4/classic/tests/pipe.test.ts +0 -101
- package/node_modules/zod/src/v4/classic/tests/prefault.test.ts +0 -74
- package/node_modules/zod/src/v4/classic/tests/preprocess.test.ts +0 -282
- package/node_modules/zod/src/v4/classic/tests/primitive.test.ts +0 -175
- package/node_modules/zod/src/v4/classic/tests/promise.test.ts +0 -81
- package/node_modules/zod/src/v4/classic/tests/prototypes.test.ts +0 -23
- package/node_modules/zod/src/v4/classic/tests/readonly.test.ts +0 -252
- package/node_modules/zod/src/v4/classic/tests/record.test.ts +0 -632
- package/node_modules/zod/src/v4/classic/tests/recursive-types.test.ts +0 -582
- package/node_modules/zod/src/v4/classic/tests/refine.test.ts +0 -570
- package/node_modules/zod/src/v4/classic/tests/registries.test.ts +0 -243
- package/node_modules/zod/src/v4/classic/tests/set.test.ts +0 -181
- package/node_modules/zod/src/v4/classic/tests/standard-schema.test.ts +0 -134
- package/node_modules/zod/src/v4/classic/tests/string-formats.test.ts +0 -125
- package/node_modules/zod/src/v4/classic/tests/string.test.ts +0 -1175
- package/node_modules/zod/src/v4/classic/tests/stringbool.test.ts +0 -106
- package/node_modules/zod/src/v4/classic/tests/template-literal.test.ts +0 -771
- package/node_modules/zod/src/v4/classic/tests/to-json-schema-methods.test.ts +0 -438
- package/node_modules/zod/src/v4/classic/tests/to-json-schema.test.ts +0 -2990
- package/node_modules/zod/src/v4/classic/tests/transform.test.ts +0 -361
- package/node_modules/zod/src/v4/classic/tests/tuple.test.ts +0 -183
- package/node_modules/zod/src/v4/classic/tests/union.test.ts +0 -219
- package/node_modules/zod/src/v4/classic/tests/url.test.ts +0 -13
- package/node_modules/zod/src/v4/classic/tests/validations.test.ts +0 -283
- package/node_modules/zod/src/v4/classic/tests/void.test.ts +0 -12
- package/node_modules/zod/src/v4/core/api.ts +0 -1798
- package/node_modules/zod/src/v4/core/checks.ts +0 -1293
- package/node_modules/zod/src/v4/core/config.ts +0 -15
- package/node_modules/zod/src/v4/core/core.ts +0 -138
- package/node_modules/zod/src/v4/core/doc.ts +0 -44
- package/node_modules/zod/src/v4/core/errors.ts +0 -448
- package/node_modules/zod/src/v4/core/index.ts +0 -16
- package/node_modules/zod/src/v4/core/json-schema-generator.ts +0 -126
- package/node_modules/zod/src/v4/core/json-schema-processors.ts +0 -667
- package/node_modules/zod/src/v4/core/json-schema.ts +0 -147
- package/node_modules/zod/src/v4/core/parse.ts +0 -195
- package/node_modules/zod/src/v4/core/regexes.ts +0 -183
- package/node_modules/zod/src/v4/core/registries.ts +0 -105
- package/node_modules/zod/src/v4/core/schemas.ts +0 -4538
- package/node_modules/zod/src/v4/core/standard-schema.ts +0 -159
- package/node_modules/zod/src/v4/core/tests/extend.test.ts +0 -59
- package/node_modules/zod/src/v4/core/tests/index.test.ts +0 -46
- package/node_modules/zod/src/v4/core/tests/locales/be.test.ts +0 -124
- package/node_modules/zod/src/v4/core/tests/locales/en.test.ts +0 -22
- package/node_modules/zod/src/v4/core/tests/locales/es.test.ts +0 -181
- package/node_modules/zod/src/v4/core/tests/locales/he.test.ts +0 -379
- package/node_modules/zod/src/v4/core/tests/locales/nl.test.ts +0 -46
- package/node_modules/zod/src/v4/core/tests/locales/ru.test.ts +0 -128
- package/node_modules/zod/src/v4/core/tests/locales/tr.test.ts +0 -69
- package/node_modules/zod/src/v4/core/tests/locales/uz.test.ts +0 -83
- package/node_modules/zod/src/v4/core/tests/record-constructor.test.ts +0 -67
- package/node_modules/zod/src/v4/core/tests/recursive-tuples.test.ts +0 -45
- package/node_modules/zod/src/v4/core/to-json-schema.ts +0 -613
- package/node_modules/zod/src/v4/core/util.ts +0 -966
- package/node_modules/zod/src/v4/core/versions.ts +0 -5
- package/node_modules/zod/src/v4/core/zsf.ts +0 -323
- package/node_modules/zod/src/v4/index.ts +0 -4
- package/node_modules/zod/src/v4/locales/ar.ts +0 -115
- package/node_modules/zod/src/v4/locales/az.ts +0 -111
- package/node_modules/zod/src/v4/locales/be.ts +0 -176
- package/node_modules/zod/src/v4/locales/bg.ts +0 -128
- package/node_modules/zod/src/v4/locales/ca.ts +0 -116
- package/node_modules/zod/src/v4/locales/cs.ts +0 -118
- package/node_modules/zod/src/v4/locales/da.ts +0 -123
- package/node_modules/zod/src/v4/locales/de.ts +0 -116
- package/node_modules/zod/src/v4/locales/en.ts +0 -119
- package/node_modules/zod/src/v4/locales/eo.ts +0 -118
- package/node_modules/zod/src/v4/locales/es.ts +0 -141
- package/node_modules/zod/src/v4/locales/fa.ts +0 -126
- package/node_modules/zod/src/v4/locales/fi.ts +0 -121
- package/node_modules/zod/src/v4/locales/fr-CA.ts +0 -116
- package/node_modules/zod/src/v4/locales/fr.ts +0 -116
- package/node_modules/zod/src/v4/locales/he.ts +0 -246
- package/node_modules/zod/src/v4/locales/hu.ts +0 -117
- package/node_modules/zod/src/v4/locales/hy.ts +0 -164
- package/node_modules/zod/src/v4/locales/id.ts +0 -115
- package/node_modules/zod/src/v4/locales/index.ts +0 -49
- package/node_modules/zod/src/v4/locales/is.ts +0 -119
- package/node_modules/zod/src/v4/locales/it.ts +0 -116
- package/node_modules/zod/src/v4/locales/ja.ts +0 -114
- package/node_modules/zod/src/v4/locales/ka.ts +0 -123
- package/node_modules/zod/src/v4/locales/kh.ts +0 -7
- package/node_modules/zod/src/v4/locales/km.ts +0 -119
- package/node_modules/zod/src/v4/locales/ko.ts +0 -121
- package/node_modules/zod/src/v4/locales/lt.ts +0 -239
- package/node_modules/zod/src/v4/locales/mk.ts +0 -118
- package/node_modules/zod/src/v4/locales/ms.ts +0 -115
- package/node_modules/zod/src/v4/locales/nl.ts +0 -121
- package/node_modules/zod/src/v4/locales/no.ts +0 -116
- package/node_modules/zod/src/v4/locales/ota.ts +0 -117
- package/node_modules/zod/src/v4/locales/pl.ts +0 -118
- package/node_modules/zod/src/v4/locales/ps.ts +0 -126
- package/node_modules/zod/src/v4/locales/pt.ts +0 -116
- package/node_modules/zod/src/v4/locales/ru.ts +0 -176
- package/node_modules/zod/src/v4/locales/sl.ts +0 -118
- package/node_modules/zod/src/v4/locales/sv.ts +0 -119
- package/node_modules/zod/src/v4/locales/ta.ts +0 -118
- package/node_modules/zod/src/v4/locales/th.ts +0 -119
- package/node_modules/zod/src/v4/locales/tr.ts +0 -111
- package/node_modules/zod/src/v4/locales/ua.ts +0 -7
- package/node_modules/zod/src/v4/locales/uk.ts +0 -117
- package/node_modules/zod/src/v4/locales/ur.ts +0 -119
- package/node_modules/zod/src/v4/locales/uz.ts +0 -116
- package/node_modules/zod/src/v4/locales/vi.ts +0 -117
- package/node_modules/zod/src/v4/locales/yo.ts +0 -124
- package/node_modules/zod/src/v4/locales/zh-CN.ts +0 -116
- package/node_modules/zod/src/v4/locales/zh-TW.ts +0 -115
- package/node_modules/zod/src/v4/mini/checks.ts +0 -32
- package/node_modules/zod/src/v4/mini/coerce.ts +0 -27
- package/node_modules/zod/src/v4/mini/external.ts +0 -40
- package/node_modules/zod/src/v4/mini/index.ts +0 -3
- package/node_modules/zod/src/v4/mini/iso.ts +0 -66
- package/node_modules/zod/src/v4/mini/parse.ts +0 -14
- package/node_modules/zod/src/v4/mini/schemas.ts +0 -1916
- package/node_modules/zod/src/v4/mini/tests/apply.test.ts +0 -24
- package/node_modules/zod/src/v4/mini/tests/assignability.test.ts +0 -129
- package/node_modules/zod/src/v4/mini/tests/brand.test.ts +0 -94
- package/node_modules/zod/src/v4/mini/tests/checks.test.ts +0 -144
- package/node_modules/zod/src/v4/mini/tests/codec.test.ts +0 -529
- package/node_modules/zod/src/v4/mini/tests/computed.test.ts +0 -36
- package/node_modules/zod/src/v4/mini/tests/error.test.ts +0 -22
- package/node_modules/zod/src/v4/mini/tests/functions.test.ts +0 -5
- package/node_modules/zod/src/v4/mini/tests/index.test.ts +0 -963
- package/node_modules/zod/src/v4/mini/tests/number.test.ts +0 -95
- package/node_modules/zod/src/v4/mini/tests/object.test.ts +0 -227
- package/node_modules/zod/src/v4/mini/tests/prototypes.test.ts +0 -43
- package/node_modules/zod/src/v4/mini/tests/recursive-types.test.ts +0 -275
- package/node_modules/zod/src/v4/mini/tests/standard-schema.test.ts +0 -50
- package/node_modules/zod/src/v4/mini/tests/string.test.ts +0 -347
- package/node_modules/zod/src/v4-mini/index.ts +0 -3
- package/node_modules/zod/v3/ZodError.cjs +0 -138
- package/node_modules/zod/v3/ZodError.d.cts +0 -164
- package/node_modules/zod/v3/ZodError.d.ts +0 -164
- package/node_modules/zod/v3/ZodError.js +0 -133
- package/node_modules/zod/v3/errors.cjs +0 -17
- package/node_modules/zod/v3/errors.d.cts +0 -5
- package/node_modules/zod/v3/errors.d.ts +0 -5
- package/node_modules/zod/v3/errors.js +0 -9
- package/node_modules/zod/v3/external.cjs +0 -22
- package/node_modules/zod/v3/external.d.cts +0 -6
- package/node_modules/zod/v3/external.d.ts +0 -6
- package/node_modules/zod/v3/external.js +0 -6
- package/node_modules/zod/v3/helpers/enumUtil.cjs +0 -2
- package/node_modules/zod/v3/helpers/enumUtil.d.cts +0 -8
- package/node_modules/zod/v3/helpers/enumUtil.d.ts +0 -8
- package/node_modules/zod/v3/helpers/enumUtil.js +0 -1
- package/node_modules/zod/v3/helpers/errorUtil.cjs +0 -9
- package/node_modules/zod/v3/helpers/errorUtil.d.cts +0 -9
- package/node_modules/zod/v3/helpers/errorUtil.d.ts +0 -9
- package/node_modules/zod/v3/helpers/errorUtil.js +0 -6
- package/node_modules/zod/v3/helpers/parseUtil.cjs +0 -124
- package/node_modules/zod/v3/helpers/parseUtil.d.cts +0 -78
- package/node_modules/zod/v3/helpers/parseUtil.d.ts +0 -78
- package/node_modules/zod/v3/helpers/parseUtil.js +0 -109
- package/node_modules/zod/v3/helpers/partialUtil.cjs +0 -2
- package/node_modules/zod/v3/helpers/partialUtil.d.cts +0 -8
- package/node_modules/zod/v3/helpers/partialUtil.d.ts +0 -8
- package/node_modules/zod/v3/helpers/partialUtil.js +0 -1
- package/node_modules/zod/v3/helpers/typeAliases.cjs +0 -2
- package/node_modules/zod/v3/helpers/typeAliases.d.cts +0 -2
- package/node_modules/zod/v3/helpers/typeAliases.d.ts +0 -2
- package/node_modules/zod/v3/helpers/typeAliases.js +0 -1
- package/node_modules/zod/v3/helpers/util.cjs +0 -137
- package/node_modules/zod/v3/helpers/util.d.cts +0 -85
- package/node_modules/zod/v3/helpers/util.d.ts +0 -85
- package/node_modules/zod/v3/helpers/util.js +0 -133
- package/node_modules/zod/v3/index.cjs +0 -33
- package/node_modules/zod/v3/index.d.cts +0 -4
- package/node_modules/zod/v3/index.d.ts +0 -4
- package/node_modules/zod/v3/index.js +0 -4
- package/node_modules/zod/v3/locales/en.cjs +0 -112
- package/node_modules/zod/v3/locales/en.d.cts +0 -3
- package/node_modules/zod/v3/locales/en.d.ts +0 -3
- package/node_modules/zod/v3/locales/en.js +0 -109
- package/node_modules/zod/v3/package.json +0 -6
- package/node_modules/zod/v3/standard-schema.cjs +0 -2
- package/node_modules/zod/v3/standard-schema.d.cts +0 -102
- package/node_modules/zod/v3/standard-schema.d.ts +0 -102
- package/node_modules/zod/v3/standard-schema.js +0 -1
- package/node_modules/zod/v3/types.cjs +0 -3777
- package/node_modules/zod/v3/types.d.cts +0 -1034
- package/node_modules/zod/v3/types.d.ts +0 -1034
- package/node_modules/zod/v3/types.js +0 -3695
- package/node_modules/zod/v4/classic/checks.cjs +0 -33
- package/node_modules/zod/v4/classic/checks.d.cts +0 -1
- package/node_modules/zod/v4/classic/checks.d.ts +0 -1
- package/node_modules/zod/v4/classic/checks.js +0 -1
- package/node_modules/zod/v4/classic/coerce.cjs +0 -47
- package/node_modules/zod/v4/classic/coerce.d.cts +0 -17
- package/node_modules/zod/v4/classic/coerce.d.ts +0 -17
- package/node_modules/zod/v4/classic/coerce.js +0 -17
- package/node_modules/zod/v4/classic/compat.cjs +0 -61
- package/node_modules/zod/v4/classic/compat.d.cts +0 -50
- package/node_modules/zod/v4/classic/compat.d.ts +0 -50
- package/node_modules/zod/v4/classic/compat.js +0 -31
- package/node_modules/zod/v4/classic/errors.cjs +0 -74
- package/node_modules/zod/v4/classic/errors.d.cts +0 -30
- package/node_modules/zod/v4/classic/errors.d.ts +0 -30
- package/node_modules/zod/v4/classic/errors.js +0 -48
- package/node_modules/zod/v4/classic/external.cjs +0 -73
- package/node_modules/zod/v4/classic/external.d.cts +0 -15
- package/node_modules/zod/v4/classic/external.d.ts +0 -15
- package/node_modules/zod/v4/classic/external.js +0 -20
- package/node_modules/zod/v4/classic/from-json-schema.cjs +0 -610
- package/node_modules/zod/v4/classic/from-json-schema.d.cts +0 -12
- package/node_modules/zod/v4/classic/from-json-schema.d.ts +0 -12
- package/node_modules/zod/v4/classic/from-json-schema.js +0 -584
- package/node_modules/zod/v4/classic/index.cjs +0 -33
- package/node_modules/zod/v4/classic/index.d.cts +0 -4
- package/node_modules/zod/v4/classic/index.d.ts +0 -4
- package/node_modules/zod/v4/classic/index.js +0 -4
- package/node_modules/zod/v4/classic/iso.cjs +0 -60
- package/node_modules/zod/v4/classic/iso.d.cts +0 -22
- package/node_modules/zod/v4/classic/iso.d.ts +0 -22
- package/node_modules/zod/v4/classic/iso.js +0 -30
- package/node_modules/zod/v4/classic/package.json +0 -6
- package/node_modules/zod/v4/classic/parse.cjs +0 -41
- package/node_modules/zod/v4/classic/parse.d.cts +0 -31
- package/node_modules/zod/v4/classic/parse.d.ts +0 -31
- package/node_modules/zod/v4/classic/parse.js +0 -15
- package/node_modules/zod/v4/classic/schemas.cjs +0 -1272
- package/node_modules/zod/v4/classic/schemas.d.cts +0 -739
- package/node_modules/zod/v4/classic/schemas.d.ts +0 -739
- package/node_modules/zod/v4/classic/schemas.js +0 -1157
- package/node_modules/zod/v4/core/api.cjs +0 -1222
- package/node_modules/zod/v4/core/api.d.cts +0 -304
- package/node_modules/zod/v4/core/api.d.ts +0 -304
- package/node_modules/zod/v4/core/api.js +0 -1082
- package/node_modules/zod/v4/core/checks.cjs +0 -601
- package/node_modules/zod/v4/core/checks.d.cts +0 -278
- package/node_modules/zod/v4/core/checks.d.ts +0 -278
- package/node_modules/zod/v4/core/checks.js +0 -575
- package/node_modules/zod/v4/core/core.cjs +0 -83
- package/node_modules/zod/v4/core/core.d.cts +0 -70
- package/node_modules/zod/v4/core/core.d.ts +0 -70
- package/node_modules/zod/v4/core/core.js +0 -76
- package/node_modules/zod/v4/core/doc.cjs +0 -39
- package/node_modules/zod/v4/core/doc.d.cts +0 -14
- package/node_modules/zod/v4/core/doc.d.ts +0 -14
- package/node_modules/zod/v4/core/doc.js +0 -35
- package/node_modules/zod/v4/core/errors.cjs +0 -213
- package/node_modules/zod/v4/core/errors.d.cts +0 -220
- package/node_modules/zod/v4/core/errors.d.ts +0 -220
- package/node_modules/zod/v4/core/errors.js +0 -182
- package/node_modules/zod/v4/core/index.cjs +0 -47
- package/node_modules/zod/v4/core/index.d.cts +0 -16
- package/node_modules/zod/v4/core/index.d.ts +0 -16
- package/node_modules/zod/v4/core/index.js +0 -16
- package/node_modules/zod/v4/core/json-schema-generator.cjs +0 -99
- package/node_modules/zod/v4/core/json-schema-generator.d.cts +0 -65
- package/node_modules/zod/v4/core/json-schema-generator.d.ts +0 -65
- package/node_modules/zod/v4/core/json-schema-generator.js +0 -95
- package/node_modules/zod/v4/core/json-schema-processors.cjs +0 -648
- package/node_modules/zod/v4/core/json-schema-processors.d.cts +0 -49
- package/node_modules/zod/v4/core/json-schema-processors.d.ts +0 -49
- package/node_modules/zod/v4/core/json-schema-processors.js +0 -605
- package/node_modules/zod/v4/core/json-schema.cjs +0 -2
- package/node_modules/zod/v4/core/json-schema.d.cts +0 -88
- package/node_modules/zod/v4/core/json-schema.d.ts +0 -88
- package/node_modules/zod/v4/core/json-schema.js +0 -1
- package/node_modules/zod/v4/core/package.json +0 -6
- package/node_modules/zod/v4/core/parse.cjs +0 -131
- package/node_modules/zod/v4/core/parse.d.cts +0 -49
- package/node_modules/zod/v4/core/parse.d.ts +0 -49
- package/node_modules/zod/v4/core/parse.js +0 -93
- package/node_modules/zod/v4/core/regexes.cjs +0 -166
- package/node_modules/zod/v4/core/regexes.d.cts +0 -79
- package/node_modules/zod/v4/core/regexes.d.ts +0 -79
- package/node_modules/zod/v4/core/regexes.js +0 -133
- package/node_modules/zod/v4/core/registries.cjs +0 -56
- package/node_modules/zod/v4/core/registries.d.cts +0 -35
- package/node_modules/zod/v4/core/registries.d.ts +0 -35
- package/node_modules/zod/v4/core/registries.js +0 -51
- package/node_modules/zod/v4/core/schemas.cjs +0 -2124
- package/node_modules/zod/v4/core/schemas.d.cts +0 -1146
- package/node_modules/zod/v4/core/schemas.d.ts +0 -1146
- package/node_modules/zod/v4/core/schemas.js +0 -2093
- package/node_modules/zod/v4/core/standard-schema.cjs +0 -2
- package/node_modules/zod/v4/core/standard-schema.d.cts +0 -126
- package/node_modules/zod/v4/core/standard-schema.d.ts +0 -126
- package/node_modules/zod/v4/core/standard-schema.js +0 -1
- package/node_modules/zod/v4/core/to-json-schema.cjs +0 -446
- package/node_modules/zod/v4/core/to-json-schema.d.cts +0 -114
- package/node_modules/zod/v4/core/to-json-schema.d.ts +0 -114
- package/node_modules/zod/v4/core/to-json-schema.js +0 -437
- package/node_modules/zod/v4/core/util.cjs +0 -710
- package/node_modules/zod/v4/core/util.d.cts +0 -199
- package/node_modules/zod/v4/core/util.d.ts +0 -199
- package/node_modules/zod/v4/core/util.js +0 -651
- package/node_modules/zod/v4/core/versions.cjs +0 -8
- package/node_modules/zod/v4/core/versions.d.cts +0 -5
- package/node_modules/zod/v4/core/versions.d.ts +0 -5
- package/node_modules/zod/v4/core/versions.js +0 -5
- package/node_modules/zod/v4/index.cjs +0 -22
- package/node_modules/zod/v4/index.d.cts +0 -3
- package/node_modules/zod/v4/index.d.ts +0 -3
- package/node_modules/zod/v4/index.js +0 -3
- package/node_modules/zod/v4/locales/ar.cjs +0 -133
- package/node_modules/zod/v4/locales/ar.d.cts +0 -5
- package/node_modules/zod/v4/locales/ar.d.ts +0 -4
- package/node_modules/zod/v4/locales/ar.js +0 -106
- package/node_modules/zod/v4/locales/az.cjs +0 -132
- package/node_modules/zod/v4/locales/az.d.cts +0 -5
- package/node_modules/zod/v4/locales/az.d.ts +0 -4
- package/node_modules/zod/v4/locales/az.js +0 -105
- package/node_modules/zod/v4/locales/be.cjs +0 -183
- package/node_modules/zod/v4/locales/be.d.cts +0 -5
- package/node_modules/zod/v4/locales/be.d.ts +0 -4
- package/node_modules/zod/v4/locales/be.js +0 -156
- package/node_modules/zod/v4/locales/bg.cjs +0 -147
- package/node_modules/zod/v4/locales/bg.d.cts +0 -5
- package/node_modules/zod/v4/locales/bg.d.ts +0 -4
- package/node_modules/zod/v4/locales/bg.js +0 -120
- package/node_modules/zod/v4/locales/ca.cjs +0 -134
- package/node_modules/zod/v4/locales/ca.d.cts +0 -5
- package/node_modules/zod/v4/locales/ca.d.ts +0 -4
- package/node_modules/zod/v4/locales/ca.js +0 -107
- package/node_modules/zod/v4/locales/cs.cjs +0 -138
- package/node_modules/zod/v4/locales/cs.d.cts +0 -5
- package/node_modules/zod/v4/locales/cs.d.ts +0 -4
- package/node_modules/zod/v4/locales/cs.js +0 -111
- package/node_modules/zod/v4/locales/da.cjs +0 -142
- package/node_modules/zod/v4/locales/da.d.cts +0 -5
- package/node_modules/zod/v4/locales/da.d.ts +0 -4
- package/node_modules/zod/v4/locales/da.js +0 -115
- package/node_modules/zod/v4/locales/de.cjs +0 -135
- package/node_modules/zod/v4/locales/de.d.cts +0 -5
- package/node_modules/zod/v4/locales/de.d.ts +0 -4
- package/node_modules/zod/v4/locales/de.js +0 -108
- package/node_modules/zod/v4/locales/en.cjs +0 -136
- package/node_modules/zod/v4/locales/en.d.cts +0 -5
- package/node_modules/zod/v4/locales/en.d.ts +0 -4
- package/node_modules/zod/v4/locales/en.js +0 -109
- package/node_modules/zod/v4/locales/eo.cjs +0 -136
- package/node_modules/zod/v4/locales/eo.d.cts +0 -5
- package/node_modules/zod/v4/locales/eo.d.ts +0 -4
- package/node_modules/zod/v4/locales/eo.js +0 -109
- package/node_modules/zod/v4/locales/es.cjs +0 -159
- package/node_modules/zod/v4/locales/es.d.cts +0 -5
- package/node_modules/zod/v4/locales/es.d.ts +0 -4
- package/node_modules/zod/v4/locales/es.js +0 -132
- package/node_modules/zod/v4/locales/fa.cjs +0 -141
- package/node_modules/zod/v4/locales/fa.d.cts +0 -5
- package/node_modules/zod/v4/locales/fa.d.ts +0 -4
- package/node_modules/zod/v4/locales/fa.js +0 -114
- package/node_modules/zod/v4/locales/fi.cjs +0 -139
- package/node_modules/zod/v4/locales/fi.d.cts +0 -5
- package/node_modules/zod/v4/locales/fi.d.ts +0 -4
- package/node_modules/zod/v4/locales/fi.js +0 -112
- package/node_modules/zod/v4/locales/fr-CA.cjs +0 -134
- package/node_modules/zod/v4/locales/fr-CA.d.cts +0 -5
- package/node_modules/zod/v4/locales/fr-CA.d.ts +0 -4
- package/node_modules/zod/v4/locales/fr-CA.js +0 -107
- package/node_modules/zod/v4/locales/fr.cjs +0 -135
- package/node_modules/zod/v4/locales/fr.d.cts +0 -5
- package/node_modules/zod/v4/locales/fr.d.ts +0 -4
- package/node_modules/zod/v4/locales/fr.js +0 -108
- package/node_modules/zod/v4/locales/he.cjs +0 -241
- package/node_modules/zod/v4/locales/he.d.cts +0 -5
- package/node_modules/zod/v4/locales/he.d.ts +0 -4
- package/node_modules/zod/v4/locales/he.js +0 -214
- package/node_modules/zod/v4/locales/hu.cjs +0 -135
- package/node_modules/zod/v4/locales/hu.d.cts +0 -5
- package/node_modules/zod/v4/locales/hu.d.ts +0 -4
- package/node_modules/zod/v4/locales/hu.js +0 -108
- package/node_modules/zod/v4/locales/hy.cjs +0 -174
- package/node_modules/zod/v4/locales/hy.d.cts +0 -5
- package/node_modules/zod/v4/locales/hy.d.ts +0 -4
- package/node_modules/zod/v4/locales/hy.js +0 -147
- package/node_modules/zod/v4/locales/id.cjs +0 -133
- package/node_modules/zod/v4/locales/id.d.cts +0 -5
- package/node_modules/zod/v4/locales/id.d.ts +0 -4
- package/node_modules/zod/v4/locales/id.js +0 -106
- package/node_modules/zod/v4/locales/index.cjs +0 -104
- package/node_modules/zod/v4/locales/index.d.cts +0 -49
- package/node_modules/zod/v4/locales/index.d.ts +0 -49
- package/node_modules/zod/v4/locales/index.js +0 -49
- package/node_modules/zod/v4/locales/is.cjs +0 -136
- package/node_modules/zod/v4/locales/is.d.cts +0 -5
- package/node_modules/zod/v4/locales/is.d.ts +0 -4
- package/node_modules/zod/v4/locales/is.js +0 -109
- package/node_modules/zod/v4/locales/it.cjs +0 -135
- package/node_modules/zod/v4/locales/it.d.cts +0 -5
- package/node_modules/zod/v4/locales/it.d.ts +0 -4
- package/node_modules/zod/v4/locales/it.js +0 -108
- package/node_modules/zod/v4/locales/ja.cjs +0 -134
- package/node_modules/zod/v4/locales/ja.d.cts +0 -5
- package/node_modules/zod/v4/locales/ja.d.ts +0 -4
- package/node_modules/zod/v4/locales/ja.js +0 -107
- package/node_modules/zod/v4/locales/ka.cjs +0 -139
- package/node_modules/zod/v4/locales/ka.d.cts +0 -5
- package/node_modules/zod/v4/locales/ka.d.ts +0 -4
- package/node_modules/zod/v4/locales/ka.js +0 -112
- package/node_modules/zod/v4/locales/kh.cjs +0 -12
- package/node_modules/zod/v4/locales/kh.d.cts +0 -5
- package/node_modules/zod/v4/locales/kh.d.ts +0 -5
- package/node_modules/zod/v4/locales/kh.js +0 -5
- package/node_modules/zod/v4/locales/km.cjs +0 -137
- package/node_modules/zod/v4/locales/km.d.cts +0 -5
- package/node_modules/zod/v4/locales/km.d.ts +0 -4
- package/node_modules/zod/v4/locales/km.js +0 -110
- package/node_modules/zod/v4/locales/ko.cjs +0 -138
- package/node_modules/zod/v4/locales/ko.d.cts +0 -5
- package/node_modules/zod/v4/locales/ko.d.ts +0 -4
- package/node_modules/zod/v4/locales/ko.js +0 -111
- package/node_modules/zod/v4/locales/lt.cjs +0 -230
- package/node_modules/zod/v4/locales/lt.d.cts +0 -5
- package/node_modules/zod/v4/locales/lt.d.ts +0 -4
- package/node_modules/zod/v4/locales/lt.js +0 -203
- package/node_modules/zod/v4/locales/mk.cjs +0 -136
- package/node_modules/zod/v4/locales/mk.d.cts +0 -5
- package/node_modules/zod/v4/locales/mk.d.ts +0 -4
- package/node_modules/zod/v4/locales/mk.js +0 -109
- package/node_modules/zod/v4/locales/ms.cjs +0 -134
- package/node_modules/zod/v4/locales/ms.d.cts +0 -5
- package/node_modules/zod/v4/locales/ms.d.ts +0 -4
- package/node_modules/zod/v4/locales/ms.js +0 -107
- package/node_modules/zod/v4/locales/nl.cjs +0 -137
- package/node_modules/zod/v4/locales/nl.d.cts +0 -5
- package/node_modules/zod/v4/locales/nl.d.ts +0 -4
- package/node_modules/zod/v4/locales/nl.js +0 -110
- package/node_modules/zod/v4/locales/no.cjs +0 -135
- package/node_modules/zod/v4/locales/no.d.cts +0 -5
- package/node_modules/zod/v4/locales/no.d.ts +0 -4
- package/node_modules/zod/v4/locales/no.js +0 -108
- package/node_modules/zod/v4/locales/ota.cjs +0 -136
- package/node_modules/zod/v4/locales/ota.d.cts +0 -5
- package/node_modules/zod/v4/locales/ota.d.ts +0 -4
- package/node_modules/zod/v4/locales/ota.js +0 -109
- package/node_modules/zod/v4/locales/package.json +0 -6
- package/node_modules/zod/v4/locales/pl.cjs +0 -136
- package/node_modules/zod/v4/locales/pl.d.cts +0 -5
- package/node_modules/zod/v4/locales/pl.d.ts +0 -4
- package/node_modules/zod/v4/locales/pl.js +0 -109
- package/node_modules/zod/v4/locales/ps.cjs +0 -141
- package/node_modules/zod/v4/locales/ps.d.cts +0 -5
- package/node_modules/zod/v4/locales/ps.d.ts +0 -4
- package/node_modules/zod/v4/locales/ps.js +0 -114
- package/node_modules/zod/v4/locales/pt.cjs +0 -135
- package/node_modules/zod/v4/locales/pt.d.cts +0 -5
- package/node_modules/zod/v4/locales/pt.d.ts +0 -4
- package/node_modules/zod/v4/locales/pt.js +0 -108
- package/node_modules/zod/v4/locales/ru.cjs +0 -183
- package/node_modules/zod/v4/locales/ru.d.cts +0 -5
- package/node_modules/zod/v4/locales/ru.d.ts +0 -4
- package/node_modules/zod/v4/locales/ru.js +0 -156
- package/node_modules/zod/v4/locales/sl.cjs +0 -136
- package/node_modules/zod/v4/locales/sl.d.cts +0 -5
- package/node_modules/zod/v4/locales/sl.d.ts +0 -4
- package/node_modules/zod/v4/locales/sl.js +0 -109
- package/node_modules/zod/v4/locales/sv.cjs +0 -137
- package/node_modules/zod/v4/locales/sv.d.cts +0 -5
- package/node_modules/zod/v4/locales/sv.d.ts +0 -4
- package/node_modules/zod/v4/locales/sv.js +0 -110
- package/node_modules/zod/v4/locales/ta.cjs +0 -137
- package/node_modules/zod/v4/locales/ta.d.cts +0 -5
- package/node_modules/zod/v4/locales/ta.d.ts +0 -4
- package/node_modules/zod/v4/locales/ta.js +0 -110
- package/node_modules/zod/v4/locales/th.cjs +0 -137
- package/node_modules/zod/v4/locales/th.d.cts +0 -5
- package/node_modules/zod/v4/locales/th.d.ts +0 -4
- package/node_modules/zod/v4/locales/th.js +0 -110
- package/node_modules/zod/v4/locales/tr.cjs +0 -132
- package/node_modules/zod/v4/locales/tr.d.cts +0 -5
- package/node_modules/zod/v4/locales/tr.d.ts +0 -4
- package/node_modules/zod/v4/locales/tr.js +0 -105
- package/node_modules/zod/v4/locales/ua.cjs +0 -12
- package/node_modules/zod/v4/locales/ua.d.cts +0 -5
- package/node_modules/zod/v4/locales/ua.d.ts +0 -5
- package/node_modules/zod/v4/locales/ua.js +0 -5
- package/node_modules/zod/v4/locales/uk.cjs +0 -135
- package/node_modules/zod/v4/locales/uk.d.cts +0 -5
- package/node_modules/zod/v4/locales/uk.d.ts +0 -4
- package/node_modules/zod/v4/locales/uk.js +0 -108
- package/node_modules/zod/v4/locales/ur.cjs +0 -137
- package/node_modules/zod/v4/locales/ur.d.cts +0 -5
- package/node_modules/zod/v4/locales/ur.d.ts +0 -4
- package/node_modules/zod/v4/locales/ur.js +0 -110
- package/node_modules/zod/v4/locales/uz.cjs +0 -136
- package/node_modules/zod/v4/locales/uz.d.cts +0 -5
- package/node_modules/zod/v4/locales/uz.d.ts +0 -4
- package/node_modules/zod/v4/locales/uz.js +0 -109
- package/node_modules/zod/v4/locales/vi.cjs +0 -135
- package/node_modules/zod/v4/locales/vi.d.cts +0 -5
- package/node_modules/zod/v4/locales/vi.d.ts +0 -4
- package/node_modules/zod/v4/locales/vi.js +0 -108
- package/node_modules/zod/v4/locales/yo.cjs +0 -134
- package/node_modules/zod/v4/locales/yo.d.cts +0 -5
- package/node_modules/zod/v4/locales/yo.d.ts +0 -4
- package/node_modules/zod/v4/locales/yo.js +0 -107
- package/node_modules/zod/v4/locales/zh-CN.cjs +0 -136
- package/node_modules/zod/v4/locales/zh-CN.d.cts +0 -5
- package/node_modules/zod/v4/locales/zh-CN.d.ts +0 -4
- package/node_modules/zod/v4/locales/zh-CN.js +0 -109
- package/node_modules/zod/v4/locales/zh-TW.cjs +0 -134
- package/node_modules/zod/v4/locales/zh-TW.d.cts +0 -5
- package/node_modules/zod/v4/locales/zh-TW.d.ts +0 -4
- package/node_modules/zod/v4/locales/zh-TW.js +0 -107
- package/node_modules/zod/v4/mini/checks.cjs +0 -34
- package/node_modules/zod/v4/mini/checks.d.cts +0 -1
- package/node_modules/zod/v4/mini/checks.d.ts +0 -1
- package/node_modules/zod/v4/mini/checks.js +0 -1
- package/node_modules/zod/v4/mini/coerce.cjs +0 -52
- package/node_modules/zod/v4/mini/coerce.d.cts +0 -7
- package/node_modules/zod/v4/mini/coerce.d.ts +0 -7
- package/node_modules/zod/v4/mini/coerce.js +0 -22
- package/node_modules/zod/v4/mini/external.cjs +0 -63
- package/node_modules/zod/v4/mini/external.d.cts +0 -12
- package/node_modules/zod/v4/mini/external.d.ts +0 -12
- package/node_modules/zod/v4/mini/external.js +0 -14
- package/node_modules/zod/v4/mini/index.cjs +0 -32
- package/node_modules/zod/v4/mini/index.d.cts +0 -3
- package/node_modules/zod/v4/mini/index.d.ts +0 -3
- package/node_modules/zod/v4/mini/index.js +0 -3
- package/node_modules/zod/v4/mini/iso.cjs +0 -64
- package/node_modules/zod/v4/mini/iso.d.cts +0 -22
- package/node_modules/zod/v4/mini/iso.d.ts +0 -22
- package/node_modules/zod/v4/mini/iso.js +0 -34
- package/node_modules/zod/v4/mini/package.json +0 -6
- package/node_modules/zod/v4/mini/parse.cjs +0 -16
- package/node_modules/zod/v4/mini/parse.d.cts +0 -1
- package/node_modules/zod/v4/mini/parse.d.ts +0 -1
- package/node_modules/zod/v4/mini/parse.js +0 -1
- package/node_modules/zod/v4/mini/schemas.cjs +0 -1046
- package/node_modules/zod/v4/mini/schemas.d.cts +0 -427
- package/node_modules/zod/v4/mini/schemas.d.ts +0 -427
- package/node_modules/zod/v4/mini/schemas.js +0 -925
- package/node_modules/zod/v4/package.json +0 -6
- package/node_modules/zod/v4-mini/index.cjs +0 -32
- package/node_modules/zod/v4-mini/index.d.cts +0 -3
- package/node_modules/zod/v4-mini/index.d.ts +0 -3
- package/node_modules/zod/v4-mini/index.js +0 -3
- package/node_modules/zod/v4-mini/package.json +0 -6
- package/plugin/dist/index.d.ts +0 -179
- package/plugin/dist/index.d.ts.map +0 -1
- package/plugin/dist/index.js +0 -3209
- package/plugin/dist/index.js.map +0 -1
package/dist/compositor.js
DELETED
|
@@ -1,3632 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* hypermem Compositor
|
|
3
|
-
*
|
|
4
|
-
* Assembles context for LLM calls by orchestrating all four memory layers:
|
|
5
|
-
* L1 Redis — hot session working memory (system, identity, recent msgs)
|
|
6
|
-
* L2 Messages — conversation history from messages.db
|
|
7
|
-
* L3 Vectors — semantic search across all indexed content
|
|
8
|
-
* L4 Library — structured knowledge (facts, preferences, knowledge, episodes)
|
|
9
|
-
*
|
|
10
|
-
* Token-budgeted: never exceeds the budget, prioritizes by configured order.
|
|
11
|
-
* Provider-neutral internally, translates at the output boundary.
|
|
12
|
-
*/
|
|
13
|
-
import { createHash } from 'node:crypto';
|
|
14
|
-
import { filterByScope } from './retrieval-policy.js';
|
|
15
|
-
import { DEFAULT_TRIGGERS, matchTriggers, logRegistryStartup, } from './trigger-registry.js';
|
|
16
|
-
import { MessageStore } from './message-store.js';
|
|
17
|
-
import { SessionTopicMap } from './session-topic-map.js';
|
|
18
|
-
import { toProviderFormat } from './provider-translator.js';
|
|
19
|
-
import { DocChunkStore } from './doc-chunk-store.js';
|
|
20
|
-
import { hybridSearch } from './hybrid-retrieval.js';
|
|
21
|
-
import { ensureCompactionFenceSchema, updateCompactionFence, getCompactionFence } from './compaction-fence.js';
|
|
22
|
-
import { getActiveContext } from './context-store.js';
|
|
23
|
-
import { rankKeystones, scoreKeystone } from './keystone-scorer.js';
|
|
24
|
-
import { buildOrgRegistryFromDb, defaultOrgRegistry } from './cross-agent.js';
|
|
25
|
-
import { getActiveFOS, matchMOD, renderFOS, renderMOD, renderLightFOS, resolveOutputTier, buildActionVerificationSummary } from './fos-mod.js';
|
|
26
|
-
import { KnowledgeStore } from './knowledge-store.js';
|
|
27
|
-
import { TemporalStore, hasTemporalSignals } from './temporal-store.js';
|
|
28
|
-
import { isOpenDomainQuery, searchOpenDomain } from './open-domain.js';
|
|
29
|
-
import { TRIM_BUDGET_POLICY, resolveTrimBudgets } from './budget-policy.js';
|
|
30
|
-
import { formatToolChainStub, parseToolChainStub, formatArtifactRef, isArtifactRef } from './degradation.js';
|
|
31
|
-
import { ToolArtifactStore } from './tool-artifact-store.js';
|
|
32
|
-
/**
|
|
33
|
-
* Files that OpenClaw's contextInjection injects into the system prompt.
|
|
34
|
-
* HyperMem must not re-inject these via doc chunk retrieval to avoid duplication.
|
|
35
|
-
* Exported so plugin and other consumers can share the same dedup set.
|
|
36
|
-
*/
|
|
37
|
-
export const OPENCLAW_BOOTSTRAP_FILES = new Set([
|
|
38
|
-
'SOUL.md', 'IDENTITY.md', 'USER.md', 'TOOLS.md',
|
|
39
|
-
'AGENTS.md', 'HEARTBEAT.md', 'MEMORY.md', 'BOOTSTRAP.md',
|
|
40
|
-
]);
|
|
41
|
-
const CACHE_PREFIX_BOUNDARY_SLOT = 'cache-prefix-boundary';
|
|
42
|
-
/**
|
|
43
|
-
* Model context window sizes by provider/model string (or partial match).
|
|
44
|
-
* Used as fallback when tokenBudget is not passed by the runtime.
|
|
45
|
-
* Order matters: first match wins. Partial substring match on the model string.
|
|
46
|
-
*/
|
|
47
|
-
const MODEL_CONTEXT_WINDOWS = [
|
|
48
|
-
// Anthropic
|
|
49
|
-
{ pattern: 'claude-opus-4', tokens: 200_000 },
|
|
50
|
-
{ pattern: 'claude-sonnet-4', tokens: 200_000 },
|
|
51
|
-
{ pattern: 'claude-3-5', tokens: 200_000 },
|
|
52
|
-
{ pattern: 'claude-3-7', tokens: 200_000 },
|
|
53
|
-
{ pattern: 'claude', tokens: 200_000 },
|
|
54
|
-
// OpenAI
|
|
55
|
-
{ pattern: 'gpt-5', tokens: 128_000 },
|
|
56
|
-
{ pattern: 'gpt-4o', tokens: 128_000 },
|
|
57
|
-
{ pattern: 'gpt-4', tokens: 128_000 },
|
|
58
|
-
{ pattern: 'o3', tokens: 128_000 },
|
|
59
|
-
{ pattern: 'o4', tokens: 128_000 },
|
|
60
|
-
// Google
|
|
61
|
-
{ pattern: 'gemini-3.1-pro', tokens: 1_000_000 },
|
|
62
|
-
{ pattern: 'gemini-3.1-flash', tokens: 1_000_000 },
|
|
63
|
-
{ pattern: 'gemini-2.5-pro', tokens: 1_000_000 },
|
|
64
|
-
{ pattern: 'gemini-2', tokens: 1_000_000 },
|
|
65
|
-
{ pattern: 'gemini', tokens: 1_000_000 },
|
|
66
|
-
// Zhipu / GLM
|
|
67
|
-
{ pattern: 'glm-5', tokens: 131_072 },
|
|
68
|
-
{ pattern: 'glm-4', tokens: 131_072 },
|
|
69
|
-
// Alibaba / Qwen
|
|
70
|
-
{ pattern: 'qwen3', tokens: 262_144 },
|
|
71
|
-
{ pattern: 'qwen', tokens: 131_072 },
|
|
72
|
-
// DeepSeek
|
|
73
|
-
{ pattern: 'deepseek-v3', tokens: 131_072 },
|
|
74
|
-
{ pattern: 'deepseek', tokens: 131_072 },
|
|
75
|
-
];
|
|
76
|
-
const MODEL_MECW = [
|
|
77
|
-
// Claude 200k: effective recall degrades above ~140k; clamp composite budget
|
|
78
|
-
{ pattern: 'claude', mecwFloor: 80_000, mecwCeiling: 140_000, preferredHistoryFraction: 0.35, preferredMemoryFraction: 0.45 },
|
|
79
|
-
// Gemini 1M: reliable up to ~180k for grounded retrieval; less for recall
|
|
80
|
-
{ pattern: 'gemini', mecwFloor: 100_000, mecwCeiling: 180_000, preferredHistoryFraction: 0.40, preferredMemoryFraction: 0.45 },
|
|
81
|
-
// OpenAI 128k: full window is trustable; use standard fractions
|
|
82
|
-
{ pattern: 'gpt', mecwFloor: 128_000, mecwCeiling: 128_000, preferredHistoryFraction: 0.40, preferredMemoryFraction: 0.40 },
|
|
83
|
-
{ pattern: 'o3', mecwFloor: 128_000, mecwCeiling: 128_000, preferredHistoryFraction: 0.40, preferredMemoryFraction: 0.40 },
|
|
84
|
-
{ pattern: 'o4', mecwFloor: 128_000, mecwCeiling: 128_000, preferredHistoryFraction: 0.40, preferredMemoryFraction: 0.40 },
|
|
85
|
-
// Smaller windows: full window is trustable
|
|
86
|
-
{ pattern: 'qwen3', mecwFloor: 262_144, mecwCeiling: 262_144, preferredHistoryFraction: 0.40, preferredMemoryFraction: 0.40 },
|
|
87
|
-
{ pattern: 'qwen', mecwFloor: 131_072, mecwCeiling: 131_072, preferredHistoryFraction: 0.40, preferredMemoryFraction: 0.40 },
|
|
88
|
-
{ pattern: 'glm', mecwFloor: 131_072, mecwCeiling: 131_072, preferredHistoryFraction: 0.40, preferredMemoryFraction: 0.40 },
|
|
89
|
-
{ pattern: 'deepseek', mecwFloor: 131_072, mecwCeiling: 131_072, preferredHistoryFraction: 0.40, preferredMemoryFraction: 0.40 },
|
|
90
|
-
];
|
|
91
|
-
/**
|
|
92
|
-
* B4: Compute model-aware lane budget fractions.
|
|
93
|
-
*
|
|
94
|
-
* Resolves the effective historyFraction and memoryFraction for a compose pass
|
|
95
|
-
* given the model and its effective budget. Uses the MECW catalog to blend
|
|
96
|
-
* away from fixed fractions when the budget approaches the MECW ceiling,
|
|
97
|
-
* so the compositor allocates proportionally for what the model can actually use.
|
|
98
|
-
*
|
|
99
|
-
* Returns:
|
|
100
|
-
* historyFraction — fraction of effective budget to give history
|
|
101
|
-
* memoryFraction — fraction of effective budget to give memory pool
|
|
102
|
-
* mecwProfile — which MECW entry matched (undefined = no match / full window)
|
|
103
|
-
* mecwApplied — true when MECW adjustment changed the fractions
|
|
104
|
-
* mecwBlend — 0..1 blend factor (0 = below floor, 1 = at/above ceiling)
|
|
105
|
-
*/
|
|
106
|
-
export function resolveModelLaneBudgets(model, effectiveBudget, configHistoryFraction, configMemoryFraction) {
|
|
107
|
-
if (!model) {
|
|
108
|
-
return { historyFraction: configHistoryFraction, memoryFraction: configMemoryFraction, mecwProfile: undefined, mecwApplied: false, mecwBlend: 0 };
|
|
109
|
-
}
|
|
110
|
-
const normalized = model.toLowerCase();
|
|
111
|
-
for (const entry of MODEL_MECW) {
|
|
112
|
-
if (!normalized.includes(entry.pattern))
|
|
113
|
-
continue;
|
|
114
|
-
// Budget is at or below the floor — full window is safe, use config fractions
|
|
115
|
-
if (effectiveBudget <= entry.mecwFloor) {
|
|
116
|
-
return { historyFraction: configHistoryFraction, memoryFraction: configMemoryFraction, mecwProfile: entry.pattern, mecwApplied: false, mecwBlend: 0 };
|
|
117
|
-
}
|
|
118
|
-
// Budget is at or above the ceiling — use preferred fractions fully
|
|
119
|
-
if (effectiveBudget >= entry.mecwCeiling) {
|
|
120
|
-
return { historyFraction: entry.preferredHistoryFraction, memoryFraction: entry.preferredMemoryFraction, mecwProfile: entry.pattern, mecwApplied: true, mecwBlend: 1 };
|
|
121
|
-
}
|
|
122
|
-
// Budget is between floor and ceiling — linear blend
|
|
123
|
-
const blend = (effectiveBudget - entry.mecwFloor) / (entry.mecwCeiling - entry.mecwFloor);
|
|
124
|
-
const historyFraction = configHistoryFraction + blend * (entry.preferredHistoryFraction - configHistoryFraction);
|
|
125
|
-
const memoryFraction = configMemoryFraction + blend * (entry.preferredMemoryFraction - configMemoryFraction);
|
|
126
|
-
return {
|
|
127
|
-
historyFraction: Math.round(historyFraction * 1000) / 1000,
|
|
128
|
-
memoryFraction: Math.round(memoryFraction * 1000) / 1000,
|
|
129
|
-
mecwProfile: entry.pattern,
|
|
130
|
-
mecwApplied: true,
|
|
131
|
-
mecwBlend: Math.round(blend * 1000) / 1000,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
// No MECW entry matched — use config fractions unchanged
|
|
135
|
-
return { historyFraction: configHistoryFraction, memoryFraction: configMemoryFraction, mecwProfile: undefined, mecwApplied: false, mecwBlend: 0 };
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Resolve effective token budget from model string.
|
|
139
|
-
* Returns the context window for the model, minus the configured reserve fraction
|
|
140
|
-
* for output tokens and hypermem operational overhead.
|
|
141
|
-
* Default reserve: 25% (leaves 75% for input context).
|
|
142
|
-
* Falls back to defaultTokenBudget if no model match.
|
|
143
|
-
*/
|
|
144
|
-
/**
|
|
145
|
-
* Resolve effective input token budget for a model.
|
|
146
|
-
*
|
|
147
|
-
* Priority:
|
|
148
|
-
* 1. If budgetFraction is set AND model window is detected: window × budgetFraction × (1 - reserve)
|
|
149
|
-
* 2. If model window detected but no budgetFraction: window × (1 - reserve)
|
|
150
|
-
* 3. Fallback to defaultTokenBudget (absolute number)
|
|
151
|
-
*/
|
|
152
|
-
function resolveModelBudget(model, defaultBudget, reserve = 0.15, budgetFraction) {
|
|
153
|
-
const window = resolveModelWindow(model, defaultBudget);
|
|
154
|
-
// If we detected an actual model window (not the fallback derivation)
|
|
155
|
-
if (model && budgetFraction != null) {
|
|
156
|
-
const normalized = model.toLowerCase();
|
|
157
|
-
for (const entry of MODEL_CONTEXT_WINDOWS) {
|
|
158
|
-
if (normalized.includes(entry.pattern)) {
|
|
159
|
-
return Math.floor(entry.tokens * budgetFraction * (1 - reserve));
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
// Original path: detected window × (1 - reserve), or absolute fallback
|
|
164
|
-
if (!model)
|
|
165
|
-
return defaultBudget;
|
|
166
|
-
const normalized = model.toLowerCase();
|
|
167
|
-
for (const entry of MODEL_CONTEXT_WINDOWS) {
|
|
168
|
-
if (normalized.includes(entry.pattern)) {
|
|
169
|
-
return Math.floor(entry.tokens * (1 - reserve));
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return defaultBudget;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Resolve the raw context window size for a model (no reserve applied).
|
|
176
|
-
* Used as totalWindow for dynamic reserve calculation.
|
|
177
|
-
* Falls back to defaultBudget / 0.85 (reverse of 15% reserve default) if no match.
|
|
178
|
-
*/
|
|
179
|
-
function resolveModelWindow(model, defaultBudget) {
|
|
180
|
-
if (!model)
|
|
181
|
-
return Math.floor(defaultBudget / 0.85);
|
|
182
|
-
const normalized = model.toLowerCase();
|
|
183
|
-
for (const entry of MODEL_CONTEXT_WINDOWS) {
|
|
184
|
-
if (normalized.includes(entry.pattern)) {
|
|
185
|
-
return entry.tokens;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return Math.floor(defaultBudget / 0.85);
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Compute dynamic context window reserve based on recent turn cost.
|
|
192
|
-
*
|
|
193
|
-
* Reserve = clamp(avg_turn_cost × horizon / totalWindow, base, max)
|
|
194
|
-
*
|
|
195
|
-
* Returns the reserve fraction and diagnostics. When dynamic reserve
|
|
196
|
-
* is clamped at max, sessionPressureHigh is set true so callers can
|
|
197
|
-
* emit a warning or trigger checkpointing.
|
|
198
|
-
*/
|
|
199
|
-
function computeDynamicReserve(recentMessages, totalWindow, config) {
|
|
200
|
-
const base = config.reserveFraction ?? config.contextWindowReserve ?? 0.25;
|
|
201
|
-
const horizon = config.dynamicReserveTurnHorizon ?? 5;
|
|
202
|
-
const max = config.dynamicReserveMax ?? 0.50;
|
|
203
|
-
const enabled = config.dynamicReserveEnabled ?? true;
|
|
204
|
-
// Cold sessions (no message history) use a minimal floor so the full window
|
|
205
|
-
// stays available. The static reserveFraction applies only once the session
|
|
206
|
-
// has messages and dynamic sampling can compute a meaningful estimate.
|
|
207
|
-
const COLD_SESSION_FLOOR = 0.15;
|
|
208
|
-
if (!enabled || totalWindow <= 0) {
|
|
209
|
-
return { reserve: COLD_SESSION_FLOOR, avgTurnCost: 0, dynamic: false, pressureHigh: false };
|
|
210
|
-
}
|
|
211
|
-
if (recentMessages.length === 0) {
|
|
212
|
-
return { reserve: COLD_SESSION_FLOOR, avgTurnCost: 0, dynamic: false, pressureHigh: false };
|
|
213
|
-
}
|
|
214
|
-
// Sample the last 20 user+assistant messages for turn cost estimation.
|
|
215
|
-
// Tool messages are excluded — they're already compressed by the gradient
|
|
216
|
-
// and don't represent per-turn user intent cost.
|
|
217
|
-
const sample = recentMessages
|
|
218
|
-
.filter(m => m.role === 'user' || m.role === 'assistant')
|
|
219
|
-
.slice(-20);
|
|
220
|
-
if (sample.length === 0) {
|
|
221
|
-
return { reserve: base, avgTurnCost: 0, dynamic: false, pressureHigh: false };
|
|
222
|
-
}
|
|
223
|
-
const totalCost = sample.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
|
|
224
|
-
const avgTurnCost = Math.floor(totalCost / sample.length);
|
|
225
|
-
const safetyTokens = avgTurnCost * horizon;
|
|
226
|
-
const dynamicFrac = safetyTokens / totalWindow;
|
|
227
|
-
if (dynamicFrac <= base) {
|
|
228
|
-
return { reserve: base, avgTurnCost, dynamic: false, pressureHigh: false };
|
|
229
|
-
}
|
|
230
|
-
if (dynamicFrac >= max) {
|
|
231
|
-
return { reserve: max, avgTurnCost, dynamic: true, pressureHigh: true };
|
|
232
|
-
}
|
|
233
|
-
return { reserve: dynamicFrac, avgTurnCost, dynamic: true, pressureHigh: false };
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Classify a session based on the ratio of tool messages in the recent sample.
|
|
237
|
-
* 'tool-heavy': >= 20% of sampled messages carry tool calls or tool results.
|
|
238
|
-
* 'plain-chat': below that threshold (text-only or occasional tool use).
|
|
239
|
-
*
|
|
240
|
-
* The 20% threshold is intentionally conservative: most tool-heavy agents
|
|
241
|
-
* have tool messages on every assistant turn, so the ratio quickly exceeds
|
|
242
|
-
* the threshold without false-positive risk for light tool users.
|
|
243
|
-
*/
|
|
244
|
-
export function classifySessionType(messages) {
|
|
245
|
-
if (messages.length === 0)
|
|
246
|
-
return 'plain-chat';
|
|
247
|
-
const toolCount = messages.filter(m => hasToolContent(m)).length;
|
|
248
|
-
return toolCount / messages.length >= 0.20 ? 'tool-heavy' : 'plain-chat';
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Estimate the average token cost per message from a recent message sample.
|
|
252
|
-
* Uses the same estimateMessageTokens heuristic as the compositor budget walk
|
|
253
|
-
* so the returned depth is directly comparable to the historyFillCap check.
|
|
254
|
-
*
|
|
255
|
-
* Returns a conservative floor (100 tokens) when the sample is empty to avoid
|
|
256
|
-
* returning Infinity when historyBudget is divided by density.
|
|
257
|
-
*/
|
|
258
|
-
export function estimateObservedMsgDensity(messages) {
|
|
259
|
-
if (messages.length === 0)
|
|
260
|
-
return 100;
|
|
261
|
-
const total = messages.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
|
|
262
|
-
return Math.max(1, Math.ceil(total / messages.length));
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Compute an adaptive history depth that pre-fits the session type.
|
|
266
|
-
*
|
|
267
|
-
* For plain-chat sessions: divides historyBudget by observed density to get a
|
|
268
|
-
* depth that fills the budget without overflow, bounded by the default maximum.
|
|
269
|
-
* Recall quality is preserved because the density estimate is honest for
|
|
270
|
-
* text-only turns.
|
|
271
|
-
*
|
|
272
|
-
* For tool-heavy sessions: applies a post-gradient compression factor
|
|
273
|
-
* (TOOL_GRADIENT_DENSITY_FACTOR = 0.30) to the observed pre-gradient density.
|
|
274
|
-
* This accounts for the gradient transform collapsing large tool payloads to
|
|
275
|
-
* prose stubs before the budget-fit walk runs. A tighter depth is chosen so
|
|
276
|
-
* the gradient-compressed messages fit inside historyFillCap without triggering
|
|
277
|
-
* a rescue trim.
|
|
278
|
-
*
|
|
279
|
-
* A 0.85 safety margin is applied to both paths so estimates that are
|
|
280
|
-
* slightly off don't cause immediate overflow on the first warm compose.
|
|
281
|
-
*
|
|
282
|
-
* Min/max bounds ensure the compositor always sees a meaningful window:
|
|
283
|
-
* - plain-chat min: 20 messages (enough for short recent context)
|
|
284
|
-
* - tool-heavy min: 15 messages (recent tool context + a few prior turns)
|
|
285
|
-
* - shared max: config.maxHistoryMessages (never exceed the DB fetch ceiling)
|
|
286
|
-
*/
|
|
287
|
-
export function computeAdaptiveHistoryDepth(sessionType, observedDensity, historyBudgetTokens, maxHistoryMessages) {
|
|
288
|
-
const SAFETY_MARGIN = 0.85;
|
|
289
|
-
if (sessionType === 'tool-heavy') {
|
|
290
|
-
// Tool-heavy: post-gradient density is much lower than pre-gradient.
|
|
291
|
-
// Gradient tiers collapse T2/T3 payloads to compact stubs (15-30% of original).
|
|
292
|
-
// Use a blended factor of 0.30 as the expected post-gradient density ratio.
|
|
293
|
-
const TOOL_GRADIENT_DENSITY_FACTOR = 0.30;
|
|
294
|
-
const postGradientDensity = Math.max(50, Math.floor(observedDensity * TOOL_GRADIENT_DENSITY_FACTOR));
|
|
295
|
-
const depth = Math.floor((historyBudgetTokens * SAFETY_MARGIN) / postGradientDensity);
|
|
296
|
-
return Math.min(maxHistoryMessages, Math.max(15, depth));
|
|
297
|
-
}
|
|
298
|
-
// Plain-chat: pre-gradient and post-gradient density are the same.
|
|
299
|
-
// historyBudget / avgMsgCost gives the message count that fills the budget.
|
|
300
|
-
const depth = Math.floor((historyBudgetTokens * SAFETY_MARGIN) / observedDensity);
|
|
301
|
-
return Math.min(maxHistoryMessages, Math.max(20, depth));
|
|
302
|
-
}
|
|
303
|
-
const DEFAULT_CONFIG = {
|
|
304
|
-
// Primary budget controls
|
|
305
|
-
budgetFraction: 0.703,
|
|
306
|
-
reserveFraction: 0.25,
|
|
307
|
-
historyFraction: 0.40,
|
|
308
|
-
memoryFraction: 0.40,
|
|
309
|
-
// Absolute fallback
|
|
310
|
-
defaultTokenBudget: 90000,
|
|
311
|
-
// History internals
|
|
312
|
-
maxHistoryMessages: 250,
|
|
313
|
-
warmHistoryBudgetFraction: 0.4,
|
|
314
|
-
keystoneHistoryFraction: 0.2,
|
|
315
|
-
keystoneMaxMessages: 15,
|
|
316
|
-
keystoneMinSignificance: 0.5,
|
|
317
|
-
// Memory internals
|
|
318
|
-
maxFacts: 28,
|
|
319
|
-
maxCrossSessionContext: 6000,
|
|
320
|
-
// Tool gradient (internal)
|
|
321
|
-
maxRecentToolPairs: 3,
|
|
322
|
-
maxProseToolPairs: 10,
|
|
323
|
-
// Dynamic reserve
|
|
324
|
-
dynamicReserveTurnHorizon: 5,
|
|
325
|
-
dynamicReserveMax: 0.50,
|
|
326
|
-
dynamicReserveEnabled: true,
|
|
327
|
-
};
|
|
328
|
-
// Tool gradient thresholds — controls how aggressively tool results are
|
|
329
|
-
// truncated as they age out of the recent window.
|
|
330
|
-
// Recent-turn policy (2026-04-07): protect turn 0 + turn 1, budget against a
|
|
331
|
-
// conservative 120k planning window, and only head+tail trim large (>40k)
|
|
332
|
-
// recent results when projected occupancy crosses the orange zone.
|
|
333
|
-
const TOOL_GRADIENT_T0_TURNS = 2; // current + 2 prior completed turns: full fidelity (matches OpenClaw keepLastAssistants: 3)
|
|
334
|
-
const TOOL_GRADIENT_T1_TURNS = 4; // turns 2-4: moderate truncation (was 3)
|
|
335
|
-
const TOOL_GRADIENT_T2_TURNS = 7; // turns 4-7: aggressive truncation (was 12)
|
|
336
|
-
// T3 = turns 8+: one-liner stub
|
|
337
|
-
const TOOL_GRADIENT_T1_CHAR_CAP = 6_000; // per-message cap (was 8k)
|
|
338
|
-
const TOOL_GRADIENT_T1_TURN_CAP = 12_000; // per-turn-pair cap (was 16k)
|
|
339
|
-
const TOOL_GRADIENT_T2_CHAR_CAP = 800; // per-message cap (was 1k)
|
|
340
|
-
const TOOL_GRADIENT_T2_TURN_CAP = 3_000; // per-turn-pair cap (was 4k)
|
|
341
|
-
const TOOL_GRADIENT_T3_CHAR_CAP = 150; // oldest tier: stub only (was 200)
|
|
342
|
-
const TOOL_GRADIENT_T3_TURN_CAP = 800; // per-turn-pair cap (was 1k)
|
|
343
|
-
const TOOL_GRADIENT_MAX_TAIL_CHARS = 3_000; // tail preserve budget for T1+
|
|
344
|
-
const TOOL_GRADIENT_MIDDLE_MARKER = '\n[... tool output truncated ...]\n';
|
|
345
|
-
const TOOL_PLANNING_BASELINE_WINDOW = 120_000;
|
|
346
|
-
const TOOL_PLANNING_MIN_RESERVE_TOKENS = 24_000;
|
|
347
|
-
const TOOL_PRESSURE_YELLOW = 0.75;
|
|
348
|
-
const TOOL_PRESSURE_ORANGE = 0.80;
|
|
349
|
-
const TOOL_PRESSURE_RED = 0.85;
|
|
350
|
-
const TOOL_RECENT_OVERSIZE_CHAR_THRESHOLD = 40_000;
|
|
351
|
-
const TOOL_RECENT_OVERSIZE_TARGET_CHARS = 40_000;
|
|
352
|
-
const TOOL_RECENT_OVERSIZE_MAX_TAIL_CHARS = 12_000;
|
|
353
|
-
const TOOL_TRIM_NOTE_PREFIX = '[hypermem_tool_result_trim';
|
|
354
|
-
// ─── Trigger Registry ────────────────────────────────────────────
|
|
355
|
-
// Moved to src/trigger-registry.ts (W5).
|
|
356
|
-
// CollectionTrigger, DEFAULT_TRIGGERS, matchTriggers imported above.
|
|
357
|
-
// Re-exported below for backward compatibility with existing consumers.
|
|
358
|
-
export { DEFAULT_TRIGGERS, matchTriggers } from './trigger-registry.js';
|
|
359
|
-
// ─── Test-only exports (not part of public API) ───────────────────────────
|
|
360
|
-
// These are exported solely for unit testing. Do not use in production code.
|
|
361
|
-
export { getTurnAge, applyToolGradient, appendToolSummary, truncateWithHeadTail, applyTierPayloadCap, evictLargeToolResults };
|
|
362
|
-
function clusterNeutralMessages(messages) {
|
|
363
|
-
const clusters = [];
|
|
364
|
-
for (let i = 0; i < messages.length; i++) {
|
|
365
|
-
const current = messages[i];
|
|
366
|
-
const cluster = [current];
|
|
367
|
-
if (current.toolCalls && current.toolCalls.length > 0) {
|
|
368
|
-
const callIds = new Set(current.toolCalls.map(tc => tc.id).filter(Boolean));
|
|
369
|
-
let j = i + 1;
|
|
370
|
-
while (j < messages.length) {
|
|
371
|
-
const candidate = messages[j];
|
|
372
|
-
if (!candidate.toolResults || candidate.toolResults.length === 0)
|
|
373
|
-
break;
|
|
374
|
-
const resultIds = candidate.toolResults.map(tr => tr.callId).filter(Boolean);
|
|
375
|
-
if (callIds.size > 0 && resultIds.length > 0 && !resultIds.some(id => callIds.has(id)))
|
|
376
|
-
break;
|
|
377
|
-
cluster.push(candidate);
|
|
378
|
-
j++;
|
|
379
|
-
}
|
|
380
|
-
i = j - 1;
|
|
381
|
-
}
|
|
382
|
-
else if (current.toolResults && current.toolResults.length > 0) {
|
|
383
|
-
let j = i + 1;
|
|
384
|
-
while (j < messages.length) {
|
|
385
|
-
const candidate = messages[j];
|
|
386
|
-
if (!candidate.toolResults || candidate.toolResults.length === 0 || (candidate.toolCalls && candidate.toolCalls.length > 0))
|
|
387
|
-
break;
|
|
388
|
-
cluster.push(candidate);
|
|
389
|
-
j++;
|
|
390
|
-
}
|
|
391
|
-
i = j - 1;
|
|
392
|
-
}
|
|
393
|
-
clusters.push({
|
|
394
|
-
messages: cluster,
|
|
395
|
-
tokenCost: cluster.reduce((sum, msg) => sum + estimateMessageTokens(msg), 0),
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
return clusters;
|
|
399
|
-
}
|
|
400
|
-
/**
|
|
401
|
-
* Public reshape helper: apply tool gradient then trim to fit within a token budget.
|
|
402
|
-
*
|
|
403
|
-
* Used by the plugin's budget-downshift pass to pre-process a Redis history window
|
|
404
|
-
* after a model switch to a smaller context window, before the full compose pipeline
|
|
405
|
-
* runs. Trims from oldest to newest until estimated token cost fits within
|
|
406
|
-
* tokenBudget * 0.65 (using the standard char/4 heuristic).
|
|
407
|
-
*
|
|
408
|
-
* @param messages NeutralMessage array from the Redis hot window
|
|
409
|
-
* @param tokenBudget Effective token budget for this session
|
|
410
|
-
* @returns Trimmed message array ready for setWindow()
|
|
411
|
-
*/
|
|
412
|
-
export function applyToolGradientToWindow(messages, tokenBudget, totalWindowTokens) {
|
|
413
|
-
const reshaped = applyToolGradient(messages, { totalWindowTokens });
|
|
414
|
-
const { softBudget: targetTokens } = resolveTrimBudgets(tokenBudget);
|
|
415
|
-
const clusters = clusterNeutralMessages(reshaped);
|
|
416
|
-
let totalTokens = clusters.reduce((sum, cluster) => sum + cluster.tokenCost, 0);
|
|
417
|
-
let start = 0;
|
|
418
|
-
// walk oldest to newest, drop until we fit
|
|
419
|
-
while (totalTokens > targetTokens && start < clusters.length - 1) {
|
|
420
|
-
totalTokens -= clusters[start].tokenCost;
|
|
421
|
-
start++;
|
|
422
|
-
}
|
|
423
|
-
return clusters.slice(start).flatMap(cluster => cluster.messages);
|
|
424
|
-
}
|
|
425
|
-
/**
|
|
426
|
-
* Canonical history must remain lossless for tool turns.
|
|
427
|
-
*
|
|
428
|
-
* If a window contains any structured tool calls or tool results, the caller
|
|
429
|
-
* should treat applyToolGradientToWindow() as a view-only transform for the
|
|
430
|
-
* current compose pass and avoid writing the reshaped messages back into the
|
|
431
|
-
* canonical cache/history store.
|
|
432
|
-
*/
|
|
433
|
-
export function canPersistReshapedHistory(messages) {
|
|
434
|
-
return !messages.some(msg => hasToolContent(msg));
|
|
435
|
-
}
|
|
436
|
-
/**
|
|
437
|
-
* Rough token estimation: ~4 chars per token for English text.
|
|
438
|
-
* This is a heuristic — actual tokenization varies by model.
|
|
439
|
-
* Good enough for budget management; exact count comes from the provider.
|
|
440
|
-
*/
|
|
441
|
-
function estimateTokens(text) {
|
|
442
|
-
if (!text)
|
|
443
|
-
return 0;
|
|
444
|
-
return Math.ceil(text.length / 4);
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* Dense token estimation for tool content (JSON, code, base64).
|
|
448
|
-
* Tool payloads are typically 2x denser than English prose.
|
|
449
|
-
*/
|
|
450
|
-
function estimateToolTokens(text) {
|
|
451
|
-
return Math.ceil(text.length / 2);
|
|
452
|
-
}
|
|
453
|
-
function estimateMessageTokens(msg) {
|
|
454
|
-
let tokens = estimateTokens(msg.textContent);
|
|
455
|
-
if (msg.toolCalls) {
|
|
456
|
-
tokens += estimateToolTokens(JSON.stringify(msg.toolCalls)); // dense: /2 not /4
|
|
457
|
-
}
|
|
458
|
-
if (msg.toolResults) {
|
|
459
|
-
tokens += estimateToolTokens(JSON.stringify(msg.toolResults)); // dense: /2 not /4
|
|
460
|
-
}
|
|
461
|
-
// Overhead per message (role, formatting)
|
|
462
|
-
tokens += 4;
|
|
463
|
-
return tokens;
|
|
464
|
-
}
|
|
465
|
-
function isDynamicBoundaryMessage(msg) {
|
|
466
|
-
return Boolean(msg.metadata?.dynamicBoundary);
|
|
467
|
-
}
|
|
468
|
-
function getStablePrefixMessages(messages) {
|
|
469
|
-
const prefix = [];
|
|
470
|
-
for (const msg of messages) {
|
|
471
|
-
if (msg.role !== 'system')
|
|
472
|
-
break;
|
|
473
|
-
if (isDynamicBoundaryMessage(msg))
|
|
474
|
-
break;
|
|
475
|
-
prefix.push(msg);
|
|
476
|
-
}
|
|
477
|
-
return prefix;
|
|
478
|
-
}
|
|
479
|
-
function computeStablePrefixHash(messages) {
|
|
480
|
-
if (messages.length === 0)
|
|
481
|
-
return undefined;
|
|
482
|
-
const hash = createHash('sha256');
|
|
483
|
-
for (const msg of messages) {
|
|
484
|
-
hash.update(msg.textContent ?? '');
|
|
485
|
-
hash.update('\n␞\n');
|
|
486
|
-
}
|
|
487
|
-
return hash.digest('hex');
|
|
488
|
-
}
|
|
489
|
-
function parseToolArgs(argumentsJson) {
|
|
490
|
-
try {
|
|
491
|
-
return JSON.parse(argumentsJson);
|
|
492
|
-
}
|
|
493
|
-
catch {
|
|
494
|
-
return {};
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
function toolLabelFromCall(name, args) {
|
|
498
|
-
switch (name) {
|
|
499
|
-
case 'read':
|
|
500
|
-
return `read ${(args.path ?? args.file_path ?? args.filePath ?? 'file')}`;
|
|
501
|
-
case 'write':
|
|
502
|
-
return `write ${(args.path ?? args.file ?? args.filePath ?? 'file')}`;
|
|
503
|
-
case 'edit':
|
|
504
|
-
return `edit ${(args.path ?? args.file ?? args.filePath ?? 'file')}`;
|
|
505
|
-
case 'exec':
|
|
506
|
-
return `exec ${String(args.command ?? '').slice(0, 80) || 'command'}`;
|
|
507
|
-
case 'web_search':
|
|
508
|
-
return `web_search ${String(args.query ?? '').slice(0, 80) || 'query'}`;
|
|
509
|
-
case 'web_fetch':
|
|
510
|
-
return `web_fetch ${String(args.url ?? '').slice(0, 80) || 'url'}`;
|
|
511
|
-
case 'sessions_send':
|
|
512
|
-
return `sessions_send ${String(args.sessionKey ?? args.label ?? '').slice(0, 80) || 'target'}`;
|
|
513
|
-
case 'memory_search':
|
|
514
|
-
return `memory_search ${String(args.query ?? '').slice(0, 80) || 'query'}`;
|
|
515
|
-
default:
|
|
516
|
-
return name;
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* Strip OpenClaw's external-content security wrapper from tool results before truncation.
|
|
521
|
-
* web_fetch results are wrapped in <<<BEGIN_EXTERNAL_UNTRUSTED_CONTENT ... >>> blocks.
|
|
522
|
-
* That preamble consumes the entire head budget in truncateWithHeadTail, leaving only
|
|
523
|
-
* the security notice + last sentence visible — the actual body becomes the middle marker.
|
|
524
|
-
* Strip the wrapper first so truncation operates on the real content.
|
|
525
|
-
*/
|
|
526
|
-
function stripSecurityPreamble(content) {
|
|
527
|
-
// Match: <<<BEGIN_EXTERNAL_UNTRUSTED_CONTENT id="...">\n...\n<<<END_EXTERNAL_UNTRUSTED_CONTENT id="...">>>
|
|
528
|
-
// Strip opening tag line and closing tag line; keep the content between.
|
|
529
|
-
const stripped = content.replace(/^[\s\S]*?<<<BEGIN_EXTERNAL_UNTRUSTED_CONTENT[^\n]*>>>?\n?/, '').replace(/\n?<<<END_EXTERNAL_UNTRUSTED_CONTENT[^\n]*>>>?[\s\S]*$/, '');
|
|
530
|
-
// If stripping removed everything or nearly everything, return original.
|
|
531
|
-
return stripped.trim().length > 20 ? stripped.trim() : content;
|
|
532
|
-
}
|
|
533
|
-
// Minimum floor: if trimming would leave less than 30% of original content, return a
|
|
534
|
-
// stripped sentinel instead of a misleading fragment. A partial result that looks
|
|
535
|
-
// complete is worse than a clear signal that the result was dropped.
|
|
536
|
-
// Applied only in applyTierPayloadCap (pressure-driven trimming), not in structural
|
|
537
|
-
// truncation paths where head+tail is always semantically useful.
|
|
538
|
-
const TOOL_GRADIENT_MIN_USEFUL_FRACTION = 0.30;
|
|
539
|
-
function truncateWithHeadTail(content, maxChars, maxTailChars = TOOL_GRADIENT_MAX_TAIL_CHARS) {
|
|
540
|
-
if (content.length <= maxChars)
|
|
541
|
-
return content;
|
|
542
|
-
const tailBudget = Math.min(Math.floor(maxChars * 0.30), maxTailChars);
|
|
543
|
-
const headBudget = Math.max(0, maxChars - tailBudget - TOOL_GRADIENT_MIDDLE_MARKER.length);
|
|
544
|
-
return content.slice(0, headBudget) + TOOL_GRADIENT_MIDDLE_MARKER + content.slice(-tailBudget);
|
|
545
|
-
}
|
|
546
|
-
function truncateHead(content, maxChars) {
|
|
547
|
-
if (content.length <= maxChars)
|
|
548
|
-
return content;
|
|
549
|
-
const marker = '…';
|
|
550
|
-
const keep = Math.max(0, maxChars - marker.length);
|
|
551
|
-
return content.slice(0, keep) + marker;
|
|
552
|
-
}
|
|
553
|
-
function firstNonEmptyLine(content) {
|
|
554
|
-
const line = content.split('\n').find(l => l.trim().length > 0) ?? '';
|
|
555
|
-
return line.trim();
|
|
556
|
-
}
|
|
557
|
-
function normalizeInline(text) {
|
|
558
|
-
return text.replace(/\s+/g, ' ').trim();
|
|
559
|
-
}
|
|
560
|
-
function hostFromUrl(raw) {
|
|
561
|
-
try {
|
|
562
|
-
return new URL(raw).hostname;
|
|
563
|
-
}
|
|
564
|
-
catch {
|
|
565
|
-
return raw;
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
function extractTopHeading(content) {
|
|
569
|
-
const heading = content.split('\n').find(line => /^#{1,3}\s+/.test(line.trim()));
|
|
570
|
-
return heading ? heading.replace(/^#{1,3}\s+/, '').trim() : '';
|
|
571
|
-
}
|
|
572
|
-
function extractExitCode(content) {
|
|
573
|
-
const match = content.match(/(?:exit code|exit|code)\s*[:=]?\s*(\d+)/i);
|
|
574
|
-
return match ? match[1] : null;
|
|
575
|
-
}
|
|
576
|
-
function estimateSearchResultCount(content) {
|
|
577
|
-
const jsonMatch = content.match(/"results"\s*:\s*\[/);
|
|
578
|
-
if (jsonMatch) {
|
|
579
|
-
const titles = content.match(/"title"\s*:/g);
|
|
580
|
-
if (titles?.length)
|
|
581
|
-
return titles.length;
|
|
582
|
-
}
|
|
583
|
-
const resultLines = content.match(/\bSource:\b|\bsiteName\b|\btitle\b/gi);
|
|
584
|
-
return resultLines?.length ? Math.min(resultLines.length, 20) : null;
|
|
585
|
-
}
|
|
586
|
-
function summarizeOutcome(label, content, maxChars) {
|
|
587
|
-
const firstLine = firstNonEmptyLine(content);
|
|
588
|
-
const base = firstLine ? `${label} — ${firstLine}` : `${label} — ${content.length} chars`;
|
|
589
|
-
return truncateHead(base, maxChars);
|
|
590
|
-
}
|
|
591
|
-
function summarizeToolInteraction(name, args, content, maxChars, compact = false) {
|
|
592
|
-
const line = normalizeInline(firstNonEmptyLine(content));
|
|
593
|
-
switch (name) {
|
|
594
|
-
case 'read': {
|
|
595
|
-
const path = String(args.path ?? args.file_path ?? args.filePath ?? 'file');
|
|
596
|
-
const heading = extractTopHeading(content);
|
|
597
|
-
const detail = heading || line || `${content.length} chars`;
|
|
598
|
-
return truncateHead(`Read ${path} — ${detail}`, maxChars);
|
|
599
|
-
}
|
|
600
|
-
case 'exec': {
|
|
601
|
-
const cmd = String(args.command ?? 'command').slice(0, compact ? 40 : 80);
|
|
602
|
-
const exitCode = extractExitCode(content);
|
|
603
|
-
const status = exitCode ? `exit ${exitCode}` : (/(error|failed|timeout|timed out)/i.test(content) ? 'failed' : 'completed');
|
|
604
|
-
const detail = line && !/^exit\s+\d+$/i.test(line) ? `, ${line}` : '';
|
|
605
|
-
return truncateHead(`Ran ${cmd} — ${status}${detail}`, maxChars);
|
|
606
|
-
}
|
|
607
|
-
case 'web_search': {
|
|
608
|
-
const query = String(args.query ?? 'query').slice(0, compact ? 40 : 80);
|
|
609
|
-
const count = estimateSearchResultCount(content);
|
|
610
|
-
const heading = extractTopHeading(content);
|
|
611
|
-
const detail = heading || line;
|
|
612
|
-
const countText = count ? ` — ${count} results` : '';
|
|
613
|
-
const summary = compact
|
|
614
|
-
? `Searched '${query}'${countText}`
|
|
615
|
-
: `Searched '${query}'${countText}${detail ? `, top: ${detail}` : ''}`;
|
|
616
|
-
return truncateHead(summary, maxChars);
|
|
617
|
-
}
|
|
618
|
-
case 'web_fetch': {
|
|
619
|
-
const url = String(args.url ?? 'url');
|
|
620
|
-
const host = hostFromUrl(url);
|
|
621
|
-
const heading = extractTopHeading(content);
|
|
622
|
-
const detail = heading || line || `${content.length} chars`;
|
|
623
|
-
return truncateHead(`Fetched ${host} — ${detail}`, maxChars);
|
|
624
|
-
}
|
|
625
|
-
case 'memory_search': {
|
|
626
|
-
const query = String(args.query ?? 'query').slice(0, compact ? 40 : 80);
|
|
627
|
-
const count = estimateSearchResultCount(content);
|
|
628
|
-
return truncateHead(`Searched memory for '${query}'${count ? ` — ${count} hits` : ''}${line ? `, top: ${line}` : ''}`, maxChars);
|
|
629
|
-
}
|
|
630
|
-
default: {
|
|
631
|
-
const label = toolLabelFromCall(name, args);
|
|
632
|
-
return compact
|
|
633
|
-
? truncateHead(`${label} — ${line || `${content.length} chars`}`, maxChars)
|
|
634
|
-
: (() => {
|
|
635
|
-
const prefix = `[${label}] `;
|
|
636
|
-
const available = Math.max(40, maxChars - prefix.length);
|
|
637
|
-
return prefix + truncateWithHeadTail(content, available);
|
|
638
|
-
})();
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
function buildTier2Envelope(label, content, maxChars, name, args) {
|
|
643
|
-
if (name && args)
|
|
644
|
-
return summarizeToolInteraction(name, args, content, maxChars, false);
|
|
645
|
-
const prefix = `[${label}] `;
|
|
646
|
-
const available = Math.max(40, maxChars - prefix.length);
|
|
647
|
-
return prefix + truncateWithHeadTail(content, available);
|
|
648
|
-
}
|
|
649
|
-
function buildTier3Envelope(label, content, maxChars, name, args) {
|
|
650
|
-
if (name && args)
|
|
651
|
-
return `[${summarizeToolInteraction(name, args, content, maxChars - 2, true)}]`;
|
|
652
|
-
return `[${summarizeOutcome(label, content, maxChars - 2)}]`;
|
|
653
|
-
}
|
|
654
|
-
/**
|
|
655
|
-
* Extract a heuristic prose summary from a tool call/result pair.
|
|
656
|
-
* Used when tool payloads are removed but continuity should remain.
|
|
657
|
-
*/
|
|
658
|
-
function extractToolProseSummary(msg, perResultCap, compact = false) {
|
|
659
|
-
const parts = [];
|
|
660
|
-
if (msg.toolCalls && msg.toolCalls.length > 0) {
|
|
661
|
-
for (const tc of msg.toolCalls) {
|
|
662
|
-
const args = parseToolArgs(tc.arguments);
|
|
663
|
-
const label = toolLabelFromCall(tc.name, args);
|
|
664
|
-
const resultContent = msg.toolResults?.find(r => r.callId === tc.id)?.content ?? '';
|
|
665
|
-
if (resultContent) {
|
|
666
|
-
parts.push(compact
|
|
667
|
-
? buildTier3Envelope(label, resultContent, perResultCap, tc.name, args)
|
|
668
|
-
: buildTier2Envelope(label, resultContent, perResultCap, tc.name, args));
|
|
669
|
-
}
|
|
670
|
-
else {
|
|
671
|
-
parts.push(compact ? `[${truncateHead(label, perResultCap - 2)}]` : label);
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
else if (msg.toolResults && msg.toolResults.length > 0) {
|
|
676
|
-
for (const tr of msg.toolResults) {
|
|
677
|
-
const label = tr.name || 'tool_result';
|
|
678
|
-
const args = {};
|
|
679
|
-
parts.push(compact
|
|
680
|
-
? buildTier3Envelope(label, tr.content ?? '', perResultCap, tr.name || 'tool_result', args)
|
|
681
|
-
: buildTier2Envelope(label, tr.content ?? '', perResultCap, tr.name || 'tool_result', args));
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
return truncateHead(parts.join('; '), Math.max(perResultCap, 120));
|
|
685
|
-
}
|
|
686
|
-
function appendToolSummary(textContent, summary) {
|
|
687
|
-
const existing = textContent ?? '';
|
|
688
|
-
if (!summary)
|
|
689
|
-
return existing;
|
|
690
|
-
return existing ? `${existing}\n[Tools: ${summary}]` : summary;
|
|
691
|
-
}
|
|
692
|
-
function getTurnAge(messages, index) {
|
|
693
|
-
let turnAge = 0;
|
|
694
|
-
for (let i = messages.length - 1; i > index; i--) {
|
|
695
|
-
const candidate = messages[i];
|
|
696
|
-
if (candidate?.role === 'user' && (!candidate.toolResults || candidate.toolResults.length === 0)) {
|
|
697
|
-
turnAge++;
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
return turnAge;
|
|
701
|
-
}
|
|
702
|
-
function hasToolContent(msg) {
|
|
703
|
-
return Boolean((msg.toolCalls && msg.toolCalls.length > 0) || (msg.toolResults && msg.toolResults.length > 0));
|
|
704
|
-
}
|
|
705
|
-
function resolveToolPlanningWindow(totalWindowTokens) {
|
|
706
|
-
const actualWindow = totalWindowTokens && totalWindowTokens > 0
|
|
707
|
-
? totalWindowTokens
|
|
708
|
-
: TOOL_PLANNING_BASELINE_WINDOW;
|
|
709
|
-
return Math.min(actualWindow, TOOL_PLANNING_BASELINE_WINDOW);
|
|
710
|
-
}
|
|
711
|
-
function computeToolPressureState(messages, totalWindowTokens) {
|
|
712
|
-
const planningWindowTokens = resolveToolPlanningWindow(totalWindowTokens);
|
|
713
|
-
const reserveTokens = Math.max(TOOL_PLANNING_MIN_RESERVE_TOKENS, Math.floor(planningWindowTokens * 0.10));
|
|
714
|
-
const usedTokens = messages.reduce((sum, msg) => sum + estimateMessageTokens(msg), 0);
|
|
715
|
-
const projectedTokens = usedTokens + reserveTokens;
|
|
716
|
-
const occupancy = planningWindowTokens > 0 ? projectedTokens / planningWindowTokens : 1;
|
|
717
|
-
let zone = 'green';
|
|
718
|
-
if (occupancy > TOOL_PRESSURE_RED)
|
|
719
|
-
zone = 'red';
|
|
720
|
-
else if (occupancy > TOOL_PRESSURE_ORANGE)
|
|
721
|
-
zone = 'orange';
|
|
722
|
-
else if (occupancy > TOOL_PRESSURE_YELLOW)
|
|
723
|
-
zone = 'yellow';
|
|
724
|
-
return {
|
|
725
|
-
planningWindowTokens,
|
|
726
|
-
reserveTokens,
|
|
727
|
-
projectedTokens,
|
|
728
|
-
occupancy,
|
|
729
|
-
zone,
|
|
730
|
-
};
|
|
731
|
-
}
|
|
732
|
-
function isStructuredTrimNote(content) {
|
|
733
|
-
return content.startsWith(TOOL_TRIM_NOTE_PREFIX);
|
|
734
|
-
}
|
|
735
|
-
function buildRecentTrimNote(originalChars, keptHeadChars, keptTailChars, pressure, resultId) {
|
|
736
|
-
const parts = [
|
|
737
|
-
TOOL_TRIM_NOTE_PREFIX,
|
|
738
|
-
'partial_result=true',
|
|
739
|
-
'reason=oversize_turn0_trim',
|
|
740
|
-
`original_chars=${originalChars}`,
|
|
741
|
-
`kept_head_chars=${keptHeadChars}`,
|
|
742
|
-
`kept_tail_chars=${keptTailChars}`,
|
|
743
|
-
`projected_occupancy_pct=${Math.round(pressure.occupancy * 100)}`,
|
|
744
|
-
`planning_window_tokens=${pressure.planningWindowTokens}`,
|
|
745
|
-
`reserve_tokens=${pressure.reserveTokens}`,
|
|
746
|
-
'retry_recommended=true',
|
|
747
|
-
];
|
|
748
|
-
if (resultId)
|
|
749
|
-
parts.push(`result_id=${resultId}`);
|
|
750
|
-
parts.push(']');
|
|
751
|
-
return parts.join(' ');
|
|
752
|
-
}
|
|
753
|
-
function countHeadTailChars(content) {
|
|
754
|
-
const markerIdx = content.indexOf(TOOL_GRADIENT_MIDDLE_MARKER);
|
|
755
|
-
if (markerIdx === -1) {
|
|
756
|
-
return { headChars: content.length, tailChars: 0 };
|
|
757
|
-
}
|
|
758
|
-
return {
|
|
759
|
-
headChars: markerIdx,
|
|
760
|
-
tailChars: content.length - markerIdx - TOOL_GRADIENT_MIDDLE_MARKER.length,
|
|
761
|
-
};
|
|
762
|
-
}
|
|
763
|
-
function trimRecentToolResult(content, pressure, resultId) {
|
|
764
|
-
if (isStructuredTrimNote(content))
|
|
765
|
-
return content;
|
|
766
|
-
const stripped = stripSecurityPreamble(content);
|
|
767
|
-
const baseOriginal = stripped.length > 0 ? stripped : content;
|
|
768
|
-
const noteSkeleton = buildRecentTrimNote(baseOriginal.length, 0, 0, pressure, resultId);
|
|
769
|
-
const availableChars = Math.max(2_000, TOOL_RECENT_OVERSIZE_TARGET_CHARS - noteSkeleton.length - 1);
|
|
770
|
-
const truncated = truncateWithHeadTail(baseOriginal, availableChars, TOOL_RECENT_OVERSIZE_MAX_TAIL_CHARS);
|
|
771
|
-
const { headChars, tailChars } = countHeadTailChars(truncated);
|
|
772
|
-
const note = buildRecentTrimNote(baseOriginal.length, headChars, tailChars, pressure, resultId);
|
|
773
|
-
return `${note}
|
|
774
|
-
${truncated}`;
|
|
775
|
-
}
|
|
776
|
-
function protectRecentToolContent(msg, pressure) {
|
|
777
|
-
if (!msg.toolResults || msg.toolResults.length === 0)
|
|
778
|
-
return msg;
|
|
779
|
-
const shouldEmergencyTrim = pressure.zone === 'orange' || pressure.zone === 'red';
|
|
780
|
-
const toolResults = msg.toolResults.map(result => {
|
|
781
|
-
const content = result.content ?? '';
|
|
782
|
-
if (!content)
|
|
783
|
-
return result;
|
|
784
|
-
if (!shouldEmergencyTrim)
|
|
785
|
-
return result;
|
|
786
|
-
if (content.length <= TOOL_RECENT_OVERSIZE_CHAR_THRESHOLD)
|
|
787
|
-
return result;
|
|
788
|
-
return {
|
|
789
|
-
...result,
|
|
790
|
-
content: trimRecentToolResult(content, pressure, result.callId || result.name || undefined),
|
|
791
|
-
};
|
|
792
|
-
});
|
|
793
|
-
return { ...msg, toolResults };
|
|
794
|
-
}
|
|
795
|
-
function applyTierPayloadCap(msg, perResultCap, perTurnCap, usedSoFar = 0, maxTailChars = TOOL_GRADIENT_MAX_TAIL_CHARS) {
|
|
796
|
-
const toolResults = msg.toolResults?.map(result => {
|
|
797
|
-
let content = result.content ?? '';
|
|
798
|
-
if (content.length > perResultCap) {
|
|
799
|
-
// Strip security preamble before truncation so it doesn't consume the head budget.
|
|
800
|
-
// web_fetch results wrapped in <<<EXTERNAL_UNTRUSTED_CONTENT>>> blocks would otherwise
|
|
801
|
-
// render the truncated result as: [security notice] + [middle marker] + [last line].
|
|
802
|
-
const stripped = stripSecurityPreamble(content);
|
|
803
|
-
// Floor check (TUNE-015): if the cap would leave less than 30% of the stripped content
|
|
804
|
-
// AND less than 2000 chars absolute, return a sentinel instead of a misleading fragment.
|
|
805
|
-
// Partial results that look complete are worse than a clear dropped-result signal.
|
|
806
|
-
// The absolute floor prevents the sentinel from firing on large natural truncations
|
|
807
|
-
// (e.g., 110k → 16k is a meaningful slice, not a misleading fragment).
|
|
808
|
-
if (perResultCap < stripped.length * TOOL_GRADIENT_MIN_USEFUL_FRACTION && perResultCap < 2_000) {
|
|
809
|
-
content = `[result too large for current context budget \u2014 ${stripped.length} chars stripped]`;
|
|
810
|
-
}
|
|
811
|
-
else {
|
|
812
|
-
// Reserve space for the \n[trimmed] marker within the cap so the total
|
|
813
|
-
// content length stays within perResultCap and doesn't overflow the
|
|
814
|
-
// per-turn aggregate cap when multiple results are truncated.
|
|
815
|
-
const TRIMMED_MARKER = '\n[trimmed]';
|
|
816
|
-
content = truncateWithHeadTail(stripped, perResultCap - TRIMMED_MARKER.length, maxTailChars) + TRIMMED_MARKER;
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
return { ...result, content };
|
|
820
|
-
}) ?? null;
|
|
821
|
-
let usedChars = usedSoFar + (toolResults?.reduce((sum, r) => sum + (r.content?.length ?? 0), 0) ?? 0);
|
|
822
|
-
if (perTurnCap != null && usedChars > perTurnCap) {
|
|
823
|
-
const downgradeSummary = extractToolProseSummary(msg, TOOL_GRADIENT_T2_CHAR_CAP, false);
|
|
824
|
-
return {
|
|
825
|
-
msg: {
|
|
826
|
-
...msg,
|
|
827
|
-
textContent: appendToolSummary(msg.textContent, downgradeSummary),
|
|
828
|
-
toolCalls: null,
|
|
829
|
-
toolResults: null,
|
|
830
|
-
},
|
|
831
|
-
usedChars: usedSoFar + downgradeSummary.length,
|
|
832
|
-
};
|
|
833
|
-
}
|
|
834
|
-
return {
|
|
835
|
-
msg: { ...msg, toolResults },
|
|
836
|
-
usedChars,
|
|
837
|
-
};
|
|
838
|
-
}
|
|
839
|
-
/**
|
|
840
|
-
* Evict tool results exceeding 800 tokens (~3200 chars) before the history
|
|
841
|
-
* budget-fit loop. Large stale results waste budget; replace them with a
|
|
842
|
-
* stub so consumers know the result existed and can re-run if needed.
|
|
843
|
-
*
|
|
844
|
-
* Applied to the already-gradient-processed history before window selection.
|
|
845
|
-
* Does NOT affect turn 0 or turn 1.
|
|
846
|
-
*/
|
|
847
|
-
const TOOL_RESULT_EVICTION_CHAR_THRESHOLD = 3_200; // ~800 tokens at 4 chars/token
|
|
848
|
-
function evictLargeToolResults(messages) {
|
|
849
|
-
return messages.map((msg, idx) => {
|
|
850
|
-
// Never evict from the protected recent-turn window.
|
|
851
|
-
const turnAge = getTurnAge(messages, idx);
|
|
852
|
-
if (turnAge <= TOOL_GRADIENT_T0_TURNS)
|
|
853
|
-
return msg;
|
|
854
|
-
if (!msg.toolResults || msg.toolResults.length === 0)
|
|
855
|
-
return msg;
|
|
856
|
-
const evicted = msg.toolResults.map(result => {
|
|
857
|
-
const content = result.content ?? '';
|
|
858
|
-
if (content.length <= TOOL_RESULT_EVICTION_CHAR_THRESHOLD)
|
|
859
|
-
return result;
|
|
860
|
-
const approxKTokens = Math.round(content.length / 4 / 1000);
|
|
861
|
-
return {
|
|
862
|
-
...result,
|
|
863
|
-
content: formatToolChainStub({
|
|
864
|
-
name: result.name || 'tool_result',
|
|
865
|
-
id: result.callId || 'unknown',
|
|
866
|
-
status: 'ejected',
|
|
867
|
-
reason: 'eviction_oversize',
|
|
868
|
-
summary: `~${approxKTokens}k tokens, use memory_search or re-run if needed`,
|
|
869
|
-
}),
|
|
870
|
-
};
|
|
871
|
-
});
|
|
872
|
-
return { ...msg, toolResults: evicted };
|
|
873
|
-
});
|
|
874
|
-
}
|
|
875
|
-
// ─── C2: Oversized artifact handling ────────────────────────────────────────
|
|
876
|
-
/**
|
|
877
|
-
* C2: Resolve the artifact oversize threshold (in tokens) for the current compose pass.
|
|
878
|
-
*
|
|
879
|
-
* The threshold scales with the effective model budget from B4 so:
|
|
880
|
-
* - Small-window models (16k–32k effective) get a proportionally tighter threshold
|
|
881
|
-
* (threshold = budget × ARTIFACT_OVERSIZE_FRACTION, floor 500, ceiling 8000).
|
|
882
|
-
* - Large-window models (200k+) get a higher ceiling but it still stays bounded
|
|
883
|
-
* so artifacts never fill the lane unconditionally.
|
|
884
|
-
*
|
|
885
|
-
* ARTIFACT_BUDGET_FRACTION: fraction of the soft budget above which a single
|
|
886
|
-
* retrieved artifact/chunk is considered oversized. Default 0.10 (10%).
|
|
887
|
-
*
|
|
888
|
-
* Headroom preservation comes from replacing the oversized artifact with a cheap
|
|
889
|
-
* reference, not from shrinking the threshold itself.
|
|
890
|
-
*/
|
|
891
|
-
const ARTIFACT_BUDGET_FRACTION = 0.10; // 10% of soft budget is the raw threshold
|
|
892
|
-
const ARTIFACT_THRESHOLD_FLOOR = 500; // never below 500 tokens (~2k chars)
|
|
893
|
-
const ARTIFACT_THRESHOLD_CEILING = 8_000; // never above 8k tokens (~32k chars)
|
|
894
|
-
export function resolveArtifactOversizeThreshold(effectiveBudget) {
|
|
895
|
-
const { softBudget } = resolveTrimBudgets(effectiveBudget);
|
|
896
|
-
const raw = Math.floor(softBudget * ARTIFACT_BUDGET_FRACTION);
|
|
897
|
-
return Math.min(ARTIFACT_THRESHOLD_CEILING, Math.max(ARTIFACT_THRESHOLD_FLOOR, raw));
|
|
898
|
-
}
|
|
899
|
-
/**
|
|
900
|
-
* C2: Degrade an oversized doc chunk to a canonical ArtifactRef string.
|
|
901
|
-
*
|
|
902
|
-
* When a retrieved chunk's content exceeds the oversize threshold (in tokens),
|
|
903
|
-
* replace it with a fetchable canonical reference instead of injecting raw content.
|
|
904
|
-
* This preserves headroom in the lane instead of filling it with a large payload.
|
|
905
|
-
*
|
|
906
|
-
* Returns:
|
|
907
|
-
* - `null` → content is within the threshold; caller should inject as-is.
|
|
908
|
-
* - `string` → canonical artifact reference; caller should inject this instead of raw content.
|
|
909
|
-
*
|
|
910
|
-
* The sizeTokens reported in the reference is the ACTUAL estimated size so downstream
|
|
911
|
-
* tooling can make informed decisions about whether to fetch.
|
|
912
|
-
*/
|
|
913
|
-
export function degradeOversizedDocChunk(chunkId, sourcePath, content, thresholdTokens) {
|
|
914
|
-
const contentTokens = estimateTokens(content);
|
|
915
|
-
if (contentTokens <= thresholdTokens)
|
|
916
|
-
return null;
|
|
917
|
-
const ref = {
|
|
918
|
-
id: chunkId,
|
|
919
|
-
path: sourcePath,
|
|
920
|
-
sizeTokens: contentTokens,
|
|
921
|
-
status: 'degraded',
|
|
922
|
-
reason: 'artifact_oversize',
|
|
923
|
-
fetchHint: 'memory_search or re-read source file',
|
|
924
|
-
};
|
|
925
|
-
return formatArtifactRef(ref);
|
|
926
|
-
}
|
|
927
|
-
/**
|
|
928
|
-
* C2: Resolve oversized artifacts in a history message array.
|
|
929
|
-
*
|
|
930
|
-
* Scans the message array and replaces user/assistant messages whose text content
|
|
931
|
-
* exceeds the model-aware artifact oversize threshold with canonical ArtifactRef
|
|
932
|
-
* strings. System messages, tool-call messages, and tool-result messages are always
|
|
933
|
-
* passed through unchanged.
|
|
934
|
-
*
|
|
935
|
-
* @param messages — neutral message array (already-assembled history window)
|
|
936
|
-
* @param effectiveBudget — effective model budget from B4 (drives the threshold)
|
|
937
|
-
* @returns { messages, refCount, tokensSaved }
|
|
938
|
-
*/
|
|
939
|
-
export function resolveOversizedArtifacts(messages, effectiveBudget) {
|
|
940
|
-
const thresholdTokens = resolveArtifactOversizeThreshold(effectiveBudget);
|
|
941
|
-
let refCount = 0;
|
|
942
|
-
let tokensSaved = 0;
|
|
943
|
-
const out = messages.map(msg => {
|
|
944
|
-
// System messages are never degraded (they are in the stable prefix).
|
|
945
|
-
if (msg.role === 'system')
|
|
946
|
-
return msg;
|
|
947
|
-
// Tool content (calls/results) is C1's domain — never touch here.
|
|
948
|
-
if (msg.toolResults || msg.toolCalls)
|
|
949
|
-
return msg;
|
|
950
|
-
const text = msg.textContent ?? '';
|
|
951
|
-
// Already a ref — idempotent; don't re-degrade.
|
|
952
|
-
if (isArtifactRef(text))
|
|
953
|
-
return msg;
|
|
954
|
-
const contentTokens = estimateTokens(text);
|
|
955
|
-
if (contentTokens <= thresholdTokens)
|
|
956
|
-
return msg;
|
|
957
|
-
// Oversized — replace with canonical artifact reference.
|
|
958
|
-
const meta = msg;
|
|
959
|
-
const id = (typeof meta['_artifactId'] === 'string' ? meta['_artifactId'] : null)
|
|
960
|
-
?? `msg-${createHash('sha1').update(`${msg.role}:${text}`).digest('hex').slice(0, 12)}`;
|
|
961
|
-
const path = (typeof meta['_artifactPath'] === 'string' ? meta['_artifactPath'] : null)
|
|
962
|
-
?? '/unknown/artifact';
|
|
963
|
-
const ref = {
|
|
964
|
-
id,
|
|
965
|
-
path,
|
|
966
|
-
sizeTokens: contentTokens,
|
|
967
|
-
status: 'degraded',
|
|
968
|
-
reason: 'artifact_oversize',
|
|
969
|
-
fetchHint: 'memory_search',
|
|
970
|
-
};
|
|
971
|
-
const refText = formatArtifactRef(ref);
|
|
972
|
-
const refTokens = estimateTokens(refText);
|
|
973
|
-
tokensSaved += contentTokens - refTokens;
|
|
974
|
-
refCount++;
|
|
975
|
-
return { ...msg, textContent: refText };
|
|
976
|
-
});
|
|
977
|
-
return { messages: out, refCount, tokensSaved };
|
|
978
|
-
}
|
|
979
|
-
/**
|
|
980
|
-
* C1: Centralized tool-chain dependency ejection.
|
|
981
|
-
*
|
|
982
|
-
* Given a set of tool-use message indices that are being ejected from the
|
|
983
|
-
* context window, this function ensures that no orphaned tool-results survive:
|
|
984
|
-
*
|
|
985
|
-
* - For each ejected assistant message carrying toolCalls, collect the set
|
|
986
|
-
* of call IDs being removed.
|
|
987
|
-
* - Walk the remaining messages: if a message's toolResults reference any
|
|
988
|
-
* of those ejected IDs:
|
|
989
|
-
* a) If the message carries ONLY tool-results and no other text, co-eject
|
|
990
|
-
* it (remove it entirely). This is the zero-cost path.
|
|
991
|
-
* b) If the message also carries text content, replace only the dependent
|
|
992
|
-
* toolResults entries with canonical ToolChainStub strings so the
|
|
993
|
-
* message is not silently mutilated.
|
|
994
|
-
*
|
|
995
|
-
* The caller is responsible for removing the ejected messages by index BEFORE
|
|
996
|
-
* or AFTER calling this function; this function operates on the full array and
|
|
997
|
-
* marks the ejected indices for removal, returning the cleaned result.
|
|
998
|
-
*
|
|
999
|
-
* @param messages Full message array (order preserved)
|
|
1000
|
-
* @param ejectIndices Set of indices into `messages` that are being ejected
|
|
1001
|
-
* (these are the tool-use / assistant messages being removed).
|
|
1002
|
-
* @param reason DegradationReason to embed in any canonical stubs.
|
|
1003
|
-
* @returns Cleaned message array + telemetry counters.
|
|
1004
|
-
*/
|
|
1005
|
-
export function resolveToolChainEjections(messages, ejectIndices, reason = 'eviction_oversize') {
|
|
1006
|
-
// Collect all tool-call IDs that are being ejected.
|
|
1007
|
-
const ejectedCallIds = new Set();
|
|
1008
|
-
for (const idx of ejectIndices) {
|
|
1009
|
-
const msg = messages[idx];
|
|
1010
|
-
if (!msg)
|
|
1011
|
-
continue;
|
|
1012
|
-
if (msg.toolCalls) {
|
|
1013
|
-
for (const tc of msg.toolCalls) {
|
|
1014
|
-
if (tc.id)
|
|
1015
|
-
ejectedCallIds.add(tc.id);
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
let coEjections = 0;
|
|
1020
|
-
let stubReplacements = 0;
|
|
1021
|
-
// If no call IDs were ejected, nothing to do beyond dropping the ejected messages.
|
|
1022
|
-
if (ejectedCallIds.size === 0) {
|
|
1023
|
-
const result = messages.filter((_, idx) => !ejectIndices.has(idx));
|
|
1024
|
-
return { messages: result, coEjections, stubReplacements };
|
|
1025
|
-
}
|
|
1026
|
-
// Walk all messages and handle dependent tool-results.
|
|
1027
|
-
const transformed = messages.map((msg, idx) => {
|
|
1028
|
-
// Already being ejected — remove.
|
|
1029
|
-
if (ejectIndices.has(idx))
|
|
1030
|
-
return null;
|
|
1031
|
-
if (!msg.toolResults || msg.toolResults.length === 0)
|
|
1032
|
-
return msg;
|
|
1033
|
-
// Determine which results in this message depend on ejected calls.
|
|
1034
|
-
const dependentResultIds = msg.toolResults
|
|
1035
|
-
.map(r => r.callId)
|
|
1036
|
-
.filter((id) => Boolean(id) && ejectedCallIds.has(id));
|
|
1037
|
-
if (dependentResultIds.length === 0)
|
|
1038
|
-
return msg;
|
|
1039
|
-
const dependentSet = new Set(dependentResultIds);
|
|
1040
|
-
// Case (a): The message carries ONLY tool-results and no other text content,
|
|
1041
|
-
// and ALL of its results are dependent on ejected calls.
|
|
1042
|
-
// Co-eject the whole message — zero budget cost, no stub needed.
|
|
1043
|
-
const hasText = Boolean(msg.textContent && msg.textContent.trim().length > 0);
|
|
1044
|
-
const hasNonDependentResults = msg.toolResults.some(r => !dependentSet.has(r.callId));
|
|
1045
|
-
if (!hasText && !hasNonDependentResults) {
|
|
1046
|
-
coEjections++;
|
|
1047
|
-
return null;
|
|
1048
|
-
}
|
|
1049
|
-
// Case (b): Message has text or unrelated results — stub only the dependent entries.
|
|
1050
|
-
const stubbedResults = msg.toolResults.map(result => {
|
|
1051
|
-
if (!result.callId || !dependentSet.has(result.callId))
|
|
1052
|
-
return result;
|
|
1053
|
-
const stubContent = formatToolChainStub({
|
|
1054
|
-
name: result.name || 'tool_result',
|
|
1055
|
-
id: result.callId || 'unknown',
|
|
1056
|
-
status: 'ejected',
|
|
1057
|
-
reason,
|
|
1058
|
-
summary: 'parent tool-use ejected from context window',
|
|
1059
|
-
});
|
|
1060
|
-
stubReplacements++;
|
|
1061
|
-
return { ...result, content: stubContent };
|
|
1062
|
-
});
|
|
1063
|
-
return { ...msg, toolResults: stubbedResults };
|
|
1064
|
-
});
|
|
1065
|
-
const result = transformed.filter((m) => m !== null);
|
|
1066
|
-
return { messages: result, coEjections, stubReplacements };
|
|
1067
|
-
}
|
|
1068
|
-
/**
|
|
1069
|
-
* Apply gradient tool treatment to a message array.
|
|
1070
|
-
*
|
|
1071
|
-
* Tiers are based on turn age, where turn age is the number of newer user
|
|
1072
|
-
* messages after the current message.
|
|
1073
|
-
*/
|
|
1074
|
-
function applyToolGradient(messages, opts) {
|
|
1075
|
-
const result = [...messages];
|
|
1076
|
-
const pressure = computeToolPressureState(messages, opts?.totalWindowTokens);
|
|
1077
|
-
const perTurnUsage = new Map();
|
|
1078
|
-
for (let i = result.length - 1; i >= 0; i--) {
|
|
1079
|
-
const msg = result[i];
|
|
1080
|
-
if (!hasToolContent(msg))
|
|
1081
|
-
continue;
|
|
1082
|
-
const turnAge = getTurnAge(result, i);
|
|
1083
|
-
const usage = perTurnUsage.get(turnAge) ?? { t0: 0, t1: 0, t2: 0, t3: 0 };
|
|
1084
|
-
if (turnAge <= TOOL_GRADIENT_T0_TURNS) {
|
|
1085
|
-
// T0/T1: preserve full recent tool results unless we hit the conservative
|
|
1086
|
-
// orange/red pressure zones and the payload itself is oversized (>40k).
|
|
1087
|
-
result[i] = protectRecentToolContent(msg, pressure);
|
|
1088
|
-
}
|
|
1089
|
-
else if (turnAge <= TOOL_GRADIENT_T1_TURNS) {
|
|
1090
|
-
const capped = applyTierPayloadCap(msg, TOOL_GRADIENT_T1_CHAR_CAP, TOOL_GRADIENT_T1_TURN_CAP, usage.t1);
|
|
1091
|
-
usage.t1 = capped.usedChars;
|
|
1092
|
-
result[i] = capped.msg;
|
|
1093
|
-
}
|
|
1094
|
-
else if (turnAge <= TOOL_GRADIENT_T2_TURNS) {
|
|
1095
|
-
const summary = extractToolProseSummary(msg, TOOL_GRADIENT_T2_CHAR_CAP, false);
|
|
1096
|
-
const allowed = Math.max(0, TOOL_GRADIENT_T2_TURN_CAP - usage.t2);
|
|
1097
|
-
const boundedSummary = truncateHead(summary, Math.min(TOOL_GRADIENT_T2_CHAR_CAP, allowed || TOOL_GRADIENT_T3_CHAR_CAP));
|
|
1098
|
-
usage.t2 += boundedSummary.length;
|
|
1099
|
-
result[i] = {
|
|
1100
|
-
...msg,
|
|
1101
|
-
textContent: appendToolSummary(msg.textContent, boundedSummary),
|
|
1102
|
-
toolCalls: null,
|
|
1103
|
-
toolResults: null,
|
|
1104
|
-
};
|
|
1105
|
-
}
|
|
1106
|
-
else {
|
|
1107
|
-
const summary = extractToolProseSummary(msg, TOOL_GRADIENT_T3_CHAR_CAP, true);
|
|
1108
|
-
const allowed = Math.max(0, TOOL_GRADIENT_T3_TURN_CAP - usage.t3);
|
|
1109
|
-
const boundedSummary = truncateHead(summary, Math.min(TOOL_GRADIENT_T3_CHAR_CAP, allowed || TOOL_GRADIENT_T3_CHAR_CAP));
|
|
1110
|
-
usage.t3 += boundedSummary.length;
|
|
1111
|
-
result[i] = {
|
|
1112
|
-
...msg,
|
|
1113
|
-
textContent: appendToolSummary(msg.textContent, boundedSummary),
|
|
1114
|
-
toolCalls: null,
|
|
1115
|
-
toolResults: null,
|
|
1116
|
-
};
|
|
1117
|
-
}
|
|
1118
|
-
perTurnUsage.set(turnAge, usage);
|
|
1119
|
-
}
|
|
1120
|
-
return result;
|
|
1121
|
-
}
|
|
1122
|
-
/** Guard: logRegistryStartup() fires only once per process, not per instance. */
|
|
1123
|
-
let _registryLogged = false;
|
|
1124
|
-
export class Compositor {
|
|
1125
|
-
config;
|
|
1126
|
-
cache;
|
|
1127
|
-
vectorStore;
|
|
1128
|
-
libraryDb;
|
|
1129
|
-
triggerRegistry;
|
|
1130
|
-
/** Cached org registry loaded from fleet_agents at construction time. */
|
|
1131
|
-
_orgRegistry;
|
|
1132
|
-
constructor(deps, config) {
|
|
1133
|
-
this.cache = deps.cache;
|
|
1134
|
-
this.vectorStore = deps.vectorStore || null;
|
|
1135
|
-
this.libraryDb = deps.libraryDb || null;
|
|
1136
|
-
this.triggerRegistry = deps.triggerRegistry || DEFAULT_TRIGGERS;
|
|
1137
|
-
// Load org registry from DB on init; fall back to hardcoded if DB empty.
|
|
1138
|
-
this._orgRegistry = this.libraryDb
|
|
1139
|
-
? buildOrgRegistryFromDb(this.libraryDb)
|
|
1140
|
-
: defaultOrgRegistry();
|
|
1141
|
-
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
1142
|
-
if (!_registryLogged) {
|
|
1143
|
-
logRegistryStartup();
|
|
1144
|
-
_registryLogged = true;
|
|
1145
|
-
}
|
|
1146
|
-
}
|
|
1147
|
-
/**
|
|
1148
|
-
* Set or replace the vector store after construction.
|
|
1149
|
-
* Called by hypermem.create() once sqlite-vec is confirmed available.
|
|
1150
|
-
*/
|
|
1151
|
-
setVectorStore(vs) {
|
|
1152
|
-
this.vectorStore = vs;
|
|
1153
|
-
}
|
|
1154
|
-
/**
|
|
1155
|
-
* Hot-reload the org registry from the fleet_agents table.
|
|
1156
|
-
* Call after fleet membership changes (new agent, org restructure)
|
|
1157
|
-
* to pick up the latest without a full restart.
|
|
1158
|
-
* Falls back to the current cached registry if the DB is unavailable.
|
|
1159
|
-
*/
|
|
1160
|
-
refreshOrgRegistry() {
|
|
1161
|
-
if (this.libraryDb) {
|
|
1162
|
-
this._orgRegistry = buildOrgRegistryFromDb(this.libraryDb);
|
|
1163
|
-
}
|
|
1164
|
-
return this._orgRegistry;
|
|
1165
|
-
}
|
|
1166
|
-
/**
|
|
1167
|
-
* Return the currently cached org registry.
|
|
1168
|
-
*/
|
|
1169
|
-
get orgRegistry() {
|
|
1170
|
-
return this._orgRegistry;
|
|
1171
|
-
}
|
|
1172
|
-
/**
|
|
1173
|
-
* Sprint 2.1: Hydrate tool-artifact stubs in the active turn.
|
|
1174
|
-
*
|
|
1175
|
-
* The active turn is the contiguous trailing block of tool-bearing messages
|
|
1176
|
-
* at the tail of the assembled window (positional, NOT turn_id-based):
|
|
1177
|
-
* - Walk backward from the last message
|
|
1178
|
-
* - Collect tool-bearing messages (toolCalls != null OR toolResults != null)
|
|
1179
|
-
* - Plus the bounding user message that opened the turn
|
|
1180
|
-
* - Stop at the first plain message once at least one tool message was found
|
|
1181
|
-
*
|
|
1182
|
-
* For every toolResult stub with an `artifact=<id>` pointer, look up the
|
|
1183
|
-
* full payload in ToolArtifactStore and replace the stub content in-place.
|
|
1184
|
-
* Uses a single batched `WHERE id IN (...)` lookup (no N+1 queries).
|
|
1185
|
-
* Touches `last_used_at` on every hydrated artifact in a single batch.
|
|
1186
|
-
*
|
|
1187
|
-
* Failure mode: if a lookup returns null (artifact missing), leave the stub
|
|
1188
|
-
* unchanged and increment hydrationMisses.
|
|
1189
|
-
*
|
|
1190
|
-
* Returns diagnostics counters.
|
|
1191
|
-
*/
|
|
1192
|
-
hydrateActiveTurnArtifacts(messages, db) {
|
|
1193
|
-
if (messages.length === 0) {
|
|
1194
|
-
return { artifactsHydrated: 0, hydrationBytes: 0, hydrationMisses: 0 };
|
|
1195
|
-
}
|
|
1196
|
-
const store = new ToolArtifactStore(db);
|
|
1197
|
-
// ── 1. Detect active turn (positional, backward walk) ─────────────────────
|
|
1198
|
-
// Collect indices belonging to the active turn.
|
|
1199
|
-
const activeTurnIndices = [];
|
|
1200
|
-
let foundToolBearing = false;
|
|
1201
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1202
|
-
const msg = messages[i];
|
|
1203
|
-
const isToolBearing = msg.toolCalls != null || msg.toolResults != null;
|
|
1204
|
-
if (isToolBearing) {
|
|
1205
|
-
foundToolBearing = true;
|
|
1206
|
-
activeTurnIndices.push(i);
|
|
1207
|
-
}
|
|
1208
|
-
else if (foundToolBearing) {
|
|
1209
|
-
// First plain message after at least one tool-bearing message — this
|
|
1210
|
-
// is the bounding user message that opened the turn. Include it and stop.
|
|
1211
|
-
activeTurnIndices.push(i);
|
|
1212
|
-
break;
|
|
1213
|
-
}
|
|
1214
|
-
else {
|
|
1215
|
-
// Haven't found any tool-bearing messages yet — still in non-tool tail
|
|
1216
|
-
// (e.g., the last message is a plain user message). No active turn.
|
|
1217
|
-
break;
|
|
1218
|
-
}
|
|
1219
|
-
}
|
|
1220
|
-
if (activeTurnIndices.length === 0 || !foundToolBearing) {
|
|
1221
|
-
return { artifactsHydrated: 0, hydrationBytes: 0, hydrationMisses: 0 };
|
|
1222
|
-
}
|
|
1223
|
-
// ── 2. Collect all artifactIds from stub toolResults in the active turn ───
|
|
1224
|
-
// Map: artifactId -> array of [msgIndex, resultIndex] for in-place replacement
|
|
1225
|
-
const artifactTargets = new Map();
|
|
1226
|
-
for (const msgIdx of activeTurnIndices) {
|
|
1227
|
-
const msg = messages[msgIdx];
|
|
1228
|
-
if (!msg.toolResults)
|
|
1229
|
-
continue;
|
|
1230
|
-
for (let resultIdx = 0; resultIdx < msg.toolResults.length; resultIdx++) {
|
|
1231
|
-
const result = msg.toolResults[resultIdx];
|
|
1232
|
-
const stub = parseToolChainStub(result.content);
|
|
1233
|
-
if (stub && stub.artifactId) {
|
|
1234
|
-
const existing = artifactTargets.get(stub.artifactId) ?? [];
|
|
1235
|
-
existing.push({ msgIdx, resultIdx });
|
|
1236
|
-
artifactTargets.set(stub.artifactId, existing);
|
|
1237
|
-
}
|
|
1238
|
-
}
|
|
1239
|
-
}
|
|
1240
|
-
if (artifactTargets.size === 0) {
|
|
1241
|
-
return { artifactsHydrated: 0, hydrationBytes: 0, hydrationMisses: 0 };
|
|
1242
|
-
}
|
|
1243
|
-
// ── 3. Batch lookup ────────────────────────────────────────────────────────
|
|
1244
|
-
const ids = Array.from(artifactTargets.keys());
|
|
1245
|
-
const placeholders = ids.map(() => '?').join(', ');
|
|
1246
|
-
const rows = db
|
|
1247
|
-
.prepare(`SELECT * FROM tool_artifacts WHERE id IN (${placeholders})`)
|
|
1248
|
-
.all(...ids);
|
|
1249
|
-
// Build id -> payload map
|
|
1250
|
-
const payloadMap = new Map();
|
|
1251
|
-
for (const row of rows) {
|
|
1252
|
-
payloadMap.set(row.id, row.payload);
|
|
1253
|
-
}
|
|
1254
|
-
// ── 4. Hydrate in-place ────────────────────────────────────────────────────
|
|
1255
|
-
let artifactsHydrated = 0;
|
|
1256
|
-
let hydrationBytes = 0;
|
|
1257
|
-
let hydrationMisses = 0;
|
|
1258
|
-
const touchIds = [];
|
|
1259
|
-
for (const [artifactId, targets] of artifactTargets) {
|
|
1260
|
-
const payload = payloadMap.get(artifactId);
|
|
1261
|
-
if (payload == null) {
|
|
1262
|
-
// Graceful miss — stub stays as-is
|
|
1263
|
-
hydrationMisses += targets.length;
|
|
1264
|
-
continue;
|
|
1265
|
-
}
|
|
1266
|
-
for (const { msgIdx, resultIdx } of targets) {
|
|
1267
|
-
const msg = messages[msgIdx];
|
|
1268
|
-
// Safety: if content doesn't look like a stub anymore (defensive idempotency check)
|
|
1269
|
-
const existingContent = msg.toolResults[resultIdx].content;
|
|
1270
|
-
if (!parseToolChainStub(existingContent)) {
|
|
1271
|
-
// Already full content — pass through unchanged
|
|
1272
|
-
continue;
|
|
1273
|
-
}
|
|
1274
|
-
// Replace stub with full payload
|
|
1275
|
-
msg.toolResults[resultIdx] = {
|
|
1276
|
-
...msg.toolResults[resultIdx],
|
|
1277
|
-
content: payload,
|
|
1278
|
-
};
|
|
1279
|
-
artifactsHydrated++;
|
|
1280
|
-
hydrationBytes += Buffer.byteLength(payload, 'utf8');
|
|
1281
|
-
}
|
|
1282
|
-
touchIds.push(artifactId);
|
|
1283
|
-
}
|
|
1284
|
-
// ── 5. Batch touch last_used_at ───────────────────────────────────────────
|
|
1285
|
-
if (touchIds.length > 0) {
|
|
1286
|
-
const ts = new Date().toISOString();
|
|
1287
|
-
const touchPlaceholders = touchIds.map(() => '?').join(', ');
|
|
1288
|
-
try {
|
|
1289
|
-
db.prepare(`UPDATE tool_artifacts SET last_used_at = ? WHERE id IN (${touchPlaceholders})`).run(ts, ...touchIds);
|
|
1290
|
-
}
|
|
1291
|
-
catch {
|
|
1292
|
-
// Touch is best-effort — hydration still succeeded
|
|
1293
|
-
}
|
|
1294
|
-
}
|
|
1295
|
-
return { artifactsHydrated, hydrationBytes, hydrationMisses };
|
|
1296
|
-
}
|
|
1297
|
-
/**
|
|
1298
|
-
* Compose a complete message array for sending to an LLM.
|
|
1299
|
-
*
|
|
1300
|
-
* Orchestrates all four memory layers:
|
|
1301
|
-
* 1. System prompt + identity (never truncated)
|
|
1302
|
-
* 2. Conversation history (L1 Redis → L2 messages.db)
|
|
1303
|
-
* 3. Active facts from library (L4)
|
|
1304
|
-
* 4. Knowledge entries relevant to conversation (L4)
|
|
1305
|
-
* 5. User preferences (L4)
|
|
1306
|
-
* 6. Semantic recall via vector search (L3)
|
|
1307
|
-
* 7. Cross-session context (L2)
|
|
1308
|
-
*
|
|
1309
|
-
* Each slot respects the remaining token budget.
|
|
1310
|
-
*/
|
|
1311
|
-
async compose(request, db, libraryDb) {
|
|
1312
|
-
const store = new MessageStore(db);
|
|
1313
|
-
const libDb = libraryDb || this.libraryDb;
|
|
1314
|
-
const toComposeOutputMessages = (inputMessages) => {
|
|
1315
|
-
// When skipProviderTranslation is set, compose returns the neutral window
|
|
1316
|
-
// typed as ProviderMessage[] by contract. The runtime translates later.
|
|
1317
|
-
return request.skipProviderTranslation
|
|
1318
|
-
? inputMessages
|
|
1319
|
-
: toProviderFormat(inputMessages, request.provider ?? request.model ?? null);
|
|
1320
|
-
};
|
|
1321
|
-
// ── C4: Window cache fast-exit ────────────────────────────
|
|
1322
|
-
// If nothing has changed since the last compose (cursor.lastSentId >= newest
|
|
1323
|
-
// message id in the DB), skip the full pipeline and return the cached window.
|
|
1324
|
-
// Particularly effective for low-frequency sessions (heartbeat agents, council
|
|
1325
|
-
// seats between rounds). TTL on the cache write remains 120s — this is a
|
|
1326
|
-
// conservative early-exit before the TTL expires, not a TTL extension.
|
|
1327
|
-
//
|
|
1328
|
-
// B2: prevPrefixHash is set when a cached bundle is found but bypassed due to
|
|
1329
|
-
// prefix-input mutation. It is surfaced in the full-compose diagnostics so
|
|
1330
|
-
// callers can confirm the bypass fired correctly.
|
|
1331
|
-
let _prevPrefixHashFromBypass;
|
|
1332
|
-
if (request.includeHistory !== false && request.skipWindowCache !== true) {
|
|
1333
|
-
try {
|
|
1334
|
-
const newestRow = db.prepare('SELECT MAX(id) AS maxId FROM messages WHERE agent_id = ?').get(request.agentId);
|
|
1335
|
-
const newestMsgId = newestRow?.maxId;
|
|
1336
|
-
if (newestMsgId != null) {
|
|
1337
|
-
const cachedBundle = await this.cache.getFreshWindowBundle(request.agentId, request.sessionKey, newestMsgId);
|
|
1338
|
-
if (cachedBundle) {
|
|
1339
|
-
// Validate the cached bundle is compatible with this request.
|
|
1340
|
-
// A mismatch on any of these means we must do a full compose:
|
|
1341
|
-
// - tokenBudget: cached total exceeds the requested cap
|
|
1342
|
-
// - slot flags: caller disabled slots that the cache populated
|
|
1343
|
-
// - historyDepth: caller wants fewer messages than the cache holds
|
|
1344
|
-
const cachedTotal = cachedBundle.meta.totalTokens;
|
|
1345
|
-
const budgetOk = !request.tokenBudget ||
|
|
1346
|
-
cachedTotal <= request.tokenBudget * 1.05;
|
|
1347
|
-
const factsOk = request.includeFacts !== false ||
|
|
1348
|
-
(cachedBundle.meta.slots['facts'] ?? 0) === 0;
|
|
1349
|
-
const libraryOk = request.includeLibrary !== false ||
|
|
1350
|
-
(cachedBundle.meta.slots['library'] ?? 0) === 0;
|
|
1351
|
-
const contextOk = request.includeContext !== false ||
|
|
1352
|
-
(cachedBundle.meta.slots['context'] ?? 0) === 0;
|
|
1353
|
-
// historyDepth constrains how many messages the caller wants;
|
|
1354
|
-
// we can't slice a cached bundle safely, so skip cache.
|
|
1355
|
-
const depthOk = !request.historyDepth;
|
|
1356
|
-
// B2: Stable-prefix hash check.
|
|
1357
|
-
// If the system/identity slots changed since this cache entry was
|
|
1358
|
-
// written, the stable prefix is stale even if cursor freshness
|
|
1359
|
-
// passes. Compute a cheap input hash from slot contents and compare
|
|
1360
|
-
// against the one stored in the cache meta. If no stored hash exists
|
|
1361
|
-
// (pre-B2 cache entries), fall through to prefix check on the
|
|
1362
|
-
// cached message content itself.
|
|
1363
|
-
let prefixInputOk = true;
|
|
1364
|
-
const _cachedPrefixInputHash = cachedBundle.meta.prefixInputHash;
|
|
1365
|
-
if (_cachedPrefixInputHash) {
|
|
1366
|
-
const _sysSlot = await this.cache.getSlot(request.agentId, request.sessionKey, 'system');
|
|
1367
|
-
const _idSlot = await this.cache.getSlot(request.agentId, request.sessionKey, 'identity');
|
|
1368
|
-
const _incomingInputHash = createHash('sha256')
|
|
1369
|
-
.update(_sysSlot ?? '')
|
|
1370
|
-
.update('\n␞\n')
|
|
1371
|
-
.update(_idSlot ?? '')
|
|
1372
|
-
.digest('hex');
|
|
1373
|
-
if (_incomingInputHash !== _cachedPrefixInputHash) {
|
|
1374
|
-
prefixInputOk = false;
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
if (budgetOk && factsOk && libraryOk && contextOk && depthOk && prefixInputOk) {
|
|
1378
|
-
const cachedSlots = {
|
|
1379
|
-
system: cachedBundle.meta.slots['system'] ?? 0,
|
|
1380
|
-
identity: cachedBundle.meta.slots['identity'] ?? 0,
|
|
1381
|
-
history: cachedBundle.meta.slots['history'] ?? 0,
|
|
1382
|
-
facts: cachedBundle.meta.slots['facts'] ?? 0,
|
|
1383
|
-
context: cachedBundle.meta.slots['context'] ?? 0,
|
|
1384
|
-
library: cachedBundle.meta.slots['library'] ?? 0,
|
|
1385
|
-
};
|
|
1386
|
-
// Sprint 2.1: hydrate active-turn artifact stubs before converting.
|
|
1387
|
-
const cachedHydration = this.hydrateActiveTurnArtifacts(cachedBundle.messages, db);
|
|
1388
|
-
return {
|
|
1389
|
-
messages: toComposeOutputMessages(cachedBundle.messages),
|
|
1390
|
-
tokenCount: cachedBundle.meta.totalTokens,
|
|
1391
|
-
slots: cachedSlots,
|
|
1392
|
-
truncated: false,
|
|
1393
|
-
hasWarnings: cachedBundle.meta.warnings.length > 0,
|
|
1394
|
-
warnings: cachedBundle.meta.warnings,
|
|
1395
|
-
diagnostics: {
|
|
1396
|
-
...cachedBundle.meta.diagnostics,
|
|
1397
|
-
windowCacheHit: true,
|
|
1398
|
-
// Carry forward the stored prefixHash so callers can observe it.
|
|
1399
|
-
prefixHash: cachedBundle.meta.prefixHash ?? cachedBundle.meta.diagnostics.prefixHash,
|
|
1400
|
-
artifactsHydrated: cachedHydration.artifactsHydrated > 0 ? cachedHydration.artifactsHydrated : undefined,
|
|
1401
|
-
hydrationBytes: cachedHydration.hydrationBytes > 0 ? cachedHydration.hydrationBytes : undefined,
|
|
1402
|
-
hydrationMisses: cachedHydration.hydrationMisses > 0 ? cachedHydration.hydrationMisses : undefined,
|
|
1403
|
-
},
|
|
1404
|
-
};
|
|
1405
|
-
}
|
|
1406
|
-
// Incompatible request — fall through to full compose.
|
|
1407
|
-
// Surface prevPrefixHash so the full compose diagnostics can report it.
|
|
1408
|
-
_prevPrefixHashFromBypass = cachedBundle.meta.prefixHash ?? cachedBundle.meta.diagnostics.prefixHash;
|
|
1409
|
-
}
|
|
1410
|
-
}
|
|
1411
|
-
}
|
|
1412
|
-
catch {
|
|
1413
|
-
// Cache fast-exit is best-effort, fall through to full compose
|
|
1414
|
-
}
|
|
1415
|
-
}
|
|
1416
|
-
// Dynamic reserve: use a lightweight SQLite sample to estimate avg turn cost
|
|
1417
|
-
// BEFORE assembling the full context. This gives us the reserve fraction we
|
|
1418
|
-
// need to compute the effective token budget at the start of compose.
|
|
1419
|
-
// Full history assembly happens later in the pipeline.
|
|
1420
|
-
const totalWindow = resolveModelWindow(request.model, this.config.defaultTokenBudget);
|
|
1421
|
-
const sampleConv = store.getConversation(request.sessionKey);
|
|
1422
|
-
const sampleMessages = sampleConv
|
|
1423
|
-
? store.getRecentMessages(sampleConv.id, 40)
|
|
1424
|
-
: [];
|
|
1425
|
-
const { reserve: dynamicReserve, avgTurnCost, dynamic: isDynamic, pressureHigh } = computeDynamicReserve(sampleMessages, totalWindow, this.config);
|
|
1426
|
-
const budget = request.tokenBudget || resolveModelBudget(request.model, this.config.defaultTokenBudget, dynamicReserve, this.config.budgetFraction);
|
|
1427
|
-
// B4: Model-aware lane budgets.
|
|
1428
|
-
// Resolve historyFraction and memoryFraction by blending config values toward
|
|
1429
|
-
// model-preferred fractions when the effective budget approaches the MECW ceiling.
|
|
1430
|
-
// This ensures the compositor doesn't allocate more history than the model can
|
|
1431
|
-
// reliably reason over, and adjusts the memory pool proportionally.
|
|
1432
|
-
const _b4ConfigHistoryFraction = this.config.historyFraction ?? 0.40;
|
|
1433
|
-
const _b4ConfigMemoryFraction = this.config.memoryFraction ?? 0.40;
|
|
1434
|
-
const { historyFraction: b4HistoryFraction, memoryFraction: b4MemoryFraction, mecwProfile: b4MecwProfile, mecwApplied: b4MecwApplied, mecwBlend: b4MecwBlend, } = resolveModelLaneBudgets(request.model, budget, _b4ConfigHistoryFraction, _b4ConfigMemoryFraction);
|
|
1435
|
-
// C2: Compute the artifact oversize threshold once per compose pass from the
|
|
1436
|
-
// effective model budget (from B4). Chunk injection paths consult this threshold
|
|
1437
|
-
// to degrade retrieved payloads that would fill the lane instead of injecting them.
|
|
1438
|
-
const c2ArtifactThresholdTokens = resolveArtifactOversizeThreshold(budget);
|
|
1439
|
-
let c2ArtifactDegradations = 0;
|
|
1440
|
-
// Sprint 4: Pre-compose history depth tightening.
|
|
1441
|
-
// Classify the session and compute an adaptive depth from observed message
|
|
1442
|
-
// density. This replaces the old fixed maxHistoryMessages ceiling that over-
|
|
1443
|
-
// fed the compositor for tool-heavy sessions.
|
|
1444
|
-
//
|
|
1445
|
-
// If the caller already passed historyDepth (plugin assemble path), honour it
|
|
1446
|
-
// as an explicit cap — the adaptive depth still applies as a lower bound so
|
|
1447
|
-
// we never request more than the budget can absorb.
|
|
1448
|
-
const s4SessionType = classifySessionType(sampleMessages);
|
|
1449
|
-
const s4ObservedDensity = estimateObservedMsgDensity(sampleMessages);
|
|
1450
|
-
const s4HistoryBudget = Math.floor(budget * b4HistoryFraction);
|
|
1451
|
-
const s4AdaptiveDepth = computeAdaptiveHistoryDepth(s4SessionType, s4ObservedDensity, s4HistoryBudget, this.config.maxHistoryMessages);
|
|
1452
|
-
// Effective depth: caller-provided historyDepth overrides adaptive when it is
|
|
1453
|
-
// the tighter constraint; otherwise use the adaptive depth.
|
|
1454
|
-
const s4EffectiveDepth = request.historyDepth
|
|
1455
|
-
? Math.min(request.historyDepth, s4AdaptiveDepth)
|
|
1456
|
-
: s4AdaptiveDepth;
|
|
1457
|
-
let remaining = budget;
|
|
1458
|
-
// Phase 0 fence enforcement: resolve the compaction fence for this conversation.
|
|
1459
|
-
// All downstream message queries use this as a lower bound to exclude zombie
|
|
1460
|
-
// messages below the fence that should have been compacted.
|
|
1461
|
-
let fenceMessageId;
|
|
1462
|
-
if (sampleConv) {
|
|
1463
|
-
try {
|
|
1464
|
-
ensureCompactionFenceSchema(db);
|
|
1465
|
-
const fence = getCompactionFence(db, sampleConv.id);
|
|
1466
|
-
if (fence)
|
|
1467
|
-
fenceMessageId = fence.fenceMessageId;
|
|
1468
|
-
}
|
|
1469
|
-
catch {
|
|
1470
|
-
// Fence lookup is best-effort — never fail composition
|
|
1471
|
-
}
|
|
1472
|
-
}
|
|
1473
|
-
const warnings = [];
|
|
1474
|
-
const slots = {
|
|
1475
|
-
system: 0,
|
|
1476
|
-
identity: 0,
|
|
1477
|
-
history: 0,
|
|
1478
|
-
facts: 0,
|
|
1479
|
-
context: 0,
|
|
1480
|
-
library: 0,
|
|
1481
|
-
};
|
|
1482
|
-
const messages = [];
|
|
1483
|
-
// ─── System Prompt (never truncated) ───────────────────────
|
|
1484
|
-
const systemContent = await this.getSlotContent(request.agentId, request.sessionKey, 'system', db);
|
|
1485
|
-
if (systemContent) {
|
|
1486
|
-
const tokens = estimateTokens(systemContent);
|
|
1487
|
-
messages.push({
|
|
1488
|
-
role: 'system',
|
|
1489
|
-
textContent: systemContent,
|
|
1490
|
-
toolCalls: null,
|
|
1491
|
-
toolResults: null,
|
|
1492
|
-
});
|
|
1493
|
-
slots.system = tokens;
|
|
1494
|
-
remaining -= tokens;
|
|
1495
|
-
}
|
|
1496
|
-
// ─── Identity (never truncated) ────────────────────────────
|
|
1497
|
-
const identityContent = await this.getSlotContent(request.agentId, request.sessionKey, 'identity', db);
|
|
1498
|
-
if (identityContent) {
|
|
1499
|
-
const tokens = estimateTokens(identityContent);
|
|
1500
|
-
messages.push({
|
|
1501
|
-
role: 'system',
|
|
1502
|
-
textContent: identityContent,
|
|
1503
|
-
toolCalls: null,
|
|
1504
|
-
toolResults: null,
|
|
1505
|
-
});
|
|
1506
|
-
slots.identity = tokens;
|
|
1507
|
-
remaining -= tokens;
|
|
1508
|
-
}
|
|
1509
|
-
// ─── Stable Output Profile Prefix ──────────────────────────
|
|
1510
|
-
// Keep deterministic output instructions on the static side of the cache
|
|
1511
|
-
// boundary so Anthropic and OpenAI warm-prefix caching can reuse them.
|
|
1512
|
-
if (remaining > 100 && request.includeLibrary !== false) {
|
|
1513
|
-
const fosEnabled = this.config?.enableFOS !== false;
|
|
1514
|
-
const modEnabled = this.config?.enableMOD !== false;
|
|
1515
|
-
const outputTier = resolveOutputTier((this.config?.hyperformProfile ?? this.config?.outputProfile ?? this.config?.outputStandard), fosEnabled, modEnabled);
|
|
1516
|
-
const stableOutputParts = [];
|
|
1517
|
-
if (outputTier.tier === 'light') {
|
|
1518
|
-
stableOutputParts.push(renderLightFOS().join('\n'));
|
|
1519
|
-
}
|
|
1520
|
-
else if (libDb) {
|
|
1521
|
-
if (outputTier.fos) {
|
|
1522
|
-
const fos = getActiveFOS(libDb);
|
|
1523
|
-
if (fos) {
|
|
1524
|
-
const fosContent = renderFOS(fos).join('\n');
|
|
1525
|
-
if (fosContent.trim())
|
|
1526
|
-
stableOutputParts.push(fosContent);
|
|
1527
|
-
}
|
|
1528
|
-
}
|
|
1529
|
-
if (outputTier.mod) {
|
|
1530
|
-
const mod = matchMOD(request.model, libDb);
|
|
1531
|
-
if (mod) {
|
|
1532
|
-
const modContent = renderMOD(mod, null, request.model || '').join('\n');
|
|
1533
|
-
if (modContent.trim())
|
|
1534
|
-
stableOutputParts.push(modContent);
|
|
1535
|
-
}
|
|
1536
|
-
}
|
|
1537
|
-
}
|
|
1538
|
-
if (stableOutputParts.length > 0) {
|
|
1539
|
-
const stableOutputContent = stableOutputParts.join('\n\n');
|
|
1540
|
-
const stableOutputTokens = estimateTokens(stableOutputContent);
|
|
1541
|
-
if (stableOutputTokens <= remaining) {
|
|
1542
|
-
messages.push({
|
|
1543
|
-
role: 'system',
|
|
1544
|
-
textContent: stableOutputContent,
|
|
1545
|
-
toolCalls: null,
|
|
1546
|
-
toolResults: null,
|
|
1547
|
-
});
|
|
1548
|
-
slots.system += stableOutputTokens;
|
|
1549
|
-
remaining -= stableOutputTokens;
|
|
1550
|
-
}
|
|
1551
|
-
}
|
|
1552
|
-
}
|
|
1553
|
-
// ─── Conversation History ──────────────────────────────────
|
|
1554
|
-
let diagCrossTopicKeystones = 0;
|
|
1555
|
-
// Sprint 4: hoisted so diagnostics block can read it regardless of includeHistory branch.
|
|
1556
|
-
let s4RescueTrimFired = false;
|
|
1557
|
-
// C1: total tool-chain degradation counters across history budget-fit and safety-valve passes.
|
|
1558
|
-
let c1CoEjections = 0;
|
|
1559
|
-
let c1StubReplacements = 0;
|
|
1560
|
-
// Hoisted: activeTopicId/name resolved inside history block, used for window dual-write (VS-1) and wiki page injection
|
|
1561
|
-
let composedActiveTopicId;
|
|
1562
|
-
let composedActiveTopicName;
|
|
1563
|
-
if (request.includeHistory !== false) {
|
|
1564
|
-
// Phase 3 (Turn DAG): resolve active context for DAG-native reads.
|
|
1565
|
-
// This is the primary branch-scoping mechanism; fence remains as transitional safety.
|
|
1566
|
-
let activeContext = null;
|
|
1567
|
-
try {
|
|
1568
|
-
activeContext = getActiveContext(db, request.agentId, request.sessionKey);
|
|
1569
|
-
}
|
|
1570
|
-
catch {
|
|
1571
|
-
// Context resolution is best-effort — fall back to fence-based reads
|
|
1572
|
-
}
|
|
1573
|
-
// P3.4: Look up the active topic for this session (non-fatal)
|
|
1574
|
-
let activeTopicId;
|
|
1575
|
-
let activeTopic;
|
|
1576
|
-
if (!request.topicId) {
|
|
1577
|
-
try {
|
|
1578
|
-
const topicMap = new SessionTopicMap(db);
|
|
1579
|
-
activeTopic = topicMap.getActiveTopic(request.sessionKey) || undefined;
|
|
1580
|
-
if (activeTopic)
|
|
1581
|
-
activeTopicId = activeTopic.id;
|
|
1582
|
-
}
|
|
1583
|
-
catch {
|
|
1584
|
-
// Topic lookup is best-effort — fall back to full history
|
|
1585
|
-
}
|
|
1586
|
-
}
|
|
1587
|
-
else {
|
|
1588
|
-
activeTopicId = request.topicId;
|
|
1589
|
-
try {
|
|
1590
|
-
activeTopic = db.prepare(`
|
|
1591
|
-
SELECT id, name
|
|
1592
|
-
FROM topics
|
|
1593
|
-
WHERE session_key = ? AND id = ?
|
|
1594
|
-
LIMIT 1
|
|
1595
|
-
`).get(request.sessionKey, request.topicId);
|
|
1596
|
-
}
|
|
1597
|
-
catch {
|
|
1598
|
-
// Topic lookup is best-effort — fall back to ID-only history fetch
|
|
1599
|
-
}
|
|
1600
|
-
}
|
|
1601
|
-
// Hoist resolved topic id+name so the window dual-write and wiki injection sections can access them
|
|
1602
|
-
composedActiveTopicId = activeTopicId;
|
|
1603
|
-
composedActiveTopicName = activeTopic?.name;
|
|
1604
|
-
const rawHistoryMessages = await this.getHistory(request.agentId, request.sessionKey, s4EffectiveDepth, // Sprint 4: adaptive depth (replaces fixed maxHistoryMessages)
|
|
1605
|
-
store, activeTopicId, fenceMessageId, activeContext);
|
|
1606
|
-
// Deduplicate history by StoredMessage.id (second line of defense after
|
|
1607
|
-
// pushHistory() tail-check dedup). Guards against any duplicates that
|
|
1608
|
-
// slipped through the warm path — e.g. bootstrap re-runs on existing sessions.
|
|
1609
|
-
const seenIds = new Set();
|
|
1610
|
-
const historyMessages = rawHistoryMessages.filter(m => {
|
|
1611
|
-
const sm = m;
|
|
1612
|
-
if (sm.id != null) {
|
|
1613
|
-
if (seenIds.has(sm.id))
|
|
1614
|
-
return false;
|
|
1615
|
-
seenIds.add(sm.id);
|
|
1616
|
-
}
|
|
1617
|
-
return true;
|
|
1618
|
-
});
|
|
1619
|
-
// ── Transform-first: apply gradient tool treatment BEFORE budget math ──
|
|
1620
|
-
// All tool payloads are in their final form before any token estimation.
|
|
1621
|
-
// This ensures estimateMessageTokens() measures actual submission cost,
|
|
1622
|
-
// not pre-transform cost (which caused overflow: dense tool JSON was
|
|
1623
|
-
// undercounted at length/4 when it should be measured post-stub).
|
|
1624
|
-
const transformedHistory = applyToolGradient(historyMessages, { totalWindowTokens: totalWindow });
|
|
1625
|
-
// ── Evict large tool results (>800 tokens) before window selection ─────
|
|
1626
|
-
// Replace oversized stale results with stubs so they don't burn budget.
|
|
1627
|
-
// Current-turn results (turn age 0) are never evicted.
|
|
1628
|
-
const evictedHistory = evictLargeToolResults(transformedHistory);
|
|
1629
|
-
const c2ResolvedHistory = resolveOversizedArtifacts(evictedHistory, budget);
|
|
1630
|
-
c2ArtifactDegradations += c2ResolvedHistory.refCount;
|
|
1631
|
-
// ── Budget-fit: walk newest→oldest, drop whole clusters ─────────────
|
|
1632
|
-
// Group tool_use + tool_result messages into clusters so they are kept
|
|
1633
|
-
// or dropped as a unit. Breaking mid-cluster creates orphaned tool
|
|
1634
|
-
// pairs that repairToolPairs has to strip downstream — wasting budget
|
|
1635
|
-
// and leaving gaps in conversation continuity.
|
|
1636
|
-
const budgetClusters = clusterNeutralMessages(c2ResolvedHistory.messages);
|
|
1637
|
-
let historyTokens = 0;
|
|
1638
|
-
const includedClusters = [];
|
|
1639
|
-
// Pre-allocate history budget. historyFraction is a fraction of the
|
|
1640
|
-
// effective token budget (post-reserve). Falls back to unbounded fill
|
|
1641
|
-
// (remaining) when historyFraction is not set.
|
|
1642
|
-
// B4: uses b4HistoryFraction (model-aware, blended from MECW catalog) instead
|
|
1643
|
-
// of raw config.historyFraction so history doesn't overflow MECW ceiling.
|
|
1644
|
-
const historyBudget = Math.floor(budget * b4HistoryFraction);
|
|
1645
|
-
const historyFillCap = Math.min(historyBudget, remaining);
|
|
1646
|
-
for (let i = budgetClusters.length - 1; i >= 0; i--) {
|
|
1647
|
-
const cluster = budgetClusters[i];
|
|
1648
|
-
if (historyTokens + cluster.tokenCost > historyFillCap && includedClusters.length > 0) {
|
|
1649
|
-
const droppedClusters = budgetClusters.slice(0, i + 1);
|
|
1650
|
-
const droppedMsgCount = droppedClusters.reduce((s, c) => s + c.messages.length, 0);
|
|
1651
|
-
const droppedToolResultCount = droppedClusters.reduce((sum, c) => sum + c.messages.filter(m => (m.toolResults?.length ?? 0) > 0).length, 0);
|
|
1652
|
-
if (droppedToolResultCount > 0) {
|
|
1653
|
-
c1CoEjections += droppedToolResultCount;
|
|
1654
|
-
console.info(`[hypermem:compositor] tool-chain co-eject reason=budget_cluster_drop count=${droppedToolResultCount} messages dropped`);
|
|
1655
|
-
}
|
|
1656
|
-
const c1Note = droppedToolResultCount > 0
|
|
1657
|
-
? ` [C1: ${droppedToolResultCount} co-ejected reason=budget_cluster_drop]`
|
|
1658
|
-
: '';
|
|
1659
|
-
warnings.push(`History truncated at cluster ${i + 1}/${budgetClusters.length} (${droppedMsgCount} messages dropped)${c1Note}`);
|
|
1660
|
-
s4RescueTrimFired = true;
|
|
1661
|
-
break;
|
|
1662
|
-
}
|
|
1663
|
-
includedClusters.unshift(cluster);
|
|
1664
|
-
historyTokens += cluster.tokenCost;
|
|
1665
|
-
}
|
|
1666
|
-
const includedHistory = includedClusters.flatMap(c => c.messages);
|
|
1667
|
-
// ── Keystone History Slot (P2.1) ──────────────────────────────────
|
|
1668
|
-
// For long conversations (≥30 messages), inject high-signal older messages
|
|
1669
|
-
// from before the recent window as recalled context. This lets the model
|
|
1670
|
-
// see key decisions and specs that happened earlier in the conversation
|
|
1671
|
-
// without them consuming the full recent history budget.
|
|
1672
|
-
const keystoneFraction = this.config.keystoneHistoryFraction ?? 0.2;
|
|
1673
|
-
const keystoneMaxMsgs = this.config.keystoneMaxMessages ?? 15;
|
|
1674
|
-
let keystoneMessages = [];
|
|
1675
|
-
let keystoneTokens = 0;
|
|
1676
|
-
if (request.includeKeystones !== false && includedHistory.length >= 30 && keystoneFraction > 0) {
|
|
1677
|
-
const keystoneResult = await this.buildKeystones(db, request.agentId, includedHistory, historyTokens, keystoneFraction, keystoneMaxMsgs, request.prompt, libDb || undefined, fenceMessageId, activeContext);
|
|
1678
|
-
if (keystoneResult) {
|
|
1679
|
-
keystoneMessages = keystoneResult.keystoneMessages;
|
|
1680
|
-
keystoneTokens = keystoneResult.keystoneTokens;
|
|
1681
|
-
// Replace includedHistory and historyTokens with the trimmed versions
|
|
1682
|
-
// (keystoneResult reflects the trimming done inside buildKeystones)
|
|
1683
|
-
includedHistory.splice(0, includedHistory.length, ...keystoneResult.trimmedHistory);
|
|
1684
|
-
historyTokens = keystoneResult.trimmedHistoryTokens;
|
|
1685
|
-
warnings.push(`Keystone: injected ${keystoneMessages.length} recalled messages`);
|
|
1686
|
-
}
|
|
1687
|
-
}
|
|
1688
|
-
// ── Cross-Topic Keystones (P3.5) ──────────────────────────────────
|
|
1689
|
-
// Pull high-signal messages from OTHER topics in this session when their
|
|
1690
|
-
// content is semantically relevant to the current topic. Non-fatal.
|
|
1691
|
-
let crossTopicMessages = [];
|
|
1692
|
-
let crossTopicTokens = 0;
|
|
1693
|
-
if (request.includeKeystones !== false && activeTopic && this.vectorStore) {
|
|
1694
|
-
try {
|
|
1695
|
-
const rawCrossTopicKeystones = await this.getKeystonesByTopic(request.agentId, request.sessionKey, activeTopic, includedHistory, db, 3, fenceMessageId, activeContext);
|
|
1696
|
-
if (rawCrossTopicKeystones.length > 0) {
|
|
1697
|
-
// Token budget: cap the full cross-topic block at 15% of remaining,
|
|
1698
|
-
// including the header line.
|
|
1699
|
-
const crossTopicHeaderTokens = estimateTokens('## Cross-Topic Context');
|
|
1700
|
-
const crossTopicBudget = Math.max(0, Math.floor(remaining * 0.15) - crossTopicHeaderTokens);
|
|
1701
|
-
let used = 0;
|
|
1702
|
-
for (const candidate of rawCrossTopicKeystones) {
|
|
1703
|
-
const msg = {
|
|
1704
|
-
role: candidate.role,
|
|
1705
|
-
textContent: candidate.content,
|
|
1706
|
-
toolCalls: null,
|
|
1707
|
-
toolResults: null,
|
|
1708
|
-
};
|
|
1709
|
-
const msgTokens = estimateMessageTokens(msg);
|
|
1710
|
-
if (used + msgTokens > crossTopicBudget)
|
|
1711
|
-
continue;
|
|
1712
|
-
crossTopicMessages.push(msg);
|
|
1713
|
-
used += msgTokens;
|
|
1714
|
-
}
|
|
1715
|
-
crossTopicTokens = used;
|
|
1716
|
-
diagCrossTopicKeystones = crossTopicMessages.length;
|
|
1717
|
-
}
|
|
1718
|
-
}
|
|
1719
|
-
catch {
|
|
1720
|
-
// Cross-topic retrieval is non-fatal — never block compose
|
|
1721
|
-
}
|
|
1722
|
-
}
|
|
1723
|
-
// Push history with keystone separators if we have keystones.
|
|
1724
|
-
if (keystoneMessages.length > 0 || crossTopicMessages.length > 0) {
|
|
1725
|
-
// Cross-topic context (from other topics) — prepended before within-session keystones
|
|
1726
|
-
if (crossTopicMessages.length > 0) {
|
|
1727
|
-
messages.push({
|
|
1728
|
-
role: 'system',
|
|
1729
|
-
textContent: '## Cross-Topic Context',
|
|
1730
|
-
toolCalls: null,
|
|
1731
|
-
toolResults: null,
|
|
1732
|
-
});
|
|
1733
|
-
messages.push(...crossTopicMessages);
|
|
1734
|
-
}
|
|
1735
|
-
// Separator before recalled context (within-session keystones)
|
|
1736
|
-
if (keystoneMessages.length > 0) {
|
|
1737
|
-
messages.push({
|
|
1738
|
-
role: 'system',
|
|
1739
|
-
textContent: '## Recalled Context (high-signal older messages)',
|
|
1740
|
-
toolCalls: null,
|
|
1741
|
-
toolResults: null,
|
|
1742
|
-
});
|
|
1743
|
-
messages.push(...keystoneMessages);
|
|
1744
|
-
}
|
|
1745
|
-
// Separator before recent conversation
|
|
1746
|
-
messages.push({
|
|
1747
|
-
role: 'system',
|
|
1748
|
-
textContent: '## Recent Conversation',
|
|
1749
|
-
toolCalls: null,
|
|
1750
|
-
toolResults: null,
|
|
1751
|
-
});
|
|
1752
|
-
messages.push(...includedHistory);
|
|
1753
|
-
// Account for separator tokens in history slot
|
|
1754
|
-
const crossTopicSepTokens = crossTopicMessages.length > 0
|
|
1755
|
-
? estimateTokens('## Cross-Topic Context')
|
|
1756
|
-
: 0;
|
|
1757
|
-
const keystoneSepTokens = keystoneMessages.length > 0
|
|
1758
|
-
? estimateTokens('## Recalled Context (high-signal older messages)')
|
|
1759
|
-
: 0;
|
|
1760
|
-
const recentSepTokens = estimateTokens('## Recent Conversation');
|
|
1761
|
-
const sepTokens = crossTopicSepTokens + keystoneSepTokens + recentSepTokens;
|
|
1762
|
-
slots.history = historyTokens + keystoneTokens + crossTopicTokens + sepTokens;
|
|
1763
|
-
remaining -= (historyTokens + keystoneTokens + crossTopicTokens + sepTokens);
|
|
1764
|
-
}
|
|
1765
|
-
else {
|
|
1766
|
-
messages.push(...includedHistory);
|
|
1767
|
-
slots.history = historyTokens;
|
|
1768
|
-
remaining -= historyTokens;
|
|
1769
|
-
}
|
|
1770
|
-
// Memory budget pool: facts, wiki, semantic recall, cross-session, and
|
|
1771
|
-
// trigger-fired doc chunks all draw from this shared pool via `remaining`.
|
|
1772
|
-
// B4: uses b4MemoryFraction (model-aware, blended from MECW catalog) instead
|
|
1773
|
-
// of raw config.memoryFraction so the memory pool scales with what the model
|
|
1774
|
-
// can effectively attend to within its MECW ceiling.
|
|
1775
|
-
let memoryBudget;
|
|
1776
|
-
{
|
|
1777
|
-
memoryBudget = Math.floor(budget * b4MemoryFraction);
|
|
1778
|
-
if (remaining > memoryBudget) {
|
|
1779
|
-
remaining = memoryBudget;
|
|
1780
|
-
}
|
|
1781
|
-
}
|
|
1782
|
-
// T1.3: Ghost message suppression.
|
|
1783
|
-
// If the last message in the included history is a warm-seeded user message
|
|
1784
|
-
// AND there's a subsequent message in SQLite that wasn't included (meaning
|
|
1785
|
-
// the assistant already responded), drop it. This prevents the model from
|
|
1786
|
-
// re-answering a question that was already handled in a prior session.
|
|
1787
|
-
// Only triggers when: (1) message has _warmed flag, (2) it's role=user,
|
|
1788
|
-
// (3) SQLite has messages after it (the response exists but wasn't included).
|
|
1789
|
-
const lastIncluded = messages[messages.length - 1];
|
|
1790
|
-
if (lastIncluded?.role === 'user') {
|
|
1791
|
-
const sm = lastIncluded;
|
|
1792
|
-
const meta = sm.metadata;
|
|
1793
|
-
if (meta?._warmed && sm.id != null) {
|
|
1794
|
-
// Check if there are any messages after this one in SQLite
|
|
1795
|
-
try {
|
|
1796
|
-
const hasMore = db.prepare('SELECT 1 FROM messages WHERE conversation_id = (SELECT conversation_id FROM messages WHERE id = ?) AND id > ? LIMIT 1').get(sm.id, sm.id);
|
|
1797
|
-
if (hasMore) {
|
|
1798
|
-
messages.pop();
|
|
1799
|
-
warnings.push('Dropped trailing warm-seeded user message with existing response (ghost suppression)');
|
|
1800
|
-
}
|
|
1801
|
-
}
|
|
1802
|
-
catch {
|
|
1803
|
-
// Ghost check is best-effort — don't block compose
|
|
1804
|
-
}
|
|
1805
|
-
}
|
|
1806
|
-
}
|
|
1807
|
-
}
|
|
1808
|
-
// ─── Cache-ordered context assembly ─────────────────────────
|
|
1809
|
-
// Stable, reusable material is lifted above the cache boundary as its
|
|
1810
|
-
// own system messages. Session-volatile material stays in the dynamic
|
|
1811
|
-
// context block below that boundary.
|
|
1812
|
-
const stablePrefixMessages = [];
|
|
1813
|
-
const volatileContextParts = [];
|
|
1814
|
-
let contextTokens = 0;
|
|
1815
|
-
// ── C1: Content fingerprint dedup set ────────────────────
|
|
1816
|
-
// Replaces fragile substring-match dedup across temporal, open-domain,
|
|
1817
|
-
// semantic recall, and cross-session paths. O(1) lookup on a normalized
|
|
1818
|
-
// 120-char prefix catches rephrased duplicates the old 60-char includes()
|
|
1819
|
-
// match missed without needing a hash.
|
|
1820
|
-
const contextFingerprints = new Set();
|
|
1821
|
-
const fingerprintEntries = new Map();
|
|
1822
|
-
// ── Compose-level diagnostics tracking vars ──────────────
|
|
1823
|
-
let diagTriggerHits = 0;
|
|
1824
|
-
let diagTriggerFallbackUsed = false;
|
|
1825
|
-
let diagFactsIncluded = 0;
|
|
1826
|
-
let diagSemanticResults = 0;
|
|
1827
|
-
let diagDocChunkCollections = 0;
|
|
1828
|
-
let diagScopeFiltered = 0;
|
|
1829
|
-
let diagFingerprintDedups = 0;
|
|
1830
|
-
let diagFingerprintCollisions = 0;
|
|
1831
|
-
let diagRetrievalMode = 'none';
|
|
1832
|
-
function normalizeFingerprintText(text) {
|
|
1833
|
-
return text.toLowerCase().replace(/\s+/g, ' ').trim();
|
|
1834
|
-
}
|
|
1835
|
-
function contentFingerprint(text) {
|
|
1836
|
-
return normalizeFingerprintText(text).slice(0, 120);
|
|
1837
|
-
}
|
|
1838
|
-
function addFingerprint(text) {
|
|
1839
|
-
const normalized = normalizeFingerprintText(text);
|
|
1840
|
-
const fingerprint = normalized.slice(0, 120);
|
|
1841
|
-
contextFingerprints.add(fingerprint);
|
|
1842
|
-
const entries = fingerprintEntries.get(fingerprint) ?? new Set();
|
|
1843
|
-
entries.add(normalized);
|
|
1844
|
-
fingerprintEntries.set(fingerprint, entries);
|
|
1845
|
-
}
|
|
1846
|
-
function isDuplicate(text) {
|
|
1847
|
-
const normalized = normalizeFingerprintText(text);
|
|
1848
|
-
const fingerprint = normalized.slice(0, 120);
|
|
1849
|
-
if (!contextFingerprints.has(fingerprint))
|
|
1850
|
-
return false;
|
|
1851
|
-
const entries = fingerprintEntries.get(fingerprint);
|
|
1852
|
-
if (entries && !entries.has(normalized))
|
|
1853
|
-
diagFingerprintCollisions += 1;
|
|
1854
|
-
return true;
|
|
1855
|
-
}
|
|
1856
|
-
// ── Wiki Page (L4: Library — active topic synthesis) ──────
|
|
1857
|
-
// Inject synthesized wiki page for the active topic before general knowledge.
|
|
1858
|
-
// Draws from the shared memory budget pool (remaining is pre-capped by memoryBudget).
|
|
1859
|
-
if (request.includeLibrary !== false && remaining > 300 && libDb && composedActiveTopicName) {
|
|
1860
|
-
const wikiContent = this.buildWikiPageContext(request.agentId, composedActiveTopicName, libDb);
|
|
1861
|
-
if (wikiContent) {
|
|
1862
|
-
const tokens = estimateTokens(wikiContent);
|
|
1863
|
-
if (tokens <= remaining) {
|
|
1864
|
-
volatileContextParts.push(wikiContent);
|
|
1865
|
-
contextTokens += tokens;
|
|
1866
|
-
remaining -= tokens;
|
|
1867
|
-
slots.library += tokens;
|
|
1868
|
-
}
|
|
1869
|
-
else if (remaining > 200) {
|
|
1870
|
-
const truncated = this.truncateToTokens(wikiContent, remaining);
|
|
1871
|
-
const truncTokens = estimateTokens(truncated);
|
|
1872
|
-
volatileContextParts.push(truncated);
|
|
1873
|
-
contextTokens += truncTokens;
|
|
1874
|
-
remaining -= truncTokens;
|
|
1875
|
-
slots.library += truncTokens;
|
|
1876
|
-
}
|
|
1877
|
-
}
|
|
1878
|
-
}
|
|
1879
|
-
// ── Facts (L4: Library) ──────────────────────────────────
|
|
1880
|
-
// scope: agent — filtered by agentId via filterByScope after fetch
|
|
1881
|
-
// Draws from the shared memory budget pool (remaining is pre-capped by memoryBudget).
|
|
1882
|
-
if (request.includeFacts !== false && remaining > 500) {
|
|
1883
|
-
const factSections = this.buildFactSectionsFromDb(request.agentId, request.sessionKey, libDb || db);
|
|
1884
|
-
if (factSections !== null) {
|
|
1885
|
-
const { stableContent, stableCount, volatileContent, volatileCount, filteredCount } = factSections;
|
|
1886
|
-
diagFactsIncluded += stableCount + volatileCount;
|
|
1887
|
-
diagScopeFiltered += filteredCount;
|
|
1888
|
-
if (stableContent) {
|
|
1889
|
-
const stableFactsBlock = `## Stable Facts\n${stableContent}`;
|
|
1890
|
-
const tokens = estimateTokens(stableFactsBlock);
|
|
1891
|
-
if (tokens <= remaining) {
|
|
1892
|
-
stablePrefixMessages.push({
|
|
1893
|
-
role: 'system',
|
|
1894
|
-
textContent: stableFactsBlock,
|
|
1895
|
-
toolCalls: null,
|
|
1896
|
-
toolResults: null,
|
|
1897
|
-
});
|
|
1898
|
-
contextTokens += tokens;
|
|
1899
|
-
remaining -= tokens;
|
|
1900
|
-
slots.facts += tokens;
|
|
1901
|
-
}
|
|
1902
|
-
else if (remaining > 200) {
|
|
1903
|
-
const truncated = this.truncateToTokens(stableFactsBlock, remaining);
|
|
1904
|
-
const truncTokens = estimateTokens(truncated);
|
|
1905
|
-
stablePrefixMessages.push({
|
|
1906
|
-
role: 'system',
|
|
1907
|
-
textContent: truncated,
|
|
1908
|
-
toolCalls: null,
|
|
1909
|
-
toolResults: null,
|
|
1910
|
-
});
|
|
1911
|
-
contextTokens += truncTokens;
|
|
1912
|
-
remaining -= truncTokens;
|
|
1913
|
-
slots.facts += truncTokens;
|
|
1914
|
-
warnings.push('Stable facts truncated to fit memory budget');
|
|
1915
|
-
}
|
|
1916
|
-
for (const line of stableContent.split('\n')) {
|
|
1917
|
-
if (line.startsWith('- ['))
|
|
1918
|
-
addFingerprint(line);
|
|
1919
|
-
}
|
|
1920
|
-
}
|
|
1921
|
-
if (volatileContent) {
|
|
1922
|
-
const volatileFactsBlock = `## Active Facts\n${volatileContent}`;
|
|
1923
|
-
const tokens = estimateTokens(volatileFactsBlock);
|
|
1924
|
-
if (tokens <= remaining) {
|
|
1925
|
-
volatileContextParts.push(volatileFactsBlock);
|
|
1926
|
-
contextTokens += tokens;
|
|
1927
|
-
remaining -= tokens;
|
|
1928
|
-
slots.facts += tokens;
|
|
1929
|
-
}
|
|
1930
|
-
else if (remaining > 200) {
|
|
1931
|
-
const truncated = this.truncateToTokens(volatileFactsBlock, remaining);
|
|
1932
|
-
const truncTokens = estimateTokens(truncated);
|
|
1933
|
-
volatileContextParts.push(truncated);
|
|
1934
|
-
contextTokens += truncTokens;
|
|
1935
|
-
remaining -= truncTokens;
|
|
1936
|
-
slots.facts += truncTokens;
|
|
1937
|
-
warnings.push('Active facts truncated to fit memory budget');
|
|
1938
|
-
}
|
|
1939
|
-
for (const line of volatileContent.split('\n')) {
|
|
1940
|
-
if (line.startsWith('- ['))
|
|
1941
|
-
addFingerprint(line);
|
|
1942
|
-
}
|
|
1943
|
-
}
|
|
1944
|
-
}
|
|
1945
|
-
// ── Temporal retrieval (L4: Library) ─────────────────────
|
|
1946
|
-
// Fires when the query has temporal signals (before/after/when/last etc).
|
|
1947
|
-
// Returns facts in time order from temporal_index. Deduplicates against
|
|
1948
|
-
// facts already included above. Uses ingest_at as occurred_at proxy (v1).
|
|
1949
|
-
const queryText = request.prompt ?? '';
|
|
1950
|
-
if (request.includeSemanticRecall !== false && queryText && hasTemporalSignals(queryText) && libDb && remaining > 300) {
|
|
1951
|
-
try {
|
|
1952
|
-
const temporalStore = new TemporalStore(libDb);
|
|
1953
|
-
const temporalFacts = temporalStore.timeRangeQuery({
|
|
1954
|
-
agentId: request.agentId,
|
|
1955
|
-
limit: 15,
|
|
1956
|
-
order: 'DESC',
|
|
1957
|
-
});
|
|
1958
|
-
if (temporalFacts.length > 0) {
|
|
1959
|
-
const beforeCount = temporalFacts.length;
|
|
1960
|
-
const novel = temporalFacts.filter(f => !isDuplicate(f.content));
|
|
1961
|
-
diagFingerprintDedups += beforeCount - novel.length;
|
|
1962
|
-
if (novel.length > 0) {
|
|
1963
|
-
const temporalBlock = novel
|
|
1964
|
-
.map(f => {
|
|
1965
|
-
const ts = new Date(f.occurredAt).toISOString().slice(0, 10);
|
|
1966
|
-
const line = `[${ts}] ${f.content}`;
|
|
1967
|
-
addFingerprint(f.content);
|
|
1968
|
-
return line;
|
|
1969
|
-
})
|
|
1970
|
-
.join('\n');
|
|
1971
|
-
const temporalSection = `## Temporal Context\n${temporalBlock}`;
|
|
1972
|
-
const tempTokens = estimateTokens(temporalSection);
|
|
1973
|
-
const tempBudget = Math.floor(remaining * 0.20);
|
|
1974
|
-
if (tempTokens <= tempBudget) {
|
|
1975
|
-
volatileContextParts.push(temporalSection);
|
|
1976
|
-
contextTokens += tempTokens;
|
|
1977
|
-
remaining -= tempTokens;
|
|
1978
|
-
slots.facts = (slots.facts ?? 0) + tempTokens;
|
|
1979
|
-
}
|
|
1980
|
-
else {
|
|
1981
|
-
const truncated = this.truncateToTokens(temporalSection, tempBudget);
|
|
1982
|
-
const truncTokens = estimateTokens(truncated);
|
|
1983
|
-
volatileContextParts.push(truncated);
|
|
1984
|
-
contextTokens += truncTokens;
|
|
1985
|
-
remaining -= truncTokens;
|
|
1986
|
-
slots.facts = (slots.facts ?? 0) + truncTokens;
|
|
1987
|
-
}
|
|
1988
|
-
}
|
|
1989
|
-
}
|
|
1990
|
-
}
|
|
1991
|
-
catch {
|
|
1992
|
-
// Temporal index not yet available (migration pending) — skip silently
|
|
1993
|
-
}
|
|
1994
|
-
}
|
|
1995
|
-
// ── Open-domain FTS retrieval (L4: Library) ──────────────────
|
|
1996
|
-
// Fires when the query looks broad/exploratory with no topical anchor.
|
|
1997
|
-
// Searches raw messages_fts — bypasses isQualityFact() quality gate so
|
|
1998
|
-
// content filtered from library.db is still reachable for open-domain
|
|
1999
|
-
// questions. Primary fix for LoCoMo open-domain F1 gap (0.133 baseline).
|
|
2000
|
-
if (request.includeSemanticRecall !== false && queryText && isOpenDomainQuery(queryText) && db && remaining > 300) {
|
|
2001
|
-
try {
|
|
2002
|
-
const rawOdResults = searchOpenDomain(db, queryText, '', 10);
|
|
2003
|
-
const beforeOd = rawOdResults.length;
|
|
2004
|
-
const odResults = rawOdResults.filter(r => !isDuplicate(r.content));
|
|
2005
|
-
diagFingerprintDedups += beforeOd - odResults.length;
|
|
2006
|
-
if (odResults.length > 0) {
|
|
2007
|
-
const odBlock = odResults
|
|
2008
|
-
.map(r => {
|
|
2009
|
-
addFingerprint(r.content);
|
|
2010
|
-
const ts = r.createdAt
|
|
2011
|
-
? new Date(r.createdAt).toISOString().slice(0, 10)
|
|
2012
|
-
: '';
|
|
2013
|
-
const prefix = ts ? `[${ts}] ` : '';
|
|
2014
|
-
const snippet = r.content.length > 300
|
|
2015
|
-
? r.content.slice(0, 300) + '…'
|
|
2016
|
-
: r.content;
|
|
2017
|
-
return `${prefix}${snippet}`;
|
|
2018
|
-
})
|
|
2019
|
-
.join('\n');
|
|
2020
|
-
const odSection = `## Open Domain Context\n${odBlock}`;
|
|
2021
|
-
const odTokens = estimateTokens(odSection);
|
|
2022
|
-
const odBudget = Math.floor(remaining * 0.20);
|
|
2023
|
-
if (odTokens <= odBudget) {
|
|
2024
|
-
volatileContextParts.push(odSection);
|
|
2025
|
-
contextTokens += odTokens;
|
|
2026
|
-
remaining -= odTokens;
|
|
2027
|
-
slots.facts = (slots.facts ?? 0) + odTokens;
|
|
2028
|
-
}
|
|
2029
|
-
else {
|
|
2030
|
-
const truncated = this.truncateToTokens(odSection, odBudget);
|
|
2031
|
-
const truncTokens = estimateTokens(truncated);
|
|
2032
|
-
volatileContextParts.push(truncated);
|
|
2033
|
-
contextTokens += truncTokens;
|
|
2034
|
-
remaining -= truncTokens;
|
|
2035
|
-
slots.facts = (slots.facts ?? 0) + truncTokens;
|
|
2036
|
-
}
|
|
2037
|
-
}
|
|
2038
|
-
}
|
|
2039
|
-
catch {
|
|
2040
|
-
// Open-domain FTS unavailable — skip silently
|
|
2041
|
-
}
|
|
2042
|
-
}
|
|
2043
|
-
}
|
|
2044
|
-
// ── Knowledge (L4: Library) ──────────────────────────────
|
|
2045
|
-
// scope: agent — filtered by agent_id in the SQL query (existing behavior)
|
|
2046
|
-
if (request.includeLibrary !== false && remaining > 500 && libDb) {
|
|
2047
|
-
const knowledgeContent = this.buildKnowledgeFromDb(request.agentId, libDb);
|
|
2048
|
-
if (knowledgeContent) {
|
|
2049
|
-
const stableKnowledgeBlock = `## Knowledge\n${knowledgeContent}`;
|
|
2050
|
-
const tokens = estimateTokens(stableKnowledgeBlock);
|
|
2051
|
-
if (tokens <= remaining * 0.2) {
|
|
2052
|
-
stablePrefixMessages.push({
|
|
2053
|
-
role: 'system',
|
|
2054
|
-
textContent: stableKnowledgeBlock,
|
|
2055
|
-
toolCalls: null,
|
|
2056
|
-
toolResults: null,
|
|
2057
|
-
});
|
|
2058
|
-
contextTokens += tokens;
|
|
2059
|
-
remaining -= tokens;
|
|
2060
|
-
slots.library += tokens;
|
|
2061
|
-
}
|
|
2062
|
-
else {
|
|
2063
|
-
const truncated = this.truncateToTokens(stableKnowledgeBlock, Math.floor(remaining * 0.2));
|
|
2064
|
-
const truncTokens = estimateTokens(truncated);
|
|
2065
|
-
stablePrefixMessages.push({
|
|
2066
|
-
role: 'system',
|
|
2067
|
-
textContent: truncated,
|
|
2068
|
-
toolCalls: null,
|
|
2069
|
-
toolResults: null,
|
|
2070
|
-
});
|
|
2071
|
-
contextTokens += truncTokens;
|
|
2072
|
-
remaining -= truncTokens;
|
|
2073
|
-
slots.library += truncTokens;
|
|
2074
|
-
warnings.push('Knowledge truncated to fit budget');
|
|
2075
|
-
}
|
|
2076
|
-
}
|
|
2077
|
-
}
|
|
2078
|
-
// ── Preferences (L4: Library) ────────────────────────────
|
|
2079
|
-
// scope: agent — filtered by agent_id OR NULL in the SQL query (existing behavior)
|
|
2080
|
-
if (request.includeLibrary !== false && remaining > 300 && libDb) {
|
|
2081
|
-
const prefsContent = this.buildPreferencesFromDb(request.agentId, libDb);
|
|
2082
|
-
if (prefsContent) {
|
|
2083
|
-
const stablePrefsBlock = `## User Preferences\n${prefsContent}`;
|
|
2084
|
-
const tokens = estimateTokens(stablePrefsBlock);
|
|
2085
|
-
if (tokens <= remaining * 0.1) {
|
|
2086
|
-
stablePrefixMessages.push({
|
|
2087
|
-
role: 'system',
|
|
2088
|
-
textContent: stablePrefsBlock,
|
|
2089
|
-
toolCalls: null,
|
|
2090
|
-
toolResults: null,
|
|
2091
|
-
});
|
|
2092
|
-
contextTokens += tokens;
|
|
2093
|
-
remaining -= tokens;
|
|
2094
|
-
slots.library += tokens;
|
|
2095
|
-
}
|
|
2096
|
-
}
|
|
2097
|
-
}
|
|
2098
|
-
// ── Semantic Recall (L3: Hybrid FTS5+KNN) ───────────────
|
|
2099
|
-
// scope: agent — buildSemanticRecall filters by agentId internally
|
|
2100
|
-
// Fires when either vector store or library DB is available.
|
|
2101
|
-
// FTS5-only (no embeddings) still returns keyword matches.
|
|
2102
|
-
// KNN-only (no FTS terms) still returns semantic matches.
|
|
2103
|
-
// Both present → Reciprocal Rank Fusion.
|
|
2104
|
-
// Use request.prompt as the retrieval query when available — it is the
|
|
2105
|
-
// live current-turn text. Falling back to getLastUserMessage(messages)
|
|
2106
|
-
// reads from the already-assembled history, which is one turn stale.
|
|
2107
|
-
if (request.includeSemanticRecall !== false && remaining > 500 && (this.vectorStore || libDb)) {
|
|
2108
|
-
const lastUserMsg = request.prompt?.trim() || this.getLastUserMessage(messages);
|
|
2109
|
-
if (lastUserMsg) {
|
|
2110
|
-
try {
|
|
2111
|
-
// Check Redis for a pre-computed embedding from afterTurn()
|
|
2112
|
-
let precomputedEmbedding;
|
|
2113
|
-
try {
|
|
2114
|
-
const cached = await this.cache.getQueryEmbedding(request.agentId, request.sessionKey);
|
|
2115
|
-
if (cached)
|
|
2116
|
-
precomputedEmbedding = cached;
|
|
2117
|
-
}
|
|
2118
|
-
catch {
|
|
2119
|
-
// Redis lookup is best-effort — fall through to Ollama
|
|
2120
|
-
}
|
|
2121
|
-
const semanticContent = await this.buildSemanticRecall(lastUserMsg, request.agentId, Math.floor(remaining * 0.12), // Cap at 12% of remaining (W4: was 0.15)
|
|
2122
|
-
libDb || undefined, precomputedEmbedding, contextFingerprints // C2: skip results already in Active Facts
|
|
2123
|
-
);
|
|
2124
|
-
if (semanticContent) {
|
|
2125
|
-
const tokens = estimateTokens(semanticContent);
|
|
2126
|
-
volatileContextParts.push(`## Related Memory\n${semanticContent}`);
|
|
2127
|
-
contextTokens += tokens;
|
|
2128
|
-
remaining -= tokens;
|
|
2129
|
-
// Semantic recall draws from multiple sources, attribute to context
|
|
2130
|
-
slots.context += tokens;
|
|
2131
|
-
// W3 diagnostics: count non-empty lines as rough results count
|
|
2132
|
-
diagSemanticResults = semanticContent.split('\n').filter(l => l.trim().length > 0).length;
|
|
2133
|
-
}
|
|
2134
|
-
}
|
|
2135
|
-
catch (err) {
|
|
2136
|
-
// Semantic search is best-effort — don't fail composition
|
|
2137
|
-
warnings.push(`Semantic recall failed: ${err.message}`);
|
|
2138
|
-
}
|
|
2139
|
-
}
|
|
2140
|
-
}
|
|
2141
|
-
// ── Doc Chunks (L4: Trigger-based retrieval) ─────────────
|
|
2142
|
-
// scope: per-tier/per-agent — queryChunks filters by agentId and tier
|
|
2143
|
-
// Demand-load governance, identity, and memory chunks based on
|
|
2144
|
-
// conversation context. Replaces full ACA file injection for
|
|
2145
|
-
// the files that have been seeded into the doc chunk index.
|
|
2146
|
-
let triggerFallbackUsed = false;
|
|
2147
|
-
if (request.includeDocChunks !== false && remaining > 400 && libDb) {
|
|
2148
|
-
// Use request.prompt when available (current-turn text, not stale history)
|
|
2149
|
-
const lastMsg = request.prompt?.trim() || this.getLastUserMessage(messages) || '';
|
|
2150
|
-
const triggered = matchTriggers(lastMsg, this.triggerRegistry);
|
|
2151
|
-
if (triggered.length > 0) {
|
|
2152
|
-
diagTriggerHits = triggered.length;
|
|
2153
|
-
diagRetrievalMode = 'triggered';
|
|
2154
|
-
const docChunkStore = new DocChunkStore(libDb);
|
|
2155
|
-
const docParts = [];
|
|
2156
|
-
const maxTotalTriggerTokens = Math.min(remaining, this.config.maxTotalTriggerTokens && this.config.maxTotalTriggerTokens > 0
|
|
2157
|
-
? this.config.maxTotalTriggerTokens
|
|
2158
|
-
: Math.floor(remaining * 0.40));
|
|
2159
|
-
let totalTriggerTokens = 0;
|
|
2160
|
-
for (const trigger of triggered) {
|
|
2161
|
-
if (remaining < 200)
|
|
2162
|
-
break;
|
|
2163
|
-
const triggerBudgetRemaining = maxTotalTriggerTokens - totalTriggerTokens;
|
|
2164
|
-
if (triggerBudgetRemaining < 200)
|
|
2165
|
-
break;
|
|
2166
|
-
const maxTokens = Math.min(trigger.maxTokens || 1000, Math.floor(remaining * 0.12), // No single collection takes > 12% of remaining (W4: was 0.15)
|
|
2167
|
-
triggerBudgetRemaining);
|
|
2168
|
-
try {
|
|
2169
|
-
// Build a relevance-based FTS5 query from the user message.
|
|
2170
|
-
//
|
|
2171
|
-
// Problem: trigger keywords are stems ('escalat', 'irreversib') for
|
|
2172
|
-
// substring matching against user messages, but FTS5 tokenizes on word
|
|
2173
|
-
// boundaries. 'escalat' does not match 'escalation' in FTS5 without a
|
|
2174
|
-
// prefix operator.
|
|
2175
|
-
//
|
|
2176
|
-
// Solution: extract actual words from the user message that contain a
|
|
2177
|
-
// matched trigger keyword, then use FTS5 prefix queries (word*) for
|
|
2178
|
-
// each extracted word. This bridges stem-matching and FTS5 indexing.
|
|
2179
|
-
const msgLower = lastMsg.toLowerCase();
|
|
2180
|
-
const matchedKeywords = trigger.keywords.filter(kw => msgLower.includes(kw.toLowerCase()));
|
|
2181
|
-
// Extract whole words from the message that overlap with matched keywords
|
|
2182
|
-
const msgWords = lastMsg.match(/\b\w{4,}\b/g) || [];
|
|
2183
|
-
const relevantWords = msgWords.filter(word => matchedKeywords.some(kw => word.toLowerCase().includes(kw.toLowerCase()) ||
|
|
2184
|
-
kw.toLowerCase().includes(word.toLowerCase().slice(0, 5))));
|
|
2185
|
-
// Build FTS5 OR query: "word1* OR word2* OR word3*"
|
|
2186
|
-
// FTS5 treats space-separated terms as AND by default — we want OR so
|
|
2187
|
-
// that any relevant term is sufficient to retrieve a matching chunk.
|
|
2188
|
-
// Prefix operator (*) ensures stems match full words in the index.
|
|
2189
|
-
// Sort by keyword match specificity (longer matched keyword = more specific term),
|
|
2190
|
-
// then cap at 6 terms to keep FTS queries reasonable.
|
|
2191
|
-
// No positional slice — all relevant words participate, not just the first 3.
|
|
2192
|
-
const sortedWords = [...new Set(relevantWords)].sort((a, b) => {
|
|
2193
|
-
const aLen = Math.max(...matchedKeywords.filter(kw => a.toLowerCase().includes(kw.toLowerCase()) || kw.toLowerCase().includes(a.toLowerCase().slice(0, 5))).map(kw => kw.length), 0);
|
|
2194
|
-
const bLen = Math.max(...matchedKeywords.filter(kw => b.toLowerCase().includes(kw.toLowerCase()) || kw.toLowerCase().includes(b.toLowerCase().slice(0, 5))).map(kw => kw.length), 0);
|
|
2195
|
-
return bLen - aLen; // Most specific match first
|
|
2196
|
-
});
|
|
2197
|
-
// Sanitize FTS5 terms: quote each word, strip internal quotes, add prefix wildcard.
|
|
2198
|
-
// Matches the pattern used in the keystone history FTS path.
|
|
2199
|
-
const sanitizeFtsTerm = (w) => `"${w.replace(/"/g, '')}"*`;
|
|
2200
|
-
const ftsTerms = sortedWords.length > 0
|
|
2201
|
-
? sortedWords.slice(0, 6).map(sanitizeFtsTerm).join(' OR ')
|
|
2202
|
-
: matchedKeywords
|
|
2203
|
-
.sort((a, b) => b.length - a.length)
|
|
2204
|
-
.slice(0, 3)
|
|
2205
|
-
.map(sanitizeFtsTerm)
|
|
2206
|
-
.join(' OR ');
|
|
2207
|
-
// Fallback uses raw message words — also sanitize to prevent FTS5 syntax errors.
|
|
2208
|
-
const ftsKeyword = ftsTerms || lastMsg.split(/\s+/).slice(0, 3)
|
|
2209
|
-
.map(sanitizeFtsTerm).join(' OR ');
|
|
2210
|
-
const chunks = docChunkStore.queryChunks({
|
|
2211
|
-
collection: trigger.collection,
|
|
2212
|
-
agentId: request.agentId,
|
|
2213
|
-
tier: request.tier,
|
|
2214
|
-
limit: trigger.maxChunks || 3,
|
|
2215
|
-
keyword: ftsKeyword,
|
|
2216
|
-
});
|
|
2217
|
-
if (chunks.length === 0)
|
|
2218
|
-
continue;
|
|
2219
|
-
const chunkLines = [];
|
|
2220
|
-
let chunkTokens = 0;
|
|
2221
|
-
for (const chunk of chunks) {
|
|
2222
|
-
// Skip chunks from files OpenClaw already injects into the system prompt
|
|
2223
|
-
const chunkBasename = chunk.sourcePath.split('/').pop() || '';
|
|
2224
|
-
if (OPENCLAW_BOOTSTRAP_FILES.has(chunkBasename))
|
|
2225
|
-
continue;
|
|
2226
|
-
// C2: degrade oversized chunks to canonical artifact references before
|
|
2227
|
-
// enforcing the per-collection budget gate. Otherwise an oversized raw
|
|
2228
|
-
// chunk gets dropped before the tiny degraded ref ever has a chance to fit.
|
|
2229
|
-
const c2ChunkRef = degradeOversizedDocChunk(chunk.id, chunk.sourcePath, chunk.content, c2ArtifactThresholdTokens);
|
|
2230
|
-
const renderedChunk = c2ChunkRef !== null
|
|
2231
|
-
? `### ${chunk.sectionPath}\n${c2ChunkRef}`
|
|
2232
|
-
: `### ${chunk.sectionPath}\n${chunk.content}`;
|
|
2233
|
-
const renderedTokens = estimateTokens(renderedChunk);
|
|
2234
|
-
if (chunkTokens + renderedTokens > maxTokens)
|
|
2235
|
-
break;
|
|
2236
|
-
chunkLines.push(renderedChunk);
|
|
2237
|
-
chunkTokens += renderedTokens;
|
|
2238
|
-
if (c2ChunkRef !== null)
|
|
2239
|
-
c2ArtifactDegradations++;
|
|
2240
|
-
}
|
|
2241
|
-
if (chunkLines.length > 0) {
|
|
2242
|
-
const collectionLabel = trigger.collection.split('/').pop() || trigger.collection;
|
|
2243
|
-
docParts.push(`## ${collectionLabel} (retrieved)\n${chunkLines.join('\n\n')}`);
|
|
2244
|
-
totalTriggerTokens += chunkTokens;
|
|
2245
|
-
contextTokens += chunkTokens;
|
|
2246
|
-
remaining -= chunkTokens;
|
|
2247
|
-
slots.library += chunkTokens;
|
|
2248
|
-
diagDocChunkCollections++;
|
|
2249
|
-
}
|
|
2250
|
-
}
|
|
2251
|
-
catch {
|
|
2252
|
-
// Doc chunk retrieval is best-effort — don't fail composition
|
|
2253
|
-
}
|
|
2254
|
-
}
|
|
2255
|
-
if (docParts.length > 0) {
|
|
2256
|
-
volatileContextParts.push(docParts.join('\n\n'));
|
|
2257
|
-
}
|
|
2258
|
-
}
|
|
2259
|
-
else if (remaining > 400 && (this.vectorStore || libDb)) {
|
|
2260
|
-
// Trigger-miss fallback: no trigger fired — attempt bounded semantic retrieval
|
|
2261
|
-
// so there is never a silent zero-memory path on doc chunks.
|
|
2262
|
-
// INVARIANT: this block is mutually exclusive with triggered-retrieval above.
|
|
2263
|
-
// If refactored to run both paths, cap combined semantic budget to avoid double-recall.
|
|
2264
|
-
try {
|
|
2265
|
-
const fallbackContent = await Promise.race([
|
|
2266
|
-
this.buildSemanticRecall(lastMsg, request.agentId, Math.floor(remaining * 0.10), libDb || undefined, undefined, contextFingerprints // C2: skip results already in Active Facts
|
|
2267
|
-
),
|
|
2268
|
-
new Promise((_, reject) => setTimeout(() => reject(new Error('fallback_knn_timeout')), 3000)),
|
|
2269
|
-
]);
|
|
2270
|
-
if (fallbackContent) {
|
|
2271
|
-
volatileContextParts.push(`## Related Memory\n${fallbackContent}`);
|
|
2272
|
-
const fallbackTokens = estimateTokens(fallbackContent);
|
|
2273
|
-
contextTokens += fallbackTokens;
|
|
2274
|
-
remaining -= fallbackTokens;
|
|
2275
|
-
slots.context += fallbackTokens;
|
|
2276
|
-
triggerFallbackUsed = true;
|
|
2277
|
-
diagTriggerFallbackUsed = true;
|
|
2278
|
-
diagRetrievalMode = 'fallback_knn';
|
|
2279
|
-
}
|
|
2280
|
-
}
|
|
2281
|
-
catch {
|
|
2282
|
-
// Fallback is best-effort — never fail composition (includes timeout)
|
|
2283
|
-
}
|
|
2284
|
-
}
|
|
2285
|
-
}
|
|
2286
|
-
// ── Session-Scoped Doc Chunks (spawn context inheritance) ────
|
|
2287
|
-
// When parentSessionKey is set, retrieve ephemeral doc chunks indexed
|
|
2288
|
-
// by buildSpawnContext() for this spawn session.
|
|
2289
|
-
if (request.parentSessionKey && remaining > 200 && libDb) {
|
|
2290
|
-
try {
|
|
2291
|
-
const spawnChunkStore = new DocChunkStore(libDb);
|
|
2292
|
-
const spawnQueryMsg = request.prompt?.trim() || this.getLastUserMessage(messages) || '';
|
|
2293
|
-
const spawnChunks = spawnChunkStore.queryDocChunks(request.agentId, spawnQueryMsg, { sessionKey: request.parentSessionKey, limit: 8 });
|
|
2294
|
-
if (spawnChunks.length > 0) {
|
|
2295
|
-
const spawnLines = [];
|
|
2296
|
-
let spawnTokens = 0;
|
|
2297
|
-
const maxSpawnTokens = Math.floor(remaining * 0.15);
|
|
2298
|
-
for (const chunk of spawnChunks) {
|
|
2299
|
-
// C2: degrade oversized spawn chunks before enforcing the lane budget,
|
|
2300
|
-
// so a bounded reference can fit even when the raw chunk cannot.
|
|
2301
|
-
const c2SpawnRef = degradeOversizedDocChunk(chunk.id, chunk.sourcePath, chunk.content, c2ArtifactThresholdTokens);
|
|
2302
|
-
const renderedChunk = c2SpawnRef ?? chunk.content;
|
|
2303
|
-
const renderedTokens = estimateTokens(renderedChunk);
|
|
2304
|
-
if (spawnTokens + renderedTokens > maxSpawnTokens)
|
|
2305
|
-
break;
|
|
2306
|
-
spawnLines.push(renderedChunk);
|
|
2307
|
-
spawnTokens += renderedTokens;
|
|
2308
|
-
if (c2SpawnRef !== null)
|
|
2309
|
-
c2ArtifactDegradations++;
|
|
2310
|
-
}
|
|
2311
|
-
if (spawnLines.length > 0) {
|
|
2312
|
-
volatileContextParts.push(`## Spawn Context Documents\n${spawnLines.join('\n\n')}`);
|
|
2313
|
-
contextTokens += spawnTokens;
|
|
2314
|
-
remaining -= spawnTokens;
|
|
2315
|
-
slots.library += spawnTokens;
|
|
2316
|
-
}
|
|
2317
|
-
}
|
|
2318
|
-
}
|
|
2319
|
-
catch {
|
|
2320
|
-
// Session-scoped chunk retrieval is best-effort
|
|
2321
|
-
}
|
|
2322
|
-
}
|
|
2323
|
-
// ── Cross-Session Context (L2: Messages) ─────────────────
|
|
2324
|
-
if (request.includeContext !== false && remaining > 500) {
|
|
2325
|
-
const crossSessionContent = this.buildCrossSessionContext(request.agentId, request.sessionKey, db, libDb, contextFingerprints // C3: skip entries already in facts/semantic recall
|
|
2326
|
-
);
|
|
2327
|
-
if (crossSessionContent) {
|
|
2328
|
-
const tokens = estimateTokens(crossSessionContent);
|
|
2329
|
-
const maxContextTokens = Math.min(this.config.maxCrossSessionContext, Math.floor(remaining * 0.2));
|
|
2330
|
-
if (tokens <= maxContextTokens) {
|
|
2331
|
-
volatileContextParts.push(`## Other Active Sessions\n${crossSessionContent}`);
|
|
2332
|
-
contextTokens += tokens;
|
|
2333
|
-
remaining -= tokens;
|
|
2334
|
-
slots.context += tokens;
|
|
2335
|
-
}
|
|
2336
|
-
else {
|
|
2337
|
-
const truncated = this.truncateToTokens(crossSessionContent, maxContextTokens);
|
|
2338
|
-
const truncTokens = estimateTokens(truncated);
|
|
2339
|
-
volatileContextParts.push(`## Other Active Sessions (truncated)\n${truncated}`);
|
|
2340
|
-
contextTokens += truncTokens;
|
|
2341
|
-
remaining -= truncTokens;
|
|
2342
|
-
slots.context += truncTokens;
|
|
2343
|
-
warnings.push('Cross-session context truncated');
|
|
2344
|
-
}
|
|
2345
|
-
}
|
|
2346
|
-
}
|
|
2347
|
-
// ── Action Verification Summary ─────────────────────────
|
|
2348
|
-
// Keep recent action history on the dynamic side of the cache boundary.
|
|
2349
|
-
if (remaining > 50 && request.includeLibrary !== false) {
|
|
2350
|
-
const pressurePct = budget > 0 ? Math.round(((budget - remaining) / budget) * 100) : 0;
|
|
2351
|
-
const actionSummary = buildActionVerificationSummary(messages, pressurePct);
|
|
2352
|
-
if (actionSummary) {
|
|
2353
|
-
const actionTokens = Math.ceil(actionSummary.length / 4);
|
|
2354
|
-
if (actionTokens <= remaining) {
|
|
2355
|
-
volatileContextParts.push(actionSummary);
|
|
2356
|
-
contextTokens += actionTokens;
|
|
2357
|
-
remaining -= actionTokens;
|
|
2358
|
-
slots.context += actionTokens;
|
|
2359
|
-
}
|
|
2360
|
-
}
|
|
2361
|
-
}
|
|
2362
|
-
const firstNonSystem = messages.findIndex(m => m.role !== 'system');
|
|
2363
|
-
const stableInsertIdx = firstNonSystem === -1 ? messages.length : firstNonSystem;
|
|
2364
|
-
if (stablePrefixMessages.length > 0) {
|
|
2365
|
-
messages.splice(stableInsertIdx, 0, ...stablePrefixMessages);
|
|
2366
|
-
}
|
|
2367
|
-
// ── Inject assembled context block ──────────────────────
|
|
2368
|
-
const assembledContextBlock = volatileContextParts.length > 0 ? volatileContextParts.join('\n\n') : undefined;
|
|
2369
|
-
if (assembledContextBlock) {
|
|
2370
|
-
const contextMsg = {
|
|
2371
|
-
role: 'system',
|
|
2372
|
-
textContent: assembledContextBlock,
|
|
2373
|
-
toolCalls: null,
|
|
2374
|
-
toolResults: null,
|
|
2375
|
-
// CACHE_PREFIX_BOUNDARY_SLOT: this message starts the volatile side of the
|
|
2376
|
-
// prompt. Everything above it is stable-prefix material eligible for reuse;
|
|
2377
|
-
// everything at or below it is per-session / per-turn context.
|
|
2378
|
-
metadata: { dynamicBoundary: true, cacheBoundarySlot: CACHE_PREFIX_BOUNDARY_SLOT },
|
|
2379
|
-
};
|
|
2380
|
-
messages.splice(stableInsertIdx + stablePrefixMessages.length, 0, contextMsg);
|
|
2381
|
-
}
|
|
2382
|
-
const stablePrefix = getStablePrefixMessages(messages);
|
|
2383
|
-
const prefixSegmentCount = stablePrefix.length;
|
|
2384
|
-
const prefixTokens = stablePrefix.reduce((sum, msg) => sum + estimateMessageTokens(msg), 0);
|
|
2385
|
-
const volatileHistoryTokens = messages.slice(prefixSegmentCount)
|
|
2386
|
-
.reduce((sum, msg) => sum + estimateMessageTokens(msg), 0);
|
|
2387
|
-
const prefixHash = computeStablePrefixHash(stablePrefix);
|
|
2388
|
-
// ─── Safety Valve: Post-Assembly Budget Check (C1-aware) ──────────────
|
|
2389
|
-
// Re-estimate total tokens after all slots are assembled. If the
|
|
2390
|
-
// composition exceeds tokenBudget * 1.05 (5% tolerance for estimation
|
|
2391
|
-
// drift), trim history messages from the oldest until we're under budget.
|
|
2392
|
-
// History is the most compressible slot — system/identity are never
|
|
2393
|
-
// truncated, and context (facts/recall/episodes) is more valuable per-token.
|
|
2394
|
-
//
|
|
2395
|
-
// C1: When an assistant message with toolCalls is ejected, its dependent
|
|
2396
|
-
// tool-result messages are co-ejected or stubbed via resolveToolChainEjections.
|
|
2397
|
-
// This ensures no orphaned tool-results survive above the stable-prefix
|
|
2398
|
-
// boundary and eliminates the downstream repairToolPairs cleanup cost.
|
|
2399
|
-
const estimatedTotal = messages.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
|
|
2400
|
-
const hardCeiling = Math.floor(budget * 1.05);
|
|
2401
|
-
if (estimatedTotal > hardCeiling) {
|
|
2402
|
-
const overage = estimatedTotal - budget;
|
|
2403
|
-
let trimmed = 0;
|
|
2404
|
-
let trimCount = 0;
|
|
2405
|
-
// Collect indices of messages to eject before mutating the array.
|
|
2406
|
-
// Walk forward from the first non-system message, trimming oldest first.
|
|
2407
|
-
const firstNonSystemIdx = messages.findIndex(m => m.role !== 'system');
|
|
2408
|
-
const ejectIndices = new Set();
|
|
2409
|
-
if (firstNonSystemIdx >= 0) {
|
|
2410
|
-
let i = firstNonSystemIdx;
|
|
2411
|
-
while (i < messages.length && trimmed < overage) {
|
|
2412
|
-
// Don't trim the last user message (current prompt).
|
|
2413
|
-
if (i === messages.length - 1 && messages[i].role === 'user')
|
|
2414
|
-
break;
|
|
2415
|
-
const msgTokens = estimateMessageTokens(messages[i]);
|
|
2416
|
-
ejectIndices.add(i);
|
|
2417
|
-
trimmed += msgTokens;
|
|
2418
|
-
trimCount++;
|
|
2419
|
-
i++;
|
|
2420
|
-
}
|
|
2421
|
-
}
|
|
2422
|
-
if (ejectIndices.size > 0) {
|
|
2423
|
-
// C1: centralized ejection — resolves dependent tool-results atomically.
|
|
2424
|
-
const ejectionResult = resolveToolChainEjections(messages, ejectIndices, 'eviction_oversize');
|
|
2425
|
-
// Replace in-place so the rest of the compose path sees the clean array.
|
|
2426
|
-
messages.length = 0;
|
|
2427
|
-
messages.push(...ejectionResult.messages);
|
|
2428
|
-
c1CoEjections += ejectionResult.coEjections;
|
|
2429
|
-
c1StubReplacements += ejectionResult.stubReplacements;
|
|
2430
|
-
slots.history = Math.max(0, slots.history - trimmed);
|
|
2431
|
-
remaining += trimmed;
|
|
2432
|
-
const c1Note = (ejectionResult.coEjections + ejectionResult.stubReplacements > 0)
|
|
2433
|
-
? ` [C1: ${ejectionResult.coEjections} co-ejected, ${ejectionResult.stubReplacements} stubbed]`
|
|
2434
|
-
: '';
|
|
2435
|
-
warnings.push(`Safety valve: trimmed ${trimCount} oldest history messages (${trimmed} tokens) to fit budget${c1Note}`);
|
|
2436
|
-
}
|
|
2437
|
-
}
|
|
2438
|
-
// ─── Sprint 2.1: Hydrate active-turn artifact stubs ────────────────────
|
|
2439
|
-
// Must run on NeutralMessages[] BEFORE provider translation.
|
|
2440
|
-
const hydrationResult = this.hydrateActiveTurnArtifacts(messages, db);
|
|
2441
|
-
// ─── Translate to provider format (unless caller wants neutral) ───
|
|
2442
|
-
// When skipProviderTranslation is set, return NeutralMessages directly.
|
|
2443
|
-
// The context engine plugin uses this: the OpenClaw runtime handles its
|
|
2444
|
-
// own provider translation, so double-translating corrupts tool calls.
|
|
2445
|
-
const outputMessages = toComposeOutputMessages(messages);
|
|
2446
|
-
// T1.3: Strip warm-replay provenance flags before output.
|
|
2447
|
-
// _warmed is an internal tag added by warmSession() to mark messages
|
|
2448
|
-
// seeded from SQLite into Redis. It must not leak into provider submissions
|
|
2449
|
-
// or be visible to the runtime (which might misinterpret it).
|
|
2450
|
-
for (const msg of outputMessages) {
|
|
2451
|
-
const m = msg;
|
|
2452
|
-
if (m.metadata && m.metadata._warmed) {
|
|
2453
|
-
const { _warmed, ...cleanMeta } = m.metadata;
|
|
2454
|
-
m.metadata = Object.keys(cleanMeta).length > 0 ? cleanMeta : undefined;
|
|
2455
|
-
}
|
|
2456
|
-
}
|
|
2457
|
-
const totalTokens = budget - remaining;
|
|
2458
|
-
// ─── Slot reconciliation ─────────────────────────────────────────────────
|
|
2459
|
-
// totalTokens = budget - remaining is the authoritative spend figure.
|
|
2460
|
-
// The slot accounting can drift from this due to history trim (which
|
|
2461
|
-
// reduces slots.history but adds back to remaining after the budget
|
|
2462
|
-
// was already committed) and FOS/MOD token rounding.
|
|
2463
|
-
// Reconcile: assign any unaccounted tokens to slots.history so that
|
|
2464
|
-
// sum(slots) === totalTokens always holds.
|
|
2465
|
-
{
|
|
2466
|
-
const slotSum = (slots.system ?? 0) + (slots.identity ?? 0) +
|
|
2467
|
-
(slots.history ?? 0) + (slots.facts ?? 0) +
|
|
2468
|
-
(slots.context ?? 0) + (slots.library ?? 0);
|
|
2469
|
-
const delta = totalTokens - slotSum;
|
|
2470
|
-
if (delta !== 0) {
|
|
2471
|
-
slots.history = (slots.history ?? 0) + delta;
|
|
2472
|
-
}
|
|
2473
|
-
}
|
|
2474
|
-
// ─── Compaction Fence Update ──────────────────────────────
|
|
2475
|
-
// Record the oldest message ID that the LLM can see in this compose
|
|
2476
|
-
// cycle. Everything below this ID becomes eligible for compaction.
|
|
2477
|
-
// If history was included, query the DB for the oldest included message.
|
|
2478
|
-
if (request.includeHistory !== false && slots.history > 0) {
|
|
2479
|
-
try {
|
|
2480
|
-
const conversation = store.getConversation(request.sessionKey);
|
|
2481
|
-
if (conversation) {
|
|
2482
|
-
// The compositor included N history messages (after truncation).
|
|
2483
|
-
// Count how many non-system messages are in the output to determine
|
|
2484
|
-
// how far back we reached.
|
|
2485
|
-
const historyMsgCount = messages.filter(m => m.role !== 'system').length;
|
|
2486
|
-
if (historyMsgCount > 0) {
|
|
2487
|
-
// Get the oldest message we would have included.
|
|
2488
|
-
// getRecentMessages returns the last N in chronological order,
|
|
2489
|
-
// so the first element is the oldest included.
|
|
2490
|
-
const oldestIncluded = db.prepare(`
|
|
2491
|
-
SELECT id FROM messages
|
|
2492
|
-
WHERE conversation_id = ?
|
|
2493
|
-
ORDER BY message_index DESC
|
|
2494
|
-
LIMIT 1 OFFSET ?
|
|
2495
|
-
`).get(conversation.id, historyMsgCount - 1);
|
|
2496
|
-
if (oldestIncluded) {
|
|
2497
|
-
ensureCompactionFenceSchema(db);
|
|
2498
|
-
updateCompactionFence(db, conversation.id, oldestIncluded.id, { minTailMessages: 8 });
|
|
2499
|
-
}
|
|
2500
|
-
}
|
|
2501
|
-
}
|
|
2502
|
-
}
|
|
2503
|
-
catch {
|
|
2504
|
-
// Fence update is best-effort — never fail composition
|
|
2505
|
-
warnings.push('Compaction fence update failed (non-fatal)');
|
|
2506
|
-
}
|
|
2507
|
-
}
|
|
2508
|
-
// W3: Build compose diagnostics
|
|
2509
|
-
let zeroResultReason;
|
|
2510
|
-
if (volatileContextParts.length === 0 && stablePrefixMessages.length === 0) {
|
|
2511
|
-
if (diagScopeFiltered > 0 && diagFactsIncluded === 0 && diagSemanticResults === 0) {
|
|
2512
|
-
zeroResultReason = 'scope_filtered_all';
|
|
2513
|
-
}
|
|
2514
|
-
else if (remaining <= 0) {
|
|
2515
|
-
zeroResultReason = 'budget_exhausted';
|
|
2516
|
-
}
|
|
2517
|
-
else if (diagTriggerHits === 0 && !diagTriggerFallbackUsed) {
|
|
2518
|
-
zeroResultReason = 'no_trigger_no_fallback';
|
|
2519
|
-
}
|
|
2520
|
-
else if ((diagTriggerHits > 0 || diagTriggerFallbackUsed) && diagFactsIncluded === 0 && diagSemanticResults === 0 && diagDocChunkCollections === 0) {
|
|
2521
|
-
// Retrieval was attempted (trigger fired or fallback ran) but returned nothing — likely a retrieval bug
|
|
2522
|
-
// rather than a genuinely empty corpus. Distinguish from 'empty_corpus' for observability.
|
|
2523
|
-
zeroResultReason = 'unknown';
|
|
2524
|
-
}
|
|
2525
|
-
else {
|
|
2526
|
-
zeroResultReason = 'empty_corpus';
|
|
2527
|
-
}
|
|
2528
|
-
}
|
|
2529
|
-
const diagnostics = {
|
|
2530
|
-
triggerHits: diagTriggerHits,
|
|
2531
|
-
triggerFallbackUsed: diagTriggerFallbackUsed,
|
|
2532
|
-
factsIncluded: diagFactsIncluded,
|
|
2533
|
-
semanticResultsIncluded: diagSemanticResults,
|
|
2534
|
-
docChunksCollections: diagDocChunkCollections,
|
|
2535
|
-
scopeFiltered: diagScopeFiltered,
|
|
2536
|
-
zeroResultReason,
|
|
2537
|
-
retrievalMode: diagRetrievalMode,
|
|
2538
|
-
crossTopicKeystones: diagCrossTopicKeystones,
|
|
2539
|
-
reserveFraction: dynamicReserve,
|
|
2540
|
-
avgTurnCostTokens: avgTurnCost,
|
|
2541
|
-
dynamicReserveActive: isDynamic,
|
|
2542
|
-
sessionPressureHigh: pressureHigh,
|
|
2543
|
-
fingerprintDedups: diagFingerprintDedups,
|
|
2544
|
-
fingerprintCollisions: diagFingerprintCollisions,
|
|
2545
|
-
windowCacheHit: false,
|
|
2546
|
-
prefixSegmentCount,
|
|
2547
|
-
prefixTokens,
|
|
2548
|
-
prefixHash,
|
|
2549
|
-
// B2: Surface the previous cached prefixHash when this full compose was
|
|
2550
|
-
// triggered by a cache bypass (stable-prefix mutation detected).
|
|
2551
|
-
prevPrefixHash: _prevPrefixHashFromBypass,
|
|
2552
|
-
volatileHistoryTokens,
|
|
2553
|
-
// Sprint 4 fields
|
|
2554
|
-
sessionType: s4SessionType,
|
|
2555
|
-
historyDepthChosen: s4EffectiveDepth,
|
|
2556
|
-
estimatedMsgDensityTokens: s4ObservedDensity,
|
|
2557
|
-
rescueTrimFired: s4RescueTrimFired,
|
|
2558
|
-
// B4: model-aware lane budget diagnostics
|
|
2559
|
-
mecwProfile: b4MecwProfile,
|
|
2560
|
-
mecwApplied: b4MecwApplied,
|
|
2561
|
-
mecwBlend: b4MecwBlend,
|
|
2562
|
-
effectiveHistoryFraction: b4HistoryFraction,
|
|
2563
|
-
effectiveMemoryFraction: b4MemoryFraction,
|
|
2564
|
-
trimSoftTarget: TRIM_BUDGET_POLICY.trimSoftTarget,
|
|
2565
|
-
trimGrowthThreshold: TRIM_BUDGET_POLICY.trimGrowthThreshold,
|
|
2566
|
-
trimHeadroomFraction: TRIM_BUDGET_POLICY.trimHeadroomFraction,
|
|
2567
|
-
// C1: tool-chain ejection telemetry
|
|
2568
|
-
toolChainCoEjections: c1CoEjections > 0 ? c1CoEjections : undefined,
|
|
2569
|
-
toolChainStubReplacements: c1StubReplacements > 0 ? c1StubReplacements : undefined,
|
|
2570
|
-
// C2: artifact oversize degradation telemetry
|
|
2571
|
-
artifactDegradations: c2ArtifactDegradations > 0 ? c2ArtifactDegradations : undefined,
|
|
2572
|
-
artifactOversizeThresholdTokens: c2ArtifactThresholdTokens,
|
|
2573
|
-
// Sprint 2.1: tool artifact hydration telemetry
|
|
2574
|
-
artifactsHydrated: hydrationResult.artifactsHydrated > 0 ? hydrationResult.artifactsHydrated : undefined,
|
|
2575
|
-
hydrationBytes: hydrationResult.hydrationBytes > 0 ? hydrationResult.hydrationBytes : undefined,
|
|
2576
|
-
hydrationMisses: hydrationResult.hydrationMisses > 0 ? hydrationResult.hydrationMisses : undefined,
|
|
2577
|
-
};
|
|
2578
|
-
if (pressureHigh) {
|
|
2579
|
-
warnings.push(`SESSION_PRESSURE_HIGH: avg_turn_cost=${avgTurnCost} tokens, dynamic reserve capped at ${Math.round(dynamicReserve * 100)}%`);
|
|
2580
|
-
}
|
|
2581
|
-
else if (dynamicReserve > 0.40) {
|
|
2582
|
-
console.info(`[hypermem:compositor] dynamic_reserve=${Math.round(dynamicReserve * 100)}% avg_turn_cost=${Math.round(avgTurnCost / 1000)}k horizon=${this.config.dynamicReserveTurnHorizon ?? 5}`);
|
|
2583
|
-
}
|
|
2584
|
-
const composedAt = new Date().toISOString();
|
|
2585
|
-
// ─── Write Window Cache ─────────────────────────────
|
|
2586
|
-
// Cache the composed message array so the plugin can serve it directly
|
|
2587
|
-
// on the next assemble() call without re-running the full compose pipeline.
|
|
2588
|
-
// Short TTL (120s). External L4 mutations should set skipWindowCache=true.
|
|
2589
|
-
//
|
|
2590
|
-
// VS-1: Dual-write, session-scoped key for backwards compat;
|
|
2591
|
-
// topic-scoped key for per-topic window retrieval when activeTopicId is set.
|
|
2592
|
-
try {
|
|
2593
|
-
// B2: Compute a cheap prefix input hash from the system + identity slot
|
|
2594
|
-
// contents that fed the stable prefix. Stored in WindowCacheMeta so the
|
|
2595
|
-
// C4 fast-exit can detect prefix mutations without re-running full compose.
|
|
2596
|
-
const _prefixInputHash = createHash('sha256')
|
|
2597
|
-
.update(systemContent ?? '')
|
|
2598
|
-
.update('\n␞\n')
|
|
2599
|
-
.update(identityContent ?? '')
|
|
2600
|
-
.digest('hex');
|
|
2601
|
-
await this.cache.setWindow(request.agentId, request.sessionKey, messages, 120);
|
|
2602
|
-
await this.cache.setWindowMeta(request.agentId, request.sessionKey, {
|
|
2603
|
-
slots: slots,
|
|
2604
|
-
totalTokens,
|
|
2605
|
-
warnings,
|
|
2606
|
-
diagnostics,
|
|
2607
|
-
composedAt,
|
|
2608
|
-
prefixHash,
|
|
2609
|
-
prefixInputHash: _prefixInputHash,
|
|
2610
|
-
}, 120);
|
|
2611
|
-
}
|
|
2612
|
-
catch {
|
|
2613
|
-
// Window cache write is best-effort
|
|
2614
|
-
}
|
|
2615
|
-
if (composedActiveTopicId) {
|
|
2616
|
-
try {
|
|
2617
|
-
await this.cache.setTopicWindow(request.agentId, request.sessionKey, composedActiveTopicId, messages, 120);
|
|
2618
|
-
}
|
|
2619
|
-
catch {
|
|
2620
|
-
// Topic window write is best-effort
|
|
2621
|
-
}
|
|
2622
|
-
}
|
|
2623
|
-
// ─── Write Session Cursor ─────────────────────────────────
|
|
2624
|
-
// Record the newest message included in the submission window.
|
|
2625
|
-
// Background indexer uses this to find unprocessed high-signal content.
|
|
2626
|
-
if (request.includeHistory !== false && slots.history > 0) {
|
|
2627
|
-
try {
|
|
2628
|
-
const historyMsgs = messages.filter(m => m.role !== 'system');
|
|
2629
|
-
const lastHistoryMsg = historyMsgs.length > 0 ? historyMsgs[historyMsgs.length - 1] : null;
|
|
2630
|
-
if (lastHistoryMsg) {
|
|
2631
|
-
const sm = lastHistoryMsg;
|
|
2632
|
-
if (sm.id != null && sm.messageIndex != null) {
|
|
2633
|
-
const cursor = {
|
|
2634
|
-
lastSentId: sm.id,
|
|
2635
|
-
lastSentIndex: sm.messageIndex,
|
|
2636
|
-
lastSentAt: composedAt,
|
|
2637
|
-
windowSize: historyMsgs.length,
|
|
2638
|
-
tokenCount: totalTokens,
|
|
2639
|
-
};
|
|
2640
|
-
await this.cache.setCursor(request.agentId, request.sessionKey, cursor);
|
|
2641
|
-
try {
|
|
2642
|
-
db.prepare(`
|
|
2643
|
-
UPDATE conversations
|
|
2644
|
-
SET cursor_last_sent_id = ?,
|
|
2645
|
-
cursor_last_sent_index = ?,
|
|
2646
|
-
cursor_last_sent_at = ?,
|
|
2647
|
-
cursor_window_size = ?,
|
|
2648
|
-
cursor_token_count = ?
|
|
2649
|
-
WHERE session_key = ?
|
|
2650
|
-
`).run(cursor.lastSentId, cursor.lastSentIndex, cursor.lastSentAt, cursor.windowSize, cursor.tokenCount, request.sessionKey);
|
|
2651
|
-
}
|
|
2652
|
-
catch {
|
|
2653
|
-
// SQLite cursor write is best-effort, don't block compose
|
|
2654
|
-
}
|
|
2655
|
-
}
|
|
2656
|
-
}
|
|
2657
|
-
}
|
|
2658
|
-
catch {
|
|
2659
|
-
// Cursor write is best-effort
|
|
2660
|
-
}
|
|
2661
|
-
}
|
|
2662
|
-
console.log(`[hypermem:compose] agent=${request.agentId} triggers=${diagTriggerHits} fallback=${diagTriggerFallbackUsed} facts=${diagFactsIncluded} semantic=${diagSemanticResults} chunks=${diagDocChunkCollections} scopeFiltered=${diagScopeFiltered} mode=${diagRetrievalMode} crossTopicKeystones=${diagCrossTopicKeystones} c2_degradations=${c2ArtifactDegradations} c2_threshold=${c2ArtifactThresholdTokens}`);
|
|
2663
|
-
return {
|
|
2664
|
-
messages: outputMessages,
|
|
2665
|
-
tokenCount: totalTokens,
|
|
2666
|
-
slots,
|
|
2667
|
-
truncated: remaining < 0 || estimatedTotal > hardCeiling,
|
|
2668
|
-
hasWarnings: warnings.length > 0,
|
|
2669
|
-
warnings,
|
|
2670
|
-
contextBlock: assembledContextBlock,
|
|
2671
|
-
diagnostics,
|
|
2672
|
-
};
|
|
2673
|
-
}
|
|
2674
|
-
/**
|
|
2675
|
-
* Warm a session from SQLite into Redis.
|
|
2676
|
-
* Called on session start or Redis cache miss.
|
|
2677
|
-
*/
|
|
2678
|
-
async warmSession(agentId, sessionKey, db, opts) {
|
|
2679
|
-
const store = new MessageStore(db);
|
|
2680
|
-
const conversation = store.getConversation(sessionKey);
|
|
2681
|
-
if (!conversation)
|
|
2682
|
-
return;
|
|
2683
|
-
// Phase 3 (Turn DAG): resolve active context for DAG-native warm preload.
|
|
2684
|
-
// Uses context.head_message_id to walk only the active branch.
|
|
2685
|
-
let activeContext = null;
|
|
2686
|
-
try {
|
|
2687
|
-
activeContext = getActiveContext(db, agentId, sessionKey);
|
|
2688
|
-
}
|
|
2689
|
-
catch {
|
|
2690
|
-
// Context resolution is best-effort
|
|
2691
|
-
}
|
|
2692
|
-
// Phase 0 fence enforcement: resolve compaction fence for warm bootstrap.
|
|
2693
|
-
// Fence remains as transitional safety — primary scoping is via DAG walk.
|
|
2694
|
-
let warmFenceMessageId;
|
|
2695
|
-
try {
|
|
2696
|
-
ensureCompactionFenceSchema(db);
|
|
2697
|
-
const fence = getCompactionFence(db, conversation.id);
|
|
2698
|
-
if (fence)
|
|
2699
|
-
warmFenceMessageId = fence.fenceMessageId;
|
|
2700
|
-
}
|
|
2701
|
-
catch {
|
|
2702
|
-
// Fence lookup is best-effort
|
|
2703
|
-
}
|
|
2704
|
-
// Fetch a generous pool from SQLite, apply gradient transform, then
|
|
2705
|
-
// token-budget-cap the warm set. This replaces the old WARM_BOOTSTRAP_CAP
|
|
2706
|
-
// message-count constant which was a blunt instrument — 100 messages of
|
|
2707
|
-
// large tool results can massively exceed the history budget allocation.
|
|
2708
|
-
// Warm budget uses the same reserve fraction as compose() so warm history
|
|
2709
|
-
// never pre-fills more than compose() would actually allow.
|
|
2710
|
-
const reserve = this.config.contextWindowReserve ?? 0.15;
|
|
2711
|
-
const effectiveBudget = resolveModelBudget(opts?.model, this.config.defaultTokenBudget, reserve, this.config.budgetFraction);
|
|
2712
|
-
const warmBudget = Math.floor(effectiveBudget * (this.config.warmHistoryBudgetFraction ?? 0.4));
|
|
2713
|
-
// Phase 3 (Turn DAG): prefer DAG walk from context head for warm preload.
|
|
2714
|
-
// This ensures only active-branch messages enter the warm cache.
|
|
2715
|
-
let rawHistory;
|
|
2716
|
-
if (activeContext?.headMessageId) {
|
|
2717
|
-
rawHistory = store.getHistoryByDAGWalk(activeContext.headMessageId, this.config.maxHistoryMessages);
|
|
2718
|
-
// DAG walk may return empty for legacy data — fall back to fence-scoped query
|
|
2719
|
-
if (rawHistory.length === 0) {
|
|
2720
|
-
rawHistory = store.getRecentMessages(conversation.id, this.config.maxHistoryMessages, warmFenceMessageId);
|
|
2721
|
-
}
|
|
2722
|
-
}
|
|
2723
|
-
else {
|
|
2724
|
-
rawHistory = store.getRecentMessages(conversation.id, this.config.maxHistoryMessages, warmFenceMessageId);
|
|
2725
|
-
}
|
|
2726
|
-
const transformedForWarm = applyToolGradient(rawHistory, {
|
|
2727
|
-
totalWindowTokens: resolveModelWindow(opts?.model, this.config.defaultTokenBudget),
|
|
2728
|
-
});
|
|
2729
|
-
// Walk newest→oldest, accumulate transformed token cost, stop when budget exhausted
|
|
2730
|
-
let warmTokens = 0;
|
|
2731
|
-
const history = [];
|
|
2732
|
-
for (let i = transformedForWarm.length - 1; i >= 0; i--) {
|
|
2733
|
-
const cost = estimateMessageTokens(transformedForWarm[i]);
|
|
2734
|
-
if (warmTokens + cost > warmBudget)
|
|
2735
|
-
break;
|
|
2736
|
-
// T1.3 Provenance flag: tag warm-seeded messages so they can be identified
|
|
2737
|
-
// downstream. The flag is stripped before provider submission in compose().
|
|
2738
|
-
// This prevents the runtime from treating warm-replayed user messages as
|
|
2739
|
-
// new inbound queries (ghost message bug).
|
|
2740
|
-
const tagged = { ...transformedForWarm[i] };
|
|
2741
|
-
tagged.metadata = { ...(tagged.metadata || {}), _warmed: true };
|
|
2742
|
-
history.unshift(tagged);
|
|
2743
|
-
warmTokens += cost;
|
|
2744
|
-
}
|
|
2745
|
-
const libDb = opts?.libraryDb || this.libraryDb;
|
|
2746
|
-
// Note: facts and context are intentionally NOT cached here.
|
|
2747
|
-
// compose() calls buildFactsFromDb() and buildCrossSessionContext() directly
|
|
2748
|
-
// from SQLite on every turn (~0.3ms each) — faster than a Redis GET round-trip.
|
|
2749
|
-
// Caching them here would create stale entries that compose() ignores anyway.
|
|
2750
|
-
// Invalidate the window cache so the next compose rebuilds with the fresh
|
|
2751
|
-
// system/identity slots. Without this, the fast-exit returns a stale bundle
|
|
2752
|
-
// that predates the warm and reports identity=0.
|
|
2753
|
-
await this.cache.invalidateWindow(agentId, sessionKey);
|
|
2754
|
-
await this.cache.warmSession(agentId, sessionKey, {
|
|
2755
|
-
system: opts?.systemPrompt,
|
|
2756
|
-
identity: opts?.identity,
|
|
2757
|
-
history,
|
|
2758
|
-
meta: {
|
|
2759
|
-
agentId,
|
|
2760
|
-
sessionKey,
|
|
2761
|
-
provider: conversation.provider,
|
|
2762
|
-
model: conversation.model,
|
|
2763
|
-
channelType: conversation.channelType,
|
|
2764
|
-
tokenCount: conversation.tokenCountIn + conversation.tokenCountOut,
|
|
2765
|
-
lastActive: conversation.updatedAt,
|
|
2766
|
-
status: conversation.status,
|
|
2767
|
-
},
|
|
2768
|
-
});
|
|
2769
|
-
}
|
|
2770
|
-
async refreshRedisGradient(agentId, sessionKey, db, tokenBudget, historyDepth) {
|
|
2771
|
-
const store = new MessageStore(db);
|
|
2772
|
-
const conversation = store.getConversation(sessionKey);
|
|
2773
|
-
if (!conversation)
|
|
2774
|
-
return;
|
|
2775
|
-
// Phase 3 (Turn DAG): resolve active context for DAG-native gradient refresh
|
|
2776
|
-
let activeContext = null;
|
|
2777
|
-
try {
|
|
2778
|
-
activeContext = getActiveContext(db, agentId, sessionKey);
|
|
2779
|
-
}
|
|
2780
|
-
catch {
|
|
2781
|
-
// Context resolution is best-effort
|
|
2782
|
-
}
|
|
2783
|
-
// Phase 0 fence enforcement for gradient refresh (transitional safety)
|
|
2784
|
-
let gradientFenceMessageId;
|
|
2785
|
-
try {
|
|
2786
|
-
ensureCompactionFenceSchema(db);
|
|
2787
|
-
const fence = getCompactionFence(db, conversation.id);
|
|
2788
|
-
if (fence)
|
|
2789
|
-
gradientFenceMessageId = fence.fenceMessageId;
|
|
2790
|
-
}
|
|
2791
|
-
catch {
|
|
2792
|
-
// Fence lookup is best-effort
|
|
2793
|
-
}
|
|
2794
|
-
// Phase 3: prefer DAG walk from context head
|
|
2795
|
-
const refreshHistoryLimit = Math.min(this.config.maxHistoryMessages, Math.max(1, historyDepth ?? this.config.maxHistoryMessages));
|
|
2796
|
-
let rawHistory;
|
|
2797
|
-
if (activeContext?.headMessageId) {
|
|
2798
|
-
rawHistory = store.getHistoryByDAGWalk(activeContext.headMessageId, refreshHistoryLimit);
|
|
2799
|
-
if (rawHistory.length === 0) {
|
|
2800
|
-
rawHistory = store.getRecentMessages(conversation.id, refreshHistoryLimit, gradientFenceMessageId);
|
|
2801
|
-
}
|
|
2802
|
-
}
|
|
2803
|
-
else {
|
|
2804
|
-
rawHistory = store.getRecentMessages(conversation.id, refreshHistoryLimit, gradientFenceMessageId);
|
|
2805
|
-
}
|
|
2806
|
-
// Sprint 3 (AfterTurn Rebuild/Trim Loop Fix): cap gradient total-window tokens
|
|
2807
|
-
// at the same 65% target that assemble.normal trims to. Previously this was
|
|
2808
|
-
// tokenBudget/0.80 (≈1.25×budget), which made applyToolGradient preserve more
|
|
2809
|
-
// content than the trim target allowed — causing assemble.normal to always trim
|
|
2810
|
-
// on the next turn even in the steady-state path. Aligning the gradient cap to
|
|
2811
|
-
// the trim target means the rebuilt window already fits within the assemble
|
|
2812
|
-
// envelope by construction.
|
|
2813
|
-
const { softBudget: gradientAssembleBudget } = resolveTrimBudgets(tokenBudget ?? 0);
|
|
2814
|
-
const transformedHistory = applyToolGradient(rawHistory, {
|
|
2815
|
-
totalWindowTokens: tokenBudget && tokenBudget > 0
|
|
2816
|
-
? gradientAssembleBudget
|
|
2817
|
-
: TOOL_PLANNING_BASELINE_WINDOW,
|
|
2818
|
-
});
|
|
2819
|
-
// If a token budget is provided, trim the gradient-compressed window to fit
|
|
2820
|
-
// before writing to Redis. The cap uses the same GRADIENT_ASSEMBLE_TARGET
|
|
2821
|
-
// (0.65) so the window written to Redis sits inside the assemble.normal trim
|
|
2822
|
-
// envelope. The next assemble() will find the window already within budget
|
|
2823
|
-
// and skip the trim entirely in the steady-state path.
|
|
2824
|
-
let historyToWrite = transformedHistory;
|
|
2825
|
-
if (tokenBudget && tokenBudget > 0) {
|
|
2826
|
-
const budgetCap = gradientAssembleBudget;
|
|
2827
|
-
let runningTokens = 0;
|
|
2828
|
-
const clusters = clusterNeutralMessages(transformedHistory);
|
|
2829
|
-
const cappedClusters = [];
|
|
2830
|
-
// Walk newest-first, keep whole clusters so tool-call/result pairs survive together.
|
|
2831
|
-
for (let i = clusters.length - 1; i >= 0; i--) {
|
|
2832
|
-
const cluster = clusters[i];
|
|
2833
|
-
if (runningTokens + cluster.tokenCost > budgetCap && cappedClusters.length > 0)
|
|
2834
|
-
break;
|
|
2835
|
-
cappedClusters.unshift(cluster);
|
|
2836
|
-
runningTokens += cluster.tokenCost;
|
|
2837
|
-
if (runningTokens >= budgetCap)
|
|
2838
|
-
break;
|
|
2839
|
-
}
|
|
2840
|
-
historyToWrite = cappedClusters.flatMap(cluster => cluster.messages);
|
|
2841
|
-
if (historyToWrite.length < transformedHistory.length) {
|
|
2842
|
-
console.log(`[hypermem] refreshRedisGradient: cluster-capped ${transformedHistory.length}→${historyToWrite.length} messages ` +
|
|
2843
|
-
`for ${agentId}/${sessionKey} (budgetCap=${budgetCap}, tokenCost=${runningTokens})`);
|
|
2844
|
-
}
|
|
2845
|
-
}
|
|
2846
|
-
await this.cache.replaceHistory(agentId, sessionKey, historyToWrite, refreshHistoryLimit);
|
|
2847
|
-
}
|
|
2848
|
-
// ─── Slot Content Resolution ─────────────────────────────────
|
|
2849
|
-
/**
|
|
2850
|
-
* Get slot content: try Redis first, fall back to SQLite.
|
|
2851
|
-
*/
|
|
2852
|
-
async getSlotContent(agentId, sessionKey, slot, db, libraryDb) {
|
|
2853
|
-
const cached = await this.cache.getSlot(agentId, sessionKey, slot);
|
|
2854
|
-
if (cached)
|
|
2855
|
-
return cached;
|
|
2856
|
-
switch (slot) {
|
|
2857
|
-
case 'facts': {
|
|
2858
|
-
const result = this.buildFactsFromDb(agentId, sessionKey, libraryDb || this.libraryDb || db);
|
|
2859
|
-
return result ? result[0] : null;
|
|
2860
|
-
}
|
|
2861
|
-
case 'context':
|
|
2862
|
-
return this.buildCrossSessionContext(agentId, sessionKey, db, libraryDb || this.libraryDb);
|
|
2863
|
-
default:
|
|
2864
|
-
return null;
|
|
2865
|
-
}
|
|
2866
|
-
}
|
|
2867
|
-
/**
|
|
2868
|
-
* Get conversation history: try Redis first, fall back to SQLite.
|
|
2869
|
-
*
|
|
2870
|
-
* When topicId is provided (P3.4), the SQLite path filters to messages
|
|
2871
|
-
* matching that topic OR with topic_id IS NULL (Option B transition safety).
|
|
2872
|
-
* The Redis path is unaffected — Redis doesn't index by topic, so topic
|
|
2873
|
-
* filtering only applies to the SQLite fallback.
|
|
2874
|
-
*/
|
|
2875
|
-
async getHistory(agentId, sessionKey, limit, store, topicId, fenceMessageId, activeContext) {
|
|
2876
|
-
// Pass limit through to Redis — this is the correct enforcement point.
|
|
2877
|
-
// Previously getHistory() ignored the limit on the Redis path (LRANGE 0 -1),
|
|
2878
|
-
// meaning historyDepth in the compose request had no effect on hot sessions.
|
|
2879
|
-
const cached = await this.cache.getHistory(agentId, sessionKey, limit);
|
|
2880
|
-
if (cached.length > 0)
|
|
2881
|
-
return cached;
|
|
2882
|
-
// Phase 3 (Turn DAG): walk from context.head_message_id backward through
|
|
2883
|
-
// parent_id links. This is the primary correctness mechanism — the fence
|
|
2884
|
-
// remains as transitional safety only.
|
|
2885
|
-
if (activeContext?.headMessageId) {
|
|
2886
|
-
const dagMessages = store.getHistoryByDAGWalk(activeContext.headMessageId, limit);
|
|
2887
|
-
if (dagMessages.length > 0)
|
|
2888
|
-
return dagMessages;
|
|
2889
|
-
// DAG walk returned empty (e.g., legacy data without parent chains) — fall through
|
|
2890
|
-
}
|
|
2891
|
-
const conversation = store.getConversation(sessionKey);
|
|
2892
|
-
if (!conversation)
|
|
2893
|
-
return [];
|
|
2894
|
-
if (topicId) {
|
|
2895
|
-
// P3.4: Option B — active topic messages + legacy NULL messages
|
|
2896
|
-
return store.getRecentMessagesByTopic(conversation.id, topicId, limit, fenceMessageId);
|
|
2897
|
-
}
|
|
2898
|
-
return store.getRecentMessages(conversation.id, limit, fenceMessageId);
|
|
2899
|
-
}
|
|
2900
|
-
// ─── L4 Library Builders ─────────────────────────────────────
|
|
2901
|
-
/**
|
|
2902
|
-
* Build facts content from library DB.
|
|
2903
|
-
*/
|
|
2904
|
-
/**
|
|
2905
|
-
* Build facts content from library DB.
|
|
2906
|
-
* Applies filterByScope (W1) to enforce retrieval access control.
|
|
2907
|
-
* Returns [content, factCount, scopeFilteredCount] or null if DB unavailable.
|
|
2908
|
-
*/
|
|
2909
|
-
buildFactsFromDb(agentId, sessionKey, db) {
|
|
2910
|
-
const sections = this.buildFactSectionsFromDb(agentId, sessionKey, db);
|
|
2911
|
-
if (!sections)
|
|
2912
|
-
return null;
|
|
2913
|
-
const combined = [sections.stableContent, sections.volatileContent]
|
|
2914
|
-
.filter((value) => Boolean(value))
|
|
2915
|
-
.join('\n');
|
|
2916
|
-
return [
|
|
2917
|
-
combined || null,
|
|
2918
|
-
sections.stableCount + sections.volatileCount,
|
|
2919
|
-
sections.filteredCount,
|
|
2920
|
-
];
|
|
2921
|
-
}
|
|
2922
|
-
buildFactSectionsFromDb(agentId, sessionKey, db) {
|
|
2923
|
-
if (!db)
|
|
2924
|
-
return null;
|
|
2925
|
-
const tableExists = db.prepare("SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='facts'").get();
|
|
2926
|
-
if (!tableExists || tableExists.cnt === 0)
|
|
2927
|
-
return null;
|
|
2928
|
-
const rawRows = db.prepare(`
|
|
2929
|
-
SELECT content, domain, confidence, agent_id, source_session_key AS session_key, scope FROM facts
|
|
2930
|
-
WHERE agent_id = ?
|
|
2931
|
-
AND superseded_by IS NULL
|
|
2932
|
-
AND (expires_at IS NULL OR expires_at > datetime('now'))
|
|
2933
|
-
AND decay_score < 0.8
|
|
2934
|
-
AND confidence >= 0.5
|
|
2935
|
-
ORDER BY confidence DESC, decay_score ASC
|
|
2936
|
-
LIMIT ?
|
|
2937
|
-
`).all(agentId, this.config.maxFacts);
|
|
2938
|
-
if (rawRows.length === 0) {
|
|
2939
|
-
return {
|
|
2940
|
-
stableContent: null,
|
|
2941
|
-
stableCount: 0,
|
|
2942
|
-
volatileContent: null,
|
|
2943
|
-
volatileCount: 0,
|
|
2944
|
-
filteredCount: 0,
|
|
2945
|
-
};
|
|
2946
|
-
}
|
|
2947
|
-
const ctx = { agentId, sessionKey };
|
|
2948
|
-
const { allowed, filteredCount } = filterByScope(rawRows.map(r => ({
|
|
2949
|
-
...r,
|
|
2950
|
-
agentId: r.agent_id,
|
|
2951
|
-
sessionKey: r.session_key,
|
|
2952
|
-
})), ctx);
|
|
2953
|
-
if (allowed.length === 0) {
|
|
2954
|
-
return {
|
|
2955
|
-
stableContent: null,
|
|
2956
|
-
stableCount: 0,
|
|
2957
|
-
volatileContent: null,
|
|
2958
|
-
volatileCount: 0,
|
|
2959
|
-
filteredCount,
|
|
2960
|
-
};
|
|
2961
|
-
}
|
|
2962
|
-
const formatRows = (rows) => {
|
|
2963
|
-
if (rows.length === 0)
|
|
2964
|
-
return null;
|
|
2965
|
-
return rows
|
|
2966
|
-
.map(r => {
|
|
2967
|
-
const fromOtherSession = r.sessionKey && r.sessionKey !== sessionKey;
|
|
2968
|
-
const sessionSuffix = fromOtherSession
|
|
2969
|
-
? `, session:${r.sessionKey.slice(-8)}`
|
|
2970
|
-
: '';
|
|
2971
|
-
return `- [${r.domain || 'general'}${sessionSuffix}] ${r.content}`;
|
|
2972
|
-
})
|
|
2973
|
-
.join('\n');
|
|
2974
|
-
};
|
|
2975
|
-
const stableRows = allowed.filter(r => r.scope !== 'session' && (!r.sessionKey || r.sessionKey !== sessionKey));
|
|
2976
|
-
const volatileRows = allowed.filter(r => !stableRows.includes(r));
|
|
2977
|
-
return {
|
|
2978
|
-
stableContent: formatRows(stableRows),
|
|
2979
|
-
stableCount: stableRows.length,
|
|
2980
|
-
volatileContent: formatRows(volatileRows),
|
|
2981
|
-
volatileCount: volatileRows.length,
|
|
2982
|
-
filteredCount,
|
|
2983
|
-
};
|
|
2984
|
-
}
|
|
2985
|
-
/**
|
|
2986
|
-
* Build knowledge content from library DB.
|
|
2987
|
-
* Prioritizes high-confidence, non-superseded entries.
|
|
2988
|
-
*/
|
|
2989
|
-
buildKnowledgeFromDb(agentId, db) {
|
|
2990
|
-
const tableExists = db.prepare("SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='knowledge'").get();
|
|
2991
|
-
if (!tableExists || tableExists.cnt === 0)
|
|
2992
|
-
return null;
|
|
2993
|
-
const rows = db.prepare(`
|
|
2994
|
-
SELECT domain, key, content, confidence FROM knowledge
|
|
2995
|
-
WHERE agent_id = ?
|
|
2996
|
-
AND superseded_by IS NULL
|
|
2997
|
-
AND (expires_at IS NULL OR expires_at > datetime('now'))
|
|
2998
|
-
ORDER BY confidence DESC, updated_at DESC
|
|
2999
|
-
LIMIT 15
|
|
3000
|
-
`).all(agentId);
|
|
3001
|
-
if (rows.length === 0)
|
|
3002
|
-
return null;
|
|
3003
|
-
// Group by domain for cleaner presentation
|
|
3004
|
-
const byDomain = {};
|
|
3005
|
-
for (const row of rows) {
|
|
3006
|
-
if (!byDomain[row.domain])
|
|
3007
|
-
byDomain[row.domain] = [];
|
|
3008
|
-
byDomain[row.domain].push({ key: row.key, content: row.content });
|
|
3009
|
-
}
|
|
3010
|
-
const lines = [];
|
|
3011
|
-
for (const [domain, entries] of Object.entries(byDomain)) {
|
|
3012
|
-
lines.push(`### ${domain}`);
|
|
3013
|
-
for (const entry of entries) {
|
|
3014
|
-
lines.push(`- **${entry.key}:** ${entry.content}`);
|
|
3015
|
-
}
|
|
3016
|
-
}
|
|
3017
|
-
return lines.join('\n');
|
|
3018
|
-
}
|
|
3019
|
-
/**
|
|
3020
|
-
* Build wiki page context for the active topic.
|
|
3021
|
-
* Queries the knowledge table for a synthesized topic page and returns it
|
|
3022
|
-
* wrapped with a header. Capped at 600 tokens.
|
|
3023
|
-
*/
|
|
3024
|
-
buildWikiPageContext(agentId, topicName, db) {
|
|
3025
|
-
const knowledgeStore = new KnowledgeStore(db);
|
|
3026
|
-
const knowledge = knowledgeStore.get(agentId, 'topic-synthesis', topicName);
|
|
3027
|
-
if (!knowledge)
|
|
3028
|
-
return null;
|
|
3029
|
-
const wrapped = `## Active Topic: ${topicName}\n${knowledge.content}`;
|
|
3030
|
-
return this.truncateToTokens(wrapped, 600);
|
|
3031
|
-
}
|
|
3032
|
-
/**
|
|
3033
|
-
* Build preferences content from library DB.
|
|
3034
|
-
* Shows user/operator preferences relevant to this agent.
|
|
3035
|
-
*/
|
|
3036
|
-
buildPreferencesFromDb(agentId, db) {
|
|
3037
|
-
const tableExists = db.prepare("SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='preferences'").get();
|
|
3038
|
-
if (!tableExists || tableExists.cnt === 0)
|
|
3039
|
-
return null;
|
|
3040
|
-
// Get preferences set by this agent or marked fleet-visible
|
|
3041
|
-
const rows = db.prepare(`
|
|
3042
|
-
SELECT subject, key, value, domain, confidence FROM preferences
|
|
3043
|
-
WHERE (agent_id = ? OR agent_id IS NULL)
|
|
3044
|
-
ORDER BY confidence DESC, updated_at DESC
|
|
3045
|
-
LIMIT 10
|
|
3046
|
-
`).all(agentId);
|
|
3047
|
-
if (rows.length === 0)
|
|
3048
|
-
return null;
|
|
3049
|
-
// Group by subject
|
|
3050
|
-
const bySubject = {};
|
|
3051
|
-
for (const row of rows) {
|
|
3052
|
-
if (!bySubject[row.subject])
|
|
3053
|
-
bySubject[row.subject] = [];
|
|
3054
|
-
bySubject[row.subject].push({ key: row.key, value: row.value, domain: row.domain });
|
|
3055
|
-
}
|
|
3056
|
-
const lines = [];
|
|
3057
|
-
for (const [subject, prefs] of Object.entries(bySubject)) {
|
|
3058
|
-
lines.push(`### ${subject}`);
|
|
3059
|
-
for (const pref of prefs) {
|
|
3060
|
-
const domainTag = pref.domain ? ` [${pref.domain}]` : '';
|
|
3061
|
-
lines.push(`- **${pref.key}:**${domainTag} ${pref.value}`);
|
|
3062
|
-
}
|
|
3063
|
-
}
|
|
3064
|
-
return lines.join('\n');
|
|
3065
|
-
}
|
|
3066
|
-
// ─── L3 Hybrid Retrieval (FTS5 + KNN) ───────────────────────
|
|
3067
|
-
/**
|
|
3068
|
-
* Build semantic recall content using hybrid FTS5+KNN retrieval.
|
|
3069
|
-
*
|
|
3070
|
-
* Uses Reciprocal Rank Fusion to merge keyword and vector results.
|
|
3071
|
-
* Gracefully degrades: FTS5-only when no vector store, KNN-only
|
|
3072
|
-
* when FTS query is empty (all stop words), both when available.
|
|
3073
|
-
*
|
|
3074
|
-
* @param precomputedEmbedding — optional pre-computed embedding for the query.
|
|
3075
|
-
* When provided, the Ollama call inside VectorStore.search() is skipped.
|
|
3076
|
-
*/
|
|
3077
|
-
async buildSemanticRecall(userMessage, agentId, maxTokens, libraryDb, precomputedEmbedding, existingFingerprints // C2: skip results already in Active Facts
|
|
3078
|
-
) {
|
|
3079
|
-
const libDb = libraryDb || this.libraryDb;
|
|
3080
|
-
if (!libDb && !this.vectorStore)
|
|
3081
|
-
return null;
|
|
3082
|
-
// Inline fingerprint helper (mirrors compose-scope version; C2 dedup only used here)
|
|
3083
|
-
const fpCheck = existingFingerprints
|
|
3084
|
-
? (text) => existingFingerprints.has(text.toLowerCase().replace(/\s+/g, ' ').trim().slice(0, 120))
|
|
3085
|
-
: () => false;
|
|
3086
|
-
// Use hybrid search when library DB is available
|
|
3087
|
-
if (libDb) {
|
|
3088
|
-
const results = await hybridSearch(libDb, this.vectorStore, userMessage, {
|
|
3089
|
-
tables: ['facts', 'knowledge', 'episodes'],
|
|
3090
|
-
limit: 10,
|
|
3091
|
-
agentId,
|
|
3092
|
-
maxKnnDistance: 1.2,
|
|
3093
|
-
precomputedEmbedding,
|
|
3094
|
-
});
|
|
3095
|
-
if (results.length === 0)
|
|
3096
|
-
return null;
|
|
3097
|
-
const lines = [];
|
|
3098
|
-
let tokens = 0;
|
|
3099
|
-
// TUNE-015: apply recency decay to recall scores.
|
|
3100
|
-
// Messages and episodes from distant past score down even if semantically relevant.
|
|
3101
|
-
// A 5-day-old task-request should not compete equally with today's messages.
|
|
3102
|
-
// - Episodes: exponential decay, half-life 7 days
|
|
3103
|
-
// - Facts/knowledge: step-function penalty for items older than 48h
|
|
3104
|
-
// (prevents completed/stale tasks from outranking recent ones)
|
|
3105
|
-
// 48-72h: multiply by 0.7
|
|
3106
|
-
// >72h: multiply by 0.5
|
|
3107
|
-
const now = Date.now();
|
|
3108
|
-
const decayedResults = results.map(result => {
|
|
3109
|
-
if (!result.createdAt)
|
|
3110
|
-
return result;
|
|
3111
|
-
const ageMs = now - new Date(result.createdAt).getTime();
|
|
3112
|
-
const ageDays = ageMs / 86_400_000;
|
|
3113
|
-
if (result.sourceTable === 'episodes') {
|
|
3114
|
-
// Exponential half-life decay for episodes
|
|
3115
|
-
const decayFactor = Math.pow(0.5, ageDays / 7);
|
|
3116
|
-
return { ...result, score: result.score * decayFactor };
|
|
3117
|
-
}
|
|
3118
|
-
// Step-function recency penalty for facts and knowledge
|
|
3119
|
-
const ageHours = ageMs / 3_600_000;
|
|
3120
|
-
if (ageHours > 72) {
|
|
3121
|
-
return { ...result, score: result.score * 0.5 };
|
|
3122
|
-
}
|
|
3123
|
-
if (ageHours > 48) {
|
|
3124
|
-
return { ...result, score: result.score * 0.7 };
|
|
3125
|
-
}
|
|
3126
|
-
return result;
|
|
3127
|
-
});
|
|
3128
|
-
// Re-sort after decay adjustment
|
|
3129
|
-
decayedResults.sort((a, b) => b.score - a.score);
|
|
3130
|
-
for (const result of decayedResults) {
|
|
3131
|
-
// TUNE-001: drop very-low-relevance results (RRF scores below 0.008 are noise)
|
|
3132
|
-
if (result.score < 0.008)
|
|
3133
|
-
continue;
|
|
3134
|
-
// TUNE-016: FTS-only results require higher floor — low-score FTS hits are noise
|
|
3135
|
-
if (result.sources.length === 1 && result.sources[0] === 'fts' && result.score < 0.05)
|
|
3136
|
-
continue;
|
|
3137
|
-
// TUNE-014: episodes require higher confidence — score:2 episodes bleed adjacent
|
|
3138
|
-
// session context and contaminate current session. Require fts+knn agreement
|
|
3139
|
-
// (score >= 0.04) for episodes to make it into assembled context.
|
|
3140
|
-
if (result.sourceTable === 'episodes' && result.score < 0.04)
|
|
3141
|
-
continue;
|
|
3142
|
-
// C2: Skip results whose content is already fingerprinted (e.g. in Active Facts)
|
|
3143
|
-
// Dedup count is not tracked separately here — compose-level counter covers the other paths.
|
|
3144
|
-
if (fpCheck(result.content))
|
|
3145
|
-
continue;
|
|
3146
|
-
const label = this.formatHybridResult(result);
|
|
3147
|
-
const lineTokens = estimateTokens(label);
|
|
3148
|
-
if (tokens + lineTokens > maxTokens)
|
|
3149
|
-
break;
|
|
3150
|
-
lines.push(label);
|
|
3151
|
-
tokens += lineTokens;
|
|
3152
|
-
}
|
|
3153
|
-
return lines.length > 0 ? lines.join('\n') : null;
|
|
3154
|
-
}
|
|
3155
|
-
// Fallback: KNN-only when no library DB (legacy path)
|
|
3156
|
-
if (!this.vectorStore)
|
|
3157
|
-
return null;
|
|
3158
|
-
const results = await this.vectorStore.search(userMessage, {
|
|
3159
|
-
tables: ['facts', 'knowledge', 'episodes'],
|
|
3160
|
-
limit: 8,
|
|
3161
|
-
maxDistance: 1.2,
|
|
3162
|
-
precomputedEmbedding,
|
|
3163
|
-
});
|
|
3164
|
-
if (results.length === 0)
|
|
3165
|
-
return null;
|
|
3166
|
-
const lines = [];
|
|
3167
|
-
let tokens = 0;
|
|
3168
|
-
for (const result of results) {
|
|
3169
|
-
const label = this.formatVectorResult(result);
|
|
3170
|
-
const lineTokens = estimateTokens(label);
|
|
3171
|
-
if (tokens + lineTokens > maxTokens)
|
|
3172
|
-
break;
|
|
3173
|
-
lines.push(label);
|
|
3174
|
-
tokens += lineTokens;
|
|
3175
|
-
}
|
|
3176
|
-
return lines.length > 0 ? lines.join('\n') : null;
|
|
3177
|
-
}
|
|
3178
|
-
/**
|
|
3179
|
-
* Format a hybrid search result for injection into context.
|
|
3180
|
-
* Shows retrieval source(s) and relevance score.
|
|
3181
|
-
*/
|
|
3182
|
-
formatHybridResult(result) {
|
|
3183
|
-
const type = result.sourceTable;
|
|
3184
|
-
const sourceTag = result.sources.length === 2 ? 'fts+knn' : result.sources[0];
|
|
3185
|
-
const scoreStr = (result.score * 100).toFixed(0);
|
|
3186
|
-
switch (type) {
|
|
3187
|
-
case 'facts':
|
|
3188
|
-
return `- [fact, ${sourceTag}, score:${scoreStr}] ${result.content}`;
|
|
3189
|
-
case 'knowledge':
|
|
3190
|
-
return `- [knowledge/${result.metadata || 'general'}, ${sourceTag}, score:${scoreStr}] ${result.content}`;
|
|
3191
|
-
case 'episodes':
|
|
3192
|
-
return `- [episode/${result.domain || 'event'}, ${sourceTag}, score:${scoreStr}] ${result.content}`;
|
|
3193
|
-
default:
|
|
3194
|
-
return `- [${type}, ${sourceTag}, score:${scoreStr}] ${result.content}`;
|
|
3195
|
-
}
|
|
3196
|
-
}
|
|
3197
|
-
/**
|
|
3198
|
-
* Format a vector-only search result (legacy fallback).
|
|
3199
|
-
*/
|
|
3200
|
-
formatVectorResult(result) {
|
|
3201
|
-
const relevance = Math.max(0, Math.round((1 - result.distance) * 100));
|
|
3202
|
-
const type = result.sourceTable;
|
|
3203
|
-
switch (type) {
|
|
3204
|
-
case 'facts':
|
|
3205
|
-
return `- [fact, ${relevance}% relevant] ${result.content}`;
|
|
3206
|
-
case 'knowledge':
|
|
3207
|
-
return `- [knowledge/${result.metadata || 'general'}, ${relevance}% relevant] ${result.content}`;
|
|
3208
|
-
case 'episodes':
|
|
3209
|
-
return `- [episode/${result.domain || 'event'}, ${relevance}% relevant] ${result.content}`;
|
|
3210
|
-
default:
|
|
3211
|
-
return `- [${type}, ${relevance}% relevant] ${result.content}`;
|
|
3212
|
-
}
|
|
3213
|
-
}
|
|
3214
|
-
// ─── L2 Cross-Session Context ────────────────────────────────
|
|
3215
|
-
/**
|
|
3216
|
-
* Build cross-session context by finding recent activity
|
|
3217
|
-
* in other sessions for this agent.
|
|
3218
|
-
*/
|
|
3219
|
-
// TODO Phase 1: buildCrossSessionContext queries OTHER conversations. Each has its
|
|
3220
|
-
// own compaction fence. Per-conversation fence filtering should be added here so
|
|
3221
|
-
// zombie messages from other sessions don't leak into cross-session context.
|
|
3222
|
-
buildCrossSessionContext(agentId, currentSessionKey, db, _libraryDb, existingFingerprints // C3: skip entries already in facts/semantic recall
|
|
3223
|
-
) {
|
|
3224
|
-
const conversation = db.prepare('SELECT id FROM conversations WHERE session_key = ?').get(currentSessionKey);
|
|
3225
|
-
if (!conversation)
|
|
3226
|
-
return null;
|
|
3227
|
-
const rows = db.prepare(`
|
|
3228
|
-
SELECT m.text_content, m.role, c.channel_type, m.created_at
|
|
3229
|
-
FROM messages m
|
|
3230
|
-
JOIN conversations c ON m.conversation_id = c.id
|
|
3231
|
-
WHERE c.agent_id = ?
|
|
3232
|
-
AND m.conversation_id != ?
|
|
3233
|
-
AND c.status = 'active'
|
|
3234
|
-
AND m.text_content IS NOT NULL
|
|
3235
|
-
AND m.is_heartbeat = 0
|
|
3236
|
-
ORDER BY m.created_at DESC
|
|
3237
|
-
LIMIT 10
|
|
3238
|
-
`).all(agentId, conversation.id);
|
|
3239
|
-
if (rows.length === 0)
|
|
3240
|
-
return null;
|
|
3241
|
-
const fpCheck = existingFingerprints
|
|
3242
|
-
? (text) => existingFingerprints.has(text.toLowerCase().replace(/\s+/g, ' ').trim().slice(0, 120))
|
|
3243
|
-
: () => false;
|
|
3244
|
-
const lines = [];
|
|
3245
|
-
for (const r of rows) {
|
|
3246
|
-
// C3: Skip cross-session entries whose content fingerprint already appears in context
|
|
3247
|
-
if (fpCheck(r.text_content))
|
|
3248
|
-
continue;
|
|
3249
|
-
const preview = r.text_content.substring(0, 200);
|
|
3250
|
-
lines.push(`- [${r.channel_type}/${r.role} @ ${r.created_at}] ${preview}`);
|
|
3251
|
-
}
|
|
3252
|
-
return lines.length > 0 ? lines.join('\n') : null;
|
|
3253
|
-
}
|
|
3254
|
-
// ─── Utilities ───────────────────────────────────────────────
|
|
3255
|
-
/**
|
|
3256
|
-
* Extract the last user message text from the composed messages.
|
|
3257
|
-
*/
|
|
3258
|
-
getLastUserMessage(messages) {
|
|
3259
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
3260
|
-
if (messages[i].role === 'user' && messages[i].textContent) {
|
|
3261
|
-
return messages[i].textContent;
|
|
3262
|
-
}
|
|
3263
|
-
}
|
|
3264
|
-
return null;
|
|
3265
|
-
}
|
|
3266
|
-
/**
|
|
3267
|
-
* Truncate text to approximately fit within a token budget.
|
|
3268
|
-
* Truncates at line boundaries when possible.
|
|
3269
|
-
*/
|
|
3270
|
-
truncateToTokens(text, maxTokens) {
|
|
3271
|
-
const maxChars = maxTokens * 4; // inverse of our estimation
|
|
3272
|
-
if (text.length <= maxChars)
|
|
3273
|
-
return text;
|
|
3274
|
-
// Try to truncate at a line boundary
|
|
3275
|
-
const truncated = text.substring(0, maxChars);
|
|
3276
|
-
const lastNewline = truncated.lastIndexOf('\n');
|
|
3277
|
-
if (lastNewline > maxChars * 0.7) {
|
|
3278
|
-
return truncated.substring(0, lastNewline) + '\n…';
|
|
3279
|
-
}
|
|
3280
|
-
return truncated + '…';
|
|
3281
|
-
}
|
|
3282
|
-
// ─── Keystone History Builder ─────────────────────────────────────
|
|
3283
|
-
/**
|
|
3284
|
-
* Query and score keystone candidates from before the current history window.
|
|
3285
|
-
*
|
|
3286
|
-
* Trims the oldest messages from includedHistory to free a keystone budget,
|
|
3287
|
-
* then queries the DB for older messages scored by episode significance,
|
|
3288
|
-
* FTS5 relevance, and recency.
|
|
3289
|
-
*
|
|
3290
|
-
* Returns null if keystones cannot be injected (no cutoff ID found,
|
|
3291
|
-
* no candidates, or all errors).
|
|
3292
|
-
*/
|
|
3293
|
-
async buildKeystones(db, agentId, includedHistory, historyTokens, keystoneFraction, keystoneMaxMsgs, prompt, libraryDb, fenceMessageId, activeContext) {
|
|
3294
|
-
const keystoneBudget = Math.floor(historyTokens * keystoneFraction);
|
|
3295
|
-
if (keystoneBudget <= 0)
|
|
3296
|
-
return null;
|
|
3297
|
-
// Trim oldest messages from includedHistory to free keystone budget.
|
|
3298
|
-
const trimmedHistory = [...includedHistory];
|
|
3299
|
-
let trimmedHistoryTokens = historyTokens;
|
|
3300
|
-
let freed = 0;
|
|
3301
|
-
while (trimmedHistory.length > 1 && freed < keystoneBudget) {
|
|
3302
|
-
const oldest = trimmedHistory.shift();
|
|
3303
|
-
const oldestTokens = estimateMessageTokens(oldest);
|
|
3304
|
-
freed += oldestTokens;
|
|
3305
|
-
trimmedHistoryTokens -= oldestTokens;
|
|
3306
|
-
}
|
|
3307
|
-
// Find the oldest message ID in the trimmed recent window (cutoff point).
|
|
3308
|
-
const oldestRecentMsg = trimmedHistory[0];
|
|
3309
|
-
const cutoffId = oldestRecentMsg?.id ?? null;
|
|
3310
|
-
if (cutoffId == null)
|
|
3311
|
-
return null;
|
|
3312
|
-
// Find the current user prompt for FTS matching.
|
|
3313
|
-
const promptForFts = prompt?.trim() ||
|
|
3314
|
-
(() => {
|
|
3315
|
-
for (let i = trimmedHistory.length - 1; i >= 0; i--) {
|
|
3316
|
-
if (trimmedHistory[i].role === 'user' && trimmedHistory[i].textContent) {
|
|
3317
|
-
return trimmedHistory[i].textContent;
|
|
3318
|
-
}
|
|
3319
|
-
}
|
|
3320
|
-
return null;
|
|
3321
|
-
})();
|
|
3322
|
-
try {
|
|
3323
|
-
// Get the conversation ID from the oldest recent message.
|
|
3324
|
-
const convRow = db.prepare('SELECT conversation_id FROM messages WHERE id = ?').get(cutoffId);
|
|
3325
|
-
if (!convRow)
|
|
3326
|
-
return null;
|
|
3327
|
-
const conversationId = convRow.conversation_id;
|
|
3328
|
-
const maxAgeHours = 168; // 7 days — tighter window gives recency real scoring weight
|
|
3329
|
-
const nowMs = Date.now();
|
|
3330
|
-
// Build episode significance map from libraryDb (episodes live there, not in messages.db).
|
|
3331
|
-
// Key: source_message_id, Value: max significance for that message.
|
|
3332
|
-
const sigMap = new Map();
|
|
3333
|
-
if (libraryDb) {
|
|
3334
|
-
try {
|
|
3335
|
-
const episodeRows = libraryDb.prepare(`
|
|
3336
|
-
SELECT source_message_id, MAX(significance) AS significance
|
|
3337
|
-
FROM episodes
|
|
3338
|
-
WHERE agent_id = ? AND source_message_id IS NOT NULL
|
|
3339
|
-
GROUP BY source_message_id
|
|
3340
|
-
`).all(agentId);
|
|
3341
|
-
for (const row of episodeRows) {
|
|
3342
|
-
sigMap.set(row.source_message_id, row.significance);
|
|
3343
|
-
}
|
|
3344
|
-
}
|
|
3345
|
-
catch {
|
|
3346
|
-
// Episodes query is best-effort
|
|
3347
|
-
}
|
|
3348
|
-
}
|
|
3349
|
-
const fenceClause = fenceMessageId != null ? 'AND m.id >= ?' : '';
|
|
3350
|
-
// Phase 3 (Turn DAG): prefer context_id scoping over conversation_id+fence
|
|
3351
|
-
const contextClause = activeContext ? 'AND m.context_id = ?' : '';
|
|
3352
|
-
const baseParams = [conversationId, cutoffId];
|
|
3353
|
-
if (fenceMessageId != null)
|
|
3354
|
-
baseParams.push(fenceMessageId);
|
|
3355
|
-
if (activeContext)
|
|
3356
|
-
baseParams.push(activeContext.id);
|
|
3357
|
-
const baseQuery = `
|
|
3358
|
-
SELECT
|
|
3359
|
-
m.id,
|
|
3360
|
-
m.message_index,
|
|
3361
|
-
m.role,
|
|
3362
|
-
m.text_content,
|
|
3363
|
-
m.created_at
|
|
3364
|
-
FROM messages m
|
|
3365
|
-
WHERE m.conversation_id = ?
|
|
3366
|
-
AND m.id < ?
|
|
3367
|
-
${fenceClause}
|
|
3368
|
-
${contextClause}
|
|
3369
|
-
AND m.text_content IS NOT NULL
|
|
3370
|
-
AND m.is_heartbeat = 0
|
|
3371
|
-
AND m.text_content != ''
|
|
3372
|
-
LIMIT 200
|
|
3373
|
-
`;
|
|
3374
|
-
let candidateRows;
|
|
3375
|
-
if (promptForFts && promptForFts.length >= 3) {
|
|
3376
|
-
// Build a safe FTS5 query: extract words ≥3 chars, up to 8, OR with prefix.
|
|
3377
|
-
const ftsTerms = (promptForFts.match(/\b\w{3,}\b/g) || [])
|
|
3378
|
-
.slice(0, 8)
|
|
3379
|
-
.map(w => `"${w.replace(/"/g, '')}"*`)
|
|
3380
|
-
.join(' OR ');
|
|
3381
|
-
if (ftsTerms) {
|
|
3382
|
-
try {
|
|
3383
|
-
const ftsParams = [conversationId, cutoffId];
|
|
3384
|
-
if (fenceMessageId != null)
|
|
3385
|
-
ftsParams.push(fenceMessageId);
|
|
3386
|
-
if (activeContext)
|
|
3387
|
-
ftsParams.push(activeContext.id);
|
|
3388
|
-
ftsParams.push(ftsTerms);
|
|
3389
|
-
candidateRows = db.prepare(`
|
|
3390
|
-
SELECT
|
|
3391
|
-
m.id,
|
|
3392
|
-
m.message_index,
|
|
3393
|
-
m.role,
|
|
3394
|
-
m.text_content,
|
|
3395
|
-
m.created_at
|
|
3396
|
-
FROM messages m
|
|
3397
|
-
WHERE m.conversation_id = ?
|
|
3398
|
-
AND m.id < ?
|
|
3399
|
-
${fenceClause}
|
|
3400
|
-
${contextClause}
|
|
3401
|
-
AND m.text_content IS NOT NULL
|
|
3402
|
-
AND m.is_heartbeat = 0
|
|
3403
|
-
AND m.text_content != ''
|
|
3404
|
-
AND m.id IN (
|
|
3405
|
-
SELECT rowid FROM messages_fts
|
|
3406
|
-
WHERE messages_fts MATCH ?
|
|
3407
|
-
LIMIT 100
|
|
3408
|
-
)
|
|
3409
|
-
LIMIT 200
|
|
3410
|
-
`).all(...ftsParams);
|
|
3411
|
-
}
|
|
3412
|
-
catch {
|
|
3413
|
-
// FTS query may fail on special characters — fall back to base query
|
|
3414
|
-
candidateRows = db.prepare(baseQuery).all(...baseParams);
|
|
3415
|
-
}
|
|
3416
|
-
}
|
|
3417
|
-
else {
|
|
3418
|
-
candidateRows = db.prepare(baseQuery).all(...baseParams);
|
|
3419
|
-
}
|
|
3420
|
-
}
|
|
3421
|
-
else {
|
|
3422
|
-
candidateRows = db.prepare(baseQuery).all(...baseParams);
|
|
3423
|
-
}
|
|
3424
|
-
if (candidateRows.length === 0)
|
|
3425
|
-
return null;
|
|
3426
|
-
// Build KeystoneCandidate objects with computed ftsRank and ageHours.
|
|
3427
|
-
const totalCandidates = candidateRows.length;
|
|
3428
|
-
const candidates = candidateRows.map((row, idx) => {
|
|
3429
|
-
const createdMs = new Date(row.created_at).getTime();
|
|
3430
|
-
const ageHours = (nowMs - createdMs) / (1000 * 60 * 60);
|
|
3431
|
-
// Normalize FTS rank by position (best match = 1.0, worst = 0.1)
|
|
3432
|
-
const ftsRank = totalCandidates > 1
|
|
3433
|
-
? 1.0 - (idx / totalCandidates) * 0.9
|
|
3434
|
-
: 1.0;
|
|
3435
|
-
return {
|
|
3436
|
-
messageId: row.id,
|
|
3437
|
-
messageIndex: row.message_index,
|
|
3438
|
-
role: row.role,
|
|
3439
|
-
content: row.text_content || '',
|
|
3440
|
-
timestamp: row.created_at,
|
|
3441
|
-
episodeSignificance: sigMap.get(row.id) ?? null,
|
|
3442
|
-
ftsRank,
|
|
3443
|
-
ageHours,
|
|
3444
|
-
};
|
|
3445
|
-
});
|
|
3446
|
-
// Score and rank candidates.
|
|
3447
|
-
const ranked = rankKeystones(candidates, maxAgeHours);
|
|
3448
|
-
// Budget-fit: take top-scored candidates until keystoneBudget exhausted.
|
|
3449
|
-
let kTokens = 0;
|
|
3450
|
-
const selectedKeystones = [];
|
|
3451
|
-
for (const candidate of ranked) {
|
|
3452
|
-
if (selectedKeystones.length >= keystoneMaxMsgs)
|
|
3453
|
-
break;
|
|
3454
|
-
const msg = {
|
|
3455
|
-
role: candidate.role,
|
|
3456
|
-
textContent: candidate.content,
|
|
3457
|
-
toolCalls: null,
|
|
3458
|
-
toolResults: null,
|
|
3459
|
-
};
|
|
3460
|
-
const msgTokens = estimateMessageTokens(msg);
|
|
3461
|
-
if (kTokens + msgTokens > keystoneBudget)
|
|
3462
|
-
continue; // skip oversized; keep trying
|
|
3463
|
-
selectedKeystones.push(candidate);
|
|
3464
|
-
kTokens += msgTokens;
|
|
3465
|
-
}
|
|
3466
|
-
if (selectedKeystones.length === 0)
|
|
3467
|
-
return null;
|
|
3468
|
-
// Sort selected keystones chronologically for injection.
|
|
3469
|
-
selectedKeystones.sort((a, b) => a.messageIndex - b.messageIndex);
|
|
3470
|
-
const keystoneMessages = selectedKeystones.map(c => ({
|
|
3471
|
-
role: c.role,
|
|
3472
|
-
textContent: c.content,
|
|
3473
|
-
toolCalls: null,
|
|
3474
|
-
toolResults: null,
|
|
3475
|
-
}));
|
|
3476
|
-
return {
|
|
3477
|
-
keystoneMessages,
|
|
3478
|
-
keystoneTokens: kTokens,
|
|
3479
|
-
trimmedHistory,
|
|
3480
|
-
trimmedHistoryTokens,
|
|
3481
|
-
};
|
|
3482
|
-
}
|
|
3483
|
-
catch {
|
|
3484
|
-
// Keystone injection is best-effort — never fail compose
|
|
3485
|
-
return null;
|
|
3486
|
-
}
|
|
3487
|
-
}
|
|
3488
|
-
// ─── Cross-Topic Keystone Retrieval (P3.5) ───────────────────────
|
|
3489
|
-
/**
|
|
3490
|
-
* Pull high-signal messages from OTHER topics in this session when their
|
|
3491
|
-
* content is semantically relevant to the current active topic.
|
|
3492
|
-
*
|
|
3493
|
-
* Heuristic-only: no model calls. Token overlap between the current topic
|
|
3494
|
-
* name + last 3 user messages and candidate message content.
|
|
3495
|
-
*
|
|
3496
|
-
* @param agentId - The agent's ID
|
|
3497
|
-
* @param sessionKey - Current session key
|
|
3498
|
-
* @param activeTopic - The current active topic (id + name)
|
|
3499
|
-
* @param currentMessages - Recently included history messages for query extraction
|
|
3500
|
-
* @param db - The messages database
|
|
3501
|
-
* @param maxKeystones - Max cross-topic keystones to return (default 3)
|
|
3502
|
-
* @returns Scored keystones sorted by score DESC, deduplicated by message id
|
|
3503
|
-
*/
|
|
3504
|
-
async getKeystonesByTopic(agentId, sessionKey, activeTopic, currentMessages, db, maxKeystones = 3, fenceMessageId, activeContext) {
|
|
3505
|
-
const otherTopics = db.prepare(`
|
|
3506
|
-
SELECT id, name
|
|
3507
|
-
FROM topics
|
|
3508
|
-
WHERE session_key = ? AND id != ?
|
|
3509
|
-
ORDER BY last_active_at DESC
|
|
3510
|
-
LIMIT 5
|
|
3511
|
-
`).all(sessionKey, activeTopic.id);
|
|
3512
|
-
if (otherTopics.length === 0)
|
|
3513
|
-
return [];
|
|
3514
|
-
// Extract key terms from active topic name + last 3 user messages
|
|
3515
|
-
const queryTerms = this.extractQueryTerms(activeTopic.name, currentMessages);
|
|
3516
|
-
if (queryTerms.size === 0)
|
|
3517
|
-
return [];
|
|
3518
|
-
const nowMs = Date.now();
|
|
3519
|
-
const maxAgeHours = 168; // 7 days, same as within-session keystones
|
|
3520
|
-
const seenIds = new Set();
|
|
3521
|
-
const allCandidates = [];
|
|
3522
|
-
for (const topic of otherTopics) {
|
|
3523
|
-
let topicMessages;
|
|
3524
|
-
try {
|
|
3525
|
-
const topicFenceClause = fenceMessageId != null ? 'AND m.id >= ?' : '';
|
|
3526
|
-
// Phase 3 (Turn DAG): constrain cross-topic queries to active context_id
|
|
3527
|
-
const topicContextClause = activeContext ? 'AND m.context_id = ?' : '';
|
|
3528
|
-
const topicParams = [sessionKey, agentId, topic.id];
|
|
3529
|
-
if (fenceMessageId != null)
|
|
3530
|
-
topicParams.push(fenceMessageId);
|
|
3531
|
-
if (activeContext)
|
|
3532
|
-
topicParams.push(activeContext.id);
|
|
3533
|
-
topicMessages = db.prepare(`
|
|
3534
|
-
SELECT m.id, m.message_index, m.role, m.text_content, m.created_at
|
|
3535
|
-
FROM messages m
|
|
3536
|
-
JOIN conversations c ON m.conversation_id = c.id
|
|
3537
|
-
WHERE c.session_key = ?
|
|
3538
|
-
AND c.agent_id = ?
|
|
3539
|
-
AND m.topic_id = ?
|
|
3540
|
-
${topicFenceClause}
|
|
3541
|
-
${topicContextClause}
|
|
3542
|
-
AND m.text_content IS NOT NULL
|
|
3543
|
-
AND m.text_content != ''
|
|
3544
|
-
AND m.is_heartbeat = 0
|
|
3545
|
-
ORDER BY m.message_index DESC
|
|
3546
|
-
LIMIT 50
|
|
3547
|
-
`).all(...topicParams);
|
|
3548
|
-
}
|
|
3549
|
-
catch {
|
|
3550
|
-
// Corrupt topic data — skip this topic, never throw
|
|
3551
|
-
continue;
|
|
3552
|
-
}
|
|
3553
|
-
if (topicMessages.length === 0)
|
|
3554
|
-
continue;
|
|
3555
|
-
const topicCandidates = topicMessages.map((msg, idx) => {
|
|
3556
|
-
const createdMs = new Date(msg.created_at).getTime();
|
|
3557
|
-
const ageHours = (nowMs - createdMs) / (1000 * 60 * 60);
|
|
3558
|
-
const ftsRank = topicMessages.length > 1
|
|
3559
|
-
? 1.0 - (idx / topicMessages.length) * 0.9
|
|
3560
|
-
: 1.0;
|
|
3561
|
-
return {
|
|
3562
|
-
messageId: msg.id,
|
|
3563
|
-
messageIndex: msg.message_index,
|
|
3564
|
-
role: msg.role,
|
|
3565
|
-
content: msg.text_content,
|
|
3566
|
-
timestamp: msg.created_at,
|
|
3567
|
-
episodeSignificance: null,
|
|
3568
|
-
ftsRank,
|
|
3569
|
-
ageHours,
|
|
3570
|
-
};
|
|
3571
|
-
});
|
|
3572
|
-
const topTopicKeystones = rankKeystones(topicCandidates, maxAgeHours).slice(0, 10);
|
|
3573
|
-
// Filter to messages with semantic overlap (≥2 matching terms)
|
|
3574
|
-
const relevant = topTopicKeystones.filter(candidate => {
|
|
3575
|
-
const contentLower = candidate.content.toLowerCase();
|
|
3576
|
-
let matches = 0;
|
|
3577
|
-
for (const term of queryTerms) {
|
|
3578
|
-
if (contentLower.includes(term)) {
|
|
3579
|
-
matches++;
|
|
3580
|
-
if (matches >= 2)
|
|
3581
|
-
return true;
|
|
3582
|
-
}
|
|
3583
|
-
}
|
|
3584
|
-
return false;
|
|
3585
|
-
});
|
|
3586
|
-
if (relevant.length === 0)
|
|
3587
|
-
continue;
|
|
3588
|
-
// Re-score filtered candidates so they compete on the same final scale
|
|
3589
|
-
for (const candidate of relevant) {
|
|
3590
|
-
if (seenIds.has(candidate.messageId))
|
|
3591
|
-
continue;
|
|
3592
|
-
seenIds.add(candidate.messageId);
|
|
3593
|
-
const score = scoreKeystone(candidate, maxAgeHours);
|
|
3594
|
-
allCandidates.push({ ...candidate, score });
|
|
3595
|
-
}
|
|
3596
|
-
}
|
|
3597
|
-
if (allCandidates.length === 0)
|
|
3598
|
-
return [];
|
|
3599
|
-
// Sort by score DESC and return top maxKeystones
|
|
3600
|
-
return allCandidates
|
|
3601
|
-
.sort((a, b) => b.score - a.score)
|
|
3602
|
-
.slice(0, maxKeystones);
|
|
3603
|
-
}
|
|
3604
|
-
/**
|
|
3605
|
-
* Extract lowercase key terms from a topic name and the last 3 user messages.
|
|
3606
|
-
* Terms are: tokens with ≥4 characters (skip short stop words).
|
|
3607
|
-
* Returns a Set for O(1) lookup.
|
|
3608
|
-
*/
|
|
3609
|
-
extractQueryTerms(topicName, messages) {
|
|
3610
|
-
const terms = new Set();
|
|
3611
|
-
const MIN_TERM_LEN = 4;
|
|
3612
|
-
// From topic name
|
|
3613
|
-
const topicTokens = topicName.toLowerCase().match(/\b[a-z0-9]{4,}\b/g) ?? [];
|
|
3614
|
-
for (const t of topicTokens)
|
|
3615
|
-
terms.add(t);
|
|
3616
|
-
// From last 3 user messages
|
|
3617
|
-
let userCount = 0;
|
|
3618
|
-
for (let i = messages.length - 1; i >= 0 && userCount < 3; i--) {
|
|
3619
|
-
const msg = messages[i];
|
|
3620
|
-
if (msg.role === 'user' && msg.textContent) {
|
|
3621
|
-
const tokens = msg.textContent.toLowerCase().match(/\b[a-z0-9]{4,}\b/g) ?? [];
|
|
3622
|
-
for (const t of tokens) {
|
|
3623
|
-
if (t.length >= MIN_TERM_LEN)
|
|
3624
|
-
terms.add(t);
|
|
3625
|
-
}
|
|
3626
|
-
userCount++;
|
|
3627
|
-
}
|
|
3628
|
-
}
|
|
3629
|
-
return terms;
|
|
3630
|
-
}
|
|
3631
|
-
}
|
|
3632
|
-
//# sourceMappingURL=compositor.js.map
|