|
| 1 | +# 0028 — Dev-mode linking: one shared runtime, one module per form |
| 2 | + |
| 3 | +**Status:** proposed · 2026-07-30 · adversarially reviewed the same day, |
| 4 | +before implementation — and the review found a real blocker. Every §1 |
| 5 | +mechanic survived being run (mutable `(ref null eq)` globals cross |
| 6 | +modules and the importer can set them; an imported tag catches a |
| 7 | +cross-module throw; eqref signatures type-check structurally at |
| 8 | +instantiation — two-module probes, both engines). What did not survive: |
| 9 | +per-module K. §2a below is the fix, and *(review)* marks the other |
| 10 | +three corrections: the ledger's ownership invariant made loud, the |
| 11 | +wasmtime-CLI claim corrected from impossible to deferred-on-cost, and |
| 12 | +the probe evidence cited instead of assumed. |
| 13 | + |
| 14 | +## The question |
| 15 | + |
| 16 | +Every decided piece of the dev loop assumes a shape nobody has emitted |
| 17 | +yet: `0009`'s open world wants per-form modules over one heap, `0026` |
| 18 | +measured that per-form assembly cost is linear in the re-declared |
| 19 | +runtime preamble and named "forms *import* the shared runtime" as the |
| 20 | +exit, and `0027` made cross-module throw identity explicitly a |
| 21 | +tag-import question. And a REPL is not a REPL until form 2 can read |
| 22 | +form 1's `def`. What exactly does dev mode emit, what does the runtime |
| 23 | +module export, and who remembers which var lives where? |
| 24 | + |
| 25 | +## The decision |
| 26 | + |
| 27 | +1. **The runtime becomes its own module, instantiated once per |
| 28 | + session.** It exports, by name: the arithmetic and comparison ops |
| 29 | + (`add sub mul quot lt`), `truthy`, `box`/`unbox`, the `true` and |
| 30 | + `false` singletons, and the `clj-exn` tag with its `exn_class` |
| 31 | + scaffold (`0027` §3 lands here: one tag instance, every form imports |
| 32 | + it, so a throw in form N is catchable — later — in form M). |
| 33 | + **Types cross by structure, not by name**: the value types and the |
| 34 | + `$Fn` substrate are identical rec groups in every module, which is |
| 35 | + exactly the wire-format property `0009` measured and |
| 36 | + `test/cljwit/rec_group_identity_test.clj` pins — nothing to export. |
| 37 | +2. **A dev-mode form compiles to a module that imports what it uses** |
| 38 | + — runtime pieces under `"rt"`, and session vars under `"vars"` by |
| 39 | + their fully qualified name. A form that `def`s a var **exports** the |
| 40 | + backing mutable global from its own module; every later form that |
| 41 | + mentions the var **imports** it. Re-`def` of an existing var |
| 42 | + imports and `global.set`s it — the open world's whole point, now |
| 43 | + across modules — and **never exports it again** *(review)*: the |
| 44 | + first definer owns the canonical global forever, and the session |
| 45 | + ledger *rejects* a duplicate var export, because the silent version |
| 46 | + of that bug is later forms reading a fresh null global as nil. |
| 47 | + `:prod` is untouched: one self-contained module, direct-linked, |
| 48 | + exactly as `0024` left it. |
| 49 | + |
| 50 | +2a. **K is a session constant in linked mode — 20 — because per-module |
| 51 | + K breaks cross-form fn values** *(review — the blocker)*. `0024` |
| 52 | + sized the `$Fn` slot table per module; two forms with different max |
| 53 | + arity therefore emit *structurally different* rec groups, which by |
| 54 | + `0009`'s own identity rule are different types — probed: a closure |
| 55 | + built under a K=1 group, passed through a var global, **traps |
| 56 | + "illegal cast"** at a K=2 importer's `ref.cast (ref $Fn)`. So every |
| 57 | + linked-mode module — the runtime included, when it ever holds fn |
| 58 | + values — emits the same `$sig0..$sig20`/`$Fn` group. Twenty is not |
| 59 | + arbitrary: **Clojure itself refuses more than 20 positional |
| 60 | + params**, so the ceiling is JVM parity, not a new restriction, and |
| 61 | + arities beyond it arrive with `apply`/varargs, not with wider |
| 62 | + structs. The cost is ~21 nullable slots per closure struct in dev |
| 63 | + mode only; `:prod` keeps `0024`'s per-module K, where the module is |
| 64 | + whole-program and prunable. `0024` §2 carries the amendment. |
| 65 | +3. **The session environment is the instantiator's ledger.** Whoever |
| 66 | + drives the session — the corpus lane today, the nREPL server next — |
| 67 | + holds one import object: the runtime instance's exports plus every |
| 68 | + var-global observed so far; after each instantiation it merges the |
| 69 | + new module's exports. The compiler's half of the same ledger is the |
| 70 | + analysis environment, which already persists vars across forms |
| 71 | + inside one `analyze-forms` call; the session makes that persistence |
| 72 | + explicit across calls. |
| 73 | +4. **The corpus gains a third compiled lane, not new entries**: every |
| 74 | + existing entry's `:forms` vector *is* a session, so the dev-linked |
| 75 | + lane runs each form as its own module against one shared runtime |
| 76 | + instance and compares the last form's value — the same oracle, the |
| 77 | + same table. Entries with cross-form `def`s (`def-uses-def`, |
| 78 | + `fn-redef-dev`) stop being single-module simulations of a session |
| 79 | + and become the real thing. |
| 80 | +5. **The linked lane lands on V8 first; the wasmtime half is deferred |
| 81 | + on cost, not capability** *(review — the first draft said the CLI |
| 82 | + could not link, which is false)*: `wasmtime run --preload` exists at |
| 83 | + 47, preloads can import from earlier preloads, and a form module |
| 84 | + carrying its evaluation in a start section runs at instantiation — |
| 85 | + the review drove a three-module session through the CLI that way. |
| 86 | + The trade it costs: start-section evaluation means **a throwing form |
| 87 | + fails instantiation**, which any driver must classify as the form's |
| 88 | + outcome, not a broken lane. The node session runner is the shape |
| 89 | + the nREPL unit consumes next, so it lands now; the wasmtime linked |
| 90 | + lane (CLI `--preload` or `cljwit.host`'s FFM world) is recorded as |
| 91 | + feasible today and lands with the server unit — a scope choice, |
| 92 | + named to keep `0007`'s one-path lesson from being re-derived. |
| 93 | + |
| 94 | +## Why |
| 95 | + |
| 96 | +- Assembly cost: `0026` measured per-form cost linear in module text |
| 97 | + (0.92–1.94 ms over 4–8.7 KB); the runtime preamble is the bulk of |
| 98 | + every small form's text, and importing it caps the per-form module at |
| 99 | + roughly the form's own code. |
| 100 | +- Cross-module type identity is not a hope: demonstrated for the `$Fn` |
| 101 | + substrate across assemblers in `0026`'s review, and pinned in the |
| 102 | + gate since `0009` for rec groups generally. |
| 103 | +- Nominal things (functions, globals, tags) already have exactly one |
| 104 | + sharing mechanism in Wasm — import/export — so the only real design |
| 105 | + freedom was the namespace layout (`"rt"`/`"vars"`) and who keeps the |
| 106 | + ledger; both follow from the nREPL unit being the next consumer. |
| 107 | + |
| 108 | +## Alternatives rejected |
| 109 | + |
| 110 | +- **Keep duplicating the runtime per form.** Works today, and `0026`'s |
| 111 | + falsifier says exactly how it dies: the margin shrinks linearly as |
| 112 | + the runtime grows toward a core library. Import-shaped forms are the |
| 113 | + named exit, taken before the preamble grows rather than after. |
| 114 | +- **A single mutable "session module" re-instantiated per form.** Loses |
| 115 | + the heap (every form's data dies with the previous instance), which |
| 116 | + is the opposite of `0009`'s world. |
| 117 | +- **Registering var globals back into the runtime module's table** |
| 118 | + (a `vars` table indexed by number). An indirection layer with its own |
| 119 | + allocator, to avoid string-keyed import resolution the host already |
| 120 | + does per instantiation; and it erases the property that a form's |
| 121 | + module *says* which vars it touches. |
| 122 | +- **Exporting types by name.** Not a thing Wasm has for GC types, and |
| 123 | + not needed — rec-group identity is structural; chasing nominal type |
| 124 | + sharing would reinvent what `0009` already measured as free. |
| 125 | + |
| 126 | +## What would falsify this |
| 127 | + |
| 128 | +- **A var global imported before its defining module exports it** — the |
| 129 | + ledger's ordering assumption. Clojure's own forward-reference story |
| 130 | + (`declare`, unbound vars) is out of the slice today (`(def y y)` |
| 131 | + refuses); if `declare` lands, the ledger needs a declared-but-unbound |
| 132 | + representation, and this note gains an amendment. |
| 133 | +- **Import resolution cost growing with session length** — hundreds of |
| 134 | + var imports per form would show up in `0026`-style measurements; the |
| 135 | + per-form import list is the form's own free variables, which for |
| 136 | + human-written forms stays small. Re-measure if a generated-code |
| 137 | + session contradicts that. |
| 138 | +- **An engine cost cliff for many-module sessions** (thousands of live |
| 139 | + instances in one store). Unmeasured; the nREPL unit's soak test is |
| 140 | + where it would surface, and `0026`'s per-form numbers bound the |
| 141 | + entry cost. |
0 commit comments