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): prevent Windows CRLF accumulation in context writers (#3594)
Trîggeredabde3e6e70a6
diff --git a/headroom/learn/writer.py b/headroom/learn/writer.pyindex 6fef4362c..06d90fcb5 100644--- a/headroom/learn/writer.py+++ b/headroom/learn/writer.py@@ -39,9 +39,10 @@ def _read_text_tolerant(file_path: Path) -> str:"""raw = file_path.read_bytes()try:- return raw.decode("utf-8")+ text = raw.decode("utf-8")except UnicodeDecodeError:- return raw.decode("utf-8", errors="replace")+ text = raw.decode("utf-8", errors="replace")+ return text.replace("\r\n", "\n").replace("\r", "\n")# =============================================================================@@ -258,7 +259,7 @@ class ClaudeCodeWriter(ContextWriter):result.add(target_path, full_content)if not dry_run:target_path.parent.mkdir(parents=True, exist_ok=True)- target_path.write_text(full_content, encoding="utf-8")+ target_path.write_text(full_content, encoding="utf-8", newline="\n")if memory_recs:memory_path = self._resolve_memory_path(project)@@ -266,7 +267,7 @@ class ClaudeCodeWriter(ContextWriter):result.add(memory_path, full_content)if not dry_run:memory_path.parent.mkdir(parents=True, exist_ok=True)- memory_path.write_text(full_content, encoding="utf-8")+ memory_path.write_text(full_content, encoding="utf-8", newline="\n")return result@@ -324,7 +325,7 @@ class ClaudeCodeWriter(ContextWriter):f"{target_path.name}. Review the diff before committing.{gitignore_hint}")if not dry_run:- legacy_path.write_text(cleaned, encoding="utf-8")+ legacy_path.write_text(cleaned, encoding="utf-8", newline="\n")else:# CLAUDE.md held nothing but the Headroom block — remove the husk.result.warnings.append(@@ -367,7 +368,7 @@ class CodexWriter(ContextWriter):result.add(agents_md, full_content)if not dry_run:agents_md.parent.mkdir(parents=True, exist_ok=True)- agents_md.write_text(full_content, encoding="utf-8")+ agents_md.write_text(full_content, encoding="utf-8", newline="\n")if memory_recs:instructions_md = project.memory_file or (project.data_path.parent / "instructions.md")@@ -375,7 +376,7 @@ class CodexWriter(ContextWriter):result.add(instructions_md, full_content)if not dry_run:instructions_md.parent.mkdir(parents=True, exist_ok=True)- instructions_md.write_text(full_content, encoding="utf-8")+ instructions_md.write_text(full_content, encoding="utf-8", newline="\n")return result@@ -405,7 +406,7 @@ class GeminiWriter(ContextWriter):result.add(gemini_md, full_content)if not dry_run:gemini_md.parent.mkdir(parents=True, exist_ok=True)- gemini_md.write_text(full_content, encoding="utf-8")+ gemini_md.write_text(full_content, encoding="utf-8", newline="\n")return result@@ -435,6 +436,6 @@ class GrokWriter(ContextWriter):result.add(grok_md, full_content)if not dry_run:grok_md.parent.mkdir(parents=True, exist_ok=True)- grok_md.write_text(full_content, encoding="utf-8")+ grok_md.write_text(full_content, encoding="utf-8", newline="\n")return resultdiff --git a/tests/test_learn/test_writer.py b/tests/test_learn/test_writer.pyindex ce451a80d..c597d55c1 100644--- a/tests/test_learn/test_writer.py+++ b/tests/test_learn/test_writer.py@@ -549,3 +549,24 @@ class TestEncodingResilience:assert "Use uv" in mergedassert "Notes — existing" in merged++ def test_read_text_tolerant_normalizes_crlf_and_cr(self, tmp_path):+ path = tmp_path / "AGENTS.md"+ path.write_bytes(b"line1\r\nline2\rline3\n")+ text = _read_text_tolerant(path)+ assert text == "line1\nline2\nline3\n"++ def test_apply_does_not_accumulate_carriage_returns_on_crlf_file(self, tmp_path):+ proj = _project(tmp_path)+ writer = ClaudeCodeWriter()+ memory_md = proj.data_path / "memory" / "MEMORY.md"+ # Simulate existing Windows CRLF file+ memory_md.write_bytes(b"# Memory Index\r\n\r\n- bullet 1\r\n")++ recs = [_rec(RecommendationTarget.MEMORY_FILE, "Errors", "- rule 1")]+ writer.write(recs, proj, dry_run=False)+ writer.write(recs, proj, dry_run=False)++ raw = memory_md.read_bytes()+ assert b"\r\r" not in raw+ assert raw.count(b"\r") == raw.count(b"\r\n")