Skip to content

feat(core): add experimental stateful Code Mode REPL - #68600

Open
rekram1-node wants to merge 9 commits into
devfrom
session-repl
Open

rekram1-node wants to merge 9 commits into
devfrom
session-repl

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

N/A (experimental feat behind optInOnly flag codemode_repl).

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds an experimental, opt-in persistent REPL for Code Mode (execute tool), guarded by the optInOnly flag codemode_repl (requires full_codemode; enable with OPENCODE_FLAGS=full_codemode,codemode_repl or experimental.codemode_repl: true).

  • psychoscript@0.14.1: bumps psychoscript from 0.11.0 to 0.14.1 (adds runtime.experimental.session() / restore() with pluggable SessionStore, lazy unloading, cross-cell const ↔ let redeclaration, source pruning on save(), O(graph) planUnload(), and deduplicated UnloadedCell records).
  • @opencode/codemode: wraps runtime.experimental.session() and runtime.experimental.restore() from psychoscript/effect, including optional SessionStore, and resets per-cell tool call lists in run.metadata when runId changes between cells.
  • SQLite persistence + lazy unloading (packages/core/src/codemode/repl.ts, sql.ts): stores one zstd-compressed root snapshot per session in codemode_repl_session and SHA-256 content-addressed blobs in codemode_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 inside saveRow'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.
  • Session lifecycle & reset notices: fork shares blob refs zero-copy when at or before 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 next execute call (quiet otherwise).
  • Isolation & public API: automations, Tool.check, and subagents stay stateless. execute keeps its name and code parameter. Only experimental.codemode_repl is added to the config flag registry (packages/client/src/promise/generated/types.ts regenerated).

How did you verify your code works?

  • cd packages/codemode && bun typecheck && bun test — 337 pass, 0 fail (on psychoscript@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 after forget, MCP inventory swap, zero-copy fork, early-fork & revert reset notices, orphan blob GC, mode-key reset, per-cell inputs scoping).
  • Deterministic @anomaly/tear 0.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 on 2dbb40e, plus 50/50 stateful (2,200 steps) + 50/50 concurrency (800 steps) re-verified on 0.14.1.
  • cd packages/client && bun run generate — regenerated packages/client/src/promise/generated/types.ts.
  • bunx oxlint on modified/new source files — clean.

Screenshots / recordings

N/A (no UI changes).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Public API: adds opt-in flag codemode_repl (experimental.codemode_repl), Runtime.experimental.session/restore and session types in @opencode/codemode, and three SQLite tables (backwards compatible)

@rekram1-node
rekram1-node force-pushed the session-repl branch 2 times, most recently from 683e019 to 2dbb40e Compare December 6, 2026 04:13
@rekram1-node
rekram1-node marked this pull request as ready for review December 6, 2026 04:34
@rekram1-node
rekram1-node enabled auto-merge (squash) December 6, 2026 04:35

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 via experimental.codemode_repl or OPENCODE_FLAGS; the regenerated client types and schema.json include it.
  • @opencode/codemode: Runtime gains a required experimental: { session, restore } member, plus new exported types Session, SessionOptions, RestoreSessionOptions, SessionStore, SessionBinding, SessionBindingKind and UnloadResult. Only code that builds its own Runtime object would need updating.
  • A migration adds three tables: codemode_repl_session, codemode_repl_blob and codemode_repl_blob_ref.
  • execute keeps 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>()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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: () =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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", () =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant