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(learn/claude): don't abort the whole scan on a null message line (#2299)
Abhay Singheed80dd4baa6
diff --git a/headroom/learn/plugins/claude.py b/headroom/learn/plugins/claude.pyindex 185ba32de..399cacfba 100644--- a/headroom/learn/plugins/claude.py+++ b/headroom/learn/plugins/claude.py@@ -208,7 +208,12 @@ class ClaudeCodePlugin(LearnPlugin, ConversationScanner):if line_type == "assistant":self._extract_tool_uses(d, tool_uses)- usage = d.get("message", {}).get("usage", {})+ # `get("message", {})` returns None for an explicit+ # {"message": null} line (the default only applies to a+ # missing key); `.get` on None then raises AttributeError,+ # which the OSError/UnicodeDecodeError guard does not catch+ # — so one malformed line crashed the whole learn run.+ usage = (d.get("message") or {}).get("usage", {})total_input_tokens += usage.get("input_tokens", 0)total_input_tokens += usage.get("cache_read_input_tokens", 0)total_input_tokens += usage.get("cache_creation_input_tokens", 0)@@ -237,7 +242,7 @@ class ClaudeCodePlugin(LearnPlugin, ConversationScanner):def _extract_tool_uses(self, d: dict, tool_uses: dict[str, tuple[str, dict]]) -> None:"""Extract tool_use blocks from an assistant message."""- msg = d.get("message", {})+ msg = d.get("message") or {}content = msg.get("content", [])if not isinstance(content, list):return@@ -261,7 +266,7 @@ class ClaudeCodePlugin(LearnPlugin, ConversationScanner):timestamp: str | None = None,) -> None:"""Extract tool_result blocks from a user message and match to tool_uses."""- msg = d.get("message", {})+ msg = d.get("message") or {}content = msg.get("content", [])if not isinstance(content, list):return@@ -327,7 +332,7 @@ class ClaudeCodePlugin(LearnPlugin, ConversationScanner):timestamp: str | None = None,) -> None:"""Extract user text messages and interruptions from a user line."""- msg = d.get("message", {})+ msg = d.get("message") or {}content = msg.get("content", "")if isinstance(content, str) and content.strip():diff --git a/tests/test_learn/test_subagent_scanning.py b/tests/test_learn/test_subagent_scanning.pyindex f533a0859..8839cf2aa 100644--- a/tests/test_learn/test_subagent_scanning.py+++ b/tests/test_learn/test_subagent_scanning.py@@ -42,6 +42,46 @@ def _write_session(path: Path, out: str = "x" * 400) -> None:path.write_text("\n".join(json.dumps(line) for line in lines))+def test_scan_survives_null_message_line(tmp_path: Path) -> None:+ # A single line with an explicit {"message": null} must not crash the scan+ # (the per-file guard only catches OSError/UnicodeDecodeError, so an+ # AttributeError here would abort the whole `learn` run). The valid tool_use+ # pair around it must still be parsed.+ path = tmp_path / "main-uuid.jsonl"+ lines = [+ {"type": "assistant", "message": None},+ {+ "type": "assistant",+ "message": {+ "usage": {"input_tokens": 100, "output_tokens": 10},+ "content": [+ {+ "type": "tool_use",+ "id": "u1",+ "name": "Read",+ "input": {"file_path": "/a.py"},+ }+ ],+ },+ },+ {"type": "user", "message": None},+ {+ "type": "user",+ "message": {+ "content": [{"type": "tool_result", "tool_use_id": "u1", "content": "x" * 400}]+ },+ },+ ]+ path.write_text("\n".join(json.dumps(line) for line in lines))++ plugin = ClaudeCodePlugin()+ session = plugin._scan_session(path)++ assert session is not None+ assert len(session.tool_calls) == 1+ assert session.total_input_tokens == 100++def test_scan_project_discovers_subagent_and_workflow_transcripts(tmp_path: Path) -> None:_write_session(tmp_path / "main-uuid.jsonl")_write_session(tmp_path / "main-uuid" / "subagents" / "agent-1.jsonl")