Brickheart

Brick Heart: handover for the next session

: Written by a human with AI assistance.

1. State at end of this session

Verified (by you, in Edge):

  • app/data/models/bulldozer.mpd (an excavator with 8 nested submodels, 45° tilts and flipped matrices) loads as individual bricks via the reference path.
  • The render path runs: composeMatrix (h), the URL-modifier hook, getColorMaterial, getPartGeometry (merge, flip 180° about X, cache by part id) and the PartsRenderer loop with its cancel token. Memory was about 57K.
  • Keyboard picking and moving worked. Export was written last, and confirmed in Stud.io.

Inferred, not read from the reference:

  • engineRotFromLdraw was my derivation (F·R·F with F = diag(1,-1,-1)). It held on the excavator, but 2fmu4y60objlb.js lines 160 to 190 have never been seen.
  • Record y and z are stored negated (engine coordinates, y up).
  • I used three's stock LDrawLoader and LDrawConditionalLineMaterial, not the reference's class m (lines 434 to 857, 935 to 1224) or M (1224).

Not done, and the main gap: the bench is not offline. bench-build.mjs writes a flat file tree from the parts embedded in the MPD and serves it statically, so it only works for that one model. I called that "a stand-in for the offline store", and it was not one.

Build order is npm run build (it empties dist), then node bench-build.mjs bulldozer.mpd, then npm run preview.

2. Agreed design (offline part files)

Principle: LDrawLoader needs .dat text for a set's parts and nothing else. The browser database holds the data. Python is kept only because _resolve (library search order, p/8 preference) already exists there. You said it is not core.

  1. Build step, ldraw_pack.py (Python).
    • Select sets through sets.subject_id (agreed change, see section 4).
    • Strip each file to line types 1 to 5 and 0 BFC lines. Round numbers if wanted.
    • Write each file's direct subfile list (deps).
    • Output one gzipped JSON line per file, {n, d, t}, instead of 0 FILE and NOFILE blocks, so the browser can stream it.
    • Regenerate the manifest (names, sizes, SHA-256).
  2. Install (once per subject, on "Make available offline").
    • Stream the pack through DecompressionStream('gzip') and split on newlines.
    • Write batches into an IndexedDB files store keyed by the normalised name.
    • Normalisation is lowercase, backslash to slash, strip a leading parts/ or p/. It is applied on write and on lookup.
    • Call navigator.storage.persist() and handle failure.
  3. Open a set.
    • Read the stems from the inventory.
    • Walk deps in IndexedDB for the closure, a few hundred rows.
    • Build an in-memory Map of name to text and answer the loader's requests from it. No pack is assembled and nothing extra is written.
  4. Inventory source. Stay on the existing sql.js database for now. Moving it to IndexedDB is the separate Phase 1 decision.
  5. Loader interception. Patch FileLoader.prototype.load, scoped to /ldraw/ URLs, to read from the Map. The reference's URL-modifier still runs first. The store interface stays the same if this later moves to a service worker.

Measured facts behind it (subject 1):

Item Value
Raw size 32,892,877 bytes
Non-comment bytes 30,861,570
Gzip, current 5,342,561 bytes (5.09 MiB)
Gzip, stripped (no rounding) 4,879,989 bytes
Saving from stripping about 9%

Stripping is for parse speed and cleanliness, not download size. Baking geometry in Python would shrink it much more, but it abandons LDrawLoader. I deferred that.

3. Plan of action for the next session

  1. Packer. Edit ldraw_pack.py as above and regenerate. Report per-subject set, part and file counts, raw and gzip sizes, and duplicate-name checks across subjects.
  2. IndexedDB layer (TypeScript).
    • normalise(name).
    • installSubject(id, onProgress).
    • getClosure(stems).
    • isInstalled(id).
    • removeSubject(id).
    • Handle failed and interrupted installs, and the temporary versus persistent distinction.
  3. Loader hook. Patch FileLoader.load, then check that LDConfig.ldr and every .dat go through it.
  4. Bench v2. Take the excavator's part list from the real installed pack, with no embedded blocks and no /bench/ldraw tree. Delete the static tree and the model.mpd fetch for parts.
  5. Offline test. Load online once, install, switch Edge to offline, reload, and confirm it renders. Report install time, extracted size, time to first render and memory against 57K.
  6. Resolve the inferred pieces. Paste 2fmu4y60objlb.js lines 160 to 190 and swap in the real engineRotFromLdraw. Confirm or correct the stock loader versus class m.

After that, in order:

  • The ?set= build page from the database inventory, with the palette (this needs your approval of the ui.json keys).
  • Connector-based PLACE, which needs 2fmu4y60objlb.js lines 205 to 337 and the connections.json format.
  • The service worker, the colour modes, and splitting the Real World pack.

Questions:

  • Stripping and printed parts: the excavator test only used plain parts. Do you want a printed part and a part with fixed-colour subparts added to the test list, to exercise colour inheritance after stripping?
  • Persistence: should the install ask for persist() when the user taps "Make available offline", or silently?

Carried-over cautions:

  • Don't guess. Mark anything unverified. British spelling.
  • All visible text in the real app goes through site/ui.json with your approval. The bench is English only and not shipped.
  • The sql.js CSP uses 'wasm-unsafe-eval', not 'unsafe-eval'.
  • Cloudflare limits are 25 MiB per file and 20,000 files, and generated artefacts must be committed.