Hermes Agent context engine: the main model writes the compaction handoff on its cached prompt prefix
fix: keep native histories within the compaction budget
Elevatormusicb998fd313334
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6178740..cf7d0b5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -6,6 +6,7 @@ All notable changes are listed here. The format follows [Keep a Changelog](https### Fixed+- Keep the original history when an indivisible native replay block exceeds the compaction budget. Check the threshold, reply reserve, and unknown request overhead before accepting the new history.- Match Copilot hostnames at a domain boundary. A lookalike hostname no longer gets Copilot replay handling.- Accept trailing whitespace removal by Hermes from complete text strings, including `api_content`, without a false `source_transform_unsupported` fallback. Keep the captured request prefix unchanged.- Keep safe request refusal codes in status and logs, instead of the generic `settings_unsupported`.diff --git a/evidence/native-conversation-integration.json b/evidence/native-conversation-integration.jsonindex 48e8752..0ef3fdd 100644--- a/evidence/native-conversation-integration.json+++ b/evidence/native-conversation-integration.json@@ -68,7 +68,6 @@"cached_tokens": 120},"fence_callers": [- "child_process_blocked from hermes_cli/source_check.py:_git_run < hermes_cli/source_check.py:_git_stdout < hermes_cli/source_check.py:_read_checkout","child_process_blocked from tools/env_probe.py:_run < tools/env_probe.py:_py_out < tools/env_probe.py:_pip_python_version","child_process_blocked from tools/env_probe.py:_run < tools/env_probe.py:_py_out < tools/env_probe.py:_python_version_of","network_host_blocked from hermes_cli/release_channels.py:read < hermes_cli/release_channels.py:read_bytes < hermes_cli/release_channels.py:read_record"@@ -81,8 +80,8 @@"history_rows": 4,"compression_count": 1,"fence_denied": {- "child_process_blocked": 5,- "network_host_blocked": 1+ "network_host_blocked": 1,+ "child_process_blocked": 3},"blocked_host_network_attempts": 1,"exit_ok": true@@ -147,7 +146,8 @@"fence_callers": ["child_process_blocked from hermes_cli/source_check.py:_git_run < hermes_cli/source_check.py:_git_stdout < hermes_cli/source_check.py:_read_checkout","child_process_blocked from tools/env_probe.py:_run < tools/env_probe.py:_py_out < tools/env_probe.py:_pip_python_version",- "child_process_blocked from tools/env_probe.py:_run < tools/env_probe.py:_py_out < tools/env_probe.py:_python_version_of"+ "child_process_blocked from tools/env_probe.py:_run < tools/env_probe.py:_py_out < tools/env_probe.py:_python_version_of",+ "network_host_blocked from hermes_cli/release_channels.py:read < hermes_cli/release_channels.py:read_bytes < hermes_cli/release_channels.py:read_record"],"request_counts": {"main": 1,@@ -157,9 +157,10 @@"history_rows": 6,"compression_count": 1,"fence_denied": {- "child_process_blocked": 6+ "child_process_blocked": 5,+ "network_host_blocked": 1},- "blocked_host_network_attempts": 0,+ "blocked_host_network_attempts": 1,"exit_ok": true}],@@ -234,7 +235,7 @@"history_rows": 6,"compression_count": 1,"fence_denied": {- "child_process_blocked": 5,+ "child_process_blocked": 6,"network_host_blocked": 1},"blocked_host_network_attempts": 1,@@ -312,7 +313,7 @@"history_rows": 8,"compression_count": 1,"fence_denied": {- "child_process_blocked": 5+ "child_process_blocked": 6},"blocked_host_network_attempts": 0,"exit_ok": true@@ -390,7 +391,7 @@"history_rows": 4,"compression_count": 1,"fence_denied": {- "child_process_blocked": 5,+ "child_process_blocked": 6,"network_host_blocked": 1},"blocked_host_network_attempts": 1,@@ -542,7 +543,7 @@"history_rows": 6,"compression_count": 1,"fence_denied": {- "child_process_blocked": 5,+ "child_process_blocked": 6,"network_host_blocked": 1},"blocked_host_network_attempts": 1,@@ -619,7 +620,7 @@"history_rows": 8,"compression_count": 1,"fence_denied": {- "child_process_blocked": 5+ "child_process_blocked": 6},"blocked_host_network_attempts": 0,"exit_ok": true@@ -633,7 +634,7 @@"warm_compaction/__init__.py": "760484ace80b9da77d7397828f69178905c9ac95df7b8114956bde727bd98aef","warm_compaction/anthropic.py": "7bd4cb0e075c4f279f62d3b6f0c3dcf7405308c7360f9136369476a59dca4fd3","warm_compaction/capture.py": "d2ce12d20fca5649d94b17d376a7bd9e21ba7f6a828b94cb62d030ca55adf791",- "warm_compaction/engine.py": "97a5ca7b779d4cfbaf4f4b2c9cd9d8e4616985d19578551d9c76679011204a12",+ "warm_compaction/engine.py": "368b4ec95f31d0f6fc868f6184730d513e02f61764be60a1fabdb00cdececfd6","warm_compaction/fallback.py": "9b2d0529767ff157bbe0d1cbf096ca6ac35217118f81eee769adf41d50189a9a","warm_compaction/handoff.py": "fcab51def8fbcc2b1928957930c9a5c58a4ad202c949fd9f870a77f3b30276ae","warm_compaction/layout.py": "3722ca05a4b84c9ed9f5cef5c7f7a175201efaba4208f3819dabcc556a2cfd57",diff --git a/evidence/provider-api-integration.json b/evidence/provider-api-integration.jsonindex fd109d0..898a51e 100644--- a/evidence/provider-api-integration.json+++ b/evidence/provider-api-integration.json@@ -446,7 +446,7 @@"warm_compaction/__init__.py": "760484ace80b9da77d7397828f69178905c9ac95df7b8114956bde727bd98aef","warm_compaction/anthropic.py": "7bd4cb0e075c4f279f62d3b6f0c3dcf7405308c7360f9136369476a59dca4fd3","warm_compaction/capture.py": "d2ce12d20fca5649d94b17d376a7bd9e21ba7f6a828b94cb62d030ca55adf791",- "warm_compaction/engine.py": "97a5ca7b779d4cfbaf4f4b2c9cd9d8e4616985d19578551d9c76679011204a12",+ "warm_compaction/engine.py": "368b4ec95f31d0f6fc868f6184730d513e02f61764be60a1fabdb00cdececfd6","warm_compaction/fallback.py": "9b2d0529767ff157bbe0d1cbf096ca6ac35217118f81eee769adf41d50189a9a","warm_compaction/handoff.py": "fcab51def8fbcc2b1928957930c9a5c58a4ad202c949fd9f870a77f3b30276ae","warm_compaction/layout.py": "3722ca05a4b84c9ed9f5cef5c7f7a175201efaba4208f3819dabcc556a2cfd57",diff --git a/tests/test_wc_protocol.py b/tests/test_wc_protocol.pyindex 9df17c6..6bf75c1 100644--- a/tests/test_wc_protocol.py+++ b/tests/test_wc_protocol.py@@ -3,6 +3,7 @@import copyimport jsonimport unittest+from unittest.mock import patchfrom warm_compaction.capture import UnsupportedRequest, final_body, request_headersfrom warm_compaction.protocol import MAX_DONE_ITEMS, envelope@@ -227,6 +228,82 @@ class NativeSendTest(unittest.TestCase):class NativeTailTest(unittest.TestCase):+ def native_history(self, mode, replay_chars):+ """Make a fresh engine and synthetic replay history with no captured request."""+ from test_wc_engine import EngineTest, old_turns+ fixture = EngineTest("test_threshold_comes_from_the_setting")+ fixture.setUp()+ self.addCleanup(fixture.doCleanups)+ engine = fixture.make(warm=False)+ engine.update_model(model="fake", context_length=16384, base_url="http://127.0.0.1:9/v1",+ api_key="fake", provider="custom", api_mode=mode)+ replay = ({"codex_reasoning_items": [{"type": "reasoning", "encrypted_content": "x" * replay_chars}]}+ if mode == "codex_responses" else {"anthropic_content_blocks": [+ {"type": "redacted_thinking", "data": "x" * replay_chars},+ {"type": "text", "text": "synthetic"}]})+ rows = [*old_turns(3), {"role": "user", "content": "synthetic last request"},+ {"role": "assistant", "content": "synthetic", **replay}]+ return fixture, engine, rows++ def test_native_replay_above_threshold_without_capture_keeps_history(self):+ for mode in ("codex_responses", "anthropic_messages"):+ with self.subTest(mode=mode):+ _fixture, engine, rows = self.native_history(mode, 36000)+ self.assertIsNone(engine._store.latest(engine._wc_session_id))+ self.assertGreater(sent_tokens(rows[-1], engine._policy(rows)), engine.threshold_tokens)+ self.assertLess(sent_tokens(rows[-1], engine._policy(rows)) + 4096, engine.context_length)+ self.assertTrue(engine.compress(rows) is rows, "Oversized native replay must keep the original history")+ self.assertEqual(engine.compression_count, 0)+ self.assertEqual((engine.warm_last["path"], engine.warm_last["reason"]), ("unchanged", "capacity"))++ def test_native_replay_above_unknown_overhead_budget_keeps_history(self):+ for mode in ("codex_responses", "anthropic_messages"):+ with self.subTest(mode=mode):+ _fixture, engine, rows = self.native_history(mode, 20000)+ replay_tokens = sent_tokens(rows[-1], engine._policy(rows))+ self.assertGreater(replay_tokens, engine.threshold_tokens // 2)+ self.assertLess(replay_tokens, engine.threshold_tokens)+ self.assertTrue(engine.compress(rows) is rows, "Oversized native replay must keep the original history")+ self.assertEqual(engine.compression_count, 0)+ self.assertEqual((engine.warm_last["path"], engine.warm_last["reason"]), ("unchanged", "capacity"))++ def test_native_replay_with_known_overhead_must_fit_below_threshold(self):+ from warm_compaction import anthropic, responses+ for mode in ("codex_responses", "anthropic_messages"):+ with self.subTest(mode=mode):+ fixture, engine, rows = self.native_history(mode, 30000)+ from warm_compaction.engine import request_overhead+ system = "synthetic system " + "s" * 4000+ body = ({"model": "fake", "instructions": system, "store": False,+ "input": responses.wire_rows(rows[:-1]), "max_output_tokens": 4096}+ if mode == "codex_responses" else {"model": "fake", "system": system,+ "messages": anthropic._rows(rows[:-1]), "max_tokens": 4096})+ capture = {"route": engine._wc_route, "digests": [row_digest(row) for row in rows[:-1]], "body": body,+ "reply": {"content": rows[-1]["content"], "tool_calls": []}}+ with patch.object(fixture.store, "latest", return_value=capture):+ self.assertIs(engine._budget_capture(capture, rows), capture)+ total = sent_tokens(rows[-1], engine._policy(rows)) + request_overhead(capture, rows)+ self.assertGreater(total, engine.threshold_tokens)+ self.assertLess(total + 4096, engine.context_length)+ self.assertTrue(engine.compress(rows) is rows,+ "Oversized native replay must keep the original history")+ self.assertEqual(engine.compression_count, 0)+ self.assertEqual((engine.warm_last["path"], engine.warm_last["reason"]), ("unchanged", "capacity"))++ def test_small_native_replay_without_capture_can_compact(self):+ from warm_compaction.rows import estimate_tokens, sent_rows+ for mode in ("codex_responses", "anthropic_messages"):+ with self.subTest(mode=mode):+ _fixture, engine, rows = self.native_history(mode, 5000)+ before = copy.deepcopy(rows)+ result = engine.compress(rows)+ self.assertIsNot(result, rows)+ self.assertEqual(rows, before)+ self.assertEqual(result[-1], rows[-1])+ self.assertLess(estimate_tokens(sent_rows(result, engine._policy(rows))), engine.threshold_tokens // 2)+ self.assertEqual(engine.compression_count, 1)+ self.assertEqual(engine.warm_last["path"], "fallback")+def test_transformed_native_settings_cannot_supply_a_budget_capture(self):from test_wc_engine import EngineTestfrom warm_compaction.responses import wire_rowsdiff --git a/warm_compaction/engine.py b/warm_compaction/engine.pyindex a398763..5224be8 100644--- a/warm_compaction/engine.py+++ b/warm_compaction/engine.py@@ -438,8 +438,15 @@ class WarmCompactionEngine(ContextEngine):end_marker=hermes_value("agent.context_compressor", "_SUMMARY_END_MARKER", HERMES_END_MARKER),marker=hermes_value("agent.context_compressor", "_DB_PERSISTED_MARKER", HERMES_DB_MARKER),copy_tokens=copy_tokens, tail_tokens=tail_tokens, policy=policy)- native_overflow = bool(policy.native_mode and int(self.context_length or 0) > 0 and (- estimate_tokens(sent_rows(new, policy)) + (overhead or 0) + reserve > int(self.context_length)))+ native_overflow = False+ if policy.native_mode:+ # An indivisible replay can exceed the tail cap. Check the complete candidate against the same+ # free budget, including the conservative allowance for an unknown request overhead.+ free = self._room([], "", overhead or 0, reserve)+ if free is not None:+ if overhead is None:+ free //= 2+ native_overflow = estimate_tokens(sent_rows(new, policy)) > free# A stale worker must not change the counter or replace a newer candidate. No model call holds this lock.with self._wc_result_lock:if self._cancelled() or self._attempt() != attempt: