This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
BldgServer is a reference implementation of the Alice-in protocol server, built with Elixir/Phoenix 1.7. It serves Alice-in visualizations to clients and manages hierarchical data entities ("buildings"), relationships ("roads"), users ("residents"), and external integrations ("batteries").
All commands run from the bldg_server/ directory:
mix deps.get # Install dependencies
mix ecto.setup # Create DB, run migrations, seed
mix ecto.migrate # Run pending migrations
mix ecto.reset # Drop DB, recreate, migrate, seed
mix phx.server # Start dev server (HTTP :4000, HTTPS :4443)
mix test # Run all tests
mix test test/path_test.exs # Run a single test file
mix test test/path_test.exs:42 # Run a specific test by lineThe mix test alias auto-runs ecto.create and ecto.migrate before tests (test DB: bldg_server_test).
Docker for local dev (starts PostgreSQL):
docker-compose upThe app requires REDIS_HOST, REDIS_PWD, and REDIS_PORT env vars at startup (hard-crashes without them). DB config reads DB_USER, DB_PASSWORD, DB_HOST with defaults.
Production release runs migrations via /app/bin/migrate.
- Bldg (
lib/bldg_server/buildings.ex, schema inbuildings/bldg.ex) — Core entity. Represents anything: buildings, members, teams, stages, milestones. Has a hierarchical address system rooted at "g" (ground), e.g.g/b(3,2)/l0/b(1,1). - Resident (
lib/bldg_server/residents.ex) — User accounts with email-based passwordless auth (magic link verification). - Battery (
lib/bldg_server/batteries.ex) — External data source integrations that attach to buildings and sync data. - Road (
lib/bldg_server/relations.ex) — Relationships/connections between buildings.
- BldgCommandExecutor (
lib/bldg_server_web/bldg_command_executor.ex) — Processes chat commands ("say" actions) in buildings, broadcasts via Phoenix PubSub on the "chat" topic. - BatteryChatDispatcher (
lib/bldg_server_web/battery_chat_dispatcher.ex) — Forwards chat messages to attached batteries' webhook endpoints. - DgraphClient (
lib/bldg_server/dgraph_client.ex) — Client for the DGraph graph database, used for staging/transient data. - StagingController — Manages temporary/staged data in the graph DB with namespace/entity_type organization.
- Notifications (
lib/bldg_server/notifications.ex) — Propagates creation/update events up the container hierarchy. - FloorChannel (
lib/bldg_server_web/channels/floor_channel.ex) — Phoenix Channel onfloor:*topics that streams real-timebldg_created/updated/deleted,resident_*, androad_*events to clients (replaces HTTP polling). Mutations inBuildings/Residents/Relationsbroadcast to the target floor and every ancestor floor, so clients doing recursivescanreceive nested changes.
The hierarchical address is central to the data model:
- Root floor:
"g"(ground) - Building addresses:
g/b(x,y)where x,y are coordinates - Nested floors:
g/b(x,y)/l0/b(x,y)— buildings contain floors which contain more buildings - Delimiter:
"/" - Each bldg has:
address,flr(parent floor),flr_url,entity_type,x,y
- PostgreSQL — Primary database (Ecto/postgrex)
- Redis (Upstash in prod) — Caching and pub/sub via Redix
- DGraph — Graph database for staging/transient data queries
- Finch (
FinchClient) — HTTP client for outbound requests (battery webhooks, DGraph API)
All API routes are under /v1/ (see lib/bldg_server_web/router.ex):
/bldgs/*— Building CRUD, lookup by address/url,look/scanfor listing,relocate_to,favorite_view_points/residents/*— Auth (login/verify), resident actions,look/scan/roads/*— Relationship management,look/scan/batteries/*— Register/unregister, attach/detach, act/staging/*— Read/write/query staged data by namespace
look/:flr returns direct children of a floor; scan/:flr returns all nested descendants.
Bldgs carry a favorite_view_points array of named camera poses (address, direction, size_delta, camera_vertical_angle); appended via POST /v1/bldgs/:address/favorite_view_points.
Deployed to Fly.io via split dev/prod stacks (fly.dev.toml, fly.prod.toml at the repo root), primary region SJC. Multi-stage Dockerfile using Elixir 1.16.2 / OTP 26.2.2. Key env vars configured via Fly secrets: DB_*, REDIS_*, DGRAPH_URL, SENDGRID_API_KEY, SECRET_KEY_BASE. Sentry is wired in for error reporting (commit 0341b20).
- Bldgs and Roads have an
ownersfield (array of email strings). Buildings.is_authorized_owner?/2checks direct ownership first, then walks up the container hierarchy (viabldg_url) to check ancestor ownership — an owner of a parent building can operate on all nested children.- Floor segments are skipped when walking up, since floors don't have their own bldg entries.
- Used by
BldgCommandExecutorfor/add owner,/remove owner,/connect,/edit, and/delete bldgcommands and by controllers for mutation authorization.
Passwordless email-based auth:
POST /residents/login→ creates a Session (status:PENDING-VERIFICATION) and emails a magic link via SendGridGET /residents/verify?token=X→ verifies the token (24h max age viaPhoenix.Token), marks sessionVERIFIED- Sessions track
ip_addressandlast_activity_time; previous sessions are markedREPLACED
The visual_language field (map) on container bldgs decouples entity_type from 3D rendering. Buildings.default_visual_language/0 defines 50+ entity-type-to-3D-object mappings (e.g., "team" → "buildingWithStorefront"). Containers set this on their floors; child bldgs inherit the mapping to determine their visual representation.
Bldgs also carry purely-presentational styling fields decoupled from semantic state: color (free-form token, validated against a palette in the client), size (T-shirt: XS/S/M/L/XL/XXL), and variant. These were historically overloaded onto state/category; migration 20260505120100 backfills the split. Roads similarly accept optional color and class fields for styled overlays.
The staging API uses DGraph with DQL queries. The namespace concept is stored as ns in the DGraph schema (renamed to avoid conflict with DGraph's internal namespace keyword). Staging data is organized by ns and entity_type.
- JSON API only (no HTML views) — all controllers render JSON via view modules in
views/. - Auto-placement logic in
Buildings.create_bldg/1handles coordinate collision by shifting x+1 and retrying. - Composite entity types (e.g., "team", "project") have predefined floor heights in
BldgController. - Building creation triggers hierarchical notifications up the container chain.
- GenServers (
BldgCommandExecutor,BatteryChatDispatcher) subscribe to Phoenix PubSub "chat" topic — commands are broadcast, not called directly. Buildings.delete_bldg_cascade/1deletes nested descendants deepest-first plus their roads before the target; residents inside deleted bldgs are left untouched.DELETE /v1/bldgs/:addressand the/delete bldg {name}chat command both route through it (authorized viais_authorized_owner?/2).- Roads cache
from_address/to_addressand endpoint coordinates at creation.Buildings.update_bldg/2diffs theaddresson update and, on change, callsRelations.cascade_bldg_relocation/3to rewrite matching endpoints (following the flr when the road lived on the bldg's prior floor) and re-broadcastroad_updated. Skipping this leaves roads visually dangling at the old position.