From 4c344432625b62ba693d9e171f5800e9c51ad84a Mon Sep 17 00:00:00 2001 From: Omar Gerardo Date: Fri, 30 Oct 2026 13:22:47 +0800 Subject: [PATCH 1/3] fix(state): sweep index generations no manifest names (#1115) Before bucketed vector storage (#1416), each index save wrote a new shard generation under mem:index:bm25:bm25:: or :vectors::, then deleted the previous one only when its manifest read had succeeded. A failed read stranded the whole generation (one production store held 162 such shards, 339.4 MB). The legacy cleanup on main deletes only the shards a legacy manifest still names, so these generations are never reached. Add a one-time sweep that runs after the legacy cleanup on every load path that completes. It lists scopes through state::list_groups (new StateKV.listGroups), keeps every generation either legacy manifest names, and deletes the shards of the rest. It deletes nothing when scopes cannot be listed, a manifest read fails, a manifest names no generation, or a scope under a legacy prefix matches neither generation format ever written. Completion is recorded at mem:index:bm25/legacy:sweep only when every delete succeeds, so a partial run retries on the next boot. Each delete and the summary go through the existing index_persist audit. The 4100+ test count updates come from docs:sync. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 4 +- README.md | 4 +- READMEs/README.ar-SA.md | 4 +- READMEs/README.bn-BD.md | 4 +- READMEs/README.cs-CZ.md | 4 +- READMEs/README.da-DK.md | 4 +- READMEs/README.de-DE.md | 4 +- READMEs/README.el-GR.md | 4 +- READMEs/README.es-ES.md | 4 +- READMEs/README.fi-FI.md | 4 +- READMEs/README.fr-FR.md | 4 +- READMEs/README.he-IL.md | 4 +- READMEs/README.hi-IN.md | 4 +- READMEs/README.hu-HU.md | 4 +- READMEs/README.id-ID.md | 4 +- READMEs/README.it-IT.md | 4 +- READMEs/README.ja-JP.md | 4 +- READMEs/README.ko-KR.md | 4 +- READMEs/README.nb-NO.md | 4 +- READMEs/README.nl-NL.md | 4 +- READMEs/README.pl-PL.md | 4 +- READMEs/README.pt-BR.md | 4 +- READMEs/README.pt-PT.md | 4 +- READMEs/README.ro-RO.md | 4 +- READMEs/README.ru-RU.md | 4 +- READMEs/README.sv-SE.md | 4 +- READMEs/README.th-TH.md | 4 +- READMEs/README.tl-PH.md | 4 +- READMEs/README.tr-TR.md | 4 +- READMEs/README.uk-UA.md | 4 +- READMEs/README.ur-PK.md | 4 +- READMEs/README.vi-VN.md | 4 +- READMEs/README.zh-CN.md | 4 +- READMEs/README.zh-TW.md | 4 +- assets/tags/light/stat-tests.svg | 4 +- assets/tags/stat-tests.svg | 4 +- scripts/docs-sync.state.json | 2 +- src/state/index-persistence.ts | 91 ++++++++++++++- src/state/kv.ts | 12 ++ test/index-persistence.test.ts | 193 +++++++++++++++++++++++++++++++ 40 files changed, 363 insertions(+), 79 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 6c88f558e2..c53122c1e1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -101,7 +101,7 @@ Hook scripts in `src/hooks/` are standalone Node.js scripts (no iii-sdk import). ## Testing -- Before every commit run, in order: `npm run build`, `npm run skills:gen`, `npm run docs:sync`, `npm run skills:check`, `npm test` (4,000+ tests). Commit anything the generators changed. CI runs the same build, skills check and tests. +- Before every commit run, in order: `npm run build`, `npm run skills:gen`, `npm run docs:sync`, `npm run skills:check`, `npm test` (4,100+ tests). Commit anything the generators changed. CI runs the same build, skills check and tests. - Before any npm publish: `npm run release:gate` must pass. It installs the packed tarballs into a clean home and runs capture, recovery, restart, export/import, MCP and status checks against the installed CLI (see CONTRIBUTING.md, Release process) - Mock pattern: `vi.mock("iii-sdk")` with mock `sdk.trigger`, `kv.get/set/list` - Test files go in `test/` with `.test.ts` extension @@ -114,4 +114,4 @@ Hook scripts in `src/hooks/` are standalone Node.js scripts (no iii-sdk import). - 6 MCP resources, 3 MCP prompts - 12 hooks, 17 skills - 322+ iii functions -- 4,000+ tests +- 4,100+ tests diff --git a/README.md b/README.md index 0cc41b258a..b371b080e4 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1307,7 +1307,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ar-SA.md b/READMEs/README.ar-SA.md index 3199b8a8d7..432c2446ec 100644 --- a/READMEs/README.ar-SA.md +++ b/READMEs/README.ar-SA.md @@ -71,7 +71,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1242,7 +1242,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.bn-BD.md b/READMEs/README.bn-BD.md index 8fe8425a88..8a85ba1375 100644 --- a/READMEs/README.bn-BD.md +++ b/READMEs/README.bn-BD.md @@ -69,7 +69,7 @@ 54 MCP টুল 12 অটো হুক 0 এক্সটার্নাল ডিবি - 4,000+ টেস্ট পাস + 4,100+ টেস্ট পাস

@@ -1255,7 +1255,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.cs-CZ.md b/READMEs/README.cs-CZ.md index d0960e654a..6733e9778b 100644 --- a/READMEs/README.cs-CZ.md +++ b/READMEs/README.cs-CZ.md @@ -69,7 +69,7 @@ 54 MCP nástrojů 12 automatických hooks 0 externích databází - 4,000+ testů úspěšně proběhlo + 4,100+ testů úspěšně proběhlo

@@ -1241,7 +1241,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.da-DK.md b/READMEs/README.da-DK.md index 0c124bb1b3..986f11e1c0 100644 --- a/READMEs/README.da-DK.md +++ b/READMEs/README.da-DK.md @@ -69,7 +69,7 @@ 54 MCP-værktøjer 12 automatiske hooks 0 eksterne databaser - 4,000+ beståede tests + 4,100+ beståede tests

@@ -1249,7 +1249,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.de-DE.md b/READMEs/README.de-DE.md index 25d246df90..7a7aee598b 100644 --- a/READMEs/README.de-DE.md +++ b/READMEs/README.de-DE.md @@ -69,7 +69,7 @@ 54 MCP-Tools 12 Auto-Hooks 0 externe DBs - 4,000+ bestandene Tests + 4,100+ bestandene Tests

@@ -1247,7 +1247,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.el-GR.md b/READMEs/README.el-GR.md index 9fdaf0a8ed..da436a98c1 100644 --- a/READMEs/README.el-GR.md +++ b/READMEs/README.el-GR.md @@ -69,7 +69,7 @@ 54 εργαλεία MCP 12 αυτόματα hooks 0 εξωτερικές βάσεις δεδομένων - 4,000+ επιτυχημένες δοκιμές + 4,100+ επιτυχημένες δοκιμές

@@ -1241,7 +1241,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.es-ES.md b/READMEs/README.es-ES.md index 9e646f18ae..9a2add4427 100644 --- a/READMEs/README.es-ES.md +++ b/READMEs/README.es-ES.md @@ -69,7 +69,7 @@ 54 tools MCP 12 hooks automáticos 0 bases de datos externas - 4,000+ tests superados + 4,100+ tests superados

@@ -1243,7 +1243,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.fi-FI.md b/READMEs/README.fi-FI.md index 0af45964b9..f0d7093a4f 100644 --- a/READMEs/README.fi-FI.md +++ b/READMEs/README.fi-FI.md @@ -69,7 +69,7 @@ 54 MCP-työkalua 12 automaattista hookia 0 ulkoista tietokantaa - 4,000+ testiä läpäisty + 4,100+ testiä läpäisty

@@ -1241,7 +1241,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.fr-FR.md b/READMEs/README.fr-FR.md index 6e8a3510ae..60ed0f017a 100644 --- a/READMEs/README.fr-FR.md +++ b/READMEs/README.fr-FR.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1250,7 +1250,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.he-IL.md b/READMEs/README.he-IL.md index 6cd626a723..580e9069b0 100644 --- a/READMEs/README.he-IL.md +++ b/READMEs/README.he-IL.md @@ -71,7 +71,7 @@ 54 כלי MCP 12 hooks אוטומטיים 0 מסדי נתונים חיצוניים - 4,000+ בדיקות עוברות + 4,100+ בדיקות עוברות

@@ -1242,7 +1242,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.hi-IN.md b/READMEs/README.hi-IN.md index c8fbebef85..04eb563111 100644 --- a/READMEs/README.hi-IN.md +++ b/READMEs/README.hi-IN.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1256,7 +1256,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.hu-HU.md b/READMEs/README.hu-HU.md index 864dbdb03c..2951739da5 100644 --- a/READMEs/README.hu-HU.md +++ b/READMEs/README.hu-HU.md @@ -69,7 +69,7 @@ 54 MCP eszköz 12 automatikus hook 0 külső adatbázis - 4,000+ sikeres teszt + 4,100+ sikeres teszt

@@ -1239,7 +1239,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.id-ID.md b/READMEs/README.id-ID.md index 37ee206858..b9b300ebfc 100644 --- a/READMEs/README.id-ID.md +++ b/READMEs/README.id-ID.md @@ -69,7 +69,7 @@ 54 tool MCP 12 hook otomatis 0 DB eksternal - 4,000+ pengujian lolos + 4,100+ pengujian lolos

@@ -1256,7 +1256,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.it-IT.md b/READMEs/README.it-IT.md index 8bf8a07fc9..76c4267bd3 100644 --- a/READMEs/README.it-IT.md +++ b/READMEs/README.it-IT.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1249,7 +1249,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ja-JP.md b/READMEs/README.ja-JP.md index ce2d9dee07..5f3bd8dd2d 100644 --- a/READMEs/README.ja-JP.md +++ b/READMEs/README.ja-JP.md @@ -69,7 +69,7 @@ 54 MCP ツール 12 自動 hooks 外部 DB 0 個 - 4,000+ 件のテストが成功 + 4,100+ 件のテストが成功

@@ -1255,7 +1255,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ko-KR.md b/READMEs/README.ko-KR.md index 9958760be9..75ea0ac701 100644 --- a/READMEs/README.ko-KR.md +++ b/READMEs/README.ko-KR.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1254,7 +1254,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.nb-NO.md b/READMEs/README.nb-NO.md index 56ebebc6ba..59f55f0809 100644 --- a/READMEs/README.nb-NO.md +++ b/READMEs/README.nb-NO.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1247,7 +1247,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.nl-NL.md b/READMEs/README.nl-NL.md index 569d1fb29c..d5f8c62a3d 100644 --- a/READMEs/README.nl-NL.md +++ b/READMEs/README.nl-NL.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1243,7 +1243,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.pl-PL.md b/READMEs/README.pl-PL.md index bdbe4eea32..d20aa558c1 100644 --- a/READMEs/README.pl-PL.md +++ b/READMEs/README.pl-PL.md @@ -69,7 +69,7 @@ 54 narzędzia MCP 12 automatycznych hooków 0 zewnętrznych baz danych - 4,000+ zaliczonych testów + 4,100+ zaliczonych testów

@@ -1239,7 +1239,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.pt-BR.md b/READMEs/README.pt-BR.md index b076d8d82e..c6c37e69f3 100644 --- a/READMEs/README.pt-BR.md +++ b/READMEs/README.pt-BR.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1247,7 +1247,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.pt-PT.md b/READMEs/README.pt-PT.md index 36ff810e19..0b57b1b8db 100644 --- a/READMEs/README.pt-PT.md +++ b/READMEs/README.pt-PT.md @@ -69,7 +69,7 @@ 54 ferramentas MCP 12 hooks automáticos 0 bases de dados externas - 4,000+ testes aprovados + 4,100+ testes aprovados

@@ -1243,7 +1243,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ro-RO.md b/READMEs/README.ro-RO.md index 890afccaef..3e1f4c5870 100644 --- a/READMEs/README.ro-RO.md +++ b/READMEs/README.ro-RO.md @@ -69,7 +69,7 @@ 54 de instrumente MCP 12 hook-uri automate 0 baze de date externe - 4,000+ teste trecute + 4,100+ teste trecute

@@ -1235,7 +1235,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ru-RU.md b/READMEs/README.ru-RU.md index af94d55bbb..3ea4e5de29 100644 --- a/READMEs/README.ru-RU.md +++ b/READMEs/README.ru-RU.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1241,7 +1241,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.sv-SE.md b/READMEs/README.sv-SE.md index a130b39bd2..3c3e72e866 100644 --- a/READMEs/README.sv-SE.md +++ b/READMEs/README.sv-SE.md @@ -69,7 +69,7 @@ 54 MCP-verktyg 12 automatiska hooks 0 externa databaser - 4,000+ godkända tester + 4,100+ godkända tester

@@ -1249,7 +1249,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.th-TH.md b/READMEs/README.th-TH.md index b549a34e6a..76671822b6 100644 --- a/READMEs/README.th-TH.md +++ b/READMEs/README.th-TH.md @@ -69,7 +69,7 @@ 54 เครื่องมือ MCP 12 hook อัตโนมัติ ไม่ต้องพึ่ง external DB เลย - ผ่านการทดสอบ 4,000+ รายการ + ผ่านการทดสอบ 4,100+ รายการ

@@ -1242,7 +1242,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.tl-PH.md b/READMEs/README.tl-PH.md index 2d4dcfe47d..ff68ebdba6 100644 --- a/READMEs/README.tl-PH.md +++ b/READMEs/README.tl-PH.md @@ -69,7 +69,7 @@ 54 MCP tool 12 awtomatikong hook 0 panlabas na DB - 4,000+ pumapasang test + 4,100+ pumapasang test

@@ -1251,7 +1251,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.tr-TR.md b/READMEs/README.tr-TR.md index 0b08152ae9..a5d36b5299 100644 --- a/READMEs/README.tr-TR.md +++ b/READMEs/README.tr-TR.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1241,7 +1241,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.uk-UA.md b/READMEs/README.uk-UA.md index ad26bff5c1..4033a79f17 100644 --- a/READMEs/README.uk-UA.md +++ b/READMEs/README.uk-UA.md @@ -69,7 +69,7 @@ 54 інструменти MCP 12 автоматичних хуків 0 зовнішніх БД - 4,000+ тестів пройдено + 4,100+ тестів пройдено

@@ -1242,7 +1242,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ur-PK.md b/READMEs/README.ur-PK.md index 62602b9130..53f941522e 100644 --- a/READMEs/README.ur-PK.md +++ b/READMEs/README.ur-PK.md @@ -71,7 +71,7 @@ 54 MCP ٹولز 12 خودکار ہکس 0 خارجی ڈیٹابیسز - 4,000+ ٹیسٹس پاس ہو رہے ہیں + 4,100+ ٹیسٹس پاس ہو رہے ہیں

@@ -1246,7 +1246,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.vi-VN.md b/READMEs/README.vi-VN.md index 3a7879a3f5..2be083144f 100644 --- a/READMEs/README.vi-VN.md +++ b/READMEs/README.vi-VN.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,000+ tests passing + 4,100+ tests passing

@@ -1251,7 +1251,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.zh-CN.md b/READMEs/README.zh-CN.md index b12e7fe2fd..a96226f914 100644 --- a/READMEs/README.zh-CN.md +++ b/READMEs/README.zh-CN.md @@ -69,7 +69,7 @@ 54 个 MCP 工具 12 个自动 hooks 0 个外部数据库 - 4,000+ 项测试通过 + 4,100+ 项测试通过

@@ -1252,7 +1252,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.zh-TW.md b/READMEs/README.zh-TW.md index bfd866fee0..5ed8dc3dce 100644 --- a/READMEs/README.zh-TW.md +++ b/READMEs/README.zh-TW.md @@ -69,7 +69,7 @@ 54 個 MCP 工具 12 個自動 hooks 0 個外部資料庫 - 4,000+ 測試通過 + 4,100+ 測試通過

@@ -1244,7 +1244,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,000+ tests +npm test # 4,100+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/assets/tags/light/stat-tests.svg b/assets/tags/light/stat-tests.svg index 915e1bab9d..7248bfad00 100644 --- a/assets/tags/light/stat-tests.svg +++ b/assets/tags/light/stat-tests.svg @@ -1,5 +1,5 @@ - + - 4000+ + 4100+ TESTS PASSING diff --git a/assets/tags/stat-tests.svg b/assets/tags/stat-tests.svg index 99b680533e..e51099c87f 100644 --- a/assets/tags/stat-tests.svg +++ b/assets/tags/stat-tests.svg @@ -1,5 +1,5 @@ - + - 4000+ + 4100+ TESTS PASSING diff --git a/scripts/docs-sync.state.json b/scripts/docs-sync.state.json index 0538665c6a..a17f6cf914 100644 --- a/scripts/docs-sync.state.json +++ b/scripts/docs-sync.state.json @@ -20,7 +20,7 @@ "12" ], "tests": [ - "4000+" + "4100+" ], "functions": [ "322" diff --git a/src/state/index-persistence.ts b/src/state/index-persistence.ts index a09af90ff4..bfb7f845b3 100644 --- a/src/state/index-persistence.ts +++ b/src/state/index-persistence.ts @@ -21,6 +21,11 @@ const PENDING_LOG_SAVE_THRESHOLD = 500; const EARLY_SAVE_MIN_GAP_MS = 5_000; const BACKFILL_MARKER_KEY = "vectors:backfill"; const MEMORY_ID_PREFIX = "mem_"; +const LEGACY_SHARD_SCOPE_PREFIXES = [`${KV.bm25Index}:bm25:`, `${KV.bm25Index}:vectors:`]; +const LEGACY_SHARD_KEY = "data"; +// Both generation formats sharded saves ever wrote: `-<32 hex>`, then generateId("idx"). +const LEGACY_SHARD_SUFFIX = /^((?:[0-9a-z]+-[0-9a-f]{32})|(?:idx_[0-9a-z]+_[0-9a-f]{12})):\d{5}$/; +const ORPHAN_SWEEP_KEY = "legacy:sweep"; function auditIndexPersistEnabled(): boolean { const raw = process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST; @@ -159,6 +164,13 @@ function isValidShardDescriptor( ); } +// undefined: not a legacy shard scope. null: under a legacy prefix but not a shape any save wrote. +function legacyShardGeneration(scope: string): string | null | undefined { + const prefix = LEGACY_SHARD_SCOPE_PREFIXES.find((candidate) => scope.startsWith(candidate)); + if (!prefix) return undefined; + return LEGACY_SHARD_SUFFIX.exec(scope.slice(prefix.length))?.[1] ?? null; +} + function isPersistedVector(row: unknown): row is PersistedVector { if (!row || typeof row !== "object") return false; const candidate = row as Partial; @@ -563,7 +575,7 @@ export class IndexPersistence { async load(): Promise { if (!this.vector) { - await this.removeLegacyBm25Snapshot(); + await this.finishLegacyCleanup(); return { vector: null, state: "none", savedAt: null }; } @@ -581,7 +593,7 @@ export class IndexPersistence { this.hasVectorCheckpoint = loaded.size > 0; this.checkpointBackfillSince = meta.backfillSince; await this.removeLegacyVectorSnapshotIfPresent(); - await this.removeLegacyBm25Snapshot(); + await this.finishLegacyCleanup(); const expectedCount = Number.isInteger(meta.count) ? meta.count : undefined; this.vectorCountShortfall = expectedCount !== undefined && loaded.size < expectedCount @@ -788,7 +800,7 @@ export class IndexPersistence { if (!manifest.ok || !legacyKey.ok) return { vector: null, state: "unavailable", savedAt: null }; const legacyPresent = manifest.value != null || (typeof legacyKey.value === "string" && legacyKey.value.length > 0); if (!legacyPresent) { - await this.removeLegacyBm25Snapshot(); + await this.finishLegacyCleanup(); return { vector: null, state: "none", savedAt: null }; } @@ -816,7 +828,7 @@ export class IndexPersistence { } await this.removeLegacyVectorSnapshot(manifest.value); - await this.removeLegacyBm25Snapshot(); + await this.finishLegacyCleanup(); await this.auditIndexPersistence("migrate", [statePath(KV.bm25Index, VECTOR_META_KEY)], { vectors: legacy.size, buckets: this.bucketCountInUse(), @@ -860,6 +872,70 @@ export class IndexPersistence { if (legacy != null) await this.deleteKey(KV.bm25Index, LEGACY_BM25_KEY, "legacy_bm25_cleanup"); } + private async finishLegacyCleanup(): Promise { + await this.removeLegacyBm25Snapshot(); + await this.sweepOrphanedGenerations(); + } + + // Legacy cleanup deletes only shards a manifest still names. A generation stranded before the + // upgrade (#1115) is named by none, so it is found by listing scopes. Any doubt deletes nothing. + private async sweepOrphanedGenerations(): Promise { + try { + if ((await this.kv.get(KV.bm25Index, ORPHAN_SWEEP_KEY)) != null) return; + const candidates = new Map(); + for (const scope of await this.kv.listGroups()) { + const generation = legacyShardGeneration(scope); + if (generation === undefined) continue; + if (generation === null) { + logger.warn("index persistence: orphan sweep skipped, unrecognized legacy shard scope", { scope }); + return; + } + candidates.set(scope, generation); + } + const named = await this.namedLegacyGenerations(); + if (!named) return; + const orphans = [...candidates].filter(([, generation]) => !named.has(generation)); + const failures = await inBatches(orphans, WRITE_CONCURRENCY, async ([scope]) => { + if (!(await this.deleteKey(scope, LEGACY_SHARD_KEY, "orphan_generation_sweep"))) throw new Error(scope); + }); + const summary = { + generations: new Set(orphans.map(([, generation]) => generation)).size, + shards: orphans.length, + failed: failures.length, + }; + if (failures.length > 0) { + logger.warn("index persistence: orphan sweep left shards behind; the next boot retries", summary); + return; + } + await this.kv.set(KV.bm25Index, ORPHAN_SWEEP_KEY, { v: 1, at: new Date(this.now()).toISOString(), ...summary }); + await this.auditIndexPersistence("sweep", [statePath(KV.bm25Index, ORPHAN_SWEEP_KEY)], summary); + logger.info("index persistence: swept orphaned legacy index generations", summary); + } catch (err) { + logger.warn("index persistence: orphan sweep failed; the next boot retries", { message: errorMessage(err) }); + } + } + + private async namedLegacyGenerations(): Promise | null> { + const named = new Set(); + for (const [key, label] of [[LEGACY_BM25_MANIFEST_KEY, "bm25"], [LEGACY_VECTOR_MANIFEST_KEY, "vector"]] as const) { + const read = await this.readIndexValue(KV.bm25Index, key, label, "manifest"); + if (!read.ok) return null; + const manifest = read.value; + if (manifest == null) continue; + const generations = typeof manifest === "object" && Array.isArray(manifest.shards) + ? manifest.shards.filter(isValidShardDescriptor).map((shard) => legacyShardGeneration(shard.scope)) + : []; + if (typeof manifest === "object" && typeof manifest.generation === "string") generations.push(manifest.generation); + const usable = generations.filter((generation): generation is string => !!generation); + for (const generation of usable) named.add(generation); + if (usable.length === 0) { + logger.warn("index persistence: orphan sweep skipped, a legacy manifest names no generation", { key }); + return null; + } + } + return named; + } + private logFailure(err: unknown): void { const now = this.now(); if (now - this.lastFailureLogAt < FAILURE_LOG_THROTTLE_MS) return; @@ -887,7 +963,7 @@ export class IndexPersistence { }); } - private async deleteKey(scope: string, key: string, reason: string): Promise { + private async deleteKey(scope: string, key: string, reason: string): Promise { let result = "deleted"; let error: string | undefined; try { @@ -903,10 +979,13 @@ export class IndexPersistence { result, error, }); + return error === undefined; } private async deleteShards(shards: IndexShardManifest["shards"], reason: string): Promise { - await inBatches(shards, WRITE_CONCURRENCY, (shard) => this.deleteKey(shard.scope, shard.key, reason)); + await inBatches(shards, WRITE_CONCURRENCY, async (shard) => { + await this.deleteKey(shard.scope, shard.key, reason); + }); } private async loadShardedData(legacyKey: string, manifestKey: string, label: string): Promise { diff --git a/src/state/kv.ts b/src/state/kv.ts index 7de13a2281..bfda4643a9 100644 --- a/src/state/kv.ts +++ b/src/state/kv.ts @@ -198,4 +198,16 @@ export class StateKV { if (this.backend !== 'redis' || !Array.isArray(values)) return values return orderLikeInsertion(values) } + + async listGroups(): Promise { + const result = await this.sdk.trigger, { groups?: unknown } | null>({ + function_id: 'state::list_groups', + payload: {}, + }) + const groups = result?.groups + if (!Array.isArray(groups) || !groups.every((group) => typeof group === 'string')) { + throw new Error('state::list_groups returned an unexpected shape') + } + return groups + } } diff --git a/test/index-persistence.test.ts b/test/index-persistence.test.ts index e80327bde5..7c5c7fdaae 100644 --- a/test/index-persistence.test.ts +++ b/test/index-persistence.test.ts @@ -29,6 +29,8 @@ function mockKV() { ops.push({ op: "list", scope }); return Array.from(store.get(scope)?.values() ?? []) as T[]; }, + listGroups: async (): Promise => + [...store].filter(([, entries]) => entries.size > 0).map(([scope]) => scope).sort(), }; } @@ -566,3 +568,194 @@ describe("index_persist audit gating", () => { expect(await indexPersistEntries()).toEqual([]); }); }); + +describe("IndexPersistence orphaned legacy generation sweep", () => { + const SWEEP_KEY = "legacy:sweep"; + const ORPHAN_IDX = "idx_mf000001_cccccccccccc"; + const ORPHAN_OLD = `mf000002-${"d".repeat(32)}`; + let kv: MockKV; + + beforeEach(() => { + kv = mockKV(); + }); + + async function writeOrphan(leg: "bm25" | "vectors", generation: string, shards = 2): Promise { + const scopes: string[] = []; + for (let i = 0; i < shards; i++) { + const scope = `${INDEX_SCOPE}:${leg}:${generation}:${String(i).padStart(5, "0")}`; + await kv.set(scope, "data", `orphan ${generation} ${i}`); + scopes.push(scope); + } + return scopes; + } + + async function writeBuckets(): Promise { + const vector = vectorWith([["obs_a", [0.1, 0.2, 0.3]]]); + await new IndexPersistence(kv as never, vector, { bucketSize: 16 }).save(); + return vector; + } + + function present(scopes: string[]): boolean[] { + return scopes.map((scope) => kv.store.get(scope)?.has("data") ?? false); + } + + function load(vector: VectorIndex | null = new VectorIndex()) { + return new IndexPersistence(kv as never, vector, { bucketSize: 16 }).load(); + } + + it("removes generations no manifest names while migrating a legacy store", async () => { + const legacy = vectorWith([["obs_a", [0.1, 0.2, 0.3]]]); + await writeLegacyVectorSnapshot(kv, legacy, { generation: "idx_mfabcd12_aaaaaaaaaaaa" }); + await writeLegacyBm25Snapshot(kv); + const orphans = [...(await writeOrphan("bm25", ORPHAN_IDX)), ...(await writeOrphan("vectors", ORPHAN_OLD))]; + + const loaded = await load(); + + expect(loaded.state).toBe("migrated"); + expectSameVectors(loaded.vector, legacy); + expect(present(orphans)).toEqual([false, false, false, false]); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).toMatchObject({ v: 1, generations: 2, shards: 4, failed: 0 }); + }); + + it("removes orphans from a store that already uses buckets and leaves the buckets alone", async () => { + const vector = await writeBuckets(); + const orphans = await writeOrphan("bm25", ORPHAN_IDX); + + const loaded = await load(); + + expect(loaded.state).toBe("buckets"); + expectSameVectors(loaded.vector, vector); + expect(present(orphans)).toEqual([false, false]); + expect(kv.store.get(vectorBucketScope(0))?.has("obs_a")).toBe(true); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).toMatchObject({ generations: 1, shards: 2 }); + }); + + it("keeps every generation a surviving manifest names", async () => { + const legacy = vectorWith([["obs_a", [0.1, 0.2, 0.3]]]); + await writeLegacyVectorSnapshot(kv, legacy, { generation: "idx_mfabcd12_aaaaaaaaaaaa" }); + const named = [...kv.store.keys()].filter((scope) => scope.includes(":vectors:")); + const orphans = await writeOrphan("vectors", ORPHAN_IDX); + + await load(null); + + expect(named.length).toBeGreaterThan(0); + expect(present(named).every(Boolean)).toBe(true); + expect(await kv.get(INDEX_SCOPE, "vectors:manifest")).not.toBeNull(); + expect(present(orphans)).toEqual([false, false]); + }); + + it("deletes nothing when a manifest read fails", async () => { + await writeBuckets(); + const orphans = await writeOrphan("bm25", ORPHAN_IDX); + const baseGet = kv.get; + kv.get = (async (scope: string, key: string) => { + if (key === "data:manifest") throw new Error("engine busy"); + return baseGet(scope, key); + }) as typeof kv.get; + + await load(); + + kv.get = baseGet; + expect(present(orphans)).toEqual([true, true]); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).toBeNull(); + }); + + it("deletes nothing when a manifest names no generation", async () => { + await writeBuckets(); + const orphans = await writeOrphan("bm25", ORPHAN_IDX); + await kv.set(INDEX_SCOPE, "vectors:manifest", { v: 1, shards: "corrupt", chars: 0 }); + + await load(null); + + expect(present(orphans)).toEqual([true, true]); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).toBeNull(); + }); + + it("deletes nothing when a legacy-prefixed scope does not parse", async () => { + await writeBuckets(); + const orphans = await writeOrphan("bm25", ORPHAN_IDX); + await kv.set(`${INDEX_SCOPE}:bm25:something-else`, "data", "unknown"); + + await load(); + + expect(present(orphans)).toEqual([true, true]); + expect(kv.store.get(`${INDEX_SCOPE}:bm25:something-else`)?.get("data")).toBe("unknown"); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).toBeNull(); + }); + + it("deletes nothing when scopes cannot be listed, and sweeps on the next boot", async () => { + await writeBuckets(); + const orphans = await writeOrphan("bm25", ORPHAN_IDX); + const baseListGroups = kv.listGroups; + kv.listGroups = async () => { + throw new Error("state::list_groups unavailable"); + }; + + await load(); + expect(present(orphans)).toEqual([true, true]); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).toBeNull(); + + kv.listGroups = baseListGroups; + await load(); + expect(present(orphans)).toEqual([false, false]); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).not.toBeNull(); + }); + + it("records no completion when a delete fails, and finishes on the next boot", async () => { + await writeBuckets(); + const orphans = await writeOrphan("bm25", ORPHAN_IDX); + const baseDelete = kv.delete; + kv.delete = async (scope: string, key: string) => { + if (scope === orphans[1]) throw new Error("delete timed out"); + return baseDelete(scope, key); + }; + + await load(); + expect(present(orphans)).toEqual([false, true]); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).toBeNull(); + + kv.delete = baseDelete; + await load(); + expect(present(orphans)).toEqual([false, false]); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).toMatchObject({ shards: 1, failed: 0 }); + }); + + it("runs once: a later boot neither lists scopes nor deletes", async () => { + await writeBuckets(); + await writeOrphan("bm25", ORPHAN_IDX); + await load(); + const later = await writeOrphan("vectors", ORPHAN_OLD); + let listed = 0; + const baseListGroups = kv.listGroups; + kv.listGroups = async () => { + listed++; + return baseListGroups(); + }; + const opsBefore = kv.ops.length; + + await load(); + + expect(listed).toBe(0); + expect(kv.ops.slice(opsBefore).filter((o) => o.op === "delete")).toEqual([]); + expect(present(later)).toEqual([true, true]); + }); + + it("audits each sweep delete and the summary when index auditing is on", async () => { + const previous = process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST; + process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST = "1"; + try { + await writeBuckets(); + await writeOrphan("bm25", ORPHAN_IDX); + + await load(); + + const entries = await kv.list<{ operation: string; details: { action?: string; reason?: string } }>(currentAuditScope()); + const audited = entries.filter((entry) => entry.operation === "index_persist"); + expect(audited.filter((entry) => entry.details.reason === "orphan_generation_sweep")).toHaveLength(2); + expect(audited.some((entry) => entry.details.action === "sweep")).toBe(true); + } finally { + if (previous === undefined) delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST; + else process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST = previous; + } + }); +}); From 76b8bfd230200e90ce530b7c74fb6753e3311f50 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 4 Nov 2026 11:47:26 +0530 Subject: [PATCH 2/3] refactor(state): drop explanatory comments from the orphan sweep The tests and the pull request description cover the generation formats and the safety rules. --- src/state/index-persistence.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/state/index-persistence.ts b/src/state/index-persistence.ts index bfb7f845b3..31359f5b07 100644 --- a/src/state/index-persistence.ts +++ b/src/state/index-persistence.ts @@ -23,7 +23,6 @@ const BACKFILL_MARKER_KEY = "vectors:backfill"; const MEMORY_ID_PREFIX = "mem_"; const LEGACY_SHARD_SCOPE_PREFIXES = [`${KV.bm25Index}:bm25:`, `${KV.bm25Index}:vectors:`]; const LEGACY_SHARD_KEY = "data"; -// Both generation formats sharded saves ever wrote: `-<32 hex>`, then generateId("idx"). const LEGACY_SHARD_SUFFIX = /^((?:[0-9a-z]+-[0-9a-f]{32})|(?:idx_[0-9a-z]+_[0-9a-f]{12})):\d{5}$/; const ORPHAN_SWEEP_KEY = "legacy:sweep"; @@ -164,7 +163,6 @@ function isValidShardDescriptor( ); } -// undefined: not a legacy shard scope. null: under a legacy prefix but not a shape any save wrote. function legacyShardGeneration(scope: string): string | null | undefined { const prefix = LEGACY_SHARD_SCOPE_PREFIXES.find((candidate) => scope.startsWith(candidate)); if (!prefix) return undefined; @@ -877,8 +875,6 @@ export class IndexPersistence { await this.sweepOrphanedGenerations(); } - // Legacy cleanup deletes only shards a manifest still names. A generation stranded before the - // upgrade (#1115) is named by none, so it is found by listing scopes. Any doubt deletes nothing. private async sweepOrphanedGenerations(): Promise { try { if ((await this.kv.get(KV.bm25Index, ORPHAN_SWEEP_KEY)) != null) return; From f63235d56de5749068dc2f295c994ecbebdde409 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 4 Nov 2026 12:02:23 +0530 Subject: [PATCH 3/3] fix(state): keep the orphan sweep open while a manifest protects shards The sweep wrote its completion marker even when a legacy manifest still named some shard scopes. If those shards later lost their manifest, for example after a vector migration whose shard delete failed, the marker blocked every later sweep and they stayed on disk. The marker is now written only when every candidate scope was an orphan; otherwise the next boot looks again. Generations are also read from every manifest descriptor that has a scope, not only from descriptors that pass validation, so a malformed descriptor still protects its shard. --- src/state/index-persistence.ts | 8 +++++++- test/index-persistence.test.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/state/index-persistence.ts b/src/state/index-persistence.ts index 31359f5b07..65240646c2 100644 --- a/src/state/index-persistence.ts +++ b/src/state/index-persistence.ts @@ -903,6 +903,10 @@ export class IndexPersistence { logger.warn("index persistence: orphan sweep left shards behind; the next boot retries", summary); return; } + if (candidates.size > orphans.length) { + if (orphans.length > 0) logger.info("index persistence: swept orphaned legacy index generations", summary); + return; + } await this.kv.set(KV.bm25Index, ORPHAN_SWEEP_KEY, { v: 1, at: new Date(this.now()).toISOString(), ...summary }); await this.auditIndexPersistence("sweep", [statePath(KV.bm25Index, ORPHAN_SWEEP_KEY)], summary); logger.info("index persistence: swept orphaned legacy index generations", summary); @@ -919,7 +923,9 @@ export class IndexPersistence { const manifest = read.value; if (manifest == null) continue; const generations = typeof manifest === "object" && Array.isArray(manifest.shards) - ? manifest.shards.filter(isValidShardDescriptor).map((shard) => legacyShardGeneration(shard.scope)) + ? manifest.shards + .filter((shard) => typeof shard?.scope === "string" && shard.scope.length > 0) + .map((shard) => legacyShardGeneration(shard.scope)) : []; if (typeof manifest === "object" && typeof manifest.generation === "string") generations.push(manifest.generation); const usable = generations.filter((generation): generation is string => !!generation); diff --git a/test/index-persistence.test.ts b/test/index-persistence.test.ts index 7c5c7fdaae..a795990e2b 100644 --- a/test/index-persistence.test.ts +++ b/test/index-persistence.test.ts @@ -644,6 +644,21 @@ describe("IndexPersistence orphaned legacy generation sweep", () => { expect(present(orphans)).toEqual([false, false]); }); + it("stays open while a manifest protects shards, and sweeps them once the manifest is gone", async () => { + const legacy = vectorWith([["obs_a", [0.1, 0.2, 0.3]]]); + await writeLegacyVectorSnapshot(kv, legacy, { generation: "idx_mfabcd12_aaaaaaaaaaaa" }); + const named = [...kv.store.keys()].filter((scope) => scope.includes(":vectors:")); + + await load(null); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).toBeNull(); + + await kv.delete(INDEX_SCOPE, "vectors:manifest"); + await load(null); + + expect(present(named).some(Boolean)).toBe(false); + expect(await kv.get(INDEX_SCOPE, SWEEP_KEY)).not.toBeNull(); + }); + it("deletes nothing when a manifest read fails", async () => { await writeBuckets(); const orphans = await writeOrphan("bm25", ORPHAN_IDX);