# ltsroy/jansetu architecture

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

- Interactive diagram: https://gitdiagram.com/ltsroy/jansetu
- Repository: https://github.com/ltsroy/jansetu

## Overview

1. **What this repo is**
   JanSetu is a Python-first decision system for MPLADS allocation. The architectural center is a deterministic analytical engine that turns public deprivation data plus citizen demand into an optimized ₹5 Cr works plan, then packages the result into an audit-ready sanction letter and dashboard.

2. **Two major boundary layers**
   The repo cleanly separates:
   - an **offline/core layer** in `jansetu/` that is deterministic, testable, and mostly pure-Python; and
   - **data/product delivery layers** around it: scrapers, cloud export scripts, AI-assisted packet generation, optimization service, and multiple UIs.

3. **Input data and provenance**
   The system ingests a heterogeneous set of public sources: SECC, Census, OSM, VIIRS, GHSL, MPLADS spend, LGD boundary keys, JJM, IMD rainfall, CAG rules, news RSS, and citizen-intake text. `jansetu/provenance.py` and `jansetu/indicators.py` form the trust boundary: they register source lineage and validate inputs before they affect scoring.

4. **Core analytical pipeline**
   The main flow inside `jansetu/` is:
   ingestion → deprivation scoring → diagnostic modeling → tiering → demand/need divergence → works eligibility → optimization.  
   The main modules are `ingest.py`, `deprivation.py`, `modeling.py`, `tiers.py`, `divergence.py`, `works.py`, and `optimize.py`.

5. **Decision logic and explainability**
   The deprivation model is intentionally glass-box: weighted indicators are decomposed back to source-tagged contributions. `modeling.py` checks whether the policy weights align with the underlying data structure, while `tiers.py` and `sensitivity.py` test robustness and deterministic grouping rather than training a predictive model.

6. **Optimization boundary**
   `jansetu.optimize` is the exact portfolio solver, implemented with **OR-Tools CP-SAT**. It enforces the budget and statutory earmarks, so the “plan” is not just a ranking; it is a constrained selection of works. `optimizer/` wraps this as a separate Cloud Run microservice with its own Dockerfile and requirements.

7. **Audit and governance outputs**
   After optimization, `audit.py` produces the fairness certificate and reproducibility hash. `watchdog.py` adds compliance overlays such as fund-lapse countdowns and CAG checks, so the final output is defensible, not just optimal.

8. **Live intelligence overlays**
   Several modules enrich the core plan with time-sensitive context:
   - `retrospective.py` compares historic spend vs deprivation,
   - `living.py` refreshes indicators with newer scheme data,
   - `foresight.py` uses IMD timing for weather-sensitive works,
   - `atlas.py` ranks across constituencies,
   - `brief.py` synthesizes these into a command brief.  
   These are overlays on top of the stable deprivation/optimization core.

9. **AI-assisted intake and letter generation**
   The `ai/` package is a separate language-model boundary. `ai/intake.py` ingests citizen text, `ai/orchestrator.py` coordinates generation, and `ai/letter.py` drafts the sanction packet. The README and deployment path indicate **Gemini** is used here, but it is not part of the core decision math.

10. **Presentation layer**
   The repo contains more than one UI surface:
   - `app.py` provides the local web app entry point,
   - `dashboard/` is a self-contained static bundle for direct browser use,
   - `frontend/` is a Vite/React TypeScript app,
   - `src/` is a small Next.js-style app with an API route (`src/app/api/live-data/route.ts`) and a hotspot map component.  
   A diagram should show these as separate presentation consumers of the same underlying JanSetu data/model outputs.

11. **Cloud and external service dependencies**
   The production path uses **BigQuery** for the warehouse layer, **Earth Engine** for VIIRS/GHSL extraction, **Google Maps** for visualization, **Firebase** for intake sync, and **Cloud Run** for the optimizer service. These services matter because they split the repo into offline analytical code versus cloud-backed data/execution paths.

12. **Tests and reproducibility as a subsystem**
   The `tests/` suite is large and explicit about invariants: deprivation monotonicity, earmark enforcement, astroturf resistance, cloud artifact contracts, and API behavior. Together with `DATA.md`, `MODELS.md`, and provenance hashing, testing is part of the architecture: it preserves reproducibility and policy traceability across the whole pipeline.

Main entry points: [`jansetu/brief.py`](https://github.com/ltsroy/jansetu/blob/main/jansetu/brief.py) (Brief), [`jansetu/pipeline.py`](https://github.com/ltsroy/jansetu/blob/main/jansetu/pipeline.py) (Pipeline), [`tests/test_pipeline.py`](https://github.com/ltsroy/jansetu/blob/main/tests/test_pipeline.py) (Tests), [`jansetu/ingest.py`](https://github.com/ltsroy/jansetu/blob/main/jansetu/ingest.py) (Ingest)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core engine"]
  node_ingest["Ingest<br/>[ingest.py]"]
  node_provenance["Provenance<br/>trust boundary<br/>[provenance.py]"]
  node_indicators["Indicators<br/>validation<br/>[indicators.py]"]
  node_deprivation["Deprivation<br/>scoring engine<br/>[deprivation.py]"]
  node_modeling["Modeling<br/>glass-box model<br/>[modeling.py]"]
  node_tiers["Tiers<br/>grouping<br/>[tiers.py]"]
  node_demand["Demand<br/>intake fusion<br/>[demand.py]"]
  node_divergence["Divergence<br/>gap analysis<br/>[divergence.py]"]
  node_works["Works<br/>eligibility<br/>[works.py]"]
  node_optimize["Optimize<br/>CP-SAT solver<br/>[optimize.py]"]
  node_audit["Audit<br/>governance<br/>[audit.py]"]
  node_watchdog["Watchdog<br/>compliance<br/>[watchdog.py]"]
  node_pipeline["Pipeline<br/>orchestration<br/>[pipeline.py]"]
end

subgraph group_overlays["Live overlays"]
  node_brief["Brief<br/>synthesis<br/>[brief.py]"]
  node_retrospective["Retrospective<br/>historical overlay<br/>[retrospective.py]"]
  node_living["Living<br/>refresh overlay<br/>[living.py]"]
  node_foresight["Foresight<br/>weather overlay<br/>[foresight.py]"]
  node_atlas["Atlas<br/>ranking overlay<br/>[atlas.py]"]
end

subgraph group_ai["AI boundary"]
  node_ai_intake["Intake AI<br/>LLM intake<br/>[intake.py]"]
  node_ai_orchestrator["AI Flow<br/>LLM orchestration<br/>[orchestrator.py]"]
  node_ai_letter["Letter AI<br/>drafting<br/>[letter.py]"]
end

subgraph group_ui["Presentation"]
  node_app_py["App<br/>[app.py]"]
  node_dashboard["Dashboard<br/>static UI<br/>[index.html]"]
  node_frontend["Frontend<br/>React UI<br/>[App.tsx]"]
  node_next_app["Next App<br/>Next UI<br/>[page.tsx]"]
end

subgraph group_infra["Data & services"]
  node_optimizer_service["Optimizer svc<br/>Cloud Run service<br/>[main.py]"]
  node_scrapers["Scrapers<br/>ingestion jobs<br/>[__init__.py]"]
  node_earthengine["Earth Engine<br/>export script<br/>[ee_export.js]"]
  node_sql[("Warehouse SQL<br/>BQ models<br/>[01_spine.sql]")]
end

subgraph group_quality["Verification"]
  node_tests["Tests<br/>invariants<br/>[test_pipeline.py]"]
end

node_pipeline -->|"orchestrates"| node_ingest
node_ingest -->|"tags lineage"| node_provenance
node_ingest -->|"normalizes"| node_indicators
node_indicators -->|"feeds"| node_deprivation
node_deprivation -->|"explains"| node_modeling
node_modeling -->|"groups"| node_tiers
node_tiers -->|"compares"| node_demand
node_demand -->|"diffs"| node_divergence
node_divergence -->|"filters"| node_works
node_works -->|"constrains"| node_optimize
node_optimize -->|"certifies"| node_audit
node_optimize -->|"overlays"| node_watchdog
node_watchdog -->|"summarizes"| node_brief
node_audit -->|"attests"| node_brief
node_retrospective -->|"enriches"| node_brief
node_living -->|"enriches"| node_brief
node_foresight -->|"enriches"| node_brief
node_atlas -->|"ranks"| node_brief
node_ai_intake -->|"inputs"| node_ai_orchestrator
node_ai_orchestrator -->|"drafts"| node_ai_letter
node_pipeline -.->|"layers"| node_retrospective
node_pipeline -.->|"layers"| node_living
node_pipeline -.->|"layers"| node_foresight
node_pipeline -.->|"layers"| node_atlas
node_pipeline -->|"produces"| node_brief
node_app_py -->|"serves"| node_brief
node_dashboard -->|"renders"| node_brief
node_frontend -->|"renders"| node_brief
node_next_app -->|"renders"| node_brief
node_next_app -->|"requests"| node_pipeline
node_optimizer_service -->|"wraps"| node_optimize
node_scrapers -->|"supplies"| node_ingest
node_earthengine -->|"supplies"| node_indicators
node_sql -->|"models"| node_deprivation
node_tests -->|"checks"| node_ingest
node_tests -->|"checks"| node_optimize
node_tests -->|"checks"| node_audit
node_tests -->|"checks"| node_ai_orchestrator
node_tests -->|"checks"| node_pipeline

click node_ingest "https://github.com/ltsroy/jansetu/blob/main/jansetu/ingest.py"
click node_provenance "https://github.com/ltsroy/jansetu/blob/main/jansetu/provenance.py"
click node_indicators "https://github.com/ltsroy/jansetu/blob/main/jansetu/indicators.py"
click node_deprivation "https://github.com/ltsroy/jansetu/blob/main/jansetu/deprivation.py"
click node_modeling "https://github.com/ltsroy/jansetu/blob/main/jansetu/modeling.py"
click node_tiers "https://github.com/ltsroy/jansetu/blob/main/jansetu/tiers.py"
click node_demand "https://github.com/ltsroy/jansetu/blob/main/jansetu/demand.py"
click node_divergence "https://github.com/ltsroy/jansetu/blob/main/jansetu/divergence.py"
click node_works "https://github.com/ltsroy/jansetu/blob/main/jansetu/works.py"
click node_optimize "https://github.com/ltsroy/jansetu/blob/main/jansetu/optimize.py"
click node_audit "https://github.com/ltsroy/jansetu/blob/main/jansetu/audit.py"
click node_watchdog "https://github.com/ltsroy/jansetu/blob/main/jansetu/watchdog.py"
click node_brief "https://github.com/ltsroy/jansetu/blob/main/jansetu/brief.py"
click node_retrospective "https://github.com/ltsroy/jansetu/blob/main/jansetu/retrospective.py"
click node_living "https://github.com/ltsroy/jansetu/blob/main/jansetu/living.py"
click node_foresight "https://github.com/ltsroy/jansetu/blob/main/jansetu/foresight.py"
click node_atlas "https://github.com/ltsroy/jansetu/blob/main/jansetu/atlas.py"
click node_pipeline "https://github.com/ltsroy/jansetu/blob/main/jansetu/pipeline.py"
click node_ai_intake "https://github.com/ltsroy/jansetu/blob/main/ai/intake.py"
click node_ai_orchestrator "https://github.com/ltsroy/jansetu/blob/main/ai/orchestrator.py"
click node_ai_letter "https://github.com/ltsroy/jansetu/blob/main/ai/letter.py"
click node_app_py "https://github.com/ltsroy/jansetu/blob/main/app.py"
click node_dashboard "https://github.com/ltsroy/jansetu/blob/main/dashboard/index.html"
click node_frontend "https://github.com/ltsroy/jansetu/blob/main/frontend/src/App.tsx"
click node_next_app "https://github.com/ltsroy/jansetu/blob/main/src/app/page.tsx"
click node_optimizer_service "https://github.com/ltsroy/jansetu/blob/main/optimizer/main.py"
click node_scrapers "https://github.com/ltsroy/jansetu/blob/main/scrapers/__init__.py"
click node_earthengine "https://github.com/ltsroy/jansetu/blob/main/earthengine/ee_export.js"
click node_sql "https://github.com/ltsroy/jansetu/blob/main/sql/01_spine.sql"
click node_tests "https://github.com/ltsroy/jansetu/blob/main/tests/test_pipeline.py"

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_ingest,node_provenance,node_indicators,node_deprivation,node_modeling,node_tiers,node_demand,node_divergence,node_works,node_optimize,node_audit,node_watchdog,node_pipeline toneBlue
class node_brief,node_retrospective,node_living,node_foresight,node_atlas toneAmber
class node_ai_intake,node_ai_orchestrator,node_ai_letter toneMint
class node_app_py,node_dashboard,node_frontend,node_next_app toneRose
class node_optimizer_service,node_scrapers,node_earthengine,node_sql toneIndigo
class node_tests toneTeal
```

## Components

### Core engine

Deterministic analytical pipeline and governance logic in jansetu/.

- [Ingest](https://github.com/ltsroy/jansetu/blob/main/jansetu/ingest.py): core ingest · Loads heterogeneous public and citizen inputs into the decision pipeline. · `jansetu/ingest.py`
- [Provenance](https://github.com/ltsroy/jansetu/blob/main/jansetu/provenance.py): trust boundary · Registers source lineage and provenance metadata. · `jansetu/provenance.py`
- [Indicators](https://github.com/ltsroy/jansetu/blob/main/jansetu/indicators.py): validation · Validates and structures source data before scoring. · `jansetu/indicators.py`
- [Deprivation](https://github.com/ltsroy/jansetu/blob/main/jansetu/deprivation.py): scoring engine · Computes weighted deprivation scores from source-tagged inputs. · `jansetu/deprivation.py`
- [Modeling](https://github.com/ltsroy/jansetu/blob/main/jansetu/modeling.py): glass-box model · Explains policy-weight fit and decomposes contributions. · `jansetu/modeling.py`
- [Tiers](https://github.com/ltsroy/jansetu/blob/main/jansetu/tiers.py): grouping · Deterministic tiering and robustness checks for constituencies or units. · `jansetu/tiers.py`
- [Demand](https://github.com/ltsroy/jansetu/blob/main/jansetu/demand.py): intake fusion · Represents citizen demand and need signals. · `jansetu/demand.py`
- [Divergence](https://github.com/ltsroy/jansetu/blob/main/jansetu/divergence.py): gap analysis · Measures demand-versus-need mismatch for prioritization. · `jansetu/divergence.py`
- [Works](https://github.com/ltsroy/jansetu/blob/main/jansetu/works.py): eligibility · Translates signals into eligible works candidates. · `jansetu/works.py`
- [Optimize](https://github.com/ltsroy/jansetu/blob/main/jansetu/optimize.py): CP-SAT solver · Exact constrained portfolio selection under budget and earmarks. · `jansetu/optimize.py`
- [Audit](https://github.com/ltsroy/jansetu/blob/main/jansetu/audit.py): governance · Produces fairness certificates and reproducibility hashes. · `jansetu/audit.py`
- [Watchdog](https://github.com/ltsroy/jansetu/blob/main/jansetu/watchdog.py): compliance · Adds lapse, CAG, and compliance overlays to the final plan. · `jansetu/watchdog.py`
- [Pipeline](https://github.com/ltsroy/jansetu/blob/main/jansetu/pipeline.py): orchestration · Coordinates the end-to-end JanSetu workflow. · `jansetu/pipeline.py`

### Live overlays

Time-sensitive enrichment and comparative analysis layered onto the core plan.

- [Brief](https://github.com/ltsroy/jansetu/blob/main/jansetu/brief.py): synthesis · Summarizes the optimized plan and overlays into a command brief. · `jansetu/brief.py`
- [Retrospective](https://github.com/ltsroy/jansetu/blob/main/jansetu/retrospective.py): historical overlay · Compares historic spend against deprivation. · `jansetu/retrospective.py`
- [Living](https://github.com/ltsroy/jansetu/blob/main/jansetu/living.py): refresh overlay · Refreshes indicators with newer scheme data. · `jansetu/living.py`
- [Foresight](https://github.com/ltsroy/jansetu/blob/main/jansetu/foresight.py): weather overlay · Uses IMD timing for weather-sensitive works. · `jansetu/foresight.py`
- [Atlas](https://github.com/ltsroy/jansetu/blob/main/jansetu/atlas.py): ranking overlay · Ranks constituencies across the broader field. · `jansetu/atlas.py`

### AI boundary

Language-model assisted intake and sanction packet generation.

- [Intake AI](https://github.com/ltsroy/jansetu/blob/main/ai/intake.py): LLM intake · Ingests citizen text at the language-model boundary. · `ai/intake.py`
- [AI Flow](https://github.com/ltsroy/jansetu/blob/main/ai/orchestrator.py): LLM orchestration · Coordinates AI-assisted packet generation. · `ai/orchestrator.py`
- [Letter AI](https://github.com/ltsroy/jansetu/blob/main/ai/letter.py): drafting · Drafts the sanction packet and letter output. · `ai/letter.py`

### Presentation

Multiple consumer surfaces for the same JanSetu outputs.

- [App](https://github.com/ltsroy/jansetu/blob/main/app.py): local web app · Python entry point for the local application. · `app.py`
- [Dashboard](https://github.com/ltsroy/jansetu/blob/main/dashboard/index.html): static UI · Self-contained browser bundle for direct use. · `dashboard/index.html`
- [Frontend](https://github.com/ltsroy/jansetu/blob/main/frontend/src/App.tsx): React UI · Vite/React TypeScript presentation app. · `frontend/src/App.tsx`
- [Next App](https://github.com/ltsroy/jansetu/blob/main/src/app/page.tsx): Next UI · Next-style app and API route consumer. · `src/app/page.tsx`

### Data & services

Scrapers, warehouse scripts, optimizer service, and external integration paths.

- [Optimizer svc](https://github.com/ltsroy/jansetu/blob/main/optimizer/main.py): Cloud Run service · Containerized service wrapper around the exact optimizer. · `optimizer/main.py`
- [Scrapers](https://github.com/ltsroy/jansetu/blob/main/scrapers/__init__.py): ingestion jobs · Source-specific collectors for news, schemes, IMD, MPLADS, and CAG. · `scrapers/__init__.py`
- [Earth Engine](https://github.com/ltsroy/jansetu/blob/main/earthengine/ee_export.js): export script · VIIRS/GHSL extraction and external geospatial export. · `earthengine/ee_export.js`
- [Warehouse SQL](https://github.com/ltsroy/jansetu/blob/main/sql/01_spine.sql): BQ models · BigQuery loading and deprivation warehouse transforms. · `sql/01_spine.sql`

### Verification

Tests and contract checks that preserve reproducibility and policy traceability.

- [Tests](https://github.com/ltsroy/jansetu/blob/main/tests/test_pipeline.py): invariants · Policy, contract, and reproducibility checks across the system. · `tests/test_pipeline.py`

## Connections

- Pipeline → Ingest: orchestrates · Coordinates the main ingestion-to-plan flow.
- Ingest → Provenance: tags lineage · Attaches source provenance to incoming records.
- Ingest → Indicators: normalizes · Prepares heterogeneous inputs for validation.
- Indicators → Deprivation: feeds · Validated indicators enter deprivation scoring.
- Deprivation → Modeling: explains · Deprivation outputs are decomposed into contributions.
- Modeling → Tiers: groups · Policy structure informs deterministic tiering.
- Tiers → Demand: compares · Tiered units are compared against demand signals.
- Demand → Divergence: diffs · Need and demand are contrasted for prioritization.
- Divergence → Works: filters · Gap analysis narrows candidate works.
- Works → Optimize: constrains · Eligible works are input to the constrained solver.
- Optimize → Audit: certifies · Selected plan is hashed and audited.
- Optimize → Watchdog: overlays · Compliance logic is applied to the plan.
- Watchdog → Brief: summarizes · Compliance context is folded into the command brief.
- Audit → Brief: attests · Audit evidence informs the final brief.
- Retrospective → Brief: enriches · Historic spend context feeds the synthesis layer.
- Living → Brief: enriches · Fresh scheme data informs the brief.
- Foresight → Brief: enriches · Weather timing context feeds the brief.
- Atlas → Brief: ranks · Cross-constituency ranking context feeds the brief.
- Intake AI → AI Flow: inputs · Citizen text enters the AI workflow.
- AI Flow → Letter AI: drafts · The orchestrator produces the sanction packet text.
- Pipeline → Retrospective: layers · Historical comparison sits above the core pipeline.
- Pipeline → Living: layers · Indicator refresh sits above the core pipeline.
- Pipeline → Foresight: layers · Weather sensitivity sits above the core pipeline.
- Pipeline → Atlas: layers · Cross-constituency ranking sits above the core pipeline.
- Pipeline → Brief: produces · The pipeline emits a synthesized command brief.
- App → Brief: serves · Local app surfaces the synthesized outputs.
- Dashboard → Brief: renders · Static dashboard presents the same plan artifacts.
- Frontend → Brief: renders · React UI consumes JanSetu outputs.
- Next App → Brief: renders · Next-style app consumes live and demo data.
- Next App → Pipeline: requests · API and page routes can pull generated data.
- Optimizer svc → Optimize: wraps · Cloud Run service exposes the exact solver.
- Scrapers → Ingest: supplies · Source collectors feed the ingestion boundary.
- Earth Engine → Indicators: supplies · Geospatial extraction contributes indicator inputs.
- Warehouse SQL → Deprivation: models · Warehouse transforms materialize deprivation data.
- Tests → Ingest: checks · Tests assert ingestion and provenance contracts.
- Tests → Optimize: checks · Tests enforce budget and earmark invariants.
- Tests → Audit: checks · Tests validate audit hashes and certificates.
- Tests → AI Flow: checks · Tests cover AI intake and letter generation contracts.
- Tests → Pipeline: checks · Tests protect end-to-end determinism.
