Skip to content

Commit 06e9e40

Browse files
committed
address 3rd Codex review round: retain checkpoint until compaction settles
One more issue from the automated Codex review on commit 92891a6, verified with live reproduction before being fixed: resume_pending_session_write() cleared run_state._pending_session_write before calling the newly-added _apply_post_write_compaction(), so if that call raised or was cancelled, the checkpoint was already gone. A later retry would then have nothing to redo the compaction step with, silently and permanently losing the requested deferred/forced Responses compaction even though the append itself had already succeeded. Fixed by moving the compaction call inside the try block, before clearing the checkpoint. The append reconciliation above already makes a retry safe against duplicate appends (it detects an already-committed batch via digest matching and skips re-appending), so this only changes when the checkpoint is released, not the retry logic itself. Added test_fresh_streamed_handoff_retains_checkpoint_when_post_write_compaction_fails to tests/test_run_impl_resume_paths.py (73 total, up from 72), confirmed to fail against the pre-fix code (checkpoint cleared despite the compaction failure) and pass after. Full verification stack clean: make format/lint/typecheck, and the full suite (9373 passed, 33 skipped, 0 failed).
1 parent 92891a6 commit 06e9e40

2 files changed

Lines changed: 96 additions & 8 deletions

File tree

‎src/agents/run_internal/session_persistence.py‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -862,18 +862,21 @@ def digests(items: Sequence[TResponseInputItem]) -> list[str]:
862862
# Backends may retain or transform their input; the durable checkpoint stays detached.
863863
await _session_add_items(session, copy.deepcopy(pending["items"]), wrapper=wrapper)
864864
run_state._current_turn_persisted_item_count = pending["persisted_count"]
865+
# Keep the checkpoint until compaction also settles: if _apply_post_write_compaction
866+
# raises below, a later retry must still be able to redo just the compaction step
867+
# instead of silently losing it. The append itself is retry-safe (the reconciliation
868+
# above detects an already-committed batch and skips re-appending it).
869+
await _apply_post_write_compaction(
870+
session,
871+
response_id=pending.get("response_id"),
872+
store=pending.get("store"),
873+
has_local_tool_outputs=pending.get("has_local_tool_outputs", False),
874+
wrapper=compaction_wrapper if compaction_wrapper is not None else wrapper,
875+
)
865876
run_state._pending_session_write = None
866877
finally:
867878
run_state._session_write_in_progress = False
868879

869-
await _apply_post_write_compaction(
870-
session,
871-
response_id=pending.get("response_id"),
872-
store=pending.get("store"),
873-
has_local_tool_outputs=pending.get("has_local_tool_outputs", False),
874-
wrapper=compaction_wrapper if compaction_wrapper is not None else wrapper,
875-
)
876-
877880

878881
async def rewind_session_items(
879882
session: Session | None,

‎tests/test_run_impl_resume_paths.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,3 +1512,88 @@ async def compact(**kwargs: Any) -> SimpleNamespace:
15121512
# The deferred decision must actually have been forced through on the delegate's own save,
15131513
# i.e. the compact API was invoked at all -- not just checked and declined.
15141514
assert len(compact_calls) == 1
1515+
1516+
1517+
@pytest.mark.asyncio
1518+
async def test_fresh_streamed_handoff_retains_checkpoint_when_post_write_compaction_fails() -> None:
1519+
"""If the post-write compaction decision raises after a checkpointed handoff batch's append
1520+
has already settled, the checkpoint (``_pending_session_write``) must survive so a later
1521+
retry can redo just the compaction step -- clearing it before the fallible compaction call
1522+
would silently and permanently lose the requested deferred/forced compaction with no way to
1523+
recover it. See .agents/references/session-persistence.md.
1524+
"""
1525+
hook_calls: list[str | None] = []
1526+
compaction_error = RuntimeError("compaction decision hook exploded")
1527+
should_fail = True
1528+
1529+
def should_trigger_compaction(context: dict[str, Any]) -> bool:
1530+
hook_calls.append(context["response_id"])
1531+
if context["response_id"] == "resp-handoff" and should_fail:
1532+
raise compaction_error
1533+
return context["response_id"] == "resp-handoff"
1534+
1535+
compact_calls: list[list[TResponseInputItem]] = []
1536+
1537+
async def compact(**kwargs: Any) -> SimpleNamespace:
1538+
items = copy.deepcopy(kwargs["input"])
1539+
compact_calls.append(items)
1540+
return SimpleNamespace(output=items, usage=None)
1541+
1542+
backend = _FailSecondAddItemsSession()
1543+
session = OpenAIResponsesCompactionSession(
1544+
"compaction-handoff-failure-test",
1545+
underlying_session=backend,
1546+
client=cast(Any, SimpleNamespace(responses=SimpleNamespace(compact=compact))),
1547+
compaction_mode="input",
1548+
should_trigger_compaction=should_trigger_compaction,
1549+
)
1550+
1551+
model = ScriptedModel(
1552+
[
1553+
{
1554+
"output": [
1555+
get_function_tool_call("transfer_to_delegate", "{}", call_id="handoff-1")
1556+
],
1557+
"response_id": "resp-handoff",
1558+
},
1559+
{"output": [get_text_message("done")], "response_id": "resp-delegate"},
1560+
]
1561+
)
1562+
delegate = Agent(name="delegate", model=model)
1563+
triage = Agent(name="triage", model=model, handoffs=[delegate])
1564+
1565+
failed_result = Runner.run_streamed(
1566+
triage, "hello", session=session, run_config=RunConfig(tracing_disabled=True)
1567+
)
1568+
with pytest.raises(RuntimeError) as append_error:
1569+
async for _ in failed_result.stream_events():
1570+
pass
1571+
assert append_error.value is backend.error
1572+
state = failed_result.to_state()
1573+
assert state._pending_session_write is not None
1574+
1575+
# Resume: the append itself now succeeds (the backend's failure was one-shot), but the
1576+
# compaction decision hook raises for the handoff's own response_id.
1577+
with pytest.raises(RuntimeError) as compaction_error_info:
1578+
await _run_session_resume(triage, state, session, False)
1579+
assert compaction_error_info.value is compaction_error
1580+
# The checkpoint must still be present so a later retry can redo compaction alone, instead
1581+
# of the handoff's requested compaction being silently and permanently lost.
1582+
assert state._pending_session_write is not None
1583+
assert state._pending_session_write.get("response_id") == "resp-handoff"
1584+
1585+
# Retry: the hook no longer fails. The append must not be repeated (no duplicate items in
1586+
# session history), but compaction must actually run this time.
1587+
should_fail = False
1588+
hook_calls.clear()
1589+
result = await _run_session_resume(triage, state, session, False)
1590+
assert result.final_output == "done"
1591+
assert hook_calls == ["resp-handoff"]
1592+
assert len(compact_calls) == 1
1593+
stored = await session.get_items()
1594+
handoff_pair = [
1595+
str(item.get("type"))
1596+
for item in stored
1597+
if isinstance(item, dict) and item.get("call_id") == "handoff-1"
1598+
]
1599+
assert handoff_pair == ["function_call", "function_call_output"]

0 commit comments

Comments
 (0)