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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix: use all user text blocks for relevance scoring (#4628)
cuteNBD12dca26aa11d
diff --git a/headroom/utils.py b/headroom/utils.pyindex cbd085895..e0ba74e5d 100644--- a/headroom/utils.py+++ b/headroom/utils.py@@ -15,6 +15,9 @@ from typing import AnyMARKER_PREFIX = "<headroom:"MARKER_SUFFIX = ">"+_REMINDER_OPEN = re.compile(r"<system-reminder(?=[\s>])[^<>]*>", re.IGNORECASE)+_REMINDER_CLOSE = re.compile(r"</system-reminder\s*>", re.IGNORECASE)+def generate_request_id() -> str:"""Generate a unique request ID."""@@ -61,6 +64,24 @@ def fast_hash(data: str | bytes, length: int = 16) -> str:return hashlib.md5(data).hexdigest()[:length] # nosec B324+def _strip_system_reminders(text: str) -> str:+ """Remove complete harness reminders while keeping surrounding user text."""+ parts: list[str] = []+ cursor = 0+ while opening := _REMINDER_OPEN.search(text, cursor):+ closing = _REMINDER_CLOSE.search(text, opening.end())+ if closing is None:+ break+ parts.append(text[cursor : opening.start()])+ end = closing.end()+ if opening.start() and end < len(text):+ if not text[opening.start() - 1].isspace() and not text[end].isspace():+ parts.append(" ")+ cursor = end+ parts.append(text[cursor:])+ return "".join(parts).strip()++def extract_user_query(messages: list[dict[str, Any]], *, latest_user_turn_only: bool = False) -> str:@@ -82,14 +103,20 @@ def extract_user_query(for msg in reversed(messages):if msg.get("role") == "user":content = msg.get("content", "")- if isinstance(content, str) and content.strip():- return content.strip()+ if isinstance(content, str):+ user_text = _strip_system_reminders(content)+ if user_text:+ return user_textif isinstance(content, list):- for block in content:- if isinstance(block, dict) and block.get("type") == "text":- text = str(block.get("text", "")).strip()- if text:- return text+ user_text = "\n".join(+ text+ for block in content+ if isinstance(block, dict) and block.get("type") == "text"+ for text in (_strip_system_reminders(str(block.get("text", ""))),)+ if text+ )+ if user_text:+ return user_textif latest_user_turn_only:return ""return ""diff --git a/tests/test_relevance_split.py b/tests/test_relevance_split.pyindex 3e2e57132..6f6e610f3 100644--- a/tests/test_relevance_split.py+++ b/tests/test_relevance_split.py@@ -125,6 +125,31 @@ def test_split_keeps_relevant_drops_irrelevant():assert "".join(t for _, t in runs) == content+def test_split_uses_all_user_text_blocks():+ from headroom.relevance.bm25 import BM25Scorer+ from headroom.utils import extract_user_query++ messages = [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "Review production incidents"},+ {"type": "text", "text": "Focus on Kafka replication"},+ ],+ }+ ]+ content = (+ "Production incident: payment gateway high latency\n\n"+ "Kafka replication lag on broker 17 exceeded 30 minutes\n\n"+ "Disk full on backup server\n\n"+ "Production incident: expired TLS certificate\n"+ )+ runs = plan_relevance_split(content, extract_user_query(messages), BM25Scorer(), threshold=0.25)+ kept = "".join(text for keep, text in runs if keep)+ assert "Kafka replication lag" in kept+ assert "Disk full on backup server" not in kept++def test_empty_query_yields_no_split():assert plan_relevance_split("x\ny\n", "", KeywordScorer(), threshold=0.5) == [(True, "x\ny\n")]diff --git a/tests/test_utils.py b/tests/test_utils.pyindex fafae8ead..3b749b9d7 100644--- a/tests/test_utils.py+++ b/tests/test_utils.py@@ -48,6 +48,85 @@ def test_extract_user_query_and_message_hashes() -> None:assert utils.extract_user_query([{"role": "assistant", "content": "skip"}]) == ""[email protected]("latest_only", [False, True])+def test_extract_user_query_combines_text_blocks(latest_only: bool) -> None:+ messages = [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": " Review incidents "},+ {"type": "image", "source": "example"},+ {"type": "text", "text": " "},+ {"type": "text", "text": " Focus on Kafka replication "},+ ],+ }+ ]+ assert utils.extract_user_query(messages, latest_user_turn_only=latest_only) == (+ "Review incidents\nFocus on Kafka replication"+ )+++def test_extract_user_query_skips_system_reminder_blocks() -> None:+ messages = [+ {"role": "user", "content": "older question"},+ {+ "role": "user",+ "content": [+ {+ "type": "text",+ "text": "<system-reminder>injected instructions</system-reminder>",+ },+ {"type": "text", "text": "real question"},+ {"type": "text", "text": "<system-reminder>more instructions</system-reminder>"},+ ],+ },+ ]+ assert utils.extract_user_query(messages) == "real question"+ assert utils.extract_user_query(messages, latest_user_turn_only=True) == "real question"++ messages[-1]["content"] = [+ {"type": "text", "text": "<system-reminder>injected</system-reminder>"}+ ]+ assert utils.extract_user_query(messages) == "older question"+ assert utils.extract_user_query(messages, latest_user_turn_only=True) == ""++[email protected]("as_blocks", [False, True])+ ("content", "expected"),+ [+ ("<system-reminder>context</system-reminder>\nFind Kafka lag", "Find Kafka lag"),+ ("Find <system-reminder>context</system-reminder> Kafka lag", "Find Kafka lag"),+ (+ "<system-reminder id='1'>a</system-reminder>Find <system-reminder>b</system-reminder>Kafka",+ "Find Kafka",+ ),+ ("<System-Reminder>context</System-Reminder>Find Kafka", "Find Kafka"),+ ("İ<system-reminder>x</system-reminder>Kafka", "İ Kafka"),+ ("Find <system-reminder>unfinished", "Find <system-reminder>unfinished"),+ (+ "Find <system-reminder-extra>example</system-reminder> Kafka",+ "Find <system-reminder-extra>example</system-reminder> Kafka",+ ),+ ],+)+def test_extract_user_query_keeps_text_around_reminders(+ content: str, expected: str, as_blocks: bool+) -> None:+ body = [{"type": "text", "text": content}] if as_blocks else content+ messages = [{"role": "user", "content": body}]+ assert utils.extract_user_query(messages, latest_user_turn_only=True) == expected++[email protected]("as_blocks", [False, True])+def test_reminder_only_turn_keeps_user_query_fallback(as_blocks: bool) -> None:+ reminder = "<system-reminder>context</system-reminder>"+ content = [{"type": "text", "text": reminder}] if as_blocks else reminder+ messages = [{"role": "user", "content": "older question"}, {"role": "user", "content": content}]+ assert utils.extract_user_query(messages) == "older question"+ assert utils.extract_user_query(messages, latest_user_turn_only=True) == ""++def test_extract_user_query_latest_user_turn_only() -> None:"""The flag stops at the newest user turn instead of resurrecting an older one."""# A tool_result continuation: the newest user turn carries no text at all.