Psychoscript: Bytecode VM Enables Pause/Resume for AI Agent Scripts
OpenCode's Psychoscript compiles agent scripts to bytecode, capturing execution position, variables, and promise wait chains into snapshots that can be restored in a different process, solving the challenge of resuming long-running agent tasks after restarts.
Reading font

Why Agents Need a Pausable VM
OpenCode's Code Mode lets models write scripts that run loops, branch, and call tools without model involvement at every step. But if the service restarts mid-script — for example, while waiting for a sub-agent to finish — the conversation history alone cannot restore the loop index, pending promises, or where to resume after the result returns.
Psychoscript: A Bytecode VM for Snapshottable Execution
On November 26, OpenCode extracted the Code Mode runtime into a separate project, Psychoscript. Author James Long explains they replaced the AST interpreter with a bytecode VM to support pause and resume. The VM compiles scripts to bytecode; execution frames record the current instruction pointer (pc), stack pointer (sp), registers, and call relationships. Objects, captured variables, and pending promises are managed by the VM. When paused, the runtime traverses object references and saves the entire graph.
Saving an await : Concrete Example
The README demonstrates with a summation script that calls fetchNumber three times (returning 10, 20, 30), with the second call delayed:
let total = 0;
for (const n of [1, 2, 3]) {
total += await fetchNumber(n);
}
return total;When paused after the second call is sent but before it returns, total is 10, loop variable n is 2. The snapshot captures the execution frame ( pc = 20, sp = 5), registers holding the accumulated value 10, loop variable 2, and the array iterator. The pending promise and its link to the waiting frame are also saved. The snapshot uses record IDs to preserve object identity (e.g., the same promise referenced by the call record and the frame), and includes source code with a bytecode fingerprint so that recompilation on resume yields identical instruction positions.
Resuming External Calls: Idempotent vs Durable
The VM records call IDs, arguments, and promise wait relations; a host Driver actually invokes the external function and tracks pending work. On resume, the driver must retrieve results. Psychoscript provides two host interfaces:
Idempotent calls ( idempotent: true): the in-flight call is saved as queued; on resume the host re-executes with the same arguments and call key (e.g., fetchNumber(2) with key jgx-idempotent:1). The service must deduplicate using the key.
Durable calls ( Host.durableFn()): splits work into start, wait, cancel. start launches the task and returns serializable JSON (e.g., {"id":"job-2","n":2}); the runtime stores this, marks the call started, then runs wait to get the result. On resume, the new process reads the saved data and calls wait again, re-attaching to the original task. start runs only once; wait may run multiple times.
Suspension Conditions
Suspension is rejected with SuspendError if an ordinary async call is still running (no retry guarantee) or a durable start hasn't yet returned a task ID. The runtime checks these conditions, captures the snapshot, verifies size limits, and only then stops the original run. Planned restarts can wait for start to return before suspending. Crash recovery between service acceptance and snapshot persistence still requires application-level tracking (request IDs, server-side deduplication, query APIs).
Even with successful resume, external data may have changed. Re-running the same snapshot with the second fetchNumber returning 200 yields 240 (10 + 200 + 30). The runtime validates host function names, namespaces, and snapshot hooks, but cannot enforce internal implementation or data freshness.
OpenCode Integration Status
As of November 20, PR #65011 merged into dev makes @opencode/codemode depend on Psychoscript. Language execution and snapshots are handled by Psychoscript; tool registry, argument validation, call hooks, and maxToolCalls limits remain in Code Mode. Used call quotas are saved in run.metadata and not double-counted on idempotent retries. Psychoscript tracks cumulative runtime; on resume only the original timeout balance remains (tool wait time counts, snapshot storage time does not).
Automatic resume on OpenCode restart still requires wiring save/restore into the startup/shutdown flow. As of November 28, Issue #66478 tracks this: on shutdown, save CodeModeJob; on startup, load snapshot. The proposal uses durable calls for agent() (start returns childSessionID, wait re-attaches and reads structured results from history) and idempotent retry for llm() with persisted script-level call limits.
References
OpenCode announcement: https://x.com/opencode/status/2125702030832349374
James Long on bytecode VM: https://x.com/jlongster/status/2125705548020543902
Psychoscript README: https://github.com/anomalyco/psychoscript/blob/e8380b61bf26a0587bf814aaaf9d762a141f0b7d/README.md
Snapshot implementation: https://github.com/anomalyco/psychoscript/blob/e8380b61bf26a0587bf814aaaf9d762a141f0b7d/src/snapshot.ts and https://github.com/anomalyco/psychoscript/blob/e8380b61bf26a0587bf814aaaf9d762a141f0b7d/src/snapshot/snapshot.ts
Suspend conditions: https://github.com/anomalyco/psychoscript/blob/e8380b61bf26a0587bf814aaaf9d762a141f0b7d/src/runtime/execute.ts
Host driver: https://github.com/anomalyco/psychoscript/blob/e8380b61bf26a0587bf814aaaf9d762a141f0b7d/src/runtime/driver.ts
OpenCode PR #65011: https://github.com/anomalyco/opencode/pull/65011
OpenCode Issue #66478: https://github.com/anomalyco/opencode/issues/66478
James Long's interactive demo: https://jamesbox.exe.xyz/vmblog/
For questions about attribution, corrections or content rights, please contactand we will review it promptly.