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
//! `llmman launch cline`.

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

use anyhow::Context;

use super::{accepts_prompt, env_dir, exec_with_env, find_on_path, node_user_profile, server};

const CLINE_NPM_INSTALL_ARGS: &[&str] = &["install", "-g", "cline@latest"];
const CLINE_INSTALL_PROMPT: &str = "Cline is not installed. Install with npm? [y/N] ";
const CLINE_INSTALL_CANCELLED: &str = "cline installation cancelled";

/// Offers the official npm install when Cline is missing. This runs before
/// the daemon starts, so declining or lacking npm has no model-pull side
/// effects. Re-resolving from PATH after npm exits also catches a global npm
/// prefix that the current shell does not yet know about.
pub(super) fn ensure_cline_installed() -> anyhow::Result<()> {
    use std::io::{BufRead, IsTerminal, Write};

    if find_on_path("cline").is_some() {
        return Ok(());
    }
    let npm = find_on_path("npm").ok_or_else(|| {
        anyhow::anyhow!(
            "cline is not installed and npm is not on PATH\n\n\
             Install Node.js from https://nodejs.org/, then re-run:\n  \
             llmman launch cline"
        )
    })?;
    anyhow::ensure!(
        std::io::stdin().is_terminal() && std::io::stderr().is_terminal(),
        "cline is not installed\n\nInstall it with:\n  npm install -g cline@latest\n\n\
         Then re-run:\n  llmman launch cline"
    );

    eprint!("{CLINE_INSTALL_PROMPT}");
    std::io::stderr().flush()?;
    let mut answer = String::new();
    std::io::stdin().lock().read_line(&mut answer)?;
    anyhow::ensure!(accepts_prompt(&answer), CLINE_INSTALL_CANCELLED);

    eprintln!("\nInstalling Cline...");
    let status = Command::new(&npm)
        .args(CLINE_NPM_INSTALL_ARGS)
        .status()
        .with_context(|| format!("failed to run {}", npm.display()))?;
    anyhow::ensure!(status.success(), "failed to install cline: {status}");
    let cline = find_on_path("cline").ok_or_else(|| {
        anyhow::anyhow!(
            "cline was installed but the binary was not found on PATH\n\n\
         You may need to restart your shell"
        )
    })?;
    let version = Command::new(&cline)
        .arg("--version")
        .status()
        .with_context(|| format!("failed to run {} --version", cline.display()))?;
    anyhow::ensure!(
        version.success(),
        "cline was installed but failed to start ({version})"
    );
    eprintln!("Cline installed successfully\n");
    Ok(())
}

/// cline: merge llmman's Ollama route into Cline's own settings, then pass
/// through exactly the arguments supplied after `--`. Cline reads both the
/// current provider store and legacy global-state fields, so keep them in
/// sync while preserving unrelated user settings in each file.
pub(super) fn launch_cline(model: &str, extra_args: &[String]) -> anyhow::Result<()> {
    let bin = find_on_path("cline").ok_or_else(|| anyhow::anyhow!("cline is not installed"))?;
    write_cline_settings(model)?;
    exec_with_env(&bin, extra_args, &[])
}

/// Cline's own resolution: `CLINE_DIR || path.join(os.homedir(), ".cline")`.
/// Node's `os.homedir()` reads `USERPROFILE` on Windows, which
/// `dirs::home_dir` ignores; disagreeing with Cline there means it
/// never sees the settings and exits "Not authenticated".
pub(super) fn cline_dir() -> anyhow::Result<PathBuf> {
    resolve_cline_dir(env_dir("CLINE_DIR"), node_user_profile(), || {
        dirs::home_dir().context("no home directory")
    })
}

fn resolve_cline_dir(
    cline_dir: Option<PathBuf>,
    user_profile: Option<PathBuf>,
    home: impl FnOnce() -> anyhow::Result<PathBuf>,
) -> anyhow::Result<PathBuf> {
    if let Some(dir) = cline_dir {
        return Ok(dir);
    }
    Ok(match user_profile {
        Some(profile) => profile,
        None => home()?,
    }
    .join(".cline"))
}

pub(super) fn cline_data_dir() -> anyhow::Result<PathBuf> {
    Ok(cline_dir()?.join("data"))
}

fn write_cline_settings(model: &str) -> anyhow::Result<()> {
    let server = server();
    let now = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
    write_cline_settings_at(&cline_data_dir()?, model, &server, &now)
}

/// How long Cline's Ollama provider waits for a response to *start*, in
/// ms. Through llmman it also has to cover llama-server prefilling
/// Cline's ~12k-token system prompt, since no bytes are sent before the
/// first token: on CPU that takes minutes (a 4-vCPU aarch64 runner
/// manages ~44 tok/s even on a 0.8B model), and llmman's own load
/// deadline is already 20 minutes. In CI run 35602511987 Cline dropped
/// its first request 5:00 into the prefill and re-sent it. 30 minutes
/// covers load plus a long prefill; connection failures still fail fast,
/// and a user-set value is kept.
///
/// Written twice, since Cline has read it from two places: older versions
/// from `providers.ollama.settings.timeout` (default 5 minutes), and 2.18
/// from `requestTimeoutMs` in `globalState.json`, whose Ollama handler
/// falls back to 30 seconds without it. In CI run 38123323946 every
/// aarch64 Cline request timed out that way and was re-sent mid-prefill.
const CLINE_RESPONSE_START_TIMEOUT_MS: u64 = 30 * 60 * 1000;

fn read_cline_json(path: &Path) -> anyhow::Result<(Option<Vec<u8>>, serde_json::Value)> {
    let raw = match std::fs::read(path) {
        Ok(raw) => Some(raw),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
        Err(error) => return Err(error).with_context(|| format!("read {}", path.display())),
    };
    let document = match raw.as_deref().map(|bytes| String::from_utf8_lossy(bytes)) {
        None => serde_json::json!({}),
        Some(text) if text.trim().is_empty() => serde_json::json!({}),
        Some(text) => serde_json::from_str(&text)
            .with_context(|| format!("parse {} as JSON", path.display()))?,
    };
    anyhow::ensure!(
        document.is_object(),
        "{} is not a JSON object",
        path.display()
    );
    Ok((raw, document))
}

/// Writes a changed Cline JSON document atomically, copying the exact prior
/// bytes to `<name>.json.bak` before replacing it.
fn write_cline_json(
    path: &Path,
    raw: Option<&[u8]>,
    before: &serde_json::Value,
    after: &serde_json::Value,
) -> anyhow::Result<()> {
    if before == after {
        return Ok(());
    }
    let parent = path
        .parent()
        .context("Cline settings path has no directory")?;
    std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
    if let Some(raw) = raw {
        let backup = path.with_extension("json.bak");
        std::fs::write(&backup, raw)
            .with_context(|| format!("back up {} to {}", path.display(), backup.display()))?;
    }
    let mut contents = serde_json::to_vec_pretty(after).context("serialize Cline settings")?;
    contents.push(b'\n');
    crate::fsutil::write_atomic(path, &contents)
        .with_context(|| format!("write {}", path.display()))
}

fn write_cline_settings_at(
    data_dir: &Path,
    model: &str,
    server: &str,
    now: &str,
) -> anyhow::Result<()> {
    let providers_path = data_dir.join("settings/providers.json");
    let (providers_raw, mut providers_document) = read_cline_json(&providers_path)?;
    let providers_before = providers_document.clone();
    let base_url = format!("{server}/v1");
    let route_changed = providers_document
        .pointer("/providers/ollama/settings/model")
        .and_then(serde_json::Value::as_str)
        != Some(model)
        || providers_document
            .pointer("/providers/ollama/settings/baseUrl")
            .and_then(serde_json::Value::as_str)
            != Some(base_url.as_str())
        || providers_document
            .pointer("/providers/ollama/settings/timeout")
            .is_none();

    let root = providers_document
        .as_object_mut()
        .expect("read_cline_json returns an object");
    root.insert("version".to_string(), serde_json::json!(1));
    root.insert("lastUsedProvider".to_string(), serde_json::json!("ollama"));
    let providers = root
        .entry("providers")
        .or_insert_with(|| serde_json::json!({}))
        .as_object_mut()
        .context("Cline providers is not a JSON object")?;
    let ollama = providers
        .entry("ollama")
        .or_insert_with(|| serde_json::json!({}))
        .as_object_mut()
        .context("Cline providers.ollama is not a JSON object")?;
    let settings = ollama
        .entry("settings")
        .or_insert_with(|| serde_json::json!({}))
        .as_object_mut()
        .context("Cline providers.ollama.settings is not a JSON object")?;
    settings.insert("provider".to_string(), serde_json::json!("ollama"));
    settings.insert("model".to_string(), serde_json::json!(model));
    settings.insert("baseUrl".to_string(), serde_json::json!(base_url));
    settings
        .entry("timeout")
        .or_insert_with(|| serde_json::json!(CLINE_RESPONSE_START_TIMEOUT_MS));
    settings.remove("apiKey");
    ollama.insert("tokenSource".to_string(), serde_json::json!("manual"));
    if route_changed {
        ollama.insert("updatedAt".to_string(), serde_json::json!(now));
    }
    write_cline_json(
        &providers_path,
        providers_raw.as_deref(),
        &providers_before,
        &providers_document,
    )?;

    let global_state_path = data_dir.join("globalState.json");
    let (global_raw, mut global_document) = read_cline_json(&global_state_path)?;
    let global_before = global_document.clone();
    let global = global_document
        .as_object_mut()
        .expect("read_cline_json returns an object");
    for key in [
        "ollamaBaseUrl",
        "actModeOllamaBaseUrl",
        "planModeOllamaBaseUrl",
    ] {
        global.insert(key.to_string(), serde_json::json!(server));
    }
    for key in ["actModeApiProvider", "planModeApiProvider"] {
        global.insert(key.to_string(), serde_json::json!("ollama"));
    }
    for key in ["actModeOllamaModelId", "planModeOllamaModelId"] {
        global.insert(key.to_string(), serde_json::json!(model));
    }
    global.insert("welcomeViewCompleted".to_string(), serde_json::json!(true));
    global
        .entry("requestTimeoutMs")
        .or_insert_with(|| serde_json::json!(CLINE_RESPONSE_START_TIMEOUT_MS));
    write_cline_json(
        &global_state_path,
        global_raw.as_deref(),
        &global_before,
        &global_document,
    )
}

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

    #[test]
    fn cline_settings_merge_ollama_and_preserve_user_state() {
        let root = std::env::temp_dir().join(format!(
            "llmman-cline-settings-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        let providers_path = root.join("settings/providers.json");
        let global_path = root.join("globalState.json");
        let model = r#"org/model.\"quoted\""#;
        let providers_original = r#"{
  "version": 7,
  "lastUsedProvider": "anthropic",
  "mine": true,
  "providers": {
    "anthropic": { "settings": { "model": "mine" } },
    "ollama": {
      "settings": { "provider": "ollama", "model": "old", "baseUrl": "http://old", "apiKey": "delete-me", "keep": 1 },
      "updatedAt": "2000-01-01T00:00:00Z",
      "other": true
    }
  }
}"#;
        let global_original = r#"{"theme":"mine","welcomeViewCompleted":false}"#;
        std::fs::create_dir_all(providers_path.parent().unwrap()).unwrap();
        std::fs::write(&providers_path, providers_original).unwrap();
        std::fs::write(&global_path, global_original).unwrap();

        write_cline_settings_at(
            &root,
            model,
            "http://127.0.0.1:17434",
            "2026-09-20T06:00:00Z",
        )
        .unwrap();

        let parsed: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&providers_path).unwrap()).unwrap();
        let entry = &parsed["providers"]["ollama"];
        assert_eq!(parsed["version"], 1);
        assert_eq!(parsed["lastUsedProvider"], "ollama");
        assert_eq!(parsed["mine"], true);
        assert_eq!(
            parsed["providers"]["anthropic"]["settings"]["model"],
            "mine"
        );
        assert_eq!(entry["settings"]["provider"], "ollama");
        assert_eq!(entry["settings"]["model"], model);
        assert_eq!(entry["settings"]["baseUrl"], "http://127.0.0.1:17434/v1");
        assert_eq!(entry["settings"]["keep"], 1);
        assert_eq!(
            entry["settings"]["timeout"], CLINE_RESPONSE_START_TIMEOUT_MS,
            "a missing response-start timeout gets llmman's slow-prefill default"
        );
        assert!(entry["settings"].get("apiKey").is_none());
        assert_eq!(entry["tokenSource"], "manual");
        assert_eq!(entry["updatedAt"], "2026-09-20T06:00:00Z");
        assert_eq!(entry["other"], true);
        assert_eq!(
            std::fs::read_to_string(root.join("settings/providers.json.bak")).unwrap(),
            providers_original
        );

        let global: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&global_path).unwrap()).unwrap();
        assert_eq!(global["theme"], "mine");
        assert_eq!(global["ollamaBaseUrl"], "http://127.0.0.1:17434");
        assert_eq!(global["actModeApiProvider"], "ollama");
        assert_eq!(global["planModeApiProvider"], "ollama");
        assert_eq!(global["actModeOllamaModelId"], model);
        assert_eq!(global["planModeOllamaModelId"], model);
        assert_eq!(global["actModeOllamaBaseUrl"], "http://127.0.0.1:17434");
        assert_eq!(global["planModeOllamaBaseUrl"], "http://127.0.0.1:17434");
        assert_eq!(global["welcomeViewCompleted"], true);
        assert_eq!(
            global["requestTimeoutMs"], CLINE_RESPONSE_START_TIMEOUT_MS,
            "a missing response-start timeout gets llmman's slow-prefill default"
        );
        assert_eq!(
            std::fs::read_to_string(root.join("globalState.json.bak")).unwrap(),
            global_original
        );

        let providers_written = std::fs::read(&providers_path).unwrap();
        let global_written = std::fs::read(&global_path).unwrap();
        write_cline_settings_at(
            &root,
            model,
            "http://127.0.0.1:17434",
            "2026-09-20T07:00:00Z",
        )
        .unwrap();
        assert_eq!(std::fs::read(&providers_path).unwrap(), providers_written);
        assert_eq!(std::fs::read(&global_path).unwrap(), global_written);

        write_cline_settings_at(
            &root,
            "new-model",
            "http://127.0.0.1:17434",
            "2026-09-20T08:00:00Z",
        )
        .unwrap();
        let changed: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&providers_path).unwrap()).unwrap();
        assert_eq!(
            changed["providers"]["ollama"]["updatedAt"],
            "2026-09-20T08:00:00Z"
        );
        let _ = std::fs::remove_dir_all(root);
    }

    #[test]
    fn cline_settings_keep_a_user_set_response_start_timeout() {
        let root = std::env::temp_dir().join(format!(
            "llmman-cline-timeout-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        let providers_path = root.join("settings/providers.json");
        std::fs::create_dir_all(providers_path.parent().unwrap()).unwrap();
        std::fs::write(
            &providers_path,
            r#"{"providers":{"ollama":{"settings":{"timeout":45000}}}}"#,
        )
        .unwrap();
        let global_path = root.join("globalState.json");
        std::fs::write(&global_path, r#"{"requestTimeoutMs":90000}"#).unwrap();

        write_cline_settings_at(
            &root,
            "some-model",
            "http://127.0.0.1:17434",
            "2026-09-20T06:00:00Z",
        )
        .unwrap();

        let parsed: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&providers_path).unwrap()).unwrap();
        assert_eq!(parsed["providers"]["ollama"]["settings"]["timeout"], 45000);
        let global: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&global_path).unwrap()).unwrap();
        assert_eq!(global["requestTimeoutMs"], 90000);
        let _ = std::fs::remove_dir_all(root);
    }

    #[test]
    fn cline_install_contract_uses_latest_and_only_yes_accepts() {
        assert_eq!(CLINE_NPM_INSTALL_ARGS, ["install", "-g", "cline@latest"]);
        assert_eq!(
            CLINE_INSTALL_PROMPT,
            "Cline is not installed. Install with npm? [y/N] "
        );
        assert_eq!(CLINE_INSTALL_CANCELLED, "cline installation cancelled");
        for answer in ["y", "Y", "yes", "YES", " yes\n"] {
            assert!(accepts_prompt(answer));
        }
        for answer in ["", "\n", "n", "no", "yep"] {
            assert!(!accepts_prompt(answer));
        }
    }

    /// Same precedence as Cline: `CLINE_DIR`, then `USERPROFILE` (Windows'
    /// `os.homedir()`), then the process home.
    #[test]
    fn cline_dir_follows_cline_dir_then_userprofile_then_home() {
        let p = |s: &str| Some(PathBuf::from(s));
        let home = || Ok(PathBuf::from("/home"));
        let resolve = |dir, profile| resolve_cline_dir(dir, profile, home).unwrap();
        assert_eq!(
            resolve(p("/explicit"), p("/profile")),
            PathBuf::from("/explicit")
        );
        assert_eq!(
            resolve(None, p("/profile")),
            PathBuf::from("/profile/.cline")
        );
        assert_eq!(resolve(None, None), PathBuf::from("/home/.cline"));
    }
}