How souvikdevloper/closepilot works
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: reconciliation.ts Reconciliation core · route.ts Versioned reconcile API · benchmark.ts Synthetic benchmark generator · closepilot-client.tsx Dashboard client
Components 15
Interfaces
- Next.js dashboard · Interactive client submits batches to the authoritative API.
app/page.tsx - Dashboard client
app/closepilot-client.tsx - Versioned reconcile API · Authoritative bounded batch-admission boundary.
app/api/v1/reconcile/route.ts - Compatibility API
app/api/reconcile/route.ts - Public API contract
public/openapi.json
Runtime
- Health endpoint
app/api/health/route.ts - Benchmark endpoint
app/api/benchmark/route.ts
Reconciliation Engine
- Reconciliation core · Normalizes inputs, applies hard gates, and runs deterministic closure stages.
lib/reconciliation.ts - Exact money arithmetic · Integer minor-unit amounts and per-currency accounting.
lib/money.ts - Narration classifier · Provides non-authoritative bank narration intent evidence.
lib/narration-classifier.ts
Verification & Evaluation
- Independent verification · Checks match facts, consumption, closure proofs, accounting, and thresholds.
lib/reconciliation.ts - SHA-256 receipt · Deterministic tamper-evident result receipt.
lib/sha256.ts - Synthetic benchmark generator
lib/benchmark.ts - Benchmark runner
scripts/benchmark.ts - Example batch request
examples/request.json
Connections 14
- 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