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
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
"""Graph orchestrator for Phase 4.

Implements graph-based orchestration using Pydantic AI agents as nodes.
Supports both iterative and deep research patterns with parallel execution.
"""

import asyncio
from collections.abc import AsyncGenerator, Callable
from typing import TYPE_CHECKING, Any, Literal

import structlog

from src.agent_factory.agents import (
    create_input_parser_agent,
    create_knowledge_gap_agent,
    create_long_writer_agent,
    create_planner_agent,
    create_thinking_agent,
    create_tool_selector_agent,
    create_writer_agent,
)
from src.agent_factory.graph_builder import (
    AgentNode,
    DecisionNode,
    ParallelNode,
    ResearchGraph,
    StateNode,
    create_deep_graph,
    create_iterative_graph,
)
from src.middleware.budget_tracker import BudgetTracker
from src.middleware.state_machine import WorkflowState, init_workflow_state
from src.orchestrator.research_flow import DeepResearchFlow, IterativeResearchFlow
from src.utils.models import AgentEvent

if TYPE_CHECKING:
    pass

logger = structlog.get_logger()


class GraphExecutionContext:
    """Context for managing graph execution state."""

    def __init__(self, state: WorkflowState, budget_tracker: BudgetTracker) -> None:
        """Initialize execution context.

        Args:
            state: Current workflow state
            budget_tracker: Budget tracker instance
        """
        self.current_node: str = ""
        self.visited_nodes: set[str] = set()
        self.node_results: dict[str, Any] = {}
        self.state = state
        self.budget_tracker = budget_tracker
        self.iteration_count = 0

    def set_node_result(self, node_id: str, result: Any) -> None:
        """Store result from node execution.

        Args:
            node_id: The node ID
            result: The execution result
        """
        self.node_results[node_id] = result

    def get_node_result(self, node_id: str) -> Any:
        """Get result from node execution.

        Args:
            node_id: The node ID

        Returns:
            The stored result, or None if not found
        """
        return self.node_results.get(node_id)

    def has_visited(self, node_id: str) -> bool:
        """Check if node was visited.

        Args:
            node_id: The node ID

        Returns:
            True if visited, False otherwise
        """
        return node_id in self.visited_nodes

    def mark_visited(self, node_id: str) -> None:
        """Mark node as visited.

        Args:
            node_id: The node ID
        """
        self.visited_nodes.add(node_id)

    def update_state(
        self, updater: Callable[[WorkflowState, Any], WorkflowState], data: Any
    ) -> None:
        """Update workflow state.

        Args:
            updater: Function to update state
            data: Data to pass to updater
        """
        self.state = updater(self.state, data)


class GraphOrchestrator:
    """
    Graph orchestrator using Pydantic AI Graphs.

    Executes research workflows as graphs with nodes (agents) and edges (transitions).
    Supports parallel execution, conditional routing, and state management.
    """

    def __init__(
        self,
        mode: Literal["iterative", "deep", "auto"] = "auto",
        max_iterations: int = 5,
        max_time_minutes: int = 10,
        use_graph: bool = True,
    ) -> None:
        """
        Initialize graph orchestrator.

        Args:
            mode: Research mode ("iterative", "deep", or "auto" to detect)
            max_iterations: Maximum iterations per loop
            max_time_minutes: Maximum time per loop
            use_graph: Whether to use graph execution (True) or agent chains (False)
        """
        self.mode = mode
        self.max_iterations = max_iterations
        self.max_time_minutes = max_time_minutes
        self.use_graph = use_graph
        self.logger = logger

        # Initialize flows (for backward compatibility)
        self._iterative_flow: IterativeResearchFlow | None = None
        self._deep_flow: DeepResearchFlow | None = None

        # Graph execution components (lazy initialization)
        self._graph: ResearchGraph | None = None
        self._budget_tracker: BudgetTracker | None = None

    async def run(self, query: str) -> AsyncGenerator[AgentEvent, None]:
        """
        Run the research workflow.

        Args:
            query: The user's research query

        Yields:
            AgentEvent objects for real-time UI updates
        """
        self.logger.info(
            "Starting graph orchestrator",
            query=query[:100],
            mode=self.mode,
            use_graph=self.use_graph,
        )

        yield AgentEvent(
            type="started",
            message=f"Starting research ({self.mode} mode): {query}",
            iteration=0,
        )

        try:
            # Determine research mode
            research_mode = self.mode
            if research_mode == "auto":
                research_mode = await self._detect_research_mode(query)

            # Use graph execution if enabled, otherwise fall back to agent chains
            if self.use_graph:
                async for event in self._run_with_graph(query, research_mode):
                    yield event
            else:
                async for event in self._run_with_chains(query, research_mode):
                    yield event

        except Exception as e:
            self.logger.error("Graph orchestrator failed", error=str(e), exc_info=True)
            yield AgentEvent(
                type="error",
                message=f"Research failed: {e!s}",
                iteration=0,
            )

    async def _run_with_graph(
        self, query: str, research_mode: Literal["iterative", "deep"]
    ) -> AsyncGenerator[AgentEvent, None]:
        """Run workflow using graph execution.

        Args:
            query: The research query
            research_mode: The research mode

        Yields:
            AgentEvent objects
        """
        # Initialize state and budget tracker
        from src.services.embeddings import get_embedding_service

        embedding_service = get_embedding_service()
        state = init_workflow_state(embedding_service=embedding_service)
        budget_tracker = BudgetTracker()
        budget_tracker.create_budget(
            loop_id="graph_execution",
            tokens_limit=100000,
            time_limit_seconds=self.max_time_minutes * 60,
            iterations_limit=self.max_iterations,
        )
        budget_tracker.start_timer("graph_execution")

        context = GraphExecutionContext(state, budget_tracker)

        # Build graph
        self._graph = await self._build_graph(research_mode)

        # Execute graph
        async for event in self._execute_graph(query, context):
            yield event

    async def _run_with_chains(
        self, query: str, research_mode: Literal["iterative", "deep"]
    ) -> AsyncGenerator[AgentEvent, None]:
        """Run workflow using agent chains (backward compatibility).

        Args:
            query: The research query
            research_mode: The research mode

        Yields:
            AgentEvent objects
        """
        if research_mode == "iterative":
            yield AgentEvent(
                type="searching",
                message="Running iterative research flow...",
                iteration=1,
            )

            if self._iterative_flow is None:
                self._iterative_flow = IterativeResearchFlow(
                    max_iterations=self.max_iterations,
                    max_time_minutes=self.max_time_minutes,
                )

            try:
                final_report = await self._iterative_flow.run(query)
            except Exception as e:
                self.logger.error("Iterative flow failed", error=str(e), exc_info=True)
                # Yield error event - outer handler will also catch and yield error event
                yield AgentEvent(
                    type="error",
                    message=f"Iterative research failed: {e!s}",
                    iteration=1,
                )
                # Re-raise so outer handler can also yield error event for consistency
                raise

            yield AgentEvent(
                type="complete",
                message=final_report,
                data={"mode": "iterative"},
                iteration=1,
            )

        elif research_mode == "deep":
            yield AgentEvent(
                type="searching",
                message="Running deep research flow...",
                iteration=1,
            )

            if self._deep_flow is None:
                self._deep_flow = DeepResearchFlow(
                    max_iterations=self.max_iterations,
                    max_time_minutes=self.max_time_minutes,
                )

            try:
                final_report = await self._deep_flow.run(query)
            except Exception as e:
                self.logger.error("Deep flow failed", error=str(e), exc_info=True)
                # Yield error event before re-raising so test can capture it
                yield AgentEvent(
                    type="error",
                    message=f"Deep research failed: {e!s}",
                    iteration=1,
                )
                raise

            yield AgentEvent(
                type="complete",
                message=final_report,
                data={"mode": "deep"},
                iteration=1,
            )

    async def _build_graph(self, mode: Literal["iterative", "deep"]) -> ResearchGraph:
        """Build graph for the specified mode.

        Args:
            mode: Research mode

        Returns:
            Constructed ResearchGraph
        """
        if mode == "iterative":
            # Get agents
            knowledge_gap_agent = create_knowledge_gap_agent()
            tool_selector_agent = create_tool_selector_agent()
            thinking_agent = create_thinking_agent()
            writer_agent = create_writer_agent()

            # Create graph
            graph = create_iterative_graph(
                knowledge_gap_agent=knowledge_gap_agent.agent,
                tool_selector_agent=tool_selector_agent.agent,
                thinking_agent=thinking_agent.agent,
                writer_agent=writer_agent.agent,
            )
        else:  # deep
            # Get agents
            planner_agent = create_planner_agent()
            knowledge_gap_agent = create_knowledge_gap_agent()
            tool_selector_agent = create_tool_selector_agent()
            thinking_agent = create_thinking_agent()
            writer_agent = create_writer_agent()
            long_writer_agent = create_long_writer_agent()

            # Create graph
            graph = create_deep_graph(
                planner_agent=planner_agent.agent,
                knowledge_gap_agent=knowledge_gap_agent.agent,
                tool_selector_agent=tool_selector_agent.agent,
                thinking_agent=thinking_agent.agent,
                writer_agent=writer_agent.agent,
                long_writer_agent=long_writer_agent.agent,
            )

        return graph

    def _emit_start_event(
        self, node: Any, current_node_id: str, iteration: int, context: GraphExecutionContext
    ) -> AgentEvent:
        """Emit start event for a node.

        Args:
            node: The node being executed
            current_node_id: Current node ID
            iteration: Current iteration number
            context: Execution context

        Returns:
            AgentEvent for the start of node execution
        """
        if node and node.node_id == "planner":
            return AgentEvent(
                type="searching",
                message="Creating report plan...",
                iteration=iteration,
            )
        elif node and node.node_id == "parallel_loops":
            # Get report plan to show section count
            report_plan = context.get_node_result("planner")
            if report_plan and hasattr(report_plan, "report_outline"):
                section_count = len(report_plan.report_outline)
                return AgentEvent(
                    type="looping",
                    message=f"Running parallel research loops for {section_count} sections...",
                    iteration=iteration,
                    data={"sections": section_count},
                )
            return AgentEvent(
                type="looping",
                message="Running parallel research loops...",
                iteration=iteration,
            )
        elif node and node.node_id == "synthesizer":
            return AgentEvent(
                type="synthesizing",
                message="Synthesizing final report from section drafts...",
                iteration=iteration,
            )
        return AgentEvent(
            type="looping",
            message=f"Executing node: {current_node_id}",
            iteration=iteration,
        )

    def _emit_completion_event(
        self, node: Any, current_node_id: str, result: Any, iteration: int
    ) -> AgentEvent:
        """Emit completion event for a node.

        Args:
            node: The node that was executed
            current_node_id: Current node ID
            result: Node execution result
            iteration: Current iteration number

        Returns:
            AgentEvent for the completion of node execution
        """
        if not node:
            return AgentEvent(
                type="looping",
                message=f"Completed node: {current_node_id}",
                iteration=iteration,
            )

        if node.node_id == "planner":
            if isinstance(result, dict) and "report_outline" in result:
                section_count = len(result["report_outline"])
                return AgentEvent(
                    type="search_complete",
                    message=f"Report plan created with {section_count} sections",
                    iteration=iteration,
                    data={"sections": section_count},
                )
            return AgentEvent(
                type="search_complete",
                message="Report plan created",
                iteration=iteration,
            )
        elif node.node_id == "parallel_loops":
            if isinstance(result, list):
                return AgentEvent(
                    type="search_complete",
                    message=f"Completed parallel research for {len(result)} sections",
                    iteration=iteration,
                    data={"sections_completed": len(result)},
                )
            return AgentEvent(
                type="search_complete",
                message="Parallel research loops completed",
                iteration=iteration,
            )
        elif node.node_id == "synthesizer":
            return AgentEvent(
                type="synthesizing",
                message="Final report synthesis completed",
                iteration=iteration,
            )
        return AgentEvent(
            type="searching" if node.node_type == "agent" else "looping",
            message=f"Completed {node.node_type} node: {current_node_id}",
            iteration=iteration,
        )

    async def _execute_graph(
        self, query: str, context: GraphExecutionContext
    ) -> AsyncGenerator[AgentEvent, None]:
        """Execute the graph from entry node.

        Args:
            query: The research query
            context: Execution context

        Yields:
            AgentEvent objects
        """
        if not self._graph:
            raise ValueError("Graph not built")

        current_node_id = self._graph.entry_node
        iteration = 0

        while current_node_id and current_node_id not in self._graph.exit_nodes:
            # Check budget
            if not context.budget_tracker.can_continue("graph_execution"):
                self.logger.warning("Budget exceeded, exiting graph execution")
                break

            # Execute current node
            iteration += 1
            context.current_node = current_node_id
            node = self._graph.get_node(current_node_id)

            # Emit start event
            yield self._emit_start_event(node, current_node_id, iteration, context)

            try:
                result = await self._execute_node(current_node_id, query, context)
                context.set_node_result(current_node_id, result)
                context.mark_visited(current_node_id)

                # Yield completion event
                yield self._emit_completion_event(node, current_node_id, result, iteration)

            except Exception as e:
                self.logger.error("Node execution failed", node_id=current_node_id, error=str(e))
                yield AgentEvent(
                    type="error",
                    message=f"Node {current_node_id} failed: {e!s}",
                    iteration=iteration,
                )
                break

            # Get next node(s)
            next_nodes = self._get_next_node(current_node_id, context)

            if not next_nodes:
                # No more nodes, check if we're at exit
                if current_node_id in self._graph.exit_nodes:
                    break
                # Otherwise, we've reached a dead end
                self.logger.warning("Reached dead end in graph", node_id=current_node_id)
                break

            current_node_id = next_nodes[0]  # For now, take first next node (handle parallel later)

        # Final event
        final_result = context.get_node_result(current_node_id) if current_node_id else None
        yield AgentEvent(
            type="complete",
            message=final_result if isinstance(final_result, str) else "Research completed",
            data={"mode": self.mode, "iterations": iteration},
            iteration=iteration,
        )

    async def _execute_node(self, node_id: str, query: str, context: GraphExecutionContext) -> Any:
        """Execute a single node.

        Args:
            node_id: The node ID
            query: The research query
            context: Execution context

        Returns:
            Node execution result
        """
        if not self._graph:
            raise ValueError("Graph not built")

        node = self._graph.get_node(node_id)
        if not node:
            raise ValueError(f"Node {node_id} not found")

        if isinstance(node, AgentNode):
            return await self._execute_agent_node(node, query, context)
        elif isinstance(node, StateNode):
            return await self._execute_state_node(node, query, context)
        elif isinstance(node, DecisionNode):
            return await self._execute_decision_node(node, query, context)
        elif isinstance(node, ParallelNode):
            return await self._execute_parallel_node(node, query, context)
        else:
            raise ValueError(f"Unknown node type: {type(node)}")

    async def _execute_agent_node(
        self, node: AgentNode, query: str, context: GraphExecutionContext
    ) -> Any:
        """Execute an agent node.

        Special handling for deep research nodes:
        - "planner": Takes query string, returns ReportPlan
        - "synthesizer": Takes query + ReportPlan + section drafts, returns final report

        Args:
            node: The agent node
            query: The research query
            context: Execution context

        Returns:
            Agent execution result
        """
        # Special handling for synthesizer node
        if node.node_id == "synthesizer":
            # Call LongWriterAgent.write_report() directly instead of using agent.run()
            from src.agent_factory.agents import create_long_writer_agent
            from src.utils.models import ReportDraft, ReportDraftSection, ReportPlan

            report_plan = context.get_node_result("planner")
            section_drafts = context.get_node_result("parallel_loops") or []

            if not isinstance(report_plan, ReportPlan):
                raise ValueError("ReportPlan not found for synthesizer")

            if not section_drafts:
                raise ValueError("Section drafts not found for synthesizer")

            # Create ReportDraft from section drafts
            report_draft = ReportDraft(
                sections=[
                    ReportDraftSection(
                        section_title=section.title,
                        section_content=draft,
                    )
                    for section, draft in zip(
                        report_plan.report_outline, section_drafts, strict=False
                    )
                ]
            )

            # Get LongWriterAgent instance and call write_report directly
            long_writer_agent = create_long_writer_agent()
            final_report = await long_writer_agent.write_report(
                original_query=query,
                report_title=report_plan.report_title,
                report_draft=report_draft,
            )

            # Estimate tokens (rough estimate)
            estimated_tokens = len(final_report) // 4  # Rough token estimate
            context.budget_tracker.add_tokens("graph_execution", estimated_tokens)

            return final_report

        # Standard agent execution
        # Prepare input based on node type
        if node.node_id == "planner":
            # Planner takes the original query
            input_data = query
        else:
            # Standard: use previous node result or query
            prev_result = context.get_node_result(context.current_node)
            input_data = prev_result if prev_result is not None else query

        # Apply input transformer if provided
        if node.input_transformer:
            input_data = node.input_transformer(input_data)

        # Execute agent
        result = await node.agent.run(input_data)

        # Transform output if needed
        output = result.output
        if node.output_transformer:
            output = node.output_transformer(output)

        # Estimate and track tokens
        if hasattr(result, "usage") and result.usage:
            tokens = result.usage.total_tokens if hasattr(result.usage, "total_tokens") else 0
            context.budget_tracker.add_tokens("graph_execution", tokens)

        return output

    async def _execute_state_node(
        self, node: StateNode, query: str, context: GraphExecutionContext
    ) -> Any:
        """Execute a state node.

        Special handling for deep research state nodes:
        - "store_plan": Stores ReportPlan in context for parallel loops
        - "collect_drafts": Stores section drafts in context for synthesizer

        Args:
            node: The state node
            query: The research query
            context: Execution context

        Returns:
            State update result
        """
        # Get previous result for state update
        # For "store_plan", get from planner node
        # For "collect_drafts", get from parallel_loops node
        if node.node_id == "store_plan":
            prev_result = context.get_node_result("planner")
        elif node.node_id == "collect_drafts":
            prev_result = context.get_node_result("parallel_loops")
        else:
            prev_result = context.get_node_result(context.current_node)

        # Update state
        updated_state = node.state_updater(context.state, prev_result)
        context.state = updated_state

        # Store result in context for next nodes to access
        context.set_node_result(node.node_id, prev_result)

        # Read state if needed
        if node.state_reader:
            return node.state_reader(context.state)

        return prev_result  # Return the stored result for next nodes

    async def _execute_decision_node(
        self, node: DecisionNode, query: str, context: GraphExecutionContext
    ) -> str:
        """Execute a decision node.

        Args:
            node: The decision node
            query: The research query
            context: Execution context

        Returns:
            Next node ID
        """
        # Get previous result for decision
        prev_result = context.get_node_result(context.current_node)

        # Make decision
        next_node_id = node.decision_function(prev_result)

        # Validate decision
        if next_node_id not in node.options:
            self.logger.warning(
                "Decision function returned invalid node",
                node_id=node.node_id,
                returned=next_node_id,
                options=node.options,
            )
            # Default to first option
            next_node_id = node.options[0]

        return next_node_id

    async def _execute_parallel_node(
        self, node: ParallelNode, query: str, context: GraphExecutionContext
    ) -> list[Any]:
        """Execute a parallel node.

        Special handling for deep research "parallel_loops" node:
        - Extracts report plan from previous node result
        - Creates IterativeResearchFlow instances for each section
        - Executes them in parallel
        - Returns section drafts

        Args:
            node: The parallel node
            query: The research query
            context: Execution context

        Returns:
            List of results from parallel nodes
        """
        # Special handling for deep research parallel_loops node
        if node.node_id == "parallel_loops":
            return await self._execute_deep_research_parallel_loops(node, query, context)

        # Standard parallel node execution
        # Execute all parallel nodes concurrently
        tasks = [
            self._execute_node(parallel_node_id, query, context)
            for parallel_node_id in node.parallel_nodes
        ]

        results = await asyncio.gather(*tasks, return_exceptions=True)

        # Handle exceptions
        for i, result in enumerate(results):
            if isinstance(result, Exception):
                self.logger.error(
                    "Parallel node execution failed",
                    node_id=node.parallel_nodes[i] if i < len(node.parallel_nodes) else "unknown",
                    error=str(result),
                )
                results[i] = None

        # Aggregate if needed
        if node.aggregator:
            aggregated = node.aggregator(results)
            # Type cast: aggregator returns Any, but we expect list[Any]
            return list(aggregated) if isinstance(aggregated, list) else [aggregated]

        return results

    async def _execute_deep_research_parallel_loops(
        self, node: ParallelNode, query: str, context: GraphExecutionContext
    ) -> list[str]:
        """Execute parallel iterative research loops for deep research.

        Args:
            node: The parallel node (should be "parallel_loops")
            query: The research query
            context: Execution context

        Returns:
            List of section draft strings
        """
        from src.agent_factory.judges import create_judge_handler
        from src.orchestrator.research_flow import IterativeResearchFlow
        from src.utils.models import ReportPlan

        # Get report plan from previous node (store_plan)
        # The plan should be stored in context.node_results from the planner node
        planner_result = context.get_node_result("planner")
        if not isinstance(planner_result, ReportPlan):
            self.logger.error(
                "Planner result is not a ReportPlan",
                type=type(planner_result),
            )
            raise ValueError("Planner must return ReportPlan for deep research")

        report_plan: ReportPlan = planner_result
        self.logger.info(
            "Executing parallel loops for deep research",
            sections=len(report_plan.report_outline),
        )

        # Create judge handler for iterative flows
        judge_handler = create_judge_handler()

        # Create and execute iterative research flows for each section
        async def run_section_research(section_index: int) -> str:
            """Run iterative research for a single section."""
            section = report_plan.report_outline[section_index]

            try:
                # Create iterative research flow
                flow = IterativeResearchFlow(
                    max_iterations=self.max_iterations,
                    max_time_minutes=self.max_time_minutes,
                    verbose=False,  # Less verbose in parallel execution
                    use_graph=False,  # Use agent chains for section research
                    judge_handler=judge_handler,
                )

                # Run research for this section
                section_draft = await flow.run(
                    query=section.key_question,
                    background_context=report_plan.background_context,
                )

                self.logger.info(
                    "Section research completed",
                    section_index=section_index,
                    section_title=section.title,
                    draft_length=len(section_draft),
                )

                return section_draft

            except Exception as e:
                self.logger.error(
                    "Section research failed",
                    section_index=section_index,
                    section_title=section.title,
                    error=str(e),
                )
                # Return empty string for failed sections
                return f"# {section.title}\n\n[Research failed: {e!s}]"

        # Execute all sections in parallel
        section_drafts = await asyncio.gather(
            *(run_section_research(i) for i in range(len(report_plan.report_outline))),
            return_exceptions=True,
        )

        # Handle exceptions and filter None results
        filtered_drafts: list[str] = []
        for i, draft in enumerate(section_drafts):
            if isinstance(draft, Exception):
                self.logger.error(
                    "Section research exception",
                    section_index=i,
                    error=str(draft),
                )
                filtered_drafts.append(
                    f"# {report_plan.report_outline[i].title}\n\n[Research failed: {draft!s}]"
                )
            elif draft is not None:
                # Type narrowing: after Exception check, draft is str | None
                assert isinstance(draft, str), "Expected str after Exception check"
                filtered_drafts.append(draft)

        self.logger.info(
            "Parallel loops completed",
            sections=len(filtered_drafts),
            total_sections=len(report_plan.report_outline),
        )

        return filtered_drafts

    def _get_next_node(self, node_id: str, context: GraphExecutionContext) -> list[str]:
        """Get next node(s) from current node.

        Args:
            node_id: Current node ID
            context: Execution context

        Returns:
            List of next node IDs
        """
        if not self._graph:
            return []

        # Get node result for condition evaluation
        node_result = context.get_node_result(node_id)

        # Get next nodes
        next_nodes = self._graph.get_next_nodes(node_id, context=node_result)

        # If this was a decision node, use its result
        node = self._graph.get_node(node_id)
        if isinstance(node, DecisionNode):
            decision_result = node_result
            if isinstance(decision_result, str):
                return [decision_result]

        # Return next node IDs
        return [next_node_id for next_node_id, _ in next_nodes]

    async def _detect_research_mode(self, query: str) -> Literal["iterative", "deep"]:
        """
        Detect research mode from query using input parser agent.

        Uses input parser agent to analyze query and determine research mode.
        Falls back to heuristic if parser fails.

        Args:
            query: The research query

        Returns:
            Detected research mode
        """
        try:
            # Use input parser agent for intelligent mode detection
            input_parser = create_input_parser_agent()
            parsed_query = await input_parser.parse(query)
            self.logger.info(
                "Research mode detected by input parser",
                mode=parsed_query.research_mode,
                query=query[:100],
            )
            return parsed_query.research_mode
        except Exception as e:
            # Fallback to heuristic if parser fails
            self.logger.warning(
                "Input parser failed, using heuristic",
                error=str(e),
                query=query[:100],
            )
            query_lower = query.lower()
            if any(
                keyword in query_lower
                for keyword in [
                    "section",
                    "sections",
                    "report",
                    "outline",
                    "structure",
                    "comprehensive",
                    "analyze",
                    "analysis",
                ]
            ):
                return "deep"
            return "iterative"


def create_graph_orchestrator(
    mode: Literal["iterative", "deep", "auto"] = "auto",
    max_iterations: int = 5,
    max_time_minutes: int = 10,
    use_graph: bool = True,
) -> GraphOrchestrator:
    """
    Factory function to create a graph orchestrator.

    Args:
        mode: Research mode
        max_iterations: Maximum iterations per loop
        max_time_minutes: Maximum time per loop
        use_graph: Whether to use graph execution (True) or agent chains (False)

    Returns:
        Configured GraphOrchestrator instance
    """
    return GraphOrchestrator(
        mode=mode,
        max_iterations=max_iterations,
        max_time_minutes=max_time_minutes,
        use_graph=use_graph,
    )