llmman 0.1.689

Run any agent on any model, models stored as OCI images
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
//! `llmman launch --optimizer`: what an integration is launched with, besides
//! the model, to make a session cheaper.
//!
//! Each optimizer says which integrations it works with ([`Optimizer::check`],
//! run before the daemon starts) and what it adds to a launch
//! ([`Optimizer::addition`]); a launcher just applies the [`Addition`].
//! `none` adds nothing.

use std::path::{Path, PathBuf};

use anyhow::Context;
use clap::ValueEnum;

use super::common::write_if_changed;
use super::{claude, codex, hdrm, opencode};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, ValueEnum)]
pub enum Optimizer {
    /// Launch as without the flag; nothing is added
    #[default]
    None,
    /// Compact the output of the shell commands the agent runs (claude and
    /// opencode)
    Tk,
    /// Ask the model for terse replies (claude, codex and opencode)
    Cvmn,
    /// Compress what the agent sends the model (claude, codex and opencode)
    Hdrm,
}

/// What an optimizer adds to a launch.
#[derive(Debug, Default, PartialEq, Eq)]
pub(super) struct Addition {
    /// Arguments placed before the ones the launcher builds.
    pub args: Vec<String>,
    /// Environment variables set for the integration.
    pub env: Vec<(String, String)>,
    /// Top-level keys added to the config llmman hands opencode.
    pub config: Option<serde_json::Value>,
    /// Where the integration reaches the model server, in place of llmman's.
    pub server: Option<String>,
}

/// The integrations `tk` works with. codex is left out: its hooks need a
/// manual review, and a rewrite needs the hook to approve the command.
const TK_INTEGRATIONS: &[&str] = &["claude", "opencode"];

/// The integrations `cvmn` works with.
const CVMN_INTEGRATIONS: &[&str] = &["claude", "codex", "opencode"];

/// The integrations `hdrm` works with.
const HDRM_INTEGRATIONS: &[&str] = &["claude", "codex", "opencode"];

const CVMN_PROMPT: &str = include_str!("cvmn.md");

/// `__LLMMAN__` is the path of llmman.
const TK_OPENCODE_PLUGIN: &str = include_str!("opencode_tk.js");

impl Optimizer {
    /// The name `--optimizer` spells it with.
    fn name(self) -> &'static str {
        match self {
            Optimizer::None => "none",
            Optimizer::Tk => "tk",
            Optimizer::Cvmn => "cvmn",
            Optimizer::Hdrm => "hdrm",
        }
    }

    /// The integrations this optimizer works with.
    fn integrations(self) -> &'static [&'static str] {
        match self {
            Optimizer::None => &[],
            Optimizer::Tk => TK_INTEGRATIONS,
            Optimizer::Cvmn => CVMN_INTEGRATIONS,
            Optimizer::Hdrm => HDRM_INTEGRATIONS,
        }
    }

    /// Refuses a launch of `integration` (a name from `llmman launch`'s list)
    /// that this optimizer cannot serve. Runs before the daemon starts and
    /// before a model is pulled; `none` accepts everything.
    pub(super) fn check(
        self,
        integration: &str,
        sandboxed: bool,
        extra_args: &[String],
    ) -> anyhow::Result<()> {
        if self == Optimizer::None {
            return Ok(());
        }
        self.ensure_integration(integration)?;
        anyhow::ensure!(
            !sandboxed,
            "--optimizer {} does not work with --sandbox.",
            self.name()
        );
        let conflict = match (self, integration) {
            (Optimizer::Tk, "claude") => claude::optimizer_conflict(extra_args),
            (Optimizer::Cvmn, "claude") => claude::cvmn_conflict(extra_args),
            (Optimizer::Cvmn, "codex") => codex::cvmn_conflict(extra_args),
            _ => None,
        };
        if let Some(why) = conflict {
            anyhow::bail!("--optimizer {} does not work with {why}", self.name());
        }
        if self == Optimizer::Tk {
            anyhow::ensure!(
                !cfg!(windows),
                "--optimizer {} is not supported on Windows.",
                self.name()
            );
            if integration == "opencode" {
                opencode::ensure_plugins()?;
            }
        }
        if self == Optimizer::Hdrm {
            hdrm::ensure_installed()?;
        }
        Ok(())
    }

    fn ensure_integration(self, integration: &str) -> anyhow::Result<()> {
        anyhow::ensure!(
            self.integrations().contains(&integration),
            "--optimizer {} does not work with {integration}: only {} can use it.\n\
             Launch {integration} without --optimizer, or use one of those.",
            self.name(),
            self.integrations().join(", ")
        );
        Ok(())
    }

    /// What this optimizer adds to a launch of `integration`. `none` adds
    /// nothing and looks at nothing, so a launch without the flag does no
    /// work it did not do before.
    pub(super) fn addition(self, integration: &str) -> anyhow::Result<Addition> {
        if self == Optimizer::None {
            return Ok(Addition::default());
        }
        if self == Optimizer::Hdrm {
            let url = hdrm::start(&super::server())?;
            // codex's profile file is shared by every launch; a `-c` is not.
            return Ok(match integration {
                "codex" => Addition {
                    args: vec!["-c".into(), codex::server_override(&url)],
                    ..Addition::default()
                },
                _ => Addition {
                    server: Some(url),
                    ..Addition::default()
                },
            });
        }
        let exe = match self {
            Optimizer::Tk => {
                std::env::current_exe().context("cannot find the path of the running llmman")?
            }
            _ => PathBuf::new(),
        };
        let dir = match integration {
            "opencode" => opencode::opencode_state_dir()?.join("llmman"),
            _ => PathBuf::new(),
        };
        self.addition_with(integration, &exe, &dir)
    }

    /// [`Optimizer::addition`] with the path of llmman and the directory for
    /// the files an integration reads.
    pub(super) fn addition_with(
        self,
        integration: &str,
        exe: &Path,
        dir: &Path,
    ) -> anyhow::Result<Addition> {
        match (self, integration) {
            (Optimizer::None, _) => Ok(Addition::default()),
            (Optimizer::Tk, "claude") => Ok(Addition {
                args: vec![
                    "--settings".into(),
                    crate::tk::hook::settings_json(utf8(exe)?),
                ],
                ..Addition::default()
            }),
            (Optimizer::Tk, "opencode") => {
                let plugin = dir.join("tk");
                let source = TK_OPENCODE_PLUGIN.replace("__LLMMAN__", &json_string(utf8(exe)?));
                write_if_changed(&plugin.join("index.js"), &source)?;
                Ok(Addition {
                    config: Some(serde_json::json!({ "plugins": [utf8(&plugin)?] })),
                    ..Addition::default()
                })
            }
            (Optimizer::Cvmn, "claude") => Ok(Addition {
                args: vec!["--append-system-prompt".into(), CVMN_PROMPT.trim().into()],
                ..Addition::default()
            }),
            (Optimizer::Cvmn, "codex") => Ok(Addition {
                args: vec![
                    "-c".into(),
                    codex::developer_instructions(CVMN_PROMPT.trim()),
                ],
                ..Addition::default()
            }),
            (Optimizer::Cvmn, "opencode") => {
                let file = dir.join("cvmn.md");
                write_if_changed(&file, CVMN_PROMPT)?;
                Ok(Addition {
                    config: Some(serde_json::json!({ "instructions": [utf8(&file)?] })),
                    ..Addition::default()
                })
            }
            (optimizer, other) => anyhow::bail!(
                "--optimizer {} does not work with {other}",
                optimizer.name()
            ),
        }
    }

    /// The line `launch` prints for `integration`, if the optimizer is on.
    pub(super) fn notice(self, integration: &str) -> Option<String> {
        match self {
            Optimizer::None => None,
            Optimizer::Tk => Some(format!(
                "[llmman] {integration}: optimizer tk (command output compacted; LLMMAN_TK=off disables)"
            )),
            Optimizer::Cvmn => Some(format!(
                "[llmman] {integration}: optimizer cvmn (terse replies requested)"
            )),
            Optimizer::Hdrm => Some(format!(
                "[llmman] {integration}: optimizer hdrm (requests compressed by a local proxy)"
            )),
        }
    }
}

fn utf8(path: &Path) -> anyhow::Result<&str> {
    path.to_str()
        .ok_or_else(|| anyhow::anyhow!("not a valid UTF-8 path: {}", path.display()))
}

/// `text` as a JavaScript string literal.
fn json_string(text: &str) -> String {
    serde_json::to_string(text).expect("a string serializes")
}

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

    #[test]
    fn the_choices_are_spelled_none_tk_cvmn_and_hdrm() {
        let names: Vec<String> = Optimizer::value_variants()
            .iter()
            .map(|o| o.to_possible_value().unwrap().get_name().to_string())
            .collect();
        assert_eq!(names, ["none", "tk", "cvmn", "hdrm"]);
        assert_eq!(Optimizer::default(), Optimizer::None);
        for o in Optimizer::value_variants() {
            assert_eq!(o.to_possible_value().unwrap().get_name(), o.name());
        }
    }

    #[test]
    fn none_works_with_everything_and_adds_nothing() {
        for name in [
            "claude",
            "opencode",
            "codex",
            "goose",
            "docker-agent",
            "nonesuch",
        ] {
            Optimizer::None.check(name, false, &[]).unwrap();
            Optimizer::None
                .check(name, true, &["--settings".into()])
                .unwrap();
            assert_eq!(Optimizer::None.addition(name).unwrap(), Addition::default());
        }
        assert_eq!(Optimizer::None.notice("claude"), None);
    }

    #[cfg(not(windows))]
    #[test]
    fn tk_works_with_claude() {
        Optimizer::Tk.check("claude", false, &[]).unwrap();
        Optimizer::Tk
            .check("claude", false, &["-p".into(), "hello".into()])
            .unwrap();
    }

    #[test]
    fn tk_names_the_integration_and_the_ones_that_work() {
        for name in ["codex", "aider", "goose", "docker-agent"] {
            let err = Optimizer::Tk
                .check(name, false, &[])
                .unwrap_err()
                .to_string();
            assert!(err.contains(&format!("does not work with {name}")), "{err}");
            assert!(err.contains("only claude, opencode can use it"), "{err}");
        }
    }

    #[test]
    fn every_integration_with_tk_is_one_the_capability_list_names() {
        // The list is the only place that says which integrations work.
        assert_eq!(TK_INTEGRATIONS, ["claude", "opencode"]);
    }

    #[test]
    fn tk_is_refused_in_a_sandbox() {
        let err = Optimizer::Tk
            .check("claude", true, &[])
            .unwrap_err()
            .to_string();
        assert!(err.contains("--sandbox"), "{err}");
    }

    #[test]
    fn tk_is_refused_beside_claude_arguments_it_cannot_honor() {
        let args = |a: &[&str]| a.iter().map(|s| s.to_string()).collect::<Vec<_>>();
        for (extra, flag) in [
            (&["--settings", "mine.json"][..], "--settings"),
            (&["--settings=mine.json"], "--settings"),
            (&["--bare"], "--bare"),
            (&["--disallowedTools", "Bash(rm:*)"], "--disallowedTools"),
            (&["--disallowed-tools=Bash(rm:*)"], "--disallowedTools"),
        ] {
            let err = Optimizer::Tk
                .check("claude", false, &args(extra))
                .unwrap_err()
                .to_string();
            assert!(err.contains(flag), "{extra:?}: {err}");
        }
        // A prompt that reads like a flag, after a second `--`, is a prompt.
        #[cfg(not(windows))]
        Optimizer::Tk
            .check("claude", false, &args(&["-p", "--", "--settings", "x"]))
            .unwrap();
    }

    #[test]
    fn tk_adds_a_settings_argument_for_claude_and_nothing_else() {
        let added = Optimizer::Tk
            .addition_with("claude", Path::new("/opt/llmman"), Path::new(""))
            .unwrap();
        assert_eq!(added.args.len(), 2);
        assert_eq!(added.args[0], "--settings");
        let settings: serde_json::Value = serde_json::from_str(&added.args[1]).unwrap();
        assert_eq!(
            settings["hooks"]["PreToolUse"][0]["hooks"][0]["command"],
            "'/opt/llmman' tk hook"
        );
        assert!(added.env.is_empty());
        assert!(added.config.is_none());
        assert!(Optimizer::Tk
            .addition_with("codex", Path::new("/opt/llmman"), Path::new(""))
            .is_err());
    }

    #[test]
    fn the_notice_says_how_to_turn_tk_off() {
        assert_eq!(
            Optimizer::Tk.notice("claude").as_deref(),
            Some(
                "[llmman] claude: optimizer tk (command output compacted; LLMMAN_TK=off disables)"
            )
        );
    }

    #[test]
    fn cvmn_works_with_claude_and_opencode() {
        Optimizer::Cvmn.check("claude", false, &[]).unwrap();
        Optimizer::Cvmn.check("opencode", false, &[]).unwrap();
        Optimizer::Cvmn
            .check("claude", false, &["-p".into(), "hello".into()])
            .unwrap();
        assert_eq!(CVMN_INTEGRATIONS, ["claude", "codex", "opencode"]);
        for name in ["aider", "goose", "docker-agent"] {
            let err = Optimizer::Cvmn
                .check(name, false, &[])
                .unwrap_err()
                .to_string();
            assert!(err.contains(&format!("does not work with {name}")), "{err}");
            assert!(
                err.contains("only claude, codex, opencode can use it"),
                "{err}"
            );
        }
    }

    #[test]
    fn cvmn_is_refused_in_a_sandbox() {
        let err = Optimizer::Cvmn
            .check("claude", true, &[])
            .unwrap_err()
            .to_string();
        assert!(err.contains("--sandbox"), "{err}");
    }

    #[test]
    fn cvmn_is_refused_beside_a_system_prompt_of_the_callers() {
        let args = |a: &[&str]| a.iter().map(|s| s.to_string()).collect::<Vec<_>>();
        for extra in [
            &["--append-system-prompt", "mine"][..],
            &["--append-system-prompt=mine"],
            &["--append-system-prompt-file", "mine.md"],
        ] {
            let err = Optimizer::Cvmn
                .check("claude", false, &args(extra))
                .unwrap_err()
                .to_string();
            assert!(err.contains("--append-system-prompt"), "{extra:?}: {err}");
        }
        // After a second `--`, it is a prompt.
        Optimizer::Cvmn
            .check(
                "claude",
                false,
                &args(&["-p", "--", "--append-system-prompt", "x"]),
            )
            .unwrap();
    }

    #[test]
    fn cvmn_appends_its_prompt_for_claude_and_nothing_else() {
        let added = Optimizer::Cvmn.addition("claude").unwrap();
        assert_eq!(added.args.len(), 2);
        assert_eq!(added.args[0], "--append-system-prompt");
        assert_eq!(added.args[1], CVMN_PROMPT.trim());
        assert!(added.args[1].starts_with("Answer tersely."));
        assert!(added.env.is_empty());
        assert!(added.config.is_none());
        assert!(Optimizer::Cvmn.addition("aider").is_err());
    }

    #[test]
    fn cvmn_sets_codexs_developer_instructions_with_a_config_override() {
        let added = Optimizer::Cvmn.addition("codex").unwrap();
        assert_eq!(added.args.len(), 2);
        assert_eq!(added.args[0], "-c");
        let (key, value) = added.args[1].split_once('=').unwrap();
        assert_eq!(key, "developer_instructions");
        // The override parses as TOML, and gives the prompt back.
        let doc: toml_edit::DocumentMut = added.args[1].parse().unwrap();
        assert_eq!(doc[key].as_str(), Some(CVMN_PROMPT.trim()));
        assert!(!value.is_empty());
    }

    #[test]
    fn opencode_gets_its_files_from_the_directory_it_is_given() {
        let dir = tempfile::tempdir().unwrap();
        let exe = Path::new("/opt/llm man/llmman");

        let added = Optimizer::Cvmn
            .addition_with("opencode", exe, dir.path())
            .unwrap();
        let file = dir.path().join("cvmn.md");
        assert_eq!(std::fs::read_to_string(&file).unwrap(), CVMN_PROMPT);
        assert_eq!(
            added.config,
            Some(serde_json::json!({ "instructions": [file.to_str().unwrap()] }))
        );
        assert!(added.args.is_empty() && added.env.is_empty());

        let added = Optimizer::Tk
            .addition_with("opencode", exe, dir.path())
            .unwrap();
        let plugin = dir.path().join("tk");
        assert_eq!(
            added.config,
            Some(serde_json::json!({ "plugins": [plugin.to_str().unwrap()] }))
        );
        let source = std::fs::read_to_string(plugin.join("index.js")).unwrap();
        assert!(
            source.contains(r#"const LLMMAN = "/opt/llm man/llmman""#),
            "{source}"
        );
        assert!(!source.contains("__LLMMAN__"));
    }

    #[test]
    fn the_notice_says_cvmn_is_on() {
        assert_eq!(
            Optimizer::Cvmn.notice("claude").as_deref(),
            Some("[llmman] claude: optimizer cvmn (terse replies requested)")
        );
    }

    #[test]
    fn hdrm_names_the_integration_and_the_ones_that_work() {
        assert_eq!(HDRM_INTEGRATIONS, ["claude", "codex", "opencode"]);
        for name in ["aider", "goose", "docker-agent"] {
            let err = Optimizer::Hdrm
                .check(name, false, &[])
                .unwrap_err()
                .to_string();
            assert!(err.contains(&format!("does not work with {name}")), "{err}");
            assert!(
                err.contains("only claude, codex, opencode can use it"),
                "{err}"
            );
        }
    }

    #[test]
    fn hdrm_is_refused_in_a_sandbox() {
        let err = Optimizer::Hdrm
            .check("claude", true, &[])
            .unwrap_err()
            .to_string();
        assert!(err.contains("--sandbox"), "{err}");
    }

    #[test]
    fn hdrm_needs_a_proxy_to_start() {
        assert!(Optimizer::Hdrm
            .addition_with("claude", Path::new(""), Path::new(""))
            .is_err());
    }

    #[test]
    fn hdrm_points_codex_at_the_proxy_with_a_config_override() {
        let doc: toml_edit::DocumentMut = codex::server_override("http://127.0.0.1:9")
            .parse()
            .unwrap();
        assert_eq!(
            doc["model_providers"]["llmman"]["base_url"].as_str(),
            Some("http://127.0.0.1:9/v1")
        );
    }

    #[test]
    fn the_notice_says_hdrm_is_on() {
        assert_eq!(
            Optimizer::Hdrm.notice("codex").as_deref(),
            Some("[llmman] codex: optimizer hdrm (requests compressed by a local proxy)")
        );
    }
}