# ammar8065/clearpath-global architecture

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

- Interactive diagram: https://gitdiagram.com/ammar8065/clearpath-global
- Repository: https://github.com/ammar8065/clearpath-global

## Overview

1. **System shape**
   - This is a single-process FastAPI application with a static JavaScript frontend bundled in the same repo.
   - The backend exposes both REST APIs and the root HTML page; the browser app drives most user interaction.

2. **Core architecture layers**
   - The backend is split into `routes/` → `services/` → `models/` / `engine/`.
   - `schemas/` define request/response contracts, `models/` map to the database, and `services/` coordinate business logic before calling the rules engine.

3. **Persistence boundary**
   - Data is stored in SQLite by default in `risk_intelligence.db`, with SQLAlchemy as the ORM.
   - Alembic manages schema evolution; migrations live in `alembic/versions/`.
   - This repository is designed to support a later database swap via `DATABASE_URL`, but the current architecture is local-first.

4. **Domain model**
   - The main persisted entities are `tenant`, `knowledge_source`, and `rule`.
   - `client` and `asset` models exist as legacy data types, along with `residency_history`, but the current privacy-first mode prevents normal persistence of client financial facts.
   - Rules are versioned and time-bounded, with `section_reference` support for mapping logic back to source material.

5. **Rules/evaluation engine**
   - The evaluation subsystem is separated into `engine/conditions.py`, `selector.py`, `scorer.py`, and `report.py`.
   - The likely flow is: choose applicable rule versions, evaluate rule conditions against submitted client data, compute weighted scoring, then assemble a structured report with citations and warnings.
   - This separation is important: selection, scoring, and report assembly are distinct internal stages rather than one monolithic evaluator.

6. **Private assessment flow**
   - The main product flow is stateless evaluation through `/evaluate/private` and `/evaluate/private/preview`.
   - The browser sends a flat `client_data` payload plus optional assessment metadata; the server evaluates it in memory and returns results without persisting the client payload.
   - This makes the evaluation path the main boundary between ephemeral client facts and durable workspace/rule/source data.

7. **API surface**
   - Functional API groups map to the domain: `/tenants`, `/sources`, `/rules`, and evaluation endpoints.
   - `/evaluate/rules/{rule_code}/versions` supports rule history inspection for the UI.
   - `/clients` and `/assets` still exist but are effectively disabled in privacy mode, returning `403` to steer users to the stateless workflow.

8. **Frontend architecture**
   - The frontend is a modular vanilla JavaScript app under `frontend/`, not a framework SPA.
   - `app.js`, `navigation.js`, `dashboard.js`, `rules.js`, `sources.js`, `tenants.js`, and the `evaluation_*` files partition the UI by workflow.
   - The private assessment UI includes guided form entry, a live readiness/completeness panel, and optional advanced JSON editing.

9. **User-facing data flow**
   - The typical loop is: manage tenants/sources/rules, then run a private assessment, inspect the generated risk report, and optionally export/import locally in the browser.
   - Repeatability is handled client-side through local JSON export/import rather than server-side storage of assessment payloads.

10. **Privacy boundary**
   - `PRIVACY_MODE` is the key architectural switch.
   - When enabled, the platform keeps operational metadata like tenants, rules, and knowledge sources, but blocks persistent storage of client financial data and evaluation results.
   - This is the main security/privacy boundary in the repository and materially changes how the API behaves.

11. **Operational tooling**
   - `seed_data.py` supports local bootstrapping.
   - `app/database/init_db.py` and `AUTO_CREATE_SCHEMA` exist as dev convenience, but the intended migration path is Alembic-first.
   - The included `.edge-headless/` and test artifacts suggest local browser-based or headless validation during development.

12. **Testing strategy**
   - `tests/` contains evaluator unit tests, regression tests, and live HTTP integration tests against a temporary migrated database.
   - That mix reflects the architecture: business logic is testable in isolation, while route behavior and migration compatibility are validated end-to-end.

Main entry points: [`app/engine`](https://github.com/ammar8065/clearpath-global/tree/main/app/engine) (Rules engine), [`app/routes`](https://github.com/ammar8065/clearpath-global/tree/main/app/routes) (API routes), [`app/services`](https://github.com/ammar8065/clearpath-global/tree/main/app/services) (Services), [`tests`](https://github.com/ammar8065/clearpath-global/tree/main/tests) (Tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_main_app["FastAPI app<br/>web-api<br/>[main.py]"]
  node_api_routes["API routes<br/>http routes"]
  node_services_layer["Services<br/>business logic"]
  node_eval_service["Evaluation svc<br/>workflow service<br/>[evaluation.py]"]
  node_domain_services["Domain svcs<br/>entity services"]
  node_eval_engine["Rules engine<br/>evaluation engine"]
  node_rule_selector["Selector<br/>engine stage<br/>[selector.py]"]
  node_condition_eval["Conditions<br/>engine stage<br/>[conditions.py]"]
  node_scoring["Scorer<br/>engine stage<br/>[scorer.py]"]
  node_report_gen["Report<br/>engine stage<br/>[report.py]"]
  node_schemas["Schemas<br/>contracts"]
  node_models[("Models<br/>orm entities")]
end

subgraph group_frontend["Frontend"]
  node_frontend_app["Browser app<br/>vanilla js ui<br/>[app.js]"]
  node_admin_views["Admin views<br/>ui modules"]
  node_eval_ui["Assessment UI<br/>ui workflow<br/>[evaluation.js]"]
end

subgraph group_data["Data"]
  node_db_layer[("DB session<br/>persistence")]
  node_migrations["Alembic<br/>migrations"]
  node_seed_bootstrap["Bootstrap<br/>local tooling<br/>[seed_data.py]"]
end

subgraph group_quality["Quality"]
  node_tests["Tests<br/>verification"]
end

node_main_app -->|"mounts"| node_api_routes
node_main_app -->|"serves"| node_frontend_app
node_api_routes -->|"calls"| node_services_layer
node_services_layer -->|"delegates"| node_domain_services
node_services_layer -->|"delegates"| node_eval_service
node_eval_service -->|"runs"| node_eval_engine
node_eval_engine -->|"stage"| node_rule_selector
node_eval_engine -->|"stage"| node_condition_eval
node_eval_engine -->|"stage"| node_scoring
node_eval_engine -->|"stage"| node_report_gen
node_api_routes -->|"validates"| node_schemas
node_services_layer -->|"persists"| node_models
node_models -->|"uses"| node_db_layer
node_db_layer -->|"evolves via"| node_migrations
node_seed_bootstrap -->|"initializes"| node_db_layer
node_frontend_app -->|"loads"| node_admin_views
node_frontend_app -->|"loads"| node_eval_ui
node_eval_ui -->|"submits"| node_api_routes
node_admin_views -->|"manages"| node_api_routes
node_tests -->|"verifies"| node_eval_engine
node_tests -->|"verifies"| node_api_routes
node_tests -->|"verifies"| node_migrations

click node_main_app "https://github.com/ammar8065/clearpath-global/blob/main/app/main.py"
click node_api_routes "https://github.com/ammar8065/clearpath-global/tree/main/app/routes"
click node_services_layer "https://github.com/ammar8065/clearpath-global/tree/main/app/services"
click node_eval_service "https://github.com/ammar8065/clearpath-global/blob/main/app/services/evaluation.py"
click node_domain_services "https://github.com/ammar8065/clearpath-global/tree/main/app/services"
click node_eval_engine "https://github.com/ammar8065/clearpath-global/tree/main/app/engine"
click node_rule_selector "https://github.com/ammar8065/clearpath-global/blob/main/app/engine/selector.py"
click node_condition_eval "https://github.com/ammar8065/clearpath-global/blob/main/app/engine/conditions.py"
click node_scoring "https://github.com/ammar8065/clearpath-global/blob/main/app/engine/scorer.py"
click node_report_gen "https://github.com/ammar8065/clearpath-global/blob/main/app/engine/report.py"
click node_schemas "https://github.com/ammar8065/clearpath-global/tree/main/app/schemas"
click node_models "https://github.com/ammar8065/clearpath-global/tree/main/app/models"
click node_db_layer "https://github.com/ammar8065/clearpath-global/tree/main/app/database"
click node_migrations "https://github.com/ammar8065/clearpath-global/tree/main/alembic"
click node_frontend_app "https://github.com/ammar8065/clearpath-global/blob/main/frontend/app.js"
click node_admin_views "https://github.com/ammar8065/clearpath-global/tree/main/frontend"
click node_eval_ui "https://github.com/ammar8065/clearpath-global/blob/main/frontend/evaluation.js"
click node_seed_bootstrap "https://github.com/ammar8065/clearpath-global/blob/main/seed_data.py"
click node_tests "https://github.com/ammar8065/clearpath-global/tree/main/tests"

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_main_app,node_api_routes,node_services_layer,node_eval_service,node_domain_services,node_eval_engine,node_rule_selector,node_condition_eval,node_scoring,node_report_gen,node_schemas,node_models toneBlue
class node_frontend_app,node_admin_views,node_eval_ui toneAmber
class node_db_layer,node_migrations,node_seed_bootstrap toneMint
class node_tests toneRose
```

## Components

### Backend

FastAPI app, API layers, domain logic, and persistence

- [FastAPI app](https://github.com/ammar8065/clearpath-global/blob/main/app/main.py): web-api · App startup, routing, and static page delivery · `app/main.py`
- [API routes](https://github.com/ammar8065/clearpath-global/tree/main/app/routes): http routes · REST endpoints for tenants, sources, rules, clients, assets, and evaluation · `app/routes`
- [Services](https://github.com/ammar8065/clearpath-global/tree/main/app/services): business logic · Coordinates API requests, persistence, and engine calls · `app/services`
- [Evaluation svc](https://github.com/ammar8065/clearpath-global/blob/main/app/services/evaluation.py): workflow service · Runs stateless private assessments and assembles evaluation results · `app/services/evaluation.py`
- [Domain svcs](https://github.com/ammar8065/clearpath-global/tree/main/app/services): entity services · CRUD and domain operations for tenants, sources, rules, clients, and assets · `app/services`
- [Rules engine](https://github.com/ammar8065/clearpath-global/tree/main/app/engine): evaluation engine · Selection, condition checking, scoring, and report assembly · `app/engine`
- [Selector](https://github.com/ammar8065/clearpath-global/blob/main/app/engine/selector.py): engine stage · Chooses applicable rule versions for an assessment · `app/engine/selector.py`
- [Conditions](https://github.com/ammar8065/clearpath-global/blob/main/app/engine/conditions.py): engine stage · Evaluates rule conditions against submitted client data · `app/engine/conditions.py`
- [Scorer](https://github.com/ammar8065/clearpath-global/blob/main/app/engine/scorer.py): engine stage · Computes weighted scores and intermediate outcomes · `app/engine/scorer.py`
- [Report](https://github.com/ammar8065/clearpath-global/blob/main/app/engine/report.py): engine stage · Builds structured results with citations and warnings · `app/engine/report.py`
- [Schemas](https://github.com/ammar8065/clearpath-global/tree/main/app/schemas): contracts · Request and response contracts for API boundaries · `app/schemas`
- [Models](https://github.com/ammar8065/clearpath-global/tree/main/app/models): orm entities · SQLAlchemy entities for tenants, sources, rules, clients, assets, and residency history · `app/models`

### Frontend

Static browser app for navigation, data entry, and evaluation workflows

- [Browser app](https://github.com/ammar8065/clearpath-global/blob/main/frontend/app.js): vanilla js ui · Static frontend shell and workflow navigation · `frontend/app.js`
- [Admin views](https://github.com/ammar8065/clearpath-global/tree/main/frontend): ui modules · Dashboard, tenants, sources, rules, and asset/client screens · `frontend`
- [Assessment UI](https://github.com/ammar8065/clearpath-global/blob/main/frontend/evaluation.js): ui workflow · Private assessment forms, readiness panel, JSON editing, and report views · `frontend/evaluation.js`

### Data

Database schema, migrations, and local bootstrap

- [DB session](https://github.com/ammar8065/clearpath-global/tree/main/app/database): persistence · Database session and declarative base wiring · `app/database`
- [Alembic](https://github.com/ammar8065/clearpath-global/tree/main/alembic): migrations · Schema evolution and versioned migrations · `alembic`
- [Bootstrap](https://github.com/ammar8065/clearpath-global/blob/main/seed_data.py): local tooling · Initial local data seeding and schema convenience helpers · `seed_data.py`

### Quality

Tests and validation coverage

- [Tests](https://github.com/ammar8065/clearpath-global/tree/main/tests): verification · Evaluator unit tests, regressions, and live HTTP integration tests · `tests`

## Connections

- FastAPI app → API routes: mounts · Registers API routes and serves the browser app
- FastAPI app → Browser app: serves · Delivers the static frontend entrypoints
- API routes → Services: calls · Routes delegate work to services
- Services → Domain svcs: delegates · CRUD and domain workflows flow through entity services
- Services → Evaluation svc: delegates · Evaluation endpoints use the stateless assessment service
- Evaluation svc → Rules engine: runs · Assessment service invokes the rules engine pipeline
- Rules engine → Selector: stage · Selects applicable rule versions first
- Rules engine → Conditions: stage · Checks submitted facts against rule conditions
- Rules engine → Scorer: stage · Computes weighted scores and outcomes
- Rules engine → Report: stage · Assembles the final structured report
- API routes → Schemas: validates · Endpoints exchange typed request and response payloads
- Services → Models: persists · Services load and write domain records through ORM models
- Models → DB session: uses · Models are bound to the SQLAlchemy database layer
- DB session → Alembic: evolves via · Alembic manages schema changes and version history
- Bootstrap → DB session: initializes · Local bootstrap helpers create or seed the database
- Browser app → Admin views: loads · Navigation exposes the administrative workflows
- Browser app → Assessment UI: loads · Private assessment workflow is a main user path
- Assessment UI → API routes: submits · Browser assessment forms call the evaluation endpoints
- Admin views → API routes: manages · Admin screens manage tenants, sources, rules, clients, and assets
- Tests → Rules engine: verifies · Unit tests cover evaluator behavior
- Tests → API routes: verifies · HTTP integration tests exercise route behavior
- Tests → Alembic: verifies · Integration coverage checks migrated database compatibility
