The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(memory): bind the checkpoint gate to every compaction authority
Jan-Stefan Janetzky8cc379b5282d
diff --git a/agent/agent_init.py b/agent/agent_init.pyindex 75a550d22ad..f9a255e6dd4 100644--- a/agent/agent_init.py+++ b/agent/agent_init.py@@ -509,6 +509,30 @@ def _normalize_run_budget_seconds(value) -> Optional[float]:return seconds+def _refuse_checkpoint_required_on_codex_app_server(+ checkpoint_required: bool, api_mode: Optional[str]+) -> None:+ """Fail closed at init when the checkpoint gate cannot be honored.++ The codex app-server owns its thread and compacts it without a truthful+ pre-compaction transcript boundary (in "native" auto-compaction mode —+ the default — Hermes never even initiates the compaction), so no+ pre-compress checkpoint can be guaranteed on this API mode. Refusing here+ keeps a turn from ever reaching a codex-owned compaction boundary; the+ compress_context() guard alone cannot cover native turns that bypass+ Hermes compression entirely.+ """+ if checkpoint_required and api_mode == "codex_app_server":+ raise RuntimeError(+ "BLOCKED_MISSING_PREREQUISITE: compression.checkpoint_required "+ "is incompatible with the codex_app_server API mode: the codex "+ "agent compacts its own thread without a truthful pre-compaction "+ "transcript boundary, so a required pre-compress checkpoint "+ "cannot be guaranteed. Disable compression.checkpoint_required "+ "or use a non-app-server API mode."+ )++def init_agent(agent,base_url: str = None,@@ -2248,6 +2272,9 @@ def init_agent(compression_checkpoint_required = is_truthy_value(_compression_cfg.get("checkpoint_required"), default=False)+ _refuse_checkpoint_required_on_codex_app_server(+ compression_checkpoint_required, getattr(agent, "api_mode", None)+ )# In-place compaction: when True, compress_context() rewrites the message# list + rebuilds the system prompt WITHOUT rotating the session id (no# parent_session_id chain, no `name #N` renumber). See #38763 anddiff --git a/agent/codex_runtime.py b/agent/codex_runtime.pyindex 40e6d050107..a1118759da3 100644--- a/agent/codex_runtime.py+++ b/agent/codex_runtime.py@@ -691,6 +691,20 @@ def run_codex_app_server_turn(Called from run_conversation() when agent.api_mode == "codex_app_server".Returns the same dict shape as the chat_completions path."""+ # Defense in depth for compression.checkpoint_required: agent init+ # already refuses this combination, but api_mode is a plain attribute a+ # future code path could mutate on a live agent. Fail closed before the+ # codex agent can compact its thread — once run_turn() executes, a+ # codex-owned compaction may already have happened with no pre-compress+ # checkpoint. Explicit-True check matches the compress_context() gate.+ if getattr(agent, "compression_checkpoint_required", False) is True:+ from agent.conversation_compression import _checkpoint_blocked++ raise _checkpoint_blocked(+ "codex_app_server owns the authoritative thread and compacts it "+ "without a truthful pre-compaction transcript boundary"+ )+from agent.transports.codex_app_server_session import (CodexAppServerSession,_ServerRequestRouting,diff --git a/agent/native_compaction.py b/agent/native_compaction.pyindex d718ad70b28..5dbd374825e 100644--- a/agent/native_compaction.py+++ b/agent/native_compaction.py@@ -116,6 +116,26 @@ def resolve_compact_threshold(return max(1_024, min(configured, upper))+_checkpoint_suppression_logged = False+++def _warn_native_compaction_suppressed_by_checkpoint_gate() -> None:+ """Log once per process that the checkpoint gate suppresses native compaction.++ The suppression itself is re-evaluated per request; only the log line is+ deduplicated so a long session does not repeat it on every API call.+ """+ global _checkpoint_suppression_logged+ if _checkpoint_suppression_logged:+ return+ _checkpoint_suppression_logged = True+ logger.warning(+ "compression.checkpoint_required is enabled: server-side native "+ "compaction (context_management) is disabled for this agent so the "+ "checkpoint-aware Hermes compressor stays authoritative."+ )++def native_compaction_context_management(agent: Any,*,@@ -137,6 +157,14 @@ def native_compaction_context_management(# included — mirrors the codex_app_server_auto contract.if not bool(getattr(agent, "compression_enabled", True)):return None+ # compression.checkpoint_required: server-side compaction is a lossy+ # boundary the provider owns — no pre-compress checkpoint can run before+ # the server replaces older context. Keep the checkpoint-aware Hermes+ # compressor authoritative instead of silently letting the server+ # compact. Explicit-True check matches the compress_context() gate.+ if getattr(agent, "compression_checkpoint_required", False) is True:+ _warn_native_compaction_suppressed_by_checkpoint_gate()+ return Noneif is_xai_responses or is_github_responses:return Noneif not is_native_compaction_model(getattr(agent, "model", None)):diff --git a/cli-config.yaml.example b/cli-config.yaml.exampleindex e03df674c06..69f322bbffd 100644--- a/cli-config.yaml.example+++ b/cli-config.yaml.example@@ -546,8 +546,12 @@ compression:# implements the pre-compress checkpoint contract (API v1) confirms its# durable checkpoint (default: false). With this on and no checkpoint, the# compaction attempt errors with BLOCKED_MISSING_PREREQUISITE and the- # uncompressed transcript is preserved for a later retry. Only enable it- # with a checkpoint-capable provider configured — see+ # uncompressed transcript is preserved for a later retry. The gate binds to+ # every compaction authority: server-side native compaction+ # (codex_responses_native) is suppressed while armed, and the+ # codex_app_server API mode is refused at agent init (the codex agent+ # compacts its own thread — no checkpoint can be guaranteed there). Only+ # enable it with a checkpoint-capable provider configured — see# website/docs/developer-guide/memory-provider-plugin.md# ("Pre-Compress Checkpoints").checkpoint_required: falsediff --git a/tests/agent/test_pre_compress_checkpoint_contract.py b/tests/agent/test_pre_compress_checkpoint_contract.pyindex 5852cc07a32..01037cfa4fa 100644--- a/tests/agent/test_pre_compress_checkpoint_contract.py+++ b/tests/agent/test_pre_compress_checkpoint_contract.py@@ -239,3 +239,87 @@ def test_compressed_summary_column_is_added_to_legacy_databases(tmp_path):model_history, _display = upgraded.get_resume_conversations("legacy")assert model_history[-1].get("_compressed_summary") is True+++def test_native_responses_compaction_is_suppressed_when_checkpoint_required():+ """checkpoint_required must keep ``context_management`` off the wire.++ Server-side native compaction is a lossy boundary the provider owns; no+ pre-compress checkpoint can run before it, so the gate suppresses the+ payload while ordinary checkpoint-aware Hermes compression stays+ available.+ """+ from types import SimpleNamespace++ from agent.native_compaction import native_compaction_context_management++ def agent(checkpoint_required):+ return SimpleNamespace(+ model="gpt-5.6",+ base_url="https://api.openai.com/v1",+ codex_responses_native_compaction=True,+ compression_enabled=True,+ compression_checkpoint_required=checkpoint_required,+ codex_responses_compact_threshold=0.8,+ context_compressor=None,+ )++ assert native_compaction_context_management(+ agent(False), is_codex_backend=True+ )+ assert (+ native_compaction_context_management(agent(True), is_codex_backend=True)+ is None+ )+++def test_codex_app_server_turn_fails_closed_before_codex_can_compact():+ """checkpoint_required + app-server must never reach ``run_turn()``.++ The codex agent compacts its own thread; once ``run_turn()`` executes, a+ codex-owned compaction may already have happened with no checkpoint. The+ turn entrypoint must raise first — the session is never even created.+ """+ from types import SimpleNamespace++ from agent.codex_runtime import run_codex_app_server_turn++ class _ExplodingSession:+ def run_turn(self, *args, **kwargs): # pragma: no cover - must not run+ raise AssertionError("run_turn() must not be reached")++ agent = SimpleNamespace(+ api_mode="codex_app_server",+ compression_checkpoint_required=True,+ _codex_session=_ExplodingSession(),+ )++ with pytest.raises(CompressionCheckpointUnavailable, match="codex_app_server"):+ run_codex_app_server_turn(+ agent,+ user_message="hello",+ original_user_message="hello",+ messages=[],+ effective_task_id="t1",+ )+++def test_agent_init_refuses_checkpoint_required_on_codex_app_server():+ """The incompatible configuration must fail closed at init time.++ In the default "native" auto-compaction mode Hermes never initiates the+ compaction, so the compress_context() guard alone cannot cover native+ turns — init_agent has to refuse before a turn exists.+ """+ from agent.agent_init import (+ _refuse_checkpoint_required_on_codex_app_server,+ )++ with pytest.raises(RuntimeError, match="BLOCKED_MISSING_PREREQUISITE"):+ _refuse_checkpoint_required_on_codex_app_server(True, "codex_app_server")++ # Every other combination stays permitted.+ _refuse_checkpoint_required_on_codex_app_server(True, "chat_completions")+ _refuse_checkpoint_required_on_codex_app_server(True, "codex_responses")+ _refuse_checkpoint_required_on_codex_app_server(False, "codex_app_server")+ _refuse_checkpoint_required_on_codex_app_server(False, None)diff --git a/website/docs/developer-guide/memory-provider-plugin.md b/website/docs/developer-guide/memory-provider-plugin.mdindex aea2844f8d1..93b2939247c 100644--- a/website/docs/developer-guide/memory-provider-plugin.md+++ b/website/docs/developer-guide/memory-provider-plugin.md@@ -165,6 +165,14 @@ uncompressed transcript is preserved, the compaction attempt errors with`BLOCKED_MISSING_PREREQUISITE`, and it can be retried once your storerecovers. With the gate off (default), nothing changes for existing providers.+The gate binds to every compaction authority, not just the Hermes+summarizer: server-side native compaction (`compression.codex_responses_native`)+is suppressed while the gate is armed so the checkpoint-aware Hermes+compressor stays authoritative, and the `codex_app_server` API mode is+refused at agent init — the codex agent compacts its own thread with no+truthful pre-compaction boundary, so a required checkpoint cannot be+guaranteed there.+What your provider receives in both modes is normalized direct evidence:user/assistant text rows only — tool results, system messages, the`tool_calls` payload of assistant messages (their prose is kept), and prior