Skip to content

The agent that grows with you

fix(tools): surface config-level model_not_found notices in delegation batch reports

liuhao10248557e0a4804d
diff --git a/tests/tools/test_async_delegation.py b/tests/tools/test_async_delegation.py
index dd8c07ab9fc..ac2d56e372f 100644
--- a/tests/tools/test_async_delegation.py
+++ b/tests/tools/test_async_delegation.py
@@ -881,3 +881,86 @@ def test_batch_truncation_banner_marks_only_truncated_task():
# The header banner for task 2 appears after task 1's summary.
assert banner_pos > clean_pos
+
+def test_batch_model_rejection_notice_prepended():
+ """A rejected delegation model must surface ONE config-level notice above
+ the per-task blocks instead of staying buried in each summary (#97654)."""
+ rejection = "HTTP 400: upstage/solar-pro-4 is not a valid model ID"
+ evt = _make_async_evt(
+ is_batch=True,
+ model="upstage/solar-pro-4",
+ goals=["task a", "task b"],
+ results=[
+ {
+ "task_index": 0,
+ "status": "completed",
+ "summary": rejection,
+ "api_calls": 1,
+ "duration_seconds": 0.74,
+ "exit_reason": "max_iterations",
+ "truncated": True,
+ },
+ {
+ "task_index": 1,
+ "status": "completed",
+ "summary": rejection,
+ "api_calls": 1,
+ "duration_seconds": 0.71,
+ "exit_reason": "max_iterations",
+ "truncated": True,
+ },
+ ],
+ )
+ text = format_process_notification(evt)
+ assert text is not None
+ assert "SUBAGENT MODEL REJECTED" in text
+ assert "upstage/solar-pro-4" in text
+ assert "2/2" in text
+ assert "delegation.model" in text
+ # The notice precedes the per-task blocks, not just trails them.
+ assert text.index("SUBAGENT MODEL REJECTED") < text.index("TASK 1/2")
+
+
+def test_batch_model_rejection_notice_absent_when_clean():
+ """Ordinary summaries must not grow a model-rejection notice."""
+ evt = _make_async_evt(
+ is_batch=True,
+ model="upstage/solar-pro4",
+ goals=["task a"],
+ results=[
+ {
+ "task_index": 0,
+ "status": "completed",
+ "summary": "did the work",
+ "api_calls": 3,
+ "exit_reason": "completed",
+ "truncated": False,
+ },
+ ],
+ )
+ text = format_process_notification(evt)
+ assert text is not None
+ assert "SUBAGENT MODEL REJECTED" not in text
+
+
+def test_batch_model_rejection_notice_requires_configured_model_in_text():
+ """A model_not_found pattern naming a DIFFERENT model than the configured
+ delegation model is task-level noise, not a config-level rejection."""
+ evt = _make_async_evt(
+ is_batch=True,
+ model="upstage/solar-pro4",
+ goals=["task a"],
+ results=[
+ {
+ "task_index": 0,
+ "status": "completed",
+ "summary": "HTTP 400: other/model-x is not a valid model ID",
+ "api_calls": 1,
+ "exit_reason": "max_iterations",
+ "truncated": True,
+ },
+ ],
+ )
+ text = format_process_notification(evt)
+ assert text is not None
+ assert "SUBAGENT MODEL REJECTED" not in text
diff --git a/tools/process_registry.py b/tools/process_registry.py
index 5d25bf7384a..926fb55cfa4 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -2960,19 +2960,21 @@ def _format_age(seconds: float) -> str:
return f"{h}h" if m == 0 else f"{h}h{m}m"
-# Model-not-found phrases lifted from agent/error_classifier.py so the
-# delegation batch renderer can spot a config-level rejection without pulling
-# the classifier's failover machinery. Kept in sync by hand.
-_MODEL_NOT_FOUND_PATTERNS = (
- "is not a valid model",
- "invalid model",
- "model not found",
- "model_not_found",
- "does not exist",
- "no such model",
- "unknown model",
- "unsupported model",
-)
+def _model_not_found_patterns() -> "list[str]":
+ """Model-not-found phrases from the failover classifier.
+
+ Imported from ``agent.error_classifier`` so the batch renderer applies
+ the SAME classification the failover path consumes — no hand-copied
+ pattern list to drift. Fails open to a minimal built-in set so a
+ classifier import problem never hides the per-task blocks.
+ (Import approach from PR #97667 by @liuhao1024.)
+ """
+ try:
+ from agent.error_classifier import _MODEL_NOT_FOUND_PATTERNS
+
+ return list(_MODEL_NOT_FOUND_PATTERNS)
+ except Exception:
+ return ["is not a valid model", "model not found", "model_not_found"]
def _delegation_config() -> dict:
@@ -3009,7 +3011,7 @@ def _delegation_model_not_found(results, config) -> bool:
).lower()
if not text or model not in text:
continue
- if any(p in text for p in _MODEL_NOT_FOUND_PATTERNS):
+ if any(p in text for p in _model_not_found_patterns()):
return True
return False
@@ -3105,6 +3107,9 @@ def _format_async_delegation(evt: dict) -> str:
lines.append("--- ERROR ---")
lines.append(f"The batch did not complete successfully: {error}")
return "\n".join(lines)
+ # Config-level rejection notice BEFORE the per-task wall — a rejected
+ # delegation model fails every task identically before doing any
+ # work, and that signal must not stay buried in the task blocks.
_notice = _delegation_model_not_found_notice(results)
if _notice:
lines.append("")