Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(code): an empty baseline is not a licence to overwrite

Davidb42ab6d57bc4
diff --git a/src/lib/__tests__/staged-apply.test.ts b/src/lib/__tests__/staged-apply.test.ts
index 8b67b373..daea1caa 100644
--- a/src/lib/__tests__/staged-apply.test.ts
+++ b/src/lib/__tests__/staged-apply.test.ts
@@ -135,12 +135,32 @@ describe('applyStagedChange refuses to overwrite a file that moved on', () => {
expect(useStagedChangesStore.getState().list(CHAT)).toHaveLength(0)
})
- it('does not read at all for a new file (no baseline to compare against)', async () => {
- fsWrite.mockResolvedValue({ status: 'saved' })
+ // An empty baseline is not a licence to overwrite. useCodex also writes an
+ // empty oldContent when the stage-time read merely FAILED, so "new file" and
+ // "could not look" are the same value here (review 2026-08-14).
+ it('a new file that really is not there is created without ceremony', async () => {
+ route(new Error('no such file'))
stage('new.py', { oldContent: '' })
await applyStagedChange(CHAT, useStagedChangesStore.getState().list(CHAT)[0])
- expect(fsWrite).toHaveBeenCalledTimes(1)
- expect(fsWrite.mock.calls[0][0]).toBe('fs_write')
+ expect(useStagedChangesStore.getState().list(CHAT)).toHaveLength(0)
+ expect(fsWrite).toHaveBeenCalledWith('fs_write', expect.anything())
+ })
+
+ it('a new file whose path is occupied refuses instead of overwriting', async () => {
+ route('somebody else already wrote this')
+ stage('new.py', { resolvedPath: '/proj/new.py', workingDirectory: '/proj', oldContent: '' })
+ const change = useStagedChangesStore.getState().list(CHAT)[0]
+
+ await expect(applyStagedChange(CHAT, change)).rejects.toThrow(/changed on disk/)
+ expect(fsWrite).not.toHaveBeenCalledWith('fs_write', expect.anything())
+ expect(useStagedChangesStore.getState().list(CHAT)).toHaveLength(1)
+ })
+
+ it('an empty file at that path is not a collision, the write just fills it', async () => {
+ route('')
+ stage('new.py', { resolvedPath: '/proj/new.py', workingDirectory: '/proj', oldContent: '' })
+ await applyStagedChange(CHAT, useStagedChangesStore.getState().list(CHAT)[0])
+ expect(useStagedChangesStore.getState().list(CHAT)).toHaveLength(0)
})
it('still writes when the file is gone — the write recreates it', async () => {
diff --git a/src/lib/staged-apply.ts b/src/lib/staged-apply.ts
index 20c183e1..fe45c2c5 100644
--- a/src/lib/staged-apply.ts
+++ b/src/lib/staged-apply.ts
@@ -37,15 +37,20 @@ import { mergeThreeWay } from './three-way-merge'
* 3. foreign edit elsewhere-> merge both and write the result
* 4. same lines both sides -> refuse, this one needs a human
*
- * Only steps 2 to 4 need the baseline. `oldContent` is also empty when the
- * stage-time read failed (useCodex treats a failed read as "new file"), and
- * gating on that would break applies that are perfectly fine.
+ * An empty `oldContent` means "there was no file here", and that used to skip
+ * the whole check. It is also what useCodex writes when the stage-time read
+ * merely FAILED, so a new-file write could land on top of a file that exists
+ * now, with no drift check, no merge and no warning. Empty is a baseline like
+ * any other: an empty base against a file that has content is exactly the
+ * insert-against-insert case mergeThreeWay already refuses, and refusing with
+ * the message below beats overwriting somebody's file in silence. A file that
+ * genuinely is not there still throws on the read and takes the create path.
*/
async function reconcile(
chatId: string,
change: StagedChange,
): Promise<{ content: string; merged: number }> {
- if (!change.oldContent) return { content: change.newContent, merged: 0 }
+ const base = change.oldContent ?? ''
let current: string
try {
const res = await backendCall<{ content?: string }>('fs_read', {
@@ -58,10 +63,10 @@ async function reconcile(
// gone or unreadable, the write recreates it, which is what the user asked for
return { content: change.newContent, merged: 0 }
}
- if (current === change.oldContent || current === change.newContent) {
+ if (current === base || current === change.newContent) {
return { content: change.newContent, merged: 0 }
}
- const merged = mergeThreeWay(change.oldContent, current, change.newContent)
+ const merged = mergeThreeWay(base, current, change.newContent)
if (merged.ok) {
return { content: merged.content, merged: merged.mergedRegions }
}