File size: 6,801 Bytes
b4ff56e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9162ac
d45d242
d9162ac
b4ff56e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9162ac
d45d242
d9162ac
b4ff56e
 
 
 
 
 
 
 
d45d242
b4ff56e
 
d45d242
b4ff56e
 
 
 
 
 
 
 
 
 
 
 
d45d242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4ff56e
 
 
 
 
 
 
 
 
 
 
 
 
d9162ac
 
 
b4ff56e
 
 
 
 
 
 
 
 
 
d45d242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4ff56e
 
 
d45d242
b4ff56e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9162ac
d45d242
d9162ac
b4ff56e
 
 
 
 
 
 
d45d242
b4ff56e
d45d242
b4ff56e
d45d242
b4ff56e
d45d242
 
b4ff56e
 
 
d45d242
b4ff56e
 
d9162ac
 
d45d242
d9162ac
b4ff56e
 
 
43839ca
b4ff56e
 
 
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
# Orchestrators Architecture

DeepCritical supports multiple orchestration patterns for research workflows.

## Research Flows

### IterativeResearchFlow

**File**: `src/orchestrator/research_flow.py`

**Pattern**: Generate observations β†’ Evaluate gaps β†’ Select tools β†’ Execute β†’ Judge β†’ Continue/Complete

**Agents Used**:
- `KnowledgeGapAgent`: Evaluates research completeness
- `ToolSelectorAgent`: Selects tools for addressing gaps
- `ThinkingAgent`: Generates observations
- `WriterAgent`: Creates final report
- `JudgeHandler`: Assesses evidence sufficiency

**Features**:
- Tracks iterations, time, budget
- Supports graph execution (`use_graph=True`) and agent chains (`use_graph=False`)
- Iterates until research complete or constraints met

**Usage**:

<!--codeinclude-->
[IterativeResearchFlow Initialization](../src/orchestrator/research_flow.py) start_line:57 end_line:80
<!--/codeinclude-->

### DeepResearchFlow

**File**: `src/orchestrator/research_flow.py`

**Pattern**: Planner β†’ Parallel iterative loops per section β†’ Synthesizer

**Agents Used**:
- `PlannerAgent`: Breaks query into report sections
- `IterativeResearchFlow`: Per-section research (parallel)
- `LongWriterAgent` or `ProofreaderAgent`: Final synthesis

**Features**:
- Uses `WorkflowManager` for parallel execution
- Budget tracking per section and globally
- State synchronization across parallel loops
- Supports graph execution and agent chains

**Usage**:

<!--codeinclude-->
[DeepResearchFlow Initialization](../src/orchestrator/research_flow.py) start_line:709 end_line:728
<!--/codeinclude-->

## Graph Orchestrator

**File**: `src/orchestrator/graph_orchestrator.py`

**Purpose**: Graph-based execution using Pydantic AI agents as nodes

**Features**:
- Uses graph execution (`use_graph=True`) or agent chains (`use_graph=False`) as fallback
- Routes based on research mode (iterative/deep/auto)
- Streams `AgentEvent` objects for UI
- Uses `GraphExecutionContext` to manage execution state

**Node Types**:
- **Agent Nodes**: Execute Pydantic AI agents
- **State Nodes**: Update or read workflow state
- **Decision Nodes**: Make routing decisions
- **Parallel Nodes**: Execute multiple nodes concurrently

**Edge Types**:
- **Sequential Edges**: Always traversed
- **Conditional Edges**: Traversed based on condition
- **Parallel Edges**: Used for parallel execution branches

**Special Node Handling**:

The `GraphOrchestrator` has special handling for certain nodes:

- **`execute_tools` node**: State node that uses `search_handler` to execute searches and add evidence to workflow state
- **`parallel_loops` node**: Parallel node that executes `IterativeResearchFlow` instances for each section in deep research mode
- **`synthesizer` node**: Agent node that calls `LongWriterAgent.write_report()` directly with `ReportDraft` instead of using `agent.run()`
- **`writer` node**: Agent node that calls `WriterAgent.write_report()` directly with findings instead of using `agent.run()`

**GraphExecutionContext**:

The orchestrator uses `GraphExecutionContext` to manage execution state:
- Tracks current node, visited nodes, and node results
- Manages workflow state and budget tracker
- Provides methods to store and retrieve node execution results

## Orchestrator Factory

**File**: `src/orchestrator_factory.py`

**Purpose**: Factory for creating orchestrators

**Modes**:
- **Simple**: Legacy orchestrator (backward compatible)
- **Advanced**: Magentic orchestrator (requires OpenAI API key)
- **Auto-detect**: Chooses based on API key availability

**Usage**:

<!--codeinclude-->
[Create Orchestrator](../src/orchestrator_factory.py) start_line:44 end_line:66
<!--/codeinclude-->

## Magentic Orchestrator

**File**: `src/orchestrator_magentic.py`

**Purpose**: Multi-agent coordination using Microsoft Agent Framework

**Features**:
- Uses `agent-framework-core`
- ChatAgent pattern with internal LLMs per agent
- `MagenticBuilder` with participants:
  - `searcher`: SearchAgent (wraps SearchHandler)
  - `hypothesizer`: HypothesisAgent (generates hypotheses)
  - `judge`: JudgeAgent (evaluates evidence)
  - `reporter`: ReportAgent (generates final report)
- Manager orchestrates agents via chat client (OpenAI or HuggingFace)
- Event-driven: converts Magentic events to `AgentEvent` for UI streaming via `_process_event()` method
- Supports max rounds, stall detection, and reset handling

**Event Processing**:

The orchestrator processes Magentic events and converts them to `AgentEvent`:
- `MagenticOrchestratorMessageEvent` β†’ `AgentEvent` with type based on message content
- `MagenticAgentMessageEvent` β†’ `AgentEvent` with type based on agent name
- `MagenticAgentDeltaEvent` β†’ `AgentEvent` for streaming updates
- `MagenticFinalResultEvent` β†’ `AgentEvent` with type "complete"

**Requirements**:
- `agent-framework-core` package
- OpenAI API key or HuggingFace authentication

## Hierarchical Orchestrator

**File**: `src/orchestrator_hierarchical.py`

**Purpose**: Hierarchical orchestrator using middleware and sub-teams

**Features**:
- Uses `SubIterationMiddleware` with `ResearchTeam` and `LLMSubIterationJudge`
- Adapts Magentic ChatAgent to `SubIterationTeam` protocol
- Event-driven via `asyncio.Queue` for coordination
- Supports sub-iteration patterns for complex research tasks

## Legacy Simple Mode

**File**: `src/legacy_orchestrator.py`

**Purpose**: Linear search-judge-synthesize loop

**Features**:
- Uses `SearchHandlerProtocol` and `JudgeHandlerProtocol`
- Generator-based design yielding `AgentEvent` objects
- Backward compatibility for simple use cases

## State Initialization

All orchestrators must initialize workflow state:

<!--codeinclude-->
[Initialize Workflow State](../src/middleware/state_machine.py) start_line:98 end_line:112
<!--/codeinclude-->

## Event Streaming

All orchestrators yield `AgentEvent` objects:

**Event Types**:
- `started`: Research started
- `searching`: Search in progress
- `search_complete`: Search completed
- `judging`: Evidence evaluation in progress
- `judge_complete`: Evidence evaluation completed
- `looping`: Iteration in progress
- `hypothesizing`: Generating hypotheses
- `analyzing`: Statistical analysis in progress
- `analysis_complete`: Statistical analysis completed
- `synthesizing`: Synthesizing results
- `complete`: Research completed
- `error`: Error occurred
- `streaming`: Streaming update (delta events)

**Event Structure**:

<!--codeinclude-->
[AgentEvent Model](../src/utils/models.py) start_line:104 end_line:126
<!--/codeinclude-->

## See Also

- [Graph Orchestration](graph_orchestration.md) - Graph-based execution details
- [Workflow Diagrams](workflow-diagrams.md) - Detailed workflow diagrams
- [API Reference - Orchestrators](../api/orchestrators.md) - API documentation