Repository navigation
Commit c5556a5
committed
core: refuse apply_diff on bytes the decoder cannot read instead of saving U+FFFD back
fs.readFile with a "utf-8" encoding replaces every byte sequence it cannot decode with U+FFFD. The tool
then applies the diff to that decoded string and saves the result back through saveDirectly or
saveChanges, so editing a file whose bytes are not valid UTF-8 used to rewrite bytes the model never
asked to change. The read now takes the bytes and decodes them with
`new TextDecoder("utf-8", { fatal: true })`, which refuses: the tool names the file and the first
undecodable byte by offset on the error path it already uses for a missing file (say("error"),
recordToolError, consecutiveMistakeCount, didToolFailInCurrentTurn, pushToolResult) and returns
before the diff is computed, approved, or written. The decision is fail-closed, consistent with the
guarded publish: a file that used to be edited lossily is now not edited at all, and the message
names converting it to UTF-8 as the remediation.
The three fs doubles in applyDiffTool.failureReporting.spec.ts returned a string from a call that
takes no encoding, which is the lie the lossy decode was riding on; they now return Buffer and the
mock is typed against the byte-returning signature it stands in for. Four rows cover the new branch:
the refusal names the file and the byte, nothing is approved or written when it fires, valid
multi-byte UTF-8 still edits, and repeated invalid bytes report the first offset and not a later one.
They are Buffer and string logic only - no inode recycling, no cmd.exe, no junction - so nothing in
them depends on how this box behaves; the ubuntu lane is what instruments this file's lane.
Verification, pinned node v22.23.4 for every run below except the project pre-push hook, which ran
pnpm's bundled node v25.9.0 on cached turbo tasks (the weaker of the two statements): the
failureReporting suite plus its two neighbours (applyPatchTool.execute, editFileTool) pass 79/79 with
stdio on file descriptors; eslint --prune-suppressions --max-warnings=0 exits 0 on both touched files
and its rewrite of src/eslint-suppressions.json was reverted, so this commit carries no suppression
churn; tsc --noEmit -p tsconfig.json reports one error, activate/registerCommands.ts:88, on the
patched tree and the identical error on pristine 303af45, so the type delta is zero. prettier
--write ran before measuring and before committing, and the number below is the committed formatted
tree's: 346 of 500 counted executable lines at base 73931fe with
base...head and --unified=0, against 333 at 303af45 - +13 for this change - measured with v22.23.4.
The gate number is quoted here in the same sentence as the base it was taken against, because a line
count without its base is not a count.1 parent 303af45 commit c5556a5
2 files changed
Lines changed: 90 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
77 | 99 | | |
78 | 100 | | |
79 | 101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
| |||
59 | 64 | | |
60 | 65 | | |
61 | 66 | | |
62 | | - | |
| 67 | + | |
63 | 68 | | |
64 | 69 | | |
65 | 70 | | |
| |||
146 | 151 | | |
147 | 152 | | |
148 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
149 | 213 | | |
150 | 214 | | |
151 | 215 | | |
| |||
326 | 390 | | |
327 | 391 | | |
328 | 392 | | |
329 | | - | |
| 393 | + | |
330 | 394 | | |
331 | 395 | | |
332 | 396 | | |
| |||
0 commit comments