fix: validate memory search result counts

This commit is contained in:
Peter Steinberger
2026-05-28 19:17:32 -04:00
parent 1610b4983f
commit e9cca2d1ef
4 changed files with 16 additions and 3 deletions

View File

@@ -62,7 +62,7 @@ const MemorySearchSchema = {
type: "object",
properties: {
query: { type: "string" },
maxResults: { type: "number" },
maxResults: { type: "integer", minimum: 1 },
minScore: { type: "number" },
corpus: { type: "string", enum: ["memory", "wiki", "all", "sessions"] },
},

View File

@@ -30,7 +30,7 @@ export async function loadMemoryToolRuntime(): Promise<MemoryToolRuntime> {
export const MemorySearchSchema = Type.Object({
query: Type.String(),
maxResults: Type.Optional(Type.Number()),
maxResults: Type.Optional(Type.Integer({ minimum: 1 })),
minScore: Type.Optional(Type.Number()),
corpus: Type.Optional(stringEnum(["memory", "wiki", "all", "sessions"])),
});

View File

@@ -58,6 +58,19 @@ describe("memory_search unavailable payloads", () => {
resetMemoryToolMockState({ searchImpl: async () => [] });
});
it("rejects fractional maxResults before searching", async () => {
const tool = createMemorySearchToolOrThrow();
await expect(
tool.execute("fractional-max-results", {
query: "hello",
maxResults: 1.5,
}),
).rejects.toThrow("maxResults must be a positive integer");
expect(getMemorySearchManagerMockCalls()).toBe(0);
});
it("returns explicit unavailable metadata for quota failures", async () => {
setMemorySearchImpl(async () => {
throw new Error("openai embeddings failed: 429 insufficient_quota");

View File

@@ -256,7 +256,7 @@ export function createMemorySearchTool(options: {
async (_toolCallId, params) => {
const rawParams = asToolParamsRecord(params);
const query = readStringParam(rawParams, "query", { required: true });
const maxResults = readNumberParam(rawParams, "maxResults");
const maxResults = readPositiveIntegerParam(rawParams, "maxResults");
const minScore = readNumberParam(rawParams, "minScore");
const requestedCorpus = readStringParam(rawParams, "corpus") as
| "memory"