Skip to content

capture: replay a Responses turn's text and tool calls as one message - #1783

Merged
burtenshaw merged 3 commits into
openenv:mainfrom
younesbelkada:capture/responses-merge-turns
Dec 1, 2026
Merged

burtenshaw merged 3 commits into
openenv:mainfrom
younesbelkada:capture/responses-merge-turns

Conversation

@younesbelkada

@younesbelkada younesbelkada commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A Responses client (Hermes) sends back a past turn that said something and called tools as a message item followed by its function_call items. _convert_input_items_to_messages turns them into two chat messages: the text alone, then a content-less assistant message with the calls. This joins them into one assistant message, as the model generated it.

Type of Change

  • Bug fix

What happened without it

With the turns split, every prompt shows the model a history of assistant turns that only talk, and the model copies them:

  • Runs ended early: on SWE-bench Pro, 25 of 49 Hermes runs ended on a turn such as "Now let me implement the changes:" with no tool call. That ends Hermes's run.
  • With the turns merged: at those same points the model called a tool every time (40 of 40).
  • Training prompts: one message per turn is also what the model sampled, so the rendered prompt keeps extending the previous call's tokens.

Changes

openai_responses.py: _merge_split_assistant_turns, applied to the converted messages, merges a text-only assistant message that is directly followed by a content-less assistant message with tool calls.

  • Reasoning is kept: if only the second message carries reasoning_content, it moves onto the merged message.
  • Everything else is unchanged: tool-only turns, text-only turns, and any other sequence of messages.

Test Plan

tests/envs/test_capture_responses_turn_merge.py:

  • text plus two tool calls become one message;
  • reasoning stays on the merged turn;
  • tool-only and text-only turns are left alone.

The first two fail on main.

PYTHONPATH=src:envs uv run pytest tests/envs/test_capture_responses_turn_merge.py tests/envs/test_harbor_tasks_and_dialects.py tests/envs/test_harbor_tito_training.py tests/envs/test_harbor_rollout_contract.py tests/envs/test_harbor_upstream_compat.py tests/envs/test_harbor_capture_request_validation.py

169 passed. ruff format --check, ruff check and usort check are clean.

Used in production as a runtime patch for Hermes GRPO training and SWE-bench Pro evals.

For reviewers: codex also speaks Responses. A codex turn that produced text and tool calls in one response is merged the same way, which is again what was sampled. If a client ever sends a text-only reply and then, in a separate call with no input in between, a tool-call-only reply, the two would also be merged. I haven't seen that sequence in any harness.

RFC Status

  • Not required (bug fix, docs, minor refactoring)

🤖 Generated with Claude Code


Note

Medium Risk
Changes how historical conversation is rendered for all Responses replay paths, which affects model behavior and training/eval prompts; logic is narrow but high-impact for agent harnesses.

Overview
Fixes Responses API → chat replay so a turn that produced assistant text and tool calls in one generation is stored as a single assistant message instead of two (text-only, then tool-only).

_convert_input_items_to_messages now post-processes through _merge_split_assistant_turns, which only joins an assistant message with content and no tool_calls when it is immediately followed by an assistant message with tool_calls and no content, carrying over merged reasoning_content when needed. Tool-only, text-only, and other sequences are left unchanged.

Adds tests/envs/test_capture_responses_turn_merge.py for merge behavior, reasoning attachment, and unchanged edge cases.

Reviewed by Cursor Bugbot for commit a28eb2a. Bugbot is set up for automated code reviews on this repo. Configure here.

A Responses client sends back a past turn that said something and called tools
as a message item followed by its function_call items. They were converted
into two chat messages, the text alone and then a content-less message with the
calls, so every prompt showed the model assistant turns that only talk. It
copied them: on SWE-bench Pro, 25 of 49 Hermes runs ended on a turn such as
"Now let me implement the changes:" with no tool call.

A text-only assistant message directly followed by a content-less assistant
message with tool calls is now one message, as the model generated it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

The docs for this pull request are available at https://openenv.github.io/OpenEnv/pr-preview/pr-1783/ (commit a28eb2a). The preview may take a minute to update and is removed when the pull request closes.

burtenshaw and others added 2 commits December 1, 2026 12:19
When the text message of a turn already carried reasoning, a reasoning item
before its tool calls was dropped by the merge. It is now appended, the way
consecutive reasoning items are joined. The case where only the tool-call half
carries reasoning, which no test covered, is pinned too.

Co-authored-by: Cursor <cursoragent@cursor.com>
@burtenshaw
burtenshaw merged commit d7f1445 into openenv:main Dec 1, 2026
17 checks passed
@younesbelkada
younesbelkada deleted the capture/responses-merge-turns branch December 1, 2026 12:26
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.

2 participants