@@ -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"
1517import { toSessionError } from "../session/to-session-error.js"
1618import { definition , normalizedName } from "../tool/runtime.js"
1719import { 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.
7593const 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
299328function inventoryTools ( inventory : Inventory ) {
300329 // A path may carry namespace metadata, a callable tool, child tools, or all three.
0 commit comments