llmtrim 0.11.0

Static, deterministic LLM prompt/payload compressor — cut input tokens 30-90% with zero extra model calls
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Private, per-Claude-Code-window subscription overrides.
//!
//! This deliberately lives outside the global llmtrim config: a slash command must not
//! restart the proxy or alter another Claude Code window.
use std::collections::BTreeMap;
use std::fs::{self, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};

const TTL: Duration = Duration::from_secs(30 * 60);
const TOUCH: Duration = Duration::from_secs(60);
const COMMAND_NAME: &str = "sub";

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
struct Registry {
    #[serde(default)]
    windows: BTreeMap<String, Window>,
    #[serde(default)]
    sessions: BTreeMap<String, String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Window {
    /// `None` follows the global policy; a value is this window's explicit override.
    intent: Option<Intent>,
    touched: u64,
    #[serde(default)]
    last_provider: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Intent {
    Enabled { provider: String },
    Disabled,
}

fn now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}
fn valid(value: &str) -> bool {
    !value.is_empty()
        && value.len() <= 160
        && value
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
        && !value.contains("..")
}
fn valid_provider(value: &str) -> bool {
    matches!(value, "codex" | "kimi" | "grok")
}

pub fn registry_path() -> Result<PathBuf> {
    Ok(crate::daemon::home_dir()?.join("claude-window-sub.json"))
}
struct RegistryLock(PathBuf);

impl Drop for RegistryLock {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.0);
    }
}

fn lock_registry(path: &Path) -> Result<RegistryLock> {
    let lock = path.with_extension("lock");
    if let Some(parent) = lock.parent() {
        fs::create_dir_all(parent)?;
    }
    for _ in 0..200 {
        match OpenOptions::new().write(true).create_new(true).open(&lock) {
            Ok(_) => return Ok(RegistryLock(lock)),
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
                let stale = fs::metadata(&lock)
                    .and_then(|m| m.modified())
                    .ok()
                    .and_then(|t| t.elapsed().ok())
                    .is_some_and(|age| age > Duration::from_secs(30));
                if stale {
                    let _ = fs::remove_file(&lock);
                } else {
                    std::thread::sleep(Duration::from_millis(10));
                }
            }
            Err(e) => return Err(e).context("creating window registry lock"),
        }
    }
    bail!("timed out waiting for window registry lock")
}

fn load_at(path: &Path) -> Result<Registry> {
    if !path.exists() {
        return Ok(Registry::default());
    }
    if fs::symlink_metadata(path)?.file_type().is_symlink() {
        bail!("refusing symlinked window registry");
    }
    let mut raw = String::new();
    fs::File::open(path)?.read_to_string(&mut raw)?;
    serde_json::from_str(&raw).context("corrupt window registry")
}
fn replace_file(tmp: &Path, destination: &Path) -> Result<()> {
    #[cfg(windows)]
    if destination.exists() {
        fs::remove_file(destination)?;
    }
    fs::rename(tmp, destination)?;
    Ok(())
}

fn save_at(path: &Path, registry: &Registry) -> Result<()> {
    let parent = path.parent().context("registry has no parent")?;
    fs::create_dir_all(parent)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(parent, fs::Permissions::from_mode(0o700))?;
    }
    let tmp = path.with_extension("json.tmp");
    if tmp.exists() && fs::symlink_metadata(&tmp)?.file_type().is_symlink() {
        bail!("refusing symlinked registry temp file");
    }
    let bytes = serde_json::to_vec_pretty(registry)?;
    let mut file = OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(true)
        .open(&tmp)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        file.set_permissions(fs::Permissions::from_mode(0o600))?;
    }
    file.write_all(&bytes)?;
    file.sync_all()?;
    replace_file(&tmp, path)?;
    Ok(())
}
fn prune(registry: &mut Registry, current: u64) {
    registry
        .windows
        .retain(|_, w| current.saturating_sub(w.touched) <= TTL.as_secs());
    registry
        .sessions
        .retain(|_, t| registry.windows.contains_key(t));
}
fn token() -> String {
    let mut b = [0u8; 24];
    if let Ok(mut f) = fs::File::open("/dev/urandom") {
        let _ = f.read_exact(&mut b);
    }
    if b.iter().all(|x| *x == 0) {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();
        let seed = nanos
            ^ ((std::process::id() as u128) << 64)
            ^ COUNTER.fetch_add(1, Ordering::Relaxed) as u128;
        for (i, x) in b.iter_mut().enumerate() {
            *x = seed.rotate_left((i * 7 % 128) as u32) as u8 ^ (i as u8).wrapping_mul(29);
        }
    }
    b.iter().map(|x| format!("{x:02x}")).collect()
}
/// Register a fresh startup/resume window, or retain its token across clear/compact.
pub fn session_start(session: &str, source: &str, existing: Option<&str>) -> Result<String> {
    if !valid(session) {
        bail!("invalid Claude session id");
    }
    let path = registry_path()?;
    let _lock = lock_registry(&path)?;
    let mut r = load_at(&path)?;
    let t = now();
    prune(&mut r, t);
    let token = if matches!(source, "clear" | "compact") {
        existing
            .filter(|x| valid(x) && r.windows.contains_key(*x))
            .map(str::to_owned)
            .unwrap_or_else(token)
    } else {
        token()
    };
    r.windows
        .entry(token.clone())
        .or_insert(Window {
            intent: None,
            touched: t,
            last_provider: None,
        })
        .touched = t;
    r.sessions.insert(session.to_owned(), token.clone());
    save_at(&path, &r)?;
    Ok(token)
}
pub fn session_end(session: &str, reason: &str) -> Result<()> {
    if !valid(session) {
        return Ok(());
    }
    let path = registry_path()?;
    let _lock = lock_registry(&path)?;
    let mut r = load_at(&path)?;
    if let Some(token) = r.sessions.remove(session)
        && reason != "clear"
    {
        r.windows.remove(&token);
        r.sessions.retain(|_, mapped| mapped != &token);
    }
    prune(&mut r, now());
    save_at(&path, &r)
}
pub fn set(session: &str, enabled: bool, provider: Option<&str>) -> Result<()> {
    if !valid(session) {
        bail!("invalid Claude session id");
    }
    let path = registry_path()?;
    let _lock = lock_registry(&path)?;
    let mut r = load_at(&path)?;
    prune(&mut r, now());
    let t = r
        .sessions
        .get(session)
        .cloned()
        .context("this Claude Code window is not registered; restart or resume it first")?;
    let w = r
        .windows
        .get_mut(&t)
        .context("window expired; restart or resume it first")?;
    w.touched = now();
    if enabled {
        let p = provider
            .map(str::to_owned)
            .or_else(|| w.last_provider.clone())
            .context(
                "no configured subscription provider; run llmtrim sub on codex, kimi, or grok first",
            )?;
        if !valid_provider(&p) {
            bail!("unsupported subscription provider");
        }
        w.last_provider = Some(p.clone());
        w.intent = Some(Intent::Enabled { provider: p });
    } else {
        w.intent = Some(Intent::Disabled);
    }
    save_at(&path, &r)
}
/// Read intent by inbound Claude session ID. Corruption deliberately falls back to global policy.
pub fn lookup(session: Option<&str>) -> Option<Intent> {
    let session = session.filter(|s| valid(s))?;
    let path = registry_path().ok()?;
    let _lock = lock_registry(&path).ok()?;
    let mut r = load_at(&path).ok()?;
    let token = r.sessions.get(session)?.clone();
    let window = r.windows.get_mut(&token)?;
    let current = now();
    if current.saturating_sub(window.touched) > TTL.as_secs() {
        r.windows.remove(&token);
        r.sessions.retain(|_, mapped| mapped != &token);
        let _ = save_at(&path, &r);
        return None;
    }
    let intent = window.intent.clone();
    if current.saturating_sub(window.touched) >= TOUCH.as_secs() {
        window.touched = current;
        let _ = save_at(&path, &r);
    }
    intent
}
pub fn status(session: &str) -> Result<Option<Intent>> {
    Ok(lookup(Some(session)))
}

/// Last provider this window successfully enabled, if any (used by bare `/sub on`).
pub fn last_provider(session: &str) -> Option<String> {
    if !valid(session) {
        return None;
    }
    let path = registry_path().ok()?;
    let _lock = lock_registry(&path).ok()?;
    let r = load_at(&path).ok()?;
    let token = r.sessions.get(session)?;
    let window = r.windows.get(token)?;
    let current = now();
    if current.saturating_sub(window.touched) > TTL.as_secs() {
        return None;
    }
    window.last_provider.clone()
}

#[cfg(unix)]
fn quoted_exe(exe: &str) -> String {
    format!("'{}'", exe.replace('\'', "'\"'\"'"))
}

#[cfg(windows)]
fn quoted_exe(exe: &str) -> String {
    format!("\"{}\"", exe.replace('"', "\"\""))
}

#[cfg(not(any(unix, windows)))]
fn quoted_exe(exe: &str) -> String {
    exe.to_string()
}

pub fn command_markdown(exe: &str) -> String {
    let exe = quoted_exe(exe);
    format!(
        "---\n\
         description: Toggle llmtrim subscription rerouting for this Claude Code window only.\n\
         disable-model-invocation: true\n\
         argument-hint: \"on [codex|kimi|grok]|off|status\"\n\
         ---\n\
         \n\
         <!-- llmtrim-owned-window-sub -->\n\
         Window-local subscription override (does not change other windows or the global\n\
         `llmtrim sub` setting).\n\
         \n\
         - `/sub on` — enable using the last provider for this window, or the global `sub`\n\
         - `/sub on codex` / `/sub on kimi` / `/sub on grok` — enable a specific provider\n\
         - `/sub off` — force Anthropic (with compression) for this window\n\
         - `/sub status` — show this window's override\n\
         \n\
         !`{exe} window-sub slash \"$ARGUMENTS\" \"$CLAUDE_CODE_SESSION_ID\"`\n"
    )
}
fn settings_path() -> Result<PathBuf> {
    let h = std::env::var_os("CLAUDE_CONFIG_DIR")
        .map(PathBuf::from)
        .or_else(|| {
            std::env::var_os("HOME")
                .or_else(|| std::env::var_os("USERPROFILE"))
                .map(|x| PathBuf::from(x).join(".claude"))
        })
        .context("could not determine Claude config directory")?;
    Ok(h.join("settings.json"))
}
const HOOK_MARKER: &str = "# llmtrim-owned-window-sub-hook";

fn owned(v: &serde_json::Value) -> bool {
    v.get("command")
        .and_then(serde_json::Value::as_str)
        .is_some_and(|command| command.trim_end().ends_with(HOOK_MARKER))
}

fn remove_owned_hooks(groups: &mut Vec<serde_json::Value>) {
    for group in groups.iter_mut() {
        if let Some(hooks) = group
            .get_mut("hooks")
            .and_then(serde_json::Value::as_array_mut)
        {
            hooks.retain(|hook| !owned(hook));
        }
    }
    groups.retain(|group| {
        group
            .get("hooks")
            .and_then(serde_json::Value::as_array)
            .is_none_or(|hooks| !hooks.is_empty())
    });
}
/// Ownership of the `/sub` skill + hooks relative to this binary.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OwnedStatus {
    Missing,
    Stale,
    Current,
}

/// Whether the Claude Code `/sub` skill + owned hooks are present.
pub fn is_installed() -> bool {
    !matches!(owned_status(), OwnedStatus::Missing)
}

/// Missing / present-but-stale-path / current for ensure probe + apply.
pub fn owned_status() -> OwnedStatus {
    let Ok(p) = settings_path() else {
        return OwnedStatus::Missing;
    };
    let Some(skill) = p
        .parent()
        .map(|dir| dir.join("skills").join(COMMAND_NAME).join("SKILL.md"))
    else {
        return OwnedStatus::Missing;
    };
    if !fs::read_to_string(&skill)
        .unwrap_or_default()
        .contains("llmtrim-owned-window-sub")
    {
        return OwnedStatus::Missing;
    }
    let Some(root) = fs::read_to_string(&p)
        .ok()
        .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
    else {
        return OwnedStatus::Missing;
    };
    let Some(hooks) = root.get("hooks").and_then(serde_json::Value::as_object) else {
        return OwnedStatus::Missing;
    };
    let desired = crate::statusline::stable_exe_string();
    let desired_quoted = quoted_exe(&desired);
    let mut saw = false;
    let mut current = true;
    for event in ["SessionStart", "SessionEnd"] {
        let Some(groups) = hooks.get(event).and_then(|v| v.as_array()) else {
            return OwnedStatus::Missing;
        };
        let mut event_ok = false;
        for group in groups {
            let Some(hs) = group.get("hooks").and_then(|v| v.as_array()) else {
                continue;
            };
            for h in hs {
                if !owned(h) {
                    continue;
                }
                saw = true;
                event_ok = true;
                let cmd = h.get("command").and_then(|c| c.as_str()).unwrap_or("");
                // Hook is current if it embeds this binary (quoted or raw).
                if !(cmd.contains(&desired_quoted) || cmd.contains(&desired)) {
                    current = false;
                }
            }
        }
        if !event_ok {
            return OwnedStatus::Missing;
        }
    }
    if !saw {
        return OwnedStatus::Missing;
    }
    if current {
        OwnedStatus::Current
    } else {
        OwnedStatus::Stale
    }
}

pub fn install(exe: &str) -> Result<()> {
    let hook_exe = quoted_exe(exe);
    let p = settings_path()?;
    let skill = p
        .parent()
        .context("settings has no parent")?
        .join("skills")
        .join(COMMAND_NAME);
    let skill_file = skill.join("SKILL.md");
    if skill_file.exists()
        && !fs::read_to_string(&skill_file)
            .unwrap_or_default()
            .contains("llmtrim-owned-window-sub")
    {
        bail!(
            "{} already exists and is not owned by llmtrim",
            skill_file.display()
        );
    }
    let mut root = if p.exists() {
        serde_json::from_str(&fs::read_to_string(&p)?)?
    } else {
        serde_json::json!({})
    };
    let obj = root
        .as_object_mut()
        .context("Claude settings root must be an object")?;
    let hooks = obj
        .entry("hooks")
        .or_insert_with(|| serde_json::json!({}))
        .as_object_mut()
        .context("Claude hooks must be an object")?;
    for (event, command) in [
        (
            "SessionStart",
            format!("{hook_exe} window-sub hook-start {HOOK_MARKER}"),
        ),
        (
            "SessionEnd",
            format!("{hook_exe} window-sub hook-end {HOOK_MARKER}"),
        ),
    ] {
        let arr = hooks
            .entry(event)
            .or_insert_with(|| serde_json::json!([]))
            .as_array_mut()
            .context("Claude hook event must be an array")?;
        remove_owned_hooks(arr);
        arr.push(serde_json::json!({"hooks":[{"type":"command","command":command,"timeout":10}]}));
    }
    let dir = p.parent().context("settings has no parent")?;
    fs::create_dir_all(dir)?;
    let tmp = p.with_extension("json.tmp");
    fs::write(&tmp, serde_json::to_vec_pretty(&root)?)?;
    replace_file(&tmp, &p)?;
    fs::create_dir_all(&skill)?;
    fs::write(skill_file, command_markdown(exe))?;
    Ok(())
}
pub fn uninstall() -> Result<()> {
    let p = settings_path()?;
    if p.exists() {
        let mut r: serde_json::Value = serde_json::from_str(&fs::read_to_string(&p)?)?;
        if let Some(h) = r
            .get_mut("hooks")
            .and_then(serde_json::Value::as_object_mut)
        {
            for e in ["SessionStart", "SessionEnd"] {
                if let Some(groups) = h.get_mut(e).and_then(serde_json::Value::as_array_mut) {
                    remove_owned_hooks(groups);
                }
            }
        }
        fs::write(&p, serde_json::to_vec_pretty(&r)?)?;
    }
    let skill = settings_path()?
        .parent()
        .unwrap()
        .join("skills")
        .join(COMMAND_NAME);
    let skill_file = skill.join("SKILL.md");
    if fs::read_to_string(&skill_file)
        .unwrap_or_default()
        .contains("llmtrim-owned-window-sub")
    {
        fs::remove_file(skill_file)?;
        if fs::read_dir(&skill)?.next().is_none() {
            fs::remove_dir(skill)?;
        }
    }
    Ok(())
}

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

    #[test]
    fn rejects_traversal_and_accepts_opaque_ids() {
        assert!(valid("01abc_DEF-9"));
        assert!(!valid("../escape"));
        assert!(!valid("bad/slash"));
    }

    #[test]
    fn registry_prunes_expired_window_and_session() {
        let mut r = Registry::default();
        r.windows.insert(
            "token".into(),
            Window {
                intent: None,
                touched: 1,
                last_provider: None,
            },
        );
        r.sessions.insert("session".into(), "token".into());
        prune(&mut r, TTL.as_secs() + 2);
        assert!(r.windows.is_empty());
        assert!(r.sessions.is_empty());
    }

    #[test]
    fn skill_uses_session_environment_without_exposing_it() {
        let text = command_markdown("llmtrim");
        assert!(text.contains("CLAUDE_CODE_SESSION_ID"));
        assert!(!text.contains("LLMTRIM_CLAUDE_WINDOW_TOKEN"));
        assert!(
            text.contains("on [codex|kimi|grok]"),
            "argument-hint should list providers: {text}"
        );
        assert!(
            text.contains("/sub on grok"),
            "skill body should document explicit provider: {text}"
        );
    }

    #[test]
    fn accepts_all_subscription_providers() {
        assert!(valid_provider("codex"));
        assert!(valid_provider("kimi"));
        assert!(valid_provider("grok"));
        assert!(!valid_provider("anthropic"));
        assert!(!valid_provider("openai"));
    }

    #[test]
    fn removing_owned_hook_preserves_neighbors_in_the_same_group() {
        let mut groups = vec![serde_json::json!({
            "hooks": [
                {"type": "command", "command": "user-hook"},
                {"type": "command", "command": format!("llmtrim window-sub hook-start {HOOK_MARKER}")}
            ]
        })];
        remove_owned_hooks(&mut groups);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0]["hooks"].as_array().unwrap().len(), 1);
        assert_eq!(groups[0]["hooks"][0]["command"], "user-hook");
    }

    #[cfg(unix)]
    #[test]
    fn executable_path_is_single_quote_escaped() {
        let command = command_markdown("/tmp/a'$(touch nope)/llmtrim");
        assert!(command.contains("'/tmp/a'\"'\"'$(touch nope)/llmtrim'"));
    }
}