File size: 17,994 Bytes
6dc9d46
 
 
 
 
696f787
 
 
6dc9d46
696f787
6dc9d46
 
 
 
 
 
696f787
6dc9d46
696f787
 
6dc9d46
696f787
6dc9d46
 
 
 
696f787
9659593
6dc9d46
 
 
 
 
 
 
 
9659593
6dc9d46
 
 
 
696f787
 
6dc9d46
 
696f787
 
6dc9d46
 
9659593
6dc9d46
 
696f787
 
6dc9d46
 
 
696f787
9659593
6dc9d46
696f787
6dc9d46
 
 
 
 
696f787
6dc9d46
9659593
 
 
 
696f787
6dc9d46
696f787
6dc9d46
 
 
 
 
 
696f787
6dc9d46
 
 
 
 
9659593
6dc9d46
9659593
6dc9d46
9659593
 
6dc9d46
 
 
 
9659593
6dc9d46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9659593
696f787
6dc9d46
 
 
 
 
 
 
 
 
 
696f787
6dc9d46
 
9659593
 
 
 
 
6dc9d46
696f787
6dc9d46
9659593
 
 
 
 
6dc9d46
696f787
6dc9d46
 
 
696f787
6dc9d46
 
9659593
 
 
 
 
6dc9d46
696f787
6dc9d46
9659593
 
 
 
 
6dc9d46
696f787
6dc9d46
 
 
9659593
6dc9d46
696f787
 
6dc9d46
 
696f787
6dc9d46
 
 
9659593
6dc9d46
 
 
 
 
 
 
 
696f787
6dc9d46
696f787
6dc9d46
9659593
6dc9d46
 
 
 
9659593
6dc9d46
 
 
 
 
 
9659593
6dc9d46
 
 
 
 
9659593
6dc9d46
 
 
 
 
 
9659593
6dc9d46
696f787
9659593
6dc9d46
 
 
 
9659593
6dc9d46
 
 
 
 
 
9659593
6dc9d46
 
 
 
 
9659593
6dc9d46
 
 
 
 
 
9659593
6dc9d46
696f787
9659593
6dc9d46
 
 
 
9659593
6dc9d46
 
 
 
 
9659593
 
6dc9d46
 
 
 
 
9659593
6dc9d46
 
 
 
 
9659593
 
6dc9d46
696f787
9659593
6dc9d46
 
 
 
9659593
6dc9d46
 
 
 
 
9659593
 
6dc9d46
 
 
 
 
9659593
6dc9d46
 
 
 
 
9659593
 
6dc9d46
696f787
6dc9d46
 
 
 
 
9659593
6dc9d46
 
 
 
 
9659593
 
6dc9d46
 
 
 
 
9659593
6dc9d46
 
 
 
 
9659593
 
6dc9d46
696f787
6dc9d46
696f787
6dc9d46
 
 
 
696f787
6dc9d46
 
 
 
9659593
696f787
6dc9d46
 
 
 
 
 
9659593
6dc9d46
 
 
 
 
696f787
6dc9d46
 
 
 
696f787
9659593
 
 
696f787
6dc9d46
696f787
6dc9d46
 
696f787
6dc9d46
 
696f787
6dc9d46
 
 
 
 
 
696f787
6dc9d46
 
9659593
6dc9d46
696f787
6dc9d46
ad2e847
9659593
6dc9d46
 
 
 
ad2e847
 
 
 
 
 
 
 
9659593
6dc9d46
696f787
6dc9d46
 
696f787
6dc9d46
 
9659593
 
 
6dc9d46
696f787
6dc9d46
9659593
696f787
9659593
6dc9d46
 
 
696f787
6dc9d46
 
 
696f787
6dc9d46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
MediGuard AI RAG-Helper - Evolution Engine
Outer Loop Director for SOP Evolution
"""

from collections.abc import Callable
from typing import Any, Literal

from pydantic import BaseModel, Field

from src.config import ExplanationSOP
from src.evaluation.evaluators import EvaluationResult


class SOPGenePool:
    """Manages version control for evolving SOPs"""

    def __init__(self):
        self.pool: list[dict[str, Any]] = []
        self.gene_pool: list[dict[str, Any]] = []  # Alias for compatibility
        self.version_counter = 0

    def add(
        self,
        sop: ExplanationSOP,
        evaluation: EvaluationResult,
        parent_version: int | None = None,
        description: str = "",
    ):
        """Add a new SOP to the gene pool"""
        self.version_counter += 1
        entry = {
            "version": self.version_counter,
            "sop": sop,
            "evaluation": evaluation,
            "parent": parent_version,
            "description": description,
        }
        self.pool.append(entry)
        self.gene_pool = self.pool  # Keep in sync
        print(f"✓ Added SOP v{self.version_counter} to gene pool: {description}")

    def get_latest(self) -> dict[str, Any] | None:
        """Get the most recent SOP"""
        return self.pool[-1] if self.pool else None

    def get_by_version(self, version: int) -> dict[str, Any] | None:
        """Retrieve specific SOP version"""
        for entry in self.pool:
            if entry["version"] == version:
                return entry
        return None

    def get_best_by_metric(self, metric: str) -> dict[str, Any] | None:
        """Get SOP with highest score on specific metric"""
        if not self.pool:
            return None

        best = max(self.pool, key=lambda x: getattr(x["evaluation"], metric).score)
        return best

    def summary(self):
        """Print summary of all SOPs in pool"""
        print("\n" + "=" * 80)
        print("SOP GENE POOL SUMMARY")
        print("=" * 80)

        for entry in self.pool:
            v = entry["version"]
            p = entry["parent"]
            desc = entry["description"]
            e = entry["evaluation"]

            parent_str = "(Baseline)" if p is None else f"(Child of v{p})"

            print(f"\nSOP v{v} {parent_str}: {desc}")
            print(f"  Clinical Accuracy:     {e.clinical_accuracy.score:.2f}")
            print(f"  Evidence Grounding:    {e.evidence_grounding.score:.2f}")
            print(f"  Actionability:         {e.actionability.score:.2f}")
            print(f"  Clarity:               {e.clarity.score:.2f}")
            print(f"  Safety & Completeness: {e.safety_completeness.score:.2f}")

        print("\n" + "=" * 80)


class Diagnosis(BaseModel):
    """Structured diagnosis from Performance Diagnostician"""

    primary_weakness: Literal[
        "clinical_accuracy", "evidence_grounding", "actionability", "clarity", "safety_completeness"
    ]
    root_cause_analysis: str = Field(description="Detailed analysis of why weakness occurred")
    recommendation: str = Field(description="High-level recommendation to fix the problem")


class SOPMutation(BaseModel):
    """Single mutated SOP with description"""

    description: str = Field(description="Brief description of mutation strategy")
    # SOP fields from ExplanationSOP
    biomarker_analyzer_threshold: float = 0.15
    disease_explainer_k: int = 5
    linker_retrieval_k: int = 3
    guideline_retrieval_k: int = 3
    explainer_detail_level: Literal["concise", "detailed", "comprehensive"] = "detailed"
    use_guideline_agent: bool = True
    include_alternative_diagnoses: bool = True
    require_pdf_citations: bool = True
    use_confidence_assessor: bool = True
    critical_value_alert_mode: Literal["strict", "moderate", "permissive"] = "strict"


class EvolvedSOPs(BaseModel):
    """Container for mutated SOPs from Architect"""

    mutations: list[SOPMutation]


def performance_diagnostician(evaluation: EvaluationResult) -> Diagnosis:
    """
    Analyzes 5D scores to identify primary weakness.
    Uses programmatic analysis for reliability and speed.
    """
    print("\n" + "=" * 70)
    print("EXECUTING: Performance Diagnostician")
    print("=" * 70)

    # Find lowest score programmatically (no LLM needed)
    scores = {
        "clinical_accuracy": evaluation.clinical_accuracy.score,
        "evidence_grounding": evaluation.evidence_grounding.score,
        "actionability": evaluation.actionability.score,
        "clarity": evaluation.clarity.score,
        "safety_completeness": evaluation.safety_completeness.score,
    }

    reasonings = {
        "clinical_accuracy": evaluation.clinical_accuracy.reasoning,
        "evidence_grounding": evaluation.evidence_grounding.reasoning,
        "actionability": evaluation.actionability.reasoning,
        "clarity": evaluation.clarity.reasoning,
        "safety_completeness": evaluation.safety_completeness.reasoning,
    }

    primary_weakness = min(scores, key=scores.get)
    weakness_score = scores[primary_weakness]
    weakness_reasoning = reasonings[primary_weakness]

    # Generate detailed root cause analysis
    root_cause_map = {
        "clinical_accuracy": f"Clinical accuracy score ({weakness_score:.2f}) indicates potential issues with medical interpretations. {weakness_reasoning[:200]}",
        "evidence_grounding": f"Evidence grounding score ({weakness_score:.2f}) suggests insufficient citations. {weakness_reasoning[:200]}",
        "actionability": f"Actionability score ({weakness_score:.2f}) indicates recommendations lack specificity. {weakness_reasoning[:200]}",
        "clarity": f"Clarity score ({weakness_score:.2f}) suggests readability issues. {weakness_reasoning[:200]}",
        "safety_completeness": f"Safety score ({weakness_score:.2f}) indicates missing risk discussions. {weakness_reasoning[:200]}",
    }

    recommendation_map = {
        "clinical_accuracy": "Increase RAG depth to access more authoritative medical sources.",
        "evidence_grounding": "Enforce strict citation requirements and increase RAG depth.",
        "actionability": "Make recommendations more specific with concrete action items.",
        "clarity": "Simplify language and reduce technical jargon for better readability.",
        "safety_completeness": "Add explicit safety warnings and ensure complete risk coverage.",
    }

    diagnosis = Diagnosis(
        primary_weakness=primary_weakness,
        root_cause_analysis=root_cause_map[primary_weakness],
        recommendation=recommendation_map[primary_weakness],
    )

    print("\n✓ Diagnosis complete")
    print(f"  Primary weakness: {diagnosis.primary_weakness} ({weakness_score:.3f})")
    print(f"  Recommendation: {diagnosis.recommendation}")

    return diagnosis


def sop_architect(diagnosis: Diagnosis, current_sop: ExplanationSOP) -> EvolvedSOPs:
    """
    Generates targeted SOP mutations to address diagnosed weakness.
    Uses programmatic generation for reliability.
    """
    print("\n" + "=" * 70)
    print("EXECUTING: SOP Architect")
    print("=" * 70)
    print(f"Target weakness: {diagnosis.primary_weakness}")

    weakness = diagnosis.primary_weakness

    # Generate mutations based on weakness type
    if weakness == "clarity":
        mut1 = SOPMutation(
            disease_explainer_k=max(3, current_sop.disease_explainer_k - 1),
            linker_retrieval_k=max(2, current_sop.linker_retrieval_k - 1),
            guideline_retrieval_k=max(2, current_sop.guideline_retrieval_k - 1),
            explainer_detail_level="concise",
            biomarker_analyzer_threshold=current_sop.biomarker_analyzer_threshold,
            use_guideline_agent=current_sop.use_guideline_agent,
            include_alternative_diagnoses=False,
            require_pdf_citations=current_sop.require_pdf_citations,
            use_confidence_assessor=current_sop.use_confidence_assessor,
            critical_value_alert_mode=current_sop.critical_value_alert_mode,
            description="Reduce retrieval depth and use concise style for clarity",
        )
        mut2 = SOPMutation(
            disease_explainer_k=current_sop.disease_explainer_k,
            linker_retrieval_k=current_sop.linker_retrieval_k,
            guideline_retrieval_k=current_sop.guideline_retrieval_k,
            explainer_detail_level="detailed",
            biomarker_analyzer_threshold=current_sop.biomarker_analyzer_threshold,
            use_guideline_agent=current_sop.use_guideline_agent,
            include_alternative_diagnoses=True,
            require_pdf_citations=False,
            use_confidence_assessor=current_sop.use_confidence_assessor,
            critical_value_alert_mode=current_sop.critical_value_alert_mode,
            description="Balanced detail with fewer citations for readability",
        )

    elif weakness == "evidence_grounding":
        mut1 = SOPMutation(
            disease_explainer_k=min(10, current_sop.disease_explainer_k + 2),
            linker_retrieval_k=min(5, current_sop.linker_retrieval_k + 1),
            guideline_retrieval_k=min(5, current_sop.guideline_retrieval_k + 1),
            explainer_detail_level="comprehensive",
            biomarker_analyzer_threshold=current_sop.biomarker_analyzer_threshold,
            use_guideline_agent=True,
            include_alternative_diagnoses=current_sop.include_alternative_diagnoses,
            require_pdf_citations=True,
            use_confidence_assessor=current_sop.use_confidence_assessor,
            critical_value_alert_mode=current_sop.critical_value_alert_mode,
            description="Maximum RAG depth with strict citation requirements",
        )
        mut2 = SOPMutation(
            disease_explainer_k=min(10, current_sop.disease_explainer_k + 1),
            linker_retrieval_k=current_sop.linker_retrieval_k,
            guideline_retrieval_k=current_sop.guideline_retrieval_k,
            explainer_detail_level="detailed",
            biomarker_analyzer_threshold=current_sop.biomarker_analyzer_threshold,
            use_guideline_agent=True,
            include_alternative_diagnoses=current_sop.include_alternative_diagnoses,
            require_pdf_citations=True,
            use_confidence_assessor=current_sop.use_confidence_assessor,
            critical_value_alert_mode=current_sop.critical_value_alert_mode,
            description="Moderate RAG increase with citation enforcement",
        )

    elif weakness == "actionability":
        mut1 = SOPMutation(
            disease_explainer_k=current_sop.disease_explainer_k,
            linker_retrieval_k=current_sop.linker_retrieval_k,
            guideline_retrieval_k=min(5, current_sop.guideline_retrieval_k + 2),
            explainer_detail_level="comprehensive",
            biomarker_analyzer_threshold=current_sop.biomarker_analyzer_threshold,
            use_guideline_agent=True,
            include_alternative_diagnoses=current_sop.include_alternative_diagnoses,
            require_pdf_citations=True,
            use_confidence_assessor=current_sop.use_confidence_assessor,
            critical_value_alert_mode="strict",
            description="Increase guideline retrieval for actionable recommendations",
        )
        mut2 = SOPMutation(
            disease_explainer_k=min(10, current_sop.disease_explainer_k + 1),
            linker_retrieval_k=min(5, current_sop.linker_retrieval_k + 1),
            guideline_retrieval_k=min(5, current_sop.guideline_retrieval_k + 1),
            explainer_detail_level="detailed",
            biomarker_analyzer_threshold=current_sop.biomarker_analyzer_threshold,
            use_guideline_agent=True,
            include_alternative_diagnoses=True,
            require_pdf_citations=True,
            use_confidence_assessor=True,
            critical_value_alert_mode="strict",
            description="Comprehensive approach with all agents enabled",
        )

    elif weakness == "clinical_accuracy":
        mut1 = SOPMutation(
            disease_explainer_k=10,
            linker_retrieval_k=5,
            guideline_retrieval_k=5,
            explainer_detail_level="comprehensive",
            biomarker_analyzer_threshold=max(0.10, current_sop.biomarker_analyzer_threshold - 0.05),
            use_guideline_agent=True,
            include_alternative_diagnoses=True,
            require_pdf_citations=True,
            use_confidence_assessor=True,
            critical_value_alert_mode="strict",
            description="Maximum RAG depth with strict thresholds for accuracy",
        )
        mut2 = SOPMutation(
            disease_explainer_k=min(10, current_sop.disease_explainer_k + 2),
            linker_retrieval_k=min(5, current_sop.linker_retrieval_k + 1),
            guideline_retrieval_k=min(5, current_sop.guideline_retrieval_k + 1),
            explainer_detail_level="comprehensive",
            biomarker_analyzer_threshold=current_sop.biomarker_analyzer_threshold,
            use_guideline_agent=True,
            include_alternative_diagnoses=True,
            require_pdf_citations=True,
            use_confidence_assessor=True,
            critical_value_alert_mode="strict",
            description="High RAG depth with comprehensive detail",
        )

    else:  # safety_completeness
        mut1 = SOPMutation(
            disease_explainer_k=min(10, current_sop.disease_explainer_k + 1),
            linker_retrieval_k=current_sop.linker_retrieval_k,
            guideline_retrieval_k=min(5, current_sop.guideline_retrieval_k + 2),
            explainer_detail_level="comprehensive",
            biomarker_analyzer_threshold=max(0.10, current_sop.biomarker_analyzer_threshold - 0.03),
            use_guideline_agent=True,
            include_alternative_diagnoses=True,
            require_pdf_citations=True,
            use_confidence_assessor=True,
            critical_value_alert_mode="strict",
            description="Strict safety mode with enhanced guidelines",
        )
        mut2 = SOPMutation(
            disease_explainer_k=min(10, current_sop.disease_explainer_k + 2),
            linker_retrieval_k=min(5, current_sop.linker_retrieval_k + 1),
            guideline_retrieval_k=min(5, current_sop.guideline_retrieval_k + 1),
            explainer_detail_level="comprehensive",
            biomarker_analyzer_threshold=current_sop.biomarker_analyzer_threshold,
            use_guideline_agent=True,
            include_alternative_diagnoses=True,
            require_pdf_citations=True,
            use_confidence_assessor=True,
            critical_value_alert_mode="strict",
            description="Maximum coverage with all safety features",
        )

    evolved = EvolvedSOPs(mutations=[mut1, mut2])

    print(f"\n✓ Generated {len(evolved.mutations)} mutations")
    for i, mut in enumerate(evolved.mutations, 1):
        print(f"  {i}. {mut.description}")
        print(f"     Disease K: {mut.disease_explainer_k}, Detail: {mut.explainer_detail_level}")

    return evolved


def run_evolution_cycle(
    gene_pool: SOPGenePool, patient_input: Any, workflow_graph: Any, evaluation_func: Callable
) -> list[dict[str, Any]]:
    """
    Executes one complete evolution cycle:
    1. Diagnose current best SOP
    2. Generate mutations
    3. Test each mutation
    4. Add to gene pool

    Returns: List of new entries added to pool
    """
    print("\n" + "=" * 80)
    print("STARTING EVOLUTION CYCLE")
    print("=" * 80)

    # Get current best (for simplicity, use latest)
    current_best = gene_pool.get_latest()
    if not current_best:
        raise ValueError("Gene pool is empty. Add baseline SOP first.")

    parent_sop = current_best["sop"]
    parent_eval = current_best["evaluation"]
    parent_version = current_best["version"]

    print(f"\nImproving upon SOP v{parent_version}")

    # Step 1: Diagnose
    diagnosis = performance_diagnostician(parent_eval)

    # Step 2: Generate mutations
    evolved_sops = sop_architect(diagnosis, parent_sop)

    # Step 3: Test each mutation
    new_entries = []
    for i, mutant_sop_model in enumerate(evolved_sops.mutations, 1):
        print(f"\n{'=' * 70}")
        print(f"TESTING MUTATION {i}/{len(evolved_sops.mutations)}: {mutant_sop_model.description}")
        print("=" * 70)

        # Convert SOPMutation to ExplanationSOP
        mutant_sop_dict = mutant_sop_model.model_dump()
        description = mutant_sop_dict.pop("description")
        mutant_sop = ExplanationSOP(**mutant_sop_dict)

        # Run workflow with mutated SOP
        from datetime import datetime

        graph_input = {
            "patient_biomarkers": patient_input.biomarkers,
            "model_prediction": patient_input.model_prediction,
            "patient_context": patient_input.patient_context,
            "plan": None,
            "sop": mutant_sop,
            "agent_outputs": [],
            "biomarker_flags": [],
            "safety_alerts": [],
            "biomarker_analysis": None,
            "final_response": None,
            "processing_timestamp": datetime.now().isoformat(),
            "sop_version": description,
        }

        try:
            final_state = workflow_graph.invoke(graph_input)

            # Evaluate output
            evaluation = evaluation_func(
                final_response=final_state["final_response"],
                agent_outputs=final_state["agent_outputs"],
                biomarkers=patient_input.biomarkers,
            )

            # Add to gene pool
            gene_pool.add(sop=mutant_sop, evaluation=evaluation, parent_version=parent_version, description=description)

            new_entries.append({"sop": mutant_sop, "evaluation": evaluation, "description": description})
        except Exception as e:
            print(f"❌ Mutation {i} failed: {e}")
            continue

    print("\n" + "=" * 80)
    print("EVOLUTION CYCLE COMPLETE")
    print("=" * 80)

    return new_entries