diff --git a/packages/core/src/codemode/bun.ts b/packages/core/src/codemode/bun.ts new file mode 100644 index 000000000000..ba255dca1b4b --- /dev/null +++ b/packages/core/src/codemode/bun.ts @@ -0,0 +1,519 @@ +export * as CodeModeBun from "./bun.js" + +import { Extension } from "@opencode/codemode" +import { Glob } from "@opencode/util/glob" +import { isUtf8 } from "buffer" +import { Context, Effect, Option } from "effect" +import { constants } from "os" +import path from "path" +import { fileURLToPath } from "url" +import { Environment } from "../environment/index.js" +import { FileAccess } from "../file-access.js" +import { FileMutation } from "../file-mutation.js" +import { FileWrite } from "../file-write.js" +import { fileDiff } from "../tool/plugin/file-diff.js" +import { CodeModeExecution } from "./execution.js" + +/** The session's file services, which the `Bun` global reads and writes through. */ +export type Services = FileAccess.Service | FileWrite.Service | FileMutation.Service | Environment.Service + +type Host = { + readonly context: FileAccess.Invocation + readonly access: FileAccess.Interface + readonly fileWrite: FileWrite.Interface + readonly fileMutation: FileMutation.Interface + readonly files: Environment.Files +} +type Range = { readonly offset: number; readonly length: number } + +// What a BunFile names: the path as the program wrote it, where it resolved to when `Bun.file` asked to read it, and +// the part of the file a slice covers. +type Handle = { readonly name: string; readonly target: FileAccess.Target; readonly range?: Range } + +// A BunFile reaches a program as a copy and comes back as another, but its functions come back as themselves: its +// `bytes` function says which file a value passed to `Bun.write` is. +const handles = new WeakMap() + +// A run gets `Bun` only with the session's file services (see `CodeModeTool.create`). +const service = (key: Context.Key) => + Effect.flatMap(Effect.serviceOption(key), (found) => + Option.isSome(found) ? Effect.succeed(found.value) : Effect.die("Code Mode Bun needs the session's file services."), + ) + +// One file operation, acting for the execute call as a tool does. A permission error reaches the program as core +// reports it; any other failure carries Node's `code`, `syscall` and `path`, so `e.code === "ENOENT"` works. +const call = (syscall: string, name: string, body: (host: Host) => Effect.Effect) => + Effect.gen(function* () { + return yield* body({ + context: yield* CodeModeExecution.Service, + access: yield* service(FileAccess.Service), + fileWrite: yield* service(FileWrite.Service), + fileMutation: yield* service(FileMutation.Service), + files: (yield* service(Environment.Service)).files, + }) + }).pipe(Effect.mapError((error) => nodeError(error, syscall, name))) + +// `Bun.file` asks read permission, and external-directory approval outside the project, once: the file's own reads +// don't ask again, and its writes ask only edit. Bun reads `size` and `lastModified` when a program asks for them. A +// program's copy of the file can't ask later, so they are read here. A missing file has Bun's values for one. +const open = (input: string | URL, options?: { readonly type?: string }) => { + supports("Bun.file", options, ["type"]) + const name = toPath(input) + return call("stat", name, (host) => + Effect.gen(function* () { + const target = yield* host.access.authorizeRead(name, host.context) + const info = yield* followed(host, target.absolute).pipe( + Effect.catchTag("Environment.NotFound", () => Effect.undefined), + ) + return file( + { name, target }, + options?.type ?? mime(name), + info?.size ?? 0, + Math.trunc(info?.mtimeMs ?? 2 ** 52 - 1), + ) + }), + ) +} + +// A BunFile's own asynchronous function. A function inside a result is called synchronously, and would make the +// program wait for its Effect and throw its failure. Handing the Effect back still pending gives the program a +// promise instead, which a failure rejects. +const later = + , A, E, R>(run: (...args: Args) => Effect.Effect) => + (...args: Args) => + Effect.succeed(run(...args)) + +// The part of Bun's BunFile that is implemented. +const file = (handle: Handle, type: string, size: number, lastModified: number) => { + const bytes = later(() => read(handle)) + handles.set(bytes, handle) + const unlink = later(() => remove(handle)) + // As Blob.slice: a negative index counts from the end, and a string in any position is the new type. + const index = (value: number | string | undefined, fallback: number) => { + if (typeof value !== "number") return fallback + return value < 0 ? Math.max(size + value, 0) : Math.min(value, size) + } + return { + name: handle.name, + type, + size, + lastModified, + text: later(() => read(handle).pipe(Effect.map(decode))), + // The program catches the SyntaxError JSON.parse throws, as it would from Bun. + json: later(() => + read(handle).pipe( + Effect.flatMap((bytes) => + Effect.try({ try: (): unknown => JSON.parse(decode(bytes)), catch: (error) => error }), + ), + ), + ), + bytes, + // The runtime has no ArrayBuffer: one crosses to a program as a Uint8Array. + arrayBuffer: later(() => read(handle)), + exists: later(() => exists(handle)), + stat: later(() => stat(handle)), + slice: (...args: ReadonlyArray): unknown => { + const start = index(args[0], 0) + const length = Math.max(index(args[1], size) - start, 0) + return file( + { ...handle, range: { offset: (handle.range?.offset ?? 0) + start, length } }, + args.find((item) => typeof item === "string") ?? type, + length, + lastModified, + ) + }, + write: later((data: unknown) => write(handle, data)), + delete: unlink, + unlink, + writer: (options?: { readonly highWaterMark?: number }) => writer(handle, options), + } +} + +const read = (handle: Handle) => + call("open", handle.name, (host) => + Effect.map( + host.files.read(handle.target.absolute, handle.range), + // A plain Uint8Array over the same memory: a driver may give a Buffer, which Bun's `bytes()` never does. + (result) => new Uint8Array(result.bytes.buffer, result.bytes.byteOffset, result.bytes.byteLength), + ), + ) + +// As in Bun, only a file exists: a directory doesn't. +const exists = (handle: Handle) => + call("access", handle.name, (host) => + Environment.typeFollowing(host.files, handle.target.absolute).pipe( + Effect.map((type) => type === "file"), + Effect.catchTag("Environment.NotFound", () => Effect.succeed(false)), + ), + ) + +// The part of Node's Stats that the session's files can tell. +const stat = (handle: Handle) => + call("stat", handle.name, (host) => + Effect.map(followed(host, handle.target.absolute), (info) => ({ + size: info.size, + mtimeMs: info.mtimeMs, + mtime: new Date(info.mtimeMs), + isFile: () => info.type === "file", + isDirectory: () => info.type === "directory", + isSymbolicLink: () => info.type === "symlink", + })), + ) + +// What a path leads to, through a symlink: reading zero bytes resolves one. A directory behind a link keeps the +// link's own size. +const followed = (host: Host, absolute: string) => + Effect.flatMap(host.files.stat(absolute), (own) => + own.type !== "symlink" + ? Effect.succeed(own) + : host.files.read(absolute, { offset: 0, length: 0 }).pipe( + Effect.map((result) => result.info), + Effect.catchTag("Environment.WrongKind", (error) => Effect.succeed({ ...own, type: error.actual })), + ), + ) + +// As in Bun, `delete` removes a file, never a directory. It holds the file's mutation lock, as a patch that deletes +// a file does. +const remove = (handle: Handle) => + call("unlink", handle.name, (host) => + Effect.gen(function* () { + const info = yield* host.files.stat(handle.target.absolute) + if (info.type === "directory") + return yield* new Environment.WrongKind({ path: handle.target.absolute, actual: info.type }) + yield* host.fileWrite.authorize(handle.target, host.context) + yield* host.files.remove(handle.target.absolute) + }).pipe(host.fileMutation.withLock([handle.target.absolute])), + ) + +// `Bun.write`: the bytes written. The destination is a path or a BunFile; the data is text, bytes, a BunFile to copy, +// or a response, whose body is read here. Like Bun, it creates missing parent directories. +const writeTo = (destination: unknown, data: unknown) => { + const to = typeof destination === "string" || destination instanceof URL ? toPath(destination) : handleOf(destination) + if (to === undefined) throw new TypeError("Bun.write expects a path or a BunFile to write to.") + return write(to, data) +} + +const write = (to: string | Handle, data: unknown) => { + const from = handleOf(data) + // A copy asks edit without a diff of the file. + return (from ? read(from) : contents(data)).pipe( + Effect.flatMap((bytes) => put(to, bytes, { parents: true, diff: from === undefined })), + ) +} + +// Writes exactly these bytes, with no formatting and no BOM handling, after asking edit permission. The request shows +// the change as a diff when both sides are text, like the write tool's. An append never reads the file: its diff +// shows only what it adds. Like an edit, it holds the file's mutation lock from reading the file to writing it, and +// writes through `FileMutation`. +const put = ( + to: string | Handle, + bytes: Uint8Array, + options: { readonly parents?: boolean; readonly diff?: boolean; readonly append?: boolean }, +) => + call("open", typeof to === "string" ? to : to.name, (host) => + Effect.gen(function* () { + const target = typeof to === "string" ? yield* host.fileWrite.target(to, host.context) : to.target + return yield* Effect.gen(function* () { + const type = yield* typeOf(host, target.absolute) + if (type === "directory") return yield* new Environment.WrongKind({ path: target.absolute, actual: type }) + if ( + type === undefined && + !options.parents && + (yield* typeOf(host, path.dirname(target.absolute))) === undefined + ) + return yield* new Environment.NotFound({ path: target.absolute }) + const after = options.diff ? text(bytes) : undefined + const before = + after === undefined || options.append || type === undefined + ? "" + : text((yield* host.files.read(target.absolute)).bytes) + yield* host.fileWrite.authorize( + target, + host.context, + after !== undefined && before !== undefined + ? fileDiff(target.resource, before, after, type === undefined ? "added" : "modified") + : undefined, + ) + yield* options.append + ? host.fileMutation.append({ target, content: bytes }) + : host.fileMutation.write({ target, content: bytes }) + return bytes.byteLength + }).pipe(host.fileMutation.withLock([target.absolute])) + }), + ) + +const typeOf = (host: Host, absolute: string) => + host.files.stat(absolute).pipe( + Effect.map((info) => info.type), + Effect.catchTag("Environment.NotFound", () => Effect.undefined), + ) + +// Bytes as text when they are valid UTF-8. +const text = (bytes: Uint8Array) => + isUtf8(bytes) ? Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString() : undefined + +const handleOf = (value: unknown) => { + const bytes = method(value, "bytes") + return bytes && handles.get(bytes) +} + +// Only a host function crosses back from a program, so a `bytes`, `arrayBuffer` or `text` function here is one a host +// value came with, such as a `fetch` response's. +const contents = (data: unknown) => { + const given = toBytes(data) + if (given) return Effect.succeed(given) + const body = method(data, "bytes") ?? method(data, "arrayBuffer") ?? method(data, "text") + if (!body) return Effect.fail(unwritable()) + return Effect.tryPromise({ try: async (): Promise => body(), catch: (error) => error }).pipe( + Effect.flatMap((result) => { + const bytes = toBytes(result) + return bytes ? Effect.succeed(bytes) : Effect.fail(unwritable()) + }), + ) +} + +const unwritable = () => + new TypeError("Bun.write expects a string, a Uint8Array, a BunFile, or a Response as the data to write.") + +// A value from a program is a plain copy, so its functions are among its own entries. +const method = (value: unknown, key: string) => { + if (typeof value !== "object" || value === null) return undefined + const found: unknown = Object.entries(value).find(([name]) => name === key)?.[1] + return typeof found === "function" ? found : undefined +} + +// Like Bun, a number or a boolean is written as its text. +const toBytes = (value: unknown) => { + if (typeof value === "string") return Buffer.from(value) + if (typeof value === "number" || typeof value === "boolean") return Buffer.from(String(value)) + if (value instanceof Uint8Array) return value + if (value instanceof ArrayBuffer) return new Uint8Array(value) +} + +// As `text()` does in Bun: UTF-8 without a leading byte order mark. +const decode = (bytes: Uint8Array) => new TextDecoder().decode(bytes) + +// A subset of Bun's FileSink. Chunks collect here until a flush: the first replaces the file and later ones append to +// it, so a long write asks edit once per flush, with what that flush adds, not once per chunk. Bun's own sink writes +// over the start of an existing file without truncating it; this one replaces the file. As in Bun, a missing parent +// directory fails the flush. +const writer = (handle: Handle, options?: { readonly highWaterMark?: number }) => { + supports("A file writer", options, ["highWaterMark"]) + const chunks: Array = [] + let buffered = 0 + let started = false + const flush = () => + Effect.suspend(() => { + if (chunks.length === 0) return Effect.succeed(0) + return put(handle, Buffer.concat(chunks), { diff: true, append: started }).pipe( + Effect.tap(() => + Effect.sync(() => { + started = true + chunks.length = 0 + buffered = 0 + }), + ), + ) + }) + return { + write: (chunk: unknown) => { + const bytes = typeof chunk === "string" || chunk instanceof Uint8Array ? toBytes(chunk) : undefined + if (!bytes) throw new TypeError("A file writer takes a string or a Uint8Array.") + chunks.push(bytes) + buffered += bytes.byteLength + return buffered < (options?.highWaterMark ?? Infinity) ? bytes.byteLength : Effect.as(flush(), bytes.byteLength) + }, + flush, + end: flush, + } +} + +type ScanOptions = { + readonly cwd?: string + readonly dot?: boolean + readonly absolute?: boolean + readonly onlyFiles?: boolean +} + +// `scan` and `scanSync` both give an array, which `for` and `for await` both take. A scan asks read permission for +// the directory it lists from. As in Bun without `followSymlinks`, a symlink is neither a file nor a directory to +// look inside: only `onlyFiles: false` lists one. +const scan = (pattern: string, input?: string | ScanOptions) => { + const options = typeof input === "string" ? { cwd: input } : input + supports("A Bun.Glob scan", options, ["cwd", "dot", "absolute", "onlyFiles"]) + const cwd = options?.cwd ?? "." + return call("scandir", cwd, (host) => + Effect.gen(function* () { + const entries = (yield* glob(host, pattern, cwd, options?.dot === true)).filter( + (entry) => options?.onlyFiles === false || entry.type === "file", + ) + if (options?.absolute !== true) return entries.map((entry) => entry.path) + const root = (yield* host.access.resolve({ path: cwd })).absolute + return entries.map((entry) => path.resolve(root, entry.path)) + }), + ) +} + +// The entries under `cwd` that a pattern matches, each by its path as the pattern names it, without a leading `./`. +const glob = (host: Host, given: string, cwd: string, dot: boolean) => + Effect.gen(function* () { + const pattern = given.replace(/^(\.\/)+/, "") + const segments = pattern.split("/") + const wild = segments.findIndex((segment) => /[*?[\]{}()!+@]/.test(segment)) + // 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. + const fixed = (wild < 0 ? segments : segments.slice(0, wild)).join("/") || (path.isAbsolute(pattern) ? "/" : ".") + const base = path.isAbsolute(pattern) ? fixed : path.join(cwd, fixed) + const root = (yield* host.access.authorizeRead(base, host.context)).absolute + if (wild < 0) { + const type = yield* typeOf(host, root) + return type === undefined ? [] : [{ path: pattern, type }] + } + const entries = yield* walk(host.files, root, { + // Deep enough for every segment after the fixed part; `**` can match any depth. + depth: pattern.includes("**") ? Infinity : segments.length - wild, + // Without `dot`, only a segment that names a leading dot itself matches inside a hidden directory. + hidden: dot || segments.slice(wild).some((segment) => /(^|[{,(|])\./.test(segment)), + }).pipe( + // As in Bun, a fixed part that isn't a directory matches nothing, while a `cwd` that isn't one fails. + Effect.catchIf( + (error) => wild > 0 && error._tag !== "Environment.Failed", + () => Effect.succeed([]), + ), + ) + return entries + .map((entry) => ({ + path: path.isAbsolute(pattern) ? path.join(base, entry.path) : path.join(path.relative(cwd, base), entry.path), + type: entry.type, + })) + .filter((entry) => Glob.match(pattern, entry.path, { dot })) + }) + +type Entry = { readonly path: string; readonly type: Environment.FileType } + +// Lists `depth` levels below `directory`: 1 is its own entries, Infinity everything. It goes into a directory whose +// name starts with a dot only when `hidden` is set. +const walk = ( + files: Environment.Files, + directory: string, + options: { readonly depth: number; readonly hidden: 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 (options.depth <= 1 || entry.type !== "directory" || (!options.hidden && entry.name.startsWith("."))) + return Effect.succeed([item]) + return walk(files, directory, { ...options, depth: options.depth - 1 }, item.path).pipe( + Effect.map((inside) => [item, ...inside]), + ) + }), + ), + Effect.map((items) => items.flat()), + ) + +// Only part of Bun's options are implemented. Naming another fails, since ignoring it would give a wrong result. +const supports = (api: string, options: object | undefined, supported: ReadonlyArray) => { + const unsupported = Object.keys(options ?? {}).find((key) => !supported.includes(key)) + if (unsupported === undefined) return + throw new TypeError( + `${api} does not support the "${unsupported}" option in Code Mode. Supported options: ${supported.join(", ")}.`, + ) +} + +const toPath = (input: string | URL) => + typeof input === "string" && !input.startsWith("file:") ? input : fileURLToPath(input) + +const messages = { + ENOENT: "no such file or directory", + EISDIR: "illegal operation on a directory", + ENOTDIR: "not a directory", +} + +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: -constants.errno[code], + syscall, + path: file, + }) + if (error instanceof Environment.Failed && error.cause instanceof Error) return error.cause + return error +} + +const codeOf = (error: unknown) => { + if (error instanceof Environment.NotFound) return "ENOENT" + if (error instanceof Environment.WrongKind) return error.actual === "directory" ? "EISDIR" : "ENOTDIR" +} + +// Bun's MIME types for common extensions, with the charset Bun adds to some text types. +const types = Object.fromEntries( + Object.entries({ + "text/plain;charset=utf-8": ["txt", "log", "ini"], + "text/javascript;charset=utf-8": ["js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx"], + "application/json;charset=utf-8": ["json", "map"], + "text/html;charset=utf-8": ["html", "htm"], + "text/css;charset=utf-8": ["css"], + "text/markdown": ["md"], + "text/csv": ["csv"], + "text/yaml": ["yaml", "yml"], + "application/toml": ["toml"], + "application/xml": ["xml"], + "application/pdf": ["pdf"], + "application/zip": ["zip"], + "application/gzip": ["gz"], + "application/x-tar": ["tar"], + "application/wasm": ["wasm"], + "image/svg+xml": ["svg"], + "image/png": ["png"], + "image/jpeg": ["jpg", "jpeg"], + "image/gif": ["gif"], + "image/webp": ["webp"], + "image/avif": ["avif"], + "image/x-icon": ["ico"], + "audio/mpeg": ["mp3"], + "audio/x-wav": ["wav"], + "video/mp4": ["mp4"], + "video/webm": ["webm"], + "font/woff": ["woff"], + "font/woff2": ["woff2"], + "font/ttf": ["ttf"], + "font/otf": ["otf"], + }).flatMap(([type, extensions]) => extensions.map((extension) => [extension, type])), +) + +const mime = (name: string) => types[path.extname(name).slice(1).toLowerCase()] ?? "application/octet-stream" + +/** A subset of Bun's file APIs, over the session's files and with the permissions the file tools ask. */ +export const extension = Extension.make({ + name: "bun", + modules: { bun: "Bun" }, + globals: { + Bun: { + file: open, + write: Extension.async(writeTo), + Glob: Extension.constructible((pattern: string) => { + if (typeof pattern !== "string") throw new TypeError("Bun.Glob expects a pattern string.") + return { + scan: (options?: string | ScanOptions) => scan(pattern, options), + scanSync: (options?: string | ScanOptions) => scan(pattern, options), + // As in Bun, `match` takes a dot file that a scan without `dot` leaves out. + match: (name: string) => Glob.match(pattern, name), + } + }), + }, + }, +}) + +/** What to show for a Bun call: the function, its path or pattern, and the file a write copies. */ +export const display = (name: string, args: ReadonlyArray) => { + const from = name === "Bun.write" ? handleOf(args[1])?.name : undefined + return { + method: name.slice(name.lastIndexOf(".") + 1), + path: handleOf(args[0])?.name ?? String(args[0]), + ...(from === undefined ? {} : { from }), + } +} diff --git a/packages/core/src/codemode/tool.ts b/packages/core/src/codemode/tool.ts index a4483a2d6c5b..5fcbea149a37 100644 --- a/packages/core/src/codemode/tool.ts +++ b/packages/core/src/codemode/tool.ts @@ -16,8 +16,13 @@ import { CallID } from "@opencode/schema/tool" import { Clock, Context, Effect, Ref, Schema, Scope, Semaphore } from "effect" import { toSessionError } from "../session/to-session-error.js" import { definition, normalizedName } from "../tool/runtime.js" +import { Environment } from "../environment/index.js" +import { FileAccess } from "../file-access.js" +import { FileMutation } from "../file-mutation.js" +import { FileWrite } from "../file-write.js" import { Location } from "../location.js" import { Permission } from "../permission.js" +import { CodeModeBun } from "./bun.js" import { CodeModeCatalog } from "./catalog.js" import { CodeModeExecution } from "./execution.js" import { CodeModeFetch } from "./fetch.js" @@ -70,6 +75,15 @@ class Run extends Context.Service< } >()("@opencode/CodeModeTool/Run") {} +/** The session's Location and file services, which a run with the experimental `path` and `Bun` globals acts through. */ +export type Services = Location.Service | CodeModeBun.Services + +/** Those services alone, so a run they are given to keeps its own scope and the rest of its context. */ +export const services = Effect.map( + Effect.context(), + Context.pick(Location.Service, Environment.Service, FileAccess.Service, FileMutation.Service, FileWrite.Service), +) + export type Inventory = { readonly tools: ReadonlyMap readonly namespaces?: ReadonlyMap @@ -90,8 +104,8 @@ export const create = ( inventory: Inventory, executeTool: (name: string, tool: Info, input: unknown, context: ToolContext) => Effect.Effect, permission: Permission.Interface, - /** With `full_codemode`, the session's Location, which turns on the experimental `path` global. */ - location?: Location.Interface, + /** With `full_codemode`, the session's services, which turn on the experimental `path` and `Bun` globals. */ + services?: Context.Context, ) => { return { name: "execute", @@ -128,7 +142,9 @@ export const create = ( ) const result = yield* runtime(inventory) .execute(code, undefined, { - extensions: location ? [CodeModeFetch.extension, CodeModePath.extension] : [CodeModeFetch.extension], + extensions: services + ? [CodeModeFetch.extension, CodeModePath.extension, CodeModeBun.extension] + : [CodeModeFetch.extension], }) .pipe( Effect.provideService(Run, { @@ -165,7 +181,7 @@ export const create = ( // 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), - (program) => (location ? Effect.provideService(program, Location.Service, location) : program), + (program) => (services ? Effect.provideContext(program, services) : program), ) const toolCalls = yield* Ref.get(calls) const collected = (yield* Ref.get(files)) @@ -229,6 +245,8 @@ function progressHooks(record: (update: (items: Array) => Array({ + const created = CodeMode.make< + typeof tools, + [typeof CodeModeFetch.extension, typeof CodeModePath.extension, typeof CodeModeBun.extension] + >({ tools, - extensions: [CodeModeFetch.extension, CodeModePath.extension], + extensions: [CodeModeFetch.extension, CodeModePath.extension, CodeModeBun.extension], hooks: runHooks, }) runtimes.set(inventory, created) diff --git a/packages/core/src/file-mutation.ts b/packages/core/src/file-mutation.ts index 425208c4d926..4ed63e9f38bd 100644 --- a/packages/core/src/file-mutation.ts +++ b/packages/core/src/file-mutation.ts @@ -38,6 +38,8 @@ export interface Interface { targets: ReadonlyArray, ) => (effect: Effect.Effect) => Effect.Effect readonly write: (input: WriteInput) => Effect.Effect + /** Add bytes to the end of a file, creating it when missing. */ + readonly append: (input: WriteInput) => Effect.Effect /** Write text while retaining an existing UTF-8 BOM and emitting at most one BOM. */ readonly writeTextPreservingBom: ( input: TextWriteInput, @@ -93,21 +95,24 @@ const layer = Layer.effect( existed, }) - const write = Effect.fn("FileMutation.write")((input: WriteInput) => + const put = (input: WriteInput, store: Files["write"]) => withTargetLock(input.target)( Effect.gen(function* () { const existed = yield* environment.files.stat(input.target.absolute).pipe( Effect.as(true), Effect.catchTag("Environment.NotFound", () => Effect.succeed(false)), ) - yield* environment.files.write( + yield* store( input.target.absolute, typeof input.content === "string" ? new TextEncoder().encode(input.content) : input.content, ) return writeResult(input.target, existed) }), - ), - ) + ) + + const write = Effect.fn("FileMutation.write")((input: WriteInput) => put(input, environment.files.write)) + + const append = Effect.fn("FileMutation.append")((input: WriteInput) => put(input, environment.files.append)) const writeTextPreservingBom = Effect.fn("FileMutation.writeTextPreservingBom")((input: TextWriteInput) => withTargetLock(input.target)( @@ -124,7 +129,7 @@ const layer = Layer.effect( ), ) - return Service.of({ withLock, write, writeTextPreservingBom }) + return Service.of({ withLock, write, append, writeTextPreservingBom }) }), ) diff --git a/packages/core/src/session/context.ts b/packages/core/src/session/context.ts index ca15f25d42e4..bd55a9a06aaf 100644 --- a/packages/core/src/session/context.ts +++ b/packages/core/src/session/context.ts @@ -6,7 +6,12 @@ import { Context, Effect, Layer } from "effect" import { Agent } from "../agent.js" import type { SessionRequestKind } from "@opencode/plugin/effect/session" import { CodeModeInstructions } from "../codemode/instructions.js" +import { CodeModeTool } from "../codemode/tool.js" import { Database } from "../database/database.js" +import { Environment } from "../environment/index.js" +import { FileAccess } from "../file-access.js" +import { FileMutation } from "../file-mutation.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,7 @@ const layer = Layer.effect( const skillInstructions = yield* SkillInstructions.Service const store = yield* SessionStore.Service const registry = yield* Tool.Service + const codeMode = yield* CodeModeTool.services const resolveModel = ( session: SessionSchema.Info, @@ -146,7 +152,7 @@ const layer = Layer.effect( const permissions = Permission.merge(agent.info.permissions, session.permissions ?? []) const loaded = yield* Effect.all( { - tools: registry.snapshot(permissions, location), + tools: registry.snapshot(permissions, codeMode), builtins: builtins.load(), discovery: discovery.load(), skills: skillInstructions.load(permissions), @@ -219,6 +225,10 @@ export const node = makeLocationNode({ Agent.node, Model.node, Database.node, + Environment.node, + FileAccess.node, + FileMutation.node, + FileWrite.node, InstructionBuiltIns.node, InstructionDiscovery.node, InstructionEntry.node, diff --git a/packages/core/src/tool.ts b/packages/core/src/tool.ts index 04de7d8752b0..a8e586a9d66f 100644 --- a/packages/core/src/tool.ts +++ b/packages/core/src/tool.ts @@ -13,7 +13,6 @@ import { CodeModeJob } from "./codemode/job.js" import { CodeModeTool } from "./codemode/tool.js" import { Flag } from "./flag.js" import { Image } from "./image.js" -import type { Location } from "./location.js" import { Permission } from "./permission.js" import { PluginHooks } from "./plugin/hooks.js" import { SessionMessage } from "./session/message.js" @@ -46,8 +45,14 @@ type Data = { export interface Interface extends State.Transformable { readonly list: () => Effect.Effect> - /** With `full_codemode`, Code Mode gets the experimental `path` global, resolving against `location`'s directory. */ - readonly snapshot: (permissions?: Permission.Ruleset, location?: Location.Interface) => Effect.Effect + /** + * With `full_codemode`, Code Mode gets the experimental `path` and `Bun` globals, which act through `codeMode`: the + * session's Location and file services. + */ + readonly snapshot: ( + permissions?: Permission.Ruleset, + codeMode?: 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`. @@ -204,7 +209,7 @@ const layer = Layer.effect( | { data: Data names: string - path: boolean + full: boolean inventory: CodeModeTool.Inventory value: CodeModeCatalog.Inventory definition: ToolDefinition @@ -268,9 +273,9 @@ 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, location) => + snapshot: Effect.fn("Tool.snapshot")((permissions, codeMode) => Effect.map(flags.enabled("full_codemode"), (enabled) => { - const path = enabled && location !== undefined + const full = enabled && codeMode !== undefined const data = state.get() const active = new Map() const rules = permissions ?? [] @@ -289,14 +294,14 @@ const layer = Layer.effect( // 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.path === path ? catalog : undefined + catalog?.data === data && catalog.names === names && catalog.full === full ? 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, - path ? location : undefined, + full ? codeMode : undefined, ) : undefined const codeModeCatalog = !codeModeEnabled @@ -307,7 +312,7 @@ const layer = Layer.effect( catalog = { data, names, - path, + full, inventory: codeModeInventory, value: codeModeCatalog, definition: codeModeDefinition, diff --git a/packages/core/test/file-mutation.test.ts b/packages/core/test/file-mutation.test.ts index b3bb756555a2..b354b0bcc7b6 100644 --- a/packages/core/test/file-mutation.test.ts +++ b/packages/core/test/file-mutation.test.ts @@ -70,6 +70,24 @@ describe("FileMutation", () => { ), ) + it.live("appends bytes to a file, creating it when missing", () => + withTempDir(({ path: directory }) => + Effect.gen(function* () { + const access = yield* FileAccess.Service + const files = yield* FileMutation.Service + const target = yield* access.resolve({ path: "log.txt" }) + + expect(yield* files.append({ target, content: "one\n" })).toMatchObject({ existed: false }) + expect(yield* files.append({ target, content: new Uint8Array([0xff, 0x0a]) })).toMatchObject({ existed: true }) + expect([...(yield* Effect.promise(() => fs.readFile(target.absolute)))]).toEqual([ + ...Buffer.from("one\n"), + 0xff, + 0x0a, + ]) + }).pipe(provide(directory)), + ), + ) + it.live("preserves exactly one BOM for text writes and normalizes created text", () => withTempDir(({ path: directory }) => Effect.gen(function* () { diff --git a/packages/core/test/fixture/codemode.ts b/packages/core/test/fixture/codemode.ts new file mode 100644 index 000000000000..d8b6cece9556 --- /dev/null +++ b/packages/core/test/fixture/codemode.ts @@ -0,0 +1,42 @@ +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 { FileMutation } from "@opencode/core/file-mutation" +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 { LayerNode } from "@opencode/util/effect/layer-node" +import { Effect, Layer } from "effect" +import { permissionLayer } from "../lib/permission" +import { hostEnvironmentLayer } from "./environment" +import { location } from "./location" + +/** What a session at `directory` gives Code Mode with `full_codemode`: its Location and file services on the host. */ +export const codeModeServices = ( + directory: string, + permission: Partial = { assert: () => Effect.void }, + formatter: Formatter.Interface["file"] = () => Effect.succeed(false), +) => + Effect.runSync( + CodeModeTool.services.pipe( + Effect.provide( + AppNodeBuilder.build( + LayerNode.group([Environment.node, FileAccess.node, FileMutation.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: formatter })), + Permission.node.replace(permissionLayer(permission)), + ], + ), + ), + ), + ) diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index 6af0132eb6e9..74743be313ad 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -1,7 +1,11 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test" +import { existsSync } from "fs" +import fs from "fs/promises" import path from "path" +import { pathToFileURL } from "url" import { CodeModeTool } from "@opencode/core/codemode/tool" -import { AbsolutePath } from "@opencode/core/schema" +import { Environment } from "@opencode/core/environment/index" +import { FileMutation } from "@opencode/core/file-mutation" import { Permission } from "@opencode/core/permission" import { Tool } from "@opencode/core/tool" import { execute } from "@opencode/core/tool/runtime" @@ -9,9 +13,10 @@ 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 { Context, Deferred, Effect, Fiber, Schema } from "effect" import { TestClock } from "effect/testing" -import { location } from "./fixture/location" +import { codeModeServices } from "./fixture/codemode" +import { tmpdir } from "./fixture/tmpdir" import { it } from "./lib/effect" import { permissionService } from "./lib/permission" @@ -25,8 +30,11 @@ 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) +const createCodeMode = ( + tools: ReadonlyMap, + permission = allowAll, + services?: Context.Context, +) => CodeModeTool.create({ tools }, (_, tool, input, context) => execute(tool, input, context), permission, services) 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 }) @@ -156,14 +164,8 @@ test("execute rejects filesystem fetches", async () => { }) describe("path", () => { - const directory = AbsolutePath.make(path.resolve("/session/dir")) - const withPath = () => - CodeModeTool.create( - { tools: new Map() }, - (_, tool, input, context) => execute(tool, input, context), - allowAll, - location({ directory }), - ) + const directory = path.resolve("/session/dir") + const withPath = () => createCodeMode(new Map(), allowAll, codeModeServices(directory)) const run = async (code: string, codeMode = withPath()) => JSON.parse((await Effect.runPromise(codeMode.execute({ code }, context))).output.output) @@ -195,6 +197,565 @@ describe("path", () => { }) }) +describe("Bun", () => { + const source = { type: "tool" as const, messageID: context.messageID, id: context.id } + const run = async (directory: string, code: string, permission = allowAll, services = codeModeServices(directory)) => + JSON.parse( + (await Effect.runPromise(createCodeMode(new Map(), permission, services).execute({ code }, context))).output + .output, + ) + const deny = (action: string) => ({ + assert: (input: Permission.AssertInput) => + input.action === action + ? Effect.fail(new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources })) + : Effect.void, + }) + // Every permission request, from the execute call's own service and from the session's file services alike. + const recorded = () => { + const asked: Array = [] + return { asked, assert: (input: Permission.AssertInput) => Effect.sync(() => void asked.push(input)) } + } + + // The session's services, recording each read and listing a program's file operations make of the environment's + // files, and each write and append they make through FileMutation. + const spied = (directory: string) => { + const services = codeModeServices(directory) + const environment = Context.get(services, Environment.Service) + const mutation = Context.get(services, FileMutation.Service) + const calls: Array> = [] + const spy = , A>(name: string, run: (...args: Args) => A) => { + return (...args: Args) => { + calls.push([name, ...args.map((arg) => (typeof arg === "string" ? path.relative(directory, arg) : arg))]) + return run(...args) + } + } + const stored = (name: string, run: FileMutation.Interface["write"]) => (input: FileMutation.WriteInput) => { + calls.push([name, path.relative(directory, input.target.absolute), input.content]) + return run(input) + } + return { + calls, + services: services.pipe( + Context.add( + Environment.Service, + Environment.Service.of({ + ...environment, + files: { + ...environment.files, + read: spy("read", environment.files.read), + list: spy("list", environment.files.list), + }, + }), + ), + Context.add( + FileMutation.Service, + FileMutation.Service.of({ + ...mutation, + write: stored("write", mutation.write), + append: stored("append", mutation.append), + }), + ), + ), + } + } + + test("without the experimental flag there is no Bun", async () => { + const result = await Effect.runPromise(createCodeMode(new Map()).execute({ code: "return [typeof Bun]" }, context)) + expect(JSON.parse(result.output.output)).toEqual(["undefined"]) + }) + + test("Bun is importable as the bun module, only with the experimental flag", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "notes.txt"), "hello") + const code = `import { file, write } from "bun" + await write("copy.txt", file("notes.txt")) + return [await file("copy.txt").text(), require("bun") === Bun]` + expect(await run(tmp.path, code)).toEqual(["hello", true]) + const without = await Effect.runPromise(createCodeMode(new Map()).execute({ code }, context)) + expect(without.output.output).toContain('Cannot import "bun"') + }) + + test("Bun.file reads text, JSON and bytes, and knows a file's size, type and whether it exists", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "notes.txt"), "hello world") + await Bun.write(path.join(tmp.path, "data.json"), '\uFEFF{"a":1}') + await Bun.write(path.join(tmp.path, "sub", "image.PNG"), new Uint8Array([0xff, 0x00])) + expect( + await run( + tmp.path, + `const notes = Bun.file("notes.txt") + const image = Bun.file("sub/image.PNG") + const bytes = await image.bytes() + const stat = await notes.stat() + return [await notes.text(), await Bun.file("data.json").json(), [...bytes], [...await image.arrayBuffer()], + bytes instanceof Uint8Array, Buffer.isBuffer(bytes), [notes.name, notes.size, notes.type], image.type, + Bun.file("data.json").type, Bun.file("Makefile").type, Bun.file("Makefile", { type: "text/x-make" }).type, + notes.lastModified === Math.trunc(stat.mtimeMs), [stat.size, stat.isFile(), stat.isDirectory()], + await notes.exists(), await Bun.file("sub").exists(), Bun.file("sub").type, + await Bun.file(${JSON.stringify(pathToFileURL(path.join(tmp.path, "notes.txt")).href)}).text()]`, + ), + ).toEqual([ + "hello world", + { a: 1 }, + [255, 0], + [255, 0], + true, + false, + ["notes.txt", 11, "text/plain;charset=utf-8"], + "image/png", + "application/json;charset=utf-8", + "application/octet-stream", + "text/x-make", + true, + [11, true, false], + true, + false, + "application/octet-stream", + "hello world", + ]) + }) + + test("a slice reads only its own bytes, counting negative indexes from the end", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "notes.txt"), "hello world") + const files = spied(tmp.path) + expect( + await run( + tmp.path, + `const notes = Bun.file("notes.txt") + const tail = notes.slice(-5) + return [await notes.slice(0, 5).text(), await tail.text(), await notes.slice(6).text(), + await notes.slice(2).slice(1, 3).text(), [...await notes.slice(4, 6).bytes()], await notes.slice(20).text(), + [tail.size, notes.slice(5, 2).size, notes.slice(-50, 50).size], [tail.name, tail.type], + notes.slice(0, 5, "text/x-part").type, notes.slice("text/x-all").size]`, + allowAll, + files.services, + ), + ).toEqual([ + "hello", + "world", + "world", + "lo", + [111, 32], + "", + [5, 0, 11], + ["notes.txt", "text/plain;charset=utf-8"], + "text/x-part", + 11, + ]) + expect(files.calls.filter(([name]) => name === "read")).toEqual([ + ["read", "notes.txt", { offset: 0, length: 5 }], + ["read", "notes.txt", { offset: 6, length: 5 }], + ["read", "notes.txt", { offset: 6, length: 5 }], + ["read", "notes.txt", { offset: 3, length: 2 }], + ["read", "notes.txt", { offset: 4, length: 2 }], + ["read", "notes.txt", { offset: 11, length: 0 }], + ]) + }) + + test("a missing file has no size, doesn't exist, and fails to read with a Node-shaped ENOENT", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "bad.json"), "{") + await fs.mkdir(path.join(tmp.path, "folder")) + expect( + await run( + tmp.path, + `const missing = Bun.file("missing.txt") + const failure = async (run) => { try { await run() } catch (e) { return [e.name, e.code, e.syscall, e.path] } } + let text + try { await missing.text() } catch (e) { text = [e instanceof Error, e.message, typeof e.errno] } + return [missing.size, missing.lastModified, await missing.exists(), text, + await failure(() => missing.json()), await failure(() => missing.slice(0, 2).bytes()), + await failure(() => missing.stat()), await failure(() => missing.delete()), + await failure(() => Bun.write("copy.txt", missing)), (await failure(() => Bun.file("bad.json").json()))[0], + await failure(() => Bun.file("folder").text()), await failure(() => Bun.file("folder").delete()), + await missing.text().catch((e) => e.code), await Bun.file("bad.json").text().then((text) => text.length)]`, + ), + ).toEqual([ + 0, + 2 ** 52 - 1, + false, + [true, "ENOENT: no such file or directory, open 'missing.txt'", "number"], + ["Error", "ENOENT", "open", "missing.txt"], + ["Error", "ENOENT", "open", "missing.txt"], + ["Error", "ENOENT", "stat", "missing.txt"], + ["Error", "ENOENT", "unlink", "missing.txt"], + ["Error", "ENOENT", "open", "missing.txt"], + "SyntaxError", + ["Error", "EISDIR", "open", "folder"], + ["Error", "EISDIR", "unlink", "folder"], + "ENOENT", + 1, + ]) + expect((await fs.readdir(tmp.path)).toSorted()).toEqual(["bad.json", "folder"]) + }) + + test("Bun.write writes text, bytes, a copy of a file and a response, creating parent directories", async () => { + await using tmp = await tmpdir() + await using server = Bun.serve({ port: 0, fetch: () => new Response("downloaded") }) + await Bun.write(path.join(tmp.path, "notes.txt"), "héllo world") + 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 Effect.runPromise( + createCodeMode(new Map(), allowAll, codeModeServices(tmp.path, undefined, formatter)).execute( + { + code: `const failure = async (run) => { try { await run() } catch (e) { return [e.name, e.code ?? e.message] } } + const notes = Bun.file("notes.txt") + return [ + await Bun.write("deep/er/text.ts", "const x =1"), + await Bun.write("bom.txt", "new"), + await Bun.write("bytes.bin", new Uint8Array([0xff, 0xfe, 0x00])), + await Bun.write("deep/copy.txt", notes), + await Bun.write(Bun.file("into.txt"), notes.slice(0, 6)), + await Bun.file("own.txt").write("own"), + await Bun.write("page.txt", await fetch(${JSON.stringify(server.url.href)})), + await Bun.write("number.txt", 5), + await failure(() => Bun.write("object.txt", { text: "x" })), + await failure(() => Bun.write({ name: "object.txt" }, "x")), + await failure(() => Bun.write("deep", "x")), + await Bun.file("own.txt").delete() ?? null, + await Bun.file("own.txt").exists(), + ]`, + }, + context, + ), + ) + + expect(JSON.parse(result.output.output)).toEqual([ + 14, + 3, + 3, + 12, + 6, + 3, + 10, + 1, + ["TypeError", "Bun.write expects a string, a Uint8Array, a BunFile, or a Response as the data to write."], + ["TypeError", "Bun.write expects a path or a BunFile to write to."], + ["Error", "EISDIR"], + null, + false, + ]) + const written = (name: string) => fs.readFile(path.join(tmp.path, name)) + expect((await written("deep/er/text.ts")).toString()).toBe("const x =1") + expect([...(await written("bom.txt"))]).toEqual([...Buffer.from("new")]) + expect([...(await written("bytes.bin"))]).toEqual([0xff, 0xfe, 0x00]) + expect((await written("deep/copy.txt")).toString()).toBe("héllo world") + expect((await written("into.txt")).toString()).toBe("héllo") + expect((await written("page.txt")).toString()).toBe("downloaded") + expect((await written("number.txt")).toString()).toBe("5") + expect((await fs.readdir(tmp.path)).toSorted()).toEqual( + ["bom.txt", "bytes.bin", "deep", "into.txt", "notes.txt", "number.txt", "page.txt"].toSorted(), + ) + expect(result.output.toolCalls.slice(0, 5)).toEqual([ + { tool: "Bun", status: "completed", input: { method: "file", path: "notes.txt" } }, + { tool: "Bun", status: "completed", input: { method: "write", path: "deep/er/text.ts" } }, + { tool: "Bun", status: "completed", input: { method: "write", path: "bom.txt" } }, + { tool: "Bun", status: "completed", input: { method: "write", path: "bytes.bin" } }, + { tool: "Bun", status: "completed", input: { method: "write", path: "deep/copy.txt", from: "notes.txt" } }, + ]) + }) + + test("a writer collects chunks, replaces the file on its first flush and appends on later ones", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "log.txt"), "an older and longer log") + const files = spied(tmp.path) + expect( + await run( + tmp.path, + `const log = Bun.file("log.txt") + const writer = log.writer() + const counts = [writer.write("one\\n"), writer.write(new Uint8Array([116, 119, 111, 10]))] + const untouched = await log.text() + counts.push(writer.flush(), writer.flush(), writer.write("three\\n"), writer.write("four\\n"), await writer.end()) + const eager = Bun.file("eager.txt").writer({ highWaterMark: 4 }) + eager.write("ab") + const buffered = await Bun.file("eager.txt").exists() + eager.write("cd") + const flushed = await Bun.file("eager.txt").text() + let unsupported, missingParent + try { Bun.file("x.txt").writer({ closeAfter: true }) } catch (e) { unsupported = [e.name, e.message] } + try { const lost = Bun.file("none/x.txt").writer(); lost.write("x"); lost.end() } catch (e) { missingParent = e.code } + return [counts, untouched, [buffered, flushed, eager.end()], unsupported, missingParent]`, + allowAll, + files.services, + ), + ).toEqual([ + [4, 4, 8, 0, 6, 5, 11], + "an older and longer log", + [false, "abcd", 0], + [ + "TypeError", + 'A file writer does not support the "closeAfter" option in Code Mode. Supported options: highWaterMark.', + ], + "ENOENT", + ]) + expect(await fs.readFile(path.join(tmp.path, "log.txt"), "utf8")).toBe("one\ntwo\nthree\nfour\n") + expect((await fs.readdir(tmp.path)).toSorted()).toEqual(["eager.txt", "log.txt"]) + expect(files.calls.filter(([name, file]) => (name === "write" || name === "append") && file === "log.txt")).toEqual( + [ + ["write", "log.txt", Buffer.from("one\ntwo\n")], + ["append", "log.txt", Buffer.from("three\nfour\n")], + ], + ) + }) + + test("Bun.Glob scans from a directory for files, with dot files, folders and absolute paths on request", 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") + const result = await run( + tmp.path, + `const scan = (pattern, options) => new Bun.Glob(pattern).scanSync(options).sort() + const awaited = [] + for await (const file of new Bun.Glob("**/*.ts").scan({ cwd: "src" })) awaited.push(file) + let unsupported, missing + try { new Bun.Glob("*").scan({ cwd: "src", followSymlinks: true }) } catch (e) { unsupported = [e.name, e.message] } + try { new Bun.Glob("*.ts").scan("missing") } catch (e) { missing = [e.code, e.syscall, e.path] } + const glob = new Bun.Glob("*.ts") + return [ + awaited.sort(), scan("**/*.ts", "src"), scan("**/*.ts", { cwd: "src", dot: true }), scan("src/*.ts"), + scan("./src/*.ts"), scan("*", { cwd: "src" }), scan("*", { cwd: "src", onlyFiles: false }), + scan("*/*.ts", { cwd: "src", absolute: true }), scan("src/**/*.md", { absolute: true }), + scan("src/a.ts"), scan("src/missing.ts"), scan("missing/*.ts"), scan("src/a.ts/*"), missing, + [...new Bun.Glob("src/*.ts").scan()], + scan("**/.hidden/*.ts", "src"), scan("{.hidden,nested}/*.ts", "src"), + [glob.match("a.ts"), glob.match("nested/a.ts"), glob.match("a.md")], + [glob.match(".a.ts"), new Bun.Glob("**/*.ts").match(".hidden/d.ts")], unsupported, + ]`, + ) + const nested = path.join("nested", "b.ts") + const hidden = path.join(".hidden", "d.ts") + expect(result).toEqual([ + ["a.ts", nested], + ["a.ts", nested], + [hidden, "a.ts", nested], + [path.join("src", "a.ts")], + [path.join("src", "a.ts")], + ["a.ts"], + ["a.ts", "nested"], + [path.join(tmp.path, "src", "nested", "b.ts")], + [path.join(tmp.path, "src", "nested", "c.md")], + ["src/a.ts"], + [], + [], + [], + ["ENOENT", "scandir", "missing"], + [path.join("src", "a.ts")], + [hidden], + [hidden, nested], + [true, false, false], + // As in Bun, `match` takes a dot file that a scan without `dot` leaves out. + [true, true], + [ + "TypeError", + 'A Bun.Glob scan does not support the "followSymlinks" option in Code Mode. Supported options: cwd, dot, absolute, onlyFiles.', + ], + ]) + }) + + // Checked against Bun 1.4.4, whose scan doesn't follow symlinks unless `followSymlinks` is set. + test.skipIf(process.platform === "win32")( + "a scan for files leaves out symlinks, lists them when folders are asked for, and reads through one it names", + async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "src", "a.ts"), "a") + await fs.symlink(path.join("src", "a.ts"), path.join(tmp.path, "link.ts")) + await fs.symlink("src", path.join(tmp.path, "linked")) + await fs.symlink("missing.ts", path.join(tmp.path, "broken.ts")) + expect( + await run( + tmp.path, + `const scan = (pattern, options) => new Bun.Glob(pattern).scanSync(options).sort() + return [scan("**/*.ts"), scan("**/*.ts", { onlyFiles: false }), scan("*", { onlyFiles: false }), + scan("linked/*.ts")]`, + ), + ).toEqual([ + ["src/a.ts"], + ["broken.ts", "link.ts", "src/a.ts"], + ["broken.ts", "link.ts", "linked", "src"], + ["linked/a.ts"], + ]) + }, + ) + + test("a scan lists only under the pattern's fixed part, as deep as the pattern reaches, skipping hidden folders", 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", "deep", "b.ts"), "b") + await Bun.write(path.join(tmp.path, "src", ".cache", "c.ts"), "c") + await Bun.write(path.join(tmp.path, "other", "d.ts"), "d") + const listed = async (pattern: string, options = {}) => { + const files = spied(tmp.path) + await run( + tmp.path, + `return new Bun.Glob(${JSON.stringify(pattern)}).scanSync(${JSON.stringify(options)})`, + allowAll, + files.services, + ) + return files.calls + .filter(([name]) => name === "list") + .map(([, directory]) => String(directory)) + .toSorted() + } + const nested = [path.join("src", "nested"), path.join("src", "nested", "deep")] + expect(await listed("src/*.ts")).toEqual(["src"]) + expect(await listed("src/*/*.ts")).toEqual(["src", path.join("src", "nested")]) + expect(await listed("src/**/*.ts")).toEqual(["src", ...nested]) + expect(await listed("src/**/.cache/*.ts")).toEqual([path.join("src", ".cache"), ...nested, "src"].toSorted()) + expect(await listed("src/**/*.ts", { dot: true })).toEqual( + [path.join("src", ".cache"), ...nested, "src"].toSorted(), + ) + }) + + test("a denied read surfaces core's permission error from Bun.file and from a scan", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "secret.txt"), "x") + expect( + await run( + tmp.path, + `const failure = (run) => { try { run() } catch (e) { return [e.name, e.message, e.code ?? null] } } + return [failure(() => Bun.file("secret.txt")), failure(() => new Bun.Glob("*.txt").scan())]`, + permissionService(deny("read")), + codeModeServices(tmp.path, deny("read")), + ), + ).toEqual([ + ["Error", "Permission denied: read", null], + ["Error", "Permission denied: read", null], + ]) + }) + + test("a file asks to be read once, a write asks edit with its diff, and a copy asks edit without one", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "notes.txt"), "old\n") + const record = recorded() + await run( + tmp.path, + `await Bun.write("notes.txt", "old\\nnew\\n") + await Bun.write("out/new.txt", "one\\ntwo\\n") + const notes = Bun.file("notes.txt") + await notes.text() + await notes.slice(0, 3).bytes() + await notes.exists() + await Bun.write("out/copy.txt", notes) + await notes.write("changed\\n") + const writer = notes.writer() + writer.write("first\\n") + writer.flush() + writer.write("second\\n") + writer.end() + await notes.delete() + new Bun.Glob("out/*.txt").scanSync() + return []`, + permissionService(record), + codeModeServices(tmp.path, record), + ) + expect(record.asked.map((input) => [input.action, input.resources, input.sessionID, input.source])).toEqual([ + ["edit", ["notes.txt"], context.sessionID, source], + ["edit", ["out/new.txt"], context.sessionID, source], + ["read", ["notes.txt"], context.sessionID, source], + ["edit", ["out/copy.txt"], context.sessionID, source], + ["edit", ["notes.txt"], context.sessionID, source], + ["edit", ["notes.txt"], context.sessionID, source], + ["edit", ["notes.txt"], context.sessionID, source], + ["edit", ["notes.txt"], context.sessionID, source], + ["read", ["out"], context.sessionID, source], + ]) + const diffs = record.asked.map((input) => input.metadata?.files) + expect(diffs[0]).toMatchObject([{ file: "notes.txt", status: "modified", additions: 1, deletions: 0 }]) + expect(diffs[1]).toMatchObject([{ file: "out/new.txt", status: "added", additions: 2 }]) + expect(diffs[3]).toBeUndefined() + expect(diffs[5]).toMatchObject([{ file: "notes.txt", status: "modified", additions: 1, deletions: 1 }]) + // An append shows only the lines it adds. + expect(diffs[6]).toMatchObject([{ file: "notes.txt", status: "modified", additions: 1, deletions: 0 }]) + expect(diffs[7]).toBeUndefined() + }) + + test("writes to one file wait for each other, and hold the lock an edit takes", async () => { + await using tmp = await tmpdir() + const file = path.join(tmp.path, "notes.txt") + const record = recorded() + const edits: Array> = [] + // While a write asks edit, something else asks for the file's lock, as an edit would, and reports whether the + // write had landed by the time it got it. + const permission = { + assert: (input: Permission.AssertInput) => + Effect.gen(function* () { + yield* record.assert(input) + edits.push( + yield* Effect.sync(() => existsSync(file)).pipe( + Context.get(services, FileMutation.Service).withLock([file]), + Effect.forkDetach({ startImmediately: true }), + ), + ) + }), + } + const services = codeModeServices(tmp.path, permission) + await run( + tmp.path, + `await Promise.all([Bun.write("notes.txt", "one\\n"), Bun.write("notes.txt", "two\\n")]) + return []`, + allowAll, + services, + ) + // The second write read the file after the first wrote it. + expect(record.asked.map((input) => input.metadata?.files)).toMatchObject([ + [{ status: "added" }], + [{ status: "modified" }], + ]) + expect(await Effect.runPromise(Effect.forEach(edits, Fiber.join))).toEqual([true, true]) + }) + + test("a file outside the project asks for its directory once, when Bun.file names it", async () => { + await using tmp = await tmpdir() + await using outside = await tmpdir() + const file = path.join(outside.path, "notes.txt") + await Bun.write(file, "outside") + const record = recorded() + expect( + await run( + tmp.path, + `const notes = Bun.file(${JSON.stringify(file)}) + const text = await notes.text() + await notes.write(text + " again") + return [text, await notes.text()]`, + permissionService(record), + codeModeServices(tmp.path, record), + ), + ).toEqual(["outside", "outside again"]) + const resource = file.replaceAll("\\", "/") + expect(record.asked.map((input) => [input.action, input.resources])).toEqual([ + ["external_directory", [path.posix.join(path.posix.dirname(resource), "*")]], + ["read", [resource]], + ["edit", [resource]], + ]) + }) + + test("a denied edit writes nothing: Bun.write and delete reject, and a writer's flush throws", async () => { + await using tmp = await tmpdir() + await Bun.write(path.join(tmp.path, "keep.txt"), "keep") + expect( + await run( + tmp.path, + `const keep = Bun.file("keep.txt") + const settled = await Promise.allSettled([Bun.write("new/a.txt", "x"), Bun.write("copy.txt", keep), + keep.write("changed"), keep.delete()]) + const writer = Bun.file("b.txt").writer() + writer.write("x") + let thrown + try { writer.end() } catch (e) { thrown = e.message } + return [settled.map((item) => item.reason?.message), thrown, await keep.text()]`, + permissionService(deny("edit")), + codeModeServices(tmp.path, deny("edit")), + ), + ).toEqual([Array(4).fill("Permission denied: edit"), "Permission denied: edit", "keep"]) + expect(await fs.readdir(tmp.path)).toEqual(["keep.txt"]) + }) +}) + describe("execute output", () => { const run = async (code: string) => (await Effect.runPromise(createCodeMode(new Map()).execute({ code }, context))).output.output diff --git a/packages/core/test/tool-registry.test.ts b/packages/core/test/tool-registry.test.ts index 110afd5f1fa9..2d14ed4be58d 100644 --- a/packages/core/test/tool-registry.test.ts +++ b/packages/core/test/tool-registry.test.ts @@ -2,7 +2,6 @@ import { describe, expect } from "bun:test" import { Agent } from "@opencode/core/agent" import { Permission } from "@opencode/core/permission" import { Flag } from "@opencode/core/flag" -import { AbsolutePath } from "@opencode/core/schema" import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" import { Image } from "@opencode/core/image" import { PluginHooks } from "@opencode/core/plugin/hooks" @@ -16,7 +15,7 @@ import { codeModeListings, executeTool, toolDefinitions, toolNodeReplacements } import { Deferred, Effect, Exit, Fiber, Layer, Logger, Schema, SchemaGetter, SchemaIssue, Scope } from "effect" import { z } from "zod" import { testEffect } from "./lib/effect" -import { location } from "./fixture/location" +import { codeModeServices } from "./fixture/codemode" const imageStore = Layer.mock(Image.Service, { normalize: (resource, content) => { @@ -78,27 +77,31 @@ const transform = (service: Tool.Interface, tools: Readonly Object.entries(tools).forEach(([name, tool]) => editor.add({ ...tool, name, options: options ?? tool.options })), ) -describe("Code Mode path", () => { - const session = location({ directory: AbsolutePath.make("/session/dir") }) - const typeOfPath = Effect.gen(function* () { +describe("Code Mode path and Bun", () => { + const types = Effect.gen(function* () { const tools = yield* Tool.Service - const snapshot = yield* tools.snapshot(undefined, session) + const snapshot = yield* tools.snapshot(undefined, codeModeServices("/session/dir")) const result = yield* snapshot.execute({ ...call("execute"), - call: { type: "tool-call", id: "call-path", name: "execute", input: { code: `return [typeof path]` } }, + call: { + type: "tool-call", + id: "call-globals", + name: "execute", + input: { code: `return [typeof path, typeof Bun]` }, + }, }) return JSON.parse(result.output.output) }) - it.effect("is not available without full_codemode", () => + it.effect("are not available without full_codemode", () => Effect.gen(function* () { - expect(yield* typeOfPath).toEqual(["undefined"]) + expect(yield* types).toEqual(["undefined", "undefined"]) }), ) - codeModeIt.effect("is available with full_codemode", () => + codeModeIt.effect("are available with full_codemode", () => Effect.gen(function* () { - expect(yield* typeOfPath).toEqual(["object"]) + expect(yield* types).toEqual(["object", "object"]) }), ) }) 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 }) } }