Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(learn): recognize escaped persisted pattern IDs (#3951)

JD Davis2e74f06ecec7
diff --git a/headroom/learn/writer.py b/headroom/learn/writer.py
index 8df93e84b..cc77a0704 100644
--- a/headroom/learn/writer.py
+++ b/headroom/learn/writer.py
@@ -116,7 +116,12 @@ def _build_section(recommendations: list[Recommendation]) -> str:
# Matches the "*~N tokens/session saved*" annotation emitted by _build_section.
_TOKENS_ANNOTATION_PATTERN = re.compile(r"\*~([\d,]+) tokens/session saved\*\n?")
-_PATTERN_ID_PATTERN = re.compile(r"<!--\s*headroom:pattern-id:([^\s>]+)\s*-->\s*$")
+# New recommendations contain raw annotations; saved blocks contain the
+# escaped delimiters emitted by sanitize_block_text. Recognize both without
+# unescaping transcript-derived content or weakening managed-block boundaries.
+_PATTERN_ID_PATTERN = re.compile(
+ r"(?:<!--|&lt;!--)\s*headroom:pattern-id:([^\s>]+)\s*(?:-->|--&gt;)\s*$"
+)
def _merge_markdown_items(
diff --git a/tests/test_learn/test_section_lifecycle.py b/tests/test_learn/test_section_lifecycle.py
index 84d45738c..4836aaa45 100644
--- a/tests/test_learn/test_section_lifecycle.py
+++ b/tests/test_learn/test_section_lifecycle.py
@@ -12,6 +12,8 @@ because it asserts on writer behaviour; it drives the real, unmodified
``_patterns_to_recommendations`` to get there.
"""
+import pytest
+
from headroom.learn.models import Recommendation, RecommendationTarget
from headroom.learn.writer import _merge_into_file
from headroom.memory.traffic_learner import (
@@ -177,6 +179,50 @@ class TestCarriedSectionLifecycle:
class TestTrafficLearnerCategoryLifecycle:
"""End-to-end through the real learner rendering."""
+ @pytest.mark.windows_newline
+ @pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["lf", "crlf"])
+ def test_saved_ids_keep_unbatched_items_and_remove_expired_items(self, tmp_path, newline):
+ context_file = tmp_path / "AGENTS.md"
+ prior = _rec(
+ "Learned: preference",
+ "- Old queue <!-- headroom:pattern-id:queue -->\n"
+ "- Prefer ripgrep <!-- headroom:pattern-id:ripgrep -->\n"
+ "- Use vendored SDK <!-- headroom:pattern-id:vendored -->",
+ )
+ prior.preserve_prior_items = True
+ context_file.write_text(
+ _merge_into_file(context_file, [prior]), encoding="utf-8", newline=newline
+ )
+
+ current = _rec("Learned: preference", "- New queue <!-- headroom:pattern-id:queue -->")
+ current.preserve_prior_items = True
+ current.active_item_ids = frozenset({"queue", "ripgrep"})
+ final = _merge_into_file(context_file, [current])
+
+ assert "New queue" in final
+ assert "Old queue" not in final
+ assert "Prefer ripgrep" in final
+ assert "Use vendored SDK" not in final
+ assert final.count("<!--") == 2 # Only the managed block delimiters stay literal.
+
+ @pytest.mark.windows_newline
+ @pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["lf", "crlf"])
+ def test_saved_ids_replace_prior_text_without_a_lifecycle_signal(self, tmp_path, newline):
+ context_file = tmp_path / "AGENTS.md"
+ prior = _rec("Learned: preference", "- Old queue <!-- headroom:pattern-id:queue -->")
+ prior.preserve_prior_items = True
+ context_file.write_text(
+ _merge_into_file(context_file, [prior]), encoding="utf-8", newline=newline
+ )
+
+ current = _rec("Learned: preference", "- New queue <!-- headroom:pattern-id:queue -->")
+ current.preserve_prior_items = True
+ final = _merge_into_file(context_file, [current])
+
+ assert "New queue" in final
+ assert "Old queue" not in final
+ assert final.count("headroom:pattern-id:queue") == 1
+
def test_category_losing_its_last_pattern_drops_its_heading(self, tmp_path):
"""An emptied category leaves the file entirely.