# rkjegan/razorpay_track_03_ghost_ledger architecture

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

- Interactive diagram: https://gitdiagram.com/rkjegan/razorpay_track_03_ghost_ledger
- Repository: https://github.com/rkjegan/razorpay_track_03_ghost_ledger

## Overview

Ghost Ledger is a bounded revenue-recovery agent: it generates and diagnoses failed-transaction data, gates recovery attempts with deterministic policy, selects payment-link or mandate retries, records decisions, and reports results. The main run also supports model training and held-out evaluation; autopsy prose is a separate, non-decision capability. The README documents the overall workflow and key guarantees; source excerpts and the SOURCE INDEX support the specific implementation relationships shown.

Main entry points: [`main.py`](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/main.py) (Pipeline runner), [`agents/recovery_base.py`](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/recovery_base.py) (Recovery sequence), [`metrics.py`](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/metrics.py) (Recovery metrics), [`database/db_client.py`](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/database/db_client.py) (SQLite store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data_diagnosis["Data and diagnosis"]
  node_generator["Synthetic data"]
  node_loader["Transaction seeding<br/>[seed_db.py]"]
  node_classifier["Root-cause classifier"]
  node_trainer["Model training<br/>[train.py]"]
  node_evaluator["Holdout evaluation<br/>[eval_holdout.py]"]
end

subgraph group_recovery["Recovery workflow"]
  node_pipeline["Pipeline runner<br/>[main.py]"]
  node_policy["Policy engine<br/>[policy_engine.py]"]
  node_sequence["Recovery sequence<br/>[recovery_base.py]"]
  node_payment_agent["Payment-link retry"]
  node_subscription_agent["Mandate retry"]
  node_razorpay["Razorpay client<br/>[razorpay_client.py]"]
end

subgraph group_records_reporting["Records and reporting"]
  node_audit["Audit trail<br/>[audit_trail.py]"]
  node_database[("SQLite store<br/>[db_client.py]")]
  node_metrics["Recovery metrics<br/>[metrics.py]"]
  node_dashboard["Recovery dashboard<br/>[app.py]"]
  node_precision_panel["Precision-recall panel"]
  node_weekly_report["Weekly recovery report<br/>[weekly_report.py]"]
end

subgraph group_explanation["Autopsy reporting"]
  node_autopsy["Autopsy reporter"]
  node_llm_service["LLM service"]
end

node_operator(("Pipeline operator"))

node_operator -->|"runs"| node_pipeline
node_pipeline -->|"generates data"| node_generator
node_generator -->|"seeds transactions"| node_loader
node_pipeline -->|"diagnoses failures"| node_classifier
node_trainer -->|"trains model"| node_classifier
node_evaluator -->|"evaluates predictions"| node_classifier
node_pipeline -->|"checks actions"| node_policy
node_pipeline -->|"dispatches payment retry"| node_payment_agent
node_pipeline -->|"dispatches mandate retry"| node_subscription_agent
node_payment_agent -->|"delegates recovery"| node_sequence
node_subscription_agent -->|"delegates recovery"| node_sequence
node_sequence -->|"evaluates attempt"| node_policy
node_sequence -->|"executes retry"| node_razorpay
node_sequence -->|"logs decisions"| node_audit
node_pipeline -->|"records decisions"| node_audit
node_audit -->|"writes records"| node_database
node_razorpay -.->|"records outcomes"| node_database
node_pipeline -->|"generates explanations"| node_autopsy
node_autopsy -->|"requests prose"| node_llm_service
node_autopsy -->|"stores reports"| node_database
node_pipeline -->|"computes results"| node_metrics
node_metrics -->|"queries records"| node_database
node_dashboard -->|"reads metrics"| node_metrics
node_dashboard -->|"renders evaluation"| node_precision_panel
node_weekly_report -->|"reads recovery figures"| node_metrics

click node_pipeline "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/main.py"
click node_generator "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/data/synthetic_generator.py"
click node_loader "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/database/seed_db.py"
click node_classifier "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/diagnoser/root_cause_classifier.py"
click node_trainer "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/diagnoser/train.py"
click node_evaluator "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/diagnoser/eval_holdout.py"
click node_policy "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/policy_engine.py"
click node_sequence "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/recovery_base.py"
click node_payment_agent "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/payment_failure_agent.py"
click node_subscription_agent "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/subscription_agent.py"
click node_razorpay "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/api/razorpay_client.py"
click node_audit "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/database/audit_trail.py"
click node_database "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/database/db_client.py"
click node_metrics "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/metrics.py"
click node_dashboard "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/dashboard/app.py"
click node_precision_panel "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/dashboard/components/precision_recall_panel.py"
click node_autopsy "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/autopsy_reporter.py"
click node_weekly_report "https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/weekly_report.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_generator,node_loader,node_classifier,node_trainer,node_evaluator toneBlue
class node_pipeline,node_policy,node_sequence,node_payment_agent,node_subscription_agent,node_razorpay toneAmber
class node_audit,node_database,node_metrics,node_dashboard,node_precision_panel,node_weekly_report toneMint
class node_autopsy,node_llm_service toneRose
class node_operator toneIndigo
```

## Components

### Data and diagnosis

- [Synthetic data](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/data/synthetic_generator.py): `data/synthetic_generator.py`
- [Transaction seeding](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/database/seed_db.py): `database/seed_db.py`
- [Root-cause classifier](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/diagnoser/root_cause_classifier.py): `diagnoser/root_cause_classifier.py`
- [Model training](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/diagnoser/train.py): `diagnoser/train.py`
- [Holdout evaluation](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/diagnoser/eval_holdout.py): `diagnoser/eval_holdout.py`

### Recovery workflow

- [Pipeline runner](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/main.py): `main.py`
- [Policy engine](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/policy_engine.py): `agents/policy_engine.py`
- [Recovery sequence](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/recovery_base.py): `agents/recovery_base.py`
- [Payment-link retry](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/payment_failure_agent.py): `agents/payment_failure_agent.py`
- [Mandate retry](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/subscription_agent.py): `agents/subscription_agent.py`
- [Razorpay client](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/api/razorpay_client.py): `api/razorpay_client.py`

### Records and reporting

- [Audit trail](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/database/audit_trail.py): `database/audit_trail.py`
- [SQLite store](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/database/db_client.py): `database/db_client.py`
- [Recovery metrics](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/metrics.py): `metrics.py`
- [Recovery dashboard](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/dashboard/app.py): `dashboard/app.py`
- [Precision-recall panel](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/dashboard/components/precision_recall_panel.py): `dashboard/components/precision_recall_panel.py`
- [Weekly recovery report](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/weekly_report.py): `weekly_report.py`

### Autopsy reporting

- [Autopsy reporter](https://github.com/rkjegan/razorpay_track_03_ghost_ledger/blob/main/agents/autopsy_reporter.py): `agents/autopsy_reporter.py`
- **LLM service**

### Other components

- **Pipeline operator**

## Connections

- Pipeline operator → Pipeline runner: runs (evidence: `README.md`)
- Pipeline runner → Synthetic data: generates data (evidence: `main.py`)
- Synthetic data → Transaction seeding: seeds transactions (evidence: `data/synthetic_generator.py`)
- Pipeline runner → Root-cause classifier: diagnoses failures (evidence: `README.md`)
- Model training → Root-cause classifier: trains model (evidence: `diagnoser/train.py`)
- Holdout evaluation → Root-cause classifier: evaluates predictions (evidence: `diagnoser/eval_holdout.py`)
- Pipeline runner → Policy engine: checks actions (evidence: `README.md`)
- Pipeline runner → Payment-link retry: dispatches payment retry (evidence: `main.py`)
- Pipeline runner → Mandate retry: dispatches mandate retry (evidence: `main.py`)
- Payment-link retry → Recovery sequence: delegates recovery (evidence: `agents/payment_failure_agent.py`)
- Mandate retry → Recovery sequence: delegates recovery (evidence: `agents/subscription_agent.py`)
- Recovery sequence → Policy engine: evaluates attempt (evidence: `agents/recovery_base.py`)
- Recovery sequence → Razorpay client: executes retry (evidence: `README.md`)
- Recovery sequence → Audit trail: logs decisions (evidence: `agents/recovery_base.py`)
- Pipeline runner → Audit trail: records decisions (evidence: `README.md`)
- Audit trail → SQLite store: writes records (evidence: `database/audit_trail.py`)
- Razorpay client → SQLite store: records outcomes
- Pipeline runner → Autopsy reporter: generates explanations (evidence: `main.py`)
- Autopsy reporter → LLM service: requests prose (evidence: `agents/autopsy_reporter.py`)
- Autopsy reporter → SQLite store: stores reports (evidence: `agents/autopsy_reporter.py`)
- Pipeline runner → Recovery metrics: computes results (evidence: `main.py`)
- Recovery metrics → SQLite store: queries records (evidence: `metrics.py`)
- Recovery dashboard → Recovery metrics: reads metrics (evidence: `dashboard/app.py`)
- Recovery dashboard → Precision-recall panel: renders evaluation (evidence: `dashboard/app.py`)
- Weekly recovery report → Recovery metrics: reads recovery figures (evidence: `weekly_report.py`)
