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(memory): resolve Trae cwd metadata from user reminders (#1737) (#1887)
Rod Boev3e85eb1880af
diff --git a/headroom/memory/storage_router.py b/headroom/memory/storage_router.pyindex 91f2fea28..5337e32f7 100644--- a/headroom/memory/storage_router.py+++ b/headroom/memory/storage_router.py@@ -448,4 +448,25 @@ def extract_system_prompt(body: Mapping[str, Any]) -> str: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 ""diff --git a/tests/test_memory_storage_router.py b/tests/test_memory_storage_router.pyindex 7e9d59bc8..9a1c7857c 100644--- a/tests/test_memory_storage_router.py+++ b/tests/test_memory_storage_router.py@@ -155,6 +155,67 @@ def test_extract_system_prompt_missing_returns_empty() -> None:assert extract_system_prompt({"messages": []}) == ""+def test_extract_system_prompt_user_reminder_with_cwd_reaches_resolver() -> None:+ body = {+ "messages": [+ {+ "role": "user",+ "content": [+ {+ "type": "text",+ "text": (+ "<system-reminder>\n\n"+ "The maximum number of terminals is 5.\n\n"+ "<available_terminal>\n"+ "- terminal_id: 9\n"+ "- cwd: S:\\workspace-zhuangxiu\\decorate-offer-api\n"+ "</available_terminal>\n\n"+ "</system-reminder>"+ ),+ }+ ],+ }+ ]+ }++ prompt = extract_system_prompt(body)+ assert "cwd:" in prompt+ resolved = ProjectResolver().resolve(_ctx(system_prompt=prompt))++ assert resolved is not None+ _, display = resolved+ assert "decorate-offer-api" in display+++def test_extract_system_prompt_ordinary_user_text_returns_empty() -> None:+ body = {"messages": [{"role": "user", "content": "Hello, can you help me refactor this?"}]}++ assert extract_system_prompt(body) == ""+++def test_extract_system_prompt_system_message_beats_user_cwd_fallback() -> None:+ body = {+ "messages": [+ {"role": "system", "content": "Working directory: /system/project"},+ {"role": "user", "content": "cwd: /user/project\nDo the thing."},+ ]+ }++ prompt = extract_system_prompt(body)+ resolved = ProjectResolver().resolve(_ctx(system_prompt=prompt))++ assert prompt == "Working directory: /system/project"+ assert resolved is not None+ _, display = resolved+ assert display == "project"+++def test_extract_system_prompt_cwd_in_non_user_message_returns_empty() -> None:+ body = {"messages": [{"role": "assistant", "content": "cwd: /spoof/project"}]}++ assert extract_system_prompt(body) == ""++# ---------------------------------------------------------------------------# BackendRouter path-layout tests (no real backend I/O — we stub the class).# ---------------------------------------------------------------------------