# souvikdevloper/closepilot architecture

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

- Interactive diagram: https://gitdiagram.com/souvikdevloper/closepilot
- Repository: https://github.com/souvikdevloper/closepilot

## Overview

#### Purpose and entry points
ClosePilot is a fail-closed finance reconciliation engine for payment/invoice, settlement/recon-item, and settlement/bank closure. It accepts batch JSON, produces explainable decisions, exceptions, metrics, and deterministic SHA-256 verification receipts; it never alters external financial systems.

Primary interfaces:
- Versioned evaluator API: `app/api/v1/reconcile/route.ts`
- Compatibility API: `app/api/reconcile/route.ts`
- Interactive Next.js dashboard: `app/page.tsx`, `app/closepilot-client.tsx`
- Public contract: `public/openapi.json`

#### Runtime and deployment boundary
The application is a TypeScript/Next.js service, intended for Vercel deployment (`.openai/hosting.json`). Route handlers execute reconciliation in-process rather than through a database, queue, or third-party payment API. Razorpay-shaped exports are input data only, not a live integration.

Operational endpoints expose health and reproducible benchmark data:
- `app/api/health/route.ts`
- `app/api/benchmark/route.ts`
- `lib/benchmark.ts`

#### API admission and bounded execution
`POST /api/v1/reconcile` is the authoritative batch boundary. It validates request shape, response mode, limits, and caller-supplied policy constraints before invoking the engine. Full responses are bounded separately from summary responses to control payload size; clients should shard large workloads by merchant, currency, and settlement date.

The UI intentionally calls this same versioned API rather than duplicating engine behavior:
- `app/api/v1/reconcile/route.ts`
- `app/closepilot-client.tsx`
- `lib/reconciliation.ts`

#### Normalization and financial safety gates
The reconciliation core normalizes records and represents money as exact integer minor units, avoiding floating-point authorization. It enforces identity uniqueness, currency and merchant scope, invoice validity, bank direction, and other hard rejection conditions before automatic release is possible.

Key utility and engine boundaries:
- `lib/reconciliation.ts`
- `lib/money.ts`

Invalid or ambiguous records remain explicit review/blocked exceptions rather than being silently dropped or force-matched.

#### Reconciliation stages and closure proofs
The engine performs bounded candidate generation and deterministic matching rather than Cartesian scans. Its primary closure flow is:
1. Match payments to invoices under one-to-one and amount/scope constraints.
2. Aggregate Razorpay settlement reconstruction credits/debits and require exact settlement-net closure.
3. Release settlement-to-bank matches only after the settlement proof closes and bank candidates satisfy exact-amount, direction, identity, threshold, and ambiguity rules.

All decision accounting, resolution metrics, per-currency totals, and optional ground-truth accuracy metrics are produced from the same execution:
- `lib/reconciliation.ts`
- `lib/money.ts`

#### Narration model as non-authoritative evidence
A local, inspectable Naive Bayes classifier interprets messy bank narrations into limited intents such as settlement or refund. Its output contributes bounded evidence only; deterministic facts and hard gates retain authorization authority.

- `lib/narration-classifier.ts`
- `lib/reconciliation.ts`

This separation prevents text classification from repairing amounts, identities, currency, merchant scope, or debit/credit direction.

#### Independent verification and reproducibility
After decisions are generated, a structurally independent verification pass checks match facts, one-to-one consumption, settlement proofs, accounting, and release thresholds. A SHA-256 receipt makes results reproducible and tamper-evident.

- `lib/reconciliation.ts`
- `lib/sha256.ts`

Synthetic benchmark generation and committed evaluation flows exercise this boundary at scale:
- `lib/benchmark.ts`
- `scripts/benchmark.ts`
- `examples/request.json`

Main entry points: [`lib/reconciliation.ts`](https://github.com/souvikdevloper/closepilot/blob/main/lib/reconciliation.ts) (Reconciliation core), [`app/api/v1/reconcile/route.ts`](https://github.com/souvikdevloper/closepilot/blob/main/app/api/v1/reconcile/route.ts) (Versioned reconcile API), [`lib/benchmark.ts`](https://github.com/souvikdevloper/closepilot/blob/main/lib/benchmark.ts) (Synthetic benchmark generator), [`app/closepilot-client.tsx`](https://github.com/souvikdevloper/closepilot/blob/main/app/closepilot-client.tsx) (Dashboard client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Interfaces"]
  node_dashboard["Next.js dashboard<br/>web UI<br/>[page.tsx]"]
  node_dashboard_client["Dashboard client<br/>API client"]
  node_v1_api["Versioned reconcile API<br/>route handler<br/>[route.ts]"]
  node_compat_api["Compatibility API<br/>route handler<br/>[route.ts]"]
  node_openapi["Public API contract<br/>OpenAPI document<br/>[openapi.json]"]
end

subgraph group_runtime["Runtime"]
  node_health["Health endpoint<br/>route handler<br/>[route.ts]"]
  node_benchmark_api["Benchmark endpoint<br/>route handler<br/>[route.ts]"]
end

subgraph group_engine["Reconciliation Engine"]
  node_reconciliation{{"Reconciliation core<br/>in-process engine<br/>[reconciliation.ts]"}}
  node_money["Exact money arithmetic<br/>financial utility<br/>[money.ts]"]
  node_narration["Narration classifier<br/>Naive Bayes evidence"]
end

subgraph group_assurance["Verification &amp; Evaluation"]
  node_verification{{"Independent verification<br/>verification pass<br/>[reconciliation.ts]"}}
  node_receipt["SHA-256 receipt<br/>reproducibility proof<br/>[sha256.ts]"]
  node_benchmark["Synthetic benchmark generator<br/>evaluation generator<br/>[benchmark.ts]"]
  node_benchmark_script["Benchmark runner<br/>script<br/>[benchmark.ts]"]
  node_example_request["Example batch request<br/>input fixture<br/>[request.json]"]
end

node_dashboard --> node_dashboard_client
node_dashboard_client -->|"POST batch"| node_v1_api
node_openapi -.->|"specifies"| node_v1_api
node_compat_api -->|"compatibility path"| node_v1_api
node_v1_api -->|"validated bounded request"| node_reconciliation
node_reconciliation -->|"exact amounts and totals"| node_money
node_reconciliation -->|"bounded narration evidence"| node_narration
node_reconciliation -->|"decisions and proofs"| node_verification
node_verification -->|"verified result hash"| node_receipt
node_benchmark_api -->|"reproducible workload"| node_benchmark
node_benchmark_script -->|"runs"| node_benchmark
node_example_request -.->|"evaluation input"| node_benchmark_script
node_benchmark -->|"synthetic batches"| node_reconciliation
node_health -.->|"same service runtime"| node_v1_api

click node_dashboard "https://github.com/souvikdevloper/closepilot/blob/main/app/page.tsx"
click node_dashboard_client "https://github.com/souvikdevloper/closepilot/blob/main/app/closepilot-client.tsx"
click node_v1_api "https://github.com/souvikdevloper/closepilot/blob/main/app/api/v1/reconcile/route.ts"
click node_compat_api "https://github.com/souvikdevloper/closepilot/blob/main/app/api/reconcile/route.ts"
click node_openapi "https://github.com/souvikdevloper/closepilot/blob/main/public/openapi.json"
click node_health "https://github.com/souvikdevloper/closepilot/blob/main/app/api/health/route.ts"
click node_benchmark_api "https://github.com/souvikdevloper/closepilot/blob/main/app/api/benchmark/route.ts"
click node_reconciliation "https://github.com/souvikdevloper/closepilot/blob/main/lib/reconciliation.ts"
click node_money "https://github.com/souvikdevloper/closepilot/blob/main/lib/money.ts"
click node_narration "https://github.com/souvikdevloper/closepilot/blob/main/lib/narration-classifier.ts"
click node_verification "https://github.com/souvikdevloper/closepilot/blob/main/lib/reconciliation.ts"
click node_receipt "https://github.com/souvikdevloper/closepilot/blob/main/lib/sha256.ts"
click node_benchmark "https://github.com/souvikdevloper/closepilot/blob/main/lib/benchmark.ts"
click node_benchmark_script "https://github.com/souvikdevloper/closepilot/blob/main/scripts/benchmark.ts"
click node_example_request "https://github.com/souvikdevloper/closepilot/blob/main/examples/request.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_dashboard,node_dashboard_client,node_v1_api,node_compat_api,node_openapi toneBlue
class node_health,node_benchmark_api toneAmber
class node_reconciliation,node_money,node_narration toneMint
class node_verification,node_receipt,node_benchmark,node_benchmark_script,node_example_request toneRose
```

## Components

### Interfaces

- [Next.js dashboard](https://github.com/souvikdevloper/closepilot/blob/main/app/page.tsx): web UI · Interactive client submits batches to the authoritative API. · `app/page.tsx`
- [Dashboard client](https://github.com/souvikdevloper/closepilot/blob/main/app/closepilot-client.tsx): API client · `app/closepilot-client.tsx`
- [Versioned reconcile API](https://github.com/souvikdevloper/closepilot/blob/main/app/api/v1/reconcile/route.ts): route handler · Authoritative bounded batch-admission boundary. · `app/api/v1/reconcile/route.ts`
- [Compatibility API](https://github.com/souvikdevloper/closepilot/blob/main/app/api/reconcile/route.ts): route handler · `app/api/reconcile/route.ts`
- [Public API contract](https://github.com/souvikdevloper/closepilot/blob/main/public/openapi.json): OpenAPI document · `public/openapi.json`

### Runtime

- [Health endpoint](https://github.com/souvikdevloper/closepilot/blob/main/app/api/health/route.ts): route handler · `app/api/health/route.ts`
- [Benchmark endpoint](https://github.com/souvikdevloper/closepilot/blob/main/app/api/benchmark/route.ts): route handler · `app/api/benchmark/route.ts`

### Reconciliation Engine

- [Reconciliation core](https://github.com/souvikdevloper/closepilot/blob/main/lib/reconciliation.ts): in-process engine · Normalizes inputs, applies hard gates, and runs deterministic closure stages. · `lib/reconciliation.ts`
- [Exact money arithmetic](https://github.com/souvikdevloper/closepilot/blob/main/lib/money.ts): financial utility · Integer minor-unit amounts and per-currency accounting. · `lib/money.ts`
- [Narration classifier](https://github.com/souvikdevloper/closepilot/blob/main/lib/narration-classifier.ts): Naive Bayes evidence · Provides non-authoritative bank narration intent evidence. · `lib/narration-classifier.ts`

### Verification & Evaluation

- [Independent verification](https://github.com/souvikdevloper/closepilot/blob/main/lib/reconciliation.ts): verification pass · Checks match facts, consumption, closure proofs, accounting, and thresholds. · `lib/reconciliation.ts`
- [SHA-256 receipt](https://github.com/souvikdevloper/closepilot/blob/main/lib/sha256.ts): reproducibility proof · Deterministic tamper-evident result receipt. · `lib/sha256.ts`
- [Synthetic benchmark generator](https://github.com/souvikdevloper/closepilot/blob/main/lib/benchmark.ts): evaluation generator · `lib/benchmark.ts`
- [Benchmark runner](https://github.com/souvikdevloper/closepilot/blob/main/scripts/benchmark.ts): script · `scripts/benchmark.ts`
- [Example batch request](https://github.com/souvikdevloper/closepilot/blob/main/examples/request.json): input fixture · `examples/request.json`

## Connections

- Next.js dashboard → Dashboard client
- Dashboard client → Versioned reconcile API: POST batch
- Public API contract → Versioned reconcile API: specifies
- Compatibility API → Versioned reconcile API: compatibility path
- Versioned reconcile API → Reconciliation core: validated bounded request
- Reconciliation core → Exact money arithmetic: exact amounts and totals
- Reconciliation core → Narration classifier: bounded narration evidence
- Reconciliation core → Independent verification: decisions and proofs
- Independent verification → SHA-256 receipt: verified result hash
- Benchmark endpoint → Synthetic benchmark generator: reproducible workload
- Benchmark runner → Synthetic benchmark generator: runs
- Example batch request → Benchmark runner: evaluation input
- Synthetic benchmark generator → Reconciliation core: synthetic batches
- Health endpoint → Versioned reconcile API: same service runtime
