From 6ec56821d63100fe5bda45b759364f561c56b359 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Mon, 19 Oct 2026 21:38:16 -0500 Subject: [PATCH] refactor(core): reuse codemode runtime across executions (#57172) --- packages/core/src/codemode/tool.ts | 93 +++++++++++++++---------- packages/core/src/tool.ts | 21 +++--- packages/core/test/tool-execute.test.ts | 56 +++++++++++++++ 3 files changed, 125 insertions(+), 45 deletions(-) diff --git a/packages/core/src/codemode/tool.ts b/packages/core/src/codemode/tool.ts index ef0d5d635b1..5aa590ff811 100644 --- a/packages/core/src/codemode/tool.ts +++ b/packages/core/src/codemode/tool.ts @@ -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 = { readonly children: Map> } -type ToolNode = Node> +type ToolNode = Node> type Tools = { - [name: string]: Tool.Tool | Namespace.Namespace | Tools + [name: string]: Tool.Tool | Namespace.Namespace | 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 + readonly hooks: CodeMode.Hooks + } +>()("@opencode/CodeModeTool/Run") {} + export type Inventory = { readonly tools: ReadonlyMap readonly namespaces?: ReadonlyMap @@ -70,7 +79,7 @@ const description = [ export const create = ( inventory: Inventory, - executeTool: (name: string, tool: Info, input: unknown, context: Context) => Effect.Effect, + executeTool: (name: string, tool: Info, input: unknown, context: ToolContext) => Effect.Effect, ) => { 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 Effect.Effect, - 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>() + +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({ tools, extensions: [CodeModeWeb.extension], hooks }) + const created = CodeMode.make({ 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 + function getNode(root: Node, path: string) { return path.split(".").reduce((parent, name) => { const child: Node = parent.children.get(name) ?? { children: new Map() } @@ -248,13 +271,13 @@ function getNode(root: Node, path: string) { } function renderTools(root: ToolNode) { - const callables = new Map>() + const callables = new Map>() const tools = renderChildren(root, [], callables) for (const [path, tool] of callables) tools[path] = tool return tools } -function renderChildren(node: ToolNode, path: ReadonlyArray, callables: Map>): Tools { +function renderChildren(node: ToolNode, path: ReadonlyArray, callables: Map>): 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, callables: function renderEntry( node: ToolNode, path: ReadonlyArray, - callables: Map>, + callables: Map>, ): Tools[string] { const tools = renderChildren(node, path, callables) // CodeMode merges this dotted tool path with the nested namespace entry. diff --git a/packages/core/src/tool.ts b/packages/core/src/tool.ts index a07468a61e1..e637b8aa9cd 100644 --- a/packages/core/src/tool.ts +++ b/packages/core/src/tool.ts @@ -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({ 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: [ diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index 50703dcec6c..d2f190f24d9 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -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 = [] + 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(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) +})