|
| 1 | +"""Debate opening statements must not prompt a rebuttal of a nonexistent argument. |
| 2 | +
|
| 3 | +Regressions for #1176: the first speaker in each debate receives an empty |
| 4 | +``current_response`` (or empty opponent responses) yet the prompt demands it |
| 5 | +rebut the other side -- so models fabricate the opponent's position. Opening |
| 6 | +speakers now get an explicit "present your own case" instruction instead of a |
| 7 | +rebuttal framing. |
| 8 | +""" |
| 9 | + |
| 10 | +from unittest.mock import MagicMock |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from tradingagents.agents.researchers.bear_researcher import create_bear_researcher |
| 15 | +from tradingagents.agents.researchers.bull_researcher import create_bull_researcher |
| 16 | +from tradingagents.agents.risk_mgmt.aggressive_debator import create_aggressive_debator |
| 17 | +from tradingagents.agents.risk_mgmt.conservative_debator import create_conservative_debator |
| 18 | +from tradingagents.agents.risk_mgmt.neutral_debator import create_neutral_debator |
| 19 | + |
| 20 | + |
| 21 | +def _captured_prompt(factory, state) -> str: |
| 22 | + """Run one agent node with a fake LLM and return the prompt it received.""" |
| 23 | + llm = MagicMock() |
| 24 | + response = MagicMock() |
| 25 | + response.content = "argument" |
| 26 | + llm.invoke.return_value = response |
| 27 | + factory(llm)(state) |
| 28 | + return llm.invoke.call_args.args[0] |
| 29 | + |
| 30 | + |
| 31 | +def _investment_state(current_response: str) -> dict: |
| 32 | + return { |
| 33 | + "company_of_interest": "NVDA", |
| 34 | + "asset_type": "stock", |
| 35 | + "market_report": "market", |
| 36 | + "sentiment_report": "sentiment", |
| 37 | + "news_report": "news", |
| 38 | + "fundamentals_report": "fundamentals", |
| 39 | + "investment_debate_state": { |
| 40 | + "history": "", |
| 41 | + "bull_history": "", |
| 42 | + "bear_history": "", |
| 43 | + "current_response": current_response, |
| 44 | + "count": 0, |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +def _risk_state(aggressive: str = "", conservative: str = "", neutral: str = "") -> dict: |
| 50 | + return { |
| 51 | + "company_of_interest": "NVDA", |
| 52 | + "asset_type": "stock", |
| 53 | + "market_report": "market", |
| 54 | + "sentiment_report": "sentiment", |
| 55 | + "news_report": "news", |
| 56 | + "fundamentals_report": "fundamentals", |
| 57 | + "trader_investment_plan": "trader plan", |
| 58 | + "risk_debate_state": { |
| 59 | + "history": "", |
| 60 | + "aggressive_history": "", |
| 61 | + "conservative_history": "", |
| 62 | + "neutral_history": "", |
| 63 | + "current_aggressive_response": aggressive, |
| 64 | + "current_conservative_response": conservative, |
| 65 | + "current_neutral_response": neutral, |
| 66 | + "count": 0, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | +# Bull / Bear researchers |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.unit |
| 77 | +def test_bull_opening_does_not_reference_bear_argument(): |
| 78 | + prompt = _captured_prompt(create_bull_researcher, _investment_state(current_response="")) |
| 79 | + assert "has not spoken yet" in prompt |
| 80 | + assert "Last bear argument:" not in prompt |
| 81 | + assert "rebut any bear argument" in prompt |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.unit |
| 85 | +def test_bull_rebuttal_keeps_real_bear_argument(): |
| 86 | + prompt = _captured_prompt( |
| 87 | + create_bull_researcher, |
| 88 | + _investment_state(current_response="Bear Analyst: rates are too high"), |
| 89 | + ) |
| 90 | + assert "Last bear argument: Bear Analyst: rates are too high" in prompt |
| 91 | + assert "has not spoken yet" not in prompt |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.unit |
| 95 | +def test_bear_opening_does_not_reference_bull_argument(): |
| 96 | + prompt = _captured_prompt(create_bear_researcher, _investment_state(current_response="")) |
| 97 | + assert "has not spoken yet" in prompt |
| 98 | + assert "Last bull argument:" not in prompt |
| 99 | + assert "rebut any bull argument" in prompt |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.unit |
| 103 | +def test_bear_rebuttal_keeps_real_bull_argument(): |
| 104 | + prompt = _captured_prompt( |
| 105 | + create_bear_researcher, |
| 106 | + _investment_state(current_response="Bull Analyst: AI demand is exploding"), |
| 107 | + ) |
| 108 | + assert "Last bull argument: Bull Analyst: AI demand is exploding" in prompt |
| 109 | + assert "has not spoken yet" not in prompt |
| 110 | + |
| 111 | + |
| 112 | +# --------------------------------------------------------------------------- |
| 113 | +# Risk debate (Aggressive / Conservative / Neutral) |
| 114 | +# --------------------------------------------------------------------------- |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.unit |
| 118 | +def test_aggressive_opening_does_not_quote_others(): |
| 119 | + prompt = _captured_prompt(create_aggressive_debator, _risk_state()) |
| 120 | + assert "not spoken yet" in prompt |
| 121 | + assert "last arguments from the conservative analyst:" not in prompt.lower() |
| 122 | + assert "last arguments from the neutral analyst:" not in prompt.lower() |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.unit |
| 126 | +def test_aggressive_rebuttal_quotes_spoken_analysts_only(): |
| 127 | + prompt = _captured_prompt( |
| 128 | + create_aggressive_debator, |
| 129 | + _risk_state(conservative="Conservative Analyst: too risky"), |
| 130 | + ) |
| 131 | + assert "Conservative Analyst: too risky" in prompt |
| 132 | + assert "has not spoken yet" not in prompt |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.unit |
| 136 | +def test_conservative_opening_does_not_quote_others(): |
| 137 | + prompt = _captured_prompt(create_conservative_debator, _risk_state()) |
| 138 | + assert "not spoken yet" in prompt |
| 139 | + assert "last arguments from the aggressive analyst:" not in prompt.lower() |
| 140 | + assert "last arguments from the neutral analyst:" not in prompt.lower() |
| 141 | + |
| 142 | + |
| 143 | +@pytest.mark.unit |
| 144 | +def test_neutral_opening_does_not_quote_others(): |
| 145 | + prompt = _captured_prompt(create_neutral_debator, _risk_state()) |
| 146 | + assert "not spoken yet" in prompt |
| 147 | + assert "last arguments from the aggressive analyst:" not in prompt.lower() |
| 148 | + assert "last arguments from the conservative analyst:" not in prompt.lower() |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.unit |
| 152 | +def test_neutral_rebuttal_quotes_spoken_analysts_only(): |
| 153 | + prompt = _captured_prompt( |
| 154 | + create_neutral_debator, |
| 155 | + _risk_state(aggressive="Aggressive Analyst: go all in"), |
| 156 | + ) |
| 157 | + assert "Aggressive Analyst: go all in" in prompt |
| 158 | + assert "has not spoken yet" not in prompt |
0 commit comments