Skip to content

fix(agent): keep tool request before its response in session history - #9480

Closed
MukundaKatta wants to merge 1 commit into
aaif-goose:mainfrom
MukundaKatta:fix/tool-request-response-ordering
Closed

fix(agent): keep tool request before its response in session history#9480
MukundaKatta wants to merge 1 commit into
aaif-goose:mainfrom
MukundaKatta:fix/tool-request-response-ordering

Conversation

@MukundaKatta

Copy link
Copy Markdown

Summary

The response placeholder for a tool call is inserted into request_to_response_map before the tool runs, so it gets a created timestamp from before execution. The matching request message is built after the tool finishes, so it gets a later timestamp. Session history is ordered by created, so the tool request can sort after its own response, or tie and order nondeterministically by id. That breaks request-before-response ordering when the conversation is reloaded.

This retrieves the response first and backdates the request when it is not already earlier, so the request always sorts before its response. The logic lives in a small ensure_request_precedes_response helper.

Testing

Added two unit tests for the helper: equal and later request timestamps get backdated, and an already-earlier request is left alone. cargo test -p goose --lib ensure_request_precedes_response passes and cargo fmt --check is clean.

Related Issues

Fixes #9461

The response placeholder for a tool call is inserted before the tool runs,
so it gets an earlier created timestamp than the request message, which is
built after execution. Session history is ordered by created timestamp, so
the tool request could sort after its own response (or tie and order
nondeterministically by id).

Retrieve the response first and backdate the request when it is not already
earlier, so request-before-response ordering holds. Add a small helper plus
unit tests for the ordering rule.

Fixes aaif-goose#9461

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fe109cbbb8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

/// when needed so request-before-response ordering is preserved.
fn ensure_request_precedes_response(request: &mut Message, response: &Message) {
if request.created >= response.created {
request.created = response.created.saturating_sub(1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve request order relative to the prompting message

When the tool response placeholder is created in the same second as the user message that triggered the turn, this backdates the assistant tool request to one second before that user message. On reload, the session store orders by created_timestamp, id, so the tool request becomes a leading assistant message; fix_conversation then removes leading assistant messages, leaving the saved tool response without its request and corrupting the next provider call. This can happen with fast providers or skipped/denied tools because created only has second granularity, so the fix needs to avoid moving the request before earlier conversation messages (or move the response forward instead).

Useful? React with 👍 / 👎.

@DOsinga

DOsinga commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Thanks so much for digging into this and sending a fix — really appreciate it! 🙏

Sorry, somebody beat you to it on this one: the same ordering bug (#9461) was already being tackled in a few earlier PRs, including #9462 (from the person who reported the issue) and a maintainer attempt in #9739. To avoid duplicate effort I'm going to close this one in favour of those.

One thing worth flagging for next time: this approach backdates the request to response.created - 1, which can push the tool request before the preceding user message when they land in the same second (timestamps are second-grained), re-introducing an ordering problem on reload. The other PRs align the request to the same second as the response and lean on the insertion-order id tiebreaker, which sidesteps that.

Please don't let this discourage you — would love to see more contributions! 🚀

@DOsinga DOsinga closed this Jun 15, 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.

Tool request messages get later timestamps than tool responses, breaking Claude API message ordering

2 participants