How mikechwu/nanotoybox works
1. Overall shape
- Atom Dojo is a multi-entrypoint repository: browser apps for interactive simulation and playback, Cloudflare serverless endpoints for sharing/account/privacy, a Python reference engine for validation, and a small companion Worker for retention tasks.
- The architecture is centered on a physics simulation core that can be driven locally in the browser or restored from shared “capsules” and histories.
2. Primary browser products
- lab/ is the main interactive playground: React UI, Three.js rendering, simulation runtime, worker orchestration, timeline recording, and handoff/export flows.
- watch/ is the read-only playback app: it opens capsule/history documents, interpolates frames for smooth playback, and supports “Interact From Here” to transfer the current state into Lab.
- viewer/ is a minimal static trajectory viewer for simple file-based viewing.
- account/, privacy/, terms/, and privacy-request/ are separate static or semi-static entrypoints for user/account/legal/privacy flows.
3. Physics runtime boundary
- The simulation engine is browser-side and designed to avoid main-thread blockage.
- Default execution is WebAssembly (lab/wasm/tersoff.wasm + JS glue) inside a Web Worker; there is a JavaScript fallback and a synchronous fallback if the worker stalls.
- The physics layer is responsible for atom positions, velocities, force calculation, integration, bond topology changes, and optional damping; the renderer/UI do not own physics state directly.
4. Lab runtime decomposition
- lab/js/runtime/ contains the major runtime subsystems: scene setup, camera, interaction, onboarding, overlay/UI hints, placement, timeline recording, handoff, and worker lifecycle.
- State is mediated through lab/js/store/ and selectors, while rendering is handled by lab/js/renderer.ts and Three.js-oriented code.
- This separation makes the Lab app a composition of small runtime controllers rather than a single monolith.
5. Watch playback architecture
- watch/js/document/ loads capsule/history files into a document model.
- watch/js/playback/ reconstructs or interpolates trajectories, including topology sources that can come from stored data or reconstructed bonds.
- watch/js/view/ owns camera input, cinematic camera behavior, renderer integration, and layout for playback-specific UI.
- The key boundary here is that Watch never mutates the source capsule; it derives a playback model and then hands off a selected frame/state to Lab.
6. Capsules, histories, and shared data model
- Shared types and topology/history logic live in src/, especially src/history/, src/topology/, src/share/, src/types/, and src/ui/.
- Capsule files are compact share artifacts: they preserve the simulation state needed for replay/remix, including atoms, velocities, bonds, and authored appearance metadata.
- Full-history files are a richer format used by Watch and validation paths when complete simulation context is available.
- The shared library is the main boundary preventing Lab, Watch, Functions, and tests from drifting on schema, code formats, and share-code rules.
7. Share-link backend and trust boundaries
- functions/ is the Cloudflare Pages Functions backend for publishing, session/auth, account APIs, capsule delivery, and privacy requests.
- Storage is split between D1 for relational/account/session/audit metadata and R2 for capsule bodies and related binary objects.
- The backend enforces auth middleware, age confirmation, CSRF-style intent/nonce flows, rate limiting, and admin gates.
- Share codes are short, human-friendly identifiers; backend endpoints map these to stored capsule records and manage publish/delete/report flows.
8. Account, policy, and privacy flows
- Account APIs in functions/api/account/ support “me”, capsule listing, deletion, age confirmation, and account cleanup.
- Privacy requests have a dedicated entrypoint and server-side nonce flow; the system is designed for data-access and deletion requests.
- Policy acceptance and age gating are first-class server-enforced concerns, not just UI prompts, which matters for the route and data-flow diagram.
9. Operational retention and cleanup
- workers/cron-sweeper/ is a separate scheduled Worker that runs cleanup/audit passes against the shared backend state.
- It handles session expiration, orphaned object sweeps, and audit-retention maintenance.
- This is an operational subsystem, not part of the interactive app path, but it closes the loop on storage hygiene and privacy retention.
10. Reference science and validation layer
- sim/ is a Python reference implementation of the carbon simulation stack: atoms, velocity Verlet integration, Tersoff potential, minimization, and structure generation.
- tests/test_*.py validate physics behavior against this reference engine, ensuring the browser implementation matches the intended model.
- sim/wasm/ contains C source and build assets for low-level Tersoff experimentation/reference, separate from the browser Wasm bundle used by the app.
11. Research and future-facing ML work
- ml/ contains force-decomposition and surrogate-model experimentation; it is explicitly research/deferred rather than part of the product runtime.
- It sits beside the main codebase as a parallel scientific pipeline, useful to know for architecture diagrams only as a non-production subsystem.
12. Canonical assets and structure library
- structures/ contains the pre-relaxed canonical geometries used by Lab and tests: buckyballs, nanotubes, graphene, diamond, and sample fixtures.
- A manifest ties these assets together as the source of initial scenes and benchmark cases.
- These assets are part of the product model because they seed both simulation entry and watch replay examples.
13. Testing and quality gates
- The repository has three major test surfaces: Vitest unit tests, Playwright end-to-end tests, and Python scientific tests.
- Unit tests cover shared logic, app runtimes, APIs, and worker behavior; E2E tests exercise browser flows like lab entry, watch playback, and Pages-dev integration; Python tests validate physics correctness.
- This reflects the repo’s architecture: browser simulation, serverless sharing, and scientific correctness each have their own verification path.
14. Build/deploy boundary
- The front end is built with Vite, while Cloudflare Pages/Functions and Workers provide deployment/runtime hosting.
- The app runs locally in a frontend-only mode or in a full-stack Pages-dev mode with D1 and R2 bindings.
- For diagramming, it helps to show three deployment planes: browser apps, Cloudflare serverless backend, and offline/reference science tooling.
Main entry points: functions Pages Functions · history History & topology · lab/index.html Lab · watch/index.html Watch
Components 22
Browser apps
Client-side entrypoints for simulation, playback, and static policy/account pages.
- Lab · Main interactive playground for editing, simulating, recording, and exporting capsules.
lab/index.html - Watch · Read-only capsule/history playback app with interpolation and Lab handoff.
watch/index.html - Viewer · Minimal trajectory viewer for simple file-based inspection.
viewer/index.html - Account · Account entrypoint for user profile and related flows.
account/index.html - Privacy · Public privacy policy entrypoint.
privacy/index.html - Terms · Public terms entrypoint.
terms/index.html - Privacy request · Dedicated privacy-request entrypoint with server-side nonce flow.
privacy-request/index.html - Lab runtime · Scene, camera, interaction, onboarding, overlay, placement, and worker orchestration.
lab/js/runtime - Timeline · History capture, export, restart-state adaptation, and transfer workflows.
lab/js/runtime/timeline - Worker bridge · Web Worker lifecycle, reconciled steps, snapshot recovery, and stall handling.
lab/js/runtime/worker - Watch docs · Loads capsule and full-history documents into a playback model.
watch/js/document - Playback · Frame search, interpolation, and topology source selection for replay.
watch/js/playback - Watch view · Camera input, cinematic view, renderer integration, and watch-specific layout.
watch/js/view
Shared core
Common schemas, topology, history, policy, and UI primitives used across runtimes.
- Shared types · Common domain and worker protocol types shared across apps and backend.
src/types - History & topology · Capsule/history schema, bond policy, connected components, and topology resolution.
src/history - Share core · Share-code generation, publish/delete primitives, rate limits, and record/audit helpers.
src/share - Handoff utils · Normalizes watch seeds and builds watch-to-lab transfer URLs.
src/watch-lab-handoff
Cloud backend
Cloudflare Functions and scheduled Worker for sharing, auth, privacy, and retention.
- Pages Functions · Cloudflare backend for auth, publishing, account APIs, capsule delivery, and privacy requests.
functions - Cron sweeper · Retention worker for session cleanup, orphan sweeps, and audit maintenance.
workers/cron-sweeper/src/index.ts
Science tooling
Reference simulation and experimental research tooling outside the browser product runtime.
- Python sim · Reference physics implementation for atoms, integration, potentials, minimization, and I/O.
sim - ML research · Surrogate-model and force-decomposition experiments kept separate from product runtime.
ml - Structures · Canonical relaxed geometries and manifest used by Lab, Watch, and tests.
structures
Connections 25
- Lab → Lab runtime: uses · Lab composes runtime controllers for scene, camera, interaction, and overlays.
- Lab runtime → Timeline: records · Runtime feeds timeline capture, export, and transfer orchestration.
- Lab runtime → Worker bridge: simulates · Runtime delegates physics execution and recovery to the worker bridge.
- Lab → Shared types: shares schema · Lab depends on shared types and worker protocol definitions.
- Lab → History & topology: reads/writes · Lab creates and exports capsule/history data through the shared domain model.
- Lab → Structures: seeds · Initial scenes and benchmarks come from the canonical structure library.
- Watch → Watch docs: loads · Watch turns capsule/history files into a document model.
- Watch → Playback: derives · Watch reconstructs and interpolates trajectories for playback.
- Watch → Watch view: renders · Watch view owns camera, renderer, and playback UI layout.
- Watch → Handoff utils: hands off · Watch transfers selected state into Lab via shared handoff utilities.
- Watch docs → History & topology: parses · Document loading relies on shared history and topology formats.
- Playback → History & topology: interprets · Playback uses shared topology/history rules to reconstruct frames.
- Watch view → Shared types: shares schema · Watch view uses the same domain and protocol shapes as Lab.
- Pages Functions → Share core: publishes · Backend exposes publish, delete, report, and lookup flows using shared share logic.
- Pages Functions → Shared types: validates · Server endpoints and auth flows rely on shared schema types.
- Pages Functions → History & topology: stores capsules · Capsule records and previews are tied to the shared history/topology model.
- Pages Functions → Cron sweeper: maintains · Backend state is periodically cleaned and audited by the scheduled worker.
- Cron sweeper → Share core: sweeps · Retention tasks reuse shared delete/audit primitives.
- Python sim → Structures: benchmarks · Python reference code loads canonical structures for validation and generation.
- Python sim → History & topology: validates model · Scientific tests compare browser behavior against the reference physics model.
- ML research → Python sim: experiments on · Research scripts build on the reference simulation stack.
- Privacy request → Pages Functions: submits · Privacy request UI depends on server-side nonce and request handling.
- Account → Pages Functions: calls · Account entrypoint is backed by account/session APIs.
- Privacy → Pages Functions: policy flow · Policy acceptance and privacy-related flows are enforced server-side.
- Terms → Pages Functions: policy flow · Terms entrypoint participates in the overall policy boundary.