Hermes Agent context engine: the main model writes the compaction handoff on its cached prompt prefix
fix: accept trailing whitespace removal in warm source checks
Elevatormusica8091de57a28
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 33f53b8..f556b52 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+- 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`.- Keep the captured `x-opencode-session` header on warm requests, including the header that Hermes adds for OpenCode routes. Refuse unknown headers and header rewrites; send the session value only as an HTTP header.- Report `missing_end_marker` or `output_token_limit` when a fallback reply fails the completion check. Incomplete replies still use the fixed summary.diff --git a/README.md b/README.mdindex 9b68dfb..a2238ac 100644--- a/README.md+++ b/README.md@@ -246,6 +246,7 @@ The plugin was faster than both other engines in 10 of 10 cases. hermes-lcm and- A model or session switch, or a host timeout, during the attempt stops it, also while the warm request is on the network: the history stays unchanged (`route_changed` or `cancelled`).- A provider or API key switch forgets the capture of the session: the next compaction uses the fallback summary (`no_capture`) until a main request on the new configuration completes. A key that Hermes gives as a function can change without a switch (a token that refreshes or rotates): the capture keeps a PBKDF2-HMAC-SHA-256 digest of the key that sent it, never the key, and the warm request is not sent with another key (reason `credential_changed`). The digest uses a random salt made when the plugin module loads. The salt and the captures stay in memory. A key change while a request is open keeps no capture.- A captured row with a field that Hermes does not send for the stored row (other than the prompt caching marker `cache_control`) refuses the warm path (`source_transform_unsupported`).+- The source check accepts exact text or removal of trailing whitespace from a complete text string, including a stored `api_content` string. Leading or inner whitespace changes, added whitespace, and changes to text inside a content list still refuse the warm path (`source_transform_unsupported`). The captured request prefix stays unchanged.- The handoff must start with `## Goal`: text before the first heading is refused (`gate:heading_preamble`).- The verbatim tail keeps the newest rows that fit in `tail_tokens`, and in the free room after the system rows and tool schemas of a usable capture, the reply reserve, and a summary: a large system prompt can make a short history compact. It stops at the first older row that does not fit, and that row goes into the summary. When the newest unit alone is larger than the tail or the free room, its largest sent payloads keep only their start and end: message text, tool call arguments (kept as a JSON object), and reasoning text. A large image or other media part is replaced by its attachment mark: a web URL or a file name stays (at most 200 characters), a data URL does not. If the cuts are not enough, the payloads are dropped, largest first; only the row structure stays. Signed `reasoning_details` stay as they are. The fallback summary gets the cut middles, and the fixed summary quotes them (at most 8,000 characters), also the cut middle of a long latest user message that goes in front of the tail. A fallback summary above its 4,096-token reserve (a dense CJK summary, for example) is not used: the fixed summary quotes the parts that the final tail cuts. Each quote makes the fixed summary larger and the tail smaller; after 10 rounds, the tail is cut at the size that the summary quotes, also when that is a little above the free room. The sizes come from the fields that Hermes sends: row metadata does not count; reasoning counts only on a route that needs it back (DeepSeek, Kimi, MiMo thinking; a capture of the route shows it, else a stored row with `reasoning_content`); `reasoning_details` counts only on a route that replays it, without the private native-assistant entries; the native-assistant entry type that the provider profile declares (`native_reasoning_details_type`) counts on every route of the provider, as Hermes replays it; a tool call counts only its id, type, name, and arguments, and a usable thought signature counts only for a Gemini or Gemma model. The fixed summary keeps bounded quotes of the earlier summary, the focus topic, the memory context, and the cut middles; together with the tool inventory (the most used names, and one count for the others) they stay in one budget (4,096 tokens, and at most the free room). When the fallback summary is used and the latest user request in front of the tail must be cut, the cut middle goes after the summary as a bounded quote, in what the summary budget has left: the fallback transcript had only the start and end of a long row. When no quote fits there, the fixed summary is used: it quotes the cut middle. A fallback summary that does not fit in the free room is not used: the fixed summary takes its place. A model, session, window, or threshold change at any time during the compaction discards its result (reason `route_changed`). On a Gemini or Gemma route, a tool call with a thought signature stays whole: Gemini needs it back as it was. A change of the provider or the key forgets the capture, also from an empty provider and key. A warm summary that does not fit in the free room (a dense reply under a low threshold, for example) is not used: the fallback summary runs (reason `summary_too_large`). A retry middleware cannot send the warm request a second time, also after a failed send. A warm reply body above 1 MiB is not read (reason `response_too_large`). The fallback request does not start after a model, provider, or session switch. The plugin API has no request on a fixed route (an override needs a trust setting), so a switch in the short time after that check, before the host resolves the auxiliary route, can still send the transcript to the new route. Reasoning is cut only when a usable capture of the route shows that the route sends it: other reasoning (of an earlier route after a model switch, for example) does not go to the fallback model or into the fixed summary. The latest user request in front of the tail gets the room that stays after the tail is cut. Hermes notifications, task lists, and recovery nudges are not user requests only when the whole text matches the Hermes template. The copied user messages get the room that stays after the tail is cut. Without a usable capture (after a restart, after a model change, or when a middleware changed the captured rows), the request overhead is unknown: the summary has no copied user messages, the summary, the prepended row, and the tail take at most half of the free room, the check before a request compacts when the history estimate reaches half of the window less the reply reserve, and the reply reserve is a quarter of the window (at most 65,536 and at least 4,096 tokens). The same reserve applies when the captured request had no positive `max_tokens` or `max_completion_tokens`: the provider default is not known. The host token count is not used for the overhead: it does not use the plugin tokenizer.- The warm request is not streamed and not retried. Its time limit is 120 seconds. The fallback request also has a 120-second limit.diff --git a/evidence/README.md b/evidence/README.mdindex 963e543..480d7af 100644--- a/evidence/README.md+++ b/evidence/README.md@@ -1,9 +1,10 @@# Evidence-These records come from the research repository of this plugin. They have metadata only: counts, codes, booleans, times, and hashes. They contain no message text, request body, or key. Paths such as `.work/...` and short commit IDs refer to that research repository.+These records cover research runs and issue checks. They have metadata only: counts, codes, booleans, times, and hashes. They contain no message text, request body, or key. Paths such as `.work/...` and short commit IDs in the research records refer to the research repository.| File | Content || --- | --- |+| [issue-12-whitespace.json](issue-12-whitespace.json) | Issue 12: refusal before the fix, three warm compactions after it, and seven integration scenarios on clean Hermes `e36a8180`; synthetic conversations and a loopback fake server || [plugin-integration.json](plugin-integration.json) | The integration check: install with the Hermes install command, manual and automatic compaction, fallback, tools, and rollback on unpatched Hermes `45871e10`, against a loopback fake server || [plugin-live.json](plugin-live.json) | Live runs, plugin against the built-in compressor: DGX and LM Studio, manual and automatic, ten synthetic cases of 100,000 or more tokens each || [lcm-bench.json](lcm-bench.json) | Live runs, plugin against hermes-lcm 0.21.0-rc2 and the built-in compressor: DGX, automatic compaction, ten synthetic cases of about 105,000 tokens |diff --git a/evidence/issue-12-whitespace.json b/evidence/issue-12-whitespace.jsonnew file mode 100644index 0000000..70c07c2--- /dev/null+++ b/evidence/issue-12-whitespace.json@@ -0,0 +1,317 @@+{+ "issue": "https://github.com/Elevatormusic/hermes-warm-compaction/issues/12",+ "date": "2026-10-07",+ "scope": "Trailing whitespace removal from complete text strings during source validation.",+ "synthetic": true,+ "privacy": "Metadata only. No conversation text, request bodies, credentials, or private paths.",+ "base_plugin_commit": "f5dd19c1c1e25edbb15e1f1754dc6e99ddbef151",+ "hermes": {+ "commit": "e36a818033f5246e5fcfe9a6967bb85e885ba155",+ "clean": true+ },+ "integration_python": "3.11.14",+ "script_sha256": "c8eea9bddb3df78cf6f2b4dfec8f2a182e221548644c4e1455e4aae57c9b5c44",+ "fixed_plugin_files_sha256": {+ "warm_compaction/__init__.py": "760484ace80b9da77d7397828f69178905c9ac95df7b8114956bde727bd98aef",+ "warm_compaction/capture.py": "a9f907ada5d83eba79628454630d9295664b8215a2f2409cfc26223bcb2ad40f",+ "warm_compaction/engine.py": "d3f24174f807a2b63441b957f1eafd45e25f2faf30427591dab85010485216bd",+ "warm_compaction/fallback.py": "9b2d0529767ff157bbe0d1cbf096ca6ac35217118f81eee769adf41d50189a9a",+ "warm_compaction/handoff.py": "fcab51def8fbcc2b1928957930c9a5c58a4ad202c949fd9f870a77f3b30276ae",+ "warm_compaction/layout.py": "b49314c5236268247c337d9fae939135c97b47384300dcdfd00b0da0c040db93",+ "warm_compaction/plugin.yaml": "baa7820b3ce9a40d2a53f70a4d0525131860ea2ec670143d0a7742b59c4d937b",+ "warm_compaction/rows.py": "f2ca69a7361affc4fd84388ab9ac718a0e9affa609c87d75efd6e0c25fa860cf",+ "warm_compaction/warm.py": "db4826e3ba7762c386f523354fe02833bdb4947c03ac80b67e058f9a67333640"+ },+ "baseline_with_final_regression_script": {+ "scenario": "manual_whitespace",+ "status": "failed",+ "checks": 48,+ "failed_checks": [+ "turn2_path_warm",+ "turn2_one_warm_request",+ "turn2_handoff_in_summary",+ "turn3_path_warm",+ "turn3_one_warm_request",+ "turn3_handoff_in_summary",+ "no_fallback_request"+ ],+ "install_checks_passed": true,+ "worker_exit_codes": [+ 0,+ 0+ ],+ "request_counts": {+ "main": 3,+ "warm": 1,+ "fallback": 2+ },+ "outcomes": [+ [+ "warm",+ "accepted"+ ],+ [+ "fallback",+ "source_transform_unsupported"+ ],+ [+ "fallback",+ "source_transform_unsupported"+ ]+ ]+ },+ "fixed_scenarios": [+ {+ "scenario": "enable_manual_warm",+ "status": "passed",+ "checks": 24,+ "failed_checks": [],+ "install_checks_passed": true,+ "worker_exit_codes": [+ 0,+ 0+ ],+ "request_counts": {+ "main": 2,+ "warm": 1+ }+ },+ {+ "scenario": "manual_whitespace",+ "status": "passed",+ "checks": 60,+ "failed_checks": [],+ "install_checks_passed": true,+ "worker_exit_codes": [+ 0,+ 0+ ],+ "request_counts": {+ "main": 3,+ "warm": 3+ },+ "outcomes": [+ [+ "warm",+ "accepted"+ ],+ [+ "warm",+ "accepted"+ ],+ [+ "warm",+ "accepted"+ ]+ ]+ },+ {+ "scenario": "manual_fallback",+ "status": "passed",+ "checks": 18,+ "failed_checks": [],+ "install_checks_passed": true,+ "worker_exit_codes": [+ 0,+ 0+ ],+ "request_counts": {+ "main": 2,+ "warm": 1,+ "fallback": 1+ }+ },+ {+ "scenario": "manual_fixed",+ "status": "passed",+ "checks": 17,+ "failed_checks": [],+ "install_checks_passed": true,+ "worker_exit_codes": [+ 0,+ 0+ ],+ "request_counts": {+ "main": 2,+ "warm": 1,+ "fallback": 1+ }+ },+ {+ "scenario": "auto_tool_loop",+ "status": "passed",+ "checks": 27,+ "failed_checks": [],+ "install_checks_passed": true,+ "worker_exit_codes": [+ 0,+ 0+ ],+ "request_counts": {+ "main": 4,+ "warm": 2+ }+ },+ {+ "scenario": "auto_warm_failures",+ "status": "passed",+ "checks": 38,+ "failed_checks": [],+ "install_checks_passed": true,+ "worker_exit_codes": [+ 0,+ 0+ ],+ "request_counts": {+ "main": 8,+ "warm": 4,+ "fallback": 3+ },+ "outcomes": [+ [+ "fallback",+ "provider_error"+ ],+ [+ "fallback",+ "provider_error"+ ],+ [+ "fallback",+ "provider_error"+ ],+ [+ "warm",+ "accepted"+ ]+ ]+ },+ {+ "scenario": "rollback",+ "status": "passed",+ "checks": 16,+ "failed_checks": [],+ "install_checks_passed": true,+ "worker_exit_codes": [+ 0,+ 0,+ 0+ ],+ "request_counts": {+ "main": 2,+ "warm": 1+ },+ "rollback_status": "passed",+ "rollback_failed_checks": []+ }+ ],+ "fixed_whitespace_checks": {+ "listed_in_engine_picker": true,+ "engine_selected": true,+ "turn1_reply": true,+ "turn1_stored_question_exact": true,+ "turn1_sent_question_stripped": true,+ "turn1_path_warm": true,+ "turn1_one_warm_request": true,+ "turn1_prefix_exact": true,+ "turn1_new_rows": true,+ "turn1_instruction_last": true,+ "turn1_not_streamed": true,+ "turn1_settings_kept": true,+ "turn1_reply_limit": true,+ "turn1_removed_rows": true,+ "turn1_summary_rows": true,+ "turn1_handoff_in_summary": true,+ "turn1_tail_question_exact": true,+ "turn1_history_saved": true,+ "turn1_saved_question_matches_host": true,+ "turn1_engine_session_follows": true,+ "turn2_reply": true,+ "turn2_stored_question_exact": true,+ "turn2_sent_question_stripped": true,+ "turn2_path_warm": true,+ "turn2_one_warm_request": true,+ "turn2_prefix_exact": true,+ "turn2_new_rows": true,+ "turn2_instruction_last": true,+ "turn2_not_streamed": true,+ "turn2_settings_kept": true,+ "turn2_reply_limit": true,+ "turn2_removed_rows": true,+ "turn2_summary_rows": true,+ "turn2_handoff_in_summary": true,+ "turn2_tail_question_exact": true,+ "turn2_history_saved": true,+ "turn2_saved_question_matches_host": true,+ "turn2_engine_session_follows": true,+ "turn3_reply": true,+ "turn3_stored_question_exact": true,+ "turn3_sent_question_stripped": true,+ "turn3_path_warm": true,+ "turn3_one_warm_request": true,+ "turn3_prefix_exact": true,+ "turn3_new_rows": true,+ "turn3_instruction_last": true,+ "turn3_not_streamed": true,+ "turn3_settings_kept": true,+ "turn3_reply_limit": true,+ "turn3_removed_rows": true,+ "turn3_summary_rows": true,+ "turn3_handoff_in_summary": true,+ "turn3_tail_question_exact": true,+ "turn3_history_saved": true,+ "turn3_saved_question_matches_host": true,+ "turn3_engine_session_follows": true,+ "three_compactions": true,+ "three_main_requests": true,+ "no_fallback_request": true,+ "plugin_made_no_blocked_call": true+ },+ "retry": {+ "scenario": "enable_manual_warm",+ "initial_status": "failed",+ "initial_error": "Windows DLL path length limit in the temporary Hermes dependency runtime.",+ "initial_worker_exit_codes": [+ 0,+ 1+ ],+ "action": "Run only this scenario again with a shorter temporary work path.",+ "result": "passed"+ },+ "unit_checks": {+ "focused_command": "python -m unittest discover -s tests -p test_wc_warm.py",+ "before_fix": {+ "status": "failed_expected",+ "tests": 61,+ "error_cases": 24,+ "error_code": "source_transform_unsupported"+ },+ "after_fix": {+ "status": "passed",+ "tests": 61+ },+ "full_command": "python -m unittest discover -s tests -p \"test_wc_*.py\"",+ "full_results": [+ {+ "python": "3.14.5",+ "status": "passed",+ "tests": 291+ },+ {+ "python": "3.13.1",+ "status": "passed",+ "tests": 291+ }+ ]+ },+ "lint": {+ "version": "0.16.10",+ "command": "ruff check .",+ "status": "passed"+ },+ "limits": [+ "Loopback fake server only; no real model or live provider route.",+ "Live cache reuse, summary quality, and speed were not measured.",+ "Leading and inner whitespace changes and content-list rewrites remain refused."+ ]+}diff --git a/scripts/check_plugin_hermes.py b/scripts/check_plugin_hermes.pyindex 6b9d3c1..ef6b93a 100644--- a/scripts/check_plugin_hermes.py+++ b/scripts/check_plugin_hermes.py@@ -37,7 +37,8 @@ SEED_QUESTION = "What is the code word?"NEXT_QUESTION = "What is the next step?"TOOL_QUESTION = "Call the note tool, then answer."TOOL_AGAIN_QUESTION = "Call the note tool again, then answer."-QUESTIONS = (SEED_QUESTION, NEXT_QUESTION, TOOL_QUESTION, TOOL_AGAIN_QUESTION)+WHITESPACE_QUESTIONS = ("Hello there ", "Hello there\n")+QUESTIONS = (SEED_QUESTION, NEXT_QUESTION, TOOL_QUESTION, TOOL_AGAIN_QUESTION, "Hello there")HANDOFF = ("## Goal\nFinish the synthetic test task.\n\n""## User instructions\n- \"Answer in one short sentence.\"\n\n"@@ -63,8 +64,8 @@ TOOL_PLUGIN = {' "parameters": {"type": "object", "properties": {}, "required": []}})\n'),}-SCENARIOS = ("enable_manual_warm", "manual_fallback", "manual_fixed", "auto_tool_loop", "auto_warm_failures",- "rollback")+SCENARIOS = ("enable_manual_warm", "manual_whitespace", "manual_fallback", "manual_fixed", "auto_tool_loop",+ "auto_warm_failures", "rollback")# The scenarios with automatic compaction in a tool loop.AUTO_SCENARIOS = ("auto_tool_loop", "auto_warm_failures")NOTICE_MARK = "Warm compaction unavailable"@@ -355,6 +356,9 @@ def synthetic_history(pairs=30):def plan_for(scenario):+ if scenario == "manual_whitespace":+ return {"main": [{"content": "The code word is BLUE-7."}, {"content": "Hello."},+ {"content": "Hello again."}]}if scenario in ("enable_manual_warm", "rollback"):return {"main": [{"content": "The code word is BLUE-7."}, {"content": "Next: report BLUE-7."}]}if scenario == "manual_fallback":@@ -535,6 +539,9 @@ def run_phase(spec, session_dir):run_auto(agent, engine, history, server, result, checks)elif scenario == "auto_warm_failures":run_auto_failures(agent, engine, history, server, result, checks, statuses, home)+ elif scenario == "manual_whitespace":+ run_manual_whitespace(agent, engine, db, history, server, result, checks,+ _compress_session_history, finalize_context_engine_compression_notification)else:run_manual(agent, engine, db, history, server, result, checks, scenario,_compress_session_history, finalize_context_engine_compression_notification)@@ -766,6 +773,55 @@ def run_manual(agent, engine, db, history, server, result, checks, scenario, comlater[0]["body"]["messages"][0].get("content") or "")+def run_manual_whitespace(agent, engine, db, history, server, result, checks, compress, finalize):+ """Repeat manual compaction when Hermes strips a string only in the sent request."""+ messages, outcomes = history, []+ for index, question in enumerate((SEED_QUESTION, *WHITESPACE_QUESTIONS), 1):+ if index > 1:+ # Add enough synthetic rows for another compaction. Only the new question has outer white space.+ extra = synthetic_history(20)+ for row in extra:+ row["content"] = row["content"].strip()+ messages = list(messages) + extra+ reply = agent.run_conversation(question, system_message=SYSTEM, conversation_history=messages)+ messages = list(reply.get("messages") or [])+ label = f"turn{index}"+ checks[f"{label}_reply"] = bool(str(reply.get("final_response") or "").strip())+ checks[f"{label}_stored_question_exact"] = len(messages) >= 2 and messages[-2].get("content") == question+ main = by_kind(server, "main")[-1]+ checks[f"{label}_sent_question_stripped"] = main["body"]["messages"][-1].get("content") == question.strip()+ session = {"agent": agent, "history": messages, "history_lock": threading.Lock(),+ "history_version": index, "session_key": agent.session_id}+ warm_before = len(by_kind(server, "warm"))+ removed, _usage = compress(session, "")+ finalize(agent, committed=True)+ last = dict(engine.warm_last or {})+ outcomes.append([last.get("path"), last.get("reason")])+ result[f"warm_last_{index}"] = last+ checks[f"{label}_path_warm"] = (last.get("path"), last.get("reason")) == ("warm", "accepted")+ checks[f"{label}_one_warm_request"] = len(by_kind(server, "warm")) == warm_before + 1+ if checks[f"{label}_one_warm_request"]:+ check_warm_request(server, checks, label)+ checks[f"{label}_removed_rows"] = removed > 0+ messages = session["history"]+ checks[f"{label}_summary_rows"] = summary_rows_ok(messages)+ checks[f"{label}_handoff_in_summary"] = len(messages) >= 2 and HANDOFF in str(messages[1].get("content"))+ checks[f"{label}_tail_question_exact"] = any(+ row.get("role") == "user" and row.get("content") == question for row in messages[2:])+ saved = db.get_messages_as_conversation(agent.session_id)+ # Hermes strips outer white space when it reloads user and assistant strings from the database.+ checks[f"{label}_history_saved"] = [row.get("content") for row in saved] == [+ str(row.get("content") or "").strip() for row in messages]+ checks[f"{label}_saved_question_matches_host"] = any(+ row.get("role") == "user" and row.get("content") == question.strip() for row in saved[2:])+ checks[f"{label}_engine_session_follows"] = engine._wc_session_id == agent.session_id+ result["outcomes"] = outcomes+ result["compression_count"] = engine.compression_count+ checks["three_compactions"] = engine.compression_count == 3+ checks["three_main_requests"] = len(by_kind(server, "main")) == 3+ checks["no_fallback_request"] = not by_kind(server, "fallback")++def run_auto(agent, engine, history, server, result, checks):checks["threshold_from_settings"] = engine.threshold_tokens == int(CONTEXT_LENGTH * 0.4)first = agent.run_conversation(TOOL_QUESTION, system_message=SYSTEM, conversation_history=history)diff --git a/tests/test_wc_warm.py b/tests/test_wc_warm.pyindex 2296638..8c7ea5e 100644--- a/tests/test_wc_warm.py+++ b/tests/test_wc_warm.py@@ -1,5 +1,6 @@"""Tests for the warm request."""+import copyimport jsonimport threadingimport unittest@@ -475,6 +476,116 @@ class BuildRequestTest(unittest.TestCase):self.build(capture)self.assertEqual(caught.exception.code, "source_transform_unsupported")+ def test_accepts_only_full_trailing_whitespace_removal_from_strings(self):+ # Hermes removes outer whitespace from complete strings, for all message roles. Leading whitespace+ # stays protected by the source check. The captured prefix and the stored rows stay unchanged.+ for index in (0, 1, 2):+ for suffix in (" ", " ", "\n\n", "\t", "\r\n", "\u00a0\u2003"):+ for remove in (False, True):+ with self.subTest(index=index, suffix=suffix, remove=remove):+ self._tool_round()+ self.rows[index]["content"] = "synthetic text" + suffix+ capture = capture_for(self.rows, self.reply)+ sent = capture["body"]["messages"]+ if remove:+ sent[index + 1]["content"] = "synthetic text"+ before_capture, before_messages = copy.deepcopy(capture), copy.deepcopy(self.messages)+ body = self.build(capture)+ self.assertEqual(body["messages"][:len(sent)], before_capture["body"]["messages"])+ self.assertEqual(capture, before_capture)+ self.assertEqual(self.messages, before_messages)++ def test_trailing_whitespace_removal_uses_the_effective_api_content(self):+ cases = (+ user("display text", api_content="sent text \n"),+ assistant("display text", api_content="sent text \n"),+ user([{"type": "text", "text": "display text"}], api_content="sent text \n"),+ user("sent text \n", api_content=""),+ assistant("sent text \n", api_content=""),+ dict(tool("c0", "sent text \n"), api_content="ignored sidecar"),+ )+ for row in cases:+ with self.subTest(row=row):+ self.rows = [row]+ self.messages = [*self.rows, self.reply, tool("c1", "r1"), user("u3")]+ capture = capture_for(self.rows, self.reply)+ capture["body"]["messages"][1] = wire_row(row)+ capture["body"]["messages"][1]["content"] = "sent text"+ before_capture, before_messages = copy.deepcopy(capture), copy.deepcopy(self.messages)+ body = self.build(capture)+ self.assertEqual(body["messages"][:2], before_capture["body"]["messages"])+ self.assertEqual(capture, before_capture)+ self.assertEqual(self.messages, before_messages)+ capture["body"]["messages"][1]["content"] = "display text"+ with self.assertRaises(WarmRefusal) as caught:+ self.build(capture)+ self.assertEqual(caught.exception.code, "source_transform_unsupported")++ def test_refuses_other_string_whitespace_changes(self):+ cases = (+ ("text", "text "),+ ("text ", "text "),+ ("text \n", "text "),+ ("text \n", "text\n"),+ ("text ", "text\t"),+ (" text \n", "text"),+ ("text \n", " text"),+ ("two words \n", "two words"),+ ("text\u200b", "text"),+ )+ for stored, sent in cases:+ with self.subTest(stored=stored, sent=sent):+ self.rows = [user(stored)]+ self.messages = [*self.rows, self.reply, tool("c1", "r1"), user("u3")]+ capture = capture_for(self.rows, self.reply)+ capture["body"]["messages"][1]["content"] = sent+ with self.assertRaises(WarmRefusal) as caught:+ self.build(capture)+ self.assertEqual(caught.exception.code, "source_transform_unsupported")++ def test_refuses_whitespace_removal_from_text_parts(self):+ image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}+ text = {"type": "text", "text": "text \n"}+ trimmed = {"type": "text", "text": "text"}+ cases = (+ ([text], [trimmed]),+ (["text \n"], ["text"]),+ ([text], "text"),+ ("text \n", [trimmed]),+ ([text, image], [trimmed, image]),+ ([image, text], [image, trimmed]),+ ([text, image, text], [text, image, trimmed]),+ )+ for stored, sent in cases:+ with self.subTest(stored=stored, sent=sent):+ self.rows = [user(stored)]+ self.messages = [*self.rows, self.reply, tool("c1", "r1"), user("u3")]+ capture = capture_for(self.rows, self.reply)+ capture["body"]["messages"][1]["content"] = sent+ with self.assertRaises(WarmRefusal) as caught:+ self.build(capture)+ self.assertEqual(caught.exception.code, "source_transform_unsupported")++ def test_trailing_whitespace_removal_does_not_bypass_other_source_checks(self):+ changes = (+ lambda row: row.update(role="user"),+ lambda row: row.update(name="other"),+ lambda row: row.update(reasoning_content="other"),+ lambda row: row.update(recipient="other"),+ lambda row: row["tool_calls"][0]["function"].update(arguments='{"path":"other"}'),+ )+ for change in changes:+ with self.subTest(change=change):+ self._tool_round()+ self.rows[1]["content"] = "call text \n"+ capture = capture_for(self.rows, self.reply)+ sent = capture["body"]["messages"][2]+ sent["content"] = "call text"+ change(sent)+ with self.assertRaises(WarmRefusal) as caught:+ self.build(capture)+ self.assertEqual(caught.exception.code, "source_transform_unsupported")+def test_refuses_changed_whitespace_inside_the_text(self):self.rows = [user("def f():\n return 1"), assistant("a1"), user("u2")]self.messages = [*self.rows, self.reply, tool("c1", "r1"), user("u3")]diff --git a/warm_compaction/warm.py b/warm_compaction/warm.pyindex 600d343..ee9ed26 100644--- a/warm_compaction/warm.py+++ b/warm_compaction/warm.py@@ -131,18 +131,23 @@ def _arguments(value: Any) -> tuple[str, str]:def _same_row(wire: Any, row: Any) -> bool:"""True when the sent row carries the stored row: the same shape; the same name (the Hermes transportremoves the name from tool rows only); the same image, audio, and file parts in the same order; each stored- text run equal to the sent text run at the same place (Hermes stores the text that it sends, with its- request-time context, in api_content; other text is a rewrite, on the same line or on its own line); and the- same tool-call arguments. The stored text is the api_content sidecar when the row has one."""+ text run equal to the sent text run at the same place, or a complete string with all trailing whitespace+ removed; and the same tool-call arguments. The stored text is the api_content sidecar when the row has+ one. Leading whitespace and whitespace in content lists must stay unchanged."""if _shape(wire) != _shape(row):return Falsename = attr(wire, "name")if name != attr(row, "name") and not (name is None and attr(row, "role") == "tool"):return False- sent_runs, sent_media = _parts(attr(wire, "content"))- stored_runs, stored_media = _parts(api_content(row))- if sent_media != stored_media or not all(- stored == sent for stored, sent in zip(stored_runs, sent_runs)):+ sent_content, stored_content = attr(wire, "content"), api_content(row)+ sent_runs, sent_media = _parts(sent_content)+ stored_runs, stored_media = _parts(stored_content)+ same_text = all(stored == sent for stored, sent in zip(stored_runs, sent_runs))+ # Hermes e36a8180 strips complete strings during request assembly. Allow only the trailing removal;+ # a change of leading whitespace can change code indentation. Do not normalize content lists.+ trailing_removed = (isinstance(sent_content, str) and isinstance(stored_content, str)+ and sent_content == stored_content.rstrip())+ if sent_media != stored_media or not (same_text or trailing_removed):return Falsereturn [_arguments(arguments) for _id, _name, arguments in tool_calls_of(wire)] == [_arguments(arguments) for _id, _name, arguments in tool_calls_of(row)]