GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(codemode): add a bytecode VM engine behind CodeMode.make({ engine: "vm" }) by jlongster · Pull Request #62357 · anomalyco/opencode

Conversation

@jlongster

@jlongster jlongster commented Nov 6, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Summary

Adds the stack bytecode VM and its precompiled prelude as a self-contained engine under packages/codemode/src/vm/, next to dev's untouched interpreter. CodeMode.make({ engine: "vm" }) selects it; the default is "legacy". The goal is to land the VM and be able to switch to it without breaking anything. No other new public API: suspend/resume, durable tools and extension snapshot hooks are left out for now.

Supersedes the PR #61856 → #62086 stack.

Design

  • Legacy interpreter (src/interpreter/*, src/stdlib/*, src/data.ts): untouched, and still the default engine.
  • VM engine (src/vm/**, 16,754 lines): self-contained under src/vm/:
    • src/vm/*.ts and src/vm/compiler/*.ts (8,269 lines): bytecode compiler, scope resolution, stack VM, async/generator frames, host scheduler, and snapshot capture/restore.
    • src/vm/runtime/*.ts (3,860 lines): object model, static intrinsic names, the Effect Driver, and executeProgram.
    • src/vm/stdlib/*.ts (2,609 lines): built-ins wired to the VM object model and prelude.
    • src/vm/prelude/* (1,034 lines plus bytecode.json): built-ins written in JS, precompiled to bytecode.
    • src/vm/snapshot/*.ts (982 lines): the internal graph codec. It isn't exported. Tests use it to prove that a run's state is plain data at every step (the round-trip mode).
  • Shared modules: only src/codemode.ts changes: an optional engine?: "legacy" | "vm" option that routes execute to the VM. tool.ts, tool-runtime.ts, extension.ts and index.ts are identical to dev.
  • Lazy loading: src/codemode.ts has only a type import of src/vm/. The VM is loaded with a cached dynamic import() on the first "vm" execution, so the default engine loads no VM code. Both engines bind the same global names, so the extension-global clash check uses dev's list; test/engines.test.ts checks that the lists match and, in a subprocess, that a default-engine run loads no src/vm module.
  • Test-only switch: src/ reads no engine environment variable. The suites import CodeMode through test/lib/, which passes the engine selected by CODEMODE_ENGINE=vm (or CODEMODE_ROUNDTRIP=1) unless a test picks one, so test:vm and test:roundtrip run the whole suite on the VM.
  • Deleting either engine later:
    • Deleting src/vm/ and the engine === "vm" branch in src/codemode.ts restores dev.
    • Deleting src/interpreter/, src/stdlib/ and src/data.ts, and making "vm" the only engine, leaves the VM standalone.

Public API

  • CodeMode.make({ ..., engine?: "legacy" | "vm" }), default "legacy". "vm" is experimental.
  • runtime.execute(code, bindings?) keeps dev's exact signature and Result on both engines.

Verification

  • Package suite:
    • legacy: 1,595 pass, 0 fail (43 files)
    • vm (CODEMODE_ENGINE=vm): 1,595 pass, 0 fail
    • Round-trip mode (bun run test:roundtrip): 1,595 pass, 0 fail
  • test262:
    • legacy: 13,329 pass, 1,678 skip, 1 known fail (language/global-code/import.js)
    • vm: 13,329 pass, same skips and known fail
    • vm round-trip: 13,329 pass, same skips and known fail
  • Typecheck: bun run typecheck:tsc is clean.

Load cost of the default engine

Measured against dev (0b1b63081c); import times are the minimum of 30 runs on a noisy machine.

dev before lazy loading now
Import src/index.ts from source 109.6 ms 125.2 ms 108.7 ms
Import in a --compile --bytecode --splitting binary – 6.9 ms 6.3 ms
Minified bundle (--minify --external effect --external acorn) 178.0 KB 517.9 KB 521.1 KB
Same with --splitting, loaded by the default engine 178.0 KB – 178.3 KB (164.9 KB entry + 13.4 KB shared chunk)
VM chunk, loaded only for engine: "vm" – – 339.5 KB

Without splitting the VM stays in the one file, but its modules are only evaluated on first use. The CLI build uses splitting: true; a compiled, bytecode, split binary of the package puts the VM in its own chunk and loads it only for a "vm" run.

Benchmarks (script/bench.ts, 11 iterations)

Benchmark Legacy (median / min) VM (median / min) Speedup (min)
numeric loop 48.3 ms / 47.6 ms 17.6 ms / 14.8 ms 3.2×
closure recursion (fib 22) 35.4 ms / 33.0 ms 4.7 ms / 4.5 ms 7.3×
array callbacks (50k) 275.8 ms / 256.5 ms 81.9 ms / 78.3 ms 3.3×
promise fan-out (5k) 34.4 ms / 31.1 ms 14.8 ms / 8.5 ms 3.7×
tool fan-out (1k) 15.0 ms / 13.6 ms 8.8 ms / 7.2 ms 1.9×
JSON round-trip 194.9 ms / 185.7 ms 53.7 ms / 48.9 ms 3.8×
generator iteration 85.2 ms / 74.0 ms 5.1 ms / 4.9 ms 15.1×
string building 82.9 ms / 75.2 ms 18.8 ms / 16.6 ms 4.5×
Map/Set ops 74.8 ms / 70.4 ms 14.0 ms / 12.9 ms 5.5×

Visual

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 72" width="240" height="72" fill="none">
  <style>
    .t { font: 600 9px ui-monospace, SFMono-Regular, Menlo, monospace; fill: currentColor; }
    .m { font: 500 8px ui-monospace, SFMono-Regular, Menlo, monospace; fill: currentColor; opacity: 0.55; }
    .box { stroke: currentColor; stroke-width: 1.25; rx: 5; }
    .op { animation: pulse 2.4s ease-in-out infinite; }
    .op2 { animation: pulse 2.4s ease-in-out 0.8s infinite; }
    .op3 { animation: pulse 2.4s ease-in-out 1.6s infinite; }
    @keyframes pulse {
      0%, 100% { opacity: 0.28; }
      40% { opacity: 0.95; }
    }
    @media (prefers-reduced-motion: reduce) {
      .op, .op2, .op3 { animation: none; opacity: 0.75; }
    }
  </style>
  <rect class="box" x="8" y="10" width="66" height="52" opacity="0.35"/>
  <text class="t" x="16" y="25">engine</text>
  <rect x="15" y="31" width="52" height="11" rx="2" fill="currentColor" opacity="0.12"/>
  <text class="m" x="19" y="39">"legacy"</text>
  <rect x="15" y="45" width="52" height="12" rx="2" fill="currentColor" opacity="0.22"/>
  <text class="t" x="19" y="54">"vm"</text>
  <path d="M74 36 H94" stroke="currentColor" stroke-width="1.25" opacity="0.45"/>
  <polygon points="94,36 89,33 89,39" fill="currentColor" opacity="0.6"/>
  <rect class="box" x="98" y="10" width="134" height="52" opacity="0.65"/>
  <text class="t" x="107" y="24">BYTECODE VM</text>
  <rect class="op" x="107" y="30" width="36" height="11" rx="2" fill="currentColor" opacity="0.75"/>
  <text class="m" x="111" y="38">LOAD</text>
  <rect class="op2" x="147" y="30" width="36" height="11" rx="2" fill="currentColor" opacity="0.55"/>
  <text class="m" x="151" y="38">CALL</text>
  <rect class="op3" x="187" y="30" width="36" height="11" rx="2" fill="currentColor" opacity="0.35"/>
  <text class="m" x="193" y="38">RET</text>
  <text class="m" x="107" y="54">same execute · both engines</text>
</svg>

Added by the opencode bot (AI-generated).

Public API: adds optional engine: "legacy" | "vm" to CodeMode.make/execute (backwards compatible)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
jlongster added 3 commits November 6, 2026 14:25
@jlongster
feat(codemode): add durable tool and extension snapshot hooks
b3363d4
@jlongster
feat(codemode): add self-contained bytecode VM engine under src/vm
87151b3
@jlongster
feat(codemode): add a bytecode VM engine behind CodeMode.make({ engin…
c661761
…e: "vm" })
@github-actions github-actions Bot added the contributor label Nov 6, 2026
opencodejr[bot]
opencodejr Bot reviewed Nov 6, 2026
View reviewed changes

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The VM engine works as described. On this branch I ran a durable tool call through start → suspend → toBytes → resume in a brand-new runtime, and it gave the correct result. A missing outcome fails with a clear SnapshotError, and both engines return identical execute results. Package tests pass on both engines (1,623 each, test262 excluded), the packages/core Code Mode tests pass on both (37 each), and tsc is clean in packages/codemode and packages/core.

Public API:

  • Removed (breaking):
    • CodeMode.Success and CodeMode.Failure (schemas and types) are gone; only Result remains.
    • make's Provided generic loses its = {} default.
  • Undocumented env var: CODEMODE_ENGINE=vm / CODEMODE_ROUNDTRIP=1 are read in library code and change the default engine.
  • Added (backwards compatible):
    • Options.engine and Options.suspend, Runtime.start and Runtime.resume.
    • CodeMode.toBytes, fromBytes, SnapshotError, SuspendError, and the Engine, SuspendOptions, Execution, Outcome, Pending, Suspended, Snapshot and SnapshotHeader types.
    • CodeMode.Location, plus DataValue as a schema value.
    • Tool.make({ durable }), Tool.Handle, Durable and DurableContext, and execute(input, { key }).
    • ToolInvocation.key and Extension.make({ snapshot }).

Concerns (details inline):

  1. The removed Success/Failure exports break existing imports. This also contradicts the description's claim that deleting src/vm/ gives back dev exactly.
  2. The env var lets the environment pick the production engine.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/codemode/src/codemode.ts Outdated
/** Schema for the structured success or diagnostic returned by CodeMode execution. */
export const Result = Schema.Union([Success, Failure])
/** Result of executing a CodeMode program. Program failures are data, not Effect failures. */
export const Result = Schema.Union([

@opencodejr opencodejr Bot Nov 6, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the exported Success and Failure schemas and types, which dev exports today. Nothing in the repo imports them, but outside callers might. Since the description says the shared modules only gain additions, could they stay, with Result = Schema.Union([Success, Failure]) as before? The {} default on make's Provided generic and the removed doc comments (e.g. on searchSignature/toolExpression) look like the same accidental drift. Without them, deleting src/vm/ wouldn't restore dev exactly as the description says.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jlongster jlongster Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed: on the current head (d32a5a8) Success and Failure are still exported from packages/codemode/src/codemode.ts (schemas and types), with Result = Schema.Union([Success, Failure]) as on dev.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

opencodejr[bot] reacted with eyes emoji
All reactions
  • 👀 1 reaction

@opencodejr opencodejr Bot Nov 6, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. On d32a5a8630, Success and Failure are both exported from packages/codemode/src/codemode.ts again, as schemas (lines 111 and 122) and as types (lines 120 and 130). Result = Schema.Union([Success, Failure]) is back at line 133.

The other drift I mentioned is gone too. I diffed codemode.ts from pr/base to pr/head, and it now only adds code:

  • a type-only import type { VmEngine }
  • the engine?: "legacy" | "vm" option
  • the engine === "vm" branch in execute
  • the lazy loadVm
  • one comment about the global-name check

The Provided default and the doc comments match dev, so deleting src/vm/ and that branch would restore dev's file exactly. I made no changes to the checkout.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/codemode/src/codemode.ts Outdated
maxOutputBytes: validateLimit("maxOutputBytes", limits?.maxOutputBytes, 0),
})

const defaultEngine = (): Engine =>

@opencodejr opencodejr Bot Nov 6, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultEngine reads CODEMODE_ENGINE / CODEMODE_ROUNDTRIP from process.env in library code. Any OpenCode process with one of these set would quietly switch every CodeMode.make caller in packages/core to the experimental VM. The same goes for the module-level roundTrip flag in vm/runtime/execute.ts, which adds snapshot round-trips to every run. If these are only test switches, could the test preload set them instead (e.g. through an internal setter, like nativeRule.check in test/preload.ts)? That keeps them out of the runtime's public behaviour.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jlongster jlongster Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed: defaultEngine is gone. The engine is only chosen through CodeMode.make({ engine }), and the default stays the existing interpreter. The round-trip test mode no longer reads CODEMODE_ROUNDTRIP either: it's an internal, non-exported setRoundTrip in vm/runtime/execute.ts that only the test helper calls. grep -rn process.env packages/codemode/src finds nothing on d32a5a8.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

opencodejr[bot] reacted with eyes emoji
All reactions
  • 👀 1 reaction

@opencodejr opencodejr Bot Nov 6, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed on d32a5a8630, this resolves the thread.

  • No environment reads in src/: git grep "process.env|Bun.env|import.meta.env" -- packages/codemode/src finds nothing. defaultEngine is gone. The only way to pick the engine is the options.engine === "vm" check in src/codemode.ts:190, and it defaults to legacy.
  • Round-trip switch is internal: setRoundTrip (src/vm/runtime/execute.ts:114-117) sets a module-level flag that starts as false. src/index.ts doesn't re-export it, and package.json only exports ".". So it can't be reached through the package entry, only by a deep import of the source path.
  • Only the tests turn it on: the sole callers are test/lib/codemode.ts and test/fixtures/prelude-roundtrip.ts. test/lib/codemode.ts is now the only place that reads CODEMODE_ENGINE / CODEMODE_ROUNDTRIP. It passes the engine into make/execute, and a test's own engine option still overrides it.

I checked this by reading and grepping the code at pr/head. I didn't rerun the test suites, and I made no changes to the checkout.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
This was referenced Nov 6, 2026
feat(core): run Code Mode on the bytecode VM behind experimental.codemodeVm #62358
Merged
feat(codemode): make Code Mode runs suspendable and resumable #61856
Closed
feat(codemode): suspend only durable work, resume with host outcomes, and save fetch responses #62086
Closed
@jlongster
refactor(codemode): drop suspend, durable tools and extension snapsho…
2c1aaa4
…t hooks from the public API

Keep only the engine switch: CodeMode.make({ engine: "legacy" | "vm" }) routes execute to the bytecode VM.
Tool, ToolRuntime and Extension are back to dev. The VM keeps its snapshot codec internal, for the round-trip
test mode.
opencodejr[bot]
opencodejr Bot reviewed Nov 6, 2026
View reviewed changes

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commits fix the Success/Failure removal and the other drift in the shared modules. tool.ts, tool-runtime.ts, extension.ts and index.ts now match dev, and codemode.ts only adds the engine option.

On this head, the package suite passes on legacy, VM and round-trip modes (1,592 each, test262 excluded), typecheck:tsc is clean, and the packages/core Code Mode tests pass. A smoke program with a tool call, a generator, a Map, a binding and an extension global gave identical results on both engines.

Public API: the only addition is the optional engine?: "legacy" | "vm" on CodeMode.make/execute, defaulting to "legacy". It is backwards compatible.

The environment-variable switch from the earlier thread is still open: the library still reads it at runtime.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jlongster
perf(codemode): load the VM engine only when it's selected
14101b2
opencodejr[bot]
opencodejr Bot reviewed Nov 6, 2026
View reviewed changes

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commits fix most of the environment-variable concern. src/codemode.ts no longer reads CODEMODE_ENGINE / CODEMODE_ROUNDTRIP; the test suites now pick the engine through test/lib/. The VM is also only loaded on the first "vm" run.

On this head, I ran the package suite with the environment flag set: a default-engine run loaded no VM modules, and an engine: "vm" run loaded the VM lazily and returned the right result. The package suite passes on both engines (1,595 each, test262 excluded), typecheck:tsc is clean, and the packages/core Code Mode tests pass.

One piece of that thread remains: the module-level CODEMODE_ROUNDTRIP read in src/vm/runtime/execute.ts. It only affects runs that opted into engine: "vm".

Public API: the only addition is the optional engine?: "legacy" | "vm" on CodeMode.make/execute, defaulting to "legacy". It is backwards compatible.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
jlongster added 2 commits November 6, 2026 16:02
@jlongster
test(codemode): make VM subprocess and import scans portable to Windows
0ad0cc4
@jlongster
refactor(codemode): turn on the VM round-trip mode from the test help…
d32a5a8
…er, not the environment
opencodejr[bot]
opencodejr Bot reviewed Nov 6, 2026
View reviewed changes

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both earlier points are fixed. src/ no longer reads any environment variable: the round-trip flag in src/vm/runtime/execute.ts is now an internal setRoundTrip, which the test lib sets under CODEMODE_ROUNDTRIP=1. The Success/Failure exports are back too.

On this head the package suite passes on the default engine, on the VM and in round-trip mode (1,595 each), and typecheck:tsc is clean.

Public API: the only addition is still the optional engine?: "legacy" | "vm". It is backwards compatible.

One new problem: these commits commit two symlinks that point to absolute paths on the author's machine (inline).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/codemode/test/test262/built-ins
@@ -0,0 +1 @@
/root/projects/opencode-dev-codemode/packages/codemode/test/test262/built-ins No newline at end of file

@opencodejr opencodejr Bot Nov 6, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file (and test/test262/language) is a committed symlink to /root/projects/opencode-dev-codemode/..., a local path on your machine. It dangles everywhere else, and test/test262/.gitignore already ignores both paths, so they look committed by accident. Could you git rm --cached both links? The test262 files are meant to come from script/sync-test262.ts.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jlongster
jlongster merged commit ffde0f7 into dev Nov 6, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@jlongster
jlongster deleted the codemode-vm-flag branch November 6, 2026 17:37
@jlongster jlongster mentioned this pull request Nov 6, 2026
fix(codemode): drop test262 corpus symlinks committed by accident #62394
Merged
@thdxr thdxr mentioned this pull request Nov 6, 2026
release: new docs at /docs, lighter home page (dev → production) #62402
Closed
autonull pushed a commit to autonull/opencode_spacegraph that referenced this pull request Nov 7, 2026
@jlongster me
feat(codemode): add a bytecode VM engine behind CodeMode.make({ engin…
07f26c2
…e: "vm" }) (anomalyco#62357)
@opencode-agent opencode-agent Bot mentioned this pull request Nov 17, 2026
refactor(codemode): break the VM import cycle #64455
Open
3 of 6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@opencodejr opencodejr[bot] opencodejr[bot] left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

contributor

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@jlongster
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.