Skip to content

The agent that grows with you

fix(gateway): keep an accepted /steer across the exhaustion deferral; drop checkpoint re-exports

Teknium848fc0ad8be7
diff --git a/agent/agent_init.py b/agent/agent_init.py
index 58ed7e0e304..2c6c3ab0197 100644
--- a/agent/agent_init.py
+++ b/agent/agent_init.py
@@ -2036,7 +2036,7 @@ def _build_context_engine(agent, _agent_cfg, cs, _custom_providers, _effective_c
if hasattr(_cc, _attr):
setattr(_cc, _attr, _value)
agent.compression_checkpoint_required = cs.checkpoint_required
- from agent.conversation_compression import _warn_checkpoint_required_without_capable_provider
+ from agent.conversation_compression_checkpoint import _warn_checkpoint_required_without_capable_provider
_warn_checkpoint_required_without_capable_provider(agent)
agent.codex_app_server_auto_compaction = cs.codex_app_server_auto
agent.codex_responses_native_compaction = cs.codex_responses_native
diff --git a/agent/codex_runtime.py b/agent/codex_runtime.py
index 8794219f8d9..11ad5243c45 100644
--- a/agent/codex_runtime.py
+++ b/agent/codex_runtime.py
@@ -729,7 +729,7 @@ def run_codex_app_server_turn(agent, *, user_message: str, original_user_message
# Defense in depth for compression.checkpoint_required: agent init refuses the combination, but
# api_mode is mutable. Explicit-True check matches compress_context().
if getattr(agent, "compression_checkpoint_required", False) is True:
- from agent.conversation_compression import _checkpoint_blocked
+ from agent.conversation_compression_checkpoint import _checkpoint_blocked
raise _checkpoint_blocked("codex_app_server owns the authoritative thread and compacts it "
"without a truthful pre-compaction transcript boundary")
_ensure_codex_session(agent, messages)
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index 922bc1d82d8..fd0873cf0d3 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -1397,14 +1397,7 @@ def run_compress_context_with_progress_timeout(
fence.revoke_commit_admission()
-from agent.conversation_compression_checkpoint import (
- _CHECKPOINT_REQUIRED_REMEDIATION,
- CompressionCheckpointUnavailable,
- _active_memory_provider_label,
- _checkpoint_blocked,
- _checkpoint_incapable,
- _warn_checkpoint_required_without_capable_provider,
-)
+from agent.conversation_compression_checkpoint import _checkpoint_blocked, _checkpoint_incapable
def _lock_api_is_absent_on_session_db(lock_db: Any) -> bool:
diff --git a/gateway/run_busy.py b/gateway/run_busy.py
index 8dd9901ec6c..932e1b8217d 100644
--- a/gateway/run_busy.py
+++ b/gateway/run_busy.py
@@ -131,6 +131,24 @@ class GatewayBusySessionMixin:
# else: no adapter — leave the head in place so we don't silently drop it.
return pending_event
+ def _defer_past_exhaustion_reset(
+ self, result: Any, adapter: Any, source: SessionSource, session_key: str, pending_event: MessageEvent,
+ ) -> None:
+ """Leave the post-turn drain to the fresh session after a compression-exhausted turn.
+
+ The outer handler owns the reset: the exact queued event goes back in the head slot so the
+ adapter's post-handler drain starts it on the fresh session (never promoted or prepared on the
+ exhausted one). An accepted leftover /steer was already taken out of the agent's buffer, so it
+ keeps its usual place ahead of that event instead of vanishing.
+ """
+ leftover_steer = (result.get("pending_steer") or "").strip()
+ if leftover_steer:
+ steer_prompt, steer_source = self._pinned_channel_inputs(session_key, None, source, internal=True)
+ self._session_state(session_key).conversation.queued_events.insert(0, pending_event)
+ pending_event = MessageEvent(text=leftover_steer, source=steer_source, channel_prompt=steer_prompt)
+ adapter._pending_messages[session_key] = pending_event
+ logger.info("Deferring queued follow-up for session %s until after its compression-exhausted reset", session_key)
+
def _merge_leftover_steer(
self, result: Any, adapter: Any, source: SessionSource, session_key: Optional[str],
pending_event: Optional[MessageEvent], pending: Optional[str],
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index 6bba5446291..95af473302a 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -1717,14 +1717,7 @@ class GatewayTurnMixin(GatewayTurnPrepareMixin, GatewayTurnHygieneMixin, Gateway
if result and adapter and session_key:
pending_event = _dequeue_pending_event(adapter, session_key)
if result.get("compression_exhausted") and pending_event is not None:
- # The outer handler owns the reset. Preserve the exact event in the head slot so the
- # adapter's post-handler drain starts it against the fresh session; do not promote or
- # prepare native media on the exhausted session.
- adapter._pending_messages[session_key] = pending_event
- logger.info(
- "Deferring queued follow-up for session %s until after its compression-exhausted reset",
- session_key,
- )
+ self._defer_past_exhaustion_reset(result, adapter, source, session_key, pending_event)
return None, None
# /queue overflow: promote the next queued event into the consumed "next-up" slot so the
# recursive drain sees it (keeps FIFO order; a mid-chain /queue can't jump the queue).
diff --git a/tests/agent/test_compaction_events.py b/tests/agent/test_compaction_events.py
index 0502658c1a7..62d26ca2907 100644
--- a/tests/agent/test_compaction_events.py
+++ b/tests/agent/test_compaction_events.py
@@ -17,7 +17,8 @@ import pytest
from agent import compaction_events
from agent.context_compressor import ContextCompressor
-from agent.conversation_compression import CompressionCheckpointUnavailable, CompressionCommitFence, compress_context
+from agent.conversation_compression import CompressionCommitFence, compress_context
+from agent.conversation_compression_checkpoint import CompressionCheckpointUnavailable
from agent.transports.codex_app_server_session import TurnResult
SECRET = "TOPSECRET_TRANSCRIPT_TEXT"
diff --git a/tests/agent/test_compression_attempt_effect.py b/tests/agent/test_compression_attempt_effect.py
index 47670adbba5..5b23d238e75 100644
--- a/tests/agent/test_compression_attempt_effect.py
+++ b/tests/agent/test_compression_attempt_effect.py
@@ -13,7 +13,8 @@ from unittest.mock import patch
import pytest
from agent.context_compressor import ContextCompressor
-from agent.conversation_compression import CompressionCheckpointUnavailable, compress_context
+from agent.conversation_compression import compress_context
+from agent.conversation_compression_checkpoint import CompressionCheckpointUnavailable
from agent.transports.codex_app_server_session import TurnResult
from hermes_cli.observability import shared_metrics_events
from hermes_cli.observability.shared_metrics_fields import compression_fields
diff --git a/tests/agent/test_compression_stall_fallback.py b/tests/agent/test_compression_stall_fallback.py
index 3c694029592..65c63bc099f 100644
--- a/tests/agent/test_compression_stall_fallback.py
+++ b/tests/agent/test_compression_stall_fallback.py
@@ -240,8 +240,10 @@ def test_total_ceiling_exhaustion_does_not_start_fallback():
try:
msgs, prompt = _run(
- worker, chain=[dict(CHAIN_ENTRY, timeout=0.3)], timeouts=timeouts, messages=original,
- idle=0.3, ceiling=0.3,
+ # idle == ceiling: the primary's idle expiry spends the whole shared budget. >= 2 s so a
+ # loaded runner still starts the primary worker inside it (tests/AGENTS.md).
+ worker, chain=[dict(CHAIN_ENTRY, timeout=2.0)], timeouts=timeouts, messages=original,
+ idle=2.0, ceiling=2.0,
)
finally:
worker.release.set()
diff --git a/tests/agent/test_pre_compress_checkpoint_contract.py b/tests/agent/test_pre_compress_checkpoint_contract.py
index 28dec1e1586..ecf312d6e2a 100644
--- a/tests/agent/test_pre_compress_checkpoint_contract.py
+++ b/tests/agent/test_pre_compress_checkpoint_contract.py
@@ -15,10 +15,12 @@ from types import SimpleNamespace
import pytest
from agent.conversation_compression import (
- CompressionCheckpointUnavailable,
- _checkpoint_blocked,
_direct_messages_for_pre_compress_memory,
_pre_compress_memory_context,
+)
+from agent.conversation_compression_checkpoint import (
+ CompressionCheckpointUnavailable,
+ _checkpoint_blocked,
_warn_checkpoint_required_without_capable_provider,
)
from agent.context_compressor import COMPRESSED_SUMMARY_METADATA_KEY
diff --git a/tests/gateway/test_queued_native_image_session_key.py b/tests/gateway/test_queued_native_image_session_key.py
index 9d8d6d06b5e..713c1e3f4e2 100644
--- a/tests/gateway/test_queued_native_image_session_key.py
+++ b/tests/gateway/test_queued_native_image_session_key.py
@@ -69,6 +69,8 @@ class CaptureQueuedNativeImageAgent:
class CompressionExhaustedAgent(CaptureQueuedNativeImageAgent):
+ pending_steer = None
+
def run_conversation(self, message, conversation_history=None, task_id=None):
type(self).calls.append(message)
return {
@@ -77,6 +79,7 @@ class CompressionExhaustedAgent(CaptureQueuedNativeImageAgent):
"api_calls": 1,
"failed": True,
"compression_exhausted": True,
+ "pending_steer": type(self).pending_steer,
}
@@ -167,10 +170,12 @@ async def test_queued_followup_uses_pending_event_session_key_for_native_images(
@pytest.mark.asyncio
[email protected]("leftover_steer", [None, "use the short answer"])
async def test_compression_exhaustion_defers_queued_event_for_fresh_session(
- monkeypatch, tmp_path
+ monkeypatch, tmp_path, leftover_steer
):
CompressionExhaustedAgent.calls = []
+ CompressionExhaustedAgent.pending_steer = leftover_steer
fake_dotenv = types.ModuleType("dotenv")
fake_dotenv.load_dotenv = lambda *args, **kwargs: None
@@ -232,9 +237,16 @@ async def test_compression_exhaustion_defers_queued_event_for_fresh_session(
assert len(CompressionExhaustedAgent.calls) == 1, (
"the queued turn must not recurse into the exhausted session"
)
- assert adapter._pending_messages[session_key] is pending_event, (
- "the exact native event must survive for the post-reset adapter drain"
- )
- assert runner._queued_events[session_key] == [overflow_event], (
- "deferral must not promote or reorder later FIFO events"
- )
+ if leftover_steer is None:
+ assert adapter._pending_messages[session_key] is pending_event, (
+ "the exact native event must survive for the post-reset adapter drain"
+ )
+ assert runner._queued_events[session_key] == [overflow_event], (
+ "deferral must not promote or reorder later FIFO events"
+ )
+ else:
+ # The agent already took the accepted /steer out of its buffer: it keeps its usual place
+ # ahead of the queued event instead of vanishing with the exhausted turn.
+ head = adapter._pending_messages[session_key]
+ assert head.text == leftover_steer
+ assert runner._queued_events[session_key] == [pending_event, overflow_event]