Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
449 changes: 449 additions & 0 deletions packages/core/src/codemode/fs.ts

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions packages/core/src/codemode/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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 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<string>) =>
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)),
),
),
}
}

/** 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) } },
})
145 changes: 87 additions & 58 deletions packages/core/src/codemode/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.<namespace>["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.<namespace>["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<Result, Error>,
permission: Permission.Interface,
/** The Location's file services, which turn on the experimental `fs` and `path` globals. */
fileServices?: Context.Context<CodeModeFs.Services>,
) => {
return {
name: "execute",
description,
description: description(fileServices !== undefined),
input: CodeMode.Input,
freeform: { type: "text" },
output: ExecuteOutput,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -222,6 +228,8 @@ function progressHooks(record: (update: (items: Array<ExecuteCall>) => Array<Exe
case "fetch":
return start(call, { tool: call.name, status: "running", input: CodeModeFetch.display(call.args) })
default:
if (call.extension === "fs")
return start(call, { tool: "fs", status: "running", input: CodeModeFs.display(call.name, call.args) })
return Effect.void
}
},
Expand Down Expand Up @@ -278,17 +286,38 @@ function renderCatalog(root: CatalogNode): ReadonlyArray<CodeModeCatalog.Tool |
// Inventories are immutable snapshots, so tool definitions, schemas, and the search index are built
// once per inventory and shared by its catalog and every execution.
const runtimes = new WeakMap<Inventory, CodeMode.Runtime<Run | CodeModeExecution.Service | Permission.Service>>()
const fsRuntimes = new WeakMap<
Inventory,
CodeMode.Runtime<Run | CodeModeExecution.Service | Permission.Service | CodeModeFs.Services>
>()

const runtime = (inventory: Inventory) =>
cached(runtimes, inventory, (tools) =>
CodeMode.make<typeof tools, [typeof CodeModeFetch.extension]>({
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<typeof tools, [typeof CodeModeFetch.extension]>({
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<T>(cache: WeakMap<Inventory, T>, 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
}

Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/environment/exec-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/environment/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export interface FilesImpl {
readonly remove: (path: string) => Effect.Effect<void, Failed>
readonly move: (from: string, to: string) => Effect.Effect<void, NotFound | Failed>
readonly mkdir: (path: string) => Effect.Effect<void, Failed>
/** Adds bytes to the end of a file, creating it when missing. */
readonly append: (path: string, bytes: Uint8Array) => Effect.Effect<void, Failed>
/** Cuts or zero-extends a file to `length` bytes. */
readonly truncate: (path: string, length: number) => Effect.Effect<void, NotFound | Failed>
}

export interface Files extends FilesImpl {}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/environment/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/environment/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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) =>
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/file-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export interface Interface {
file: string,
context: FileAccess.Invocation,
) => Effect.Effect<FileAccess.Target, Environment.Failed | Permission.Error | SessionErrors.NotFoundError>
/** 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<FileAccess.Target>,
context: FileAccess.Invocation,
preview?: FileDiff.Info,
) => Effect.Effect<void, Permission.Error | SessionErrors.NotFoundError>
Expand Down Expand Up @@ -67,13 +67,13 @@ const layer = Layer.effect(
})

const authorize = Effect.fn("FileWrite.authorize")(function* (
target: FileAccess.Target,
target: FileAccess.Target | ReadonlyArray<FileAccess.Target>,
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),
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/session/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<CodeModeFs.Services>()

const resolveModel = (
session: SessionSchema.Info,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading