The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(tui_gateway): a manual-compress busy race maps to the busy reply
kshitijk4poor1ece75f5ee17
diff --git a/tests/tui_gateway/test_busy_compression_demotion.py b/tests/tui_gateway/test_busy_compression_demotion.pyindex ec514089089..acec96f843b 100644--- a/tests/tui_gateway/test_busy_compression_demotion.py+++ b/tests/tui_gateway/test_busy_compression_demotion.py@@ -218,6 +218,8 @@ def test_submit_during_manual_compress_is_queued_and_reply_persists(monkeypatch)assert entered.wait(5)server._interrupt_session_turn("sid", session) # Stop mid-compaction must not release the busy claimassert session["running"] is True+ second = server._methods["session.compress"]("r3", {"session_id": "sid"}) # the locked claim maps to busy+ assert second["error"]["code"] == 4009resp = server._handle_busy_submit("r2", "sid", session, "question", "ws-1")gate.set()rpc.join(5)diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.pyindex 745ba5f1a20..883b9cebd94 100644--- a/tui_gateway/methods_session.py+++ b/tui_gateway/methods_session.py@@ -2152,11 +2152,11 @@ def _(rid, params: dict) -> dict:session, err = _sess(params, rid)if err:return err- if session.get("running"):- return _err(rid, 4009, busy_message("compress"))try:with _manual_compress_turn(sid := params.get("session_id", ""), session):return _compress_live(rid, sid, session, _str_param(params, "focus_topic"))+ except CompressionBusy as e:+ return _err(rid, 4009, str(e))except CompressionLockHeld as e:_status_update(sid, "ready")from agent.manual_compression_feedback import describe_compression_lock_skipdiff --git a/tui_gateway/methods_slash.py b/tui_gateway/methods_slash.pyindex f97c6bef304..9865d115a77 100644--- a/tui_gateway/methods_slash.py+++ b/tui_gateway/methods_slash.py@@ -491,6 +491,8 @@ def _mirror_slash_side_effects(sid: str, session: dict, command: str) -> str:# profile scope or /model's credential read raises UnscopedSecretError under multiplex (#122655).with _session_profile_runtime_scope(session):return mirror(sid, session, agent, arg) or ""+ except CompressionBusy as e: # a turn won the race after the unlocked running check above+ return str(e)except Exception as e:if name == "compress" and agent:from agent.conversation_compression import finalize_context_engine_compression_notificationdiff --git a/tui_gateway/methods_tools.py b/tui_gateway/methods_tools.pyindex a14c51d6017..6c341a831e8 100644--- a/tui_gateway/methods_tools.py+++ b/tui_gateway/methods_tools.py@@ -1004,6 +1004,8 @@ def _cmd_compress(rid, params, session, name, arg):try:output = _compress_live_with_feedback(sid, session, session["agent"], arg, snapshot_kwargs=True)return _exec_out(rid, output)+ except CompressionBusy as exc: # a turn won the race after the unlocked pre-check above+ return _err(rid, 4009, str(exc))except Exception as exc:_tools_mod("agent.conversation_compression").finalize_context_engine_compression_notification(session["agent"], committed=False)diff --git a/tui_gateway/session_compression.py b/tui_gateway/session_compression.pyindex 90917cd8869..7a5981d49db 100644--- a/tui_gateway/session_compression.py+++ b/tui_gateway/session_compression.py@@ -219,6 +219,10 @@ class CompressionLockHeld(Exception):super().__init__(f"Compression lock held: {holder or 'unknown'}")+class CompressionBusy(Exception):+ """Raised by _manual_compress_turn when a turn already holds the session; callers map it to their busy reply."""++@contextlib.contextmanagerdef _manual_compress_turn(sid: str, session: dict):"""Hold the session busy (``running``) for a whole manual compaction: snapshot, LLM summary, commit@@ -227,7 +231,7 @@ def _manual_compress_turn(sid: str, session: dict):about to bump, which dropped the turn's reply (#133504). The queued prompt drains on release."""with session["history_lock"]:if session.get("running"):- raise RuntimeError(busy_message("compress"))+ raise CompressionBusy(busy_message("compress"))session["running"] = session["_manual_compress_active"] = Truetry:yield