Skip to content

Commit e0566c1

Browse files
committed
fix(sessions): detect empty snapshot clears and pin the response id across the lock wait
The prefix check compares two empty lists when the snapshot was taken on an empty session, so a clear_session during the request went unseen and the compacted output repopulated the cleared session. clear_session and pop_item now bump a generation counter under the lock, captured with the snapshot and compared before the replacement. resolved_mode was derived from the response id at entry, but the id was read again after the lock wait, where a second call may have overwritten it. The id is captured once beside the mode resolution and used from there on.
1 parent 8dd60cf commit e0566c1

2 files changed

Lines changed: 105 additions & 8 deletions

File tree

‎src/agents/memory/openai_responses_compaction_session.py‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ def __init__(
142142
# Serialize wrapper mutations against compaction snapshot/replace/restore so a
143143
# cancellation rollback cannot rewrite past a newer concurrent write.
144144
self._mutation_lock = asyncio.Lock()
145+
# Bumped by clear_session and pop_item under the lock. The prefix check in
146+
# run_compaction cannot see a destructive rewrite when both the snapshot and
147+
# the post flight history are empty, so the counter records it explicitly.
148+
self._destructive_generation = 0
145149

146150
@property
147151
def client(self) -> AsyncOpenAI:
@@ -201,13 +205,18 @@ async def run_compaction(
201205
"when using previous_response_id compaction."
202206
)
203207

208+
# resolved_mode was derived from the response id as it is right now. A
209+
# concurrent call can overwrite self._response_id while this one waits on
210+
# the lock below, so everything after this line uses the paired local.
211+
response_id = self._response_id
212+
204213
async with self._mutation_lock:
205214
compaction_candidate_items, session_items = await self._ensure_compaction_candidates()
206215

207216
force = args.get("force", False) if args else False
208217
should_compact = force or self.should_trigger_compaction(
209218
{
210-
"response_id": self._response_id,
219+
"response_id": response_id,
211220
"compaction_mode": resolved_mode,
212221
"compaction_candidate_items": compaction_candidate_items,
213222
"session_items": session_items,
@@ -217,26 +226,27 @@ async def run_compaction(
217226
if not should_compact:
218227
logger.debug(
219228
"skip: decision hook declined compaction for %s (mode=%s)",
220-
self._response_id,
229+
response_id,
221230
resolved_mode,
222231
)
223232
return
224233

225234
# Capture the full stored history while the lock still excludes writers.
226235
# It anchors the post-flight check that detects concurrent mutations.
227236
snapshot_items = await self._get_all_underlying_session_items()
237+
snapshot_generation = self._destructive_generation
228238

229239
self._deferred_response_id = None
230240
logger.debug(
231241
"compact: start for %s using %s (mode=%s)",
232-
self._response_id,
242+
response_id,
233243
self.model,
234244
resolved_mode,
235245
)
236246

237247
compact_kwargs: dict[str, Any] = {"model": self.model}
238248
if resolved_mode == "previous_response_id":
239-
compact_kwargs["previous_response_id"] = self._response_id
249+
compact_kwargs["previous_response_id"] = response_id
240250
else:
241251
compact_kwargs["input"] = session_items
242252

@@ -253,17 +263,22 @@ async def run_compaction(
253263
async with self._mutation_lock:
254264
previous_items = await self._get_all_underlying_session_items()
255265
baseline_count = len(snapshot_items)
256-
if previous_items[:baseline_count] != snapshot_items:
266+
if (
267+
self._destructive_generation != snapshot_generation
268+
or previous_items[:baseline_count] != snapshot_items
269+
):
257270
# A concurrent clear_session or pop_item rewrote history while the
258271
# compaction request was in flight, so the snapshot no longer
259272
# describes the stored items. Replacing them would resurrect
260-
# deleted history; keep the current items and drop the caches.
273+
# deleted history; keep the current items and drop the caches. The
274+
# generation counter catches the case the prefix check cannot: a
275+
# clear_session while the snapshot itself was empty.
261276
self._compaction_candidate_items = None
262277
self._session_items = None
263278
logger.warning(
264279
"Skipped compaction replacement for %s (mode=%s): session history "
265280
"diverged from the compaction snapshot while the request was in flight.",
266-
self._response_id,
281+
response_id,
267282
resolved_mode,
268283
)
269284
return
@@ -280,7 +295,7 @@ async def run_compaction(
280295

281296
logger.debug(
282297
"compact: done for %s (mode=%s, output=%s, candidates=%s, concurrent_tail=%s)",
283-
self._response_id,
298+
response_id,
284299
resolved_mode,
285300
len(output_items),
286301
len(self._compaction_candidate_items or []),
@@ -458,6 +473,7 @@ async def pop_item(self) -> TResponseInputItem | None:
458473
if popped:
459474
self._compaction_candidate_items = None
460475
self._session_items = None
476+
self._destructive_generation += 1
461477
return popped
462478

463479
async def clear_session(self) -> None:
@@ -466,6 +482,7 @@ async def clear_session(self) -> None:
466482
self._compaction_candidate_items = []
467483
self._session_items = []
468484
self._deferred_response_id = None
485+
self._destructive_generation += 1
469486

470487
async def _ensure_compaction_candidates(
471488
self,

‎tests/memory/test_openai_responses_compaction_session.py‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,86 @@ async def test_replacement_preserves_metadata_tail_across_storage_round_trip(
19931993
assert cached_tail.get("call_id") == "call_tail"
19941994
assert TOOL_CALL_SESSION_TITLE_KEY not in cached_tail
19951995

1996+
@pytest.mark.asyncio
1997+
async def test_clear_session_with_empty_snapshot_is_not_repopulated(self) -> None:
1998+
"""A clear during flight must hold even when the snapshot itself was empty.
1999+
2000+
With previous_response_id compaction the local session can be empty when
2001+
the request starts, so the post flight prefix check compares two empty
2002+
lists and cannot see the clear. The destructive generation counter can.
2003+
"""
2004+
compacted_item = cast(TResponseInputItem, {"type": "compaction", "summary": "compacted"})
2005+
underlying = SimpleListSession(history=[])
2006+
compact_entered = asyncio.Event()
2007+
release_compact = asyncio.Event()
2008+
session = OpenAIResponsesCompactionSession(
2009+
session_id="clear-empty-baseline",
2010+
underlying_session=underlying,
2011+
client=self.create_gated_compact_client(
2012+
[compacted_item], compact_entered, release_compact
2013+
),
2014+
compaction_mode="previous_response_id",
2015+
)
2016+
2017+
compaction_task = asyncio.create_task(
2018+
session.run_compaction({"force": True, "response_id": "resp_baseline"})
2019+
)
2020+
try:
2021+
await compact_entered.wait()
2022+
await session.clear_session()
2023+
finally:
2024+
release_compact.set()
2025+
await compaction_task
2026+
2027+
assert await underlying.get_items() == []
2028+
assert await session.get_items() == []
2029+
2030+
@pytest.mark.asyncio
2031+
async def test_compact_uses_response_id_captured_before_lock_wait(self) -> None:
2032+
"""The compact call must use the response id its mode was resolved from.
2033+
2034+
While one run_compaction waits on the mutation lock, a second call can
2035+
overwrite the shared response id at entry. The waiter must not pick up
2036+
the newer id after resuming.
2037+
"""
2038+
compacted_item = cast(TResponseInputItem, {"type": "compaction", "summary": "compacted"})
2039+
underlying = SimpleListSession(history=[])
2040+
mock_compact_response = MagicMock()
2041+
mock_compact_response.output = [compacted_item]
2042+
mock_client = MagicMock()
2043+
mock_client.responses.compact = AsyncMock(return_value=mock_compact_response)
2044+
session = OpenAIResponsesCompactionSession(
2045+
session_id="captured-response-id",
2046+
underlying_session=underlying,
2047+
client=mock_client,
2048+
compaction_mode="previous_response_id",
2049+
)
2050+
2051+
await session._mutation_lock.acquire()
2052+
try:
2053+
first = asyncio.create_task(
2054+
session.run_compaction({"force": True, "response_id": "resp_first"})
2055+
)
2056+
await asyncio.sleep(0)
2057+
await asyncio.sleep(0)
2058+
second = asyncio.create_task(
2059+
session.run_compaction({"force": True, "response_id": "resp_second"})
2060+
)
2061+
await asyncio.sleep(0)
2062+
await asyncio.sleep(0)
2063+
# The second call already overwrote the shared id while the first waits.
2064+
assert session._response_id == "resp_second"
2065+
finally:
2066+
session._mutation_lock.release()
2067+
await first
2068+
await second
2069+
2070+
sent_ids = [
2071+
call.kwargs["previous_response_id"]
2072+
for call in mock_client.responses.compact.call_args_list
2073+
]
2074+
assert sent_ids == ["resp_first", "resp_second"]
2075+
19962076

19972077
class TestTypeGuard:
19982078
def test_is_compaction_aware_session_true(self) -> None:

0 commit comments

Comments
 (0)