diff --git a/.omo/evidence/coherence-audit/assembly.md b/.omo/evidence/coherence-audit/assembly.md new file mode 100644 index 0000000..4ef32e6 --- /dev/null +++ b/.omo/evidence/coherence-audit/assembly.md @@ -0,0 +1,187 @@ +# Coherence Audit: Assembly (ma-assembly) + +Scope: README's claim - "Yjs CRDT for offline-first proposals/rosters/votes +with liquid-democracy tally, a 3-layer resilience model (local-first, WebRTC +partition-and-heal, QR-sneakernet air-gapped fallback), and a signed public +testimony board reusing the same event-log infrastructure as group voting." +Testimony board sync is explicitly OUT of scope here (already audited and +fixed this session - see `.omo/evidence/service-sync-reevaluation.md`). + +Files read: `src/lib/governance.ts`, `src/lib/assemblySync.ts`, +`src/lib/qrSneakernet.ts`, `src/lib/governanceEvents.ts`, +`src/miniapps/assembly/useAssemblyEngine.ts`, `src/miniapps/assembly/AssemblyScreen.tsx`, +plus grep across `src/` and `.omo/evidence` / `.omo/plans` for cross-references. + +## 1. Liquid democracy - REAL, not mislabeled + +`src/lib/governance.ts` implements actual vote delegation, not plain +one-resident-one-vote QV wearing a label: + +- `DelegationRecord { delegatorId, delegateId }` + `putDelegation` / + `removeDelegation` (governance.ts:60-120, CRDT-backed `Y.Map`). +- `resolveEffectiveVoter` (governance.ts ~190-230) walks the + `delegatorId -> delegateId` chain transitively (A delegates to B, B + delegates to C -> C's ballot counts for A), with a `visited` set that + detects a cycle (A -> B -> C -> A) and deterministically falls back to the + starting resident's own ballot instead of looping or double-counting. +- `computeQvTally` (governance.ts ~270-330) resolves every enrolled + resident's effective voter through `resolveEffectiveVoter` before adding + their `sqrt(credits)` weight to a tally bucket - delegation moves *whose + choice* counts, never whose credits are spent (the module's own stated + design, matching real liquid-democracy semantics, e.g. Behrens et al.). +- Wired into the UI: `AssemblyScreen.tsx` has a "Your delegation" card + (`assembly-delegate-select` / `assembly-delegate-submit` / + `assembly-revoke-delegation`) calling `useAssemblyEngine`'s `delegate` / + `revokeDelegation`, which call `putDelegation` / `removeDelegation` + directly. Casting your own ballot and having an active delegation are + mutually exclusive in the UI (`canVoteDirectly = isEnrolled && + myDelegation === undefined`), matching the documented "delegation is + global, not per-proposal" semantics. + +Verdict: liquid democracy is a real, correctly-implemented, UI-reachable +mechanism - transitive, cycle-safe, distinct from the QV ballot/credit +mechanism it sits on top of. No gap here. + +## 2(a). Local-first layer - REAL + +`useAssemblyEngine` persists the `AssemblyDoc`/`GovernanceDoc` via +`openPersistedAssemblyDoc` (`y-indexeddb`), re-renders on `doc.on("update")`, +and exposes a `synced` flag read directly off `IndexeddbPersistence.synced` +(never hard-coded). Proposals, roster, delegations, credits all live in the +Yjs CRDT and work with the WebRTC sync toggle permanently off - the toggle +defaults to `false` ("Assembly stays fully offline/local-first until a +resident opts in", per the hook's own doc comment). Genuine zero-network +operation. + +## 2(b). WebRTC partition-and-heal layer - REAL and wired + +`src/lib/assemblySync.ts`'s `connectAssemblySync` wraps a genuine +`y-webrtc` `WebrtcProvider` (real signaling + RTCDataChannel transport, not +a stub) with a self-hostable default signaling URL +(`VITE_SIGNALING_URL` / `ws://localhost:4444`) and an opt-in third-party +fallback. `useAssemblyEngine` wires this to a real "Sync with devices using +the same code" toggle in `AssemblyScreen.tsx` with a live peer count +(`connectedPeerCount()`, never fabricated - stays honestly 0 when +partitioned or when `RTCPeerConnection` is undefined, e.g. in tests). + +This is independently confirmed by prior work in this repo: gate review +`.omo/evidence/the-commons-hollow-polish/task-11-the-commons-hollow-polish-gate-review.md` +documents a Playwright test driving two isolated browser contexts through +the *shipped* screen's own UI - connect over real WebRTC, prove a proposal +seeded on side A arrives on side B, partition (toggle off + `setOffline`, +tearing the real `RTCDataChannel`), independent proposals on each side +proven not to cross, heal (toggle on + network restored), and converge to +the identical union set. `service-sync-reevaluation.md` separately verdicts +this "works as claimed." This layer is genuine and demonstrable end-to-end +through the real UI, not a console-only harness. + +## 2(c) / 3. QR-sneakernet layer - GAP FOUND: real transport, zero UI wiring, camera never reachable + +`src/lib/qrSneakernet.ts` is a genuinely well-built transport: + +- `encodeDelta` / `decodeFrames` / `FrameCollector` chunk a real Yjs CRDT + delta (`Y.encodeStateAsUpdate`) into self-describing frame strings with + per-frame CRC32, sequence/total, and whole-delta length+CRC32 (so a + misread, dropped, or cross-transfer frame is detected rather than + silently corrupting the reassembly). +- `renderFrameToDataURL` / `renderFramesToDataURLs` use the real `qrcode` + package to render actual QR code images. +- `scanFrameFromImage` uses the real `jsQR` package against a + `CameraImage { data, width, height }` (the exact shape `canvas.getImageData` + produces) - this is a genuine QR decode, not a copy-paste text blob. +- The task-24 build evidence + (`.omo/evidence/the-commons-hollow/task-24-the-commons-hollow.json`) + confirms a real round-trip: a 17,779-byte Yjs delta (well over one QR's + ~2.9KB capacity) chunked into 35 frames, reassembled out-of-order with a + dropped-then-retried frame, byte-for-byte equal to the original, then + applied to a target `Y.Doc` via `Y.applyUpdate` - genuinely exercising the + CRDT, not a toy string. + +**But it is never called from anywhere outside its own test file.** Grep +across `src/` for `qrSneakernet|scanFrameFromImage|FrameCollector|encodeDelta|decodeFrames` +and across `src/miniapps` for `qrSneakernet` turns up matches only in +`qrSneakernet.ts` itself and `qrSneakernet.test.ts`; `miniappSharing.ts` +mentions it only in a comment ("Assembly's QR-sneakernet transport ... kept +independent here"), never an import. `AssemblyScreen.tsx` / `useAssemblyEngine.ts` +import `QRCode` from the `qrcode` package directly, but only to render the +WebRTC **sync-room-code string** as a QR code (`QRCode.toDataURL(syncRoomCode, ...)`) +- a plain text room-id QR, unrelated to `qrSneakernet.ts` and carrying no +Yjs delta at all. `encodeAssemblyState` / `applyAssemblyUpdate` +(`src/lib/assembly.ts`, the functions `qrSneakernet.ts`'s own header says +callers should pair it with) are likewise used only in `assembly.test.ts` +and `assemblySync.test.ts` - never in a mini-app screen. + +There is also no `getUserMedia` / `BarcodeDetector` / any camera API call +anywhere in `src/` (confirmed by grep: zero matches). The task-24 evidence +file says this explicitly under its own `real_qr_packages_wired.camera_note`: +*"no real camera needed for the acceptance tests ... `scanFrameFromImage` is +the production jsQR entry point a `