4 Solution Strategy

Core Architectural Decisions

Technology Choices

GoalTechnology ChoiceRationale
Single application runtime (HTTP + stateful chat logic)Elixir/OTP 1.19 (Erlang/OTP 27)OTP Supervisor trees, Horde CRDT clustering, ETS in-memory state; one BEAM node serves everything
HTTP surface (Matrix C-S API + media)Bandit + PlugPure-Elixir HTTP server; in-process dispatch to domain functions
Admin UIPhoenix HTTP controllers + Phoenix LiveView (Dashboard, AuditLog, Bootstrap)Server-rendered admin pages on the same Bandit listener; CRUD pages use PRG HTTP controllers, real-time pages remain LiveViews
Reliable event log + schema authorityPostgreSQL 16Append-only event log, raw-SQL migrations via Nebu.Release.migrate/0 (ADR-018), RLS policies
No RedisETS (Elixir built-in)Session state, since-token cursors - eliminates a stateful dependency
No NATS/Kafkapg Process Groups (Elixir built-in)Pub/sub fanout - eliminates an external broker
AuthOIDC-only (Authorization Code + PKCE), Assent/JOSENo local accounts, no password hashing; IdP owns identity lifecycle

Key Architectural Patterns

1. Single Elixir/OTP application, in-process domains (ADR-019/020/021)

Nebu is one Mix app (:nebu, Nebu.Application, one supervisor tree). The HTTP surface and all stateful chat logic live in the same BEAM node, split into module namespaces: Nebu.Matrix.* (Matrix C-S API + rooms via Horde, sessions via ETS + PostgreSQL, presence, permissions, Ed25519 signing, event dispatch, sync, and the Nebu.Matrix.Http* surface), Nebu.Media.* (upload/ download/crypto/storage against S3/MinIO), Nebu.Admin.* (Phoenix HTTP controller + LiveView Admin UI + admin OIDC login), Nebu.Web.* (shared HTTP plumbing: the single Bandit listener's router + plugs, JWKS cache, token denylist), and Nebu.Compliance.* (audit log). The HTTP layer calls domain functions directly — there is no inter-process hop. PostgreSQL is append-only for events and audit logs.

2. In-process command/DTO layer (amended ADR-005)

ADR-005 originally chose a gRPC server-streaming EventBus + unary fallback for a gateway↔core network boundary. With the Go gateway removed, that boundary no longer exists: the HTTP routers call the domain functions in-process. A gRPC server module (Nebu.Matrix.EventDispatcher.Server) plus the generated Core.* protobuf structs survive as the in-process command/DTO layer the routers invoke directly. A gRPC network listener on port 9000 still starts but nothing dials it (vestige, slated for a future cleanup).

3. Content-Hash Event IDs (Matrix Room Version 6+)

Event IDs are computed as $<base64url(SHA-256(canonical_json(event)))> - tamper-evident, federation-compatible, reproducible at recovery. No UUID generation.

4. Raw-SQL migrations applied by the release (ADR-018)

There is no ORM and no golang-migrate. Migrations are raw SQL applied by Nebu.Release.migrate/0 before the application boots; the migration SQL is the only canonical data model.

5. Server-side session and media key escrow

Matrix sessions are opaque tokens stored server-side (decoupled from OIDC token lifetime); media objects are AES-256-GCM encrypted at rest with per-object keys stored in PostgreSQL (managed E2EE key escrow, ADR-011).

5a. To-device queue (story to-device-messaging)

Matrix "send-to-device" messages (device verification, key requests) are queued as opaque rows in PostgreSQL per recipient device, delivered by /sync and deleted only when the client's next sync presents the since token that covers them (a retry delivers again, nothing is lost); a new message wakes the long poll of its target device through the pg Process Groups. No Redis, no broker; ADR-011 stays.

5b. Device-key directory and device-list stream (story device-key-directory)

POST /keys/claim hands one-time keys out exactly once with a single set based UPDATE ... FOR UPDATE SKIP LOCKED statement and returns the fallback key without consuming it; GET /keys/changes and the device_lists of /sync read a PostgreSQL change stream (device_list_changes) whose positions are drawn at commit from the existing sync_global_pos_seq and travel as the last group of the sync token. The restriction (own devices and users who share a joined room) is evaluated inside the statement, so unknown and unreachable targets are indistinguishable (no oracle). No Redis, no broker; the rate buckets are node-local; ADR-011 stays.

6. BEAM-native clustering

Horizontal scaling uses libcluster + Horde + pg Process Groups — no Redis, no NATS, no Kafka. Room GenServers migrate across nodes via the Horde CRDT registry; node registration uses a pre-shared secret (Nebu.NodeRegistration).

7. Locality of Behavior — No Domain Model, No ORM (ADR-018)

SQL is written inline in each endpoint/controller implementation file. No Ecto.Schema, no Repository pattern, no Service layer (Nebu.Repo is the Ecto connection pool only). The raw-SQL migrations are the sole canonical data model. This keeps every endpoint fully self-contained: handler, auth check, SQL, and response shape are all visible in one file — optimized for LLM-assisted development (BMAD methodology) where minimizing context-graph depth reduces hallucination risk and speeds review. SQL duplication across files is a deliberate property of this strategy, not technical debt.

Source: _bmad-output/planning-artifacts/architecture.md, §Core Architectural Decisions, §Implementation Patterns & Consistency Rules; ADR-018, ADR-019, ADR-020, ADR-021