Add forkable SCI runtime worlds - #1084
Draft
whilo wants to merge 28 commits into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This draft adds forkable SCI runtime worlds behind an explicit opt-in:
The default
:standardmode does not allocate a runtime world and retains SCI's direct Var, function, atom, delay, memoize, and namespace paths. In standard mode,sci/forkkeeps its historical namespace-environment behavior. Full live-state isolation is enabled only in forkable mode.This split is intended to make the feature usable by embeddings such as Spindel and Dvergr without imposing its indirections on babashka, nbb, or ordinary SCI users.
Forkable runtime
In forkable mode,
sci/forktakes a quiescent snapshot of interpreter-owned state while preserving stable SCI handles. Parent, child, siblings, and repeated descendants then evolve independently.The implementation uses dense lineage-local slots rather than a persistent-map lookup on every access. It covers:
Forkablecooperation protocolThe accompanying audit documents the ownership boundary. Arbitrary host closure graphs are not traversed, and external capabilities such as clocks, RNGs, I/O, and running futures require an embedding policy.
Suspended continuations
Forking a quiescent runtime does not implicitly copy a running continuation. Embeddings that suspend interpreted work can now do this explicitly:
The capsule is opaque. It captures the complete persistent dynamic-binding frame immediately, preserves binding-box aliases, gives each retarget an independent set of boxes, selects the target world during invocation, unwinds nested bindings correctly, and rejects unrelated SCI lineages. The explicit-context capture arity supports host-invoked interpreted functions after top-level evaluation has returned.
sci/with-detached-contextis the complementary host boundary for recursively constructing an independent interpreter without inheriting the caller's active world or binding frame.Interactive demo
Run a tree of forked REPL worlds with:
The demo supports
:fork NAME,:use ID, and:treealongside ordinary SCI forms. Each node shows inherited history and later divergence in functions and state created before the fork.Performance
These are local JVM median-of-medians across three fresh processes. Each process warmed up and took seven samples. The machine was held in its
powersaveprofile, so the relative comparison matters more than the absolute milliseconds.master:standard:forkableThe important result is that standard mode shows no regression in these hot-path probes. Forkable mode still pays a material 2.5-3.9x cost relative to standard mode depending on the operation; this draft exposes that tradeoff rather than making every embedding pay it.
Verification
This remains a draft for architectural discussion. The commits are intentionally split into reviewable checkpoints and can be decomposed into smaller PRs if that better fits SCI.
Closes #1083