chore: sync current main for savings source cap re-review

This commit is contained in:
JerrettDavis
2026-11-05 11:22:18 -06:00
49 changed files with 1284 additions and 112 deletions
+4 -4
View File
@@ -5,14 +5,14 @@
},
"metadata": {
"description": "Headroom marketplace for Claude Code and GitHub Copilot CLI plugins.",
"version": "0.42.0"
"version": "0.43.0"
},
"plugins": [
{
"name": "headroom",
"source": "./plugins/headroom-agent-hooks",
"description": "Headroom startup hooks for Claude Code and GitHub Copilot CLI.",
"version": "0.42.0",
"version": "0.43.0",
"author": {
"name": "Headroom Contributors",
"url": "https://github.com/headroomlabs-ai/headroom"
@@ -30,7 +30,7 @@
"name": "headroom-skill",
"source": "./plugins/headroom-skill",
"description": "Skill: add headroom-ai compress() to a Python or TypeScript app to cut LLM input tokens (tool outputs, logs, JSON, chat history). No API key.",
"version": "0.42.0",
"version": "0.43.0",
"author": {
"name": "Headroom Contributors",
"url": "https://github.com/headroomlabs-ai/headroom"
@@ -52,7 +52,7 @@
"name": "headroom-snip",
"source": "./plugins/headroom-snip",
"description": "Claude Code mod: watch Headroom snip tokens off every request, with an animated band, a /headroom log and a running total.",
"version": "0.42.0",
"version": "0.43.0",
"author": {
"name": "Headroom Contributors",
"url": "https://github.com/headroomlabs-ai/headroom"
+3 -3
View File
@@ -5,14 +5,14 @@
},
"metadata": {
"description": "Headroom marketplace for Claude Code and GitHub Copilot CLI plugins.",
"version": "0.42.0"
"version": "0.43.0"
},
"plugins": [
{
"name": "headroom",
"source": "./plugins/headroom-agent-hooks",
"description": "Headroom startup hooks for Claude Code and GitHub Copilot CLI.",
"version": "0.42.0",
"version": "0.43.0",
"author": {
"name": "Headroom Contributors",
"url": "https://github.com/headroomlabs-ai/headroom"
@@ -30,7 +30,7 @@
"name": "headroom-skill",
"source": "./plugins/headroom-skill",
"description": "Skill: add headroom-ai compress() to a Python or TypeScript app to cut LLM input tokens (tool outputs, logs, JSON, chat history). No API key.",
"version": "0.42.0",
"version": "0.43.0",
"author": {
"name": "Headroom Contributors",
"url": "https://github.com/headroomlabs-ai/headroom"
+1 -1
View File
@@ -1,3 +1,3 @@
{
".": "0.42.0"
".": "0.43.0"
}
+6 -6
View File
@@ -1,10 +1,10 @@
{
"version": "0.42.0",
"version": "0.43.0",
"packages": {
"pypi": "0.42.0",
"npm-sdk": "0.42.0",
"npm-openclaw": "0.42.0",
"npm-opencode": "0.42.0",
"agent-hooks-plugin": "0.42.0"
"pypi": "0.43.0",
"npm-sdk": "0.43.0",
"npm-openclaw": "0.43.0",
"npm-opencode": "0.43.0",
"agent-hooks-plugin": "0.43.0"
}
}
+148
View File
@@ -284,6 +284,154 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **code:** fix two `CodeAwareCompressor` AST-reassembly bugs: an exported JS/TS function or class (`export function foo() {`) produced a duplicated `export export` keyword and invalid syntax, because line-based node slicing (used to preserve indentation) pulled in the preceding `export` sibling's text on top of the `export_statement` handler's own prefix reconstruction. Separately, in every supported language, a doc comment immediately above a top-level function, class, or type was detached from its declaration during extraction and re-emitted in a cluster at the end of the compressed output instead of staying attached to what it documents.
- * **proxy:** Buffered upstream responses containing a `server_tool_use` (or any other unrecognized Anthropic content block) no longer turn a fully-generated response into an HTTP 502. `StreamingMixin._response_to_sse` raised `ValueError` on unknown block types after the entire upstream generation had already been buffered, so a slow-but-successful response failed and the client retried the whole multi-minute request. Unknown blocks are now emitted verbatim in `content_block_start` (following the existing redacted_thinking` pattern), so `server_tool_use`, `server_tool_result`, `mcp_tool_use`, and future block types round-trip ([#1806](https://github.com/headroomlabs-ai/headroom/issues/1806)).
## [0.43.0](https://github.com/headroomlabs-ai/headroom/compare/v0.42.0...v0.43.0) (2026-11-05)
### ⚠ BREAKING CHANGES
* **mcp:** port MCP servers to SDK 2.x ([#4603](https://github.com/headroomlabs-ai/headroom/issues/4603))
### Features
* **ccr:** answer headroom_retrieve on streaming chat completions ([#4631](https://github.com/headroomlabs-ai/headroom/issues/4631)) ([31dcaf4](https://github.com/headroomlabs-ai/headroom/commit/31dcaf41f7fafaebec0838a05ff328da456faef0))
* **mcp:** port MCP servers to SDK 2.x ([#4603](https://github.com/headroomlabs-ai/headroom/issues/4603)) ([ee2252a](https://github.com/headroomlabs-ai/headroom/commit/ee2252a3155c87e948c8893b79af5824eb30e64c))
* **plugins:** headroom skill for Claude Code, Codex, Copilot and skills.sh ([#4533](https://github.com/headroomlabs-ai/headroom/issues/4533)) ([66e1467](https://github.com/headroomlabs-ai/headroom/commit/66e14676827366c2bf8d15e252f8766cb27b1a98))
* **savings:** show the proxy's model-routing savings in headroom savings ([#4304](https://github.com/headroomlabs-ai/headroom/issues/4304)) ([aaab338](https://github.com/headroomlabs-ai/headroom/commit/aaab338b64cb55929de904411db65db126e22ec0))
### Bug Fixes
* [BUG] A timeout in one image compression call resets the shared worker pool and affects concurrent requests ([#4528](https://github.com/headroomlabs-ai/headroom/issues/4528)) ([e9bcfe7](https://github.com/headroomlabs-ai/headroom/commit/e9bcfe73fadf69f0b27b7bc338ea7d2380931054))
* [BUG] DirectMem0Adapter.search_memories calls removed mem0 search API ([#4518](https://github.com/headroomlabs-ai/headroom/issues/4518)) ([d9cee45](https://github.com/headroomlabs-ai/headroom/commit/d9cee452681c79d6f13919e9e651e88a774c0874))
* [BUG] Saved request metrics lose their cache optimization fields ([#4584](https://github.com/headroomlabs-ai/headroom/issues/4584)) ([b4c9bdf](https://github.com/headroomlabs-ai/headroom/commit/b4c9bdf100d97fb038beba95de6d4ad6a8786090))
* [BUG] Volatile SQLite metrics stores lose their schema before the first save ([#4633](https://github.com/headroomlabs-ai/headroom/issues/4633)) ([d8ff0fe](https://github.com/headroomlabs-ai/headroom/commit/d8ff0fe9d2cb2477f6662a90f8fcd7e030a52666))
* Add info about apply to README.md ([#1592](https://github.com/headroomlabs-ai/headroom/issues/1592)) ([4d34680](https://github.com/headroomlabs-ai/headroom/commit/4d34680c34ad379d693820869e77d4a2a6be2cdf))
* **backends/litellm:** keep cache_control on text blocks for Bedrock prompt caching ([#4565](https://github.com/headroomlabs-ai/headroom/issues/4565)) ([23a5bb1](https://github.com/headroomlabs-ai/headroom/commit/23a5bb1c81dc943398f9271f068a02487207f2e7))
* **backends:** forward the standard service_tier param (litellm + anyllm) ([#2468](https://github.com/headroomlabs-ai/headroom/issues/2468)) ([62cc38a](https://github.com/headroomlabs-ai/headroom/commit/62cc38a6b09dbcf723681a0ff56acd02131ad1f1))
* **cache:** build semantic entry before evicting so a failed embedding keeps live entries ([#4581](https://github.com/headroomlabs-ai/headroom/issues/4581)) ([0d4c450](https://github.com/headroomlabs-ai/headroom/commit/0d4c450d774691568b850f0d22ce17e087405281))
* **cache:** count a tool_result with no text as replayable in the frozen prefix ([#4523](https://github.com/headroomlabs-ai/headroom/issues/4523)) ([fba09f6](https://github.com/headroomlabs-ai/headroom/commit/fba09f674c8a9c91b39a563a70ab7727b44d1dbc))
* **cache:** fail closed on SQLite corruption instead of deleting the store online ([#4361](https://github.com/headroomlabs-ai/headroom/issues/4361)) ([9801833](https://github.com/headroomlabs-ai/headroom/commit/98018333f997783487af15efe4d9e631ecd85939))
* **cache:** invalidate configured registry instances on replacement ([#4363](https://github.com/headroomlabs-ai/headroom/issues/4363)) ([1bcdca2](https://github.com/headroomlabs-ai/headroom/commit/1bcdca2d9dd279f49aa4276d523d93c2cfeb5a51))
* **cache:** namespace SemanticCacheLayer by provider, model and metadata ([#4473](https://github.com/headroomlabs-ai/headroom/issues/4473)) ([99cdaaf](https://github.com/headroomlabs-ai/headroom/commit/99cdaaf1c7de2bd81a8e64f198462fd1ceec3a17))
* **cache:** preserve OpenAI tool block metadata on cached replay ([#4334](https://github.com/headroomlabs-ai/headroom/issues/4334)) ([f1035ac](https://github.com/headroomlabs-ai/headroom/commit/f1035ac73788a577fb5495a59eed0fc925d42782))
* **cache:** scope router results to request compression policy ([#4590](https://github.com/headroomlabs-ai/headroom/issues/4590)) ([5d40fe7](https://github.com/headroomlabs-ai/headroom/commit/5d40fe76471b12ffe2a5c81309d9df617ca60b41))
* **ccr:** buffer a stream only for a marker new this turn ([#4607](https://github.com/headroomlabs-ai/headroom/issues/4607)) ([946a70c](https://github.com/headroomlabs-ai/headroom/commit/946a70cfcbdc522f940b01f0aeb2b7a214f54cb5))
* **ccr:** keep proactive expansion out of the cached prefix ([#4300](https://github.com/headroomlabs-ai/headroom/issues/4300)) ([b1f340e](https://github.com/headroomlabs-ai/headroom/commit/b1f340e0b35d4659d7ebc993d9dcc08057da93e9))
* **cli:** document that code-aware compression is enabled by default ([#4616](https://github.com/headroomlabs-ai/headroom/issues/4616)) ([affd17c](https://github.com/headroomlabs-ai/headroom/commit/affd17c5fde174ad78287eef1696251e18363722))
* **client:** close upstream responses when wrapped streams are closed ([#4482](https://github.com/headroomlabs-ai/headroom/issues/4482)) ([3d8dcf4](https://github.com/headroomlabs-ai/headroom/commit/3d8dcf476c148f633e66eb98c478095adfd9354b))
* **client:** honor configured cache optimizer enablement ([#4501](https://github.com/headroomlabs-ai/headroom/issues/4501)) ([d4c0810](https://github.com/headroomlabs-ai/headroom/commit/d4c0810ea3ebdd7e49e5d29f4e0dfd2da1511b02))
* **client:** preserve Anthropic create stream flags ([#4500](https://github.com/headroomlabs-ai/headroom/issues/4500)) ([3910caa](https://github.com/headroomlabs-ai/headroom/commit/3910caabd68311ae99d5659aa2d470eb2c15ea0e))
* **codex:** accept multi-line inline tables in Codex config.toml ([#4436](https://github.com/headroomlabs-ai/headroom/issues/4436)) ([4a851ed](https://github.com/headroomlabs-ai/headroom/commit/4a851ed25469a51de00ccd00d14d0a6ab62a0866))
* **compaction:** reject negative dictionary indices and non-array legends in expand_compacted ([#4470](https://github.com/headroomlabs-ai/headroom/issues/4470)) ([3c1fcbd](https://github.com/headroomlabs-ai/headroom/commit/3c1fcbdfde31069795b03faf9f43fdd73e6941d9))
* **core:** case-insensitive Bearer scheme, clamp SQLite CCR TTLs, redact the Redis URL in logs ([#4561](https://github.com/headroomlabs-ai/headroom/issues/4561)) ([9e8cc64](https://github.com/headroomlabs-ai/headroom/commit/9e8cc64b48ebd4e2fb31332f261464d3bd0550b7))
* **dedup:** bound line-number digit run and never fold into a longer pointer ([#4488](https://github.com/headroomlabs-ai/headroom/issues/4488)) ([21e8867](https://github.com/headroomlabs-ai/headroom/commit/21e8867504df57c8f99aa30bd889e7eed01cd49e))
* **deps:** clear the glob and minimatch npm audit findings ([#4599](https://github.com/headroomlabs-ai/headroom/issues/4599)) ([2e49e53](https://github.com/headroomlabs-ai/headroom/commit/2e49e5304b245fdaca0d2640bb1a59cb1ffa63fc))
* **diff:** keep combined-diff changes made against a later parent ([#4318](https://github.com/headroomlabs-ai/headroom/issues/4318)) ([4b788fd](https://github.com/headroomlabs-ai/headroom/commit/4b788fd04d407cea0113c74f979f7cc3072a0132))
* **docker:** install the redis extra in every image ([#4514](https://github.com/headroomlabs-ai/headroom/issues/4514)) ([8d5dde7](https://github.com/headroomlabs-ai/headroom/commit/8d5dde77656c9bb3daa354a14ffa6904cb8311aa))
* **docker:** pass RUNTIME_HOME=/root for root bake targets ([#4445](https://github.com/headroomlabs-ai/headroom/issues/4445)) ([670767e](https://github.com/headroomlabs-ai/headroom/commit/670767eeb49f224a7a0bcbcc31b3584a35110b7d))
* **doctor:** detect root openai_base_url Codex routing and agent-configured proxy ports ([#4490](https://github.com/headroomlabs-ai/headroom/issues/4490)) ([222f2d1](https://github.com/headroomlabs-ai/headroom/commit/222f2d1df3c6296462bc38c871cd29c221af55bc))
* **evals:** reject changed numeric probe values ([#4502](https://github.com/headroomlabs-ai/headroom/issues/4502)) ([1fe8549](https://github.com/headroomlabs-ai/headroom/commit/1fe8549f9ca77a2bc1280e4319980e0b9b31f5b9))
* **install:** stabilize Windows task lifecycle and eliminate console window popup ([#4627](https://github.com/headroomlabs-ai/headroom/issues/4627)) ([6b903cb](https://github.com/headroomlabs-ai/headroom/commit/6b903cbc81c13f417485f059aa6ef7aca6b0c4ec))
* **install:** tolerate absent Windows tasks in stop_supervisor ([#4465](https://github.com/headroomlabs-ai/headroom/issues/4465)) ([#4468](https://github.com/headroomlabs-ai/headroom/issues/4468)) ([50af2ff](https://github.com/headroomlabs-ai/headroom/commit/50af2ff7a81efb89d6752374a811be4fa2eec6f6))
* **kompress:** keep words past the tokenizer's truncation point ([#4328](https://github.com/headroomlabs-ai/headroom/issues/4328)) ([bb13384](https://github.com/headroomlabs-ai/headroom/commit/bb13384829069e7e0559fccf77620891e0c90be0))
* **kompress:** read the pinned snapshot from Python's one HF cache in the native proxy ([#4438](https://github.com/headroomlabs-ai/headroom/issues/4438)) ([eef44f6](https://github.com/headroomlabs-ai/headroom/commit/eef44f60cc56118a0de98455012cc18ff3eaafbd))
* **langgraph:** replace the compressed ToolMessage in graph state ([#4620](https://github.com/headroomlabs-ai/headroom/issues/4620)) ([70a8f09](https://github.com/headroomlabs-ai/headroom/commit/70a8f0979e585aa4e9b6f663fa7528340fb4efd6))
* **mcp:** confine headroom_read to the workspace (GHSA-8g49-pg6g-xgrf) ([#4525](https://github.com/headroomlabs-ai/headroom/issues/4525)) ([8ab2e91](https://github.com/headroomlabs-ai/headroom/commit/8ab2e914687266f5b602f6d3a78f13026fd5ef88))
* **memory:** bound verb-pattern word runs to stop quadratic backtracking in bridge import ([#4370](https://github.com/headroomlabs-ai/headroom/issues/4370)) ([#4376](https://github.com/headroomlabs-ai/headroom/issues/4376)) ([d232d09](https://github.com/headroomlabs-ai/headroom/commit/d232d097cf33d92f3a5112c14a306ba4a80330ee))
* **memory:** fail close unresolved project operations ([#2637](https://github.com/headroomlabs-ai/headroom/issues/2637)) ([957ab18](https://github.com/headroomlabs-ai/headroom/commit/957ab1830d73a39ba7662ee9b01dd22c7abd403e))
* **memory:** keep learned evidence until persistence succeeds ([#4229](https://github.com/headroomlabs-ai/headroom/issues/4229)) ([662a7f6](https://github.com/headroomlabs-ai/headroom/commit/662a7f6fd836ca87db5f2093c6487b500e8da2eb))
* **memory:** preserve parent scopes when clearing memories ([#4305](https://github.com/headroomlabs-ai/headroom/issues/4305)) ([a8375a1](https://github.com/headroomlabs-ai/headroom/commit/a8375a1d26c5fc6dd619bfa7a50a15bbba18acf3))
* **mixed-content:** do not carve ISO-timestamped log rows as search results ([#4463](https://github.com/headroomlabs-ai/headroom/issues/4463)) ([abbade9](https://github.com/headroomlabs-ai/headroom/commit/abbade9b4c761a82f711d0687703d0409fbe6edc))
* **offline:** guard the learn and memory-eval LLM calls under HEADROOM_OFFLINE ([#4344](https://github.com/headroomlabs-ai/headroom/issues/4344)) ([dd361bb](https://github.com/headroomlabs-ai/headroom/commit/dd361bb7a3a1d3f2e3c7572bf544ea4ea895b0a5))
* **openclaw/release:** ship responsive npm integrations ([#1458](https://github.com/headroomlabs-ai/headroom/issues/1458)) ([33f2044](https://github.com/headroomlabs-ai/headroom/commit/33f204406cc670588b9a873678b74b4af6ca3620))
* **openclaw:** record turn advancements as one marker file per key ([#4212](https://github.com/headroomlabs-ai/headroom/issues/4212)) ([24acbe8](https://github.com/headroomlabs-ai/headroom/commit/24acbe8f32b6d49e3f61097398330e7529729d3e))
* **opencode:** only route POST requests to LLM endpoint paths ([#4464](https://github.com/headroomlabs-ai/headroom/issues/4464)) ([336d462](https://github.com/headroomlabs-ai/headroom/commit/336d46225370c946ad8420ecb3f6bac81193e2f7))
* **opencode:** route Copilot Claude models through the proxy ([#4594](https://github.com/headroomlabs-ai/headroom/issues/4594)) ([3d9a6f2](https://github.com/headroomlabs-ai/headroom/commit/3d9a6f2cf7fefa2cd0d347521e0050d604a50d22))
* **output-savings:** rewrite a pre-rule ledger once instead of warning on every /stats poll ([#4196](https://github.com/headroomlabs-ai/headroom/issues/4196)) ([96e81ac](https://github.com/headroomlabs-ai/headroom/commit/96e81ac6703dcfcf4535f989319d677775e2384d))
* **packaging:** rename the sandbox extra to lean and correct SECURITY.md claims ([#3727](https://github.com/headroomlabs-ai/headroom/issues/3727)) ([09b7e8c](https://github.com/headroomlabs-ai/headroom/commit/09b7e8c1e41779b4c22f1a4ec941b59a61e621f1))
* **packaging:** use direct CLI module entry point ([#2665](https://github.com/headroomlabs-ai/headroom/issues/2665)) ([2892214](https://github.com/headroomlabs-ai/headroom/commit/28922148816b2b6ebf192abfcd6e65db8bb71511))
* **pipeline:** store the pipeline input in CCR behind offload markers ([#4576](https://github.com/headroomlabs-ai/headroom/issues/4576)) ([4d7765c](https://github.com/headroomlabs-ai/headroom/commit/4d7765c84b06d1a8630faa32a897a39a40261cd1))
* **pricing:** add Claude Haiku 5.5 rates, 1M context and its 100K long-prompt card ([#4037](https://github.com/headroomlabs-ai/headroom/issues/4037)) ([703755e](https://github.com/headroomlabs-ai/headroom/commit/703755e873d3af74b89d25792c7c9869322b0467))
* **privacy:** turn off Mem0's PostHog telemetry by default ([#4619](https://github.com/headroomlabs-ai/headroom/issues/4619)) ([fe69f38](https://github.com/headroomlabs-ai/headroom/commit/fe69f381168035ea6527bb7b908d11eeeb01838f))
* **providers:** isolate resolved Anthropic context limits ([#4593](https://github.com/headroomlabs-ai/headroom/issues/4593)) ([51258ef](https://github.com/headroomlabs-ai/headroom/commit/51258ef14a987b2c3137027e0c90f586eb290462))
* **proxy/memory:** harden _extract_tool_calls against malformed choices and blocks ([#2483](https://github.com/headroomlabs-ai/headroom/issues/2483)) ([effa5a5](https://github.com/headroomlabs-ai/headroom/commit/effa5a5886be7e3c5bc553b86da5b506046d461f))
* **proxy:** allow loopback-published container hosts to access /debug/memory ([#4259](https://github.com/headroomlabs-ai/headroom/issues/4259)) ([e546e71](https://github.com/headroomlabs-ai/headroom/commit/e546e71aeb32c355584d37abb61e8d587efce0c3))
* **proxy:** answer /api/hello probes behind the token gate, log rejection context ([#4150](https://github.com/headroomlabs-ai/headroom/issues/4150)) ([673831a](https://github.com/headroomlabs-ai/headroom/commit/673831a456ef0f551de8dc3b2cbb0deae3861c65))
* **proxy:** cap cache_control breakpoints on OpenAI chat path ([#4471](https://github.com/headroomlabs-ai/headroom/issues/4471)) ([#4491](https://github.com/headroomlabs-ai/headroom/issues/4491)) ([55602fc](https://github.com/headroomlabs-ai/headroom/commit/55602fc1e207e7101917e45bf6dba30ce544558c))
* **proxy:** do not book Gemini countTokens as a request outcome ([#4412](https://github.com/headroomlabs-ai/headroom/issues/4412)) ([4d2dd62](https://github.com/headroomlabs-ai/headroom/commit/4d2dd62b7c61724497b4bde33ef8685717f6a682))
* **proxy:** don't fail startup when libc_ver() can't read the interpreter ([#4519](https://github.com/headroomlabs-ai/headroom/issues/4519)) ([28172a9](https://github.com/headroomlabs-ai/headroom/commit/28172a921f4e3945aa43061a9e5cdd0ffd066c0e))
* **proxy:** forward Copilot accounting headers on streaming path ([#4431](https://github.com/headroomlabs-ai/headroom/issues/4431)) ([66023a0](https://github.com/headroomlabs-ai/headroom/commit/66023a0187b3260747ece4e99e6932098805442a))
* **proxy:** handle ClientDisconnect in anthropic_messages ([#3532](https://github.com/headroomlabs-ai/headroom/issues/3532)) ([251d149](https://github.com/headroomlabs-ai/headroom/commit/251d149a13ae482c1a0843d899b0321e175ac66d))
* **proxy:** honor x-headroom-base-url on provider passthrough routes ([#4596](https://github.com/headroomlabs-ai/headroom/issues/4596)) ([5c8772b](https://github.com/headroomlabs-ai/headroom/commit/5c8772b7210caeb291a202a58d4b5e88d51ff35b))
* **proxy:** install SOCKS support for websocket proxies ([#3357](https://github.com/headroomlabs-ai/headroom/issues/3357)) ([79181ca](https://github.com/headroomlabs-ai/headroom/commit/79181ca0fe35798b25104ff93b2c3a6c38baa72c))
* **proxy:** keep Gemini memory context on entries with non-text parts ([#4506](https://github.com/headroomlabs-ai/headroom/issues/4506)) ([35a2b76](https://github.com/headroomlabs-ai/headroom/commit/35a2b763f09bb147888aeeec69f72d1ed3ae872b))
* **proxy:** keep health probes and the Codex attestation out of the proxy log ([#4611](https://github.com/headroomlabs-ai/headroom/issues/4611)) ([78736a1](https://github.com/headroomlabs-ai/headroom/commit/78736a16bc46ee0bd86b7aae8ef04f7f9cfa9d6d))
* **proxy:** keep read maturation out of the provider-confirmed prefix ([#4522](https://github.com/headroomlabs-ai/headroom/issues/4522)) ([526e06b](https://github.com/headroomlabs-ai/headroom/commit/526e06b7e0120820e5b1be44b4ded70698bb86c8))
* **proxy:** keep tool deferral on Copilot Business/Enterprise/Individual hosts ([#4608](https://github.com/headroomlabs-ai/headroom/issues/4608)) ([be456d4](https://github.com/headroomlabs-ai/headroom/commit/be456d4e3f46c7f4dae725c2dd150324000c724f))
* **proxy:** log unknown model ids as one bounded token; skip unparseable PERF lines ([#4374](https://github.com/headroomlabs-ai/headroom/issues/4374)) ([c014bd6](https://github.com/headroomlabs-ai/headroom/commit/c014bd6311742e35c86ef64adfbec88ab264ef28))
* **proxy:** preserve response cache keys across in-place hooks ([#4457](https://github.com/headroomlabs-ai/headroom/issues/4457)) ([03590cd](https://github.com/headroomlabs-ai/headroom/commit/03590cd61e17ad1ef313e38a35932f729ff0c871))
* **proxy:** recognise every local spelling of the proxy in the cc-switch loop guard ([#4405](https://github.com/headroomlabs-ai/headroom/issues/4405)) ([d9958bf](https://github.com/headroomlabs-ai/headroom/commit/d9958bfdff047ff2a7ef26f9e29fc7263f790346))
* **proxy:** reconcile Chat content-block cache markers after prefix replay ([#4333](https://github.com/headroomlabs-ai/headroom/issues/4333)) ([35ca9b8](https://github.com/headroomlabs-ai/headroom/commit/35ca9b835616f7923eab23e99e9561a8da452f9f))
* **proxy:** route /v1/models to the x-headroom-base-url upstream ([#4308](https://github.com/headroomlabs-ai/headroom/issues/4308)) ([6ef52c2](https://github.com/headroomlabs-ai/headroom/commit/6ef52c2047688e41c7903a0331149a915f20d6b3))
* **proxy:** skip memory-context search in cache mode on OpenAI routes ([#4632](https://github.com/headroomlabs-ai/headroom/issues/4632)) ([365f6fe](https://github.com/headroomlabs-ai/headroom/commit/365f6fe5233c4e102584ee4c3e7a140aeccb6a75))
* **proxy:** skip memory-context search on OpenAI paths in cache mode ([#4634](https://github.com/headroomlabs-ai/headroom/issues/4634)) ([3654037](https://github.com/headroomlabs-ai/headroom/commit/36540379e0dee0c071e3a3ff00bea9b16eb3902a))
* **proxy:** skip the memory-context search in cache mode ([#4606](https://github.com/headroomlabs-ai/headroom/issues/4606)) ([053925b](https://github.com/headroomlabs-ai/headroom/commit/053925b03c6cae028731634f0ad3557518f931d5))
* **proxy:** strip only one leading space from SSE field values ([#4306](https://github.com/headroomlabs-ai/headroom/issues/4306)) ([9226eaf](https://github.com/headroomlabs-ai/headroom/commit/9226eaf3852f6d7462ebb95e6d09af3b33eb53c3))
* **proxy:** strip upstream transport framing headers from Gemini response replays ([#4605](https://github.com/headroomlabs-ai/headroom/issues/4605)) ([dfcac78](https://github.com/headroomlabs-ai/headroom/commit/dfcac78119b7038b2e20ad38cead2f4e7fe60c43))
* **read-lifecycle:** keep Reads fresh after a failed Edit or Write ([#4461](https://github.com/headroomlabs-ai/headroom/issues/4461)) ([4c3799f](https://github.com/headroomlabs-ai/headroom/commit/4c3799fd81567cd516cdc5c1463f23cea99d6594))
* **relevance:** cap embedding batch size so ORT's arena stays small ([#4507](https://github.com/headroomlabs-ai/headroom/issues/4507)) ([a2d4fe7](https://github.com/headroomlabs-ai/headroom/commit/a2d4fe72381e186651466ee94e92ce9d8d1396cc))
* **relevance:** hand the embedding arena back after each run on macOS ([#4567](https://github.com/headroomlabs-ai/headroom/issues/4567)) ([2363f83](https://github.com/headroomlabs-ai/headroom/commit/2363f833092052227a9b98a9e21a0b1a69647eab))
* **relevance:** load the Rust default embedder from Python's pinned snapshot ([#4190](https://github.com/headroomlabs-ai/headroom/issues/4190)) ([9bb4b13](https://github.com/headroomlabs-ai/headroom/commit/9bb4b130ff87a2914a8f74dbcc6b49b49c3c87a8))
* **router:** keep small shell results verbatim ([#3590](https://github.com/headroomlabs-ai/headroom/issues/3590)) ([#4426](https://github.com/headroomlabs-ai/headroom/issues/4426)) ([4ee6164](https://github.com/headroomlabs-ai/headroom/commit/4ee616453fcebed5ab7ce847e1da1016c827a4ed))
* **router:** preserve read protection for quiet sed options ([#4521](https://github.com/headroomlabs-ai/headroom/issues/4521)) ([312ffec](https://github.com/headroomlabs-ai/headroom/commit/312ffec8ead5c8c93b8f4c794ca7f588a7b84bd2))
* **router:** validate lossless provider output on the excluded-tool path ([#4486](https://github.com/headroomlabs-ai/headroom/issues/4486)) ([9f068a6](https://github.com/headroomlabs-ai/headroom/commit/9f068a6c135730c1bd4f1d800083f9318a3067a4))
* **scripts:** resolve hooks dir via git so install-git-hooks works in linked worktrees ([#4176](https://github.com/headroomlabs-ai/headroom/issues/4176)) ([0f4275f](https://github.com/headroomlabs-ai/headroom/commit/0f4275f8b331f3db14c41d958c85180a3c70a318))
* **sdk:** carry Vercel AI SDK reasoning parts and provider options through compression ([#4564](https://github.com/headroomlabs-ai/headroom/issues/4564)) ([a26faae](https://github.com/headroomlabs-ai/headroom/commit/a26faaeee34ecfde57608aea4a1ec7ea2b33272f))
* **sdk:** send Anthropic messages to /v1/compress natively so thinking blocks survive ([#4489](https://github.com/headroomlabs-ai/headroom/issues/4489)) ([cd5e2b9](https://github.com/headroomlabs-ai/headroom/commit/cd5e2b9e73eb8bd783ab872bdda71263b941e2d3))
* **security:** lstat before narrowing sqlite files ([#4527](https://github.com/headroomlabs-ai/headroom/issues/4527)) ([2cf64ef](https://github.com/headroomlabs-ai/headroom/commit/2cf64ef0cc918c7cdb3de98d56f48fdc9c4c2b4b))
* **smart-crusher:** preserve unchanged JSON bytes ([#4044](https://github.com/headroomlabs-ai/headroom/issues/4044)) ([93279f3](https://github.com/headroomlabs-ai/headroom/commit/93279f3208b1afffdbba87c2fdecbeea246d5234))
* **tabular:** keep empty records and separator-like Markdown body rows ([#4582](https://github.com/headroomlabs-ai/headroom/issues/4582)) ([5cebb90](https://github.com/headroomlabs-ai/headroom/commit/5cebb90db1f179b00eafdf79568efbc98c8ef366))
* **tabular:** keep interior blank CSV rows instead of dropping them ([#4563](https://github.com/headroomlabs-ai/headroom/issues/4563)) ([8c360ca](https://github.com/headroomlabs-ai/headroom/commit/8c360ca69fd3fc9cd6bc1e4b02109c1e5595385c))
* **thinking:** isolate compaction memo by compressor instance ([#4569](https://github.com/headroomlabs-ai/headroom/issues/4569)) ([5dcb1e4](https://github.com/headroomlabs-ai/headroom/commit/5dcb1e4adec4f6e82d943488d8aecb8967753e4e))
* **thinking:** leave a thinking block with a null or non-string value unchanged ([#4562](https://github.com/headroomlabs-ai/headroom/issues/4562)) ([cc1eb54](https://github.com/headroomlabs-ai/headroom/commit/cc1eb5402aade58204b96b6b4b4d280cdd5456d2))
* **toin:** preserve stores that exceed the byte limit ([#4589](https://github.com/headroomlabs-ai/headroom/issues/4589)) ([f9cae66](https://github.com/headroomlabs-ai/headroom/commit/f9cae66b6b19d6e6acfa4bf6da031642c256404a))
* **toin:** retain pending changes after filesystem save failures ([#4568](https://github.com/headroomlabs-ai/headroom/issues/4568)) ([8b15a15](https://github.com/headroomlabs-ai/headroom/commit/8b15a15799898d8c0a396ca0d39f951be4e0cfcf))
* **toin:** skip a malformed pattern instead of aborting the store load ([#4484](https://github.com/headroomlabs-ai/headroom/issues/4484)) ([8b628d9](https://github.com/headroomlabs-ai/headroom/commit/8b628d968574053b26849b21e5cea2e2593691c5))
* **tokenizers:** coerce structured text values in message token counters ([#4472](https://github.com/headroomlabs-ai/headroom/issues/4472)) ([6cc27a4](https://github.com/headroomlabs-ai/headroom/commit/6cc27a453d9cf69c76dc8134eb6739891471716e))
* **tokenizers:** don't cache token-count inputs larger than max_chars ([#4469](https://github.com/headroomlabs-ai/headroom/issues/4469)) ([aebb729](https://github.com/headroomlabs-ai/headroom/commit/aebb729ef3f03bee8852d067c365f075957638f9))
* **tokenizers:** price Anthropic document blocks by page count, not as ([fc1ba89](https://github.com/headroomlabs-ai/headroom/commit/fc1ba891bff2d1e0cb53c4ffd4037f5066988c8e))
* **tokenizers:** price Anthropic document blocks by page count, not as base64 text ([#4441](https://github.com/headroomlabs-ai/headroom/issues/4441)) ([fc1ba89](https://github.com/headroomlabs-ai/headroom/commit/fc1ba891bff2d1e0cb53c4ffd4037f5066988c8e))
* **tokenizers:** resolve tiktoken encoding for gateway-prefixed model ids ([#4637](https://github.com/headroomlabs-ai/headroom/issues/4637)) ([e9331b6](https://github.com/headroomlabs-ai/headroom/commit/e9331b6b963b5b6554f45943abed2366aaa25ea6))
* **transforms:** enforce role, mutable, and cache zone guards in batch compression ([#4480](https://github.com/headroomlabs-ai/headroom/issues/4480)) ([#4494](https://github.com/headroomlabs-ai/headroom/issues/4494)) ([40cd7e7](https://github.com/headroomlabs-ai/headroom/commit/40cd7e7503c66a903ac88e6a090aa0bdac7b315d))
* **transforms:** honour Subscription toin_read_only when the router calls SmartCrusher ([#4493](https://github.com/headroomlabs-ai/headroom/issues/4493)) ([7c11d46](https://github.com/headroomlabs-ai/headroom/commit/7c11d462e6d8265a2cfdb8a1cf62026fb4d1f401))
* **transforms:** preserve YAML block scalar data during comment elision ([#4498](https://github.com/headroomlabs-ai/headroom/issues/4498)) ([534cec0](https://github.com/headroomlabs-ai/headroom/commit/534cec0e14db48e787ada13535e0763fc7c52e8e))
* **transforms:** recognise code fences whose info string is not a bare word ([#4291](https://github.com/headroomlabs-ai/headroom/issues/4291)) ([a8df6f9](https://github.com/headroomlabs-ai/headroom/commit/a8df6f919f1c4730971fc2481fd18853ddcfe6b9))
* **transforms:** stop an embedded markdown table from dropping the rest of a tool result ([#4216](https://github.com/headroomlabs-ai/headroom/issues/4216)) ([b66c9c7](https://github.com/headroomlabs-ai/headroom/commit/b66c9c7db6fb7314c366fd2c28866d0ab688f211))
* **transforms:** tolerate malformed assistant tool calls in ContentRouter.apply ([#4459](https://github.com/headroomlabs-ai/headroom/issues/4459)) ([cddd2fd](https://github.com/headroomlabs-ai/headroom/commit/cddd2fd60ed711d7fc1df75b1d5e1cac947ae6c8))
* **update-check:** skip releases with no files and do not fall back to an uninstallable version ([#4560](https://github.com/headroomlabs-ai/headroom/issues/4560)) ([c1dae5a](https://github.com/headroomlabs-ai/headroom/commit/c1dae5adae2b93871314143bba4cb03b7b971e05))
* use all user text blocks for relevance scoring ([#4628](https://github.com/headroomlabs-ai/headroom/issues/4628)) ([12dca26](https://github.com/headroomlabs-ai/headroom/commit/12dca26aa11d99b3af6c6d68cdf96ff90ef415aa))
* **wrap:** overwrite drifted selfheal hook command instead of skipping it ([#2723](https://github.com/headroomlabs-ai/headroom/issues/2723)) ([2643f34](https://github.com/headroomlabs-ai/headroom/commit/2643f34a542d944ba3ad4a0a574b5e9e9d9bd474))
* **wrap:** route OMP OpenAI providers through Headroom ([#2755](https://github.com/headroomlabs-ai/headroom/issues/2755)) ([fae1e84](https://github.com/headroomlabs-ai/headroom/commit/fae1e849cb59c85cfbd46e76a05e6c299e446b02))
### Dependencies
* Bump @anthropic-ai/sdk from 0.133.0 to 1.3.0 in /sdk/typescript ([#4554](https://github.com/headroomlabs-ai/headroom/issues/4554)) ([74277d4](https://github.com/headroomlabs-ai/headroom/commit/74277d47ecfbca349ddb99f81094c4dd30539d43))
* Bump lru from 0.18.5 to 0.19.0 ([#4382](https://github.com/headroomlabs-ai/headroom/issues/4382)) ([038af59](https://github.com/headroomlabs-ai/headroom/commit/038af599e1a3c5e7e6b3047781aaf9ba2bbbf8bd))
* bump ruff from 0.17.0 to 0.17.2 in the pip-minor-patch group across 1 directory ([#4380](https://github.com/headroomlabs-ai/headroom/issues/4380)) ([64f43bd](https://github.com/headroomlabs-ai/headroom/commit/64f43bdf8f2080d5d7c1874fe1572acbb05872b6))
* bump ruff from 0.17.2 to 0.17.3 in the pip-minor-patch group ([#4652](https://github.com/headroomlabs-ai/headroom/issues/4652)) ([cb9602a](https://github.com/headroomlabs-ai/headroom/commit/cb9602aef0e4914f723a33ef6bad3a7c06eb485a))
* Bump the cargo-minor-patch group across 1 directory with 12 updates ([#4553](https://github.com/headroomlabs-ai/headroom/issues/4553)) ([b9049d7](https://github.com/headroomlabs-ai/headroom/commit/b9049d703af340ff90ed867f86e5fafa10c155c9))
* Bump the npm-minor-patch group across 1 directory with 6 updates ([#4621](https://github.com/headroomlabs-ai/headroom/issues/4621)) ([5e4827d](https://github.com/headroomlabs-ai/headroom/commit/5e4827de12c9fdf33d7d36b1939eee73a18fe6c5))
* Bump the npm-minor-patch group across 2 directories with 14 updates ([#4455](https://github.com/headroomlabs-ai/headroom/issues/4455)) ([f10725e](https://github.com/headroomlabs-ai/headroom/commit/f10725e6b9940d8bc2fcde950c86f8ac97543cb6))
* Bump the npm-minor-patch group across 3 directories with 9 updates ([#4495](https://github.com/headroomlabs-ai/headroom/issues/4495)) ([f367728](https://github.com/headroomlabs-ai/headroom/commit/f36772805178e3fed1a95313e8211c57a291b953))
* Bump tsup from 8.5.1 to 9.0.0 in /plugins/openclaw ([#4384](https://github.com/headroomlabs-ai/headroom/issues/4384)) ([9d87151](https://github.com/headroomlabs-ai/headroom/commit/9d871516101ff5428b592acb213aeacd30afc328))
* Bump tsup from 8.5.1 to 9.0.0 in /plugins/opencode ([#4385](https://github.com/headroomlabs-ai/headroom/issues/4385)) ([99e223c](https://github.com/headroomlabs-ai/headroom/commit/99e223c0e2c2ea6eb88ce28b160ae179897a9c67))
* Bump tsup from 8.5.1 to 9.0.0 in /sdk/typescript ([#4386](https://github.com/headroomlabs-ai/headroom/issues/4386)) ([e666fd8](https://github.com/headroomlabs-ai/headroom/commit/e666fd8e35dbe7c0433739297873b6a0a51c26cf))
* qualify latest Anthropic SDK 1.4.0 in docs ([#4556](https://github.com/headroomlabs-ai/headroom/issues/4556)) ([65bcc4c](https://github.com/headroomlabs-ai/headroom/commit/65bcc4c313aadfb609973563573b8bb3f308d81f))
* qualify latest compatible docs and E2E tools ([#4580](https://github.com/headroomlabs-ai/headroom/issues/4580)) ([e42f74e](https://github.com/headroomlabs-ai/headroom/commit/e42f74ebc9a784014ad9799b75f862efe4bf69e3))
* require Python 3.11; clear Dependabot alerts in the 3.10 lock fork ([#4517](https://github.com/headroomlabs-ai/headroom/issues/4517)) ([3c6f82b](https://github.com/headroomlabs-ai/headroom/commit/3c6f82b06091206379106f8fe1a54f2400ed7a93))
* update tomlkit requirement from <1.0,>=0.15.0 to >=0.15.2,<1.0 ([#4653](https://github.com/headroomlabs-ai/headroom/issues/4653)) ([00d5a5c](https://github.com/headroomlabs-ai/headroom/commit/00d5a5ca619f6e23da54d8b7e3fa4cc7af444035))
## [0.42.0](https://github.com/headroomlabs-ai/headroom/compare/v0.41.0...v0.42.0) (2026-10-26)
@@ -10,6 +10,7 @@
//! workers (see `RUST_DEV.md` "Multi-worker deployment").
use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;
use std::time::{Duration, Instant};
@@ -33,11 +34,18 @@ use crate::ccr::{max_lifetime_for, CcrStore, DEFAULT_CAPACITY, DEFAULT_TTL};
/// small sweep.
pub struct InMemoryCcrStore {
map: DashMap<String, Entry>,
/// FIFO insertion order. Stale entries (already removed from `map`
/// via TTL expiry) are tolerated — `pop_front` + `map.remove` is a
/// no-op for missing keys, and capacity-bounded sweeps loop until
/// they actually evict a real entry.
order: Mutex<VecDeque<String>>,
/// FIFO insertion order as `(hash, seq)`. A queue item is live only
/// while `map[hash].seq == seq`: an entry removed by TTL expiry and
/// stored again later gets a new `seq`, so its old queue item can
/// neither evict the fresh entry nor pin it. Stale items are skipped
/// by eviction and dropped by `compact_order` once the queue grows
/// past twice the capacity.
order: Mutex<VecDeque<(String, u64)>>,
next_seq: AtomicU64,
/// Test-only pause point between inserting a fresh entry and
/// recording it in `order`, to reproduce writer interleavings.
#[cfg(test)]
after_insert_hook: Mutex<Option<Box<dyn FnOnce() + Send>>>,
ttl: Duration,
max_lifetime: Duration,
capacity: usize,
@@ -46,6 +54,7 @@ pub struct InMemoryCcrStore {
#[derive(Clone)]
struct Entry {
payload: String,
seq: u64,
inserted: Instant,
last_accessed: Instant,
}
@@ -77,25 +86,37 @@ impl InMemoryCcrStore {
Self {
map: DashMap::with_capacity(capacity),
order: Mutex::new(VecDeque::with_capacity(capacity)),
next_seq: AtomicU64::new(0),
#[cfg(test)]
after_insert_hook: Mutex::new(None),
ttl,
max_lifetime,
capacity,
}
}
/// Sweep the order queue, dropping leading entries that no longer
/// exist in the map (already expired or evicted), then evict
/// real entries until `map.len() < capacity`. Called only from
/// `put` on a fresh-key insert path.
fn evict_until_under_capacity(&self) {
let mut guard = self.order.lock().expect("ccr order mutex poisoned");
/// Evict real entries, oldest first, until `map.len() < capacity`.
/// The caller holds the `order` lock, which serializes every
/// fresh-key insert, so no entry can be in the map without its
/// queue item while this runs.
fn evict_until_under_capacity(&self, guard: &mut VecDeque<(String, u64)>) {
while self.map.len() >= self.capacity {
let Some(oldest) = guard.pop_front() else {
let Some((oldest, seq)) = guard.pop_front() else {
break;
};
// `remove` is a no-op if `oldest` was already lazy-expired.
// Loop continues until we actually shrink the map.
self.map.remove(&oldest);
// Remove only the entry this queue item was recorded for. A
// stale item (the entry expired, or was stored again since)
// removes nothing, and the loop moves on to the next one.
self.map.remove_if(&oldest, |_, entry| entry.seq == seq);
}
}
/// Drop queue items whose entry is gone or was re-stored, so the
/// queue stays within a constant factor of the live entries even
/// when entries keep expiring through `get` without any eviction.
fn compact_order(&self, order: &mut VecDeque<(String, u64)>) {
if order.len() > self.capacity.max(16) * 2 {
order.retain(|(hash, seq)| self.map.get(hash).is_some_and(|e| e.seq == *seq));
}
}
}
@@ -119,28 +140,39 @@ impl CcrStore for InMemoryCcrStore {
return;
}
// New entry. Cap-bound first (may sweep a few stale order
// entries), then insert and append to the FIFO queue.
// New entry. The capacity check, the insert and the queue append
// happen under one `order` lock, so a concurrent fresh insert can
// never sit in the map without its queue item (which would let
// eviction run out of queue while the map is still full). Lock
// order is always `order` then a map shard; no path takes `order`
// while holding a shard guard.
let mut order = self.order.lock().expect("ccr order mutex poisoned");
if let Some(mut existing) = self.map.get_mut(hash) {
// Another writer stored this hash while we waited for the lock.
let now = Instant::now();
existing.payload = payload.to_string();
existing.inserted = now;
existing.last_accessed = now;
return;
}
if self.map.len() >= self.capacity {
self.evict_until_under_capacity();
self.evict_until_under_capacity(&mut order);
}
let now = Instant::now();
let seq = self.next_seq.fetch_add(1, Ordering::Relaxed);
let entry = Entry {
payload: payload.to_string(),
seq,
inserted: now,
last_accessed: now,
};
let prev = self.map.insert(hash.to_string(), entry);
if prev.is_none() {
// Truly new key — record in FIFO order. (If `prev.is_some()`
// it means another thread re-inserted between our get_mut
// miss and this insert; treat that as a fast-path overwrite
// and skip the queue append to avoid duplicates.)
self.order
.lock()
.expect("ccr order mutex poisoned")
.push_back(hash.to_string());
self.map.insert(hash.to_string(), entry);
#[cfg(test)]
if let Some(hook) = self.after_insert_hook.lock().expect("hook mutex").take() {
hook();
}
order.push_back((hash.to_string(), seq));
self.compact_order(&mut order);
}
fn get(&self, hash: &str) -> Option<String> {
@@ -238,6 +270,80 @@ mod tests {
assert_eq!(store.len(), 0);
}
#[test]
fn reput_after_expiry_keeps_the_newest_entry() {
// a expires and is dropped by get(); b is stored; a is stored again
// (now the newest entry). At capacity, the oldest live entry is b,
// so storing c must evict b and keep the fresh a.
let store = InMemoryCcrStore::with_capacity_and_ttl(2, Duration::from_millis(30));
store.put("a", "old");
std::thread::sleep(Duration::from_millis(60));
assert_eq!(store.get("a"), None);
store.put("b", "2");
store.put("a", "fresh");
store.put("c", "3");
let a = store.get("a");
assert_eq!(store.get("b"), None, "b was the oldest live entry");
assert_eq!(
a.as_deref(),
Some("fresh"),
"fresh a was evicted instead of b"
);
}
#[test]
fn order_queue_stays_bounded_when_entries_expire_through_get() {
let store = InMemoryCcrStore::with_capacity_and_ttl(4, Duration::from_millis(1));
for i in 0..2000 {
let k = format!("k{i}");
store.put(&k, "v");
std::thread::sleep(Duration::from_millis(2));
assert_eq!(store.get(&k), None);
}
let n = store.order.lock().unwrap().len();
assert!(
n <= 32,
"order queue grew to {n} while the map held {}",
store.len()
);
}
#[test]
fn concurrent_fresh_inserts_never_exceed_capacity() {
use std::sync::{mpsc, Arc};
// Capacity 1. Writer A stores x and pauses after the map insert,
// before x is recorded in the order queue. Writer C then stores y.
// If C could run in that gap it would find no queue item to evict
// and leave both x and y in a capacity-1 store.
let store = Arc::new(InMemoryCcrStore::with_capacity_and_ttl(1, DEFAULT_TTL));
let (paused_tx, paused_rx) = mpsc::channel::<()>();
let (release_tx, release_rx) = mpsc::channel::<()>();
*store.after_insert_hook.lock().unwrap() = Some(Box::new(move || {
paused_tx.send(()).unwrap();
release_rx.recv().unwrap();
}));
let a = {
let s = Arc::clone(&store);
std::thread::spawn(move || s.put("x", "1"))
};
paused_rx.recv().unwrap();
let c = {
let s = Arc::clone(&store);
std::thread::spawn(move || s.put("y", "2"))
};
// Give C time to run into the gap if it can.
std::thread::sleep(Duration::from_millis(200));
release_tx.send(()).unwrap();
a.join().unwrap();
c.join().unwrap();
assert!(
store.len() <= 1,
"capacity 1 store holds {} entries",
store.len()
);
assert_eq!(store.get("y").as_deref(), Some("2"));
}
#[test]
fn store_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
@@ -645,6 +645,29 @@ fn build_session(path: &Path) -> Result<Session, Box<dyn std::error::Error + Sen
Ok(session)
}
/// Whether this process can build Kompress sessions: the same CPU-then-loader
/// preconditions every `Kompress` constructor checks, in the same order. The
/// CPU must run the precompiled ONNX Runtime (AVX2 on x86), and only then is
/// the library resolved (`ORT_DYLIB_PATH`, or the pip `onnxruntime` under
/// `VIRTUAL_ENV`, `CONDA_PREFIX` or `./.venv`) and committed, once per
/// process. `Err` is `"<reason>: <detail>"`. For harnesses that should skip,
/// not fail, on a machine that can't run Kompress.
pub fn onnx_runtime_ready() -> Result<(), String> {
onnx_runtime_ready_with(
crate::onnx_cpu::onnx_runtime_supported_by_cpu(),
crate::transforms::magika_detector::dynamic_ort_loader_ready,
)
}
/// [`onnx_runtime_ready`] with the CPU verdict and loader passed in, so tests
/// can check the loader never runs on an unsupported CPU.
fn onnx_runtime_ready_with(
cpu_ok: bool,
loader: impl FnOnce() -> Result<(), String>,
) -> Result<(), String> {
ort_session_gate(cpu_ok, loader).map_err(|(reason, e)| format!("{reason}: {e}"))
}
/// The tokenizer and the ONNX artifact `onnx` (e.g.
/// `"onnx/kompress-int8-wo.onnx"`) that the cache-only loader reads for
/// `config`: the pinned snapshots in Python's hub cache, as
@@ -890,6 +913,27 @@ mod tests {
assert!(called.get());
}
#[test]
fn onnx_runtime_ready_checks_the_cpu_before_the_loader() {
// The public readiness check (used by harnesses to skip) must keep
// the constructors' order: on an unsupported CPU it reports the CPU
// reason without initializing the native runtime.
let called = std::cell::Cell::new(false);
let out = onnx_runtime_ready_with(false, || {
called.set(true);
Ok(())
});
let message = out.unwrap_err();
assert!(message.starts_with("cpu_unsupported: "), "{message}");
assert!(!called.get(), "loader ran although the CPU check failed");
assert_eq!(
onnx_runtime_ready_with(true, || Err("no dylib".into())),
Err("ort_loader_unavailable: no dylib".to_string())
);
assert_eq!(onnx_runtime_ready_with(true, || Ok(())), Ok(()));
}
/// `from_pretrained` must refuse before `Api::new()` when the operator
/// air-gapped the box.
///
@@ -189,10 +189,8 @@ fn discover_onnxruntime_libraries() -> Vec<PathBuf> {
}
}
if let Ok(cwd) = std::env::current_dir() {
roots.push(cwd.join(".venv"));
roots.push(cwd.join("venv"));
}
// The cwd may be an untrusted agent workspace. Only discover an environment
// there when the operator explicitly selects it with VIRTUAL_ENV/CONDA_PREFIX.
if let Some(user_profile) = env_path("USERPROFILE") {
roots.extend(versioned_children(
@@ -527,6 +525,81 @@ pub fn map_magika_label(label: &str) -> ContentType {
mod tests {
use super::*;
fn check_discovery_root(directory: &str, active_env: Option<&str>) {
let root = tempfile::tempdir().unwrap();
let environment = root.path().join(directory);
#[cfg(target_os = "windows")]
let library = environment.join("Lib/site-packages/onnxruntime/capi/onnxruntime.dll");
#[cfg(target_os = "macos")]
let library =
environment.join("lib/python3.11/site-packages/onnxruntime/capi/libonnxruntime.dylib");
#[cfg(all(not(target_os = "windows"), not(target_os = "macos")))]
let library =
environment.join("lib/python3.11/site-packages/onnxruntime/capi/libonnxruntime.so");
std::fs::create_dir_all(library.parent().unwrap()).unwrap();
std::fs::write(&library, b"not a native library").unwrap();
// Isolate process cwd and environment from parallel unit tests. The
// child enumerates candidates only; it never loads the stub library.
let mut child = std::process::Command::new(std::env::current_exe().unwrap());
child
.arg("--exact")
.arg("transforms::magika_detector::tests::discovery_root_child")
.arg("--nocapture")
.current_dir(root.path())
.env_remove("VIRTUAL_ENV")
.env_remove("CONDA_PREFIX")
.env("HEADROOM_TEST_ORT_LIBRARY", &library)
.env(
"HEADROOM_TEST_ORT_EXPECTED",
if active_env.is_some() { "yes" } else { "no" },
);
if let Some(variable) = active_env {
child.env(variable, &environment);
}
let output = child.output().unwrap();
assert!(
output.status.success(),
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn discovery_root_child() {
let Some(library) = env_path("HEADROOM_TEST_ORT_LIBRARY") else {
return;
};
let expected = std::env::var("HEADROOM_TEST_ORT_EXPECTED").unwrap() == "yes";
let candidates = discover_onnxruntime_libraries();
assert_eq!(
candidates.contains(&library),
expected,
"candidates: {candidates:?}"
);
}
#[test]
fn discovery_ignores_cwd_dot_venv() {
check_discovery_root(".venv", None);
}
#[test]
fn discovery_ignores_cwd_venv() {
check_discovery_root("venv", None);
}
#[test]
fn discovery_keeps_explicit_virtual_env() {
check_discovery_root(".venv", Some("VIRTUAL_ENV"));
}
#[test]
fn discovery_keeps_explicit_conda_prefix() {
check_discovery_root("venv", Some("CONDA_PREFIX"));
}
#[test]
fn ort_telemetry_stays_off_unless_explicitly_allowed() {
assert!(!ort_telemetry_opted_in(None), "unset must stay off");
@@ -0,0 +1,78 @@
//! Kompress must return an error, not hang, when ONNX Runtime can't load.
//!
//! The crate loads ONNX Runtime dynamically (`ort-load-dynamic`). When the
//! dylib can't be loaded, `ort` 2.0.0-rc.12 deadlocks in its API-setup error
//! path (recursive `OnceLock` init) instead of returning an error, so every
//! session must go through `dynamic_ort_loader_ready` first, as magika and
//! fastembed do. Kompress used to call `Session::builder()` directly: with
//! the model in the HF cache and no loadable ONNX Runtime, the
//! `kompress_parity` test hung forever.
//!
//! This is its own test binary because the loader result is process-wide.
#![cfg(feature = "ml")]
use std::io::Write;
use std::sync::mpsc;
use std::time::Duration;
use headroom_core::transforms::kompress::{Kompress, KompressConfig, KompressError};
/// Smallest tokenizer `tokenizers` accepts, so loading gets past the
/// tokenizer and reaches the ONNX Runtime session.
const TOKENIZER_JSON: &str = r#"{
"version": "1.0",
"truncation": null,
"padding": null,
"added_tokens": [],
"normalizer": null,
"pre_tokenizer": null,
"post_processor": null,
"decoder": null,
"model": {"type": "WordLevel", "vocab": {"[UNK]": 0}, "unk_token": "[UNK]"}
}"#;
#[test]
fn missing_onnx_runtime_is_an_error_not_a_hang() {
let dir = tempfile::tempdir().expect("tempdir");
let tokenizer = dir.path().join("tokenizer.json");
std::fs::write(&tokenizer, TOKENIZER_JSON).expect("write tokenizer");
let onnx = dir.path().join("model.onnx");
std::fs::write(&onnx, b"never read: ONNX Runtime fails to load first").expect("write onnx");
std::env::set_var(
"ORT_DYLIB_PATH",
dir.path().join("libonnxruntime-does-not-exist.dylib"),
);
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(Kompress::from_files(
&tokenizer,
&onnx,
KompressConfig::default(),
));
});
match rx.recv_timeout(Duration::from_secs(60)) {
Ok(Err(err @ KompressError::Onnx { .. })) => {
// On an x86 CPU without AVX2 the CPU gate answers first, before
// any library is loaded; either reason proves it returned.
let message = err.to_string();
assert!(
message.contains("ORT_DYLIB_PATH") || message.contains("cpu_unsupported"),
"error should name ORT_DYLIB_PATH (or the CPU gate): {message}"
);
}
Ok(Err(other)) => panic!("expected KompressError::Onnx, got: {other}"),
Ok(Ok(_)) => panic!("Kompress loaded without an ONNX Runtime library"),
Err(_) => {
// The stuck thread also wedges normal process exit (ort's
// teardown waits on the same lock), so abort instead of
// returning a failure the harness could never report. Write to
// stderr directly: libtest's capture would swallow `eprintln!`.
let _ = std::io::stderr()
.write_all(b"Kompress::from_files deadlocked loading a missing ONNX Runtime\n");
std::process::abort();
}
}
}
+29 -12
View File
@@ -10,6 +10,14 @@
//! mirroring the parity harness's "stub → Skipped" tolerance. Run it
//! locally after `python scripts/record_kompress_trace.py` to get the
//! real assertion.
//!
//! Runtime-gated too: with the model cached but no way to run it (a CPU
//! without AVX2, or no ONNX Runtime library: `ORT_DYLIB_PATH` unset and no
//! pip `onnxruntime` under `VIRTUAL_ENV`, `CONDA_PREFIX` or `./.venv`), it
//! also SKIPS, the way `magika_idle_threads` skips without an ONNX Runtime.
//! The check is `kompress::onnx_runtime_ready`, the constructors' own
//! CPU-then-loader preconditions, so it never loads the runtime on a CPU
//! that can't run it.
use std::fs;
use std::path::{Path, PathBuf};
@@ -23,17 +31,27 @@ fn cached_artifacts() -> Option<(PathBuf, PathBuf)> {
kompress::cached_artifacts(&KompressConfig::default(), "onnx/kompress-int8-wo.onnx")
}
/// [`cached_artifacts`], or `None` (with the reason on stderr) when the
/// model isn't cached or this machine can't run ONNX Runtime.
fn loadable_artifacts() -> Option<(PathBuf, PathBuf)> {
let Some(paths) = cached_artifacts() else {
eprintln!(
"SKIP: kompress model/tokenizer not in HF cache; \
run `python scripts/record_kompress_trace.py` first"
);
return None;
};
if let Err(reason) = kompress::onnx_runtime_ready() {
eprintln!("SKIP: can't run ONNX Runtime for kompress here: {reason}");
return None;
}
Some(paths)
}
#[test]
fn kompress_matches_python_fixtures_byte_for_byte() {
let (tok, onnx) = match cached_artifacts() {
Some(paths) => paths,
None => {
eprintln!(
"SKIP: kompress model/tokenizer not in HF cache; \
run `python scripts/record_kompress_trace.py` first"
);
return;
}
let Some((tok, onnx)) = loadable_artifacts() else {
return;
};
let fixtures_dir =
@@ -94,9 +112,8 @@ fn kompress_matches_python_fixtures_byte_for_byte() {
fn short_input_passes_through() {
// Pure-logic check — no model needed. Fewer than MIN_WORDS words must
// pass through unchanged regardless of model availability... but the
// engine needs a model to construct. Guard on cache like the main test.
let Some((tok, onnx)) = cached_artifacts() else {
eprintln!("SKIP: model not cached");
// engine needs a model to construct. Guard like the main test.
let Some((tok, onnx)) = loadable_artifacts() else {
return;
};
let kompress = Kompress::from_files(&tok, &onnx, KompressConfig::default()).unwrap();
+4 -1
View File
@@ -138,7 +138,10 @@ def _resolve_and_pin() -> str | None:
installed = _installed_ort_version()
if installed is not None and installed < _MIN_RUST_ORT_API_VERSION:
logger.warning(
# By design on Intel macOS, where the proxy extra pins onnxruntime
# <1.24 (no newer x86_64 wheels): Python detection is the supported
# path there, so this is information, not a warning.
logger.info(
"onnxruntime %d.%d exposes an older C API than Rust detection "
"requires (1.24+); leaving %s unset and using Python detection",
*installed,
+6 -2
View File
@@ -71,6 +71,8 @@ _SECRET_KEY_VALUE_RE = re.compile(
)
_AUTH_VALUE_RE = re.compile(r"(?i)\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{12,}")
_API_KEY_VALUE_RE = re.compile(r"\bsk-[A-Za-z0-9_-]{12,}\b")
# Exactly one CCR marker and nothing else (see ``store`` and #2694, #4266).
_BARE_CCR_MARKER_RE = re.compile(r"<<ccr:[^>\n]+>>")
def _get_env_default_ttl_seconds() -> int:
@@ -383,7 +385,9 @@ class CompressionStore:
# loudly rather than silently converting "retrievable" into "gone".
# Narrow by design: only a *bare* marker is rejected. Legitimate
# originals may legally CONTAIN markers (nested offloads, a tool that
# echoed one), and refusing those would drop recoverable data.
# echoed one), and refusing those would drop recoverable data. That
# includes an original that starts with one marker and ends with
# another (#4266), so the check is a full match of a single marker.
#
# The rejected value is never echoed into the log. It is provably a
# bare marker here, but `original` is the store's credential-bearing
@@ -391,7 +395,7 @@ class CompressionStore:
# OAuth token), and an error path is exactly where that sort of leak
# survives review. `hash_key` already identifies the entry.
stripped = original.strip()
if stripped.startswith("<<ccr:") and stripped.endswith(">>") and "\n" not in stripped:
if _BARE_CCR_MARKER_RE.fullmatch(stripped):
logger.error(
"CCR store: refusing to persist a bare retrieval marker as "
"original_content (hash=%s tool=%s strategy=%s len=%d) — the "
+9 -3
View File
@@ -27,6 +27,7 @@ from headroom.pricing.litellm_model_resolution import (
pricing_lookup_candidates,
resolve_litellm_model_name,
)
from headroom.utils import remember_bounded
# litellm calls `dotenv.load_dotenv()` during its own import, which loads
# the project `.env` into `os.environ`. We don't want that side effect —
@@ -59,7 +60,10 @@ except ImportError:
litellm = None # type: ignore[assignment]
LITELLM_AVAILABLE = False
# Least-recently-used first. Keyed by the client-supplied model name, so the
# entry count is capped (#4479); an evicted name is simply resolved again.
_resolved_model_cache: dict[str, str] = {}
_RESOLVED_MODEL_CACHE_MAXSIZE = 256
logger = logging.getLogger("headroom.pricing")
@@ -113,14 +117,16 @@ def resolve_litellm_model(model: str) -> str:
the bare-prefix rules. Shared by the live (cost.py) and persisted
(savings_tracker) pricing paths so both figures price identically.
"""
if model in _resolved_model_cache:
return _resolved_model_cache[model]
cached = _resolved_model_cache.get(model)
if cached is not None:
remember_bounded(_resolved_model_cache, model, cached, _RESOLVED_MODEL_CACHE_MAXSIZE)
return cached
priced: str | None = None
alias = _static_alias_map()
if alias:
priced = _reduce_to_priced_key(alias.get(model, model))
resolved = priced if priced is not None else _resolve_litellm_model_uncached(model)
_resolved_model_cache[model] = resolved
remember_bounded(_resolved_model_cache, model, resolved, _RESOLVED_MODEL_CACHE_MAXSIZE)
return resolved
+2 -1
View File
@@ -51,8 +51,9 @@ async def handle_model_metadata_endpoint(
) -> Response:
"""Handle OpenAI-compatible model metadata with Codex ChatGPT-auth support."""
assert proxy.http_client is not None
chatgpt_client = getattr(proxy, "http_client_h1", None) or proxy.http_client
chatgpt_response = await handle_chatgpt_model_metadata(
proxy.http_client,
chatgpt_client,
request,
endpoint.upstream_path,
)
+2 -2
View File
@@ -212,9 +212,9 @@ def _register_openai_responses_subpath_route(
spec: OpenAIResponsesSubpathRoute,
) -> None:
async def openai_responses_subpath(request: Request, sub_path: str):
assert proxy.http_client is not None
codex_subpath_client = getattr(proxy, "http_client_h1", None) or proxy.http_client
chatgpt_response = await handle_chatgpt_codex_responses_subpath(
proxy.http_client,
codex_subpath_client,
request,
sub_path,
)
+8 -3
View File
@@ -2074,16 +2074,21 @@ def _extract_responses_usage(event: dict[str, Any]) -> tuple[int, int, int, int,
return input_tokens, output_tokens, cached, write, uncached, inferred
def _prefers_http1_passthrough(base_url: str) -> bool:
"""Whether passthrough to this host must use HTTP/1.1.
def prefers_http1_passthrough(base_url: str) -> bool:
"""Whether passthrough/streaming to this host must use HTTP/1.1.
ChatGPT's Cloudflare edge issues a managed challenge to our HTTP/2
fingerprint on sensitive account endpoints; HTTP/1.1 is accepted.
fingerprint on sensitive account endpoints, and multiplexing large
streaming request bodies over HTTP/2 can stall on flow-control windows.
HTTP/1.1 avoids both issues.
"""
host = (urlparse(base_url).hostname or "").lower()
return host == "chatgpt.com" or host.endswith(".chatgpt.com")
_prefers_http1_passthrough = prefers_http1_passthrough
def _openai_outcome_provider(
method: str, path: str, custom_base_url: str | None, upstream_base_url: str
) -> str:
+18 -4
View File
@@ -219,6 +219,18 @@ class StreamingMixin:
_mid_turn_queues: dict[str, asyncio.Queue] = {}
_active_streams: set[str] = set()
def _select_streaming_client(self, url: str) -> httpx.AsyncClient:
"""Select HTTP client for upstream streaming.
ChatGPT endpoints prefer HTTP/1.1 to avoid Cloudflare bot-management
challenges and HTTP/2 flow-control window stalls on large turns.
"""
from headroom.proxy.handlers.openai import prefers_http1_passthrough
if prefers_http1_passthrough(url):
return getattr(self, "http_client_h1", None) or self.http_client
return self.http_client
@staticmethod
def _get_session_key(body: dict, session_header: str | None = None) -> str:
"""Return session identity from an explicit header or a body-derived hash.
@@ -697,10 +709,11 @@ class StreamingMixin:
]
continuation_bytes = json.dumps({**base_body, "messages": messages}).encode("utf-8")
assert self.http_client is not None
continuation_request = self.http_client.build_request(
continuation_client = self._select_streaming_client(url)
continuation_request = continuation_client.build_request(
"POST", url, content=continuation_bytes, headers=headers
)
continuation = await self.http_client.send(continuation_request, stream=True)
continuation = await continuation_client.send(continuation_request, stream=True)
rounds += 1
async with contextlib.aclosing(continuation) as upstream:
if upstream.status_code >= 400:
@@ -1415,6 +1428,7 @@ class StreamingMixin:
# Open connection before generator to capture upstream response headers
# (needed to forward ratelimit headers to the client via StreamingResponse)
assert self.http_client is not None, "http_client must be initialized before streaming"
streaming_client = self._select_streaming_client(url)
try:
retry_attempts = max(1, getattr(self.config, "retry_max_attempts", 3))
upstream_response = None
@@ -1422,10 +1436,10 @@ class StreamingMixin:
for attempt in range(retry_attempts):
try:
_upstream_req = self.http_client.build_request(
_upstream_req = streaming_client.build_request(
"POST", url, content=outbound_bytes, headers=outbound_headers
)
upstream_response = await self.http_client.send(_upstream_req, stream=True)
upstream_response = await streaming_client.send(_upstream_req, stream=True)
if _codex_wire_debug:
capture_codex_wire_debug(
"http_stream_upstream_response_headers",
+2 -1
View File
@@ -57,7 +57,8 @@ def _parse_frame(frame: bytes) -> tuple[str | None, dict[str, Any] | None]:
"""Return ``(event_name, payload)`` for one complete SSE frame."""
event_name: str | None = None
data_lines: list[str] = []
for line in frame.decode("utf-8").splitlines():
for raw_line in frame.splitlines():
line = raw_line.decode("utf-8")
if line.startswith(SSE_EVENT_LINE_PREFIX):
event_name = line[len(SSE_EVENT_LINE_PREFIX) :].strip()
elif line.startswith(SSE_DATA_LINE_PREFIX):
+10 -2
View File
@@ -2693,15 +2693,23 @@ class HeadroomProxy(
if timeout is not None:
post_kwargs["timeout"] = timeout
from headroom.proxy.handlers.openai import prefers_http1_passthrough
req_client = (
getattr(self, "http_client_h1", None) or self.http_client
if prefers_http1_passthrough(url)
else self.http_client
)
for attempt in range(self.config.retry_max_attempts):
try:
if stream:
# For streaming, we return early - retry happens at higher level
return await self.http_client.post( # type: ignore[union-attr]
return await req_client.post( # type: ignore[union-attr]
url, **post_kwargs
)
else:
response = await self.http_client.post( # type: ignore[union-attr]
response = await req_client.post( # type: ignore[union-attr]
url, **post_kwargs
)
+2 -2
View File
@@ -48,10 +48,10 @@ def parse_sse_events_from_byte_buffer(
event_bytes = bytes(buf[:idx])
del buf[: idx + terminator_len]
event_text = event_bytes.decode("utf-8")
event_name: str | None = None
data_lines: list[str] = []
for line in event_text.splitlines():
for raw_line in event_bytes.splitlines():
line = raw_line.decode("utf-8")
if not line:
continue
if line.startswith(":"):
+13 -5
View File
@@ -120,6 +120,7 @@ from pathlib import Path
from typing import Any
from headroom.offline import pin_litellm_cost_map_local
from headroom.utils import remember_bounded
logger = logging.getLogger(__name__)
@@ -511,7 +512,11 @@ def _config_snapshot() -> dict[str, str]:
return out
# Least-recently-used first. ``model`` comes from the request body, so the
# entry count is capped (#4479); a model that falls out is simply looked up
# again the next time it is seen.
_model_cache: dict[str, str | None] = {}
_MODEL_CACHE_MAXSIZE = 256
def _public_model(model: str) -> str | None:
@@ -530,8 +535,13 @@ def _public_model(model: str) -> str | None:
"""
if not model:
return None
if model in _model_cache:
return _model_cache[model]
try:
cached = _model_cache[model]
except KeyError:
pass
else:
remember_bounded(_model_cache, model, cached, _MODEL_CACHE_MAXSIZE)
return cached
resolved: str | None = None
try:
@@ -546,9 +556,7 @@ def _public_model(model: str) -> str | None:
except Exception:
resolved = None
# ponytail: unbounded dict, but it is keyed by distinct model ids seen in
# one process — a handful. Cap it if a router ever fans out over hundreds.
_model_cache[model] = resolved
remember_bounded(_model_cache, model, resolved, _MODEL_CACHE_MAXSIZE)
return resolved
+13 -5
View File
@@ -11,6 +11,7 @@ import re
from collections.abc import Callable
from typing import TYPE_CHECKING
from ..utils import remember_bounded
from .base import TokenCounter
from .estimator import EstimatingTokenCounter
@@ -144,8 +145,13 @@ class TokenizerRegistry:
# Registered model factories (model -> factory function)
_model_factories: dict[str, Callable[[str], TokenCounter]] = {}
# Cache for auto-detected tokenizers
# Cache for auto-detected tokenizers, least-recently-used first. ``model``
# is the ``model`` field of a proxied request body, so the entry count is
# capped (#4479): each entry owns its own TokenCountCache, and a client
# sending a new name per request must not grow this without limit. Real
# traffic uses a handful of models, far below the cap.
_cache: dict[str, TokenCounter] = {}
_CACHE_MAXSIZE = 64
def __new__(cls) -> TokenizerRegistry:
"""Singleton pattern."""
@@ -198,8 +204,10 @@ class TokenizerRegistry:
# Check cache
cache_key = f"{model_lower}:{backend or 'auto'}"
if cache_key in registry._cache:
return registry._cache[cache_key]
cached = registry._cache.get(cache_key)
if cached is not None:
remember_bounded(registry._cache, cache_key, cached, cls._CACHE_MAXSIZE)
return cached
uses_model_factory = backend is None and model_lower in registry._model_factories
@@ -209,7 +217,7 @@ class TokenizerRegistry:
tokenizer = registry._model_factories[model_lower](model)
else:
tokenizer = registry._create_tokenizer(model, backend)
registry._cache[cache_key] = tokenizer
remember_bounded(registry._cache, cache_key, tokenizer, cls._CACHE_MAXSIZE)
return tokenizer
except Exception as e:
if fallback:
@@ -218,7 +226,7 @@ class TokenizerRegistry:
)
tokenizer = EstimatingTokenCounter()
if not uses_model_factory:
registry._cache[cache_key] = tokenizer
remember_bounded(registry._cache, cache_key, tokenizer, cls._CACHE_MAXSIZE)
return tokenizer
raise ValueError(f"No tokenizer available for {model}: {e}") from e
+12 -3
View File
@@ -1350,8 +1350,12 @@ def _detect_content(content: str) -> DetectionResult:
from headroom._ort import rust_ort_runtime_compatible
if not rust_ort_runtime_compatible():
# Expected on every install without the proxy extra's ONNX Runtime
# (plain ``pip install headroom-ai``): the pure-Python detector is the
# supported path there, so this is information, not a warning. The
# circuit breaker above makes it fire once per process.
_detect_native_unhealthy = True
logger.warning(
logger.info(
"Native content detection requires ONNX Runtime 1.24+; "
"using pure-Python detection for this process."
)
@@ -5189,6 +5193,13 @@ class ContentRouter(Transform):
self._kompress_max_tokens,
self._kompress_gate_fires,
)
if protected:
# The fallback compressors take no ``ccr_original`` and would
# store the ``{{HEADROOM_TAG_N}}`` placeholder text in CCR; any
# placeholder line they drop is also discarded by
# ``restore_tags``, losing the tagged block outright. Pass the
# original through unchanged instead (as the budget branch above).
return content, _estimate_tokens(content)
out = text_to_compress
crusher = self._get_text_crusher()
if crusher is not None:
@@ -5209,8 +5220,6 @@ class ContentRouter(Transform):
"Kompress size-gate -> LogCompressor failed (%s); passing through", e
)
out = text_to_compress
if protected:
out = restore_tags(out, protected)
return out, _estimate_tokens(out)
# Reached only when the gate is enabled and this eligible block is
+39 -1
View File
@@ -9,7 +9,10 @@ import re
import uuid
import warnings
from datetime import datetime, timezone
from typing import Any
from typing import Any, TypeVar
_K = TypeVar("_K")
_V = TypeVar("_V")
# Marker format for Headroom modifications
MARKER_PREFIX = "<headroom:"
@@ -42,6 +45,41 @@ def compute_short_hash(data: str | bytes, length: int = 16) -> str:
return compute_hash(data)[:length]
def remember_bounded(cache: dict[_K, _V], key: _K, value: _V, maxsize: int) -> None:
"""Store ``key`` as the newest entry of ``cache``, keeping at most ``maxsize``.
``cache`` is a plain dict used as a least-recently-used cache: insertion
order is recency order, so the first key is the stalest and is evicted
first. Call this again on a cache hit to mark the key recently used.
Meant for caches keyed by client-controlled strings, such as a proxied
request's ``model`` field: the entry count stays bounded however many
distinct keys arrive, and an evicted key is recomputed on its next use
rather than answered wrongly.
Safe to call from several threads without a lock: a race can evict a key
early or leave the cache briefly one entry over ``maxsize``, never raise.
Args:
cache: The dict to update in place.
key: Cache key.
value: Value to store.
maxsize: Maximum number of entries. ``<= 0`` stores nothing.
"""
if maxsize <= 0:
cache.clear()
return
cache.pop(key, None)
while len(cache) >= maxsize:
try:
oldest = next(iter(cache))
except (StopIteration, RuntimeError):
# Emptied, or resized by another thread mid-iteration.
break
cache.pop(oldest, None)
cache[key] = value
def fast_hash(data: str | bytes, length: int = 16) -> str:
"""Fast non-cryptographic content hash for caches and dedup.
@@ -1,6 +1,6 @@
{
"name": "headroom",
"version": "0.42.0",
"version": "0.43.0",
"description": "Headroom startup hooks for Claude Code and GitHub Copilot CLI.",
"author": {
"name": "Headroom Contributors",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "headroom",
"version": "0.42.0",
"version": "0.43.0",
"description": "Headroom startup hooks for Claude Code and GitHub Copilot CLI.",
"author": {
"name": "Headroom Contributors",
@@ -1,6 +1,6 @@
{
"name": "headroom-skill",
"version": "0.42.0",
"version": "0.43.0",
"description": "Skill: add headroom-ai compress() to a Python or TypeScript app to cut LLM input tokens (tool outputs, logs, JSON, chat history). No API key.",
"author": {
"name": "Headroom Contributors",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "headroom-skill",
"version": "0.42.0",
"version": "0.43.0",
"description": "Skill: add headroom-ai compress() to a Python or TypeScript app to cut LLM input tokens (tool outputs, logs, JSON, chat history). No API key.",
"author": {
"name": "Headroom Contributors",
@@ -1,6 +1,6 @@
{
"name": "headroom-snip",
"version": "0.42.0",
"version": "0.43.0",
"description": "Watch Headroom snip tokens off every Claude Code request: an animated band, a /headroom log and a running total.",
"author": {
"name": "Headroom Contributors",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "headroom-openclaw",
"version": "0.42.0",
"version": "0.43.0",
"description": "Headroom context compression plugin for OpenClaw — 70-90% token savings with zero LLM calls",
"type": "module",
"main": "./dist/index.js",
@@ -20,14 +20,14 @@ export function createHeadroomRetrieveTool(config: RetrieveToolConfig) {
description:
"Retrieve original uncompressed content from Headroom's compression store. " +
"Use when compressed context mentions a hash and you need the full details. " +
"Pass the hash from the compression marker (24 hex characters). " +
"Pass the hash from the compression marker (12 or 24 hex characters). " +
"Retrieval is by hash and always returns the full original content.",
parameters: {
type: "object" as const,
properties: {
hash: {
type: "string",
description: "The 24-character hex hash from the compression marker",
description: "The 12- or 24-character hex hash from the compression marker",
},
},
required: ["hash"],
@@ -44,14 +44,17 @@ export function createHeadroomRetrieveTool(config: RetrieveToolConfig) {
const hash = typeof args?.hash === "string" ? args.hash : "";
// Validate hash format
if (!/^[a-f0-9]{24}$/i.test(hash)) {
if (!/^(?:[a-f0-9]{12}|[a-f0-9]{24})$/i.test(hash)) {
return JSON.stringify({
error: "Invalid hash format. Expected 24 hex characters.",
error: "Invalid hash format. Expected 12 or 24 hex characters.",
});
}
try {
const url = `${proxyOrigin}/v1/retrieve/${hash}`;
// The store keys entries by lowercase hex, so normalize once here
// instead of requiring every caller to pass a pre-lowercased hash.
const normalizedHash = hash.toLowerCase();
const url = `${proxyOrigin}/v1/retrieve/${normalizedHash}`;
const resp = await fetch(url, {
signal: AbortSignal.timeout(10_000),
@@ -0,0 +1,111 @@
/**
* Hash format accepted by the headroom_retrieve tool.
*
* The Rust SmartCrusher row-drop path keys its CCR marker with a 12-char
* SHA-256 hex prefix (`hash_canonical` in
* crates/headroom-core/src/transforms/smart_crusher/crusher.rs), mirrored into
* the compression store through `explicit_hash`, so a 12-char hash is a valid
* retrieval key just like a 24-char one.
*/
import { describe, it, expect, afterEach, vi } from "vitest";
import { createHeadroomRetrieveTool } from "../src/tools/headroom-retrieve.js";
const PROXY_URL = "http://127.0.0.1:8788";
function stubRetrieveOk() {
const mock = vi.fn().mockResolvedValue({
ok: true,
status: 200,
json: () => Promise.resolve({ original_content: "the full original text" }),
});
vi.stubGlobal("fetch", mock);
return mock;
}
function retrieve(hash: string) {
const tool = createHeadroomRetrieveTool({ proxyUrl: PROXY_URL });
return tool.execute({ hash });
}
afterEach(() => {
vi.unstubAllGlobals();
});
describe("headroom_retrieve hash validation", () => {
it("accepts a 12-character hex hash and queries the store", async () => {
const fetchMock = stubRetrieveOk();
const result = await retrieve("a1b2c3d4e5f6");
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${PROXY_URL}/v1/retrieve/a1b2c3d4e5f6`);
expect(JSON.parse(result)).toEqual({ original_content: "the full original text" });
});
it("queries the store with the lowercased key for an uppercase 12-character hash", async () => {
const fetchMock = stubRetrieveOk();
await retrieve("A1B2C3D4E5F6");
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${PROXY_URL}/v1/retrieve/a1b2c3d4e5f6`);
});
it("still accepts a 24-character hex hash", async () => {
const fetchMock = stubRetrieveOk();
const result = await retrieve("a1b2c3d4e5f6a1b2c3d4e5f6");
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${PROXY_URL}/v1/retrieve/a1b2c3d4e5f6a1b2c3d4e5f6`);
expect(JSON.parse(result)).toEqual({ original_content: "the full original text" });
});
it("queries the store with the lowercased key for an uppercase 24-character hash", async () => {
const fetchMock = stubRetrieveOk();
await retrieve("A1B2C3D4E5F6A1B2C3D4E5F6");
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${PROXY_URL}/v1/retrieve/a1b2c3d4e5f6a1b2c3d4e5f6`);
});
it("rejects a non-hex 12-character hash without querying the store", async () => {
const fetchMock = stubRetrieveOk();
const result = await retrieve("zzzzzzzzzzzz");
expect(fetchMock).not.toHaveBeenCalled();
expect(JSON.parse(result).error).toBe("Invalid hash format. Expected 12 or 24 hex characters.");
});
it("rejects a 13-character hash without querying the store", async () => {
const fetchMock = stubRetrieveOk();
const result = await retrieve("a1b2c3d4e5f67");
expect(fetchMock).not.toHaveBeenCalled();
expect(JSON.parse(result).error).toBe("Invalid hash format. Expected 12 or 24 hex characters.");
});
it("rejects an empty hash without querying the store", async () => {
const fetchMock = stubRetrieveOk();
const result = await retrieve("");
expect(fetchMock).not.toHaveBeenCalled();
expect(JSON.parse(result).error).toBe("Invalid hash format. Expected 12 or 24 hex characters.");
});
});
describe("headroom_retrieve hash documentation", () => {
it("documents both supported hash lengths", () => {
const tool = createHeadroomRetrieveTool({ proxyUrl: PROXY_URL });
const mentionsBothLengths = /\b12\b[^\n]*\b24\b/;
expect(tool.description).toMatch(mentionsBothLengths);
expect(tool.parameters.properties.hash.description).toMatch(mentionsBothLengths);
});
});
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "headroom-opencode",
"version": "0.42.0",
"version": "0.43.0",
"description": "Headroom proxy integration plugin for OpenCode - routes LLM traffic through the Headroom proxy for token compression",
"type": "module",
"main": "./dist/index.js",
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "maturin"
[project]
name = "headroom-ai"
version = "0.42.0"
version = "0.43.0"
description = "Compress tool outputs, logs, JSON and chat history before each LLM call. Fewer input tokens, same answers; in-process, no API key. Library, proxy and MCP server."
# PyPI renders this file; GitHub renders README.md. Keep the two in step on the
# install commands and the savings claim (README.md "Proof" is the source).
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "headroom-ai",
"version": "0.42.0",
"version": "0.43.0",
"description": "Compress tool outputs, logs, JSON and chat history before each LLM call. Fewer input tokens, same answers. Adapters for OpenAI, Anthropic, Gemini and the Vercel AI SDK. Runs against the local Headroom proxy (pip install \"headroom-ai[proxy]\").",
"keywords": [
"llm",
+2 -2
View File
@@ -9,13 +9,13 @@
"source": "github",
"id": "1129940957"
},
"version": "0.42.0",
"version": "0.43.0",
"packages": [
{
"registryType": "pypi",
"registryBaseUrl": "https://pypi.org",
"identifier": "headroom-ai",
"version": "0.42.0",
"version": "0.43.0",
"runtimeHint": "uvx",
"runtimeArguments": [
{
+193
View File
@@ -0,0 +1,193 @@
"""Regression: caches keyed by the request's ``model`` string stay bounded (#4479).
``model`` comes straight off a proxied request body, so a client that sends a
new name on every request must not be able to grow a process-wide cache
without limit. #4407 bounded the per-model maps in the cost and savings
trackers; these tests pin the same property for the three remaining caches:
- ``TokenizerRegistry._cache`` (``headroom/tokenizers/registry.py``)
- the beacon's ``_model_cache`` behind ``_public_model``
(``headroom/telemetry/session.py``)
- ``_resolved_model_cache`` behind ``resolve_litellm_model``
(``headroom/pricing/litellm_pricing.py``)
Each is least-recently-used: a name in steady use is never evicted, and an
evicted name is recomputed on its next use rather than answered wrongly.
"""
from __future__ import annotations
import pytest
from headroom.pricing import litellm_pricing as lp
from headroom.telemetry import session
from headroom.tokenizers.estimator import EstimatingTokenCounter
from headroom.tokenizers.registry import TokenizerRegistry
from headroom.utils import remember_bounded
# --------------------------------------------------------------------------
# remember_bounded
# --------------------------------------------------------------------------
def test_remember_bounded_evicts_least_recently_inserted():
cache: dict[str, int] = {}
for i in range(5):
remember_bounded(cache, f"k{i}", i, maxsize=3)
assert list(cache) == ["k2", "k3", "k4"]
def test_remember_bounded_reinsert_moves_key_to_newest():
cache: dict[str, int] = {}
for i in range(3):
remember_bounded(cache, f"k{i}", i, maxsize=3)
remember_bounded(cache, "k0", 0, maxsize=3) # a hit re-records the key
remember_bounded(cache, "k3", 3, maxsize=3)
assert list(cache) == ["k2", "k0", "k3"]
def test_remember_bounded_nonpositive_maxsize_stores_nothing():
cache: dict[str, int] = {"stale": 1}
remember_bounded(cache, "k", 1, maxsize=0)
assert cache == {}
def test_remember_bounded_tolerates_a_dict_resized_by_another_thread():
class ResizedDuringIteration(dict):
def __iter__(self):
raise RuntimeError("dictionary changed size during iteration")
cache = ResizedDuringIteration({"k0": 0, "k1": 1})
remember_bounded(cache, "k2", 2, maxsize=2)
# The eviction is skipped for this call rather than raising on the request
# path; the next insert that can iterate trims the cache again.
assert dict.__getitem__(cache, "k2") == 2
# --------------------------------------------------------------------------
# TokenizerRegistry._cache
# --------------------------------------------------------------------------
@pytest.fixture
def clean_tokenizer_cache():
TokenizerRegistry.clear_cache()
yield
TokenizerRegistry.clear_cache()
def test_tokenizer_registry_cache_is_bounded(clean_tokenizer_cache):
cap = TokenizerRegistry._CACHE_MAXSIZE
for i in range(cap + 50):
TokenizerRegistry.get(f"client-model-{i}-" + "m" * 1_000)
assert len(TokenizerRegistry._cache) == cap
def test_tokenizer_registry_estimation_fallback_is_bounded(clean_tokenizer_cache):
cap = TokenizerRegistry._CACHE_MAXSIZE
for i in range(cap + 10):
tokenizer = TokenizerRegistry.get(f"client-model-{i}", backend="no-such-backend")
assert isinstance(tokenizer, EstimatingTokenCounter)
assert len(TokenizerRegistry._cache) == cap
def test_tokenizer_registry_keeps_a_model_in_steady_use(clean_tokenizer_cache):
cap = TokenizerRegistry._CACHE_MAXSIZE
hot = TokenizerRegistry.get("gpt-4o")
for i in range(cap + 50):
TokenizerRegistry.get(f"client-model-{i}")
assert TokenizerRegistry.get("gpt-4o") is hot
oldest = TokenizerRegistry.get("client-model-0")
assert "client-model-0:auto" in TokenizerRegistry._cache
assert oldest is TokenizerRegistry.get("client-model-0")
# --------------------------------------------------------------------------
# telemetry.session._model_cache
# --------------------------------------------------------------------------
@pytest.fixture
def clean_public_model_cache():
session._model_cache.clear()
yield
session._model_cache.clear()
def test_public_model_cache_is_bounded(clean_public_model_cache):
cap = session._MODEL_CACHE_MAXSIZE
for i in range(cap + 50):
assert session._public_model(f"custom-{i}-" + "m" * 1_000) is None
assert len(session._model_cache) == cap
def test_public_model_evicted_name_is_recomputed(clean_public_model_cache, monkeypatch):
calls: list[str] = []
real_pin = session.pin_litellm_cost_map_local
def counting_pin() -> None:
calls.append("probe")
real_pin()
monkeypatch.setattr(session, "pin_litellm_cost_map_local", counting_pin)
cap = session._MODEL_CACHE_MAXSIZE
session._public_model("seed-model")
for i in range(cap):
session._public_model(f"custom-{i}")
probes_before = len(calls)
assert session._public_model("seed-model") is None
assert len(calls) == probes_before + 1
assert len(session._model_cache) == cap
# --------------------------------------------------------------------------
# litellm_pricing._resolved_model_cache
# --------------------------------------------------------------------------
@pytest.fixture
def counted_resolution(monkeypatch):
lp._resolved_model_cache.clear()
calls: dict[str, int] = {}
def fake_uncached(model: str) -> str:
calls[model] = calls.get(model, 0) + 1
return f"resolved/{model}"
monkeypatch.setattr(lp, "_resolve_litellm_model_uncached", fake_uncached)
monkeypatch.delenv("HEADROOM_MODEL_ALIAS_MAP", raising=False)
yield calls
lp._resolved_model_cache.clear()
def test_resolved_model_cache_is_bounded(counted_resolution):
cap = lp._RESOLVED_MODEL_CACHE_MAXSIZE
for i in range(cap + 50):
assert lp.resolve_litellm_model(f"custom-{i}") == f"resolved/custom-{i}"
assert len(lp._resolved_model_cache) == cap
def test_resolved_model_cache_is_lru(counted_resolution):
cap = lp._RESOLVED_MODEL_CACHE_MAXSIZE
lp.resolve_litellm_model("hot-model")
lp.resolve_litellm_model("cold-model")
for i in range(cap):
lp.resolve_litellm_model(f"custom-{i}")
lp.resolve_litellm_model("hot-model")
assert counted_resolution["hot-model"] == 1
assert lp.resolve_litellm_model("cold-model") == "resolved/cold-model"
assert counted_resolution["cold-model"] == 2
+30
View File
@@ -551,6 +551,36 @@ class TestCompressionStoreRetrieve:
entry = store.retrieve("nonexistent_hash_key")
assert entry is None
@pytest.mark.parametrize(
"original",
[
"<<ccr:abcd>> Keep this original instruction and the next reference <<ccr:ef01>>",
"<<ccr:abcd>><<ccr:ef01>>",
"<<ccr:abcd,base64,2.0KB>> then <<ccr:ef01 5_rows_offloaded>>",
],
)
def test_original_with_separate_markers_is_stored(self, store: CompressionStore, original):
"""An original that starts and ends with two different markers is content (#4266)."""
hash_key = store.store(original=original, compressed="x")
entry = store.retrieve(hash_key)
assert entry is not None
assert entry.original_content == original
@pytest.mark.parametrize(
"marker", ["<<ccr:abcd>>", " <<ccr:abcd,base64,2.0KB>>\n", "<<ccr:abcd 5_rows_offloaded>>"]
)
def test_bare_marker_is_refused_and_keeps_existing_entry(self, store: CompressionStore, marker):
"""A bare marker is never stored, even over an existing entry (#2694)."""
hash_key = store.store(original="real bytes", compressed="x", explicit_hash="abcd")
assert store.store(original=marker, compressed="x", explicit_hash="abcd") == hash_key
entry = store.retrieve(hash_key)
assert entry is not None
assert entry.original_content == "real bytes"
def test_retrieve_expired_entry_returns_none(self, store_with_short_ttl: CompressionStore):
"""retrieve() returns None for expired entry."""
hash_key = store_with_short_ttl.store(original="[1]", compressed="[]")
+33
View File
@@ -670,6 +670,39 @@ class TestClearScope:
remaining = await memory_system.count(MemoryFilter(user_id="alice", session_id="s2"))
assert remaining == 1
@pytest.mark.asyncio
async def test_clear_scope_keeps_a_surviving_turn_in_the_indexes(self, memory_system):
"""Clearing one turn must not unindex the turn that survives (#4303).
The orchestrator collects the ids to remove from the indexes with a
query, then deletes the rows. If the query describes a wider scope
than the delete, a turn that is still stored loses its index entry and
stops being findable.
"""
await memory_system.add_batch(
[
{
"content": "planner t1",
"user_id": "alice",
"agent_id": "planner",
"turn_id": "t1",
},
{
"content": "planner t2",
"user_id": "alice",
"agent_id": "planner",
"turn_id": "t2",
},
]
)
count = await memory_system.clear_scope(user_id="alice", agent_id="planner", turn_id="t1")
assert count == 1
# t2's row survived, so it must still be findable through the indexes.
found = await memory_system.text_search("planner t2", user_id="alice")
assert [r.memory.content for r in found] == ["planner t2"]
@pytest.mark.asyncio
async def test_clear_scope_empty(self, memory_system):
"""Test clearing scope with no matching memories."""
+36
View File
@@ -378,6 +378,42 @@ class TestSQLiteMemoryStore:
remaining = await store.query(MemoryFilter(user_id="alice"))
assert len(remaining) == 2
@pytest.mark.asyncio
async def test_clear_scope_narrows_to_the_turn_without_a_session(self, store):
"""agent_id + turn_id without a session must scope to that turn (#4303)."""
await store.save_batch(
[
Memory(content="planner t1", user_id="alice", agent_id="planner", turn_id="t1"),
Memory(content="planner t2", user_id="alice", agent_id="planner", turn_id="t2"),
]
)
deleted = await store.clear_scope("alice", agent_id="planner", turn_id="t1")
assert deleted == 1
remaining = await store.query(MemoryFilter(user_id="alice", agent_id="planner"))
assert [m.content for m in remaining] == ["planner t2"]
# The query has to describe the same scope the delete used.
assert (
len(await store.query(MemoryFilter(user_id="alice", agent_id="planner", turn_id="t1")))
== 0
)
@pytest.mark.asyncio
async def test_clear_scope_narrows_with_parent_scopes(self, store):
"""A narrower scope must not delete rows outside its parents (#4303)."""
await store.save_batch(
[
Memory(content="s1 planner", user_id="alice", session_id="s1", agent_id="planner"),
Memory(content="s2 planner", user_id="alice", session_id="s2", agent_id="planner"),
]
)
deleted = await store.clear_scope("alice", session_id="s1", agent_id="planner")
assert deleted == 1
assert len(await store.query(MemoryFilter(user_id="alice", session_id="s2"))) == 1
# =============================================================================
# LRU Cache Tests
+23
View File
@@ -143,6 +143,29 @@ class TestMemoryToolStreamFilter:
assert forwarded[0]["type"] == "content_block_start"
assert {e["index"] for e in forwarded if "index" in e} == {1}
@pytest.mark.parametrize("separator", ["\u0085", "\u2028", "\u2029"])
def test_continuation_reindexes_text_with_unicode_line_separator(self, separator: str) -> None:
flt = MemoryToolStreamFilter(MEMORY_TOOLS, index_offset=2, forward_message_start=False)
start = {
"type": "content_block_start",
"index": 0,
"content_block": {"type": "text", "text": ""},
}
delta = {
"type": "content_block_delta",
"index": 0,
"delta": {"type": "text_delta", "text": f"before{separator}after"},
}
raw = b"".join(
f"event: {payload['type']}\r\ndata: {json.dumps(payload, ensure_ascii=False)}\r\n\r\n".encode()
for payload in (start, delta)
)
forwarded = _events(b"".join(flt.feed(raw)))
assert [event["index"] for event in forwarded] == [2, 2]
assert forwarded[1]["delta"]["text"] == f"before{separator}after"
def test_client_tool_alongside_memory_call_keeps_tool_use_stop(self) -> None:
flt = MemoryToolStreamFilter(MEMORY_TOOLS)
forwarded = _events(b"".join(flt.feed(_sse([SAVE, BASH], "tool_use"))))
+31
View File
@@ -54,6 +54,37 @@ def test_handle_model_metadata_endpoint_returns_chatgpt_response_when_present(mo
assert response.json() == {"client": "h2", "upstream_path": "/backend-api/models"}
def test_handle_model_metadata_endpoint_prefers_http1_client(monkeypatch) -> None:
async def fake_chatgpt_metadata(
http_client,
request: Request,
upstream_path: str,
) -> Response:
return JSONResponse({"client": http_client, "upstream_path": upstream_path})
monkeypatch.setattr(
"headroom.providers.model_metadata.handle_chatgpt_model_metadata",
fake_chatgpt_metadata,
)
proxy = type("Proxy", (), {"http_client": "h2", "http_client_h1": "h1"})()
app = FastAPI()
@app.get("/probe")
async def probe(request: Request):
return await handle_model_metadata_endpoint(
proxy,
request,
endpoint=MODEL_METADATA_LIST_ENDPOINT,
provider_api_base_url="https://api.openai.test",
provider_name="openai",
)
with TestClient(app) as client:
response = client.get("/probe")
assert response.json() == {"client": "h1", "upstream_path": "/backend-api/models"}
def test_handle_model_metadata_endpoint_falls_back_to_selected_provider(monkeypatch) -> None:
async def fake_chatgpt_metadata(http_client, request: Request, upstream_path: str) -> None:
return None
+3
View File
@@ -757,6 +757,7 @@ def test_openai_response_subpath_aliases_and_chatgpt_auth_use_expected_targets(m
with TestClient(_app()) as client:
fake = FakeAsyncClient()
client.app.state.proxy.http_client = fake
client.app.state.proxy.http_client_h1 = fake
assert client.get("/v1/codex/responses/items/resp_1").status_code == 200
assert client.post("/backend-api/responses/items/resp_2").status_code == 200
assert client.delete("/backend-api/codex/responses/items/resp_3").status_code == 200
@@ -1071,6 +1072,7 @@ def test_v1_models_fetches_codex_registry_under_chatgpt_auth(monkeypatch) -> Non
with TestClient(_app()) as client:
fake_http_client = FakeAsyncClient()
client.app.state.proxy.http_client = fake_http_client
client.app.state.proxy.http_client_h1 = fake_http_client
response = client.get(
"/v1/models?client_version=0.135.0",
headers={
@@ -1194,6 +1196,7 @@ def test_v1_models_get_single_dynamic_under_chatgpt_auth() -> None:
with TestClient(_app()) as client:
fake_http_client = FakeAsyncClient()
client.app.state.proxy.http_client = fake_http_client
client.app.state.proxy.http_client_h1 = fake_http_client
ok = client.get(
"/v1/models/gpt-5.3-codex-spark",
headers={
+3
View File
@@ -79,6 +79,7 @@ def test_codex_responses_subpath_aliases_delegate_to_passthrough():
with TestClient(create_app(ProxyConfig())) as client:
fake_http_client = FakeAsyncClient()
client.app.state.proxy.http_client = fake_http_client
client.app.state.proxy.http_client_h1 = fake_http_client
client.app.state.proxy.OPENAI_API_URL = "https://api.openai.test"
pi_response = client.post(
@@ -141,6 +142,7 @@ def test_codex_responses_subpath_passthrough_derives_chatgpt_routing_from_jwt(pa
with TestClient(create_app(ProxyConfig())) as client:
fake_http_client = FakeAsyncClient()
client.app.state.proxy.http_client = fake_http_client
client.app.state.proxy.http_client_h1 = fake_http_client
client.app.state.proxy.OPENAI_API_URL = "https://api.openai.test"
response = client.post(
@@ -192,6 +194,7 @@ def test_codex_model_metadata_fetches_codex_registry_for_chatgpt_auth(monkeypatc
with TestClient(create_app(ProxyConfig())) as client:
fake_http_client = FakeAsyncClient()
client.app.state.proxy.http_client = fake_http_client
client.app.state.proxy.http_client_h1 = fake_http_client
client.app.state.proxy.OPENAI_API_URL = "https://api.openai.test"
list_response = client.get(
+74
View File
@@ -17,7 +17,9 @@ from headroom.proxy.handlers.openai import (
_decode_openai_bearer_payload,
_passthrough_usage_from_json,
_prefers_http1_passthrough,
prefers_http1_passthrough,
)
from headroom.proxy.handlers.streaming import StreamingMixin
from headroom.proxy.helpers import (
_headroom_bypass_enabled,
is_third_party_model,
@@ -778,6 +780,78 @@ def test_chatgpt_passthrough_falls_back_when_h1_client_missing() -> None:
assert handler.http_client.calls == 1
def test_streaming_client_selection() -> None:
assert prefers_http1_passthrough("https://chatgpt.com/backend-api/codex/responses") is True
assert prefers_http1_passthrough("https://api.openai.com/v1/chat/completions") is False
mixin = object.__new__(StreamingMixin)
mixin.http_client = "h2"
mixin.http_client_h1 = "h1"
assert mixin._select_streaming_client("https://chatgpt.com/backend-api/codex/responses") == "h1"
assert mixin._select_streaming_client("https://api.openai.com/v1/chat/completions") == "h2"
mixin.http_client_h1 = None
assert mixin._select_streaming_client("https://chatgpt.com/backend-api/codex/responses") == "h2"
@pytest.mark.asyncio
async def test_retry_request_selects_http1_for_chatgpt(monkeypatch) -> None:
calls = []
class DummyClient:
def __init__(self, name: str):
self.name = name
async def post(self, url, **kwargs):
calls.append((self.name, url))
return httpx.Response(200, json={"ok": True})
proxy = object.__new__(HeadroomProxy)
proxy.config = type("Config", (), {"retry_max_attempts": 1, "retry_enabled": False})()
proxy.http_client = DummyClient("h2")
proxy.http_client_h1 = DummyClient("h1")
await proxy._retry_request("POST", "https://chatgpt.com/backend-api/codex/responses", {}, {})
assert calls == [("h1", "https://chatgpt.com/backend-api/codex/responses")]
calls.clear()
await proxy._retry_request("POST", "https://api.openai.com/v1/chat/completions", {}, {})
assert calls == [("h2", "https://api.openai.com/v1/chat/completions")]
@pytest.mark.asyncio
async def test_proxy_routes_openai_responses_subpath_selects_http1(monkeypatch) -> None:
from fastapi import FastAPI
from fastapi.testclient import TestClient
from headroom.providers.openai_responses import OpenAIResponsesSubpathRoute
from headroom.providers.proxy_routes import _register_openai_responses_subpath_route
received_clients = []
async def fake_subpath(http_client, request, sub_path):
received_clients.append(http_client)
return httpx.Response(200, json={"ok": True})
monkeypatch.setattr(
"headroom.providers.proxy_routes.handle_chatgpt_codex_responses_subpath",
fake_subpath,
)
app = FastAPI()
proxy = type("Proxy", (), {"http_client": "h2", "http_client_h1": "h1"})()
spec = OpenAIResponsesSubpathRoute(
path="/v1/responses/{sub_path:path}",
methods=("GET", "POST"),
)
_register_openai_responses_subpath_route(app, proxy, spec)
client = TestClient(app)
client.get("/v1/responses/items/resp_1")
assert received_clients == ["h1"]
def test_passthrough_usage_normalizes_vertex_usage_metadata() -> None:
usage = _passthrough_usage_from_json(
{
+22
View File
@@ -1,5 +1,7 @@
from __future__ import annotations
import json
import pytest
from headroom.proxy.sse_byte_buffer_policy import (
@@ -28,11 +30,31 @@ def test_parse_sse_events_preserves_split_utf8_tail() -> None:
assert parse_sse_events_from_byte_buffer(buf) == [(None, "hello \U0001f642")]
def test_parse_sse_events_splits_crlf_lines() -> None:
buf = bytearray(b"event: delta\r\ndata: one\r\ndata: two\r\n\r\n")
assert parse_sse_events_from_byte_buffer(buf) == [("delta", "one\ntwo")]
assert not buf
def test_parse_sse_events_raises_on_complete_invalid_utf8_event() -> None:
with pytest.raises(UnicodeDecodeError):
parse_sse_events_from_byte_buffer(bytearray(b"data: \xff\n\n"))
@pytest.mark.parametrize("separator", ["\u0085", "\u2028", "\u2029"])
def test_parse_sse_events_preserves_unicode_line_separators_in_json(
separator: str,
) -> None:
payload = {"text": f"before{separator}after"}
wire = f"data: {json.dumps(payload, ensure_ascii=False)}\n\n".encode()
events = parse_sse_events_from_byte_buffer(bytearray(wire))
assert len(events) == 1
assert json.loads(events[0][1]) == payload
def test_parse_sse_events_strips_only_one_leading_space() -> None:
# SSE spec: "If value starts with a U+0020 SPACE character, remove it".
# Further leading whitespace is part of the payload.
@@ -74,3 +74,28 @@ def test_gate_fires_through_strategy_dispatch(monkeypatch, strategy):
router._apply_strategy_to_content(big, strategy, "")
assert router._kompress_gate_fires == 1
def test_gate_keeps_tagged_blocks_out_of_lossy_fallback(monkeypatch):
# Over the gate, with custom tags protected: the TextCrusher/LogCompressor
# fallback must not see the {{HEADROOM_TAG_N}} placeholder text. It would
# store the placeholders in CCR and ``restore_tags`` discards any wrap whose
# placeholder line it dropped, so the tagged content would vanish from both
# the output and the retrieval path.
router = ContentRouter()
router._kompress_max_tokens = 50 # tiny ceiling (x4 = 200 chars)
def _boom():
raise AssertionError("kompress must not be invoked for oversized (gated) input")
monkeypatch.setattr(router, "_get_kompress", _boom)
lines = [f"<item>row {i} value</item> note text here for item number {i}" for i in range(200)]
text = "Results from the fetch tool:\n\n" + "\n".join(lines)
out, _ = router._try_ml_compressor(text, "")
assert router._kompress_gate_fires == 1
assert out == text
assert out.count("<item>") == 200
assert "{{HEADROOM_TAG" not in out
+16 -2
View File
@@ -11,6 +11,7 @@ monkeypatched so every branch runs on any CI OS.
from __future__ import annotations
import json
import logging
import os
import subprocess
import sys
@@ -86,10 +87,14 @@ def test_incompatible_package_is_not_pinned(monkeypatch, tmp_path, caplog):
capi.mkdir(parents=True)
(capi / "libonnxruntime.so.1.23.2").write_bytes(b"old")
_fake_spec_for(monkeypatch, pkg)
caplog.set_level(logging.INFO, logger=_ort.logger.name)
assert _ort.ensure_ort_dylib_pinned() is None
assert not _ort.rust_ort_runtime_compatible()
assert "older C API" in caplog.text
older = [r for r in caplog.records if "older C API" in r.getMessage()]
assert len(older) == 1
# Supported configuration (Intel macOS pins onnxruntime <1.24): not a warning.
assert older[0].levelno == logging.INFO
def test_content_router_bypasses_native_detector_for_incompatible_ort(monkeypatch, caplog):
@@ -98,12 +103,21 @@ def test_content_router_bypasses_native_detector_for_incompatible_ort(monkeypatc
monkeypatch.setenv("HEADROOM_DETECT_BACKEND", "rust")
monkeypatch.setattr(_ort, "rust_ort_runtime_compatible", lambda: False)
monkeypatch.setattr(router, "_detect_native_unhealthy", False)
caplog.set_level(logging.INFO, logger=router.logger.name)
result = router._detect_content('{"safe": true}')
assert result.content_type.value.startswith("json")
assert router._detect_native_unhealthy is True
assert "requires ONNX Runtime 1.24+" in caplog.text
fallback = [r for r in caplog.records if "requires ONNX Runtime 1.24+" in r.getMessage()]
assert len(fallback) == 1
# A base install has no ONNX Runtime by design; the fallback is the
# supported path there, so it must not surface as a warning on first import.
assert fallback[0].levelno == logging.INFO
caplog.clear()
router._detect_content('{"safe": true}')
assert "requires ONNX Runtime 1.24+" not in caplog.text
def test_pins_to_package_capi_dll(monkeypatch, tmp_path):
Generated
+1 -1
View File
@@ -1422,7 +1422,7 @@ wheels = [
[[package]]
name = "headroom-ai"
version = "0.42.0"
version = "0.43.0"
source = { editable = "." }
dependencies = [
{ name = "ast-grep-cli" },