mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 08:41:23 +00:00
fix(memory): handle ENOENT gracefully in readFile instead of throwing
When a memory file doesn't exist yet (e.g. daily log `2026-02-19.md`),
`readFile` now returns `{ text: "", path }` instead of propagating the
ENOENT error. This prevents noisy error responses from the memory read
tool and aligns with the "graceful degradation" recommendation in #9307.
Closes #9307
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -12,7 +12,7 @@ let searchImpl: () => Promise<unknown[]> = async () => [
|
||||
source: "memory" as const,
|
||||
},
|
||||
];
|
||||
let readFileImpl: () => Promise<string> = async () => "";
|
||||
let readFileImpl: () => Promise<unknown> = async () => "";
|
||||
|
||||
const stubManager = {
|
||||
search: vi.fn(async () => await searchImpl()),
|
||||
@@ -59,7 +59,7 @@ beforeEach(() => {
|
||||
source: "memory" as const,
|
||||
},
|
||||
];
|
||||
readFileImpl = async () => "";
|
||||
readFileImpl = async () => ""; // default: return empty string
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
@@ -189,4 +189,23 @@ describe("memory tools", () => {
|
||||
error: "path required",
|
||||
});
|
||||
});
|
||||
|
||||
it("returns empty text without error when file does not exist (ENOENT)", async () => {
|
||||
readFileImpl = async () => {
|
||||
return { text: "", path: "memory/2026-02-19.md" };
|
||||
};
|
||||
|
||||
const cfg = { agents: { list: [{ id: "main", default: true }] } };
|
||||
const tool = createMemoryGetTool({ config: cfg });
|
||||
expect(tool).not.toBeNull();
|
||||
if (!tool) {
|
||||
throw new Error("tool missing");
|
||||
}
|
||||
|
||||
const result = await tool.execute("call_enoent", { path: "memory/2026-02-19.md" });
|
||||
expect(result.details).toEqual({
|
||||
text: "",
|
||||
path: "memory/2026-02-19.md",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user