From dff6820a95393ce6ef5c73ce4021c3a3700e05b7 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Mon, 16 Nov 2026 23:14:06 -0600 Subject: [PATCH 1/9] feat(core): add Node-style fs and path to Code Mode behind full_codemode --- packages/core/src/codemode/fs.ts | 257 ++++++++++++++++++++++++ packages/core/src/codemode/path.ts | 9 + packages/core/src/codemode/tool.ts | 145 +++++++------ packages/core/src/file-write.ts | 8 +- packages/core/src/session/context.ts | 16 +- packages/core/src/tool.ts | 15 +- packages/core/test/tool-execute.test.ts | 251 ++++++++++++++++++++++- 7 files changed, 631 insertions(+), 70 deletions(-) create mode 100644 packages/core/src/codemode/fs.ts create mode 100644 packages/core/src/codemode/path.ts diff --git a/packages/core/src/codemode/fs.ts b/packages/core/src/codemode/fs.ts new file mode 100644 index 000000000000..e57a90b4064e --- /dev/null +++ b/packages/core/src/codemode/fs.ts @@ -0,0 +1,257 @@ +export * as CodeModeFs from "./fs.js" + +import { Extension } from "@opencode/codemode" +import type { Context as ToolContext } from "@opencode/schema/tool" +import { Effect } from "effect" +import { constants as os } from "os" +import path from "path" +import { fileURLToPath } from "url" +import { Environment } from "../environment/index.js" +import { FileAccess } from "../file-access.js" +import { FileWrite } from "../file-write.js" +import { Permission } from "../permission.js" +import { CodeModeExecution } from "./execution.js" + +/** The services the `fs` global needs from the execute call's Location. */ +export type Services = FileAccess.Service | FileWrite.Service | Environment.Service + +type Host = { + readonly context: ToolContext + readonly access: FileAccess.Interface + readonly fileWrite: FileWrite.Interface + readonly files: Environment.Files +} +type Encoding = BufferEncoding | { readonly encoding?: BufferEncoding | null } | null +type Data = string | Uint8Array +type PathLike = string | URL +type Op = (...args: never[]) => Effect.Effect + +// Each operation is written once and backs both `fs.promises.x` and `fs.xSync`. Reads ask the read permission like the +// read tool; every mutation asks edit for each path it touches, so the same rules and prompts apply. +const ops = { + readFile: op("open", function* (host, file, options?: Encoding) { + const bytes = (yield* host.files.read(yield* read(host, file))).bytes + const encoding = typeof options === "string" ? options : options?.encoding + return encoding ? Buffer.from(bytes).toString(encoding) : bytes + }), + writeFile: op("open", (host, file, data: Data, options?: Encoding) => write(host, file, toBytes(data, options))), + appendFile: op("open", function* (host, file, data: Data, options?: Encoding) { + const current = yield* host.files.read((yield* host.access.resolve({ path: file })).absolute).pipe( + Effect.map((result) => result.bytes), + Effect.catchTag("Environment.NotFound", () => Effect.succeed(new Uint8Array())), + ) + yield* write(host, file, Buffer.concat([current, toBytes(data, options)])) + }), + readdir: op("scandir", function* (host, file, options?: { withFileTypes?: boolean; recursive?: boolean }) { + const entries = yield* walk(host.files, yield* read(host, file), options?.recursive === true) + if (options?.withFileTypes !== true) return entries.map((entry) => entry.path) + return entries.map((entry) => ({ + name: path.basename(entry.path), + parentPath: path.join(file, path.dirname(entry.path)), + ...kind(entry.type), + })) + }), + stat: op("stat", (host, file) => stat(host, file, true)), + lstat: op("lstat", (host, file) => stat(host, file, false)), + access: op("access", function* (host, file) { + yield* Environment.typeFollowing(host.files, yield* read(host, file)) + }), + mkdir: op("mkdir", function* (host, file, options?: { recursive?: boolean }) { + const [absolute] = yield* edit(host, [file]) + if (options?.recursive !== true && (yield* Environment.exists(host.files, absolute))) + return yield* Effect.fail("EEXIST" as const) + yield* host.files.mkdir(absolute) + }), + rm: op("rm", remove), + unlink: op("unlink", (host, file) => remove(host, file)), + rename: op("rename", function* (host, file, to: PathLike) { + const source = (yield* host.access.resolve({ path: file })).absolute + const target = (yield* host.access.resolve({ path: toPath(to) })).absolute + // As in Node: a directory may replace an empty directory; anything else onto a directory fails. + if ((yield* typeOf(host, target)) === "directory") { + if ((yield* typeOf(host, source)) !== "directory") return yield* Effect.fail("EISDIR" as const) + if ((yield* tree(host, target)).length > 0) return yield* Effect.fail("ENOTEMPTY" as const) + } + // Paths inside a moved directory change too, under both their old and new names. + const inside = yield* tree(host, source) + yield* edit(host, [source, target, ...inside.flatMap((item) => [path.join(source, item), path.join(target, item)])]) + if ((yield* typeOf(host, target)) === "directory") yield* host.files.remove(target) + yield* host.files.move(source, target) + }), + copyFile: op("copyfile", function* (host, file, to: PathLike, mode?: number) { + const bytes = (yield* host.files.read(yield* read(host, file))).bytes + const destination = toPath(to) + if ((mode ?? 0) & constants.COPYFILE_EXCL) { + const absolute = (yield* host.access.resolve({ path: destination })).absolute + if ((yield* typeOf(host, absolute)) !== undefined) return yield* Effect.fail("EEXIST" as const) + } + yield* write(host, destination, bytes) + }), +} + +const variants = (wrap: (operation: Op) => Op, suffix: string) => + Object.fromEntries(Object.entries(ops).map(([name, operation]: [string, Op]) => [name + suffix, wrap(operation)])) +const constants = { F_OK: 0, R_OK: 4, W_OK: 2, X_OK: 1, COPYFILE_EXCL: 1 } + +export const extension = Extension.make({ + name: "fs", + globals: { + fs: { + // A copy that isn't marked async is synchronous, and blocks on the Effect it returns. + ...variants((operation) => operation.bind(undefined), "Sync"), + existsSync: op("access", function* (host, file) { + return yield* Environment.exists(host.files, yield* read(host, file)) + }), + constants, + promises: { ...variants(Extension.async, ""), constants }, + }, + }, +}) + +/** What to show for an fs call: the method and the path. */ +export const display = (name: string, args: ReadonlyArray) => ({ + method: name.slice(name.lastIndexOf(".") + 1), + path: String(args[0]), +}) + +function op, A>( + syscall: string, + body: (host: Host, file: string, ...args: Args) => Generator, A, unknown>, +) { + return (input: PathLike, ...args: Args) => { + const file = toPath(input) + return Effect.gen(function* () { + const host = { + context: yield* CodeModeExecution.Service, + access: yield* FileAccess.Service, + fileWrite: yield* FileWrite.Service, + files: (yield* Environment.Service).files, + } + return yield* body(host, file, ...args) + }).pipe(Effect.mapError((error) => nodeError(error, syscall, file))) + } +} + +const toPath = (input: PathLike) => + typeof input === "string" && !input.startsWith("file:") ? input : fileURLToPath(input) + +function* read(host: Host, file: string) { + return (yield* host.access.authorizeRead(file, host.context)).absolute +} + +// One edit request covering every path, after any external-directory approval. +function* edit(host: Host, files: ReadonlyArray) { + const targets = yield* Effect.forEach(files, (file) => host.access.resolve({ path: file, kind: "file" })) + yield* host.access.authorizeExternal(targets, host.context) + yield* host.fileWrite.authorize(targets, host.context) + return targets.map((target) => target.absolute) +} + +// Valid UTF-8 goes through FileWrite like the write tool (diff, BOM, formatting); other bytes are written as they are. +function* write(host: Host, file: string, content: Uint8Array) { + const text = Buffer.from(content).toString("utf8") + if (Buffer.from(text).equals(content)) + return void (yield* host.fileWrite.write({ path: file, content: text }, host.context)) + const [absolute] = yield* edit(host, [file]) + yield* host.files.write(absolute, content) +} + +// Every path inside a removed directory is authorized, so an edit rule for one of them still applies. +function* remove(host: Host, file: string, options?: { recursive?: boolean; force?: boolean }) { + const absolute = (yield* host.access.resolve({ path: file })).absolute + const type = yield* typeOf(host, absolute) + if (type === undefined && options?.force === true) return + if (type === undefined) return yield* new Environment.NotFound({ path: absolute }) + if (type === "directory" && options?.recursive !== true) + return yield* new Environment.WrongKind({ path: absolute, actual: type }) + yield* edit(host, [absolute, ...(yield* tree(host, absolute)).map((item) => path.join(absolute, item))]) + yield* host.files.remove(absolute) +} + +function* stat(host: Host, file: string, follow: boolean) { + const absolute = yield* read(host, file) + const own = yield* host.files.stat(absolute) + // `stat` describes what a symlink points at; reading zero bytes resolves it. A directory target keeps the link's size. + const info = + follow && own.type === "symlink" + ? yield* host.files.read(absolute, { offset: 0, length: 0 }).pipe( + Effect.map((result) => result.info), + Effect.catchTag("Environment.WrongKind", () => Effect.succeed({ ...own, type: "directory" as const })), + ) + : own + return { size: info.size, mtimeMs: info.mtimeMs, mtime: new Date(info.mtimeMs), ...kind(info.type) } +} + +const typeOf = (host: Host, absolute: string) => + host.files.stat(absolute).pipe( + Effect.map((info) => info.type), + Effect.catchTag("Environment.NotFound", () => Effect.undefined), + ) + +// The paths inside a directory, relative to it; nothing for a file. +const tree = (host: Host, absolute: string) => + walk(host.files, absolute, true).pipe( + Effect.map((entries) => entries.map((entry) => entry.path)), + Effect.orElseSucceed(() => []), + ) + +type Entry = { readonly path: string; readonly type: Environment.FileType } + +const walk = ( + files: Environment.Files, + directory: string, + recursive: boolean, + prefix = "", +): Effect.Effect, Environment.NotFound | Environment.WrongKind | Environment.Failed> => + files.list(path.join(directory, prefix)).pipe( + Effect.flatMap((entries) => + Effect.forEach(entries, (entry) => { + const item = { path: path.join(prefix, entry.name), type: entry.type } + if (!recursive || entry.type !== "directory") return Effect.succeed([item]) + return walk(files, directory, true, item.path).pipe(Effect.map((inside) => [item, ...inside])) + }), + ), + Effect.map((items) => items.flat()), + ) + +const kind = (type: Environment.FileType) => ({ + isFile: () => type === "file", + isDirectory: () => type === "directory", + isSymbolicLink: () => type === "symlink", +}) + +const toBytes = (data: Data, options?: Encoding) => + typeof data === "string" + ? Buffer.from(data, (typeof options === "string" ? options : options?.encoding) ?? "utf8") + : data + +const messages = { + ENOENT: "no such file or directory", + EEXIST: "file already exists", + EISDIR: "illegal operation on a directory", + ENOTDIR: "not a directory", + ENOTEMPTY: "directory not empty", +} + +// Errors carry Node's `code`, `syscall` and `path`, so `e.code === "ENOENT"` works. +const nodeError = (error: unknown, syscall: string, file: string) => { + const code = codeOf(error) + if (code) + return Object.assign(new Error(`${code}: ${messages[code]}, ${syscall} '${file}'`), { + code, + errno: -os.errno[code], + syscall, + path: file, + }) + if (error instanceof Environment.Failed && error.cause instanceof Error) return error.cause + // The feedback is the user talking to the model, so it must reach the program as the error's message. + if (error instanceof Permission.CorrectedError) + return new Error(`The user rejected ${syscall} ${file}: ${error.feedback}`) + return error +} + +const codeOf = (error: unknown): keyof typeof messages | undefined => { + if (typeof error === "string" && error in messages) return error as keyof typeof messages + if (error instanceof Environment.NotFound) return "ENOENT" + if (error instanceof Environment.WrongKind) return error.actual === "directory" ? "EISDIR" : "ENOTDIR" +} diff --git a/packages/core/src/codemode/path.ts b/packages/core/src/codemode/path.ts new file mode 100644 index 000000000000..75267424b5b4 --- /dev/null +++ b/packages/core/src/codemode/path.ts @@ -0,0 +1,9 @@ +export * as CodeModePath from "./path.js" + +import { Extension } from "@opencode/codemode" +import path from "path" + +// Only the functions and separators cross: `posix` and `win32` point back at the module, which a global cannot hold. +const { posix: _posix, win32: _win32, ...functions } = path.posix + +export const extension = Extension.make({ name: "path", globals: { path: functions } }) diff --git a/packages/core/src/codemode/tool.ts b/packages/core/src/codemode/tool.ts index 8c757a7c3601..3f68a1c30578 100644 --- a/packages/core/src/codemode/tool.ts +++ b/packages/core/src/codemode/tool.ts @@ -20,6 +20,8 @@ import { Permission } from "../permission.js" import { CodeModeCatalog } from "./catalog.js" import { CodeModeExecution } from "./execution.js" import { CodeModeFetch } from "./fetch.js" +import { CodeModeFs } from "./fs.js" +import { CodeModePath } from "./path.js" const ExecuteFile = Schema.Struct({ data: Schema.String, @@ -74,24 +76,29 @@ export type Inventory = { } // Invariant model-facing guidance; the changing tool catalog is delivered through Instructions. -const description = [ - "Run JavaScript in a confined Code Mode runtime to script tool calls and HTTP requests and compose their results.", - "Write plain JavaScript and send it exactly as written: no code fence, and no JSON or other wrapper around it.", - "`fetch` is available for HTTP requests. Imports, direct filesystem access, and timers are unavailable: there is no `setTimeout` or sleep. All other external access goes through `tools`.", - "Inside `execute`, the only callable tools are those explicitly listed in the Code Mode catalog instructions or returned by the `search` function. Ignore tools shown outside the Code Mode catalog. They are not available in the Code Mode runtime.", - 'Call tools through `tools` using only exact paths and signatures from the catalog. Do not infer or normalize tool names; preserve bracket notation such as `tools.["tool-name"](input)`.', - "Prefer an explicit `return`; if omitted, the final top-level expression becomes the result.", - "Await every call whose completion matters; pending calls are interrupted when execution ends. Run independent calls concurrently with `Promise.allSettled`.", -].join("\n") +const description = (fs: boolean) => + [ + "Run JavaScript in a confined Code Mode runtime to script tool calls and HTTP requests and compose their results.", + "Write plain JavaScript and send it exactly as written: no code fence, and no JSON or other wrapper around it.", + fs + ? "`fetch` is available for HTTP requests, and Node's `fs` (with `fs.promises`) and `path` for files, also importable as `node:fs`, `fs/promises` and `node:path`. Other modules and packages, and timers, are unavailable: there is no `setTimeout` or sleep. All other external access goes through `tools`." + : "`fetch` is available for HTTP requests. Imports, direct filesystem access, and timers are unavailable: there is no `setTimeout` or sleep. All other external access goes through `tools`.", + "Inside `execute`, the only callable tools are those explicitly listed in the Code Mode catalog instructions or returned by the `search` function. Ignore tools shown outside the Code Mode catalog. They are not available in the Code Mode runtime.", + 'Call tools through `tools` using only exact paths and signatures from the catalog. Do not infer or normalize tool names; preserve bracket notation such as `tools.["tool-name"](input)`.', + "Prefer an explicit `return`; if omitted, the final top-level expression becomes the result.", + "Await every call whose completion matters; pending calls are interrupted when execution ends. Run independent calls concurrently with `Promise.allSettled`.", + ].join("\n") export const create = ( inventory: Inventory, executeTool: (name: string, tool: Info, input: unknown, context: ToolContext) => Effect.Effect, permission: Permission.Interface, + /** The Location's file services, which turn on the experimental `fs` and `path` globals. */ + fileServices?: Context.Context, ) => { return { name: "execute", - description, + description: description(fileServices !== undefined), input: CodeMode.Input, freeform: { type: "text" }, output: ExecuteOutput, @@ -122,44 +129,43 @@ export const create = ( yield* Effect.sleep(wait).pipe(Effect.andThen(lock.withPermit(publish)), Effect.forkIn(scope)) }), ) - const result = yield* runtime(inventory) - .execute(code) - .pipe( - Effect.provideService(Run, { - executeTool: (name, tool, input) => - Effect.gen(function* () { - const index = yield* Ref.getAndUpdate(callIndex, (index) => index + 1) - // Its own ID, so plugin hooks and traces can tell the program's calls apart. It has no row, so - // until the program ends its permission requests show on this call's. - const id = CallID.make(`call_${Identifier.ascending()}`) - yield* permission.nested(id, context.id).pipe(Scope.provide(scope)) - // The program sees the same failure text the model gets for a direct call. - const executed = yield* executeTool(name, tool, input, { - ...context, - id, - // Progress replaces the execute call's own, so an inner tool's updates would hide the call rows. - progress: () => Effect.void, - // A stop is the execute call's to handle: a nested call never fails it with its own error. - cancel: undefined, - }).pipe( - Effect.mapError((error) => new ToolFailure({ message: toSessionError(error).message, error })), - ) - const content = resultContent(executed) - const outputFileParts = outputFiles(content) - if (outputFileParts.length > 0) - yield* Ref.update(files, (items) => [...items, { index, files: outputFileParts }]) - return resultValue(executed) - }), - hooks: progressHooks(record), - }), - // A failed or interrupted call keeps its last progress, so send pending rows before it settles. - Effect.onError(() => - lock.withPermit(Ref.get(trailing).pipe(Effect.flatMap((pending) => (pending ? publish : Effect.void)))), - ), - // Readers outlive the fetch call that returned them, up to the end of this program's scope. - Effect.provideService(CodeModeExecution.Service, { ...context, signal: yield* Effect.abortSignal }), - Effect.provideService(Permission.Service, permission), - ) + const program = fileServices + ? fsRuntime(inventory).execute(code).pipe(Effect.provideContext(fileServices)) + : runtime(inventory).execute(code) + const result = yield* program.pipe( + Effect.provideService(Run, { + executeTool: (name, tool, input) => + Effect.gen(function* () { + const index = yield* Ref.getAndUpdate(callIndex, (index) => index + 1) + // Its own ID, so plugin hooks and traces can tell the program's calls apart. It has no row, so + // until the program ends its permission requests show on this call's. + const id = CallID.make(`call_${Identifier.ascending()}`) + yield* permission.nested(id, context.id).pipe(Scope.provide(scope)) + // The program sees the same failure text the model gets for a direct call. + const executed = yield* executeTool(name, tool, input, { + ...context, + id, + // Progress replaces the execute call's own, so an inner tool's updates would hide the call rows. + progress: () => Effect.void, + // A stop is the execute call's to handle: a nested call never fails it with its own error. + cancel: undefined, + }).pipe(Effect.mapError((error) => new ToolFailure({ message: toSessionError(error).message, error }))) + const content = resultContent(executed) + const outputFileParts = outputFiles(content) + if (outputFileParts.length > 0) + yield* Ref.update(files, (items) => [...items, { index, files: outputFileParts }]) + return resultValue(executed) + }), + hooks: progressHooks(record), + }), + // A failed or interrupted call keeps its last progress, so send pending rows before it settles. + Effect.onError(() => + lock.withPermit(Ref.get(trailing).pipe(Effect.flatMap((pending) => (pending ? publish : Effect.void)))), + ), + // Readers outlive the fetch call that returned them, up to the end of this program's scope. + Effect.provideService(CodeModeExecution.Service, { ...context, signal: yield* Effect.abortSignal }), + Effect.provideService(Permission.Service, permission), + ) const toolCalls = yield* Ref.get(calls) const collected = (yield* Ref.get(files)) .toSorted((left, right) => left.index - right.index) @@ -222,6 +228,8 @@ function progressHooks(record: (update: (items: Array) => Array>() +const fsRuntimes = new WeakMap< + Inventory, + CodeMode.Runtime +>() + +const runtime = (inventory: Inventory) => + cached(runtimes, inventory, (tools) => + CodeMode.make({ + tools, + extensions: [CodeModeFetch.extension], + hooks: runHooks, + }), + ) -function runtime(inventory: Inventory) { - const cached = runtimes.get(inventory) - if (cached) return cached - const tools = inventoryTools(inventory) - const created = CodeMode.make({ - tools, - extensions: [CodeModeFetch.extension], - hooks: runHooks, - }) - runtimes.set(inventory, created) +// The runtime with the experimental `fs` and `path` globals. +const fsRuntime = (inventory: Inventory) => + cached(fsRuntimes, inventory, (tools) => + CodeMode.make< + typeof tools, + [typeof CodeModeFetch.extension, typeof CodeModeFs.extension, typeof CodeModePath.extension] + >({ + tools, + extensions: [CodeModeFetch.extension, CodeModeFs.extension, CodeModePath.extension], + hooks: runHooks, + }), + ) + +function cached(cache: WeakMap, inventory: Inventory, make: (tools: Tools) => T) { + const found = cache.get(inventory) + if (found) return found + const created = make(inventoryTools(inventory)) + cache.set(inventory, created) return created } diff --git a/packages/core/src/file-write.ts b/packages/core/src/file-write.ts index 2afb8d3b85df..7b5e04d7e2ad 100644 --- a/packages/core/src/file-write.ts +++ b/packages/core/src/file-write.ts @@ -28,9 +28,9 @@ export interface Interface { file: string, context: FileAccess.Invocation, ) => Effect.Effect - /** Request edit approval for a resolved target, showing the change when it is known. */ + /** Request edit approval for resolved targets in one request, showing the change when it is known. */ readonly authorize: ( - target: FileAccess.Target, + target: FileAccess.Target | ReadonlyArray, context: FileAccess.Invocation, preview?: FileDiff.Info, ) => Effect.Effect @@ -67,13 +67,13 @@ const layer = Layer.effect( }) const authorize = Effect.fn("FileWrite.authorize")(function* ( - target: FileAccess.Target, + target: FileAccess.Target | ReadonlyArray, context: FileAccess.Invocation, preview?: FileDiff.Info, ) { yield* permission.assert({ action: "edit", - resources: [target.resource], + resources: [...new Set((Array.isArray(target) ? target : [target]).map((item) => item.resource))], save: ["*"], metadata: preview ? { files: [preview] } : undefined, ...Permission.invocation(context), diff --git a/packages/core/src/session/context.ts b/packages/core/src/session/context.ts index eea20c76fb22..ce968eaca228 100644 --- a/packages/core/src/session/context.ts +++ b/packages/core/src/session/context.ts @@ -5,8 +5,13 @@ import { Permission } from "../permission.js" import { Context, Effect, Layer } from "effect" import { Agent } from "../agent.js" import type { SessionRequestKind } from "@opencode/plugin/effect/session" +import type { CodeModeFs } from "../codemode/fs.js" import { CodeModeInstructions } from "../codemode/instructions.js" import { Database } from "../database/database.js" +import { Environment } from "../environment/index.js" +import { Flag } from "../flag.js" +import { FileAccess } from "../file-access.js" +import { FileWrite } from "../file-write.js" import { makeLocationNode } from "@opencode/util/effect/app-node" import { InstructionDiscovery } from "../instruction-discovery.js" import { Instructions } from "../instructions/index.js" @@ -90,6 +95,8 @@ const layer = Layer.effect( const skillInstructions = yield* SkillInstructions.Service const store = yield* SessionStore.Service const registry = yield* Tool.Service + const flags = yield* Flag.Service + const codeModeFiles = yield* Effect.context() const resolveModel = ( session: SessionSchema.Info, @@ -146,7 +153,10 @@ const layer = Layer.effect( const permissions = Permission.merge(agent.info.permissions, session.permissions ?? []) const loaded = yield* Effect.all( { - tools: registry.snapshot(permissions), + tools: Effect.gen(function* () { + const fs = yield* flags.enabled("full_codemode") + return yield* registry.snapshot(permissions, fs ? codeModeFiles : undefined) + }), builtins: builtins.load(), discovery: discovery.load(), skills: skillInstructions.load(permissions), @@ -217,6 +227,10 @@ export const node = makeLocationNode({ layer, deps: [ Agent.node, + Flag.node, + Environment.node, + FileAccess.node, + FileWrite.node, Model.node, Database.node, InstructionBuiltIns.node, diff --git a/packages/core/src/tool.ts b/packages/core/src/tool.ts index 9238cb81bf5a..774f2b2bf65a 100644 --- a/packages/core/src/tool.ts +++ b/packages/core/src/tool.ts @@ -9,6 +9,7 @@ import { Context, Effect, Layer, Ref, Result, Schema, SchemaIssue, Types } from import { makeLocationNode } from "@opencode/util/effect/app-node" import type { Agent } from "./agent.js" import { CodeModeCatalog } from "./codemode/catalog.js" +import type { CodeModeFs } from "./codemode/fs.js" import { CodeModeTool } from "./codemode/tool.js" import { Image } from "./image.js" import { Permission } from "./permission.js" @@ -43,7 +44,11 @@ type Data = { export interface Interface extends State.Transformable { readonly list: () => Effect.Effect> - readonly snapshot: (permissions?: Permission.Ruleset) => Effect.Effect + /** `codeModeFiles` turns on the experimental `fs` and `path` globals in Code Mode, acting through those services. */ + readonly snapshot: ( + permissions?: Permission.Ruleset, + codeModeFiles?: Context.Context, + ) => Effect.Effect /** * Runs a Code Mode program with no model step over only the Code Mode tools named in `tools` (still subject to * whole-tool permission filtering and each tool's own executor policy), with no `fetch`. @@ -199,6 +204,7 @@ const layer = Layer.effect( | { data: Data names: string + fs: boolean inventory: CodeModeTool.Inventory value: CodeModeCatalog.Inventory definition: ToolDefinition @@ -262,7 +268,7 @@ const layer = Layer.effect( transform: state.transform, reload: state.reload, list: () => Effect.sync(() => Array.from(state.get().tools.values())), - snapshot: Effect.fn("Tool.snapshot")((permissions) => + snapshot: Effect.fn("Tool.snapshot")((permissions, codeModeFiles) => Effect.sync(() => { const data = state.get() const active = new Map() @@ -281,13 +287,15 @@ const layer = Layer.effect( // Discovery is immutable for a registry revision and visible tool set. Keep executors // fresh, but share the inventory, whose prepared runtime and much larger rendered catalog // are cached by identity, and the execute tool's definition across steps. - const cached = catalog?.data === data && catalog.names === names ? catalog : undefined + const fs = codeModeFiles !== undefined + const cached = catalog?.data === data && catalog.names === names && catalog.fs === fs ? catalog : undefined const codeModeInventory = cached?.inventory ?? { tools: codeModeTools, namespaces: data.namespaces } const codeModeTool = codeModeEnabled ? CodeModeTool.create( codeModeInventory, (name, tool, input, context) => run(codeModeInventory.tools, tool, name, input, context), permission, + codeModeFiles, ) : undefined const codeModeCatalog = !codeModeEnabled @@ -298,6 +306,7 @@ const layer = Layer.effect( catalog = { data, names, + fs, inventory: codeModeInventory, value: codeModeCatalog, definition: codeModeDefinition, diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index 05023200b082..5aaa010c6e6a 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -1,16 +1,31 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test" +import fs from "fs/promises" +import path from "path" +import { pathToFileURL } from "url" +import type { CodeModeFs } from "@opencode/core/codemode/fs" import { CodeModeTool } from "@opencode/core/codemode/tool" +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { Environment } from "@opencode/core/environment/index" +import { FileAccess } from "@opencode/core/file-access" +import { FileWrite } from "@opencode/core/file-write" +import { Formatter } from "@opencode/core/formatter" +import { Location } from "@opencode/core/location" import { Permission } from "@opencode/core/permission" +import { AbsolutePath } from "@opencode/core/schema" import { Tool } from "@opencode/core/tool" import { execute } from "@opencode/core/tool/runtime" import { Agent } from "@opencode/schema/agent" import { Session } from "@opencode/schema/session" import { SessionMessage } from "@opencode/schema/session-message" import type { Info } from "@opencode/schema/tool" -import { Deferred, Effect, Fiber, Schema } from "effect" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { Context, Deferred, Effect, Fiber, Layer, Schema } from "effect" import { TestClock } from "effect/testing" +import { hostEnvironmentLayer } from "./fixture/environment" +import { location } from "./fixture/location" +import { tmpdir } from "./fixture/tmpdir" import { it } from "./lib/effect" -import { permissionService } from "./lib/permission" +import { permissionService, permissionLayer } from "./lib/permission" const context = { sessionID: Session.ID.make("ses_execute"), @@ -22,8 +37,28 @@ const context = { const allowAll = permissionService({ assert: () => Effect.void }) -const createCodeMode = (tools: ReadonlyMap, permission = allowAll) => - CodeModeTool.create({ tools }, (_, tool, input, context) => execute(tool, input, context), permission) +// The file services of a Location at `directory`, asking `permission` like the execute call's own. +const fileServices = (directory: string, permission: Partial = { assert: () => Effect.void }) => + Effect.runSync( + Effect.context().pipe( + Effect.provide( + AppNodeBuilder.build(LayerNode.group([Environment.node, FileAccess.node, FileWrite.node]), [ + Environment.node.replace(hostEnvironmentLayer), + Location.node.replace( + Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))), + ), + Formatter.node.replace(Layer.mock(Formatter.Service, { file: () => Effect.succeed(false) })), + Permission.node.replace(permissionLayer(permission)), + ]), + ), + ), + ) + +const createCodeMode = ( + tools: ReadonlyMap, + permission = allowAll, + files?: Context.Context, +) => CodeModeTool.create({ tools }, (_, tool, input, context) => execute(tool, input, context), permission, files) test("execute takes raw code as freeform text, and Code Mode calls freeform tools with a string", async () => { const input = Schema.Struct({ text: Schema.NonEmptyString }) @@ -405,6 +440,214 @@ describe("fetch permissions", () => { }) }) +describe("fs", () => { + test("without the experimental flag there is no fs or path", async () => { + const result = await Effect.runPromise( + createCodeMode(new Map()).execute({ code: "return [typeof fs, typeof path]" }, context), + ) + + expect(JSON.parse(result.output.output)).toEqual(["undefined", "undefined"]) + }) + + const source = { type: "tool" as const, messageID: context.messageID, id: context.id } + + test("fs reads and writes files in the session's directory, asking read and edit permission as the execute call", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "notes.txt"), "hello") + const asked: Array = [] + const record = { assert: (input: Permission.AssertInput) => Effect.sync(() => void asked.push(input)) } + const result = await Effect.runPromise( + createCodeMode(new Map(), permissionService(record), fileServices(tmp.path, record)).execute( + { + code: `import { readFile } from "node:fs/promises" + const text = await readFile("notes.txt", "utf8") + fs.writeFileSync("out/copy.txt", text + " world") + return [text, fs.readdirSync("out"), path.join("out", "..", "notes.txt")]`, + }, + context, + ), + ) + + expect(JSON.parse(result.output.output)).toEqual(["hello", ["copy.txt"], "notes.txt"]) + expect(await Bun.file(path.join(tmp.path, "out", "copy.txt")).text()).toBe("hello world") + expect(asked.map((input) => [input.action, input.resources, input.sessionID, input.source])).toEqual([ + ["read", ["notes.txt"], context.sessionID, source], + ["edit", ["out/copy.txt"], context.sessionID, source], + ["read", ["out"], context.sessionID, source], + ]) + expect(asked[1]?.metadata).toMatchObject({ files: [{ file: "out/copy.txt", status: "added", additions: 1 }] }) + expect(result.output.toolCalls).toEqual([ + { tool: "fs", status: "completed", input: { method: "readFile", path: "notes.txt" } }, + { tool: "fs", status: "completed", input: { method: "writeFileSync", path: "out/copy.txt" } }, + { tool: "fs", status: "completed", input: { method: "readdirSync", path: "out" } }, + ]) + }) + + test("fs follows Node for copy exclusivity, rename onto directories, nested listings, and file URLs", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "a.txt"), "a") + await Bun.write(path.join(tmp.path, "full/x.txt"), "x") + await Bun.write(path.join(tmp.path, "src/nested/b.txt"), "b") + const result = await Effect.runPromise( + createCodeMode(new Map(), allowAll, fileServices(tmp.path)).execute( + { + code: `const code = (f) => { try { f() } catch (e) { return [e.code, typeof e.errno] } } + fs.mkdirSync("empty") + fs.mkdirSync("moved") + fs.renameSync("empty", "moved") + return [ + code(() => fs.copyFileSync("a.txt", "full/x.txt", fs.constants.COPYFILE_EXCL)), + code(() => fs.renameSync("a.txt", "full")), + code(() => fs.renameSync("moved", "full")), + fs.readdirSync("src", { withFileTypes: true, recursive: true }).map((e) => [e.parentPath, e.name]), + fs.readFileSync(new URL(${JSON.stringify(pathToFileURL(path.join(tmp.path, "a.txt")).href)}), "utf8"), + ]`, + }, + context, + ), + ) + + expect(JSON.parse(result.output.output)).toEqual([ + ["EEXIST", "number"], + ["EISDIR", "number"], + ["ENOTEMPTY", "number"], + [ + ["src", "nested"], + [path.join("src", "nested"), "b.txt"], + ], + "a", + ]) + }) + + test("a denied write never touches disk: the sync call throws and the promise rejects", async () => { + await using tmp = await tmpdir() + const denyEdits = { + assert: (input: Permission.AssertInput) => + input.action === "edit" + ? Effect.fail( + new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources }), + ) + : Effect.void, + } + const result = await Effect.runPromise( + createCodeMode(new Map(), permissionService(denyEdits), fileServices(tmp.path, denyEdits)).execute( + { + code: `let thrown + 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")]`, + }, + context, + ), + ) + + const [thrown, status, exists] = JSON.parse(result.output.output) + expect(thrown).toContain("Permission denied") + expect([status, exists]).toEqual(["rejected", false]) + expect(await fs.readdir(tmp.path)).toEqual([]) + }) + + test("a missing file fails with a Node-shaped ENOENT error", async () => { + await using tmp = await tmpdir() + const result = await Effect.runPromise( + createCodeMode(new Map(), allowAll, fileServices(tmp.path)).execute( + { + code: `try { await fs.promises.readFile("missing.txt") } catch (e) { + return [e instanceof Error, e.code, e.syscall, e.path, e.message] + }`, + }, + context, + ), + ) + + expect(JSON.parse(result.output.output)).toEqual([ + true, + "ENOENT", + "open", + "missing.txt", + "ENOENT: no such file or directory, open 'missing.txt'", + ]) + }) +}) + +test("canonical execution distinguishes declared, model-only, and raw schema outputs", async () => { + const declared: Info = { + name: "declared", + description: "Declared", + input: Schema.Struct({ value: Schema.String }), + output: Schema.Struct({ value: Schema.String }), + execute: ({ value }) => Effect.succeed({ output: { value } }), + } + const modelOnlyInput = Schema.Struct({}) + const modelOnly = { + name: "model_only", + description: "Model only", + input: modelOnlyInput, + execute: () => Effect.succeed({ content: "visible only", metadata: { kind: "model" } }), + } satisfies Info + const raw: Info = { + name: "raw", + description: "Raw", + input: {}, + output: {}, + execute: (input) => Effect.succeed({ output: input, content: "raw" }), + } + + expect(await Effect.runPromise(execute(declared, { value: "encoded" }, context))).toEqual({ + output: { value: "encoded" }, + content: [{ type: "text", text: '{"value":"encoded"}' }], + }) + expect(await Effect.runPromise(execute(modelOnly, {}, context))).toEqual({ + output: undefined, + content: [{ type: "text", text: "visible only" }], + metadata: { kind: "model" }, + }) + expect(await Effect.runPromise(execute(raw, { unchecked: true }, context))).toEqual({ + output: { unchecked: true }, + content: [{ type: "text", text: "raw" }], + }) +}) + +test("declared outputs cannot bypass validation and raw outputs stay JSON-compatible", async () => { + const missing: Info = { + name: "missing", + description: "Missing output", + input: Schema.Struct({}), + output: Schema.String, + execute: () => Effect.succeed({ content: "not an output" }), + } + const invalid: Info = { + name: "invalid", + description: "Invalid raw output", + input: {}, + output: {}, + execute: () => Effect.succeed({ output: 1n, content: "not JSON" }), + } + + expect((await Effect.runPromiseExit(execute(missing, {}, context))).toString()).toContain( + "Tool did not return its declared output", + ) + expect((await Effect.runPromiseExit(execute(invalid, {}, context))).toString()).toContain( + "Tool returned a non-JSON value", + ) +}) + +test("foreign typed failures settle as Tool.Error at the untrusted boundary", async () => { + class ForeignFailure extends Schema.TaggedError()("Plugin.ForeignFailure", { + message: Schema.String, + }) {} + const lying: Info = { + name: "lying", + description: "Fails with a non-Tool.Error typed failure", + input: Schema.Struct({}), + execute: () => new ForeignFailure({ message: "transport died" }) as never, + } + + const error = await Effect.runPromise(execute(lying, {}, context).pipe(Effect.flip)) + expect(error).toBeInstanceOf(Tool.Error) + expect(error.message).toBe("transport died") +}) + test("execute supports callable namespace tools", async () => { const callable: Info = { name: "admin", From 1d42c8c86b759c83ad822f75a1427725d2d46dd6 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Mon, 16 Nov 2026 23:16:49 -0600 Subject: [PATCH 2/9] fix(core): report a denied Code Mode fs call as EACCES --- packages/core/src/codemode/fs.ts | 14 +++++++++++--- packages/core/test/tool-execute.test.ts | 6 ++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/core/src/codemode/fs.ts b/packages/core/src/codemode/fs.ts index e57a90b4064e..4a532ecba4bc 100644 --- a/packages/core/src/codemode/fs.ts +++ b/packages/core/src/codemode/fs.ts @@ -244,9 +244,17 @@ const nodeError = (error: unknown, syscall: string, file: string) => { path: file, }) if (error instanceof Environment.Failed && error.cause instanceof Error) return error.cause - // The feedback is the user talking to the model, so it must reach the program as the error's message. - if (error instanceof Permission.CorrectedError) - return new Error(`The user rejected ${syscall} ${file}: ${error.feedback}`) + // 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 } diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index 5aaa010c6e6a..6d9438f4f676 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -533,7 +533,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 } + try { fs.writeFileSync("a.txt", "x") } catch (e) { thrown = [e.message, e.code] } const [rejected] = await Promise.allSettled([fs.promises.writeFile("b.txt", "x")]) return [thrown, rejected.status, fs.existsSync("a.txt")]`, }, @@ -542,7 +542,9 @@ describe("fs", () => { ) const [thrown, status, exists] = JSON.parse(result.output.output) - expect(thrown).toContain("Permission denied") + 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([]) }) From a57c7dc1e0702a341e327a5d1158277217892dc0 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Mon, 16 Nov 2026 23:25:19 -0600 Subject: [PATCH 3/9] fix(core): approve outside paths before Code Mode fs checks them, and read UTF-8 files as text --- packages/core/src/codemode/fs.ts | 24 ++++++++--- packages/core/test/tool-execute.test.ts | 54 +++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/packages/core/src/codemode/fs.ts b/packages/core/src/codemode/fs.ts index 4a532ecba4bc..5ba706d59c0e 100644 --- a/packages/core/src/codemode/fs.ts +++ b/packages/core/src/codemode/fs.ts @@ -32,10 +32,15 @@ const ops = { readFile: op("open", function* (host, file, options?: Encoding) { const bytes = (yield* host.files.read(yield* read(host, file))).bytes const encoding = typeof options === "string" ? options : options?.encoding - return encoding ? Buffer.from(bytes).toString(encoding) : bytes + if (encoding) return Buffer.from(bytes).toString(encoding) + // Node returns a Buffer, whose toString() is UTF-8, so `JSON.parse(fs.readFileSync(p))` works. Programs have no + // Buffer, so text comes back as text and only other bytes as a Uint8Array. + const text = Buffer.from(bytes).toString("utf8") + return Buffer.from(text).equals(bytes) ? text : bytes }), writeFile: op("open", (host, file, data: Data, options?: Encoding) => write(host, file, toBytes(data, options))), appendFile: op("open", function* (host, file, data: Data, options?: Encoding) { + yield* outside(host, [file]) const current = yield* host.files.read((yield* host.access.resolve({ path: file })).absolute).pipe( Effect.map((result) => result.bytes), Effect.catchTag("Environment.NotFound", () => Effect.succeed(new Uint8Array())), @@ -65,8 +70,7 @@ const ops = { rm: op("rm", remove), unlink: op("unlink", (host, file) => remove(host, file)), rename: op("rename", function* (host, file, to: PathLike) { - const source = (yield* host.access.resolve({ path: file })).absolute - const target = (yield* host.access.resolve({ path: toPath(to) })).absolute + const [source, target] = yield* outside(host, [file, toPath(to)]) // As in Node: a directory may replace an empty directory; anything else onto a directory fails. if ((yield* typeOf(host, target)) === "directory") { if ((yield* typeOf(host, source)) !== "directory") return yield* Effect.fail("EISDIR" as const) @@ -82,8 +86,8 @@ const ops = { const bytes = (yield* host.files.read(yield* read(host, file))).bytes const destination = toPath(to) if ((mode ?? 0) & constants.COPYFILE_EXCL) { - const absolute = (yield* host.access.resolve({ path: destination })).absolute - if ((yield* typeOf(host, absolute)) !== undefined) return yield* Effect.fail("EEXIST" as const) + const [absolute] = yield* outside(host, [destination]) + if ((yield* typeOf(host, absolute!)) !== undefined) return yield* Effect.fail("EEXIST" as const) } yield* write(host, destination, bytes) }), @@ -139,6 +143,14 @@ function* read(host: Host, file: string) { return (yield* host.access.authorizeRead(file, host.context)).absolute } +// Approves paths outside the project before anything looks at them on disk, so a program can't learn what exists +// there without approval. +function* outside(host: Host, files: ReadonlyArray) { + const targets = yield* Effect.forEach(files, (file) => host.access.resolve({ path: file })) + yield* host.access.authorizeExternal(targets, host.context) + return targets.map((target) => target.absolute) +} + // One edit request covering every path, after any external-directory approval. function* edit(host: Host, files: ReadonlyArray) { const targets = yield* Effect.forEach(files, (file) => host.access.resolve({ path: file, kind: "file" })) @@ -158,7 +170,7 @@ function* write(host: Host, file: string, content: Uint8Array) { // Every path inside a removed directory is authorized, so an edit rule for one of them still applies. function* remove(host: Host, file: string, options?: { recursive?: boolean; force?: boolean }) { - const absolute = (yield* host.access.resolve({ path: file })).absolute + const [absolute] = yield* outside(host, [file]) const type = yield* typeOf(host, absolute) if (type === undefined && options?.force === true) return if (type === undefined) return yield* new Environment.NotFound({ path: absolute }) diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index 6d9438f4f676..7e05e6915791 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -570,6 +570,60 @@ describe("fs", () => { "ENOENT: no such file or directory, open 'missing.txt'", ]) }) + + test("readFile without an encoding gives text for UTF-8 files and bytes otherwise", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "data.json"), '{"a":1}') + await Bun.write(path.join(tmp.path, "image.bin"), new Uint8Array([0xff, 0xfe, 0x00])) + const result = await Effect.runPromise( + createCodeMode(new Map(), allowAll, fileServices(tmp.path)).execute( + { + code: `const bin = fs.readFileSync("image.bin") + return [JSON.parse(fs.readFileSync("data.json")).a, fs.readFileSync("data.json").toString(), bin instanceof Uint8Array, [...bin]]`, + }, + context, + ), + ) + + expect(JSON.parse(result.output.output)).toEqual([1, '{"a":1}', true, [255, 254, 0]]) + }) + + test("a path outside the project is approved before anything checks it on disk", async () => { + await using tmp = await tmpdir() + await using outside = await tmpdir() + await Bun.write(path.join(outside.path, "here.txt"), "x") + await Bun.write(path.join(outside.path, "full", "file.txt"), "x") + const denyOutside = { + assert: (input: Permission.AssertInput) => + input.action === "external_directory" + ? Effect.fail( + new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources }), + ) + : Effect.void, + } + const at = (name: string) => JSON.stringify(path.join(outside.path, name)) + const result = await Effect.runPromise( + 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 } } + return [ + code(() => fs.rmSync(${at("here.txt")})), + code(() => fs.rmSync(${at("missing.txt")}, { force: true })), + code(() => fs.rmSync(${at("missing.txt")})), + code(() => fs.renameSync("a.txt", ${at("full")})), + code(() => fs.copyFileSync("a.txt", ${at("here.txt")}, fs.constants.COPYFILE_EXCL)), + code(() => fs.appendFileSync(${at("here.txt")}, "y")), + ]`, + }, + context, + ), + ) + + // 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(await Bun.file(path.join(outside.path, "here.txt")).text()).toBe("x") + }) }) test("canonical execution distinguishes declared, model-only, and raw schema outputs", async () => { From fa0d9bb6ec70a22a28845a8b3f1aa7c196ad8a73 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Mon, 16 Nov 2026 23:36:11 -0600 Subject: [PATCH 4/9] feat(core): add mkdtemp, rmdir, truncate, cp and glob to Code Mode fs, and return Buffers from readFile --- packages/core/src/codemode/fs.ts | 201 +++++++++++++++++++++--- packages/core/test/tool-execute.test.ts | 90 +++++++++++ packages/util/src/glob.ts | 4 +- 3 files changed, 267 insertions(+), 28 deletions(-) diff --git a/packages/core/src/codemode/fs.ts b/packages/core/src/codemode/fs.ts index 5ba706d59c0e..55feca91c701 100644 --- a/packages/core/src/codemode/fs.ts +++ b/packages/core/src/codemode/fs.ts @@ -6,6 +6,7 @@ import { Effect } from "effect" import { constants as os } from "os" import path from "path" import { fileURLToPath } from "url" +import { Glob } from "@opencode/util/glob" import { Environment } from "../environment/index.js" import { FileAccess } from "../file-access.js" import { FileWrite } from "../file-write.js" @@ -32,40 +33,70 @@ const ops = { readFile: op("open", function* (host, file, options?: Encoding) { const bytes = (yield* host.files.read(yield* read(host, file))).bytes const encoding = typeof options === "string" ? options : options?.encoding - if (encoding) return Buffer.from(bytes).toString(encoding) - // Node returns a Buffer, whose toString() is UTF-8, so `JSON.parse(fs.readFileSync(p))` works. Programs have no - // Buffer, so text comes back as text and only other bytes as a Uint8Array. - const text = Buffer.from(bytes).toString("utf8") - return Buffer.from(text).equals(bytes) ? text : bytes + return encoding ? Buffer.from(bytes).toString(encoding) : Buffer.from(bytes) }), - writeFile: op("open", (host, file, data: Data, options?: Encoding) => write(host, file, toBytes(data, options))), - appendFile: op("open", function* (host, file, data: Data, options?: Encoding) { - yield* outside(host, [file]) - const current = yield* host.files.read((yield* host.access.resolve({ path: file })).absolute).pipe( - Effect.map((result) => result.bytes), - Effect.catchTag("Environment.NotFound", () => Effect.succeed(new Uint8Array())), - ) - yield* write(host, file, Buffer.concat([current, toBytes(data, options)])) + writeFile: op("open", function* (host, file, data: Data, options?: Encoding | { readonly flag?: string }) { + const flag = typeof options === "object" && options !== null && "flag" in options ? options.flag : undefined + if (flag?.startsWith("a")) return yield* append(host, file, toBytes(data, encodingOf(options))) + if (flag?.includes("x")) { + const [absolute] = yield* outside(host, [file]) + if ((yield* typeOf(host, absolute!)) !== undefined) return yield* Effect.fail("EEXIST" as const) + } + yield* write(host, file, toBytes(data, encodingOf(options))) }), + appendFile: op("open", (host, file, data: Data, options?: Encoding) => append(host, file, toBytes(data, options))), readdir: op("scandir", function* (host, file, options?: { withFileTypes?: boolean; recursive?: boolean }) { const entries = yield* walk(host.files, yield* read(host, file), options?.recursive === true) if (options?.withFileTypes !== true) return entries.map((entry) => entry.path) - return entries.map((entry) => ({ - name: path.basename(entry.path), - parentPath: path.join(file, path.dirname(entry.path)), - ...kind(entry.type), - })) + return entries.map((entry) => + dirent(path.basename(entry.path), path.join(file, path.dirname(entry.path)), entry.type), + ) + }), + stat: op("stat", function* (host, file, options?: { throwIfNoEntry?: boolean }) { + return yield* missingIsUndefined(stat(host, file, true), options) + }), + lstat: op("lstat", function* (host, file, options?: { throwIfNoEntry?: boolean }) { + return yield* missingIsUndefined(stat(host, file, false), options) }), - stat: op("stat", (host, file) => stat(host, file, true)), - lstat: op("lstat", (host, file) => stat(host, file, false)), access: op("access", function* (host, file) { yield* Environment.typeFollowing(host.files, yield* read(host, file)) }), mkdir: op("mkdir", function* (host, file, options?: { recursive?: boolean }) { + if (options?.recursive === true) return yield* mkdirp(host, file) const [absolute] = yield* edit(host, [file]) - if (options?.recursive !== true && (yield* Environment.exists(host.files, absolute))) - return yield* Effect.fail("EEXIST" as const) - yield* host.files.mkdir(absolute) + if ((yield* typeOf(host, absolute!)) !== undefined) return yield* Effect.fail("EEXIST" as const) + if ((yield* typeOf(host, path.dirname(absolute!))) === undefined) return yield* Effect.fail("ENOENT" as const) + yield* host.files.mkdir(absolute!) + }), + // A new directory named `prefix` plus six random characters. + mkdtemp: op("mkdtemp", function* (host, prefix) { + for (;;) { + const directory = + prefix + Array.from(crypto.getRandomValues(new Uint8Array(6)), (byte) => alphabet[byte % 62]).join("") + const [absolute] = yield* edit(host, [directory]) + if ((yield* typeOf(host, absolute!)) !== undefined) continue + if ((yield* typeOf(host, path.dirname(absolute!))) === undefined) return yield* Effect.fail("ENOENT" as const) + yield* host.files.mkdir(absolute!) + return directory + } + }), + rmdir: op("rmdir", function* (host, file, options?: { recursive?: boolean }) { + const [absolute] = yield* outside(host, [file]) + const type = yield* typeOf(host, absolute!) + if (type === undefined) return yield* Effect.fail("ENOENT" as const) + if (type !== "directory") return yield* Effect.fail("ENOTDIR" as const) + const inside = yield* tree(host, absolute!) + if (inside.length > 0 && options?.recursive !== true) return yield* Effect.fail("ENOTEMPTY" as const) + yield* edit(host, [absolute!, ...inside.map((item) => path.join(absolute!, item))]) + yield* host.files.remove(absolute!) + }), + truncate: op("open", function* (host, file, length?: number) { + const [absolute] = yield* edit(host, [file]) + const bytes = (yield* host.files.read(absolute!)).bytes + const size = Math.max(0, length ?? 0) + const next = new Uint8Array(size) + next.set(bytes.subarray(0, size)) + yield* host.files.write(absolute!, next) }), rm: op("rm", remove), unlink: op("unlink", (host, file) => remove(host, file)), @@ -91,6 +122,59 @@ const ops = { } yield* write(host, destination, bytes) }), + cp: op( + "cp", + function* (host, file, to: PathLike, options?: { recursive?: boolean; force?: boolean; errorOnExist?: boolean }) { + const destination = toPath(to) + const [source] = yield* outside(host, [file]) + const type = yield* typeOf(host, yield* read(host, file)) + if (type === undefined) return yield* Effect.fail("ENOENT" as const) + const overwrite = options?.force !== false + const copyOne = function* (from: string, into: string) { + const bytes = (yield* host.files.read(yield* read(host, from))).bytes + const [absolute] = yield* outside(host, [into]) + if ((yield* typeOf(host, absolute!)) !== undefined && !overwrite) { + if (options?.errorOnExist === true) return yield* Effect.fail("EEXIST" as const) + return + } + yield* write(host, into, bytes) + } + if (type !== "directory") return yield* copyOne(file, destination) + if (options?.recursive !== true) return yield* Effect.fail("EISDIR" as const) + const entries = yield* walk(host.files, source!, true) + for (const entry of entries.filter((item) => item.type === "directory")) + yield* mkdirp(host, path.join(destination, entry.path)) + yield* mkdirp(host, destination) + for (const entry of entries.filter((item) => item.type !== "directory")) + yield* copyOne(path.join(file, entry.path), path.join(destination, entry.path)) + }, + ), + glob: op( + "scandir", + function* (host, pattern, options?: { cwd?: string; exclude?: ReadonlyArray; withFileTypes?: boolean }) { + const cwd = options?.cwd ?? "." + const segments = pattern.split("/") + const literal = segments.findIndex((segment) => /[*?[\]{}()!+@]/.test(segment)) + // The fixed part of the pattern is where listing starts, and what read permission is asked for. + const base = path.join(cwd, (literal < 0 ? segments : segments.slice(0, literal)).join("/") || ".") + const root = yield* read(host, base) + const shown = (relative: string) => path.join(path.relative(cwd, base), relative) + const matches = (relative: string) => + Glob.match(pattern, shown(relative), { dot: false }) && + !(options?.exclude ?? []).some((item) => Glob.match(item, shown(relative), { dot: false })) + if (literal < 0) { + const type = yield* typeOf(host, root) + if (type === undefined) return [] + return [options?.withFileTypes === true ? dirent(path.basename(base), path.dirname(base), type) : pattern] + } + const entries = (yield* walk(host.files, root, pattern.includes("**"))).filter((entry) => matches(entry.path)) + if (options?.withFileTypes === true) + return entries.map((entry) => + dirent(path.basename(entry.path), path.join(base, path.dirname(entry.path)), entry.type), + ) + return entries.map((entry) => shown(entry.path)) + }, + ), } const variants = (wrap: (operation: Op) => Op, suffix: string) => @@ -103,9 +187,11 @@ export const extension = Extension.make({ fs: { // A copy that isn't marked async is synchronous, and blocks on the Effect it returns. ...variants((operation) => operation.bind(undefined), "Sync"), - existsSync: op("access", function* (host, file) { - return yield* Environment.exists(host.files, yield* read(host, file)) - }), + // As in Node, existsSync never throws: a path it may not check doesn't exist. + existsSync: (file: PathLike) => + op("access", function* (host, file) { + return yield* Environment.exists(host.files, yield* read(host, file)) + })(file).pipe(Effect.orElseSucceed(() => false)), constants, promises: { ...variants(Extension.async, ""), constants }, }, @@ -168,6 +254,50 @@ function* write(host: Host, file: string, content: Uint8Array) { yield* host.files.write(absolute, content) } +// Appends under the path's lock, so concurrent appends to one file keep every chunk. +function* append(host: Host, file: string, bytes: Uint8Array) { + const [absolute] = yield* outside(host, [file]) + const current = yield* host.files.read(absolute!).pipe( + Effect.map((result) => result.bytes), + Effect.catchTag("Environment.NotFound", () => Effect.succeed(new Uint8Array())), + ) + yield* write(host, file, Buffer.concat([current, bytes])) +} + +// A recursive mkdir: like Node, it returns the first directory it created, written the way the path was given, or +// undefined when the directory already existed. +function* mkdirp(host: Host, file: string): Generator, string | undefined, unknown> { + const [absolute] = yield* edit(host, [file]) + const existing = yield* typeOf(host, absolute!) + if (existing === "directory") return undefined + if (existing !== undefined) return yield* Effect.fail("EEXIST" as const) + const created = yield* firstMissing(host, absolute!) + yield* host.files.mkdir(absolute!) + return path.join(file, path.relative(absolute!, created)) +} + +// The first directory that doesn't exist yet on the way to `absolute`. +function* firstMissing(host: Host, absolute: string): Generator, string, unknown> { + const parent = path.dirname(absolute) + if (parent === absolute) return absolute + const type = yield* typeOf(host, parent) + if (type === undefined) return yield* firstMissing(host, parent) + if (type !== "directory" && type !== "symlink") return yield* Effect.fail("ENOTDIR" as const) + return absolute +} + +// `throwIfNoEntry: false` makes a missing path undefined instead of ENOENT. +const missingIsUndefined = ( + effect: Generator, A, unknown>, + options?: { readonly throwIfNoEntry?: boolean }, +) => + Effect.gen(() => effect).pipe( + Effect.catchIf( + (error) => options?.throwIfNoEntry === false && error instanceof Environment.NotFound, + () => Effect.undefined, + ), + ) + // Every path inside a removed directory is authorized, so an edit rule for one of them still applies. function* remove(host: Host, file: string, options?: { recursive?: boolean; force?: boolean }) { const [absolute] = yield* outside(host, [file]) @@ -230,8 +360,27 @@ const kind = (type: Environment.FileType) => ({ isFile: () => type === "file", isDirectory: () => type === "directory", isSymbolicLink: () => type === "symlink", + isFIFO: () => false, + isSocket: () => false, + isBlockDevice: () => false, + isCharacterDevice: () => false, }) +// Node's Dirent: `path` is the deprecated name of `parentPath`. +const dirent = (name: string, parentPath: string, type: Environment.FileType) => ({ + name, + parentPath, + path: parentPath, + ...kind(type), +}) + +const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + +const encodingOf = (options: Encoding | { readonly flag?: string } | undefined) => + typeof options === "string" + ? options + : (options as { readonly encoding?: BufferEncoding | null } | null | undefined)?.encoding + const toBytes = (data: Data, options?: Encoding) => typeof data === "string" ? Buffer.from(data, (typeof options === "string" ? options : options?.encoding) ?? "utf8") diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index 7e05e6915791..e91afe7dfe60 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -571,6 +571,96 @@ describe("fs", () => { ]) }) + const runFs = async (directory: string, code: string, permission = allowAll, files = fileServices(directory)) => { + const result = await Effect.runPromise(createCodeMode(new Map(), permission, files).execute({ code }, context)) + return JSON.parse(result.output.output) + } + + test("mkdir, mkdtemp, rmdir and truncate behave like Node", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "long.txt"), "abcdef") + expect( + await runFs( + tmp.path, + `const first = fs.mkdirSync("a/b/c", { recursive: true }) + const again = fs.mkdirSync("a/b/c", { recursive: true }) + let missingParent + try { fs.mkdirSync("x/y") } catch (e) { missingParent = e.code } + const temp = fs.mkdtempSync("tmp-") + let notEmpty + try { fs.rmdirSync("a") } catch (e) { notEmpty = e.code } + fs.rmdirSync("a", { recursive: true }) + fs.truncateSync("long.txt", 3) + return [first, again ?? null, missingParent, /^tmp-[A-Za-z0-9]{6}$/.test(temp), fs.statSync(temp).isDirectory(), + notEmpty, fs.existsSync("a"), fs.readFileSync("long.txt", "utf8")]`, + ), + ).toEqual(["a", null, "ENOENT", true, true, "ENOTEMPTY", false, "abc"]) + }) + + test("cp copies files and folders, and glob matches paths like Node", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "src", "a.ts"), "a") + await Bun.write(path.join(tmp.path, "src", "nested", "b.ts"), "b") + await Bun.write(path.join(tmp.path, "src", "nested", "c.md"), "c") + await Bun.write(path.join(tmp.path, "src", ".hidden", "d.ts"), "d") + expect( + await runFs( + tmp.path, + `fs.cpSync("src", "copy", { recursive: true }) + fs.cpSync("src/a.ts", "one.ts") + let needsRecursive + try { fs.cpSync("src", "nope") } catch (e) { needsRecursive = e.code } + return [fs.globSync("copy/**/*.ts").sort(), fs.readFileSync("one.ts", "utf8"), needsRecursive, + fs.globSync("src/*.ts"), fs.globSync("src/**/*", { exclude: ["**/*.md"] }).sort(), + fs.globSync("src/a.ts", { withFileTypes: true }).map((d) => [d.name, d.isFile()])]`, + ), + ).toEqual([ + ["copy/a.ts", "copy/nested/b.ts"], + "a", + "EISDIR", + ["src/a.ts"], + ["src/a.ts", "src/nested", "src/nested/b.ts"], + [["a.ts", true]], + ]) + }) + + test("readFile returns a Buffer, stat can skip a missing path, and writeFile takes append and exclusive flags", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "log.txt"), "one") + expect( + await runFs( + tmp.path, + `fs.writeFileSync("log.txt", "two", { flag: "a" }) + let exclusive + try { fs.writeFileSync("log.txt", "x", { flag: "wx" }) } catch (e) { exclusive = e.code } + const entry = fs.readdirSync(".", { withFileTypes: true })[0] + return [Buffer.isBuffer(fs.readFileSync("log.txt")), fs.readFileSync("log.txt").toString(), exclusive, + fs.statSync("missing", { throwIfNoEntry: false }) ?? null, [entry.name, entry.parentPath, entry.isFIFO()]]`, + ), + ).toEqual([true, "onetwo", "EEXIST", null, ["log.txt", ".", false]]) + }) + + test("existsSync is false for a path it may not check, instead of throwing", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "secret.txt"), "x") + const denyReads = { + assert: (input: Permission.AssertInput) => + input.action === "read" + ? Effect.fail( + new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources }), + ) + : Effect.void, + } + expect( + await runFs( + tmp.path, + `return fs.existsSync("secret.txt")`, + permissionService(denyReads), + fileServices(tmp.path, denyReads), + ), + ).toBe(false) + }) + test("readFile without an encoding gives text for UTF-8 files and bytes otherwise", async () => { await using tmp = await tmpdir() await Bun.write(path.join(tmp.path, "data.json"), '{"a":1}') diff --git a/packages/util/src/glob.ts b/packages/util/src/glob.ts index 45e69e05a664..316b730618fc 100644 --- a/packages/util/src/glob.ts +++ b/packages/util/src/glob.ts @@ -26,7 +26,7 @@ export namespace Glob { return glob(pattern, toGlobOptions(options)) } - export function match(pattern: string, filepath: string): boolean { - return minimatch(filepath, pattern, { dot: true }) + export function match(pattern: string, filepath: string, options: { dot?: boolean } = {}): boolean { + return minimatch(filepath, pattern, { dot: options.dot ?? true }) } } From a968a76592e306c388f29af68a81ee3a1d20a2ad Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Mon, 16 Nov 2026 23:44:24 -0600 Subject: [PATCH 5/9] fix(core): match glob wildcards below the first segment and keep absolute patterns absolute --- packages/core/src/codemode/fs.ts | 27 ++++++++++++++++--------- packages/core/test/tool-execute.test.ts | 5 ++++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/core/src/codemode/fs.ts b/packages/core/src/codemode/fs.ts index 55feca91c701..24bc95fe8eb8 100644 --- a/packages/core/src/codemode/fs.ts +++ b/packages/core/src/codemode/fs.ts @@ -46,7 +46,7 @@ const ops = { }), appendFile: op("open", (host, file, data: Data, options?: Encoding) => append(host, file, toBytes(data, options))), readdir: op("scandir", function* (host, file, options?: { withFileTypes?: boolean; recursive?: boolean }) { - const entries = yield* walk(host.files, yield* read(host, file), options?.recursive === true) + const entries = yield* walk(host.files, yield* read(host, file), options?.recursive === true ? Infinity : 1) if (options?.withFileTypes !== true) return entries.map((entry) => entry.path) return entries.map((entry) => dirent(path.basename(entry.path), path.join(file, path.dirname(entry.path)), entry.type), @@ -141,7 +141,7 @@ const ops = { } if (type !== "directory") return yield* copyOne(file, destination) if (options?.recursive !== true) return yield* Effect.fail("EISDIR" as const) - const entries = yield* walk(host.files, source!, true) + const entries = yield* walk(host.files, source!, Infinity) for (const entry of entries.filter((item) => item.type === "directory")) yield* mkdirp(host, path.join(destination, entry.path)) yield* mkdirp(host, destination) @@ -155,10 +155,14 @@ const ops = { const cwd = options?.cwd ?? "." const segments = pattern.split("/") const literal = segments.findIndex((segment) => /[*?[\]{}()!+@]/.test(segment)) - // The fixed part of the pattern is where listing starts, and what read permission is asked for. - const base = path.join(cwd, (literal < 0 ? segments : segments.slice(0, literal)).join("/") || ".") + // The fixed part of the pattern is where listing starts, and what read permission is asked for. An absolute + // pattern keeps its root and gives absolute paths, as in Node. + const fixed = + (literal < 0 ? segments : segments.slice(0, literal)).join("/") || (path.isAbsolute(pattern) ? "/" : ".") + const base = path.isAbsolute(pattern) ? fixed : path.join(cwd, fixed) const root = yield* read(host, base) - const shown = (relative: string) => path.join(path.relative(cwd, base), relative) + const shown = (relative: string) => + path.isAbsolute(pattern) ? path.join(base, relative) : path.join(path.relative(cwd, base), relative) const matches = (relative: string) => Glob.match(pattern, shown(relative), { dot: false }) && !(options?.exclude ?? []).some((item) => Glob.match(item, shown(relative), { dot: false })) @@ -167,7 +171,9 @@ const ops = { if (type === undefined) return [] return [options?.withFileTypes === true ? dirent(path.basename(base), path.dirname(base), type) : pattern] } - const entries = (yield* walk(host.files, root, pattern.includes("**"))).filter((entry) => matches(entry.path)) + // Deep enough for every segment after the fixed part; `**` can match any depth. + const depth = pattern.includes("**") ? Infinity : segments.length - literal + const entries = (yield* walk(host.files, root, depth)).filter((entry) => matches(entry.path)) if (options?.withFileTypes === true) return entries.map((entry) => dirent(path.basename(entry.path), path.join(base, path.dirname(entry.path)), entry.type), @@ -332,25 +338,26 @@ const typeOf = (host: Host, absolute: string) => // The paths inside a directory, relative to it; nothing for a file. const tree = (host: Host, absolute: string) => - walk(host.files, absolute, true).pipe( + walk(host.files, absolute, Infinity).pipe( Effect.map((entries) => entries.map((entry) => entry.path)), Effect.orElseSucceed(() => []), ) type Entry = { readonly path: string; readonly type: Environment.FileType } +// Lists `depth` levels below `directory`: 1 is its own entries, Infinity everything. const walk = ( files: Environment.Files, directory: string, - recursive: boolean, + depth: number, prefix = "", ): Effect.Effect, Environment.NotFound | Environment.WrongKind | Environment.Failed> => files.list(path.join(directory, prefix)).pipe( Effect.flatMap((entries) => Effect.forEach(entries, (entry) => { const item = { path: path.join(prefix, entry.name), type: entry.type } - if (!recursive || entry.type !== "directory") return Effect.succeed([item]) - return walk(files, directory, true, item.path).pipe(Effect.map((inside) => [item, ...inside])) + if (depth <= 1 || entry.type !== "directory") return Effect.succeed([item]) + return walk(files, directory, depth - 1, item.path).pipe(Effect.map((inside) => [item, ...inside])) }), ), Effect.map((items) => items.flat()), diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index e91afe7dfe60..e5ad58e543df 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -612,7 +612,8 @@ describe("fs", () => { try { fs.cpSync("src", "nope") } catch (e) { needsRecursive = e.code } return [fs.globSync("copy/**/*.ts").sort(), fs.readFileSync("one.ts", "utf8"), needsRecursive, fs.globSync("src/*.ts"), fs.globSync("src/**/*", { exclude: ["**/*.md"] }).sort(), - fs.globSync("src/a.ts", { withFileTypes: true }).map((d) => [d.name, d.isFile()])]`, + fs.globSync("src/a.ts", { withFileTypes: true }).map((d) => [d.name, d.isFile()]), + fs.globSync("src/*/b.ts"), fs.globSync(path.join(${JSON.stringify(tmp.path)}, "src", "*/*.md"))]`, ), ).toEqual([ ["copy/a.ts", "copy/nested/b.ts"], @@ -621,6 +622,8 @@ describe("fs", () => { ["src/a.ts"], ["src/a.ts", "src/nested", "src/nested/b.ts"], [["a.ts", true]], + ["src/nested/b.ts"], + [path.join(tmp.path, "src", "nested", "c.md")], ]) }) From f9f7f6bb2dbf9763e9df8e76255315110592d922 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Tue, 17 Nov 2026 00:41:30 -0600 Subject: [PATCH 6/9] fix(core): write exact bytes from Code Mode fs, and resolve path against the session directory --- packages/core/src/codemode/fs.ts | 84 +++++++++++++++++-------- packages/core/src/codemode/path.ts | 27 +++++++- packages/core/test/tool-execute.test.ts | 54 ++++++++++++++-- 3 files changed, 131 insertions(+), 34 deletions(-) diff --git a/packages/core/src/codemode/fs.ts b/packages/core/src/codemode/fs.ts index 24bc95fe8eb8..5856e9ba60f3 100644 --- a/packages/core/src/codemode/fs.ts +++ b/packages/core/src/codemode/fs.ts @@ -10,11 +10,13 @@ import { Glob } from "@opencode/util/glob" 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" /** The services the `fs` global needs from the execute call's Location. */ -export type Services = FileAccess.Service | FileWrite.Service | Environment.Service +export type Services = FileAccess.Service | FileWrite.Service | Environment.Service | Location.Service type Host = { readonly context: ToolContext @@ -22,7 +24,8 @@ type Host = { readonly fileWrite: FileWrite.Interface readonly files: Environment.Files } -type Encoding = BufferEncoding | { readonly encoding?: BufferEncoding | null } | null +// "buffer" asks for a Buffer where Node would otherwise return a string (readdir names, readlink, realpath). +type Encoding = BufferEncoding | "buffer" | { readonly encoding?: BufferEncoding | "buffer" | null } | null type Data = string | Uint8Array type PathLike = string | URL type Op = (...args: never[]) => Effect.Effect @@ -33,7 +36,7 @@ const ops = { readFile: op("open", function* (host, file, options?: Encoding) { const bytes = (yield* host.files.read(yield* read(host, file))).bytes const encoding = typeof options === "string" ? options : options?.encoding - return encoding ? Buffer.from(bytes).toString(encoding) : Buffer.from(bytes) + return encoding && encoding !== "buffer" ? Buffer.from(bytes).toString(encoding) : Buffer.from(bytes) }), writeFile: op("open", function* (host, file, data: Data, options?: Encoding | { readonly flag?: string }) { const flag = typeof options === "object" && options !== null && "flag" in options ? options.flag : undefined @@ -45,9 +48,15 @@ const ops = { yield* write(host, file, toBytes(data, encodingOf(options))) }), appendFile: op("open", (host, file, data: Data, options?: Encoding) => append(host, file, toBytes(data, options))), - readdir: op("scandir", function* (host, file, options?: { withFileTypes?: boolean; recursive?: boolean }) { - const entries = yield* walk(host.files, yield* read(host, file), options?.recursive === true ? Infinity : 1) - if (options?.withFileTypes !== true) return entries.map((entry) => entry.path) + readdir: op("scandir", function* (host, file, options?: Encoding | { withFileTypes?: boolean; recursive?: boolean }) { + const settings = typeof options === "object" && options !== null ? options : {} + const entries = yield* walk( + host.files, + yield* read(host, file), + "recursive" in settings && settings.recursive === true ? Infinity : 1, + ) + if (!("withFileTypes" in settings) || settings.withFileTypes !== true) + return entries.map((entry) => (encodingOf(options) === "buffer" ? Buffer.from(entry.path) : entry.path)) return entries.map((entry) => dirent(path.basename(entry.path), path.join(file, path.dirname(entry.path)), entry.type), ) @@ -93,9 +102,8 @@ const ops = { truncate: op("open", function* (host, file, length?: number) { const [absolute] = yield* edit(host, [file]) const bytes = (yield* host.files.read(absolute!)).bytes - const size = Math.max(0, length ?? 0) - const next = new Uint8Array(size) - next.set(bytes.subarray(0, size)) + const next = new Uint8Array(Math.max(0, length ?? 0)) + next.set(bytes.subarray(0, next.length)) yield* host.files.write(absolute!, next) }), rm: op("rm", remove), @@ -251,23 +259,44 @@ function* edit(host: Host, files: ReadonlyArray) { return targets.map((target) => target.absolute) } -// Valid UTF-8 goes through FileWrite like the write tool (diff, BOM, formatting); other bytes are written as they are. +// Writes exactly these bytes, as Node does: no formatting and no BOM handling. The edit request shows the change as a +// diff when both sides are text, like the write tool's. A missing parent directory is ENOENT, as in Node. function* write(host: Host, file: string, content: Uint8Array) { - const text = Buffer.from(content).toString("utf8") - if (Buffer.from(text).equals(content)) - return void (yield* host.fileWrite.write({ path: file, content: text }, host.context)) - const [absolute] = yield* edit(host, [file]) - yield* host.files.write(absolute, content) + const prepared = yield* prepare(host, file, () => content) + yield* host.files.write(prepared.target, content) } -// Appends under the path's lock, so concurrent appends to one file keep every chunk. function* append(host: Host, file: string, bytes: Uint8Array) { - const [absolute] = yield* outside(host, [file]) - const current = yield* host.files.read(absolute!).pipe( + const joined = (current: Uint8Array | undefined) => Buffer.concat([current ?? new Uint8Array(), bytes]) + const prepared = yield* prepare(host, file, joined) + yield* host.files.write(prepared.target, joined(prepared.current)) +} + +// Resolves a file a write will change, asks edit permission with the resulting change, and checks Node's preconditions. +function* prepare(host: Host, file: string, next: (current: Uint8Array | undefined) => Uint8Array) { + const target = yield* host.fileWrite.target(file, host.context) + const current = yield* host.files.read(target.absolute).pipe( Effect.map((result) => result.bytes), - Effect.catchTag("Environment.NotFound", () => Effect.succeed(new Uint8Array())), + Effect.catchTag("Environment.NotFound", () => Effect.undefined), ) - yield* write(host, file, Buffer.concat([current, bytes])) + if (current === undefined && (yield* typeOf(host, path.dirname(target.absolute))) === undefined) + return yield* Effect.fail("ENOENT" as const) + const before = text(current ?? new Uint8Array()) + const after = text(next(current)) + yield* host.fileWrite.authorize( + target, + host.context, + before !== undefined && after !== undefined + ? fileDiff(target.resource, before, after, current ? "modified" : "added") + : undefined, + ) + return { target: target.absolute, current } +} + +// Bytes as text when they are valid UTF-8. +const text = (bytes: Uint8Array) => { + const decoded = Buffer.from(bytes).toString("utf8") + return Buffer.from(decoded).equals(bytes) ? decoded : undefined } // A recursive mkdir: like Node, it returns the first directory it created, written the way the path was given, or @@ -381,16 +410,21 @@ const dirent = (name: string, parentPath: string, type: Environment.FileType) => ...kind(type), }) +const textEncoding = (encoding: BufferEncoding | "buffer" | null | undefined): BufferEncoding => + encoding && encoding !== "buffer" ? encoding : "utf8" + const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" -const encodingOf = (options: Encoding | { readonly flag?: string } | undefined) => - typeof options === "string" - ? options - : (options as { readonly encoding?: BufferEncoding | null } | null | undefined)?.encoding +// The encoding an options argument names: a string, or an object's `encoding`. +const encodingOf = (options: unknown): BufferEncoding | "buffer" | undefined => { + if (typeof options === "string") return options as BufferEncoding | "buffer" + if (typeof options !== "object" || options === null || !("encoding" in options)) return undefined + return typeof options.encoding === "string" ? (options.encoding as BufferEncoding | "buffer") : undefined +} const toBytes = (data: Data, options?: Encoding) => typeof data === "string" - ? Buffer.from(data, (typeof options === "string" ? options : options?.encoding) ?? "utf8") + ? Buffer.from(data, textEncoding(typeof options === "string" ? options : options?.encoding)) : data const messages = { diff --git a/packages/core/src/codemode/path.ts b/packages/core/src/codemode/path.ts index 75267424b5b4..473193e6a954 100644 --- a/packages/core/src/codemode/path.ts +++ b/packages/core/src/codemode/path.ts @@ -1,9 +1,30 @@ export * as CodeModePath from "./path.js" import { Extension } from "@opencode/codemode" +import { Effect } from "effect" import path from "path" +import { Location } from "../location.js" -// Only the functions and separators cross: `posix` and `win32` point back at the module, which a global cannot hold. -const { posix: _posix, win32: _win32, ...functions } = path.posix +// Only functions and separators cross; `posix` and `win32` point back at their module, which a global can't hold, so +// each becomes a copy of its own functions. Programs have no `process.cwd()`: resolve and relative start from the +// session's directory, where `fs` resolves relative paths too. +const flavor = (module: path.PlatformPath) => { + const { posix: _posix, win32: _win32, ...functions } = module + return { + ...functions, + resolve: (...parts: Array) => + Location.Service.use((location) => Effect.succeed(module.resolve(location.directory, ...parts))), + relative: (from: string, to: string) => + Location.Service.use((location) => + Effect.succeed( + module.relative(module.resolve(location.directory, from), module.resolve(location.directory, to)), + ), + ), + } +} -export const extension = Extension.make({ name: "path", globals: { path: functions } }) +/** The host's own path module, as Node gives it, with `path.posix` and `path.win32`. */ +export const extension = Extension.make({ + name: "path", + globals: { path: { ...flavor(path), posix: flavor(path.posix), win32: flavor(path.win32) } }, +}) diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index e5ad58e543df..e557cda54506 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -38,16 +38,20 @@ const context = { const allowAll = permissionService({ assert: () => Effect.void }) // The file services of a Location at `directory`, asking `permission` like the execute call's own. -const fileServices = (directory: string, permission: Partial = { assert: () => Effect.void }) => +const fileServices = ( + directory: string, + permission: Partial = { assert: () => Effect.void }, + formatter: Formatter.Interface["file"] = () => Effect.succeed(false), +) => Effect.runSync( Effect.context().pipe( Effect.provide( - AppNodeBuilder.build(LayerNode.group([Environment.node, FileAccess.node, FileWrite.node]), [ + AppNodeBuilder.build(LayerNode.group([Environment.node, FileAccess.node, FileWrite.node, Location.node]), [ Environment.node.replace(hostEnvironmentLayer), Location.node.replace( Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))), ), - Formatter.node.replace(Layer.mock(Formatter.Service, { file: () => Effect.succeed(false) })), + Formatter.node.replace(Layer.mock(Formatter.Service, { file: formatter })), Permission.node.replace(permissionLayer(permission)), ]), ), @@ -461,23 +465,29 @@ describe("fs", () => { { code: `import { readFile } from "node:fs/promises" const text = await readFile("notes.txt", "utf8") + let missingParent + try { fs.writeFileSync("none/copy.txt", "x") } catch (e) { missingParent = e.code } + fs.mkdirSync("out") fs.writeFileSync("out/copy.txt", text + " world") - return [text, fs.readdirSync("out"), path.join("out", "..", "notes.txt")]`, + return [text, missingParent, fs.readdirSync("out"), path.join("out", "..", "notes.txt")]`, }, context, ), ) - expect(JSON.parse(result.output.output)).toEqual(["hello", ["copy.txt"], "notes.txt"]) + expect(JSON.parse(result.output.output)).toEqual(["hello", "ENOENT", ["copy.txt"], "notes.txt"]) expect(await Bun.file(path.join(tmp.path, "out", "copy.txt")).text()).toBe("hello world") expect(asked.map((input) => [input.action, input.resources, input.sessionID, input.source])).toEqual([ ["read", ["notes.txt"], context.sessionID, source], + ["edit", ["out"], context.sessionID, source], ["edit", ["out/copy.txt"], context.sessionID, source], ["read", ["out"], context.sessionID, source], ]) - expect(asked[1]?.metadata).toMatchObject({ files: [{ file: "out/copy.txt", status: "added", additions: 1 }] }) + expect(asked[2]?.metadata).toMatchObject({ files: [{ file: "out/copy.txt", status: "added", additions: 1 }] }) expect(result.output.toolCalls).toEqual([ { tool: "fs", status: "completed", input: { method: "readFile", path: "notes.txt" } }, + { tool: "fs", status: "error", input: { method: "writeFileSync", path: "none/copy.txt" } }, + { tool: "fs", status: "completed", input: { method: "mkdirSync", path: "out" } }, { tool: "fs", status: "completed", input: { method: "writeFileSync", path: "out/copy.txt" } }, { tool: "fs", status: "completed", input: { method: "readdirSync", path: "out" } }, ]) @@ -643,6 +653,38 @@ describe("fs", () => { ).toEqual([true, "onetwo", "EEXIST", null, ["log.txt", ".", false]]) }) + test("writes keep exactly the bytes written: no formatting, no BOM handling, no parent directories", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "bom.txt"), "\uFEFFold") + // A formatter that rewrites every file it's given, so a write that ran it would read back "formatted". + const formatter = (file: string) => Effect.promise(() => fs.writeFile(file, "formatted")).pipe(Effect.as(true)) + const result = await runFs( + tmp.path, + `fs.writeFileSync("bom.txt", "new") + fs.writeFileSync("ugly.ts", "const x =1") + let missing + try { fs.writeFileSync("no/such/dir.txt", "x") } catch (e) { missing = e.code } + fs.appendFileSync("ugly.ts", ";") + return [missing]`, + allowAll, + fileServices(tmp.path, undefined, formatter), + ) + expect(result).toEqual(["ENOENT"]) + expect([...(await fs.readFile(path.join(tmp.path, "bom.txt")))]).toEqual([...Buffer.from("new")]) + expect(await fs.readFile(path.join(tmp.path, "ugly.ts"), "utf8")).toBe("const x =1;") + }) + + test("path is the host's path module, resolving against the session's directory", async () => { + await using tmp = await tmpdir() + const [resolved, relative, posix, win32, sep] = await runFs( + tmp.path, + `return [path.resolve("a", "b"), path.relative("a", "c"), path.posix.join("a", "b"), path.win32.join("a", "b"), path.sep]`, + ) + expect(resolved).toBe(path.resolve(tmp.path, "a", "b")) + expect(relative).toBe(path.join("..", "c")) + expect([posix, win32, sep]).toEqual(["a/b", "a\\b", path.sep]) + }) + test("existsSync is false for a path it may not check, instead of throwing", async () => { await using tmp = await tmpdir() await Bun.write(path.join(tmp.path, "secret.txt"), "x") From 897fdaba3616005df0d2ea5c56f8ef89424e8ab3 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Tue, 17 Nov 2026 01:02:32 -0600 Subject: [PATCH 7/9] refactor(core): let permission errors from Code Mode fs through as core reports them --- packages/core/src/codemode/fs.ts | 12 ------------ packages/core/test/tool-execute.test.ts | 8 +++----- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/packages/core/src/codemode/fs.ts b/packages/core/src/codemode/fs.ts index 5856e9ba60f3..504a835ad76e 100644 --- a/packages/core/src/codemode/fs.ts +++ b/packages/core/src/codemode/fs.ts @@ -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 } diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index e557cda54506..4af5cd9c827e 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -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") }) }) From 45da483dde38d94cafc5e7bd5ac7b18804e3b80a Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Tue, 17 Nov 2026 01:26:07 -0600 Subject: [PATCH 8/9] fix(core): append and truncate on the host for Code Mode fs, so concurrent appends keep every chunk --- packages/core/src/codemode/fs.ts | 7 ++--- .../core/src/environment/exec-defaults.ts | 6 ++++ packages/core/src/environment/files.ts | 4 +++ packages/core/src/environment/local.ts | 2 ++ packages/core/src/environment/memory.ts | 29 +++++++++++++++++++ packages/core/src/workspace.ts | 2 ++ .../core/test/lib/environment-conformance.ts | 14 +++++++++ packages/core/test/tool-execute.test.ts | 11 +++++++ 8 files changed, 70 insertions(+), 5 deletions(-) diff --git a/packages/core/src/codemode/fs.ts b/packages/core/src/codemode/fs.ts index 504a835ad76e..4c39d7781a06 100644 --- a/packages/core/src/codemode/fs.ts +++ b/packages/core/src/codemode/fs.ts @@ -100,10 +100,7 @@ const ops = { }), truncate: op("open", function* (host, file, length?: number) { const [absolute] = yield* edit(host, [file]) - const bytes = (yield* host.files.read(absolute!)).bytes - const next = new Uint8Array(Math.max(0, length ?? 0)) - next.set(bytes.subarray(0, next.length)) - yield* host.files.write(absolute!, next) + yield* host.files.truncate(absolute!, Math.max(0, length ?? 0)) }), rm: op("rm", remove), unlink: op("unlink", (host, file) => remove(host, file)), @@ -268,7 +265,7 @@ function* write(host: Host, file: string, content: Uint8Array) { function* append(host: Host, file: string, bytes: Uint8Array) { const joined = (current: Uint8Array | undefined) => Buffer.concat([current ?? new Uint8Array(), bytes]) const prepared = yield* prepare(host, file, joined) - yield* host.files.write(prepared.target, joined(prepared.current)) + yield* host.files.append(prepared.target, bytes) } // Resolves a file a write will change, asks edit permission with the resulting change, and checks Node's preconditions. diff --git a/packages/core/src/environment/exec-defaults.ts b/packages/core/src/environment/exec-defaults.ts index 9e147a605cad..29501ee688a6 100644 --- a/packages/core/src/environment/exec-defaults.ts +++ b/packages/core/src/environment/exec-defaults.ts @@ -145,6 +145,12 @@ export const execDefaults = (spawner: ChildProcessSpawner["Service"]): FilesImpl move: (from, to) => run(from, moveScript, [to]).pipe(Effect.flatMap((result) => classifyPlain(from, result, () => undefined))), mkdir: (path) => run(path, `mkdir -p -- "$1"`).pipe(Effect.flatMap((result) => complete(path, result))), + append: (path, bytes) => + run(path, `cat >> "$1"`, [], bytes).pipe(Effect.flatMap((result) => complete(path, result))), + truncate: (path, length) => + run(path, `[ -e "$1" ] || exit ${NOT_FOUND}; truncate -s "$2" -- "$1"`, [String(length)]).pipe( + Effect.flatMap((result) => classifyPlain(path, result, () => undefined)), + ), } } diff --git a/packages/core/src/environment/files.ts b/packages/core/src/environment/files.ts index aae4d6d075af..d1f36c350278 100644 --- a/packages/core/src/environment/files.ts +++ b/packages/core/src/environment/files.ts @@ -47,6 +47,10 @@ export interface FilesImpl { readonly remove: (path: string) => Effect.Effect readonly move: (from: string, to: string) => Effect.Effect readonly mkdir: (path: string) => Effect.Effect + /** Adds bytes to the end of a file, creating it when missing. */ + readonly append: (path: string, bytes: Uint8Array) => Effect.Effect + /** Cuts or zero-extends a file to `length` bytes. */ + readonly truncate: (path: string, length: number) => Effect.Effect } export interface Files extends FilesImpl {} diff --git a/packages/core/src/environment/local.ts b/packages/core/src/environment/local.ts index d0c246c4708e..60c0faf0255b 100644 --- a/packages/core/src/environment/local.ts +++ b/packages/core/src/environment/local.ts @@ -64,6 +64,8 @@ export const makeLocalDriver = (spawner: ChildProcessSpawner["Service"]): Driver yield* attempt(from, () => fs.rename(from, destination)) }), mkdir: (value) => attempt(value, () => fs.mkdir(value, { recursive: true }).then(() => undefined)), + append: (value, bytes) => attempt(value, () => fs.appendFile(value, bytes)), + truncate: (value, length) => attempt(value, () => fs.truncate(value, length), true), } return { spawner, overrides } diff --git a/packages/core/src/environment/memory.ts b/packages/core/src/environment/memory.ts index c7f23115645e..897e67eab3bb 100644 --- a/packages/core/src/environment/memory.ts +++ b/packages/core/src/environment/memory.ts @@ -60,6 +60,18 @@ export const makeMemoryDriver = (): MemoryDriver => { nodes.set(target, { type: "directory", mtimeMs: Date.now() }) } const failed = (value: string, cause: unknown) => new Failed({ path: value, cause }) + // Rewrites a file's bytes through symlinks, creating the file when missing. + const change = (value: string, update: (current: Uint8Array) => Uint8Array) => + Effect.try({ + try: () => { + const target = resolveKey(value, true) ?? key(value) + const current = nodes.get(target) + if (current && current.type !== "file") throw new Error(`Path is not a file: ${value}`) + if (!current) requireParent(target) + nodes.set(target, { type: "file", bytes: update(current?.bytes ?? new Uint8Array()), mtimeMs: Date.now() }) + }, + catch: (cause) => failed(value, cause), + }) const overrides: FilesImpl = { stat: (value) => Effect.suspend(() => { @@ -137,6 +149,23 @@ export const makeMemoryDriver = (): MemoryDriver => { }) }), mkdir: (value) => Effect.try({ try: () => mkdirSync(value), catch: (cause) => failed(value, cause) }), + append: (value, bytes) => + change(value, (current) => { + const next = new Uint8Array(current.length + bytes.length) + next.set(current) + next.set(bytes, current.length) + return next + }), + truncate: (value, length) => + Effect.gen(function* () { + const target = resolveKey(value, true) + if (target === undefined || !nodes.has(target)) return yield* new NotFound({ path: value }) + yield* change(value, (current) => { + const next = new Uint8Array(length) + next.set(current.subarray(0, length)) + return next + }) + }), } const spawner = make((command) => diff --git a/packages/core/src/workspace.ts b/packages/core/src/workspace.ts index c5619ebca546..0b6349d73ff1 100644 --- a/packages/core/src/workspace.ts +++ b/packages/core/src/workspace.ts @@ -310,6 +310,8 @@ const layer = (options: Options) => remove: (path) => withFiles(path, (files) => files.remove(path)), move: (from, to) => withFiles(from, (files) => files.move(from, to)), mkdir: (path) => withFiles(path, (files) => files.mkdir(path)), + append: (path, bytes) => withFiles(path, (files) => files.append(path, bytes)), + truncate: (path, length) => withFiles(path, (files) => files.truncate(path, length)), } return { spawner, overrides } }), diff --git a/packages/core/test/lib/environment-conformance.ts b/packages/core/test/lib/environment-conformance.ts index 63d96924c72a..80c2d7d3a96c 100644 --- a/packages/core/test/lib/environment-conformance.ts +++ b/packages/core/test/lib/environment-conformance.ts @@ -178,6 +178,20 @@ export const environmentConformance = ( }), ) + check("appends and truncates", (harness) => + Effect.gen(function* () { + const target = `${harness.root}/bytes.txt` + yield* harness.files.append(target, bytes("abc")) + yield* harness.files.append(target, bytes("def")) + expect(text((yield* harness.files.read(target)).bytes)).toBe("abcdef") + yield* harness.files.truncate(target, 2) + expect(text((yield* harness.files.read(target)).bytes)).toBe("ab") + yield* harness.files.truncate(target, 4) + expect([...(yield* harness.files.read(target)).bytes]).toEqual([97, 98, 0, 0]) + expect(yield* Effect.flip(harness.files.truncate(`${harness.root}/missing`, 0))).toBeInstanceOf(NotFound) + }), + ) + check("moves files and removes trees idempotently", (harness) => Effect.gen(function* () { const source = `${harness.root}/source/file` diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index 4af5cd9c827e..f962ba775cd9 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -672,6 +672,17 @@ describe("fs", () => { expect(await fs.readFile(path.join(tmp.path, "ugly.ts"), "utf8")).toBe("const x =1;") }) + test("concurrent appends to one file keep every chunk", async () => { + await using tmp = await tmpdir() + await runFs( + tmp.path, + `await Promise.all(Array.from({ length: 20 }, (_, i) => fs.promises.appendFile("log.txt", i + "\\n"))) + return []`, + ) + const lines = (await fs.readFile(path.join(tmp.path, "log.txt"), "utf8")).trim().split("\n") + expect(lines.map(Number).toSorted((a, b) => a - b)).toEqual(Array.from({ length: 20 }, (_, i) => i)) + }) + test("path is the host's path module, resolving against the session's directory", async () => { await using tmp = await tmpdir() const [resolved, relative, posix, win32, sep] = await runFs( From 55619e90c2382550848b6d217cdff8fc2a619e73 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Tue, 17 Nov 2026 01:31:16 -0600 Subject: [PATCH 9/9] perf(core): show only the appended lines in a Code Mode fs append's edit request, without reading the file --- packages/core/src/codemode/fs.ts | 29 +++++++++++-------------- packages/core/test/tool-execute.test.ts | 17 +++++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/packages/core/src/codemode/fs.ts b/packages/core/src/codemode/fs.ts index 4c39d7781a06..5c245674dbbd 100644 --- a/packages/core/src/codemode/fs.ts +++ b/packages/core/src/codemode/fs.ts @@ -258,35 +258,32 @@ function* edit(host: Host, files: ReadonlyArray) { // Writes exactly these bytes, as Node does: no formatting and no BOM handling. The edit request shows the change as a // diff when both sides are text, like the write tool's. A missing parent directory is ENOENT, as in Node. function* write(host: Host, file: string, content: Uint8Array) { - const prepared = yield* prepare(host, file, () => content) - yield* host.files.write(prepared.target, content) + yield* host.files.write(yield* prepare(host, file, content, false), content) } function* append(host: Host, file: string, bytes: Uint8Array) { - const joined = (current: Uint8Array | undefined) => Buffer.concat([current ?? new Uint8Array(), bytes]) - const prepared = yield* prepare(host, file, joined) - yield* host.files.append(prepared.target, bytes) + yield* host.files.append(yield* prepare(host, file, bytes, true), bytes) } -// Resolves a file a write will change, asks edit permission with the resulting change, and checks Node's preconditions. -function* prepare(host: Host, file: string, next: (current: Uint8Array | undefined) => Uint8Array) { +// Resolves a file a write will change, checks Node's preconditions, and asks edit permission with the change as a diff +// when it is text. A replacing write diffs against the current file, as the write tool does. An append never reads the +// file: its diff shows only the bytes being added. +function* prepare(host: Host, file: string, content: Uint8Array, appending: boolean) { const target = yield* host.fileWrite.target(file, host.context) - const current = yield* host.files.read(target.absolute).pipe( - Effect.map((result) => result.bytes), - Effect.catchTag("Environment.NotFound", () => Effect.undefined), - ) - if (current === undefined && (yield* typeOf(host, path.dirname(target.absolute))) === undefined) + const type = yield* typeOf(host, target.absolute) + if (type === "directory") return yield* Effect.fail("EISDIR" as const) + if (type === undefined && (yield* typeOf(host, path.dirname(target.absolute))) === undefined) return yield* Effect.fail("ENOENT" as const) - const before = text(current ?? new Uint8Array()) - const after = text(next(current)) + const before = appending || type === undefined ? "" : text((yield* host.files.read(target.absolute)).bytes) + const after = text(content) yield* host.fileWrite.authorize( target, host.context, before !== undefined && after !== undefined - ? fileDiff(target.resource, before, after, current ? "modified" : "added") + ? fileDiff(target.resource, before, after, type === undefined ? "added" : "modified") : undefined, ) - return { target: target.absolute, current } + return target.absolute } // Bytes as text when they are valid UTF-8. diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index f962ba775cd9..ca02bbdd7547 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -672,6 +672,23 @@ describe("fs", () => { expect(await fs.readFile(path.join(tmp.path, "ugly.ts"), "utf8")).toBe("const x =1;") }) + test("an append asks edit with only the new lines, not a diff of the whole file", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "log.txt"), "old line 1\nold line 2\n") + const asked: Array = [] + const record = { assert: (input: Permission.AssertInput) => Effect.sync(() => void asked.push(input)) } + await runFs( + tmp.path, + `fs.appendFileSync("log.txt", "new line\\n"); return []`, + permissionService(record), + fileServices(tmp.path, record), + ) + const edit = asked.find((input) => input.action === "edit") + expect(edit?.metadata).toMatchObject({ files: [{ file: "log.txt", status: "modified", additions: 1, deletions: 0 }] }) + expect(JSON.stringify(edit?.metadata)).not.toContain("old line") + expect(await fs.readFile(path.join(tmp.path, "log.txt"), "utf8")).toBe("old line 1\nold line 2\nnew line\n") + }) + test("concurrent appends to one file keep every chunk", async () => { await using tmp = await tmpdir() await runFs(