Skip to content

Commit 7f321ab

Browse files
authored
feat(core): run Code Mode on the bytecode VM behind experimental.codemodeVm (#62358)
1 parent 714d653 commit 7f321ab

7 files changed

Lines changed: 391 additions & 272 deletions

File tree

‎packages/client/src/promise/generated/types.ts‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/core/src/codemode/tool.ts‎

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import type {
1111
Namespace as ToolNamespace,
1212
Result,
1313
} from "@opencode/schema/tool"
14-
import { Clock, Context, Effect, Ref, Schema, Semaphore } from "effect"
14+
import { Clock, Context, Effect, Option, Ref, Schema, Semaphore } from "effect"
15+
import { Config } from "../config.js"
16+
import { Flag } from "../flag.js"
1517
import { toSessionError } from "../session/to-session-error.js"
1618
import { definition, normalizedName } from "../tool/runtime.js"
1719
import { Permission } from "../permission.js"
@@ -71,6 +73,22 @@ export type Inventory = {
7173
readonly namespaces?: ReadonlyMap<string, ToolNamespace>
7274
}
7375

76+
/** Reads `experimental.codemode_vm` (or `OPENCODE_FLAGS`) on every call; without one, it is off. */
77+
const vmEnabled = Effect.gen(function* () {
78+
const overrides = (yield* Flag.Overrides) ?? ""
79+
for (const item of overrides
80+
.split(",")
81+
.map((part) => part.trim())
82+
.filter(Boolean)
83+
.toReversed()) {
84+
if (item === "codemode_vm" || item === "codemodeVm") return true
85+
if (item === "-codemode_vm" || item === "-codemodeVm") return false
86+
}
87+
const config = yield* Effect.serviceOption(Config.Service)
88+
if (Option.isNone(config)) return false
89+
return Config.latest(yield* config.value.entries(), "experimental")?.codemode_vm === true
90+
})
91+
7492
// Invariant model-facing guidance; the changing tool catalog is delivered through Instructions.
7593
const description = [
7694
"Run JavaScript in a confined Code Mode runtime to script tool calls and HTTP requests and compose their results.",
@@ -118,7 +136,7 @@ export const create = (
118136
yield* Effect.sleep(wait).pipe(Effect.andThen(lock.withPermit(publish)), Effect.forkIn(scope))
119137
}),
120138
)
121-
const result = yield* runtime(inventory)
139+
const result = yield* runtime(inventory, (yield* vmEnabled) ? "vm" : "legacy")
122140
.execute(code)
123141
.pipe(
124142
Effect.provideService(Run, {
@@ -225,7 +243,8 @@ export const catalog = (inventory: Inventory) => {
225243
)
226244
const root: CatalogNode = { children: new Map() }
227245
for (const namespace of inventory.namespaces?.values() ?? []) getNode(root, namespace.name).namespace = namespace
228-
for (const tool of runtime(inventory).catalog)
246+
// Either engine's runtime has the same catalog; reuse whichever exists rather than preparing the tools again.
247+
for (const tool of (runtimes.legacy.get(inventory) ?? runtimes.vm.get(inventory) ?? runtime(inventory)).catalog)
229248
getNode(root, tool.path).tool = {
230249
type: "tool",
231250
name: tool.path.split(".").at(-1) ?? tool.path,
@@ -261,19 +280,23 @@ function renderCatalog(root: CatalogNode): ReadonlyArray<CodeModeCatalog.Tool |
261280
}
262281

263282
// Inventories are immutable snapshots, so tool definitions, schemas, and the search index are built
264-
// once per inventory and shared by its catalog and every execution.
265-
const runtimes = new WeakMap<Inventory, CodeMode.Runtime<Run | CodeModeExecution.Service | Permission.Service>>()
283+
// once per inventory and engine, and shared by its catalog and every execution.
284+
const runtimes = {
285+
legacy: new WeakMap<Inventory, CodeMode.Runtime<Run | CodeModeExecution.Service | Permission.Service>>(),
286+
vm: new WeakMap<Inventory, CodeMode.Runtime<Run | CodeModeExecution.Service | Permission.Service>>(),
287+
}
266288

267-
function runtime(inventory: Inventory) {
268-
const cached = runtimes.get(inventory)
289+
function runtime(inventory: Inventory, engine: "legacy" | "vm" = "legacy") {
290+
const cached = runtimes[engine].get(inventory)
269291
if (cached) return cached
270292
const tools = inventoryTools(inventory)
271293
const created = CodeMode.make<typeof tools, [typeof CodeModeFetch.extension]>({
272294
tools,
273295
extensions: [CodeModeFetch.extension],
274296
hooks: runHooks,
297+
engine,
275298
})
276-
runtimes.set(inventory, created)
299+
runtimes[engine].set(inventory, created)
277300
return created
278301
}
279302

@@ -287,14 +310,20 @@ export const check = (
287310
limits: CodeMode.ExecutionLimits,
288311
executeTool: (name: string, tool: Info, input: unknown) => Effect.Effect<Result, Error>,
289312
) =>
290-
CodeMode.make({ tools: inventoryTools(inventory), hooks: runHooks, limits })
291-
.execute(code, bindings)
292-
.pipe(
293-
Effect.provideService(Run, {
294-
executeTool: (name, tool, input) => executeTool(name, tool, input).pipe(Effect.map(resultValue)),
295-
hooks: {},
296-
}),
297-
)
313+
vmEnabled.pipe(
314+
Effect.flatMap((vm) =>
315+
CodeMode.make({
316+
tools: inventoryTools(inventory),
317+
hooks: runHooks,
318+
limits,
319+
engine: vm ? "vm" : "legacy",
320+
}).execute(code, bindings),
321+
),
322+
Effect.provideService(Run, {
323+
executeTool: (name, tool, input) => executeTool(name, tool, input).pipe(Effect.map(resultValue)),
324+
hooks: {},
325+
}),
326+
)
298327

299328
function inventoryTools(inventory: Inventory) {
300329
// A path may carry namespace metadata, a callable tool, child tools, or all three.

‎packages/core/src/config/normalize.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,15 @@ function normalizeExperimental(
465465
const value = decodeEncoded(schema, experimental[key], ["experimental", key], diagnostics)
466466
if (value !== undefined) result[key] = value
467467
})
468+
if (!own(experimental, "codemode_vm") && own(experimental, "codemodeVm")) {
469+
const value = decodeEncoded(
470+
ConfigExperimental.Info.fields.codemode_vm,
471+
experimental.codemodeVm,
472+
["experimental", "codemodeVm"],
473+
diagnostics,
474+
)
475+
if (value !== undefined) result.codemode_vm = value
476+
}
468477
}
469478
}
470479
if (Object.keys(result).length || (isRecord(input.experimental) && !Object.keys(input.experimental).length))

‎packages/core/test/config/normalization.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ describe("ConfigNormalize", () => {
554554
background_compaction: true,
555555
media_blob: true,
556556
part_rows: true,
557+
codemode_vm: true,
557558
image_generate: { model: "openai/gpt-image-2" },
558559
subagent_depth: 0,
559560
location_idle_timeout: "2 hours",
@@ -567,6 +568,7 @@ describe("ConfigNormalize", () => {
567568
background_compaction: true,
568569
media_blob: true,
569570
part_rows: true,
571+
codemode_vm: true,
570572
image_generate: { model: { providerID: "openai", model: "gpt-image-2" } },
571573
subagent_depth: 0,
572574
location_idle_timeout: "7200000 millis",

0 commit comments

Comments
 (0)