Skip to content

fix(gaia-agent): report tools_used in _usage_from_result on unprinted turns - #7265

Merged
kovtcharov-amd merged 3 commits into
amd:mainfrom
txltedxgod:fix/stdio-error-tools-used
Nov 12, 2026
Merged

kovtcharov-amd merged 3 commits into
amd:mainfrom
txltedxgod:fix/stdio-error-tools-used

Conversation

@txltedxgod

@txltedxgod txltedxgod commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Include tools_used in _usage_from_result inside hub/agents/gaia/python/gaia_agent/stdio.py so turns that return without printing an answer event (such as error exits and step-limit exits whose closing summary fails) still report their executed tool call count in final["usage"].

Why

Without a printed answer event, non-verbose gaia -p relies exclusively on final["usage"] to populate num_tool_calls. Because _usage_from_result previously extracted only steps, input_tokens, tokens, and cached_tokens, non-verbose error turns always recorded 0 tool calls even when tools had executed before the failure.

Linked issue

Closes #7262

Changes

  • Added _tool_count_from_result(value, fallback=...) in hub/agents/gaia/python/gaia_agent/stdio.py to resolve tools_used from an explicit tools_used integer, else count role == "tool" entries in this turn's conversation, falling back to handler._tool_count (never model_messages, which replays earlier turns) while keeping the omit-when-zero rule.
  • Updated _usage_from_result and its run_turn call site to pass tool_count=getattr(handler, "_tool_count", 0) and include tools_used when > 0.
  • Added unit and wire-format tests in hub/agents/gaia/python/tests/test_stdio.py covering error exits with conversation, fallback to handler._tool_count, explicit zero/bool guards, and that earlier turns' tool results in model_messages are not counted.

Test plan

  • pytest hub/agents/gaia/python/tests/test_stdio.py tests/unit/test_print_mode.py tests/unit/test_final_usage_completeness.py tests/unit/test_turn_metrics_wire.py -q (282 passed)
  • pytest hub/agents/gaia/python/tests/test_transport_dispatch.py -q (22 passed)
  • black --check, isort --check-only, and flake8 passed on modified files

Evidence

  • Agent exposed in the Agent UI (Chat, Email, …) — N/A: change is in the gaia-agent stdio wire usage builder for unprinted turns rather than the web UI.
  • MCP tools / servers — N/A: no MCP server or client interface touched.
  • CLI — Verified gaia_agent.stdio.run_turn over the newline-delimited stdio transport with a simulated error exit after tool execution (python -c "... stdio.run_turn(...)"):
    {"type": "final", "answer": "Language model unreachable at step 2", "usage": {"steps": 2, "input_tokens": 500, "tools_used": 1}}
  • HTTP API / REST — N/A: no HTTP endpoint modified.

Checklist

  • I have linked a GitHub issue above (Closes #7262).
  • I have described why this change is being made, not just what changed.
  • I have run linting and tests locally (black, isort, flake8, pytest).
  • I have attached real-world evidence matched to the surface I changed, or marked each surface N/A.
  • I have updated documentation if user-visible behavior changed — N/A (bugfix matching existing documented usage.tools_used schema).

model_messages replays the session's earlier turns, so counting its tool
results reported tools from previous turns as this one's. Without the
turn's own conversation, the stream handler's count for the turn is used.
Adds the flagship changelog entry for the fix.
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Thanks @txltedxgod, this is a clean fix for #7262, and the wire-format tests made it easy to check. I pushed one follow-up commit to your branch: without the turn's own conversation, the count now comes from this turn's stream handler instead of model_messages.

model_messages replays the session's earlier turns, so on a second or later turn it would have counted the earlier turns' tools as this one's. I also added the flagship changelog entry the fix ships with.

🔍 Technical details
  • _tool_count_from_result checks explicit tools_used first, then role == "tool" entries in conversation, then handler._tool_count. Counting from conversation matches the in-process gaia -p record (print_mode.py).
  • model_messages comes from TurnMessages.recorded, which includes the history loaded at the start of the turn and the synthetic "No result was recorded" closures.
  • New tests: earlier turns' tool results are not counted, a negative or zero explicit count is left out, an empty conversation wins over the handler count, and malformed entries are skipped.
  • Changelog: hub/agents/gaia/npm/CHANGELOG.md under [Unreleased]. The version bump happens at release.
  • 4 tests in test_stdio.py fail on main's stdio.py and all 242 in test_stdio.py + test_print_mode.py pass with the fix.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 12, 2026
Merged via the queue into amd:main with commit 4819195 Nov 12, 2026
36 of 40 checks passed
@kovtcharov-amd kovtcharov-amd mentioned this pull request Nov 17, 2026
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.

gaia -p reports 0 tool calls when a turn ends in an error

2 participants