Brickheart

Brickheart Build session handover

: Written by a human with AI assistance.

1. Goal and current state

Brick Heart is an accessibility-focused LEGO site and app. The app (app.brickheart.org, local first) lets people place parts from a palette into a scene, with placements checked by BrickAgent/BrickNet. The target flow for the next session is: DB entry → pick pack → build the set's MPD → palette beside the scene → viewer shows the MPD with scattered parts → UI edits update the MPD → rendering and collision checks.

Done so far:

  • Data pipeline (Python) produces a 2,210-set DB, and every kept part and colour maps to LDraw.
  • Three gzipped LDraw packs are written.

Not started: the viewer, palette UI, MPD builder, voice and IDs.

2. Working preferences

  • Ask clarifying questions before writing code. The user does the work and wants one step at a time.
  • Don't guess, gather data first, and say what is verified and what isn't.
  • All DB work stays in Python (load_data.py). Node/TypeScript only reads the finished artefacts.
  • Tooling scripts are .mjs. App source in src/ is TypeScript.

3. Repo and constraints

  • Repo ~/brickheart-web (Ubuntu, VS Code, GitHub to Cloudflare Workers). Folders: site/, dev/, app/, shared/assets, shared/engine.
  • dist/ is generated and gitignored, and Cloudflare only runs templating, so it can't run the pipeline or read the 29k-file LDraw library or rebrickable.db. Generated data must be committed, then copied into dist/ by the app's build.mjs.
  • app/ looks like a Vite TypeScript scaffold (package app@0.0.0). Its tsconfig: es2023, bundler resolution, types: vite/client, erasableSyntaxOnly, verbatimModuleSyntax, noEmit, include: ["src"]. It is not yet in the root workspaces.
  • Site rules that apply to the app: relative links, strict CSP (no inline scripts or styles), accessibility basics (focus rings, 44px targets, reduced motion, forced-colours).
  • Cloudflare static assets: 25 MiB per file, 20,000 files on the free plan (100,000 on paid). Docs state no compression exception, so assume the limit is on the uploaded file.

4. Product spec essentials (from the decision log)

  • Browser app, offline-capable after a first visit. No LLM and no mouse drag, ever; touch and audio friendly. Voice (NeuDecide ONNX, about 43 MB) is wanted but optional. Out of scope: minifigs, flexible parts, nested assemblies, editing assemblies after creation.
  • MVP scope (user's words): the viewer only needs to show the MPD with LDrawLoader.js, no spec customisation. Then a basic UI to place bricks from the palette on top of one in the scene. Reference viewers: github.com/kulits/BrickAgent/blob/master/src/brickagent/viewer/README.md#browser and github.com/PolymerLabs/brick-viewer/tree/master/docs.
  • Palette: either a finite set inventory (every physical piece has its own ID) or an unlimited era or designer palette (stack of 3 per part). IDs are 2 to 5 digits, global, spread by spoken-sound edit distance, and belong to inventory pieces rather than being created at placement. Not implemented anywhere yet.
  • Placement: PLACE with no target joins stud 0 to hole 0, and the first brick goes at 0,0,0. MOVE counts in studs, plates and bricks. Directions are screen-relative, and UP and DOWN are world-vertical (-Y is up).
  • History is the truth: ordered change lines, with the whole file regenerated after every command. Undo and redo are temporary and in-app. Save and reopen are required.
  • File format: a valid .mpd. App state lives in comment lines beside each part, with a unique app prefix and no LDraw reserved words. Zone 5 is the main block, other built zones are separate FILE blocks, and the palette is written after the last STEP. Spec default palette layout: one slot per part type, copies stacked in a line, on one side behind the cameras. A deleted part's line moves back to its slot.
  • Zones and cameras: 9 zones, each 16, 32 or 64 studs square, and 4 camera presets. Not needed for the MVP.
  • Milestone 0 checks still open: BrickAgent/BrickNet signatures and whether attach() accepts a virtual surface and runs in a browser (Pyodide or a port); mirror-twin data; LDraw axis signs for front and left (unconfirmed); how LFrame handles submodels and the step cut-off; NeuDecide weights and ONNX port; whether 0 // app comments survive round trips through other apps; whether an empty block is a problem in target apps.

5. Data pipeline state

load_data.py rebuilds rebrickable.db from scratch on every run (schema.sql, Rebrickable CSVs and the enrichment CSVs). Final DB: 2,210 sets, 2,022 parts, 74 colours, 70,496 inventory_parts rows, 11.07 MB. Sets have 15 to 399 parts. The pipeline also does the following: keep version 1 inventories, drop minifigs and stickers, drop side-project themes, roll colours up to the 74 modern colours, keep the top 100 printed parts (the rest swapped for plain parts), and map mold variants to the newest mold.

Added this session:

  • fetch_ldraw_map.py (one-off, requests, run by the user) writes ldraw_map.json from the whole Rebrickable parts catalogue (paginated) plus the colours list.
  • load_ldraw_map(conn) adds parts.ldraw_stem (the BrickNet canonical stem; NULL means no model) and colors.ldraw_code.
    • A part resolves if one of its LDraw ids is a BrickNet stem or alias, has a collider file, and is not in missing_ldraw_parts.csv. The first id that passes is kept.
    • A colour resolves if one of its LDraw codes is in BrickNet's colour table.
  • remove_unbuildable_sets(conn, check_parts, check_colors) is strict: any non-spare part or colour without a mapping removes the set. The log shows it ran once, after the colour roll-up, and removed 5,112 sets. Confirm the wiring in load_data.py.
  • ldraw_weight.py and remove_heavy_part_sets remove sets with a part over MAX_PART_TRIANGLES. The value is a placeholder of 10,000. It removed 13 parts and 28 sets, including Plate 16 x 16 (91405), so the threshold is not final.
  • missing_ldraw_parts.csv is a draft list from BrickArchitect (single LDRAW_ID column, true LDraw ids, Scala/Duplo/Clikits to be excluded). The site disallows automated access, so it's built by hand.

Key tables: sets, themes (carries subject_id), inventories (set_num to id, with set_palette), inventory_parts (inventory_id, part_num, color_id, quantity, is_spare), parts (ldraw_stem), colors (rgb, is_trans, ldraw_code), model_subject (1 Real World, 2 Original Fiction, 3 Licensed Fiction, 0 Unknown). Keep LDraw stems as strings.

6. LDraw and BrickNet facts

  • LDraw library at ~/bricknet/ldraw (parts/, parts/s/, p/, p/8 with 127 files, p/48 with 926, plus unofficial/). Units: 20 LDU per stud, 8 per plate, 24 per brick height. A type-1 line is 1 <colour> x y z a b c d e f g h i <file>.
  • BrickNet (Python, pip install bricknet, needs pybullet): catalogue of 14,583 parts, with 14,462 colliders found locally in ~/.local/share/bricknet/cvx/<stem>.cvx.obj (also glb/, inset/). bricknet.data has load_catalog (stem_to_id, id_to_stem, conn_counts, code_to_color, color_to_code), load_aliases and load_connectors. Catalogue ids are LDraw stems without .dat.
  • Aliases: 2,522 old stems map to a canonical stem plus a 4x4 transform. The loader keeps only the canonical stem and discards the transform. The five I saw were identity, but non-identity ones would change a part's pose.
  • Rebrickable colour -1 ("unknown") maps to 17 LDraw codes. Check it is absent from the final colours table.

7. The packs (this session's output)

app/data/ldraw/subject-1.mpd.gz, subject-2.mpd.gz, subject-3.mpd.gz, plus manifest.json (name, file, sets, parts, files, raw and gzip bytes, sha256), written by ldraw_pack.py. Sizes:

Pack Sets Parts Files Raw Gzip
1 Real World 1,316 1,672 3,880 29.89 MiB 4.83 MiB
2 Original World 299 899 2,102 12.24 MiB 1.98 MiB
3 Licensed World 439 1,048 2,478 16.90 MiB 2.72 MiB

Total 9.52 MiB gzipped. Parts used in more than one subject are duplicated across packs.

Pack format: sorted 0 FILE <name> ... 0 NOFILE blocks, with names written as parts reference them (3001.dat, s\3001s01.dat, lowercase). Primitives resolve to p/8 where it exists, filed under the plain name. Comments are kept, output is deterministic, and there are no colour definitions.

8. Target flow and checks for the next session

  1. DB entry: set number gives its inventory (non-spare rows with ldraw_stem, ldraw_code and quantity) and its pack via themes.subject_id. How the browser reads the DB is undecided (section 9).
  2. Pack load: fetch the gzipped pack and decompress it in the browser (DecompressionStream). Check the host doesn't double-encode .gz.
  3. Build the set MPD: the first FILE block in an MPD is the main model, so write the main model block first and append the pack blocks after it.
  • The wall is vertical, not a floor grid.
  1. Palette UI beside the scene: list the parts and colours of the set, using colors.rgb for swatches and parts.name as labels.
  2. Viewer: LDrawLoader.js (three.js) renders the MPD. It needs colour definitions, so LDConfig.ldr has to ship with the app.
  3. UI updates the MPD: keep an ordered change history and regenerate the MPD text after each command, as the spec says. How the viewer refreshes (full reparse or diff) is untested and needs a performance check with large sets.
  4. Checks: colours and transparency render correctly; low-res primitives look acceptable; aliased parts sit in the right pose; connector and collision checking works (see the first open decision below).

9. Open decisions

  • Collision checking in the browser is the biggest risk. BrickNet and BrickAgent are Python with pybullet. Whether they can run offline in a static browser app (Pyodide, a port, or a local sidecar for the MVP) is unverified.
  • How the browser reads the data: the SQLite DB (sql.js or similar), a JSON export per set, or D1 online. Offline use argues for bundled static data.
  • Offline caching: cache everything on first visit (packs about 9.5 MiB gzipped, DB 11 MB, voice model 44 MB) or load per subject. Unanswered.
  • Placement MVP: which stud-to-hole rule and which BrickNet connector data to use for "place on top of one in the scene".
  • Non-identity aliases: how many, and whether the MPD builder needs to apply the transform.
  • MAX_PART_TRIANGLES value and any exceptions for basic plates.
  • missing_ldraw_parts.csv: finish it, and decide whether subject 0 and the no-subject set are dropped, merged, or given a pack.

10. Verified and not verified

Verified by output the user pasted: pack sizes above, 14,583 catalogue stems and 14,462 colliders, 2,522 aliases, the pipeline log and final DB counts, the Cloudflare limits (searched), and the API's LDraw id format (stems, with 10049pr0001 mapping to 10049p01).

Not verified:

  • LDrawLoader reading backslash and lowercase FILE names, or handling NOFILE.
  • Whether ldraw_code is non-NULL for all 74 final colours (check SELECT COUNT(*) FROM colors WHERE ldraw_code IS NULL).
  • Whether the 10 parts that had no ldraw_stem in an earlier build are spare-only.
  • Memory use on phones when 30 MiB of raw pack text is decompressed.
  • Whether the colour check removed sets that a palette tweak could rescue. The cause of the 5,112 removals was never broken down by parts versus colours.

11. Housekeeping

  • Add app to workspaces when it gets its build.mjs.
  • ldraw_pack.py imports the private _resolve from ldraw_weight.py. Tidy later.
  • Commit the three packs and manifest.json. The manifest hashes can version the offline cache.