mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 06:34:31 +00:00
fix(agents): only recover edit when oldText no longer in file (review feedback)
This commit is contained in:
committed by
Peter Steinberger
parent
bf6aa7ca67
commit
0fb3f188b2
@@ -44,18 +44,15 @@ describe("createHostWorkspaceEditTool post-write recovery", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("returns success when upstream throws but file on disk contains newText", async () => {
|
||||
it("returns success when upstream throws but file has newText and no longer has oldText", async () => {
|
||||
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-edit-recovery-"));
|
||||
const filePath = path.join(tmpDir, "MEMORY.md");
|
||||
const oldText = "# Memory";
|
||||
const newText = "Blog Writing";
|
||||
await fs.writeFile(filePath, `# Memory\n\n${newText}\n`, "utf-8");
|
||||
await fs.writeFile(filePath, `\n\n${newText}\n`, "utf-8");
|
||||
|
||||
const tool = createHostWorkspaceEditTool(tmpDir);
|
||||
const result = await tool.execute(
|
||||
"call-1",
|
||||
{ path: filePath, oldText: "# Memory", newText },
|
||||
undefined,
|
||||
);
|
||||
const result = await tool.execute("call-1", { path: filePath, oldText, newText }, undefined);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
const content = Array.isArray((result as { content?: unknown }).content)
|
||||
@@ -75,4 +72,17 @@ describe("createHostWorkspaceEditTool post-write recovery", () => {
|
||||
tool.execute("call-1", { path: filePath, oldText: "x", newText: "never-written" }, undefined),
|
||||
).rejects.toThrow("Simulated post-write failure");
|
||||
});
|
||||
|
||||
it("rethrows when file still contains oldText (pre-write failure; avoid false success)", async () => {
|
||||
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-edit-recovery-"));
|
||||
const filePath = path.join(tmpDir, "pre-write-fail.md");
|
||||
const oldText = "replace me";
|
||||
const newText = "new content";
|
||||
await fs.writeFile(filePath, `before ${oldText} after ${newText}`, "utf-8");
|
||||
|
||||
const tool = createHostWorkspaceEditTool(tmpDir);
|
||||
await expect(
|
||||
tool.execute("call-1", { path: filePath, oldText, newText }, undefined),
|
||||
).rejects.toThrow("Simulated post-write failure");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user