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(core): expose compress_openai_responses_live_zone via PyO3 (hot-fix c1/2)
chopratejasc48735d0298b
diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.jsonindex cdb699f4b..5bd29ddb2 100644--- a/.claude-plugin/marketplace.json+++ b/.claude-plugin/marketplace.json@@ -5,14 +5,14 @@},"metadata": {"description": "Headroom marketplace for Claude Code and GitHub Copilot CLI plugins.",- "version": "0.21.0"+ "version": "0.21.4"},"plugins": [{"name": "headroom","source": "./plugins/headroom-agent-hooks","description": "Headroom startup hooks for Claude Code and GitHub Copilot CLI.",- "version": "0.21.0",+ "version": "0.21.4","author": {"name": "Headroom Contributors","url": "https://github.com/chopratejas/headroom"diff --git a/.github/plugin/marketplace.json b/.github/plugin/marketplace.jsonindex cdb699f4b..5bd29ddb2 100644--- a/.github/plugin/marketplace.json+++ b/.github/plugin/marketplace.json@@ -5,14 +5,14 @@},"metadata": {"description": "Headroom marketplace for Claude Code and GitHub Copilot CLI plugins.",- "version": "0.21.0"+ "version": "0.21.4"},"plugins": [{"name": "headroom","source": "./plugins/headroom-agent-hooks","description": "Headroom startup hooks for Claude Code and GitHub Copilot CLI.",- "version": "0.21.0",+ "version": "0.21.4","author": {"name": "Headroom Contributors","url": "https://github.com/chopratejas/headroom"diff --git a/crates/headroom-py/src/lib.rs b/crates/headroom-py/src/lib.rsindex 249d91d43..ccbdc09bf 100644--- a/crates/headroom-py/src/lib.rs+++ b/crates/headroom-py/src/lib.rs@@ -28,9 +28,11 @@ use headroom_core::transforms::tag_protector::{protect_tags as rust_protect_tags, restore_tags as rust_restore_tags,};use headroom_core::transforms::{+ compress_openai_responses_live_zone as rust_compress_openai_responses_live_zone,detect as rust_detect_chain, is_json_array_of_dicts as rust_is_json_array_of_dicts,- ContentType as RustContentType, DetectionResult as RustDetectionResult, DiffCompressionResult,- DiffCompressor, DiffCompressorConfig, DiffCompressorStats,+ AuthMode as RustLiveZoneAuthMode, ContentType as RustContentType,+ DetectionResult as RustDetectionResult, DiffCompressionResult, DiffCompressor,+ DiffCompressorConfig, DiffCompressorStats, LiveZoneOutcome,LogCompressionResult as RustLogResult, LogCompressor as RustLogCompressor,LogCompressorConfig as RustLogConfig, LogCompressorStats as RustLogStats,LogFormat as RustLogFormat, LogLevel as RustLogLevel,@@ -38,7 +40,7 @@ use headroom_core::transforms::{SearchCompressorConfig as RustSearchConfig, SearchCompressorStats as RustSearchStats,};use pyo3::prelude::*;-use pyo3::types::{PyDict, PyString};+use pyo3::types::{PyBytes, PyDict, PyString};/// Identity stub used by the Python smoke test to verify linkage.#[pyfunction]@@ -1459,6 +1461,72 @@ fn known_html_tag_names() -> Vec<&'static str> {// ─── Module init ───────────────────────────────────────────────────────────+/// Apply OpenAI `/v1/responses` live-zone compression to a request body.+///+/// Hot-fix entry point added 2026-05-06: re-enables `/v1/responses`+/// compression on the Python proxy after PR-C5 retired the Python+/// pipeline. PR-C5's "Rust handles it" claim assumed the standalone+/// `crates/headroom-proxy` binary would sit in front of Python; that+/// binary is not deployed by the CLI (`headroom proxy`,+/// `headroom wrap codex`). This binding lets the Python proxy call+/// the live-zone dispatcher inline so Codex `/v1/responses` traffic+/// is compressed end-to-end.+///+/// # Arguments+/// * `body` — raw request body bytes (post memory-injection).+/// * `auth_mode` — one of `"payg"`, `"oauth"`, `"subscription"`,+/// `"unknown"`. Currently unused by the dispatcher (the policy+/// gating is upstream); accepted for forward-compat.+/// * `model` — model name from the request body. Empty string defaults+/// to `headroom_core::transforms::live_zone::DEFAULT_MODEL`.+///+/// # Returns+/// `(body, modified)`:+/// * Modified: `(new_body_bytes, True)` — caller forwards the new bytes.+/// * Unchanged / passthrough: `(input_bytes, False)` — caller forwards+/// the original.+///+/// # Failure mode+/// Never raises. The dispatcher's `LiveZoneError` outcomes (body not+/// JSON, no `messages`/`input` array) are passthrough conditions, not+/// failures — matching the Rust proxy's+/// `compress_openai_responses_request` contract.+#[pyfunction]+#[pyo3(signature = (body, auth_mode = "payg", model = ""))]+fn compress_openai_responses_live_zone(+ py: Python<'_>,+ body: &[u8],+ auth_mode: &str,+ model: &str,+) -> (Py<PyBytes>, bool) {+ let mode = match auth_mode.to_ascii_lowercase().as_str() {+ "payg" => RustLiveZoneAuthMode::Payg,+ "oauth" => RustLiveZoneAuthMode::OAuth,+ "subscription" => RustLiveZoneAuthMode::Subscription,+ _ => RustLiveZoneAuthMode::Unknown,+ };+ let model_str = if model.is_empty() {+ headroom_core::transforms::live_zone::DEFAULT_MODEL+ } else {+ model+ };++ match rust_compress_openai_responses_live_zone(body, mode, model_str) {+ Ok(LiveZoneOutcome::NoChange { .. }) => (PyBytes::new_bound(py, body).unbind(), false),+ Ok(LiveZoneOutcome::Modified { new_body, .. }) => {+ // `RawValue::get` returns the underlying serialized JSON+ // as `&str`; bytes are valid UTF-8 by construction.+ let bytes = new_body.get().as_bytes();+ (PyBytes::new_bound(py, bytes).unbind(), true)+ }+ Err(_) => {+ // BodyNotJson / NoMessagesArray are non-fatal: nothing to+ // compress, fall through to passthrough byte-for-byte.+ (PyBytes::new_bound(py, body).unbind(), false)+ }+ }+}+#[pymodule]fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {m.add_function(wrap_pyfunction!(hello, m)?)?;@@ -1487,5 +1555,6 @@ fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {m.add_function(wrap_pyfunction!(score_line, m)?)?;m.add_function(wrap_pyfunction!(content_has_error_indicators, m)?)?;m.add_function(wrap_pyfunction!(keyword_registry_snapshot, m)?)?;+ m.add_function(wrap_pyfunction!(compress_openai_responses_live_zone, m)?)?;Ok(())}diff --git a/plugins/headroom-agent-hooks/.claude-plugin/plugin.json b/plugins/headroom-agent-hooks/.claude-plugin/plugin.jsonindex b45711d10..1ca7cfe16 100644--- a/plugins/headroom-agent-hooks/.claude-plugin/plugin.json+++ b/plugins/headroom-agent-hooks/.claude-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "headroom",- "version": "0.21.0",+ "version": "0.21.4","description": "Headroom startup hooks for Claude Code and GitHub Copilot CLI.","author": {"name": "Headroom Contributors",diff --git a/plugins/headroom-agent-hooks/.github/plugin/plugin.json b/plugins/headroom-agent-hooks/.github/plugin/plugin.jsonindex 9eafe1aa8..a33cddbd2 100644--- a/plugins/headroom-agent-hooks/.github/plugin/plugin.json+++ b/plugins/headroom-agent-hooks/.github/plugin/plugin.json@@ -1,6 +1,6 @@{"name": "headroom",- "version": "0.21.0",+ "version": "0.21.4","description": "Headroom startup hooks for Claude Code and GitHub Copilot CLI.","author": {"name": "Headroom Contributors",diff --git a/tests/test_responses_pyo3_compression.py b/tests/test_responses_pyo3_compression.pynew file mode 100644index 000000000..3e7dc4a4a--- /dev/null+++ b/tests/test_responses_pyo3_compression.py@@ -0,0 +1,129 @@+"""Hot-fix tests: PyO3 inline `/v1/responses` compression.++Re-enables compression after PR-C5 retired the Python pipeline. The+standalone Rust proxy binary (`crates/headroom-proxy`) was supposed to+handle this, but it's not deployed by the CLI today. This module+exposes a PyO3 binding so the Python proxy can call the live-zone+dispatcher in-process.++These tests pin:++1. The binding is exposed and callable.+2. Round-trip: a body with no eligible content passes through unchanged.+3. Round-trip: a body with a compressible function-call output gets compressed.+4. Errors are non-fatal: malformed JSON / missing input array → passthrough.+5. Auth-mode parsing accepts every variant the F1 classifier produces.+"""++from __future__ import annotations++import json++import pytest+++def _ensure_binding():+ """Skip if the Rust extension hasn't been built (mirrors existing pattern)."""+ try:+ from headroom._core import compress_openai_responses_live_zone++ return compress_openai_responses_live_zone+ except ImportError:+ pytest.skip("headroom._core not built — run scripts/build_rust_extension.sh")+++class TestBindingExposed:+ """The pyfunction is reachable from Python."""++ def test_callable(self):+ compress = _ensure_binding()+ assert callable(compress), "compress_openai_responses_live_zone must be callable"+++class TestPassthroughCases:+ """Bodies the dispatcher cannot compress should be returned byte-for-byte+ with `modified=False`. Matches the Rust proxy's `Outcome::Passthrough`+ contract."""++ def test_not_json_passthrough(self):+ compress = _ensure_binding()+ body = b"this is not JSON at all"+ out, modified = compress(body, "payg", "gpt-4o-mini")+ assert out == body+ assert modified is False++ def test_no_input_array_passthrough(self):+ compress = _ensure_binding()+ body = json.dumps({"model": "gpt-4o-mini"}).encode()+ out, modified = compress(body, "payg", "gpt-4o-mini")+ assert out == body+ assert modified is False++ def test_empty_input_array_passthrough(self):+ compress = _ensure_binding()+ body = json.dumps({"model": "gpt-4o-mini", "input": []}).encode()+ out, modified = compress(body, "payg", "gpt-4o-mini")+ assert out == body+ assert modified is False++ def test_no_eligible_items_passthrough(self):+ compress = _ensure_binding()+ # Single user message under the byte threshold — no compression+ # applies, but still valid input.+ body = json.dumps(+ {+ "model": "gpt-4o-mini",+ "input": [{"type": "message", "role": "user", "content": "hi"}],+ }+ ).encode()+ out, modified = compress(body, "payg", "gpt-4o-mini")+ assert modified is False+ # Body should be byte-equal (passthrough, not re-serialized).+ assert out == body+++class TestAuthModeAccepted:+ """Every F1 AuthMode value is accepted; unrecognised falls back to+ Unknown (does not raise)."""++ @pytest.mark.parametrize(+ "auth_mode",+ ["payg", "oauth", "subscription", "unknown", "", "garbage"],+ )+ def test_accepts(self, auth_mode):+ compress = _ensure_binding()+ body = json.dumps({"model": "gpt-4o-mini", "input": []}).encode()+ # Should not raise on any string input.+ out, modified = compress(body, auth_mode, "gpt-4o-mini")+ assert isinstance(out, bytes)+ assert modified is False+++class TestModelDefault:+ """Empty `model` defaults to `headroom_core`'s `DEFAULT_MODEL`."""++ def test_empty_model_uses_default(self):+ compress = _ensure_binding()+ body = json.dumps({"input": []}).encode()+ out, modified = compress(body, "payg", "")+ assert isinstance(out, bytes)+ assert modified is False+++class TestNoExceptionsLeak:+ """The binding's contract is `never raises` (matches the Rust proxy's+ `compress_openai_responses_request` passthrough-on-error semantics).+ Pin this so future maintainers don't accidentally introduce a+ raising path."""++ def test_garbage_bytes_no_raise(self):+ compress = _ensure_binding()+ out, modified = compress(b"\xff\xfe\x00\xff", "payg", "gpt-4o-mini")+ assert modified is False+ assert out == b"\xff\xfe\x00\xff"++ def test_empty_body_no_raise(self):+ compress = _ensure_binding()+ out, modified = compress(b"", "payg", "gpt-4o-mini")+ assert modified is False+ assert out == b""