refactor(core): reuse codemode runtime across executions (#57172)

This commit is contained in:
Aiden Cline
2026-10-19 21:38:16 -05:00
committed by GitHub
Unverified
parent 49f52ff2d5
commit 6ec56821d6
3 changed files with 125 additions and 45 deletions
+58 -35
View File
@@ -1,16 +1,16 @@
export * as CodeModeTool from "./tool.js"
import { CodeMode, Namespace, Tool, toolError } from "@opencode/codemode"
import { CodeMode, Namespace, Tool } from "@opencode/codemode"
import type {
Content,
Context,
Context as ToolContext,
Error,
Info,
Metadata,
Namespace as ToolNamespace,
Result,
} from "@opencode/schema/tool"
import { Effect, Ref, Schema, Semaphore } from "effect"
import { Context, Effect, Ref, Schema, Semaphore } from "effect"
import { definition, normalizedName } from "../tool/runtime.js"
import { CodeModeCatalog } from "./catalog.js"
import { CodeModeWeb } from "./web.js"
@@ -47,12 +47,21 @@ type Node<T> = {
readonly children: Map<string, Node<T>>
}
type ToolNode = Node<Tool.Tool<never>>
type ToolNode = Node<Tool.Tool<Run>>
type Tools = {
[name: string]: Tool.Tool<never> | Namespace.Namespace<never> | Tools
[name: string]: Tool.Tool<Run> | Namespace.Namespace<Run> | Tools
}
// Per-execution parts of a runtime shared by every execution against the same inventory.
class Run extends Context.Service<
Run,
{
readonly executeTool: (name: string, tool: Info, input: unknown) => Effect.Effect<unknown, unknown>
readonly hooks: CodeMode.Hooks
}
>()("@opencode/CodeModeTool/Run") {}
export type Inventory = {
readonly tools: ReadonlyMap<string, Info>
readonly namespaces?: ReadonlyMap<string, ToolNamespace>
@@ -70,7 +79,7 @@ const description = [
export const create = (
inventory: Inventory,
executeTool: (name: string, tool: Info, input: unknown, context: Context) => Effect.Effect<Result, Error>,
executeTool: (name: string, tool: Info, input: unknown, context: ToolContext) => Effect.Effect<Result, Error>,
) => {
return {
name: "execute",
@@ -87,25 +96,28 @@ export const create = (
lock.withPermit(
Ref.updateAndGet(calls, update).pipe(Effect.tap((toolCalls) => context.progress({ toolCalls }))),
)
const result = yield* runtime(
inventory,
(name, tool, input) =>
Effect.gen(function* () {
const index = yield* Ref.getAndUpdate(callIndex, (index) => index + 1)
const executed = yield* executeTool(name, tool, input, context)
const content =
typeof executed.content === "string"
? [{ type: "text" as const, text: executed.content }]
: (executed.content ?? [])
const outputFileParts = outputFiles(content)
if (outputFileParts.length > 0)
yield* Ref.update(files, (items) => [...items, { index, files: outputFileParts }])
if (executed.output !== undefined) return executed.output
const text = content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n")
return text === "" ? null : text
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)
const executed = yield* executeTool(name, tool, input, context)
const content =
typeof executed.content === "string"
? [{ type: "text" as const, text: executed.content }]
: (executed.content ?? [])
const outputFileParts = outputFiles(content)
if (outputFileParts.length > 0)
yield* Ref.update(files, (items) => [...items, { index, files: outputFileParts }])
if (executed.output !== undefined) return executed.output
const text = content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n")
return text === "" ? null : text
}),
hooks: progressHooks(record),
}),
progressHooks(record),
).execute(code)
)
const toolCalls = yield* Ref.get(calls)
const collected = (yield* Ref.get(files))
.toSorted((left, right) => left.index - right.index)
@@ -183,7 +195,7 @@ export const catalog = (inventory: Inventory) => {
)
const root: CatalogNode = { children: new Map() }
for (const namespace of inventory.namespaces?.values() ?? []) getNode(root, namespace.name).namespace = namespace
for (const tool of runtime(inventory, () => Effect.fail(toolError("Execute context is unavailable"))).catalog)
for (const tool of runtime(inventory).catalog)
getNode(root, tool.path).tool = {
type: "tool",
name: tool.path.split(".").at(-1) ?? tool.path,
@@ -218,11 +230,13 @@ function renderCatalog(root: CatalogNode): ReadonlyArray<CodeModeCatalog.Tool |
})
}
function runtime(
inventory: Inventory,
executeTool: (name: string, tool: Info, input: unknown) => Effect.Effect<unknown, unknown>,
hooks?: CodeMode.Hooks,
) {
// 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>>()
function runtime(inventory: Inventory) {
const cached = runtimes.get(inventory)
if (cached) return cached
// A path may carry namespace metadata, a callable tool, child tools, or all three.
const root: ToolNode = { children: new Map() }
for (const namespace of inventory.namespaces?.values() ?? []) getNode(root, namespace.name).namespace = namespace
@@ -232,13 +246,22 @@ function runtime(
description: child.description,
input: child.inputSchema,
output: child.outputSchema ?? Schema.NullOr(Schema.String),
execute: (input) => executeTool(name, registration, input),
execute: (input) => Run.use((run) => run.executeTool(name, registration, input)),
})
}
const tools = renderTools(root)
return CodeMode.make<typeof tools>({ tools, extensions: [CodeModeWeb.extension], hooks })
const created = CodeMode.make<typeof tools>({ tools, extensions: [CodeModeWeb.extension], hooks: runHooks })
runtimes.set(inventory, created)
return created
}
const runHooks = {
"tool.before": (call) => Run.use((run) => run.hooks["tool.before"]?.(call) ?? Effect.void),
"tool.after": (call, result) => Run.use((run) => run.hooks["tool.after"]?.(call, result) ?? Effect.void),
"extension.before": (call) => Run.use((run) => run.hooks["extension.before"]?.(call) ?? Effect.void),
"extension.after": (call, result) => Run.use((run) => run.hooks["extension.after"]?.(call, result) ?? Effect.void),
} satisfies CodeMode.Hooks<Run>
function getNode<T>(root: Node<T>, path: string) {
return path.split(".").reduce((parent, name) => {
const child: Node<T> = parent.children.get(name) ?? { children: new Map() }
@@ -248,13 +271,13 @@ function getNode<T>(root: Node<T>, path: string) {
}
function renderTools(root: ToolNode) {
const callables = new Map<string, Tool.Tool<never>>()
const callables = new Map<string, Tool.Tool<Run>>()
const tools = renderChildren(root, [], callables)
for (const [path, tool] of callables) tools[path] = tool
return tools
}
function renderChildren(node: ToolNode, path: ReadonlyArray<string>, callables: Map<string, Tool.Tool<never>>): Tools {
function renderChildren(node: ToolNode, path: ReadonlyArray<string>, callables: Map<string, Tool.Tool<Run>>): Tools {
return Object.fromEntries(
Array.from(node.children).flatMap(([name, child]) => {
const next = [...path, name]
@@ -272,7 +295,7 @@ function renderChildren(node: ToolNode, path: ReadonlyArray<string>, callables:
function renderEntry(
node: ToolNode,
path: ReadonlyArray<string>,
callables: Map<string, Tool.Tool<never>>,
callables: Map<string, Tool.Tool<Run>>,
): Tools[string] {
const tools = renderChildren(node, path, callables)
// CodeMode merges this dotted tool path with the nested namespace entry.
+11 -10
View File
@@ -155,7 +155,9 @@ const layer = Layer.effect(
}
})
let catalog: { data: Data; names: string; value: CodeModeCatalog.Inventory } | undefined
let catalog:
| { data: Data; names: string; inventory: CodeModeTool.Inventory; value: CodeModeCatalog.Inventory }
| undefined
const state = State.create<Data, Editor>({
name: "tool",
initial: () => ({
@@ -233,9 +235,13 @@ const layer = Layer.effect(
}
const direct = new Map(Array.from(active).filter(([, tool]) => tool.options?.codemode === false))
const codeModeTools = new Map(Array.from(active).filter(([, tool]) => tool.options?.codemode !== false))
const namespaces = data.namespaces
const codeModeInventory = { tools: codeModeTools, namespaces }
const names = Array.from(codeModeTools.keys()).join("\0")
const codeModeEnabled = !whollyDisabled("execute", rules)
// Discovery is immutable for a registry revision and visible tool set. Keep request
// definitions/executors fresh, but share the inventory, whose prepared runtime and much
// larger rendered catalog are cached by identity, across steps.
const cached = catalog?.data === data && catalog.names === names ? catalog : undefined
const codeModeInventory = cached?.inventory ?? { tools: codeModeTools, namespaces: data.namespaces }
const codeModeTool = codeModeEnabled
? CodeModeTool.create(codeModeInventory, (name, tool, input, context) =>
beforeExecute(name, input, context).pipe(
@@ -243,15 +249,10 @@ const layer = Layer.effect(
),
)
: undefined
const names = Array.from(codeModeTools.keys()).join("\0")
// Discovery is immutable for a registry revision and visible tool set. Keep request
// definitions/executors fresh, but share the much larger rendered catalog across steps.
const codeModeCatalog = !codeModeEnabled
? undefined
: catalog?.data === data && catalog.names === names
? catalog.value
: CodeModeTool.catalog(codeModeInventory)
if (codeModeCatalog) catalog = { data, names, value: codeModeCatalog }
: (cached?.value ?? CodeModeTool.catalog(codeModeInventory))
if (codeModeCatalog) catalog = { data, names, inventory: codeModeInventory, value: codeModeCatalog }
return {
...(codeModeCatalog === undefined ? {} : { codeModeCatalog }),
definitions: [
+56
View File
@@ -154,3 +154,59 @@ test("execute supports callable namespace tools", async () => {
})
expect(result.content).toEqual([{ type: "text", text: '[\n "admin",\n "created"\n]' }])
})
test("executions share the prepared runtime of an inventory while keeping per-call state", async () => {
let conversions = 0
const echo: Info = {
name: "echo",
description: "Echo a value",
input: {
"~standard": {
version: 1,
vendor: "test",
validate: (value: unknown) => ({ value }),
jsonSchema: {
input: () => {
conversions++
return { type: "object", properties: { value: { type: "string" } }, required: ["value"] }
},
output: () => ({}),
},
},
},
execute: (input, context) => Effect.succeed({ content: `${context.id}:${(input as { value: string }).value}` }),
}
const inventory = { tools: new Map([["echo", echo]]) }
const codeMode = CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context))
const run = (id: string) => {
const progress: Array<Tool.Metadata> = []
return CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context))
.execute(
{ code: `return await tools.echo({ value: "${id}" })` },
{ ...context, id: Tool.CallID.make(id), progress: (update) => Effect.sync(() => progress.push(update)) },
)
.pipe(Effect.map((result) => ({ output: result.output.output, progress })))
}
expect(CodeModeTool.catalog(inventory).tools).toHaveLength(1)
const results = await Effect.runPromise(Effect.all([run("first"), run("second")], { concurrency: "unbounded" }))
expect<unknown>(results).toEqual(
["first", "second"].map((id) => ({
output: `${id}:${id}`,
progress: [
{ toolCalls: [{ tool: "echo", status: "running", input: { value: id } }] },
{ toolCalls: [{ tool: "echo", status: "completed", input: { value: id } }] },
],
})),
)
expect(
(await Effect.runPromise(codeMode.execute({ code: 'return await tools.echo({ value: "x" })' }, context))).output
.output,
).toBe("call_execute:x")
expect(conversions).toBe(1)
await Effect.runPromise(
createCodeMode(new Map(inventory.tools)).execute({ code: 'return await tools.echo({ value: "next" })' }, context),
)
expect(conversions).toBe(2)
})