Clearing a compaction session retains the previous response chain

Author: sylvesterkaczmarekCreated Sep 4, 2026Updated Sep 11, 2026
Labelsfeature:sessions

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

python
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.

Source: openai/openai-agents-python