Skip to content

test(sdk): pin events_to_messages boundaries + fix responses_reasoning_item batch drop - #4526

Merged
enyst merged 7 commits into
OpenHands:mainfrom
georgeglarson:test/events-to-messages-extensive
Aug 19, 2026
Merged

enyst merged 7 commits into
OpenHands:mainfrom
georgeglarson:test/events-to-messages-extensive

Conversation

@georgeglarson

@georgeglarson georgeglarson commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

All tests pass human validation.
I think the tests are correct in design and execution.


AGENT:

Why

LLMConvertibleEvent.events_to_messages (event/base.py:108) is the projection that turns the persisted event log into the messages[] the LLM actually sees — it re-batches parallel tool calls by llm_response_id and coalesces adjacent plain user messages for strict role alternation. It has carried # TODO: We should add extensive tests for this since the function was written, and the existing suite (15 tests) covers the happy paths but none of the boundaries.

While writing characterization tests for those boundaries, one of them failed and exposed a real bug: _combine_action_events reconstructs a multi-tool-call response from the first event's fields but never passes responses_reasoning_item — so the OpenAI Responses-API reasoning item survives when an action converts as a singleton (action.py:143 passes it) and silently disappears the moment the same response carries parallel tool calls. response_dispatch.py:169-174 stamps reasoning state on the first batch event only (by design), so the first event holding the field and the combiner dropping it is a live inconsistency, not a theoretical one.

Summary

  • test(sdk): characterization tests pinning documented-but-unpinned boundary behavior, retiring the TODO: user-message merge cascading across 3+ events, mixed text/image merge order, parallel batches preserving first-event reasoning state (reasoning_content / thinking_blocks), and the condensation-summary/user-message fusion.
  • fix(sdk) (found by one of those tests): _combine_action_events now passes responses_reasoning_item=events[0].responses_reasoning_item, mirroring the adjacent reasoning_content / thinking_blocks lines; the parity test that exposed the drop is its regression test, asserting whole-item equality so future field loss fails it.
  • docs(sdk): the events_to_messages() docstring now states the read-only projection contract (events are immutable once appended to the log; merges build new content lists rather than mutating the events' own messages), and the retired TODO is removed.

Issue Number

Fixes #4525

How to Test

make build
uv run pytest tests/sdk/event/test_events_to_messages.py -v   # 20 tests, 5 new
CI=true uv run python -m pytest tests/sdk -n auto -q          # full sdk package

Full-suite result on this branch: 5974 passed, 0 failed (7 skipped, 10 xfailed), run by the human author on Linux, Python 3.13. A full-repo run (tests/) passed 9391 with 27 failures, all host-environment artifacts unrelated to the change (stock shell-integration escape codes in pty-spawned terminal tests, environment-variable leakage into live-server tests, and one order-dependent OpenAPI test that passes in isolation on both base and this head); the browser_use e2e suite was excluded locally because BrowserToolExecutor teardown deadlocks off-CI. Pre-commit suite (ruff format/lint, pycodestyle, pyright, import rules, tool registration) clean.

Two notes for reviewers:

  1. The bug, demonstrated. Check out the first commit's parent and run test_parallel_batch_preserves_responses_reasoning_item — it fails with responses_reasoning_item=None on the combined message despite the first event carrying the item. The fix flips it green. It was also validated live against GPT-5-nano on the real Responses API: a recombined parallel batch kept its reasoning item, and the regression test fails on main as expected — results recorded in .pr/gpt-5-nano-integration-results.md on this branch.
  2. A deliberate pin, since confirmed. CondensationSummaryEvent.to_llm_message() produces a plain user message, so a condensation summary adjacent to a user MessageEvent in the view is fused with it into a single user turn (the summarizing condenser inserts the summary at forgetting_start, and the kept suffix may legally begin with a user message). test_condensation_summary_merges_with_adjacent_user_message pins the current behavior — API-valid and content-complete, with the summary's structural boundary erased in the projection. Raised here as a design question; confirmed by the maintainer as intended behavior (a normal consequence of the plain-user merge for strict role alternation), so the pin stays as documentation.

Video/Screenshots

image

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

The change is a one-line production fix (_combine_action_events) plus a docstring, and 5 test functions; the tests are the bulk of the diff by design — they close the long-standing events_to_messages TODO.

Design Doc

N/A — one-line production change plus tests.

_combine_action_events reconstructs the original LLM response from the
first event's fields but omitted responses_reasoning_item, so the
OpenAI Responses-API reasoning item survived as a singleton action yet
vanished the moment the response carried parallel tool calls. Mirror
the adjacent reasoning_content/thinking_blocks lines, with a parity
regression test.
Characterization tests for the projection's documented-but-unpinned
edges: trailing parallel batches at end of stream (exact tool-call
order), interleaved response ids not re-batching, user-message merge
cascading across 3+ events with mixed text/image order preserved,
projection purity w.r.t. input events (the deepcopy defense), and the
condensation-summary fusion with adjacent user turns.

Addresses the long-standing TODO on events_to_messages.
@georgeglarson
georgeglarson marked this pull request as ready for review August 18, 2026 14:49
@enyst enyst added the integration-test Runs the integration tests and comments the results label Aug 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi! I started running the integration tests on your PR. You will receive a comment with the results shortly.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

👋 This PR needs a couple of things fixed before OpenHands can review it:

  • the PR description's HUMAN: section needs at least 20 characters describing what you tested, not just the template placeholder

Push an update once this is addressed and this check re-runs automatically.

This is an automated check - no AI was used to generate this comment.

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟡 Acceptable — The production fix is minimal and correct, but the test expansion is broader than the regression and live eval evidence is still pending.

[IMPROVEMENT OPPORTUNITIES]

  • [tests/sdk/event/test_events_to_messages.py, line 193] Scope: The input-immutability test characterizes existing deep-copy behavior rather than the responses_reasoning_item batch-drop fix. Removing it would keep this PR focused.
  • [tests/sdk/event/test_events_to_messages.py, line 473] Regression assertion: Compare the whole preserved responses_reasoning_item with the first event's item, not only its ID. That directly states the contract and catches future field loss.
  • [tests/sdk/event/test_events_to_messages.py, line 537] Duplicate coverage: The trailing-batch test overlaps the existing three-call parallel test. Add an exact order assertion to the existing test instead.
  • [tests/sdk/event/test_events_to_messages.py, line 584] Unrealistic case: Interleaved reuse of one LLM response ID is not produced by the SDK and does not exercise this fix. It can be dropped.

[TESTING]

  • The focused event-conversion suite passes locally: 23 passed.
  • The new reasoning-item regression test fails against origin/main because the combined message has responses_reasoning_item=None, confirming it catches the bug.
  • GitHub's integration workflow did not execute: actions/checkout@v7 refused fork code in the pull_request_target job. Per repository policy, this review remains COMMENT pending a lightweight live eval and human maintainer confirmation.

[RISK ASSESSMENT]

  • [Overall PR] ⚠️ Risk Assessment: 🟡 MEDIUM
    The code change is only two field assignments and preserves existing batching order and tool calls, but it changes replayed OpenAI Responses history in core LLM message construction. That is agent/eval-sensitive behavior and warrants the requested live integration run before merge.

VERDICT:
✅ Worth merging after live eval confirmation: The core fix is sound; the test cleanup suggestions are nonblocking.

KEY INSIGHT:
The first action already owns the response-level reasoning metadata, so copying that metadata onto the single recombined assistant message restores the original LLM response without adding a new batching rule.

Improve this review? If any feedback above seems incorrect or irrelevant to this repository, you can teach the reviewer to do better:

  1. Add a .agents/skills/custom-codereview-guide.md file to your branch (or edit it if one already exists) with the /codereview trigger and the context the reviewer is missing (e.g., "Security concerns about X do not apply here because Y"). See the customization docs for the required frontmatter format.
  2. Re-request a review - the reviewer reads guidelines from the PR branch, so your changes take effect immediately.
  3. When your PR is merged, the guideline file goes through normal code review by repository maintainers.

Resolve with AI? Install the iterate skill in your agent and run /iterate to automatically drive this PR through CI, review, and QA until it's merge-ready.

Was this review helpful? React with 👍 or 👎 to give feedback.

AI-generated review posted by OpenHands on behalf of the user.

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

Copy link
Copy Markdown
Contributor

📁 PR Artifacts Notice

This PR contains a .pr/ directory with temporary PR-specific documents. Because this is a fork PR, the directory will be automatically removed from main immediately after merge.

- drop test_trailing_parallel_batch_at_end_of_stream — redundant with
  test_parallel_function_calling_same_response_id, which already places
  a 3-call batch at end of stream
- drop test_interleaved_response_ids_do_not_batch — a response id
  cannot recur non-adjacently in a real log (ids are per-response
  hashes), so the scenario is unreachable
- cascade test: drop the isinstance filter; the merged message holds
  only TextContent by construction (matches the file's type: ignore
  idiom)
- events_to_messages(): docstring now states the read-only projection
  contract (events are immutable once appended to the log), so the
  invariant lives in the repo; the no-mutation test stays as its
  regression pin
- remove the 'add extensive tests' TODO — this branch is those tests
@georgeglarson
georgeglarson marked this pull request as draft August 18, 2026 18:33
- restore the isinstance filter in the cascade test — the review
  resolved that nit as keep (runtime narrowing, clearer failure);
  rewriting it was churn
- drop test_events_to_messages_does_not_mutate_input_events — the
  repo-wide event immutability contract should be documented and
  tested separately; the events_to_messages() docstring carries the
  read-only note here
- fold an exact tool-call order assertion into
  test_parallel_function_calling_same_response_id (replacing
  membership checks) — keeps the deleted trailing-batch test's one
  useful extra check without duplicating its scenario
- strengthen the responses_reasoning_item regression to whole-item
  equality, so future field loss fails the test
@georgeglarson

Copy link
Copy Markdown
Contributor Author

Thanks. Current head d820af43f aligns with every item from both rounds:

  • isinstance filter: restored.
  • Trailing-batch test: deleted, and its one useful extra check (exact tool-call order) is folded into test_parallel_function_calling_same_response_id, replacing the membership checks.
  • Interleaved-response-id test: deleted.
  • No-mutation test: deleted.
  • Regression test strengthened: whole-item equality on responses_reasoning_item, not just the id — future field loss now fails it.
  • The TODO is gone.

@georgeglarson
georgeglarson marked this pull request as ready for review August 18, 2026 19:32
@all-hands-bot

Copy link
Copy Markdown
Collaborator

🚦 CI is currently failing on this PR's latest commit.

Please fix the failing checks before OpenHands reviews it - this is re-checked automatically once you push a new commit. (A maintainer can also request @all-hands-bot as a reviewer to have it reviewed regardless of CI status.)

This is an automated check - no AI was used to generate this comment.

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@enyst
enyst merged commit bb26768 into OpenHands:main Aug 19, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-test Runs the integration tests and comments the results

Projects

None yet

Development

Successfully merging this pull request may close these issues.

events_to_messages lacks boundary tests (its own TODO); writing them exposed responses_reasoning_item being dropped in parallel-call batches

3 participants