Skip to content

Commit d5c31a1

Browse files
core: let ApplyDiffTool accept what readFile's encoding overload hands back
The previous commit made the tool read bytes and decode fatally. CI did not run green on it: the ubuntu arm of #1910 checks out the merge ref of this branch onto main (job 129175169868, "HEAD is now at b4a8d6a Merge c5556a5 into e487cd7"), and main carries src/core/tools/__tests__/applyDiffTool.outsideWorkspace.spec.ts, which does not exist at this branch's base and therefore cannot be edited here. Its fs double answers readFile with the string "old\n" at line 143 - the encoding overload - and a strict byte-only read turns that into a refusal, so 9 of its 30 rows failed with "expected askApproval to be called 1 times, but got 0". The read now declares readFile's own union - string | Buffer - and treats a string as already decoded by whoever passed the encoding, because the original bytes are unrecoverable from it. The fatal decode, and so the refusal, applies where the tool holds bytes, which is every real read: no encoding is passed, so the byte overload is the one production calls. This keeps the fail-closed decision intact for files while not forcing every string-returning double in the repo - editFileTool, applyPatchTool, outsideWorkspace - to be rewritten by a PR that is about undecodable bytes. The trade is stated in the code comment and pinned by a row that says a pre-decoded file still edits, which is which side gives way. Measured, all with pinned node v22.23.4 and stdio on file descriptors: on the merge candidate itself (worktree at b4a8d6a, node_modules junctioned from wt-u2-1911, whose pnpm-lock blob 0fb1d69 differs from the merge ref's 2035d1d - disclosed rather than hidden), applyDiffTool.outsideWorkspace. spec.ts fails 9 rows under the strict read and passes with this one, together with applyDiffTool. failureReporting.spec.ts, 50 tests, exit 0; the whole core subtree on that merge candidate is reported separately below. In 1910's own tree, applyDiffTool.failureReporting with applyPatchTool.execute and editFileTool passes 80/80; eslint --prune-suppressions --max-warnings=0 exits 0 on both touched files and its suppressions rewrite was reverted again; prettier --check is clean on both. Gate: 347 of 500 counted executable lines at base 73931fe with base...head and --unified=0 on the prettier-formatted tree, against 333 at 303af45 - +14 for the pair of commits - measured with v22.23.4. The whole core subtree on that merge candidate ran 197 files, 4250 passed and 9 skipped (4259), against CI's 4250 passed and 9 failed under the strict read. Two pushes to #1910 is two review resets under dismiss_stale_reviews_on_push, and this one could not be folded into the first because the failure only exists in the merge with main.
1 parent c5556a5 commit d5c31a1

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎src/core/tools/ApplyDiffTool.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> {
8181
// which is the fail-closed decision the guarded publish already makes about a target it cannot
8282
// vouch for. The file is reported by name and the first undecodable byte is reported by offset,
8383
// because the refusal is only actionable if the model can find the byte.
84-
const originalBytes = await fs.readFile(absolutePath)
84+
// The union is readFile's, not a cast: this call resolves to the byte overload because no encoding
85+
// is passed, but the encoding overload is what the repo's existing fs doubles answer with -
86+
// applyDiffTool.outsideWorkspace.spec.ts:143 returns "old\n" from it - and a string that came back
87+
// from that overload has already been decoded by whoever asked for it, with the original bytes
88+
// unrecoverable. Those callers keep the pre-change behaviour; the refusal below applies where the
89+
// tool actually holds bytes, which is every real read.
90+
const raw: string | Buffer = await fs.readFile(absolutePath)
91+
const originalBytes = Buffer.isBuffer(raw) ? raw : Buffer.from(raw, "utf-8")
8592
let originalContent: string
8693
try {
8794
originalContent = new TextDecoder("utf-8", { fatal: true }).decode(originalBytes)

‎src/core/tools/__tests__/applyDiffTool.failureReporting.spec.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ vi.mock("fs/promises", () => ({
2121
// ApplyDiffTool reads the file with no encoding, so the call the double stands in for is the
2222
// byte-returning one: fs.readFile returns Buffer unless an encoding is passed. vi.mocked resolves
2323
// the encoding overload, so the mock is typed against the signature the tool actually calls.
24-
const mockedReadFile = vi.mocked(fs.readFile as (path: string) => Promise<Buffer>)
24+
// readFile returns string or Buffer across its overloads; the tool calls the byte overload and the
25+
// double answers both, so the double is typed against the union rather than against one overload.
26+
const mockedReadFile = vi.mocked(fs.readFile as (path: string) => Promise<string | Buffer>)
2527

2628
vi.mock("../../../utils/fs", () => ({
2729
fileExistsAtPath: vi.fn().mockResolvedValue(true),
@@ -152,7 +154,7 @@ describe("ApplyDiffTool failure reporting", () => {
152154
}
153155

154156
describe("bytes the decoder cannot read", () => {
155-
async function runOnBytes(bytes: Buffer): Promise<string> {
157+
async function runOnBytes(bytes: string | Buffer): Promise<string> {
156158
mockedReadFile.mockResolvedValue(bytes)
157159
const applyDiff = vi.fn().mockResolvedValue({ success: true, content: "unused" })
158160
mockTask.diffStrategy = { getName: () => "test", applyDiff }
@@ -202,6 +204,18 @@ describe("ApplyDiffTool failure reporting", () => {
202204
expect(mockTask.diffViewProvider.saveChanges).toHaveBeenCalled()
203205
})
204206

207+
// The tolerance above is not free: it means a caller that pre-decoded the file keeps the lossy
208+
// behaviour. It exists because CI runs main's applyDiffTool.outsideWorkspace.spec.ts against this
209+
// file (the merge ref of 1910 onto main failed 9 of its rows under the strict read), and that spec
210+
// is main's, so this branch cannot edit it. This row is the pin: it says which side gives way.
211+
it("edits a file a caller already decoded, which is what the encoding overload hands back", async () => {
212+
const output = await runOnBytes("line one\nline two\nline three\n")
213+
214+
expect(output).toContain("Changes successfully applied.")
215+
expect(mockTask.consecutiveMistakeCount).toBe(0)
216+
expect(mockTask.diffViewProvider.saveChanges).toHaveBeenCalled()
217+
})
218+
205219
it("reports the first undecodable byte when invalid bytes repeat", async () => {
206220
const output = await runOnBytes(Buffer.from([0x41, 0xc3, 0x28, 0x42, 0xfe, 0x43, 0xff]))
207221

0 commit comments

Comments
 (0)