Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/functions/lessons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "all", (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
Expand Down
7 changes: 7 additions & 0 deletions src/state/search-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down Expand Up @@ -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)!;
Expand Down
27 changes: 27 additions & 0 deletions test/lesson-index-recall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
Loading