Repository navigation
fix: preserve original memory types in search responses #1646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b41a517
fix: preserve original memory types in search responses
reevesc88 965838e
Merge remote-tracking branch 'origin/main' into fix/preserve-memory-type
rohitg00 0000efa
fix(search): keep compact search alive on a failed type read and alig…
rohitg00 00a76a1
chore: remove code comments and issue references
rohitg00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { memoryToObservation, lessonToObservation } from "../src/state/memory-utils.js"; | ||
| import { registerSmartSearchFunction } from "../src/functions/smart-search.js"; | ||
| import { buildRecallResponse } from "../src/functions/recall-response.js"; | ||
| import { CompressOutputSchema } from "../src/eval/schemas.js"; | ||
| import type { Memory, Lesson, CompressedObservation, HybridSearchResult } from "../src/types.js"; | ||
| import type { IIIClient } from "iii-sdk"; | ||
| import type { StateKV } from "../src/state/kv.js"; | ||
|
|
||
| vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } })); | ||
|
|
||
| const types: Memory["type"][] = ["pattern", "preference", "architecture", "bug", "workflow", "fact"]; | ||
| function memory(type: Memory["type"]): Memory { | ||
| return { | ||
| id: `mem_${type}`, type, title: "Synthetic type preservation", content: "Synthetic content. ".repeat(200), | ||
| createdAt: "2026-10-31T14:44:33.707Z", updatedAt: "2026-10-31T14:44:33.707Z", | ||
| concepts: ["synthetic"], files: [], sessionIds: [], strength: 7, version: 1, isLatest: true, | ||
| project: "project-a", agentId: "owner", | ||
| }; | ||
| } | ||
|
|
||
| function fixture(saved: Memory, legacy = false, failMemoryReads = false) { | ||
| const store = new Map<string, Map<string, unknown>>([["mem:memories", new Map([[saved.id, saved]])]]); | ||
| const kv = { | ||
| get: async (scope: string, id: string) => { | ||
| if (failMemoryReads && scope === "mem:memories") throw new Error("state read failed"); | ||
| return store.get(scope)?.get(id) ?? null; | ||
| }, | ||
| set: async (scope: string, id: string, value: unknown) => { | ||
| if (!store.has(scope)) store.set(scope, new Map()); | ||
| store.get(scope)!.set(id, value); | ||
| return value; | ||
| }, | ||
| list: async (scope: string) => [...(store.get(scope)?.values() ?? [])], | ||
| }; | ||
| let handler: (args: object) => Promise<{ | ||
| mode: string; | ||
| results: Array<{ obsId: string; observation?: CompressedObservation }>; | ||
| truncated?: boolean; | ||
| }>; | ||
| const sdk = { registerFunction: (_: string, fn: typeof handler) => { handler = fn; } }; | ||
| const observation = memoryToObservation(saved); | ||
| if (legacy) delete observation.memoryType; | ||
| const search = vi.fn(async (): Promise<HybridSearchResult[]> => [{ | ||
| observation, sessionId: "memory", layer: "memory", combinedScore: 1, | ||
| bm25Score: 1, vectorScore: 0, graphScore: 0, | ||
| }]); | ||
| registerSmartSearchFunction(sdk as unknown as IIIClient, kv as unknown as StateKV, search); | ||
| return { call: (args: object) => handler(args), saved }; | ||
| } | ||
|
|
||
| describe("original memory type metadata", () => { | ||
| it("keeps compact search working when the stored memory read fails", async () => { | ||
| const f = fixture(memory("bug"), false, true); | ||
| const compact = await f.call({ query: "Synthetic", agentId: "*", targetLayer: "memory", includeLessons: false }); | ||
| expect(compact.results[0]).toMatchObject({ obsId: f.saved.id, type: "decision", memoryType: "bug" }); | ||
| }); | ||
|
|
||
| it.each(types)("preserves %s without changing the observation type or input", (type) => { | ||
| const saved = memory(type); | ||
| const before = structuredClone(saved); | ||
| const result = memoryToObservation(Object.freeze(saved)); | ||
| expect(result).toMatchObject({ id: saved.id, type: "decision", memoryType: type, narrative: saved.content, agentId: "owner" }); | ||
| expect(saved).toEqual(before); | ||
| expect(CompressOutputSchema.safeParse(result).success).toBe(true); | ||
| expect(CompressOutputSchema.safeParse({ ...result, type }).success).toBe(false); | ||
| }); | ||
|
|
||
| it.each(types)("preserves %s in compact and expanded search, including a legacy index", async (type) => { | ||
| const f = fixture(memory(type), true); | ||
| const scope = { project: "project-a", agentId: "*", targetLayer: "memory", includeLessons: false }; | ||
| const compact = await f.call({ query: "Synthetic", ...scope }); | ||
| expect(compact.results[0]).toMatchObject({ obsId: f.saved.id, type: "decision", memoryType: type }); | ||
| const expanded = await f.call({ expandIds: [f.saved.id], ...scope }); | ||
| expect(expanded.results[0].observation).toMatchObject({ type: "decision", memoryType: type, narrative: f.saved.content }); | ||
| expect(expanded.truncated).toBe(false); | ||
| expect((await f.call({ query: "Synthetic", ...scope, project: "project-b" })).results).toEqual([]); | ||
| expect((await f.call({ expandIds: [f.saved.id], ...scope, project: "project-b" })).results).toEqual([]); | ||
| expect((await f.call({ query: "Synthetic", ...scope, agentId: "other" })).results).toEqual([]); | ||
| expect((await f.call({ expandIds: [f.saved.id], ...scope, agentId: "other" })).results).toEqual([]); | ||
| expect((await f.call({ query: "Synthetic", ...scope, targetLayer: "observation" })).results).toEqual([]); | ||
| }); | ||
|
|
||
| it.each(["full", "compact", "narrative"] as const)("retains memoryType in %s recall with and without clipping", (format) => { | ||
| const observation = memoryToObservation(memory("fact")); | ||
| observation.title = "Synthetic memory title ".repeat(100); | ||
| const result = { observation, sessionId: "memory", layer: "memory" as const, score: 1 }; | ||
| for (const budget of [undefined, 250]) { | ||
| const response = buildRecallResponse([result], format, budget); | ||
| const first = response.results[0]; | ||
| expect(first).toBeDefined(); | ||
| const metadata = "observation" in first ? first.observation : first; | ||
| expect(metadata).toHaveProperty("memoryType", "fact"); | ||
| if (budget) { | ||
| expect(first).toHaveProperty("content_truncated", true); | ||
| expect(response.tokens_used).toBeLessThanOrEqual(budget); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it("does not assign a memory type to lessons or ordinary observations", () => { | ||
| const lesson = { id: "lsn_test", content: "Lesson", context: "", tags: [], confidence: 7, createdAt: "2026-10-31" } as Lesson; | ||
| expect(lessonToObservation(lesson)).not.toHaveProperty("memoryType"); | ||
| const observation: CompressedObservation = { ...memoryToObservation(memory("fact")), id: "obs_test", type: "file_read" }; | ||
| delete observation.memoryType; | ||
| const result = buildRecallResponse([{ observation, sessionId: "session", layer: "observation", score: 1 }], "compact"); | ||
| expect(result.results[0]).toHaveProperty("type", "file_read"); | ||
| expect(result.results[0]).not.toHaveProperty("memoryType"); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.