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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
"""
Comprehensive Stock Analysis Workflow

This workflow orchestrates all 11 agents for comprehensive stock research:
- Technical Analysis: Indicator, Pattern, Trend, Decision agents
- Fundamental Analysis: Fundamentals, Sentiment, News agents
- Synthesis: Technical Analyst (bridges technical + fundamental)
- Research Debate: Researcher Team (bull vs bear)
- Risk Assessment: Risk Manager
- Final Decision: Portfolio Manager

The workflow uses LangGraph for parallel execution and state management.
"""

import json
import logging
import time
import traceback
from typing import Annotated, Any, Dict, Optional, TypedDict

import pandas as pd
from langgraph.graph import END, StateGraph
from langgraph.graph.message import add_messages

from agents.fundamental.fundamentals_agent import FundamentalsAgent
from agents.fundamental.news_agent import NewsAgent
from agents.fundamental.sentiment_agent import SentimentAgent
from agents.fundamental.technical_analyst import TechnicalAnalystAgent
from agents.management.portfolio_manager import PortfolioManagerAgent
from agents.management.researcher_team import ResearcherTeamAgent
from agents.management.risk_manager import RiskManagerAgent
from agents.technical.decision_agent import DecisionAgent
from agents.technical.indicator_agent import IndicatorAgent
from agents.technical.pattern_agent import PatternAgent
from agents.technical.trend_agent import TrendAgent
from config.default_config import DEFAULT_CONFIG
from data.providers.base import DataProvider
from data.providers.yahoo_finance import YahooFinanceProvider
from data.schemas.market_data import validate_ohlc
from graph.state.trading_state import TechnicalWorkflowState
from utils.charts.chart_generator import ChartGenerator

logger = logging.getLogger(__name__)


class ComprehensiveState(TypedDict, total=False):
    """State for comprehensive workflow."""

    # Input
    ticker: str
    timeframe: str

    # Market data
    market_data: pd.DataFrame
    fundamental_data: Dict[str, Any]
    news_data: list

    # Technical analysis results (4 agents)
    indicator_analysis: Dict[str, Any]
    pattern_analysis: Dict[str, Any]
    trend_analysis: Dict[str, Any]
    decision_analysis: Dict[str, Any]

    # Fundamental analysis results (3 agents)
    fundamentals_analysis: Dict[str, Any]
    sentiment_analysis: Dict[str, Any]
    news_analysis: Dict[str, Any]

    # Synthesis results
    technical_analyst: Dict[str, Any]  # Bridges technical + fundamental

    # Research debate results
    researcher_synthesis: Dict[str, Any]  # Bull vs bear debate

    # Risk assessment results
    risk_assessment: Dict[str, Any]

    # Final decision
    portfolio_decision: Dict[str, Any]

    # Cross-validation
    contradictions: list

    # Output artifacts
    chart_path: str
    messages: Annotated[list, add_messages]  # Use reducer to handle concurrent updates
    error: str | None


class ComprehensiveWorkflow:
    """Comprehensive stock analysis workflow with 11 agents."""

    def __init__(self, config: Optional[Dict[str, Any]] = None):
        """
        Initialize the comprehensive workflow.

        Args:
            config: Optional configuration override
        """
        self.config = config or DEFAULT_CONFIG
        self.data_provider = YahooFinanceProvider()
        self.chart_generator = ChartGenerator()

        # Technical agents - pass config for indicator parameters
        self.indicator_agent = IndicatorAgent(config=self.config)
        self.pattern_agent = PatternAgent(config=self.config)
        self.trend_agent = TrendAgent(config=self.config)
        self.decision_agent = DecisionAgent(config=self.config)

        # Fundamental agents - pass config for LLM settings
        self.fundamentals_agent = FundamentalsAgent(config=self.config)
        self.sentiment_agent = SentimentAgent(config=self.config)
        self.news_agent = NewsAgent(config=self.config)

        # Synthesis agent - pass config for runtime LLM overrides
        self.technical_analyst = TechnicalAnalystAgent(config=self.config)

        # Management agents - pass config for runtime LLM overrides
        self.researcher_team = ResearcherTeamAgent(config=self.config)
        self.risk_manager = RiskManagerAgent(config=self.config)
        self.portfolio_manager = PortfolioManagerAgent(config=self.config)

        self.workflow = self._build_workflow()

    def _build_workflow(self) -> StateGraph:
        """Build the LangGraph workflow."""
        workflow = StateGraph(ComprehensiveState)

        # Data fetching nodes (parallel)
        workflow.add_node("fetch_market_data", self._fetch_market_data)
        workflow.add_node("fetch_fundamental_data", self._fetch_fundamental_data)
        workflow.add_node("fetch_news_data", self._fetch_news_data)

        # Technical analysis nodes (parallel)
        workflow.add_node("indicator_analysis", self._run_indicator_agent)
        workflow.add_node("pattern_analysis", self._run_pattern_agent)
        workflow.add_node("trend_analysis", self._run_trend_agent)
        workflow.add_node("decision_analysis", self._run_decision_agent)

        # Fundamental analysis nodes (parallel)
        workflow.add_node("fundamentals_analysis", self._run_fundamentals_agent)
        workflow.add_node("sentiment_analysis", self._run_sentiment_agent)
        workflow.add_node("news_analysis", self._run_news_agent)

        # Synthesis node (waits for all 7 analysis agents)
        workflow.add_node("technical_analyst", self._run_technical_analyst)

        # Research debate node
        workflow.add_node("researcher_debate", self._run_researcher_team)

        # Risk assessment node
        workflow.add_node("risk_assessment", self._run_risk_manager)

        # Final decision node
        workflow.add_node("portfolio_decision", self._run_portfolio_manager)

        # Cross-validation node
        workflow.add_node("cross_validation", self._detect_contradictions)

        # Chart generation node
        workflow.add_node("generate_chart", self._generate_chart)

        # Define workflow edges
        workflow.set_entry_point("fetch_market_data")

        # After market data, fetch fundamental and news in parallel
        workflow.add_edge("fetch_market_data", "fetch_fundamental_data")
        workflow.add_edge("fetch_market_data", "fetch_news_data")

        # After fundamental data, run fundamental agents in parallel
        workflow.add_edge("fetch_fundamental_data", "fundamentals_analysis")
        workflow.add_edge("fetch_fundamental_data", "sentiment_analysis")

        # After news data, run news agent
        workflow.add_edge("fetch_news_data", "news_analysis")

        # After market data, run technical agents in parallel
        workflow.add_edge("fetch_market_data", "indicator_analysis")
        workflow.add_edge("fetch_market_data", "pattern_analysis")
        workflow.add_edge("fetch_market_data", "trend_analysis")

        # Decision agent waits for indicator, pattern, trend
        workflow.add_edge("indicator_analysis", "decision_analysis")
        workflow.add_edge("pattern_analysis", "decision_analysis")
        workflow.add_edge("trend_analysis", "decision_analysis")

        # Technical analyst waits for all 7 analysis agents
        workflow.add_edge("decision_analysis", "technical_analyst")
        workflow.add_edge("fundamentals_analysis", "technical_analyst")
        workflow.add_edge("sentiment_analysis", "technical_analyst")
        workflow.add_edge("news_analysis", "technical_analyst")

        # Researcher debate waits for technical analyst
        workflow.add_edge("technical_analyst", "researcher_debate")

        # Risk manager waits for researcher debate
        workflow.add_edge("researcher_debate", "risk_assessment")

        # Portfolio manager waits for risk manager
        workflow.add_edge("risk_assessment", "portfolio_decision")

        # Cross-validation waits for portfolio decision
        workflow.add_edge("portfolio_decision", "cross_validation")

        # Chart generation waits for cross-validation
        workflow.add_edge("cross_validation", "generate_chart")

        # End after chart generation
        workflow.add_edge("generate_chart", END)

        return workflow.compile()

    def run(self, ticker: str, timeframe: str = "1y") -> ComprehensiveState:
        """
        Run comprehensive analysis workflow.

        Args:
            ticker: Stock ticker symbol
            timeframe: Analysis timeframe (default: 1y)

        Returns:
            Final state with all analysis results
        """
        start_time = time.time()

        logger.info(
            json.dumps(
                {
                    "workflow": "comprehensive",
                    "action": "start",
                    "ticker": ticker,
                    "timeframe": timeframe,
                    "timestamp": time.time(),
                }
            )
        )

        try:
            initial_state: ComprehensiveState = {
                "ticker": ticker,
                "timeframe": timeframe,
                "messages": [],
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
                "contradictions": [],
            }

            final_state = self.workflow.invoke(initial_state)

            execution_time = time.time() - start_time
            logger.info(
                json.dumps(
                    {
                        "workflow": "comprehensive",
                        "action": "complete",
                        "execution_time": execution_time,
                        "ticker": ticker,
                        "recommendation": final_state.get("portfolio_decision", {})
                        .get("decision", {})
                        .get("recommendation", "N/A"),
                        "timestamp": time.time(),
                    }
                )
            )

            return final_state

        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "workflow": "comprehensive",
                        "action": "error",
                        "ticker": ticker,
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            raise

    # Data fetching nodes

    def _fetch_market_data(self, state: ComprehensiveState) -> dict:
        """Fetch market data from Yahoo Finance."""
        ticker = state["ticker"]
        timeframe = state["timeframe"]

        try:
            # Calculate date range based on timeframe
            from datetime import datetime, timedelta

            end_date = datetime.now()

            # Map timeframe to appropriate lookback period
            if timeframe in ["1m", "5m", "15m", "30m"]:
                start_date = end_date - timedelta(days=7)  # 1 week for intraday
            elif timeframe in ["1h", "4h"]:
                start_date = end_date - timedelta(days=60)  # 2 months for hourly
            elif timeframe == "1d":
                start_date = end_date - timedelta(days=365)  # 1 year for daily
            elif timeframe == "1w":
                start_date = end_date - timedelta(days=730)  # 2 years for weekly
            elif timeframe == "1mo":
                start_date = end_date - timedelta(
                    days=1825
                )  # 5 years for monthly (60 data points)
            elif timeframe == "3mo":
                start_date = end_date - timedelta(
                    days=3650
                )  # 10 years for quarterly (40 points)
            elif timeframe == "1y":
                start_date = end_date - timedelta(days=3650)  # 10 years for yearly
            elif timeframe == "5y":
                start_date = end_date - timedelta(days=7300)  # 20 years for 5-year
            else:
                start_date = end_date - timedelta(days=730)  # Default 2 years

            df = self.data_provider.fetch_ohlc(
                ticker=ticker,
                timeframe=timeframe,
                start_date=start_date.strftime("%Y-%m-%d"),
                end_date=end_date.strftime("%Y-%m-%d"),
            )
            df = validate_ohlc(df)
            return {"market_data": df}
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "fetch_market_data",
                        "action": "error",
                        "ticker": ticker,
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {
                "error": f"Failed to fetch market data: {str(e)}",
                "market_data": pd.DataFrame(),
            }

    def _fetch_fundamental_data(self, state: ComprehensiveState) -> dict:
        """Fetch fundamental data from Yahoo Finance with asset-type awareness."""
        ticker = state["ticker"]

        try:
            # Detect asset type
            asset_type = DataProvider.detect_asset_type(ticker)
            asset_characteristics = DataProvider.get_asset_characteristics(asset_type)

            logger.info(
                json.dumps(
                    {
                        "node": "fetch_fundamental_data",
                        "action": "asset_type_detected",
                        "ticker": ticker,
                        "asset_type": asset_type,
                        "has_fundamentals": asset_characteristics["has_fundamentals"],
                        "timestamp": time.time(),
                    }
                )
            )

            # Only fetch traditional fundamentals for stocks
            if asset_type == "stock" and asset_characteristics["has_fundamentals"]:
                fundamental_data = self.data_provider.fetch_fundamentals(ticker)
                logger.info(
                    json.dumps(
                        {
                            "node": "fetch_fundamental_data",
                            "action": "fetched_stock_fundamentals",
                            "ticker": ticker,
                            "timestamp": time.time(),
                        }
                    )
                )
            else:
                # For non-stock assets, create placeholder with asset type info
                fundamental_data = {
                    "asset_type": asset_type,
                    "has_traditional_fundamentals": False,
                    "note": f"Traditional fundamental data not applicable for {asset_type} assets. "
                    f"Analysis will focus on {', '.join(asset_characteristics['analysis_focus'][:3])}.",
                }
                logger.info(
                    json.dumps(
                        {
                            "node": "fetch_fundamental_data",
                            "action": "skipped_fundamentals",
                            "ticker": ticker,
                            "asset_type": asset_type,
                            "reason": "Not applicable for this asset type",
                            "timestamp": time.time(),
                        }
                    )
                )

            return {"fundamental_data": fundamental_data}
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "fetch_fundamental_data",
                        "action": "error",
                        "ticker": ticker,
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"fundamental_data": {}}

    def _fetch_news_data(self, state: ComprehensiveState) -> dict:
        """Fetch news data from Yahoo Finance."""
        ticker = state["ticker"]

        try:
            news_data = self.data_provider.fetch_news(ticker)
            return {"news_data": news_data}
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "fetch_news_data",
                        "action": "error",
                        "ticker": ticker,
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"news_data": []}

    # Technical analysis nodes

    def _run_indicator_agent(self, state: ComprehensiveState) -> dict:
        """Run indicator analysis."""
        try:
            # Create technical workflow state for the agent
            technical_state: TechnicalWorkflowState = {
                "ticker": state["ticker"],
                "timeframe": state["timeframe"],
                "market_data": {
                    "ohlc_data": state["market_data"].to_dict(orient="records")
                },
                "messages": [],
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
            }
            result_state = self.indicator_agent.run(technical_state)
            result = result_state.get("indicators", {})
            return {
                "indicator_analysis": result,
                "messages": result_state.get(
                    "messages", []
                ),  # Pass through agent messages
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "indicator_analysis",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"indicator_analysis": {}}

    def _run_pattern_agent(self, state: ComprehensiveState) -> dict:
        """Run pattern analysis."""
        try:
            # Create technical workflow state for the agent
            technical_state: TechnicalWorkflowState = {
                "ticker": state["ticker"],
                "timeframe": state["timeframe"],
                "market_data": {
                    "ohlc_data": state["market_data"].to_dict(orient="records")
                },
                "messages": [],
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
            }
            result_state = self.pattern_agent.run(technical_state)
            result = result_state.get("patterns", {})
            return {
                "pattern_analysis": result,
                "messages": result_state.get("messages", []),
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "pattern_analysis",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"pattern_analysis": {}}

    def _run_trend_agent(self, state: ComprehensiveState) -> dict:
        """Run trend analysis."""
        try:
            # Create technical workflow state for the agent
            technical_state: TechnicalWorkflowState = {
                "ticker": state["ticker"],
                "timeframe": state["timeframe"],
                "market_data": {
                    "ohlc_data": state["market_data"].to_dict(orient="records")
                },
                "messages": [],
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
            }
            result_state = self.trend_agent.run(technical_state)
            result = result_state.get("trends", {})
            return {
                "trend_analysis": result,
                "messages": result_state.get("messages", []),
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "trend_analysis",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"trend_analysis": {}}

    def _run_decision_agent(self, state: ComprehensiveState) -> dict:
        """Run decision analysis."""
        try:
            # Create technical workflow state for the agent
            technical_state: TechnicalWorkflowState = {
                "ticker": state["ticker"],
                "timeframe": state["timeframe"],
                "market_data": {
                    "ohlc_data": state["market_data"].to_dict(orient="records")
                },
                "indicators": state.get("indicator_analysis", {}),
                "patterns": state.get("pattern_analysis", {}),
                "trends": state.get("trend_analysis", {}),
                "messages": [],
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
            }
            result_state = self.decision_agent.run(technical_state)
            result = result_state.get("decision", {})
            return {
                "decision_analysis": result,
                "messages": result_state.get("messages", []),
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "decision_analysis",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"decision_analysis": {}}

    # Fundamental analysis nodes

    def _run_fundamentals_agent(self, state: ComprehensiveState) -> dict:
        """Run fundamentals analysis."""
        try:
            # Create technical workflow state for the agent
            technical_state: TechnicalWorkflowState = {
                "ticker": state["ticker"],
                "timeframe": state["timeframe"],
                "fundamental_data": state.get("fundamental_data", {}),
                "messages": [],
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
            }
            result_state = self.fundamentals_agent.run(technical_state)
            result = result_state.get("fundamentals", {})
            return {
                "fundamentals_analysis": result,
                "messages": result_state.get("messages", []),
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "fundamentals_analysis",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"fundamentals_analysis": {}}

    def _run_sentiment_agent(self, state: ComprehensiveState) -> dict:
        """Run sentiment analysis."""
        try:
            # Create technical workflow state for the agent
            technical_state: TechnicalWorkflowState = {
                "ticker": state["ticker"],
                "timeframe": state["timeframe"],
                "news_data": state.get("news_data", []),
                "messages": [],
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
            }
            result_state = self.sentiment_agent.run(technical_state)
            result = result_state.get("sentiment", {})
            return {
                "sentiment_analysis": result,
                "messages": result_state.get("messages", []),
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "sentiment_analysis",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"sentiment_analysis": {}}

    def _run_news_agent(self, state: ComprehensiveState) -> dict:
        """Run news analysis."""
        try:
            # Create technical workflow state for the agent
            technical_state: TechnicalWorkflowState = {
                "ticker": state["ticker"],
                "timeframe": state["timeframe"],
                "news_data": state.get("news_data", []),
                "messages": [],
                "_cost_tracker": state.get(
                    "_cost_tracker"
                ),  # Pass cost tracker to agents
            }
            result_state = self.news_agent.run(technical_state)
            result = result_state.get("news", {})
            return {
                "news_analysis": result,
                "messages": result_state.get("messages", []),
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "news_analysis",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"news_analysis": {}}

    # Synthesis node

    def _run_technical_analyst(self, state: ComprehensiveState) -> dict:
        """Run technical analyst synthesis."""
        try:
            # Extract investment style if available
            investment_style = None
            if "config" in state:
                config = state["config"]
                if isinstance(config, dict):
                    investment_style = config.get("investment_style")
            if not investment_style:
                investment_style = state.get("investment_style")

            result = self.technical_analyst.analyze(
                state["ticker"],
                state["timeframe"],
                state.get("indicator_analysis", {}),
                state.get("pattern_analysis", {}),
                state.get("trend_analysis", {}),
                state.get("fundamentals_analysis", {}),
                investment_style=investment_style,
            )
            return {
                "technical_analyst": result,
                "messages": [result.get("assessment", "")],
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "technical_analyst",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"technical_analyst": {}}

    # Research debate node

    def _run_researcher_team(self, state: ComprehensiveState) -> dict:
        """Run researcher team debate."""
        try:
            # Extract investment style if available
            investment_style = None
            if "config" in state:
                config = state["config"]
                if isinstance(config, dict):
                    investment_style = config.get("investment_style")
            if not investment_style:
                investment_style = state.get("investment_style")

            # Combine all analysis for debate
            all_analysis = {
                "indicator_analysis": state.get("indicator_analysis", {}),
                "pattern_analysis": state.get("pattern_analysis", {}),
                "trend_analysis": state.get("trend_analysis", {}),
                "decision_analysis": state.get("decision_analysis", {}),
                "fundamentals_analysis": state.get("fundamentals_analysis", {}),
                "sentiment_analysis": state.get("sentiment_analysis", {}),
                "news_analysis": state.get("news_analysis", {}),
                "technical_analyst": state.get("technical_analyst", {}),
            }

            result = self.researcher_team.analyze(
                state["ticker"],
                state["timeframe"],
                all_analysis,
                investment_style=investment_style,
            )

            # Format the complete debate output as markdown
            debate_output = "# Research Team Investment Debate\n\n"

            # Bull Case
            bullish_case = result.get("bullish_case", {})
            debate_output += "## 🐂 Bull Researcher\n\n"
            debate_output += (
                bullish_case.get("argument", "No bullish case generated") + "\n\n"
            )

            # Bear Case
            bearish_case = result.get("bearish_case", {})
            debate_output += "## 🐻 Bear Researcher\n\n"
            debate_output += (
                bearish_case.get("argument", "No bearish case generated") + "\n\n"
            )

            # Synthesis
            synthesis = result.get("synthesis", {})
            debate_output += "## ⚖️ Neutral Moderator - Balanced Assessment\n\n"
            debate_output += (
                synthesis.get("synthesis", "No synthesis generated") + "\n\n"
            )

            # Signal summary
            debate_output += "### Signal Summary\n\n"
            debate_output += (
                f"- **Bullish Signals**: {bullish_case.get('signal_count', 0)}\n"
            )
            debate_output += (
                f"- **Bearish Signals**: {bearish_case.get('signal_count', 0)}\n"
            )
            debate_output += f"- **Overall Lean**: {synthesis.get('overall_lean', 'neutral').upper()}\n"

            return {
                "researcher_synthesis": result,
                "messages": state.get("messages", [])
                + [
                    {
                        "agent_name": "researcher_team",
                        "content": debate_output,
                        "metadata": {
                            "bullish_case": bullish_case,
                            "bearish_case": bearish_case,
                            "synthesis": synthesis,
                        },
                    }
                ],
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "researcher_debate",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"researcher_synthesis": {}}

    # Risk assessment node

    def _run_risk_manager(self, state: ComprehensiveState) -> dict:
        """Run risk manager assessment."""
        try:
            # Extract investment style if available
            investment_style = None
            if "config" in state:
                config = state["config"]
                if isinstance(config, dict):
                    investment_style = config.get("investment_style")
            if not investment_style:
                investment_style = state.get("investment_style")

            all_analysis = {
                "indicator_analysis": state.get("indicator_analysis", {}),
                "pattern_analysis": state.get("pattern_analysis", {}),
                "trend_analysis": state.get("trend_analysis", {}),
                "decision_analysis": state.get("decision_analysis", {}),
                "fundamentals_analysis": state.get("fundamentals_analysis", {}),
                "sentiment_analysis": state.get("sentiment_analysis", {}),
                "news_analysis": state.get("news_analysis", {}),
                "technical_analyst": state.get("technical_analyst", {}),
            }

            result = self.risk_manager.analyze(
                state["ticker"],
                state["timeframe"],
                state.get("market_data", pd.DataFrame()),
                all_analysis,
                state.get("researcher_synthesis", {}),
                investment_style=investment_style,
            )
            return {
                "risk_assessment": result,
                "messages": [result.get("assessment", "")],
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "risk_assessment",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"risk_assessment": {}}

    # Final decision node

    def _run_portfolio_manager(self, state: ComprehensiveState) -> dict:
        """Run portfolio manager final decision."""
        try:
            # Extract investment style if available
            investment_style = None
            if "config" in state:
                config = state["config"]
                if isinstance(config, dict):
                    investment_style = config.get("investment_style")
            if not investment_style:
                investment_style = state.get("investment_style")

            all_analysis = {
                "indicator_analysis": state.get("indicator_analysis", {}),
                "pattern_analysis": state.get("pattern_analysis", {}),
                "trend_analysis": state.get("trend_analysis", {}),
                "decision_analysis": state.get("decision_analysis", {}),
                "fundamentals_analysis": state.get("fundamentals_analysis", {}),
                "sentiment_analysis": state.get("sentiment_analysis", {}),
                "news_analysis": state.get("news_analysis", {}),
                "technical_analyst": state.get("technical_analyst", {}),
            }

            result = self.portfolio_manager.analyze(
                state["ticker"],
                state["timeframe"],
                all_analysis,
                state.get("researcher_synthesis", {}),
                state.get("risk_assessment", {}),
                investment_style=investment_style,
            )
            return {
                "portfolio_decision": result,
                "messages": [result.get("rationale", "")],
            }
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "portfolio_decision",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"portfolio_decision": {}}

    # Cross-validation node

    def _detect_contradictions(self, state: ComprehensiveState) -> dict:
        """Detect contradictions between agents."""
        contradictions = []

        # Check technical vs fundamental alignment
        technical_analyst = state.get("technical_analyst", {})
        alignment = technical_analyst.get("alignment", {})
        alignment_score = alignment.get("alignment_score", 1.0)

        if alignment_score < 0.5:
            contradictions.append(
                {
                    "type": "technical_fundamental_divergence",
                    "severity": "high" if alignment_score == 0 else "moderate",
                    "description": f"Technical and fundamental analysis diverge (alignment: {alignment_score:.2f})",
                }
            )

        # Check research debate balance
        researcher_synthesis = state.get("researcher_synthesis", {})
        synthesis = researcher_synthesis.get("synthesis", {})
        signal_ratio = synthesis.get("signal_ratio", 0.5)

        if 0.4 < signal_ratio < 0.6:
            contradictions.append(
                {
                    "type": "mixed_signals",
                    "severity": "moderate",
                    "description": f"Mixed signals from research team (ratio: {signal_ratio:.2f})",
                }
            )

        # Check recommendation vs risk
        portfolio_decision = state.get("portfolio_decision", {})
        decision = portfolio_decision.get("decision", {})
        recommendation = decision.get("recommendation", "hold")

        risk_assessment = state.get("risk_assessment", {})
        risk_score = risk_assessment.get("risk_score", 50)

        if recommendation == "buy" and risk_score > 70:
            contradictions.append(
                {
                    "type": "recommendation_risk_mismatch",
                    "severity": "high",
                    "description": f"Buy recommendation despite high risk score ({risk_score:.1f}/100)",
                }
            )

        return {"contradictions": contradictions}

    # Chart generation node

    def _generate_chart(self, state: ComprehensiveState) -> dict:
        """Generate analysis chart."""
        try:
            market_data = state.get("market_data", pd.DataFrame())

            # Skip chart generation if market data is empty or missing
            if market_data.empty:
                logger.warning(
                    json.dumps(
                        {
                            "node": "generate_chart",
                            "action": "skipped",
                            "ticker": state["ticker"],
                            "reason": "market_data is empty",
                            "timestamp": time.time(),
                        }
                    )
                )
                return {"chart_path": ""}

            # generate_candlestick_chart returns (Figure, filepath) tuple
            fig, chart_path = self.chart_generator.generate_candlestick_chart(
                df=market_data,
                ticker=state["ticker"],
                timeframe=state["timeframe"],
                title=f"{state['ticker']} - {state['timeframe']} Analysis",
            )
            return {"chart_path": chart_path or ""}
        except Exception as e:
            logger.error(
                json.dumps(
                    {
                        "node": "generate_chart",
                        "action": "error",
                        "ticker": state["ticker"],
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )
            return {"chart_path": ""}