Skip to content

[tool] fix: forward extra_fields through ToolAgentLoop output#5515

Closed
cavities12 wants to merge 1 commit intoverl-project:mainfrom
cavities12:fix/forward-extra-fields
Closed

[tool] fix: forward extra_fields through ToolAgentLoop output#5515
cavities12 wants to merge 1 commit intoverl-project:mainfrom
cavities12:fix/forward-extra-fields

Conversation

@cavities12
Copy link
Copy Markdown
Collaborator

@cavities12 cavities12 commented Mar 8, 2026

What does this PR do?

Fixes ToolAgentLoop.run() silently dropping custom data from agent_data.extra_fields.

When tools write session data (e.g. tool_history, browse cache state) to agent_data.extra_fields during tool.execute(), this data is lost because the output is constructed with extra_fields={}. This PR replaces the empty dict with dict(agent_data.extra_fields) to shallow-copy all custom fields into the output.

turn_scores and tool_rewards are merged on top via .update(), preserving their override priority.

Checklist Before Starting

Test

Unit test: Added tests/experimental/agent_loop/test_extra_fields_forwarding.py (CPU-only, runs with pytest):

Test Verifies
test_custom_extra_fields_survive Custom data (tool_history, session_id) appears in output
test_turn_scores_and_tool_rewards_merged Standard fields merged alongside custom fields
test_turn_scores_overrides_custom_field .update() correctly overrides conflicting keys
test_empty_extra_fields_still_has_turn_scores Empty extra_fields still produces turn_scores/tool_rewards
test_shallow_copy_isolation Modifying output doesn't mutate original dict

Production validation: Verified during multi-turn GRPO training with custom tool session data.

API and Usage Example

No API changes.

Design & Code Changes

One-line fix in tool_agent_loop.py:

-            extra_fields={},
+            extra_fields=dict(agent_data.extra_fields),

dict(...) creates a shallow copy, preventing mutations to output.extra_fields from affecting agent_data.extra_fields.

Checklist Before Submitting

  • Read the Contribute Guide.
  • Apply pre-commit checks: pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=always
  • Add / Update the documentation. — N/A, no user-facing API change.
  • Add unit or end-to-end test(s) to the CI workflow: tests/experimental/agent_loop/test_extra_fields_forwarding.py (CPU-only, runs with pytest).
  • Once your PR is ready for CI, send a message in the ci-request channel. — Will request after review.
  • If your PR is related to the recipe submodule, update the reference. — N/A.

ToolAgentLoop.run() constructs AgentLoopOutput with extra_fields={},
silently dropping any custom data written to agent_data.extra_fields
during tool execution (e.g. tool_history, session state).

Replace the empty dict with dict(agent_data.extra_fields) to shallow-copy
all custom fields into the output. turn_scores and tool_rewards are then
merged on top via .update(), preserving their override priority.
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly fixes an issue where custom data in agent_data.extra_fields was being dropped. The solution, which involves creating a shallow copy of the fields, is appropriate and prevents unintended side effects. The change is accompanied by a comprehensive new test suite that thoroughly validates the fix and covers important edge cases. The implementation is clean and the tests are well-structured. No issues were found.

@ArronHZG
Copy link
Copy Markdown
Collaborator

#5487

I have fixed the issue in this PR; merging it will suffice. @wuxibin89

@wuxibin89 wuxibin89 closed this Mar 10, 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.

5 participants