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,161 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* migrations.test.ts — S49B main-db → turns.db migration tests.
|
|
3
|
-
*
|
|
4
|
-
* Seeds a legacy main sqlite.db (with S48-era turn tables + session_state
|
|
5
|
-
* conversation pointers), then opens the turns store and asserts rows moved,
|
|
6
|
-
* legacy dropped, and the move is idempotent + flag-gated. No network.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { test, beforeEach, afterEach } from "node:test";
|
|
10
|
-
import assert from "node:assert/strict";
|
|
11
|
-
import { mkdtempSync, rmSync, mkdirSync } from "node:fs";
|
|
12
|
-
import { tmpdir } from "node:os";
|
|
13
|
-
import { join } from "node:path";
|
|
14
|
-
import { DatabaseSync } from "node:sqlite";
|
|
15
|
-
import { openTurnStore, closeTurnStore } from "./connection.js";
|
|
16
|
-
import { TurnsConfig } from "../../config/turns.js";
|
|
17
|
-
|
|
18
|
-
let tmpDir: string;
|
|
19
|
-
let counter = 0;
|
|
20
|
-
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
tmpDir = mkdtempSync(join(tmpdir(), "mc-migrate-"));
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
rmSync(tmpDir, { recursive: true, force: true });
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
function stateDir(): string {
|
|
30
|
-
return join(tmpDir, `run-${counter++}`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Seed a legacy main db with S48-era turn tables + a conversation pointer. */
|
|
34
|
-
function seedLegacyMainDb(dir: string): void {
|
|
35
|
-
mkdirSync(dir, { recursive: true });
|
|
36
|
-
const db = new DatabaseSync(join(dir, "sqlite.db"));
|
|
37
|
-
db.exec(`
|
|
38
|
-
CREATE TABLE turns (
|
|
39
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT, conversation_id TEXT NOT NULL,
|
|
40
|
-
session_id TEXT NOT NULL, turn_index INTEGER NOT NULL, role TEXT,
|
|
41
|
-
started_at INTEGER NOT NULL, ended_at INTEGER, ctx_tokens INTEGER,
|
|
42
|
-
ctx_percent REAL, pressure_band TEXT, model_id TEXT, epoch_id TEXT,
|
|
43
|
-
UNIQUE(session_id, turn_index)
|
|
44
|
-
);
|
|
45
|
-
CREATE TABLE turn_recall (
|
|
46
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT, turn_id INTEGER NOT NULL,
|
|
47
|
-
checkpoint_id TEXT NOT NULL, score REAL NOT NULL, source TEXT NOT NULL,
|
|
48
|
-
raptor_level INTEGER, UNIQUE(turn_id, checkpoint_id)
|
|
49
|
-
);
|
|
50
|
-
CREATE TABLE conversation_branches (
|
|
51
|
-
conversation_id TEXT PRIMARY KEY, parent_conversation_id TEXT NOT NULL,
|
|
52
|
-
fork_turn_id INTEGER NOT NULL, created_at INTEGER NOT NULL
|
|
53
|
-
);
|
|
54
|
-
CREATE TABLE session_state (
|
|
55
|
-
session_id TEXT PRIMARY KEY, injected_checkpoint_ids TEXT,
|
|
56
|
-
stored_region_hashes TEXT, conversation_id TEXT, last_turn_id INTEGER
|
|
57
|
-
);
|
|
58
|
-
INSERT INTO turns (conversation_id, session_id, turn_index, started_at, ended_at, ctx_tokens)
|
|
59
|
-
VALUES ('conv_leg', 'sess_leg', 0, 100, 200, 500);
|
|
60
|
-
INSERT INTO turn_recall (turn_id, checkpoint_id, score, source)
|
|
61
|
-
VALUES (1, 'cp_leg', 0.6, 'flat');
|
|
62
|
-
INSERT INTO conversation_branches (conversation_id, parent_conversation_id, fork_turn_id, created_at)
|
|
63
|
-
VALUES ('conv_child', 'conv_leg', 1, 300);
|
|
64
|
-
INSERT INTO session_state (session_id, conversation_id) VALUES ('sess_leg', 'conv_leg');
|
|
65
|
-
`);
|
|
66
|
-
db.close();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function tableExists(db: DatabaseSync, name: string): boolean {
|
|
70
|
-
return (
|
|
71
|
-
db
|
|
72
|
-
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name=?")
|
|
73
|
-
.get(name) !== undefined
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
test("fresh dir (no main db) → no-op, marks migrated", () => {
|
|
78
|
-
const dir = stateDir();
|
|
79
|
-
const db = openTurnStore(dir);
|
|
80
|
-
// openTurnStore ran the migration hook; with no sqlite.db it should just mark.
|
|
81
|
-
const row = db
|
|
82
|
-
.prepare("SELECT value FROM turns_meta WHERE key='migrated_from_main'")
|
|
83
|
-
.get() as { value: string } | undefined;
|
|
84
|
-
assert.equal(row?.value, "1");
|
|
85
|
-
closeTurnStore(dir);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test("legacy rows moved, legacy tables dropped, conversation pointer migrated", () => {
|
|
89
|
-
const dir = stateDir();
|
|
90
|
-
seedLegacyMainDb(dir);
|
|
91
|
-
const db = openTurnStore(dir); // triggers migration
|
|
92
|
-
|
|
93
|
-
// Rows present in turns.db.
|
|
94
|
-
const turns = db.prepare("SELECT * FROM turns").all() as Array<
|
|
95
|
-
Record<string, unknown>
|
|
96
|
-
>;
|
|
97
|
-
assert.equal(turns.length, 1);
|
|
98
|
-
assert.equal(turns[0].conversation_id, "conv_leg");
|
|
99
|
-
const recall = db.prepare("SELECT * FROM turn_recall").all() as Array<
|
|
100
|
-
Record<string, unknown>
|
|
101
|
-
>;
|
|
102
|
-
assert.equal(recall.length, 1);
|
|
103
|
-
assert.equal(recall[0].checkpoint_id, "cp_leg");
|
|
104
|
-
const branches = db
|
|
105
|
-
.prepare("SELECT * FROM conversation_forks")
|
|
106
|
-
.all() as Array<Record<string, unknown>>;
|
|
107
|
-
assert.equal(branches.length, 1);
|
|
108
|
-
assert.equal(branches[0].parent_conversation_id, "conv_leg");
|
|
109
|
-
assert.equal(branches[0].child_conversation_id, "conv_child");
|
|
110
|
-
assert.equal(branches[0].fork_turn_index, 1);
|
|
111
|
-
// Conversation pointer migrated to session_conversations (session_id → conversation_id).
|
|
112
|
-
const ptr = db
|
|
113
|
-
.prepare(
|
|
114
|
-
"SELECT conversation_id FROM session_conversations WHERE session_id='sess_leg'",
|
|
115
|
-
)
|
|
116
|
-
.get() as { conversation_id: string } | undefined;
|
|
117
|
-
assert.equal(ptr?.conversation_id, "conv_leg");
|
|
118
|
-
|
|
119
|
-
closeTurnStore(dir);
|
|
120
|
-
|
|
121
|
-
// Legacy tables dropped from the main db.
|
|
122
|
-
const main = new DatabaseSync(join(dir, "sqlite.db"));
|
|
123
|
-
assert.ok(!tableExists(main, "turns"));
|
|
124
|
-
assert.ok(!tableExists(main, "turn_recall"));
|
|
125
|
-
assert.ok(!tableExists(main, "conversation_branches"));
|
|
126
|
-
assert.ok(tableExists(main, "session_state")); // left intact
|
|
127
|
-
main.close();
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
test("idempotent: re-open does not re-copy or re-drop", () => {
|
|
131
|
-
const dir = stateDir();
|
|
132
|
-
seedLegacyMainDb(dir);
|
|
133
|
-
openTurnStore(dir);
|
|
134
|
-
closeTurnStore(dir);
|
|
135
|
-
// Second open — marker set, should be a no-op even though main db now lacks turn tables.
|
|
136
|
-
const db = openTurnStore(dir);
|
|
137
|
-
const turns = db.prepare("SELECT * FROM turns").all() as Array<
|
|
138
|
-
Record<string, unknown>
|
|
139
|
-
>;
|
|
140
|
-
assert.equal(turns.length, 1); // still exactly the migrated row, not duplicated
|
|
141
|
-
closeTurnStore(dir);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
test("flag OFF → legacy tables untouched, no marker write from hook", () => {
|
|
145
|
-
const dir = stateDir();
|
|
146
|
-
seedLegacyMainDb(dir);
|
|
147
|
-
const original = TurnsConfig.TURNS_DB_ENABLED;
|
|
148
|
-
TurnsConfig.TURNS_DB_ENABLED = false;
|
|
149
|
-
try {
|
|
150
|
-
const db = openTurnStore(dir);
|
|
151
|
-
closeTurnStore(dir);
|
|
152
|
-
void db;
|
|
153
|
-
} finally {
|
|
154
|
-
TurnsConfig.TURNS_DB_ENABLED = original;
|
|
155
|
-
}
|
|
156
|
-
// Main db turn tables must be intact (migration hook was gated off).
|
|
157
|
-
const main = new DatabaseSync(join(dir, "sqlite.db"));
|
|
158
|
-
assert.ok(tableExists(main, "turns"));
|
|
159
|
-
assert.ok(tableExists(main, "conversation_branches"));
|
|
160
|
-
main.close();
|
|
161
|
-
});
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* sqlite-store.test.ts — SqliteTurnStore compliance + SQLite-specific tests.
|
|
3
|
-
*
|
|
4
|
-
* Compliance: shared suite run in-memory (fast).
|
|
5
|
-
* SQLite-specific: file-backed tests verifying:
|
|
6
|
-
* - Separate turns.db (not sqlite.db)
|
|
7
|
-
* - WAL mode active
|
|
8
|
-
* - Connection caching (same path → same connection)
|
|
9
|
-
* - dbSizeBytes reports file size
|
|
10
|
-
* - File persistence across store instances
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
14
|
-
import assert from "node:assert/strict";
|
|
15
|
-
import { existsSync, statSync, rmSync, mkdtempSync } from "node:fs";
|
|
16
|
-
import { join } from "node:path";
|
|
17
|
-
import { tmpdir } from "node:os";
|
|
18
|
-
import { SqliteTurnStore } from "./sqlite-store.js";
|
|
19
|
-
import { runComplianceSuite } from "./contract-compliance.test.js";
|
|
20
|
-
import { closeAllTurnDbs } from "./connection.js";
|
|
21
|
-
import type { TurnEntry } from "./types.js";
|
|
22
|
-
|
|
23
|
-
// ── Compliance suite (in-memory, fast) ──────────────────────────
|
|
24
|
-
|
|
25
|
-
runComplianceSuite(
|
|
26
|
-
"SqliteTurnStore",
|
|
27
|
-
(options) => new SqliteTurnStore(options),
|
|
28
|
-
{ stateDir: join(tmpdir(), "turns-compliance-sqlite"), inMemory: true },
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
// ── SQLite-specific file-backed tests ───────────────────────────
|
|
32
|
-
|
|
33
|
-
describe("SqliteTurnStore — file-backed", () => {
|
|
34
|
-
let stateDir: string;
|
|
35
|
-
let store: SqliteTurnStore;
|
|
36
|
-
|
|
37
|
-
beforeEach(() => {
|
|
38
|
-
closeAllTurnDbs(); // clear any cached connections
|
|
39
|
-
stateDir = mkdtempSync(join(tmpdir(), "turns-file-"));
|
|
40
|
-
store = new SqliteTurnStore({ stateDir });
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
afterEach(() => {
|
|
44
|
-
try {
|
|
45
|
-
store.close();
|
|
46
|
-
} catch {
|
|
47
|
-
// best-effort
|
|
48
|
-
}
|
|
49
|
-
closeAllTurnDbs();
|
|
50
|
-
try {
|
|
51
|
-
rmSync(stateDir, { recursive: true, force: true });
|
|
52
|
-
} catch {
|
|
53
|
-
// best-effort
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("creates turns.db separate from sqlite.db", () => {
|
|
58
|
-
const turnsDbPath = join(stateDir, "turns.db");
|
|
59
|
-
assert.ok(existsSync(turnsDbPath), "turns.db should exist");
|
|
60
|
-
|
|
61
|
-
// sqlite.db should NOT be created by the turns store
|
|
62
|
-
const sqliteDbPath = join(stateDir, "sqlite.db");
|
|
63
|
-
assert.ok(!existsSync(sqliteDbPath), "sqlite.db should NOT be created");
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("uses WAL journal mode", () => {
|
|
67
|
-
const row = (store as unknown as { db: { prepare: (s: string) => { get: () => unknown } } })
|
|
68
|
-
.db.prepare("PRAGMA journal_mode")
|
|
69
|
-
.get() as { journal_mode: string };
|
|
70
|
-
assert.equal(row.journal_mode, "wal");
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("caches connections for the same path", () => {
|
|
74
|
-
// Creating a second store with the same stateDir should reuse
|
|
75
|
-
// the cached connection (not create a new file lock conflict)
|
|
76
|
-
const store2 = new SqliteTurnStore({ stateDir });
|
|
77
|
-
try {
|
|
78
|
-
// Both should be able to write without locking errors
|
|
79
|
-
const entry: TurnEntry = {
|
|
80
|
-
conversationId: "conv_cache",
|
|
81
|
-
sessionId: "sess_cache",
|
|
82
|
-
turnIndex: 0,
|
|
83
|
-
role: "assistant",
|
|
84
|
-
endedAt: Date.now(),
|
|
85
|
-
};
|
|
86
|
-
store.asWriter().appendTurn(entry);
|
|
87
|
-
store2.asWriter().appendTurn({
|
|
88
|
-
...entry,
|
|
89
|
-
turnIndex: 1,
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
assert.equal(store.asReader().countTurns("conv_cache"), 2);
|
|
93
|
-
} finally {
|
|
94
|
-
store2.close();
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it("reports dbSizeBytes for file-backed store", () => {
|
|
99
|
-
const entry: TurnEntry = {
|
|
100
|
-
conversationId: "conv_size",
|
|
101
|
-
sessionId: "sess_size",
|
|
102
|
-
turnIndex: 0,
|
|
103
|
-
role: "assistant",
|
|
104
|
-
endedAt: Date.now(),
|
|
105
|
-
};
|
|
106
|
-
store.asWriter().appendTurn(entry);
|
|
107
|
-
|
|
108
|
-
// prune returns a PruneReport with freedBytes; we can also
|
|
109
|
-
// verify the file exists and has content
|
|
110
|
-
const turnsDbPath = join(stateDir, "turns.db");
|
|
111
|
-
const size = statSync(turnsDbPath).size;
|
|
112
|
-
assert.ok(size > 0, "turns.db should have non-zero size after writes");
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("persists data across store instances", () => {
|
|
116
|
-
const entry: TurnEntry = {
|
|
117
|
-
conversationId: "conv_persist",
|
|
118
|
-
sessionId: "sess_persist",
|
|
119
|
-
turnIndex: 0,
|
|
120
|
-
role: "assistant",
|
|
121
|
-
endedAt: Date.now(),
|
|
122
|
-
model: "test-model",
|
|
123
|
-
};
|
|
124
|
-
store.asWriter().appendTurn(entry);
|
|
125
|
-
store.close();
|
|
126
|
-
closeAllTurnDbs();
|
|
127
|
-
|
|
128
|
-
// Re-open the same stateDir
|
|
129
|
-
const store2 = new SqliteTurnStore({ stateDir });
|
|
130
|
-
try {
|
|
131
|
-
const turns = store2.asReader().query({ conversationId: "conv_persist" });
|
|
132
|
-
assert.equal(turns.length, 1);
|
|
133
|
-
assert.equal(turns[0].model, "test-model");
|
|
134
|
-
} finally {
|
|
135
|
-
store2.close();
|
|
136
|
-
closeAllTurnDbs();
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it("supports custom dbPath", () => {
|
|
141
|
-
const customPath = join(stateDir, "custom-turns.db");
|
|
142
|
-
const customStore = new SqliteTurnStore({
|
|
143
|
-
stateDir,
|
|
144
|
-
dbPath: customPath,
|
|
145
|
-
});
|
|
146
|
-
try {
|
|
147
|
-
assert.ok(existsSync(customPath), "custom db path should be created");
|
|
148
|
-
customStore.asWriter().appendTurn({
|
|
149
|
-
conversationId: "conv_custom",
|
|
150
|
-
sessionId: "sess_custom",
|
|
151
|
-
turnIndex: 0,
|
|
152
|
-
role: "user",
|
|
153
|
-
endedAt: Date.now(),
|
|
154
|
-
});
|
|
155
|
-
assert.equal(customStore.asReader().countTurns("conv_custom"), 1);
|
|
156
|
-
} finally {
|
|
157
|
-
customStore.close();
|
|
158
|
-
closeAllTurnDbs();
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
});
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* vectorIndex.test.ts — Slice 2 async PGlite/HNSW vector index.
|
|
3
|
-
*
|
|
4
|
-
* Proves: cross-repo nearest-neighbor recall, repoId scoping, the dimension
|
|
5
|
-
* guard (non-512 vectors skipped, never corrupt the index), and graceful
|
|
6
|
-
* degradation when the index is disabled (kill-switch) — all without touching
|
|
7
|
-
* the synchronous node:sqlite store.
|
|
8
|
-
*
|
|
9
|
-
* The index is a WASM Postgres (PGlite) — fully local, zero network
|
|
10
|
-
* (PREVENT-PI-004). Each test isolates state via MEGACOMPACT_VECTOR_INDEX_DIR.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { test } from "node:test";
|
|
14
|
-
import assert from "node:assert/strict";
|
|
15
|
-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
16
|
-
import { tmpdir } from "node:os";
|
|
17
|
-
import { join } from "node:path";
|
|
18
|
-
import {
|
|
19
|
-
EMBEDDING_DIM,
|
|
20
|
-
initVectorIndex,
|
|
21
|
-
upsertEmbedding,
|
|
22
|
-
searchAsync,
|
|
23
|
-
closeVectorIndex,
|
|
24
|
-
isVectorIndexDisabled,
|
|
25
|
-
} from "./vectorIndex.js";
|
|
26
|
-
|
|
27
|
-
/** A 512-dim unit-ish vector with a single spike at `idx` (deterministic NN). */
|
|
28
|
-
function spikeVec(idx: number, magnitude = 1): number[] {
|
|
29
|
-
const v = new Array<number>(EMBEDDING_DIM).fill(0);
|
|
30
|
-
v[idx % EMBEDDING_DIM] = magnitude;
|
|
31
|
-
return v;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function isolateIndexDir(): string {
|
|
35
|
-
const dir = mkdtempSync(join(tmpdir(), "mc-vecidx-"));
|
|
36
|
-
process.env.MEGACOMPACT_VECTOR_INDEX_DIR = dir;
|
|
37
|
-
return dir;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
test("cross-repo HNSW nearest-neighbor recall across repos + repoId scoping", async () => {
|
|
41
|
-
delete process.env.MEGACOMPACT_PGLITE_DISABLED;
|
|
42
|
-
const dir = isolateIndexDir();
|
|
43
|
-
try {
|
|
44
|
-
await closeVectorIndex(); // ensure a fresh singleton for this dir
|
|
45
|
-
const pg = await initVectorIndex();
|
|
46
|
-
assert.ok(pg, "index should initialize (PGlite WASM available)");
|
|
47
|
-
|
|
48
|
-
// repoA: two checkpoints; repoB: one checkpoint. Distinct spike directions.
|
|
49
|
-
await upsertEmbedding("/repoA/.pi/mega-compact", "sessA", "chkpt_001", spikeVec(0));
|
|
50
|
-
await upsertEmbedding("/repoA/.pi/mega-compact", "sessA", "chkpt_002", spikeVec(5));
|
|
51
|
-
await upsertEmbedding("/repoB/.pi/mega-compact", "sessB", "chkpt_001", spikeVec(0));
|
|
52
|
-
|
|
53
|
-
// Cross-repo query near spike(0): nearest are the two spike(0) rows, one per repo.
|
|
54
|
-
const cross = await searchAsync(spikeVec(0), { k: 2 });
|
|
55
|
-
assert.equal(cross.length, 2, "cross-repo returns two nearest");
|
|
56
|
-
const repos = new Set(cross.map((h) => h.repoId));
|
|
57
|
-
assert.ok(repos.has("/repoA/.pi/mega-compact"), "hit from repoA");
|
|
58
|
-
assert.ok(repos.has("/repoB/.pi/mega-compact"), "hit from repoB");
|
|
59
|
-
assert.ok(cross[0].score > 0.99, "top hit is near-identical (cosine ~1)");
|
|
60
|
-
|
|
61
|
-
// Scoped to repoA only: excludes repoB even though repoB has an identical vec.
|
|
62
|
-
const scoped = await searchAsync(spikeVec(0), { k: 5, repoId: "/repoA/.pi/mega-compact" });
|
|
63
|
-
assert.ok(scoped.length >= 1, "scoped returns repoA hits");
|
|
64
|
-
assert.ok(
|
|
65
|
-
scoped.every((h) => h.repoId === "/repoA/.pi/mega-compact"),
|
|
66
|
-
"repoId filter excludes other repos",
|
|
67
|
-
);
|
|
68
|
-
} finally {
|
|
69
|
-
await closeVectorIndex();
|
|
70
|
-
rmSync(dir, { recursive: true, force: true });
|
|
71
|
-
delete process.env.MEGACOMPACT_VECTOR_INDEX_DIR;
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test("dimension guard: non-512 vectors are skipped, never corrupt the index", async () => {
|
|
76
|
-
delete process.env.MEGACOMPACT_PGLITE_DISABLED;
|
|
77
|
-
const dir = isolateIndexDir();
|
|
78
|
-
try {
|
|
79
|
-
await closeVectorIndex();
|
|
80
|
-
await initVectorIndex();
|
|
81
|
-
// Wrong-dimension vector (BYO embedder mismatch) must be silently skipped.
|
|
82
|
-
await upsertEmbedding("/repoC/.pi/mega-compact", "sessC", "chkpt_001", [1, 2, 3]);
|
|
83
|
-
const hits = await searchAsync(spikeVec(0), { k: 5 });
|
|
84
|
-
assert.equal(hits.length, 0, "no rows stored for a mismatched-dim vector");
|
|
85
|
-
|
|
86
|
-
// A correct-dim vector still stores fine afterward (index not corrupted).
|
|
87
|
-
await upsertEmbedding("/repoC/.pi/mega-compact", "sessC", "chkpt_002", spikeVec(3));
|
|
88
|
-
const ok = await searchAsync(spikeVec(3), { k: 1 });
|
|
89
|
-
assert.equal(ok.length, 1, "valid vector stored after a skipped one");
|
|
90
|
-
assert.equal(ok[0].checkpointId, "chkpt_002");
|
|
91
|
-
} finally {
|
|
92
|
-
await closeVectorIndex();
|
|
93
|
-
rmSync(dir, { recursive: true, force: true });
|
|
94
|
-
delete process.env.MEGACOMPACT_VECTOR_INDEX_DIR;
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test("corrupt-dir self-heal: torn PGlite dir is deleted + retried, never crashes", async () => {
|
|
99
|
-
delete process.env.MEGACOMPACT_PGLITE_DISABLED;
|
|
100
|
-
const dir = isolateIndexDir();
|
|
101
|
-
try {
|
|
102
|
-
await closeVectorIndex();
|
|
103
|
-
// Tear the dir: garbage bytes where PGlite expects its data/ files.
|
|
104
|
-
mkdirSync(dir, { recursive: true });
|
|
105
|
-
writeFileSync(join(dir, "data"), Buffer.from([0xde, 0xad, 0xbe, 0xef, 0xde, 0xad]));
|
|
106
|
-
// openPgLite(retryOnCorrupt=true) deletes + retries; if that still fails it
|
|
107
|
-
// disables gracefully. Either path must NOT throw.
|
|
108
|
-
const pg = await initVectorIndex();
|
|
109
|
-
assert.ok(pg, "index self-healed after corruption ");
|
|
110
|
-
// And the index works after heal: upsert + search round-trip.
|
|
111
|
-
await upsertEmbedding("/repoE/.pi/mega-compact", "sessE", "chkpt_001", spikeVec(7));
|
|
112
|
-
const hits = await searchAsync(spikeVec(7), { k: 1 });
|
|
113
|
-
assert.equal(hits.length, 1, "search works on the healed index");
|
|
114
|
-
assert.equal(hits[0].checkpointId, "chkpt_001");
|
|
115
|
-
} finally {
|
|
116
|
-
await closeVectorIndex();
|
|
117
|
-
rmSync(dir, { recursive: true, force: true });
|
|
118
|
-
delete process.env.MEGACOMPACT_VECTOR_INDEX_DIR;
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
test("kill-switch: MEGACOMPACT_PGLITE_DISABLED disables the index gracefully", async () => {
|
|
123
|
-
const dir = isolateIndexDir();
|
|
124
|
-
process.env.MEGACOMPACT_PGLITE_DISABLED = "true";
|
|
125
|
-
try {
|
|
126
|
-
await closeVectorIndex();
|
|
127
|
-
assert.equal(isVectorIndexDisabled(), true, "kill-switch reported disabled");
|
|
128
|
-
const pg = await initVectorIndex();
|
|
129
|
-
assert.equal(pg, undefined, "init returns undefined when disabled");
|
|
130
|
-
// Upsert + search are no-ops that never throw and return empty.
|
|
131
|
-
await upsertEmbedding("/repoD/.pi/mega-compact", "sessD", "chkpt_001", spikeVec(0));
|
|
132
|
-
const hits = await searchAsync(spikeVec(0), { k: 3 });
|
|
133
|
-
assert.deepEqual(hits, [], "search returns [] when disabled");
|
|
134
|
-
} finally {
|
|
135
|
-
delete process.env.MEGACOMPACT_PGLITE_DISABLED;
|
|
136
|
-
await closeVectorIndex();
|
|
137
|
-
rmSync(dir, { recursive: true, force: true });
|
|
138
|
-
delete process.env.MEGACOMPACT_VECTOR_INDEX_DIR;
|
|
139
|
-
}
|
|
140
|
-
});
|
package/src/store.test.ts
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { mkdtempSync, rmSync, mkdirSync, writeFileSync } from "node:fs";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { gzipSync } from "node:zlib";
|
|
7
|
-
import {
|
|
8
|
-
compressSmart,
|
|
9
|
-
decompressSmart,
|
|
10
|
-
readGzJson,
|
|
11
|
-
writeGzJson,
|
|
12
|
-
normalizeSessionId,
|
|
13
|
-
nextCheckpointId,
|
|
14
|
-
} from "./store.js";
|
|
15
|
-
|
|
16
|
-
const baseTmp = mkdtempSync(join(tmpdir(), "mc-store-"));
|
|
17
|
-
let counter = 0;
|
|
18
|
-
function tmpDir() {
|
|
19
|
-
return join(baseTmp, `d-${counter++}`);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// ---------------------------------------------------------------------------
|
|
23
|
-
// compressSmart / decompressSmart
|
|
24
|
-
// ---------------------------------------------------------------------------
|
|
25
|
-
|
|
26
|
-
test("compressSmart: tiny payload (<512B) uses RAW tier", () => {
|
|
27
|
-
const data = Buffer.from(JSON.stringify({ hello: "world" }));
|
|
28
|
-
assert.ok(data.length < 512);
|
|
29
|
-
const compressed = compressSmart(data);
|
|
30
|
-
// Versioned header: 0xEC 0x01 [version=1] [tier=0x00 RAW]; payload after byte 4.
|
|
31
|
-
assert.equal(compressed[0], 0xec, "magic hi");
|
|
32
|
-
assert.equal(compressed[1], 0x01, "magic lo / version marker");
|
|
33
|
-
assert.equal(compressed[2], 0x01, "format version 1");
|
|
34
|
-
assert.equal(compressed[3], 0x00, "tag RAW");
|
|
35
|
-
// Payload after the 4-byte header should be identical to original
|
|
36
|
-
assert.deepEqual(compressed.subarray(4), data);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("compressSmart: medium payload (4KB–32KB) uses GZIP-6 tier", () => {
|
|
40
|
-
// ~8KB of repetitive text
|
|
41
|
-
const data = Buffer.from("the quick brown fox jumps over the lazy dog. ".repeat(180));
|
|
42
|
-
assert.ok(data.length >= 4096 && data.length < 32768);
|
|
43
|
-
const compressed = compressSmart(data);
|
|
44
|
-
// Versioned header + tier tag 0x02 (GZIP-6) at byte 3.
|
|
45
|
-
assert.equal(compressed[0], 0xec);
|
|
46
|
-
assert.equal(compressed[3], 0x02, "tag GZIP-6");
|
|
47
|
-
// Compressed should be smaller
|
|
48
|
-
assert.ok(compressed.length < data.length, "compressed smaller than raw");
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test("compressSmart: large payload (>32KB) uses BROTLI tier", () => {
|
|
52
|
-
// ~40KB of repetitive text
|
|
53
|
-
const data = Buffer.from("this is a long summary of a coding session. ".repeat(900));
|
|
54
|
-
assert.ok(data.length >= 32768);
|
|
55
|
-
const compressed = compressSmart(data);
|
|
56
|
-
// Versioned header + tier tag 0x05 (BROTLI_4) at byte 3.
|
|
57
|
-
assert.equal(compressed[0], 0xec);
|
|
58
|
-
assert.equal(compressed[3], 0x05, "tag BROTLI_4");
|
|
59
|
-
// Compressed should be smaller
|
|
60
|
-
assert.ok(compressed.length < data.length, "brotli compressed smaller than raw");
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test("compressSmart: small payload (512B–4KB) uses GZIP-1 tier", () => {
|
|
64
|
-
// ~1.5KB
|
|
65
|
-
const data = Buffer.from("a moderately sized summary with some repetition. ".repeat(30));
|
|
66
|
-
assert.ok(data.length >= 512 && data.length < 4096);
|
|
67
|
-
const compressed = compressSmart(data);
|
|
68
|
-
assert.equal(compressed[0], 0xec);
|
|
69
|
-
assert.equal(compressed[3], 0x01, "tag GZIP-1");
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("decompressSmart roundtrips all tiers", () => {
|
|
73
|
-
const sizes = [
|
|
74
|
-
{ label: "tiny", gen: () => Buffer.from("small") },
|
|
75
|
-
{ label: "small", gen: () => Buffer.from("x".repeat(600)) },
|
|
76
|
-
{ label: "medium", gen: () => Buffer.from("y".repeat(8000)) },
|
|
77
|
-
{ label: "large", gen: () => Buffer.from("z".repeat(40000)) },
|
|
78
|
-
];
|
|
79
|
-
for (const { label, gen } of sizes) {
|
|
80
|
-
const original = gen();
|
|
81
|
-
const compressed = compressSmart(original);
|
|
82
|
-
const decompressed = decompressSmart(compressed);
|
|
83
|
-
assert.deepEqual(decompressed, original, `roundtrip failed for ${label} (${original.length}B)`);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test("decompressSmart handles legacy untagged gzip files (backward compat)", () => {
|
|
88
|
-
const data = Buffer.from(JSON.stringify({ legacy: true }));
|
|
89
|
-
const legacyGzip = gzipSync(data); // no tag byte, starts with 0x1f
|
|
90
|
-
assert.equal(legacyGzip[0], 0x1f, "gzip magic byte present");
|
|
91
|
-
const result = decompressSmart(legacyGzip);
|
|
92
|
-
assert.deepEqual(JSON.parse(result.toString()), { legacy: true });
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test("readGzJson / writeGzJson roundtrip with smart compression", () => {
|
|
96
|
-
const dir = tmpDir();
|
|
97
|
-
const path = join(dir, "test.json.gz");
|
|
98
|
-
const data = [{ id: "a", value: 42 }, { id: "b", value: 99 }];
|
|
99
|
-
writeGzJson(path, data);
|
|
100
|
-
const loaded = readGzJson<typeof data>(path, []);
|
|
101
|
-
assert.deepEqual(loaded, data);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
test("readGzJson reads legacy gzip files written by old code", () => {
|
|
105
|
-
const dir = tmpDir();
|
|
106
|
-
const path = join(dir, "legacy.json.gz");
|
|
107
|
-
const data = { old: true };
|
|
108
|
-
// Simulate old writeGzJson (plain gzip, no tag)
|
|
109
|
-
mkdirSync(join(path, ".."), { recursive: true });
|
|
110
|
-
const buf = gzipSync(Buffer.from(JSON.stringify(data), "utf-8"));
|
|
111
|
-
writeFileSync(path, buf);
|
|
112
|
-
|
|
113
|
-
const loaded = readGzJson<typeof data>(path, { old: false });
|
|
114
|
-
assert.deepEqual(loaded, { old: true });
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
test("compression tier: GZIP-1 and GZIP-6 tiers produce valid, smaller output", () => {
|
|
118
|
-
// GZIP-1 tier (~600B input, 512B–4KB band)
|
|
119
|
-
const small = compressSmart(Buffer.from("compress me ".repeat(200)));
|
|
120
|
-
assert.equal(small[0], 0xec, "versioned magic");
|
|
121
|
-
assert.equal(small[3], 0x01, "GZIP-1 tag for small input");
|
|
122
|
-
assert.ok(small.length < 600, "GZIP-1 output smaller than input");
|
|
123
|
-
assert.deepEqual(decompressSmart(small), Buffer.from("compress me ".repeat(200)));
|
|
124
|
-
|
|
125
|
-
// GZIP-6 tier (~8KB input, 4KB–32KB band)
|
|
126
|
-
const big = compressSmart(Buffer.from("compress me ".repeat(1800)));
|
|
127
|
-
assert.equal(big[0], 0xec, "versioned magic");
|
|
128
|
-
assert.equal(big[3], 0x02, "GZIP-6 tag for medium input");
|
|
129
|
-
assert.ok(big.length < Buffer.from("compress me ".repeat(1800)).length, "GZIP-6 output smaller than input");
|
|
130
|
-
assert.deepEqual(decompressSmart(big), Buffer.from("compress me ".repeat(1800)));
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
test("Fix E: pressure escalates gzip tier strength (sync, no zstd)", () => {
|
|
134
|
-
const small = Buffer.from("compress me ".repeat(200)); // 512B–4KB band
|
|
135
|
-
const medium = Buffer.from("compress me ".repeat(1800)); // 4KB–32KB band
|
|
136
|
-
|
|
137
|
-
// Low pressure → cheap levels (gzip-1 / gzip-6).
|
|
138
|
-
const lowSmall = compressSmart(small, 0);
|
|
139
|
-
const lowMedium = compressSmart(medium, 0);
|
|
140
|
-
assert.equal(lowSmall[3], 0x01, "small tier tag");
|
|
141
|
-
assert.equal(lowMedium[3], 0x02, "medium tier tag");
|
|
142
|
-
assert.ok(decompressSmart(lowSmall).equals(small), "low-pressure small roundtrips");
|
|
143
|
-
assert.ok(decompressSmart(lowMedium).equals(medium), "low-pressure medium roundtrips");
|
|
144
|
-
|
|
145
|
-
// High pressure → stronger levels (gzip-9 / gzip-9); tag unchanged, output
|
|
146
|
-
// must still decode to the exact original (versioned header preserved).
|
|
147
|
-
const highSmall = compressSmart(small, 1);
|
|
148
|
-
const highMedium = compressSmart(medium, 1);
|
|
149
|
-
assert.equal(highSmall[3], 0x01, "tag unchanged under pressure");
|
|
150
|
-
assert.equal(highMedium[3], 0x02, "tag unchanged under pressure");
|
|
151
|
-
assert.ok(decompressSmart(highSmall).equals(small), "high-pressure small roundtrips");
|
|
152
|
-
assert.ok(decompressSmart(highMedium).equals(medium), "high-pressure medium roundtrips");
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// ---------------------------------------------------------------------------
|
|
156
|
-
// normalizeSessionId
|
|
157
|
-
// ---------------------------------------------------------------------------
|
|
158
|
-
|
|
159
|
-
test("normalizeSessionId adds sess_ prefix when missing", () => {
|
|
160
|
-
assert.equal(normalizeSessionId("abc"), "sess_abc");
|
|
161
|
-
assert.equal(normalizeSessionId("sess_abc"), "sess_abc");
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// ---------------------------------------------------------------------------
|
|
165
|
-
// nextCheckpointId
|
|
166
|
-
// ---------------------------------------------------------------------------
|
|
167
|
-
|
|
168
|
-
test("nextCheckpointId returns chkpt_001 for new session", () => {
|
|
169
|
-
const dir = tmpDir();
|
|
170
|
-
assert.equal(nextCheckpointId("sess_nci1", dir), "chkpt_001");
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
test("nextCheckpointId increments highest existing id", () => {
|
|
174
|
-
const dir = tmpDir();
|
|
175
|
-
const sid = "sess_nci2";
|
|
176
|
-
// Write a checkpoints file with two entries to simulate existing state
|
|
177
|
-
const fakeCheckpoints = [
|
|
178
|
-
{ checkpointId: "chkpt_001", sessionId: "sess_nci2", summary: "a", keyDecisions: [], nextSteps: [], filesModified: [], tokenEstimate: 100, regionHash: "h1", embedding: [], timestamp: 1 },
|
|
179
|
-
{ checkpointId: "chkpt_003", sessionId: "sess_nci2", summary: "b", keyDecisions: [], nextSteps: [], filesModified: [], tokenEstimate: 100, regionHash: "h2", embedding: [], timestamp: 2 },
|
|
180
|
-
];
|
|
181
|
-
writeGzJson(join(dir, "sess_nci2.checkpoints.json.gz"), fakeCheckpoints);
|
|
182
|
-
assert.equal(nextCheckpointId(sid, dir), "chkpt_004");
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
// ---------------------------------------------------------------------------
|
|
186
|
-
// cleanup
|
|
187
|
-
// ---------------------------------------------------------------------------
|
|
188
|
-
|
|
189
|
-
test("cleanup", () => {
|
|
190
|
-
rmSync(baseTmp, { recursive: true, force: true });
|
|
191
|
-
});
|