File size: 9,852 Bytes
645673f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Multi-agent investment debate: Pitcher -> Skeptic -> Judge.

The debate replaces the single-LLM analyst call with three specialised
agents that produce a more rigorous, hallucination-resistant verdict.

Toggle: set ``USE_DEBATE=true`` in the environment to enable.

Architecture (LangGraph subgraph):
    pitcher_node (Gemma)  -> bull_case
    skeptic_node (Mistral) -> bear_case
    judge_node   (Nemotron) -> InvestmentVerdict
"""

import os
import warnings
from typing import TypedDict

from langgraph.graph import StateGraph, START, END
from langgraph.types import RetryPolicy

from src.core.logger import get_logger
from src.llm import MODEL_CHAIN

logger = get_logger(__name__)

PITCHER_MODEL = os.getenv("DEBATE_PITCHER_MODEL", "google/gemma-3-27b-it:free")
SKEPTIC_MODEL = os.getenv("DEBATE_SKEPTIC_MODEL", "mistralai/mistral-small-3.1-24b-instruct:free")
JUDGE_MODEL = os.getenv("DEBATE_JUDGE_MODEL", MODEL_CHAIN[0])


class DebateState(TypedDict, total=False):
    """Internal state for the debate subgraph."""
    ticker: str
    company_name: str
    financial_data_summary: str
    deep_fundamentals: str
    sec_context: str
    strategy: str
    price: float
    eps: float
    book_value: float
    ebitda: float
    bull_case: str
    bear_case: str
    final_verdict: str


def _make_llm(model: str, max_tokens: int = 2048):
    from langchain_openai import ChatOpenAI

    return ChatOpenAI(
        model=model,
        api_key=os.getenv("OPENROUTER_API_KEY"),
        base_url="https://openrouter.ai/api/v1",
        temperature=0,
        max_tokens=max_tokens,
    )


# ---------------------------------------------------------------------------
# Node 1 — The Pitcher (bullish thesis)
# ---------------------------------------------------------------------------

def pitcher_node(state: DebateState) -> dict:
    """Build the strongest possible investment thesis for the ticker."""
    ticker = state.get("ticker", "")
    company = state.get("company_name", ticker)
    fundamentals = state.get("deep_fundamentals", "")
    sec = state.get("sec_context", "")
    price = state.get("price", 0)
    eps = state.get("eps", 0)
    bv = state.get("book_value", 0)
    ebitda = state.get("ebitda", 0)

    prompt = (
        f"You are a bullish stock pitcher. Write the strongest possible "
        f"investment thesis for {company} ({ticker}).\n\n"
        f"HARD DATA: Price=${price} | EPS={eps} | Book/Share={bv} | EBITDA={ebitda}\n\n"
    )
    if fundamentals:
        prompt += f"FUNDAMENTALS:\n{fundamentals[:3000]}\n\n"
    if sec:
        prompt += f"SEC FILINGS:\n{sec[:2000]}\n\n"

    prompt += (
        "Focus on:\n"
        "1. Insider activity (buying signals)\n"
        "2. The ONE catalyst that could drive the stock higher\n"
        "3. Valuation upside (margin of safety math)\n"
        "4. Competitive advantages or turnaround signals\n\n"
        "Be specific and data-driven. Only cite facts present in the data above."
    )

    llm = _make_llm(PITCHER_MODEL)
    try:
        response = llm.invoke(prompt)
        bull_case = response.content
    except Exception as exc:
        logger.warning("Pitcher (%s) failed: %s — trying fallback", PITCHER_MODEL, exc)
        fallback = _make_llm(MODEL_CHAIN[-1])
        response = fallback.invoke(prompt)
        bull_case = response.content

    logger.info("Pitcher delivered bull case for %s (%d chars)", ticker, len(bull_case))
    return {"bull_case": bull_case}


# ---------------------------------------------------------------------------
# Node 2 — The Skeptic (bearish challenge)
# ---------------------------------------------------------------------------

def skeptic_node(state: DebateState) -> dict:
    """Challenge the bull case with skeptical analysis grounded in data."""
    ticker = state.get("ticker", "")
    company = state.get("company_name", ticker)
    bull_case = state.get("bull_case", "")
    fundamentals = state.get("deep_fundamentals", "")
    sec = state.get("sec_context", "")
    price = state.get("price", 0)
    eps = state.get("eps", 0)
    bv = state.get("book_value", 0)
    ebitda = state.get("ebitda", 0)

    prompt = (
        f"You are a skeptical risk analyst. Read the BULL CASE below and "
        f"tear it apart for {company} ({ticker}).\n\n"
        f"HARD DATA: Price=${price} | EPS={eps} | Book/Share={bv} | EBITDA={ebitda}\n\n"
    )
    if fundamentals:
        prompt += f"FUNDAMENTALS:\n{fundamentals[:3000]}\n\n"
    if sec:
        prompt += f"SEC FILINGS:\n{sec[:2000]}\n\n"

    prompt += (
        f"BULL CASE TO CHALLENGE:\n{bull_case[:3000]}\n\n"
        "Your job:\n"
        "1. Use ONLY the provided data — if a claim has no evidence in the data, "
        "call it out as FABRICATED\n"
        "2. Identify the biggest risk to an investor\n"
        "3. Point out any math errors or unsupported assumptions\n"
        "4. State what specific evidence would prove the bear case right\n\n"
        "Be thorough but concise."
    )

    llm = _make_llm(SKEPTIC_MODEL)
    try:
        response = llm.invoke(prompt)
        bear_case = response.content
    except Exception as exc:
        logger.warning("Skeptic (%s) failed: %s — trying fallback", SKEPTIC_MODEL, exc)
        fallback = _make_llm(MODEL_CHAIN[-1])
        response = fallback.invoke(prompt)
        bear_case = response.content

    logger.info("Skeptic delivered bear case for %s (%d chars)", ticker, len(bear_case))
    return {"bear_case": bear_case}


# ---------------------------------------------------------------------------
# Node 3 — The Judge (final verdict with structured output)
# ---------------------------------------------------------------------------

def judge_node(state: DebateState) -> dict:
    """Synthesise the debate into a structured InvestmentVerdict."""
    from src.models.verdict import InvestmentVerdict
    from src.llm import get_structured_llm

    ticker = state.get("ticker", "")
    company = state.get("company_name", ticker)
    bull_case = state.get("bull_case", "")
    bear_case = state.get("bear_case", "")
    sec = state.get("sec_context", "")
    price = state.get("price", 0)
    eps = state.get("eps", 0)
    bv = state.get("book_value", 0)
    ebitda = state.get("ebitda", 0)
    strategy = state.get("strategy", "GRAHAM CLASSIC")

    prompt = (
        f"You are the Chief Investment Officer making the final call on "
        f"{company} ({ticker}).\n\n"
        f"HARD DATA: Price=${price} | EPS={eps} | Book/Share={bv} | EBITDA={ebitda}\n\n"
        f"BULL CASE (from the Pitcher):\n{bull_case[:3000]}\n\n"
        f"BEAR CASE (from the Skeptic):\n{bear_case[:3000]}\n\n"
    )
    if sec:
        prompt += f"SEC FILINGS:\n{sec[:2000]}\n\n"

    prompt += (
        "RULES:\n"
        "1. If the Skeptic flagged any claims as FABRICATED, you MUST downgrade\n"
        "2. Weight data-backed arguments more heavily\n"
        "3. Use strict " + strategy + " math for the quantitative base\n"
        "4. Your verdict must be one of: STRONG BUY, BUY, WATCH, AVOID\n\n"
        "Produce a structured investment memo with:\n"
        "- quantitative_base: Price vs valuation math\n"
        "- lynch_pitch: The best data-backed catalyst\n"
        "- munger_invert: The key risk from the bear case\n"
        "- verdict: Your final call\n"
        "- bottom_line: One sentence summary"
    )

    structured_llm = get_structured_llm(max_tokens=4096).with_structured_output(
        InvestmentVerdict
    )

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", message="Pydantic serializer warnings")
        result = structured_llm.invoke(prompt)

    verdict_text = result.to_report()
    logger.info("Judge delivered verdict for %s: %s", ticker, result.verdict)

    return {"final_verdict": verdict_text, "_structured_result": result}


# ---------------------------------------------------------------------------
# Compile the debate subgraph
# ---------------------------------------------------------------------------

_debate_retry = RetryPolicy(max_attempts=3, initial_interval=2.0)

_debate_graph = StateGraph(DebateState)
_debate_graph.add_node("pitcher", pitcher_node, retry=_debate_retry)
_debate_graph.add_node("skeptic", skeptic_node, retry=_debate_retry)
_debate_graph.add_node("judge", judge_node, retry=_debate_retry)
_debate_graph.add_edge(START, "pitcher")
_debate_graph.add_edge("pitcher", "skeptic")
_debate_graph.add_edge("skeptic", "judge")
_debate_graph.add_edge("judge", END)

debate_app = _debate_graph.compile()


def run_debate(
    ticker: str,
    company_name: str,
    financial_data_summary: str,
    deep_fundamentals: str,
    sec_context: str,
    strategy: str,
    price: float,
    eps: float,
    book_value: float,
    ebitda: float,
) -> dict:
    """Run the full pitcher -> skeptic -> judge debate for a ticker.

    Returns a dict with ``final_verdict`` (str), ``bull_case``, ``bear_case``,
    and ``_structured_result`` (InvestmentVerdict).
    """
    initial_state: DebateState = {
        "ticker": ticker,
        "company_name": company_name,
        "financial_data_summary": financial_data_summary,
        "deep_fundamentals": deep_fundamentals,
        "sec_context": sec_context,
        "strategy": strategy,
        "price": price,
        "eps": eps,
        "book_value": book_value,
        "ebitda": ebitda,
    }

    result = debate_app.invoke(initial_state)
    return {
        "final_verdict": result.get("final_verdict", ""),
        "bull_case": result.get("bull_case", ""),
        "bear_case": result.get("bear_case", ""),
        "_structured_result": result.get("_structured_result"),
    }


def is_debate_enabled() -> bool:
    """Check if multi-agent debate mode is turned on."""
    return os.getenv("USE_DEBATE", "").lower() in ("true", "1", "yes")