Skip to content

The agent that grows with you

fix(memory): bind the checkpoint gate to post-turn micro-compaction too

Teknium1ee524f77d1c
diff --git a/agent/agent_init.py b/agent/agent_init.py
index f9a255e6dd4..8763af043bf 100644
--- a/agent/agent_init.py
+++ b/agent/agent_init.py
@@ -2799,6 +2799,19 @@ def init_agent(
agent.compression_in_place = compression_in_place
# Apply micro-compaction settings to the compressor (feature is opt-in)
_cc = getattr(agent, "context_compressor", None)
+ # compression.checkpoint_required: micro-compaction is a lossy rewrite
+ # authority too — it absorbs the oldest uncompacted exchanges into a
+ # rolling summary post-turn, with no pre-compress checkpoint hook in its
+ # path. Suppress it while the gate is armed so the checkpoint-aware
+ # batch compressor stays the only lossy authority (mirrors the
+ # server-side native-compaction suppression in native_compaction.py).
+ if compression_checkpoint_required and compression_micro_compact:
+ logger.warning(
+ "compression.checkpoint_required is enabled: post-turn "
+ "micro-compaction is disabled for this agent so every lossy "
+ "rewrite passes through the checkpoint-gated compressor."
+ )
+ compression_micro_compact = False
if _cc is not None and hasattr(_cc, "_micro_compact_enabled"):
_cc._micro_compact_enabled = compression_micro_compact
if _cc is not None and hasattr(_cc, "_micro_compact_every_n_turns"):
diff --git a/agent/turn_finalizer.py b/agent/turn_finalizer.py
index e3b20d876e4..8b1c8e64e2a 100644
--- a/agent/turn_finalizer.py
+++ b/agent/turn_finalizer.py
@@ -414,6 +414,14 @@ def finalize_turn(
and getattr(_compressor, '_micro_compact_enabled', False) is True
and callable(getattr(_compressor, '_micro_compact', None))
and final_response
+ # compression.checkpoint_required: agent init already
+ # forces _micro_compact_enabled off, but the compressor
+ # attribute is plain state a future path could flip on a
+ # live agent. Micro-compaction has no checkpoint hook in
+ # its path, so it must never run while the gate is armed.
+ and getattr(
+ agent, "compression_checkpoint_required", False
+ ) is not True
# Persistence-isolated agents (background review fork)
# must not micro-compact: the pass burns a real aux-LLM
# call on a throwaway replay transcript, and if the
diff --git a/cli-config.yaml.example b/cli-config.yaml.example
index 1da48f14154..1a8021ff984 100644
--- a/cli-config.yaml.example
+++ b/cli-config.yaml.example
@@ -548,7 +548,8 @@ compression:
# compaction attempt errors with BLOCKED_MISSING_PREREQUISITE and the
# 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_responses_native) is suppressed while armed, post-turn
+ # micro-compaction is forced off (no checkpoint hook in its path), 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
diff --git a/tests/agent/test_pre_compress_checkpoint_contract.py b/tests/agent/test_pre_compress_checkpoint_contract.py
index fe121c23417..86dbfe82a32 100644
--- a/tests/agent/test_pre_compress_checkpoint_contract.py
+++ b/tests/agent/test_pre_compress_checkpoint_contract.py
@@ -1,11 +1,12 @@
-"""Host-side contract tests for the opt-in pre-compress checkpoint API (v1).
+"""Host-side contract tests for the opt-in pre-compress checkpoint API (v2).
The contract has three parts:
-- providers opt in by advertising ``pre_compress_checkpoint_api_version``;
+- providers opt in by advertising ``pre_compress_checkpoint_api_version = 2``
+ (v1 is the implicit historical best-effort contract with raw messages);
- ``MemoryManager`` exposes capability probing and a ``require_checkpoint``
mode whose failure must propagate instead of being swallowed;
- the compression host normalizes messages to direct user/assistant evidence
- before handing them to providers.
+ before handing them to v2+ providers.
"""
import pytest
@@ -349,3 +350,89 @@ def test_agent_init_refuses_checkpoint_required_on_codex_app_server():
_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)
+
+
+def test_turn_finalizer_never_micro_compacts_while_checkpoint_gate_armed(
+ monkeypatch,
+):
+ """Micro-compaction is a lossy rewrite authority with no checkpoint hook.
+
+ Even if a live agent's compressor has ``_micro_compact_enabled`` flipped
+ on (agent init forces it off under the gate, but it is plain mutable
+ state), the post-turn finalizer must refuse to call ``_micro_compact()``
+ while ``compression_checkpoint_required`` is armed — otherwise assistant
+ evidence is absorbed into a rolling summary that the checkpoint filter
+ later excludes, and the evidence never reaches the durable provider.
+ """
+ from tests.agent.test_turn_finalizer_final_response_persistence import (
+ FakeAgent,
+ )
+ from agent.turn_finalizer import finalize_turn
+
+ monkeypatch.setattr("hermes_cli.plugins.invoke_hook", lambda *_a, **_kw: [])
+
+ class _RecordingCompressor:
+ _micro_compact_enabled = True
+
+ def __init__(self):
+ self.calls = 0
+
+ def _micro_compact(self, messages):
+ self.calls += 1
+ return list(messages)
+
+ def _run(checkpoint_required: bool):
+ agent = FakeAgent()
+ compressor = _RecordingCompressor()
+ agent.context_compressor = compressor
+ agent.compression_checkpoint_required = checkpoint_required
+ finalize_turn(
+ agent,
+ final_response="Done.",
+ api_call_count=1,
+ interrupted=False,
+ failed=False,
+ messages=[
+ {"role": "user", "content": "do it"},
+ {"role": "assistant", "content": "Done."},
+ ],
+ conversation_history=[],
+ effective_task_id="task",
+ turn_id="turn",
+ user_message="do it",
+ original_user_message="do it",
+ _should_review_memory=False,
+ _turn_exit_reason="completed",
+ )
+ return compressor.calls
+
+ # Gate armed: micro-compaction never runs.
+ assert _run(checkpoint_required=True) == 0
+
+ # Gate off: micro-compaction remains reachable — sabotage control proving
+ # this harness genuinely exercises the call site (the finalizer swallows
+ # compressor exceptions, so a call counter is the observable signal).
+ assert _run(checkpoint_required=False) == 1
+
+
+def test_agent_init_suppresses_micro_compaction_under_checkpoint_gate():
+ """checkpoint_required forces micro-compaction off at init.
+
+ Both keys can be enabled together in config; the gate must win so every
+ lossy rewrite passes through the checkpoint-aware batch compressor.
+ """
+ import inspect
+
+ from agent import agent_init
+
+ source = inspect.getsource(agent_init)
+ # The suppression must happen before the compressor attribute assignment.
+ suppress_idx = source.find(
+ "if compression_checkpoint_required and compression_micro_compact:"
+ )
+ assign_idx = source.find("_cc._micro_compact_enabled = compression_micro_compact")
+ assert suppress_idx != -1, (
+ "init_agent must suppress micro-compaction when checkpoint_required"
+ )
+ assert assign_idx != -1
+ assert suppress_idx < assign_idx
diff --git a/website/docs/developer-guide/memory-provider-plugin.md b/website/docs/developer-guide/memory-provider-plugin.md
index d5fd99d15d7..26a54b7fcb8 100644
--- a/website/docs/developer-guide/memory-provider-plugin.md
+++ b/website/docs/developer-guide/memory-provider-plugin.md
@@ -168,11 +168,13 @@ recovers. 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.
+is suppressed while the gate is armed, post-turn micro-compaction
+(`compression.micro_compact`) is forced off at agent init (it absorbs old
+exchanges into a rolling summary with no checkpoint hook in its path), 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. The checkpoint-aware Hermes
+compressor stays the only lossy authority.
What your provider receives depends on its declared API version. Version 1
providers (the implicit default — every pre-existing provider) keep the