Skip to content

Commit 9042e80

Browse files
fix(memory): fall back to wiki for missing all-corpus reads (#100904)
* Fix memory_get all supplement fallback * refactor(memory): use typed missing-read contract --------- Co-authored-by: Vincent Koc <[email protected]>
1 parent 3442a8e commit 9042e80

2 files changed

Lines changed: 99 additions & 4 deletions

File tree

‎extensions/memory-core/src/tools.citations.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,4 +629,79 @@ describe("memory tools", () => {
629629
lineCount: 5,
630630
});
631631
});
632+
633+
it("falls back to a wiki corpus supplement when memory_get corpus=all misses memory without throwing", async () => {
634+
setMemoryReadFileImpl(async (params: MemoryReadParams) => ({
635+
text: "",
636+
path: params.relPath,
637+
}));
638+
registerMemoryCorpusSupplement("memory-wiki", {
639+
search: async () => [],
640+
get: async () => ({
641+
corpus: "wiki",
642+
path: "memory/entities/alpha.md",
643+
title: "Alpha",
644+
kind: "entity",
645+
content: "Alpha wiki entry after empty miss",
646+
fromLine: 3,
647+
lineCount: 5,
648+
}),
649+
});
650+
651+
const tool = createMemoryGetToolOrThrow();
652+
const result = await tool.execute("call_get_all_empty_miss_fallback", {
653+
path: "memory/entities/alpha.md",
654+
from: 3,
655+
lines: 5,
656+
corpus: "all",
657+
});
658+
659+
expect(result.details).toEqual({
660+
corpus: "wiki",
661+
path: "memory/entities/alpha.md",
662+
title: "Alpha",
663+
kind: "entity",
664+
text: "Alpha wiki entry after empty miss",
665+
fromLine: 3,
666+
lineCount: 5,
667+
});
668+
});
669+
670+
it("preserves an empty in-file range for memory_get corpus=all", async () => {
671+
setMemoryReadFileImpl(async (params: MemoryReadParams) => ({
672+
text: "",
673+
path: params.relPath,
674+
from: params.from ?? 1,
675+
lines: 0,
676+
}));
677+
const getSupplement = vi.fn(async () => ({
678+
corpus: "wiki" as const,
679+
path: "memory/entities/alpha.md",
680+
title: "Alpha",
681+
kind: "entity",
682+
content: "Alpha wiki entry",
683+
fromLine: 10,
684+
lineCount: 5,
685+
}));
686+
registerMemoryCorpusSupplement("memory-wiki", {
687+
search: async () => [],
688+
get: getSupplement,
689+
});
690+
691+
const tool = createMemoryGetToolOrThrow();
692+
const result = await tool.execute("call_get_all_empty_range", {
693+
path: "memory/entities/alpha.md",
694+
from: 10,
695+
lines: 5,
696+
corpus: "all",
697+
});
698+
699+
expect(result.details).toEqual({
700+
text: "",
701+
path: "memory/entities/alpha.md",
702+
from: 10,
703+
lines: 0,
704+
});
705+
expect(getSupplement).not.toHaveBeenCalled();
706+
});
632707
});

‎extensions/memory-core/src/tools.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Memory Core plugin module implements tools behavior.
22
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
3-
import type { MemorySource } from "openclaw/plugin-sdk/memory-core-host-engine-storage";
3+
import type {
4+
MemoryReadResult,
5+
MemorySource,
6+
} from "openclaw/plugin-sdk/memory-core-host-engine-storage";
47
import {
58
asToolParamsRecord,
69
jsonResult,
@@ -360,16 +363,33 @@ async function resolveMemoryReadFailureResult(params: {
360363
return jsonResult({ path: params.relPath, text: "", disabled: true, error: message });
361364
}
362365

363-
async function executeMemoryReadResult<T>(params: {
364-
read: () => Promise<T>;
366+
function isMissingMemoryReadResult(result: MemoryReadResult, relPath: string): boolean {
367+
return result.path === relPath && result.text === "" && result.from === undefined;
368+
}
369+
370+
async function executeMemoryReadResult(params: {
371+
read: () => Promise<MemoryReadResult>;
365372
requestedCorpus?: "memory" | "wiki" | "all";
366373
relPath: string;
367374
from?: number;
368375
lines?: number;
369376
agentSessionKey?: string;
370377
}) {
371378
try {
372-
return jsonResult(await params.read());
379+
const result = await params.read();
380+
if (params.requestedCorpus === "all" && isMissingMemoryReadResult(result, params.relPath)) {
381+
const supplement = await getSupplementMemoryReadResult({
382+
relPath: params.relPath,
383+
from: params.from,
384+
lines: params.lines,
385+
agentSessionKey: params.agentSessionKey,
386+
corpus: params.requestedCorpus,
387+
});
388+
if (supplement) {
389+
return jsonResult(supplement);
390+
}
391+
}
392+
return jsonResult(result);
373393
} catch (error) {
374394
return await resolveMemoryReadFailureResult({
375395
error,

0 commit comments

Comments
 (0)