fix: validate memory get ranges

This commit is contained in:
Peter Steinberger
2026-05-28 19:11:33 -04:00
parent 56a5d7e865
commit 361753908e
5 changed files with 23 additions and 6 deletions

View File

@@ -74,8 +74,8 @@ const MemoryGetSchema = {
type: "object",
properties: {
path: { type: "string" },
from: { type: "number" },
lines: { type: "number" },
from: { type: "integer", minimum: 1 },
lines: { type: "integer", minimum: 1 },
corpus: { type: "string", enum: ["memory", "wiki", "all"] },
},
required: ["path"],

View File

@@ -207,6 +207,21 @@ describe("memory tools", () => {
expect(getMemorySearchManagerMockCalls()).toBe(0);
});
it("rejects fractional memory_get ranges before reading files", async () => {
setMemoryBackend("builtin");
const tool = createMemoryGetToolOrThrow();
await expect(
tool.execute("call_fractional_range", {
path: "memory/2026-02-19.md",
from: 1.5,
lines: 2,
}),
).rejects.toThrow("from must be a positive integer");
expect(getReadAgentMemoryFileMockCalls()).toBe(0);
expect(getMemorySearchManagerMockCalls()).toBe(0);
});
it("returns truncation metadata and a continuation notice for partial memory_get results", async () => {
setMemoryBackend("builtin");
setMemoryReadFileImpl(async (params: MemoryReadParams) => ({

View File

@@ -37,8 +37,8 @@ export const MemorySearchSchema = Type.Object({
export const MemoryGetSchema = Type.Object({
path: Type.String(),
from: Type.Optional(Type.Number()),
lines: Type.Optional(Type.Number()),
from: Type.Optional(Type.Integer()),
lines: Type.Optional(Type.Integer()),
corpus: Type.Optional(stringEnum(["memory", "wiki", "all"])),
});

View File

@@ -4,6 +4,7 @@ import {
asToolParamsRecord,
jsonResult,
readNumberParam,
readPositiveIntegerParam,
readStringParam,
type MemoryCorpusSearchResult,
type OpenClawConfig,
@@ -443,8 +444,8 @@ export function createMemoryGetTool(options: {
async (_toolCallId, params) => {
const rawParams = asToolParamsRecord(params);
const relPath = readStringParam(rawParams, "path", { required: true });
const from = readNumberParam(rawParams, "from", { integer: true });
const lines = readNumberParam(rawParams, "lines", { integer: true });
const from = readPositiveIntegerParam(rawParams, "from");
const lines = readPositiveIntegerParam(rawParams, "lines");
const requestedCorpus = readStringParam(rawParams, "corpus") as
| "memory"
| "wiki"

View File

@@ -8,6 +8,7 @@ export {
asToolParamsRecord,
jsonResult,
readNumberParam,
readPositiveIntegerParam,
readStringParam,
type AnyAgentTool,
} from "../agents/tools/common.js";