fix(tools): merge subagents metrics (DelegateTool)#2221
Merged
VascoSch92 merged 18 commits intomainfrom Feb 27, 2026
Merged
Conversation
- Simplify builtin agent tests (no mocks, focus on registry contracts) - Extend delegation example to demonstrate built-in explore/bash agents Co-authored-by: openhands <openhands@all-hands.dev>
Collaborator
all-hands-bot
left a comment
There was a problem hiding this comment.
Found a critical bug and a testing gap that need attention before merge.
Co-authored-by: OpenHands Bot <contact@all-hands.dev>
all-hands-bot
approved these changes
Feb 26, 2026
Collaborator
all-hands-bot
left a comment
There was a problem hiding this comment.
🟢 Good Taste - Clean Fix
Verdict: ✅ Worth merging
This is how bug fixes should be done:
- Root cause identified: Pydantic shallow-copies private attrs, so all sub-agents shared the same
_metricsreference - Minimal fix applied: Move LLM copy inside loop + call
reset_metrics()to break the shared reference - Proper integration: Merge sub-agent metrics back into parent stats after delegation
- Solid test coverage: 3 tests verify independent metrics, proper merging, and no double-counting
No complexity, no special cases, no breaking changes. All previous review concerns resolved.
Key Insight: Understanding data ownership (shallow vs deep copy) led to the right two-line fix rather than working around symptoms.
all-hands-bot
approved these changes
Feb 26, 2026
Collaborator
all-hands-bot
left a comment
There was a problem hiding this comment.
🟢 Clean Fix - LGTM
Taste Rating: 🟢 Good taste - Straightforward bug fix with proper testing.
What I Like:
- Root cause correctly identified: Pydantic shallow-copies private attrs, causing shared Metrics references
- Minimal fix: Move LLM copy inside loop +
reset_metrics()to break the shared reference - Proper integration: Sub-agent metrics merged back into parent stats after delegation
- Solid tests: Verify independent metrics, proper merging, and no double-counting. Tests use real objects, not just mocks.
Verdict: ✅ Ready to merge. This solves a real problem (incorrect token counts for sub-agents) with a proportional solution and comprehensive test coverage.
Contributor
Coverage Report •
|
||||||||||||||||||||
5 tasks
Contributor
API breakage checks (Griffe)Result: Passed |
Contributor
Agent server REST API breakage checks (OpenAPI)Result: Passed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
(ref #2180)
There was a small problem with the token count for sub-agent spawning. In fact, all sub-agents were sharing the same metrics object, which was being reset by the
LLMRegistryand not merged into the parent agent's Metrics object.The fix consists of two parts:
LLMcreation, ensuring everyLLMhas its own unique Metrics object.Experiment
I used the following script
Output with the fix
output without the fix
Checklist
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.12-nodejs22golang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:bf8b2fb-pythonRun
All tags pushed for this build
About Multi-Architecture Support
bf8b2fb-python) is a multi-arch manifest supporting both amd64 and arm64bf8b2fb-python-amd64) are also available if needed