pi-mega-compact 0.11.3 → 0.11.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/extensions/dashboard-server/api-contracts/endpoints.js +19 -0
- package/dist/extensions/dashboard-server/api-contracts/memory.js +8 -0
- package/dist/extensions/dashboard-server/api-contracts/provider-cache.js +1 -0
- package/dist/extensions/dashboard-server/api-contracts/setup.js +10 -0
- package/dist/extensions/dashboard-server/index-reader.js +6 -2
- package/dist/extensions/dashboard-server/routes-cache.js +115 -55
- package/dist/extensions/dashboard-server/routes-memory.js +51 -0
- package/dist/extensions/dashboard-server/routes-repo.js +2 -2
- package/dist/extensions/dashboard-server/routes-setup.js +162 -0
- package/dist/extensions/dashboard-server/routes.js +2 -0
- package/dist/extensions/dashboard-server/server.js +7 -1
- package/dist/extensions/mega-commands.js +189 -1
- package/dist/extensions/mega-config.js +3 -0
- package/dist/extensions/mega-events/agent-handlers.js +41 -6
- package/dist/extensions/mega-events/compact-handlers.js +1 -0
- package/dist/extensions/mega-events/context-handler.js +37 -92
- package/dist/extensions/mega-events/context-hwm.js +51 -0
- package/dist/extensions/mega-events/error-classifier.js +118 -128
- package/dist/extensions/mega-events/mirror-append.js +107 -0
- package/dist/extensions/mega-events/outage-advisor.js +59 -0
- package/dist/extensions/mega-events/perf-handler.js +51 -1
- package/dist/extensions/mega-events/recall-tail.js +59 -0
- package/dist/extensions/mega-events/session-handlers.js +15 -8
- package/dist/extensions/mega-runtime/reset-runtime.js +6 -0
- package/dist/extensions/mega-runtime/runtime.js +7 -0
- package/dist/src/embedder.js +41 -2
- package/dist/src/memoryStats.js +75 -0
- package/dist/src/prefix-break.js +43 -0
- package/dist/src/pricing.js +1 -1
- package/dist/src/store/sqlite/perf-samples.js +144 -16
- package/dist/src/store/turns/memory-store.js +8 -0
- package/dist/src/store/turns/sqlite-store.js +14 -3
- package/dist/src/store/turns/types.js +18 -1
- package/dist/src/store/vectorIndex.js +26 -5
- package/extensions/dashboard-client/dist/assets/{AchievementTiles-vEo2MtE2.js → AchievementTiles-BoL7qBOJ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AchievementTiles-vEo2MtE2.js.map → AchievementTiles-BoL7qBOJ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/AchievementsTab-BFDE3HYc.js +2 -0
- package/extensions/dashboard-client/dist/assets/{AchievementsTab-DjxfYrDc.js.map → AchievementsTab-BFDE3HYc.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/CacheTab-eKVc-NV1.js +2 -0
- package/extensions/dashboard-client/dist/assets/CacheTab-eKVc-NV1.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{ConfigTab-Cm6ymbmx.js → ConfigTab-DDFE-ckA.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ConfigTab-Cm6ymbmx.js.map → ConfigTab-DDFE-ckA.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-DXWl_YzI.js → EventsTab-B6UxZ7PM.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-DXWl_YzI.js.map → EventsTab-B6UxZ7PM.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{GameTab-BLDudcO5.js → GameTab-DehUX-KE.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{GameTab-BLDudcO5.js.map → GameTab-DehUX-KE.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-DLkKCztn.js → MaintenanceTab-0GWRt4wn.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-DLkKCztn.js.map → MaintenanceTab-0GWRt4wn.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-Cq4u-rZF.js → MetricsTab-C3rwMVsN.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-Cq4u-rZF.js.map → MetricsTab-C3rwMVsN.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/OverviewTab-39O9_x5j.js +2 -0
- package/extensions/dashboard-client/dist/assets/{OverviewTab-D4kGvsiQ.js.map → OverviewTab-39O9_x5j.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/ReposTab-Cfj6Q5sH.js +2 -0
- package/extensions/dashboard-client/dist/assets/ReposTab-Cfj6Q5sH.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{SessionsTab-DZC2-GCn.js → SessionsTab-DXDTCi3r.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-DZC2-GCn.js.map → SessionsTab-DXDTCi3r.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/SetupTab-mMlfunJa.js +2 -0
- package/extensions/dashboard-client/dist/assets/SetupTab-mMlfunJa.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/TimeSavedCard-Cw1dJ4e9.js +2 -0
- package/extensions/dashboard-client/dist/assets/TimeSavedCard-Cw1dJ4e9.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{TopicsTab-01G71hv1.js → TopicsTab-BAw972BV.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TopicsTab-01G71hv1.js.map → TopicsTab-BAw972BV.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-DLRg0LZT.js → TurnsTab-Cf5mGIdl.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-DLRg0LZT.js.map → TurnsTab-Cf5mGIdl.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/format-COjDIVGj.js +2 -0
- package/extensions/dashboard-client/dist/assets/format-COjDIVGj.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{index-BX0bcgLu.js → index-4gQ7auad.js} +10 -10
- package/extensions/dashboard-client/dist/assets/index-4gQ7auad.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/pricing-CvE4NllQ.js +2 -0
- package/extensions/dashboard-client/dist/assets/pricing-CvE4NllQ.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{useSSE-BBYt5UPB.js → useSSE-DNgMuyP6.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-BBYt5UPB.js.map → useSSE-DNgMuyP6.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +1 -1
- package/extensions/dashboard-client/src/App.tsx +4 -0
- package/extensions/dashboard-client/src/api/client.ts +14 -0
- package/extensions/dashboard-client/src/components/ActiveReposTable.tsx +21 -12
- package/extensions/dashboard-client/src/components/ProviderCacheCard.tsx +56 -0
- package/extensions/dashboard-client/src/components/SavingsByModelTable.tsx +38 -57
- package/extensions/dashboard-client/src/tabs/CacheTab.tsx +1 -0
- package/extensions/dashboard-client/src/tabs/SetupTab.tsx +355 -0
- package/extensions/dashboard-client/tsconfig.json +3 -2
- package/extensions/dashboard-client/vite.config.ts +2 -0
- package/extensions/dashboard-server/api-contracts/endpoints.ts +36 -3
- package/extensions/dashboard-server/api-contracts/index.ts +7 -0
- package/extensions/dashboard-server/api-contracts/memory.ts +27 -0
- package/extensions/dashboard-server/api-contracts/multi-repo.ts +231 -231
- package/extensions/dashboard-server/api-contracts/provider-cache.ts +37 -0
- package/extensions/dashboard-server/api-contracts/setup.ts +58 -0
- package/extensions/dashboard-server/index-reader.ts +138 -121
- package/extensions/dashboard-server/routes-cache.ts +161 -85
- package/extensions/dashboard-server/routes-memory.ts +63 -0
- package/extensions/dashboard-server/routes-repo.ts +36 -36
- package/extensions/dashboard-server/routes-setup.ts +181 -0
- package/extensions/dashboard-server/routes.ts +2 -0
- package/extensions/dashboard-server/server.ts +6 -0
- package/extensions/mega-commands.ts +211 -1
- package/extensions/mega-config.ts +18 -0
- package/extensions/mega-events/agent-handlers.ts +44 -5
- package/extensions/mega-events/compact-handlers.ts +1 -0
- package/extensions/mega-events/context-handler.ts +45 -110
- package/extensions/mega-events/context-hwm.ts +85 -0
- package/extensions/mega-events/error-classifier.ts +156 -124
- package/extensions/mega-events/mirror-append.ts +155 -0
- package/extensions/mega-events/outage-advisor.ts +79 -0
- package/extensions/mega-events/perf-handler.ts +79 -5
- package/extensions/mega-events/recall-tail.ts +88 -0
- package/extensions/mega-events/session-handlers.ts +14 -7
- package/extensions/mega-runtime/helpers.ts +14 -0
- package/extensions/mega-runtime/reset-runtime.ts +6 -0
- package/extensions/mega-runtime/runtime.ts +7 -0
- package/package.json +5 -2
- package/src/embedder.ts +42 -2
- package/src/memoryStats.ts +107 -0
- package/src/prefix-break.ts +83 -0
- package/src/pricing.ts +2 -2
- package/src/store/sqlite/perf-samples.ts +215 -22
- package/src/store/turns/memory-store.ts +10 -1
- package/src/store/turns/sqlite-store.ts +27 -15
- package/src/store/turns/types.ts +21 -0
- package/src/store/vectorIndex.ts +33 -4
- package/dist/extensions/conflict-scan.test.js +0 -115
- package/dist/extensions/dashboard-server/api-contracts.test.js +0 -884
- package/dist/extensions/dashboard-server/html.test.js +0 -52
- package/dist/extensions/dashboard-server/perf-server.test.js +0 -80
- package/dist/extensions/dashboard-server/routes-cache.test.js +0 -248
- package/dist/extensions/dashboard-server/server.test.js +0 -120
- package/dist/extensions/dashboard-server-s32.test.js +0 -181
- package/dist/extensions/dashboard-server.test.js +0 -339
- package/dist/extensions/mega-cache-replay.test.js +0 -400
- package/dist/extensions/mega-compact-s38.test.js +0 -820
- package/dist/extensions/mega-compact.test.js +0 -1112
- package/dist/extensions/mega-events.test.js +0 -47
- package/dist/extensions/mega-game-cmds.test.js +0 -163
- package/dist/extensions/mega-runtime/snapshot.test.js +0 -127
- package/dist/extensions/mega-runtime/state.test.js +0 -266
- package/dist/extensions/mega-runtime/widget.test.js +0 -343
- package/dist/extensions/mega-shutdown-widget.test.js +0 -121
- package/dist/extensions/mega-teamrun.test.js +0 -156
- package/dist/extensions/mega-trim.test.js +0 -58
- package/dist/extensions/mega-turn-store.test.js +0 -68
- package/dist/src/boundary.test.js +0 -170
- package/dist/src/compact.test.js +0 -78
- package/dist/src/config/themes.test.js +0 -94
- package/dist/src/dedup/dedup.test.js +0 -41
- package/dist/src/dedup/l1.test.js +0 -50
- package/dist/src/dedup/raptor/buildHistory.test.js +0 -292
- package/dist/src/dedup/raptor/multilevel-serve.test.js +0 -229
- package/dist/src/dedup/raptor/multilevel.test.js +0 -238
- package/dist/src/dedup/raptor/promote.test.js +0 -69
- package/dist/src/dedup/raptor/raptor.test.js +0 -248
- package/dist/src/dedup/raptor/retrieval.test.js +0 -95
- package/dist/src/dedup/raptor/serve-gate.test.js +0 -298
- package/dist/src/dedup/sprint12.test.js +0 -219
- package/dist/src/dedup-engine.test.js +0 -472
- package/dist/src/driftDetection.test.js +0 -87
- package/dist/src/e2e.test.js +0 -698
- package/dist/src/engine.test.js +0 -111
- package/dist/src/extractive.test.js +0 -147
- package/dist/src/fork.test.js +0 -81
- package/dist/src/game/scoring.test.js +0 -98
- package/dist/src/httpEmbedder.test.js +0 -277
- package/dist/src/importance.test.js +0 -372
- package/dist/src/intent.test.js +0 -78
- package/dist/src/log.test.js +0 -42
- package/dist/src/mechanical-fix.test.js +0 -65
- package/dist/src/memory.test.js +0 -70
- package/dist/src/memoryConsolidate.test.js +0 -38
- package/dist/src/memoryOps.test.js +0 -129
- package/dist/src/memoryRecall.test.js +0 -171
- package/dist/src/memoryRoundtrip.test.js +0 -137
- package/dist/src/metrics/turns.test.js +0 -152
- package/dist/src/mirror/mirror.test.js +0 -185
- package/dist/src/raptor-inject-summaries.test.js +0 -162
- package/dist/src/ratio.bench.test.js +0 -897
- package/dist/src/recall.integration.test.js +0 -77
- package/dist/src/recall.test.js +0 -379
- package/dist/src/sprint14.test.js +0 -219
- package/dist/src/sprint4x-rag-verification.test.js +0 -93
- package/dist/src/store/compression.test.js +0 -139
- package/dist/src/store/memoryIndex.test.js +0 -51
- package/dist/src/store/migrate.test.js +0 -139
- package/dist/src/store/pgOpenGuard.test.js +0 -74
- package/dist/src/store/phase01.test.js +0 -81
- package/dist/src/store/phase04.test.js +0 -54
- package/dist/src/store/sprint10.test.js +0 -186
- package/dist/src/store/sqlite/game-achievements.test.js +0 -67
- package/dist/src/store/sqlite/game-scores.test.js +0 -106
- package/dist/src/store/sqlite/game-state.test.js +0 -76
- package/dist/src/store/sqlite/global-index.test.js +0 -136
- package/dist/src/store/sqlite/mechanical-fix.test.js +0 -146
- package/dist/src/store/sqlite/perf-samples.test.js +0 -146
- package/dist/src/store/sqlite/raptor.test.js +0 -106
- package/dist/src/store/sqlite/turns.test.js +0 -183
- package/dist/src/store/sqlite.cachehit.test.js +0 -55
- package/dist/src/store/sqlite.dbmaint.test.js +0 -158
- package/dist/src/store/sqlite.dbmirror.test.js +0 -184
- package/dist/src/store/turns/contract-compliance.test.js +0 -428
- package/dist/src/store/turns/memory-store.test.js +0 -9
- package/dist/src/store/turns/migrations.test.js +0 -138
- package/dist/src/store/turns/sqlite-store.test.js +0 -144
- package/dist/src/store/vectorIndex.test.js +0 -123
- package/dist/src/store.test.js +0 -161
- package/dist/src/supersede.test.js +0 -36
- package/dist/src/topics/cluster.test.js +0 -150
- package/dist/src/topics/store.test.js +0 -117
- package/dist/src/types.test.js +0 -9
- package/dist/src/vector-search-cache.test.js +0 -157
- package/dist/src/vectorStore.test.js +0 -479
- package/dist/src/wiki.test.js +0 -171
- package/extensions/conflict-scan.test.ts +0 -129
- package/extensions/dashboard-client/dist/assets/AchievementsTab-DjxfYrDc.js +0 -2
- package/extensions/dashboard-client/dist/assets/CacheTab-Dnpv94wq.js +0 -2
- package/extensions/dashboard-client/dist/assets/CacheTab-Dnpv94wq.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/OverviewTab-D4kGvsiQ.js +0 -2
- package/extensions/dashboard-client/dist/assets/ReposTab-C0Wnrf2u.js +0 -2
- package/extensions/dashboard-client/dist/assets/ReposTab-C0Wnrf2u.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/TimeSavedCard-UqljSyP-.js +0 -2
- package/extensions/dashboard-client/dist/assets/TimeSavedCard-UqljSyP-.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/index-BX0bcgLu.js.map +0 -1
- package/extensions/dashboard-server/api-contracts.test.ts +0 -1065
- package/extensions/dashboard-server/html.test.ts +0 -62
- package/extensions/dashboard-server/perf-server.test.ts +0 -101
- package/extensions/dashboard-server/routes-cache.test.ts +0 -302
- package/extensions/dashboard-server/server.test.ts +0 -131
- package/extensions/dashboard-server-s32.test.ts +0 -195
- package/extensions/dashboard-server.test.ts +0 -382
- package/extensions/mega-cache-replay.test.ts +0 -526
- package/extensions/mega-compact-s38.test.ts +0 -872
- package/extensions/mega-compact.test.ts +0 -1509
- package/extensions/mega-events.test.ts +0 -55
- package/extensions/mega-game-cmds.test.ts +0 -197
- package/extensions/mega-runtime/snapshot.test.ts +0 -139
- package/extensions/mega-runtime/state.test.ts +0 -262
- package/extensions/mega-runtime/widget.test.ts +0 -468
- package/extensions/mega-shutdown-widget.test.ts +0 -141
- package/extensions/mega-teamrun.test.ts +0 -176
- package/extensions/mega-trim.test.ts +0 -64
- package/extensions/mega-turn-store.test.ts +0 -108
- package/src/boundary.test.ts +0 -187
- package/src/compact.test.ts +0 -99
- package/src/config/themes.test.ts +0 -116
- package/src/dedup/dedup.test.ts +0 -46
- package/src/dedup/l1.test.ts +0 -57
- package/src/dedup/raptor/buildHistory.test.ts +0 -353
- package/src/dedup/raptor/multilevel-serve.test.ts +0 -273
- package/src/dedup/raptor/multilevel.test.ts +0 -325
- package/src/dedup/raptor/promote.test.ts +0 -82
- package/src/dedup/raptor/raptor.test.ts +0 -297
- package/src/dedup/raptor/retrieval.test.ts +0 -118
- package/src/dedup/raptor/serve-gate.test.ts +0 -348
- package/src/dedup/sprint12.test.ts +0 -242
- package/src/dedup-engine.test.ts +0 -670
- package/src/driftDetection.test.ts +0 -100
- package/src/e2e.test.ts +0 -843
- package/src/engine.test.ts +0 -123
- package/src/extractive.test.ts +0 -174
- package/src/fork.test.ts +0 -94
- package/src/game/scoring.test.ts +0 -103
- package/src/httpEmbedder.test.ts +0 -286
- package/src/importance.test.ts +0 -538
- package/src/intent.test.ts +0 -87
- package/src/log.test.ts +0 -47
- package/src/mechanical-fix.test.ts +0 -70
- package/src/memory.test.ts +0 -92
- package/src/memoryConsolidate.test.ts +0 -47
- package/src/memoryOps.test.ts +0 -148
- package/src/memoryRecall.test.ts +0 -194
- package/src/memoryRoundtrip.test.ts +0 -155
- package/src/metrics/turns.test.ts +0 -165
- package/src/mirror/mirror.test.ts +0 -240
- package/src/raptor-inject-summaries.test.ts +0 -228
- package/src/ratio.bench.test.ts +0 -1316
- package/src/recall.integration.test.ts +0 -96
- package/src/recall.test.ts +0 -467
- package/src/sprint14.test.ts +0 -245
- package/src/sprint4x-rag-verification.test.ts +0 -119
- package/src/store/compression.test.ts +0 -168
- package/src/store/memoryIndex.test.ts +0 -61
- package/src/store/migrate.test.ts +0 -158
- package/src/store/pgOpenGuard.test.ts +0 -89
- package/src/store/phase01.test.ts +0 -88
- package/src/store/phase04.test.ts +0 -58
- package/src/store/sprint10.test.ts +0 -214
- package/src/store/sqlite/game-achievements.test.ts +0 -80
- package/src/store/sqlite/game-scores.test.ts +0 -132
- package/src/store/sqlite/game-state.test.ts +0 -89
- package/src/store/sqlite/global-index.test.ts +0 -222
- package/src/store/sqlite/mechanical-fix.test.ts +0 -162
- package/src/store/sqlite/perf-samples.test.ts +0 -169
- package/src/store/sqlite/raptor.test.ts +0 -139
- package/src/store/sqlite/turns.test.ts +0 -218
- package/src/store/sqlite.cachehit.test.ts +0 -68
- package/src/store/sqlite.dbmaint.test.ts +0 -193
- package/src/store/sqlite.dbmirror.test.ts +0 -229
- package/src/store/turns/contract-compliance.test.ts +0 -562
- package/src/store/turns/memory-store.test.ts +0 -15
- package/src/store/turns/migrations.test.ts +0 -161
- package/src/store/turns/sqlite-store.test.ts +0 -161
- package/src/store/vectorIndex.test.ts +0 -140
- package/src/store.test.ts +0 -191
- package/src/supersede.test.ts +0 -42
- package/src/topics/cluster.test.ts +0 -176
- package/src/topics/store.test.ts +0 -132
- package/src/types.test.ts +0 -10
- package/src/vector-search-cache.test.ts +0 -190
- package/src/vectorStore.test.ts +0 -533
- package/src/wiki.test.ts +0 -217
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* multilevel-serve.test.ts — S42B multi-level RAPTOR serve acceptance tests.
|
|
3
|
-
*
|
|
4
|
-
* Verifies the raptorSearchHits multilevel wiring (RAPTOR_MULTILEVEL_ENABLED):
|
|
5
|
-
* (a) flag ON + leafExpansion OFF → search surfaces cluster hits carrying
|
|
6
|
-
* raptorSummary + raptorLevel (clusters survive dedup; on a 2-topic
|
|
7
|
-
* fixture a cluster's aggregate theme wins over its individual leaves
|
|
8
|
-
* when k is smaller than the cluster's leaf set)
|
|
9
|
-
* (b) flag OFF → search results never carry cluster markers (leaf-only path)
|
|
10
|
-
* (c) shadow mode gates the multilevel merge too (no RAPTOR hits regardless)
|
|
11
|
-
* (d) formatRecallBlock labels a raptorLevel hit as "cluster summary"
|
|
12
|
-
*
|
|
13
|
-
* No network — default extractive summarizer + trigram embedder.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { test, beforeEach, afterEach } from "node:test";
|
|
17
|
-
import assert from "node:assert/strict";
|
|
18
|
-
import { mkdtempSync, rmSync } from "node:fs";
|
|
19
|
-
import { tmpdir } from "node:os";
|
|
20
|
-
import { join } from "node:path";
|
|
21
|
-
import { VectorStore, vectorList, vectorSearch } from "../../vectorStore.js";
|
|
22
|
-
import { runRaptor, isShadowMode } from "./index.js";
|
|
23
|
-
import { compactSession } from "../../engine.js";
|
|
24
|
-
import { Logger } from "../../log.js";
|
|
25
|
-
import { loadDedupConfig } from "../../config/dedup.js";
|
|
26
|
-
import { formatRecallBlock } from "../../recall.js";
|
|
27
|
-
import { normalizeSessionId } from "../../store.js";
|
|
28
|
-
import { multilevelRetrieval } from "./multilevel.js";
|
|
29
|
-
import type { EngineMessage } from "../../types.js";
|
|
30
|
-
import type { DedupConfigShape } from "../../config/dedup.js";
|
|
31
|
-
|
|
32
|
-
/* ------------------------------------------------------------------ helpers */
|
|
33
|
-
|
|
34
|
-
let tmpDir: string;
|
|
35
|
-
let counter = 0;
|
|
36
|
-
|
|
37
|
-
beforeEach(() => {
|
|
38
|
-
tmpDir = mkdtempSync(join(tmpdir(), "mc-ml-"));
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
afterEach(() => {
|
|
42
|
-
rmSync(tmpDir, { recursive: true, force: true });
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
function stateDir(): string {
|
|
46
|
-
return join(tmpDir, `run-${counter++}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Config with dedup tiers disabled (distinct checkpoints) + live RAPTOR. */
|
|
50
|
-
function cfg(overrides?: Record<string, unknown>): DedupConfigShape {
|
|
51
|
-
return {
|
|
52
|
-
...loadDedupConfig(),
|
|
53
|
-
RAPTOR_ENABLED: true,
|
|
54
|
-
L0_ENABLED: false,
|
|
55
|
-
L1_ENABLED: false,
|
|
56
|
-
L2_ENABLED: false,
|
|
57
|
-
...overrides,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function msg(text: string, toolName?: string): EngineMessage {
|
|
62
|
-
return toolName
|
|
63
|
-
? { role: "assistant", text, toolName, input: text, output: text }
|
|
64
|
-
: { role: "user", text };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function seedSession(
|
|
68
|
-
store: VectorStore,
|
|
69
|
-
sid: string,
|
|
70
|
-
count: number,
|
|
71
|
-
topic: string,
|
|
72
|
-
startTs = 1,
|
|
73
|
-
) {
|
|
74
|
-
for (let i = 1; i <= count; i++) {
|
|
75
|
-
compactSession(
|
|
76
|
-
{
|
|
77
|
-
sessionId: sid,
|
|
78
|
-
messages: [
|
|
79
|
-
msg(`${topic} checkpoint ${i} with unique content alpha beta`),
|
|
80
|
-
msg(`acknowledged ${i}`, "Edit"),
|
|
81
|
-
],
|
|
82
|
-
keepFrom: 2,
|
|
83
|
-
timestamp: startTs + i,
|
|
84
|
-
},
|
|
85
|
-
store,
|
|
86
|
-
);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function buildTree(store: VectorStore, sid: string) {
|
|
91
|
-
const nsid = normalizeSessionId(sid);
|
|
92
|
-
const all = vectorList(store, nsid);
|
|
93
|
-
const leaves = all.map((cp) => ({
|
|
94
|
-
id: cp.checkpointId,
|
|
95
|
-
messages: [],
|
|
96
|
-
sourceText: cp.normalizedText ?? cp.summary ?? cp.regionHash,
|
|
97
|
-
embedding: cp.embedding,
|
|
98
|
-
}));
|
|
99
|
-
return runRaptor(leaves, {
|
|
100
|
-
stateDir: store.stateDir,
|
|
101
|
-
sessionId: nsid,
|
|
102
|
-
logger: new Logger(),
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function withEnv(env: Record<string, string | undefined>, fn: () => void) {
|
|
107
|
-
const saved: Record<string, string | undefined> = {};
|
|
108
|
-
for (const k of Object.keys(env)) saved[k] = process.env[k];
|
|
109
|
-
for (const [k, v] of Object.entries(env)) {
|
|
110
|
-
if (v === undefined) delete process.env[k];
|
|
111
|
-
else process.env[k] = v;
|
|
112
|
-
}
|
|
113
|
-
try {
|
|
114
|
-
fn();
|
|
115
|
-
} finally {
|
|
116
|
-
for (const [k, v] of Object.entries(saved)) {
|
|
117
|
-
if (v === undefined) delete process.env[k];
|
|
118
|
-
else process.env[k] = v;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/* ------------------------------------------------------------------- tests */
|
|
124
|
-
|
|
125
|
-
// ─── (a) multilevelRetrieval surfaces cluster hits on a controlled fixture ──
|
|
126
|
-
//
|
|
127
|
-
// The full vectorSearch path applies MMR + dedup that, on small trigram
|
|
128
|
-
// fixtures, tends to surface leaves over clusters (a cluster's centroid is at
|
|
129
|
-
// best as similar as its best leaf, and the level weight makes it strictly
|
|
130
|
-
// less). The S42B *wiring* is best asserted at the retrieval layer where the
|
|
131
|
-
// cluster-vs-leaf outcome is deterministic: a hand-built 2-cluster tree with a
|
|
132
|
-
// clean query makes the matching cluster win.
|
|
133
|
-
|
|
134
|
-
test("S42B(a): multilevelRetrieval surfaces cluster hits carrying summary+level", () => {
|
|
135
|
-
// Build via the real pipeline so the tree shape is realistic, then query
|
|
136
|
-
// multilevelRetrieval directly with leafExpansion OFF so clusters survive.
|
|
137
|
-
const sd = stateDir();
|
|
138
|
-
const s = new VectorStore({
|
|
139
|
-
dedupSim: 0.9,
|
|
140
|
-
stateDir: sd,
|
|
141
|
-
config: cfg({ RAPTOR_LEAF_EXPANSION: false }),
|
|
142
|
-
});
|
|
143
|
-
const sid = "ml-direct";
|
|
144
|
-
seedSession(s, sid, 12, "multilevel topic");
|
|
145
|
-
buildTree(s, sid);
|
|
146
|
-
|
|
147
|
-
const nsid = normalizeSessionId(sid);
|
|
148
|
-
// Rehydrate and call multilevelRetrieval with k=1 and aggressive level
|
|
149
|
-
// weights so a cluster can win. This proves the SearchHit synthesis path
|
|
150
|
-
// (cluster node → raptorSummary/raptorLevel) is reachable.
|
|
151
|
-
const tree = runRaptor(
|
|
152
|
-
vectorList(s, nsid).map((cp) => ({
|
|
153
|
-
id: cp.checkpointId,
|
|
154
|
-
messages: [],
|
|
155
|
-
sourceText: cp.normalizedText ?? cp.summary ?? cp.regionHash,
|
|
156
|
-
embedding: cp.embedding,
|
|
157
|
-
})),
|
|
158
|
-
{ stateDir: sd, sessionId: nsid, logger: new Logger() },
|
|
159
|
-
);
|
|
160
|
-
assert.ok(tree, "tree built");
|
|
161
|
-
// When leafExpansion is OFF, deduplicateMultilevelHits keeps clusters whose
|
|
162
|
-
// leaves are NOT in the (small) result set. With a large fixture and k=2,
|
|
163
|
-
// at least one internal node must survive — assert that the multilevel path
|
|
164
|
-
// returns >=1 non-leaf hit under these conditions.
|
|
165
|
-
const ml = multilevelRetrieval("multilevel topic alpha", tree!, {
|
|
166
|
-
embedder: s.embedder,
|
|
167
|
-
levelWeights: [1.0, 0.99, 0.98, 0.97, 0.96], // near-flat so clusters compete
|
|
168
|
-
leafExpansion: false,
|
|
169
|
-
maxLeafExpansion: 1,
|
|
170
|
-
k: 2,
|
|
171
|
-
mmrLambda: 0.99, // near-greedy so top-scored items survive
|
|
172
|
-
});
|
|
173
|
-
// S42B contract: the multilevel path CAN return non-leaf hits, and when it
|
|
174
|
-
// does, they carry summary + level. If the fixture's trigram similarity
|
|
175
|
-
// diffs make every leaf outscore every cluster even at near-flat weights,
|
|
176
|
-
// the wiring is still proven by S42B(b)/(c)/(d) below — but we assert that
|
|
177
|
-
// the returned hit set is well-formed either way.
|
|
178
|
-
for (const h of ml) {
|
|
179
|
-
assert.equal(typeof h.summary, "string", "hit carries summary");
|
|
180
|
-
assert.equal(typeof h.level, "number", "hit carries level");
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
// ─── (b) flag OFF → leaf-only path, no cluster markers ──────────────────────
|
|
185
|
-
|
|
186
|
-
test("S42B(b): RAPTOR_MULTILEVEL_ENABLED=false → no cluster hits (leaf-only path)", () => {
|
|
187
|
-
const sd = stateDir();
|
|
188
|
-
const s = new VectorStore({
|
|
189
|
-
dedupSim: 0.9,
|
|
190
|
-
stateDir: sd,
|
|
191
|
-
config: cfg({ RAPTOR_MULTILEVEL_ENABLED: false }),
|
|
192
|
-
});
|
|
193
|
-
const sid = "ml-off";
|
|
194
|
-
seedSession(s, sid, 12, "leaftopic");
|
|
195
|
-
buildTree(s, sid);
|
|
196
|
-
|
|
197
|
-
const origShadow = process.env.RAPTOR_SHADOW_MODE;
|
|
198
|
-
process.env.RAPTOR_SHADOW_MODE = "false";
|
|
199
|
-
try {
|
|
200
|
-
const hits = vectorSearch(s, sid, "leaftopic alpha", 8);
|
|
201
|
-
assert.ok(hits.length > 0, "leaf search returns hits");
|
|
202
|
-
const clusterHits = hits.filter((h) => h.raptorLevel !== undefined);
|
|
203
|
-
assert.equal(clusterHits.length, 0, "no cluster hits when flag OFF");
|
|
204
|
-
} finally {
|
|
205
|
-
if (origShadow === undefined) delete process.env.RAPTOR_SHADOW_MODE;
|
|
206
|
-
else process.env.RAPTOR_SHADOW_MODE = origShadow;
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
// ─── (c) shadow mode gates the multilevel merge ─────────────────────────────
|
|
211
|
-
|
|
212
|
-
test("S42B(c): shadow mode → no RAPTOR merge regardless of multilevel flag", () => {
|
|
213
|
-
const sd = stateDir();
|
|
214
|
-
const s = new VectorStore({ dedupSim: 0.9, stateDir: sd, config: cfg() });
|
|
215
|
-
const sid = "ml-shadow";
|
|
216
|
-
seedSession(s, sid, 12, "shadowtopic");
|
|
217
|
-
buildTree(s, sid);
|
|
218
|
-
|
|
219
|
-
withEnv(
|
|
220
|
-
{ RAPTOR_SHADOW_MODE: "true", RAPTOR_MULTILEVEL_ENABLED: "true" },
|
|
221
|
-
() => {
|
|
222
|
-
assert.ok(isShadowMode(), "shadow mode active");
|
|
223
|
-
const hits = vectorSearch(s, sid, "shadowtopic alpha", 8);
|
|
224
|
-
// Shadow mode returns flat hits only — no cluster markers.
|
|
225
|
-
const clusterHits = hits.filter((h) => h.raptorLevel !== undefined);
|
|
226
|
-
assert.equal(
|
|
227
|
-
clusterHits.length,
|
|
228
|
-
0,
|
|
229
|
-
"shadow mode suppresses multilevel merge",
|
|
230
|
-
);
|
|
231
|
-
},
|
|
232
|
-
);
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
// ─── (d) formatRecallBlock labels cluster hits as "cluster summary" ─────────
|
|
236
|
-
|
|
237
|
-
test("S42B(d): formatRecallBlock labels a raptorLevel hit as cluster summary", () => {
|
|
238
|
-
// Directly exercise the formatter with a synthetic cluster hit. The wiring
|
|
239
|
-
// (raptorSummary/raptorLevel → "cluster summary" label, raptorSummary body,
|
|
240
|
-
// no Key files line) is a pure formatter contract, independent of whether
|
|
241
|
-
// a cluster won the MMR race in (a).
|
|
242
|
-
const block = formatRecallBlock([
|
|
243
|
-
{
|
|
244
|
-
checkpoint: {
|
|
245
|
-
checkpointId: "r1_0",
|
|
246
|
-
sessionId: "sess_fmt",
|
|
247
|
-
summary: "fallback body",
|
|
248
|
-
keyDecisions: [],
|
|
249
|
-
nextSteps: [],
|
|
250
|
-
filesModified: ["src/x.ts"], // present but must NOT be rendered for cluster hits
|
|
251
|
-
tokenEstimate: 0,
|
|
252
|
-
regionHash: "x",
|
|
253
|
-
embedding: [],
|
|
254
|
-
timestamp: 0,
|
|
255
|
-
},
|
|
256
|
-
score: 0.8,
|
|
257
|
-
raptorSummary: "hierarchical cluster summary text",
|
|
258
|
-
raptorLevel: 1,
|
|
259
|
-
},
|
|
260
|
-
]);
|
|
261
|
-
assert.ok(
|
|
262
|
-
/Recalled cluster summary \[1\] \(level 1, relevance 80%\)/.test(block),
|
|
263
|
-
"cluster hit labeled with level: " + block.split("\n")[0],
|
|
264
|
-
);
|
|
265
|
-
assert.ok(
|
|
266
|
-
block.includes("hierarchical cluster summary text"),
|
|
267
|
-
"cluster body uses raptorSummary",
|
|
268
|
-
);
|
|
269
|
-
assert.ok(
|
|
270
|
-
!/Key files:/.test(block),
|
|
271
|
-
"cluster hit has no Key files line even when filesModified is non-empty",
|
|
272
|
-
);
|
|
273
|
-
});
|
|
@@ -1,325 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* multilevel.test.ts — hermetic unit tests for S42A multi-level RAPTOR retrieval.
|
|
3
|
-
*
|
|
4
|
-
* Tests the scoreTreeLevels → expandLeafDescendants → deduplicateMultilevelHits
|
|
5
|
-
* → multilevelRetrieval pipeline. No network, no live store — uses TrigramEmbedder
|
|
6
|
-
* and synthetic RaptorTrees built from makeLeaves().
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { test } from "node:test";
|
|
10
|
-
import assert from "node:assert/strict";
|
|
11
|
-
import { TrigramEmbedder } from "../../embedder.js";
|
|
12
|
-
import { buildRaptorTree, type Leaf } from "./tree.js";
|
|
13
|
-
import {
|
|
14
|
-
scoreTreeLevels,
|
|
15
|
-
expandLeafDescendants,
|
|
16
|
-
deduplicateMultilevelHits,
|
|
17
|
-
multilevelRetrieval,
|
|
18
|
-
buildChildParentIndex,
|
|
19
|
-
type MultilevelHit,
|
|
20
|
-
} from "./multilevel.js";
|
|
21
|
-
import type { RaptorTree, RaptorNode } from "./tree.js";
|
|
22
|
-
import type { EngineMessage } from "../../types.js";
|
|
23
|
-
|
|
24
|
-
function msg(text: string): EngineMessage {
|
|
25
|
-
return { role: "user", text };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Build N distinct leaves with deterministic content. */
|
|
29
|
-
function makeLeaves(n: number, embedder = new TrigramEmbedder()): Leaf[] {
|
|
30
|
-
const leaves: Leaf[] = [];
|
|
31
|
-
for (let i = 0; i < n; i++) {
|
|
32
|
-
const text = `topic ${i % 7}: the module ${i} validated the session token and refreshed the cache for region ${i}`;
|
|
33
|
-
leaves.push({
|
|
34
|
-
id: `leaf_${i}`,
|
|
35
|
-
messages: [msg(text)],
|
|
36
|
-
sourceText: text,
|
|
37
|
-
embedding: embedder.embed(text),
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
return leaves;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// ── test: scoreTreeLevels returns nodes at all levels ────────────────────────
|
|
44
|
-
|
|
45
|
-
test("scoreTreeLevels returns results at multiple tree levels", () => {
|
|
46
|
-
const embedder = new TrigramEmbedder();
|
|
47
|
-
const leaves = makeLeaves(50);
|
|
48
|
-
const tree = buildRaptorTree(leaves, { embedder, clustersPerLevel: 4 });
|
|
49
|
-
|
|
50
|
-
const hits = scoreTreeLevels("the auth module validates the session token", tree, {
|
|
51
|
-
embedder,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
assert.ok(hits.length > 0, "should return hits");
|
|
55
|
-
|
|
56
|
-
// Should include both leaf (level 0) and cluster (level ≥ 1) hits.
|
|
57
|
-
const levels = new Set(hits.map((h) => h.level));
|
|
58
|
-
assert.ok(levels.has(0), "should have leaf-level hits");
|
|
59
|
-
if (tree.levels > 1) {
|
|
60
|
-
const hasCluster = [...levels].some((l) => l >= 1);
|
|
61
|
-
assert.ok(hasCluster, "should have cluster-level hits for multi-level tree");
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// All hits should have valid scores.
|
|
65
|
-
for (const h of hits) {
|
|
66
|
-
assert.ok(h.score >= 0 && h.score <= 1, `score ${h.score} out of range`);
|
|
67
|
-
assert.ok(h.rawScore >= 0 && h.rawScore <= 1, `rawScore ${h.rawScore} out of range`);
|
|
68
|
-
assert.ok(h.score <= h.rawScore, "weighted score <= raw score (level weights ≤ 1)");
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Hits should be sorted by score descending.
|
|
72
|
-
for (let i = 1; i < hits.length; i++) {
|
|
73
|
-
assert.ok(
|
|
74
|
-
hits[i - 1].score >= hits[i].score,
|
|
75
|
-
`hits not sorted: hit[${i - 1}].score=${hits[i - 1].score} < hit[${i}].score=${hits[i].score}`,
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// ── test: level weights affect scoring ───────────────────────────────────────
|
|
81
|
-
|
|
82
|
-
test("level weights shift scores: higher weight for level → higher weighted score", () => {
|
|
83
|
-
const embedder = new TrigramEmbedder();
|
|
84
|
-
const leaves = makeLeaves(50);
|
|
85
|
-
const tree = buildRaptorTree(leaves, { embedder, clustersPerLevel: 4 });
|
|
86
|
-
|
|
87
|
-
const query = "the auth module validates the session token";
|
|
88
|
-
|
|
89
|
-
// Uniform weights: all levels scored equally.
|
|
90
|
-
const uniform = scoreTreeLevels(query, tree, {
|
|
91
|
-
embedder,
|
|
92
|
-
levelWeights: [1.0, 1.0, 1.0, 1.0, 1.0],
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// Penalized weights: higher levels penalized.
|
|
96
|
-
const penalized = scoreTreeLevels(query, tree, {
|
|
97
|
-
embedder,
|
|
98
|
-
levelWeights: [1.0, 0.1, 0.1, 0.1, 0.1],
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
// With penalized weights, cluster-level hits should score lower.
|
|
102
|
-
const clusterUniform = uniform.filter((h) => h.level >= 1);
|
|
103
|
-
const clusterPenalized = penalized.filter((h) => h.level >= 1);
|
|
104
|
-
|
|
105
|
-
if (clusterUniform.length > 0 && clusterPenalized.length > 0) {
|
|
106
|
-
const avgUniform =
|
|
107
|
-
clusterUniform.reduce((s, h) => s + h.score, 0) / clusterUniform.length;
|
|
108
|
-
const avgPenalized =
|
|
109
|
-
clusterPenalized.reduce((s, h) => s + h.score, 0) / clusterPenalized.length;
|
|
110
|
-
assert.ok(
|
|
111
|
-
avgPenalized < avgUniform,
|
|
112
|
-
`penalized cluster avg (${avgPenalized.toFixed(3)}) should be < uniform (${avgUniform.toFixed(3)})`,
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
// ── test: expandLeafDescendants adds leaf hits ──────────────────────────────
|
|
118
|
-
|
|
119
|
-
test("expandLeafDescendants adds leaf descendants for cluster hits", () => {
|
|
120
|
-
const embedder = new TrigramEmbedder();
|
|
121
|
-
const leaves = makeLeaves(50);
|
|
122
|
-
const tree = buildRaptorTree(leaves, { embedder, clustersPerLevel: 4 });
|
|
123
|
-
|
|
124
|
-
const query = "the auth module validates the session token";
|
|
125
|
-
const scored = scoreTreeLevels(query, tree, { embedder });
|
|
126
|
-
const qv = embedder.embed(query);
|
|
127
|
-
|
|
128
|
-
// Take only cluster hits (level ≥ 1).
|
|
129
|
-
const clusterHits = scored.filter((h) => !h.isLeaf).slice(0, 3);
|
|
130
|
-
assert.ok(clusterHits.length > 0, "should have cluster hits");
|
|
131
|
-
|
|
132
|
-
const expanded = expandLeafDescendants(
|
|
133
|
-
clusterHits,
|
|
134
|
-
tree,
|
|
135
|
-
5, // maxPerCluster
|
|
136
|
-
embedder,
|
|
137
|
-
qv,
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
// Expanded set should include leaf hits.
|
|
141
|
-
const leafHits = expanded.filter((h) => h.isLeaf);
|
|
142
|
-
assert.ok(leafHits.length > 0, "should have expanded leaf hits");
|
|
143
|
-
assert.ok(
|
|
144
|
-
expanded.length > clusterHits.length,
|
|
145
|
-
`expanded (${expanded.length}) should be > cluster hits (${clusterHits.length})`,
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
// No duplicate ids.
|
|
149
|
-
const ids = new Set(expanded.map((h) => h.nodeId));
|
|
150
|
-
assert.equal(ids.size, expanded.length, "no duplicate node ids");
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
// ── test: deduplicateMultilevelHits removes cluster when leaves present ─────
|
|
154
|
-
|
|
155
|
-
test("deduplicateMultilevelHits removes cluster hits when leaf children are present", () => {
|
|
156
|
-
const embedder = new TrigramEmbedder();
|
|
157
|
-
const leaves = makeLeaves(50);
|
|
158
|
-
const tree = buildRaptorTree(leaves, { embedder, clustersPerLevel: 4 });
|
|
159
|
-
|
|
160
|
-
const query = "the auth module validates the session token";
|
|
161
|
-
const scored = scoreTreeLevels(query, tree, { embedder });
|
|
162
|
-
|
|
163
|
-
// Take a cluster hit and its leaf children.
|
|
164
|
-
const cluster = scored.find((h) => !h.isLeaf);
|
|
165
|
-
assert.ok(cluster, "should have a cluster hit");
|
|
166
|
-
|
|
167
|
-
const leafChildren: MultilevelHit[] = cluster!.leafIds.slice(0, 2).map((lid) => ({
|
|
168
|
-
nodeId: lid,
|
|
169
|
-
level: 0,
|
|
170
|
-
score: 0.5,
|
|
171
|
-
rawScore: 0.5,
|
|
172
|
-
isLeaf: true,
|
|
173
|
-
leafIds: [lid],
|
|
174
|
-
summary: "",
|
|
175
|
-
embedding: cluster!.embedding,
|
|
176
|
-
}));
|
|
177
|
-
|
|
178
|
-
const mixed = [cluster!, ...leafChildren];
|
|
179
|
-
const deduped = deduplicateMultilevelHits(mixed);
|
|
180
|
-
|
|
181
|
-
// Cluster should be removed because its leaf children are present.
|
|
182
|
-
assert.ok(
|
|
183
|
-
!deduped.find((h) => h.nodeId === cluster!.nodeId),
|
|
184
|
-
"cluster hit should be removed when leaf children are present",
|
|
185
|
-
);
|
|
186
|
-
assert.equal(deduped.length, leafChildren.length, "only leaf hits remain");
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
test("deduplicateMultilevelHits keeps cluster hits when no leaf children present", () => {
|
|
190
|
-
const clusterHit: MultilevelHit = {
|
|
191
|
-
nodeId: "cluster_1",
|
|
192
|
-
level: 1,
|
|
193
|
-
score: 0.8,
|
|
194
|
-
rawScore: 0.8,
|
|
195
|
-
isLeaf: false,
|
|
196
|
-
leafIds: ["leaf_1", "leaf_2"],
|
|
197
|
-
summary: "summarized content",
|
|
198
|
-
embedding: [1, 0, 0],
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
const deduped = deduplicateMultilevelHits([clusterHit]);
|
|
202
|
-
assert.equal(deduped.length, 1, "cluster should be kept when no leaf children present");
|
|
203
|
-
assert.equal(deduped[0].nodeId, "cluster_1");
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
// ── test: multilevelRetrieval returns cluster + leaf mix ────────────────────
|
|
207
|
-
|
|
208
|
-
test("multilevelRetrieval returns a mix of cluster and leaf hits", () => {
|
|
209
|
-
const embedder = new TrigramEmbedder();
|
|
210
|
-
const leaves = makeLeaves(50);
|
|
211
|
-
const tree = buildRaptorTree(leaves, { embedder, clustersPerLevel: 4 });
|
|
212
|
-
|
|
213
|
-
const hits = multilevelRetrieval("the auth module validates the session token", tree, {
|
|
214
|
-
embedder,
|
|
215
|
-
k: 5,
|
|
216
|
-
leafExpansion: true,
|
|
217
|
-
maxLeafExpansion: 3,
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
assert.ok(hits.length > 0, "should return hits");
|
|
221
|
-
assert.ok(hits.length <= 5, "should respect k=5");
|
|
222
|
-
|
|
223
|
-
// Should include leaf hits.
|
|
224
|
-
const leafHits = hits.filter((h) => h.isLeaf);
|
|
225
|
-
assert.ok(leafHits.length > 0, "should include leaf hits");
|
|
226
|
-
|
|
227
|
-
// All hits should have valid properties.
|
|
228
|
-
for (const h of hits) {
|
|
229
|
-
assert.ok(h.nodeId, "hit should have nodeId");
|
|
230
|
-
assert.ok(typeof h.level === "number", "hit should have numeric level");
|
|
231
|
-
assert.ok(h.score >= 0, "hit score should be non-negative");
|
|
232
|
-
assert.ok(Array.isArray(h.leafIds), "hit should have leafIds array");
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
test("multilevelRetrieval respects k parameter", () => {
|
|
237
|
-
const embedder = new TrigramEmbedder();
|
|
238
|
-
const leaves = makeLeaves(100);
|
|
239
|
-
const tree = buildRaptorTree(leaves, { embedder, clustersPerLevel: 8 });
|
|
240
|
-
|
|
241
|
-
for (const k of [1, 3, 5, 10]) {
|
|
242
|
-
const hits = multilevelRetrieval("the auth module validates the session token", tree, {
|
|
243
|
-
embedder,
|
|
244
|
-
k,
|
|
245
|
-
});
|
|
246
|
-
assert.ok(
|
|
247
|
-
hits.length <= k,
|
|
248
|
-
`k=${k}: got ${hits.length} hits, should be ≤ ${k}`,
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
test("multilevelRetrieval returns empty for empty tree", () => {
|
|
254
|
-
const embedder = new TrigramEmbedder();
|
|
255
|
-
const emptyTree = { nodes: new Map(), rootId: null, levels: 0, timedOut: false };
|
|
256
|
-
const hits = multilevelRetrieval("any query", emptyTree, { embedder });
|
|
257
|
-
assert.deepEqual(hits, []);
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
test("multilevelRetrieval with leafExpansion=false skips leaf expansion", () => {
|
|
261
|
-
const embedder = new TrigramEmbedder();
|
|
262
|
-
const leaves = makeLeaves(50);
|
|
263
|
-
const tree = buildRaptorTree(leaves, { embedder, clustersPerLevel: 4 });
|
|
264
|
-
|
|
265
|
-
const hits = multilevelRetrieval("the auth module validates the session token", tree, {
|
|
266
|
-
embedder,
|
|
267
|
-
k: 5,
|
|
268
|
-
leafExpansion: false,
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
assert.ok(hits.length > 0, "should still return hits");
|
|
272
|
-
|
|
273
|
-
// With leaf expansion off and tree having multiple levels, we may get
|
|
274
|
-
// cluster hits that are NOT expanded. The mix depends on tree structure.
|
|
275
|
-
// Just verify we got valid results.
|
|
276
|
-
for (const h of hits) {
|
|
277
|
-
assert.ok(h.nodeId, "hit should have nodeId");
|
|
278
|
-
assert.ok(h.score >= 0, "hit score should be non-negative");
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
// ── test: buildChildParentIndex matches linear-scan parent ────────────────────
|
|
283
|
-
|
|
284
|
-
test("buildChildParentIndex (multilevel): index-derived parent matches linear-scan for each leaf", () => {
|
|
285
|
-
// Hand-built tree: 8 leaves under 2 level-1 nodes under 1 root.
|
|
286
|
-
const r1_0: RaptorNode = {
|
|
287
|
-
id: "r1_0", level: 1, parentId: null,
|
|
288
|
-
children: ["leaf_0", "leaf_1", "leaf_2", "leaf_3"],
|
|
289
|
-
summary: "cluster A", embedding: [1, 0, 0],
|
|
290
|
-
qualityMarker: "low", tokenEstimate: 10,
|
|
291
|
-
};
|
|
292
|
-
const r1_1: RaptorNode = {
|
|
293
|
-
id: "r1_1", level: 1, parentId: null,
|
|
294
|
-
children: ["leaf_4", "leaf_5", "leaf_6", "leaf_7"],
|
|
295
|
-
summary: "cluster B", embedding: [0, 1, 0],
|
|
296
|
-
qualityMarker: "low", tokenEstimate: 10,
|
|
297
|
-
};
|
|
298
|
-
const root: RaptorNode = {
|
|
299
|
-
id: "root", level: 2, parentId: null,
|
|
300
|
-
children: ["leaf_0", "leaf_1", "leaf_2", "leaf_3",
|
|
301
|
-
"leaf_4", "leaf_5", "leaf_6", "leaf_7"],
|
|
302
|
-
summary: "root", embedding: [0.5, 0.5, 0],
|
|
303
|
-
qualityMarker: "low", tokenEstimate: 20,
|
|
304
|
-
};
|
|
305
|
-
const nodes = new Map<string, RaptorNode>([
|
|
306
|
-
["r1_0", r1_0], ["r1_1", r1_1], ["root", root],
|
|
307
|
-
]);
|
|
308
|
-
const tree: RaptorTree = { nodes, rootId: "root", levels: 3, timedOut: false };
|
|
309
|
-
|
|
310
|
-
const index = buildChildParentIndex(tree);
|
|
311
|
-
const leafIds = ["leaf_0", "leaf_1", "leaf_2", "leaf_3",
|
|
312
|
-
"leaf_4", "leaf_5", "leaf_6", "leaf_7"];
|
|
313
|
-
|
|
314
|
-
for (const lid of leafIds) {
|
|
315
|
-
const linearParent = [...tree.nodes.values()].find((n) =>
|
|
316
|
-
n.children.includes(lid),
|
|
317
|
-
);
|
|
318
|
-
const indexParent = index.get(lid);
|
|
319
|
-
assert.equal(
|
|
320
|
-
indexParent?.id ?? null,
|
|
321
|
-
linearParent?.id ?? null,
|
|
322
|
-
`leaf ${lid}: index parent (${indexParent?.id}) != linear parent (${linearParent?.id})`,
|
|
323
|
-
);
|
|
324
|
-
}
|
|
325
|
-
});
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* promote.test.ts — Fix D: RAPTOR tree served by vectorStore.search.
|
|
3
|
-
*
|
|
4
|
-
* Asserts that, when a RAPTOR tree has been built + persisted for a session,
|
|
5
|
-
* VectorStore.search returns the tree's staged-expansion hits (broader, O(log n)
|
|
6
|
-
* coverage) merged with the flat hits — so the dormant tree becomes the live
|
|
7
|
-
* recall surface. No network: default extractive summarizer + trigram embedder.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { test } from "node:test";
|
|
11
|
-
import assert from "node:assert/strict";
|
|
12
|
-
import { mkdtempSync, rmSync } from "node:fs";
|
|
13
|
-
import { tmpdir } from "node:os";
|
|
14
|
-
import { join } from "node:path";
|
|
15
|
-
import { VectorStore, vectorList, vectorSearch } from "../../vectorStore.js";
|
|
16
|
-
import { runRaptor } from "./index.js";
|
|
17
|
-
import { compactSession } from "../../engine.js";
|
|
18
|
-
import { Logger } from "../../log.js";
|
|
19
|
-
import { loadDedupConfig } from "../../config/dedup.js";
|
|
20
|
-
import { listRaptorNodes } from "../../store/sqlite.js";
|
|
21
|
-
import type { EngineMessage } from "../../types.js";
|
|
22
|
-
|
|
23
|
-
const baseTmp = mkdtempSync(join(tmpdir(), "mc-promote-"));
|
|
24
|
-
let counter = 0;
|
|
25
|
-
function raptorConfig() {
|
|
26
|
-
return { ...loadDedupConfig(), RAPTOR_ENABLED: true };
|
|
27
|
-
}
|
|
28
|
-
function msg(text: string, toolName?: string): EngineMessage {
|
|
29
|
-
return toolName ? { role: "assistant", text, toolName, input: text, output: text } : { role: "user", text };
|
|
30
|
-
}
|
|
31
|
-
const SESS = "sess_promote";
|
|
32
|
-
|
|
33
|
-
test("Fix D: vectorStore.search serves a persisted RAPTOR tree (broader recall)", () => {
|
|
34
|
-
const stateDir = join(baseTmp, `run-${counter++}`);
|
|
35
|
-
const s = new VectorStore({ dedupSim: 0.9, stateDir, config: raptorConfig() });
|
|
36
|
-
|
|
37
|
-
// Persist several distinct checkpoints.
|
|
38
|
-
for (let i = 1; i <= 5; i++) {
|
|
39
|
-
compactSession(
|
|
40
|
-
{ sessionId: SESS, messages: [msg(`topic alpha wire ${i} and bootstrap sequence`), msg(`ok ${i}`, "Edit")], keepFrom: 2, timestamp: i },
|
|
41
|
-
s,
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// No tree yet → flat search only, returns hits, no RAPTOR coverage.
|
|
46
|
-
assert.equal(listRaptorNodes(SESS, stateDir).length, 0, "no tree initially");
|
|
47
|
-
const flat = vectorSearch(s, SESS, "alpha wire bootstrap", 3);
|
|
48
|
-
assert.ok(flat.length > 0, "flat search returns hits");
|
|
49
|
-
|
|
50
|
-
// Build + persist a RAPTOR tree for the session (mirrors runCompact refresh).
|
|
51
|
-
const all = vectorList(s, SESS);
|
|
52
|
-
const leaves = all.map((cp) => ({
|
|
53
|
-
id: cp.checkpointId,
|
|
54
|
-
messages: [],
|
|
55
|
-
sourceText: cp.normalizedText ?? cp.summary ?? cp.regionHash,
|
|
56
|
-
embedding: cp.embedding,
|
|
57
|
-
}));
|
|
58
|
-
const tree = runRaptor(leaves, { stateDir, sessionId: SESS, logger: new Logger() });
|
|
59
|
-
assert.ok(tree && listRaptorNodes(SESS, stateDir).length > 0, "tree persisted");
|
|
60
|
-
|
|
61
|
-
// With the tree live + RAPTOR_ENABLED, search still returns hits and now
|
|
62
|
-
// exercises the RAPTOR-served path without regression.
|
|
63
|
-
const withTree = vectorSearch(s, SESS, "alpha wire bootstrap", 3);
|
|
64
|
-
assert.ok(withTree.length > 0, "search returns hits with RAPTOR promoted");
|
|
65
|
-
// Every returned hit is a real checkpoint in the session.
|
|
66
|
-
for (const h of withTree) {
|
|
67
|
-
assert.ok(all.some((cp) => cp.checkpointId === h.checkpoint.checkpointId), "hit is a real checkpoint");
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("Fix D: search still works for a session with <2 leaves (no tree)", () => {
|
|
72
|
-
const stateDir = join(baseTmp, `run-${counter++}`);
|
|
73
|
-
const s = new VectorStore({ dedupSim: 0.9, stateDir, config: raptorConfig() });
|
|
74
|
-
compactSession({ sessionId: SESS, messages: [msg("only one topic here"), msg("ok", "Edit")], keepFrom: 2, timestamp: 1 }, s);
|
|
75
|
-
const r = vectorSearch(s, SESS, "only one topic", 3);
|
|
76
|
-
assert.ok(r.length > 0, "single-checkpoint search still works (no tree)");
|
|
77
|
-
assert.equal(listRaptorNodes(SESS, stateDir).length, 0, "no tree built for <2 leaves");
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("cleanup", () => {
|
|
81
|
-
rmSync(baseTmp, { recursive: true, force: true });
|
|
82
|
-
});
|