|
| 1 | +# Terraphim MedGemma Competition - Comprehensive Project Summary |
| 2 | + |
| 3 | +**Last Updated**: 2026-02-22 |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Terraphim MedGemma Competition is a Rust workspace implementing a **personalized medicine clinical decision support system** for the Google MedGemma Impact Challenge. It combines medical knowledge graphs (SNOMED CT, UMLS, PrimeKG), pharmacogenomic safety validation (CPIC), and Google's MedGemma foundation models through an Erlang/OTP-inspired multi-agent architecture. |
| 8 | + |
| 9 | +## Workspace Structure (11 crates) |
| 10 | + |
| 11 | +``` |
| 12 | +Cargo.toml (workspace root, edition 2021, MIT license) |
| 13 | +crates/ |
| 14 | + medgemma-client/ # MedGemma model inference (multi-backend) |
| 15 | + terraphim-automata/ # Entity extraction (Aho-Corasick, <50us) |
| 16 | + terraphim-kg/ # Knowledge graph (SNOMED CT + PrimeKG) |
| 17 | + terraphim-pgx/ # Pharmacogenomics (CPIC guidelines) |
| 18 | + terraphim-medical-agents/ # Multi-agent orchestration (OTP patterns) |
| 19 | + terraphim-api/ # REST API (Axum) |
| 20 | + terraphim-demo/ # Full clinical workflow demo |
| 21 | + terraphim-evaluation/ # Evaluation harness (10-case smoke suite) |
| 22 | + terraphim-thesaurus/ # Role-specific UMLS terminology |
| 23 | + terraphim-medical-learning/ # Case-based learning (anonymized) |
| 24 | + terraphim-medical-roles/ # Specialist role abstractions |
| 25 | +``` |
| 26 | + |
| 27 | +## Architecture |
| 28 | + |
| 29 | +### End-to-End Pipeline |
| 30 | + |
| 31 | +``` |
| 32 | +Clinical Text Input |
| 33 | + | |
| 34 | + v |
| 35 | +[Entity Extraction] -- terraphim-automata (Aho-Corasick, 48.9M UMLS terms) |
| 36 | + | |
| 37 | + v |
| 38 | +[Knowledge Graph Grounding] -- terraphim-kg (SNOMED CT + PrimeKG) |
| 39 | + | \ |
| 40 | + | [PGx Safety Check] -- terraphim-pgx (CPIC) |
| 41 | + v |
| 42 | +[MedGemma Inference] -- medgemma-client (local GGUF / HF / mock) |
| 43 | + | |
| 44 | + v |
| 45 | +[Safety Validation] -- SafetyValidationAgent (hard-gate) |
| 46 | + | |
| 47 | + v |
| 48 | +Treatment Recommendation Output |
| 49 | +``` |
| 50 | + |
| 51 | +### Multi-Agent System (terraphim-medical-agents) |
| 52 | + |
| 53 | +Erlang/OTP-inspired architecture with: |
| 54 | +- **6 Specialized Agents**: ClinicalReasoning, Imaging, Pharmacogenomics, KnowledgeGraph, TreatmentPlanning, SafetyValidation |
| 55 | +- **Messaging**: Mailboxes with at-least-once/at-most-once/exactly-once delivery |
| 56 | +- **Supervision**: OneForOne, OneForAll, RestForOne restart strategies |
| 57 | +- **Task Orchestration**: Topological sort for dependency-aware parallel execution |
| 58 | +- **Two-Layer Validation**: Coherence (graph connectivity) + Completeness (category coverage) |
| 59 | + |
| 60 | +### Clinical Pipeline Phases |
| 61 | +1. **Extraction**: Entity extraction from clinical text |
| 62 | +2. **Parallel Analysis**: KG grounding + Imaging + PGx (concurrent) |
| 63 | +3. **Synthesis**: Treatment plan generation |
| 64 | +4. **Validation**: Safety gate (hard-block authority) |
| 65 | + |
| 66 | +## Key Performance Characteristics |
| 67 | + |
| 68 | +| Component | Metric | Target | Achieved | |
| 69 | +|-----------|--------|--------|----------| |
| 70 | +| Entity Extraction | Latency | <2ms | <50us (40x better) | |
| 71 | +| Knowledge Graph | Ancestor query | <10ms | <1us (O(1) cache) | |
| 72 | +| KG Artifact Load | Startup | <500ms | <100ms | |
| 73 | +| UMLS Artifact Load | Startup | <1s | <100ms | |
| 74 | +| MedGemma (GPU) | Inference | <30s | 5-10s | |
| 75 | +| MedGemma (CPU Q4) | Inference | <60s | 30-60s | |
| 76 | + |
| 77 | +## Data Sources |
| 78 | + |
| 79 | +| Source | Size | Format | Purpose | |
| 80 | +|--------|------|--------|---------| |
| 81 | +| UMLS | 48.9M terms, 4.2M CUIs | TSV | Entity extraction | |
| 82 | +| SNOMED CT | 1.5M concepts, 4.5M descriptions | RF2 | Medical hierarchy | |
| 83 | +| PrimeKG | 100K+ nodes, 4M+ edges | CSV | Drug-disease relationships | |
| 84 | +| CPIC | Guidelines per gene-drug pair | JSON | Pharmacogenomic safety | |
| 85 | +| MedGemma GGUF | 2.3GB (Q4_K_M) | GGUF | LLM inference | |
| 86 | + |
| 87 | +## Crate Dependency Graph |
| 88 | + |
| 89 | +``` |
| 90 | +terraphim-api (HTTP REST) |
| 91 | + +-- terraphim-medical-agents (orchestration) |
| 92 | + | +-- medgemma-client (LLM inference) |
| 93 | + | +-- terraphim-pgx (PGx safety) |
| 94 | + | +-- terraphim-kg (knowledge graph) |
| 95 | + | +-- terraphim-automata (entity extraction) |
| 96 | + | +-- terraphim-medical-learning (case learning) |
| 97 | + | +-- terraphim-thesaurus (role terminology) |
| 98 | + | +-- terraphim-medical-roles (role abstractions) |
| 99 | + +-- terraphim-automata |
| 100 | + +-- terraphim-pgx |
| 101 | +
|
| 102 | +terraphim-demo (full workflow demo) |
| 103 | + +-- (all of the above + HTTP proxy) |
| 104 | +
|
| 105 | +terraphim-evaluation (testing harness) |
| 106 | + +-- generic gate interface |
| 107 | +``` |
| 108 | + |
| 109 | +## Individual Crate Summaries |
| 110 | + |
| 111 | +- [medgemma-client](summary-crates-medgemma-client.md) - Multi-backend MedGemma inference |
| 112 | +- [terraphim-automata](summary-crates-terraphim-automata.md) - Aho-Corasick entity extraction |
| 113 | +- [terraphim-kg](summary-crates-terraphim-kg.md) - SNOMED CT + PrimeKG knowledge graph |
| 114 | +- [terraphim-medical-agents](summary-crates-terraphim-medical-agents.md) - OTP-style multi-agent system |
| 115 | +- [terraphim-pgx](summary-crates-terraphim-pgx.md) - CPIC pharmacogenomics validator |
| 116 | +- [terraphim-api](summary-crates-terraphim-api.md) - Axum REST API |
| 117 | +- [terraphim-demo](summary-crates-terraphim-demo.md) - Clinical workflow demo |
| 118 | +- [terraphim-evaluation](summary-crates-terraphim-evaluation.md) - Evaluation harness |
| 119 | +- [terraphim-thesaurus](summary-crates-terraphim-thesaurus.md) - Role-specific UMLS terminology |
| 120 | +- [terraphim-medical-learning](summary-crates-terraphim-medical-learning.md) - Case-based learning |
| 121 | +- [terraphim-medical-roles](summary-crates-terraphim-medical-roles.md) - Specialist role abstractions |
| 122 | + |
| 123 | +## Key Architectural Patterns |
| 124 | + |
| 125 | +1. **Artifact-Based Loading**: One-time build of binary artifacts (bincode + zstd) for instant production startup |
| 126 | +2. **Sharded Aho-Corasick**: Overcomes 2M pattern limit by distributing across 59 automaton shards |
| 127 | +3. **OTP Supervision**: Erlang-style process management with restart strategies and intensity limits |
| 128 | +4. **Circuit Breaker Fallback**: Multiple inference backends with health-based routing |
| 129 | +5. **Two-Layer Validation**: Coherence + completeness ensures quality clinical responses |
| 130 | +6. **Safety Hard-Gate**: SafetyValidationAgent as non-bypassable final authority |
| 131 | +7. **Role-Based Confidence**: Different confidence thresholds per medical specialty |
| 132 | + |
| 133 | +## Technology Stack |
| 134 | + |
| 135 | +- **Language**: Rust (edition 2021) |
| 136 | +- **Async Runtime**: Tokio |
| 137 | +- **Web Framework**: Axum 0.7 |
| 138 | +- **Graph Library**: petgraph |
| 139 | +- **Pattern Matching**: daachorse (double-array Aho-Corasick) |
| 140 | +- **Serialization**: serde + bincode + zstd |
| 141 | +- **HTTP Client**: reqwest with rustls-tls |
| 142 | +- **LLM Inference**: Python subprocess (transformers) + llama-cpp-2 (planned) |
| 143 | +- **Python Bindings**: PyO3/maturin |
| 144 | +- **Testing**: criterion (benchmarks), built-in test framework |
| 145 | + |
| 146 | +## Documentation Structure |
| 147 | + |
| 148 | +``` |
| 149 | +.docs/ |
| 150 | + summary.md # This file |
| 151 | + summary-crates-*.md # Per-crate summaries |
| 152 | + define/ # Business scenarios, domain model |
| 153 | + design/ # Architecture, TDRs, implementation plans |
| 154 | + develop/ # Code architecture, roadmap, test plan |
| 155 | + discovery/ # Personas, risk scan, Wardley map, SOTA |
| 156 | + research/ # Competitive landscape, model evaluation |
| 157 | + validation/ # Phase 5 validation reports |
| 158 | + verification/ # Phase 4 verification reports |
| 159 | +docs/ |
| 160 | + ARCHITECTURE.md # Mermaid diagrams and API reference |
| 161 | + submission/technical-writeup.md |
| 162 | +``` |
0 commit comments