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): make Code Mode runs suspendable and resumable by jlongster · Pull Request #61856 · anomalyco/opencode Conversation
|
dev |
this PR | |
|---|---|---|
| array callbacks (50k) | 258 ms | 113 ms |
| promise fan-out (5k) | 36 ms | 13 ms |
| fib 22 | 34 ms | 18 ms |
| Map/Set ops | 73 ms | 39 ms |
| 816-tool-call analytics script | 2.0–2.2 s | 1.1 s |
For 25 real Code Mode scripts taken from sessions (stub tools, identical results on both builds), total time drops from 462 ms to 250 ms, and the typical script is 1.12× faster. Each execution costs about 0.08 ms more to set up, so scripts under 1 ms are slightly slower.
Snapshot sizes for three of those scripts, suspended at 50% and 90% of their steps: 16–42 KB of JSON (2.7–4.3 KB gzipped). Suspending takes about 2 ms.
toolCalls order are unchanged.TypeError.<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 72" width="240" height="72" fill="none" stroke="currentColor"> <style> .bar { animation: run 4s linear infinite; transform-origin: 20px 36px; } .pause { opacity: 0; animation: pause 4s linear infinite; } @keyframes run { 0% { transform: scaleX(0.05) } 40% { transform: scaleX(0.5) } 60% { transform: scaleX(0.5) } 100% { transform: scaleX(1) } } @keyframes pause { 0%, 39% { opacity: 0 } 41%, 59% { opacity: 1 } 61%, 100% { opacity: 0 } } @media (prefers-reduced-motion: reduce) { .bar, .pause { animation: none } .bar { transform: scaleX(0.5) } .pause { opacity: 1 } } </style> <rect x="20" y="28" width="200" height="16" rx="8" opacity="0.25" /> <rect class="bar" x="20" y="28" width="200" height="16" rx="8" fill="currentColor" opacity="0.7" stroke="none" /> <g class="pause" stroke-width="3" stroke-linecap="round"> <line x1="116" y1="12" x2="116" y2="22" /> <line x1="124" y1="12" x2="124" y2="22" /> <path d="M112 52h16v8h-16z" stroke-width="1.5" opacity="0.6" /> </g> </svg>
Added by the opencode bot (AI-generated).
Public API: adds suspend/resume (runtime.start, runtime.resume, CodeMode.toBytes/fromBytes, SnapshotError) and durable tools (Tool.make({ durable, retry }), an idempotency key for execute and the hooks) to @opencode/codemode (backwards compatible)
src/prelude/). The prelude is parsed once per process, and its functions are named by path, so snapshots refer to them by name. This covers collection constructors, groupBy, the Set methods, Object.fromEntries, iterator helpers and Iterator.from, the JSON reviver, replace callbacks and Array.from over iterables. Array callbacks, comparator sort and the Promise combinators stay native, for speed and interruption handling. Frame kinds drop from 83 to 67, and all 18 iteration consumers are gone. src is now +9,834 / −5,012 lines vs dev. Built-ins moved to the prelude are 2–6× slower on 50k-element inputs.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…gExp state as data
…ween evaluators Move the operators, property references, hoisting and iterator checks that never run program code out of the generator evaluator into operations.ts, and the driver's signal and activation types out of runner.ts into signals.ts, which needs no Effect at run time. The machine evaluator builds on both.
Program code can now run as explicit frames on a stack stepped by a loop (src/machine) instead of host generators. Each frame is a kind, a node, a phase, slots and a scope chain; expressions that cannot run program code still finish inline within one step. Abrupt completions unwind frame by frame through try, for...of and array-pattern handlers. Async bodies, generator bodies and calls the runner starts are machine tasks the runner drives as activation bodies; built-ins that call program code run as opaque native frames whose program calls become frames. CODEMODE_EVALUATOR=machine selects it; the generator evaluator stays the default.
…thin Effect loop The machine now owns the scheduler: job queue, inbox, parked awaits, unhandled rejections, and promise operations. An await cuts the stack at the innermost async boundary (the program's frame or an async function's call frame) and parks the frames on the promise as data. Host calls are HostCall records the machine hands to the driver at its stops; blocking host work is a blocking HostCall. The generator evaluator, runner.ts, and CODEMODE_EVALUATOR are gone.
…e built-ins as data frames
…calls as data frames and drop the host generator bridge
…round-trip test mode
…and at both hooks
…fter, and document suspend and resume hooks
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The suspend/resume machinery works as described, but there is one bug: the snapshot build id changes with what a process has loaded.
I ran the full Code Mode suite 3 times and it fails on 1 of 1551 tests every time (base: 1500/1500). resumes in another process gets a SnapshotError saying the state was saved by another build. The test passes alone and with every other test file except test262.test.ts: its harness registers a Throws frame kind, which changes the parent's build id. test:roundtrip (1551 pass), the core Code Mode tests and tsc --noEmit all pass. Details inline.
Public API (@opencode/codemode), all backwards-compatible additions:
runtime.start / runtime.resumeCodeMode.toBytes / fromBytes / SnapshotErrorSnapshot / SnapshotHeader typesTool.make({ durable, retry })Tool.execute(input, context?), which gains an optional keyExisting execute() callers and Tool.make({ execute }) tools are unaffected. One behaviour change: extension-returned functions throw after a resume.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| let build: string | undefined | ||
|
|
||
| /** This build's id; a snapshot resumes only on the build that wrote it. */ | ||
| export const buildId = (bare: Bare): string => |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The build id hashes codecShape(), which reads the process-global kinds/consumers registries, and the result is cached the first time it is used. So the id depends on which modules have registered frame kinds, and when, rather than only on the build. In this PR's own full bun test run, test262/run.ts registers Throws before the save. The child process in resumes in another process then computes a different id and rejects the state (saved by another Code Mode build (…9c2c86…); this runtime is …41c90a…). That test fails on every full-suite run.
A host that loads an extension or module defining a kind lazily, or in a different order from the resuming process, would hit the same refusal. Could the id come from the intrinsics plus a fixed, build-time set of kinds, or could the registry be frozen before the first buildId call?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…nd on what was imported
…rogram code, starting with array callbacks
…from iterables and toLocaleString to the prelude
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The build-id issue is fixed: frame kinds and consumers are now sealed once the package has loaded, so the id no longer depends on what a process imported. The full suite passes twice in a row (1554/1554, including resumes in another process and the new kinds.test.ts), as do test:roundtrip and tsc --noEmit. Every module in src that defines a kind or consumer is imported by machine/kinds.ts. The public API is unchanged since the last review. Nothing new to flag.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…etOrInsertComputed, and Set methods to the prelude
…t8Array sources, and Headers/URLSearchParams pairs to the prelude
…reviver, and string replace callbacks to the prelude; drop Drain and consumers
…de generators; drop the Helper cursor and its kinds
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The build-id fix is still in place: kinds stay sealed, and kinds.test.ts plus the cross-process resume test pass. The prelude move doesn't change behaviour for the built-ins I tried against dev (collection forEach, groupBy, the new Map error, iterator helpers, replace callbacks). bun test and test:roundtrip both pass (1557/1557), and tsc --noEmit is clean. The public API is the same as in my earlier review. Nothing new to flag.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Closing: superseded by a new bytecode VM engine that lands side by side with dev's original interpreter behind |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Superseded by #62357 (bytecode VM engine side by side with dev's interpreter, |
Sorry, something went wrong.
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.