Skip to content

Latest commit

 

History

History
529 lines (472 loc) · 34.6 KB

File metadata and controls

529 lines (472 loc) · 34.6 KB

Code Map

This is a runtime-focused map of the app. It is meant to answer two questions quickly:

  1. Where does the app start in server mode and standalone mode?
  2. Which files and functions should you follow for a given kind of change?

For the higher-level architecture, see ARCHITECTURE.md. For cleanup priorities and known structural pressure points, see CODE_QUALITY_ROADMAP.md. For maintenance workflows and repo-owned guardrails, see MAINTENANCE.md.

Start Here

Server runtime

  1. app.py Entry script for local Flask development. Calls create_app() and starts the dev server.
  2. backend/api.py Flask app factory. create_app(...) wires config, frontend assets, topology metadata, simulation extensions, and routes.
  3. backend/simulation/bootstrap.py register_simulation(app) creates and stores the rule registry and session registry.
  4. backend/web/routes.py HTTP entrypoints for state fetches and mutations. Server-mode simulation calls use /api/sessions/<session_id>/... and resolve the session coordinator lazily.
  5. backend/web/state_actions.py Bridges validated request payloads to simulation mutations.
  6. backend/simulation/sessions.py Owns session-id validation and per-session coordinator lifecycle/persistence paths.
  7. backend/simulation/coordinator.py Coordinates runtime, persistence, restore, and simulation service.
  8. backend/simulation/service.py Main synchronous simulation API used by the host layers.
  9. backend/simulation/engine.py Pure stepping engine that asks the current rule for each cell's next state.

Standalone browser runtime

  1. tools/standalone_build.py Python-first standalone packager that stages the transient build input, runs the standalone Vite build, and finalizes the published static output.
  2. frontend/shell/app-shell-body.html Shared shell source used by both Flask and the standalone wrapper.
  3. tools/standalone_shell.py Shared standalone wrapper renderer used by python -m tools build standalone-shell and the standalone build flow.
  4. frontend/standalone.ts startStandaloneApp() loads bootstrap JSON, creates the standalone worker environment, and then calls initApp(...).
  5. frontend/standalone/worker-client.ts createStandaloneEnvironment(...) creates the Web Worker, talks to Pyodide, and exposes a SimulationBackend.
  6. frontend/standalone-worker.ts Worker entrypoint that loads Python and forwards commands into the browser runtime host.
  7. backend/browser_runtime.py Python runtime host for standalone mode. Exposes initialize_runtime(...), handle_request(...), and tick_running().

Browser UI startup

  1. frontend/server-entry.ts Canonical server host entrypoint.
  2. frontend/app-runtime.ts initApp(...) mounts the wall⇄Lab workspace router into the shared shell and marks the app ready once the landing route settles; the editor controller boots lazily on first entry to #/lab.
  3. frontend/app-controller.ts createAppController(...) composes state, view, actions, sync, and interactions.
  4. frontend/app-controller-startup.ts initializeAppController(...) orchestrates service construction, interaction wiring, and hydration.
  5. frontend/app-controller-services.ts Builds the session/config-sync/simulation-mutation service layer.
  6. frontend/app-controller-wiring.ts Connects interactions, viewport sync, and the action surface.
  7. frontend/app-controller-hydration.ts Preserves the async startup order for rules, controls, refresh, and listeners.
  8. frontend/app-actions.ts Creates the action surface used by controls and interaction handlers.
  9. frontend/app-view.ts Renders the control shell and canvas-facing UI.

Request Flow

Server-backed flow

Browser UI
  -> frontend/api.ts
  -> backend/web/routes.py (/api/sessions/<session_id>/...)
  -> backend/simulation/sessions.py
  -> backend/web/state_actions.py
  -> backend/simulation/coordinator.py
  -> backend/simulation/service.py
  -> backend/simulation/engine.py + backend/rules/*
  -> snapshot response
  -> frontend/state/snapshot-reducer.ts
  -> canvas + controls rerender

Standalone flow

Browser UI
  -> frontend/standalone/worker-client.ts
  -> frontend/standalone-worker.ts
  -> backend/browser_runtime.py
  -> backend/simulation/service.py
  -> backend/simulation/engine.py + backend/rules/*
  -> worker response
  -> frontend/state/snapshot-reducer.ts
  -> canvas + controls rerender

Frontend Map

App composition

App state

Actions and control flow

Controls and view

Canvas, rendering, and geometry

Interaction and editor stack

Patterns, presets, and topology metadata

Compare mode

A self-contained overlay that runs one seed under one rule across many tilings and renders the result. It is mounted once from frontend/app-runtime.ts and talks to the backend through SimulationBackend.compareSeed(...) (HTTP POST /api/compare on the server, the /api/compare worker command in standalone). The UI asks for optional state payloads so each result row can open or copy share links for the seed state and final state on that tiling, and can expand an inline begin/end thumbnail.

  • frontend/compare/compare-panel.ts mountComparePanel(...): the launcher button, modal form (rule/seed/traversal/steps/tilings), run orchestration, results rendering, compare-row share/open actions, and the per-row thumbnail expander (fetches geometry via SimulationBackend.previewTopology(...), cached per tiling).
  • frontend/compare/compare-charts.ts Pure layout helpers plus the SVG phase portrait (live(t)/live(0) per tiling) and the end-state classification grid, including the optional per-row action cell hook.
  • frontend/compare/compare-thumbnail.ts Renders a tiling's board as an SVG (one polygon per cell, coloured by state) from a topology_preview payload, with pure bounds/fit helpers.
  • frontend/compare/compare-seed-pad.ts createSeedPad(...): a paint grid for designing the seed by drawing, two-way bound to the seed bit string (the exact inverse of the traversal: read row-major to bits, fill row-major from bits). Pure bit/grid helpers are exported for tests.
  • frontend/compare/compare-seed-preview.ts createSeedPreview(...): a live strip showing where the drawn seed lands on the first few selected tilings. Fetches each tiling's geometry and canonical cell order once (via previewTopology with a traversal), then places the seed client-side (placeSeedOnOrder, the same mapping the comparison uses) and re-renders begin-state thumbnails as you draw.
  • frontend/compare/compare-panel.css and frontend/compare/compare-options.ts Injected stylesheet (imported ?inline so it ships minified inside the compare-panel chunk) and the traversal option list.

Backend Map

Flask host and request layer

  • backend/api.py create_app(...)
  • backend/app_shell.py Shared shell rendering for the Flask wrapper and standalone shell generation.
  • backend/web/routes.py Thin Flask routes and JSON response helpers. Main endpoints: get_state(), get_rules(), get_bootstrap(), start(), pause(), resume(), step(), reset(), update_config(), toggle_cell(), set_cell(), set_cells(), compare() (POST /api/compare, runs a seed sweep without mutating simulation state), topology_preview() (POST /api/topology/preview, builds one tiling with per-cell geometry for thumbnails)
  • backend/web/state_actions.py StateActionService
  • backend/web/requests.py get_payload(request)
  • backend/contract_validation.py Shared payload validation for both Flask and standalone browser runtime.

Simulation façade and runtime

Models, topology, persistence

Topology catalog and generated tilings

Rules

Seed comparison

backend/simulation/seeding maps one seed onto many topologies so the same rule can be compared across tilings. It is headless and reuses the cached TopologyFrame, so it works without Flask.

  • backend/simulation/seeding/traversal.py Canonical cell-id orderings (bfs_ring_order, row_major_order) plus paint_bits. The seed bit-string is walked in traversal order, preserving live-cell count across tilings.
  • backend/simulation/seeding/metrics.py Rule-agnostic measurement: population, hamming change rate, and end-state classify (extinct / still-life / oscillator / unsettled).
  • backend/simulation/seeding/comparison.py compare_seed(...) sweeps a seed across geometries, detects cycles, isolates per-tiling errors, flags degenerate seeds, and can optionally return sparse begin/end board states for callers that need reconstructable links. Accepts a pattern for shape-mode seeding.
  • backend/simulation/seeding/shapes.py Policy A: a named-pattern library (glider, R-pentomino, …) and place_pattern(...), which centres a recognisable shape on a topology and lights the nearest cell to each point — preserving the 2-D shape across tilings instead of the live-cell count.

Tests And Tooling

Tests

  • tests/api API contract and route coverage.
  • tests/unit Backend service, topology, persistence, validation, and frontend-support unit tests.
  • tests/e2e Playwright-backed browser coverage for both server and standalone runtimes.

Tooling

  • tools/cli.py Unified python -m tools ... entrypoint. Registers the public command groups and dispatches into task-focused command modules.
  • tools/command_docs.py Single source of truth for command metadata, examples, and generated tools documentation.
  • tools/commands/build.py Public build group wiring for standalone, standalone-shell, and bundle-size.
  • tools/standalone_build.py Builds the static standalone site and writes the provenance manifest.
  • tools/standalone_shell.py Shared standalone-shell renderer behind python -m tools build standalone-shell.
  • tools/playwright_runner.py Public Playwright runner behind python -m tools test e2e. Lists suites, prepares standalone builds, repairs Linux browser libraries when possible, and dispatches Python unittest modules.
  • tools/commands/browser.py Public browser-diagnosis command wiring for review, managed checks, sweeps, diffs, workbenches, and standalone smoke tests.
  • tools/render_review/review.py Focused browser-backed render-review implementation. Renders one topology through the real canvas path, supports named profiles, profile-owned literature metadata, optional reference montages, and writes PNG plus JSON summary output.
  • tools/render_review/browser_check.py Managed browser-diagnosis runner. Owns standalone/server host startup, readiness, logging, cleanup, and run-manifest output for focused render reviews or targeted Python unittest browser checks.
  • tools/render_review/sweep.py Small-matrix render-review orchestrator. Expands one named profile across selected hosts, themes, and sizes, reuses the managed runner for each case, and writes one top-level sweep manifest plus one comparable artifact directory per case.
  • tools/render_review/diff_review.py One-command render-review comparison sheet builder. It can run a new sweep or consume an existing sweep-manifest.json, then emits one HTML sheet plus one PNG contact sheet for side-by-side review.
  • tools/dev_processes.py Repo-scoped process inspection and cleanup helper for known browser/server helper processes, including the quick stale-host cleanup command and Windows listening-port discovery.
  • tools/commands/tilings.py Public tilings group wiring for validation, verification, reporting, previews, sketching, aperiodic scaffolding, and seed comparison.
  • tools/compare_seed.py Backs python -m tools tilings compare: runs a seed under one rule across many tilings and reports population, end-state classification, and the degenerate-seed guard as a table or JSON.
  • tools/validate_tilings.py Manifest-wide geometric sanity checker for catalog tilings.
  • tools/verify_reference_tilings.py Literature-faithfulness verifier for the full topology catalog.
  • tools/export_bootstrap_data.py Exports bootstrap metadata for standalone mode.
  • tools/tools_docs.py Generates and checks docs/TOOLS.md from the command registry.
  • tools/render_review/browser_support/artifacts.py Shared browser-failure artifact writer used by the managed runner, render-review tool, and Playwright harness so local diagnosis and failing tests emit the same core bundle shape.
  • vite.config.ts Frontend build configuration.

Current Refactor Targets

Use this list to decide where cleanup work should start. These are the files with the most structural pressure today, not necessarily the files with the most defects.

If You Want To Change...