Skip to content

Refactor agent_runner: extract methods from send() and _build_options()#1

Merged
nikitaxru merged 3 commits intomainfrom
refactor/agent-runner-extract-methods
Mar 13, 2026
Merged

Refactor agent_runner: extract methods from send() and _build_options()#1
nikitaxru merged 3 commits intomainfrom
refactor/agent-runner-extract-methods

Conversation

@nikitaxru
Copy link
Copy Markdown
Contributor

Summary

  • Break down AgentSession.send() (~100 lines → ~40) into 4 focused methods: _process_assistant_message(), _apply_result_message(), _read_tool_state(), _build_success_result()
  • Break down AgentRunner._build_options() (~100 lines → ~58) by extracting _build_mcp_servers(), _build_allowed_tools(), and moving hardcoded tool lists into Final[tuple] constants
  • Add 5 characterization tests covering previously untested behavioral contracts (mailbox/comm tools, ToolState consumption/reset, AGENT_OUTPUT events)

Test plan

  • All 66 test_agent_runner.py tests pass (61 existing + 5 new)
  • Full test suite: 2002 passed
  • ruff check — clean
  • ruff format --check — clean
  • mypy — no issues
  • Pre-commit hooks pass

🤖 Generated with Claude Code

Break down two ~100-line methods into focused, single-responsibility
helpers while preserving the public API contract.

send() (was ~100 lines, now ~40):
- _process_assistant_message(): text collection + rate limit detection
- _apply_result_message(): cost/token extraction + event publishing
- _read_tool_state(): ToolState consumption with reset semantics
- _build_success_result(): output assembly + PR URL extraction

_build_options() (was ~100 lines, now ~58):
- _build_mcp_servers(): MCP server dict construction
- _build_allowed_tools(): conditional tool list assembly
- _BASE_ALLOWED_TOOLS, _WORKSPACE_TOOLS, _COMM_TOOLS as Final tuples

Adds 5 characterization tests covering previously untested contracts:
- mailbox/comm branch in _build_options()
- tool_state.needs_info_requested consumption/reset in send()
- tool_state.proposals consumption/reset in send()
- AGENT_OUTPUT event publishing per TextBlock

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@cursor
Copy link
Copy Markdown

cursor bot commented Mar 13, 2026

PR Summary

Low Risk
Primarily a refactor of agent session option-building and response-processing code with added tests; behavior should be unchanged, but regressions could affect tool allowlists, event emission, or token/cost reporting.

Overview
Refactors AgentSession.send() by extracting focused helpers for processing AssistantMessage text/rate-limit detection, applying ResultMessage cost/token/session updates, consuming/resetting ToolState side-channel flags, and assembling the final AgentResult.

Refactors AgentRunner._build_options() by splitting MCP server construction vs allowed-tool list assembly, moving hardcoded tool names into Final constants, and adding a lazy import path when mailbox enables the comm MCP server.

Adds characterization tests to lock in behavior around mailbox enabling comm tools, ToolState consumption/reset semantics, and publishing AGENT_OUTPUT events per TextBlock.

Written by Cursor Bugbot for commit 8a22d29. This will update automatically on new commits. Configure here.

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

session_id was written during construction but never read from the tuple.
The value is already persisted via self._session_id side effect in
_apply_result_message before the tuple is returned.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nikitaxru nikitaxru changed the base branch from opensource-release to main March 13, 2026 19:58
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nikitaxru nikitaxru merged commit 756d8b3 into main Mar 13, 2026
4 checks passed
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.

1 participant