CiscsoPonce Cursor commited on
Commit
d406c40
·
1 Parent(s): 9a0c3ef

fix(debate): start the skeptic on the free router instead of Gemma

Browse files

Gemma's shared pool can refuse a whole hunt, so the skeptic now tries the router, then Lightning, then Gemma before the single-analyst fallback.

Co-authored-by: Cursor <cursoragent@cursor.com>

Files changed (3) hide show
  1. README.md +1 -1
  2. src/agents/debate.py +49 -22
  3. tests/test_ops_fixes.py +22 -0
README.md CHANGED
@@ -77,7 +77,7 @@ START --> [pitcher (Nemotron Ultra 550B)] --> [skeptic (Gemma 4 31B)] --> [judge
77
 
78
  **Free models (OpenRouter `:free` tier, dynamically verified at runtime)**:
79
  - **Pitcher**: `nvidia/nemotron-3-ultra-550b-a55b:free` (also default `STRUCTURED_MODEL` and head of `MODEL_CHAIN`)
80
- - **Skeptic**: `google/gemma-4-31b-it:free` (stage-2 fallback across both chains)
81
  - **Judge**: `dots-studio/dots-3-note-preview:free` (dedicated JSON role)
82
  - **Fallback chain (`MODEL_CHAIN` in `src/llm.py`)**: `nemotron-3-ultra-550b-a55b:free` → `gemma-4-31b-it:free` → `dots-3-note-preview:free` → `nvidia/nemotron-3.5-lightning:free` → `openrouter/free` router catch-all
83
  - **Structured chain (`STRUCTURED_CHAIN`)**: walks `nemotron-3-ultra-550b-a55b:free` → `gemma-4-31b-it:free` → `dots-3-note-preview:free` → `openrouter/free` so framework fields survive rate limits.
 
77
 
78
  **Free models (OpenRouter `:free` tier, dynamically verified at runtime)**:
79
  - **Pitcher**: `nvidia/nemotron-3-ultra-550b-a55b:free` (also default `STRUCTURED_MODEL` and head of `MODEL_CHAIN`)
80
+ - **Skeptic**: `openrouter/free`, then Lightning, then Gemma. Gemma's shared pool can refuse a whole hunt, so it is no longer the first skeptic.
81
  - **Judge**: `dots-studio/dots-3-note-preview:free` (dedicated JSON role)
82
  - **Fallback chain (`MODEL_CHAIN` in `src/llm.py`)**: `nemotron-3-ultra-550b-a55b:free` → `gemma-4-31b-it:free` → `dots-3-note-preview:free` → `nvidia/nemotron-3.5-lightning:free` → `openrouter/free` router catch-all
83
  - **Structured chain (`STRUCTURED_CHAIN`)**: walks `nemotron-3-ultra-550b-a55b:free` → `gemma-4-31b-it:free` → `dots-3-note-preview:free` → `openrouter/free` so framework fields survive rate limits.
src/agents/debate.py CHANGED
@@ -37,7 +37,9 @@ logger = get_logger(__name__)
37
  # 2026-10-12 benchmark showed returning empty/truncated completions and
38
  # rate-limiting constantly. Ultra-550B is the strongest free reasoner.
39
  PITCHER_MODEL = os.getenv("DEBATE_PITCHER_MODEL", "nvidia/nemotron-3-ultra-550b-a55b:free")
40
- SKEPTIC_MODEL = os.getenv("DEBATE_SKEPTIC_MODEL", "google/gemma-4-31b-it:free")
 
 
41
  # Judge is the structured-output role; default to the dedicated JSON model so
42
  # it stays DISTINCT from pitcher and skeptic. _judge_model_chain() falls back
43
  # through MODEL_CHAIN if it is rate-limited.
@@ -48,20 +50,42 @@ JUDGE_MODEL = os.getenv("DEBATE_JUDGE_MODEL", "dots-studio/dots-3-note-preview:f
48
  _DEBATE_DELAY = int(os.getenv("DEBATE_DELAY_SECONDS", "10"))
49
 
50
 
51
- def _fallback_for(primary: str) -> str:
52
- """Next free model that is not this role and not another role's primary.
53
 
54
- A rate-limited skeptic must not fall through onto the pitcher's model:
55
- four regions were doing that at once and exhausting both.
 
56
  """
57
  reserved = {PITCHER_MODEL, SKEPTIC_MODEL, JUDGE_MODEL} - {primary}
58
- for m in MODEL_CHAIN:
59
- if m != primary and m not in reserved:
60
- return m
61
- for m in MODEL_CHAIN:
62
- if m != primary:
63
- return m
64
- return MODEL_CHAIN[-1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
 
67
  class DebateState(TypedDict, total=False):
@@ -149,16 +173,19 @@ def _make_llm(model: str, max_tokens: int = 2048):
149
  )
150
 
151
 
152
- def _resilient_llm_call(model: str, prompt: str, fallback_model: str) -> str:
153
- """Call ``model``, then ``fallback_model``.
154
 
155
- A 429 waits once (the free tier is about 8 requests a minute) and moves
156
- on. Camping on the same model for 15s, 30s, 45s and 60s is what stacked
157
- four regions onto Gemma and then onto the pitcher's model.
158
  """
 
 
 
 
159
  last_error = None
160
  seen = []
161
- for candidate in (model, fallback_model):
162
  if not candidate or candidate in seen:
163
  continue
164
  seen.append(candidate)
@@ -192,14 +219,14 @@ def _resilient_llm_call(model: str, prompt: str, fallback_model: str) -> str:
192
  time.sleep(5)
193
  continue
194
  switched = True
195
- if switched and candidate == model:
196
  logger.warning(
197
- "Primary model %s exhausted, switching to fallback %s",
198
- model, fallback_model,
199
  )
200
 
201
  raise RuntimeError(
202
- f"Both {model} and {fallback_model} failed due to rate limits. Last error: {last_error}"
203
  )
204
 
205
 
 
37
  # 2026-10-12 benchmark showed returning empty/truncated completions and
38
  # rate-limiting constantly. Ultra-550B is the strongest free reasoner.
39
  PITCHER_MODEL = os.getenv("DEBATE_PITCHER_MODEL", "nvidia/nemotron-3-ultra-550b-a55b:free")
40
+ # Gemma's free pool is a shared upstream limit. Spacing cannot clear it, so
41
+ # the skeptic starts on the OpenRouter router and only then tries a fixed model.
42
+ SKEPTIC_MODEL = os.getenv("DEBATE_SKEPTIC_MODEL", "openrouter/free")
43
  # Judge is the structured-output role; default to the dedicated JSON model so
44
  # it stays DISTINCT from pitcher and skeptic. _judge_model_chain() falls back
45
  # through MODEL_CHAIN if it is rate-limited.
 
50
  _DEBATE_DELAY = int(os.getenv("DEBATE_DELAY_SECONDS", "10"))
51
 
52
 
53
+ def _backup_candidates(primary: str) -> list[str]:
54
+ """Free models to try after ``primary``.
55
 
56
+ Other roles are skipped so the skeptic does not land on the pitcher.
57
+ The router and Lightning come before Gemma, whose shared pool can be
58
+ refused for a whole hunt.
59
  """
60
  reserved = {PITCHER_MODEL, SKEPTIC_MODEL, JUDGE_MODEL} - {primary}
61
+ gemma = "google/gemma-4-31b-it:free"
62
+ preferred = [
63
+ "openrouter/free",
64
+ "nvidia/nemotron-3.5-lightning:free",
65
+ ]
66
+ ordered = preferred + [m for m in MODEL_CHAIN if m not in preferred and m != gemma]
67
+ if gemma in MODEL_CHAIN:
68
+ ordered.append(gemma)
69
+ out = []
70
+ for m in ordered:
71
+ if m and m != primary and m not in reserved and m not in out:
72
+ out.append(m)
73
+ if not out:
74
+ for m in MODEL_CHAIN:
75
+ if m != primary and m not in out:
76
+ out.append(m)
77
+ return out
78
+
79
+
80
+ def _fallback_for(primary: str) -> str:
81
+ """First backup for ``primary``. Kept for callers that want one model."""
82
+ chain = _backup_candidates(primary)
83
+ return chain[0] if chain else primary
84
+
85
+
86
+ def _call_chain(primary: str) -> list[str]:
87
+ """Primary plus two backups. The debate used to die after the second."""
88
+ return [primary] + _backup_candidates(primary)[:2]
89
 
90
 
91
  class DebateState(TypedDict, total=False):
 
173
  )
174
 
175
 
176
+ def _resilient_llm_call(model: str, prompt: str, fallback_model: str | None = None) -> str:
177
+ """Call up to three free models.
178
 
179
+ A 429 waits once (about 8 requests a minute) and moves on. The chain is
180
+ the role model, then two backups that are not the other roles.
 
181
  """
182
+ chain = _call_chain(model)
183
+ if fallback_model and fallback_model not in chain:
184
+ chain = [model, fallback_model] + [m for m in chain if m != model]
185
+ chain = chain[:3]
186
  last_error = None
187
  seen = []
188
+ for index, candidate in enumerate(chain):
189
  if not candidate or candidate in seen:
190
  continue
191
  seen.append(candidate)
 
219
  time.sleep(5)
220
  continue
221
  switched = True
222
+ if switched and index < len(chain) - 1:
223
  logger.warning(
224
+ "Model %s exhausted, switching to %s",
225
+ candidate, chain[index + 1],
226
  )
227
 
228
  raise RuntimeError(
229
+ f"Debate models failed ({', '.join(seen)}). Last error: {last_error}"
230
  )
231
 
232
 
tests/test_ops_fixes.py CHANGED
@@ -124,8 +124,12 @@ import src.agents.debate as debate
124
  pitcher = debate.PITCHER_MODEL
125
  skeptic_fb = debate._fallback_for(debate.SKEPTIC_MODEL)
126
  pitcher_fb = debate._fallback_for(debate.PITCHER_MODEL)
 
 
127
  check("skeptic backup is not the pitcher", skeptic_fb != pitcher, skeptic_fb)
128
  check("pitcher backup is not the skeptic", pitcher_fb != debate.SKEPTIC_MODEL, pitcher_fb)
 
 
129
 
130
  calls = []
131
  sleeps = []
@@ -156,6 +160,24 @@ check("429 switches after one wait", text == "bear case" and calls.count(debate.
156
  check("429 does not camp on the same model", calls.count(debate.SKEPTIC_MODEL) < 2)
157
  check("fallback call is the non-pitcher model", calls[-1] == skeptic_fb, calls)
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  overlap = {"n": 0, "max": 0}
160
  gate = threading.Lock()
161
 
 
124
  pitcher = debate.PITCHER_MODEL
125
  skeptic_fb = debate._fallback_for(debate.SKEPTIC_MODEL)
126
  pitcher_fb = debate._fallback_for(debate.PITCHER_MODEL)
127
+ skeptic_chain = debate._call_chain(debate.SKEPTIC_MODEL)
128
+ check("skeptic starts on the router", debate.SKEPTIC_MODEL == "openrouter/free", debate.SKEPTIC_MODEL)
129
  check("skeptic backup is not the pitcher", skeptic_fb != pitcher, skeptic_fb)
130
  check("pitcher backup is not the skeptic", pitcher_fb != debate.SKEPTIC_MODEL, pitcher_fb)
131
+ check("skeptic gets two backups", len(skeptic_chain) == 3, skeptic_chain)
132
+ check("gemma is not the first skeptic", skeptic_chain[0] != "google/gemma-4-31b-it:free", skeptic_chain)
133
 
134
  calls = []
135
  sleeps = []
 
160
  check("429 does not camp on the same model", calls.count(debate.SKEPTIC_MODEL) < 2)
161
  check("fallback call is the non-pitcher model", calls[-1] == skeptic_fb, calls)
162
 
163
+ third_calls = []
164
+
165
+
166
+ class _LLMTwoFails:
167
+ def __init__(self, model):
168
+ self.model = model
169
+
170
+ def invoke(self, prompt):
171
+ third_calls.append(self.model)
172
+ if self.model != skeptic_chain[-1]:
173
+ raise RuntimeError("429 Too Many Requests")
174
+ return _Msg("third model")
175
+
176
+
177
+ debate._make_llm = lambda model, max_tokens=2048: _LLMTwoFails(model)
178
+ third = debate._resilient_llm_call(debate.SKEPTIC_MODEL, "prompt")
179
+ check("third model runs after two rate limits", third == "third model" and third_calls == skeptic_chain, third_calls)
180
+
181
  overlap = {"n": 0, "max": 0}
182
  gate = threading.Lock()
183