Skip to content

The agent that grows with you

fix(gateway): hygiene no-commit warning names the real cause instead of "no session_db"

teknium1762c4d357602
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index 77b0f7c7675..569e1d637cb 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -139,6 +139,30 @@ def bound_model_input_without_hygiene(history: List[Any], limit: int) -> List[An
return history[:head_end] + history[tail_start:]
+def hygiene_no_commit_reason(agent) -> str:
+ """Name WHY a hygiene compression left the session id unchanged with no in-place commit.
+ The terminal ``else`` used to blame "no session_db on the hygiene agent" for every route into it,
+ but that is one of several causes (#71097): an attempt that ABORTED before any commit boundary
+ (lock skip, transient cooldown, summary timeout, codex thread interrupted) leaves
+ ``_last_compression_attempt_in_place`` at ``None``; a DB-less agent is only the case when
+ ``_session_db`` really is missing. Read the per-attempt signals the compressor sets, in that order."""
+ if not bool(getattr(agent, "_last_compression_attempt_recorded", False)):
+ return "compression did not run"
+ lock_skip = getattr(agent, "_compression_skipped_due_to_lock", None)
+ if lock_skip is True or isinstance(lock_skip, str):
+ return "attempt skipped: compression lease held by another process"
+ blocked = getattr(agent, "_compression_blocked_transient", None)
+ if blocked:
+ return f"attempt blocked: {blocked}"
+ if getattr(agent, "_last_compression_attempt_in_place", None) is None:
+ detail = "summary timed out" if getattr(agent, "_last_compression_timed_out", False) else "aborted before commit"
+ warning = getattr(agent, "_last_compression_summary_warning", None)
+ return f"attempt {detail}" + (f": {warning}" if warning else "")
+ if getattr(agent, "_session_db", None) is None:
+ return "no session_db on the hygiene agent"
+ return "in-place commit did not complete"
+
+
class GatewayTurnMixin:
"""Agent-turn execution for GatewayRunner (see module docstring)."""
@@ -1101,9 +1125,9 @@ class GatewayTurnMixin:
_new_count = plan.msg_count
_new_tokens = plan.approx_tokens
logger.warning(
- "Gateway hygiene compression for session %s did not rotate or compact in place (no "
- "session_db on the hygiene agent) — preserving the original transcript instead "
- "of overwriting it with the summary (#21301).", session_entry.session_id,
+ "Gateway hygiene compression for session %s did not rotate or compact in place (%s) — "
+ "preserving the original transcript instead of overwriting it with the summary (#21301).",
+ session_entry.session_id, hygiene_no_commit_reason(_hyg_agent),
)
logger.info(
diff --git a/tests/gateway/test_hygiene_no_commit_reason.py b/tests/gateway/test_hygiene_no_commit_reason.py
new file mode 100644
index 00000000000..91e3d8b02f0
--- /dev/null
+++ b/tests/gateway/test_hygiene_no_commit_reason.py
@@ -0,0 +1,46 @@
+"""The hygiene "did not rotate or compact in place" warning names the real cause (#71097).
+
+The gateway's terminal branch used to blame "no session_db on the hygiene agent" for every
+route into it, including attempts that aborted before any commit boundary on a DB-backed agent.
+"""
+
+from types import SimpleNamespace
+
+from gateway.run_turn import hygiene_no_commit_reason
+
+
+def _agent(**signals):
+ base = {
+ "_last_compression_attempt_recorded": True,
+ "_last_compression_attempt_in_place": None,
+ "_compression_skipped_due_to_lock": None,
+ "_compression_blocked_transient": None,
+ "_last_compression_timed_out": False,
+ "_last_compression_summary_warning": None,
+ "_session_db": object(),
+ }
+ base.update(signals)
+ return SimpleNamespace(**base)
+
+
+def test_aborted_attempt_on_db_backed_agent_is_not_blamed_on_missing_session_db():
+ # codex_app_server thread interrupted / summary aborted: the compressor never reached the
+ # commit boundary, so `_last_compression_attempt_in_place` stays None while `_session_db` is real.
+ reason = hygiene_no_commit_reason(_agent())
+ assert "no session_db" not in reason
+ assert "aborted" in reason
+
+ timed_out = hygiene_no_commit_reason(_agent(_last_compression_timed_out=True))
+ assert "timed out" in timed_out and "no session_db" not in timed_out
+
+ lock = hygiene_no_commit_reason(_agent(_compression_skipped_due_to_lock="other-sid"))
+ assert "lease" in lock
+
+ blocked = hygiene_no_commit_reason(_agent(_compression_blocked_transient="cooldown:59"))
+ assert "cooldown:59" in blocked
+
+
+def test_missing_session_db_is_still_named_when_it_is_the_cause():
+ reason = hygiene_no_commit_reason(_agent(_last_compression_attempt_in_place=False, _session_db=None))
+ assert reason == "no session_db on the hygiene agent"
+ assert hygiene_no_commit_reason(_agent(_last_compression_attempt_recorded=False)) == "compression did not run"