tinyjuice 0.2.1

Pluggable token compression for OpenHuman.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Glue between the agent tool loop and the TokenJuice content router.
//!
//! Exposes the entry points the agent loop calls after a tool returns output:
//!
//! - [`compact_tool_output_with_policy`] — full version with the tool's JSON
//!   arguments and exit code; derives a command/argv and content hint, routes
//!   through the content router, and returns `(text, CompactionStats)`.
//! - [`compact_output`] — minimal version (content + tool name + enable flag)
//!   for call sites that only have those, returning just the text.
//!
//! Both are **pass-through safe**: if compression doesn't meaningfully shrink
//! the payload, or the input is under the byte floor, or the router/CCR is
//! disabled, the original string is returned untouched.
//!
//! Runtime options (the `[tokenjuice]` config block) are installed once at
//! startup via [`configure`]; callers don't thread `Config` through.

use once_cell::sync::OnceCell;
use serde_json::Value;
use std::sync::RwLock;

use super::compress::route;
use super::types::{AgentTokenjuiceCompression, CompressInput, CompressOptions, ContentHint};

/// Skip compaction for outputs smaller than this (bytes) by default. Tiny
/// outputs have no headroom and risk distortion. Overridable per the config's
/// `min_bytes_to_compress` once [`configure`] runs.
const DEFAULT_MIN_COMPACT_INPUT_BYTES: usize = 512;

/// Process-global runtime options, installed from config at startup.
fn options_cell() -> &'static RwLock<CompressOptions> {
    static OPTS: OnceCell<RwLock<CompressOptions>> = OnceCell::new();
    OPTS.get_or_init(|| {
        RwLock::new(CompressOptions {
            min_bytes_to_compress: DEFAULT_MIN_COMPACT_INPUT_BYTES,
            ..Default::default()
        })
    })
}

/// Install the runtime [`CompressOptions`] (called once from config at startup).
/// Also configures the CCR cache limits/disk tier indirectly via the caller.
pub fn configure(opts: CompressOptions) {
    *options_cell().write().unwrap_or_else(|p| p.into_inner()) = opts;
}

/// Snapshot the current runtime options.
pub fn current_options() -> CompressOptions {
    options_cell()
        .read()
        .unwrap_or_else(|p| p.into_inner())
        .clone()
}

fn options_for_agent(profile: AgentTokenjuiceCompression) -> Option<CompressOptions> {
    match profile {
        AgentTokenjuiceCompression::Off => None,
        AgentTokenjuiceCompression::Auto | AgentTokenjuiceCompression::Full => {
            Some(current_options())
        }
        AgentTokenjuiceCompression::Light => {
            let mut opts = current_options();
            // Coding agents need raw, exact tool text more than aggressive token
            // savings. Disabling CCR makes every lossy compressor decline in
            // route(), while still allowing any truly lossless reduction.
            opts.ccr_enabled = false;
            opts.ml_text_enabled = false;
            Some(opts)
        }
    }
}

/// Install the full TokenJuice runtime configuration in one call at startup:
/// router/compressor options, CCR cache limits, and the optional on-disk tier.
/// Kept free of the config-schema type so `tokenjuice` stays decoupled — the
/// caller maps `Config.tokenjuice` into these primitives.
#[allow(clippy::too_many_arguments)]
pub fn install_config(
    options: CompressOptions,
    max_cache_entries: usize,
    max_cache_bytes: usize,
    ccr_ttl_secs: Option<u64>,
    disk_tier_root: Option<std::path::PathBuf>,
) {
    configure(options);
    super::cache::configure(max_cache_entries, max_cache_bytes, ccr_ttl_secs);
    // Enable or disable the disk tier to match the setting — a `None` here means
    // the user turned it off, so clear any previously-installed disk root rather
    // than leaving the process writing originals to disk until restart.
    match disk_tier_root {
        Some(root) => super::cache::enable_disk_tier(root),
        None => super::cache::disable_disk_tier(),
    }
    log::debug!("[tokenjuice] runtime config installed");
}

/// Statistics for a single compaction call (back-compat shape).
#[derive(Debug, Clone)]
pub struct CompactionStats {
    pub tool_name: String,
    pub original_bytes: usize,
    pub compacted_bytes: usize,
    /// The compressor kind (or `none/...`) that handled the output.
    pub rule_id: String,
    pub applied: bool,
}

impl CompactionStats {
    pub fn ratio(&self) -> f64 {
        if self.original_bytes == 0 {
            1.0
        } else {
            self.compacted_bytes as f64 / self.original_bytes as f64
        }
    }
}

/// Compact a tool call's output using an agent-level TokenJuice profile.
///
/// * `tool_name` — the agent-level tool name (`shell`, `grep`, `browser_navigate`).
/// * `arguments` — the raw JSON arguments; used to derive command/argv (for the
///   log/command rule path) and a file extension (for code/JSON/HTML hints).
/// * `output` — the captured tool output (already credential-scrubbed).
/// * `exit_code` — enables failure-preserving behaviour in the log compressor.
///
/// Returns `(text, stats)`. When `stats.applied == false` the text is the
/// untouched original.
pub async fn compact_tool_output_with_policy(
    tool_name: &str,
    arguments: Option<&Value>,
    output: &str,
    exit_code: Option<i32>,
    profile: AgentTokenjuiceCompression,
) -> (String, CompactionStats) {
    let original_bytes = output.len();

    let Some(opts) = options_for_agent(profile) else {
        log::debug!(
            "[tokenjuice] agent profile disabled compaction tool={} bytes={}",
            tool_name,
            original_bytes
        );
        return (
            output.to_string(),
            CompactionStats {
                tool_name: tool_name.to_string(),
                original_bytes,
                compacted_bytes: original_bytes,
                rule_id: "none/agent-profile-off".to_string(),
                applied: false,
            },
        );
    };

    // A recovery tool's output is the original we previously offloaded — never
    // re-compact it, or the agent could never see the full data it asked for.
    if super::cache::is_recovery_tool(tool_name) {
        return (
            output.to_string(),
            CompactionStats {
                tool_name: tool_name.to_string(),
                original_bytes,
                compacted_bytes: original_bytes,
                rule_id: "none/recovery-tool".to_string(),
                applied: false,
            },
        );
    }

    let (command, argv) = extract_command_argv(arguments);
    let hint = ContentHint {
        source_tool: Some(tool_name.to_string()),
        extension: extract_extension(arguments),
        query: extract_query(arguments),
        ..Default::default()
    };

    let input = CompressInput {
        content: output,
        kind: super::types::ContentKind::PlainText,
        hint: &hint,
        exit_code,
        command,
        argv,
        original_bytes,
    };

    let res = route(input, &opts).await;
    let stats = CompactionStats {
        tool_name: tool_name.to_string(),
        original_bytes,
        compacted_bytes: res.compacted_bytes,
        rule_id: if res.applied {
            res.compressor.as_str().to_string()
        } else {
            format!("none/{}", res.content_kind.as_str())
        },
        applied: res.applied,
    };
    (res.text, stats)
}

/// Minimal compaction for call sites that only have content + tool name. The
/// `enabled` flag is an explicit kill-switch on top of the configured options.
pub async fn compact_output(content: String, tool_name: &str, enabled: bool) -> String {
    compact_output_with_policy(
        content,
        tool_name,
        enabled,
        AgentTokenjuiceCompression::Full,
    )
    .await
}

/// Minimal compaction with an agent-level TokenJuice profile.
pub async fn compact_output_with_policy(
    content: String,
    tool_name: &str,
    enabled: bool,
    profile: AgentTokenjuiceCompression,
) -> String {
    // The call-site `enabled` flag and the configured router switch are both
    // hard off-switches; either one short-circuits to the untouched original.
    if !enabled || !current_options().router_enabled {
        return content;
    }
    let (text, _stats) =
        compact_tool_output_with_policy(tool_name, None, &content, None, profile).await;
    text
}

/// Derive `(command, argv)` from a tool's JSON arguments.
fn extract_command_argv(arguments: Option<&Value>) -> (Option<String>, Option<Vec<String>>) {
    let Some(Value::Object(map)) = arguments else {
        return (None, None);
    };

    if let Some(Value::Array(arr)) = map.get("argv") {
        let argv: Vec<String> = arr
            .iter()
            .filter_map(|v| v.as_str().map(|s| s.to_owned()))
            .collect();
        if !argv.is_empty() {
            let command = argv.join(" ");
            return (Some(command), Some(argv));
        }
    }

    let cmd_str = map
        .get("command")
        .and_then(Value::as_str)
        .or_else(|| map.get("cmd").and_then(Value::as_str));

    if let Some(cmd) = cmd_str {
        if let Some(Value::Array(args)) = map.get("args") {
            let mut argv = vec![cmd.to_owned()];
            argv.extend(args.iter().filter_map(|v| v.as_str().map(|s| s.to_owned())));
            return (Some(format!("{cmd} {}", argv[1..].join(" "))), Some(argv));
        }
        let argv: Vec<String> = cmd.split_whitespace().map(|s| s.to_owned()).collect();
        return (Some(cmd.to_owned()), (!argv.is_empty()).then_some(argv));
    }

    (None, None)
}

/// Derive a file extension hint from common path-bearing argument shapes.
fn extract_extension(arguments: Option<&Value>) -> Option<String> {
    let Some(Value::Object(map)) = arguments else {
        return None;
    };
    let path = ["path", "file_path", "file", "filename"]
        .iter()
        .find_map(|k| map.get(*k).and_then(Value::as_str))?;
    let ext = std::path::Path::new(path)
        .extension()
        .and_then(|e| e.to_str())?;
    Some(ext.to_ascii_lowercase())
}

/// Derive a search-query hint from common query-bearing argument shapes.
fn extract_query(arguments: Option<&Value>) -> Option<String> {
    let Some(Value::Object(map)) = arguments else {
        return None;
    };
    ["query", "pattern", "search", "q", "regex"]
        .iter()
        .find_map(|k| map.get(*k).and_then(Value::as_str))
        .map(str::to_string)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[tokio::test]
    async fn skips_short_output() {
        let (out, stats) = compact_tool_output_with_policy(
            "shell",
            None,
            "hello world",
            Some(0),
            AgentTokenjuiceCompression::Full,
        )
        .await;
        assert_eq!(out, "hello world");
        assert!(!stats.applied);
        assert_eq!(stats.original_bytes, 11);
    }

    #[tokio::test]
    async fn compacts_long_git_status_via_argv() {
        let mut lines = vec!["On branch main".to_owned()];
        for i in 0..200 {
            lines.push(format!("\tmodified:   src/file_{i}.rs"));
        }
        let output = lines.join("\n");
        let args = json!({"command": "git status"});
        let (compacted, stats) = compact_tool_output_with_policy(
            "shell",
            Some(&args),
            &output,
            Some(0),
            AgentTokenjuiceCompression::Full,
        )
        .await;
        assert!(stats.applied, "expected compaction, got {:?}", stats);
        assert!(compacted.len() < output.len());
    }

    #[tokio::test]
    async fn passes_through_incompressible_output() {
        let unique_lines: Vec<String> = (0..200)
            .map(|i| format!("unique-payload-chunk-{i}-{}", "x".repeat(30)))
            .collect();
        let output = unique_lines.join("\n");
        let (returned, stats) = compact_tool_output_with_policy(
            "unknown_tool",
            None,
            &output,
            Some(0),
            AgentTokenjuiceCompression::Full,
        )
        .await;
        if !stats.applied {
            assert_eq!(returned, output);
        }
    }

    #[tokio::test]
    async fn disabled_flag_is_passthrough() {
        let big = "x".repeat(5000);
        assert_eq!(compact_output(big.clone(), "grep", false).await, big);
    }

    #[tokio::test]
    async fn light_agent_profile_declines_lossy_ccr_compaction() {
        let mut lines = vec!["On branch main".to_owned()];
        for i in 0..200 {
            lines.push(format!("\tmodified:   src/file_{i}.rs"));
        }
        let output = lines.join("\n");
        let args = json!({"command": "git status"});
        let (returned, stats) = compact_tool_output_with_policy(
            "shell",
            Some(&args),
            &output,
            Some(0),
            AgentTokenjuiceCompression::Light,
        )
        .await;
        assert_eq!(returned, output);
        assert!(!stats.applied);
    }

    #[tokio::test]
    async fn off_agent_profile_bypasses_router() {
        let big = "x".repeat(5000);
        let returned =
            compact_output_with_policy(big.clone(), "grep", true, AgentTokenjuiceCompression::Off)
                .await;
        assert_eq!(returned, big);
    }

    #[test]
    fn extract_argv_handles_common_shapes() {
        let (cmd, argv) = extract_command_argv(Some(&json!({"command": "git status"})));
        assert_eq!(cmd.as_deref(), Some("git status"));
        assert_eq!(argv.unwrap(), vec!["git", "status"]);

        let (cmd, _) = extract_command_argv(Some(&json!({"command": "cargo", "args": ["test"]})));
        assert_eq!(cmd.as_deref(), Some("cargo test"));
    }

    #[test]
    fn extract_extension_and_query() {
        assert_eq!(
            extract_extension(Some(&json!({"path": "src/lib.rs"}))).as_deref(),
            Some("rs")
        );
        assert_eq!(
            extract_query(Some(&json!({"pattern": "foo bar"}))).as_deref(),
            Some("foo bar")
        );
    }
}