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(transforms): stop folding datetime-prefixed user messages as search results (#3221)

Raúl7784bb184614
diff --git a/headroom/transforms/content_detector.py b/headroom/transforms/content_detector.py
index 5e364fed6..bb8325e42 100644
--- a/headroom/transforms/content_detector.py
+++ b/headroom/transforms/content_detector.py
@@ -457,6 +457,23 @@ def _try_detect_html(content: str) -> DetectionResult | None:
)
+def _is_search_result_line(line: str) -> bool:
+ """True when a line looks like ``path:line:content`` grep output.
+
+ The bare ``^[^\\s:]+:\\d+:`` shape also matches ISO-8601 timestamps
+ (``…T09:57:59…``) and XML-ish wrappers harnesses prepend to user turns
+ (Copilot CLI's ``<current_datetime>…`` line), which misroutes prose to
+ the SearchCompressor — and that compressor keeps only matching lines,
+ deleting the rest. So the pre-colon segment must additionally look like
+ a file path: no angle brackets and no ``=`` (rules out markup tags and
+ ``key=value:12:`` log lines).
+ """
+ if not _SEARCH_RESULT_PATTERN.match(line):
+ return False
+ prefix = line.split(":", 1)[0]
+ return "<" not in prefix and ">" not in prefix and "=" not in prefix
+
+
def _try_detect_search(content: str) -> DetectionResult | None:
"""Try to detect grep/ripgrep search results."""
lines = content.split("\n")[:100] # Check first 100 lines
@@ -465,10 +482,16 @@ def _try_detect_search(content: str) -> DetectionResult | None:
matching_lines = 0
for line in lines:
- if line.strip() and _SEARCH_RESULT_PATTERN.match(line):
+ if line.strip() and _is_search_result_line(line):
matching_lines += 1
- if matching_lines == 0:
+ # Absolute floor: a single coincidental `word:digits:` line (a timestamp,
+ # a URL, a time literal inside prose) must not classify a whole payload as
+ # search results — the SearchCompressor drops every non-matching line, so
+ # a false positive is data loss. A genuine one-line grep result loses
+ # nothing by staying uncompressed: all of its lines match, so the
+ # compressor would have kept it verbatim anyway.
+ if matching_lines < 2:
return None
# Calculate confidence based on proportion of matching lines
diff --git a/tests/test_transforms_content_detection.py b/tests/test_transforms_content_detection.py
index 1201c2a60..e74776292 100644
--- a/tests/test_transforms_content_detection.py
+++ b/tests/test_transforms_content_detection.py
@@ -170,6 +170,49 @@ def test_search_detection_uses_match_ratio() -> None:
assert _try_detect_search("\n\n") is None
+def test_search_detection_rejects_datetime_prefixed_user_message() -> None:
+ """Regression: wrap-copilot ate one-line interactive prompts (2026-08-23).
+
+ Copilot CLI prepends ``<current_datetime>…</current_datetime>`` to every
+ interactive user turn. The ISO-8601 ``T09:57:59`` matched the grep
+ ``file:line:`` pattern, so a datetime + one-line prompt classified as
+ SEARCH_RESULTS (1 match / 2 lines = 50% ≥ 30%) and the SearchCompressor
+ deleted the prompt line — the model received only the timestamp.
+ """
+ incident = (
+ "<current_datetime>2026-08-23T09:57:59.792+02:00</current_datetime>\n"
+ "\n"
+ "Please update the PR desc and check .overlay/ for hints."
+ )
+ assert _try_detect_search(incident) is None
+ assert detect_content_type(incident).content_type is not ContentType.SEARCH_RESULTS
+
+
+def test_search_detection_requires_two_matching_lines() -> None:
+ """A single coincidental ``word:digits:`` line must not classify prose."""
+ assert _try_detect_search("src/foo.py:12:def foo():") is None
+ assert (
+ _try_detect_search(
+ "Meeting at 09:30:00 tomorrow.\nBring the reports.\nDo not forget coffee."
+ )
+ is None
+ )
+ # Two genuine grep lines still classify.
+ two = "src/foo.py:12:def foo():\nsrc/bar.py:34: foo()"
+ result = _try_detect_search(two)
+ assert result is not None
+ assert result.content_type is ContentType.SEARCH_RESULTS
+
+
+def test_search_detection_rejects_tag_like_and_key_value_prefixes() -> None:
+ """Markup / key=value lines are not file paths even with ``:\\d+:`` inside."""
+ assert (
+ _try_detect_search('<log time="10:00:00">started</log>\n<log time="10:00:01">stopped</log>')
+ is None
+ )
+ assert _try_detect_search("timeout=30:12:retried\ntimeout=31:12:retried") is None
+
+
def test_log_detection_prefers_build_output_patterns() -> None:
log_output = "\n".join(
[
diff --git a/tests/test_transforms_content_router.py b/tests/test_transforms_content_router.py
index 4f76c6386..74ce9b5c9 100644
--- a/tests/test_transforms_content_router.py
+++ b/tests/test_transforms_content_router.py
@@ -1785,3 +1785,23 @@ def test_detect_content_overrides_html_misroute_for_grep_and_logs(
"</section></main></body></html>"
)
assert _detect_content(html).content_type is ContentType.HTML
+
+
+def test_datetime_prefixed_user_prompt_survives_router() -> None:
+ """Regression (2026-08-23): interactive wrap-copilot prompts were deleted.
+
+ Copilot CLI prepends ``<current_datetime>…</current_datetime>`` to every
+ interactive user turn; the ISO timestamp matched the grep ``file:line:``
+ detector, the one-line prompt classified as SEARCH_RESULTS, and
+ SearchCompressor kept only the datetime line — the model received no
+ request and answered "How can I help you today?". The router must never
+ route this shape to the search line-filter and must keep the prose.
+ """
+ prompt = (
+ "<current_datetime>2026-08-23T09:57:59.792+02:00</current_datetime>\n"
+ "\n"
+ "Please update the PR desc and check .overlay/ for hints."
+ )
+ result = ContentRouter().compress(prompt)
+ assert result.strategy_used is not CompressionStrategy.SEARCH
+ assert "Please update the PR desc" in result.compressed