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): a non-UTF-8 file is refused at staging and apply instead of read as empty

purpledece8bedeedc
diff --git a/src/hooks/codex/staged-writes.ts b/src/hooks/codex/staged-writes.ts
index 7e8b6ee6..42e0f58d 100644
--- a/src/hooks/codex/staged-writes.ts
+++ b/src/hooks/codex/staged-writes.ts
@@ -113,6 +113,13 @@ export function createStagedWriter(deps: StagedWriterDeps): StagedWriter {
} else {
try {
const r = await readFile(resolvedPath, stageReadCtx)
+ // A file that is not UTF-8 (Latin-1, UTF-16 from PowerShell) comes
+ // back without content. Read as '' it was reviewed as a brand new
+ // file, an all-insert diff hiding what gets overwritten (bug hunt
+ // 01.10.2026, C8). Refused like file_edit refuses it.
+ if (r?.encoding === 'binary' || r?.encoding === 'base64') {
+ return `file_write: ${path} exists but is not a UTF-8 text file, so the review cannot show what this write would replace. It was not staged. Ask the user before overwriting it.`
+ }
oldContent = r?.content ?? ''
} catch {
// New file — leave oldContent empty so the diff renders an
diff --git a/src/lib/__tests__/a-non-utf8-file-is-not-reviewed-as-new.test.ts b/src/lib/__tests__/a-non-utf8-file-is-not-reviewed-as-new.test.ts
new file mode 100644
index 00000000..1f30bc92
--- /dev/null
+++ b/src/lib/__tests__/a-non-utf8-file-is-not-reviewed-as-new.test.ts
@@ -0,0 +1,43 @@
+/**
+ * Bug hunt 01.10.2026 (C8). fs_read answers a file that is not UTF-8 (Latin-1,
+ * UTF-16 written by PowerShell) with `{ encoding: 'binary' }` and no content.
+ * Staging read that as '' and showed the overwrite as a brand new file, and
+ * Apply's drift check compared '' with '' and wrote over it. Staging now
+ * refuses such a file, as file_edit already did, and Apply refuses a file
+ * that is not text on disk.
+ */
+import { it, expect, vi, beforeEach } from 'vitest'
+
+vi.mock('../../api/backend', () => ({ backendCall: vi.fn() }))
+vi.mock('../../stores/chatStore', () => ({ useChatStore: { getState: () => ({ addMessage: vi.fn(), conversations: [] }) } }))
+
+import { backendCall } from '../../api/backend'
+import { applyStagedChange } from '../staged-apply'
+import { createStagedWriter } from '../../hooks/codex/staged-writes'
+import { useStagedChangesStore } from '../../stores/stagedChangesStore'
+
+const call = backendCall as unknown as ReturnType<typeof vi.fn>
+const CONV = 'conv-c8'
+
+beforeEach(() => {
+ call.mockReset()
+ useStagedChangesStore.getState().clear(CONV)
+})
+
+it('staging a write over a non-UTF-8 file is refused, nothing is queued', async () => {
+ const writer = createStagedWriter({
+ convId: CONV, workDir: '/home/u/repo', workspaceSlug: 'slug',
+ readFile: async () => ({ encoding: 'binary' }) as never,
+ })
+ const out = await writer.stageFileWrite({ path: 'legacy.txt', content: 'new text' })
+ expect(out).toMatch(/^file_write: legacy\.txt exists but is not a UTF-8 text file/)
+ expect(useStagedChangesStore.getState().list(CONV)).toHaveLength(0)
+})
+
+it('Apply does not write over a file that is not text on disk', async () => {
+ useStagedChangesStore.getState().stage(CONV, { path: 'legacy.txt', oldContent: '', newContent: 'new text', diff: '' })
+ call.mockImplementation(async (cmd: string) => (cmd === 'fs_read' ? { encoding: 'binary' } : { status: 'saved' }))
+ const change = useStagedChangesStore.getState().list(CONV)[0]
+ await expect(applyStagedChange(CONV, change)).rejects.toThrow(/not a UTF-8 text file on disk/)
+ expect(call.mock.calls.some(([cmd]) => cmd === 'fs_write')).toBe(false)
+})
diff --git a/src/lib/staged-apply.ts b/src/lib/staged-apply.ts
index 577467a2..87215f74 100644
--- a/src/lib/staged-apply.ts
+++ b/src/lib/staged-apply.ts
@@ -81,17 +81,25 @@ async function reconcile(
): Promise<{ content: string; merged: number }> {
const base = change.oldContent ?? ''
let current: string
+ let res: { content?: string; encoding?: string } | undefined
try {
- const res = await backendCall<{ content?: string }>('fs_read', {
+ res = await backendCall<{ content?: string; encoding?: string }>('fs_read', {
path: change.resolvedPath || change.path,
chatId: jail,
workingDirectory: change.workingDirectory,
})
- current = res?.content ?? ''
} catch {
// gone or unreadable, the write recreates it, which is what the user asked for
return { content: change.newContent, merged: 0 }
}
+ // Not UTF-8 on disk (bug hunt 01.10.2026, C8): read as '' it matched an
+ // empty base and was overwritten without the drift check ever seeing it.
+ if (typeof res?.content !== 'string' && (res?.encoding === 'binary' || res?.encoding === 'base64')) {
+ throw new Error(
+ `${change.path} is not a UTF-8 text file on disk, so this edit cannot be checked against it and was not applied. Reject this one and let the model read the file again.`,
+ )
+ }
+ current = res?.content ?? ''
// The file on disk decides the form; an empty file inherits the model's.
const eol = current ? eolOf(current) : eolOf(change.newContent)
const baseLf = toLf(base)