Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

fix(vision): validate imageRef against managed store + guard topK NaN

Rohit Ghumarec736b397601c
diff --git a/src/functions/vision-search.ts b/src/functions/vision-search.ts
index 3b5d54e9..844ec9d5 100644
--- a/src/functions/vision-search.ts
+++ b/src/functions/vision-search.ts
@@ -2,6 +2,7 @@ import type { ISdk } from "iii-sdk";
import type { EmbeddingProvider } from "../types.js";
import { KV } from "../state/schema.js";
import { StateKV } from "../state/kv.js";
+import { isManagedImagePath } from "../utils/image-store.js";
import { recordAudit } from "./audit.js";
import { logger } from "../logger.js";
@@ -33,6 +34,13 @@ export function registerVisionSearchFunctions(
if (!data?.imageRef || typeof data.imageRef !== "string") {
return { success: false, error: "imageRef required" };
}
+ if (!isManagedImagePath(data.imageRef)) {
+ return { success: false, error: "imageRef must point to a file under the managed image store" };
+ }
+ const refCount = await kv.get<number>(KV.imageRefs, data.imageRef);
+ if (!refCount || Number(refCount) < 1) {
+ return { success: false, error: "imageRef not registered in mem:image-refs" };
+ }
try {
const vec = await imageProvider.embedImage(data.imageRef);
const stored: StoredEmbedding = {
@@ -72,7 +80,11 @@ export function registerVisionSearchFunctions(
if (!imageProvider?.embedImage) {
return { success: false, error: "image embeddings disabled (set AGENTMEMORY_IMAGE_EMBEDDINGS=true)" };
}
- const topK = Math.min(50, Math.max(1, data?.topK ?? 10));
+ const requestedTopK =
+ typeof data?.topK === "number" && Number.isFinite(data.topK)
+ ? Math.trunc(data.topK)
+ : 10;
+ const topK = Math.min(50, Math.max(1, requestedTopK));
let queryVec: Float32Array | null = null;
try {
@@ -84,6 +96,13 @@ export function registerVisionSearchFunctions(
: `data:image/png;base64,${data.queryImageBase64}`;
queryVec = await imageProvider.embedImage(b64);
} else if (data?.queryImageRef) {
+ if (!isManagedImagePath(data.queryImageRef)) {
+ return { success: false, error: "queryImageRef must point to a file under the managed image store" };
+ }
+ const refCount = await kv.get<number>(KV.imageRefs, data.queryImageRef);
+ if (!refCount || Number(refCount) < 1) {
+ return { success: false, error: "queryImageRef not registered in mem:image-refs" };
+ }
queryVec = await imageProvider.embedImage(data.queryImageRef);
} else {
return { success: false, error: "queryText, queryImageRef, or queryImageBase64 required" };
diff --git a/test/vision-search.test.ts b/test/vision-search.test.ts
index 55cf1306..015b3e2b 100644
--- a/test/vision-search.test.ts
+++ b/test/vision-search.test.ts
@@ -1,8 +1,15 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
+import { homedir } from "node:os";
+import { join } from "node:path";
import { registerVisionSearchFunctions } from "../src/functions/vision-search.js";
import type { EmbeddingProvider } from "../src/types.js";
import { KV } from "../src/state/schema.js";
+const IMAGES_DIR = join(homedir(), ".agentmemory", "images");
+const LOGIN_REF = join(IMAGES_DIR, "login.png");
+const DASH_REF = join(IMAGES_DIR, "dashboard.png");
+const OTHER_REF = join(IMAGES_DIR, "other.png");
+
function mockKV() {
const store = new Map<string, Map<string, unknown>>();
return {
@@ -52,6 +59,10 @@ describe("vision-search", () => {
},
};
+ async function seedRef(ref: string): Promise<void> {
+ await kv.set(KV.imageRefs, ref, 1);
+ }
+
beforeEach(() => {
kv = mockKV();
const handlers: Record<string, (data: Record<string, unknown>) => Promise<Record<string, unknown>>> = {};
@@ -66,42 +77,66 @@ describe("vision-search", () => {
});
it("embeds and stores image vectors in KV.imageEmbeddings", async () => {
- const res = (await visionEmbed({ imageRef: "/tmp/login.png" })) as { success: boolean; dimensions: number };
+ await seedRef(LOGIN_REF);
+ const res = (await visionEmbed({ imageRef: LOGIN_REF })) as { success: boolean; dimensions: number };
expect(res.success).toBe(true);
expect(res.dimensions).toBe(3);
const stored = await kv.list(KV.imageEmbeddings);
expect(stored.length).toBe(1);
});
+ it("rejects imageRef outside the managed image store", async () => {
+ const res = (await visionEmbed({ imageRef: "/etc/passwd" })) as { success: boolean; error: string };
+ expect(res.success).toBe(false);
+ expect(res.error).toMatch(/managed/);
+ });
+
+ it("rejects imageRef not registered in mem:image-refs", async () => {
+ const res = (await visionEmbed({ imageRef: LOGIN_REF })) as { success: boolean; error: string };
+ expect(res.success).toBe(false);
+ expect(res.error).toMatch(/not registered/);
+ });
+
it("text query ranks the matching image first", async () => {
- await visionEmbed({ imageRef: "/tmp/login.png" });
- await visionEmbed({ imageRef: "/tmp/dashboard.png" });
- await visionEmbed({ imageRef: "/tmp/other.png" });
+ for (const r of [LOGIN_REF, DASH_REF, OTHER_REF]) await seedRef(r);
+ await visionEmbed({ imageRef: LOGIN_REF });
+ await visionEmbed({ imageRef: DASH_REF });
+ await visionEmbed({ imageRef: OTHER_REF });
const res = (await visionSearch({ queryText: "the login form", topK: 3 })) as {
success: boolean;
results: Array<{ imageRef: string; score: number }>;
};
expect(res.success).toBe(true);
- expect(res.results[0].imageRef).toBe("/tmp/login.png");
+ expect(res.results[0].imageRef).toBe(LOGIN_REF);
expect(res.results[0].score).toBeGreaterThan(res.results[1].score);
});
it("image-to-image query finds the same image first", async () => {
- await visionEmbed({ imageRef: "/tmp/login.png" });
- await visionEmbed({ imageRef: "/tmp/dashboard.png" });
+ await seedRef(LOGIN_REF);
+ await seedRef(DASH_REF);
+ await visionEmbed({ imageRef: LOGIN_REF });
+ await visionEmbed({ imageRef: DASH_REF });
- const res = (await visionSearch({ queryImageRef: "/tmp/login.png", topK: 2 })) as {
+ const res = (await visionSearch({ queryImageRef: LOGIN_REF, topK: 2 })) as {
success: boolean;
results: Array<{ imageRef: string; score: number }>;
};
- expect(res.results[0].imageRef).toBe("/tmp/login.png");
+ expect(res.results[0].imageRef).toBe(LOGIN_REF);
expect(res.results[0].score).toBeGreaterThan(0.9);
});
+ it("queryImageRef outside managed store is rejected", async () => {
+ const res = (await visionSearch({ queryImageRef: "/etc/passwd" })) as { success: boolean; error: string };
+ expect(res.success).toBe(false);
+ expect(res.error).toMatch(/managed/);
+ });
+
it("sessionId filters out embeddings from other sessions", async () => {
- await visionEmbed({ imageRef: "/tmp/login.png", sessionId: "sess_a" });
- await visionEmbed({ imageRef: "/tmp/dashboard.png", sessionId: "sess_b" });
+ await seedRef(LOGIN_REF);
+ await seedRef(DASH_REF);
+ await visionEmbed({ imageRef: LOGIN_REF, sessionId: "sess_a" });
+ await visionEmbed({ imageRef: DASH_REF, sessionId: "sess_b" });
const res = (await visionSearch({ queryText: "anything", sessionId: "sess_a", topK: 5 })) as {
success: boolean;
@@ -111,6 +146,20 @@ describe("vision-search", () => {
expect(res.results.length).toBe(1);
});
+ it("clamps NaN/fractional topK to a valid integer", async () => {
+ await seedRef(LOGIN_REF);
+ await visionEmbed({ imageRef: LOGIN_REF });
+ const resNan = (await visionSearch({ queryText: "x", topK: Number.NaN })) as {
+ success: boolean;
+ results: unknown[];
+ };
+ expect(resNan.success).toBe(true);
+ expect(resNan.results.length).toBe(1);
+ const resFrac = (await visionSearch({ queryText: "x", topK: 3.7 })) as { success: boolean; results: unknown[] };
+ expect(resFrac.success).toBe(true);
+ expect(resFrac.results.length).toBe(1);
+ });
+
it("returns 503-equivalent error when provider is absent", async () => {
const handlers: Record<string, (data: Record<string, unknown>) => Promise<Record<string, unknown>>> = {};
const sdk = {