Skip to content

fix: repair orphaned toolUse in the last session message during restore#2073

Open
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Zelys-DFKH:fix/fix-broken-tool-use-last-message
Open

fix: repair orphaned toolUse in the last session message during restore#2073
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Zelys-DFKH:fix/fix-broken-tool-use-last-message

Conversation

@Zelys-DFKH
Copy link
Copy Markdown

Description

_fix_broken_tool_use explicitly skipped the last message in the session history,
relying on the agent-class fallback (_has_tool_use_in_latest_message) to handle
it. That fallback only works when the same agent instance re-enters the event loop
within the same process. When a different process restores a session that ended with
an orphaned toolUse — for example, after a runtime timeout while waiting for a
long-running tool — and a new user message is then appended, the orphaned toolUse
reaches the model without a corresponding toolResult, producing a
ValidationException.

The fix removes the guard and handles the last-message case the same way as interior
messages: append a synthetic toolResult with status: "error". The original tool
execution context is already lost at restore time, so reporting an error to the model
and letting it decide how to proceed is the correct behavior.

Related Issues

Resolves #2025

Documentation PR

N/A

Type of Change

Bug fix

Testing

  • Updated test_fix_broken_tool_use_ignores_last_message (which asserted the old,
    incorrect behavior) to test_fix_broken_tool_use_repairs_orphaned_last_message,
    verifying that a synthetic toolResult is appended.
  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

_fix_broken_tool_use skipped the last message with an explicit guard,
relying on the agent-class fallback (_has_tool_use_in_latest_message).
That fallback only works within the same process. When a new process
restores a session that ended with an orphaned toolUse (e.g. after a
runtime timeout), the guard causes the broken history to be sent to
the model, producing a ValidationException.

Remove the guard and handle the last-message case by appending a
synthetic toolResult with status 'error'. The tool execution context
is already lost at restore time, so letting the model decide how to
proceed is the correct behavior.

Updates the test that asserted the old (incorrect) behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] _fix_broken_tool_use does not repair orphaned toolUse in the last message, causing session corruption on process termination

1 participant