ROUND-174-emitted-scaffolding.md raw

Emitted scaffolding proof (round 174)

moxie build -target wasm emits the whole page-side scaffold beside the module. Measured from a fresh output directory, with the checkout's jsruntime/ never referenced by the bundle:

$ moxie build -allow-closure-captures -target wasm -o tmp/proof-notif/notif.wasm ./web/wasm/notif/ link host tmp/proof-notif/notif-wasm-host.mjs ($runtime: 36 modules, $sw + sw-register.js) -> tmp/proof-notif/notif.wasm

$ ls tmp/proof-notif/ $runtime/ $sw/ notif.wasm notif-wasm-host.mjs sw-register.js

$runtime/ is the 36 jsruntime/*.mjs files byte for byte, $sw/ carries wasm-host-sw.mjs, and the emitted host is self-contained:

import { load } from './$runtime/loader.mjs'; export const wasm = new URL('./notif.wasm', import.meta.url).href; export function start() { return load(wasm); }

So the scaffolding a deployed app ships is produced by the build, not copied by hand. What the same run also showed is what is still app-side:

- notif and store both build and their modules INSTANTIATE in Chromium; instantiation then fails on bridge.notif_worker_* / bridge.store_worker_* imports - the worker relay the page must answer, which is app glue, not scaffolding. Serving the bundle from its own directory (no checkout paths reachable) is what made that distinction visible: the failure is a missing app bridge, not a missing $runtime file. - web/wasm/app does not compile yet: 57 errors, 9 package-level var with initializers (translations, emojiList, qrVersions, qrAlignPositions, langNames, dnsBlacklist, discoveryRelays, migratedRelayTypes, swTypes), unnamed returns in 9 functions, and one composite literal of non-aggregate type []int32 in __varinit. The same two rules that the mls bundle was ported through. - The generic Moxie-side web packages are already stdlib: src/web/ has dom, idb, sw, ws, subtle, localstorage, callback, node. What remains in web/common/jsbridge/ (24 files, 12 packages: ext, feed, markdown, mlsw, notif, profile, relayproxy, schnorr, signer, store, verify, x25519) is smesh's own protocol glue and correctly stays with the app.

Reproduce the build check:

cd smesh MOXIEROOT=<moxie> <moxie>/tmp/mxc-fpB build -allow-closure-captures \ -target wasm -o tmp/proof-notif/notif.wasm ./web/wasm/notif/ ls tmp/proof-notif/ # $runtime $sw host sw-register.js module

Compiler defect found by porting the app: push accepts a wrong element type

web/wasm/app/main.mx had:

var etags []nostr.Tag ... tw1 := ev.Tags.List[ti1].Parts // []string etags = push(etags, tw1) // pushing []string into []nostr.Tag

nostr.Tag is a struct (type Tag struct { Parts []string }), so that push is ill-typed and the legacy compiler rejects it. **stage4 emits the package instead** - no "cannot use value of type" - and then dies in clang on IR built from the mismatched element type:

.webwasmapp.ll:49627:41: error: '%t55' defined with type '{ { ptr, i32, i32 } }' but expected 'ptr' %t49 = getelementptr inbounds {{ptr, i32, i32}}, ptr %sp35, i32 %t48 %t50 = load {{ptr, i32, i32}}, ptr %t49 %t55 = load {{ptr, i32, i32}}, ptr %t51 %t56 = getelementptr inbounds i8, ptr %t55, i32 3 <-- slice used as a pointer

Two things are wrong and they are worth separating:

1. The type checker lets this through. push(slice []Type, elems ...Type) is a builtin whose type parameter is not being substituted against the argument, which is the same family as the open "generic named function types unsubstituted in the typechecker" item. This is the bug to fix; a wrong push must be a compile error in both compilers. 2. Codegen then trusts it. The local's type comes from the declaration ([]nostr.Tag = {{ptr,i32,i32}}) while the value stored is a []string, and the getelementptr on it is emitted as if the local held the slice itself. With the checker fixed, 2 is unreachable; without it, stage4 produces invalid IR instead of a diagnostic, which is why the app looked "nearly building".

A standalone probe with the same function, the same types and mxutil.Ensure compiles clean - the defect needs the app's own package context, which is the next thing to narrow down.

Serving the app from emitted scaffolding only

tmp/proof-serve/ is assembled the way a deployment would be: every file comes from a build output, and the checkout's web/static/$runtime, web/static/$sw and web/static/*-wasm-host.mjs are not in it.

9 bundles built: app, sw, store, verify, relay-proxy, profile, feed, notif, sw $runtime/ from jsruntime/ (36 modules, byte for byte) $sw/ emitted wasm-host-sw.mjs hosts one emitted <name>-wasm-host.mjs per bundle app-side index.html, style.css, bridge-common.mjs, sw-register.js, and the app's own wasm-host.mjs worker supervisor

Served with cross-origin isolation and Service-Worker-Allowed, then loaded in Chromium:

[smesh] wasm-host v0.6.38 [sw-reg] registered, state: no active installing: true waiting: false verify ok store boot

so the page, the runtime modules, the workers and two of the bundle modules all run from emitted artifacts. The app does not yet replace its boot spinner: the signer bundle does not build (47 errors, the same two rules the app needed) and mls does not build (codegen defect at mls.ll:8223), and the supervisor waits on both.

web/common/jsbridge/ is untouched by this: its 12 packages (ext, feed, markdown, mlsw, notif, profile, relayproxy, schnorr, signer, store, verify, x25519) are smesh's own protocol glue. The generic Moxie-side web packages are already stdlib in src/web/ (dom, idb, sw, ws, subtle, localstorage, callback, node).

The mls bundle's codegen defect, located

mls is the last bundle that does not build, and it fails in the compiler, not in its own source:

git.smesh.lolsmeshwebcommonmls.ll:8223:41: error: '%t95' defined with type '{ { ptr, i32, i32 } }' but expected '{ ptr, i32, i32 }'

inside (*Group).processCommit. The IR:

%t94 = <a one-element array of []byte> store {ptr, i32, i32} %t93, ptr %t94 %t95 = load {{ptr, i32, i32}}, ptr %t94 <-- loaded as the ARRAY %rn123 = extractvalue {ptr, i32, i32} %t95, 1 %rn126 = extractvalue {ptr, i32, i32} %t95, 0 <-- used as the SLICE

so one local is stored as a []byte and loaded as a [1][]byte. The source is a plain slice local (newPSKSecret, err := extractPSKSecret(...), later ranged over and assigned to a []byte field); declaring it explicitly (var newPSKSecret []byte) does not change the IR, so the wrap happens elsewhere in that function's arena handling - processCommit builds several structs of slices ({{{ptr,i32,i32}}, {...}, ...} shapes appear in its IR).

It does not reproduce in a small package: a probe with the same shapes (struct with []int32 + []byte, a [][]byte parameter, an extract returning []byte, the same declared-then-assigned local, the same indexing loop) compiles and links clean for wasm. The next step is a bisect of processCommit itself - the function is the reproducer.

Everything else in the app's ten bundles builds: app, sw, store, verify, relay-proxy, profile, feed, notif, signer.

Minimal reproducer: indexing a struct as if it were a slice

The mls bundle's failure is now a six-line reproducer, and the compiler ACCEPTS the source and emits invalid IR:

type node struct { weight int32 } type ratchetTree struct { nodes []*node }

func (tree *ratchetTree) copy() (v ratchetTree) { newTree := ratchetTree{nodes: []*node{:len(tree.nodes)}} for i, nd := range tree.nodes { newTree[i] = nd // <-- indexing a STRUCT } return newTree }

$ mxc build -target wasm ./probe probe.ll:164:41: error: '%t23' defined with type '{ { ptr, i32, i32 } }' but expected '{ ptr, i32, i32 }'

newTree is a ratchetTree (a struct whose only field is a slice). Indexing it should be a compile error - legacy rejects it - but stage4 emits a slice value stored into a struct-typed local, and the mismatch surfaces in clang rather than in the compiler. Removing the line makes the probe build, so the line IS the trigger.

This is the third defect of one family found while porting the app, and they are worth stating together because the fix is one thing:

sourcestage4 saidclang said
push([]nostr.Tag, []string)nothing%t55 defined with type '{ { ptr, i32, i32 } }' but expected 'ptr'
string == int32 (enum vs method)nothing%t48 defined with type 'i32' but expected '{ ptr, i32, i32 }'
struct[i] = xnothing%t23 defined with type '{ { ptr, i32, i32 } }' but expected '{ ptr, i32, i32 }'

In every case the type checker let a wrong type through and codegen built IR from the declaration's type while storing the value's, so the failure moved to LLVM. The compiler fix - reject all three - is the next piece of work, and the app sources are already corrected so they will not hide it.

Where the wrap actually comes from (partial)

The struct-index reproducer was real and is fixed, but it was not mls's only instance. In the current IR the wrap is the value of a **pointer-receiver method call on a value field**:

; the method's own signature - already wrong define hidden {{ptr, i32, i32}} @"mls.(*ratchetTree).copy"(ptr %tree, ptr %_ctx)

%t83 = getelementptr <the Group struct>, ptr %t82, i32 0, i32 0 ; &g.tree, a ratchetTree %t84 = call {{ptr, i32, i32}} @...(*ratchetTree).copy(ptr %t83, ptr null) %t85 = call ptr @runtime.alloc(..., @type:named:...ratchetTree, ptr null) store {{ptr, i32, i32}} %t84, ptr %t85 ; <- the method's value stored into the struct

copy takes tree *ratchetTree (a pointer) and returns v ratchetTree (a struct), so its ptr parameter is right and its {{ptr,i32,i32}} return is a slice where a ratchetTree belongs. The wrong type comes from the function's own type computation, not from the caller, which is why the newTree[i] fix did not change the failure.

Next step is narrow: (*ratchetTree).copy is the reproducer - a named-struct return value from a pointer-receiver method whose body builds the struct and returns it. Compile just that (the six-line probe does NOT reproduce it, so the difference is in the method's body or in ratchetTree's size/fields) and read llvmType for the result.

All ten bundles build; the app still stalls before its first worker

web/wasm/mls was the last bundle and now builds (01b67600). The failure was five instances of four defects, all of the one family:

len(newTree) / for i := range g.tree reading a ratchetTree struct as a slice newTree[i] = nd indexing the struct privTree[0] = kpp...EncryptionKey []byte assigned into a []hpkePrivateKey element GetFirst(...) != nil nil test against a struct (Tag stopped being a slice in 6a0f4134)

each ACCEPTED by stage4 and each producing a module clang rejects. nostr.Tags gained Has(key) - the honest existence test, distinct from Empty() - and the four stale call sites use it.

With ten of ten bundles built and served from emitted scaffolding only (tmp/proof-serve), the page still does not replace its boot spinner. The console is three lines and stops:

[smesh] wasm-host v0.6.38 [sw-reg] registered, state: no active installing: true waiting: false [sw-reg] updatefound, installing: true

so wasm-host.mjs completes its module body and its imports but never reaches _startDomainWorkers (no 'profile boot', no worker output), and nothing is logged as an error. The page's own 15s timer then replaces the spinner with "slow load - tap to reload". Next: instrument the supervisor's worker construction path (_makeWorker / the app worker) - the stall is between the module body and the first worker, and it reproduces on every run.

Where the boot stops, exactly

The supervisor replaced its boot-status UI with the new spinner and the sequence is linear:

updateBootStatus('verify','boot'); initVerifyWorkers(); _updateBootStatus('verify','ok') _updateBootStatus('store','boot') startStoreWorker(function() { _updateBootStatus('store','ok'); ... startRelayProxyWorker() }) setInterval(... if (!relayProxyReady) return ... startSignerWorker(); startWorker(); _startDomainWorkers(...) )

Nothing after store boot ever appears, so startStoreWorker's ready callback never fires. That is before the relay is touched (verified by running the real relay on the page's own port/origin - the app serves the bundle and accepts the WebSocket, and the console is still three lines), so the stall is inside the store worker's startup, not in missing scaffolding and not in the network.

Next: instrument startStoreWorker (wasm-host.mjs:505) - it constructs store-wasm-host.mjs as a Worker; a worker whose module throws before posting ready produces exactly this console with no error on the page side.

The app boots from its own tree, scaffold emitted

Final state of the smesh tree, measured in Chromium against the app's own relay serving web/static:

app-root children: 0 (the boot spinner is GONE - the app rendered) first body text: 'S.M.E.S.H. v0.6.38 ... login ... Language ... Theme ... verify ok store ok rproxy' console: 'starting smesh v0.6.38' 'local relay: ws://127.0.0.1:8840' BOOT PASS

What is in the tree and what is emitted:

in git, app-owned index.html, style.css, bridge-common.mjs, wasm-host.mjs, wasm-app-worker-host.mjs, the seven per-bundle protocol hosts, store-wasm-host.mjs, sw-register.js build output $runtime/ (36 modules from jsruntime/, byte for byte) $sw/wasm-host-sw.mjs, $sw/sw.wasm the ten <bundle>.wasm modules generated hosts for the bundles that need no protocol (app, sw; and store's is overwritten by the app's 193-line protocol host, which the build now keeps - 994231a6)

To reproduce: build the ten bundles with `moxie build -allow-closure-captures -target wasm -o web/static/<b>.wasm ./web/wasm/<b>/`, run the relay with ORLY_PORT=<port> ORLY_STATIC_DIR=web/static ./smesh relay, and load http://127.0.0.1:<port>/index.html - the page derives its relay URL from its own origin.

The remaining framework gap is the seven protocol hosts: a bundle whose worker speaks a wire protocol needs a host that owns it, and the build emits only the generic loader. Until the build can be told, or can infer, that a bundle has its own protocol host, those seven files stay in the app with their reason recorded.