From aadd4d101ca62b0b6eaee0764b36db573c502bbf Mon Sep 17 00:00:00 2001 From: March <183773531+Mike-Animal-Counseling@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:57:48 -0800 Subject: [PATCH 1/2] fix(lessons): apply recall filters before the candidate limit Signed-off-by: March <183773531+Mike-Animal-Counseling@users.noreply.github.com> --- src/functions/lessons.ts | 9 ++++++--- src/state/search-index.ts | 7 +++++++ test/lesson-index-recall.test.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/functions/lessons.ts b/src/functions/lessons.ts index 8c12ed594f..c643aff43b 100644 --- a/src/functions/lessons.ts +++ b/src/functions/lessons.ts @@ -192,14 +192,17 @@ export function registerLessonsFunctions(sdk: IIIClient, kv: StateKV): void { const fetchLimit = filtering ? Math.max(limit * 10, 100) : Math.max(limit * 5, 50); - const hits = idx.search(data.query, fetchLimit); + const hits = idx.search(data.query, fetchLimit, "lesson", (id) => { + const lesson = lessonRecords.get(id); + if (!lesson || lesson.deleted || lesson.confidence < minConfidence) return false; + return !data.project || lesson.project === data.project; + }); const maxHit = hits.length > 0 ? hits[0].score : 0; const scored: Array<{ lesson: Lesson; score: number }> = []; for (let i = 0; i < hits.length; i++) { const l = lessonRecords.get(hits[i].obsId); - if (!l || l.deleted || l.confidence < minConfidence) continue; - if (data.project && l.project !== data.project) continue; + if (!l) continue; const relevance = maxHit > 0 ? hits[i].score / maxHit : 0; const daysSinceReinforced = l.lastReinforcedAt diff --git a/src/state/search-index.ts b/src/state/search-index.ts index 36bf6f1e2d..273942dc44 100644 --- a/src/state/search-index.ts +++ b/src/state/search-index.ts @@ -177,6 +177,7 @@ export class SearchIndex { query: string, limit = 20, targetLayer: SearchLayer = "all", + accepts?: (obsId: string) => boolean, ): Array<{ obsId: string; sessionId: string; score: number }> { const rawTerms = this.tokenize(query.toLowerCase()); if (rawTerms.length === 0) return []; @@ -255,6 +256,12 @@ export class SearchIndex { } } + if (accepts) { + for (const obsId of scores.keys()) { + if (!accepts(obsId)) scores.delete(obsId); + } + } + return Array.from(scores.entries()) .map(([obsId, score]) => { const entry = this.entries.get(obsId)!; diff --git a/test/lesson-index-recall.test.ts b/test/lesson-index-recall.test.ts index d9bb8f0f2d..75d4479af8 100644 --- a/test/lesson-index-recall.test.ts +++ b/test/lesson-index-recall.test.ts @@ -61,6 +61,33 @@ describe("lesson recall through the lesson index", () => { expect(res.lessons[0].score).toBeGreaterThan(res.lessons[1].score); }); + it.each([ + { filter: "project", queryFilters: { project: "web" }, otherProject: "infra", otherConfidence: 0.9 }, + { filter: "confidence", queryFilters: { minConfidence: 0.8 }, otherProject: "web", otherConfidence: 0.3 }, + ])("recalls a matching lesson below the global candidate limit when filtered by $filter", async ({ queryFilters, otherProject, otherConfidence }) => { + const { sdk } = await setup(); + for (let i = 0; i < 101; i++) { + await sdk.trigger("mem::lesson-save", { + content: `calibration reading ${i}`, + project: otherProject, + confidence: otherConfidence, + }); + } + const saved = (await sdk.trigger("mem::lesson-save", { + content: "Review the operating instructions with the team and verify the machine is ready before starting.", + context: "calibration", + project: "web", + confidence: 0.9, + })) as { lesson: { id: string } }; + + const res = (await sdk.trigger("mem::lesson-recall", { + query: "calibration", + limit: 1, + ...queryFilters, + })) as { lessons: Array<{ id: string }> }; + expect(res.lessons.map((lesson) => lesson.id)).toEqual([saved.lesson.id]); + }); + it("recalls lessons that existed before the index was built (lazy rebuild)", async () => { const { sdk, kv } = await setup(); await kv.set("mem:lessons", "lsn_pre", { From eb2eecfca749da365c63b4aaed40baae3e8dbc8d Mon Sep 17 00:00:00 2001 From: March <183773531+Mike-Animal-Counseling@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:31:44 -0800 Subject: [PATCH 2/2] fix(lessons): use a supported search layer Signed-off-by: March <183773531+Mike-Animal-Counseling@users.noreply.github.com> --- src/functions/lessons.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/lessons.ts b/src/functions/lessons.ts index c643aff43b..ef71323015 100644 --- a/src/functions/lessons.ts +++ b/src/functions/lessons.ts @@ -192,7 +192,7 @@ export function registerLessonsFunctions(sdk: IIIClient, kv: StateKV): void { const fetchLimit = filtering ? Math.max(limit * 10, 100) : Math.max(limit * 5, 50); - const hits = idx.search(data.query, fetchLimit, "lesson", (id) => { + const hits = idx.search(data.query, fetchLimit, "all", (id) => { const lesson = lessonRecords.get(id); if (!lesson || lesson.deleted || lesson.confidence < minConfidence) return false; return !data.project || lesson.project === data.project;