Skip to content

Commit 802cc03

Browse files
fix(sessions): recover handoffs after cancelled compaction (#5197)
* fix(sessions): recover handoffs after cancelled compaction * fix(sessions): preserve compaction evidence across restored retries * fix(sessions): require confirmed compaction before settling recovery
1 parent c42f3c6 commit 802cc03

4 files changed

Lines changed: 414 additions & 24 deletions

File tree

‎src/agents/memory/openai_responses_compaction_session.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
OpenAIResponsesCompactionArgs,
2424
OpenAIResponsesCompactionAwareSession,
2525
SessionABC,
26+
_await_mutation,
2627
_CompactionSnapshot,
2728
)
2829

@@ -456,8 +457,20 @@ async def _run_compaction_locked(
456457
)
457458

458459
if snapshot is not None:
459-
try:
460+
461+
async def replace_snapshot(snapshot: _CompactionSnapshot) -> bool:
460462
replaced = await snapshot.replace_suffix(suffix_start, output_items)
463+
if replaced:
464+
# Publish the backend's successful outcome before caller cancellation
465+
# is propagated. Runner can settle its checkpoint without inferring
466+
# success from history that merely differs from the original append.
467+
self._deferred_response_id = None
468+
if wrapper is not None:
469+
wrapper._session_compaction_completed = True # type: ignore[attr-defined]
470+
return replaced
471+
472+
try:
473+
replaced = await _await_mutation(replace_snapshot(snapshot))
461474
if not replaced:
462475
logger.warning(
463476
"Skipped compaction replacement because the stored suffix changed."

‎src/agents/run_internal/session_persistence.py‎

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,12 @@ async def save_result_to_session(
821821
"store": store,
822822
"has_local_tool_outputs": has_local_tool_outputs,
823823
}
824+
model_exchange = getattr(compaction_wrapper, "_session_compaction_model_exchange", None)
825+
if model_exchange is not None:
826+
resumed_write_state._pending_session_write["compaction_model_exchange"] = {
827+
"item_digests": list(model_exchange[0]),
828+
"reasoning_item_id_policy": model_exchange[1],
829+
}
824830
# resume_pending_session_write() applies post-write compaction itself once the
825831
# checkpoint settles, whether that happens inline below or on a later, separate
826832
# resume -- so it is not repeated after this call returns.
@@ -912,16 +918,18 @@ def digests(items: Sequence[TResponseInputItem]) -> list[str]:
912918

913919
run_state._session_write_in_progress = True
914920
try:
921+
acknowledged = pending.get("append_acknowledged", False)
915922
before = pending["before"]
916-
if before is None:
923+
committed = False
924+
if before is None and not acknowledged:
917925
# No append has started. Retain the batch even if this first read fails.
918926
tail = await _session_get_items(
919927
session, limit=len(pending["items"]) + 1, wrapper=wrapper
920928
)
921929
pending["before"] = digests(tail)
922930
append = True
923931
else:
924-
expected = before + digests(pending["items"])
932+
expected = (before or []) + digests(pending["items"])
925933
observed_generation: int | None = None
926934
get_with_generation = getattr(session, "_get_items_with_generation", None)
927935
if wrapper is not None and callable(get_with_generation):
@@ -934,32 +942,53 @@ def digests(items: Sequence[TResponseInputItem]) -> list[str]:
934942
observed = digests(tail)
935943
committed = observed == expected
936944
unchanged = observed[-len(before) :] == before if before else not observed
937-
if committed == unchanged:
945+
if (acknowledged and not committed) or (not acknowledged and committed == unchanged):
938946
raise UserError(
939-
"Cannot reconcile the pending Session write: history changed or is ambiguous. "
940-
"Repair the original Session before resuming; do not rerun the completed tool."
947+
"Cannot reconcile the pending Session write: history changed or is "
948+
"ambiguous. Repair the original Session before resuming; do not rerun "
949+
"the completed tool."
941950
)
942-
append = unchanged
951+
append = not acknowledged and unchanged
943952
# The original append can advance the wrapper generation even when it fails
944953
# atomically. Reconciled unchanged history is also safe to append against;
945954
# subsequent mutations still revoke ownership through the normal generation check.
946-
if observed_generation is not None and wrapper is not None:
955+
# An acknowledged append can regain compaction ownership only when its exact
956+
# history remains, as after replacement rollback. Never re-append that batch.
957+
if (
958+
observed_generation is not None
959+
and wrapper is not None
960+
and (not acknowledged or committed)
961+
):
947962
wrapper._session_compaction_generation = observed_generation # type: ignore[attr-defined]
948963
if append:
949-
# Backends may retain or transform their input; the durable checkpoint stays detached.
964+
# Keep the checkpoint detached from backend input retention or transformation.
950965
await _session_add_items(session, copy.deepcopy(pending["items"]), wrapper=wrapper)
966+
# Restore evidence from the successful exchange, never from all stored history.
967+
# A changed history after replacement must not regain compaction ownership.
968+
model_exchange = pending.get("compaction_model_exchange")
969+
if wrapper is not None and model_exchange is not None and (not acknowledged or committed):
970+
wrapper._session_compaction_model_exchange = ( # type: ignore[attr-defined]
971+
tuple(model_exchange["item_digests"]),
972+
model_exchange["reasoning_item_id_policy"],
973+
)
974+
pending["append_acknowledged"] = True
951975
run_state._current_turn_persisted_item_count = pending["persisted_count"]
952-
# Keep the checkpoint until compaction also settles: if _apply_post_write_compaction
953-
# raises below, a later retry must still be able to redo just the compaction step
954-
# instead of silently losing it. The append itself is retry-safe (the reconciliation
955-
# above detects an already-committed batch and skips re-appending it).
956-
await _apply_post_write_compaction(
957-
session,
958-
response_id=pending.get("response_id"),
959-
store=pending.get("store"),
960-
has_local_tool_outputs=pending.get("has_local_tool_outputs", False),
961-
wrapper=wrapper,
962-
)
976+
# A confirmed atomic replacement settles this write even when the caller is
977+
# cancelled before receiving its acknowledgement. All other failed replacements
978+
# retain the checkpoint and must reconcile against the original appended history.
979+
if wrapper is not None:
980+
wrapper._session_compaction_completed = False # type: ignore[attr-defined]
981+
try:
982+
await _apply_post_write_compaction(
983+
session,
984+
response_id=pending.get("response_id"),
985+
store=pending.get("store"),
986+
has_local_tool_outputs=pending.get("has_local_tool_outputs", False),
987+
wrapper=wrapper,
988+
)
989+
finally:
990+
if wrapper is not None and getattr(wrapper, "_session_compaction_completed", False):
991+
run_state._pending_session_write = None
963992
run_state._pending_session_write = None
964993
finally:
965994
run_state._session_write_in_progress = False

‎src/agents/run_state.py‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,23 @@
168168
]
169169

170170

171+
class _SessionCompactionModelExchange(TypedDict):
172+
"""Digests of one successful model exchange and its resolved replay policy."""
173+
174+
item_digests: list[str]
175+
reasoning_item_id_policy: Literal["preserve", "omit"] | None
176+
177+
171178
class _PendingSessionWrite(TypedDict):
172179
"""One canonical resumed-output append awaiting acknowledgement."""
173180

174181
session_id: str
175182
items: list[TResponseInputItem]
176183
before: list[str] | None
177184
persisted_count: int
185+
# Once acknowledged, only compaction remains; its replacement can change the history.
186+
append_acknowledged: NotRequired[bool]
187+
compaction_model_exchange: NotRequired[_SessionCompactionModelExchange]
178188
# Compaction inputs for the batch this checkpoint is settling, so a later, separate
179189
# resume_pending_session_write() call (not the original save_result_to_session() call)
180190
# can still apply the same post-write Responses compaction decision. Optional so a
@@ -242,7 +252,8 @@ def _default_run_state_validation_error(
242252
"Binds restored local MCP calls to their configured server and original tool name, "
243253
"preserves independent apply_patch approval scopes, and binds function-tool approval "
244254
"decisions to their owning agent, and retains compaction metadata for pending "
245-
"Session writes."
255+
"Session writes, including acknowledgement and model-exchange evidence for "
256+
"compaction retry."
246257
),
247258
}
248259
SUPPORTED_SCHEMA_VERSIONS = frozenset(SCHEMA_VERSION_SUMMARIES)
@@ -4512,10 +4523,21 @@ async def _build_run_state_from_json(
45124523
# Released schema 1.17 wrote exactly four keys. Compaction metadata belongs to 1.18;
45134524
# older checkpoints remain readable without claiming the newer recovery behavior.
45144525
optional_pending_write_keys = (
4515-
{"response_id", "store", "has_local_tool_outputs"}
4526+
{
4527+
"response_id",
4528+
"store",
4529+
"has_local_tool_outputs",
4530+
"append_acknowledged",
4531+
"compaction_model_exchange",
4532+
}
45164533
if (schema_major, schema_minor) >= (1, 18)
45174534
else set()
45184535
)
4536+
compaction_exchange = (
4537+
pending_write.get("compaction_model_exchange")
4538+
if isinstance(pending_write, dict)
4539+
else None
4540+
)
45194541
if (
45204542
(schema_major, schema_minor) < (1, 17)
45214543
or not isinstance(state._current_step, NextStepRunAgain | NextStepInterruption)
@@ -4545,6 +4567,26 @@ async def _build_run_state_from_json(
45454567
and pending_write["store"] is not None
45464568
and not isinstance(pending_write["store"], bool)
45474569
)
4570+
or (
4571+
"append_acknowledged" in pending_write
4572+
and not isinstance(pending_write["append_acknowledged"], bool)
4573+
)
4574+
or (
4575+
pending_write.get("append_acknowledged") is True and pending_write["before"] is None
4576+
)
4577+
or (
4578+
"compaction_model_exchange" in pending_write
4579+
and (
4580+
not isinstance(compaction_exchange, dict)
4581+
or set(compaction_exchange) != {"item_digests", "reasoning_item_id_policy"}
4582+
or not isinstance(compaction_exchange["item_digests"], list)
4583+
or not all(
4584+
isinstance(digest, str) for digest in compaction_exchange["item_digests"]
4585+
)
4586+
or compaction_exchange["reasoning_item_id_policy"]
4587+
not in (None, "preserve", "omit")
4588+
)
4589+
)
45484590
or (
45494591
"has_local_tool_outputs" in pending_write
45504592
and not isinstance(pending_write["has_local_tool_outputs"], bool)

0 commit comments

Comments
 (0)