Skip to content

Commit 901a543

Browse files
committed
fix(sessions): compute response boundaries before the batch append
_add_items_for_response paired a response id with its item count by reading the full history back after the batch was appended. When that read was cancelled or failed transiently, the batch was already durable but the error still propagated, so the run treated a persisted turn as failed and a retry would persist the logical turn again. The boundary and the ever recorded flag were also left unset, and the plain save path keeps no pending write checkpoint that could reconcile the state. The hook now reads the count before the append and seeds the boundary as that count plus the batch length. The mutation lock excludes every other writer for the whole region, so the sum equals the count the read after the append used to return. Once the append succeeds the recording is pure assignment that cannot fail, leaving nothing that can raise between the durable write and its bookkeeping. When the read itself fails the append has not started, no boundary is recorded, the ever recorded flag stays untouched, and a retry begins clean. The resumed pending write path records boundaries through the same hook, so it follows the same order without further changes. The new regression persists a batch through the hook against a store that fails every read issued after an append: the call completes, the batch lands exactly once, the recorded boundary matches the count a read after the append would have produced, and the compaction keyed on the response preserves the turn persisted past it. Without the source change the injected read failure propagates out of the hook after the durable write.
1 parent c755d21 commit 901a543

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎src/agents/memory/openai_responses_compaction_session.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,20 @@ async def _add_items_for_response(
556556
between the batch and the count recorded for it, so run_compaction can
557557
later preserve everything past the boundary regardless of what other
558558
runs appended before its own snapshot.
559+
560+
The count that seeds the boundary is read before the append. The lock
561+
excludes every other writer for the whole region, so that count plus
562+
the batch length equals the count a read after the append would return,
563+
and once the append succeeds the recording below is pure assignment
564+
that cannot fail. A history read that failed after a successful append
565+
would surface an error for a batch the backend already holds, and
566+
retrying the turn would persist it again. When the read itself fails,
567+
the append has not started, so no boundary is recorded, the ever
568+
recorded flag stays untouched, and a retry begins clean.
559569
"""
560570
async with self._mutation_lock:
571+
boundary = len(await self._get_all_underlying_session_items()) + len(items)
561572
await self._add_items_locked(items)
562-
boundary = len(await self._get_all_underlying_session_items())
563573
self._response_boundaries.pop(response_id, None)
564574
self._response_boundaries[response_id] = boundary
565575
self._response_boundaries_ever_recorded = True

‎tests/memory/test_openai_responses_compaction_session.py‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,6 +2477,77 @@ async def test_compaction_skips_for_recorded_response_after_clear_session(
24772477
mock_client.responses.compact.assert_not_awaited()
24782478
assert "Skipped compaction for resp_recorded" in caplog.text
24792479

2480+
@pytest.mark.asyncio
2481+
async def test_persisted_batch_survives_read_failure_after_append(self) -> None:
2482+
"""A failing history read must not fail a batch the backend already holds.
2483+
2484+
Ordering under test: a response's batch is persisted through the
2485+
boundary hook while the underlying store fails every read issued
2486+
after an append. The count that seeds the boundary is read before
2487+
the append, so the hook records the boundary without touching the
2488+
failing read and the caller never sees an error for a turn that is
2489+
already stored; a read after the append would raise here, the run
2490+
would treat the persisted turn as failed, and a retry would append
2491+
it again. The compaction keyed on the response then proves the
2492+
recorded boundary equals the count a read after the append would
2493+
have produced: the turn persisted after the batch is preserved as
2494+
the tail.
2495+
"""
2496+
turn = cast(
2497+
TResponseInputItem, {"type": "message", "role": "assistant", "content": "turn a"}
2498+
)
2499+
later_turn = cast(
2500+
TResponseInputItem, {"type": "message", "role": "assistant", "content": "turn later"}
2501+
)
2502+
summary = cast(TResponseInputItem, {"type": "compaction", "summary": "through a"})
2503+
2504+
class ReadFailsAfterAppendSession(SimpleListSession):
2505+
def __init__(self) -> None:
2506+
super().__init__()
2507+
self.add_calls = 0
2508+
self.failing = True
2509+
2510+
async def add_items(self, items: list[TResponseInputItem]) -> None:
2511+
self.add_calls += 1
2512+
await super().add_items(items)
2513+
2514+
async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:
2515+
if self.failing and self.add_calls:
2516+
raise RuntimeError("history read failed after the batch was appended")
2517+
return await super().get_items(limit)
2518+
2519+
underlying = ReadFailsAfterAppendSession()
2520+
mock_compact_response = MagicMock()
2521+
mock_compact_response.output = [summary]
2522+
mock_client = MagicMock()
2523+
mock_client.responses.compact = AsyncMock(return_value=mock_compact_response)
2524+
session = OpenAIResponsesCompactionSession(
2525+
session_id="read-failure-boundary",
2526+
underlying_session=underlying,
2527+
client=mock_client,
2528+
compaction_mode="previous_response_id",
2529+
)
2530+
2531+
await session._add_items_for_response([turn], response_id="resp_a")
2532+
2533+
# The append landed exactly once and the boundary bookkeeping finished
2534+
# even though every read after the append would have failed.
2535+
assert underlying.add_calls == 1
2536+
assert session._response_boundaries["resp_a"] == 1
2537+
assert session._response_boundaries_ever_recorded is True
2538+
2539+
underlying.failing = False
2540+
assert await underlying.get_items() == [turn]
2541+
2542+
await session.add_items([later_turn])
2543+
await session.run_compaction({"force": True, "response_id": "resp_a"})
2544+
2545+
# The recorded boundary matches the count a read after the append
2546+
# would have produced, so the turn persisted after the batch is the
2547+
# tail the replacement preserves.
2548+
assert await underlying.get_items() == [summary, later_turn]
2549+
assert await session.get_items() == [summary, later_turn]
2550+
24802551

24812552
class TestTypeGuard:
24822553
def test_is_compaction_aware_session_true(self) -> None:

0 commit comments

Comments
 (0)