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 
 
 
 
 
 
7c07ade
 
 
 
d0b14c0
7c07ade
d0b14c0
 
 
 
 
62d32ab
d0b14c0
 
 
 
 
 
 
7ecca95
 
 
d0b14c0
7ecca95
 
 
d0b14c0
 
 
 
 
 
 
 
7ecca95
 
d0b14c0
7ecca95
d0b14c0
 
7ecca95
d0b14c0
 
 
 
 
7ecca95
d0b14c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
# Phase 4 Implementation Spec: Orchestrator & UI

**Goal**: Connect the Brain and the Body, then give it a Face.
**Philosophy**: "Streaming is Trust."
**Estimated Effort**: 4-5 hours
**Prerequisite**: Phases 1-3 complete

---

## 1. The Slice Definition

This slice connects:
1. **Orchestrator**: The main loop calling `SearchHandler` β†’ `JudgeHandler`.
2. **Synthesis**: Generate a final markdown report.
3. **UI**: Gradio streaming chat interface.
4. **Deployment**: Dockerfile + HuggingFace Spaces config.

**Files**:
- `src/utils/models.py`: Add AgentState, AgentEvent
- `src/orchestrator.py`: Main agent loop
- `src/app.py`: Gradio UI
- `Dockerfile`: Container build
- `README.md`: HuggingFace Space config (at root)

---

## 2. Models (`src/utils/models.py`)

Add these to the existing models file (after JudgeAssessment):

```python
# Add to src/utils/models.py (after JudgeAssessment class)

from enum import Enum
from typing import Any


class AgentState(str, Enum):
    """States of the agent during execution."""

    INITIALIZING = "initializing"
    SEARCHING = "searching"
    JUDGING = "judging"
    SYNTHESIZING = "synthesizing"
    COMPLETE = "complete"
    ERROR = "error"


class AgentEvent(BaseModel):
    """An event emitted during agent execution (for streaming UI)."""

    state: AgentState = Field(description="Current agent state")
    message: str = Field(description="Human-readable status message")
    iteration: int = Field(default=0, ge=0, description="Current iteration number")
    data: dict[str, Any] | None = Field(
        default=None,
        description="Optional payload (e.g., evidence count, assessment scores)"
    )

    def to_display(self) -> str:
        """Format for UI display."""
        icon = {
            AgentState.INITIALIZING: "πŸ”„",
            AgentState.SEARCHING: "πŸ”",
            AgentState.JUDGING: "βš–οΈ",
            AgentState.SYNTHESIZING: "πŸ“",
            AgentState.COMPLETE: "βœ…",
            AgentState.ERROR: "❌",
        }.get(self.state, "▢️")
        return f"{icon} **[{self.state.value.upper()}]** {self.message}"


class AgentResult(BaseModel):
    """Final result from the agent."""

    question: str = Field(description="The original research question")
    report: str = Field(description="The synthesized markdown report")
    evidence_count: int = Field(description="Total evidence items collected")
    iterations: int = Field(description="Number of search iterations")
    candidates: list["DrugCandidate"] = Field(
        default_factory=list,
        description="Drug candidates identified"
    )
    quality_score: int = Field(default=0, description="Final quality score")
```

---

## 3. Orchestrator (`src/orchestrator.py`)

```python
"""Main agent orchestrator - coordinates Search β†’ Judge β†’ Synthesize loop."""
import structlog
from typing import AsyncGenerator
from pydantic_ai import Agent

from src.utils.config import settings
from src.utils.exceptions import DeepCriticalError
from src.utils.models import (
    AgentEvent,
    AgentState,
    AgentResult,
    Evidence,
    JudgeAssessment,
)
from src.tools.pubmed import PubMedTool
from src.tools.websearch import WebTool
from src.tools.search_handler import SearchHandler
from src.agent_factory.judges import JudgeHandler
from src.prompts.judge import build_synthesis_prompt

logger = structlog.get_logger()


def _get_model_string() -> str:
    """Get the PydanticAI model string from settings."""
    provider = settings.llm_provider
    model = settings.llm_model
    if ":" in model:
        return model
    return f"{provider}:{model}"


# Synthesis agent for generating the final report
synthesis_agent = Agent(
    model=_get_model_string(),
    result_type=str,
    system_prompt="""You are a biomedical research report writer.
Generate comprehensive, well-structured markdown reports on drug repurposing research.
Include citations, mechanisms of action, and recommendations.
Be objective and scientific.""",
)


class Orchestrator:
    """Main orchestrator for the DeepCritical agent."""

    def __init__(
        self,
        search_handler: SearchHandler | None = None,
        judge_handler: JudgeHandler | None = None,
        max_iterations: int | None = None,
    ):
        """Initialize the orchestrator.

        Args:
            search_handler: Optional SearchHandler (for testing).
            judge_handler: Optional JudgeHandler (for testing).
            max_iterations: Max search iterations (default from settings).
        """
        self.search_handler = search_handler or SearchHandler([
            PubMedTool(),
            WebTool(),
        ])
        self.judge_handler = judge_handler or JudgeHandler()
        self.max_iterations = max_iterations or settings.max_iterations

    async def run(self, question: str) -> AsyncGenerator[AgentEvent, None]:
        """Run the agent loop, yielding events for streaming UI.

        Args:
            question: The research question to investigate.

        Yields:
            AgentEvent objects for each state change.
        """
        logger.info("orchestrator_starting", question=question[:100])

        # Track state
        all_evidence: list[Evidence] = []
        iteration = 0
        last_assessment: JudgeAssessment | None = None

        try:
            # Initial event
            yield AgentEvent(
                state=AgentState.INITIALIZING,
                message=f"Starting research on: {question[:100]}...",
                iteration=0,
            )

            # Main search β†’ judge loop
            while iteration < self.max_iterations:
                iteration += 1

                # === SEARCH PHASE ===
                yield AgentEvent(
                    state=AgentState.SEARCHING,
                    message=f"Searching (iteration {iteration}/{self.max_iterations})...",
                    iteration=iteration,
                )

                # Determine search query
                if last_assessment and last_assessment.next_search_queries:
                    # Use judge's suggested queries
                    search_query = last_assessment.next_search_queries[0]
                else:
                    # Use original question
                    search_query = question

                # Execute search
                search_result = await self.search_handler.execute(
                    search_query,
                    max_results_per_tool=10,
                )

                # Accumulate evidence (deduplicate by URL)
                existing_urls = {e.citation.url for e in all_evidence}
                new_evidence = [
                    e for e in search_result.evidence
                    if e.citation.url not in existing_urls
                ]
                all_evidence.extend(new_evidence)

                yield AgentEvent(
                    state=AgentState.SEARCHING,
                    message=f"Found {len(new_evidence)} new items ({len(all_evidence)} total)",
                    iteration=iteration,
                    data={
                        "new_count": len(new_evidence),
                        "total_count": len(all_evidence),
                        "sources": search_result.sources_searched,
                    },
                )

                # === JUDGE PHASE ===
                yield AgentEvent(
                    state=AgentState.JUDGING,
                    message="Evaluating evidence quality...",
                    iteration=iteration,
                )

                last_assessment = await self.judge_handler.assess(
                    question,
                    all_evidence[-20:],  # Evaluate most recent 20 items
                )

                yield AgentEvent(
                    state=AgentState.JUDGING,
                    message=(
                        f"Quality: {last_assessment.overall_quality_score}/10, "
                        f"Coverage: {last_assessment.coverage_score}/10"
                    ),
                    iteration=iteration,
                    data={
                        "quality_score": last_assessment.overall_quality_score,
                        "coverage_score": last_assessment.coverage_score,
                        "sufficient": last_assessment.sufficient,
                        "candidates": len(last_assessment.candidates),
                    },
                )

                # Check if we should stop
                if not await self.judge_handler.should_continue(last_assessment):
                    logger.info(
                        "orchestrator_sufficient_evidence",
                        iteration=iteration,
                        evidence_count=len(all_evidence),
                    )
                    break

                # Log why we're continuing
                if last_assessment.gaps:
                    logger.info(
                        "orchestrator_continuing",
                        gaps=last_assessment.gaps[:3],
                        next_query=last_assessment.next_search_queries[:1],
                    )

            # === SYNTHESIS PHASE ===
            yield AgentEvent(
                state=AgentState.SYNTHESIZING,
                message="Generating research report...",
                iteration=iteration,
            )

            report = await self._synthesize_report(
                question,
                all_evidence,
                last_assessment,
            )

            # === COMPLETE ===
            yield AgentEvent(
                state=AgentState.COMPLETE,
                message="Research complete!",
                iteration=iteration,
                data={
                    "evidence_count": len(all_evidence),
                    "candidates": (
                        len(last_assessment.candidates) if last_assessment else 0
                    ),
                    "report_length": len(report),
                },
            )

            # Yield final report as special event
            yield AgentEvent(
                state=AgentState.COMPLETE,
                message=report,  # The report itself
                iteration=iteration,
                data={"is_report": True},
            )

        except Exception as e:
            logger.error("orchestrator_error", error=str(e))
            yield AgentEvent(
                state=AgentState.ERROR,
                message=f"Error: {str(e)}",
                iteration=iteration,
            )
            raise DeepCriticalError(f"Orchestrator failed: {e}") from e

    async def _synthesize_report(
        self,
        question: str,
        evidence: list[Evidence],
        assessment: JudgeAssessment | None,
    ) -> str:
        """Generate the final research report.

        Args:
            question: The research question.
            evidence: All collected evidence.
            assessment: The final judge assessment.

        Returns:
            Markdown formatted report.
        """
        if not assessment:
            # Fallback assessment
            assessment = JudgeAssessment(
                sufficient=True,
                recommendation="synthesize",
                reasoning="Manual synthesis requested.",
                overall_quality_score=5,
                coverage_score=5,
            )

        # Build synthesis prompt
        prompt = build_synthesis_prompt(question, assessment, evidence)

        # Generate report
        result = await synthesis_agent.run(prompt)

        return result.data

    async def run_to_completion(self, question: str) -> AgentResult:
        """Run the agent and return final result (non-streaming).

        Args:
            question: The research question.

        Returns:
            AgentResult with report and metadata.
        """
        report = ""
        evidence_count = 0
        iterations = 0
        candidates = []
        quality_score = 0

        async for event in self.run(question):
            iterations = event.iteration
            if event.data:
                if event.data.get("is_report"):
                    report = event.message
                if "evidence_count" in event.data:
                    evidence_count = event.data["evidence_count"]
                if "candidates" in event.data:
                    candidates = event.data.get("candidates", [])
                if "quality_score" in event.data:
                    quality_score = event.data["quality_score"]

        return AgentResult(
            question=question,
            report=report,
            evidence_count=evidence_count,
            iterations=iterations,
            candidates=candidates,
            quality_score=quality_score,
        )
```

---

## 4. UI (`src/app.py`)

```python
"""Gradio UI for DeepCritical agent."""
import gradio as gr
from typing import AsyncGenerator

from src.orchestrator import Orchestrator
from src.utils.models import AgentEvent, AgentState


async def chat(
    message: str,
    history: list[list[str]],
) -> AsyncGenerator[str, None]:
    """Process a chat message and stream responses.

    Args:
        message: User's research question.
        history: Chat history (not used, fresh agent each time).

    Yields:
        Streaming response text.
    """
    if not message.strip():
        yield "Please enter a research question."
        return

    orchestrator = Orchestrator()
    full_response = ""

    try:
        async for event in orchestrator.run(message):
            # Format event for display
            if event.data and event.data.get("is_report"):
                # Final report - yield as-is
                full_response = event.message
                yield full_response
            else:
                # Status update
                status = event.to_display()
                full_response += f"\n{status}"
                yield full_response

    except Exception as e:
        yield f"\n❌ **Error**: {str(e)}"


def create_app() -> gr.Blocks:
    """Create the Gradio application.

    Returns:
        Configured Gradio Blocks app.
    """
    with gr.Blocks(
        title="DeepCritical - Drug Repurposing Research Agent",
        theme=gr.themes.Soft(),
    ) as app:
        gr.Markdown(
            """
            # 🧬 DeepCritical
            ## AI-Powered Drug Repurposing Research Agent

            Enter a research question about drug repurposing to get started.
            The agent will search PubMed and the web, evaluate evidence quality,
            and generate a comprehensive research report.

            **Example questions:**
            - "Can metformin be repurposed to treat Alzheimer's disease?"
            - "What existing drugs might help treat long COVID fatigue?"
            - "Are there diabetes drugs that could treat Parkinson's?"
            """
        )

        chatbot = gr.Chatbot(
            label="Research Assistant",
            height=600,
            show_copy_button=True,
            render_markdown=True,
        )

        msg = gr.Textbox(
            label="Research Question",
            placeholder="e.g., Can metformin be repurposed to treat Alzheimer's disease?",
            lines=2,
            max_lines=5,
        )

        with gr.Row():
            submit_btn = gr.Button("πŸ”¬ Research", variant="primary")
            clear_btn = gr.Button("πŸ—‘οΈ Clear")

        # Examples
        gr.Examples(
            examples=[
                "Can metformin be repurposed to treat Alzheimer's disease?",
                "What existing drugs might help treat long COVID fatigue?",
                "Are there cancer drugs that could treat autoimmune diseases?",
                "Can diabetes medications help with heart failure?",
            ],
            inputs=msg,
        )

        # Event handlers
        async def respond(message: str, chat_history: list):
            """Handle user message and stream response."""
            chat_history = chat_history or []
            chat_history.append([message, ""])

            async for response in chat(message, chat_history):
                chat_history[-1][1] = response
                yield "", chat_history

        submit_btn.click(
            respond,
            inputs=[msg, chatbot],
            outputs=[msg, chatbot],
        )

        msg.submit(
            respond,
            inputs=[msg, chatbot],
            outputs=[msg, chatbot],
        )

        clear_btn.click(lambda: (None, []), outputs=[msg, chatbot])

        gr.Markdown(
            """
            ---
            **Disclaimer**: This tool is for research purposes only.
            Always consult healthcare professionals for medical decisions.

            Built with ❀️ using PydanticAI, Gradio, and Claude.
            """
        )

    return app


# Create the app instance
app = create_app()

if __name__ == "__main__":
    app.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
    )
```

---

## 5. Deployment Files

### `Dockerfile`

```dockerfile
# DeepCritical Docker Image
FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Install uv for fast package management
RUN pip install uv

# Copy dependency files
COPY pyproject.toml .
COPY uv.lock* .

# Install dependencies
RUN uv sync --no-dev

# Copy source code
COPY src/ src/

# Expose Gradio port
EXPOSE 7860

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1

# Run the app
CMD ["uv", "run", "python", "src/app.py"]
```

### `README.md` (HuggingFace Space Config)

> Note: This is for the HuggingFace Space, placed at project root.

```markdown
---
title: DeepCritical
emoji: 🧬
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: 5.0.0
python_version: 3.11
app_file: src/app.py
pinned: false
license: mit
---

# DeepCritical - Drug Repurposing Research Agent

An AI-powered research assistant that searches biomedical literature to identify
drug repurposing opportunities.

## Features

- πŸ” Searches PubMed and web sources
- βš–οΈ Evaluates evidence quality using AI
- πŸ“ Generates comprehensive research reports
- πŸ’Š Identifies drug repurposing candidates

## How to Use

1. Enter a research question about drug repurposing
2. Wait for the agent to search and analyze literature
3. Review the generated research report

## Example Questions

- "Can metformin be repurposed to treat Alzheimer's disease?"
- "What existing drugs might help treat long COVID?"
- "Are there diabetes drugs that could treat Parkinson's?"

## Technical Details

Built with:
- PydanticAI for structured LLM outputs
- PubMed E-utilities for biomedical search
- DuckDuckGo for web search
- Gradio for the interface

## Disclaimer

This tool is for research purposes only. Always consult healthcare professionals.
```

---

## 6. TDD Workflow

### Test File: `tests/unit/test_orchestrator.py`

```python
"""Unit tests for Orchestrator."""
import pytest
from unittest.mock import AsyncMock, MagicMock, patch


class TestOrchestrator:
    """Tests for Orchestrator."""

    @pytest.mark.asyncio
    async def test_run_yields_events(self, mocker):
        """Orchestrator.run should yield AgentEvents."""
        from src.orchestrator import Orchestrator
        from src.utils.models import (
            AgentEvent,
            AgentState,
            SearchResult,
            JudgeAssessment,
            Evidence,
            Citation,
        )

        # Mock search handler
        mock_search = MagicMock()
        mock_search.execute = AsyncMock(return_value=SearchResult(
            query="test",
            evidence=[
                Evidence(
                    content="Test evidence",
                    citation=Citation(
                        source="pubmed",
                        title="Test",
                        url="https://example.com",
                        date="2024",
                    ),
                )
            ],
            sources_searched=["pubmed", "web"],
            total_found=1,
        ))

        # Mock judge handler - return "synthesize" immediately
        mock_judge = MagicMock()
        mock_judge.assess = AsyncMock(return_value=JudgeAssessment(
            sufficient=True,
            recommendation="synthesize",
            reasoning="Good evidence.",
            overall_quality_score=8,
            coverage_score=8,
            candidates=[],
        ))
        mock_judge.should_continue = AsyncMock(return_value=False)

        # Mock synthesis
        mocker.patch(
            "src.orchestrator.synthesis_agent.run",
            new=AsyncMock(return_value=MagicMock(data="# Test Report"))
        )

        orchestrator = Orchestrator(
            search_handler=mock_search,
            judge_handler=mock_judge,
            max_iterations=3,
        )

        events = []
        async for event in orchestrator.run("test question"):
            events.append(event)

        # Should have multiple events
        assert len(events) >= 4  # init, search, judge, complete

        # Check state progression
        states = [e.state for e in events]
        assert AgentState.INITIALIZING in states
        assert AgentState.SEARCHING in states
        assert AgentState.JUDGING in states
        assert AgentState.COMPLETE in states

    @pytest.mark.asyncio
    async def test_run_respects_max_iterations(self, mocker):
        """Orchestrator should stop at max_iterations."""
        from src.orchestrator import Orchestrator
        from src.utils.models import SearchResult, JudgeAssessment, Evidence, Citation

        # Mock search
        mock_search = MagicMock()
        mock_search.execute = AsyncMock(return_value=SearchResult(
            query="test",
            evidence=[
                Evidence(
                    content="Test",
                    citation=Citation(
                        source="pubmed",
                        title="Test",
                        url="https://example.com",
                        date="2024",
                    ),
                )
            ],
            sources_searched=["pubmed"],
            total_found=1,
        ))

        # Mock judge - always say "continue"
        mock_judge = MagicMock()
        mock_judge.assess = AsyncMock(return_value=JudgeAssessment(
            sufficient=False,
            recommendation="continue",
            reasoning="Need more evidence.",
            overall_quality_score=4,
            coverage_score=4,
            next_search_queries=["more research"],
        ))
        mock_judge.should_continue = AsyncMock(return_value=True)

        # Mock synthesis
        mocker.patch(
            "src.orchestrator.synthesis_agent.run",
            new=AsyncMock(return_value=MagicMock(data="# Report"))
        )

        orchestrator = Orchestrator(
            search_handler=mock_search,
            judge_handler=mock_judge,
            max_iterations=2,  # Low limit
        )

        iterations_seen = set()
        async for event in orchestrator.run("test"):
            iterations_seen.add(event.iteration)

        # Should not exceed max_iterations
        assert max(iterations_seen) <= 2

    @pytest.mark.asyncio
    async def test_run_handles_errors(self, mocker):
        """Orchestrator should yield error event on failure."""
        from src.orchestrator import Orchestrator
        from src.utils.models import AgentState
        from src.utils.exceptions import DeepCriticalError

        # Mock search to raise error
        mock_search = MagicMock()
        mock_search.execute = AsyncMock(side_effect=Exception("Search failed"))

        orchestrator = Orchestrator(
            search_handler=mock_search,
            judge_handler=MagicMock(),
            max_iterations=3,
        )

        events = []
        with pytest.raises(DeepCriticalError):
            async for event in orchestrator.run("test"):
                events.append(event)

        # Should have error event
        error_events = [e for e in events if e.state == AgentState.ERROR]
        assert len(error_events) >= 1

    @pytest.mark.asyncio
    async def test_run_to_completion_returns_result(self, mocker):
        """run_to_completion should return AgentResult."""
        from src.orchestrator import Orchestrator
        from src.utils.models import SearchResult, JudgeAssessment, AgentResult, Evidence, Citation

        # Mock search
        mock_search = MagicMock()
        mock_search.execute = AsyncMock(return_value=SearchResult(
            query="test",
            evidence=[
                Evidence(
                    content="Test",
                    citation=Citation(
                        source="pubmed",
                        title="Test",
                        url="https://example.com",
                        date="2024",
                    ),
                )
            ],
            sources_searched=["pubmed"],
            total_found=1,
        ))

        # Mock judge
        mock_judge = MagicMock()
        mock_judge.assess = AsyncMock(return_value=JudgeAssessment(
            sufficient=True,
            recommendation="synthesize",
            reasoning="Good.",
            overall_quality_score=8,
            coverage_score=8,
        ))
        mock_judge.should_continue = AsyncMock(return_value=False)

        # Mock synthesis
        mocker.patch(
            "src.orchestrator.synthesis_agent.run",
            new=AsyncMock(return_value=MagicMock(data="# Test Report\n\nContent here."))
        )

        orchestrator = Orchestrator(
            search_handler=mock_search,
            judge_handler=mock_judge,
        )

        result = await orchestrator.run_to_completion("test question")

        assert isinstance(result, AgentResult)
        assert result.question == "test question"
        assert "Test Report" in result.report


class TestAgentEvent:
    """Tests for AgentEvent model."""

    def test_to_display_formats_correctly(self):
        """to_display should format event with icon."""
        from src.utils.models import AgentEvent, AgentState

        event = AgentEvent(
            state=AgentState.SEARCHING,
            message="Searching PubMed...",
            iteration=1,
        )

        display = event.to_display()

        assert "πŸ”" in display
        assert "SEARCHING" in display
        assert "Searching PubMed" in display

    def test_to_display_handles_all_states(self):
        """to_display should handle all AgentState values."""
        from src.utils.models import AgentEvent, AgentState

        for state in AgentState:
            event = AgentEvent(state=state, message="Test")
            display = event.to_display()
            assert state.value.upper() in display
```

---

## 7. Implementation Checklist

- [ ] Add `AgentState`, `AgentEvent`, `AgentResult` models to `src/utils/models.py`
- [ ] Implement `src/orchestrator.py` (complete Orchestrator class)
- [ ] Implement `src/app.py` (complete Gradio UI)
- [ ] Create `Dockerfile`
- [ ] Update root `README.md` for HuggingFace Spaces
- [ ] Write tests in `tests/unit/test_orchestrator.py`
- [ ] Run `uv run pytest tests/unit/test_orchestrator.py -v` β€” **ALL TESTS MUST PASS**
- [ ] Run `uv run ruff check src` β€” **NO ERRORS**
- [ ] Run `uv run mypy src` β€” **NO ERRORS**
- [ ] Run `uv run python src/app.py` β€” **VERIFY UI LOADS**
- [ ] Test with real query locally
- [ ] Build Docker image: `docker build -t deepcritical .`
- [ ] Commit: `git commit -m "feat: phase 4 orchestrator and UI complete"`

---

## 8. Definition of Done

Phase 4 is **COMPLETE** when:

1. βœ… All unit tests pass
2. βœ… Orchestrator yields streaming AgentEvents
3. βœ… Orchestrator respects max_iterations
4. βœ… Graceful error handling with error events
5. βœ… Gradio UI renders streaming updates
6. βœ… Ruff and mypy pass with no errors
7. βœ… Docker builds successfully
8. βœ… Manual smoke test works:

```bash
# Run locally
uv run python src/app.py

# Open http://localhost:7860 and test:
# "What existing drugs might help treat long COVID fatigue?"

# Verify:
# - Status updates stream in real-time
# - Final report is formatted as markdown
# - No errors in console
```

---

## 9. Deployment to HuggingFace Spaces

### Option A: Via GitHub (Recommended)

1. Push your code to GitHub
2. Create a new Space on HuggingFace (Gradio SDK)
3. Connect your GitHub repo
4. Add secrets in Space settings:
   - `OPENAI_API_KEY` (or `ANTHROPIC_API_KEY`)
5. Deploy automatically on push

### Option B: Manual Upload

1. Create new Gradio Space on HuggingFace
2. Upload all files:
   - `src/` directory
   - `pyproject.toml`
   - `README.md`
3. Add secrets in Space settings
4. Wait for build

### Verify Deployment

1. Visit your Space URL
2. Ask: "What drugs could treat long COVID?"
3. Verify:
   - Streaming events appear
   - Final report is generated
   - No timeout errors

---

## 10. Post-MVP Enhancements (Optional)

After completing the MVP, consider:

1. **RAG Enhancement**: Add vector storage for evidence retrieval
2. **Clinical Trials**: Integrate ClinicalTrials.gov API
3. **Drug Database**: Add DrugBank or ChEMBL integration
4. **Report Export**: Add PDF/DOCX export
5. **History**: Save research sessions
6. **Multi-turn**: Allow follow-up questions

---

**πŸŽ‰ Congratulations! Phase 4 is the MVP.**

After completing Phase 4, you have a working drug repurposing research agent
that can be demonstrated at the hackathon!