From 0d390688e3fd50e6058aedd0bba4d3d49e8f5246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=82CHES?= Date: Fri, 13 Mar 2026 12:27:39 -0600 Subject: [PATCH] fix: prevent move operation from silently overwriting existing files (#219) (#223) Check if the destination file exists before performing a move in hashline-edit. If it does, return an error instead of silently overwriting the file. Co-authored-by: Claude Opus 4.6 (1M context) --- .../src/core/tools/hashline-edit.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/pi-coding-agent/src/core/tools/hashline-edit.ts b/packages/pi-coding-agent/src/core/tools/hashline-edit.ts index a1ad76bac..c423f805f 100644 --- a/packages/pi-coding-agent/src/core/tools/hashline-edit.ts +++ b/packages/pi-coding-agent/src/core/tools/hashline-edit.ts @@ -259,6 +259,20 @@ export function createHashlineEditTool(cwd: string, options?: HashlineEditToolOp // Write result const finalContent = bom + restoreLineEndings(result.lines, originalEnding); const writePath = move ? resolveToCwd(move, cwd) : absolutePath; + + // Prevent silent overwrite when moving to an existing file + if (move && writePath !== absolutePath) { + try { + await ops.access(writePath); + // If access succeeds, the file exists — refuse the move + throw new Error(`Destination file already exists: ${writePath}. Use a different path or delete the existing file first.`); + } catch (err: any) { + // Re-throw our own error; swallow only "file not found" + if (err.message?.startsWith("Destination file already exists:")) throw err; + // File doesn't exist — safe to proceed + } + } + await ops.writeFile(writePath, finalContent); // If moved, delete original