# okeson453/crash architecture

> Architecture diagram of the GitHub repository okeson453/crash, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/okeson453/crash
- Repository: https://github.com/okeson453/crash

## Overview

#### Purpose and entry points
This is a TypeScript/Node.js platform that automates observation and controlled participation in BC.Game Crash rounds, with operational control, accounting, analytics, and multi-tenant platform features. Runtime composition begins at `src/index.ts`, with dependency wiring in `src/app/composition.ts` and upgrade-specific wiring in `src/app/spec-upgrade-wiring.ts`. Configuration is loaded and validated before services start via `src/config/loader.ts` and `src/config/schema.ts`.

#### Control plane and lifecycle orchestration
The core runtime is an event-driven orchestrator governed by a deterministic state machine. `src/core/orchestrator.ts` coordinates lifecycle actions; `src/core/state-machine/machine.ts` and `src/core/state-machine/guards.ts` enforce permitted transitions and fail-safe conditions. `src/core/session-supervisor.ts`, recovery management, emergency stop, and instance locking isolate active sessions and prevent unsafe concurrent operation.

#### Eventing, jobs, and durability boundary
Domain components communicate through the event bus rather than direct control coupling: `src/core/event-bus/bus.ts`, `src/core/event-bus/events.ts`, and `src/core/event-bus/outbox-publisher.ts`. The persistent outbox/log boundary is backed by PostgreSQL, while Redis supports stream-based event transport and work dispatch in `src/core/event-bus/streams/redis-streams-bus.ts` and `src/core/job-queue/index.ts`. This supports recovery and deferred worker execution.

#### Game/browser acquisition boundary
Game state is acquired through adapters and browser automation. `src/game/adapter.ts` and `src/game/adapters/aggregator.ts` combine API, DOM, and WebSocket observation paths; `src/game/observer.ts` turns these into round-level signals. Browser session ownership, login, health, profile consistency, and anti-detection behavior sit behind `src/browser/manager.ts`, `src/browser/session.ts`, and `src/browser/evasion/index.ts`. This boundary depends on a Playwright/Chromium browser runtime, proxies, and protocol interception.

#### Decision, prediction, and risk gates
Observed rounds feed opportunity ranking, prediction, and policy evaluation before any execution. `src/opportunity/ranker.ts`, `src/prediction/prediction-engine.ts`, and `src/prediction/acie/engine.ts` provide signal and model paths. The decision boundary is `src/decision/decision-engine.ts`; financial/risk constraints are enforced by `src/betting/risk-engine.ts`, `src/risk/velocity-controller.ts`, and `src/risk-engine/drawdown.ts`. The system is designed to pause or observe when confidence, limits, or state integrity are insufficient.

#### Bet execution, settlement, and ledger integrity
Approved actions pass through `src/betting/betting-coordinator.ts`, `src/betting/executor.ts`, and `src/betting/cashout.ts`; idempotency and confirmation guard side effects. Post-round truth is resolved by `src/settlement/authoritative-settlement-engine.ts`, drift controls, and reconciliation. Financial records are maintained through `src/ledger/balance-tracker.ts`, `src/ledger/daily-entries.ts`, and `src/ledger/reconciliation.ts`, with database migrations including daily caps, audit logs, financial integrity, and double-entry settlement in `migrations/011_terms_stake_daily.sql`, `migrations/013_financial_integrity.sql`, and `migrations/017_double_entry_settlement.sql`.

#### Operator and tenant platform interfaces
Telegram is the primary operational interface: `src/telegram/gateway.ts`, `src/telegram/router.ts`, and `src/telegram/commands/control.ts` authenticate and route operator commands and notifications. A separate Next.js admin dashboard resides in `admin-dashboard/app/page.tsx`, using a proxy route at `admin-dashboard/app/api/proxy/route.ts`. Tenant provisioning/control, billing, subscriptions, and payments are centralized in `src/platform/control-plane.ts`, `src/platform/tenant-manager.ts`, and `src/platform/billing/subscription-service.ts`; external payment integrations include Stripe and Paystack.

#### Persistence, operations, and observability
PostgreSQL 15+ with TimescaleDB is the durable system of record; Redis is the cache/stream infrastructure. Access is encapsulated in `src/persistence/client.ts`, `src/persistence/transaction.ts`, and repositories. Docker Compose defines local, production, and tenant deployments in `docker/docker-compose.yml`, `docker/docker-compose.prod.yml`, and `docker/docker-compose.tenant.yml`. Metrics, logs, tracing, alerts, and health checks are implemented under `src/observability/`, with Prometheus and Grafana provisioning under `docker/prometheus/prometheus.yml` and `docker/grafana/`.

Main entry points: [`src/core/orchestrator.ts`](https://github.com/okeson453/crash/blob/main/src/core/orchestrator.ts) (Lifecycle orchestrator), [`src/core/event-bus/bus.ts`](https://github.com/okeson453/crash/blob/main/src/core/event-bus/bus.ts) (Domain event bus), [`src/game/observer.ts`](https://github.com/okeson453/crash/blob/main/src/game/observer.ts) (Round observer), [`src/decision/decision-engine.ts`](https://github.com/okeson453/crash/blob/main/src/decision/decision-engine.ts) (Decision engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Core Runtime"]
  node_entry["Runtime entry<br/>Node entrypoint<br/>[index.ts]"]
  node_composition["Composition root<br/>dependency wiring<br/>[composition.ts]"]
  node_config["Validated configuration<br/>configuration loader<br/>[loader.ts]"]
  node_orchestrator["Lifecycle orchestrator<br/>control plane<br/>[orchestrator.ts]"]
  node_state_machine["Safety state machine<br/>deterministic state machine<br/>[machine.ts]"]
  node_event_bus["Domain event bus<br/>eventing boundary<br/>[bus.ts]"]
  node_jobs["Deferred jobs<br/>job queue<br/>[index.ts]"]
end

subgraph group_game["Game Acquisition"]
  node_game_observer["Round observer<br/>game signal acquisition<br/>[observer.ts]"]
  node_game_adapters["Game adapters<br/>acquisition adapters<br/>[aggregator.ts]"]
  node_browser["Browser session boundary<br/>Playwright automation<br/>[manager.ts]"]
end

subgraph group_decision["Decision &amp; Execution"]
  node_decision_engine["Decision engine<br/>policy evaluation<br/>[decision-engine.ts]"]
  node_prediction["Prediction engine<br/>signal model"]
  node_risk["Risk gates<br/>financial controls<br/>[risk-engine.ts]"]
  node_betting["Betting coordinator<br/>side-effect coordinator"]
  node_settlement["Authoritative settlement<br/>post-round reconciliation"]
  node_ledger[("Financial ledger<br/>double-entry accounting<br/>[balance-tracker.ts]")]
end

subgraph group_platform["Operations &amp; Platform"]
  node_telegram["Telegram operations<br/>operator gateway<br/>[gateway.ts]"]
  node_tenant_control["Tenant control plane<br/>multi-tenant platform<br/>[control-plane.ts]"]
  node_admin_dashboard["Admin dashboard<br/>Next.js interface<br/>[page.tsx]"]
end

subgraph group_infra["Durability &amp; Infrastructure"]
  node_postgres[("PostgreSQL + TimescaleDB<br/>system of record<br/>[client.ts]")]
  node_redis["Redis streams<br/>event transport"]
  node_observability["Observability<br/>metrics, logs and alerts"]
end

node_entry -->|"loads"| node_config
node_entry -->|"starts"| node_composition
node_composition -->|"wires"| node_orchestrator
node_orchestrator -->|"governed by"| node_state_machine
node_orchestrator -->|"publishes and consumes"| node_event_bus
node_event_bus -->|"durable outbox/log"| node_postgres
node_event_bus -->|"stream transport"| node_redis
node_redis -->|"dispatches"| node_jobs
node_orchestrator -->|"controls observation"| node_game_observer
node_game_observer -->|"uses"| node_game_adapters
node_game_adapters -->|"acquires via"| node_browser
node_game_observer -->|"round signals"| node_decision_engine
node_prediction -->|"confidence signals"| node_decision_engine
node_decision_engine -->|"proposed action"| node_risk
node_risk -->|"approved actions"| node_betting
node_betting -->|"execution outcome"| node_settlement
node_settlement -->|"posts settled truth"| node_ledger
node_ledger -->|"persists"| node_postgres
node_telegram -->|"operator commands"| node_orchestrator
node_orchestrator -.->|"notifications"| node_telegram
node_admin_dashboard -->|"admin control"| node_tenant_control
node_tenant_control -->|"tenant and billing data"| node_postgres
node_orchestrator -.->|"telemetry and health"| node_observability

click node_entry "https://github.com/okeson453/crash/blob/main/src/index.ts"
click node_composition "https://github.com/okeson453/crash/blob/main/src/app/composition.ts"
click node_config "https://github.com/okeson453/crash/blob/main/src/config/loader.ts"
click node_orchestrator "https://github.com/okeson453/crash/blob/main/src/core/orchestrator.ts"
click node_state_machine "https://github.com/okeson453/crash/blob/main/src/core/state-machine/machine.ts"
click node_event_bus "https://github.com/okeson453/crash/blob/main/src/core/event-bus/bus.ts"
click node_jobs "https://github.com/okeson453/crash/blob/main/src/core/job-queue/index.ts"
click node_game_observer "https://github.com/okeson453/crash/blob/main/src/game/observer.ts"
click node_game_adapters "https://github.com/okeson453/crash/blob/main/src/game/adapters/aggregator.ts"
click node_browser "https://github.com/okeson453/crash/blob/main/src/browser/manager.ts"
click node_decision_engine "https://github.com/okeson453/crash/blob/main/src/decision/decision-engine.ts"
click node_prediction "https://github.com/okeson453/crash/blob/main/src/prediction/prediction-engine.ts"
click node_risk "https://github.com/okeson453/crash/blob/main/src/betting/risk-engine.ts"
click node_betting "https://github.com/okeson453/crash/blob/main/src/betting/betting-coordinator.ts"
click node_settlement "https://github.com/okeson453/crash/blob/main/src/settlement/authoritative-settlement-engine.ts"
click node_ledger "https://github.com/okeson453/crash/blob/main/src/ledger/balance-tracker.ts"
click node_telegram "https://github.com/okeson453/crash/blob/main/src/telegram/gateway.ts"
click node_tenant_control "https://github.com/okeson453/crash/blob/main/src/platform/control-plane.ts"
click node_admin_dashboard "https://github.com/okeson453/crash/blob/main/admin-dashboard/app/page.tsx"
click node_postgres "https://github.com/okeson453/crash/blob/main/src/persistence/client.ts"
click node_redis "https://github.com/okeson453/crash/blob/main/src/core/event-bus/streams/redis-streams-bus.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_composition,node_config,node_orchestrator,node_state_machine,node_event_bus,node_jobs toneBlue
class node_game_observer,node_game_adapters,node_browser toneAmber
class node_decision_engine,node_prediction,node_risk,node_betting,node_settlement,node_ledger toneMint
class node_telegram,node_tenant_control,node_admin_dashboard toneRose
class node_postgres,node_redis,node_observability toneIndigo
```

## Components

### Core Runtime

- [Runtime entry](https://github.com/okeson453/crash/blob/main/src/index.ts): Node entrypoint · `src/index.ts`
- [Composition root](https://github.com/okeson453/crash/blob/main/src/app/composition.ts): dependency wiring · Includes upgrade-specific runtime wiring. · `src/app/composition.ts`
- [Validated configuration](https://github.com/okeson453/crash/blob/main/src/config/loader.ts): configuration loader · `src/config/loader.ts`
- [Lifecycle orchestrator](https://github.com/okeson453/crash/blob/main/src/core/orchestrator.ts): control plane · Coordinates controlled round participation. · `src/core/orchestrator.ts`
- [Safety state machine](https://github.com/okeson453/crash/blob/main/src/core/state-machine/machine.ts): deterministic state machine · Guards permitted transitions and fail-safe states. · `src/core/state-machine/machine.ts`
- [Domain event bus](https://github.com/okeson453/crash/blob/main/src/core/event-bus/bus.ts): eventing boundary · Decouples domain control flow and publishes durable events. · `src/core/event-bus/bus.ts`
- [Deferred jobs](https://github.com/okeson453/crash/blob/main/src/core/job-queue/index.ts): job queue · `src/core/job-queue/index.ts`

### Game Acquisition

- [Round observer](https://github.com/okeson453/crash/blob/main/src/game/observer.ts): game signal acquisition · Normalizes API, DOM, and WebSocket observations into rounds. · `src/game/observer.ts`
- [Game adapters](https://github.com/okeson453/crash/blob/main/src/game/adapters/aggregator.ts): acquisition adapters · `src/game/adapters/aggregator.ts`
- [Browser session boundary](https://github.com/okeson453/crash/blob/main/src/browser/manager.ts): Playwright automation · Owns login, browser health, profiles, and evasion. · `src/browser/manager.ts`

### Decision & Execution

- [Decision engine](https://github.com/okeson453/crash/blob/main/src/decision/decision-engine.ts): policy evaluation · `src/decision/decision-engine.ts`
- [Prediction engine](https://github.com/okeson453/crash/blob/main/src/prediction/prediction-engine.ts): signal model · `src/prediction/prediction-engine.ts`
- [Risk gates](https://github.com/okeson453/crash/blob/main/src/betting/risk-engine.ts): financial controls · Enforces limits, velocity, and drawdown constraints. · `src/betting/risk-engine.ts`
- [Betting coordinator](https://github.com/okeson453/crash/blob/main/src/betting/betting-coordinator.ts): side-effect coordinator · Applies idempotent execution and cashout control. · `src/betting/betting-coordinator.ts`
- [Authoritative settlement](https://github.com/okeson453/crash/blob/main/src/settlement/authoritative-settlement-engine.ts): post-round reconciliation · `src/settlement/authoritative-settlement-engine.ts`
- [Financial ledger](https://github.com/okeson453/crash/blob/main/src/ledger/balance-tracker.ts): double-entry accounting · Tracks balances, daily entries, and reconciliation. · `src/ledger/balance-tracker.ts`

### Operations & Platform

- [Telegram operations](https://github.com/okeson453/crash/blob/main/src/telegram/gateway.ts): operator gateway · Authenticates commands and sends operational notifications. · `src/telegram/gateway.ts`
- [Tenant control plane](https://github.com/okeson453/crash/blob/main/src/platform/control-plane.ts): multi-tenant platform · Centralizes tenant provisioning, billing, and subscriptions. · `src/platform/control-plane.ts`
- [Admin dashboard](https://github.com/okeson453/crash/blob/main/admin-dashboard/app/page.tsx): Next.js interface · `admin-dashboard/app/page.tsx`

### Durability & Infrastructure

- [PostgreSQL + TimescaleDB](https://github.com/okeson453/crash/blob/main/src/persistence/client.ts): system of record · Stores outbox, audit, settlement, and financial records. · `src/persistence/client.ts`
- [Redis streams](https://github.com/okeson453/crash/blob/main/src/core/event-bus/streams/redis-streams-bus.ts): event transport · Provides stream transport and work dispatch. · `src/core/event-bus/streams/redis-streams-bus.ts`
- **Observability**: metrics, logs and alerts

## Connections

- Runtime entry → Validated configuration: loads
- Runtime entry → Composition root: starts
- Composition root → Lifecycle orchestrator: wires
- Lifecycle orchestrator → Safety state machine: governed by
- Lifecycle orchestrator → Domain event bus: publishes and consumes
- Domain event bus → PostgreSQL + TimescaleDB: durable outbox/log
- Domain event bus → Redis streams: stream transport
- Redis streams → Deferred jobs: dispatches
- Lifecycle orchestrator → Round observer: controls observation
- Round observer → Game adapters: uses
- Game adapters → Browser session boundary: acquires via
- Round observer → Decision engine: round signals
- Prediction engine → Decision engine: confidence signals
- Decision engine → Risk gates: proposed action
- Risk gates → Betting coordinator: approved actions
- Betting coordinator → Authoritative settlement: execution outcome
- Authoritative settlement → Financial ledger: posts settled truth
- Financial ledger → PostgreSQL + TimescaleDB: persists
- Telegram operations → Lifecycle orchestrator: operator commands
- Lifecycle orchestrator → Telegram operations: notifications
- Admin dashboard → Tenant control plane: admin control
- Tenant control plane → PostgreSQL + TimescaleDB: tenant and billing data
- Lifecycle orchestrator → Observability: telemetry and health
