fix: repair orphaned toolUse in the last session message during restore#2073
fix: repair orphaned toolUse in the last session message during restore#2073Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Conversation
_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.
|
This PR duplicates #2026, which has been open since April 1st — four days before this one was created. I'd also like to flag a broader concern: @Zelys-DFKH opened 21 PRs (#2056–#2077) against this repository within roughly two days (April 4–5), spanning unrelated areas such as streaming, Bedrock, MCP, multiagent, session management, hooks, Anthropic, and OpenTelemetry. The pattern — commenting on an issue and then opening a PR minutes later, across a wide range of topics — strongly suggests automated bulk PR generation, likely using an AI coding agent. |
|
I've been doing thousands of annual commits like a madman since before AI was around. Mostly privately. I apologize if I took something you wanted: there was a lot of low-hanging fruit that gave me an itch. On the duplicate: the search I ran didn't catch #2026 because its title has a typo, "toolUser" instead of "toolUse." Happy to close mine or port the description over to that one. Maintainers' call. Your comment identifying AI writing patterns has three em dashes, double-hedged conclusions ("strongly suggests... likely using"), an eight-item list, the phrase "broader concern" as vague framing, and a subordinate clause inside a subordinate clause. That's as AI as it gets. Just worth noticing, as you said. Either way, good catch on #2026. I missed it and would not have duped it but for its typo. |
Description
_fix_broken_tool_useexplicitly skipped the last message in the session history,relying on the agent-class fallback (
_has_tool_use_in_latest_message) to handleit. 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 along-running tool — and a new user message is then appended, the orphaned
toolUsereaches the model without a corresponding
toolResult, producing aValidationException.The fix removes the guard and handles the last-message case the same way as interior
messages: append a synthetic
toolResultwithstatus: "error". The original toolexecution 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
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
toolResultis appended.hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.