fix(memory): make extract_system_prompt concatenative, not first-match (#3644)

Closes #3595

## Problem

`extract_system_prompt` was first-match-wins: it returned as soon as the
top-level `system` field had content, before ever looking at
`messages[]`. Claude Code always sends a non-empty `system`, so the
`messages[]` scan — including the cwd-in-user-message tier added for
Trae in #1737 — was unreachable for every Claude Code request.

Claude Code 2.x moved its `<env>` block (which carries `Primary working
directory:`) out of `system` and into an `isMeta` user message. With the
scan below `system` dead, that block is never seen, and CCR/memory
workspace resolution falls through to the fail-closed empty scope — for
the default configuration, with no header workaround needed.

Reproduced against the exact reported shape:

```python
body = {
    "system": "You are Claude Code.",
    "messages": [{"role": "user", "content": [{"type": "text", "text": "<env>\nPrimary working directory: /work/myproj\n</env>"}]}],
}
extract_system_prompt(body)   # before: "You are Claude Code."  (cwd invisible)
                               # after:  "You are Claude Code.\n<env>\n...\n</env>"
```

## Fix

Every source (`system` field, `role="system"` messages, and the
user-message cwd fallback) is now scanned and **concatenated** instead
of short-circuiting.

The existing anti-spoofing precedence is preserved exactly: a
`cwd:`-family line already present in a trusted source (`system` field
or a `role="system"` message) still wins over one appearing in a user
message, so a user turn can't redirect resolution at another project's
memory store by pasting its own `cwd:` line. Covered by
`test_extract_system_prompt_user_cwd_cannot_override_system_field_cwd`.

## Testing

- 3 new regressions in `tests/test_memory_storage_router.py`, covering:
the exact CC 2.x isMeta-user-message shape (`system` as string), the
same with `system` as a block list, and the anti-spoofing precedence
guard.
- Full existing suite in that file (31 → 36 tests) passes unchanged.
- `ruff check` / `ruff format --check` clean.

Scope note: this fixes the one tier that needs no client cooperation.
#3597 (header namespace mismatch) and #3596 (`project_root_override`
hardcoded to `None` for CCR) are separate, already-tracked breaks in the
same resolution chain — see #3603 and #3606.

Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
Tejas Chopra
2026-09-24 18:22:01 +02:00
committed by GitHub
co-authored by JD Davis
parent c1fc84ade6
commit 3f3cf19e1a
2 changed files with 154 additions and 52 deletions
+81 -52
View File
@@ -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.
"""
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)
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_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)
role_system_text = _first_role_system_text(messages)
if role_system_text:
parts.append(role_system_text)
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
# 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 ""
return "\n".join(parts)
+73
View File
@@ -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"