Skip to content

Commit 9faaa29

Browse files
nftpoetristteknium1
authored andcommitted
fix(delegate): inherit parent fallback_chain in _build_child_agent
_build_child_agent constructed child AIAgents without passing fallback_model, leaving _fallback_chain=[] for every subagent. When a subagent hit a rate-limit or credential exhaustion the runtime fallback check (run_agent.py:7486 / 12267) found an empty chain and failed immediately — even though the parent agent was configured with fallback_providers and would have recovered. The cron scheduler already propagates fallback_model correctly (scheduler.py:1038). Fix closes the parity gap by reading the parent's _fallback_chain (the normalised list form accepted by AIAgent's fallback_model parameter) and threading it through. Empty chains coerce to None so AIAgent initialises _fallback_chain=[] as usual rather than iterating an empty list.
1 parent cb33c73 commit 9faaa29

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎tests/tools/test_delegate.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,5 +2403,52 @@ def worker():
24032403
self.assertIsNone(_get_approval_callback())
24042404

24052405

2406+
class TestFallbackModelInheritance(unittest.TestCase):
2407+
"""Subagents must inherit the parent's fallback provider chain."""
2408+
2409+
def test_child_inherits_fallback_chain(self):
2410+
"""_build_child_agent passes parent._fallback_chain as fallback_model."""
2411+
parent = _make_mock_parent(depth=0)
2412+
fallback_entry = {"provider": "openrouter", "model": "gpt-4o-mini", "api_key": "sk-or-x"}
2413+
parent._fallback_chain = [fallback_entry]
2414+
2415+
with patch("run_agent.AIAgent") as MockAgent:
2416+
MockAgent.return_value = MagicMock()
2417+
_build_child_agent(
2418+
task_index=0,
2419+
goal="test fallback inheritance",
2420+
context=None,
2421+
toolsets=None,
2422+
model=None,
2423+
max_iterations=10,
2424+
parent_agent=parent,
2425+
task_count=1,
2426+
)
2427+
2428+
_, kwargs = MockAgent.call_args
2429+
self.assertEqual(kwargs["fallback_model"], [fallback_entry])
2430+
2431+
def test_child_gets_no_fallback_when_parent_chain_empty(self):
2432+
"""When parent._fallback_chain is empty, fallback_model is None."""
2433+
parent = _make_mock_parent(depth=0)
2434+
parent._fallback_chain = []
2435+
2436+
with patch("run_agent.AIAgent") as MockAgent:
2437+
MockAgent.return_value = MagicMock()
2438+
_build_child_agent(
2439+
task_index=0,
2440+
goal="test no fallback",
2441+
context=None,
2442+
toolsets=None,
2443+
model=None,
2444+
max_iterations=10,
2445+
parent_agent=parent,
2446+
task_count=1,
2447+
)
2448+
2449+
_, kwargs = MockAgent.call_args
2450+
self.assertIsNone(kwargs["fallback_model"])
2451+
2452+
24062453
if __name__ == "__main__":
24072454
unittest.main()

‎tools/delegate_tool.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,12 @@ def _child_thinking(text: str) -> None:
10261026
except Exception as exc:
10271027
logger.debug("Could not load delegation reasoning_effort: %s", exc)
10281028

1029+
# Inherit the parent's fallback provider chain so subagents can recover
1030+
# from rate-limits and credential exhaustion exactly like the top-level
1031+
# agent does. _fallback_chain is a list accepted by AIAgent's
1032+
# fallback_model parameter (which handles both list and dict forms).
1033+
parent_fallback = getattr(parent_agent, "_fallback_chain", None) or None
1034+
10291035
child = AIAgent(
10301036
base_url=effective_base_url,
10311037
api_key=effective_api_key,
@@ -1038,6 +1044,7 @@ def _child_thinking(text: str) -> None:
10381044
max_tokens=getattr(parent_agent, "max_tokens", None),
10391045
reasoning_config=child_reasoning,
10401046
prefill_messages=getattr(parent_agent, "prefill_messages", None),
1047+
fallback_model=parent_fallback,
10411048
enabled_toolsets=child_toolsets,
10421049
quiet_mode=True,
10431050
ephemeral_system_prompt=child_prompt,

0 commit comments

Comments
 (0)