Merge upstream main into fix-fixed-width-column-positions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-11-05 23:47:08 +07:00
co-authored by Claude Opus 5.5
141 changed files with 8085 additions and 1482 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"
+2 -2
View File
@@ -41,7 +41,7 @@ jobs:
python-version: "3.12"
- name: Install uv
uses: astral-sh/setup-uv@v10.3.2
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
- name: Export production dependency set from uv.lock
run: |
@@ -97,7 +97,7 @@ jobs:
- name: Install uv
if: steps.gate.outputs.run == 'true'
uses: astral-sh/setup-uv@v10.3.2
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
- name: Resolve what pip installs from the package metadata
if: steps.gate.outputs.run == 'true'
+1 -1
View File
@@ -27,7 +27,7 @@ repos:
# unconditionally, so installing hooks is not required for enforcement.
args: [--assume-in-merge]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.17.2
rev: v0.17.3
hooks:
- id: ruff
args: [--fix]
+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 &lt;1.0,&gt;=0.15.0 to &gt;=0.15.2,&lt;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)
+7
View File
@@ -102,6 +102,13 @@ Headroom uses a `pyproject.toml`/`maturin` build backend. Older `pip`
versions may fail editable installs by looking for `setup.py`; upgrade `pip`
first or use `uv sync --extra dev`.
Handler tests that read request bodies should use a real Starlette `Request`
with ASGI receive messages, including streamed chunks, rather than patching the
body reader. Size-limit regressions must configure an explicit test ceiling;
the production ceiling is independent of the compression admission threshold.
Avoid replacing imported proxy packages in `sys.modules`: it can split live
helper and exception identities across full-suite test orderings.
### Dev Containers
Two configs ship for VS Code / Codespaces:
+4 -2
View File
@@ -113,8 +113,10 @@ host, and what the local admin surface does and does not allow. The summary:
are plaintext and are not encrypted at rest. Set `HEADROOM_CCR_BACKEND=memory` if your
deployment cannot accept disk persistence.
- **Anonymous session summaries are uploaded by default.** The `HEADROOM_BEACON` telemetry
beacon is opt-*out*: it POSTs content-free session counters, a random per-install UUID,
version, OS, and architecture to Headroom Labs. No prompts, completions, file contents,
beacon is opt-*out*: it POSTs content-free session counters and distributions, a random per-install
UUID, version, OS, and architecture to Headroom Labs, plus the values of 15 allow-listed
`HEADROOM_*` settings and a bucketed structural descriptor of tool outputs (field count,
depth, shape flags). `headroom telemetry --show` prints the exact payload. No prompts, completions, file contents,
or paths are included. Disable with `HEADROOM_BEACON=off`, `DO_NOT_TRACK=1`, or
`HEADROOM_OFFLINE=1`. This is separate from `HEADROOM_TELEMETRY`, which is opt-in and
stays on the machine.
+25 -1
View File
@@ -161,7 +161,13 @@ pub fn classify(headers: &HeaderMap) -> AuthMode {
None => "",
};
if let Some(token) = auth.strip_prefix("Bearer ") {
// RFC 9110 §11.1: auth scheme names are case-insensitive; tolerate
// extra whitespace between scheme and token.
let bearer = auth
.get(..7)
.filter(|scheme| scheme.eq_ignore_ascii_case("Bearer "))
.map(|_| auth[7..].trim_start());
if let Some(token) = bearer {
// Order matters: the OAuth shape `sk-ant-oat*` shares a
// prefix with `sk-ant-api*` only at `sk-ant-`, so we check
// the OAuth shape FIRST. Real OAuth access tokens are
@@ -236,6 +242,24 @@ mod inline_tests {
assert_eq!(classify(&headers), AuthMode::Payg);
}
fn classify_auth(value: &str) -> AuthMode {
let mut headers = HeaderMap::new();
headers.insert("authorization", HeaderValue::from_str(value).unwrap());
classify(&headers)
}
#[test]
fn bearer_scheme_is_case_insensitive_and_token_is_trimmed() {
// Controls: well-formed headers keep their classification.
assert_eq!(classify_auth("Bearer sk-ant-api03-fake"), AuthMode::Payg);
assert_eq!(classify_auth("Bearer sk-ant-oat01-fake"), AuthMode::OAuth);
// Lowercase scheme is still Bearer -> Payg, not the non-Bearer OAuth branch.
assert_eq!(classify_auth("bearer sk-ant-api03-fake"), AuthMode::Payg);
assert_eq!(classify_auth("BEARER sk-ant-api03-fake"), AuthMode::Payg);
// Extra space before the token must not hide the oat prefix.
assert_eq!(classify_auth("Bearer sk-ant-oat01-fake"), AuthMode::OAuth);
}
#[test]
fn unparseable_auth_falls_back_to_default() {
// Non-UTF-8 Authorization header — the warn! fires but we
@@ -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>() {}
+32 -1
View File
@@ -137,7 +137,7 @@ pub fn from_config(config: &CcrBackendConfig) -> Result<Box<dyn CcrStore>, CcrBa
tracing::info!(
target = "ccr.backend",
backend = "redis",
url = %url,
url = %redact_redis_url(url),
ttl_seconds = *ttl_seconds,
"ccr_backend_initialized"
);
@@ -150,3 +150,34 @@ pub fn from_config(config: &CcrBackendConfig) -> Result<Box<dyn CcrStore>, CcrBa
}),
}
}
/// Redis URL with userinfo, path and query dropped (`scheme://host[:port]`),
/// safe to log. Mirrors the Python client, which logs only the host.
#[cfg_attr(not(feature = "redis"), allow(dead_code))]
fn redact_redis_url(url: &str) -> String {
let Some((scheme, rest)) = url.split_once("://") else {
return "<unparseable redis url>".to_string();
};
let authority = &rest[..rest.find(['/', '?', '#']).unwrap_or(rest.len())];
let host = authority.rsplit('@').next().unwrap_or(authority);
format!("{scheme}://{host}")
}
#[cfg(test)]
mod tests {
use super::redact_redis_url;
#[test]
fn redis_url_credentials_are_not_logged() {
let out = redact_redis_url("redis://:secret@host:6379/0");
assert!(!out.contains("secret"), "{out}");
assert_eq!(out, "redis://host:6379");
let out = redact_redis_url("rediss://user:p@ss@cache.example.com?password=secret");
assert_eq!(out, "rediss://cache.example.com");
assert_eq!(
redact_redis_url("redis://localhost:6379"),
"redis://localhost:6379"
);
assert!(!redact_redis_url("secret-without-scheme").contains("secret"));
}
}
@@ -29,7 +29,7 @@
use redis::Commands;
use crate::ccr::{max_lifetime_for, CcrStore};
use crate::ccr::{max_lifetime_for, secs_i64, CcrStore};
/// Key prefix applied to every CCR entry. Configurable per-deployment
/// so multiple proxies sharing one Redis don't collide.
@@ -190,7 +190,7 @@ impl CcrStore for RedisCcrStore {
let _: redis::RedisResult<()> = conn.del(&key);
return None;
}
let rearm: redis::RedisResult<()> = conn.expire(&key, new_ttl as i64);
let rearm: redis::RedisResult<()> = conn.expire(&key, secs_i64(new_ttl));
if let Err(err) = rearm {
tracing::warn!(
target = "ccr.redis",
@@ -48,7 +48,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use rusqlite::{params, Connection, OptionalExtension};
use crate::ccr::{max_lifetime_for, CcrStore};
use crate::ccr::{max_lifetime_for, secs_i64, CcrStore};
/// SQLite-backed CCR store.
pub struct SqliteCcrStore {
@@ -160,7 +160,7 @@ impl SqliteCcrStore {
"DELETE FROM ccr_entries
WHERE last_accessed + ttl_seconds < ?1
OR created_at + ?2 < ?1",
params![now as i64, self.max_lifetime_seconds as i64],
params![secs_i64(now), secs_i64(self.max_lifetime_seconds)],
)?;
Ok(purged)
}
@@ -194,7 +194,7 @@ impl SqliteCcrStore {
WHERE hash = ?1
AND last_accessed + ttl_seconds >= ?2
AND created_at + ?3 >= ?2",
params![hash, now as i64, self.max_lifetime_seconds as i64],
params![hash, secs_i64(now), secs_i64(self.max_lifetime_seconds)],
|r| r.get::<_, Vec<u8>>(0),
)
.optional()
@@ -213,7 +213,7 @@ impl SqliteCcrStore {
// row's idle clock. Still under the same mutex as the lookup.
if let Err(err) = conn.execute(
"UPDATE ccr_entries SET last_accessed = ?2 WHERE hash = ?1",
params![hash, now as i64],
params![hash, secs_i64(now)],
) {
tracing::warn!(
target = "ccr.sqlite",
@@ -244,8 +244,8 @@ impl CcrStore for SqliteCcrStore {
params![
hash,
payload.as_bytes(),
now as i64,
self.default_ttl_seconds as i64,
secs_i64(now),
secs_i64(self.default_ttl_seconds),
],
);
// Loud-failure rule: surface as a structured warning. Caller
@@ -329,4 +329,13 @@ mod tests {
assert_eq!(store.get_at(&hash, 111), None);
assert_eq!(store.len(), 0, "expired row must be purged");
}
#[test]
fn huge_ttl_means_keep_not_expire_immediately() {
let dir = tempfile::tempdir().expect("tempdir");
let store = SqliteCcrStore::open(dir.path().join("ccr.sqlite"), u64::MAX)
.expect("open sqlite store");
store.put("k", "payload");
assert_eq!(store.get("k").as_deref(), Some("payload"));
}
}
+6
View File
@@ -79,6 +79,12 @@ pub fn max_lifetime_for(idle_ttl: Duration) -> Duration {
idle_ttl.saturating_mul(DEFAULT_MAX_LIFETIME_MULTIPLIER)
}
/// `u64` seconds -> `i64` for SQLite/Redis. Saturates: `u64::MAX` ("never
/// expire") must not wrap to -1 and expire at once.
pub(crate) fn secs_i64(secs: u64) -> i64 {
i64::try_from(secs).unwrap_or(i64::MAX)
}
/// Compute the canonical CCR key for `payload`. BLAKE3 → first 24 hex
/// chars (96 bits — collision-resistant for the bounded LRU population
/// the proxy will hold). Centralized here so every call site (live-zone
+5 -5
View File
@@ -1,10 +1,10 @@
//! Shared CPU-capability guard for the precompiled ONNX Runtime binary.
//!
//! Both ONNX entry points in this crate — Magika content detection
//! ([`crate::transforms::magika_detector`]) and the embedding relevance
//! scorer ([`crate::relevance::EmbeddingScorer`]) — link the same
//! precompiled ONNX Runtime shipped by `ort-sys` (pulled in via
//! fastembed's `ort-download-binaries*` feature).
//! All ONNX entry points in this crate — Magika content detection
//! ([`crate::transforms::magika_detector`]), the embedding relevance
//! scorer ([`crate::relevance::EmbeddingScorer`]) and the Kompress
//! compressor ([`crate::transforms::kompress`]) — load the same
//! precompiled ONNX Runtime binary at run time (`ort-load-dynamic`).
//!
//! On x86/x86_64 that binary contains AVX2-family instructions. Executing
//! it on a CPU without AVX2 (common inside Docker / QEMU / older cloud VMs)
@@ -429,19 +429,22 @@ impl DiffCompressor {
}
}
// Trim context inside each kept hunk.
let mut compressed_hunks: Vec<DiffHunk> = Vec::with_capacity(selected.len());
// Trim context inside each kept hunk. Trimming can split a hunk
// in two, so the kept/removed counts stay per original hunk.
let selected_count = selected.len();
let mut compressed_hunks: Vec<DiffHunk> = Vec::with_capacity(selected_count);
for hunk in selected {
let trimmed = reduce_context(&hunk, self.config.max_context_lines);
if trimmed.lines.len() > largest_kept {
largest_kept = trimmed.lines.len();
for trimmed in reduce_context(&hunk, self.config.max_context_lines) {
if trimmed.lines.len() > largest_kept {
largest_kept = trimmed.lines.len();
}
context_kept_total += trimmed.context_lines;
compressed_hunks.push(trimmed);
}
context_kept_total += trimmed.context_lines;
compressed_hunks.push(trimmed);
}
hunks_kept_total += compressed_hunks.len();
hunks_removed_total += original_hunk_count - compressed_hunks.len();
hunks_kept_total += selected_count;
hunks_removed_total += original_hunk_count - selected_count;
compressed_files.push(DiffFile {
hunks: compressed_hunks,
@@ -1042,7 +1045,14 @@ fn extract_line_number(header: &str) -> usize {
// ─── Context trimming ──────────────────────────────────────────────────────
fn reduce_context(hunk: &DiffHunk, max_context: usize) -> DiffHunk {
/// Trim each hunk's context to `max_context` lines around its changes.
///
/// Dropping context moves where a hunk starts and can leave a gap between
/// two changes, so every contiguous run of kept lines becomes its own hunk
/// with a header recomputed from the original line numbers (the same hunks
/// `git diff -U<max_context>` prints). A hunk that loses no lines keeps its
/// header byte-for-byte.
fn reduce_context(hunk: &DiffHunk, max_context: usize) -> Vec<DiffHunk> {
let parents = hunk_parents(&hunk.header);
let change_kind = |l: &str| -> (bool, bool) {
if parents > 1 {
@@ -1052,6 +1062,34 @@ fn reduce_context(hunk: &DiffHunk, max_context: usize) -> DiffHunk {
}
};
// Lines past the extent the header declares (the empty string left by a
// trailing newline, a format-patch signature) are not part of the hunk:
// they never count as a change, ride along with the last run, and are
// not numbered.
let ranges = parse_hunk_ranges(&hunk.header, parents);
let body_len = ranges
.as_ref()
.map_or(hunk.lines.len(), |r| hunk_body_len(&hunk.lines, parents, r));
let signature = (body_len..hunk.lines.len()).find(|&i| hunk.lines[i] == "-- ");
// An edit before any signature means the header understates its hunk.
// That is malformed input, so the hunk passes through as it came.
let understated = hunk.lines[body_len..signature.unwrap_or(hunk.lines.len())]
.iter()
.any(|l| {
let (added, deleted) = change_kind(l);
added || deleted
});
if understated {
return vec![build_hunk(
hunk.header.clone(),
hunk.lines.clone(),
&change_kind,
hunk.score,
)];
}
// Indices of `+`/`-` lines.
let change_positions: Vec<usize> = hunk
.lines
@@ -1059,7 +1097,7 @@ fn reduce_context(hunk: &DiffHunk, max_context: usize) -> DiffHunk {
.enumerate()
.filter_map(|(i, l)| {
let (added, deleted) = change_kind(l);
if added || deleted {
if i < body_len && (added || deleted) {
Some(i)
} else {
None
@@ -1067,53 +1105,115 @@ fn reduce_context(hunk: &DiffHunk, max_context: usize) -> DiffHunk {
})
.collect();
let mut keep = std::collections::BTreeSet::new();
if change_positions.is_empty() {
// No-changes hunk → keep up to `max_context` leading lines, like Python.
let take = max_context.min(hunk.lines.len());
let lines: Vec<String> = hunk.lines.iter().take(take).cloned().collect();
return DiffHunk {
header: hunk.header.clone(),
lines,
additions: 0,
deletions: 0,
context_lines: take,
score: hunk.score,
};
}
// Indices to keep: each change + `max_context` lines either side.
let mut keep = std::collections::BTreeSet::new();
for &pos in &change_positions {
keep.insert(pos);
let lo = pos.saturating_sub(max_context);
for i in lo..pos {
keep.insert(i);
keep.extend(0..max_context.min(hunk.lines.len()));
} else {
// Indices to keep: each change + `max_context` lines either side.
for &pos in &change_positions {
keep.insert(pos);
let lo = pos.saturating_sub(max_context);
for i in lo..pos {
keep.insert(i);
}
let hi = (pos + max_context + 1).min(hunk.lines.len());
for i in (pos + 1)..hi {
keep.insert(i);
}
}
let hi = (pos + max_context + 1).min(hunk.lines.len());
for i in (pos + 1)..hi {
keep.insert(i);
// Bug-fix: ALWAYS keep `\ No newline at end of file` markers (and any
// other backslash-prefixed metadata) regardless of distance from a
// change. These are structural patch markers, not context — losing
// them breaks round-trippable patches and changes the semantic
// meaning of the trailing line in the file. Mirrors the same fix in
// the Python source.
for (i, line) in hunk.lines.iter().enumerate() {
if line.starts_with('\\') {
keep.insert(i);
}
}
}
// Bug-fix: ALWAYS keep `\ No newline at end of file` markers (and any
// other backslash-prefixed metadata) regardless of distance from a
// change. These are structural patch markers, not context — losing
// them breaks round-trippable patches and changes the semantic
// meaning of the trailing line in the file. Mirrors the same fix in
// the Python source.
for (i, line) in hunk.lines.iter().enumerate() {
if line.starts_with('\\') {
keep.insert(i);
// A format-patch signature and what follows it stay whatever the context
// radius, up to the blank lines at the very end.
let last = hunk.lines.iter().rposition(|l| !l.is_empty());
if let (Some(signature), Some(last)) = (signature, last) {
keep.extend(signature..=last);
}
let untouched = keep.range(..body_len).next().is_none() || keep.len() == hunk.lines.len();
let Some(ranges) = ranges.filter(|_| !untouched) else {
// Nothing trimmed, or a header we can't renumber: one hunk under
// the original header.
return vec![build_hunk(
hunk.header.clone(),
keep.iter().map(|&i| hunk.lines[i].clone()).collect(),
&change_kind,
hunk.score,
)];
};
// Split the kept lines into contiguous runs.
let mut runs: Vec<Vec<usize>> = Vec::new();
for &i in &keep {
match runs.last_mut() {
Some(run) if i >= body_len || run.last() == Some(&(i - 1)) => run.push(i),
_ => runs.push(vec![i]),
}
}
let mut new_lines: Vec<String> = Vec::with_capacity(keep.len());
let marker = "@".repeat(parents + 1);
let (mut old_next, mut new_next) = (ranges.old_next, ranges.new_next);
let mut out = Vec::with_capacity(runs.len());
let mut pos = 0usize;
for run in runs {
// Advance the line numbers over everything before this run.
while pos < run[0] {
advance(&hunk.lines[pos], parents, &mut old_next, &mut new_next);
pos += 1;
}
let (old_start, new_start) = (old_next.clone(), new_next);
for &i in run.iter().filter(|&&i| i < body_len) {
advance(&hunk.lines[i], parents, &mut old_next, &mut new_next);
}
pos = run[run.len() - 1] + 1;
let mut header = marker.clone();
for (start, next) in old_start.iter().zip(&old_next) {
header.push_str(&format_range('-', *start, next - start, parents > 1));
}
header.push_str(&format_range(
'+',
new_start,
new_next - new_start,
parents > 1,
));
header.push(' ');
header.push_str(&marker);
header.push_str(&ranges.heading);
out.push(build_hunk(
header,
run.iter().map(|&i| hunk.lines[i].clone()).collect(),
&change_kind,
hunk.score,
));
}
out
}
fn build_hunk(
header: String,
lines: Vec<String>,
change_kind: &dyn Fn(&str) -> (bool, bool),
score: f64,
) -> DiffHunk {
let mut additions = 0usize;
let mut deletions = 0usize;
let mut context_lines = 0usize;
for &i in &keep {
let line = &hunk.lines[i];
new_lines.push(line.clone());
for line in &lines {
let (added, deleted) = change_kind(line);
if deleted {
deletions += 1;
@@ -1123,14 +1223,130 @@ fn reduce_context(hunk: &DiffHunk, max_context: usize) -> DiffHunk {
context_lines += 1;
}
}
DiffHunk {
header: hunk.header.clone(),
lines: new_lines,
header,
lines,
additions,
deletions,
context_lines,
score: hunk.score,
score,
}
}
/// Line numbers parsed from a hunk header: where each old side (one per
/// parent) and the new side start, how many lines each spans, and the
/// trailing section heading (` def foo():`).
struct HunkRanges {
old_next: Vec<usize>,
old_len: Vec<usize>,
new_next: usize,
new_len: usize,
heading: String,
}
/// `None` if the header doesn't have the `@@ -a,b +c,d @@` shape (with one
/// `-` range per parent).
fn parse_hunk_ranges(header: &str, parents: usize) -> Option<HunkRanges> {
let marker = "@".repeat(parents + 1);
let rest = header.strip_prefix(&marker)?;
let end = rest.find(&format!(" {marker}"))?;
let heading = rest[end + 1 + marker.len()..].to_string();
let ranges: Vec<&str> = rest[..end].split_whitespace().collect();
if ranges.len() != parents + 1 {
return None;
}
// Returns (next line number, line count). A zero-length range names the
// line *before* the hunk, except in a combined header, where it names the
// next one.
let parse = |range: &str, sign: char| -> Option<(usize, usize)> {
let body = range.strip_prefix(sign)?;
let (start, count) = match body.split_once(',') {
Some((s, c)) => (s.parse::<usize>().ok()?, c.parse::<usize>().ok()?),
None => (body.parse::<usize>().ok()?, 1),
};
Some((
if count == 0 && parents == 1 {
start.checked_add(1)?
} else {
start
},
count,
))
};
let old = ranges[..parents]
.iter()
.map(|r| parse(r, '-'))
.collect::<Option<Vec<_>>>()?;
let (new_next, new_len) = parse(ranges[parents], '+')?;
Some(HunkRanges {
old_next: old.iter().map(|&(n, _)| n).collect(),
old_len: old.iter().map(|&(_, c)| c).collect(),
new_next,
new_len,
heading,
})
}
/// Number of leading lines that make up the hunk its header describes.
fn hunk_body_len(lines: &[String], parents: usize, ranges: &HunkRanges) -> usize {
let (mut old, mut new) = (ranges.old_next.clone(), ranges.new_next);
let done = |old: &[usize], new: usize| {
new - ranges.new_next >= ranges.new_len
&& old
.iter()
.zip(&ranges.old_next)
.zip(&ranges.old_len)
.all(|((o, start), len)| o - start >= *len)
};
let mut n = 0;
while n < lines.len() && !done(&old, new) {
advance(&lines[n], parents, &mut old, &mut new);
n += 1;
}
n
}
/// Step the per-side line numbers over one hunk line.
fn advance(line: &str, parents: usize, old_next: &mut [usize], new_next: &mut usize) {
if line.starts_with('\\') {
return;
}
let cols = line.as_bytes();
let col = |j: usize| cols.get(j).copied().unwrap_or(b' ');
let deleted = (0..parents).any(|j| col(j) == b'-');
// A deleted line exists only in the parents marked `-`; any other line
// is in the result and in every parent not marked `+`.
for (j, next) in old_next.iter_mut().enumerate() {
let in_parent = if deleted {
col(j) == b'-'
} else {
col(j) != b'+'
};
if in_parent {
*next = next.saturating_add(1);
}
}
if !deleted {
*new_next = new_next.saturating_add(1);
}
}
/// One side of a hunk header, as git writes it: ` -12,3`, ` -12` for a
/// single line, and the line before the hunk for an empty range. Combined
/// headers always carry the count and name the next line for an empty range.
fn format_range(sign: char, next_line: usize, count: usize, combined: bool) -> String {
if combined {
return format!(" {sign}{next_line},{count}");
}
let start = if count == 0 {
next_line.saturating_sub(1)
} else {
next_line
};
if count == 1 {
format!(" {sign}{start}")
} else {
format!(" {sign}{start},{count}")
}
}
@@ -1834,6 +2050,237 @@ mod tests {
assert_eq!((r.additions, r.deletions), (3, 4));
}
/// Trimmed hunks get headers that match their lines. Expected ranges
/// are what `git diff -U2` / `-U0` print for the same change.
#[test]
fn trimmed_hunks_are_renumbered_like_git() {
let mut lines = vec![
"diff --git a/f b/f".to_string(),
"--- a/f".to_string(),
"+++ b/f".to_string(),
"@@ -7,14 +7,14 @@ l6".to_string(),
];
for i in 7..=20 {
match i {
10 | 17 => {
lines.push(format!("-l{i}"));
lines.push(format!("+l{i} x"));
}
_ => lines.push(format!(" l{i}")),
}
}
lines.push("@@ -23,6 +23,7 @@ l22".to_string());
for i in 23..=28 {
lines.push(format!(" l{i}"));
if i == 25 {
lines.push("+new after 25".to_string());
}
}
let input = lines.join("\n") + "\n";
let headers = |max_context_lines: usize| -> Vec<String> {
let cfg = DiffCompressorConfig {
min_lines_for_ccr: 5,
max_context_lines,
..Default::default()
};
let r = DiffCompressor::new(cfg).compress(&input, "");
r.compressed
.lines()
.filter(|l| l.starts_with("@@"))
.map(str::to_string)
.collect()
};
assert_eq!(
headers(2),
[
"@@ -8,5 +8,5 @@ l6",
"@@ -15,5 +15,5 @@ l6",
"@@ -24,4 +24,5 @@ l22"
]
);
assert_eq!(
headers(0),
[
"@@ -10 +10 @@ l6",
"@@ -17 +17 @@ l6",
"@@ -25,0 +26 @@ l22"
]
);
// Nothing to trim: the header is left alone.
assert!(headers(3).contains(&"@@ -7,14 +7,14 @@ l6".to_string()));
}
/// Line numbers near `usize::MAX` must not overflow while renumbering.
#[test]
fn renumbering_huge_line_numbers_does_not_panic() {
let mut lines = vec![
"diff --git a/f b/f".to_string(),
"--- a/f".to_string(),
"+++ b/f".to_string(),
format!("@@ -{0},0 +{0},5 @@", usize::MAX),
];
lines.extend((0..5).map(|i| format!("+added_{i}")));
lines.push(format!("@@ -{0},6 +{0},6 @@", usize::MAX - 2));
lines.extend((0..5).map(|i| format!(" ctx_{i}")));
lines.push("-old".to_string());
let cfg = DiffCompressorConfig {
min_lines_for_ccr: 5,
..Default::default()
};
let r = DiffCompressor::new(cfg).compress(&lines.join("\n"), "");
assert!(r.compressed.contains("-old"));
}
/// Combined-diff hunks are renumbered per parent.
#[test]
fn trimmed_combined_hunk_is_renumbered_per_parent() {
let input = [
"diff --combined f",
"--- a/f",
"+++ b/f",
"@@@ -1,8 -1,8 +1,8 @@@",
" c1",
" c2",
" c3",
"- p1_old",
" -p2_old",
"++merged",
" c4",
" c5",
" c6",
" c7",
"",
]
.join("\n");
let cfg = DiffCompressorConfig {
min_lines_for_ccr: 5,
..Default::default()
};
let r = DiffCompressor::new(cfg).compress(&input, "");
assert!(
r.compressed
.contains("@@@ -2,5 -2,5 +2,5 @@@\n c2\n c3\n- p1_old\n"),
"{}",
r.compressed
);
}
/// An empty range in a combined header names the next line, not the one
/// before. Inputs and expected headers come from `git show --cc`, and
/// `-U0` for the second.
#[test]
fn empty_combined_ranges_match_git() {
let compress = |input: &[&str], max_context_lines| {
let cfg = DiffCompressorConfig {
min_lines_for_ccr: 5,
max_context_lines,
..Default::default()
};
DiffCompressor::new(cfg)
.compress(&input.join("\n"), "")
.compressed
};
let empty_first_parent = [
"diff --cc f.txt",
"--- a/f.txt",
"+++ b/f.txt",
"@@@ -1,0 -1,4 +1,4 @@@",
"+ a",
"+ b",
"+ c",
" -d",
"++X",
"",
];
let r = compress(&empty_first_parent, 0);
assert!(r.contains("@@@ -1,0 -1,4 +1,4 @@@\n+ a\n"), "{r}");
let empty_mid_file = [
"diff --cc g.txt",
"--- a/g.txt",
"+++ b/g.txt",
"@@@ -17,6 -17,8 +17,7 @@@ tw",
" 17",
" 18",
" 19",
" -two-a",
" -two-b",
"++x",
" 20",
" 21",
" 22",
"",
];
let r = compress(&empty_mid_file, 0);
assert!(r.contains("@@@ -20,0 -20,2 +20,1 @@@ tw\n -two-a\n"), "{r}");
}
/// Edits past the extent the header declares are malformed input: the
/// hunk passes through as it came.
#[test]
fn edits_past_an_understated_header_are_kept() {
let compress = |hunk: &[&str]| {
let input = ["diff --git a/f b/f", "--- a/f", "+++ b/f"]
.iter()
.chain(hunk)
.copied()
.collect::<Vec<_>>()
.join("\n");
let cfg = DiffCompressorConfig {
min_lines_for_ccr: 5,
max_context_lines: 0,
..Default::default()
};
DiffCompressor::new(cfg).compress(&input, "").compressed
};
let r = compress(&["@@ -1 +1 @@", "-a", "+b", "-c", "+d", ""]);
assert!(r.contains("@@ -1 +1 @@\n-a\n+b\n-c\n+d"), "{r}");
let r = compress(&["@@ -1,2 +1,2 @@", " x", "-a", "+b", "-c", "+d", ""]);
assert!(r.contains("@@ -1,2 +1,2 @@\n x\n-a\n+b\n-c\n+d"), "{r}");
}
/// What follows a hunk in `git format-patch` output stays whatever the
/// context radius, and doesn't count towards the hunk's line numbers.
#[test]
fn format_patch_envelope_survives_trimming() {
let input = [
"diff --git a/f b/f",
"--- a/f",
"+++ b/f",
"@@ -1,5 +1,5 @@",
"-a",
"+b",
" c1",
" c2",
" c3",
" c4",
"-- ",
"2.43.0",
"",
"From 0000000 Mon Sep 17 00:00:00 2001",
"---",
" g | 2 +-",
"",
]
.join("\n");
let cfg = DiffCompressorConfig {
min_lines_for_ccr: 5,
max_context_lines: 2,
..Default::default()
};
let r = DiffCompressor::new(cfg).compress(&input, "");
assert!(
r.compressed.contains(
"@@ -1,3 +1,3 @@\n-a\n+b\n c1\n c2\n-- \n2.43.0\n\nFrom 0000000 Mon Sep 17 00:00:00 2001\n---\n g | 2 +-"
),
"{}",
r.compressed
);
}
/// Bug-fix test: pre-diff content (commit headers, email-style
/// metadata) is preserved verbatim before the diff sections. Before
/// the fix, anything before the first `diff --git` was silently
@@ -608,7 +608,39 @@ fn load_tokenizer(path: &Path, repo: &str) -> Result<Tokenizer, KompressError> {
Ok(tokenizer)
}
/// Decision behind [`ort_session_preconditions`]: an ONNX Runtime session may
/// only be built when the CPU can run the precompiled binary (else `SIGILL`,
/// #1723) and the dynamic loader is pinned (else `ort` deadlocks, #1715).
/// The CPU check comes first and `loader` is only invoked when it passes:
/// the loader initializes the native runtime, which itself can `SIGILL` on
/// a CPU without AVX2 (same order as the magika guard). `Err` carries a
/// stable `reason` tag plus detail for logging.
pub(crate) fn ort_session_gate(
cpu_ok: bool,
loader: impl FnOnce() -> Result<(), String>,
) -> Result<(), (&'static str, String)> {
if !cpu_ok {
return Err((
"cpu_unsupported",
"ONNX Runtime backend requires AVX2 on this x86 CPU".to_string(),
));
}
loader().map_err(|e| ("ort_loader_unavailable", e))
}
/// Run the two guards the crate requires before any `ort` session is built
/// (same pair, same order, as magika and the embedding scorer).
pub(crate) fn ort_session_preconditions() -> Result<(), (&'static str, String)> {
ort_session_gate(
crate::onnx_cpu::onnx_runtime_supported_by_cpu(),
crate::transforms::magika_detector::dynamic_ort_loader_ready,
)
}
fn build_session(path: &Path) -> Result<Session, Box<dyn std::error::Error + Send + Sync>> {
// Every Kompress constructor (from_files / from_pretrained / from_cache)
// funnels through here, so guarding here covers all of them.
ort_session_preconditions().map_err(|(reason, e)| format!("{reason}: {e}"))?;
let session = Session::builder()?.commit_from_file(path)?;
Ok(session)
}
@@ -829,6 +861,35 @@ mod tests {
assert_eq!(lookup(), None);
}
#[test]
fn ort_session_gate_rejects_unsupported_cpu_and_loader_failure() {
assert_eq!(ort_session_gate(true, || Ok(())), Ok(()));
let (reason, _) = ort_session_gate(false, || Ok(())).unwrap_err();
assert_eq!(reason, "cpu_unsupported");
let (reason, e) = ort_session_gate(true, || Err("no dylib".into())).unwrap_err();
assert_eq!((reason, e.as_str()), ("ort_loader_unavailable", "no dylib"));
}
#[test]
fn ort_session_gate_never_invokes_the_loader_on_an_unsupported_cpu() {
// The loader initializes the native runtime, which can SIGILL on a CPU
// without AVX2, so it must not run once the CPU check has failed.
let called = std::cell::Cell::new(false);
let out = ort_session_gate(false, || {
called.set(true);
Ok(())
});
assert_eq!(out.unwrap_err().0, "cpu_unsupported");
assert!(!called.get(), "loader ran although the CPU check failed");
// ...and it is invoked exactly when the CPU is fine.
let called = std::cell::Cell::new(false);
let _ = ort_session_gate(true, || {
called.set(true);
Ok(())
});
assert!(called.get());
}
/// `from_pretrained` must refuse before `Api::new()` when the operator
/// air-gapped the box.
///
@@ -645,11 +645,18 @@ pub fn kompress_init_runs() -> usize {
#[cfg(feature = "ml")]
fn run_kompress_init() {
KOMPRESS_INIT_RUNS.fetch_add(1, Ordering::Relaxed);
let slot = match Kompress::from_cache(KompressConfig::default()) {
Ok(k) => k, // None => HF cache cold: deterministic NoOp downstream
Err(e) => {
tracing::warn!(event = "kompress_init_failed", error = %e);
None
// Before any `ort` call: unsupported CPU => SIGILL, unpinned loader =>
// deadlock. Same guards magika and the embedding scorer run first.
let slot = if let Err((reason, error)) = super::kompress::ort_session_preconditions() {
tracing::warn!(event = "kompress_init_skipped", reason, error = %error);
None
} else {
match Kompress::from_cache(KompressConfig::default()) {
Ok(k) => k, // None => HF cache cold: deterministic NoOp downstream
Err(e) => {
tracing::warn!(event = "kompress_init_failed", error = %e);
None
}
}
};
let ready = slot.is_some();
@@ -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");
+1 -1
View File
@@ -109,7 +109,7 @@ CCR is **on by default**. Disable the markers and the injected retrieval tool wi
TOIN learns which fields matter for a given tool over repeated calls — which items get retrieved, which fields carry signal — and feeds that back into SmartCrusher's importance scoring so compression gets sharper for the tools you actually use.
TOIN is **local and observation-only**: it aggregates statistics on your own machine (or your own proxy instance). Nothing about your tools or traffic is shared across users or sent off the box. For a brand-new tool type it falls back to statistical heuristics and improves as it observes more calls.
TOIN is **local and observation-only**: it aggregates statistics on your own machine (or your own proxy instance). The learned patterns stay on your machine and are not shared across users. When the anonymous session beacon is on (the default; see [Security model](/docs/security-model#the-beacon-is-on-by-default)), a content-free structural descriptor of each tool output (bucketed field count, nesting depth, shape flags) and token counts are included in it; the same switches turn that off. For a brand-new tool type it falls back to statistical heuristics and improves as it observes more calls.
## What Headroom does not rewrite
+47
View File
@@ -21,6 +21,53 @@ See [Runtime Rollouts](/docs/runtime-rollouts) for policy, provenance, and
contributor rules.
If Codex history disappeared after using an older wrapper, see [Recover Codex State](/docs/codex-recovery) before wrapping Codex again.
## Proxy request-body admission
| Variable | Default | Purpose |
|----------|---------|---------|
| `HEADROOM_REQUEST_BODY_MAX_BYTES` | `104857600` (100 MiB) | Hard whole-request ceiling, resolved at process startup. Must be a positive integer. |
The ceiling applies to the complete history, not each message or text chunk.
Declared oversized HTTP bodies are rejected before reading them; chunked bodies
are metered as they arrive. Oversized wire bodies return HTTP 413 by default,
before JSON parsing, image classification, tokenization, or compression.
Decompressed gzip, deflate, Brotli, and Zstandard bodies obey the same ceiling;
`/v1/compress` returns the same size refusal for oversized decoded bodies,
including requests with `x-headroom-bypass: true`.
The previous 100 MiB admission default is retained. Anthropic Messages,
OpenAI Chat/Responses, Gemini generateContent, and Bedrock InvokeModel HTTP
routes have a separate **16 MiB compression threshold**, measured on the decoded body.
Larger admitted histories skip body normalization, image processing, memory
injection, tokenization, and compression. Native upstreams receive the original
decoded JSON bytes; obsolete compression headers are removed. Streaming and
non-streaming responses retain their normal protocol.
Required auth-specific wire settings still apply: ChatGPT Responses receives
`store: false` even when the client omitted it or set it to true. This
reserializes the body only when that setting needs rewriting; an already-valid
body remains byte-faithful. Vertex route-forced streaming is retained even
without a `stream` body field.
Envelope/model routing currently requires one JSON parse. Configured or
per-request translating backends still perform the necessary wire conversion,
but do not run request-side compression. Passthrough outcomes report zero
compression savings and use provider-reported usage when available; missing
usage is not presented as a local token measurement.
The soft threshold applies to HTTP forwarding, not explicit `/v1/compress`
calls or WebSocket messages. Auth, routing, rate and budget gates remain in
force. Operators can choose a different hard ceiling and restart the proxy:
```bash
HEADROOM_REQUEST_BODY_MAX_BYTES=33554432 headroom proxy
```
Neither threshold is a process-memory limit or a mechanism for cancelling
workers already running. JSON decoding, transport buffers, protocol translation,
and third-party ASGI middleware still require memory; bound concurrency
separately on small hosts.
## SDK Modes (`default_mode` / `headroom_mode`)
These modes apply to SDK usage via `HeadroomClient(default_mode=...)` or per-request `headroom_mode=...`. They are **not** the same as the proxy `--mode` flag.
+10 -1
View File
@@ -137,7 +137,7 @@ HEADROOM_SAVINGS_PROFILE=agent-90 headroom proxy --port 8787
| `--memory-top-k` | `10` | Number of memories to inject as context |
| `--learn` | `false` | Enable live traffic learning; implies `--memory` |
| `--no-learn` | `false` | Explicitly disable traffic learning |
| `--min-evidence` | `5` | Minimum observations before a learned pattern is persisted |
| `--min-evidence` | `5` | Minimum observations before a learned pattern is persisted; `1` saves the first sighting |
| `--codex-wire-debug` | `false` | Write local Codex wire snapshots and matching proxy log traces |
| `HEADROOM_COMPRESS_PASSTHROUGH` | `0` | Also compress custom proxy paths that fall through to the catch-all handler (OpenAI Responses-shaped bodies, path ends in `/responses`). No `--compress-passthrough` flag on `headroom proxy`; the direct module entry point (`python -m headroom.proxy.server`) does accept one |
@@ -148,6 +148,15 @@ headroom proxy --codex-wire-debug
HEADROOM_COMPRESS_PASSTHROUGH=1 headroom proxy
```
Live learning includes observations received while a save is queued or in flight.
A pattern is marked as saved only after its evidence total is persisted. A full
queue, failed save or cancellation retains the accumulated evidence for the next
sighting and the shutdown snapshot.
With LocalBackend, recovery reuses a row that committed before indexing failed
instead of inserting another memory, and refreshes its indexes. Reconciliation
is idempotent even when a cancelled SQLite update finishes late.
Automatic memory lookup renders a separate, bounded embedding query:
| Input | Maximum characters |
+27 -9
View File
@@ -53,9 +53,15 @@ and two separate guards bound it.
on a private, loopback, link-local, or otherwise non-public address: the confused-deputy
case where the proxy is used to reach cloud metadata (`169.254.169.254`) or RFC1918
hosts the caller cannot reach directly. NAT64-embedded IPv4 is unwrapped before the
check, so it cannot be smuggled through an IPv6 literal. A rejected override is
**ignored rather than fatal** — the request proceeds to the configured upstream and a
warning is logged. `HEADROOM_ALLOWED_BASE_URLS` (comma-separated hosts or URLs)
check, so it cannot be smuggled through an IPv6 literal. What happens to a rejected
override depends on the route. The OpenAI Chat Completions and Responses handlers
(`/v1/chat/completions`, `/v1/responses` and their unprefixed and Codex/ChatGPT-backend
aliases) **ignore** it: the request proceeds to the configured upstream and a warning
is logged. The other routes that read the header **reject** the request with HTTP 400
(`Rejected unsafe upstream base URL`) before contacting any upstream: Anthropic
Messages and token counting, the native Gemini routes, model listing, embeddings,
moderations, audio, and the generic passthrough. A client that sends an unsafe override
should be ready for either outcome. `HEADROOM_ALLOWED_BASE_URLS` (comma-separated hosts or URLs)
re-permits specific internal endpoints as an explicit operator choice: a bare host
permits every safe scheme and port on that host, a full URL permits only its exact
normalized origin. DNS resolution is bounded by `HEADROOM_UPSTREAM_RESOLVE_TIMEOUT_S`
@@ -97,7 +103,7 @@ and both the memory store and the Copilot token can be relocated independently.
| License cache | `<workspace>/license_cache.json` | Cached license envelope | Per envelope | No | Inherits umask |
| Settings store | `<workspace>/settings.json`, or `$HEADROOM_SETTINGS_PATH` | Dashboard-managed knob values — **including two header maps that carry credentials**, see [The settings store](#the-settings-store) | Until deleted | No | `0600`, inherited from `mkstemp` |
| Proxy log | `<workspace>/logs/proxy.log` + 5 rotations | INFO-level operational log, always on; carries the admin audit stream | 10 MB × 5 backups, then rotated out | No | Inherits umask |
| Request log | Opt-in, `--log-file` / `$HEADROOM_LOG_FILE` | JSONL per-request records; **full request and response content** when `--log-messages` is on | Until deleted | No | Inherits umask |
| Request log | Opt-in, `--log-file` / `$HEADROOM_LOG_FILE` | JSONL per-request records; **full request and response content** when `--log-messages` is on | Until deleted | No | `0600`, owner-only |
The config directory is `$HEADROOM_CONFIG_DIR`, else `$HEADROOM_WORKSPACE_DIR/config`,
else `~/.headroom/config`.
@@ -107,7 +113,7 @@ rests on filesystem permissions and on the security of the account the proxy run
### The settings store
The dashboard settings GUI persists a curated set of 59 knobs to
The dashboard settings GUI persists a curated set of 64 knobs to
`<workspace>/settings.json`. Two of them are marked secret, and both are credential-shaped:
`ANTHROPIC_TARGET_API_HEADERS` and `OPENAI_TARGET_API_HEADERS` are free-form JSON header
maps merged into forwarded requests — the field's own help text gives
@@ -296,7 +302,7 @@ credential-bearing traffic through it.
The settings routes are the wider surface, and they should be read as part of the same
threat model as `/admin/*` rather than as a separate UI concern. `POST /settings`
validates against the 59-field registry described under
validates against the 64-field registry described under
[The settings store](#the-settings-store) and writes the result to disk;
`POST /settings/apply` does the same and then **restarts the proxy** so the values take
effect (self-restarting on service deployments, returning the host command under Docker).
@@ -330,7 +336,7 @@ logger, not a file of its own, so in a default install it lands in
The loopback and same-origin gates are an effective boundary against remote and
browser-based attackers. They are not a boundary against local ones. Any process that can
bind a loopback socket as your user can flip the seven runtime knobs, write the 59
bind a loopback socket as your user can flip the ten runtime knobs, write the 64
settings and restart the proxy to apply them, read `/v1/retrieve*`, and reach the debug
routes. There is no token, no mTLS, and no per-caller authorization — the guards
distinguish *where a request came from*, not *who sent it*.
@@ -349,12 +355,14 @@ Beyond your configured LLM providers, Headroom may contact:
| `pypi.org` | Daily version check for update notices | On | `HEADROOM_UPDATE_CHECK=off`; also skipped in `--stateless`, in CI, and from source checkouts |
| `huggingface.co` | Downloads models for the optional ML compression paths | On first use of those features | Pre-provision the model cache, or leave the features disabled |
| `app.headroomlabs.ai` | License validation and usage reporting | Only with a license key configured | Omit the license key |
| `api.anthropic.com` | Subscription usage poll (`/api/oauth/usage`) for the quota panel; sends your Claude OAuth token as a bearer credential | On, but it only polls when a token is found (`CLAUDE_CODE_OAUTH_TOKEN` or the Claude credentials file); every 300 s | `HEADROOM_NO_SUBSCRIPTION_TRACKING=1` or `HEADROOM_OFFLINE` |
| `api.github.com` | GitHub Copilot quota poll (`/copilot_internal/user`); sends a GitHub token as a bearer credential | On, but it only polls when one of `GITHUB_COPILOT_GITHUB_TOKEN`, `GITHUB_TOKEN`, `COPILOT_GITHUB_TOKEN` or `GITHUB_COPILOT_API_TOKEN` is set; every 60 s | No dedicated switch found. Unset those variables, or use `HEADROOM_OFFLINE` |
| `mobile.events.data.microsoft.com` | ONNX Runtime's own usage telemetry to Microsoft (ORT 1.31+ on Linux and macOS): hashed device id, model file and graph names, model hashes, execution providers, hardware ids | **Off**: importing `headroom` sets `ORT_DISABLE_TELEMETRY=1` unless it is already set | Leave `ORT_DISABLE_TELEMETRY` unset or `1`; setting it to `0` turns ORT's telemetry back on |
### The beacon is on by default
This is the one egress path that is enabled without any action from you, so it warrants
its own treatment. It is governed by `HEADROOM_BEACON` and is **opt-out**
This is the one egress path that is enabled by default whatever credentials you have, so it
warrants its own treatment. It is governed by `HEADROOM_BEACON` and is **opt-out**
(`BEACON_DEFAULT_ON = True`). Session summaries are POSTed to
`https://headroom-beacon.headroom-beacon.workers.dev/v1/logs`, currently a Cloudflare
`workers.dev` address.
@@ -372,6 +380,16 @@ snapshot — not a delta — carrying:
latency/overhead in milliseconds, passthrough turns, and response-cache hits.
- **Context**: request sources (`proxy` / `mcp`), the sorted provider and model names seen,
a failure count, and those failures split by HTTP status code.
- **Schema v2 additions** (`schema_version: 2`, additive over v1): `quality` (tokens re-read
because compression removed them), `hist` (fixed-bucket distributions), `shapes` (content
type and strategy counts, plus a bucketed structural descriptor per tool output: field
count rounded down to a power of two, nesting depth and shape-flag letters, never field
names or TOIN's structure hash), `cache` (prompt-cache hit rate by gap since the previous
turn), `trajectory`, `clients` (harness name and turn count), `errors` (4xx counts by
status), `strata` (output-shaper A/B arm and request features), and `config` (the values
of 15 allow-listed `HEADROOM_*` variables, each reduced to a short slug; the list is
`_CONFIG_ENV_KEYS` in `headroom/telemetry/session.py`). `headroom telemetry --show` prints
the exact payload.
Alongside each report go resource attributes: a random per-install UUID, Headroom version,
OS type, CPU architecture, and — when known — install mode and detected stack.
+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,
+7 -1
View File
@@ -87,7 +87,7 @@ class SemanticCacheConfig:
# Similarity threshold for cache hit (0.0 - 1.0)
similarity_threshold: float = 0.95
# Maximum entries in cache
# Maximum entries in cache (must be positive)
max_entries: int = 1000
# TTL in seconds (0 = no expiry)
@@ -212,7 +212,13 @@ class SemanticCache:
Returns:
Cache key for the entry
Raises:
ValueError: If max_entries is not positive.
"""
if self.config.max_entries <= 0:
raise ValueError("max_entries must be positive")
self._cleanup_expired()
# Create cache key. Prefer the full-context hash: two requests that share
+17
View File
@@ -132,6 +132,23 @@ class CCRResponseHandler:
self._retrieval_count = 0
self._retrieval_count_lock = __import__("threading").Lock()
def serve_withheld_retrievals(self, tool_calls: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""Run Anthropic ``headroom_retrieve`` calls withheld from a stream.
Returns one ``tool_result`` block per call that names a hash, in call
order, for the continuation round the streaming path sends upstream.
A call without a parsable hash gets no result, so the caller can tell
the round cannot be replayed.
"""
ccr_calls, _ = self._parse_ccr_tool_calls({"content": tool_calls}, "anthropic")
if not ccr_calls:
return []
with self._retrieval_count_lock:
self._retrieval_count += len(ccr_calls)
results = [self._execute_retrieval(call) for call in ccr_calls]
message = self._create_tool_result_message(results, "anthropic")
return list(message.get("content", []))
def has_ccr_tool_calls(
self,
response: dict[str, Any],
+3
View File
@@ -481,6 +481,9 @@ def _run_memory_eval(
# Create answer function based on provider
answer_fn = None
if answer_model:
from headroom.offline import guard_egress
guard_egress(f"{answer_model} API for the memory eval answers")
try:
pin_litellm_cost_map_local()
import litellm
+3
View File
@@ -387,6 +387,9 @@ def _make_llm_judge(model: str) -> Any:
best-effort: any failure (no key, parse error) returns None so the caller
falls back to the heuristic.
"""
from headroom.offline import guard_egress
guard_egress(f"{model} API for the headroom learn --llm-judge verbosity judge")
def judge(signals: dict) -> tuple[int, str] | None:
try:
+6 -4
View File
@@ -982,10 +982,12 @@ class HeadroomConfig:
"""
if model in self.model_context_limits:
return self.model_context_limits[model]
# Try prefix matching for versioned model names
for known_model, limit in self.model_context_limits.items():
if model.startswith(known_model):
return limit
# Try prefix matching for versioned model names. The longest prefix
# wins, so "gpt-4o-2024-08-06" uses a "gpt-4o" override rather than a
# "gpt-4" one that happens to come first in the dict.
matches = [known for known in self.model_context_limits if model.startswith(known)]
if matches:
return self.model_context_limits[max(matches, key=len)]
return None
+2
View File
@@ -176,6 +176,8 @@ def create_litellm_judge(
"LiteLLM package required for LLM judge. Install with: pip install litellm"
) from e
guard_egress(f"{model} API for the memory eval LLM judge")
def judge(question: str, ground_truth: str, prediction: str) -> tuple[float, str]:
prompt = JUDGE_PROMPT.format(
question=question,
+3
View File
@@ -29,6 +29,8 @@ from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
from headroom.offline import guard_egress
logger = logging.getLogger(__name__)
# LoCoMo dataset URL
@@ -231,6 +233,7 @@ def download_locomo(cache_dir: Path | None = None) -> Path:
logger.info(f"Using cached LoCoMo dataset: {cache_path}")
return cache_path
guard_egress("LoCoMo eval-dataset download", LOCOMO_URL)
logger.info(f"Downloading LoCoMo dataset from {LOCOMO_URL}...")
urllib.request.urlretrieve(LOCOMO_URL, cache_path) # nosec B310
logger.info(f"Downloaded to {cache_path}")
+3
View File
@@ -323,6 +323,9 @@ OUTPUT FORMAT (JSON only):
IMPORTANT: Every event MUST have a specific date. If you cannot determine the date, state it as "around {session_datetime}"."""
from headroom.offline import guard_egress
guard_egress(f"{self.config.extraction_model} API for memory eval extraction")
try:
response = litellm.completion(
model=self.config.extraction_model,
+6
View File
@@ -467,6 +467,9 @@ Categories to use:
# Get just the save tool
save_tool = next(t for t in MEMORY_TOOLS if t["function"]["name"] == "memory_save")
from headroom.offline import guard_egress
guard_egress(f"{self._config.save_model} API for the memory eval save phase")
for session in conversation.sessions:
# Format session as dialogue
session_text = f"\n[Session Date: {session.datetime}]\n"
@@ -689,6 +692,9 @@ The answer should contain ONLY the specific information requested, nothing more.
max_rounds = 3
final_answer = "Information not found"
from headroom.offline import guard_egress
guard_egress(f"{self._config.answer_model} API for the memory eval answers")
for round_num in range(max_rounds):
try:
response = await asyncio.to_thread(
+3 -7
View File
@@ -604,13 +604,9 @@ def stop_runtime(manifest: DeploymentManifest) -> None:
pass
for _ in range(_STOP_POLL_ATTEMPTS):
if not pid_alive(pid):
if not pid_alive(pid):
_clear_pid(manifest.profile, expected_pid=pid)
return
raise RuntimeError(
f"Cannot stop deployment '{manifest.profile}': runtime identity changed"
)
if _process_identity(pid, manifest) is not True:
_clear_pid(manifest.profile, expected_pid=pid)
return
if _process_identity(pid, manifest) is False:
raise RuntimeError(
f"Cannot stop deployment '{manifest.profile}': runtime identity changed"
)
+57 -11
View File
@@ -181,6 +181,19 @@ def _render_windows_runner(
]
def _render_windows_vbs_launcher(vbs_path: Path, ps1_path: Path) -> ArtifactRecord:
"""Render a non-interactive VBS launcher that runs powershell with SW_HIDE (no console window)."""
vbs_path.parent.mkdir(parents=True, exist_ok=True)
vbs_content = (
'Set WshShell = CreateObject("WScript.Shell")\r\n'
'Set fso = CreateObject("Scripting.FileSystemObject")\r\n'
"scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)\r\n"
f'WshShell.Run "powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File """ & scriptDir & "\\{ps1_path.name}""", 0, True\r\n'
)
_write_private_text(vbs_path, vbs_content, OWNER_ONLY_FILE_MODE)
return ArtifactRecord(kind="script", path=str(vbs_path))
def render_runner_scripts(manifest: DeploymentManifest) -> list[ArtifactRecord]:
"""Render runner/watchdog scripts for the deployment profile."""
@@ -356,8 +369,15 @@ def _windows_task_xml(
" <StartWhenAvailable>true</StartWhenAvailable>\n"
" </Settings>\n"
' <Actions Context="Author">\n'
f" <Exec>\n <Command>{_xml_escape(command)}</Command>\n </Exec>\n"
" </Actions>\n"
+ (
" <Exec>\n"
" <Command>wscript.exe</Command>\n"
f' <Arguments>//B //Nologo "{_xml_escape(command)}"</Arguments>\n'
" </Exec>\n"
if command.lower().endswith(".vbs")
else f" <Exec>\n <Command>{_xml_escape(command)}</Command>\n </Exec>\n"
)
+ " </Actions>\n"
"</Task>\n"
)
@@ -615,10 +635,15 @@ def install_supervisor(manifest: DeploymentManifest, *, start: bool = True) -> l
[name for name, _task_cmd, _trigger_xml in preferred_tasks],
must_delete=must_delete,
)
vbs_record = _render_windows_vbs_launcher(
Path(health_cmd).with_suffix(".vbs"),
Path(health_cmd).with_suffix(".ps1"),
)
records.append(vbs_record)
_register_windows_task(
health_name,
_windows_task_xml(
health_cmd,
vbs_record.path,
trigger_xml=_windows_health_trigger(),
scope=manifest.scope,
logon_type="InteractiveToken",
@@ -646,6 +671,11 @@ def install_supervisor(manifest: DeploymentManifest, *, start: bool = True) -> l
)
# `/End` and `/Delete` report a missing task as "cannot find the file specified";
# `/Change` reports it as "does not exist in the system".
_SCHTASKS_ABSENT_TEXT = ("cannot find the file specified", "does not exist in the system")
def start_supervisor(manifest: DeploymentManifest) -> None:
"""Start the installed supervisor or runtime for a deployment."""
@@ -687,16 +717,32 @@ def start_supervisor(manifest: DeploymentManifest) -> None:
_bootstrap_with_retry(domain, plist_path, action="start")
return
if _is_windows():
subprocess.run(
["schtasks", "/Change", "/TN", f"{manifest.service_name}-health", "/ENABLE"],
check=True,
health = f"{manifest.service_name}-health"
result = run(
["schtasks", "/Change", "/TN", health, "/ENABLE"],
capture_output=True,
text=True,
)
subprocess.run(["schtasks", "/Run", "/TN", f"{manifest.service_name}-startup"], check=True)
_require_removal_success(
result,
"schtasks health enable",
absent_text=_SCHTASKS_ABSENT_TEXT,
)
result = run(
["schtasks", "/Run", "/TN", f"{manifest.service_name}-startup"],
capture_output=True,
text=True,
)
detail = _result_text(result)
if result.returncode != 0:
if any(text in detail.lower() for text in _SCHTASKS_ABSENT_TEXT):
from .runtime import runtime_ready, start_detached_agent
# `/End` and `/Delete` report a missing task as "cannot find the file specified";
# `/Change` reports it as "does not exist in the system".
_SCHTASKS_ABSENT_TEXT = ("cannot find the file specified", "does not exist in the system")
if not runtime_ready(manifest):
start_detached_agent(manifest.profile)
return
raise click.ClickException(f"schtasks startup run failed: {detail}")
return
def stop_supervisor(manifest: DeploymentManifest) -> None:
+6
View File
@@ -1093,7 +1093,12 @@ def _call_llm(
the provider: "claude-*" → Anthropic, "gpt-*" → OpenAI, "gemini/*" → Google, etc.
For CLI-based models (ending in "-cli"), delegates to ``_call_cli_llm``.
"""
from headroom.offline import guard_egress
if model in _CLI_MODEL_IDS:
# The provider CLI sends the digest to its own API, so refuse before
# spawning it rather than letting the subprocess reach the network.
guard_egress(f"{model} provider CLI for headroom learn session analysis")
return _call_cli_llm(digest, model, on_progress=on_progress)
pin_litellm_cost_map_local()
@@ -1108,6 +1113,7 @@ def _call_llm(
if model.startswith("claude"):
api_base = "https://api.anthropic.com"
guard_egress(f"{model} API for headroom learn session analysis", api_base)
response = litellm.completion(
model=model,
messages=[
+16 -52
View File
@@ -452,49 +452,18 @@ class SQLiteMemoryStore:
conditions: list[str] = []
params: list[Any] = []
# Hierarchical scope filtering
# Every supplied scope narrows the match. IDs can be reused under
# different parents, so no child scope can replace a parent predicate.
if filter.user_id is not None:
conditions.append("user_id = ?")
params.append(filter.user_id)
# Hierarchical filtering: when filtering by user_id only,
# return USER-level and below (all that user's memories)
# This is implicit - we just filter by user_id
if filter.session_id is not None:
conditions.append("session_id = ?")
params.append(filter.session_id)
# agent_id and turn_id are independent narrowing constraints:
# turn_id must be applied even when agent_id is absent. Nesting
# the turn_id check inside the agent_id block dropped the turn
# filter for a (session_id + turn_id, no agent_id) query, so it
# returned the whole session instead of the one turn.
if filter.agent_id is not None:
conditions.append("agent_id = ?")
params.append(filter.agent_id)
if filter.turn_id is not None:
conditions.append("turn_id = ?")
params.append(filter.turn_id)
elif filter.agent_id is not None:
# Agent without session - unusual but supported
conditions.append("agent_id = ?")
params.append(filter.agent_id)
elif filter.turn_id is not None:
# Turn without session/agent - unusual but supported
conditions.append("turn_id = ?")
params.append(filter.turn_id)
elif filter.session_id is not None:
# Session without user - filter by session only
if filter.session_id is not None:
conditions.append("session_id = ?")
params.append(filter.session_id)
elif filter.agent_id is not None:
# Agent without user/session
if filter.agent_id is not None:
conditions.append("agent_id = ?")
params.append(filter.agent_id)
elif filter.turn_id is not None:
# Turn only
if filter.turn_id is not None:
conditions.append("turn_id = ?")
params.append(filter.turn_id)
@@ -878,22 +847,17 @@ class SQLiteMemoryStore:
Returns:
Number of memories deleted.
"""
conditions = ["user_id = ?"]
params: list[Any] = [user_id]
if turn_id is not None:
# Clear only specific turn
conditions.append("turn_id = ?")
params.append(turn_id)
elif agent_id is not None:
# Clear agent and its turns
conditions.append("agent_id = ?")
params.append(agent_id)
elif session_id is not None:
# Clear session and its agents/turns
conditions.append("session_id = ?")
params.append(session_id)
# If only user_id, clear all user's memories
# Use the same scope as the query that selects IDs for index cleanup.
# Clearing includes historical versions as well as active memories.
conditions, params = self._build_query_conditions(
MemoryFilter(
user_id=user_id,
session_id=session_id,
agent_id=agent_id,
turn_id=turn_id,
include_superseded=True,
)
)
where_clause = " AND ".join(conditions)
+200 -78
View File
@@ -26,10 +26,12 @@ import os
import re
import sqlite3
import time
import uuid
from collections import OrderedDict
from dataclasses import dataclass, field
from datetime import datetime, timezone
from enum import Enum
from itertools import chain
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar
@@ -539,6 +541,10 @@ class TrafficLearner:
# Background save queue
self._save_queue: asyncio.Queue[ExtractedPattern] = asyncio.Queue(maxsize=100)
# Keep queued/in-flight dedup separate from confirmed persistence.
self._queued_patterns: dict[str, ExtractedPattern] = {}
# Keep an attempt's idempotency key until its whole evidence total is durable.
self._save_tokens: dict[str, str] = {}
self._save_task: asyncio.Task[None] | None = None
self._stopping = False
@@ -565,7 +571,7 @@ class TrafficLearner:
# Hydrate persisted dedup state before workers spin up so cross-session
# re-sightings bump existing rows instead of creating duplicates.
await self._hydrate_persisted_state()
# Rehydrate sub-threshold evidence from the previous process. Without
# Rehydrate pending evidence from the previous process. Without
# this every restart zeroed pending counts, so users with short or
# restart-heavy sessions could never cross min_evidence.
await self._load_pending_state()
@@ -593,27 +599,15 @@ class TrafficLearner:
except asyncio.CancelledError:
pass
# Drain any patterns left in the queue (worker may have been cancelled mid-flight)
# Failed or cancelled saves remain pending for the shutdown snapshot.
while not self._save_queue.empty():
pattern = self._save_queue.get_nowait()
try:
pattern = self._save_queue.get_nowait()
if self._backend is not None:
await self._backend.save_memory(
content=pattern.content,
user_id=self._user_id,
importance=pattern.importance,
metadata={
"source": "traffic_learner",
"category": pattern.category.value,
"evidence_count": pattern.evidence_count,
**pattern.metadata,
},
)
self._patterns_saved += 1
except Exception:
break
await self._save_pattern(pattern)
except Exception as exc:
logger.warning("Traffic learner shutdown save failed: %r", exc)
# Snapshot sub-threshold evidence so it survives the restart.
# Snapshot pending evidence so it survives the restart.
await self._save_pending_state()
# Final flush on shutdown — bypass debounce.
@@ -1288,6 +1282,11 @@ class TrafficLearner:
self._patterns_extracted += 1
self._flush_dirty = True
h = pattern.content_hash
queued = self._queued_patterns.get(h)
if queued is not None:
queued.evidence_count += 1
queued.metadata["last_seen_at"] = datetime.now(timezone.utc).isoformat()
return
# Already saved — bump the persisted row's evidence_count rather
# than creating a duplicate.
@@ -1308,42 +1307,86 @@ class TrafficLearner:
# Bound the pending accumulator so one-off patterns can't grow it
# without limit; drop the least-recently-corroborated pending entry.
if len(self._pattern_counts) >= self._max_pending_patterns:
self._pattern_counts.popitem(last=False)
evicted, _ = self._pattern_counts.popitem(last=False)
self._save_tokens.pop(evicted, None)
self._pattern_counts[h] = (pattern, 1)
return # First sighting — wait for more evidence
# Check if evidence threshold met
_, count = self._pattern_counts[h]
pattern, count = self._pattern_counts[h]
now_iso = datetime.now(timezone.utc).isoformat()
pattern.metadata.setdefault("first_seen_at", now_iso)
pattern.metadata["last_seen_at"] = now_iso
if count >= self._min_evidence:
# Ready to save
del self._pattern_counts[h]
self._saved_hashes.add(h)
# Trim saved hashes to prevent unbounded growth, and drop the evicted
# hash's persisted-id entry in lockstep. ``_persisted_ids`` is only
# ever read behind an ``h in _saved_hashes`` guard (the dedup check
# above), so an id for a hash no longer tracked is dead weight;
# without this it grew one entry per distinct persisted pattern for
# the whole process lifetime while ``_saved_hashes`` stayed bounded.
if len(self._saved_hashes) > self._dedup_window:
# Remove oldest (arbitrary, set is unordered, but prevents growth)
evicted = self._saved_hashes.pop()
self._persisted_ids.pop(evicted, None)
# Persist the real accumulated count, not the dataclass default.
pattern.evidence_count = count
try:
self._save_queue.put_nowait(pattern)
except asyncio.QueueFull:
logger.debug("Traffic learner save queue full, dropping pattern")
logger.debug("Traffic learner save queue full, keeping pending evidence")
return
del self._pattern_counts[h]
self._queued_patterns[h] = pattern
async def _save_worker(self) -> None:
"""Background worker that persists patterns to memory backend."""
while True:
def _restore_pattern(self, pattern: ExtractedPattern) -> None:
"""Keep corroborated evidence eligible after an unsuccessful save."""
h = pattern.content_hash
if (
h not in self._pattern_counts
and len(self._pattern_counts) >= self._max_pending_patterns
):
evicted, _ = self._pattern_counts.popitem(last=False)
self._save_tokens.pop(evicted, None)
self._pattern_counts[h] = (pattern, pattern.evidence_count)
self._pattern_counts.move_to_end(h)
self._flush_dirty = True
async def _find_persisted_pattern(
self, pattern: ExtractedPattern, save_token: str
) -> tuple[str, int] | None:
"""Resolve an uncertain local save before another insert is allowed."""
db_path = _resolve_backend_db_path(self._backend)
if db_path is None or not db_path.exists():
return None
def _read() -> tuple[str, int] | None:
conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)
try:
pattern = await self._save_queue.get()
if self._backend is None:
continue
row = conn.execute(
"SELECT id, COALESCE(json_extract(metadata, '$.learner_saved_evidence'), 0) "
"FROM memories WHERE user_id = ? AND content = ? "
"AND json_extract(metadata, '$.source') = 'traffic_learner' "
"AND json_extract(metadata, '$.category') = ? "
"AND json_extract(metadata, '$.content_hash') = ? "
"AND json_extract(metadata, '$.learner_save_token') = ? "
"ORDER BY json_extract(metadata, '$.last_seen_at') DESC, id DESC LIMIT 1",
(
self._user_id,
pattern.content,
pattern.category.value,
pattern.content_hash,
save_token,
),
).fetchone()
finally:
conn.close()
return (row[0], int(row[1])) if row is not None else None
return await asyncio.to_thread(_read)
async def _save_pattern(self, pattern: ExtractedPattern) -> None:
h = pattern.content_hash
try:
if self._backend is None:
self._restore_pattern(pattern)
return
existing: tuple[str, int] | None = None
save_token = self._save_tokens.get(h)
if save_token is None:
save_token = uuid.uuid4().hex
self._save_tokens[h] = save_token
else:
existing = await self._find_persisted_pattern(pattern, save_token)
if existing is None:
saved_count = pattern.evidence_count
now_iso = datetime.now(timezone.utc).isoformat()
memory = await self._backend.save_memory(
content=pattern.content,
@@ -1352,27 +1395,62 @@ class TrafficLearner:
metadata={
"source": "traffic_learner",
"category": pattern.category.value,
"evidence_count": pattern.evidence_count,
"evidence_count": saved_count,
"first_seen_at": now_iso,
"last_seen_at": now_iso,
**pattern.metadata,
"content_hash": h,
"learner_save_token": save_token,
"learner_saved_evidence": saved_count,
},
)
self._patterns_saved += 1
# Track id so future re-sightings bump this row — but only while
# the hash is still within the dedup window. If it was evicted
# from ``_saved_hashes`` between enqueue and now, recording its id
# would re-leak an entry that can never be read again (the dedup
# read at the top of ``_accumulate`` is gated on ``_saved_hashes``).
memory_id = getattr(memory, "id", None)
if memory_id is not None and pattern.content_hash in self._saved_hashes:
self._persisted_ids[pattern.content_hash] = memory_id
logger.debug(f"Traffic learner saved pattern: {pattern.content[:80]}")
else:
memory_id, saved_count = existing
# Observations can arrive during either the save or its index refresh.
# Keep ownership queued until every observed count is durable.
refresh = existing is not None
while refresh or pattern.evidence_count > saved_count:
if memory_id is None:
raise RuntimeError("Saved learner pattern has no memory id")
target_count = pattern.evidence_count
last_seen = pattern.metadata.get("last_seen_at")
updated = await self._bump_persisted_evidence(
memory_id,
count=0,
last_seen_at=last_seen if isinstance(last_seen, str) else None,
save_attempt=(save_token, target_count),
)
if not updated:
raise RuntimeError("Learner evidence update was not persisted")
saved_count = target_count
refresh = False
except (Exception, asyncio.CancelledError):
self._restore_pattern(pattern)
raise
else:
self._patterns_saved += 1
self._saved_hashes.add(h)
self._save_tokens.pop(h, None)
if len(self._saved_hashes) > self._dedup_window:
self._persisted_ids.pop(self._saved_hashes.pop(), None)
if memory_id is not None and h in self._saved_hashes:
self._persisted_ids[h] = memory_id
logger.debug("Traffic learner saved pattern: %s", pattern.content[:80])
finally:
self._queued_patterns.pop(h, None)
async def _save_worker(self) -> None:
"""Background worker that persists patterns to memory backend."""
while True:
try:
pattern = await self._save_queue.get()
await self._save_pattern(pattern)
except asyncio.CancelledError:
break
except Exception as e:
logger.warning(f"Traffic learner save failed: {e}")
except Exception as exc:
logger.warning("Traffic learner save failed: %r", exc)
async def _hydrate_persisted_state(self) -> None:
"""Load existing traffic_learner rows into _saved_hashes / _persisted_ids.
@@ -1446,14 +1524,14 @@ class TrafficLearner:
self._persisted_ids[h] = memory_id
def _pending_state_path(self) -> Path | None:
"""Sidecar JSON next to memory.db holding sub-threshold evidence."""
"""Sidecar JSON next to memory.db holding unsaved evidence."""
db_path = _resolve_backend_db_path(self._backend)
if db_path is None:
return None
return db_path.with_name("pending_patterns.json")
async def _save_pending_state(self) -> None:
"""Snapshot the pending accumulator so evidence survives restarts.
"""Snapshot pending and queued evidence so it survives restarts.
Best-effort: failures are logged at debug and never propagate — the
accumulator degrades gracefully back to per-process behavior.
@@ -1470,8 +1548,12 @@ class TrafficLearner:
"entity_refs": pattern.entity_refs,
"metadata": pattern.metadata,
"content_hash": h,
"save_token": self._save_tokens.get(h),
}
for h, (pattern, count) in self._pattern_counts.items()
for h, (pattern, count) in chain(
self._pattern_counts.items(),
((h, (p, p.evidence_count)) for h, p in self._queued_patterns.items()),
)
]
def _write() -> None:
@@ -1486,12 +1568,12 @@ class TrafficLearner:
logger.debug("Traffic learner pending-state save failed: %s", e)
async def _load_pending_state(self) -> None:
"""Rehydrate sub-threshold evidence saved by a previous process.
"""Rehydrate unsaved evidence from a previous process.
Runs after _hydrate_persisted_state so entries whose pattern crossed
the threshold in the meantime (hash now in _saved_hashes) are skipped;
their re-sightings bump the persisted row instead. Unreadable or
malformed files are ignored — the next snapshot overwrites them.
Runs after _hydrate_persisted_state. Confirmed patterns are skipped,
but an interrupted save's token keeps its pending evidence eligible
for reconciliation with the committed row. Unreadable or malformed
files are ignored — the next snapshot overwrites them.
"""
path = self._pending_state_path()
if path is None or not path.exists():
@@ -1507,8 +1589,16 @@ class TrafficLearner:
for entry in entries[: self._max_pending_patterns]:
try:
h = entry["content_hash"]
if not h or h in self._saved_hashes or h in self._pattern_counts:
if not h or h in self._pattern_counts:
continue
save_token = entry.get("save_token")
if not isinstance(save_token, str) or not save_token:
save_token = None
if h in self._saved_hashes:
if save_token is None:
continue
self._saved_hashes.discard(h)
self._persisted_ids.pop(h, None)
pattern = ExtractedPattern(
category=PatternCategory(entry["category"]),
content=entry["content"],
@@ -1518,30 +1608,59 @@ class TrafficLearner:
content_hash=h,
)
self._pattern_counts[h] = (pattern, max(1, int(entry.get("count", 1))))
if save_token is not None:
self._save_tokens[h] = save_token
except Exception:
continue
async def _bump_persisted_evidence(self, memory_id: str) -> None:
"""Atomically increment a persisted row's metadata.evidence_count."""
async def _bump_persisted_evidence(
self,
memory_id: str,
count: int = 1,
last_seen_at: str | None = None,
*,
save_attempt: tuple[str, int] | None = None,
) -> bool:
"""Atomically add evidence to a persisted row and refresh its indexes."""
db_path = _resolve_backend_db_path(self._backend)
if db_path is None or not db_path.exists():
return
return False
now_iso = datetime.now(timezone.utc).isoformat()
now_iso = last_seen_at or datetime.now(timezone.utc).isoformat()
def _bump() -> bool:
from ..fileperms import connect_private_sqlite
conn = connect_private_sqlite(db_path, what="memory store")
try:
cursor = conn.execute(
"UPDATE memories SET metadata = json_set("
"metadata, '$.evidence_count', "
"COALESCE(json_extract(metadata, '$.evidence_count'), 0) + 1, "
"'$.last_seen_at', ?"
") WHERE id = ?",
(now_iso, memory_id),
)
if save_attempt is None:
cursor = conn.execute(
"UPDATE memories SET metadata = json_set("
"metadata, '$.evidence_count', "
"COALESCE(json_extract(metadata, '$.evidence_count'), 0) + ?, "
"'$.last_seen_at', "
"MAX(COALESCE(json_extract(metadata, '$.last_seen_at'), ''), ?)"
") WHERE id = ?",
(count, now_iso, memory_id),
)
else:
# A cancelled worker's SQLite write can still finish. Apply
# only evidence beyond this save attempt's durable watermark.
save_token, target = save_attempt
cursor = conn.execute(
"UPDATE memories SET metadata = json_set("
"metadata, '$.evidence_count', "
"COALESCE(json_extract(metadata, '$.evidence_count'), 0) + "
"MAX(0, ? - COALESCE("
"json_extract(metadata, '$.learner_saved_evidence'), 0)), "
"'$.learner_saved_evidence', MAX(COALESCE("
"json_extract(metadata, '$.learner_saved_evidence'), 0), ?), "
"'$.last_seen_at', "
"MAX(COALESCE(json_extract(metadata, '$.last_seen_at'), ''), ?)"
") WHERE id = ? "
"AND json_extract(metadata, '$.learner_save_token') = ?",
(target, target, now_iso, memory_id, save_token),
)
conn.commit()
return cursor.rowcount > 0
finally:
@@ -1551,7 +1670,7 @@ class TrafficLearner:
updated = await asyncio.to_thread(_bump)
except Exception as e:
logger.debug("Traffic learner evidence bump failed for %s: %s", memory_id, e)
return
return False
refresh = getattr(self._backend, "refresh_memory_indexes", None)
if updated and refresh is not None:
@@ -1563,6 +1682,9 @@ class TrafficLearner:
memory_id,
e,
)
if save_attempt is not None:
raise
return updated
# =========================================================================
# Convenience: Extract from Anthropic messages format
+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
+14 -2
View File
@@ -20262,23 +20262,35 @@ function headroomShellEnv(proxyUrl, project, options) {
...options.backend ? { HEADROOM_BACKEND: options.backend } : {}
};
}
function openAiWireSuffix(pkg) {
function modelWireSuffix(pkg) {
if (!pkg?.startsWith("@opencode/ai/providers/")) return void 0;
if (pkg === "@opencode/ai/providers/openai-compatible" || pkg.endsWith("/chat")) {
return "/chat/completions";
}
if (pkg.endsWith("-responses") || pkg.endsWith("/responses")) return "/responses";
if (pkg === "@opencode/ai/providers/anthropic" || pkg === "@opencode/ai/providers/anthropic-compatible" || pkg.endsWith("/messages")) {
return "/messages";
}
return void 0;
}
function anthropicBaseKeepsPath(baseURL) {
try {
const pathname = trimTrailingSlashes(new URL(baseURL).pathname);
return pathname === "" || pathname === "/v1";
} catch {
return false;
}
}
function routeModelsThroughProxy(models, proxyUrl, project, excludeHosts, sessionToken) {
for (const model of models.list()) {
const providerID = String(model.providerID);
const modelID = String(model.id);
const provider = models.provider.get(providerID)?.provider;
const suffix = openAiWireSuffix(model.package ?? provider?.package);
const suffix = modelWireSuffix(model.package ?? provider?.package);
const baseURL = model.settings?.baseURL ?? provider?.settings?.baseURL;
if (!suffix || typeof baseURL !== "string") continue;
if (!modelBaseRoutesThroughProxy(baseURL, proxyUrl, excludeHosts)) continue;
if (suffix === "/messages" && !anthropicBaseKeepsPath(baseURL)) continue;
const upstream = new URL(baseURL);
models.update(providerID, modelID, (draft) => {
draft.settings = { ...draft.settings, baseURL: `${proxyUrl}/v1` };
+7 -1
View File
@@ -9,6 +9,7 @@ from typing import Any
from fastapi import FastAPI, HTTPException, Request, WebSocket
from fastapi.responses import Response
from headroom.log_safety import redact_url
from headroom.providers.cloudcode import normalize_cloudcode_passthrough_path
from headroom.providers.codex.endpoints import codex_backend_url
from headroom.providers.codex.headers import drop_header
@@ -108,6 +109,11 @@ def _register_provider_passthrough_route(
spec: ProviderPassthroughRoute,
) -> None:
async def provider_passthrough(request: Request):
# A reachability probe the token gate let through without a credential
# (/api/hello): answer it here, never relay it upstream anonymously.
if getattr(request.state, "headroom_local_hello_probe", False):
return Response(status_code=200)
# Honor the per-request upstream override like /v1/messages and the
# catch-all do. Clients that speak one wire format but authenticate
# against a custom OpenAI-compatible gateway must reach that gateway
@@ -523,7 +529,7 @@ def register_provider_routes(app: FastAPI, proxy: Any) -> None:
custom_base = request.headers.get("x-headroom-base-url", "").strip()
if custom_base:
if not await is_safe_upstream_url_async(custom_base):
logger.warning("rejecting unsafe x-headroom-base-url: %r", custom_base)
logger.warning("rejecting unsafe x-headroom-base-url: %s", redact_url(custom_base))
raise HTTPException(status_code=400, detail="Rejected unsafe upstream base URL")
# Only the destination is overridden: keep the provider the request's
# auth selects, so Anthropic-auth requests still get model-id
+3 -1
View File
@@ -87,7 +87,9 @@ def classify_auth_signals(signals: AuthSignals) -> AuthMode:
# Only the scheme is case-folded; the credential itself stays case-sensitive.
scheme, sep, credentials = auth.partition(" ")
if sep and scheme.lower() == "bearer":
token = credentials
# RFC 9110 §11.4 allows one or more spaces between the scheme and the
# credentials; trim them so `Bearer sk-ant-oat...` still matches.
token = credentials.lstrip(" ")
if token.startswith("sk-ant-oat"):
return AuthMode.OAUTH
if token.startswith("sk-ant-api") or token.startswith("sk-"):
+223 -125
View File
@@ -630,28 +630,41 @@ class AnthropicHandlerMixin:
return False
@staticmethod
def _outgoing_body_has_redeemable_marker(body: Any) -> bool:
"""Does the body about to be sent carry a marker retrieval could expand?
def _outgoing_redeemable_markers(
body: Any,
previous_forwarded_messages: list[dict[str, Any]] | None = None,
) -> tuple[bool, bool]:
"""Which owned markers does the outgoing body carry?
``headroom_retrieve`` exists only to expand a ``<<ccr:...>>`` marker, so
a request carrying none cannot benefit from the buffered path (#3071).
Returns ``(any_owned, new_owned)``. ``headroom_retrieve`` exists only to
expand a ``<<ccr:...>>`` marker, so a request carrying none cannot use
it (#3071).
``new_owned`` is a marker the model has not seen yet: one absent from
what this session forwarded last turn. With no forwarded history to
compare against, every owned marker counts as new. "New" is decided
against the forwarded history, not by message position: token mode can
compress an earlier tool result on this request, so a marker first
seen now may sit anywhere in the history.
Ownership is verified rather than shape-matched: the marker shape is not
unique to Headroom, and adopting another context tool's hash would send
the model to an endpoint that is guaranteed to miss (#2836). A hash that
survives ``verify_ownership`` is redeemable right now.
Errors are swallowed deliberately and answered ``True``. This gates a
wire-format decision, and the safe direction on an unexpected message
shape is the long-standing buffered behavior, not a silent change.
Errors are swallowed deliberately and answered ``(True, True)``. This
gates a wire-format decision, and the safe direction on an unexpected
message shape is the long-standing buffered behavior, not a silent
change.
"""
if not isinstance(body, dict):
return True
return True, True
messages = body.get("messages")
if not isinstance(messages, list) or not messages:
return False
return False, False
try:
from headroom.ccr.tool_injection import CCRToolInjector
from headroom.proxy.helpers import has_new_ccr_markers
probe = CCRToolInjector(
provider="anthropic",
@@ -660,12 +673,27 @@ class AnthropicHandlerMixin:
)
probe.scan_for_markers(messages)
if not probe.detected_hashes:
return False
return False, False
probe.verify_ownership()
return bool(probe.detected_hashes)
if not probe.detected_hashes:
return False, False
return True, has_new_ccr_markers(
current_detected_hashes=list(probe.detected_hashes),
previous_forwarded_messages=previous_forwarded_messages,
provider="anthropic",
)
except Exception: # pragma: no cover - defensive
logger.debug("CCR: marker probe failed; keeping the buffered path", exc_info=True)
return True
return True, True
@classmethod
def _outgoing_body_has_redeemable_marker(
cls,
body: Any,
previous_forwarded_messages: list[dict[str, Any]] | None = None,
) -> bool:
"""Does the outgoing body show the model an owned marker it has not seen?"""
return cls._outgoing_redeemable_markers(body, previous_forwarded_messages)[1]
@staticmethod
def _extract_anthropic_cache_ttl_metrics(usage: dict[str, Any] | None) -> tuple[int, int]:
@@ -1164,11 +1192,13 @@ class AnthropicHandlerMixin:
from headroom.proxy.helpers import (
MAX_MESSAGE_ARRAY_LENGTH,
MAX_REQUEST_BODY_SIZE,
RequestBodyTooLarge,
_get_image_compressor,
compute_turn_id,
read_request_json_with_bytes,
)
from headroom.proxy.modes import is_cache_mode, is_token_mode
from headroom.proxy.request_limit_policy import COMPRESSION_BODY_MAX_BYTES
from headroom.utils import extract_user_query
start_time = time.time()
@@ -1317,6 +1347,115 @@ class AnthropicHandlerMixin:
},
)
# Extract headers and tags
headers = dict(request.headers.items())
headers.pop("host", None)
headers.pop("content-length", None)
# The body reader content-decodes the inbound body (zstd/gzip/deflate/br)
# before either forwarding path sends plain JSON upstream.
# A stale content-encoding makes the upstream try to decompress
# already-decoded JSON and reject it with HTTP 400 (#1542 — same
# fix the /v1/responses path already carries).
headers.pop("content-encoding", None)
headers.pop("transfer-encoding", None)
# Strip accept-encoding so httpx negotiates its own encoding.
# Edge proxies (Cloudflare Workers, etc.) may forward "br, zstd" which
# the upstream can honor; if httpx lacks brotli support the response
# body is undecipherable → 502.
headers.pop("accept-encoding", None)
tags = extract_tags(headers)
from headroom.proxy.savings_attribution import bind_scope
bind_scope(tags, request.scope)
# Identify the harness (codex / claude-code / aider / etc.)
# from User-Agent or X-Client. Surfaced via the funnel into
# PERF logs and RequestLog.tags — see RequestOutcome.client.
client = classify_client(headers, default="claude")
# PR-A5 (P5-49): strip internal x-headroom-* from upstream-bound
# headers AFTER `_extract_tags` reads them. Inbound bypass gating
# uses the original `request.headers`; memory user-id
# is read from `request.headers` below if needed. From this
# point on, `headers` is the upstream-bound copy.
from headroom.proxy.helpers import (
_strip_internal_headers,
apply_keep_last_turns,
log_outbound_headers,
merge_extra_headers,
)
_pre_strip_count = sum(1 for k in headers if k.lower().startswith("x-headroom-"))
headers = _strip_internal_headers(headers)
# `upstream_base_url` is the per-request `x-headroom-base-url`
# override when the client sent one. These headers are secrets, so
# they only travel to a host the operator designated.
headers = merge_extra_headers(
headers,
self.config.anthropic_extra_headers,
upstream_url=upstream_base_url,
config=self.config,
)
log_outbound_headers(
forwarder="anthropic_messages",
stripped_count=_pre_strip_count
- sum(1 for k in headers if k.lower().startswith("x-headroom-")),
request_id=request_id,
)
# Subscription tracker: notify on OAuth requests (not API-key requests)
_auth_header = headers.get("authorization", "")
if _auth_header.startswith("Bearer ") and not _auth_header.startswith(
"Bearer sk-ant-api"
):
from headroom.subscription.tracker import (
get_subscription_tracker as _get_sub_tracker,
)
_sub_tracker = _get_sub_tracker()
if _sub_tracker is not None:
from headroom.subscription.credential_policy import (
is_local_operator_connection,
)
# Only the local operator's bearer may become the polled
# account; a network caller only marks activity (01-F16).
_sub_tracker.notify_active(
_auth_header,
from_local_operator=is_local_operator_connection(request),
)
# Rate limiting
if self.rate_limiter:
# One identity rule for every provider: peer-owned, and
# credential-scoped only for proxy-token / direct loopback
# callers (headroom/proxy/rate_limit_identity.py).
rate_key = rate_limit_identity(request, headers)
allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
await self.metrics.record_rate_limited(
provider=provider_name, source="headroom"
)
# Unit 4: release the pre-upstream semaphore before we
# bail out of the handler via HTTPException — FastAPI's
# exception handler will NOT run our ``finally``.
await _finalize_pre_upstream()
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
headers={"Retry-After": str(int(wait_seconds) + 1)},
)
# Budget check
cost_tracker = self.cost_tracker
if cost_tracker:
allowed, remaining = cost_tracker.check_budget()
if not allowed:
# Unit 4: release the pre-upstream semaphore before we
# bail out of the handler via HTTPException.
await _finalize_pre_upstream()
raise HTTPException(
status_code=429,
detail=cost_tracker.budget_denial_detail(),
)
# Parse request — capture both the parsed dict AND the original
# bytes so the forwarder can pick byte-faithful passthrough when
# nothing mutated the body (PR-A3, fixes P0-2). The mutation
@@ -1327,6 +1466,8 @@ class AnthropicHandlerMixin:
try:
async with stage_timer.measure("read_request_json"):
body, original_body_bytes = await read_request_json_with_bytes(request)
except RequestBodyTooLarge:
raise
except ClientDisconnect:
# The client (e.g. an interrupted/compacting Claude Code
# session) went away mid-read. There is no one left to answer
@@ -1348,6 +1489,56 @@ class AnthropicHandlerMixin:
},
},
)
if len(original_body_bytes) > COMPRESSION_BODY_MAX_BYTES:
logger.info(
"[%s] event=compression_passthrough reason=request_body_soft_limit "
"bytes=%d threshold=%d",
request_id,
len(original_body_bytes),
COMPRESSION_BODY_MAX_BYTES,
)
from headroom.proxy.route_advice import resolver_for
backend = resolver_for(self).for_request(request, body=body)
passthrough_model = body.get("model") or model_override or "unknown"
if not isinstance(passthrough_model, str):
passthrough_model = "unknown"
if backend is not None:
if model_override and not body.get("model"):
body["model"] = model_override
await _finalize_pre_upstream()
return await self._handle_backend_passthrough(
body,
headers,
backend,
"anthropic",
passthrough_model,
request_id,
start_time,
tags,
force_stream=force_stream,
)
passthrough_base = upstream_base_url or self.ANTHROPIC_API_URL
passthrough_url = build_anthropic_upstream_url(
passthrough_base, request.url.path, request.url.query
)
assert self.http_client is not None, "http_client must be initialized"
upstream_request = self.http_client.build_request(
request.method,
passthrough_url,
headers=headers,
content=original_body_bytes,
)
await _finalize_pre_upstream()
return await self.handle_passthrough(
request,
passthrough_base,
endpoint_name="messages",
provider=provider_name,
_upstream_request=upstream_request,
_model=passthrough_model,
_start_time=start_time,
)
# This is a boolean capability classification only. The
# safeguards value itself is never copied into logs, metrics, or
# any Headroom-owned state.
@@ -1480,115 +1671,6 @@ class AnthropicHandlerMixin:
# NOTE: Upstream temporarily disabled broad image compression due to
# token-counting inaccuracies. We only compress the latest non-frozen
# user turn later in this handler to preserve Anthropic prefix caching.
# Extract headers and tags
headers = dict(request.headers.items())
headers.pop("host", None)
headers.pop("content-length", None)
# read_request_json_with_bytes already content-decoded the inbound
# body (zstd/gzip/deflate/br), so the bytes we forward are plain.
# A stale content-encoding makes the upstream try to decompress
# already-decoded JSON and reject it with HTTP 400 (#1542 — same
# fix the /v1/responses path already carries).
headers.pop("content-encoding", None)
headers.pop("transfer-encoding", None)
# Strip accept-encoding so httpx negotiates its own encoding.
# Edge proxies (Cloudflare Workers, etc.) may forward "br, zstd" which
# the upstream can honor; if httpx lacks brotli support the response
# body is undecipherable → 502.
headers.pop("accept-encoding", None)
tags = extract_tags(headers)
from headroom.proxy.savings_attribution import bind_scope
bind_scope(tags, request.scope)
# Identify the harness (codex / claude-code / aider / etc.)
# from User-Agent or X-Client. Surfaced via the funnel into
# PERF logs and RequestLog.tags — see RequestOutcome.client.
client = classify_client(headers, default="claude")
# PR-A5 (P5-49): strip internal x-headroom-* from upstream-bound
# headers AFTER `_extract_tags` reads them. Inbound bypass gating
# uses `request.headers.get(...)` directly above; memory user-id
# is read from `request.headers` below if needed. From this
# point on, `headers` is the upstream-bound copy.
from headroom.proxy.helpers import (
_strip_internal_headers,
apply_keep_last_turns,
log_outbound_headers,
merge_extra_headers,
)
_pre_strip_count = sum(1 for k in headers if k.lower().startswith("x-headroom-"))
headers = _strip_internal_headers(headers)
# `upstream_base_url` is the per-request `x-headroom-base-url`
# override when the client sent one. These headers are secrets, so
# they only travel to a host the operator designated.
headers = merge_extra_headers(
headers,
self.config.anthropic_extra_headers,
upstream_url=upstream_base_url,
config=self.config,
)
log_outbound_headers(
forwarder="anthropic_messages",
stripped_count=_pre_strip_count
- sum(1 for k in headers if k.lower().startswith("x-headroom-")),
request_id=request_id,
)
# Subscription tracker: notify on OAuth requests (not API-key requests)
_auth_header = headers.get("authorization", "")
if _auth_header.startswith("Bearer ") and not _auth_header.startswith(
"Bearer sk-ant-api"
):
from headroom.subscription.tracker import (
get_subscription_tracker as _get_sub_tracker,
)
_sub_tracker = _get_sub_tracker()
if _sub_tracker is not None:
from headroom.subscription.credential_policy import (
is_local_operator_connection,
)
# Only the local operator's bearer may become the polled
# account; a network caller only marks activity (01-F16).
_sub_tracker.notify_active(
_auth_header,
from_local_operator=is_local_operator_connection(request),
)
# Rate limiting
if self.rate_limiter:
# One identity rule for every provider: peer-owned, and
# credential-scoped only for proxy-token / direct loopback
# callers (headroom/proxy/rate_limit_identity.py).
rate_key = rate_limit_identity(request, headers)
allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
await self.metrics.record_rate_limited(
provider=provider_name, source="headroom"
)
# Unit 4: release the pre-upstream semaphore before we
# bail out of the handler via HTTPException — FastAPI's
# exception handler will NOT run our ``finally``.
await _finalize_pre_upstream()
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
headers={"Retry-After": str(int(wait_seconds) + 1)},
)
# Budget check
cost_tracker = self.cost_tracker
if cost_tracker:
allowed, remaining = cost_tracker.check_budget()
if not allowed:
# Unit 4: release the pre-upstream semaphore before we
# bail out of the handler via HTTPException.
await _finalize_pre_upstream()
raise HTTPException(
status_code=429,
detail=cost_tracker.budget_denial_detail(),
)
# Memory: Get user ID when memory is enabled (fallback to "default" for simple DevEx).
# Reads `request.headers` directly because the local `headers` dict was
@@ -4286,19 +4368,34 @@ class AnthropicHandlerMixin:
and ccr_response_handler_enabled
and has_ccr_retrieve_tool(tools if tools is not None else body.get("tools"))
)
buffered_retrieval_can_help = (
retrieve_tool_is_offered and self._outgoing_body_has_redeemable_marker(body)
#
# A marker the model already saw on an earlier turn can still be
# retrieved, so it does not justify buffering every later turn
# (#3587), but it does mean a retrieval call may come. Those
# turns stream with ``headroom_retrieve`` withheld from the
# client and served by the proxy in a continuation round.
owned_marker, new_owned_marker = (
self._outgoing_redeemable_markers(body, previous_forwarded_messages)
if retrieve_tool_is_offered
else (False, False)
)
wants_buffered_stream_ccr = bool(buffered_retrieval_can_help)
if retrieve_tool_is_offered and not buffered_retrieval_can_help:
wants_buffered_stream_ccr = bool(new_owned_marker)
if retrieve_tool_is_offered and not owned_marker:
logger.info(
f"[{request_id}] CCR: headroom_retrieve is resident but this "
"request carries no redeemable marker, so server-side "
"retrieval cannot fire; keeping the streaming path (#3071)"
)
elif retrieve_tool_is_offered and not new_owned_marker:
logger.info(
f"[{request_id}] CCR: every redeemable marker was already "
"forwarded; streaming with headroom_retrieve served by the "
"proxy (#3587)"
)
buffered_stream_ccr = (
wants_buffered_stream_ccr and not outbound_locked_to_client_bytes
)
serve_ccr_retrieval_in_stream = bool(owned_marker) and not buffered_stream_ccr
if wants_buffered_stream_ccr and outbound_locked_to_client_bytes:
logger.info(
f"[{request_id}] CCR: signed thinking blocks force byte-faithful "
@@ -4472,6 +4569,7 @@ class AnthropicHandlerMixin:
session_key=session_key,
server_memory_tool_names=server_memory_tool_names,
client_beta=client_anthropic_beta or "",
serve_ccr_retrieval=serve_ccr_retrieval_in_stream,
)
else:
# Whatever set it — the client's own ``stream: false`` or
+11
View File
@@ -84,6 +84,7 @@ class BedrockHandlerMixin:
read_request_json_with_bytes,
)
from headroom.proxy.modes import is_cache_mode
from headroom.proxy.request_limit_policy import COMPRESSION_BODY_MAX_BYTES
from headroom.utils import extract_user_query
start_time = time.time()
@@ -176,12 +177,22 @@ class BedrockHandlerMixin:
messages = body.get("messages")
bypass = (
_headroom_bypass_enabled(request.headers)
or len(raw) > COMPRESSION_BODY_MAX_BYTES
or not getattr(self.config, "optimize", True) # type: ignore[attr-defined]
or is_cache_mode(getattr(self.config, "mode", "token")) # type: ignore[attr-defined]
or not isinstance(messages, list)
or not messages
or len(messages) > MAX_MESSAGE_ARRAY_LENGTH
)
if len(raw) > COMPRESSION_BODY_MAX_BYTES:
tags["passthrough_reason"] = "request_body_soft_limit"
logger.info(
"[%s] event=compression_passthrough reason=request_body_soft_limit "
"bytes=%d threshold=%d",
request_id,
len(raw),
COMPRESSION_BODY_MAX_BYTES,
)
outbound = raw
original_tokens = 0
+148 -24
View File
@@ -225,6 +225,76 @@ class GeminiHandlerMixin:
# else: dropped by compression — omit
return result
@staticmethod
def _carry_tail_append_into_preserved(
original_contents: list[dict],
preserved_indices: set[int],
preserved_contents: dict[int, dict],
before_messages: list[dict],
after_messages: list[dict],
) -> bool:
"""Carry a memory tail append onto a preserved contents[] entry.
``_rebuild_gemini_contents`` forwards preserved (non-text) entries
verbatim and discards their converted message, so text appended to
that message never reaches the upstream body (#4419). Find the one
message the tail append changed, map it to its contents[] index the
same way the rebuild does (the k-th non-system message pairs with the
k-th entry that has a text part), and, when that entry is preserved
and is the latest user entry, append the same suffix to a copy of its
last text part.
Returns False when the append would be dropped: the target is a
preserved entry older than the latest user entry. Writing memory into
that older turn would change an already-cached prefix, so the caller
skips the injection instead of logging one that never reaches the wire.
"""
if len(before_messages) != len(after_messages):
return True
changed = [i for i, (b, a) in enumerate(zip(before_messages, after_messages)) if b is not a]
if len(changed) != 1:
return True
msg_idx = changed[0]
before = before_messages[msg_idx].get("content")
after = after_messages[msg_idx].get("content")
if not (isinstance(before, str) and isinstance(after, str)):
return True
if not after.startswith(before) or len(after) == len(before):
return True
suffix = after[len(before) :]
ordinal = sum(1 for m in before_messages[:msg_idx] if m.get("role") != "system")
text_entry_indices = [
idx
for idx, content in enumerate(original_contents)
if any(isinstance(p.get("text"), str) for p in GeminiHandlerMixin._dict_parts(content))
]
if ordinal >= len(text_entry_indices):
return True
content_idx = text_entry_indices[ordinal]
if content_idx not in preserved_indices:
return True
latest_user_idx = max(
(
idx
for idx, content in enumerate(original_contents)
if (content.get("role", "user") if isinstance(content, dict) else "user") == "user"
),
default=None,
)
if content_idx != latest_user_idx:
return False
entry = preserved_contents[content_idx]
# Copy the raw list so every part (including non-dict ones) is kept as-is.
parts = list(entry["parts"])
last_text = max(
i for i, p in enumerate(parts) if isinstance(p, dict) and isinstance(p.get("text"), str)
)
parts[last_text] = {**parts[last_text], "text": parts[last_text]["text"] + suffix}
preserved_contents[content_idx] = {**entry, "parts": parts}
return True
def _gemini_contents_to_messages(
self,
contents: list[dict],
@@ -344,7 +414,12 @@ class GeminiHandlerMixin:
from fastapi import HTTPException
from fastapi.responses import JSONResponse, Response
from headroom.proxy.helpers import MAX_REQUEST_BODY_SIZE, _read_request_json
from headroom.proxy.helpers import (
MAX_REQUEST_BODY_SIZE,
RequestBodyTooLarge,
read_request_json_with_bytes,
)
from headroom.proxy.request_limit_policy import COMPRESSION_BODY_MAX_BYTES
from headroom.utils import extract_user_query
def _stream_url(base_url: str) -> str:
@@ -374,7 +449,9 @@ class GeminiHandlerMixin:
# Parse request
try:
body = await _read_request_json(request)
body, original_body_bytes = await read_request_json_with_bytes(request)
except RequestBodyTooLarge:
raise
except (json.JSONDecodeError, ValueError) as e:
return JSONResponse(
status_code=400,
@@ -415,6 +492,64 @@ class GeminiHandlerMixin:
stripped_count=_pre_strip_count_gem,
request_id=request_id,
)
# Rate limiting: one identity rule for every provider
# (headroom/proxy/rate_limit_identity.py).
if self.rate_limiter:
rate_key = rate_limit_identity(request, headers)
allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
await self.metrics.record_rate_limited(provider=provider_name, source="headroom")
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
)
# Budget check
cost_tracker = self.cost_tracker
if cost_tracker:
allowed, remaining = cost_tracker.check_budget()
if not allowed:
raise HTTPException(
status_code=429,
detail=cost_tracker.budget_denial_detail(),
)
if len(original_body_bytes) > COMPRESSION_BODY_MAX_BYTES:
is_streaming = request.query_params.get("alt") == "sse" or request.url.path.endswith(
":streamGenerateContent"
)
passthrough_path = request.url.path
if is_streaming:
passthrough_path = passthrough_path.replace(
":generateContent", ":streamGenerateContent"
)
passthrough_base = upstream_base_url or self.GEMINI_API_URL
passthrough_url = build_copilot_upstream_url(passthrough_base, passthrough_path)
if is_streaming:
passthrough_url = _stream_url(passthrough_url)
elif request.url.query:
passthrough_url = f"{passthrough_url}?{request.url.query}"
headers.pop("content-encoding", None)
headers.pop("transfer-encoding", None)
assert self.http_client is not None, "http_client must be initialized"
upstream_request = self.http_client.build_request(
request.method, passthrough_url, headers=headers, content=original_body_bytes
)
logger.info(
"[%s] event=compression_passthrough reason=request_body_soft_limit "
"bytes=%d threshold=%d",
request_id,
len(original_body_bytes),
COMPRESSION_BODY_MAX_BYTES,
)
return await self.handle_passthrough(
request,
passthrough_base,
endpoint_name="streamGenerateContent" if is_streaming else "generateContent",
provider=provider_name,
_upstream_request=upstream_request,
_model=model,
_start_time=start_time,
)
# Memory: Get user ID when memory is enabled. Reads `request.headers`
# directly because `headers` was stripped of `x-headroom-*` (PR-A5).
@@ -472,28 +607,6 @@ class GeminiHandlerMixin:
)
memory_decision.apply_to_tags(tags)
# Rate limiting: one identity rule for every provider
# (headroom/proxy/rate_limit_identity.py).
if self.rate_limiter:
rate_key = rate_limit_identity(request, headers)
allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
await self.metrics.record_rate_limited(provider=provider_name, source="headroom")
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
)
# Budget check
cost_tracker = self.cost_tracker
if cost_tracker:
allowed, remaining = cost_tracker.check_budget()
if not allowed:
raise HTTPException(
status_code=429,
detail=cost_tracker.budget_denial_detail(),
)
# Convert Gemini format to messages for optimization
system_instruction = body.get("systemInstruction")
messages, preserved_indices = self._gemini_contents_to_messages(
@@ -735,6 +848,17 @@ class GeminiHandlerMixin:
provider="openai",
)
)
# Entries with non-text parts are restored verbatim by
# _rebuild_gemini_contents; carry the append onto them so
# it is not dropped after being logged, or skip it.
if bytes_appended > 0 and not self._carry_tail_append_into_preserved(
contents,
preserved_indices,
preserved_contents,
optimized_messages,
new_messages,
):
bytes_appended = 0
if bytes_appended > 0:
optimized_messages = new_messages
logger.info(
+509 -224
View File
@@ -31,6 +31,7 @@ from headroom.proxy.conversation_savings import savings_conversation_key
from headroom.proxy.handlers._failure_logging import log_hook_failure, log_request_failure
from headroom.proxy.helpers import (
COMPRESSION_TIMEOUT_SECONDS,
RequestBodyTooLarge,
_headroom_bypass_enabled,
extract_tags,
invalid_request_body_message,
@@ -41,6 +42,7 @@ from headroom.proxy.identity import resolve_memory_identity
from headroom.proxy.loopback_guard import is_loopback_host
from headroom.proxy.modes import is_cache_mode
from headroom.proxy.provider_usage import (
anthropic_cache_ttl_buckets,
billed_input_for_provider,
billed_input_from_usage,
usage_int,
@@ -513,6 +515,17 @@ def _resolve_openai_chat_handler_path(base_url: str, model: str | None) -> str:
return _OPENAI_CHAT_COMPLETIONS_PATH
def _resolve_openai_chat_forward_path(
request_headers: dict[str, str], custom_upstream_base_url: str | None
) -> str:
"""Keep Chat Completions bodies on their effective Chat wire destination."""
if custom_upstream_base_url is not None:
return _resolve_openai_handler_path(
request_headers, handler_path=_OPENAI_CHAT_COMPLETIONS_PATH
)
return "/v1/chat/completions"
def _append_request_query(url: str, query: str) -> str:
if not query:
return url
@@ -627,6 +640,7 @@ def _passthrough_usage_from_json(payload: Any, provider: str | None = None) -> d
output_tokens = usage.get("completion_tokens")
details = usage.get("prompt_tokens_details") or usage.get("input_tokens_details") or {}
cache_read = details.get("cached_tokens") if isinstance(details, dict) else None
cache_5m, cache_1h = anthropic_cache_ttl_buckets(usage)
return {
# The billed total. Anthropic-shape usage reports input_tokens as
# the uncached tail only (cache buckets are disjoint); OpenAI's
@@ -642,6 +656,8 @@ def _passthrough_usage_from_json(payload: Any, provider: str | None = None) -> d
details.get("cache_write_tokens") if isinstance(details, dict) else None,
)
),
"cache_creation_ephemeral_5m_input_tokens": cache_5m,
"cache_creation_ephemeral_1h_input_tokens": cache_1h,
}
return {}
@@ -2068,6 +2084,20 @@ def _prefers_http1_passthrough(base_url: str) -> bool:
return host == "chatgpt.com" or host.endswith(".chatgpt.com")
def _openai_outcome_provider(
method: str, path: str, custom_base_url: str | None, upstream_base_url: str
) -> str:
"""Keep native and opaque forwarding on the same bounded provider taxonomy."""
_, provider = _custom_base_passthrough_telemetry(method, path, custom_base_url or "")
return provider or (
CUSTOM_BASE_PROVIDER
if custom_base_url
else "xai"
if _is_xai_upstream(upstream_base_url)
else "openai"
)
class OpenAIHandlerMixin:
"""Mixin providing OpenAI API handler methods for HeadroomProxy."""
@@ -3500,9 +3530,10 @@ class OpenAIHandlerMixin:
COMPRESSION_TIMEOUT_SECONDS,
MAX_MESSAGE_ARRAY_LENGTH,
MAX_REQUEST_BODY_SIZE,
_read_request_json,
read_request_json_with_bytes,
)
from headroom.proxy.modes import is_cache_mode, is_token_mode
from headroom.proxy.request_limit_policy import COMPRESSION_BODY_MAX_BYTES
from headroom.utils import extract_user_query
start_time = time.time()
@@ -3542,7 +3573,9 @@ class OpenAIHandlerMixin:
# Parse request
try:
body = await _read_request_json(request)
body, original_body_bytes = await read_request_json_with_bytes(request)
except RequestBodyTooLarge:
raise
except (json.JSONDecodeError, ValueError) as e:
return JSONResponse(
status_code=400,
@@ -3556,16 +3589,6 @@ class OpenAIHandlerMixin:
)
model = body.get("model", "unknown")
messages = body.get("messages", [])
# O1 (2026-09-27 perf audit): the snapshot of the original
# conversation aliases the live list unless hooks, pipeline extensions
# or turn hooks are configured; those can mutate `messages` in place
# (pre_compress receives the live list), so then it is an
# independently owned copy (see snapshot_original_messages).
from headroom.proxy.helpers import snapshot_original_messages
original_client_messages = snapshot_original_messages(
messages, hooks=self.config.hooks, extensions=self.pipeline_extensions
)
custom_upstream_base_url = _resolve_openai_upstream_base(request.headers)
upstream_base_url = self._resolve_openai_upstream(request)
# Ghost text and next edits: CAPI does not serve these models. A
@@ -3587,6 +3610,139 @@ class OpenAIHandlerMixin:
if custom_upstream_base_url is not None
else f"/v1{handler_path_suffix}"
)
headers = dict(request.headers.items())
headers.pop("host", None)
headers.pop("content-length", None)
# The parsed body was already content-decoded upstream, so the bytes we
# forward are plain JSON. A stale content-encoding makes OpenAI try to
# decompress already-decoded JSON and reject it with HTTP 400 (#1542 —
# same fix the /v1/responses path already carries).
headers.pop("content-encoding", None)
headers.pop("transfer-encoding", None)
# Strip accept-encoding so httpx negotiates its own encoding.
# Cloudflare Workers forward "br, zstd" which OpenAI may honor;
# if httpx lacks brotli support the response body is undecipherable → 502.
headers.pop("accept-encoding", None)
tags = extract_tags(headers)
from headroom.proxy.savings_attribution import bind_scope
bind_scope(tags, request.scope)
client = classify_client(headers)
# PR-A5 (P5-49): strip internal x-headroom-* from upstream-bound
# headers AFTER `_extract_tags` reads them. Inbound bypass gating
# uses `request.headers.get(...)` above; memory user-id reads
# `request.headers` below. From this point on, `headers` is the
# upstream-bound copy.
from headroom.proxy.helpers import (
_strip_internal_headers,
apply_keep_last_turns,
log_outbound_headers,
merge_extra_headers,
)
_pre_strip_count_chat = sum(1 for k in headers if k.lower().startswith("x-headroom-"))
headers = _strip_internal_headers(headers)
# Configured backends own their destination and authentication, so they
# retain the existing extra-header policy. The direct path selects
# extras from the resolved OpenAI-compatible upstream. Direct custom
# upstreams must also pass the operator-designated-host check before
# receiving those extras.
headers = merge_extra_headers(
headers,
self.config.openai_extra_headers
if self.anthropic_backend is not None
else self._openai_extra_headers_for_upstream(upstream_base_url),
upstream_url=(None if self.anthropic_backend is not None else custom_upstream_base_url),
config=self.config,
)
log_outbound_headers(
forwarder="openai_chat_completions",
stripped_count=_pre_strip_count_chat,
request_id=request_id,
)
openai_chat_outcome_provider = _openai_outcome_provider(
request.method, handler_path, custom_upstream_base_url, upstream_base_url
)
# Rate limiting
if self.rate_limiter:
rate_key = rate_limit_identity(request, headers)
allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
await self.metrics.record_rate_limited(
provider=openai_chat_outcome_provider, source="headroom"
)
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
)
# Budget check
cost_tracker = self.cost_tracker
if cost_tracker:
allowed, remaining = cost_tracker.check_budget()
if not allowed:
raise HTTPException(
status_code=429,
detail=cost_tracker.budget_denial_detail(),
)
if len(original_body_bytes) > COMPRESSION_BODY_MAX_BYTES:
from headroom.proxy.route_advice import resolver_for
logger.info(
"[%s] event=compression_passthrough reason=request_body_soft_limit "
"bytes=%d threshold=%d",
request_id,
len(original_body_bytes),
COMPRESSION_BODY_MAX_BYTES,
)
backend = resolver_for(self).for_request(
request, body=body, native_providers=("openai",)
)
passthrough_model = body.get("model", "unknown")
if not isinstance(passthrough_model, str):
passthrough_model = "unknown"
if backend is not None:
return await self._handle_backend_passthrough(
body,
headers,
backend,
"openai",
passthrough_model,
request_id,
start_time,
tags,
)
handler_path = _resolve_openai_chat_forward_path(
request.headers, custom_upstream_base_url
)
url = _append_request_query(
build_copilot_upstream_url(upstream_base_url, handler_path),
request.url.query,
)
assert self.http_client is not None, "http_client must be initialized"
upstream_request = self.http_client.build_request(
request.method, url, headers=headers, content=original_body_bytes
)
return await self.handle_passthrough(
request,
upstream_base_url,
endpoint_name="chat/completions",
provider=openai_chat_outcome_provider,
_upstream_request=upstream_request,
_model=passthrough_model,
_start_time=start_time,
)
# O1 (2026-09-27 perf audit): the snapshot of the original
# conversation aliases the live list unless hooks, pipeline extensions
# or turn hooks are configured; those can mutate `messages` in place
# (pre_compress receives the live list), so then it is an
# independently owned copy (see snapshot_original_messages).
from headroom.proxy.helpers import snapshot_original_messages
original_client_messages = snapshot_original_messages(
messages, hooks=self.config.hooks, extensions=self.pipeline_extensions
)
input_event = self.pipeline_extensions.emit(
PipelineStage.INPUT_RECEIVED,
operation="proxy.request",
@@ -3669,84 +3825,14 @@ class OpenAIHandlerMixin:
if compressor and hasattr(compressor, "close"):
compressor.close()
headers = dict(request.headers.items())
headers.pop("host", None)
headers.pop("content-length", None)
# The parsed body was already content-decoded upstream, so the bytes we
# forward are plain JSON. A stale content-encoding makes OpenAI try to
# decompress already-decoded JSON and reject it with HTTP 400 (#1542 —
# same fix the /v1/responses path already carries).
headers.pop("content-encoding", None)
headers.pop("transfer-encoding", None)
# Strip accept-encoding so httpx negotiates its own encoding.
# Cloudflare Workers forward "br, zstd" which OpenAI may honor;
# if httpx lacks brotli support the response body is undecipherable → 502.
headers.pop("accept-encoding", None)
tags = extract_tags(headers)
from headroom.proxy.savings_attribution import bind_scope
bind_scope(tags, request.scope)
client = classify_client(headers)
# Surface the image-compression decision (computed earlier) into
# tags now that the tags dict exists. Same observability pattern
# the funnel uses for passthrough_reason + memory_skip_reason.
_image_decision.apply_to_tags(tags)
# PR-A5 (P5-49): strip internal x-headroom-* from upstream-bound
# headers AFTER `_extract_tags` reads them. Inbound bypass gating
# uses `request.headers.get(...)` above; memory user-id reads
# `request.headers` below. From this point on, `headers` is the
# upstream-bound copy.
from headroom.proxy.helpers import (
_strip_internal_headers,
apply_keep_last_turns,
log_outbound_headers,
merge_extra_headers,
)
_pre_strip_count_chat = sum(1 for k in headers if k.lower().startswith("x-headroom-"))
headers = _strip_internal_headers(headers)
# Configured backends own their destination and authentication, so they
# retain the existing extra-header policy. The direct path selects
# extras from the resolved OpenAI-compatible upstream. Direct custom
# upstreams must also pass the operator-designated-host check before
# receiving those extras.
headers = merge_extra_headers(
headers,
self.config.openai_extra_headers
if self.anthropic_backend is not None
else self._openai_extra_headers_for_upstream(upstream_base_url),
upstream_url=(None if self.anthropic_backend is not None else custom_upstream_base_url),
config=self.config,
)
log_outbound_headers(
forwarder="openai_chat_completions",
stripped_count=_pre_strip_count_chat,
request_id=request_id,
)
custom_upstream_base_url = _resolve_openai_upstream_base(request.headers)
handler_path = (
_resolve_openai_handler_path(
request.headers,
handler_path=_OPENAI_CHAT_COMPLETIONS_PATH,
)
if custom_upstream_base_url is not None
else "/v1/chat/completions"
)
_, custom_chat_provider = _custom_base_passthrough_telemetry(
request.method,
handler_path,
custom_upstream_base_url or "",
)
# Fixed taxonomy from the shared helper (zen, zai, meta, openai, xai);
# any other custom base is the shared "custom" bucket. Never derive the
# label from the request-controlled hostname — see the review on #3759.
# Grok CLI routed to xAI without a base-url header is still xai.
openai_chat_outcome_provider = custom_chat_provider or (
CUSTOM_BASE_PROVIDER
if custom_upstream_base_url
else "xai"
if _is_xai_upstream(upstream_base_url)
else "openai"
handler_path = _resolve_openai_chat_forward_path(request.headers, custom_upstream_base_url)
openai_chat_outcome_provider = _openai_outcome_provider(
request.method, handler_path, custom_upstream_base_url, upstream_base_url
)
# Memory: Get user ID when memory is enabled. Reads `request.headers`
@@ -3796,29 +3882,6 @@ class OpenAIHandlerMixin:
)
memory_decision.apply_to_tags(tags)
# Rate limiting
if self.rate_limiter:
rate_key = rate_limit_identity(request, headers)
allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
await self.metrics.record_rate_limited(
provider=openai_chat_outcome_provider, source="headroom"
)
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
)
# Budget check
cost_tracker = self.cost_tracker
if cost_tracker:
allowed, remaining = cost_tracker.check_budget()
if not allowed:
raise HTTPException(
status_code=429,
detail=cost_tracker.budget_denial_detail(),
)
# Snapshot cache-key fields ONCE here (pre-upstream), reused verbatim
# at the cache.set site below — re-reading body at set risks a mutated
# body (e.g. tools reassigned) and a key mismatch (#327). OpenAI's
@@ -6076,6 +6139,7 @@ class OpenAIHandlerMixin:
MAX_REQUEST_BODY_SIZE,
read_request_json_with_bytes,
)
from headroom.proxy.request_limit_policy import COMPRESSION_BODY_MAX_BYTES
from headroom.utils import extract_user_query
start_time = time.time()
@@ -6120,6 +6184,8 @@ class OpenAIHandlerMixin:
# (#1542); byte-faithful passthrough avoids that.
try:
body, original_body_bytes = await read_request_json_with_bytes(request)
except RequestBodyTooLarge:
raise
except (json.JSONDecodeError, ValueError) as e:
return JSONResponse(
status_code=400,
@@ -6255,6 +6321,90 @@ class OpenAIHandlerMixin:
headers, is_chatgpt_auth = _resolve_codex_routing_headers(headers)
if is_chatgpt_auth:
client = "codex"
# Rate limiting
if self.rate_limiter:
rate_key = rate_limit_identity(request, headers)
allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
await self.metrics.record_rate_limited(provider="openai", source="headroom")
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
)
# Budget check
cost_tracker = self.cost_tracker
if cost_tracker:
allowed, remaining = cost_tracker.check_budget()
if not allowed:
raise HTTPException(
status_code=429,
detail=cost_tracker.budget_denial_detail(),
)
# Route to correct endpoint based on auth mode.
# ChatGPT session auth (codex login) uses chatgpt.com, not api.openai.com.
if is_chatgpt_auth:
url = codex_responses_http_url()
else:
handler_path = (
_resolve_openai_handler_path(request.headers, handler_path=_OPENAI_RESPONSES_PATH)
if custom_upstream_base_url is not None
else "/v1/responses"
)
# Reuse the OpenAI-compatible candidate resolved at request entry.
# In this non-ChatGPT branch it is also the actual upstream, keeping
# direct routing and header selection aligned.
url = build_copilot_upstream_url(
openai_upstream_base_url,
handler_path,
)
url = _append_request_query(url, request.url.query)
if len(original_body_bytes) > COMPRESSION_BODY_MAX_BYTES:
outbound_body_bytes = original_body_bytes
if _ensure_chatgpt_responses_store_false(body, is_chatgpt_auth=is_chatgpt_auth):
outbound_body_bytes = json.dumps(
body, separators=(",", ":"), ensure_ascii=False
).encode("utf-8")
assert self.http_client is not None, "http_client must be initialized"
upstream_request = self.http_client.build_request(
request.method, url, headers=headers, content=outbound_body_bytes
)
logger.info(
"[%s] event=compression_passthrough reason=request_body_soft_limit "
"bytes=%d threshold=%d",
request_id,
len(original_body_bytes),
COMPRESSION_BODY_MAX_BYTES,
)
passthrough_provider = (
"openai"
if is_chatgpt_auth
else _openai_outcome_provider(
request.method, handler_path, custom_upstream_base_url, openai_upstream_base_url
)
)
return await self.handle_passthrough(
request,
openai_upstream_base_url,
endpoint_name="responses",
provider=passthrough_provider,
_upstream_request=upstream_request,
_model=model if isinstance(model, str) else "unknown",
_start_time=start_time,
)
from headroom.proxy.helpers import capture_codex_wire_debug
capture_codex_wire_debug(
"http_inbound_request",
request_id=request_id,
transport="http",
direction="client_to_headroom",
method=request.method,
url=str(request.url),
headers=dict(request.headers.items()),
body=body,
metadata={"path": request.url.path, "stream": stream},
)
codex_project = None
codex_project_root_override = (
getattr(getattr(self.memory_handler, "config", None), "project_root_override", "")
@@ -6384,27 +6534,6 @@ class OpenAIHandlerMixin:
request_context=memory_request_ctx,
)
# Rate limiting
if self.rate_limiter:
rate_key = rate_limit_identity(request, headers)
allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
await self.metrics.record_rate_limited(provider="openai", source="headroom")
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
)
# Budget check
cost_tracker = self.cost_tracker
if cost_tracker:
allowed, remaining = cost_tracker.check_budget()
if not allowed:
raise HTTPException(
status_code=429,
detail=cost_tracker.budget_denial_detail(),
)
# Token counting on converted messages (offloaded off the event loop — GH #1701)
tokenizer, original_tokens = await self._count_tokens_offloaded(model, messages)
# Messages-only count, preserved for the output shaper's turn/size
@@ -6635,25 +6764,6 @@ class OpenAIHandlerMixin:
f"(backend '{self.anthropic_backend.name}' not used for Responses API)"
)
# Route to correct endpoint based on auth mode.
# ChatGPT session auth (codex login) uses chatgpt.com, not api.openai.com.
if is_chatgpt_auth:
url = codex_responses_http_url()
else:
handler_path = (
_resolve_openai_handler_path(request.headers, handler_path=_OPENAI_RESPONSES_PATH)
if custom_upstream_base_url is not None
else "/v1/responses"
)
# Reuse the OpenAI-compatible candidate resolved at request entry.
# In this non-ChatGPT branch it is also the actual upstream, keeping
# direct routing and header selection aligned.
url = build_copilot_upstream_url(
openai_upstream_base_url,
handler_path,
)
url = _append_request_query(url, request.url.query)
# Responses re-sends the whole transcript every turn and the router
# recompresses all of it, so ``tokens_saved`` below is the running
# total for the CONVERSATION, not for this turn. Carrying the key and
@@ -10929,7 +11039,7 @@ class OpenAIHandlerMixin:
from headroom.proxy.gateway_responses import build_view as build_responses_view
from headroom.proxy.gateway_responses import is_responses_body
from headroom.proxy.gateway_responses import mark_view as mark_responses_view
from headroom.proxy.helpers import _read_request_json
from headroom.proxy.helpers import RequestBodyTooLarge, _read_request_json
def _gateway_invalid(e: CompressTurnError) -> JSONResponse:
return JSONResponse(
@@ -10951,6 +11061,8 @@ class OpenAIHandlerMixin:
if request.headers.get("x-headroom-bypass", "").lower() == "true":
try:
body = await _read_request_json(request)
except RequestBodyTooLarge:
raise
except (json.JSONDecodeError, ValueError) as e:
return JSONResponse(
status_code=400,
@@ -10976,6 +11088,8 @@ class OpenAIHandlerMixin:
try:
body = await _read_request_json(request)
except RequestBodyTooLarge:
raise
except Exception:
return JSONResponse(
status_code=400,
@@ -11922,12 +12036,116 @@ class OpenAIHandlerMixin:
except (TypeError, ValueError):
return body
async def _handle_backend_passthrough(
self,
body: dict[str, Any],
headers: dict[str, str],
backend: Any,
wire_provider: str,
model: str,
request_id: str,
start_time: float,
tags: dict[str, str],
*,
force_stream: bool = False,
) -> Response:
"""Retain configured backend translation without request-side transforms."""
from fastapi.responses import JSONResponse
tags["passthrough_reason"] = "request_body_soft_limit"
if body.get("stream", False) or force_stream:
# No local tokenization ran. Zero is the existing "unmeasured"
# convention; these finalizers separately record actual provider usage.
if wire_provider == "openai":
return await self._stream_openai_via_backend(
body,
headers,
model,
request_id,
start_time,
0,
0,
0,
[],
tags,
0,
backend=backend,
)
return await self._stream_response_bedrock(
body,
headers,
wire_provider,
model,
request_id,
0,
0,
0,
[],
tags,
0,
backend=backend,
)
try:
response = (
await backend.send_openai_message(body, headers)
if wire_provider == "openai"
else await backend.send_message(body, headers)
)
payload = response.body
status_code = response.status_code
response_headers = _sanitize_forwarded_response_headers(response.headers)
except Exception as exc:
logger.error("[%s] Backend passthrough failed: %s", request_id, exc)
error = public_errors.classify_or_internal(exc)
payload = (
public_errors.openai_error_body(error, error_type="api_error")
if wire_provider == "openai"
else public_errors.anthropic_error_body(
error, request_id=str(request_id), error_type="api_error"
)
)
status_code = 500
response_headers = {}
usage = _passthrough_usage_from_json(payload, wire_provider)
input_tokens = usage.get("input_tokens", 0)
cache_read = usage.get("cache_read_input_tokens", 0)
cache_write = usage.get("cache_creation_input_tokens", 0)
await self._record_request_outcome(
RequestOutcome(
request_id=request_id,
provider=backend.name,
model=model,
original_tokens=input_tokens,
optimized_tokens=input_tokens,
provider_input_tokens=input_tokens,
output_tokens=usage.get("output_tokens", 0),
tokens_saved=0,
attempted_input_tokens=0,
cache_read_tokens=cache_read,
cache_write_tokens=cache_write,
cache_write_5m_tokens=usage.get("cache_creation_ephemeral_5m_input_tokens", 0),
cache_write_1h_tokens=usage.get("cache_creation_ephemeral_1h_input_tokens", 0),
uncached_input_tokens=max(0, input_tokens - cache_read - cache_write),
status_code=status_code,
total_latency_ms=(time.time() - start_time) * 1000,
tags=tags,
client=classify_client(headers),
)
)
return JSONResponse(content=payload, status_code=status_code, headers=response_headers)
async def handle_passthrough(
self,
request: Request,
base_url: str,
endpoint_name: str | None = None,
provider: str | None = None,
*,
_upstream_request: httpx.Request | None = None,
_model: str | None = None,
_start_time: float | None = None,
) -> Response:
"""Pass through request unchanged.
@@ -11939,7 +12157,11 @@ class OpenAIHandlerMixin:
"""
from fastapi.responses import Response
if endpoint_name in {"streamGenerateContent", "streamRawPredict"} and provider:
if (
_upstream_request is None
and endpoint_name in {"streamGenerateContent", "streamRawPredict"}
and provider
):
return await self._handle_streaming_passthrough(
request=request,
base_url=base_url,
@@ -11947,7 +12169,7 @@ class OpenAIHandlerMixin:
provider=provider,
)
start_time = time.time()
start_time = _start_time if _start_time is not None else time.time()
path = request.url.path
if provider == "anthropic" and endpoint_name == "models" and path.startswith("/v1/models/"):
from headroom.providers.anthropic import sanitize_anthropic_model_id
@@ -11991,26 +12213,40 @@ class OpenAIHandlerMixin:
from starlette.requests import ClientDisconnect
try:
body = await request.body()
except ClientDisconnect:
logger.debug("Client disconnected during body read for passthrough")
return Response(status_code=204)
from headroom.proxy.request_limit_policy import COMPRESSION_BODY_MAX_BYTES
# Hermes owns its scoped coding-agent protocols; keep that adapter out of
# the generic OpenAI passthrough handler.
from headroom.providers.hermes import compress_scoped_passthrough_body
if _upstream_request is None:
try:
body = await request.body()
except ClientDisconnect:
logger.debug("Client disconnected during body read for passthrough")
return Response(status_code=204)
else:
# Native handlers have already admitted and content-decoded this
# request. Keep their exact target and bytes, without reprocessing.
body = _upstream_request.content
headers = dict(_upstream_request.headers)
url = str(_upstream_request.url)
origin_passthrough_url = None
optimize_enabled = bool(getattr(getattr(self, "config", None), "optimize", False))
original_body = body
body = compress_scoped_passthrough_body(
path,
body,
optimize=optimize_enabled,
bypass=_headroom_bypass_enabled(request.headers),
)
if body is not original_body:
headers["content-length"] = str(len(body))
soft_passthrough = _upstream_request is not None or len(body) > COMPRESSION_BODY_MAX_BYTES
if soft_passthrough:
tags["passthrough_reason"] = "request_body_soft_limit"
if not soft_passthrough:
# Hermes owns its scoped coding-agent protocols; keep that adapter out of
# the generic OpenAI passthrough handler.
from headroom.providers.hermes import compress_scoped_passthrough_body
optimize_enabled = bool(getattr(getattr(self, "config", None), "optimize", False))
original_body = body
body = compress_scoped_passthrough_body(
path,
body,
optimize=optimize_enabled,
bypass=_headroom_bypass_enabled(request.headers),
)
if body is not original_body:
headers["content-length"] = str(len(body))
# Opt-in: compress requests that fall through here because their path
# doesn't match a built-in API route (custom wrapper-proxy paths like
@@ -12019,7 +12255,8 @@ class OpenAIHandlerMixin:
# the exact same ContentRouter/Kompress path the native handler runs.
_pt_config = getattr(self, "config", None)
if (
getattr(_pt_config, "compress_passthrough", False)
not soft_passthrough
and getattr(_pt_config, "compress_passthrough", False)
and getattr(_pt_config, "optimize", False)
and request.method == "POST"
and path.rstrip("/").endswith("/responses")
@@ -12040,20 +12277,45 @@ class OpenAIHandlerMixin:
# Those endpoints answer fine over HTTP/1.1, so forward ChatGPT
# passthrough on the HTTP/1.1 client. Other hosts keep HTTP/2.
passthrough_client = self.http_client
if _prefers_http1_passthrough(base_url):
if _prefers_http1_passthrough(url):
passthrough_client = self.http_client_h1 or self.http_client
try:
# Retry once on a transient keep-alive close (httpx
# RemoteProtocolError / "incomplete chunked read"): the upstream
# closed a pooled connection httpx then reused. A fresh connection
# succeeds, mirroring what a direct curl call does. See GH #1112.
response = await request_with_transient_retry(
passthrough_client, # type: ignore[arg-type]
method=request.method,
url=url,
headers=headers,
content=body,
)
if _upstream_request is None:
# Retry once on a transient keep-alive close (httpx
# RemoteProtocolError / "incomplete chunked read"): the upstream
# closed a pooled connection httpx then reused. A fresh connection
# succeeds, mirroring what a direct curl call does. See GH #1112.
response = await request_with_transient_retry(
passthrough_client, # type: ignore[arg-type]
method=request.method,
url=url,
headers=headers,
content=body,
)
else:
assert passthrough_client is not None, "http_client must be initialized"
_upstream_request.headers = httpx.Headers(headers)
response = await passthrough_client.send(_upstream_request, stream=True)
if (
endpoint_name
and provider
and response.headers.get("content-type", "")
.lower()
.startswith("text/event-stream")
):
return await self._handle_streaming_passthrough(
request,
base_url,
endpoint_name,
provider,
_upstream_response=response,
_model=_model,
_start_time=start_time,
)
try:
await response.aread()
finally:
await response.aclose()
except (httpx.ConnectError, httpx.TimeoutException) as e:
logger.warning(
"Passthrough request failed before upstream response: %s %s -> %s: %s",
@@ -12162,16 +12424,20 @@ class OpenAIHandlerMixin:
RequestOutcome(
request_id=request_id,
provider=provider,
model=_passthrough_model_from_path(path, endpoint_name),
model=_model
if _model is not None
else _passthrough_model_from_path(path, endpoint_name),
status_code=response.status_code,
original_tokens=input_tokens,
optimized_tokens=input_tokens,
provider_input_tokens=input_tokens,
output_tokens=output_tokens,
tokens_saved=0,
attempted_input_tokens=input_tokens,
attempted_input_tokens=0 if soft_passthrough else input_tokens,
cache_read_tokens=cache_read_tokens,
cache_write_tokens=cache_write_tokens,
cache_write_5m_tokens=usage.get("cache_creation_ephemeral_5m_input_tokens", 0),
cache_write_1h_tokens=usage.get("cache_creation_ephemeral_1h_input_tokens", 0),
uncached_input_tokens=uncached_input_tokens,
total_latency_ms=latency_ms,
tags=tags,
@@ -12191,13 +12457,17 @@ class OpenAIHandlerMixin:
base_url: str,
endpoint_name: str,
provider: str,
*,
_upstream_response: httpx.Response | None = None,
_model: str | None = None,
_start_time: float | None = None,
) -> Response:
"""Stream pass-through responses without buffering the upstream body."""
from fastapi.responses import Response, StreamingResponse
from headroom.proxy.helpers import MAX_SSE_BUFFER_SIZE
start_time = time.time()
start_time = _start_time if _start_time is not None else time.time()
path = request.url.path
url = build_copilot_upstream_url(base_url, path)
if request.url.query:
@@ -12208,6 +12478,8 @@ class OpenAIHandlerMixin:
headers.pop("accept-encoding", None)
client = classify_client(headers)
tags = extract_tags(headers)
if _upstream_response is not None:
tags["passthrough_reason"] = "request_body_soft_limit"
from headroom.proxy.helpers import _strip_internal_headers, log_outbound_headers
@@ -12221,14 +12493,20 @@ class OpenAIHandlerMixin:
from starlette.requests import ClientDisconnect
try:
body = await request.body()
except ClientDisconnect:
logger.debug("Client disconnected during body read for streaming passthrough")
return Response(status_code=204)
headers = await apply_copilot_api_auth(headers, url=url)
if _upstream_response is None:
try:
body = await request.body()
except ClientDisconnect:
logger.debug("Client disconnected during body read for streaming passthrough")
return Response(status_code=204)
headers = await apply_copilot_api_auth(headers, url=url)
request_id = await self._next_request_id()
stream_provider = "gemini" if provider == "vertex:google" else "anthropic"
if provider in {"gemini", "vertex:google"}:
stream_provider = "gemini"
elif endpoint_name in {"chat/completions", "responses"}:
stream_provider = "openai"
else:
stream_provider = "anthropic"
stream_state: dict[str, Any] = {
"input_tokens": None,
"output_tokens": None,
@@ -12241,37 +12519,40 @@ class OpenAIHandlerMixin:
"ttfb_ms": None,
}
assert self.http_client is not None, "http_client must be initialized before streaming"
try:
upstream_request = self.http_client.build_request(
request.method,
url,
headers=headers,
content=body,
)
upstream_response = await self.http_client.send(upstream_request, stream=True)
except (httpx.ConnectError, httpx.TimeoutException) as e:
logger.warning(
"Streaming passthrough failed before upstream response: %s %s -> %s: %s",
request.method,
path,
url,
e,
)
from headroom.proxy.tls_diagnostics import describe_upstream_failure_async
if _upstream_response is None:
assert self.http_client is not None, "http_client must be initialized before streaming"
try:
upstream_request = self.http_client.build_request(
request.method,
url,
headers=headers,
content=body,
)
upstream_response = await self.http_client.send(upstream_request, stream=True)
except (httpx.ConnectError, httpx.TimeoutException) as e:
logger.warning(
"Streaming passthrough failed before upstream response: %s %s -> %s: %s",
request.method,
path,
url,
e,
)
from headroom.proxy.tls_diagnostics import describe_upstream_failure_async
tls_hint = await describe_upstream_failure_async(e, url)
return Response(
content=json.dumps(
public_errors.openai_error_body(
public_errors.classify_or_internal(e),
error_type="connection_error",
hint=tls_hint,
)
),
status_code=502,
media_type="application/json",
)
tls_hint = await describe_upstream_failure_async(e, url)
return Response(
content=json.dumps(
public_errors.openai_error_body(
public_errors.classify_or_internal(e),
error_type="connection_error",
hint=tls_hint,
)
),
status_code=502,
media_type="application/json",
)
else:
upstream_response = _upstream_response
response_headers = _sanitize_forwarded_response_headers(
upstream_response.headers,
@@ -12362,13 +12643,17 @@ class OpenAIHandlerMixin:
RequestOutcome(
request_id=request_id,
provider=provider,
model=_passthrough_model_from_path(path, endpoint_name),
model=_model
if _model is not None
else _passthrough_model_from_path(path, endpoint_name),
original_tokens=input_tokens,
optimized_tokens=input_tokens,
provider_input_tokens=input_tokens,
output_tokens=output_tokens,
tokens_saved=0,
attempted_input_tokens=input_tokens,
attempted_input_tokens=0
if _upstream_response is not None
else input_tokens,
cache_read_tokens=cache_read_tokens,
cache_write_tokens=cache_write_tokens,
cache_write_5m_tokens=stream_state[
+119 -29
View File
@@ -520,17 +520,26 @@ class StreamingMixin:
server_memory_tool_names: frozenset[str],
stream_state: dict[str, Any],
request_id: str,
serve_retrieval: bool = False,
) -> AsyncIterator[bytes]:
"""Execute withheld memory tool calls and stream continuation rounds.
"""Execute withheld proxy tool calls and stream continuation rounds.
``memory_filter`` has forwarded one upstream round to the client minus
the proxy-owned memory tool calls and the message tail. When the round
stopped on memory tool calls alone, run them, send the results upstream
the proxy-owned tool calls and the message tail. When the round
stopped on proxy tool calls alone, run them, send the results upstream
as the next turn, and stream that round's blocks into the same client
message. Otherwise end the client message. A round that also called a
client tool still has its memory calls executed, but the turn goes back
to the client, which cannot carry the memory results.
With ``serve_retrieval``, ``headroom_retrieve`` is a proxy tool too: the
client never declared it, so a call that reached it would fail (#3587).
The filter then holds client tool calls until the round ends, and a
round that retrieved alongside a client tool is continued without the
unrun client calls, for the model to re-issue with the content in
hand. That is what the buffered path does with the same round.
"""
from headroom.ccr.tool_injection import CCR_TOOL_NAME
from headroom.proxy.helpers import MAX_SSE_BUFFER_SIZE
try:
@@ -541,18 +550,47 @@ class StreamingMixin:
if not isinstance(messages, list):
messages = None
headers = {k: v for k, v in outbound_headers.items() if k.lower() != "content-length"}
proxy_tool_names = server_memory_tool_names | (
frozenset({CCR_TOOL_NAME}) if serve_retrieval else frozenset()
)
def is_memory_call(block: Any) -> bool:
def is_proxy_call(block: Any) -> bool:
return (
isinstance(block, dict)
and block.get("type") == "tool_use"
and block.get("name") in server_memory_tool_names
and block.get("name") in proxy_tool_names
)
def is_client_call(block: Any) -> bool:
return (
isinstance(block, dict)
and block.get("type") == "tool_use"
and not is_proxy_call(block)
)
rounds = 0
# What the client received across rounds; the prefix tracker must see
# this, not round one, because it is what the client sends back.
visible_content: list[Any] = []
def record_visible(
round_response: dict[str, Any] | None,
round_content: list[Any],
*,
dropped_client_calls: bool,
) -> None:
if round_response is None:
return
visible_content.extend(
block
for block in round_content
if not is_proxy_call(block) and not (dropped_client_calls and is_client_call(block))
)
stream_state["memory_client_message"] = {
**round_response,
"content": list(visible_content),
}
# Usage of rounds whose message_delta the client never receives.
prior_usage: dict[str, int] = {}
while True:
@@ -560,13 +598,9 @@ class StreamingMixin:
if response is not None:
round_content = response.get("content")
content = round_content if isinstance(round_content, list) else []
visible_content.extend(block for block in content if not is_memory_call(block))
stream_state["memory_client_message"] = {
**response,
"content": list(visible_content),
}
if not memory_filter.hid_tool_calls:
record_visible(response, content, dropped_client_calls=False)
for frame in memory_filter.closing_frames(prior_usage=prior_usage):
yield frame
return
@@ -574,7 +608,11 @@ class StreamingMixin:
# Run the calls the filter recorded, so they are not lost when the
# round could not be reconstructed. Only the proxy's own calls: a
# memory-named tool the client declared is the client's to run.
memory_calls = memory_filter.hidden_calls()
hidden_calls = memory_filter.hidden_calls()
memory_calls = [c for c in hidden_calls if c.get("name") in server_memory_tool_names]
retrieval_calls = [
c for c in hidden_calls if serve_retrieval and c.get("name") == CCR_TOOL_NAME
]
tool_results: list[dict[str, Any]] = []
if memory_calls and memory_user_id is not None and self.memory_handler is not None:
tool_results = await self.memory_handler.handle_memory_tool_calls(
@@ -583,24 +621,38 @@ class StreamingMixin:
"anthropic",
request_context=memory_request_ctx,
)
logger.info(
f"[{request_id}] Memory: Executed {len(tool_results)}/"
f"{len(memory_filter.hidden_tool_names)} proxy-handled tool call(s) "
"withheld from the SSE stream"
)
logger.info(
f"[{request_id}] Memory: Executed {len(tool_results)}/"
f"{len(memory_calls)} proxy-handled tool call(s) "
"withheld from the SSE stream"
)
ccr_handler = getattr(self, "ccr_response_handler", None)
if retrieval_calls and ccr_handler is not None:
retrieval_results = ccr_handler.serve_withheld_retrievals(retrieval_calls)
tool_results = [*tool_results, *retrieval_results]
logger.info(
f"[{request_id}] CCR: Served {len(retrieval_results)}/"
f"{len(retrieval_calls)} headroom_retrieve call(s) withheld from "
"the SSE stream"
)
replayable = response is not None and len(tool_results) == sum(
1 for block in content if is_memory_call(block)
1 for block in content if is_proxy_call(block)
)
# Unrun client calls can be dropped only while the client has not
# seen them, and only for a retrieval: memory results alone do not
# give the model a reason to decide again.
drop_client_calls = bool(retrieval_calls) and memory_filter.holds_client_tool_use
can_continue = (
memory_filter.stop_reason == "tool_use"
and not memory_filter.visible_tool_use
and (drop_client_calls or not memory_filter.holds_client_tool_use)
and messages is not None
and replayable
and rounds < self._MEMORY_CONTINUATION_MAX_ROUNDS
)
if not can_continue:
if memory_filter.visible_tool_use:
if memory_filter.visible_tool_use or memory_filter.holds_client_tool_use:
logger.info(
f"[{request_id}] Memory: Round also called a client tool; "
"returning the turn to the client"
@@ -608,18 +660,31 @@ class StreamingMixin:
elif rounds >= self._MEMORY_CONTINUATION_MAX_ROUNDS:
logger.warning(
f"[{request_id}] Memory: Stopped after {rounds} continuation "
"round(s) that only called memory tools"
"round(s) that only called proxy tools"
)
elif not replayable:
logger.warning(
f"[{request_id}] Memory: Could not rebuild the round for a "
"continuation; ending the turn after running its memory calls"
"continuation; ending the turn after running its proxy calls"
)
record_visible(response, content, dropped_client_calls=False)
for frame in memory_filter.closing_frames(prior_usage=prior_usage):
yield frame
return
assert response is not None and messages is not None and isinstance(base_body, dict)
record_visible(response, content, dropped_client_calls=drop_client_calls)
# Blocks held after the first tool call (rarely any but the calls
# themselves) still belong to the client's message.
for frame in memory_filter.release_held(drop_tool_use=drop_client_calls):
yield frame
if drop_client_calls:
logger.info(
f"[{request_id}] CCR: Round called headroom_retrieve alongside a "
"client tool; serving the retrieval and dropping the unrun client "
"call(s) for the model to re-issue"
)
content = [block for block in content if not is_client_call(block)]
round_usage = response.get("usage")
if isinstance(round_usage, dict):
for key in _ROUND_USAGE_KEYS:
@@ -647,9 +712,10 @@ class StreamingMixin:
yield _upstream_error_frame(error_bytes, request_id)
return
memory_filter = MemoryToolStreamFilter(
server_memory_tool_names,
proxy_tool_names,
index_offset=memory_filter.next_index,
forward_message_start=False,
hold_tool_use=serve_retrieval,
)
# Same cap as the first round's buffer; past it the round is
# still streamed, just not rebuilt for a further continuation.
@@ -1082,6 +1148,7 @@ class StreamingMixin:
savings_measure: ItemMeasure | None = None,
server_memory_tool_names: frozenset[str] | None = None,
client_beta: str | None = None,
serve_ccr_retrieval: bool = False,
) -> Response | StreamingResponse:
"""Stream response with metrics tracking and memory tool handling.
@@ -1130,6 +1197,7 @@ class StreamingMixin:
savings_measure=savings_measure,
server_memory_tool_names=server_memory_tool_names,
client_beta=client_beta,
serve_ccr_retrieval=serve_ccr_retrieval,
)
except (Exception, asyncio.CancelledError):
self._cleanup_mid_turn_stream(session_key)
@@ -1165,6 +1233,7 @@ class StreamingMixin:
savings_measure: ItemMeasure | None = None,
server_memory_tool_names: frozenset[str] | None = None,
client_beta: str | None = None,
serve_ccr_retrieval: bool = False,
) -> Response | StreamingResponse:
"""Actual streaming implementation, guarded by _stream_response's cleanup wrapper."""
from fastapi.responses import Response, StreamingResponse
@@ -1339,6 +1408,9 @@ class StreamingMixin:
and self.memory_handler is not None
and provider == "anthropic"
)
# Withholding headroom_retrieve needs the Anthropic SSE shape the
# filter and the continuation round are written for.
serve_ccr_retrieval = serve_ccr_retrieval and provider == "anthropic"
# Open connection before generator to capture upstream response headers
# (needed to forward ratelimit headers to the client via StreamingResponse)
@@ -1479,7 +1551,7 @@ class StreamingMixin:
if upstream_response.status_code >= 400:
logger.warning(
"[%s] Forwarding upstream streaming error status=%s url=%s",
"[%s] Upstream provider returned an error; forwarding it to the client status=%s url=%s",
request_id,
upstream_response.status_code,
url,
@@ -1672,11 +1744,23 @@ class StreamingMixin:
pending_messages: list[dict] = []
# Proxy-injected memory tools are invisible to the client, so their
# tool_use blocks must never reach it (GH #2195).
from headroom.ccr.tool_injection import CCR_TOOL_NAME
# Memory tools are withheld only when this request can run them;
# the continuation must classify calls exactly as the filter did.
filter_memory_tool_names = (
server_memory_tool_names or frozenset() if memory_enabled else frozenset()
)
proxy_tool_names = filter_memory_tool_names | (
frozenset({CCR_TOOL_NAME}) if serve_ccr_retrieval else frozenset()
)
memory_filter = (
MemoryToolStreamFilter(server_memory_tool_names)
if memory_enabled and server_memory_tool_names
MemoryToolStreamFilter(proxy_tool_names, hold_tool_use=serve_ccr_retrieval)
if proxy_tool_names
else None
)
# A withheld retrieval needs the round rebuilt for its continuation.
retrieval_tracking = serve_ccr_retrieval
def _client_frame(frame: bytes) -> bytes:
"""``_guard_client_bytes``, noting when the terminal event goes out."""
@@ -1743,6 +1827,7 @@ class StreamingMixin:
_track_sse = (
_codex_wire_debug
or memory_enabled
or retrieval_tracking
or (prefix_tracker is not None and provider == "anthropic")
)
if _track_sse:
@@ -1755,6 +1840,7 @@ class StreamingMixin:
"disabling memory detection for this request"
)
memory_enabled = False
retrieval_tracking = False
# Parse complete SSE events from buffer. A failure (e.g.
# invalid UTF-8) is raised once the chunk is forwarded:
@@ -1815,7 +1901,7 @@ class StreamingMixin:
# be an upstream protocol violation — surface loudly.
full_sse_data: str = full_sse_bytes.decode("utf-8") if full_sse_bytes else ""
if memory_enabled and full_sse_data:
if (memory_enabled or retrieval_tracking) and full_sse_data:
# Check for Claude Code credential error
if "only authorized for use with Claude Code" in full_sse_data:
logger.warning(
@@ -1845,9 +1931,10 @@ class StreamingMixin:
outbound_bytes=outbound_bytes,
memory_user_id=memory_user_id,
memory_request_ctx=memory_request_ctx,
server_memory_tool_names=server_memory_tool_names or frozenset(),
server_memory_tool_names=filter_memory_tool_names,
stream_state=stream_state,
request_id=request_id,
serve_retrieval=serve_ccr_retrieval,
):
guarded_frame = _client_frame(frame)
if guarded_frame:
@@ -1856,8 +1943,10 @@ class StreamingMixin:
parsed_response = (
stream_state.pop("memory_client_message", None) or parsed_response
)
elif parsed_response and self.memory_handler.has_memory_tool_calls(
parsed_response, provider
elif (
memory_enabled
and parsed_response
and self.memory_handler.has_memory_tool_calls(parsed_response, provider)
):
logger.info(
f"[{request_id}] Memory: Detected tool calls in streaming response"
@@ -1890,9 +1979,10 @@ class StreamingMixin:
outbound_bytes=outbound_bytes,
memory_user_id=memory_user_id,
memory_request_ctx=memory_request_ctx,
server_memory_tool_names=server_memory_tool_names or frozenset(),
server_memory_tool_names=filter_memory_tool_names,
stream_state=stream_state,
request_id=request_id,
serve_retrieval=serve_ccr_retrieval,
):
guarded_frame = _client_frame(frame)
if guarded_frame:
+10 -2
View File
@@ -1327,8 +1327,11 @@ def append_text_to_latest_user_input_item(
return body_input, 0
# Maximum request body size (100MB - increased to support image-heavy requests)
MAX_REQUEST_BODY_SIZE = 100 * 1024 * 1024
# Bound the entire history before JSON parsing or compression. Larger image-heavy
# bodies can amplify far beyond their wire size during preprocessing (#4139).
MAX_REQUEST_BODY_SIZE = request_limit_policy.resolve_request_body_max_bytes(
os.environ.get(request_limit_policy.REQUEST_BODY_MAX_BYTES_ENV)
)
# A *decompressed* body obeys the same ceiling as an uncompressed one. The
# Content-Length gate at the handlers only ever saw the compressed wire size, so
@@ -3507,6 +3510,11 @@ async def read_request_json_with_bytes(request: Request) -> tuple[dict[str, Any]
"Request body must be a JSON object, not " + type(result).__name__
)
# Large histories are routed from their envelope only. Leave their content
# untouched for the soft-limit passthrough, before any normalization/copies.
if len(raw) > request_limit_policy.COMPRESSION_BODY_MAX_BYTES:
return result, raw
# Drop output-only blocks (see ``strip_output_only_request_blocks``) before
# any downstream deepcopy / compression / 400-retry path. This is the shared
# reader for the Anthropic, OpenAI, and Bedrock handlers, so one guard here
+81 -8
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):
@@ -90,6 +91,12 @@ class MemoryToolStreamFilter:
extend the message the client already opened.
max_retained_bytes: Limit on the bytes held back at any time;
``DEFAULT_MAX_RETAINED_BYTES`` when None.
hold_tool_use: Hold every block from the round's first ``tool_use``
on, until the caller decides with ``release_held``. A withheld
``headroom_retrieve`` alongside a client tool is served by
dropping the client call for the model to re-issue, which is
only possible while that call has not reached the client.
Text before the first tool call still streams live.
"""
def __init__(
@@ -99,8 +106,17 @@ class MemoryToolStreamFilter:
index_offset: int = 0,
forward_message_start: bool = True,
max_retained_bytes: int | None = None,
hold_tool_use: bool = False,
) -> None:
self._tool_names = frozenset(tool_names)
self._hold_tool_use = hold_tool_use
self._holding = False
# Frames held from the first tool_use on, with their upstream index;
# client indices are assigned when they are released.
self._held: list[tuple[str, dict[str, Any], bytes, int]] = []
self._held_bytes = 0
# Upstream indices of held client tool_use blocks.
self._held_tool_use: set[int] = set()
self._max_retained_bytes = (
DEFAULT_MAX_RETAINED_BYTES if max_retained_bytes is None else max_retained_bytes
)
@@ -130,6 +146,39 @@ class MemoryToolStreamFilter:
def hidden_tool_names(self) -> list[str]:
return [block["name"] for block, _ in self._hidden.values()]
@property
def holds_client_tool_use(self) -> bool:
"""A client ``tool_use`` block is held and has not reached the client."""
return bool(self._held_tool_use)
def release_held(self, *, drop_tool_use: bool = False) -> list[bytes]:
"""Return the held frames, re-indexed, for the client.
``drop_tool_use`` leaves out the held client ``tool_use`` blocks: the
round goes back to the model with the proxy's results instead, and
the model re-issues whatever it still needs.
"""
frames: list[bytes] = []
dropped = self._held_tool_use if drop_tool_use else set()
for event_name, payload, frame, upstream_index in self._held:
if upstream_index in dropped:
continue
if event_name == "content_block_start":
block = payload.get("content_block")
if isinstance(block, dict) and block.get("type") == "tool_use":
self.visible_tool_use = True
self._index_map[upstream_index] = self.next_index
self.next_index += 1
client_index = self._index_map.get(upstream_index, upstream_index)
if client_index == upstream_index:
frames.append(frame)
else:
frames.append(_render_frame(event_name, {**payload, "index": client_index}))
self._held.clear()
self._held_bytes = 0
self._held_tool_use.clear()
return frames
def hidden_calls(self) -> list[dict[str, Any]]:
"""The withheld ``tool_use`` blocks that finished with a complete input.
@@ -181,7 +230,12 @@ class MemoryToolStreamFilter:
forwarded = self._route(frame)
if forwarded is not None:
out.append(forwarded)
retained = len(self._buffer) + self._tail_bytes + sum(self._hidden_bytes.values())
retained = (
len(self._buffer)
+ self._tail_bytes
+ self._held_bytes
+ sum(self._hidden_bytes.values())
)
if retained > self._max_retained_bytes:
self._buffer.clear()
self._hidden.clear()
@@ -189,6 +243,9 @@ class MemoryToolStreamFilter:
self._hidden_stopped.clear()
self._tail.clear()
self._tail_bytes = 0
self._held.clear()
self._held_bytes = 0
self._held_tool_use.clear()
raise MemoryToolStreamOverflowError(
f"memory tool stream filter would retain {retained} bytes "
f"(limit {self._max_retained_bytes})"
@@ -232,11 +289,18 @@ class MemoryToolStreamFilter:
self._hidden[upstream_index] = (block, [])
self._hidden_bytes[upstream_index] = len(frame)
self._hidden_stopped.discard(upstream_index)
self._holding = self._hold_tool_use
return None
if isinstance(block, dict) and block.get("type") == "tool_use":
self.visible_tool_use = True
self._index_map[upstream_index] = self.next_index
self.next_index += 1
is_tool_use = isinstance(block, dict) and block.get("type") == "tool_use"
if self._hold_tool_use and (self._holding or is_tool_use):
self._holding = True
if is_tool_use:
self._held_tool_use.add(upstream_index)
else:
if is_tool_use:
self.visible_tool_use = True
self._index_map[upstream_index] = self.next_index
self.next_index += 1
if upstream_index in self._hidden:
delta = payload.get("delta")
@@ -252,17 +316,26 @@ class MemoryToolStreamFilter:
elif event_name == "content_block_stop":
self._hidden_stopped.add(upstream_index)
return None
if self._holding and upstream_index not in self._index_map:
self._held.append((event_name, payload, frame, upstream_index))
self._held_bytes += len(frame)
return None
client_index = self._index_map.get(upstream_index, upstream_index)
if client_index == upstream_index:
return frame
return _render_frame(event_name, {**payload, "index": client_index})
def closing_frames(self, *, prior_usage: dict[str, int] | None = None) -> list[bytes]:
"""End the client message after this round without a continuation."""
"""End the client message after this round without a continuation.
Held frames are released first, client tool calls included: with no
continuation, the round is the model's answer as it stands.
"""
held = self.release_held()
ends_on_hidden_call = (
self.hid_tool_calls and not self.visible_tool_use and self.stop_reason == "tool_use"
)
return self.tail_frames(
return held + self.tail_frames(
stop_reason="end_turn" if ends_on_hidden_call else None,
prior_usage=prior_usage,
)
+18
View File
@@ -5,6 +5,11 @@ from __future__ import annotations
SSE_EVENT_MAX_BYTES_ENV = "HEADROOM_SSE_BUFFER_MAX_BYTES"
SSE_EVENT_MAX_BYTES_DEFAULT = 1 * 1024 * 1024
REQUEST_BODY_MAX_BYTES_ENV = "HEADROOM_REQUEST_BODY_MAX_BYTES"
REQUEST_BODY_MAX_BYTES_DEFAULT = 100 * 1024 * 1024
# Larger admitted histories bypass compression instead of failing admission.
COMPRESSION_BODY_MAX_BYTES = 16 * 1024 * 1024
BODY_TOO_LARGE_STATUS_ENV = "HEADROOM_PROXY_BODY_TOO_LARGE_STATUS"
BODY_TOO_LARGE_STATUS_DEFAULT = 413
@@ -22,6 +27,19 @@ def resolve_sse_event_max_bytes(raw: str | None) -> int:
return value
def resolve_request_body_max_bytes(raw: str | None) -> int:
"""Resolve the whole-request ceiling before the proxy starts."""
if raw is None or raw == "":
return REQUEST_BODY_MAX_BYTES_DEFAULT
try:
value = int(raw)
except ValueError as exc:
raise ValueError(f"{REQUEST_BODY_MAX_BYTES_ENV} must be an integer, got {raw!r}") from exc
if value <= 0:
raise ValueError(f"{REQUEST_BODY_MAX_BYTES_ENV} must be positive, got {value}")
return value
def resolve_body_too_large_status(raw: str | None) -> int:
"""Resolve the HTTP status code for body-too-large rejections."""
if raw is None or raw == "":
+99 -7
View File
@@ -119,7 +119,7 @@ from headroom.providers.registry import (
)
from headroom.proxy import runtime_env
from headroom.proxy.audit import is_auditable_path, record_admin_action
from headroom.proxy.auth_mode import should_stamp_codex_client
from headroom.proxy.auth_mode import CLIENT_UA_MAP, should_stamp_codex_client
from headroom.proxy.background_compression import BackgroundCompressor
from headroom.proxy.budget_basis_policy import resolve_estimated_basis_policy
from headroom.proxy.buffered_ccr_response import DEFAULT_BUFFERED_CCR_GRACE_SECONDS
@@ -185,6 +185,7 @@ from headroom.proxy.project_context import (
set_registered_cwd,
strip_project_path_prefix,
)
from headroom.proxy.project_policy import split_project_path
from headroom.proxy.prometheus_metrics import PrometheusMetrics
from headroom.proxy.rate_limiter import TokenBucketRateLimiter
from headroom.proxy.request_body_limit import RequestBodyLimitMiddleware
@@ -3033,6 +3034,75 @@ def read_proxy_token(headers: Mapping[str, str]) -> str | None:
return None
# Generic HTTP clients a ``proxy_auth_rejected`` line may name by their
# User-Agent product token, after the agent harnesses in CLIENT_UA_MAP. Any
# other User-Agent logs as ``other``: the header is caller-controlled text.
_AUTH_REJECTION_CLIENTS = frozenset(
{
"anthropic",
"axios",
"codex_cli_rs",
"curl",
"go-http-client",
"mozilla",
"node",
"okhttp",
"openai",
"python-httpx",
"python-requests",
"python-urllib",
"wget",
}
)
def _auth_rejection_context(headers: Mapping[str, str]) -> tuple[str, str]:
"""``(ua, xff)`` for a rejection log line, drawn from a fixed vocabulary.
Both headers are caller-controlled, so neither is logged as received.
``ua`` is the harness name ``CLIENT_UA_MAP`` gives the User-Agent (the
same names the rest of the proxy uses, e.g. ``claude-code``, ``codex``),
else its product token when ``_AUTH_REJECTION_CLIENTS`` lists it, else
``other``. ``X-Client`` is deliberately not consulted: unlike
``classify_client``, nothing here may echo free text. ``xff`` is the leftmost
``X-Forwarded-For`` hop when it is a plain IP address, else ``invalid``.
Either is ``none`` when its header is absent. ``xff`` is unverified, so it
only helps attribute rejected callers behind a gateway whose peer address
is all the gate sees. It is never used for any decision. Credentials are
never read here.
"""
from headroom.proxy.forwarded_policy import header_first
ua_lower = str(headers.get("user-agent") or "").lower()
product = ua_lower.partition("/")[0].strip()
harness = next((name for needle, name in CLIENT_UA_MAP if needle in ua_lower), None)
if not ua_lower:
ua = "none"
elif harness is not None:
ua = harness
else:
ua = product if product in _AUTH_REJECTION_CLIENTS else "other"
hop = header_first(str(headers.get("x-forwarded-for") or ""))
try:
address = ipaddress.ip_address(hop)
except ValueError:
return ua, "invalid" if hop else "none"
# An IPv6 zone index (fe80::1%<zone>) is free text, so refuse it too.
return ua, "invalid" if getattr(address, "scope_id", None) else str(address)
def _auth_rejection_path(path: object) -> str:
"""The request path for a rejection log line, with control characters escaped.
The ASGI server percent-decodes the path, so an unauthenticated caller can
put raw CR/LF, NUL or terminal escapes in it (``%0A``, ``%1B``) and forge or
garble log lines. ``unicode_escape`` renders every non-printable or
non-ASCII character (and the backslash itself) as an escape sequence.
"""
return str(path).encode("unicode_escape").decode("ascii")
_PROXY_TOKEN_HEADER = b"x-headroom-proxy-token"
@@ -3157,11 +3227,15 @@ class WebSocketAuthMiddleware:
await self.app(scope, receive, send)
return
user_agent, forwarded_for = _auth_rejection_context(headers)
logger.warning(
"event=proxy_auth_rejected transport=websocket path=%s client=%s reason=%s",
scope.get("path"),
"event=proxy_auth_rejected transport=websocket path=%s client=%s reason=%s "
"ua=%s xff=%s",
_auth_rejection_path(scope.get("path")),
client_host,
"missing_token" if provided is None else "bad_token",
user_agent,
forwarded_for,
)
# Receive the handshake before refusing it: ASGI servers send
# ``websocket.connect`` and wait for the application to answer, and
@@ -4290,17 +4364,35 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
path = request.url.path
client = getattr(request, "client", None)
client_host = getattr(client, "host", None) if client is not None else None
exempt = request.method == "GET" and path in _AUTH_EXEMPT_PATHS
# Anthropic SDK clients (Claude Code among them) probe reachability
# with an unauthenticated GET/HEAD /api/hello that never carries
# custom headers, so it cannot hold the proxy token. Let it past
# the token check, marked so its passthrough route answers it
# locally and empty instead of relaying an anonymous request
# upstream. It still runs through extension middleware, which sits
# inside this gate and may refuse it. The path is matched as
# routed: a /p/<project> prefix is only stripped further in.
hello_probe = (
request.method in ("GET", "HEAD") and split_project_path(path)[1] == "/api/hello"
)
if hello_probe:
request.state.headroom_local_hello_probe = True
exempt = hello_probe or (request.method == "GET" and path in _AUTH_EXEMPT_PATHS)
if not exempt and not is_loopback_host(client_host):
provided = _extract_proxy_token(request.headers)
if provided is None or not hmac.compare_digest(
provided.encode("utf-8", "replace"), _proxy_token_bytes
):
user_agent, forwarded_for = _auth_rejection_context(request.headers)
logger.warning(
"event=proxy_auth_rejected path=%s client=%s reason=%s",
path,
"event=proxy_auth_rejected method=%s path=%s client=%s reason=%s "
"ua=%s xff=%s",
request.method,
_auth_rejection_path(path),
client_host,
"missing_token" if provided is None else "bad_token",
user_agent,
forwarded_for,
)
rejection = JSONResponse(status_code=401, content={"error": "unauthorized"})
_apply_security_headers(rejection)
@@ -5794,7 +5886,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
)
# Debug endpoints
@app.get("/debug/memory", dependencies=[Depends(_require_loopback)])
@app.get("/debug/memory", dependencies=[Depends(_require_loopback_or_container_gateway)])
async def debug_memory():
"""Get detailed memory usage statistics.
+10 -4
View File
@@ -10,6 +10,12 @@ SSE_EVENT_LINE_PREFIX = "event:"
SSE_DATA_LINE_PREFIX = "data:"
def _field_value(line: str, prefix: str) -> str:
"""Value after ``prefix``; per the SSE spec only one leading space is removed."""
value = line[len(prefix) :]
return value[1:] if value.startswith(" ") else value
def find_sse_event_terminator(buf: bytearray) -> tuple[int, int] | None:
"""Return the earliest complete SSE event terminator in ``buf``."""
matches = [
@@ -42,18 +48,18 @@ 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(":"):
continue
if line.startswith(SSE_EVENT_LINE_PREFIX):
event_name = line[len(SSE_EVENT_LINE_PREFIX) :].lstrip()
event_name = _field_value(line, SSE_EVENT_LINE_PREFIX)
elif line.startswith(SSE_DATA_LINE_PREFIX):
data_lines.append(line[len(SSE_DATA_LINE_PREFIX) :].lstrip())
data_lines.append(_field_value(line, SSE_DATA_LINE_PREFIX))
if data_lines:
events.append((event_name, "\n".join(data_lines)))
+24 -2
View File
@@ -26,6 +26,8 @@ class FileSystemTOINBackend:
Args:
path: Path to the JSON storage file.
max_load_bytes: Maximum file size in bytes for loading and saving.
None disables the limit.
"""
def __init__(self, path: str, *, max_load_bytes: int | None = None) -> None:
@@ -48,7 +50,7 @@ class FileSystemTOINBackend:
):
logger.warning(
"TOIN data file %s exceeds the configured load limit (%d bytes); "
"ignoring it until the store is rebuilt",
"leaving it unchanged",
self._path,
self._max_load_bytes,
)
@@ -70,9 +72,20 @@ class FileSystemTOINBackend:
data: Serialized TOIN data to persist.
Raises:
ValueError: The existing file or serialized data exceeds the size limit.
OSError: If persistence fails, so the caller can retain pending changes.
"""
try:
# A file skipped by load() must not be replaced with partial state.
if (
self._max_load_bytes is not None
and self._path.exists()
and self._path.stat().st_size > self._max_load_bytes
):
raise ValueError(
f"Existing TOIN data file {self._path} exceeds the configured "
f"size limit ({self._max_load_bytes} bytes); refusing to overwrite it"
)
self._path.parent.mkdir(parents=True, exist_ok=True)
fd, tmp_path = tempfile.mkstemp(dir=self._path.parent, prefix=".toin_", suffix=".tmp")
@@ -80,8 +93,17 @@ class FileSystemTOINBackend:
# Stream compact JSON directly to the temp file. Building a
# second multi-gigabyte string was the primary autosave RSS
# spike reported in #2886.
with open(fd, "w") as f:
with open(fd, "w", newline="\n") as f:
json.dump(data, f, separators=(",", ":"))
# Check actual bytes after flushing, before replacing a valid store.
if (
self._max_load_bytes is not None
and Path(tmp_path).stat().st_size > self._max_load_bytes
):
raise ValueError(
f"Serialized TOIN data exceeds the configured size limit "
f"({self._max_load_bytes} bytes)"
)
Path(tmp_path).replace(self._path)
except Exception:
try:
+20 -11
View File
@@ -1,7 +1,8 @@
"""Session aggregation for the anonymous telemetry beacon.
One wide event per session, emitted when the session goes idle. Everything here
is off unless ``HEADROOM_TELEMETRY`` is explicitly on — see
is on by default and off when the beacon is disabled (``HEADROOM_BEACON=off``,
``HEADROOM_TELEMETRY=off``, ``DO_NOT_TRACK=1`` or offline mode) — see
:mod:`headroom.telemetry.beacon`.
# What counts as a session
@@ -119,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__)
@@ -510,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:
@@ -529,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:
@@ -545,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
@@ -1905,8 +1914,8 @@ def record_compression(strategy: str, original_tokens: int, compressed_tokens: i
(``PrometheusMetrics.tokens_saved_by_strategy``); they just never left the
process.
Same discipline as the rest of this module: off by default and cheap when
off, never raises. This runs once per routing decision, so it must not do
Same discipline as the rest of this module: cheap when the beacon is
disabled, never raises. This runs once per routing decision, so it must not do
anything a request would notice.
"""
from headroom.telemetry.beacon import is_beacon_enabled
@@ -2022,7 +2031,7 @@ def record_content_shape(
Single-observation form of :func:`record_content_shapes`, which is what the
router actually calls. Same discipline as everything else in this module:
off by default, cheap when off, never raises, and never touches the
cheap when the beacon is disabled, never raises, and never touches the
aggregator lock.
"""
record_content_shapes(((content_type, strategy, original_tokens, compressed_tokens),))
@@ -2200,8 +2209,8 @@ def _drain_staged_tool_shapes() -> dict[str, list[int]]:
def record_outcome(outcome: Any) -> None:
"""Beacon entry point, called from the proxy's outcome funnel.
Off by default and cheap when off: the enabled check short-circuits before
the aggregator is ever constructed, so a user who never opted in pays one
Cheap when the beacon is disabled: the enabled check short-circuits before
the aggregator is ever constructed, so a user who has opted out pays one
env lookup per request and allocates nothing.
"""
from headroom.telemetry.beacon import is_beacon_enabled
+1 -1
View File
@@ -489,7 +489,7 @@ class TOINConfig:
storage_path: str = field(default_factory=get_default_toin_storage_path)
auto_save_interval: int = 600 # Auto-save every 10 minutes
max_patterns: int = DEFAULT_MAX_PATTERNS
max_storage_bytes: int = DEFAULT_MAX_STORAGE_BYTES
max_storage_bytes: int = DEFAULT_MAX_STORAGE_BYTES # Filesystem load and save limit
# Network learning thresholds
min_samples_for_recommendation: int = 10
+28 -8
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
@@ -141,8 +142,16 @@ class TokenizerRegistry:
# Registered factories (backend -> factory function)
_factories: dict[str, Callable[[str], TokenCounter]] = {}
# Cache for auto-detected tokenizers
# Registered model factories (model -> factory function)
_model_factories: dict[str, Callable[[str], TokenCounter]] = {}
# 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."""
@@ -163,6 +172,7 @@ class TokenizerRegistry:
"moonshot": self._create_moonshot,
"estimation": self._create_estimation,
}
self._model_factories = {}
@classmethod
def get(
@@ -194,13 +204,20 @@ 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
# Create tokenizer
try:
tokenizer = registry._create_tokenizer(model, backend)
registry._cache[cache_key] = tokenizer
if uses_model_factory:
tokenizer = registry._model_factories[model_lower](model)
else:
tokenizer = registry._create_tokenizer(model, backend)
remember_bounded(registry._cache, cache_key, tokenizer, cls._CACHE_MAXSIZE)
return tokenizer
except Exception as e:
if fallback:
@@ -208,7 +225,8 @@ class TokenizerRegistry:
f"Failed to create tokenizer for {model}: {e}. Falling back to estimation."
)
tokenizer = EstimatingTokenCounter()
registry._cache[cache_key] = tokenizer
if not uses_model_factory:
remember_bounded(registry._cache, cache_key, tokenizer, cls._CACHE_MAXSIZE)
return tokenizer
raise ValueError(f"No tokenizer available for {model}: {e}") from e
@@ -234,8 +252,10 @@ class TokenizerRegistry:
if tokenizer is not None:
registry._tokenizers[model_lower] = tokenizer
registry._model_factories.pop(model_lower, None)
elif factory is not None:
registry._factories[model_lower] = factory
registry._model_factories[model_lower] = factory
registry._tokenizers.pop(model_lower, None)
else:
raise ValueError("Must provide either tokenizer or factory")
@@ -269,7 +289,7 @@ class TokenizerRegistry:
def list_registered(cls) -> list[str]:
"""List explicitly registered models."""
registry = cls()
return list(registry._tokenizers.keys())
return list(dict.fromkeys((*registry._tokenizers, *registry._model_factories)))
@classmethod
def clear_cache(cls) -> None:
+4 -1
View File
@@ -170,7 +170,10 @@ def get_encoding_for_model(model: str) -> str:
# verbatim, matched no prefix, and silently took DEFAULT_ENCODING -- so the
# encoding a model got depended on the casing of whichever request warmed
# the cache first, and flipped across restarts.
model = model.lower()
# Gateway-wrapped ids ("azure/gpt-4o", "openrouter/openai/gpt-5") are picked
# for tiktoken by the registry's unwrapping but arrive here as-is, so match on
# the bare id or they all take DEFAULT_ENCODING.
model = model.lower().rsplit("/", 1)[-1]
# Direct lookup
if model in MODEL_TO_ENCODING:
+98 -8
View File
@@ -643,6 +643,10 @@ class CodeStructure:
comments: list[str] = field(default_factory=list)
top_level_code: list[str] = field(default_factory=list)
other: list[str] = field(default_factory=list)
# Set when the fixed assembly order (declarations before top-level code)
# moves a definition across executable code, changing class bases,
# decorators, or default expressions evaluated at definition time.
reorder_unsafe: bool = False
@dataclass
@@ -1611,6 +1615,14 @@ class CodeAwareCompressor(Transform):
else:
structure = self._extract_generic_structure(root, code)
# Declarations are assembled before the top-level code bucket, so
# executable code that precedes a definition would be moved after it.
# That changes definition-time behavior (class bases, decorators,
# default expressions), so keep the original instead of returning a
# semantically reordered result.
if structure.reorder_unsafe:
return code, structure, {}
# Assemble compressed code
compressed = self._assemble_compressed(structure, language)
@@ -1645,6 +1657,11 @@ class CodeAwareCompressor(Transform):
"""
structure = CodeStructure()
captured_byte_ranges: list[tuple[int, int]] = []
# Import text keyed by start byte, so a root-level import that turns
# out to follow executable code can later be moved back out of
# ``structure.imports`` (which is emitted first) unchanged.
import_entries: dict[int, str] = {}
demoted_import_starts: set[int] = set()
def _validated_candidate(node: Any, compressed: str) -> str:
if candidate_validator is None:
@@ -1664,7 +1681,9 @@ class CodeAwareCompressor(Transform):
# Import statements
if node_type in lang_config.import_nodes:
leading = _get_leading_comment_text(node, code, captured_byte_ranges)
structure.imports.append(leading + _get_node_text(node, code))
entry = leading + _get_node_text(node, code)
structure.imports.append(entry)
import_entries[node.start_byte] = entry
captured_byte_ranges.append((node.start_byte, node.end_byte))
return
@@ -1816,15 +1835,62 @@ class CodeAwareCompressor(Transform):
# e.g. tree-sitter-c-sharp rejects top-level `#region` after a type
# declaration, which would fail validation and forfeit compression.
first_captured = min((r[0] for r in captured_byte_ranges), default=None)
declaration_nodes = (
lang_config.function_nodes
| lang_config.class_nodes
| lang_config.type_nodes
| (lang_config.container_node_types or frozenset())
)
if lang_config.decorator_node:
declaration_nodes = declaration_nodes | {lang_config.decorator_node}
saw_top_level_code = False
for child in root.children:
child_range = (child.start_byte, child.end_byte)
if child_range not in captured_byte_ranges:
text = _get_node_text(child, code).strip()
if text:
if first_captured is not None and child.end_byte <= first_captured:
structure.header_code.append(text)
else:
structure.top_level_code.append(text)
if child_range in captured_byte_ranges:
# Python runs top-level statements in source order. An import
# that follows executable code cannot be hoisted above it:
# `import sys; sys.path.insert(0, 'vendor'); import plugin`
# would import the plugin before the path update. Emit such
# imports in place with the rest of the top-level code.
if (
language == CodeLanguage.PYTHON
and saw_top_level_code
and child.type in lang_config.import_nodes
):
entry = import_entries.get(child.start_byte)
if entry is not None:
structure.top_level_code.append(entry)
demoted_import_starts.add(child.start_byte)
# Declarations are emitted before the top-level code bucket,
# so a definition after executable code would run before it.
if (
language == CodeLanguage.PYTHON
and saw_top_level_code
and child.type in declaration_nodes
):
structure.reorder_unsafe = True
continue
# In Python a `;` between two statements on one physical line
# is a root-level anonymous token. Emitted as its own fragment
# it becomes a bare `;` line and invalidates the output.
if not child.is_named and child.type in (";", ","):
continue
text = _get_node_text(child, code).strip()
if text:
if first_captured is not None and child.end_byte <= first_captured:
structure.header_code.append(text)
else:
structure.top_level_code.append(text)
saw_top_level_code = True
if demoted_import_starts:
# Python only: the import branch above is the sole producer of
# ``structure.imports`` entries for this language.
structure.imports = [
entry
for start, entry in import_entries.items()
if start not in demoted_import_starts
]
return structure
@@ -2043,6 +2109,21 @@ class CodeAwareCompressor(Transform):
continue
body_stmts.append((child.start_point[0], child.end_point[0]))
# Statements can share a physical line (`a = 1; b = 2`). Lines are kept
# whole, so merge overlapping ranges into one entry: keeping both would
# duplicate the shared line, and dropping the later range would lose
# continuation lines of a multi-line statement after the `;`.
merged_stmts: list[tuple[int, int]] = []
for start_row, end_row in body_stmts:
if merged_stmts and start_row <= merged_stmts[-1][1]:
merged_stmts[-1] = (
merged_stmts[-1][0],
max(merged_stmts[-1][1], end_row),
)
else:
merged_stmts.append((start_row, end_row))
body_stmts = merged_stmts
# Calculate lines per statement and keep whole statements until budget
kept_lines: list[str] = []
kept_line_count = 0
@@ -2160,6 +2241,7 @@ class CodeAwareCompressor(Transform):
# Process each child of the class body individually
body_parts: list[str] = []
processed_ranges: list[tuple[int, int]] = []
last_processed_end = -1
for child in body_node.children:
if not child.is_named:
@@ -2170,6 +2252,14 @@ class CodeAwareCompressor(Transform):
child_end = child.end_point[0]
if child.end_point[1] == 0 and child_end > child_start:
child_end -= 1
# Statements can share a physical line (`x = 1; y = 2`). The line
# slice already carries all of them; skip only rows already
# emitted, keeping any continuation lines of this node.
if child_start <= last_processed_end:
child_start = last_processed_end + 1
if child_start > child_end:
continue
last_processed_end = child_end
child_text = "\n".join(code_lines[child_start : child_end + 1])
# Methods/functions inside the class — compress individually
+27 -9
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
@@ -8109,11 +8118,14 @@ class ContentRouter(Transform):
route_counts["already_compressed"] += 1
continue
# Two-tier compression cache → shared helper
# Two-tier compression cache → shared helper. Tool output is
# gated (#1307), so it keys into the gated namespace like the
# string path: an ungated entry for the same bytes must not
# answer this lookup before the gate runs (#4477).
compressed_content, was_compressed = self._compress_block_content(
content=tool_text,
content_key=self._content_cache_key(
tool_text, bias=bias, context=block_context
tool_text, bias=bias, context=block_context, gated=True
),
context=block_context,
bias=bias,
@@ -8204,11 +8216,15 @@ class ContentRouter(Transform):
route_counts["already_compressed"] += 1
continue
# Two-tier compression cache → shared helper
# Two-tier compression cache → shared helper. A role:"tool" /
# "function" message in block-list form is tool output, the
# same ground truth as its string form, so it gets the same
# #1307 gate and the gated cache namespace (#4477).
enforce_reversibility = role in ("tool", "function")
compressed_content, _was_compressed = self._compress_block_content(
content=text_content,
content_key=self._content_cache_key(
text_content, bias=1.0, context=context
text_content, bias=1.0, context=context, gated=enforce_reversibility
),
context=context,
bias=1.0,
@@ -8219,6 +8235,7 @@ class ContentRouter(Transform):
compressed_details=compressed_details,
strategy_label="text_block",
details_prefix="text",
enforce_reversibility=enforce_reversibility,
)
if compressed_content is not None:
new_blocks.append({**block, "text": compressed_content})
@@ -8398,8 +8415,9 @@ class ContentRouter(Transform):
# (kompress/text/code/html) that emitted no CCR retrieve marker is
# unrecoverable, so the agent would act on a fabricated summary
# (#1307). The string/`role=="tool"` path guards this; mirror it
# here for tool_result blocks (never cached, so the Tier-2 path
# above can't serve a poisoned entry).
# here for tool output in block form. Callers that enforce the gate
# key with ``gated=True``, so the Tier-2 path above never serves
# them an ungated entry for the same bytes.
#
# EXCEPTION: no-CCR mode (config.ccr_inject_marker=False). Here the
# operator has *deliberately* disabled retrieval markers — recovery
+7
View File
@@ -21,6 +21,11 @@ from __future__ import annotations
MIN_LINE_CHARS = 300
MAX_SPACE_RATIO = 0.06
# Chinese, Japanese and Thai prose puts no spaces between words, so a long
# paragraph passes the space test. Minified assets, base64 and RSC payloads are
# almost entirely ASCII, so a line with more non-ASCII than this is text, even
# when HTML markup or long URLs around the prose make up most of the line.
MAX_NON_ASCII_RATIO = 0.1
# A single dense line (a JWT, a signed URL, a PATH, an RSA modulus) is a value
# the agent asked for, not a dump: a block is only elided when its dense lines
# add up to at least this many chars. Real bundle dumps are tens of KB.
@@ -41,6 +46,8 @@ def is_dense_line(line: str) -> bool:
# asked for; minified assets and encoded blobs never carry tabs.
if n < MIN_LINE_CHARS or "\t" in line or (line.count(" ") / n) >= MAX_SPACE_RATIO:
return False
if not line.isascii() and n - len(line.encode("ascii", "ignore")) > n * MAX_NON_ASCII_RATIO:
return False
stripped = line.strip()
if stripped[:1] in "{[" and stripped[-1:] in "}]":
return False
@@ -87,6 +87,8 @@ _KOMPRESS_MUST_KEEP_RE = re.compile(
r"|(?i:\b(?:and|or|nor|xor)\b)"
)
_KOMPRESS_MUST_KEEP_ENV = "HEADROOM_KOMPRESS_MUST_KEEP"
#: Tokenizer budget per chunk. Words past it are never scored (#4309).
_KOMPRESS_MAX_TOKENS = 512
KOMPRESS_BACKEND_ENV = "HEADROOM_KOMPRESS_BACKEND"
KOMPRESS_ONNX_FILENAME_ENV = "HEADROOM_KOMPRESS_ONNX_FILENAME"
@@ -142,6 +144,39 @@ def _add_kompress_must_keep_words(
kept_ids.add(word_idx + chunk_start)
def _add_truncated_words(
kept_ids: set[int],
word_ids: list[int | None],
chunk_words: list[str],
chunk_start: int,
token_count: int,
) -> None:
"""Keep the words the tokenizer's token cut took away.
Chunks are cut by words (``chunk_words``) but tokenized with
``truncation=True, max_length=512``. Words past token 512 get no
``word_id``, so they are never scored and never make ``kept_ids`` -- they
are dropped silently, regardless of importance, and the loss is
positional: whatever sits at the end of the chunk disappears (#4309).
The cut can also land inside a word. Its visible prefix counts as
covered, so the model scores part of a word and may drop the whole of
it, taking the unseen suffix with it. The last covered word is kept as
well whenever the chunk reached the token limit.
"""
covered = max((wid for wid in word_ids if wid is not None), default=-1) + 1
first_dropped = covered
if token_count >= _KOMPRESS_MAX_TOKENS and covered > 0:
first_dropped = covered - 1
for local_idx in range(first_dropped, len(chunk_words)):
kept_ids.add(local_idx + chunk_start)
def _real_token_count(attention_row: Any) -> int:
"""Number of real (non-padding) tokens in an attention-mask row."""
return sum(1 for mask in attention_row if mask)
_TABULAR_ROW_SPLIT_RE = re.compile(r"\s{2,}")
@@ -2059,6 +2094,7 @@ class KompressCompressor(Transform):
input_ids = encoding["input_ids"]
attention_mask = encoding["attention_mask"]
word_ids = encoding.word_ids(batch_index=0)
token_count = _real_token_count(attention_mask[0])
# One row, so no padding: every token in it is real.
scored = _words_seen_whole(word_ids, len(word_ids), len(chunk_words))
if scored == 0:
@@ -2157,6 +2193,11 @@ class KompressCompressor(Transform):
continue
if bool(mask_list[idx]):
kept_ids.add(wid + chunk_start)
# The words past the tokenizer's token cut were never
# scored, so the model cannot have kept them. A forced
# ratio is a hard budget the caller asked for, so only the
# model-decides path takes them back (#4309).
_add_truncated_words(kept_ids, word_ids, chunk_words, chunk_start, token_count)
# Hard override: always keep must-keep tokens regardless of model score.
# Numbers, error names, paths, and flags carry meaning agents cannot
@@ -2591,6 +2632,17 @@ class KompressCompressor(Transform):
for wid, score in word_scores.items():
if score > self.config.score_threshold:
kept_ids_per_text[text_idx].add(wid + chunk_start)
# The words past the tokenizer's token cut were never
# scored, so the model cannot have kept them. A forced
# ratio is a hard budget the caller asked for, so only
# the model-decides path takes them back (#4309).
_add_truncated_words(
kept_ids_per_text[text_idx],
word_ids,
chunk_words,
chunk_start,
_real_token_count(attention_mask[batch_idx]),
)
_add_kompress_must_keep_words(
kept_ids_per_text[text_idx], chunk_words, chunk_start
+13 -2
View File
@@ -29,7 +29,12 @@ class ContentSection:
atomic: bool = False
_CODE_FENCE_PATTERN = re.compile(r"^```(\w*)\s*$", re.MULTILINE)
# Group 1 is the whole info string: any text without a backtick, so
# ``c++``, ``c#``, ``objective-c``, ``js {1,3}`` and `` c++`` (space first) all
# open a fence. ``\w*`` missed such openers and then took the closing fence
# for one. A single quantifier keeps a long line linear; the language is the
# info string's first word (see ``_fence_language``).
_CODE_FENCE_PATTERN = re.compile(r"^```([^\n`]*)$", re.MULTILINE)
# ``[^\S\n]*``, not ``\s*``: under MULTILINE, ``^\s*`` rescans every
# following blank line from each line start (quadratic in a blank run).
_JSON_BLOCK_START = re.compile(r"^[^\S\n]*[\[{]", re.MULTILINE)
@@ -37,6 +42,12 @@ _SEARCH_RESULT_PATTERN = re.compile(r"^\S+:\d+:", re.MULTILINE)
_PROSE_PATTERN = re.compile(r"[A-Z][a-z]+\s+\w+\s+\w+")
def _fence_language(info_string: str) -> str:
"""First word of a fence info string, or ``"unknown"`` for a bare fence."""
words = info_string.split()
return words[0] if words else "unknown"
def _is_search_line(line: str) -> bool:
"""True for grep match lines and ``-A``/``-B``/``-C`` context lines.
@@ -150,7 +161,7 @@ def split_into_sections(content: str, *, isolate: tuple[str, ...] = ()) -> list[
continue
if match := _CODE_FENCE_PATTERN.match(line):
language = match.group(1) or "unknown"
language = _fence_language(match.group(1))
code_lines = []
start_line = i
i += 1
+11 -1
View File
@@ -77,8 +77,18 @@ def parse_csv(content: str, delimiter: str = ",") -> tuple[list[str], list[list[
parsed = [row for row in reader if row and not (len(row) == 1 and not row[0].strip())]
except csv.Error:
return [], []
if not parsed:
# Trim all-blank rows at both ends, but keep interior ones so the row
# count survives, like spreadsheet_ingest._rows_to_csv. One pass and one
# slice: popping index 0 per leading blank row is quadratic. An interior
# all-blank row is normalized to empty cells, so a whitespace-only row and
# an empty row compress the same.
filled = [i for i, row in enumerate(parsed) if any(cell.strip() for cell in row)]
if not filled:
return [], []
parsed = [
row if any(cell.strip() for cell in row) else [""] * len(row)
for row in parsed[filled[0] : filled[-1] + 1]
]
headers = [h.strip() for h in parsed[0]]
return headers, parsed[1:]
+4 -1
View File
@@ -162,7 +162,10 @@ def compact_thinking_to_text(
if not (isinstance(block, dict) and block.get("type") == "thinking"):
new_content.append(block)
continue
text = block.get("thinking", "")
text = block.get("thinking")
if not isinstance(text, str): # null/non-str: leave the block as it is
new_content.append(block)
continue
words = len(text.split())
if words < min_words:
new_content.append(block)
+7 -5
View File
@@ -158,11 +158,9 @@ def _select_latest(data: dict[str, Any], *, allow_pre: bool) -> str | None:
candidates: list[Version] = []
if isinstance(releases, dict):
for ver_str, files in releases.items():
# Skip releases whose every artifact is yanked.
if (
isinstance(files, list)
and files
and all(isinstance(f, dict) and f.get("yanked") for f in files)
# Skip releases with no artifacts, or whose every artifact is yanked.
if isinstance(files, list) and (
not files or all(isinstance(f, dict) and f.get("yanked") for f in files)
):
continue
try:
@@ -174,6 +172,10 @@ def _select_latest(data: dict[str, Any], *, allow_pre: bool) -> str | None:
candidates.append(ver)
if candidates:
return str(max(candidates))
if isinstance(releases, dict) and releases:
# Releases are listed but none is installable (all yanked or file-less):
# do not fall back to info.version, which names one of them.
return None
# Fallback to info.version when releases is absent/empty.
info = data.get("info")
+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",
+3
View File
@@ -23,6 +23,9 @@ This plugin can auto-start a local `headroom proxy` when needed. OpenClaw treats
## Local Development Install (Detection-Friendly)
Building from source requires Node.js `^22.18.0 || ^24.11.0 || >=26.0.0` for the tsup 9 toolchain. Use a supported Node release before running `npm ci` and `npm run build`. This is a source-build requirement; the published plugin uses the Node runtime supported by your OpenClaw host and does not run tsup during plugin loading.
If you are testing from this repo, run npm install/build from the plugin directory so local launcher detection aligns with runtime paths. These linked installs are supported:
```bash
+325 -293
View File
File diff suppressed because it is too large Load Diff
+3 -3
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",
@@ -31,12 +31,12 @@
"devDependencies": {
"@typescript/native": "npm:typescript@^7.0.2",
"@types/node": "^26.1.1",
"tsup": "^8.0.0",
"tsup": "^9.0.0",
"typescript": "^6.0.3",
"vitest": "^5.0.0"
},
"overrides": {
"esbuild": "^0.28.1"
"esbuild": "^0.28.2"
},
"openclaw": {
"hooks": [
@@ -32,8 +32,16 @@ export function createHeadroomRetrieveTool(config: RetrieveToolConfig) {
},
required: ["hash"],
},
execute: async (args: { hash: string }): Promise<string> => {
const { hash } = args;
// OpenClaw calls a registered tool as execute(toolCallId, params, signal?,
// onUpdate?). The hash used to be read from the first argument, so the
// tool-call id was validated as a hash and every real call failed before
// any request was sent (#4638). A direct execute({ hash }) call still works.
execute: async (
toolCallIdOrArgs: string | { hash: string },
params?: { hash: string },
): Promise<string> => {
const args = typeof toolCallIdOrArgs === "string" ? params : toolCallIdOrArgs;
const hash = typeof args?.hash === "string" ? args.hash : "";
// Validate hash format
if (!/^[a-f0-9]{24}$/i.test(hash)) {
@@ -0,0 +1,63 @@
import { describe, it, expect, afterEach, vi } from "vitest";
import { createHeadroomRetrieveTool } from "../src/tools/headroom-retrieve.js";
const HASH = "a".repeat(24);
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
function stubFetch() {
const mock = vi.fn().mockResolvedValue({
ok: true,
status: 200,
json: () => Promise.resolve({ original_content: "offline fixture" }),
text: () => Promise.resolve(""),
});
vi.stubGlobal("fetch", mock);
return mock;
}
describe("headroom_retrieve execute arguments (#4638)", () => {
it("reads the hash from params when called the way OpenClaw calls tools", async () => {
const fetchMock = stubFetch();
const tool = createHeadroomRetrieveTool({ proxyUrl: "http://127.0.0.1:8080" });
const result = await tool.execute("call_fixture", { hash: HASH });
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(String(fetchMock.mock.calls[0][0])).toBe(`http://127.0.0.1:8080/v1/retrieve/${HASH}`);
expect(JSON.parse(result)).toEqual({ original_content: "offline fixture" });
});
it("still accepts a direct execute({ hash }) call", async () => {
const fetchMock = stubFetch();
const tool = createHeadroomRetrieveTool({ proxyUrl: "http://127.0.0.1:8080" });
const result = await tool.execute({ hash: HASH });
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(JSON.parse(result)).toEqual({ original_content: "offline fixture" });
});
it("rejects a bad hash in params without sending a request", async () => {
const fetchMock = stubFetch();
const tool = createHeadroomRetrieveTool({ proxyUrl: "http://127.0.0.1:8080" });
const result = await tool.execute("call_fixture", { hash: "not-a-hash" });
expect(fetchMock).not.toHaveBeenCalled();
expect(result).toContain("Invalid hash format");
});
it("rejects a missing params object without sending a request", async () => {
const fetchMock = stubFetch();
const tool = createHeadroomRetrieveTool({ proxyUrl: "http://127.0.0.1:8080" });
const result = await tool.execute("call_fixture");
expect(fetchMock).not.toHaveBeenCalled();
expect(result).toContain("Invalid hash format");
});
});
+4 -1
View File
@@ -27,6 +27,9 @@ npm run build
npm run build:standalone
```
For a reproducible real OpenCode 2.x → proxy → local SSE upstream check, see
[the transport runtime proof](RUNTIME_PROOF.md). It records the tested scope and limitations.
## Provider Config Helpers
Use these helpers when you need to generate OpenCode config that routes a `headroom` provider through a running Headroom proxy.
@@ -73,7 +76,7 @@ The default export is `{ id: "headroom", server, setup }`. OpenCode 1.x calls `s
On 2.x, setup returns a cleanup that releases the transport, so hot reloads do not stack interceptors.
OpenCode 2.x keeps its own reference to `fetch` from before plugins load, so patching `fetch` alone does not catch its model traffic. On 2.x the plugin also rewrites each remote model that uses the OpenAI chat-completions or responses format so that it points at the proxy. Headers name the real upstream, as the transport does. Models on other formats (Anthropic messages, Google) are left alone. `headroom wrap opencode` routes native Anthropic and OpenAI through the proxy separately, via their `baseURL`.
OpenCode 2.x keeps its own reference to `fetch` from before plugins load, so patching `fetch` alone does not catch its model traffic. For models with a resolved `baseURL`, the plugin also rewrites OpenAI chat-completions, Responses, and Anthropic Messages traffic so that it points at the proxy — including GitHub Copilot's Claude models. Anthropic Messages base URLs with a path prefix (for example a gateway rooted at `/api/...`) stay on their own base URL, because the proxy's Messages route ignores the original-path header and cannot preserve that prefix. Headers name the real upstream and request path, as the transport does, and authentication settings are retained. Local, excluded, and unsupported-protocol models are left alone. `headroom wrap opencode` routes native Anthropic and OpenAI through the proxy separately, via their `baseURL`.
To keep a provider off the proxy, list its host. An entry matches the host and all of its subdomains, and `.example.com` / `*.example.com` are accepted as spellings of `example.com`:
+120
View File
@@ -0,0 +1,120 @@
# OpenCode 2.x Anthropic Messages transport proof
This records the actual OpenCode runtime boundary requested in
[PR #4594's review](https://github.com/headroomlabs-ai/headroom/pull/4594#pullrequestreview-5745390972).
The OpenCode process, its Anthropic adapter, built plugin, and Headroom proxy are real.
Only the upstream API is a controlled local fixture. No live Copilot credentials are used.
## Environment
Recorded on **2026-11-04 at 04:23 UTC**, against Headroom source
`8766328a6c20909520b0a7ac7b5d487900316084`:
- Linux 6.18.57, x86_64; Python 3.12.14; Node 24.22.1; npm 11.9.0.
- Official OpenCode **2.2.5**, from `@opencode/cli-linux-x64-baseline@2.2.5`.
- `headroom-opencode` 0.42.0; Headroom 0.42.1-dev from this checkout.
- Built `dist-standalone/index.js`, byte-identical to the checked-in wheel bundle.
SHA-256: `de88ab2120df79ad67fad3f7a06f8618ea093707cfcf1180921b1ae42e7b1cea`.
- Real provider adapter: `@opencode/ai/providers/anthropic`, model `claude-sonnet-4-6`.
This is a configured Copilot-shaped provider, not OpenCode's live Copilot discovery flow.
- **Python-only degraded proxy mode**, explicitly observed in startup logs because
this environment has no `headroom._core`. `HEADROOM_REQUIRE_RUST_CORE=false`
permits that mode; `--no-optimize --no-cache` limits the experiment to transport.
## Exact commands / steps
From the repository root, prepare the source proxy and build the plugin:
```sh
uv sync --frozen --extra proxy --no-install-project
npm --prefix plugins/opencode ci
npm --prefix plugins/opencode test
npm --prefix plugins/opencode run typecheck
npm --prefix plugins/opencode run build
npm --prefix plugins/opencode run build:standalone
cmp plugins/opencode/dist-standalone/index.js headroom/providers/opencode/_dist/index.js
```
Use an official OpenCode 2.x executable. For the Linux x86_64 run above, the binary
was installed without executing a package install script:
```sh
npm install --prefix /tmp/opencode-runtime --omit=optional --ignore-scripts \
--no-save --no-audit --no-fund @opencode/cli-linux-x64-baseline@2.2.5
OPENCODE_BIN=/tmp/opencode-runtime/node_modules/@opencode/cli-linux-x64-baseline/bin/opencode
.venv/bin/python plugins/opencode/scripts/runtime-proof.py \
--opencode "$OPENCODE_BIN" --output /tmp/headroom-opencode-proof
```
The installation/output directories are selectable; `--output` must be new.
The script requires POSIX process groups and the `127.0.0.2` loopback alias.
It starts a new real proxy and a strict-path SSE fixture for each scenario, then runs:
```sh
opencode run --standalone --print-logs --log-level debug --format json \
'Reply with HEADROOM_RUNTIME_OK.'
```
Each run uses isolated HOME/XDG directories, a minimal environment, disabled model
metadata fetching, and synthetic bearer/API-key values. This isolates default
credential discovery, not filesystem access. The upstream binds only to `127.0.0.2`,
which exercises model rewriting without the plugin's `localhost`/`127.0.0.1`
exclusion. That address is explicitly allowlisted in the test proxy. Ports are
allocated dynamically. OpenCode loads the built standalone **directory**, whose
`index.js` is its plugin entrypoint. The script does not call `headroomSetup` itself,
replace the OpenCode context, or send the model request with a manual `fetch`.
## Observed result
All four scenarios exited successfully:
| Scenario | Plugin | Proxy POSTs | Upstream POSTs | Upstream path |
| --- | --- | --- | --- | --- |
| Baseline | disabled | 0 | 2 | `/messages` |
| Copilot-shaped root URL | enabled | 2 | 2 | `/v1/messages` |
| `/v1` gateway | enabled | 2 | 2 | `/v1/messages` |
| Prefixed gateway | enabled | 0 | 2 | `/api/coding/paas/v4/messages` |
OpenCode made a title request and a user-turn request in each case. Every upstream
request retained the synthetic `Authorization` and `x-api-key`, the expected model,
`stream: true`, and `anthropic-version: 2023-06-01`. None contained `x-headroom-*`.
Each user turn received `HEADROOM_RUNTIME_OK` as an OpenCode text event, followed by
`step_finish`. The fixture flushes individual SSE events rather than returning a
JSON completion.
The proxied root-URL run logged these events (irrelevant fields omitted):
```text
04:23:10.316Z msg="loading plugin" entrypoint=file:///<checkout>/plugins/opencode/dist-standalone/index.js
04:23:10.975 event=proxy_inbound_request method=POST path=/v1/messages
user-agent=opencode/latest/2.2.5/cli
x-headroom-base-url=http://127.0.0.2:41341
x-headroom-original-path=/messages x-headroom-project=copilot-shaped
04:23:10.979 event=outbound_headers forwarder=anthropic_messages stripped_count=3
04:23:10.998 event=outbound_request forwarder=streaming method=POST
path=http://127.0.0.2:41341/v1/messages body_mutated=false source=passthrough
```
The script verifies proxy and upstream counts, exact upstream paths, preserved
synthetic auth, stripped internal headers, plugin loading, and a completed streamed
client reply. A fixture request to an unexpected endpoint receives 404. The root
URL case deliberately records the existing proxy contract of forwarding to
`/v1/messages`; it does not establish which endpoints a live Copilot account accepts.
The prefix exclusion is exercised by the real Anthropic adapter with the full
prefix intact and no model POST reaching Headroom.
Machine-readable results are in [runtime-proof-result.json](scripts/runtime-proof-result.json).
A rerun saves generated config, proxy/OpenCode logs, upstream observations and its
summary under the selected output directory. Plugin tests also passed **64/64**,
as did typecheck, both builds, bundle comparison, and Ruff checks for this script.
## Not tested
- Live `api.githubcopilot.com`, account discovery, subscription authentication,
token refresh, Copilot-specific headers, or actual model inference.
- A live z.ai gateway or its provider-specific adapter. The prefix control uses
the same real Anthropic adapter against a local path fixture.
- Rust-core behavior, compression/optimization, cache behavior, or production rollout.
- HTTPS/TLS, HTTP/2, rate limits, retries, or streaming latency/backpressure.
- OpenCode versions other than 2.2.5. The script rejects non-2.x versions and records
the actual Rust-core startup mode, which may differ on another machine.
+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",
@@ -0,0 +1,57 @@
{
"opencode": "opencode v2.2.5",
"python": "3.12.14",
"started_utc": "2026-11-04T04:23:01.378019+00:00",
"headroom_source": "8766328a6c20909520b0a7ac7b5d487900316084",
"plugin_sha256": "de88ab2120df79ad67fad3f7a06f8618ea093707cfcf1180921b1ae42e7b1cea",
"results": [
{
"case": "baseline",
"rust_core": "disabled",
"plugin": false,
"base_path": "",
"proxy_requests": 0,
"upstream_requests": 2,
"upstream_path": "/messages",
"auth_preserved": true,
"internal_headers_at_upstream": false,
"client_text": "HEADROOM_RUNTIME_OK"
},
{
"case": "copilot-shaped",
"rust_core": "disabled",
"plugin": true,
"base_path": "",
"proxy_requests": 2,
"upstream_requests": 2,
"upstream_path": "/v1/messages",
"auth_preserved": true,
"internal_headers_at_upstream": false,
"client_text": "HEADROOM_RUNTIME_OK"
},
{
"case": "v1-gateway",
"rust_core": "disabled",
"plugin": true,
"base_path": "/v1",
"proxy_requests": 2,
"upstream_requests": 2,
"upstream_path": "/v1/messages",
"auth_preserved": true,
"internal_headers_at_upstream": false,
"client_text": "HEADROOM_RUNTIME_OK"
},
{
"case": "prefixed-gateway",
"rust_core": "disabled",
"plugin": true,
"base_path": "/api/coding/paas/v4",
"proxy_requests": 0,
"upstream_requests": 2,
"upstream_path": "/api/coding/paas/v4/messages",
"auth_preserved": true,
"internal_headers_at_upstream": false,
"client_text": "HEADROOM_RUNTIME_OK"
}
]
}
+441
View File
@@ -0,0 +1,441 @@
#!/usr/bin/env python3
"""Exercise the built plugin with real OpenCode 2.x and Headroom processes.
Only the upstream is a fixture. No provider account or real API key is used.
Run with the repository's proxy dependencies installed in this Python environment.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import re
import shutil
import signal
import socket
import subprocess
import sys
import tempfile
import threading
import time
from datetime import datetime, timezone
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from urllib.request import ProxyHandler, build_opener
REPO = Path(__file__).resolve().parents[3]
PLUGIN = REPO / "plugins/opencode/dist-standalone"
TEXT = "HEADROOM_RUNTIME_OK"
MODEL = "claude-sonnet-4-6"
AUTH = "Bearer proof-token-not-a-secret"
KEY = "test-key-not-a-secret"
def require(condition, message: str):
if not condition:
raise RuntimeError(message)
def stop_process_group(process):
# Both children are started in dedicated sessions; never target the caller's group.
try:
os.killpg(process.pid, signal.SIGTERM)
except ProcessLookupError:
pass
try:
process.wait(timeout=10)
except subprocess.TimeoutExpired:
pass
finally:
try:
os.killpg(process.pid, signal.SIGKILL)
except ProcessLookupError:
pass
process.wait(timeout=10)
def events(model: str):
yield (
"message_start",
{
"type": "message_start",
"message": {
"id": "msg_proof",
"type": "message",
"role": "assistant",
"model": model,
"content": [],
"stop_reason": None,
"stop_sequence": None,
"usage": {"input_tokens": 12, "output_tokens": 0},
},
},
)
yield (
"content_block_start",
{
"type": "content_block_start",
"index": 0,
"content_block": {"type": "text", "text": ""},
},
)
yield (
"content_block_delta",
{
"type": "content_block_delta",
"index": 0,
"delta": {"type": "text_delta", "text": TEXT},
},
)
yield "content_block_stop", {"type": "content_block_stop", "index": 0}
yield (
"message_delta",
{
"type": "message_delta",
"delta": {"stop_reason": "end_turn", "stop_sequence": None},
"usage": {"output_tokens": 4},
},
)
yield "message_stop", {"type": "message_stop"}
def run_case(opencode: str, output: Path, name: str, base_path: str, enabled: bool):
case = output / name
case.mkdir()
records: list[dict] = []
expected_path = (
"/v1/messages" if enabled and base_path in ("", "/v1") else base_path + "/messages"
)
expected_proxy = enabled and base_path in ("", "/v1")
class Upstream(BaseHTTPRequestHandler):
def log_message(self, *_):
pass
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
headers = {key.lower(): value for key, value in self.headers.items()}
records.append(
{
"path": self.path,
"headers": headers,
"model": body.get("model"),
"stream": body.get("stream"),
}
)
# Do not let an unexpected endpoint pass just because this is a fixture.
if self.path != expected_path:
self.send_error(404, "Unexpected Messages path")
return
self.send_response(200)
self.send_header("Content-Type", "text/event-stream")
self.send_header("Connection", "close")
self.end_headers()
for event, data in events(body["model"]):
self.wfile.write(f"event: {event}\ndata: {json.dumps(data)}\n\n".encode())
self.wfile.flush()
time.sleep(0.01)
# 127.0.0.2 is local but differs from the plugin's deliberately excluded
# localhost/127.0.0.1 destinations, exercising remote-model routing safely.
upstream = ThreadingHTTPServer(("127.0.0.2", 0), Upstream)
upstream.daemon_threads = True
upstream_thread = threading.Thread(target=upstream.serve_forever, daemon=True)
upstream_thread.start()
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
proxy_port = sock.getsockname()[1]
proxy_url = f"http://127.0.0.1:{proxy_port}"
upstream_url = f"http://127.0.0.2:{upstream.server_port}"
# Use isolated HOME/XDG directories and a minimal environment instead of
# inheriting provider credentials/config. This is not a filesystem sandbox.
env = {
"PATH": os.environ.get("PATH", ""),
"HOME": str(case / "proxy-home"),
"TMPDIR": str(case / "tmp"),
"TZ": "UTC",
"NO_PROXY": "*",
"no_proxy": "*",
"PYTHONPATH": str(REPO),
"HEADROOM_BEACON": "off",
"HEADROOM_TELEMETRY": "off",
"HEADROOM_UPDATE_CHECK": "off",
"HEADROOM_REQUIRE_RUST_CORE": "false",
"HEADROOM_ALLOWED_BASE_URLS": "127.0.0.2",
"LITELLM_LOCAL_MODEL_COST_MAP": "true",
}
for directory in (
"proxy-home",
"tmp",
"work",
"oc-home",
"oc-data",
"oc-config",
"oc-cache",
"oc-state",
):
(case / directory).mkdir()
cli_env = {
"PATH": env["PATH"],
"HOME": str(case / "oc-home"),
"TMPDIR": env["TMPDIR"],
"TZ": "UTC",
"NO_PROXY": "*",
"OPENCODE_DISABLE_MODELS_FETCH": "1",
}
for variable, directory in (
("DATA", "oc-data"),
("CONFIG", "oc-config"),
("CACHE", "oc-cache"),
("STATE", "oc-state"),
):
cli_env[f"XDG_{variable}_HOME"] = str(case / directory)
config = {
"update": "disable",
"share": "disabled",
"snapshots": False,
"model": f"proof-copilot/{MODEL}",
"providers": {
"proof-copilot": {
"name": "Controlled Copilot-shaped stand-in",
"package": "@opencode/ai/providers/anthropic",
"settings": {"baseURL": upstream_url + base_path, "apiKey": KEY},
"headers": {"Authorization": AUTH},
"models": {
MODEL: {
"name": "Claude proof",
"limit": {"context": 200000, "output": 512},
"capabilities": {"tools": False, "input": ["text"], "output": ["text"]},
}
},
}
},
# OpenCode 2.x local package entries point at a directory, not a file.
"plugins": [
{
"package": str(PLUGIN),
"options": {
"proxyUrl": proxy_url,
"project": name,
"debug": True,
},
}
]
if enabled
else [],
}
(case / "work/opencode.json").write_text(json.dumps(config, indent=2) + "\n")
proxy = None
try:
with (case / "proxy.log").open("w") as proxy_log:
proxy = subprocess.Popen(
[
sys.executable,
"-m",
"headroom.cli",
"proxy",
"--host",
"127.0.0.1",
"--port",
str(proxy_port),
"--no-optimize",
"--no-cache",
],
cwd=REPO,
env=env,
stdout=proxy_log,
stderr=subprocess.STDOUT,
start_new_session=True,
)
opener = build_opener(ProxyHandler({}))
for _ in range(60):
if proxy.poll() is not None:
raise RuntimeError(f"Proxy exited; see {case / 'proxy.log'}")
try:
with opener.open(proxy_url + "/livez", timeout=1) as response:
if response.status == 200:
break
except OSError:
pass
time.sleep(1)
else:
raise RuntimeError("Proxy did not become live")
with (case / "opencode.log").open("w") as cli_log:
cli = subprocess.Popen(
[
opencode,
"run",
"--standalone",
"--print-logs",
"--log-level",
"debug",
"--format",
"json",
f"Reply with {TEXT}.",
],
cwd=case / "work",
env=cli_env,
stdout=cli_log,
stderr=subprocess.STDOUT,
start_new_session=True,
)
try:
returncode = cli.wait(timeout=90)
finally:
stop_process_group(cli)
if returncode:
raise RuntimeError(f"OpenCode exited {returncode}; see {case / 'opencode.log'}")
finally:
if proxy is not None:
stop_process_group(proxy)
upstream.shutdown()
upstream.server_close()
upstream_thread.join()
(case / "upstream.json").write_text(json.dumps(records, indent=2) + "\n")
cli_log = (case / "opencode.log").read_text()
proxy_log = (case / "proxy.log").read_text()
client_events = [
json.loads(line) for line in cli_log.splitlines() if line.startswith('{"type":')
]
text = "".join(event["part"]["text"] for event in client_events if event.get("type") == "text")
require(text == TEXT, f"SSE text did not reach the client: {text!r}")
require(
any(event.get("type") == "step_finish" for event in client_events),
"No completed client turn",
)
require(records, "No upstream request observed")
require(
all(record["path"] == expected_path for record in records),
"Failed check: all((record['path'] == expected_path for record in records))",
)
for record in records:
headers = record["headers"]
require(headers.get("authorization") == AUTH, "Bearer auth changed")
require(headers.get("x-api-key") == KEY, "API key changed")
require(
headers.get("anthropic-version") == "2023-06-01",
"Failed check: headers.get('anthropic-version') == '2023-06-01'",
)
require(
"opencode/" in headers.get("user-agent", ""),
"Failed check: 'opencode/' in headers.get('user-agent', '')",
)
require(
not any(key.startswith("x-headroom-") for key in headers), "Internal headers leaked"
)
require(
record["model"] == MODEL and record["stream"] is True,
"Failed check: record['model'] == MODEL and record['stream'] is True",
)
inbound = [
line
for line in proxy_log.splitlines()
if "event=proxy_inbound_request" in line and "method=POST" in line
]
require(bool(inbound) == expected_proxy, "Unexpected proxy routing")
if expected_proxy:
require(len(inbound) == len(records), "Proxy/upstream request counts differ")
require(
all(
"path=/v1/messages" in line and '"x-headroom-base-url"' in line for line in inbound
),
"Failed check: all(('path=/v1/messages' in line and '\"x-headroom-base-url\"' in line for line in inbound))",
)
require(
f"path={upstream_url}/v1/messages" in proxy_log,
"Failed check: f'path={upstream_url}/v1/messages' in proxy_log",
)
require("stripped_count=3" in proxy_log, "Failed check: 'stripped_count=3' in proxy_log")
if enabled:
require(
'msg="loading plugin"' in cli_log and str(PLUGIN) in cli_log,
"Built plugin was not loaded",
)
core_mode = "disabled" if "event=rust_core_disabled" in proxy_log else "loaded"
require("event=rust_core_" + core_mode in proxy_log, "Rust core mode was not observed")
summary = {
"case": name,
"rust_core": core_mode,
"plugin": enabled,
"base_path": base_path,
"proxy_requests": len(inbound),
"upstream_requests": len(records),
"upstream_path": expected_path,
"auth_preserved": True,
"internal_headers_at_upstream": False,
"client_text": text,
}
print(json.dumps(summary), flush=True)
return summary
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--opencode", default=shutil.which("opencode"), help="OpenCode 2.x executable"
)
parser.add_argument(
"--output", type=Path, help="New directory for logs (default: temporary directory)"
)
args = parser.parse_args()
require(
os.name == "posix", "This local proof requires POSIX process groups and loopback aliases"
)
if not args.opencode or not (PLUGIN / "index.js").is_file():
parser.error("Install OpenCode 2.x and run npm run build:standalone first")
opencode = str(Path(args.opencode).resolve())
output = (
args.output.resolve()
if args.output
else Path(tempfile.mkdtemp(prefix="headroom-opencode-"))
)
if args.output:
output.mkdir(parents=True, exist_ok=False)
version_env = {
"PATH": os.environ.get("PATH", ""),
"HOME": str(output),
"XDG_DATA_HOME": str(output / "version-data"),
"XDG_CONFIG_HOME": str(output / "version-config"),
"XDG_CACHE_HOME": str(output / "version-cache"),
"XDG_STATE_HOME": str(output / "version-state"),
}
version = subprocess.check_output(
[opencode, "--version"], env=version_env, text=True, timeout=15
).strip()
require(re.search(r"(?:^|\s)v?2\.", version), "This proof requires OpenCode 2.x")
require(
(PLUGIN / "index.js").read_bytes()
== (REPO / "headroom/providers/opencode/_dist/index.js").read_bytes(),
"Rebuilt plugin differs from the checked-in wheel bundle",
)
metadata = {
"opencode": version,
"python": sys.version.split()[0],
"started_utc": datetime.now(timezone.utc).isoformat(),
"headroom_source": subprocess.check_output(
["git", "rev-parse", "HEAD"], cwd=REPO, text=True
).strip(),
"plugin_sha256": hashlib.sha256((PLUGIN / "index.js").read_bytes()).hexdigest(),
}
print(f"OpenCode: {version}\nEvidence: {output}", flush=True)
results = [
run_case(opencode, output, *case)
for case in (
("baseline", "", False),
("copilot-shaped", "", True),
("v1-gateway", "/v1", True),
("prefixed-gateway", "/api/coding/paas/v4", True),
)
]
(output / "summary.json").write_text(
json.dumps({**metadata, "results": results}, indent=2) + "\n"
)
print("PASS: all four real OpenCode / proxy transport scenarios", flush=True)
if __name__ == "__main__":
main()
+55 -13
View File
@@ -242,6 +242,60 @@ describe("headroomSetup (OpenCode 2.x)", () => {
});
});
it.each([
["@opencode/ai/providers/anthropic", "https://api.githubcopilot.com", "/messages"],
["@opencode/ai/providers/anthropic-compatible", "https://gateway.example/v1", "/v1/messages"],
])("routes Anthropic Messages models through the proxy (%s)", async (packageName, baseURL, originalPath) => {
// GitHub Copilot serves Claude models as @opencode/ai/providers/anthropic
// with a prefix-free Copilot baseURL; the OpenAI-only wire check used to
// skip them.
const models: FakeModel[] = [{ id: "claude-sonnet-5.5", providerID: "github-copilot" }];
const { context } = pluginContextV2(
{ proxyUrl: "http://127.0.0.1:8787" },
{
providers: [{ id: "github-copilot", package: packageName, settings: { baseURL } }],
models,
},
);
const cleanup = await headroomSetup(context);
await cleanup?.();
expect(models[0]).toMatchObject({
settings: { baseURL: "http://127.0.0.1:8787/v1" },
headers: {
"x-headroom-base-url": new URL(baseURL).origin,
"x-headroom-original-path": originalPath,
"x-headroom-project": "project-1",
},
});
});
it("leaves a prefixed Anthropic Messages gateway on its own base URL", async () => {
// The proxy's Anthropic route rebuilds the upstream path from its incoming
// /v1/messages and ignores x-headroom-original-path, so rewriting a
// prefixed gateway would drop that prefix. Keep those models direct.
const models: FakeModel[] = [{ id: "glm-5.2", providerID: "zai-coding-plan" }];
const { context } = pluginContextV2(
{ proxyUrl: "http://127.0.0.1:8787" },
{
providers: [
{
id: "zai-coding-plan",
package: "@opencode/ai/providers/zai-coding-plan/messages",
settings: { baseURL: "https://api.z.ai/api/coding/paas/v4" },
},
],
models,
},
);
const cleanup = await headroomSetup(context);
await cleanup?.();
expect(models[0]).toEqual({ id: "glm-5.2", providerID: "zai-coding-plan" });
});
it("prefers a model's own baseURL over its provider's", async () => {
const models: FakeModel[] = [
{
@@ -404,12 +458,10 @@ describe("headroomSetup (OpenCode 2.x)", () => {
});
});
it("leaves local, already-proxied, non-OpenAI-wire and URL-less models alone", async () => {
it("leaves local, already-proxied, URL-less and unsupported-protocol models alone", async () => {
const models: FakeModel[] = [
{ id: "llama", providerID: "ollama" },
{ id: "gpt-4o", providerID: "openai" },
{ id: "claude", providerID: "anthropic-gateway" },
{ id: "glm", providerID: "zai-coding-plan", package: "@opencode/ai/providers/zai-coding-plan/messages" },
{ id: "sonnet", providerID: "anthropic" },
{ id: "gemini", providerID: "google" },
];
@@ -428,16 +480,6 @@ describe("headroomSetup (OpenCode 2.x)", () => {
package: "@opencode/ai/providers/openai/responses",
settings: { baseURL: "http://127.0.0.1:8787/v1" },
},
{
id: "anthropic-gateway",
package: "@opencode/ai/providers/anthropic",
settings: { baseURL: "https://gateway.example/v1" },
},
{
id: "zai-coding-plan",
package: "@opencode/ai/providers/zai-coding-plan/chat",
settings: { baseURL: "https://api.z.ai/api/coding/paas/v4" },
},
{ id: "anthropic", package: "@opencode/ai/providers/anthropic" },
{
id: "google",
+30 -6
View File
@@ -70,22 +70,45 @@ function headroomShellEnv(
};
}
// The endpoint an OpenCode 2.x package calls under its baseURL, for packages
// on the OpenAI chat-completions or responses wire format; undefined otherwise.
// `.../messages` (Anthropic wire) and other protocols are left alone.
function openAiWireSuffix(pkg: string | undefined): string | undefined {
// The endpoint an OpenCode 2.x package calls under its baseURL, for packages on
// a wire format the proxy can route; undefined otherwise. GitHub Copilot's
// Claude models use the Anthropic Messages wire (`/messages`), so they must be
// rewritten like the OpenAI wires: skipping them let those requests bypass the
// proxy entirely (#4248).
function modelWireSuffix(pkg: string | undefined): string | undefined {
if (!pkg?.startsWith("@opencode/ai/providers/")) return undefined;
if (pkg === "@opencode/ai/providers/openai-compatible" || pkg.endsWith("/chat")) {
return "/chat/completions";
}
if (pkg.endsWith("-responses") || pkg.endsWith("/responses")) return "/responses";
if (
pkg === "@opencode/ai/providers/anthropic"
|| pkg === "@opencode/ai/providers/anthropic-compatible"
|| pkg.endsWith("/messages")
) {
return "/messages";
}
return undefined;
}
// The proxy's Anthropic route rebuilds the upstream path from its incoming
// `/v1/messages` and ignores `x-headroom-original-path`, so a rewritten model
// reaches the right endpoint only when its base URL has no path prefix beyond
// `/v1`. A prefixed gateway (for example `https://api.z.ai/api/coding/paas/v4`)
// would lose that prefix, so those models stay on their own base URL.
function anthropicBaseKeepsPath(baseURL: string): boolean {
try {
const pathname = trimTrailingSlashes(new URL(baseURL).pathname);
return pathname === "" || pathname === "/v1";
} catch {
return false;
}
}
// OpenCode 2.x sends model traffic through Effect's FetchHttpClient, which
// captures `globalThis.fetch` on the process's first HTTP request, before any
// plugin runs, so the transport's fetch patch never sees it. Point each
// remote OpenAI-wire model at the proxy instead, with the same routing headers
// remote model on a routable wire format at the proxy instead, with the same
// the transport sends: the upstream origin, and the real path the proxy
// appends to it.
type ModelEditor = Parameters<Parameters<PluginV2.Context["model"]["transform"]>[0]>[0];
@@ -102,10 +125,11 @@ function routeModelsThroughProxy(
const providerID = String(model.providerID);
const modelID = String(model.id);
const provider = models.provider.get(providerID)?.provider;
const suffix = openAiWireSuffix(model.package ?? provider?.package);
const suffix = modelWireSuffix(model.package ?? provider?.package);
const baseURL = model.settings?.baseURL ?? provider?.settings?.baseURL;
if (!suffix || typeof baseURL !== "string") continue;
if (!modelBaseRoutesThroughProxy(baseURL, proxyUrl, excludeHosts)) continue;
if (suffix === "/messages" && !anthropicBaseKeepsPath(baseURL)) continue;
const upstream = new URL(baseURL);
models.update(providerID, modelID, (draft) => {
draft.settings = { ...draft.settings, baseURL: `${proxyUrl}/v1` };
+3 -3
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).
@@ -76,7 +76,7 @@ dependencies = [
# future patched one.
"ast-grep-cli>=0.30.0,!=0.44.1",
"pyyaml>=6.0", # omp wrap: parse/merge omp's models.yml registry
"tomlkit>=0.15.0,<1.0", # Loss-minimizing Codex config recovery
"tomlkit>=0.15.2,<1.0", # Loss-minimizing Codex config recovery
# Verify upstream TLS through the OS trust store (macOS Keychain, Windows
# cert store), where IT installs corporate TLS-inspection roots (Zscaler,
# Netskope). Pure Python, no deps; the same library pip uses by default.
@@ -328,7 +328,7 @@ dev = [
"pytest>=7.0.0",
"pytest-cov>=4.0.0",
"pytest-asyncio>=0.21.0",
"ruff==0.17.2",
"ruff==0.17.3",
"mypy>=1.0.0",
"pre-commit>=3.0.0",
"openai>=1.0.0",
+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": [
{
+31
View File
@@ -19,6 +19,37 @@ import pytest
from tests._skip_helpers import external_model_skip_reason
@pytest.fixture
def gemini_request():
"""A real ASGI request whose JSON arrives in two stream messages."""
from starlette.requests import Request
body = json.dumps({"contents": [{"role": "user", "parts": [{"text": "hello"}]}]}).encode()
middle = len(body) // 2
messages = iter(
[
{"type": "http.request", "body": body[:middle], "more_body": True},
{"type": "http.request", "body": body[middle:], "more_body": False},
]
)
async def receive():
return next(messages, {"type": "http.disconnect"})
return Request(
{
"type": "http",
"method": "POST",
"path": "/v1beta/models/gemini-pro:generateContent",
"query_string": b"",
"headers": [(b"content-type", b"application/json")],
"scheme": "https",
"server": ("gemini.example", 443),
},
receive,
)
@pytest.fixture(autouse=True)
def _undo_process_trust_injection():
"""Undo ``truststore.inject_into_ssl()`` after any test that triggered it.
@@ -13,7 +13,7 @@
"output": {
"additions": 40,
"cache_key": "4ced183e23d56ca575d6ff00",
"compressed": "diff --git a/file_0.py b/file_0.py\n--- a/file_0.py\n+++ b/file_0.py\n@@ -1,10 +1,12 @@\n context_3_0\n context_4_0\n-removed_0_0\n-removed_1_0\n-removed_2_0\n+added_0_0\n+added_1_0\n+added_2_0\n+added_3_0\n+added_4_0\n tail_0_0\n tail_1_0\ndiff --git a/file_1.py b/file_1.py\n--- a/file_1.py\n+++ b/file_1.py\n@@ -1,10 +1,12 @@\n context_3_1\n context_4_1\n-removed_0_1\n-removed_1_1\n-removed_2_1\n+added_0_1\n+added_1_1\n+added_2_1\n+added_3_1\n+added_4_1\n tail_0_1\n tail_1_1\ndiff --git a/file_2.py b/file_2.py\n--- a/file_2.py\n+++ b/file_2.py\n@@ -1,10 +1,12 @@\n context_3_2\n context_4_2\n-removed_0_2\n-removed_1_2\n-removed_2_2\n+added_0_2\n+added_1_2\n+added_2_2\n+added_3_2\n+added_4_2\n tail_0_2\n tail_1_2\ndiff --git a/file_3.py b/file_3.py\n--- a/file_3.py\n+++ b/file_3.py\n@@ -1,10 +1,12 @@\n context_3_3\n context_4_3\n-removed_0_3\n-removed_1_3\n-removed_2_3\n+added_0_3\n+added_1_3\n+added_2_3\n+added_3_3\n+added_4_3\n tail_0_3\n tail_1_3\ndiff --git a/file_4.py b/file_4.py\n--- a/file_4.py\n+++ b/file_4.py\n@@ -1,10 +1,12 @@\n context_3_4\n context_4_4\n-removed_0_4\n-removed_1_4\n-removed_2_4\n+added_0_4\n+added_1_4\n+added_2_4\n+added_3_4\n+added_4_4\n tail_0_4\n tail_1_4\ndiff --git a/file_5.py b/file_5.py\n--- a/file_5.py\n+++ b/file_5.py\n@@ -1,10 +1,12 @@\n context_3_5\n context_4_5\n-removed_0_5\n-removed_1_5\n-removed_2_5\n+added_0_5\n+added_1_5\n+added_2_5\n+added_3_5\n+added_4_5\n tail_0_5\n tail_1_5\ndiff --git a/file_6.py b/file_6.py\n--- a/file_6.py\n+++ b/file_6.py\n@@ -1,10 +1,12 @@\n context_3_6\n context_4_6\n-removed_0_6\n-removed_1_6\n-removed_2_6\n+added_0_6\n+added_1_6\n+added_2_6\n+added_3_6\n+added_4_6\n tail_0_6\n tail_1_6\ndiff --git a/file_7.py b/file_7.py\n--- a/file_7.py\n+++ b/file_7.py\n@@ -1,10 +1,12 @@\n context_3_7\n context_4_7\n-removed_0_7\n-removed_1_7\n-removed_2_7\n+added_0_7\n+added_1_7\n+added_2_7\n+added_3_7\n+added_4_7\n tail_0_7\n tail_1_7\n[8 files changed, +40 -24 lines]\n[177 lines compressed to 129. Retrieve full diff: hash=4ced183e23d56ca575d6ff00]",
"compressed": "diff --git a/file_0.py b/file_0.py\n--- a/file_0.py\n+++ b/file_0.py\n@@ -4,7 +4,9 @@\n context_3_0\n context_4_0\n-removed_0_0\n-removed_1_0\n-removed_2_0\n+added_0_0\n+added_1_0\n+added_2_0\n+added_3_0\n+added_4_0\n tail_0_0\n tail_1_0\ndiff --git a/file_1.py b/file_1.py\n--- a/file_1.py\n+++ b/file_1.py\n@@ -4,7 +4,9 @@\n context_3_1\n context_4_1\n-removed_0_1\n-removed_1_1\n-removed_2_1\n+added_0_1\n+added_1_1\n+added_2_1\n+added_3_1\n+added_4_1\n tail_0_1\n tail_1_1\ndiff --git a/file_2.py b/file_2.py\n--- a/file_2.py\n+++ b/file_2.py\n@@ -4,7 +4,9 @@\n context_3_2\n context_4_2\n-removed_0_2\n-removed_1_2\n-removed_2_2\n+added_0_2\n+added_1_2\n+added_2_2\n+added_3_2\n+added_4_2\n tail_0_2\n tail_1_2\ndiff --git a/file_3.py b/file_3.py\n--- a/file_3.py\n+++ b/file_3.py\n@@ -4,7 +4,9 @@\n context_3_3\n context_4_3\n-removed_0_3\n-removed_1_3\n-removed_2_3\n+added_0_3\n+added_1_3\n+added_2_3\n+added_3_3\n+added_4_3\n tail_0_3\n tail_1_3\ndiff --git a/file_4.py b/file_4.py\n--- a/file_4.py\n+++ b/file_4.py\n@@ -4,7 +4,9 @@\n context_3_4\n context_4_4\n-removed_0_4\n-removed_1_4\n-removed_2_4\n+added_0_4\n+added_1_4\n+added_2_4\n+added_3_4\n+added_4_4\n tail_0_4\n tail_1_4\ndiff --git a/file_5.py b/file_5.py\n--- a/file_5.py\n+++ b/file_5.py\n@@ -4,7 +4,9 @@\n context_3_5\n context_4_5\n-removed_0_5\n-removed_1_5\n-removed_2_5\n+added_0_5\n+added_1_5\n+added_2_5\n+added_3_5\n+added_4_5\n tail_0_5\n tail_1_5\ndiff --git a/file_6.py b/file_6.py\n--- a/file_6.py\n+++ b/file_6.py\n@@ -4,7 +4,9 @@\n context_3_6\n context_4_6\n-removed_0_6\n-removed_1_6\n-removed_2_6\n+added_0_6\n+added_1_6\n+added_2_6\n+added_3_6\n+added_4_6\n tail_0_6\n tail_1_6\ndiff --git a/file_7.py b/file_7.py\n--- a/file_7.py\n+++ b/file_7.py\n@@ -4,7 +4,9 @@\n context_3_7\n context_4_7\n-removed_0_7\n-removed_1_7\n-removed_2_7\n+added_0_7\n+added_1_7\n+added_2_7\n+added_3_7\n+added_4_7\n tail_0_7\n tail_1_7\n[8 files changed, +40 -24 lines]\n[177 lines compressed to 129. Retrieve full diff: hash=4ced183e23d56ca575d6ff00]",
"compressed_line_count": 129,
"deletions": 24,
"files_affected": 8,
@@ -13,7 +13,7 @@
"output": {
"additions": 40,
"cache_key": "11ee99638b134ba374157005",
"compressed": "diff --git a/file_0.py b/file_0.py\n--- a/file_0.py\n+++ b/file_0.py\n@@ -1,10 +1,12 @@\n context_3_0\n context_4_0\n-removed_0_0\n-removed_1_0\n-removed_2_0\n+added_0_0\n+added_1_0\n+added_2_0\n+added_3_0\n+added_4_0\n tail_0_0\n tail_1_0\ndiff --git a/file_1.py b/file_1.py\n--- a/file_1.py\n+++ b/file_1.py\n@@ -1,10 +1,12 @@\n context_3_1\n context_4_1\n-removed_0_1\n-removed_1_1\n-removed_2_1\n+added_0_1\n+added_1_1\n+added_2_1\n+added_3_1\n+added_4_1\n tail_0_1\n tail_1_1\ndiff --git a/file_2.py b/file_2.py\n--- a/file_2.py\n+++ b/file_2.py\n@@ -1,10 +1,12 @@\n context_3_2\n context_4_2\n-removed_0_2\n-removed_1_2\n-removed_2_2\n+added_0_2\n+added_1_2\n+added_2_2\n+added_3_2\n+added_4_2\n tail_0_2\n tail_1_2\ndiff --git a/file_3.py b/file_3.py\n--- a/file_3.py\n+++ b/file_3.py\n@@ -1,10 +1,12 @@\n context_3_3\n context_4_3\n-removed_0_3\n-removed_1_3\n-removed_2_3\n+added_0_3\n+added_1_3\n+added_2_3\n+added_3_3\n+added_4_3\n tail_0_3\n tail_1_3\ndiff --git a/file_4.py b/file_4.py\n--- a/file_4.py\n+++ b/file_4.py\n@@ -1,10 +1,12 @@\n context_3_4\n context_4_4\n-removed_0_4\n-removed_1_4\n-removed_2_4\n+added_0_4\n+added_1_4\n+added_2_4\n+added_3_4\n+added_4_4\n tail_0_4\n tail_1_4\ndiff --git a/file_5.py b/file_5.py\n--- a/file_5.py\n+++ b/file_5.py\n@@ -1,10 +1,12 @@\n context_3_5\n context_4_5\n-removed_0_5\n-removed_1_5\n-removed_2_5\n+added_0_5\n+added_1_5\n+added_2_5\n+added_3_5\n+added_4_5\n tail_0_5\n tail_1_5\ndiff --git a/file_6.py b/file_6.py\n--- a/file_6.py\n+++ b/file_6.py\n@@ -1,10 +1,12 @@\n context_3_6\n context_4_6\n-removed_0_6\n-removed_1_6\n-removed_2_6\n+added_0_6\n+added_1_6\n+added_2_6\n+added_3_6\n+added_4_6\n tail_0_6\n tail_1_6\ndiff --git a/file_7.py b/file_7.py\n--- a/file_7.py\n+++ b/file_7.py\n@@ -1,10 +1,12 @@\n context_3_7\n context_4_7\n-removed_0_7\n-removed_1_7\n-removed_2_7\n+added_0_7\n+added_1_7\n+added_2_7\n+added_3_7\n+added_4_7\n tail_0_7\n tail_1_7\n[8 files changed, +40 -24 lines]\n[177 lines compressed to 129. Retrieve full diff: hash=11ee99638b134ba374157005]",
"compressed": "diff --git a/file_0.py b/file_0.py\n--- a/file_0.py\n+++ b/file_0.py\n@@ -4,7 +4,9 @@\n context_3_0\n context_4_0\n-removed_0_0\n-removed_1_0\n-removed_2_0\n+added_0_0\n+added_1_0\n+added_2_0\n+added_3_0\n+added_4_0\n tail_0_0\n tail_1_0\ndiff --git a/file_1.py b/file_1.py\n--- a/file_1.py\n+++ b/file_1.py\n@@ -4,7 +4,9 @@\n context_3_1\n context_4_1\n-removed_0_1\n-removed_1_1\n-removed_2_1\n+added_0_1\n+added_1_1\n+added_2_1\n+added_3_1\n+added_4_1\n tail_0_1\n tail_1_1\ndiff --git a/file_2.py b/file_2.py\n--- a/file_2.py\n+++ b/file_2.py\n@@ -4,7 +4,9 @@\n context_3_2\n context_4_2\n-removed_0_2\n-removed_1_2\n-removed_2_2\n+added_0_2\n+added_1_2\n+added_2_2\n+added_3_2\n+added_4_2\n tail_0_2\n tail_1_2\ndiff --git a/file_3.py b/file_3.py\n--- a/file_3.py\n+++ b/file_3.py\n@@ -4,7 +4,9 @@\n context_3_3\n context_4_3\n-removed_0_3\n-removed_1_3\n-removed_2_3\n+added_0_3\n+added_1_3\n+added_2_3\n+added_3_3\n+added_4_3\n tail_0_3\n tail_1_3\ndiff --git a/file_4.py b/file_4.py\n--- a/file_4.py\n+++ b/file_4.py\n@@ -4,7 +4,9 @@\n context_3_4\n context_4_4\n-removed_0_4\n-removed_1_4\n-removed_2_4\n+added_0_4\n+added_1_4\n+added_2_4\n+added_3_4\n+added_4_4\n tail_0_4\n tail_1_4\ndiff --git a/file_5.py b/file_5.py\n--- a/file_5.py\n+++ b/file_5.py\n@@ -4,7 +4,9 @@\n context_3_5\n context_4_5\n-removed_0_5\n-removed_1_5\n-removed_2_5\n+added_0_5\n+added_1_5\n+added_2_5\n+added_3_5\n+added_4_5\n tail_0_5\n tail_1_5\ndiff --git a/file_6.py b/file_6.py\n--- a/file_6.py\n+++ b/file_6.py\n@@ -4,7 +4,9 @@\n context_3_6\n context_4_6\n-removed_0_6\n-removed_1_6\n-removed_2_6\n+added_0_6\n+added_1_6\n+added_2_6\n+added_3_6\n+added_4_6\n tail_0_6\n tail_1_6\ndiff --git a/file_7.py b/file_7.py\n--- a/file_7.py\n+++ b/file_7.py\n@@ -4,7 +4,9 @@\n context_3_7\n context_4_7\n-removed_0_7\n-removed_1_7\n-removed_2_7\n+added_0_7\n+added_1_7\n+added_2_7\n+added_3_7\n+added_4_7\n tail_0_7\n tail_1_7\n[8 files changed, +40 -24 lines]\n[177 lines compressed to 129. Retrieve full diff: hash=11ee99638b134ba374157005]",
"compressed_line_count": 129,
"deletions": 24,
"files_affected": 8,
@@ -13,7 +13,7 @@
"output": {
"additions": 1,
"cache_key": null,
"compressed": "commit 0123456789abcdef\nAuthor: Tester <t@example.com>\nDate: Mon Apr 25 12:00:00 2026\n\n msg line 0\n msg line 1\n msg line 2\n msg line 3\n msg line 4\n msg line 5\n msg line 6\n msg line 7\n msg line 8\n msg line 9\n msg line 10\n msg line 11\n msg line 12\n msg line 13\n msg line 14\n msg line 15\n msg line 16\n msg line 17\n msg line 18\n msg line 19\n msg line 20\n msg line 21\n msg line 22\n msg line 23\n msg line 24\n msg line 25\n msg line 26\n msg line 27\n msg line 28\n msg line 29\n msg line 30\n msg line 31\n msg line 32\n msg line 33\n msg line 34\n msg line 35\n msg line 36\n msg line 37\n msg line 38\n msg line 39\n msg line 40\n msg line 41\n msg line 42\n msg line 43\n msg line 44\n msg line 45\n msg line 46\n msg line 47\n msg line 48\n msg line 49\n msg line 50\n msg line 51\n msg line 52\n msg line 53\n msg line 54\n msg line 55\n msg line 56\n msg line 57\n msg line 58\n msg line 59\n\ndiff --git a/auth/old_handler.py b/auth/new_handler.py\nsimilarity index 92%\nrename from auth/old_handler.py\nrename to auth/new_handler.py\n--- a/auth/old_handler.py\n+++ b/auth/new_handler.py\n@@ -1,8 +1,8 @@\n import os\n import sys\n-from auth import legacy\n+from auth import modern\n\n def authenticate(user):\n[1 files changed, +1 -1 lines]",
"compressed": "commit 0123456789abcdef\nAuthor: Tester <t@example.com>\nDate: Mon Apr 25 12:00:00 2026\n\n msg line 0\n msg line 1\n msg line 2\n msg line 3\n msg line 4\n msg line 5\n msg line 6\n msg line 7\n msg line 8\n msg line 9\n msg line 10\n msg line 11\n msg line 12\n msg line 13\n msg line 14\n msg line 15\n msg line 16\n msg line 17\n msg line 18\n msg line 19\n msg line 20\n msg line 21\n msg line 22\n msg line 23\n msg line 24\n msg line 25\n msg line 26\n msg line 27\n msg line 28\n msg line 29\n msg line 30\n msg line 31\n msg line 32\n msg line 33\n msg line 34\n msg line 35\n msg line 36\n msg line 37\n msg line 38\n msg line 39\n msg line 40\n msg line 41\n msg line 42\n msg line 43\n msg line 44\n msg line 45\n msg line 46\n msg line 47\n msg line 48\n msg line 49\n msg line 50\n msg line 51\n msg line 52\n msg line 53\n msg line 54\n msg line 55\n msg line 56\n msg line 57\n msg line 58\n msg line 59\n\ndiff --git a/auth/old_handler.py b/auth/new_handler.py\nsimilarity index 92%\nrename from auth/old_handler.py\nrename to auth/new_handler.py\n--- a/auth/old_handler.py\n+++ b/auth/new_handler.py\n@@ -1,5 +1,5 @@\n import os\n import sys\n-from auth import legacy\n+from auth import modern\n\n def authenticate(user):\n[1 files changed, +1 -1 lines]",
"compressed_line_count": 79,
"deletions": 1,
"files_affected": 1,
@@ -13,7 +13,7 @@
"output": {
"additions": 40,
"cache_key": "706080f4af877cb632f87e1d",
"compressed": "diff --git a/file_0.py b/file_0.py\n--- a/file_0.py\n+++ b/file_0.py\n@@ -1,10 +1,12 @@\n context_3_0\n context_4_0\n-removed_0_0\n-removed_1_0\n-removed_2_0\n+added_0_0\n+added_1_0\n+added_2_0\n+added_3_0\n+added_4_0\n tail_0_0\n tail_1_0\ndiff --git a/file_1.py b/file_1.py\n--- a/file_1.py\n+++ b/file_1.py\n@@ -1,10 +1,12 @@\n context_3_1\n context_4_1\n-removed_0_1\n-removed_1_1\n-removed_2_1\n+added_0_1\n+added_1_1\n+added_2_1\n+added_3_1\n+added_4_1\n tail_0_1\n tail_1_1\ndiff --git a/file_2.py b/file_2.py\n--- a/file_2.py\n+++ b/file_2.py\n@@ -1,10 +1,12 @@\n context_3_2\n context_4_2\n-removed_0_2\n-removed_1_2\n-removed_2_2\n+added_0_2\n+added_1_2\n+added_2_2\n+added_3_2\n+added_4_2\n tail_0_2\n tail_1_2\ndiff --git a/file_3.py b/file_3.py\n--- a/file_3.py\n+++ b/file_3.py\n@@ -1,10 +1,12 @@\n context_3_3\n context_4_3\n-removed_0_3\n-removed_1_3\n-removed_2_3\n+added_0_3\n+added_1_3\n+added_2_3\n+added_3_3\n+added_4_3\n tail_0_3\n tail_1_3\ndiff --git a/file_4.py b/file_4.py\n--- a/file_4.py\n+++ b/file_4.py\n@@ -1,10 +1,12 @@\n context_3_4\n context_4_4\n-removed_0_4\n-removed_1_4\n-removed_2_4\n+added_0_4\n+added_1_4\n+added_2_4\n+added_3_4\n+added_4_4\n tail_0_4\n tail_1_4\ndiff --git a/file_5.py b/file_5.py\n--- a/file_5.py\n+++ b/file_5.py\n@@ -1,10 +1,12 @@\n context_3_5\n context_4_5\n-removed_0_5\n-removed_1_5\n-removed_2_5\n+added_0_5\n+added_1_5\n+added_2_5\n+added_3_5\n+added_4_5\n tail_0_5\n tail_1_5\ndiff --git a/file_6.py b/file_6.py\n--- a/file_6.py\n+++ b/file_6.py\n@@ -1,10 +1,12 @@\n context_3_6\n context_4_6\n-removed_0_6\n-removed_1_6\n-removed_2_6\n+added_0_6\n+added_1_6\n+added_2_6\n+added_3_6\n+added_4_6\n tail_0_6\n tail_1_6\ndiff --git a/file_7.py b/file_7.py\n--- a/file_7.py\n+++ b/file_7.py\n@@ -1,10 +1,12 @@\n context_3_7\n context_4_7\n-removed_0_7\n-removed_1_7\n-removed_2_7\n+added_0_7\n+added_1_7\n+added_2_7\n+added_3_7\n+added_4_7\n tail_0_7\n tail_1_7\n[8 files changed, +40 -24 lines]\n[177 lines compressed to 129. Retrieve full diff: hash=706080f4af877cb632f87e1d]",
"compressed": "diff --git a/file_0.py b/file_0.py\n--- a/file_0.py\n+++ b/file_0.py\n@@ -4,7 +4,9 @@\n context_3_0\n context_4_0\n-removed_0_0\n-removed_1_0\n-removed_2_0\n+added_0_0\n+added_1_0\n+added_2_0\n+added_3_0\n+added_4_0\n tail_0_0\n tail_1_0\ndiff --git a/file_1.py b/file_1.py\n--- a/file_1.py\n+++ b/file_1.py\n@@ -4,7 +4,9 @@\n context_3_1\n context_4_1\n-removed_0_1\n-removed_1_1\n-removed_2_1\n+added_0_1\n+added_1_1\n+added_2_1\n+added_3_1\n+added_4_1\n tail_0_1\n tail_1_1\ndiff --git a/file_2.py b/file_2.py\n--- a/file_2.py\n+++ b/file_2.py\n@@ -4,7 +4,9 @@\n context_3_2\n context_4_2\n-removed_0_2\n-removed_1_2\n-removed_2_2\n+added_0_2\n+added_1_2\n+added_2_2\n+added_3_2\n+added_4_2\n tail_0_2\n tail_1_2\ndiff --git a/file_3.py b/file_3.py\n--- a/file_3.py\n+++ b/file_3.py\n@@ -4,7 +4,9 @@\n context_3_3\n context_4_3\n-removed_0_3\n-removed_1_3\n-removed_2_3\n+added_0_3\n+added_1_3\n+added_2_3\n+added_3_3\n+added_4_3\n tail_0_3\n tail_1_3\ndiff --git a/file_4.py b/file_4.py\n--- a/file_4.py\n+++ b/file_4.py\n@@ -4,7 +4,9 @@\n context_3_4\n context_4_4\n-removed_0_4\n-removed_1_4\n-removed_2_4\n+added_0_4\n+added_1_4\n+added_2_4\n+added_3_4\n+added_4_4\n tail_0_4\n tail_1_4\ndiff --git a/file_5.py b/file_5.py\n--- a/file_5.py\n+++ b/file_5.py\n@@ -4,7 +4,9 @@\n context_3_5\n context_4_5\n-removed_0_5\n-removed_1_5\n-removed_2_5\n+added_0_5\n+added_1_5\n+added_2_5\n+added_3_5\n+added_4_5\n tail_0_5\n tail_1_5\ndiff --git a/file_6.py b/file_6.py\n--- a/file_6.py\n+++ b/file_6.py\n@@ -4,7 +4,9 @@\n context_3_6\n context_4_6\n-removed_0_6\n-removed_1_6\n-removed_2_6\n+added_0_6\n+added_1_6\n+added_2_6\n+added_3_6\n+added_4_6\n tail_0_6\n tail_1_6\ndiff --git a/file_7.py b/file_7.py\n--- a/file_7.py\n+++ b/file_7.py\n@@ -4,7 +4,9 @@\n context_3_7\n context_4_7\n-removed_0_7\n-removed_1_7\n-removed_2_7\n+added_0_7\n+added_1_7\n+added_2_7\n+added_3_7\n+added_4_7\n tail_0_7\n tail_1_7\n[8 files changed, +40 -24 lines]\n[177 lines compressed to 129. Retrieve full diff: hash=706080f4af877cb632f87e1d]",
"compressed_line_count": 129,
"deletions": 24,
"files_affected": 8,
@@ -13,7 +13,7 @@
"output": {
"additions": 40,
"cache_key": "416e7f5d3ae0f718cc2609eb",
"compressed": "diff --git a/file_0.py b/file_0.py\n--- a/file_0.py\n+++ b/file_0.py\n@@ -1,10 +1,12 @@\n context_3_0\n context_4_0\n-removed_0_0\n-removed_1_0\n-removed_2_0\n+added_0_0\n+added_1_0\n+added_2_0\n+added_3_0\n+added_4_0\n tail_0_0\n tail_1_0\ndiff --git a/file_1.py b/file_1.py\n--- a/file_1.py\n+++ b/file_1.py\n@@ -1,10 +1,12 @@\n context_3_1\n context_4_1\n-removed_0_1\n-removed_1_1\n-removed_2_1\n+added_0_1\n+added_1_1\n+added_2_1\n+added_3_1\n+added_4_1\n tail_0_1\n tail_1_1\ndiff --git a/file_2.py b/file_2.py\n--- a/file_2.py\n+++ b/file_2.py\n@@ -1,10 +1,12 @@\n context_3_2\n context_4_2\n-removed_0_2\n-removed_1_2\n-removed_2_2\n+added_0_2\n+added_1_2\n+added_2_2\n+added_3_2\n+added_4_2\n tail_0_2\n tail_1_2\ndiff --git a/file_3.py b/file_3.py\n--- a/file_3.py\n+++ b/file_3.py\n@@ -1,10 +1,12 @@\n context_3_3\n context_4_3\n-removed_0_3\n-removed_1_3\n-removed_2_3\n+added_0_3\n+added_1_3\n+added_2_3\n+added_3_3\n+added_4_3\n tail_0_3\n tail_1_3\ndiff --git a/file_4.py b/file_4.py\n--- a/file_4.py\n+++ b/file_4.py\n@@ -1,10 +1,12 @@\n context_3_4\n context_4_4\n-removed_0_4\n-removed_1_4\n-removed_2_4\n+added_0_4\n+added_1_4\n+added_2_4\n+added_3_4\n+added_4_4\n tail_0_4\n tail_1_4\ndiff --git a/file_5.py b/file_5.py\n--- a/file_5.py\n+++ b/file_5.py\n@@ -1,10 +1,12 @@\n context_3_5\n context_4_5\n-removed_0_5\n-removed_1_5\n-removed_2_5\n+added_0_5\n+added_1_5\n+added_2_5\n+added_3_5\n+added_4_5\n tail_0_5\n tail_1_5\ndiff --git a/file_6.py b/file_6.py\n--- a/file_6.py\n+++ b/file_6.py\n@@ -1,10 +1,12 @@\n context_3_6\n context_4_6\n-removed_0_6\n-removed_1_6\n-removed_2_6\n+added_0_6\n+added_1_6\n+added_2_6\n+added_3_6\n+added_4_6\n tail_0_6\n tail_1_6\ndiff --git a/file_7.py b/file_7.py\n--- a/file_7.py\n+++ b/file_7.py\n@@ -1,10 +1,12 @@\n context_3_7\n context_4_7\n-removed_0_7\n-removed_1_7\n-removed_2_7\n+added_0_7\n+added_1_7\n+added_2_7\n+added_3_7\n+added_4_7\n tail_0_7\n tail_1_7\n[8 files changed, +40 -24 lines]\n[177 lines compressed to 129. Retrieve full diff: hash=416e7f5d3ae0f718cc2609eb]",
"compressed": "diff --git a/file_0.py b/file_0.py\n--- a/file_0.py\n+++ b/file_0.py\n@@ -4,7 +4,9 @@\n context_3_0\n context_4_0\n-removed_0_0\n-removed_1_0\n-removed_2_0\n+added_0_0\n+added_1_0\n+added_2_0\n+added_3_0\n+added_4_0\n tail_0_0\n tail_1_0\ndiff --git a/file_1.py b/file_1.py\n--- a/file_1.py\n+++ b/file_1.py\n@@ -4,7 +4,9 @@\n context_3_1\n context_4_1\n-removed_0_1\n-removed_1_1\n-removed_2_1\n+added_0_1\n+added_1_1\n+added_2_1\n+added_3_1\n+added_4_1\n tail_0_1\n tail_1_1\ndiff --git a/file_2.py b/file_2.py\n--- a/file_2.py\n+++ b/file_2.py\n@@ -4,7 +4,9 @@\n context_3_2\n context_4_2\n-removed_0_2\n-removed_1_2\n-removed_2_2\n+added_0_2\n+added_1_2\n+added_2_2\n+added_3_2\n+added_4_2\n tail_0_2\n tail_1_2\ndiff --git a/file_3.py b/file_3.py\n--- a/file_3.py\n+++ b/file_3.py\n@@ -4,7 +4,9 @@\n context_3_3\n context_4_3\n-removed_0_3\n-removed_1_3\n-removed_2_3\n+added_0_3\n+added_1_3\n+added_2_3\n+added_3_3\n+added_4_3\n tail_0_3\n tail_1_3\ndiff --git a/file_4.py b/file_4.py\n--- a/file_4.py\n+++ b/file_4.py\n@@ -4,7 +4,9 @@\n context_3_4\n context_4_4\n-removed_0_4\n-removed_1_4\n-removed_2_4\n+added_0_4\n+added_1_4\n+added_2_4\n+added_3_4\n+added_4_4\n tail_0_4\n tail_1_4\ndiff --git a/file_5.py b/file_5.py\n--- a/file_5.py\n+++ b/file_5.py\n@@ -4,7 +4,9 @@\n context_3_5\n context_4_5\n-removed_0_5\n-removed_1_5\n-removed_2_5\n+added_0_5\n+added_1_5\n+added_2_5\n+added_3_5\n+added_4_5\n tail_0_5\n tail_1_5\ndiff --git a/file_6.py b/file_6.py\n--- a/file_6.py\n+++ b/file_6.py\n@@ -4,7 +4,9 @@\n context_3_6\n context_4_6\n-removed_0_6\n-removed_1_6\n-removed_2_6\n+added_0_6\n+added_1_6\n+added_2_6\n+added_3_6\n+added_4_6\n tail_0_6\n tail_1_6\ndiff --git a/file_7.py b/file_7.py\n--- a/file_7.py\n+++ b/file_7.py\n@@ -4,7 +4,9 @@\n context_3_7\n context_4_7\n-removed_0_7\n-removed_1_7\n-removed_2_7\n+added_0_7\n+added_1_7\n+added_2_7\n+added_3_7\n+added_4_7\n tail_0_7\n tail_1_7\n[8 files changed, +40 -24 lines]\n[177 lines compressed to 129. Retrieve full diff: hash=416e7f5d3ae0f718cc2609eb]",
"compressed_line_count": 129,
"deletions": 24,
"files_affected": 8,
@@ -25,18 +25,23 @@ import asyncio
from fastapi import Request
from headroom.proxy.handlers.anthropic import AnthropicHandlerMixin
from headroom.proxy.models import ProxyConfig
class _MinimalAnthropicHandler(AnthropicHandlerMixin):
"""The smallest handler that can reach the ClientDisconnect guard.
The guard sits before any of the heavier machinery (compression,
memory, CCR, upstream dispatch) executes, so none of that needs
stubbing here — only ``_next_request_id``, which the real
implementation backs with an ``asyncio.Lock`` + counter this
minimal handler never initializes.
Header policy and optional limit checks may precede body reading. Provide
their normal configuration while leaving rate/budget limiting disabled;
the disconnect still comes from the real ASGI receive channel, before
compression, memory, CCR, or upstream dispatch.
"""
def __init__(self) -> None:
self.config = ProxyConfig()
self.rate_limiter = None
self.cost_tracker = None
async def _next_request_id(self) -> str:
return "req-test"
+10
View File
@@ -90,6 +90,16 @@ def test_bearer_case_insensitive_preserves_oauth_and_jwt_shapes() -> None:
assert classify_auth_signals(AuthSignals(authorization=f"bearer {jwt}")) is AuthMode.OAUTH
def test_extra_spaces_between_scheme_and_token_are_ignored() -> None:
# Same answer as the Rust classifier (headroom_core::auth_mode::classify).
assert classify_auth_signals(AuthSignals(authorization="Bearer sk-ant-oat01-abc")) is (
AuthMode.OAUTH
)
assert classify_auth_signals(AuthSignals(authorization="bearer sk-ant-api03-abc")) is (
AuthMode.PAYG
)
def test_credential_case_is_not_folded() -> None:
# Only the scheme is case-folded; the token keeps its case, so a PAYG
# `sk-...` prefix still matches exactly (it is lowercase by construction).
+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
+37
View File
@@ -53,6 +53,43 @@ class TestSemanticCache:
entry = cache.get("Unknown query", messages_hash="unknown")
assert entry is None
@pytest.mark.parametrize("max_entries", [0, -1])
def test_put_rejects_nonpositive_capacity(self, max_entries, monkeypatch):
cache = SemanticCache(SemanticCacheConfig(max_entries=max_entries))
# Fail promptly instead of hanging if the unguarded eviction loop returns.
monkeypatch.setattr(
cache, "_evict_oldest", lambda: pytest.fail("Invalid capacity must not evict entries")
)
with pytest.raises(ValueError, match="max_entries must be positive"):
cache.put("query", "response", messages_hash="request")
assert cache.get_stats()["entries"] == 0
@pytest.mark.parametrize("max_entries", [0, -1])
@pytest.mark.parametrize("messages_hash", ["existing", "new"])
def test_invalid_capacity_change_preserves_entries(
self, max_entries, messages_hash, monkeypatch
):
cache = SemanticCache(SemanticCacheConfig(max_entries=1))
cache.put("query", "original", messages_hash="existing")
cache.config.max_entries = max_entries
stats = cache.get_stats()
monkeypatch.setattr(
cache, "_evict_oldest", lambda: pytest.fail("Invalid capacity must not evict entries")
)
with pytest.raises(ValueError, match="max_entries must be positive"):
cache.put("query", "replacement", messages_hash=messages_hash)
assert cache.get_stats() == stats
assert cache.get("query", messages_hash="existing").response == "original"
assert cache.get("query", messages_hash="new") is None
cache.config.max_entries = 1
cache.put("query", "updated", messages_hash="existing")
assert cache.get("query", messages_hash="existing").response == "updated"
def test_same_query_different_context_does_not_collide(self, cache):
"""Two requests that share a trailing user message but differ in earlier
context (distinct messages_hash) must not overwrite each other. Before the
@@ -90,11 +90,13 @@ def ccr_marker() -> str:
def _body(*, with_thinking: bool, marker: str | None = None) -> dict:
first = "hi" if marker is None else f"hi — earlier output is at <<ccr:{marker}>>"
messages: list[dict] = [{"role": "user", "content": first}]
# The marker rides in the latest user turn: only content new this turn
# gates buffering (#3587).
latest = "hi" if marker is None else f"hi — earlier output is at <<ccr:{marker}>>"
messages: list[dict] = [{"role": "user", "content": "hi" if with_thinking else latest}]
if with_thinking:
messages.append(SIGNED_THINKING_TURN)
messages.append({"role": "user", "content": "continue"})
messages.append({"role": "user", "content": latest})
return {
"model": "claude-sonnet-4-20250514",
"max_tokens": 64,
@@ -287,6 +289,116 @@ def test_buffering_is_gated_on_a_redeemable_marker(
assert calls["stream_body"]["stream"] is True
def _owned_marker(content: str) -> str:
"""Store one more original in the (fixture-reset) store; return its hash."""
from headroom.cache.compression_store import get_compression_store
return get_compression_store().store(
content,
"<<ccr:placeholder>>",
original_tokens=100,
compressed_tokens=5,
tool_name="Read",
)
def _tool_turn(tool_use_id: str, output: str) -> list[dict]:
return [
{
"role": "assistant",
"content": [{"type": "tool_use", "id": tool_use_id, "name": "Read", "input": {}}],
},
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": tool_use_id, "content": output}],
},
]
def _agent_body(messages: list[dict]) -> dict:
return {
"model": "claude-sonnet-4-20250514",
"max_tokens": 64,
"stream": True,
"tools": [RETRIEVE_TOOL],
"messages": messages,
}
def _replay_session(turns: list[list[dict]]) -> list[str]:
"""Send each message list as one turn of the same session; report the path taken."""
paths: list[str] = []
async def fake_stream_response(url, headers, body, *args, **kwargs): # noqa: ANN001
paths.append("streamed")
return fastapi.responses.StreamingResponse(iter([SSE_BODY]), media_type="text/event-stream")
async def fake_retry(method, url, headers, req_body, *args, **kwargs): # noqa: ANN001
paths.append("buffered")
return httpx.Response(
200,
json={
"id": "msg_1",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-4-20250514",
"content": [{"type": "text", "text": "ok"}],
"stop_reason": "end_turn",
"usage": {"input_tokens": 10, "output_tokens": 5},
},
headers={"content-type": "application/json"},
)
app = create_app(_config())
with TestClient(app) as client:
client.app.state.proxy._stream_response = fake_stream_response
client.app.state.proxy._retry_request = fake_retry
for messages in turns:
resp = client.post("/v1/messages", json=_agent_body(messages), headers=_headers())
assert resp.status_code == 200, resp.text
return paths
def test_a_marker_already_forwarded_does_not_buffer_later_turns(ccr_marker: str) -> None:
"""A marker carried forward in history does not make every later turn buffer.
The marker was in front of the model on the turn that introduced it, and
that turn buffered. Buffering every later turn for it is #3587: the
buffered share climbed toward 90% of turns with zero retrievals.
"""
first = [
{"role": "user", "content": "read the logs"},
*_tool_turn("tu_1", f"earlier output at <<ccr:{ccr_marker}>>"),
]
second = [*first, *_tool_turn("tu_2", "short")]
assert _replay_session([first, second]) == ["buffered", "streamed"]
def test_a_marker_first_shown_in_an_earlier_message_still_buffers(ccr_marker: str) -> None:
"""New to the model is what counts, not where the marker sits.
Token mode can compress an earlier tool result on a later request, so the
model first sees that marker in the middle of the history. Retrieval has
to be handled server-side there too, or the call reaches the client.
"""
newly_compressed = _owned_marker("a different, uncompressed tool output")
first = [
{"role": "user", "content": "read the logs"},
*_tool_turn("tu_1", f"earlier output at <<ccr:{ccr_marker}>>"),
]
second = [
{"role": "user", "content": "read the logs"},
*_tool_turn(
"tu_1",
f"earlier output at <<ccr:{ccr_marker}>> and <<ccr:{newly_compressed}>>",
),
*_tool_turn("tu_2", "short"),
]
assert _replay_session([first, second]) == ["buffered", "buffered"]
def test_cache_hit_never_replays_a_foreign_content_type() -> None:
"""A cache entry cannot hand a caller a wire format it did not ask for."""
body = {
+320
View File
@@ -0,0 +1,320 @@
"""``headroom_retrieve`` on a streamed turn is served by the proxy (#3587).
A turn whose redeemable markers were all forwarded on earlier turns streams
instead of buffering, so the client gets the first token right away. The model
can still ask for one of those originals. The client never declared
``headroom_retrieve`` (a coding agent answers "No such tool available"), so
the call must not reach it: the stream withholds it, the proxy serves it from
the compression store, and the continuation round streams into the same client
message.
"""
from __future__ import annotations
import json
from typing import Any
from unittest.mock import AsyncMock, MagicMock
import httpx
import pytest
fastapi = pytest.importorskip("fastapi")
from fastapi.testclient import TestClient # noqa: E402
from headroom.ccr.tool_injection import create_ccr_tool_definition # noqa: E402
from headroom.proxy.memory_tool_stream import MemoryToolStreamFilter # noqa: E402
from headroom.proxy.server import ProxyConfig, create_app # noqa: E402
RETRIEVE_ONLY = frozenset({"headroom_retrieve"})
ORIGINAL = "line 1 of the original build log\nline 2: the error the model needs"
def _frame(payload: dict[str, Any]) -> bytes:
return f"event: {payload['type']}\ndata: {json.dumps(payload)}\n\n".encode()
def _sse(blocks: list[dict[str, Any]], stop_reason: str) -> bytes:
"""An Anthropic SSE response carrying ``blocks``."""
frames = [
_frame(
{
"type": "message_start",
"message": {
"id": "msg_1",
"type": "message",
"role": "assistant",
"model": "sonnet-test",
"content": [],
"stop_reason": None,
"usage": {"input_tokens": 10, "output_tokens": 1},
},
}
)
]
for index, block in enumerate(blocks):
if block["type"] == "text":
start = {"type": "text", "text": ""}
delta = {"type": "text_delta", "text": block["text"]}
else:
start = {**block, "input": {}}
delta = {"type": "input_json_delta", "partial_json": json.dumps(block["input"])}
frames.append(
_frame({"type": "content_block_start", "index": index, "content_block": start})
)
frames.append(_frame({"type": "content_block_delta", "index": index, "delta": delta}))
frames.append(_frame({"type": "content_block_stop", "index": index}))
frames.append(
_frame(
{
"type": "message_delta",
"delta": {"stop_reason": stop_reason, "stop_sequence": None},
"usage": {"output_tokens": 5},
}
)
)
frames.append(_frame({"type": "message_stop"}))
return b"".join(frames)
def _events(raw: bytes) -> list[dict[str, Any]]:
return [
json.loads(line[len("data: ") :])
for line in raw.decode().splitlines()
if line.startswith("data: ")
]
def _tool_names(events: list[dict[str, Any]]) -> list[str]:
return [
e["content_block"]["name"]
for e in events
if e["type"] == "content_block_start" and e["content_block"].get("type") == "tool_use"
]
TEXT = {"type": "text", "text": "Let me look at the earlier output."}
BASH = {"type": "tool_use", "id": "toolu_bash", "name": "Bash", "input": {"command": "ls"}}
def _retrieve(hash_key: str) -> dict[str, Any]:
return {
"type": "tool_use",
"id": "toolu_ccr",
"name": "headroom_retrieve",
"input": {"hash": hash_key},
}
class TestHoldingFilter:
def test_text_before_the_first_tool_call_streams_live(self) -> None:
flt = MemoryToolStreamFilter(RETRIEVE_ONLY, hold_tool_use=True)
forwarded = _events(b"".join(flt.feed(_sse([TEXT, BASH], "tool_use"))))
assert [e["type"] for e in forwarded] == [
"message_start",
"content_block_start",
"content_block_delta",
"content_block_stop",
]
assert flt.holds_client_tool_use
def test_without_a_retrieval_the_round_is_byte_identical(self) -> None:
raw = _sse([TEXT, BASH], "tool_use")
flt = MemoryToolStreamFilter(RETRIEVE_ONLY, hold_tool_use=True)
out = b"".join(flt.feed(raw)) + b"".join(flt.closing_frames())
assert out == raw
def test_dropping_held_client_calls(self) -> None:
flt = MemoryToolStreamFilter(RETRIEVE_ONLY, hold_tool_use=True)
flt.feed(_sse([BASH, _retrieve("abc")], "tool_use"))
assert flt.hidden_tool_names == ["headroom_retrieve"]
assert flt.release_held(drop_tool_use=True) == []
assert flt.next_index == 0
assert not flt.visible_tool_use
def _sse_response(raw: bytes) -> MagicMock:
response = MagicMock()
response.headers = httpx.Headers({"content-type": "text/event-stream"})
response.status_code = 200
async def aiter_bytes(raw: bytes = raw):
# Split mid-frame so the filter has to reassemble events.
yield raw[:41]
yield raw[41:]
response.aiter_bytes = aiter_bytes
response.aclose = AsyncMock()
return response
@pytest.fixture
def owned_marker():
"""A marker the proxy owns, with its original in the compression store."""
from headroom.cache.backends import InMemoryBackend
from headroom.cache.compression_store import get_compression_store, reset_compression_store
reset_compression_store()
store = get_compression_store(backend=InMemoryBackend())
hash_key = store.store(
ORIGINAL,
"<<ccr:placeholder>>",
original_tokens=100,
compressed_tokens=5,
tool_name="Bash",
)
try:
yield hash_key
finally:
reset_compression_store()
def _two_turn_session(
marker: str, second_turn_rounds: list[bytes]
) -> tuple[list[dict[str, Any]], list[dict[str, Any]], int]:
"""Turn 1 introduces ``marker``; turn 2 only carries it in history.
Returns the client's SSE events for turn 2, the bodies the proxy sent
upstream on turn 2's stream (first round, then continuations), and how
many buffered upstream requests turn 2 made.
"""
first_turn = [
{"role": "user", "content": "build it"},
{
"role": "assistant",
"content": [{"type": "tool_use", "id": "tu_1", "name": "Bash", "input": {}}],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "tu_1",
"content": f"build log compressed at <<ccr:{marker}>>",
}
],
},
]
second_turn = [
*first_turn,
{"role": "assistant", "content": [{"type": "text", "text": "The build failed."}]},
{"role": "user", "content": "what was the exact error?"},
]
buffered_calls: list[int] = []
async def fake_retry(method, url, headers, req_body, *args, **kwargs): # noqa: ANN001
buffered_calls.append(1)
return httpx.Response(
200,
json={
"id": "msg_t1",
"type": "message",
"role": "assistant",
"model": "sonnet-test",
"content": [{"type": "text", "text": "The build failed."}],
"stop_reason": "end_turn",
"usage": {"input_tokens": 10, "output_tokens": 5},
},
headers={"content-type": "application/json"},
)
config = ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
memory_enabled=False,
)
headers = {"x-api-key": "test-key", "anthropic-version": "2023-06-01"}
tools = [
create_ccr_tool_definition("anthropic"),
{"name": "Bash", "description": "run", "input_schema": {"type": "object"}},
]
def body(messages: list[dict[str, Any]]) -> dict[str, Any]:
return {
"model": "sonnet-test",
"max_tokens": 64,
"stream": True,
"tools": tools,
"messages": messages,
}
app = create_app(config)
with TestClient(app) as client:
proxy = client.app.state.proxy
proxy._retry_request = fake_retry
first = client.post("/v1/messages", json=body(first_turn), headers=headers)
assert first.status_code == 200, first.text
assert len(buffered_calls) == 1, "the turn that introduces the marker buffers"
upstream = MagicMock(spec=httpx.AsyncClient)
upstream.build_request = MagicMock(return_value=MagicMock())
upstream.send = AsyncMock(side_effect=[_sse_response(r) for r in second_turn_rounds])
proxy.http_client = upstream
second = client.post("/v1/messages", json=body(second_turn), headers=headers)
assert second.status_code == 200, second.text
sent = [json.loads(c.kwargs["content"]) for c in upstream.build_request.call_args_list]
return _events(second.content), sent, len(buffered_calls) - 1
def test_later_turn_retrieval_is_served_by_the_proxy(owned_marker: str) -> None:
events, sent, buffered = _two_turn_session(
owned_marker,
[
_sse([TEXT, _retrieve(owned_marker)], "tool_use"),
_sse([{"type": "text", "text": "The error is on line 2."}], "end_turn"),
],
)
assert buffered == 0, "a turn with only already-forwarded markers keeps streaming"
assert "headroom_retrieve" not in _tool_names(events)
texts = [e["delta"]["text"] for e in events if e["type"] == "content_block_delta"]
assert texts == ["Let me look at the earlier output.", "The error is on line 2."]
stops = [e["delta"]["stop_reason"] for e in events if e["type"] == "message_delta"]
assert stops == ["end_turn"]
assert [e["type"] for e in events].count("message_start") == 1
assert len(sent) == 2
continuation = sent[1]["messages"]
assert continuation[-2]["content"][-1]["name"] == "headroom_retrieve"
served = continuation[-1]["content"][0]
assert served["type"] == "tool_result" and served["tool_use_id"] == "toolu_ccr"
assert json.loads(served["content"])["original_content"] == ORIGINAL
@pytest.mark.parametrize("client_call_first", [True, False])
def test_retrieval_mixed_with_a_client_tool(owned_marker: str, client_call_first: bool) -> None:
calls = [BASH, _retrieve(owned_marker)]
if not client_call_first:
calls.reverse()
reissued = {**BASH, "id": "toolu_bash_2"}
events, sent, buffered = _two_turn_session(
owned_marker,
[
_sse(calls, "tool_use"),
_sse([{"type": "text", "text": "Now rerun it."}, reissued], "tool_use"),
],
)
assert buffered == 0
# Only the model's decision made with the content in hand reaches the client.
assert _tool_names(events) == ["Bash"]
started = [e["content_block"] for e in events if e["type"] == "content_block_start"]
assert [b.get("id") for b in started if b.get("type") == "tool_use"] == ["toolu_bash_2"]
stops = [e["delta"]["stop_reason"] for e in events if e["type"] == "message_delta"]
assert stops == ["tool_use"]
assert [e["index"] for e in events if e["type"] == "content_block_start"] == [0, 1]
continuation = sent[1]["messages"]
assert [b["name"] for b in continuation[-2]["content"]] == ["headroom_retrieve"]
assert json.loads(continuation[-1]["content"][0]["content"])["original_content"] == ORIGINAL
def test_old_marker_turn_without_retrieval_streams_unchanged(owned_marker: str) -> None:
raw = _sse([{"type": "text", "text": "It failed on line 2."}, BASH], "tool_use")
events, sent, buffered = _two_turn_session(owned_marker, [raw])
assert buffered == 0
assert len(sent) == 1, "no continuation round without a retrieval"
assert events == _events(raw)
+16
View File
@@ -173,6 +173,22 @@ class TestHeadroomConfig:
assert config.get_context_limit("claude-3-opus") == 200000
assert config.get_context_limit("claude-3-sonnet") == 200000
def test_get_context_limit_prefers_longest_prefix(self):
"""A more specific override wins over a shorter one, whatever the dict order."""
config = HeadroomConfig(
model_context_limits={
"gpt-4": 8192,
"gpt-4o": 128000,
"claude-3": 100000,
"claude-3-5-sonnet": 200000,
}
)
assert config.get_context_limit("gpt-4o-2024-08-06") == 128000
assert config.get_context_limit("claude-3-5-sonnet-20241022") == 200000
# The shorter prefix still applies when it is the only match.
assert config.get_context_limit("gpt-4-0613") == 8192
assert config.get_context_limit("claude-3-opus") == 100000
def test_get_context_limit_not_found(self):
"""get_context_limit returns None for unknown model."""
config = HeadroomConfig(model_context_limits={"gpt-4": 128000})

Some files were not shown because too many files have changed in this diff Show More