Skip to content

fix(agent): respect configured max iterations for subagents - #3532

Merged
Re-bin merged 1 commit into
HKUDS:mainfrom
boogieLing:R0/issue-970-subagent-max-iterations
Apr 30, 2026
Merged

Re-bin merged 1 commit into
HKUDS:mainfrom
boogieLing:R0/issue-970-subagent-max-iterations

Conversation

@boogieLing

Copy link
Copy Markdown
Contributor

Summary

  • Pass the parent agent's configured max_iterations into SubagentManager
  • Use that value for subagent AgentRunSpec instead of the old hardcoded 15
  • Default directly constructed SubagentManager instances to AgentDefaults().max_tool_iterations
  • Keep subagent limits synchronized when AgentLoop.max_iterations changes at runtime, including updates made through my(action="set", key="max_iterations", ...)
  • Add regression tests for direct construction, AgentLoop wiring, runtime synchronization, and MyTool updates

Root Cause

The main agent loop already reads agents.defaults.maxToolIterations and stores it as self.max_iterations, but subagents did not use that setting. SubagentManager._run_subagent() always passed max_iterations=15 to AgentRunSpec, so long-running subagent work could stop early even when users configured a higher iteration budget.

Fix

This changes subagent iteration budgeting to follow the same runtime limit as the parent agent:

  • AgentLoop passes its configured max_iterations into SubagentManager
  • SubagentManager stores the value and passes it through to AgentRunSpec
  • direct SubagentManager construction falls back to AgentDefaults().max_tool_iterations
  • AgentLoop._sync_subagent_runtime_limits() keeps the subagent manager aligned before each agent loop run
  • MyTool calls the same sync helper after changing max_iterations

Impact

Users who increase maxToolIterations now get the same budget for spawned subagents. This avoids premature subagent termination for longer research, polling, or multi-step background tasks while keeping the change scoped to the existing runtime limit.

Fixes #970.

Validation

  • uv run --extra dev pytest tests/agent/tools/test_subagent_tools.py tests/agent/tools/test_self_tool_runtime_sync.py tests/agent/test_runner.py::test_subagent_max_iterations_announces_existing_fallback -q
  • uv run --extra dev ruff check nanobot/agent/subagent.py nanobot/agent/loop.py nanobot/agent/tools/self.py tests/agent/tools/test_subagent_tools.py tests/agent/tools/test_self_tool_runtime_sync.py

Note: the targeted pytest run passes with one existing async queue warning from tests/agent/test_runner.py::test_subagent_max_iterations_announces_existing_fallback.

@Re-bin Re-bin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right fix.

The bug was that the parent agent already respected agents.defaults.maxToolIterations, but subagents still used a hardcoded max_iterations=15. This PR wires the configured limit into SubagentManager, uses it for subagent AgentRunSpec, and keeps it synced when max_iterations changes at runtime through the my tool.

I merged latest origin/main locally and reran the relevant tests:

  • python -m pytest tests/agent/tools/test_subagent_tools.py tests/agent/tools/test_self_tool_runtime_sync.py tests/agent/test_runner.py::test_subagent_max_iterations_announces_existing_fallback -q → 10 passed
  • python -m ruff check nanobot/agent/subagent.py nanobot/agent/loop.py nanobot/agent/tools/self.py tests/agent/tools/test_subagent_tools.py tests/agent/tools/test_self_tool_runtime_sync.py → passed

The scope is narrow, the tests cover direct construction, AgentLoop wiring, runtime sync, and MyTool updates.

From my side, this is ready to merge.

@Re-bin
Re-bin merged commit 3c20d16 into HKUDS:main Apr 30, 2026
8 checks passed
@boogieLing
boogieLing deleted the R0/issue-970-subagent-max-iterations branch April 30, 2026 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subagent max_iterations is hardcoded to 15, causing long-running tasks to time out

2 participants