Skip to content

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.

fix(smart-crusher): recurse at adaptive array limit (#3770)

AxelRay7790bdeebda2
diff --git a/crates/headroom-core/src/transforms/smart_crusher/crusher.rs b/crates/headroom-core/src/transforms/smart_crusher/crusher.rs
index 9fb40c9e7..9135884fd 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/crusher.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/crusher.rs
@@ -544,6 +544,36 @@ impl SmartCrusher {
// compressed / marker-substituted by the hook.
let result =
self.crush_array_with_source(&rows, arr, query_context, bias);
+ // Adaptive sizing can decide to keep every row even after the
+ // analysis threshold is crossed. Match the below-threshold path
+ // so nested values still receive their own safe transforms.
+ if result.strategy_info == "none:adaptive_at_limit" {
+ info_parts.push(format!(
+ "{}({}->{})",
+ result.strategy_info,
+ n,
+ result.items.len()
+ ));
+ if prose_hook.is_some() {
+ return (Value::Array(rows), info_parts.join(","));
+ }
+
+ let mut processed: Vec<Value> = Vec::with_capacity(n);
+ for item in arr {
+ let (p_item, p_info) = self.process_value_with_hook(
+ item,
+ depth + 1,
+ query_context,
+ bias,
+ prose_hook,
+ );
+ processed.push(p_item);
+ if !p_info.is_empty() {
+ info_parts.push(p_info);
+ }
+ }
+ return (Value::Array(processed), info_parts.join(","));
+ }
// Lossless path won → substitute the array
// with the compacted string in place. This
// makes the lossless win visible to the
@@ -2038,6 +2068,40 @@ mod tests {
);
}
+ #[test]
+ fn crush_recurses_into_object_arrays_at_adaptive_limit() {
+ let c = crusher();
+ let description =
+ "Curated collection of business tables covering customer, product, ".repeat(12);
+
+ for n in 5..=8 {
+ let rows: Vec<Value> = (0..n)
+ .map(|i| {
+ json!({
+ "name": format!("domain_{i}"),
+ "description": description,
+ "tables": 12 + i,
+ "owner": "data-platform"
+ })
+ })
+ .collect();
+ let input = json!({"domains": rows}).to_string();
+ let result = c.crush(&input, "list the available domains", 1.0);
+
+ assert!(
+ result.strategy.contains("string_ccr:"),
+ "n={n} should recurse into rows at the adaptive limit: strategy={} output={}",
+ result.strategy,
+ result.compressed
+ );
+ assert!(
+ result.compressed.contains("<<ccr:"),
+ "n={n} should offload long row strings: {}",
+ result.compressed
+ );
+ }
+ }
+
/// Issue #3634 MCP `saga-mcp_task_get` payload (813 bytes, original
/// key order, tags encoded as a JSON string).
const MCP_TASK_GET_JSON: &str = r#"{"id":79,"epic_id":9,"title":"test","description":null,"status":"done","priority":"medium","sort_order":0,"assigned_to":null,"estimated_hours":null,"actual_hours":null,"due_date":null,"source_ref":null,"metadata":"{}","created_at":"2026-09-15 20:32:06","updated_at":"2026-09-16 11:59:27","description_locked":0,"is_deleted":0,"deleted_at":null,"deleted_by":null,"delete_reason":null,"epic_name":"latency-routing","tags":"[\"cherry-pick\",\"dedicated branch\"]","subtasks":[{"id":163,"task_id":79,"title":"test2","status":"todo","sort_order":1,"created_at":"2026-09-15 20:32:13","updated_at":"2026-09-16 14:02:26"},{"id":165,"task_id":79,"title":"test4","status":"todo","sort_order":2,"created_at":"2026-09-15 20:38:47","updated_at":"2026-09-17 20:29:04"}],"notes":[],"comments":[],"depends_on":[],"dependents":[]}"#;