Skip to content

Commit 7620a58

Browse files
fix(memory-wiki): guard wiki_get against missing or wrong-typed lookup (#122549)
* fix(memory-wiki): return clean error from wiki_get on missing lookup wiki_get crashed with "Cannot read properties of undefined (reading 'trim')" when called with a wrong parameter name (e.g. path instead of lookup), leaving lookup undefined. - Validate and trim lookup at the wiki_get tool boundary; return a clean error (found: false) when it is missing or empty instead of falling through to page resolution - Add regression test covering the wrong-param call path * fix(memory-wiki): normalize wiki_get parameters Punchcard-Session: calm-workshop-cedar-hx --------- Co-authored-by: Vincent Koc <[email protected]>
1 parent d94c755 commit 7620a58

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎extensions/memory-wiki/src/query.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,29 @@ describe("getMemoryWikiPage", () => {
17851785
);
17861786
});
17871787

1788+
it("reports a clean error instead of crashing for malformed wiki_get params", async () => {
1789+
const { config } = await createQueryVault({
1790+
initialize: true,
1791+
config: { search: { backend: "shared", corpus: "memory" } },
1792+
});
1793+
const tool = createWikiGetTool(config, createAppConfig());
1794+
const malformedParams = [
1795+
null,
1796+
{ path: "sources/example/note.md" },
1797+
{ lookup: " " },
1798+
{ lookup: 42 },
1799+
];
1800+
1801+
for (const params of malformedParams) {
1802+
const result = await tool.execute("wiki-get-bad-param", params);
1803+
1804+
expect(result.details).toEqual({ found: false });
1805+
expect(result.content).toEqual([
1806+
{ type: "text", text: "wiki_get requires a non-empty `lookup` path or id." },
1807+
]);
1808+
}
1809+
});
1810+
17881811
it("normalizes extensionless shared memory lookups before reading", async () => {
17891812
const { config } = await createQueryVault({
17901813
initialize: true,

‎extensions/memory-wiki/src/tool.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import path from "node:path";
33
import { optionalFiniteNumberSchema } from "openclaw/plugin-sdk/channel-actions";
44
import type { OpenClawPluginToolContext } from "openclaw/plugin-sdk/plugin-entry";
5+
import { asNonArrayRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
56
import { Type } from "typebox";
67
import type { AnyAgentTool, OpenClawConfig } from "../api.js";
78
import { applyMemoryWikiMutation, normalizeMemoryWikiMutationInput } from "./apply.js";
@@ -277,13 +278,20 @@ export function createWikiGetTool(
277278
"Read a wiki page by id or relative path, or fall back to the active memory corpus when shared search is enabled.",
278279
parameters: WikiGetSchema,
279280
execute: async (_toolCallId, rawParams) => {
280-
const params = rawParams as {
281-
lookup: string;
281+
const params = asNonArrayRecord(rawParams) as {
282+
lookup?: string;
282283
fromLine?: number;
283284
lineCount?: number;
284285
backend?: ResolvedMemoryWikiConfig["search"]["backend"];
285286
corpus?: ResolvedMemoryWikiConfig["search"]["corpus"];
286287
};
288+
const lookup = typeof params.lookup === "string" ? params.lookup.trim() : "";
289+
if (!lookup) {
290+
return {
291+
content: [{ type: "text", text: "wiki_get requires a non-empty `lookup` path or id." }],
292+
details: { found: false },
293+
};
294+
}
287295
await syncImportedSourcesIfNeeded(config, appConfig);
288296
const result = await getMemoryWikiPage({
289297
config,
@@ -292,15 +300,15 @@ export function createWikiGetTool(
292300
agentSessionKey: memoryContext.agentSessionKey,
293301
sandboxed: memoryContext.sandboxed,
294302
conversationRecall: memoryContext.conversationRecall,
295-
lookup: params.lookup,
303+
lookup,
296304
fromLine: params.fromLine,
297305
lineCount: params.lineCount,
298306
...(params.backend ? { searchBackend: params.backend } : {}),
299307
...(params.corpus ? { searchCorpus: params.corpus } : {}),
300308
});
301309
if (!result) {
302310
return {
303-
content: [{ type: "text", text: `Wiki page not found: ${params.lookup}` }],
311+
content: [{ type: "text", text: `Wiki page not found: ${lookup}` }],
304312
details: { found: false },
305313
};
306314
}

0 commit comments

Comments
 (0)