Skip to content

Hermes Agent context engine: the main model writes the compaction handoff on its cached prompt prefix

Compare the text and tool arguments of each captured row before the warm request

Elevatormusicf99eeb920e84
diff --git a/tests/test_wc_warm.py b/tests/test_wc_warm.py
index 424df81..bace0ad 100644
--- a/tests/test_wc_warm.py
+++ b/tests/test_wc_warm.py
@@ -131,6 +131,28 @@ class BuildRequestTest(unittest.TestCase):
self.build(capture)
self.assertEqual(caught.exception.code, "source_transform_unsupported")
+ def _tool_round(self):
+ rows = [user("u1"), assistant("", [("c0", "read", "{\"path\":\"a\"}")]), tool("c0", "r0"), user("u2")]
+ self.rows, self.messages = rows, [*rows, self.reply, tool("c1", "r1"), user("u3")]
+ return capture_for(self.rows, self.reply)
+
+ def test_refuses_a_rewritten_row_with_the_same_shape(self):
+ capture = self._tool_round()
+ capture["body"]["messages"][-1]["content"] = "A different request."
+ rewritten = self._tool_round()
+ rewritten["body"]["messages"][-3]["tool_calls"][0]["function"]["arguments"] = "{\"path\":\"other\"}"
+ for capture in (capture, rewritten):
+ with self.subTest(), self.assertRaises(WarmRefusal) as caught:
+ self.build(capture)
+ self.assertEqual(caught.exception.code, "source_transform_unsupported")
+
+ def test_accepts_request_time_context_and_reformatted_arguments(self):
+ capture = self._tool_round()
+ sent = capture["body"]["messages"]
+ sent[-1]["content"] += "\n\n[recalled context: the user wants short answers]"
+ sent[-3]["tool_calls"][0]["function"]["arguments"] = "{ \"path\": \"a\" }"
+ self.assertEqual(self.build(capture)["messages"][: len(sent)], sent)
+
def test_refuses_when_the_window_is_too_small(self):
with self.assertRaises(WarmRefusal) as caught:
self.build(capture_for(self.rows, self.reply), context_length=4_096)
diff --git a/warm_compaction/warm.py b/warm_compaction/warm.py
index 57d0d52..c800523 100644
--- a/warm_compaction/warm.py
+++ b/warm_compaction/warm.py
@@ -9,7 +9,7 @@ import urllib.error
import urllib.request
from typing import Any, Callable
-from .rows import attr, compact_json, estimate_tokens, reply_text, row_digest, tool_calls_of
+from .rows import attr, compact_json, estimate_tokens, plain_text, reply_text, row_digest, tool_calls_of
DEFAULT_RESERVE = 4096
TIMEOUT_S = 120.0
@@ -78,13 +78,37 @@ def _shape(row: Any) -> tuple:
return attr(row, "role"), attr(row, "tool_call_id") or None, calls
+def _words(content: Any) -> str:
+ return " ".join(plain_text(content).split())
+
+
+def _arguments(value: Any) -> Any:
+ """Return the JSON value of tool-call arguments, so that a change of spacing or key order is not a change."""
+ if isinstance(value, str):
+ try:
+ return json.loads(value)
+ except ValueError:
+ return " ".join(value.split())
+ return value
+
+
+def _same_row(wire: Any, row: Any) -> bool:
+ """True when the sent row carries the stored row: the same shape, the stored text inside the sent text
+ (Hermes can add request-time context to a row), and the same tool-call arguments."""
+ if _shape(wire) != _shape(row) or _words(attr(row, "content")) not in _words(attr(wire, "content")):
+ return False
+ return [_arguments(arguments) for _id, _name, arguments in tool_calls_of(wire)] == [
+ _arguments(arguments) for _id, _name, arguments in tool_calls_of(row)]
+
+
def check_source(body: dict[str, Any], history_rows: list) -> None:
- """Refuse a request whose messages are not system rows followed by the stored rows."""
+ """Refuse a request whose messages are not system rows followed by the stored rows. A row that a hook or
+ a middleware rewrote would make the handoff summarize text that is not in the history it replaces."""
sent = body["messages"]
offset = len(sent) - len(history_rows)
if offset < 0 or any(attr(row, "role") not in SYSTEM_ROLES for row in sent[:offset]):
raise WarmRefusal("source_transform_unsupported")
- if any(_shape(wire) != _shape(row) for wire, row in zip(sent[offset:], history_rows)):
+ if not all(_same_row(wire, row) for wire, row in zip(sent[offset:], history_rows)):
raise WarmRefusal("source_transform_unsupported")