fix(agent): stamp tool request with originating response timestamp - #9479
Closed
jpatel3 wants to merge 1 commit into
Closed
fix(agent): stamp tool request with originating response timestamp#9479jpatel3 wants to merge 1 commit into
jpatel3 wants to merge 1 commit into
Conversation
Tool requests (assistant/tool_use) were created after tool execution while their paired tool responses (user/tool_result) were created before it. When execution crossed a one-second boundary, the request received a later created_timestamp than the response. The session DB returns messages ORDER BY created_timestamp, id, so the response sorted ahead of its request and the Claude API rejected the conversation with "unexpected tool_use_id in tool_result blocks". Carry the originating response's timestamp onto the tool request message (mirroring how thinking content is already stamped) so the request and response share a timestamp and the auto-increment id tiebreaker preserves insertion order. Fixes aaif-goose#9461 Signed-off-by: Jaimin Patel <jpatel@tuvalabs.com>
Collaborator
|
Thanks for digging into this and the clear write-up in #9461! This turned out to be a popular bug — the same fix landed in #9462, which clamps the tool request timestamp to its response so the session ordering stays correct. Since that's now merged on |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #9461.
Tool request messages (
assistant/tool_use) were being assigned acreatedtimestamp after tool execution, while their paired tool response messages (user/tool_result) were stamped before execution. When a tool ran long enough to cross a one-second boundary, the request ended up with a later timestamp than its response.The session DB retrieves messages with
ORDER BY created_timestamp, id(crates/goose/src/session/session_manager.rs). Theidauto-increment tiebreaker only matters when timestamps are equal — so an out-of-order pair causes thetool_resultto sort ahead of itstool_use, and the Claude API rejects the conversation with:Fix
In the tool-request construction loop in
crates/goose/src/agents/agent.rs, carry the originating provider response'screatedtimestamp onto the tool-request message. This mirrors how thinking/reasoning content is already stamped (thinking_msgusesresponse.created). The request and response now share a timestamp, so the insertion-orderidtiebreaker preserves the correct request-before-response ordering.Test
Added
test_tool_request_not_stamped_after_responseincrates/goose/tests/agent.rs. A mock provider emits a tool request stamped well in the past; the test drives the agent through one tool turn and asserts the persisted tool-request keeps that timestamp and is never stamped after its response. Verified the test fails without the fix (the request gets the wall-clock time) and passes with it.