Inspiration
Frontline healthcare in Bangladesh faces two coupled failures: 92.4% of surveyed community pharmacy staff dispense antibiotics without a prescription (PMC12778233), while ~50% of public ICU beds sit unstaffed (New Age, Apr 2026) and 74.5% of maternal and perinatal deaths in LMIC cohorts involve two or more referrals (J Glob Health 2026). Ambulances routinely arrive at hospitals missing a staffed ICU bed, ventilator, or compatible blood — or lose the final bed to a concurrent booking.
We built ShasthoGrid so an AI-assisted clinical system never acts like an unverified chatbot: every messy Bangla/English intake is scrubbed, checked by deterministic solvers, gated by RBAC and human sign-off, and backed by race-proof database transactions.
What it does
- Shastho Copilot: Converts unstructured English, Bangla, and Banglish paramedic notes into structured vitals and constraints (
POST /api/v1/copilot/triage-assist), scrubs PII before external model calls, blocks prompt injection (422 PROMPT_INJECTION_BLOCKED), and flags missing vitals without ever inventing numbers. - CareMatch + ConflictProof (OR-Tools CP-SAT): Matches patients to hospitals with staffed ICU beds, ventilators, specialties, ABO/Rh-compatible blood, and OSRM/Haversine ETA. When no single hospital satisfies every rule, ConflictProof computes exact Minimal Unsatisfiable Subsets (MUS) and verified repairs (e.g., split bed + blood courier co-dispatch) without ever relaxing clinical safety constraints (
C_ICU,C_VENT,C_SPEC,C_BLOOD). - LockStep + RaceLab: Reserves bed + ventilator + blood in a single ACID transaction with deterministic global lock ordering (
reservations -> hospital_resources -> blood_inventories, sorted by ID),pg_advisory_xact_lockidempotency, DBCHECKconstraints, and a 30-secondSKIP LOCKEDexpiry sweeper. RaceLab proves zero double-bookings under 25 simultaneous requests (1 x 201 Created,24 x 409 Conflict). - RxSolve + FreshStock + SyncSafe: Offline-first WHO AWaRe antibiotic stewardship (
>=60%Access target) with FEFO batch selection, pharmacist approval gates for Watch/Reserve drugs, Dexie IndexedDB offline queue, and an explicit HTTP409conflict-resolution workbench. - CareRadar 3D & StewardLens: 3D hospital, pharmacy, and blood-bank locator (
/radar) plus clinic/regional stewardship analytics (/dashboard/analytics,/insights) with de-identified CSV and Lakehouse NDJSON exports.
Decision Map (4 Typed Decisions)
- Unstructured Note -> Structured Intake (
CopilotIn->Extraction): Hybrid LLM (Groq -> Gemini) + deterministic EN/BN/Banglish parser. Selected because phrasing varies across languages, while deterministic guards reject any hallucinated vital not literally present in the note (guard_rejected_fields). - Acuity & Blood Compatibility (
News2In->News2Out): Deterministic RCP NEWS2 (Scales 1 & 2) and 8x8 ABO/Rh rules. Selected because physiological scoring and blood compatibility must be 100% deterministic. - Hospital/Blood & Antibiotic Matching (
CareRequest->CareMatchOut/SolverResult): OR-Tools CP-SAT 3-pass solver + exact MUS enumeration. Selected for provable optimality, exact conflict explanations, and unbreakable safety invariants. - Bed/Blood Hold & Sync Conflict Merge (
ReservationCreateIn->ReservationOut): PostgreSQLFOR UPDATErow locks +Idempotency-Keyadvisory locks. Selected to guarantee zero double-bookings under concurrency.
Accountability Layer
- Data Sources & Freshness: Every capacity count tracks
data_source(SEED_DEMO,STAFF,FEED) andreported_at(LIVE <= 6h,STALE > 6h,DEMO). Production CareMatch blocks simulated demo facilities (503 DEMO_DATA_BLOCKED_IN_PRODUCTION) unless explicitly permitted. - Permissions & Rate Limiting: 7 RBAC roles with clinic/facility BOLA isolation (
403/404+audit_logsentries), scoped SHA-256sgf_feed keys, and fail-closed sensitive rate limiting (503 RATE_LIMITER_UNAVAILABLE) when Redis is unreachable. - Human Escalation & Undo: Every Copilot run sets
requires_human_approval: true; Watch/Reserve prescriptions require pharmacist sign-off (PENDING_APPROVAL); holds can beCANCELLEDorEXPIRED(restoring stock), dispenses can beVOIDED, and offline409conflicts are resolved explicitly by staff.
How we built it
- Backend: FastAPI, Python 3.12, SQLAlchemy Async, PostgreSQL 16, Alembic migrations, Redis / Upstash, OR-Tools CP-SAT, Groq & Gemini APIs.
- Frontend: Next.js 16, React 19, TypeScript, Tailwind CSS, Serwist PWA, Dexie (IndexedDB), MapLibre GL, Playwright + Axe (
wcag22aa).
Challenges & How We Catch Incorrect AI Decisions
A key challenge was preventing an LLM from "smoothing over" an emergency by inventing normal vitals or recommending antibiotics disallowed by the solver.
- Recorded Interception Example (
test_copilot_uses_llm_but_guards_invented_numbers): Given a Farmgate obstetric emergency note (BP 84,SpO2 89), a mocked LLM hallucinates normal vitals (spo2: 99,systolic_bp: 120).guard_against_invented_numbersverifies every numeric output against the scrubbed note, rejects{"spo2", "systolic_bp"}intoguard_rejected_fields, and restoressystolic_bp: 84andspo2: 89from the deterministic parser — preserving the true high-acuity dispatch decision.
Testing Evidence
- 174 passing backend tests (
pytest), covering CP-SAT MUS minimality, 25-way bed contention (test_25_concurrent_requests_for_the_final_bed), opposing split-dispatch deadlock freedom, prompt-injection blocking (422), PII scrubbing, cross-clinic isolation (404), production demo gate (503), and Redis fail-closed (503). - Playwright + Axe WCAG 2.2 AA audit: All 9 primary routes pass with 0 critical/serious violations and 0 horizontal overflow at
390x844and1440x900.
What's next
- Connect live DGHS / hospital HIS feeds via scoped
sgf_keys in FacilityDesk. - Add automated
axechecks and dependency scanning into every pull request via GitHub Actions.
Built With
- alembic
- dexie
- docker
- fastapi
- google-gemini
- groq
- maplibre-gl
- nextjs
- openstreetmap
- or-tools
- playwright
- postgresql
- python
- react
- redis
- sqlalchemy
- tailwindcss
- typescript
- upstash
