Skip to content

Clearing a compaction session retains the previous response chain #4864

Closed

Description

@sylvesterkaczmarek

Describe the bug

OpenAIResponsesCompactionSession.clear_session() clears the underlying history and cached compaction items, and resets _deferred_response_id, but it leaves _response_id and _last_unstored_response_id unchanged.

As a result, later compaction can reuse response-chain state from history the caller explicitly cleared. In previous_response_id mode, a stale response ID can still be sent to responses.compact after the session is empty.

Reproduction

session = OpenAIResponsesCompactionSession(
    session_id="test",
    underlying_session=mock_session,
    client=mock_client,
    should_trigger_compaction=lambda _context: False,
)

await session.run_compaction({"response_id": "resp-old", "store": True})
await session.clear_session()

await session.run_compaction(
    {"force": True, "compaction_mode": "previous_response_id"}
)

On current main, the second call proceeds instead of raising that no response ID is available for the cleared session. A focused regression reaches the mocked compact client after the clear.

Expected behavior

A successful clear_session() should reset all response-chain state owned by the wrapper: _response_id, _deferred_response_id, and _last_unstored_response_id, in addition to the cached history.

After clear, explicit previous_response_id compaction without a newly supplied response ID should fail before any client call. If the underlying clear fails, response-chain state should remain unchanged because the old history may still exist.

Activity

  1. Neonstudio28 commented on Sep 11, 2026

    @Neonstudio28

    I implemented and tested a focused fix on the published fork branch fix/issue-4864-clear-response-chain, commit ce343f0. It clears _response_id, _deferred_response_id, and _last_unstored_response_id after a successful clear, preserves them if the underlying clear fails, and adds regression tests. The focused compaction test file passes (66 tests), plus Ruff, mypy, and pyright for the changed file.

  2. sylvesterkaczmarek commented on Sep 11, 2026

    @sylvesterkaczmarek
    ContributorAuthor

    Thanks for validating the same fix shape. There is already an open implementation PR for this issue, #4865, covering the response-chain reset and regressions. To avoid duplicate implementation/review work, please consolidate any follow-up there rather than opening a second PR.

  3. jbeckwith-oai commented on Sep 28, 2026

    @jbeckwith-oai
    Contributor

    Fixed on main by #5000, which superseded #4865. Clearing now invalidates all response-chain state, including after uncertain backend outcomes. The fix is not yet included in v0.22.3. Closing this as resolved; until a release includes it, recreating the compaction wrapper after a successful clear avoids retaining the old chain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions