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(subscription/copilot): preserve remaining=0 for exhausted quota (#1997)
Abhay Singhcbb775015e30
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 33e1817b1..ddcf906ce 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -105,6 +105,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0* **cache/semantic:** key entries by the full-context hash, not the trailing query text. `SemanticCache.put` stored each response under `sha256(query)[:16]` where `query` is only the last user message, and the exact-match branch of `get` returned the slot without checking the stored entry's `messages_hash`. Two requests that share a trailing message ("continue", "yes", "run the tests") but differ in earlier context therefore collided on one slot — the second overwrote the first, and the first's hash then resolved to the second's cached response (wrong data served). Entries are now keyed by `messages_hash` when present, and `get` verifies `entry.messages_hash` before returning.* **proxy/openai:** stop PRE_SEND from reintroducing `tools: []` after the direct #728 fix. The OpenAI request handler now mirrors the existing `tools or _original_tools is not None` body-write guard during PRE_SEND write-back, so providers that reject empty tool arrays no longer see a tools field when the client omitted it, while explicit client `tools: []` remains preserved ([#1983](https://github.com/headroomlabs-ai/headroom/issues/1983)).* **proxy/openai:** keep the exact Responses function name `terminal` resident during OpenAI tool-search deferral so cache-mode optimization stops forwarding `terminal.terminal` and triggering the reserved-namespace 400 on Codex Responses ([#1946](https://github.com/headroomlabs-ai/headroom/issues/1946)).+* **subscription/copilot:** show a fully-consumed Copilot quota as 100% used instead of unknown. `parse_copilot_quota` read `remaining = raw.get("remaining") or raw.get("quota_remaining")`, so a category reporting `remaining: 0` (quota fully spent) had that legitimate `0` treated as falsy and — with no `quota_remaining` alias in the real payload — collapsed to `None`. `CopilotQuotaCategory.used`/`used_percent` then returned `None`, so the dashboard rendered the exhausted category as `used: -` / 0% (green gauge) rather than `300/300` / 100%. Now uses an explicit `is None` check.* **proxy/gemini:** thread the savings-profile kwargs into the native Gemini/Vertex compression paths. `handle_gemini_generate_content`, `handle_google_cloudcode_stream`, and `handle_gemini_count_tokens` called `openai_pipeline.apply()` without `proxy_pipeline_kwargs(self.config)`, so `HEADROOM_SAVINGS_PROFILE` and the ProxyConfig knobs (`target_ratio`/`min_tokens_to_compress`/`protect_recent`/`max_items_after_crush`/...) were silently dropped on the Gemini path — those requests compressed with router defaults instead of the configured profile, diverging from the Claude/Codex/Cursor paths. This is the same fix #1534 made for the OpenAI chat path; it now covers Gemini too.* **wrap:** `headroom wrap claude` no longer installs RTK or lean-ctx by default. Claude context-tool setup is now explicit via `--context-tool`, `--no-context-tool` remains accepted, and other wrap commands keep their current defaults ([#1915](https://github.com/headroomlabs-ai/headroom/issues/1915)).* **proxy/openai:** thread the savings-profile kwargs into the live `/v1/chat/completions` compression path. The chat handler called `openai_pipeline.apply()` without `proxy_pipeline_kwargs(config)`, so `HEADROOM_SAVINGS_PROFILE=agent-90` (and the individual `compress_user_messages`/`target_ratio`/`min_tokens_to_compress`/... knobs) were silently dropped — OpenAI-compatible clients like OpenCode kept protecting user messages and missed the configured profile. Both the token-mode and non-token chat branches now pass the profile kwargs, matching `handlers/anthropic.py` and the dedicated OpenAI compress endpoint ([#1534](https://github.com/headroomlabs-ai/headroom/issues/1534)).diff --git a/headroom/subscription/copilot_quota.py b/headroom/subscription/copilot_quota.pyindex acdb29954..7a0cf9849 100644--- a/headroom/subscription/copilot_quota.py+++ b/headroom/subscription/copilot_quota.py@@ -163,7 +163,13 @@ def parse_copilot_quota(data: dict[str, Any]) -> CopilotQuotaSnapshot:raw = raw_qs.get(cat_name)if not raw:continue- remaining = raw.get("remaining") or raw.get("quota_remaining")+ # Use an explicit None check, not `or`: a fully-consumed category reports+ # `remaining: 0`, and `0 or raw.get("quota_remaining")` would discard that+ # legitimate 0 (→ None), so `used`/`used_percent` then render the exhausted+ # quota as unknown / 0% on the dashboard.+ remaining = raw.get("remaining")+ if remaining is None:+ remaining = raw.get("quota_remaining")cat = CopilotQuotaCategory(name=cat_name,entitlement=raw.get("entitlement"),diff --git a/tests/test_copilot_quota.py b/tests/test_copilot_quota.pyindex 09c97420b..94f7843e0 100644--- a/tests/test_copilot_quota.py+++ b/tests/test_copilot_quota.py@@ -149,6 +149,23 @@ class TestParseCopilotQuota:snap = parse_copilot_quota(data)assert snap.categories["chat"].remaining == 75+ def test_fully_exhausted_remaining_zero_is_preserved(self):+ """A fully-consumed category reports remaining: 0. That legitimate 0 must+ survive (not become None), so used/used_percent report 100% not unknown."""+ data = {+ "quota_snapshots": {+ "chat": {+ "entitlement": 300,+ "remaining": 0,+ }+ }+ }+ snap = parse_copilot_quota(data)+ chat = snap.categories["chat"]+ assert chat.remaining == 0+ assert chat.used == 300+ assert chat.used_percent == pytest.approx(100.0)+def test_unlimited_category(self):data = {"quota_snapshots": {"completions": {"unlimited": True}}}snap = parse_copilot_quota(data)