# cavack/wfh architecture

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

- Interactive diagram: https://gitdiagram.com/cavack/wfh
- Repository: https://github.com/cavack/wfh

## Overview

#### Purpose and Safety Boundary
WaterfallHunter is an observational research/monitoring system for USDT perpetual-futures “waterfall” setups. It evaluates and records signals but is explicitly non-trading: `LIVE_TRADING_ENABLED=false` is the invariant. Decision outputs, execution suitability, and AI advice remain gated evidence rather than order-routing authority. Core runtime: `backend/src/waterfallhunter/main.py`, `backend/src/waterfallhunter/config.py`, `backend/src/waterfallhunter/core/lbank_execution.py`.

#### Service Entry Points
The FastAPI backend is the evaluator, persistence/API boundary, and likely dashboard-data provider, entered through `backend/src/waterfallhunter/main.py`. The Next.js frontend provides the read-only monitoring UI via `frontend/app/dashboard/page.tsx`, composed from evidence, ranking, and funnel views. A separate health/notification process runs from `watchdog/main.py`.

#### Market Discovery and Evidence Acquisition
Discovery adapters collect tradable-market and supporting evidence from exchange, DEX, and on-chain sources. LBank scanning feeds the focused execution/evidence path; DEX Screener and on-chain collection provide complementary discovery signals. Provider selection is centralized to isolate external integrations. Key paths: `backend/src/waterfallhunter/discovery/lbank_scanner.py`, `backend/src/waterfallhunter/discovery/dexscreener.py`, `backend/src/waterfallhunter/core/provider_registry.py`.

#### Signal Evaluation Waterfall
Candidates pass staged deterministic analysis: candle and channel patterns, derivatives and microstructure context, market-regime assessment, anti-chase protection, scoring, then final ranking. The funnel and lifecycle modules represent stage progression and protect against stale or improperly persisted triggers. Key paths: `backend/src/waterfallhunter/core/signal_funnel.py`, `backend/src/waterfallhunter/core/score_v2.py`, `backend/src/waterfallhunter/core/final_ranking.py`; `backend/src/waterfallhunter/core/stage_lifecycle.py`.

#### Validation, Risk, and Advisory Gates
The system applies cross-exchange validation, position/risk calculations, and an advisory AI veto. Gemini is the configured AI provider; failure must leave deterministic evaluation operating without local-model fallback. Execution suitability is a separate observational judgment and cannot replace established proxies until evidence-based promotion. Key paths: `backend/src/waterfallhunter/core/multi_exchange_validator.py`, `backend/src/waterfallhunter/core/risk_manager.py`, `backend/src/waterfallhunter/core/ai_veto.py`; `backend/src/waterfallhunter/core/lbank_execution_suitability.py`.

#### Persistence, Outcomes, and Replay
Database-backed lifecycle state separates production/natural outcomes from historical imports. Signal ledgers and execution stores retain decisions and outcomes; provenance/evidence recording makes decision packets auditable and feature replayable. API route modules expose these records without making them trading controls. Key paths: `backend/src/waterfallhunter/core/db.py`, `backend/src/waterfallhunter/core/lbank_signal_ledger.py`, `backend/src/waterfallhunter/core/historical_outcome_store.py`; `backend/src/waterfallhunter/core/feature_replay.py`.

#### Operations and Infrastructure
Docker Compose runs backend, frontend, and watchdog, with application state in the externalized `waterfall_data` volume. Monitoring infrastructure is Prometheus, Alertmanager, and Grafana; backend notification support forms the bridge from evaluated conditions to operators. Key paths: `docker-compose.yml`, `deploy/prometheus/prometheus.yml`, `deploy/grafana/dashboards/operations.json`; `backend/src/waterfallhunter/core/notifier.py`.

Main entry points: [`backend/src/waterfallhunter/main.py`](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/main.py) (Backend API and evaluator), [`backend/src/waterfallhunter/core/signal_funnel.py`](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/signal_funnel.py) (Signal funnel), [`docker-compose.yml`](https://github.com/cavack/wfh/blob/main/docker-compose.yml) (Docker Compose), [`backend/src/waterfallhunter/core/stage_lifecycle.py`](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/stage_lifecycle.py) (Stage lifecycle)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Read-only UI"]
  node_frontend["Monitoring dashboard<br/>Next.js<br/>[page.tsx]"]
end

subgraph group_discovery["Evidence acquisition"]
  node_provider_registry{{"Provider registry<br/>integration selector"}}
  node_lbank_scanner["LBank scanner<br/>market adapter<br/>[lbank_scanner.py]"]
  node_dexscreener["DEX Screener<br/>market adapter<br/>[dexscreener.py]"]
end

subgraph group_evaluation["Signal evaluation"]
  node_backend["Backend API and evaluator<br/>FastAPI<br/>[main.py]"]
  node_safety_config["Trading safety invariant<br/>configuration<br/>[config.py]"]
  node_signal_funnel["Signal funnel<br/>deterministic pipeline<br/>[signal_funnel.py]"]
  node_stage_lifecycle["Stage lifecycle<br/>lifecycle guard<br/>[stage_lifecycle.py]"]
  node_scoring["Signal scoring<br/>scoring engine<br/>[score_v2.py]"]
  node_ranking["Final ranking<br/>ranking engine<br/>[final_ranking.py]"]
  node_cross_validation["Cross-exchange validation<br/>validation gate"]
  node_risk_manager["Risk calculation<br/>risk gate<br/>[risk_manager.py]"]
  node_ai_veto["Gemini advisory veto<br/>advisory gate<br/>[ai_veto.py]"]
  node_execution_suitability["Execution suitability<br/>observational assessment"]
  node_lbank_execution{{"LBank execution boundary<br/>non-trading boundary<br/>[lbank_execution.py]"}}
end

subgraph group_records["Records and replay"]
  node_database[("Lifecycle database<br/>database layer<br/>[db.py]")]
  node_signal_ledger[("Signal ledger<br/>decision store")]
  node_outcome_store[("Historical outcomes<br/>outcome store")]
  node_feature_replay["Feature replay<br/>replay pipeline<br/>[feature_replay.py]"]
end

subgraph group_operations["Operations"]
  node_notifier["Backend notifier<br/>notification bridge<br/>[notifier.py]"]
  node_watchdog["Watchdog process<br/>health process<br/>[main.py]"]
  node_compose["Docker Compose<br/>runtime orchestration<br/>[docker-compose.yml]"]
  node_prometheus["Prometheus<br/>metrics monitoring<br/>[prometheus.yml]"]
  node_grafana["Grafana operations dashboard<br/>observability UI<br/>[operations.json]"]
end

node_frontend -->|"reads monitoring records"| node_backend
node_provider_registry -->|"selects"| node_lbank_scanner
node_provider_registry -->|"selects"| node_dexscreener
node_lbank_scanner -->|"market candidates"| node_signal_funnel
node_dexscreener -->|"discovery evidence"| node_signal_funnel
node_signal_funnel -->|"stage progression"| node_stage_lifecycle
node_stage_lifecycle -->|"valid candidates"| node_scoring
node_scoring -->|"scored signals"| node_cross_validation
node_cross_validation -->|"validated evidence"| node_risk_manager
node_risk_manager -->|"risk context"| node_ai_veto
node_ai_veto -->|"advisory-gated evidence"| node_ranking
node_ranking -->|"ranked candidates"| node_execution_suitability
node_execution_suitability -.->|"observational suitability only"| node_lbank_execution
node_safety_config -.->|"disables live trading"| node_lbank_execution
node_backend -->|"runs acquisition"| node_provider_registry
node_backend -->|"runs evaluation"| node_signal_funnel
node_ranking -->|"decision packets"| node_signal_ledger
node_stage_lifecycle -->|"persists state"| node_database
node_signal_ledger -->|"stores records"| node_database
node_outcome_store -->|"stores outcomes"| node_database
node_signal_ledger -->|"recorded evidence"| node_feature_replay
node_outcome_store -->|"historical outcomes"| node_feature_replay
node_backend -->|"evaluated conditions"| node_notifier
node_watchdog -->|"health events"| node_notifier
node_prometheus -->|"metrics"| node_grafana
node_compose -.->|"runs"| node_backend
node_compose -.->|"runs"| node_frontend
node_compose -.->|"runs"| node_watchdog

click node_frontend "https://github.com/cavack/wfh/blob/main/frontend/app/dashboard/page.tsx"
click node_backend "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/main.py"
click node_safety_config "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/config.py"
click node_provider_registry "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/provider_registry.py"
click node_lbank_scanner "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/discovery/lbank_scanner.py"
click node_dexscreener "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/discovery/dexscreener.py"
click node_signal_funnel "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/signal_funnel.py"
click node_stage_lifecycle "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/stage_lifecycle.py"
click node_scoring "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/score_v2.py"
click node_ranking "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/final_ranking.py"
click node_cross_validation "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/multi_exchange_validator.py"
click node_risk_manager "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/risk_manager.py"
click node_ai_veto "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/ai_veto.py"
click node_execution_suitability "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/lbank_execution_suitability.py"
click node_lbank_execution "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/lbank_execution.py"
click node_database "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/db.py"
click node_signal_ledger "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/lbank_signal_ledger.py"
click node_outcome_store "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/historical_outcome_store.py"
click node_feature_replay "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/feature_replay.py"
click node_notifier "https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/notifier.py"
click node_watchdog "https://github.com/cavack/wfh/blob/main/watchdog/main.py"
click node_compose "https://github.com/cavack/wfh/blob/main/docker-compose.yml"
click node_prometheus "https://github.com/cavack/wfh/blob/main/deploy/prometheus/prometheus.yml"
click node_grafana "https://github.com/cavack/wfh/blob/main/deploy/grafana/dashboards/operations.json"

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_frontend toneBlue
class node_provider_registry,node_lbank_scanner,node_dexscreener toneAmber
class node_backend,node_safety_config,node_signal_funnel,node_stage_lifecycle,node_scoring,node_ranking,node_cross_validation,node_risk_manager,node_ai_veto,node_execution_suitability,node_lbank_execution toneMint
class node_database,node_signal_ledger,node_outcome_store,node_feature_replay toneRose
class node_notifier,node_watchdog,node_compose,node_prometheus,node_grafana toneIndigo
```

## Components

### Read-only UI

- [Monitoring dashboard](https://github.com/cavack/wfh/blob/main/frontend/app/dashboard/page.tsx): Next.js · Read-only evidence, ranking, and funnel views. · `frontend/app/dashboard/page.tsx`

### Evidence acquisition

- [Provider registry](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/provider_registry.py): integration selector · Centralizes external-provider selection. · `backend/src/waterfallhunter/core/provider_registry.py`
- [LBank scanner](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/discovery/lbank_scanner.py): market adapter · `backend/src/waterfallhunter/discovery/lbank_scanner.py`
- [DEX Screener](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/discovery/dexscreener.py): market adapter · `backend/src/waterfallhunter/discovery/dexscreener.py`

### Signal evaluation

- [Backend API and evaluator](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/main.py): FastAPI · Primary evaluation, persistence, and API boundary. · `backend/src/waterfallhunter/main.py`
- [Trading safety invariant](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/config.py): configuration · LIVE_TRADING_ENABLED remains false. · `backend/src/waterfallhunter/config.py`
- [Signal funnel](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/signal_funnel.py): deterministic pipeline · Advances candidates through analysis stages. · `backend/src/waterfallhunter/core/signal_funnel.py`
- [Stage lifecycle](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/stage_lifecycle.py): lifecycle guard · Prevents stale or improperly persisted triggers. · `backend/src/waterfallhunter/core/stage_lifecycle.py`
- [Signal scoring](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/score_v2.py): scoring engine · Scores pattern, derivatives, microstructure, regime, and anti-chase evidence. · `backend/src/waterfallhunter/core/score_v2.py`
- [Final ranking](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/final_ranking.py): ranking engine · `backend/src/waterfallhunter/core/final_ranking.py`
- [Cross-exchange validation](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/multi_exchange_validator.py): validation gate · `backend/src/waterfallhunter/core/multi_exchange_validator.py`
- [Risk calculation](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/risk_manager.py): risk gate · `backend/src/waterfallhunter/core/risk_manager.py`
- [Gemini advisory veto](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/ai_veto.py): advisory gate · Failure preserves deterministic evaluation without local fallback. · `backend/src/waterfallhunter/core/ai_veto.py`
- [Execution suitability](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/lbank_execution_suitability.py): observational assessment · Evidence-gated judgment, never order authority. · `backend/src/waterfallhunter/core/lbank_execution_suitability.py`
- [LBank execution boundary](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/lbank_execution.py): non-trading boundary · Retains no-live-trading execution constraint. · `backend/src/waterfallhunter/core/lbank_execution.py`

### Records and replay

- [Lifecycle database](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/db.py): database layer · Stores lifecycle state and application records. · `backend/src/waterfallhunter/core/db.py`
- [Signal ledger](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/lbank_signal_ledger.py): decision store · Retains auditable signal decisions. · `backend/src/waterfallhunter/core/lbank_signal_ledger.py`
- [Historical outcomes](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/historical_outcome_store.py): outcome store · Separates historical imports from natural production outcomes. · `backend/src/waterfallhunter/core/historical_outcome_store.py`
- [Feature replay](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/feature_replay.py): replay pipeline · Replays recorded evidence and features. · `backend/src/waterfallhunter/core/feature_replay.py`

### Operations

- [Backend notifier](https://github.com/cavack/wfh/blob/main/backend/src/waterfallhunter/core/notifier.py): notification bridge · Sends evaluated-condition notifications to operators. · `backend/src/waterfallhunter/core/notifier.py`
- [Watchdog process](https://github.com/cavack/wfh/blob/main/watchdog/main.py): health process · `watchdog/main.py`
- [Docker Compose](https://github.com/cavack/wfh/blob/main/docker-compose.yml): runtime orchestration · Runs backend, frontend, watchdog, and externalized state volume. · `docker-compose.yml`
- [Prometheus](https://github.com/cavack/wfh/blob/main/deploy/prometheus/prometheus.yml): metrics monitoring · `deploy/prometheus/prometheus.yml`
- [Grafana operations dashboard](https://github.com/cavack/wfh/blob/main/deploy/grafana/dashboards/operations.json): observability UI · `deploy/grafana/dashboards/operations.json`

## Connections

- Monitoring dashboard → Backend API and evaluator: reads monitoring records
- Provider registry → LBank scanner: selects
- Provider registry → DEX Screener: selects
- LBank scanner → Signal funnel: market candidates
- DEX Screener → Signal funnel: discovery evidence
- Signal funnel → Stage lifecycle: stage progression
- Stage lifecycle → Signal scoring: valid candidates
- Signal scoring → Cross-exchange validation: scored signals
- Cross-exchange validation → Risk calculation: validated evidence
- Risk calculation → Gemini advisory veto: risk context
- Gemini advisory veto → Final ranking: advisory-gated evidence
- Final ranking → Execution suitability: ranked candidates
- Execution suitability → LBank execution boundary: observational suitability only
- Trading safety invariant → LBank execution boundary: disables live trading
- Backend API and evaluator → Provider registry: runs acquisition
- Backend API and evaluator → Signal funnel: runs evaluation
- Final ranking → Signal ledger: decision packets
- Stage lifecycle → Lifecycle database: persists state
- Signal ledger → Lifecycle database: stores records
- Historical outcomes → Lifecycle database: stores outcomes
- Signal ledger → Feature replay: recorded evidence
- Historical outcomes → Feature replay: historical outcomes
- Backend API and evaluator → Backend notifier: evaluated conditions
- Watchdog process → Backend notifier: health events
- Prometheus → Grafana operations dashboard: metrics
- Docker Compose → Backend API and evaluator: runs
- Docker Compose → Monitoring dashboard: runs
- Docker Compose → Watchdog process: runs
