From 026f4fe94d1dde980270f9d32df4a9b7fcb25916 Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Sun, 1 Nov 2026 07:30:32 +0800 Subject: [PATCH 1/8] fix: exclude unscoped memories from project-filtered recall Signed-off-by: reevesc88 --- 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/functions/search.ts | 1 + test/search-project-isolation.test.ts | 167 ++++++++++++++++++++++++++ 39 files changed, 241 insertions(+), 73 deletions(-) create mode 100644 test/search-project-isolation.test.ts 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/functions/search.ts b/src/functions/search.ts index 7e8bf9baf1..ce417d9db4 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -906,6 +906,7 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { : null if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) continue const session = await loadSession(r.sessionId) + if (projectFilter && memoryProject === null && !session) continue if (session) { if (projectFilter && memoryProject === null && session.project !== projectFilter) continue if (cwdFilter && session.cwd !== cwdFilter) continue diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts new file mode 100644 index 0000000000..88ff9bda7c --- /dev/null +++ b/test/search-project-isolation.test.ts @@ -0,0 +1,167 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +vi.mock("../src/logger.js", () => ({ + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, +})); +vi.mock("../src/functions/access-tracker.js", () => ({ + recordAccessBatch: vi.fn(), +})); + +import { + getSearchIndex, + registerSearchFunction, + setHybridRanker, +} from "../src/functions/search.js"; +import { memoryToObservation } from "../src/state/memory-utils.js"; +import { KV } from "../src/state/schema.js"; +import type { Memory, SearchResult, Session } from "../src/types.js"; + +const timestamp = "2026-01-01T00:00:00Z"; + +describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( + "mem::search project isolation via %s", + (ranking) => { + let records: Map>; + let memories: Memory[]; + let search: (data: Record) => Promise<{ results: SearchResult[] }>; + + const put = (scope: string, key: string, value: unknown) => { + if (!records.has(scope)) records.set(scope, new Map()); + records.get(scope)!.set(key, value); + }; + + const addMemory = (overrides: Partial = {}) => { + const memory: Memory = { + id: "mem_unscoped", + createdAt: timestamp, + updatedAt: timestamp, + type: "fact", + title: "isolation marker", + content: "isolation marker", + concepts: ["isolation"], + files: [], + sessionIds: [], + strength: 7, + version: 1, + isLatest: true, + ...overrides, + }; + memories.push(memory); + put(KV.memories, memory.id, memory); + getSearchIndex().add(memoryToObservation(memory), "memory"); + return memory; + }; + + const addSession = (project: string) => { + const session: Session = { + id: "session_existing", + project, + cwd: "/test/project", + startedAt: timestamp, + status: "completed", + observationCount: 0, + }; + put(KV.sessions, session.id, session); + return session; + }; + + const recall = async (overrides: Record = {}) => { + const result = await search({ + query: "isolation", + project: "project-a", + targetLayer: "memory", + agentId: "*", + ...overrides, + }); + return result.results.map((hit) => hit.observation.id); + }; + + beforeEach(() => { + records = new Map(); + memories = []; + getSearchIndex().clear(); + setHybridRanker(null); + if (ranking !== "keyword") { + setHybridRanker(async () => { + if (ranking === "hybrid-fallback") throw new Error("ranker unavailable"); + return memories.map((memory, index) => { + const observation = memoryToObservation(memory); + return { + observation, + sessionId: observation.sessionId, + combinedScore: 1 / (index + 1), + layer: "memory" as const, + }; + }); + }); + } + const kv = { + get: async (scope: string, key: string) => records.get(scope)?.get(key) ?? null, + list: async (scope: string) => [...(records.get(scope)?.values() ?? [])], + }; + const sdk = { + registerFunction: (_id: string, handler: typeof search) => { search = handler; }, + }; + registerSearchFunction(sdk as never, kv as never); + }); + + afterEach(() => { + setHybridRanker(null); + getSearchIndex().clear(); + vi.restoreAllMocks(); + }); + + it("excludes an unscoped memory with no session", async () => { + addMemory(); + expect(await recall()).toEqual([]); + }); + + it("excludes an unscoped memory with a dangling session reference", async () => { + addMemory({ sessionIds: ["session_missing"] }); + expect(await recall()).toEqual([]); + }); + + it("includes a legacy memory through its matching session", async () => { + const session = addSession("project-a"); + const memory = addMemory({ sessionIds: [session.id] }); + expect(await recall()).toEqual([memory.id]); + }); + + it("excludes a legacy memory belonging to another session project", async () => { + const session = addSession("project-b"); + addMemory({ sessionIds: [session.id] }); + expect(await recall()).toEqual([]); + }); + + it("includes an explicitly matching memory without a session", async () => { + const memory = addMemory({ project: "project-a" }); + expect(await recall()).toEqual([memory.id]); + }); + + it("rejects an explicit foreign project even when its session matches", async () => { + const session = addSession("project-a"); + addMemory({ project: "project-b", sessionIds: [session.id] }); + expect(await recall()).toEqual([]); + }); + + it("preserves unscoped memories when no project filter is requested", async () => { + const memory = addMemory(); + expect(await recall({ project: undefined })).toEqual([memory.id]); + }); + + it("fills the result limit after excluding an unscoped first candidate", async () => { + const unscoped = addMemory(); + const matching = addMemory({ id: "mem_matching", project: "project-a" }); + // Candidate ordering is external to the ownership filter under test. + vi.spyOn(getSearchIndex(), "search").mockReturnValue( + [unscoped, matching].map((memory, index) => ({ + obsId: memory.id, + sessionId: "memory", + score: 1 / (index + 1), + layer: "memory" as const, + })), + ); + expect(await recall({ limit: 1 })).toEqual([matching.id]); + }); + }, +); From c7db676e8d83871756f598cc99a4cc100b1b7ebb Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Sun, 1 Nov 2026 10:37:09 +0800 Subject: [PATCH 2/8] fix: refill recall candidates after project filtering Signed-off-by: reevesc88 --- src/functions/search.ts | 156 +++++++++++++------------- test/search-project-isolation.test.ts | 49 +++++++- 2 files changed, 126 insertions(+), 79 deletions(-) diff --git a/src/functions/search.ts b/src/functions/search.ts index ce417d9db4..6b1943f70e 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -824,7 +824,7 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { // queries return underfilled pages when same-agent matches // rank lower than cross-agent ones in the hybrid score. const filtering = !!(projectFilter || cwdFilter || filterAgentId) - const fetchLimit = filtering ? Math.max(effectiveLimit * 10, 100) : effectiveLimit + let fetchLimit = filtering ? Math.max(effectiveLimit * 10, 100) : effectiveLimit // Hybrid results carry the observation the ranker already loaded, // so the load pass below doesn't refetch every record it just // enriched. @@ -847,26 +847,6 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { } return true } - if (hybridRanker) { - try { - const hybrid = await hybridRanker(query, fetchLimit, targetLayer, authorizeGraph) - results = hybrid.map((r) => ({ - obsId: r.observation.id, - sessionId: r.sessionId, - score: r.combinedScore, - observation: r.observation, - layer: r.layer, - })) - } catch (err) { - logger.warn("hybrid ranking failed, falling back to keyword search", { - error: err instanceof Error ? err.message : String(err), - }) - results = idx.search(query, fetchLimit, targetLayer) - } - } else { - results = idx.search(query, fetchLimit, targetLayer) - } - // Resolve session -> project/cwd once per sessionId we touch. const sessionCache = new Map() const loadSession = async (sessionId: string): Promise => { @@ -885,63 +865,89 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { return proj } - // First pass: filter by session (sequential — benefits from session cache). - // Memory entries with a synthetic sessionId take a secondary KV.memories - // path so project filtering works correctly for them too. - // - // When agentId filtering is active we can't cap at effectiveLimit - // here — the second pass (post-load) is what drops cross-agent - // rows, and capping early would underfill the result page. Use - // fetchLimit as the upper bound in that case; the final - // truncation lives at the end of the second pass. - const earlyCap = filterAgentId ? fetchLimit : effectiveLimit - const candidates: typeof results = [] - for (const r of results) { - if (candidates.length >= earlyCap) break - const layer = r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) - if (!matchesSearchLayer(r.obsId, r.sessionId, targetLayer, layer)) continue - if (filtering) { - const memoryProject = projectFilter && layer === "memory" - ? await loadMemoryProject(r.obsId) - : null - if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) continue - const session = await loadSession(r.sessionId) - if (projectFilter && memoryProject === null && !session) continue - if (session) { - if (projectFilter && memoryProject === null && session.project !== projectFilter) continue - if (cwdFilter && session.cwd !== cwdFilter) continue + let enriched: SearchResult[] = [] + while (true) { + if (hybridRanker) { + try { + const hybrid = await hybridRanker(query, fetchLimit, targetLayer, authorizeGraph) + results = hybrid.map((r) => ({ + obsId: r.observation.id, + sessionId: r.sessionId, + score: r.combinedScore, + observation: r.observation, + layer: r.layer, + })) + } catch (err) { + logger.warn("hybrid ranking failed, falling back to keyword search", { + error: err instanceof Error ? err.message : String(err), + }) + results = idx.search(query, fetchLimit, targetLayer) } + } else { + results = idx.search(query, fetchLimit, targetLayer) } - candidates.push(r) - } - const obsResults = await Promise.all( - candidates.map(async (r) => { - if (r.observation) return { observation: r.observation, layer: r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) } - const obs = await kv - .get(KV.observations(r.sessionId), r.obsId) - .catch(() => null) - if (obs) return { observation: obs, layer: "observation" as const } - const mem = await kv - .get(KV.memories, r.obsId) - .catch(() => null) - return mem ? { observation: memoryToObservation(mem), layer: "memory" as const } : null - }) - ) - const enriched: SearchResult[] = [] - for (let i = 0; i < candidates.length; i++) { - const resolved = obsResults[i] - if (!resolved) continue - const obs = resolved.observation - if (!matchesSearchLayer(obs.id, obs.sessionId, targetLayer, resolved.layer)) continue - if (filterAgentId !== undefined && obs.agentId !== filterAgentId) continue - if (enriched.length >= effectiveLimit) break - enriched.push({ - observation: withoutObservationSource(obs), - layer: resolved.layer, - score: candidates[i].score, - sessionId: candidates[i].sessionId, - }) + // First pass: filter by session (sequential — benefits from session cache). + // Memory entries with a synthetic sessionId take a secondary KV.memories + // path so project filtering works correctly for them too. + // + // When agentId filtering is active we can't cap at effectiveLimit + // here — the second pass (post-load) is what drops cross-agent + // rows, and capping early would underfill the result page. Use + // fetchLimit as the upper bound in that case; the final + // truncation lives at the end of the second pass. + const earlyCap = filterAgentId ? fetchLimit : effectiveLimit + const candidates: typeof results = [] + for (const r of results) { + if (candidates.length >= earlyCap) break + const layer = r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) + if (!matchesSearchLayer(r.obsId, r.sessionId, targetLayer, layer)) continue + if (filtering) { + const memoryProject = projectFilter && layer === "memory" + ? await loadMemoryProject(r.obsId) + : null + if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) continue + const session = await loadSession(r.sessionId) + if (projectFilter && memoryProject === null && !session) continue + if (session) { + if (projectFilter && memoryProject === null && session.project !== projectFilter) continue + if (cwdFilter && session.cwd !== cwdFilter) continue + } + } + candidates.push(r) + } + + const obsResults = await Promise.all( + candidates.map(async (r) => { + if (r.observation) return { observation: r.observation, layer: r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) } + const obs = await kv + .get(KV.observations(r.sessionId), r.obsId) + .catch(() => null) + if (obs) return { observation: obs, layer: "observation" as const } + const mem = await kv + .get(KV.memories, r.obsId) + .catch(() => null) + return mem ? { observation: memoryToObservation(mem), layer: "memory" as const } : null + }) + ) + enriched = [] + for (let i = 0; i < candidates.length; i++) { + const resolved = obsResults[i] + if (!resolved) continue + const obs = resolved.observation + if (!matchesSearchLayer(obs.id, obs.sessionId, targetLayer, resolved.layer)) continue + if (filterAgentId !== undefined && obs.agentId !== filterAgentId) continue + if (enriched.length >= effectiveLimit) break + enriched.push({ + observation: withoutObservationSource(obs), + layer: resolved.layer, + score: candidates[i].score, + sessionId: candidates[i].sessionId, + }) + } + if (!filtering || enriched.length >= effectiveLimit || results.length < fetchLimit) break + // A full candidate window can hide valid matches behind rejected records. + fetchLimit *= 2 } const response = buildRecallResponse(enriched, format as RecallFormat, tokenBudget) diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index 88ff9bda7c..aeaa024f28 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -23,6 +23,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( (ranking) => { let records: Map>; let memories: Memory[]; + let hybridLimits: number[]; let search: (data: Record) => Promise<{ results: SearchResult[] }>; const put = (scope: string, key: string, value: unknown) => { @@ -79,10 +80,12 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( beforeEach(() => { records = new Map(); memories = []; + hybridLimits = []; getSearchIndex().clear(); setHybridRanker(null); if (ranking !== "keyword") { - setHybridRanker(async () => { + setHybridRanker(async (_query, limit) => { + hybridLimits.push(limit); if (ranking === "hybrid-fallback") throw new Error("ranker unavailable"); return memories.map((memory, index) => { const observation = memoryToObservation(memory); @@ -92,7 +95,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( combinedScore: 1 / (index + 1), layer: "memory" as const, }; - }); + }).slice(0, limit); }); } const kv = { @@ -149,12 +152,50 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( expect(await recall({ project: undefined })).toEqual([memory.id]); }); + const orderedKeywordResults = () => + vi.spyOn(getSearchIndex(), "search").mockImplementation((_query, limit) => + memories.slice(0, limit).map((memory, index) => ({ + obsId: memory.id, + sessionId: memory.sessionIds[0] ?? "memory", + score: 1 / (index + 1), + layer: "memory" as const, + })), + ); + + it("refills multiple full windows without returning excluded projects", async () => { + for (let i = 0; i < 205; i++) { + addMemory({ id: `mem_excluded_${i}`, project: i % 2 ? "project-b" : undefined }); + } + const matching = addMemory({ id: "mem_matching", project: "project-a" }); + orderedKeywordResults(); + expect(await recall({ limit: 1 })).toEqual([matching.id]); + }); + + it("refills after the loaded agent filter consumes a full window", async () => { + for (let i = 0; i < 100; i++) { + addMemory({ id: `mem_foreign_agent_${i}`, project: "project-a", agentId: "agent-b" }); + } + const matching = addMemory({ id: "mem_matching", project: "project-a", agentId: "agent-a" }); + orderedKeywordResults(); + expect(await recall({ limit: 1, agentId: "agent-a" })).toEqual([matching.id]); + }); + + it("stops refilling when the candidate source is exhausted", async () => { + for (let i = 0; i < 100; i++) addMemory({ id: `mem_unscoped_${i}` }); + const keyword = orderedKeywordResults(); + expect(await recall({ limit: 1 })).toEqual([]); + const requestedLimits = ranking === "hybrid" + ? hybridLimits + : keyword.mock.calls.map((call) => call[1]); + expect(requestedLimits).toEqual([100, 200]); + }); + it("fills the result limit after excluding an unscoped first candidate", async () => { const unscoped = addMemory(); const matching = addMemory({ id: "mem_matching", project: "project-a" }); // Candidate ordering is external to the ownership filter under test. - vi.spyOn(getSearchIndex(), "search").mockReturnValue( - [unscoped, matching].map((memory, index) => ({ + vi.spyOn(getSearchIndex(), "search").mockImplementation((_query, limit) => + [unscoped, matching].slice(0, limit).map((memory, index) => ({ obsId: memory.id, sessionId: "memory", score: 1 / (index + 1), From 680b719bc96991e90eba8a024ef1dfa04372fd27 Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Sun, 1 Nov 2026 10:46:38 +0800 Subject: [PATCH 3/8] fix: bound recall refill work and cache loaded candidates Signed-off-by: reevesc88 --- README.md | 2 ++ src/functions/search.ts | 46 ++++++++++++++++++--------- test/search-project-isolation.test.ts | 34 +++++++++++++++++++- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b371b080e4..dc293dab20 100644 --- a/README.md +++ b/README.md @@ -866,6 +866,8 @@ Fused with Reciprocal Rank Fusion (RRF, k=60) and session-diversified (max 3 res When a vector index is populated, `mem::search` (behind `memory_recall`) uses the hybrid BM25 + vector ranker. Without embeddings it uses BM25. `smart-search` can additionally fuse structural graph matches when graph data exists, including in keyless mode. Lesson recall runs on a dedicated in-memory BM25 index instead of scanning the whole corpus per query. Superseded memory versions are excluded from every recall path; the version chain keeps their history. +Filtered `memory_recall` expands its ranked candidate window until it fills the requested result limit, exhausts the candidate source, or reaches 1,000 candidates. This bound limits work for queries with many unrelated matches. Results can contain fewer items when qualifying records rank below that budget. + Vectors survive a crash or force-kill. The vector index is saved in buckets at most every `AGENTMEMORY_INDEX_SAVE_INTERVAL_MS` (10 minutes). Every vector added or removed in between is also written right away to a small pending log in the state store, and the next start replays it without calling the embedding provider. Each successful save empties the log. For older documents without vectors, `AGENTMEMORY_VECTOR_BACKFILL=all` opts into a bounded backfill at startup, capped by `AGENTMEMORY_VECTOR_BACKFILL_MAX` (500). Keep the opt-in set across restarts to process more documents. See [source and vector recovery](docs/reliability/source-and-vector-recovery.md). `/agentmemory/status` and the viewer show the pending log size and the backfill state. Keyless installs write nothing. BM25 tokenizes Greek, Cyrillic, Hebrew, Arabic, and accented Latin out of the box. For Chinese / Japanese / Korean memories, install the optional segmenters (`npm install @node-rs/jieba tiny-segmenter`) to split CJK runs into word-level tokens; without them, agentmemory soft-falls to whole-run tokenization and prints a one-time hint on stderr. diff --git a/src/functions/search.ts b/src/functions/search.ts index 6b1943f70e..fff88f0ad2 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -750,6 +750,7 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { } const targetLayer = data.targetLayer ?? 'all' const MAX_LIMIT = 100 + const MAX_CANDIDATES = 1000 let effectiveLimit = 20 if (data.limit !== undefined) { if (!Number.isInteger(data.limit) || data.limit < 1) { @@ -865,6 +866,10 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { return proj } + const observationCache = new Map>() let enriched: SearchResult[] = [] while (true) { if (hybridRanker) { @@ -917,19 +922,30 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { candidates.push(r) } - const obsResults = await Promise.all( - candidates.map(async (r) => { - if (r.observation) return { observation: r.observation, layer: r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) } - const obs = await kv - .get(KV.observations(r.sessionId), r.obsId) - .catch(() => null) - if (obs) return { observation: obs, layer: "observation" as const } - const mem = await kv - .get(KV.memories, r.obsId) - .catch(() => null) - return mem ? { observation: memoryToObservation(mem), layer: "memory" as const } : null - }) - ) + const obsResults: Array<{ observation: CompressedObservation; layer: SearchResultLayer } | null> = [] + for (let offset = 0; offset < candidates.length; offset += 10) { + obsResults.push(...await Promise.all( + candidates.slice(offset, offset + 10).map((r) => { + const key = JSON.stringify([r.obsId, r.sessionId]) + let pending = observationCache.get(key) + if (!pending) { + pending = (async () => { + if (r.observation) return { observation: r.observation, layer: r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) } + const obs = await kv + .get(KV.observations(r.sessionId), r.obsId) + .catch(() => null) + if (obs) return { observation: obs, layer: "observation" as const } + const mem = await kv + .get(KV.memories, r.obsId) + .catch(() => null) + return mem ? { observation: memoryToObservation(mem), layer: "memory" as const } : null + })() + observationCache.set(key, pending) + } + return pending + }) + )) + } enriched = [] for (let i = 0; i < candidates.length; i++) { const resolved = obsResults[i] @@ -945,9 +961,9 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { sessionId: candidates[i].sessionId, }) } - if (!filtering || enriched.length >= effectiveLimit || results.length < fetchLimit) break + if (!filtering || enriched.length >= effectiveLimit || results.length < fetchLimit || fetchLimit >= MAX_CANDIDATES) break // A full candidate window can hide valid matches behind rejected records. - fetchLimit *= 2 + fetchLimit = Math.min(fetchLimit * 2, MAX_CANDIDATES) } const response = buildRecallResponse(enriched, format as RecallFormat, tokenBudget) diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index aeaa024f28..0a513020e9 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -24,6 +24,9 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( let records: Map>; let memories: Memory[]; let hybridLimits: number[]; + let memoryReads: string[]; + let activeObservationReads: number; + let maxObservationReads: number; let search: (data: Record) => Promise<{ results: SearchResult[] }>; const put = (scope: string, key: string, value: unknown) => { @@ -81,6 +84,9 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( records = new Map(); memories = []; hybridLimits = []; + memoryReads = []; + activeObservationReads = 0; + maxObservationReads = 0; getSearchIndex().clear(); setHybridRanker(null); if (ranking !== "keyword") { @@ -99,7 +105,16 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( }); } const kv = { - get: async (scope: string, key: string) => records.get(scope)?.get(key) ?? null, + get: async (scope: string, key: string) => { + if (scope === KV.observations("memory")) { + memoryReads.push(key); + activeObservationReads++; + maxObservationReads = Math.max(maxObservationReads, activeObservationReads); + await Promise.resolve(); + activeObservationReads--; + } + return records.get(scope)?.get(key) ?? null; + }, list: async (scope: string) => [...(records.get(scope)?.values() ?? [])], }; const sdk = { @@ -178,6 +193,23 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( const matching = addMemory({ id: "mem_matching", project: "project-a", agentId: "agent-a" }); orderedKeywordResults(); expect(await recall({ limit: 1, agentId: "agent-a" })).toEqual([matching.id]); + if (ranking !== "hybrid") { + expect(memoryReads.filter((id) => id === "mem_foreign_agent_0")).toHaveLength(1); + expect(maxObservationReads).toBeLessThanOrEqual(10); + } + }); + + it("bounds candidate scanning even when every record is rejected", async () => { + for (let i = 0; i < 1001; i++) { + addMemory({ id: `mem_foreign_${i}`, project: "project-b" }); + } + addMemory({ id: "mem_beyond_budget", project: "project-a" }); + const keyword = orderedKeywordResults(); + expect(await recall({ limit: 1 })).toEqual([]); + const requestedLimits = ranking === "hybrid" + ? hybridLimits + : keyword.mock.calls.map((call) => call[1]); + expect(requestedLimits).toEqual([100, 200, 400, 800, 1000]); }); it("stops refilling when the candidate source is exhausted", async () => { From 5445ccf5f29d851ec2ea10b1e6d07ae3a8c293da Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Sun, 1 Nov 2026 10:52:44 +0800 Subject: [PATCH 4/8] fix: reject directory orphans and skip unloadable recall hits Signed-off-by: reevesc88 --- src/functions/search.ts | 49 +++++++++++++-------------- test/search-project-isolation.test.ts | 26 ++++++++++++++ 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/functions/search.ts b/src/functions/search.ts index fff88f0ad2..dccbf80a12 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -896,12 +896,8 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { // Memory entries with a synthetic sessionId take a secondary KV.memories // path so project filtering works correctly for them too. // - // When agentId filtering is active we can't cap at effectiveLimit - // here — the second pass (post-load) is what drops cross-agent - // rows, and capping early would underfill the result page. Use - // fetchLimit as the upper bound in that case; the final - // truncation lives at the end of the second pass. - const earlyCap = filterAgentId ? fetchLimit : effectiveLimit + // Missing records and agent mismatches must not consume result slots. + const earlyCap = fetchLimit const candidates: typeof results = [] for (const r of results) { if (candidates.length >= earlyCap) break @@ -914,6 +910,7 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) continue const session = await loadSession(r.sessionId) if (projectFilter && memoryProject === null && !session) continue + if (cwdFilter && layer === "observation" && !session) continue if (session) { if (projectFilter && memoryProject === null && session.project !== projectFilter) continue if (cwdFilter && session.cwd !== cwdFilter) continue @@ -922,10 +919,11 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { candidates.push(r) } - const obsResults: Array<{ observation: CompressedObservation; layer: SearchResultLayer } | null> = [] - for (let offset = 0; offset < candidates.length; offset += 10) { - obsResults.push(...await Promise.all( - candidates.slice(offset, offset + 10).map((r) => { + enriched = [] + for (let offset = 0; offset < candidates.length && enriched.length < effectiveLimit; offset += 10) { + const batch = candidates.slice(offset, offset + 10) + const obsResults = await Promise.all( + batch.map((r) => { const key = JSON.stringify([r.obsId, r.sessionId]) let pending = observationCache.get(key) if (!pending) { @@ -944,22 +942,21 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { } return pending }) - )) - } - enriched = [] - for (let i = 0; i < candidates.length; i++) { - const resolved = obsResults[i] - if (!resolved) continue - const obs = resolved.observation - if (!matchesSearchLayer(obs.id, obs.sessionId, targetLayer, resolved.layer)) continue - if (filterAgentId !== undefined && obs.agentId !== filterAgentId) continue - if (enriched.length >= effectiveLimit) break - enriched.push({ - observation: withoutObservationSource(obs), - layer: resolved.layer, - score: candidates[i].score, - sessionId: candidates[i].sessionId, - }) + ) + for (let i = 0; i < batch.length; i++) { + const resolved = obsResults[i] + if (!resolved) continue + const obs = resolved.observation + if (!matchesSearchLayer(obs.id, obs.sessionId, targetLayer, resolved.layer)) continue + if (filterAgentId !== undefined && obs.agentId !== filterAgentId) continue + if (enriched.length >= effectiveLimit) break + enriched.push({ + observation: withoutObservationSource(obs), + layer: resolved.layer, + score: batch[i].score, + sessionId: batch[i].sessionId, + }) + } } if (!filtering || enriched.length >= effectiveLimit || results.length < fetchLimit || fetchLimit >= MAX_CANDIDATES) break // A full candidate window can hide valid matches behind rejected records. diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index 0a513020e9..bdbc48b9ac 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -222,6 +222,32 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( expect(requestedLimits).toEqual([100, 200]); }); + it("rejects orphaned observations when a directory filter is requested", async () => { + const orphan = addMemory({ id: "obs_orphan", sessionIds: ["session_missing"] }); + const observation = memoryToObservation(orphan); + getSearchIndex().add(observation, "observation"); + put(KV.observations("session_missing"), orphan.id, observation); + if (ranking === "hybrid") { + setHybridRanker(async () => [{ observation, sessionId: "session_missing", combinedScore: 1, layer: "observation" }]); + } + expect(await recall({ project: undefined, cwd: "/test/project", targetLayer: "all" })).toEqual([]); + }); + + it("preserves sessionless memories when a directory filter is requested", async () => { + const memory = addMemory({ project: "project-a" }); + expect(await recall({ cwd: "/test/project" })).toEqual([memory.id]); + }); + + it("does not let an unloadable observation consume the result limit", async () => { + const session = addSession("project-a"); + const matching = addMemory({ id: "mem_matching", project: "project-a" }); + vi.spyOn(getSearchIndex(), "search").mockImplementation((_query, limit) => [ + { obsId: "obs_missing", sessionId: session.id, score: 2, layer: "observation" as const }, + { obsId: matching.id, sessionId: "memory", score: 1, layer: "memory" as const }, + ].slice(0, limit)); + expect(await recall({ limit: 1, targetLayer: "all" })).toEqual([matching.id]); + }); + it("fills the result limit after excluding an unscoped first candidate", async () => { const unscoped = addMemory(); const matching = addMemory({ id: "mem_matching", project: "project-a" }); From d386c23f95ee6104666d7ba1c8290b06c13f9475 Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Sun, 1 Nov 2026 11:01:12 +0800 Subject: [PATCH 5/8] fix: stop filtering recall candidates after a full result batch Signed-off-by: reevesc88 --- src/functions/search.ts | 49 ++++++++++++--------------- test/search-project-isolation.test.ts | 29 ++++++++++++++++ 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/functions/search.ts b/src/functions/search.ts index dccbf80a12..174e9386ee 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -892,36 +892,29 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { results = idx.search(query, fetchLimit, targetLayer) } - // First pass: filter by session (sequential — benefits from session cache). - // Memory entries with a synthetic sessionId take a secondary KV.memories - // path so project filtering works correctly for them too. - // - // Missing records and agent mismatches must not consume result slots. - const earlyCap = fetchLimit - const candidates: typeof results = [] - for (const r of results) { - if (candidates.length >= earlyCap) break - const layer = r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) - if (!matchesSearchLayer(r.obsId, r.sessionId, targetLayer, layer)) continue - if (filtering) { - const memoryProject = projectFilter && layer === "memory" - ? await loadMemoryProject(r.obsId) - : null - if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) continue - const session = await loadSession(r.sessionId) - if (projectFilter && memoryProject === null && !session) continue - if (cwdFilter && layer === "observation" && !session) continue - if (session) { - if (projectFilter && memoryProject === null && session.project !== projectFilter) continue - if (cwdFilter && session.cwd !== cwdFilter) continue + // A full page must stop further session lookups as well as record loads. + enriched = [] + for (let offset = 0; offset < Math.min(results.length, fetchLimit) && enriched.length < effectiveLimit; offset += 10) { + const candidates: typeof results = [] + for (const r of results.slice(offset, Math.min(offset + 10, fetchLimit))) { + const layer = r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) + if (!matchesSearchLayer(r.obsId, r.sessionId, targetLayer, layer)) continue + if (filtering) { + const memoryProject = projectFilter && layer === "memory" + ? await loadMemoryProject(r.obsId) + : null + if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) continue + const session = await loadSession(r.sessionId) + if (projectFilter && memoryProject === null && !session) continue + if (cwdFilter && layer === "observation" && !session) continue + if (session) { + if (projectFilter && memoryProject === null && session.project !== projectFilter) continue + if (cwdFilter && session.cwd !== cwdFilter) continue + } } + candidates.push(r) } - candidates.push(r) - } - - enriched = [] - for (let offset = 0; offset < candidates.length && enriched.length < effectiveLimit; offset += 10) { - const batch = candidates.slice(offset, offset + 10) + const batch = candidates const obsResults = await Promise.all( batch.map((r) => { const key = JSON.stringify([r.obsId, r.sessionId]) diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index bdbc48b9ac..49c5f36842 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -25,6 +25,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( let memories: Memory[]; let hybridLimits: number[]; let memoryReads: string[]; + let sessionReads: string[]; let activeObservationReads: number; let maxObservationReads: number; let search: (data: Record) => Promise<{ results: SearchResult[] }>; @@ -85,6 +86,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( memories = []; hybridLimits = []; memoryReads = []; + sessionReads = []; activeObservationReads = 0; maxObservationReads = 0; getSearchIndex().clear(); @@ -106,6 +108,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( } const kv = { get: async (scope: string, key: string) => { + if (scope === KV.sessions) sessionReads.push(key); if (scope === KV.observations("memory")) { memoryReads.push(key); activeObservationReads++; @@ -222,6 +225,32 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( expect(requestedLimits).toEqual([100, 200]); }); + it("stops session filtering after a bounded batch fills the page", async () => { + const observations = []; + for (let i = 0; i < 100; i++) { + const session = { ...addSession("project-a"), id: `session_${i}` }; + put(KV.sessions, session.id, session); + const memory = addMemory({ id: `obs_valid_${i}`, sessionIds: [session.id] }); + const observation = memoryToObservation(memory); + observations.push(observation); + getSearchIndex().add(observation, "observation"); + put(KV.observations(session.id), observation.id, observation); + } + vi.spyOn(getSearchIndex(), "search").mockImplementation((_query, limit) => + observations.slice(0, limit).map((observation, i) => ({ + obsId: observation.id, sessionId: observation.sessionId, score: 1 / (i + 1), layer: "observation" as const, + })), + ); + if (ranking === "hybrid") { + setHybridRanker(async (_query, limit) => observations.slice(0, limit).map((observation, i) => ({ + observation, sessionId: observation.sessionId, combinedScore: 1 / (i + 1), layer: "observation" as const, + }))); + } + expect(await recall({ limit: 1, targetLayer: "observation" })).toEqual(["obs_valid_0"]); + expect(sessionReads.length).toBeLessThanOrEqual(10); + expect(sessionReads).not.toContain("session_10"); + }); + it("rejects orphaned observations when a directory filter is requested", async () => { const orphan = addMemory({ id: "obs_orphan", sessionIds: ["session_missing"] }); const observation = memoryToObservation(orphan); From c31c982c2b34774f1253b74f43f6bef25f073522 Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Sun, 1 Nov 2026 11:07:03 +0800 Subject: [PATCH 6/8] fix: reuse recall request work across candidate windows --- src/functions/search.ts | 69 +++++++++++++-------------- src/index.ts | 4 +- src/state/hybrid-search.ts | 20 ++++++-- test/hybrid-search.test.ts | 21 +++++++- test/recall-layer.test.ts | 6 +-- test/search-project-isolation.test.ts | 32 +++++++++++++ 6 files changed, 107 insertions(+), 45 deletions(-) diff --git a/src/functions/search.ts b/src/functions/search.ts index dccbf80a12..5cb14bfa2c 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -1,5 +1,6 @@ import type { IIIClient } from 'iii-sdk' import type { GraphObservationAuthorization } from "./graph-retrieval.js"; +import type { HybridSearchRequest } from "../state/hybrid-search.js"; import type { IndexPersistenceStatus } from "../state/index-persistence.js"; import type { CompressedObservation, Memory, SearchResult, Session } from '../types.js' import { KV } from '../state/schema.js' @@ -33,6 +34,7 @@ type HybridRanker = ( limit: number, targetLayer?: SearchLayer, authorizeGraph?: GraphObservationAuthorization, + request?: HybridSearchRequest, ) => Promise> let hybridRanker: HybridRanker | null = null @@ -849,12 +851,12 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { return true } // Resolve session -> project/cwd once per sessionId we touch. - const sessionCache = new Map() + const sessionCache = new Map>() const loadSession = async (sessionId: string): Promise => { if (sessionCache.has(sessionId)) return sessionCache.get(sessionId)! - const s = await kv.get(KV.sessions, sessionId) - sessionCache.set(sessionId, s ?? null) - return s ?? null + const pending = kv.get(KV.sessions, sessionId).then((session) => session ?? null) + sessionCache.set(sessionId, pending) + return pending } const memoryProjectCache = new Map() @@ -870,11 +872,13 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { observation: CompressedObservation layer: SearchResultLayer } | null>>() + const hybridRequest: HybridSearchRequest = {} let enriched: SearchResult[] = [] while (true) { + hybridRequest.candidateCount = undefined if (hybridRanker) { try { - const hybrid = await hybridRanker(query, fetchLimit, targetLayer, authorizeGraph) + const hybrid = await hybridRanker(query, fetchLimit, targetLayer, authorizeGraph, hybridRequest) results = hybrid.map((r) => ({ obsId: r.observation.id, sessionId: r.sessionId, @@ -883,6 +887,7 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { layer: r.layer, })) } catch (err) { + hybridRequest.candidateCount = undefined logger.warn("hybrid ranking failed, falling back to keyword search", { error: err instanceof Error ? err.message : String(err), }) @@ -892,38 +897,29 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { results = idx.search(query, fetchLimit, targetLayer) } - // First pass: filter by session (sequential — benefits from session cache). - // Memory entries with a synthetic sessionId take a secondary KV.memories - // path so project filtering works correctly for them too. - // - // Missing records and agent mismatches must not consume result slots. - const earlyCap = fetchLimit - const candidates: typeof results = [] - for (const r of results) { - if (candidates.length >= earlyCap) break - const layer = r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) - if (!matchesSearchLayer(r.obsId, r.sessionId, targetLayer, layer)) continue - if (filtering) { - const memoryProject = projectFilter && layer === "memory" - ? await loadMemoryProject(r.obsId) - : null - if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) continue - const session = await loadSession(r.sessionId) - if (projectFilter && memoryProject === null && !session) continue - if (cwdFilter && layer === "observation" && !session) continue - if (session) { - if (projectFilter && memoryProject === null && session.project !== projectFilter) continue - if (cwdFilter && session.cwd !== cwdFilter) continue - } - } - candidates.push(r) - } - enriched = [] - for (let offset = 0; offset < candidates.length && enriched.length < effectiveLimit; offset += 10) { - const batch = candidates.slice(offset, offset + 10) + let batchSize = Math.min(10, effectiveLimit) + for (let offset = 0; offset < results.length && enriched.length < effectiveLimit;) { + const batch = results.slice(offset, offset + batchSize) + offset += batch.length const obsResults = await Promise.all( - batch.map((r) => { + batch.map(async (r) => { + const layer = r.layer ?? idx.layerOf(r.obsId) ?? getSearchResultLayer(r.obsId, r.sessionId) + if (!matchesSearchLayer(r.obsId, r.sessionId, targetLayer, layer)) return null + if (filtering) { + const memoryProject = projectFilter && layer === "memory" + ? await loadMemoryProject(r.obsId) + : null + if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) return null + const session = await loadSession(r.sessionId) + if (projectFilter && memoryProject === null && !session) return null + if (cwdFilter && layer === "observation" && !session) return null + if (session) { + if (projectFilter && memoryProject === null && session.project !== projectFilter) return null + if (cwdFilter && session.cwd !== cwdFilter) return null + } + } + const key = JSON.stringify([r.obsId, r.sessionId]) let pending = observationCache.get(key) if (!pending) { @@ -957,8 +953,9 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { sessionId: batch[i].sessionId, }) } + batchSize = Math.min(10, Math.max(effectiveLimit - enriched.length, batchSize * 2)) } - if (!filtering || enriched.length >= effectiveLimit || results.length < fetchLimit || fetchLimit >= MAX_CANDIDATES) break + if (!filtering || enriched.length >= effectiveLimit || (hybridRequest.candidateCount ?? results.length) < fetchLimit || fetchLimit >= MAX_CANDIDATES) break // A full candidate window can hide valid matches behind rejected records. fetchLimit = Math.min(fetchLimit * 2, MAX_CANDIDATES) } diff --git a/src/index.ts b/src/index.ts index 9727dcc911..495e4adae6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -438,8 +438,8 @@ async function main() { graphWeight, ); - const hybridRanker = (query: string, limit: number, targetLayer?: SearchLayer, authorizeGraph?: Parameters[3]) => - hybridSearch.search(query, limit, targetLayer, authorizeGraph); + const hybridRanker = (query: string, limit: number, targetLayer?: SearchLayer, authorizeGraph?: Parameters[3], request?: Parameters[4]) => + hybridSearch.search(query, limit, targetLayer, authorizeGraph, request); registerSmartSearchFunction(sdk, kv, hybridRanker); setHybridRanker(hybridRanker); registerRecentSearchesSweepFunction(sdk, kv); diff --git a/src/state/hybrid-search.ts b/src/state/hybrid-search.ts index 8a1ed46291..60646a81a8 100644 --- a/src/state/hybrid-search.ts +++ b/src/state/hybrid-search.ts @@ -24,6 +24,11 @@ import { logger } from "../logger.js"; const RRF_K = 60; const GRAPH_BUDGET_MS = 500; +export interface HybridSearchRequest { + embedding?: { query: string; value: Promise }; + candidateCount?: number; +} + export class HybridSearch { private graphRetrieval: GraphRetrieval; private lastGraphTimeoutWarningAt = -Infinity; @@ -41,8 +46,8 @@ export class HybridSearch { this.graphRetrieval = new GraphRetrieval(kv, (id) => this.bm25.layerOf(id), (id) => this.bm25.sessionOf(id)); } - async search(query: string, limit = 20, targetLayer: SearchLayer = "all", authorizeGraph?: GraphObservationAuthorization): Promise { - return this.tripleStreamSearch(query, limit, targetLayer, this.graphRetrieval.createQuery(authorizeGraph)); + async search(query: string, limit = 20, targetLayer: SearchLayer = "all", authorizeGraph?: GraphObservationAuthorization, request?: HybridSearchRequest): Promise { + return this.tripleStreamSearch(query, limit, targetLayer, this.graphRetrieval.createQuery(authorizeGraph), undefined, true, request); } async searchWithExpansion( @@ -95,6 +100,7 @@ export class HybridSearch { graphQuery: GraphQuery, entityHints?: string[], applyReranking = true, + request?: HybridSearchRequest, ): Promise { const retrievalLimit = this.rerankEnabled ? Math.max(limit, 20) : limit; const bm25Results = this.bm25.search(query, retrievalLimit * 2, targetLayer); @@ -108,7 +114,14 @@ export class HybridSearch { if (this.vector && this.embeddingProvider && this.vector.size > 0) { try { - queryEmbedding = await this.embeddingProvider.embed(query); + if (request) { + if (!request.embedding || request.embedding.query !== query) { + request.embedding = { query, value: this.embeddingProvider.embed(query) }; + } + queryEmbedding = await request.embedding.value; + } else { + queryEmbedding = await this.embeddingProvider.embed(query); + } vectorResults = this.vector.search(queryEmbedding, retrievalLimit * 2, targetLayer, (id) => this.bm25.layerOf(id)); } catch { // fall through to BM25-only @@ -234,6 +247,7 @@ export class HybridSearch { combinedScore, })); + if (request) request.candidateCount = combined.length; const retrievalDepth = Math.max(limit, 20); const diversified = this.diversifyBySession(combined, retrievalDepth); const enriched = await this.enrichResults(diversified, retrievalDepth); diff --git a/test/hybrid-search.test.ts b/test/hybrid-search.test.ts index 4d921fd1cb..465b970cd8 100644 --- a/test/hybrid-search.test.ts +++ b/test/hybrid-search.test.ts @@ -1,5 +1,7 @@ -import { describe, it, expect, beforeEach } from "vitest"; +import { describe, it, expect, beforeEach, vi } from "vitest"; +import { VectorIndex } from "../src/state/vector-index.js"; import { HybridSearch } from "../src/state/hybrid-search.js"; +import { KV } from "../src/state/schema.js"; import { SearchIndex } from "../src/state/search-index.js"; import type { CompressedObservation, EmbeddingProvider } from "../src/types.js"; @@ -52,6 +54,23 @@ describe("HybridSearch", () => { kv = mockKV(); }); + it("reuses query embeddings only within a recall request", async () => { + const obs = makeObs(); + bm25.add(obs); + await kv.set(KV.observations(obs.sessionId), obs.id, obs); + const vector = new VectorIndex(); + vector.add(obs.id, obs.sessionId, new Float32Array([1, 0])); + const embed = vi.fn(async () => new Float32Array([1, 0])); + const provider = { name: "test", dimensions: 2, embed, embedBatch: vi.fn() }; + const hybrid = new HybridSearch(bm25, vector, provider, kv as never, 0.4, 0.6, 0); + const request = {}; + await hybrid.search("auth", 100, "all", undefined, request); + await hybrid.search("auth", 200, "all", undefined, request); + expect(embed).toHaveBeenCalledTimes(1); + await hybrid.search("auth", 100, "all", undefined, {}); + expect(embed).toHaveBeenCalledTimes(2); + }); + it("returns BM25-only results when no vector index is provided", async () => { const obs = makeObs({ id: "obs_1", sessionId: "ses_1" }); bm25.add(obs); diff --git a/test/recall-layer.test.ts b/test/recall-layer.test.ts index ae87827a67..37309555c7 100644 --- a/test/recall-layer.test.ts +++ b/test/recall-layer.test.ts @@ -11,7 +11,7 @@ import { setVectorIndex, } from "../src/functions/search.js"; import { registerSmartSearchFunction } from "../src/functions/smart-search.js"; -import { HybridSearch } from "../src/state/hybrid-search.js"; +import { HybridSearch, type HybridSearchRequest } from "../src/state/hybrid-search.js"; import { memoryToObservation } from "../src/state/memory-utils.js"; import { KV } from "../src/state/schema.js"; import { SearchIndex } from "../src/state/search-index.js"; @@ -60,7 +60,7 @@ function fixture() { const vector = new VectorIndex(); const embedding = { name: "fixed", dimensions: 2, embed: async () => new Float32Array([1, 0]), embedBatch: async () => [] }; const hybrid = new HybridSearch(index, vector, embedding, kv as never, 0.4, 0.6, 0); - const search = vi.fn((query: string, limit: number, targetLayer?: "all" | "memory" | "observation", authorizeGraph?: GraphObservationAuthorization) => hybrid.search(query, limit, targetLayer, authorizeGraph)); + const search = vi.fn((query: string, limit: number, targetLayer?: "all" | "memory" | "observation", authorizeGraph?: GraphObservationAuthorization, request?: HybridSearchRequest) => hybrid.search(query, limit, targetLayer, authorizeGraph, request)); registerSearchFunction(sdk as never, kv as never); registerSmartSearchFunction(sdk as never, kv as never, search); return { @@ -120,7 +120,7 @@ describe("recall layer selection", () => { setHybridRanker(f.search); const recall = await f.call("mem::search", { query: "quasar policy", limit: 1, targetLayer: "memory" }); expect(recall.results[0]).toMatchObject({ layer: "memory", observation: { id: "mem_decision" } }); - expect(f.search).toHaveBeenLastCalledWith("quasar policy", 1, "memory", expect.any(Function)); + expect(f.search).toHaveBeenLastCalledWith("quasar policy", 1, "memory", expect.any(Function), expect.any(Object)); const smart = await f.call("mem::smart-search", { query: "quasar policy", limit: 1, targetLayer: "memory" }); expect(smart.results).toEqual([expect.objectContaining({ obsId: "mem_decision", layer: "memory" })]); expect(smart).not.toHaveProperty("lessons"); diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index bdbc48b9ac..6e39a8f99c 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -12,6 +12,7 @@ import { registerSearchFunction, setHybridRanker, } from "../src/functions/search.js"; +import { HybridSearch } from "../src/state/hybrid-search.js"; import { memoryToObservation } from "../src/state/memory-utils.js"; import { KV } from "../src/state/schema.js"; import type { Memory, SearchResult, Session } from "../src/types.js"; @@ -25,6 +26,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( let memories: Memory[]; let hybridLimits: number[]; let memoryReads: string[]; + let sessionReads: string[]; let activeObservationReads: number; let maxObservationReads: number; let search: (data: Record) => Promise<{ results: SearchResult[] }>; @@ -85,6 +87,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( memories = []; hybridLimits = []; memoryReads = []; + sessionReads = []; activeObservationReads = 0; maxObservationReads = 0; getSearchIndex().clear(); @@ -106,6 +109,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( } const kv = { get: async (scope: string, key: string) => { + if (scope === KV.sessions) sessionReads.push(key); if (scope === KV.observations("memory")) { memoryReads.push(key); activeObservationReads++; @@ -177,6 +181,34 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( })), ); + it.each(["project", "directory"])("stops session checks once a %s-filtered page is full", async (filter) => { + for (let i = 0; i < 100; i++) { + const session = { ...addSession("project-a"), id: `session_${i}` }; + put(KV.sessions, session.id, session); + addMemory({ id: `mem_${i}`, sessionIds: [session.id] }); + } + orderedKeywordResults(); + expect(await recall({ limit: 1, ...(filter === "directory" ? { project: undefined, cwd: "/test/project" } : {}) })).toEqual(["mem_0"]); + expect(sessionReads).toEqual(["session_0"]); + }); + + if (ranking === "hybrid") { + it("expands a real hybrid window shortened by a missing record", async () => { + for (let i = 0; i < 100; i++) { + addMemory({ id: `mem_foreign_${i.toString().padStart(3, "0")}`, project: "project-b" }); + } + records.get(KV.memories)!.delete("mem_foreign_000"); + const matching = addMemory({ id: "mem_matching", project: "project-a" }); + const hybrid = new HybridSearch(getSearchIndex(), null, null, { + get: async (scope: string, key: string) => records.get(scope)?.get(key) ?? null, + } as never, 0.4, 0.6, 0); + const ranker = vi.fn(hybrid.search.bind(hybrid)); + setHybridRanker(ranker); + expect(await recall({ limit: 1 })).toEqual([matching.id]); + expect(ranker.mock.calls.map((call) => call[1])).toEqual([100, 200]); + }); + } + it("refills multiple full windows without returning excluded projects", async () => { for (let i = 0; i < 205; i++) { addMemory({ id: `mem_excluded_${i}`, project: i % 2 ? "project-b" : undefined }); From ec82f6fce4b20c1e0460263e90700ce313ef3b80 Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Sun, 1 Nov 2026 11:25:46 +0800 Subject: [PATCH 7/8] fix: preserve recall results through transient retrieval failures Signed-off-by: reevesc88 --- src/functions/search.ts | 9 +++++--- src/state/hybrid-search.ts | 8 ++++++- test/hybrid-search.test.ts | 23 +++++++++++++++++++ test/search-project-isolation.test.ts | 33 +++++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/functions/search.ts b/src/functions/search.ts index 8d6cb2193d..f674c1c1bb 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -874,9 +874,10 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { } | null>>() const hybridRequest: HybridSearchRequest = {} let enriched: SearchResult[] = [] + let useHybridRanker = hybridRanker !== null while (true) { hybridRequest.candidateCount = undefined - if (hybridRanker) { + if (useHybridRanker && hybridRanker) { try { const hybrid = await hybridRanker(query, fetchLimit, targetLayer, authorizeGraph, hybridRequest) results = hybrid.map((r) => ({ @@ -888,9 +889,11 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { })) } catch (err) { hybridRequest.candidateCount = undefined - logger.warn("hybrid ranking failed, falling back to keyword search", { + logger.warn("hybrid ranking failed", { error: err instanceof Error ? err.message : String(err), }) + if (enriched.length > 0) break + useHybridRanker = false results = idx.search(query, fetchLimit, targetLayer) } } else { @@ -912,7 +915,7 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { ? await loadMemoryProject(r.obsId) : null if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) return null - const session = await loadSession(r.sessionId) + const session = projectFilter || cwdFilter ? await loadSession(r.sessionId) : null if (projectFilter && memoryProject === null && !session) return null if (cwdFilter && layer === "observation" && !session) return null if (session) { diff --git a/src/state/hybrid-search.ts b/src/state/hybrid-search.ts index 60646a81a8..caeb8f03b8 100644 --- a/src/state/hybrid-search.ts +++ b/src/state/hybrid-search.ts @@ -118,7 +118,13 @@ export class HybridSearch { if (!request.embedding || request.embedding.query !== query) { request.embedding = { query, value: this.embeddingProvider.embed(query) }; } - queryEmbedding = await request.embedding.value; + const embedding = request.embedding; + try { + queryEmbedding = await embedding.value; + } catch (error) { + if (request.embedding === embedding) request.embedding = undefined; + throw error; + } } else { queryEmbedding = await this.embeddingProvider.embed(query); } diff --git a/test/hybrid-search.test.ts b/test/hybrid-search.test.ts index 465b970cd8..fde7aa2deb 100644 --- a/test/hybrid-search.test.ts +++ b/test/hybrid-search.test.ts @@ -71,6 +71,29 @@ describe("HybridSearch", () => { expect(embed).toHaveBeenCalledTimes(2); }); + it("retries a failed embedding and then reuses the recovered request embedding", async () => { + const bm25Obs = makeObs({ id: "obs_bm25" }); + const vectorObs = makeObs({ id: "obs_vector" }); + bm25.add(bm25Obs); + await kv.set(KV.observations(bm25Obs.sessionId), bm25Obs.id, bm25Obs); + await kv.set(KV.observations(vectorObs.sessionId), vectorObs.id, vectorObs); + const vector = new VectorIndex(); + vector.add(vectorObs.id, vectorObs.sessionId, new Float32Array([1, 0])); + const embed = vi.fn<() => Promise>() + .mockRejectedValueOnce(new Error("provider unavailable")) + .mockResolvedValue(new Float32Array([1, 0])); + const provider = { name: "test", dimensions: 2, embed, embedBatch: vi.fn() }; + const hybrid = new HybridSearch(bm25, vector, provider, kv as never, 0.4, 0.6, 0); + const request = {}; + const first = await hybrid.search("auth", 100, "all", undefined, request); + expect(first.map((hit) => hit.observation.id)).not.toContain(vectorObs.id); + const recovered = await hybrid.search("auth", 200, "all", undefined, request); + expect(recovered.map((hit) => hit.observation.id)).toContain(vectorObs.id); + expect(embed).toHaveBeenCalledTimes(2); + await hybrid.search("auth", 400, "all", undefined, request); + expect(embed).toHaveBeenCalledTimes(2); + }); + it("returns BM25-only results when no vector index is provided", async () => { const obs = makeObs({ id: "obs_1", sessionId: "ses_1" }); bm25.add(obs); diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index d86bd60281..7d1755f523 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -27,6 +27,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( let hybridLimits: number[]; let memoryReads: string[]; let sessionReads: string[]; + let failSessionReads: boolean; let activeObservationReads: number; let maxObservationReads: number; let search: (data: Record) => Promise<{ results: SearchResult[] }>; @@ -88,6 +89,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( hybridLimits = []; memoryReads = []; sessionReads = []; + failSessionReads = false; activeObservationReads = 0; maxObservationReads = 0; getSearchIndex().clear(); @@ -109,7 +111,10 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( } const kv = { get: async (scope: string, key: string) => { - if (scope === KV.sessions) sessionReads.push(key); + if (scope === KV.sessions) { + sessionReads.push(key); + if (failSessionReads) throw new Error("session store unavailable"); + } if (scope === KV.observations("memory")) { memoryReads.push(key); activeObservationReads++; @@ -280,6 +285,30 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( expect(sessionReads).not.toContain("session_10"); }); + it("does not read sessions for agent-only filtering", async () => { + const memory = addMemory({ project: "project-a", agentId: "agent-a" }); + failSessionReads = true; + expect(await recall({ project: undefined, agentId: "agent-a" })).toEqual([memory.id]); + expect(sessionReads).toEqual([]); + }); + + if (ranking === "hybrid") { + it("preserves accepted hybrid hits when a later refill fails", async () => { + const matching = addMemory({ id: "mem_vector_only", project: "project-a" }); + for (let i = 0; i < 99; i++) addMemory({ id: `mem_foreign_${i}`, project: "project-b" }); + let calls = 0; + setHybridRanker(async () => { + if (++calls > 1) throw new Error("refill unavailable"); + return memories.map((memory, i) => ({ + observation: memoryToObservation(memory), sessionId: "memory", combinedScore: 1 / (i + 1), layer: "memory" as const, + })); + }); + const keyword = vi.spyOn(getSearchIndex(), "search").mockReturnValue([]); + expect(await recall({ limit: 2 })).toEqual([matching.id]); + expect(keyword).not.toHaveBeenCalled(); + }); + } + it("rejects orphaned observations when a directory filter is requested", async () => { const orphan = addMemory({ id: "obs_orphan", sessionIds: ["session_missing"] }); const observation = memoryToObservation(orphan); @@ -293,7 +322,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( it("preserves sessionless memories when a directory filter is requested", async () => { const memory = addMemory({ project: "project-a" }); - expect(await recall({ cwd: "/test/project" })).toEqual([memory.id]); + expect(await recall({ project: undefined, cwd: "/test/project" })).toEqual([memory.id]); }); it("does not let an unloadable observation consume the result limit", async () => { From 200cedf56544870fd11f60fbb235dc49172d6f1a Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 4 Nov 2026 13:30:53 +0530 Subject: [PATCH 8/8] chore: remove code comments and issue references --- src/functions/search.ts | 2 -- test/search-project-isolation.test.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/src/functions/search.ts b/src/functions/search.ts index 7ae84b5ae5..0cf8b28a7a 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -911,7 +911,6 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { results = idx.search(query, fetchLimit, targetLayer) } - // A full page must stop further session lookups as well as record loads. enriched = [] let batchSize = Math.min(10, effectiveLimit) for (let offset = 0; offset < Math.min(results.length, fetchLimit) && enriched.length < effectiveLimit;) { @@ -971,7 +970,6 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { batchSize = Math.min(10, Math.max(effectiveLimit - enriched.length, batchSize * 2)) } if (!filtering || enriched.length >= effectiveLimit || (hybridRequest.candidateCount ?? results.length) < fetchLimit || fetchLimit >= MAX_CANDIDATES) break - // A full candidate window can hide valid matches behind rejected records. fetchLimit = Math.min(fetchLimit * 2, MAX_CANDIDATES) } diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index 7d1755f523..99069dd7eb 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -338,7 +338,6 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( it("fills the result limit after excluding an unscoped first candidate", async () => { const unscoped = addMemory(); const matching = addMemory({ id: "mem_matching", project: "project-a" }); - // Candidate ordering is external to the ownership filter under test. vi.spyOn(getSearchIndex(), "search").mockImplementation((_query, limit) => [unscoped, matching].slice(0, limit).map((memory, index) => ({ obsId: memory.id,