Hermes Agent context engine: the main model writes the compaction handoff on its cached prompt prefix
Prevent stale compaction workers from replacing current status
Elevatormusic080f6de42b45
diff --git a/README.md b/README.mdindex 74c2d5a..d15dec2 100644--- a/README.md+++ b/README.md@@ -163,7 +163,7 @@ The warm request uses the key that Hermes gives to the engine. The fallback requ## Check the result-Hermes writes one metadata line for each compaction attempt to `logs/agent.log` in the Hermes home folder:+Hermes writes compaction metadata to `logs/agent.log` in the Hermes home folder:```textwarm_compaction: path=warm reason=accepted elapsed_s=10.656 prompt_tokens=108021 cached_tokens=107968@@ -173,7 +173,7 @@ warm_compaction: path=warm reason=accepted elapsed_s=10.656 prompt_tokens=108021- `reason` is `accepted` or the refusal code of the warm request: `disabled`, `no_capture`, `api_mode_unsupported`, `route_changed`, `settings_unsupported`, `source_transform_unsupported`, `history_changed`, `capacity`, `cancelled`, `middleware_unavailable`, `middleware_refused`, `middleware_rewrite`, `middleware_repeated`, `middleware_after_capture`, `middleware_order_unknown`, `headers_unknown`, `tls_unknown`, `tls_unverified`, `middleware_changed_reply`, `provider_error`, `timeout`, `incomplete_response`, or `gate:<reason>`. The warm request goes through the Hermes `llm_request` and `llm_execution` middleware, as a main request does.- `cached_tokens` is `None` when the server does not report it. Then the cache reuse is unknown.-The engine status (`get_status()`) has the same values for the latest attempt in `warm_last`. The log never contains message text, request bodies, or keys.+The engine status (`get_status()`) has the same values in `warm_last`. After a new attempt starts, an older worker cannot replace this status or write a final metadata line. The log never contains message text, request bodies, or keys.Manual `/compress` can report `No changes from compression` when all messages fit in the recent-history tail. Then the plugin has no older history to summarize. The request estimate also includes system instructions and tool definitions, which the plugin does not compress. Use `/context` to see the token counts for each part. This unchanged result does not count as a warm-path failure.diff --git a/evidence/plugin-integration.json b/evidence/plugin-integration.jsonindex 34b2232..9b53d89 100644--- a/evidence/plugin-integration.json+++ b/evidence/plugin-integration.json@@ -1,6 +1,6 @@{"check": "standalone warm_compaction plugin on unpatched Hermes Agent",- "started_utc": "2026-10-07T03:43:35Z",+ "started_utc": "2026-10-07T03:59:22Z","hermes": {"commit": "45871e100feceb89769536c88e5e6e265226a409","clean": true@@ -10,7 +10,7 @@"plugin_files_sha256": {"warm_compaction/__init__.py": "760484ace80b9da77d7397828f69178905c9ac95df7b8114956bde727bd98aef","warm_compaction/capture.py": "b7ba65676334da63dacc5cebdd2399f46f85e35545be22fccae9443cebcf7ce0",- "warm_compaction/engine.py": "56c44d835f5149a9d39ee8235f5cbc8fc778e0b3e0791198103af5edae5c6646",+ "warm_compaction/engine.py": "33910bde19aa7b9a63778a82a9a1a4ddf863dc4c46835fd9561fbcc216ea7cbb","warm_compaction/fallback.py": "dc2b7fbfdee292601e52549d23d9b7224d40da1d8c06f553f3d0b8e10a23534d","warm_compaction/handoff.py": "fcab51def8fbcc2b1928957930c9a5c58a4ad202c949fd9f870a77f3b30276ae","warm_compaction/layout.py": "b49314c5236268247c337d9fae939135c97b47384300dcdfd00b0da0c040db93",@@ -79,11 +79,11 @@"threshold_tokens": 100000,"context_length": 200000},- "compress_seconds": 0.143,+ "compress_seconds": 0.142,"warm_last": {"path": "warm","reason": "accepted",- "elapsed_s": 0.042,+ "elapsed_s": 0.041,"prompt_tokens": 33855,"cached_tokens": 16927},@@ -192,7 +192,7 @@"install","run"],- "process_seconds": 73.363+ "process_seconds": 70.006},{"scenario": "manual_fallback",@@ -254,11 +254,11 @@"threshold_tokens": 100000,"context_length": 200000},- "compress_seconds": 0.301,+ "compress_seconds": 0.298,"warm_last": {"path": "fallback","reason": "provider_error",- "elapsed_s": 0.14,+ "elapsed_s": 0.156,"prompt_tokens": null,"cached_tokens": null},@@ -266,7 +266,7 @@"final_session_id": "wc-manual_fallback","continuation_keeps_custom_system_text": false,"fence_denied": {- "child_process_blocked": 6,+ "child_process_blocked": 5,"network_host_blocked": 1},"fence_blocked_programs": [@@ -367,7 +367,7 @@"install","run"],- "process_seconds": 40.173+ "process_seconds": 41.015},{"scenario": "manual_fixed",@@ -428,7 +428,7 @@"threshold_tokens": 100000,"context_length": 200000},- "compress_seconds": 0.302,+ "compress_seconds": 0.299,"warm_last": {"path": "fixed","reason": "provider_error",@@ -541,7 +541,7 @@"install","run"],- "process_seconds": 41.571+ "process_seconds": 39.251},{"scenario": "auto_tool_loop",@@ -629,14 +629,14 @@"warm_last_1": {"path": "warm","reason": "accepted",- "elapsed_s": 0.032,+ "elapsed_s": 0.031,"prompt_tokens": 34449,"cached_tokens": 17224},"warm_last_2": {"path": "warm","reason": "accepted",- "elapsed_s": 0.016,+ "elapsed_s": 0.031,"prompt_tokens": 18744,"cached_tokens": 9372},@@ -744,7 +744,7 @@"install","run"],- "process_seconds": 43.307+ "process_seconds": 42.949},{"scenario": "auto_warm_failures",@@ -1036,7 +1036,7 @@"install","run"],- "process_seconds": 45.512+ "process_seconds": 44.747},{"scenario": "rollback",@@ -1089,11 +1089,11 @@"threshold_tokens": 100000,"context_length": 200000},- "compress_seconds": 0.194,+ "compress_seconds": 0.144,"warm_last": {"path": "warm","reason": "accepted",- "elapsed_s": 0.015,+ "elapsed_s": 0.016,"prompt_tokens": 33845,"cached_tokens": 16922},@@ -1272,10 +1272,10 @@"run","rollback"],- "process_seconds": 52.328+ "process_seconds": 48.135}],- "finished_utc": "2026-10-07T03:48:32Z",+ "finished_utc": "2026-10-07T04:04:08Z","passed": true,"run_folder": "outside repository"}diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.pyindex 6c03fee..0b2b037 100644--- a/tests/test_wc_engine.py+++ b/tests/test_wc_engine.py@@ -4,6 +4,7 @@ import jsonimport threadingimport unittestfrom types import SimpleNamespace+from unittest.mock import patchimport wc_hermes_stubfrom warm_compaction.fallback import END_LINE@@ -864,21 +865,34 @@ class EngineTest(unittest.TestCase):self.commit(engine)self.assertEqual(engine._warm_failures, 1)- def test_a_late_worker_cannot_change_the_current_candidate_or_counter(self):+ def test_a_late_worker_cannot_change_the_current_result_or_counter(self):+ for wait_at in ("fallback", "build"):+ for committed in (False, True):+ with self.subTest(wait_at=wait_at, committed=committed):+ self.check_late_worker_publication(wait_at, committed)++ def check_late_worker_publication(self, wait_at, committed):+ from warm_compaction import layout+entered, release = threading.Event(), threading.Event()results, errors = [], []engine = self.make()history = [*old_turns(), assistant("done")]- complete = self.llm.complete+ complete, build = self.llm.complete, layout.build- def delayed_complete(*args, **kwargs):- if threading.current_thread().name == "old-compaction":+ def wait_for_replacement(phase):+ if phase == wait_at and threading.current_thread().name == "old-compaction":entered.set()if not release.wait(5):raise TimeoutError("test worker was not released")++ def delayed_complete(*args, **kwargs):+ wait_for_replacement("fallback")return complete(*args, **kwargs)- self.llm.complete = delayed_complete+ def delayed_build(*args, **kwargs):+ wait_for_replacement("build")+ return build(*args, **kwargs)def old_attempt():try:@@ -886,23 +900,40 @@ class EngineTest(unittest.TestCase):except BaseException as error:errors.append(error)- worker = threading.Thread(target=old_attempt, name="old-compaction", daemon=True)- worker.start()- try:- self.assertTrue(entered.wait(5), "old worker did not enter the fallback")- engine.compress(history)+ with (patch.object(self.llm, "complete", side_effect=delayed_complete),+ patch.object(layout, "build", side_effect=delayed_build),+ self.assertLogs("warm_compaction.engine", level="INFO") as logs):+ worker = threading.Thread(target=old_attempt, name="old-compaction", daemon=True)+ worker.start()+ try:+ self.assertTrue(entered.wait(5), "old worker did not reach the wait point")+ engine.compress(history)+ if committed:+ self.commit(engine)+ current = dict(engine.warm_last)+ current_status = engine.get_status()+ current_pending = engine._pending_warm_result+ metadata = [line for line in logs.output if "warm_compaction: path=" in line]+ self.assertEqual(len(metadata), 1)+ finally:+ release.set()+ worker.join(5)+ self.assertFalse(worker.is_alive())+ self.assertEqual(errors, [])+ self.assertEqual(len(results), 1)+ self.assertTrue(results[0] is history, "old worker returned a candidate")self.assertEqual(engine.compression_count, 1)- finally:- release.set()- worker.join(5)- self.assertFalse(worker.is_alive())- self.assertEqual(errors, [])- self.assertEqual(len(results), 1)- self.assertTrue(results[0] is history, "old worker returned a candidate")- self.assertEqual(engine.compression_count, 1)- self.assertEqual(engine._warm_failures, 0)- self.commit(engine)- self.assertEqual(engine._warm_failures, 1)+ self.assertEqual(engine._pending_warm_result, current_pending)+ self.assertEqual(engine._warm_failures, int(committed))+ with self.subTest(publication="warm_last"):+ self.assertEqual(engine.warm_last, current)+ with self.subTest(publication="status"):+ self.assertEqual(engine.get_status(), current_status)+ with self.subTest(publication="metadata_log"):+ self.assertEqual([line for line in logs.output if "warm_compaction: path=" in line],+ metadata)+ self.commit(engine)+ self.assertEqual(engine._warm_failures, 1)def test_an_unusable_capture_does_not_set_the_reasoning_rule(self):# The capture is of this route, but its rows are not the stored rows: it does not show what the nextdiff --git a/warm_compaction/engine.py b/warm_compaction/engine.pyindex 9fd5aa6..13c0f40 100644--- a/warm_compaction/engine.py+++ b/warm_compaction/engine.py@@ -161,7 +161,7 @@ class WarmCompactionEngine(ContextEngine):self.compression_count = 0self.warm_last: dict[str, Any] | None = Noneself._wc_session_id = ""- self._wc_result_lock = threading.Lock()+ self._wc_result_lock = threading.RLock()self._wc_attempt_serial = 0self._pending_warm_result: tuple[str, int, dict[str, Any]] | None = Noneself._wc_route: tuple = (None, None, None)@@ -273,9 +273,10 @@ class WarmCompactionEngine(ContextEngine):self.awaiting_real_usage_after_compression = Falsedef get_status(self) -> dict[str, Any]:- status = super().get_status()- status["warm_last"] = dict(self.warm_last) if self.warm_last else None- return status+ with self._wc_result_lock:+ status = super().get_status()+ status["warm_last"] = dict(self.warm_last) if self.warm_last else None+ return statusdef compress(self, messages: list, current_tokens: int | None = None, focus_topic: str | None = None,force: bool = False, memory_context: str = "") -> list:@@ -334,13 +335,13 @@ class WarmCompactionEngine(ContextEngine):record["path"] = "fallback"if self._cancelled():record.update(path="cancelled", reason=record["reason"] or "cancelled")- self._finish(record, started)+ self._finish(record, started, attempt[-1])return messagesif self._attempt() != attempt:# A model or session switch during the attempt (while a request was on the network, for example): the# summary is of the old route or session. Hermes keeps the history.record.update(path="cancelled", reason="route_changed")- self._finish(record, started)+ self._finish(record, started, attempt[-1])return messages# The fixed summary takes at most the reserve, and at most the room.fixed_budget = self._fixed_budget(overhead, reserve)@@ -417,10 +418,10 @@ class WarmCompactionEngine(ContextEngine):with self._wc_result_lock:if self._cancelled() or self._attempt() != attempt:record.update(path="cancelled", reason="cancelled" if self._cancelled() else "route_changed")- self._finish(record, started)+ self._finish(record, started, attempt[-1])return messagesself.compression_count += 1- self._finish(record, started)+ self._finish(record, started, attempt[-1])# The host can still reject this candidate. Only its successful boundary updates the failure streak.self._pending_warm_result = (attempt[3], self.compression_count, dict(record))return new@@ -670,12 +671,17 @@ class WarmCompactionEngine(ContextEngine):except Exception:return False- def _finish(self, record: dict[str, Any], started: float) -> None:- record["elapsed_s"] = round(self._clock() - started, 3)- self.warm_last = record- logger.info("warm_compaction: path=%s reason=%s elapsed_s=%s prompt_tokens=%s cached_tokens=%s",- record["path"], record["reason"], record["elapsed_s"], record["prompt_tokens"],- record["cached_tokens"])+ def _finish(self, record: dict[str, Any], started: float, attempt_serial: int) -> None:+ """Publish metadata only while this local attempt owns the engine."""+ # The final candidate already holds this reentrant lock. All other exits use the same publication guard.+ with self._wc_result_lock:+ if attempt_serial != self._wc_attempt_serial:+ return+ record["elapsed_s"] = round(self._clock() - started, 3)+ self.warm_last = record+ logger.info("warm_compaction: path=%s reason=%s elapsed_s=%s prompt_tokens=%s cached_tokens=%s",+ record["path"], record["reason"], record["elapsed_s"], record["prompt_tokens"],+ record["cached_tokens"])def _clear_warm_failures(self) -> None:self._warm_failures, self._warm_fixed = 0, 0