Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(detection): contain unidiff panic on orphaned +++ target line (#1548)
Manmit Singhe386c097d6d5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0d681c572..a1a555e6c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## Unreleased### Fixed+- Content detection no longer crashes the proxy on text containing an+ orphaned `+++ ` target line with no preceding `--- ` source line (common in+ `set -x` xtrace output and partial diffs). The bundled `unidiff` 0.4.0 parser+ panics on that input instead of returning an error; the Rust diff detector now+ contains the panic and treats the fragment as plain text, so the request is+ compressed and forwarded normally instead of returning HTTP 500+ ([#1547](https://github.com/headroomlabs-ai/headroom/issues/1547)).- Proactive expansion blocks injected into user turns are now wrapped in`<headroom_proactive_expansion>` XML tags, giving downstream consumers(LLMs, loggers, attribution parsers) a machine-readable provenancediff --git a/crates/headroom-core/src/transforms/unidiff_detector.rs b/crates/headroom-core/src/transforms/unidiff_detector.rsindex e5aa4f084..ef9616c11 100644--- a/crates/headroom-core/src/transforms/unidiff_detector.rs+++ b/crates/headroom-core/src/transforms/unidiff_detector.rs@@ -61,16 +61,28 @@ pub fn is_diff(content: &str) -> bool {return false;}- let mut patch = PatchSet::new();- if patch.parse(content).is_err() {- return false;- }+ // `unidiff` 0.4.0 does not return `Err` on every malformed input — a+ // `+++ ` target line with no preceding `--- ` source line makes it+ // `unwrap()` a `None` and panic (lib.rs:665). Inputs of that shape are+ // common (`set -x` xtrace, partial diffs quoted out of context). This+ // detector runs inside a thread-pool worker on the Python side, where a+ // native panic surfaces as an uncaught `PanicException` and 500s the whole+ // request. Contain any parser panic here and treat the fragment as "not a+ // diff" — consistent with the workspace's no-`panic = "abort"` policy of+ // surviving bad input rather than taking the process down.+ std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ let mut patch = PatchSet::new();+ if patch.parse(content).is_err() {+ return false;+ }- // `PatchSet::is_empty()` covers "found zero files"; the inner- // loop covers "found a file but with zero hunks" (e.g. mode-only- // changes). For diff-compressor routing we want at least one- // hunk — that's where the actual line-level change content lives.- !patch.is_empty() && patch.files().iter().any(|f| !f.is_empty())+ // `PatchSet::is_empty()` covers "found zero files"; the inner+ // loop covers "found a file but with zero hunks" (e.g. mode-only+ // changes). For diff-compressor routing we want at least one+ // hunk — that's where the actual line-level change content lives.+ !patch.is_empty() && patch.files().iter().any(|f| !f.is_empty())+ }))+ .unwrap_or(false)}/// [`ContentType`]-typed wrapper. Returns `Some(ContentType::GitDiff)`@@ -229,4 +241,16 @@ mod tests {assert_eq!(detect_diff("{}"), None);assert_eq!(detect_diff(""), None);}++ #[test]+ fn orphaned_target_line_does_not_panic() {+ // `unidiff` 0.4.0 panics (unwrap on `None`) when it meets a+ // `+++ ` target line with no preceding `--- ` source line.+ // That shape is common in `set -x` xtrace output and partial+ // diffs quoted out of context. It must degrade to "not a diff",+ // never abort the caller.+ assert!(!is_diff("+++ x"));+ assert_eq!(detect_diff("+++ x"), None);+ assert!(!is_diff("some prose\n+++ target without a source\nmore"));+ }}