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
Merge upstream/main into feat/docker-e2e-wrap-validation
JerrettDavisaddcfcffcdc1
diff --cc headroom/cli/wrap.pyindex afe05a5d9,afe05a5d9..128db09f6--- a/headroom/cli/wrap.py+++ b/headroom/cli/wrap.py@@@ -436,14 -436,14 +436,23 @@@ def _copy_openclaw_plugin_into_extensioraise click.ClickException(f"Plugin dist folder missing at {dist_dir}. Build the plugin first.")++ hook_shim_dir = plugin_dir / "hook-shim"++ if not hook_shim_dir.exists():++ raise click.ClickException(++ f"Plugin hook-shim folder missing at {hook_shim_dir}. Build the plugin first."++ )extensions_dir = _resolve_openclaw_extensions_dir(openclaw_bin)target_dir = extensions_dir / "headroom"target_dist = target_dir / "dist"++ target_hook_shim = target_dir / "hook-shim"target_dir.mkdir(parents=True, exist_ok=True)if target_dist.exists():shutil.rmtree(target_dist)++ if target_hook_shim.exists():++ shutil.rmtree(target_hook_shim)shutil.copytree(dist_dir, target_dist)++ shutil.copytree(hook_shim_dir, target_hook_shim)for filename in ("openclaw.plugin.json", "package.json", "README.md"):source = plugin_dir / filenamediff --cc headroom/proxy/handlers/openai.pyindex 40150e77e,984484731..f088cff4f--- a/headroom/proxy/handlers/openai.py+++ b/headroom/proxy/handlers/openai.py@@@ -1060,7 -1076,13 +1080,15 @@@ class OpenAIHandlerMixinif instructions and opt and opt[0].get("role") == "system":body["instructions"] = opt[0]["content"]opt = opt[1:]- body["input"] = messages_to_responses_items(opt, input_data, preserved)+ if result.tokens_after <= original_tokens:- body["input"] = messages_to_responses_items(opt, input_data, preserved)++ body["input"] = messages_to_responses_items(++ opt, input_data, preserved++ )+ else:+ logger.warning(+ f"[{request_id}] WS optimization inflated tokens "+ f"({original_tokens} -> {result.tokens_after}), reverting"+ )tokens_saved = max(0, original_tokens - result.tokens_after)first_msg_raw = json.dumps(body)logger.info(diff --cc headroom/proxy/helpers.pyindex 98adbe177,41faa0626..e4d857133--- a/headroom/proxy/helpers.py+++ b/headroom/proxy/helpers.py@@@ -78,8 -78,15 +78,13 @@@ def _setup_file_logging() -> Nonehandler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))- # Attach to the headroom root logger so all sub-loggers are captured- logging.getLogger("headroom").addHandler(handler)+ # Attach to the headroom root logger so all sub-loggers are captured.+ # Disable propagation to root to avoid duplicate writes when+ # wrap.py redirects stderr to the same log file.+ headroom_logger = logging.getLogger("headroom")- if not any(- isinstance(h, RotatingFileHandler) for h in headroom_logger.handlers- ):++ if not any(isinstance(h, RotatingFileHandler) for h in headroom_logger.handlers):+ headroom_logger.addHandler(handler)+ headroom_logger.propagate = Falseexcept OSError:# Non-fatal: can't write logs (read-only fs, permissions, etc.)passdiff --cc tests/test_cli/test_wrap_openclaw.pyindex efecf64c4,efecf64c4..3072e9074--- a/tests/test_cli/test_wrap_openclaw.py+++ b/tests/test_cli/test_wrap_openclaw.py@@@ -25,6 -25,6 +25,9 @@@ def plugin_dir(tmp_path: Path) -> Pathplugin.mkdir(parents=True)(plugin / "package.json").write_text('{"name":"headroom-openclaw"}\n')(plugin / "openclaw.plugin.json").write_text('{"id":"headroom"}\n')++ hook_shim = plugin / "hook-shim"++ hook_shim.mkdir()++ (hook_shim / "index.js").write_text("export default {};\n")return plugin@@@ -416,11 -416,11 +419,24 @@@ def test_copy_openclaw_plugin_into_exte(plugin / "package.json").write_text("{}\n")(plugin / "openclaw.plugin.json").write_text("{}\n")++ with patch("headroom.cli.wrap._resolve_openclaw_extensions_dir", return_value=tmp_path):++ with pytest.raises(Exception, match="Plugin hook-shim folder missing"):++ wrap_cli._copy_openclaw_plugin_into_extensions(++ plugin_dir=plugin, openclaw_bin="openclaw"++ )++++ hook_shim = plugin / "hook-shim"++ hook_shim.mkdir()++ (hook_shim / "index.js").write_text("shim\n")++ext_root = tmp_path / ".openclaw" / "extensions"target_headroom = ext_root / "headroom"target_dist = target_headroom / "dist"++ target_hook_shim = target_headroom / "hook-shim"target_dist.mkdir(parents=True)(target_dist / "old.js").write_text("old\n")++ target_hook_shim.mkdir(parents=True)++ (target_hook_shim / "old.js").write_text("old-shim\n")with patch("headroom.cli.wrap._resolve_openclaw_extensions_dir", return_value=ext_root):out = wrap_cli._copy_openclaw_plugin_into_extensions(@@@ -430,3 -430,3 +446,5 @@@assert out == target_headroomassert (target_dist / "index.js").exists()assert not (target_dist / "old.js").exists()++ assert (target_hook_shim / "index.js").exists()++ assert not (target_hook_shim / "old.js").exists()