Repository navigation
feat(core): add experimental stateful Code Mode REPL - #68600
rekram1-node wants to merge 9 commits into
Conversation
683e019 to
2dbb40e
Compare
There was a problem hiding this comment.
I ran the PR from source and the REPL works as described. With OPENCODE_FLAGS=full_codemode,codemode_repl, a binding set in one execute call was visible in the next (repl:42). With only full_codemode, calls stay stateless (ReferenceError). repl.test.ts passes (3), packages/codemode tests pass (337), and core typecheck:tsc is clean. Nothing blocks a merge.
Public API (all additions, backwards compatible):
- New opt-in flag
codemode_repl, set viaexperimental.codemode_replorOPENCODE_FLAGS; the regenerated client types andschema.jsoninclude it. @opencode/codemode:Runtimegains a requiredexperimental: { session, restore }member, plus new exported typesSession,SessionOptions,RestoreSessionOptions,SessionStore,SessionBinding,SessionBindingKindandUnloadResult. Only code that builds its ownRuntimeobject would need updating.- A migration adds three tables:
codemode_repl_session,codemode_repl_blobandcodemode_repl_blob_ref. executekeeps its name and inputs; only its description changes when the flag is on.
Worth a look:
- Planned server restart: a REPL cell can't suspend, so a running cell is lost on restart, while a normal full Code Mode run is saved and resumed. Fine for an experiment, but the gap should be tracked.
- Process-wide state: the warm-interpreter cache and per-session locks live in process-wide maps (see inline).
- Tests: the new tests are large and cover many scenarios each (see inline).
| epoch: number | ||
| } | ||
|
|
||
| const warm = new Map<SessionSchema.ID, WarmEntry>() |
There was a problem hiding this comment.
warm and locks are process-wide maps, not tied to a layer or Database instance, and they are cleared from projector cases (RevertEvent.Staged/Cleared) as well as from Session.revert. In tests that build several Database layers in one process, a warm entry made against one database can be reused against another with the same session ID. Owning this cache in a scoped service, with the projector hooks going through it, would keep its lifetime explicit and would still work as the experiment grows.
| ), | ||
| ) | ||
| }), | ||
| suspend: () => |
There was a problem hiding this comment.
REPL runs are registered as full runs, so suspendAll during a planned handoff tries this suspend and it fails. The cell is then lost on restart (the wait reports it lost), while non-REPL full runs are saved and resumed. Since the last finished cell is already saved, maybe refuse new REPL cells during a handoff with a clear message (the handoff.active() check already does this), or say in the flag description that in-flight cells don't survive a restart.
| }).pipe(Effect.scoped), | ||
| ) | ||
|
|
||
| it.live("keeps CodeModeTool.create stateless when repl flag is false and rejects mid-run suspend when true", () => |
There was a problem hiding this comment.
This test doesn't check what its name says: it never calls CodeModeTool.create. It only asserts CodeModeTool.plain.repl is undefined and that suspend() fails. It can be dropped, or folded into a single suspend assertion. The two tests above are also hard to follow: each runs a long chain of scenarios with shared state (persistence → unload → restore → fork → parent delete → early fork → revert → GC). Splitting them into a few small, focused cases (persist and restore, cold unload with lazy load, fork, revert tombstone, mode-key reset) would make failures much easier to pin down.
Issue for this PR
N/A (experimental
featbehindoptInOnlyflagcodemode_repl).Type of change
What does this PR do?
Adds an experimental, opt-in persistent REPL for Code Mode (
executetool), guarded by theoptInOnlyflagcodemode_repl(requiresfull_codemode; enable withOPENCODE_FLAGS=full_codemode,codemode_replorexperimental.codemode_repl: true).psychoscript@0.14.1: bumpspsychoscriptfrom0.11.0to0.14.1(addsruntime.experimental.session()/restore()with pluggableSessionStore, lazy unloading, cross-cellconst↔letredeclaration, source pruning onsave(), O(graph)planUnload(), and deduplicatedUnloadedCellrecords).@opencode/codemode: wrapsruntime.experimental.session()andruntime.experimental.restore()frompsychoscript/effect, including optionalSessionStore, and resets per-cell tool call lists inrun.metadatawhenrunIdchanges between cells.packages/core/src/codemode/repl.ts,sql.ts): stores one zstd-compressed root snapshot per session incodemode_repl_sessionand SHA-256 content-addressed blobs incodemode_repl_blob+codemode_repl_blob_ref. After each settled cell (including throws), bindings untouched for >5 cells unload automatically; island writes are staged in memory and flushed atomically insidesaveRow's transaction so fenced/cancelled cells cannot strand orphan blobs. Idle warm sessions evict in O(1) by LRU (MAX_WARM_SESSIONS = 64/MAX_WARM_BYTES = 256 MiB, counting ~600 KiB interpreter baseline per warm entry) with waiter-safe lock cleanup.copiedSeq; mid-history forks and confirmed reverts store a 0-byte reset tombstone (preserved across repeated reverts or early forks of tombstoned sessions) and release blob refs; session delete cleans unreferenced blobs. Tool catalog changes (e.g. MCP add/remove) transfer state cleanly to the new runtime. Every wipe prints a 1-line[repl reset: <reason>; lost bindings: ...]footer on the nextexecutecall (quiet otherwise).Tool.check, and subagents stay stateless.executekeeps its name andcodeparameter. Onlyexperimental.codemode_replis added to the config flag registry (packages/client/src/promise/generated/types.tsregenerated).How did you verify your code works?
cd packages/codemode && bun typecheck && bun test— 337 pass, 0 fail (onpsychoscript@0.14.1).cd packages/core && bun typecheck && bun run typecheck:tsc— 0 errors across 2,104 files.cd packages/core && bun test test/codemode/repl.test.ts— 3 pass, 0 fail (40 assertions: persistence, cold unloading, lazy restore afterforget, MCP inventory swap, zero-copy fork, early-fork & revert reset notices, orphan blob GC, mode-key reset, per-cellinputsscoping).@anomaly/tear0.2.0 model-based tests against real SQLite +psychoscript@0.14.1— 200/200 stateful runs (8,800 steps) + 150/150 concurrency runs (2,400 steps) passed on2dbb40e, plus 50/50 stateful (2,200 steps) + 50/50 concurrency (800 steps) re-verified on0.14.1.cd packages/client && bun run generate— regeneratedpackages/client/src/promise/generated/types.ts.bunx oxlinton modified/new source files — clean.Screenshots / recordings
N/A (no UI changes).
Checklist
Added by the opencode bot (AI-generated).
Public API: adds opt-in flag
codemode_repl(experimental.codemode_repl),Runtime.experimental.session/restoreand session types in@opencode/codemode, and three SQLite tables (backwards compatible)