edit_file and apply_patch corrupt every non-UTF-8 byte in the file

Cerrado
#159 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
77/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
typescript
Área
desktop, tooling

Línea de trabajo

Start with src/main/tools/files.ts around edit_file, apply_patch.ts at the cited update paths, and write_file's overwrite path; inspect planPatch and the existing unit tests. Add a Windows-1252 fixture to verify non-UTF-8 bytes are refused without rewriting the file, then run the relevant tests. Done when all three write paths reject bytes that fail a UTF-8 round-trip.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug priority: high severity: high

Found in the 2026-10-07 code review and confirmed with a probe against the real tools.

Problem

edit_file (src/main/tools/files.ts:246-253) and apply_patch (apply_patch.ts:203,267) decode the whole file with readFile(file, 'utf8') and write it back as UTF-8. isBinaryFile (text_files.ts) only checks for NUL bytes, so Windows-1252, Latin-1 and Shift-JIS files pass. Every byte that isn't valid UTF-8 becomes EF BF BD, anywhere in the file, not just in the edited region. The approval diff compares the decoded strings, so it shows only the edited hunk.

Scenario

A legacy .properties file contains café in Windows-1252. The agent edits an unrelated line. The user approves a clean-looking diff, and é is replaced. The probe caf\xe9\n… came out containing ef bf bd.

Fix

Refuse to edit when the bytes don't round-trip:

const bytes = await readFile(file);
const before = bytes.toString('utf8');
if (!Buffer.from(before, 'utf8').equals(bytes))
  throw new ToolError(`${input.path} is not UTF-8; editing it would rewrite other bytes.`);

Add the same check in planPatch for updates and in write_file when it overwrites an existing file. Optionally mention the encoding in read_file's result. Add unit tests with a Windows-1252 fixture.

Lenguaje dominante
TypeScript
Estrellas
2
Forks
2
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.