Skip to content

run_agent sanitizer misclassifies tool results when tool_call_id has surrounding whitespace #9999

@shm197

Description

@shm197

Bug Description

run_agent.py can misclassify a valid tool result as missing when tool_call_id contains leading or trailing whitespace.

The current pre-call sanitizer normalizes assistant-side tool call IDs via _build_assistant_message(), but _sanitize_api_messages() compares tool results using the raw tool_call_id string from persisted history.

This can cause Hermes to inject:

[Result unavailable — see context summary above]

…even though the real tool result is present in the session.

Steps to Reproduce

  1. Build a message list where the assistant tool call uses functions.cronjob:24
  2. Add a matching role="tool" message whose tool_call_id is " functions.cronjob:24"
  3. Call AIAgent._sanitize_api_messages(messages)

Expected Behavior

The tool result should be preserved because the IDs are semantically the same after trimming whitespace.

Actual Behavior

The sanitizer treats the tool result as missing/orphaned and may inject a synthetic stub:

[Result unavailable — see context summary above]

Root Cause Analysis

_build_assistant_message() already strips whitespace from assistant-side tool call IDs, but _sanitize_api_messages() was still:

  • collecting result_call_ids from raw tool_call_id values
  • filtering orphaned tool messages using raw tool_call_id values

So assistant-side IDs and tool-result IDs could diverge purely because of surrounding whitespace.

Proposed Fix

Normalize tool-call IDs before every comparison in _sanitize_api_messages().

A small helper such as AIAgent._normalize_tool_call_id() is enough.

Regression Test Suggestion

Add coverage for both:

  1. Preserving a valid tool result when tool_call_id has leading whitespace
  2. Still stripping a truly orphaned tool result when its tool_call_id has surrounding whitespace

Are you willing to submit a PR for this?

  • I'd like to fix this myself and submit a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/agentCore agent loop, run_agent.py, prompt buildertype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions