GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
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× |
<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.
There was an error while loading. Please reload this page.
There was a problem 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:
CodeMode.Success and CodeMode.Failure (schemas and types) are gone; only Result remains.make's Provided generic loses its = {} default.CODEMODE_ENGINE=vm / CODEMODE_ROUNDTRIP=1 are read in library code and change the default engine.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):
Success/Failure exports break existing imports. This also contradicts the description's claim that deleting src/vm/ gives back dev exactly.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| /** 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([ |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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:
import type { VmEngine }engine?: "legacy" | "vm" optionengine === "vm" branch in executeloadVmThe 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.
There was an error while loading. Please reload this page.
| maxOutputBytes: validateLimit("maxOutputBytes", limits?.maxOutputBytes, 0), | ||
| }) | ||
|
|
||
| const defaultEngine = (): Engine => |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Confirmed on d32a5a8630, this resolves the thread.
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.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.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.
There was an error while loading. Please reload this page.
…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.
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
| @@ -0,0 +1 @@ | |||
| /root/projects/opencode-dev-codemode/packages/codemode/test/test262/built-ins No newline at end of file | |||
There was a problem 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.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.