refactor(core): let permission errors from Code Mode fs through as core reports them

This commit is contained in:
Aiden Cline 2026-11-17 01:02:32 -06:00
parent f9f7f6bb2d
commit 897fdaba36
2 changed files with 3 additions and 17 deletions

View file

@ -11,7 +11,6 @@ import { Environment } from "../environment/index.js"
import { FileAccess } from "../file-access.js"
import { FileWrite } from "../file-write.js"
import { Location } from "../location.js"
import { Permission } from "../permission.js"
import { fileDiff } from "../tool/plugin/file-diff.js"
import { CodeModeExecution } from "./execution.js"
@ -446,17 +445,6 @@ const nodeError = (error: unknown, syscall: string, file: string) => {
path: file,
})
if (error instanceof Environment.Failed && error.cause instanceof Error) return error.cause
// A denial carries Node's EACCES, so a program can tell it from a missing file. The feedback is the user talking to
// the model, so it must reach the program as the error's message.
if (error instanceof Permission.BlockedError || error instanceof Permission.CorrectedError)
return Object.assign(
new Error(
error instanceof Permission.CorrectedError
? `The user rejected ${syscall} ${file}: ${error.feedback}`
: `EACCES: ${error.message}, ${syscall} '${file}'`,
),
{ code: "EACCES", errno: -os.errno.EACCES, syscall, path: file },
)
return error
}

View file

@ -543,7 +543,7 @@ describe("fs", () => {
createCodeMode(new Map(), permissionService(denyEdits), fileServices(tmp.path, denyEdits)).execute(
{
code: `let thrown
try { fs.writeFileSync("a.txt", "x") } catch (e) { thrown = [e.message, e.code] }
try { fs.writeFileSync("a.txt", "x") } catch (e) { thrown = [e.message] }
const [rejected] = await Promise.allSettled([fs.promises.writeFile("b.txt", "x")])
return [thrown, rejected.status, fs.existsSync("a.txt")]`,
},
@ -553,8 +553,6 @@ describe("fs", () => {
const [thrown, status, exists] = JSON.parse(result.output.output)
expect(thrown[0]).toContain("Permission denied")
// EACCES, as Node reports a denied file, so a program can tell it from ENOENT.
expect(thrown[1]).toBe("EACCES")
expect([status, exists]).toEqual(["rejected", false])
expect(await fs.readdir(tmp.path)).toEqual([])
})
@ -741,7 +739,7 @@ describe("fs", () => {
createCodeMode(new Map(), permissionService(denyOutside), fileServices(tmp.path, denyOutside)).execute(
{
code: `fs.writeFileSync("a.txt", "a")
const code = (run) => { try { run(); return "ok" } catch (e) { return e.code } }
const code = (run) => { try { run(); return "ok" } catch (e) { return e.message } }
return [
code(() => fs.rmSync(${at("here.txt")})),
code(() => fs.rmSync(${at("missing.txt")}, { force: true })),
@ -756,7 +754,7 @@ describe("fs", () => {
)
// Every call fails the same way whether or not the outside path exists, and nothing outside changed.
expect(JSON.parse(result.output.output)).toEqual(["EACCES", "EACCES", "EACCES", "EACCES", "EACCES", "EACCES"])
expect(JSON.parse(result.output.output)).toEqual(Array(6).fill("Permission denied: external_directory"))
expect(await Bun.file(path.join(outside.path, "here.txt")).text()).toBe("x")
})
})