The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): revoke commit admission on every host unwind (review F2)
Tekniumefdd22988406
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.pyindex edb96ee6697..63eb3a2b2ca 100644--- a/agent/conversation_compression.py+++ b/agent/conversation_compression.py@@ -24,6 +24,7 @@ Three concerns live here:``run_agent`` keeps thin wrappers for each so existing call sites(``self._compress_context(...)``) keep working. Tests that exercisethese paths see no behavioural change.+"""from __future__ import annotations@@ -441,6 +442,13 @@ class CompressionCommitFence:# flight" even while the commit itself is hung — which is exactly when# the overrun warning must be able to fire.self._commit_phase = threading.Event()+ # Lock-free admission revocation (#76354 review F2). Set by+ # :meth:`revoke_commit_admission` on ANY host unwind (KeyboardInterrupt,+ # cancellation, unexpected exception) without touching the fence lock,+ # so a host that cannot afford to block behind an in-flight commit can+ # still guarantee no FUTURE commit is admitted. Plain bool store —+ # atomic in CPython.+ self._admission_revoked = False# Forward-progress telemetry: the compression worker touches this# whenever the streamed summary call produces a token (see# ContextCompressor._call_summary_llm). Waiters use it to distinguish@@ -497,10 +505,7 @@ class CompressionCommitFence:def begin_commit(self, cancel_event: Any = None) -> bool:"""Atomically admit commit unless a hard cancellation already won."""self._lock.acquire()- if self._cancelled or (- cancel_event is not None and bool(cancel_event.is_set())- ):- self._cancelled = True+ if self._cancelled or self._admission_revoked:self._lock.release()return Falseself._commit_started = True@@ -529,7 +534,23 @@ class CompressionCommitFence:@propertydef is_cancelled(self) -> bool:"""True after cancellation won before the commit boundary."""- return self._cancelled+ return self._cancelled or self._admission_revoked++ def revoke_commit_admission(self) -> None:+ """Revoke FUTURE commit admission without acquiring the fence lock.++ #76354 review F2: every host unwind path (KeyboardInterrupt, task+ cancellation, unexpected exception while waiting) must guarantee a+ detached worker cannot later enter the commit boundary and mutate+ durable/session state. This is deliberately lock-free: a commit that+ is ALREADY in flight cannot be safely abandoned (the invariant+ "commit never abandoned mid-mutation" holds), but no NEW commit will+ be admitted after this call — ``begin_commit`` re-checks the flag+ under the fence lock.+ """+ self._admission_revoked = True++# Defaults for the in-agent (non-hygiene) progress-aware compress_context wrap.@@ -554,6 +575,7 @@ _compress_timeout_executor_lock = threading.Lock()_COMMIT_OVERRUN_WAIT_SLICE_SECONDS = 30.0+def _get_compress_timeout_executor():"""Return the process-wide compress-timeout DaemonThreadPoolExecutor."""global _compress_timeout_executor@@ -683,115 +705,144 @@ def run_compress_context_with_progress_timeout(# parent conversation/approval context into the worker.future = executor.submit(propagate_context_to_thread(worker), fence)wait_started = time.monotonic()- while True:- waited = time.monotonic() - wait_started- remaining_ceiling = ceiling - waited- if remaining_ceiling <= 0:- break- wait_slice = min(idle, remaining_ceiling)- try:- return future.result(timeout=wait_slice)- except concurrent.futures.TimeoutError:- waited = time.monotonic() - wait_started- since_progress = fence.seconds_since_progress()- if since_progress < idle and waited < ceiling:- logger.info(- "Context compression still streaming after %.0fs "- "(last progress %.1fs ago) — extending wait "- "(ceiling %.0fs)",- waited,- since_progress,- ceiling,- )- continue- break-- cancelled: Optional[bool] = None- while cancelled is None:- # F1: ``begin_commit`` retains the fence lock until ``finish_commit``,- # so a hung commit makes ``try_cancel_before_commit`` return None- # forever. The lock-free phase marker breaks the spin so the- # overrun-warning loop below is reachable WHILE the commit is still- # blocked.- if fence.commit_in_flight:- cancelled = False- break- cancelled = fence.try_cancel_before_commit()- if cancelled is None:- time.sleep(0.001)- if not cancelled:- # Pre-commit ceiling already elapsed, but begin_commit() won the race.- # Waiting is intentional: SessionDB mutation cannot be fence-cancelled.- # The wait is bounded in increments against the remaining ceiling: a- # commit that overruns total_ceiling_seconds is logged loudly and- # surfaced once (on_commit_overrun), then waited on in bounded slices- # with escalating log level until it completes. Guarantee: summary- # phase bounded by ceiling; commit phase logged + surfaced if it- # exceeds it — never silently hung, never abandoned mid-commit.- overrun_surfaced = False- overrun_reports = 0+ # F2: EVERY host unwind (KeyboardInterrupt, task cancellation, unexpected+ # exception while waiting) must revoke future commit admission before the+ # host resumes, or a detached worker could later commit and mutate durable+ # state behind the caller's back. ``handled_exit`` marks the paths that+ # settle admission themselves (worker result returned, or fence cancel+ # won); everything else revokes in the ``finally``.+ handled_exit = False+ try:while True:waited = time.monotonic() - wait_started- remaining = ceiling - waited- if remaining <= 0:- # Ceiling breached while the commit is in flight. Wait in- # bounded increments so each overrun window is visible in- # logs rather than one silent unbounded block.- remaining = min(- _COMMIT_OVERRUN_WAIT_SLICE_SECONDS,- max(ceiling, 0.05),- )- overrun_reports += 1- log = logger.warning if overrun_reports <= 2 else logger.error- log(- "Context compression SessionDB commit still running "- "%.1fs past the total ceiling (waited %.1fs, ceiling "- "%.1fs); commit cannot be abandoned mid-flight — "- "continuing to wait (check SessionDB health if this "- "persists)",- waited - ceiling,- waited,- ceiling,- )- if not overrun_surfaced and on_commit_overrun is not None:- overrun_surfaced = True- try:- on_commit_overrun(waited, ceiling)- except Exception:- logger.debug(- "compress_context commit-overrun callback failed",- exc_info=True,- )+ remaining_ceiling = ceiling - waited+ if remaining_ceiling <= 0:+ break+ wait_slice = min(idle, remaining_ceiling)try:- return future.result(timeout=remaining)+ result = future.result(timeout=wait_slice)+ handled_exit = True+ return resultexcept concurrent.futures.TimeoutError:- # Fence progress (commit-phase touch_progress) is informative- # only — the commit must complete regardless; loop and- # re-report with the updated overrun window.- continue+ waited = time.monotonic() - wait_started+ since_progress = fence.seconds_since_progress()+ if since_progress < idle and waited < ceiling:+ logger.info(+ "Context compression still streaming after %.0fs "+ "(last progress %.1fs ago) — extending wait "+ "(ceiling %.0fs)",+ waited,+ since_progress,+ ceiling,+ )+ continue+ break- waited = time.monotonic() - wait_started- since_progress = fence.seconds_since_progress()- if on_timeout is not None:- try:- on_timeout(idle, waited, since_progress)- except Exception:- logger.debug(- "compress_context timeout callback failed",- exc_info=True,++ cancelled: Optional[bool] = None+ while cancelled is None:+ # F1: ``begin_commit`` retains the fence lock until+ # ``finish_commit``, so a hung commit makes+ # ``try_cancel_before_commit`` return None forever. The lock-free+ # phase marker breaks the spin so the overrun-warning loop below+ # is reachable WHILE the commit is still blocked.+ if fence.commit_in_flight:+ cancelled = False+ break+ cancelled = fence.try_cancel_before_commit()+ if cancelled is None:+ time.sleep(0.001)+ if not cancelled:+ # Pre-commit ceiling already elapsed, but begin_commit() won the+ # race. Waiting is intentional: SessionDB mutation cannot be+ # fence-cancelled. The wait is bounded in increments against the+ # remaining ceiling: a commit that overruns total_ceiling_seconds+ # is logged loudly and surfaced once (on_commit_overrun), then+ # waited on in bounded slices with escalating log level until it+ # completes. Guarantee: summary phase bounded by ceiling; commit+ # phase logged + surfaced if it exceeds it — never silently hung,+ # never abandoned mid-commit. F1: this loop is reachable WHILE+ # the commit is blocked (commit_in_flight is lock-free), so the+ # warning + on_commit_overrun fire during the hang, not after it.+ overrun_surfaced = False+ overrun_reports = 0+ while True:+ waited = time.monotonic() - wait_started+ remaining = ceiling - waited+ if remaining <= 0:+ # Ceiling breached while the commit is in flight. Wait in+ # bounded increments so each overrun window is visible in+ # logs rather than one silent unbounded block.+ remaining = min(+ _COMMIT_OVERRUN_WAIT_SLICE_SECONDS,+ max(ceiling, 0.05),+ )+ overrun_reports += 1+ log = (+ logger.warning if overrun_reports <= 2 else logger.error+ )+ log(+ "Context compression SessionDB commit still running "+ "%.1fs past the total ceiling (waited %.1fs, ceiling "+ "%.1fs); commit cannot be abandoned mid-flight — "+ "continuing to wait (check SessionDB health if this "+ "persists)",+ waited - ceiling,+ waited,+ ceiling,+ )+ if not overrun_surfaced and on_commit_overrun is not None:+ overrun_surfaced = True+ try:+ on_commit_overrun(waited, ceiling)+ except Exception:+ logger.debug(+ "compress_context commit-overrun callback "+ "failed",+ exc_info=True,+ )+ try:+ result = future.result(timeout=remaining)+ handled_exit = True+ return result+ except concurrent.futures.TimeoutError:+ # Fence progress (commit-phase touch_progress) is+ # informative only — the commit must complete regardless;+ # loop and re-report with the updated overrun window.+ continue++ # Idle-timeout path: cancellation won before the commit boundary.+ # The fence blocks any future commit.+ handled_exit = True+ waited = time.monotonic() - wait_started+ since_progress = fence.seconds_since_progress()+ if on_timeout is not None:+ try:+ on_timeout(idle, waited, since_progress)+ except Exception:+ logger.debug(+ "compress_context timeout callback failed",+ exc_info=True,+ )+ else:+ logger.warning(+ "Context compression made no progress for %.1fs "+ "(total wait %.1fs, ceiling %.1fs); continuing without "+ "compression",+ since_progress,+ waited,+ ceiling,)- else:- logger.warning(- "Context compression made no progress for %.1fs "- "(total wait %.1fs, ceiling %.1fs); continuing without "- "compression",- since_progress,- waited,- ceiling,- )- # Leave the future on the shared pool: fence cancel won, so a late- # commit cannot land (same detachment model as gateway hygiene).- return messages, _resolve_fallback_prompt()+ # Leave the future on the shared pool: fence cancel won, so a late+ # commit cannot land (same detachment model as gateway hygiene).+ return messages, _resolve_fallback_prompt()+ finally:+ if not handled_exit:+ # F2: KeyboardInterrupt / cancellation / any unexpected exception+ # while waiting — revoke commit admission (and release the+ # worker's durable lease via the holder-qualified hook) before+ # the host unwinds, so the detached worker can never publish.+ fence.revoke_commit_admission()def _lock_api_is_absent_on_session_db(lock_db: Any) -> bool:diff --git a/gateway/run.py b/gateway/run.pyindex 6a54feac157..971686f6d3b 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -16746,6 +16746,24 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew_werr,)raise+ except BaseException:+ # #76354 F2: non-timeout unwind while the+ # detached hygiene worker may still run —+ # KeyboardInterrupt, task cancellation, or+ # any unexpected error. Revoke commit+ # admission (and release the worker's+ # durable lease via the holder-qualified+ # hook) BEFORE the host unwinds so the+ # worker can never commit later.+ _hyg_commit_fence.revoke_commit_admission()+ if not _hyg_cleanup_deferred:+ self._defer_agent_cleanup_until_future_done(+ _hyg_future,+ _hyg_agent,+ context="session hygiene unwind",+ )+ _hyg_cleanup_deferred = True+ raise# _compress_context ends the old session and creates# a new session_id. Write compressed messages intodiff --git a/tests/agent/test_compression_review_76354.py b/tests/agent/test_compression_review_76354.pyindex 3b995c586f0..d0aba01aff0 100644--- a/tests/agent/test_compression_review_76354.py+++ b/tests/agent/test_compression_review_76354.py@@ -26,6 +26,7 @@ import timeimport pytest+import agent.conversation_compression as ccfrom agent.conversation_compression import (CompressionCommitFence,run_compress_context_with_progress_timeout,@@ -136,3 +137,102 @@ class TestF1CommitOverrunWhileHung:assert fence.commit_in_flight is Truefence.finish_commit()assert fence.commit_in_flight is False+++class _KIOnFirstResultFuture:+ """Future proxy raising on the host's first result() call."""++ def __init__(self, inner, exc, gate=None):+ self._inner = inner+ self._exc = exc+ self._raised = False+ self._gate = gate++ def result(self, timeout=None):+ if not self._raised:+ self._raised = True+ if self._gate is not None:+ # Ensure the pooled worker has genuinely STARTED before the+ # host unwinds, so the test exercises "unwind with a live+ # worker" rather than the queued-job skip path.+ assert self._gate.wait(timeout=5)+ raise self._exc+ return self._inner.result(timeout=timeout)++ def __getattr__(self, name):+ return getattr(self._inner, name)+++class _InjectingExecutor:+ def __init__(self, inner, exc, gate=None):+ self._inner = inner+ self._exc = exc+ self._gate = gate++ def submit(self, fn, *args, **kwargs):+ return _KIOnFirstResultFuture(+ self._inner.submit(fn, *args, **kwargs), self._exc, self._gate+ )+++class TestF2HostUnwindRevokesAdmission:+ @pytest.mark.parametrize(+ "exc_type", [KeyboardInterrupt, RuntimeError], ids=["ki", "generic"]+ )+ def test_unwind_revokes_commit_admission_before_host_returns(+ self, monkeypatch, exc_type+ ):+ """KI/exception while waiting → detached worker can never commit.++ The worker is still blocked pre-commit when the host unwinds; the+ assertions run BEFORE the worker is released.+ """+ original = [{"role": "user", "content": "keep"}]+ started = threading.Event()+ release = threading.Event()+ fence_box = {}+ commit_admitted = {}++ def worker(fence: CompressionCommitFence):+ fence_box["fence"] = fence+ started.set()+ assert release.wait(timeout=10)+ commit_admitted["value"] = fence.begin_commit()+ if commit_admitted["value"]:+ fence.finish_commit()+ return ([{"role": "assistant", "content": "late"}], "x")++ real_executor = cc._get_compress_timeout_executor()+ monkeypatch.setattr(+ cc,+ "_get_compress_timeout_executor",+ lambda: _InjectingExecutor(real_executor, exc_type(), gate=started),+ )++ with pytest.raises(exc_type):+ run_compress_context_with_progress_timeout(+ worker=worker,+ messages=original,+ system_prompt_fallback="fallback",+ idle_timeout_seconds=5.0,+ total_ceiling_seconds=5.0,+ )++ # ── Host has unwound; worker is STILL blocked pre-commit ─────────+ assert started.wait(timeout=2)+ fence = fence_box["fence"]+ assert not release.is_set()+ assert fence.is_cancelled, (+ "host unwind must revoke commit admission while the worker "+ "is still running"+ )+ # Now release the worker and prove its commit was refused.+ release.set()+ deadline = time.time() + 5+ while time.time() < deadline and "value" not in commit_admitted:+ time.sleep(0.01)+ assert commit_admitted.get("value") is False, (+ "a worker surviving a host unwind must be denied the commit "+ "boundary"+ )+ _drain_admission_slots()