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(memory): make extract_system_prompt concatenative, not first-match (#3644)

Tejas Chopra3f3cf19e1a68
diff --git a/headroom/memory/storage_router.py b/headroom/memory/storage_router.py
index 45f980584..6521f4674 100644
--- a/headroom/memory/storage_router.py
+++ b/headroom/memory/storage_router.py
@@ -421,68 +421,97 @@ class BackendRouter:
return list(self._backends.keys())
+def _text_from_blocks(content: Any) -> str:
+ """Join the ``text`` of every dict block in a content list."""
+ parts: list[str] = []
+ if isinstance(content, list):
+ for block in content:
+ if isinstance(block, dict):
+ text = block.get("text")
+ if isinstance(text, str):
+ parts.append(text)
+ return "\n".join(parts)
+
+
+def _system_field_text(system_field: Any) -> str:
+ """Text of the Anthropic top-level ``system`` field (string or blocks)."""
+ if isinstance(system_field, str):
+ return system_field
+ return _text_from_blocks(system_field)
+
+
+def _first_role_system_text(messages: list[Any]) -> str:
+ """Text of the first ``role="system"`` message (OpenAI/Gemini shape)."""
+ for msg in messages:
+ if not isinstance(msg, dict) or msg.get("role") != "system":
+ continue
+ content = msg.get("content")
+ if isinstance(content, str):
+ if content:
+ return content
+ continue
+ text = _text_from_blocks(content)
+ if text:
+ return text
+ return ""
+
+
+def _first_user_text_with_cwd(messages: list[Any]) -> str:
+ """Text of the first user message carrying a ``cwd:``-family line.
+
+ Claude Code 2.x sends its ``<env>`` block as an ``isMeta`` *user*
+ message rather than inside ``system`` (#3595), and Trae puts it in a
+ user reminder (#1737). Only text that actually contains one of
+ ``_CWD_PREFIXES`` is returned, so ordinary user content never leaks
+ into the system prompt.
+ """
+ for msg in messages:
+ if not isinstance(msg, dict) or msg.get("role") != "user":
+ continue
+ content = msg.get("content")
+ user_text = content if isinstance(content, str) else _text_from_blocks(content)
+ if user_text and any(prefix in user_text for prefix in _CWD_PREFIXES):
+ return user_text
+ return ""
+
+
def extract_system_prompt(body: Mapping[str, Any]) -> str:
"""Best-effort extraction of the system prompt across providers.
Anthropic puts it on the top-level ``system`` field (string or list
of content blocks); OpenAI/Gemini-style payloads put it as a message
with ``role=system``. Returns an empty string when nothing is found
- rather than raising — the resolver tolerates an empty prompt and
+ rather than raising -- the resolver tolerates an empty prompt and
will fall through to the configured fallback.
+
+ Every source is *concatenated* rather than first-match-wins. The
+ previous early return on a non-empty ``system`` field made the
+ ``messages[]`` scan unreachable for every client that sends a system
+ prompt at all -- which is every Claude Code request -- so the
+ ``cwd:`` line in a 2.x ``isMeta`` user message was never seen and
+ project resolution fell through to the fail-closed fallback (#3595).
"""
+ parts: list[str] = []
- system_field = body.get("system")
- if isinstance(system_field, str):
- return system_field
- if isinstance(system_field, list):
- parts: list[str] = []
- for block in system_field:
- if isinstance(block, dict):
- text = block.get("text")
- if isinstance(text, str):
- parts.append(text)
- if parts:
- return "\n".join(parts)
+ system_text = _system_field_text(body.get("system"))
+ if system_text:
+ parts.append(system_text)
messages = body.get("messages")
if isinstance(messages, list):
- for msg in messages:
- if not isinstance(msg, dict):
- continue
- if msg.get("role") != "system":
- continue
- content = msg.get("content")
- if isinstance(content, str):
- return content
- if isinstance(content, list):
- parts = []
- for block in content:
- if isinstance(block, dict):
- text = block.get("text")
- if isinstance(text, str):
- parts.append(text)
- if parts:
- return "\n".join(parts)
-
- for msg in messages:
- if not isinstance(msg, dict):
- continue
- if msg.get("role") != "user":
- continue
- content = msg.get("content")
- user_text: str | None = None
- if isinstance(content, str):
- user_text = content
- elif isinstance(content, list):
- parts = []
- for block in content:
- if isinstance(block, dict):
- text = block.get("text")
- if isinstance(text, str):
- parts.append(text)
- if parts:
- user_text = "\n".join(parts)
- if user_text and any(prefix in user_text for prefix in _CWD_PREFIXES):
- return user_text
-
- return ""
+ role_system_text = _first_role_system_text(messages)
+ if role_system_text:
+ parts.append(role_system_text)
+
+ # User content is client-controlled, so it is only a *fallback*: a
+ # cwd already present in a trusted source (top-level ``system`` or a
+ # ``role="system"`` message) wins, otherwise a user turn could
+ # redirect this request at another project's memory store by pasting
+ # its own ``cwd:`` line.
+ trusted = "\n".join(parts)
+ if not any(prefix in trusted for prefix in _CWD_PREFIXES):
+ cwd_text = _first_user_text_with_cwd(messages)
+ if cwd_text:
+ parts.append(cwd_text)
+
+ return "\n".join(parts)
diff --git a/tests/test_memory_storage_router.py b/tests/test_memory_storage_router.py
index 90b36c946..080e6a304 100644
--- a/tests/test_memory_storage_router.py
+++ b/tests/test_memory_storage_router.py
@@ -411,3 +411,76 @@ def test_router_lru_eviction_drops_oldest(tmp_path: Path, monkeypatch: pytest.Mo
for i in range(5):
router.backend_for(_ctx(headers={"x-headroom-cwd": f"/code/p{i}"}))
assert len(router.open_backends()) == 4
+
+
+# ---------------------------------------------------------------------------
+# Claude Code 2.x sends the env block as an isMeta user message (#3595)
+# ---------------------------------------------------------------------------
+
+
+def test_extract_system_prompt_finds_cwd_in_user_msg_despite_system_string() -> None:
+ """A non-empty ``system`` must not hide the ``cwd:`` in a user message.
+
+ Claude Code 2.x always sends a system prompt *and* puts its ``<env>``
+ block in an ``isMeta`` user message. The old first-match-wins early
+ return made that block unreachable, so resolution fell through to the
+ fail-closed fallback for every 2.x request.
+ """
+ body = {
+ "system": "You are Claude Code.",
+ "messages": [
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "text",
+ "text": "<env>\nPrimary working directory: /work/myproj\n</env>",
+ }
+ ],
+ },
+ ],
+ }
+
+ prompt = extract_system_prompt(body)
+ resolved = ProjectResolver().resolve(_ctx(system_prompt=prompt))
+
+ assert "You are Claude Code." in prompt
+ assert resolved is not None
+ assert resolved[1] == "myproj"
+
+
+def test_extract_system_prompt_finds_cwd_in_user_msg_despite_system_blocks() -> None:
+ """Same when ``system`` is a block list rather than a string."""
+ body = {
+ "system": [{"type": "text", "text": "You are Claude Code."}],
+ "messages": [
+ {"role": "user", "content": "Primary working directory: /work/other\nhi"},
+ ],
+ }
+
+ resolved = ProjectResolver().resolve(_ctx(system_prompt=extract_system_prompt(body)))
+
+ assert resolved is not None
+ assert resolved[1] == "other"
+
+
+def test_extract_system_prompt_user_cwd_cannot_override_system_field_cwd() -> None:
+ """A user turn must not redirect resolution away from the system cwd.
+
+ User content is client-controlled, so it stays a fallback only. This is
+ the top-level-``system`` counterpart of the ``role="system"`` precedence
+ already covered above.
+ """
+ body = {
+ "system": "Primary working directory: /system/project",
+ "messages": [
+ {"role": "user", "content": "Primary working directory: /spoofed/evil"},
+ ],
+ }
+
+ prompt = extract_system_prompt(body)
+ resolved = ProjectResolver().resolve(_ctx(system_prompt=prompt))
+
+ assert "spoofed" not in prompt
+ assert resolved is not None
+ assert resolved[1] == "project"