lucy-cli 1.7.0

A small local JSONL agent harness
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
use std::fs::{self, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

#[cfg(unix)]
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};

pub const DEFAULT_BASE_URL: &str = "https://openrouter.ai/api/v1";
pub const DEFAULT_API_KEY_ENV: &str = "OPENAI_API_KEY";
pub const GENERATED_API_KEY_ENV: &str = "OPENROUTER_API_KEY";
pub const DEFAULT_SYSTEM_PROMPT: &str = "You can access computer resources. Use the provided tools to achieve the user's requirements. When needed, use cmd to read a relevant skill's SKILL.md.";

const GENERATED_CONFIG: &str = r#"system_prompt = "You can access computer resources. Use the provided tools to achieve the user's requirements. When needed, use cmd to read a relevant skill's SKILL.md."

[llm]
base_url = "https://openrouter.ai/api/v1"
model = ""
api_key_env = "OPENROUTER_API_KEY"
# Optional reasoning effort sent as the OpenAI Chat Completions "reasoning_effort"
# field, e.g. "low", "medium", "high". Omit or leave unset to send no effort.
# Use a value your provider and model support; an unsupported value fails at runtime.
# effort = "medium"
"#;

#[derive(Debug)]
pub struct ConfigError(String);

impl ConfigError {
    fn new(message: impl Into<String>) -> Self {
        Self(message.into())
    }
}

impl std::fmt::Display for ConfigError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(&self.0)
    }
}

impl std::error::Error for ConfigError {}

impl From<io::Error> for ConfigError {
    fn from(_error: io::Error) -> Self {
        Self::new("configuration file error")
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct Config {
    #[serde(default = "default_system_prompt")]
    pub system_prompt: String,
    #[serde(default)]
    pub llm: LlmConfig,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct LlmConfig {
    #[serde(default = "default_base_url")]
    pub base_url: String,
    #[serde(default)]
    pub model: String,
    #[serde(default)]
    pub api_key_env: Option<String>,
    #[serde(default)]
    pub effort: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct LlmSettings {
    pub base_url: String,
    pub model: String,
    pub api_key_env: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effort: Option<String>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            system_prompt: DEFAULT_SYSTEM_PROMPT.to_owned(),
            llm: LlmConfig::default(),
        }
    }
}

impl Default for LlmConfig {
    fn default() -> Self {
        Self {
            base_url: DEFAULT_BASE_URL.to_owned(),
            model: String::new(),
            api_key_env: None,
            effort: None,
        }
    }
}

fn default_system_prompt() -> String {
    DEFAULT_SYSTEM_PROMPT.to_owned()
}

fn default_base_url() -> String {
    DEFAULT_BASE_URL.to_owned()
}

impl Config {
    pub fn load_or_create(home: &Path) -> Result<Self, ConfigError> {
        Self::ensure_exists(home)?;
        Self::load_from_path(&config_path(home))
    }

    pub fn ensure_exists(home: &Path) -> Result<(), ConfigError> {
        let path = config_path(home);
        ensure_private_dir(&config_dir(home))?;
        ensure_not_symlink(&path)?;

        if !path.exists() {
            migrate_legacy_config(home, &path)?;
        }
        if path.exists() {
            ensure_private_file(&path)?;
            return Ok(());
        }
        if generated_config_contains_active_key() {
            return Err(ConfigError::new("configuration bootstrap rejected"));
        }

        let mut options = OpenOptions::new();
        options.write(true).create_new(true);
        #[cfg(unix)]
        options.mode(0o600);
        match options.open(&path) {
            Ok(mut file) => {
                file.write_all(GENERATED_CONFIG.as_bytes())?;
                file.flush()?;
                ensure_private_file(&path)?;
                Ok(())
            }
            Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {
                ensure_private_file(&path)?;
                Ok(())
            }
            Err(_error) => Err(ConfigError::new("unable to create config.toml")),
        }
    }

    pub fn load_from_path(path: &Path) -> Result<Self, ConfigError> {
        ensure_not_symlink(path)
            .map_err(|_error| ConfigError::new("unable to secure config.toml"))?;
        let bytes = fs::read_to_string(path)
            .map_err(|_error| ConfigError::new("unable to read config.toml"))?;
        ensure_private_file(path)
            .map_err(|_error| ConfigError::new("unable to secure config.toml"))?;
        toml::from_str(&bytes)
            .map_err(|_| ConfigError::new("unable to parse config.toml: invalid TOML"))
    }

    /// Update only the user-selected fields while preserving other TOML content.
    pub fn save_selection(
        home: &Path,
        model: &str,
        effort: Option<&str>,
    ) -> Result<(), ConfigError> {
        let path = config_path(home);
        let source = fs::read_to_string(&path)
            .map_err(|_| ConfigError::new("unable to read config.toml"))?;
        let mut document: toml::Value = toml::from_str(&source)
            .map_err(|_| ConfigError::new("unable to parse config.toml: invalid TOML"))?;
        let llm = document
            .as_table_mut()
            .and_then(|root| root.get_mut("llm"))
            .and_then(toml::Value::as_table_mut)
            .ok_or_else(|| ConfigError::new("config.toml is missing [llm]"))?;
        llm.insert("model".to_owned(), toml::Value::String(model.to_owned()));
        match effort.filter(|value| !value.trim().is_empty()) {
            Some(value) => {
                llm.insert(
                    "effort".to_owned(),
                    toml::Value::String(value.trim().to_owned()),
                );
            }
            None => {
                llm.remove("effort");
            }
        }
        let rendered = toml::to_string_pretty(&document)
            .map_err(|_| ConfigError::new("unable to write config.toml"))?;
        fs::write(&path, rendered).map_err(|_| ConfigError::new("unable to write config.toml"))?;
        ensure_private_file(&path).map_err(|_| ConfigError::new("unable to secure config.toml"))
    }

    pub fn resolved_llm(&self) -> Result<LlmSettings, ConfigError> {
        let base_url = self.llm.base_url.trim().to_owned();
        if base_url.is_empty() {
            return Err(ConfigError::new("llm.base_url must not be empty"));
        }

        let api_key_env = self
            .llm
            .api_key_env
            .as_deref()
            .unwrap_or(DEFAULT_API_KEY_ENV)
            .trim()
            .to_owned();
        if api_key_env.is_empty() {
            return Err(ConfigError::new("llm.api_key_env must not be empty"));
        }

        let effort = self.llm.effort.as_deref().map(str::trim).map(str::to_owned);

        Ok(LlmSettings {
            base_url,
            model: self.llm.model.trim().to_owned(),
            api_key_env,
            effort,
        })
    }
}

/// Return Lucy's XDG configuration directory. An unset, empty, or relative
/// `XDG_CONFIG_HOME` falls back to the standard `$HOME/.config` location.
pub fn config_dir(home: &Path) -> PathBuf {
    config_dir_from_xdg_home(home, std::env::var_os("XDG_CONFIG_HOME").as_deref())
}

fn config_dir_from_xdg_home(home: &Path, xdg_config_home: Option<&std::ffi::OsStr>) -> PathBuf {
    xdg_config_home
        .filter(|path| !path.is_empty())
        .map(PathBuf::from)
        .filter(|path| path.is_absolute())
        .unwrap_or_else(|| home.join(".config"))
        .join("lucy")
}

/// Lucy's legacy state directory. Sessions intentionally remain here while
/// user-editable configuration moves to the XDG configuration directory.
pub fn lucy_dir(home: &Path) -> PathBuf {
    home.join(".lucy")
}

pub fn config_path(home: &Path) -> PathBuf {
    config_dir(home).join("config.toml")
}

fn legacy_config_path(home: &Path) -> PathBuf {
    home.join(".lucy").join("config.toml")
}

fn migrate_legacy_config(home: &Path, destination: &Path) -> Result<(), ConfigError> {
    let legacy = legacy_config_path(home);
    let legacy_dir = legacy.parent().expect("legacy config has a parent");
    ensure_not_symlink(legacy_dir)
        .map_err(|_error| ConfigError::new("unable to secure legacy config.toml"))?;

    let metadata = match fs::symlink_metadata(&legacy) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
        Err(_error) => return Err(ConfigError::new("unable to inspect legacy config.toml")),
    };
    if metadata.file_type().is_symlink() || !metadata.is_file() {
        return Err(ConfigError::new("unable to secure legacy config.toml"));
    }
    let bytes = fs::read(&legacy)
        .map_err(|_error| ConfigError::new("unable to read legacy config.toml"))?;
    ensure_private_file(&legacy)
        .map_err(|_error| ConfigError::new("unable to secure legacy config.toml"))?;

    let mut options = OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    options.mode(0o600);
    let mut destination_file = options
        .open(destination)
        .map_err(|_error| ConfigError::new("unable to migrate legacy config.toml"))?;
    destination_file
        .write_all(&bytes)
        .and_then(|()| destination_file.flush())
        .map_err(|_error| ConfigError::new("unable to migrate legacy config.toml"))?;
    ensure_private_file(destination)
        .map_err(|_error| ConfigError::new("unable to secure config.toml"))?;
    fs::remove_file(legacy)
        .map_err(|_error| ConfigError::new("unable to remove legacy config.toml"))?;
    Ok(())
}

fn generated_config_contains_active_key() -> bool {
    std::env::var(GENERATED_API_KEY_ENV)
        .ok()
        .filter(|secret| !secret.is_empty())
        .is_some_and(|secret| GENERATED_CONFIG.contains(&secret))
}

pub(crate) fn ensure_not_symlink(path: &Path) -> io::Result<()> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() => Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "symlinks are not allowed for protected paths",
        )),
        Ok(_) => Ok(()),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error),
    }
}

pub(crate) fn ensure_private_dir(path: &Path) -> io::Result<()> {
    ensure_not_symlink(path)?;
    fs::create_dir_all(path)?;
    let metadata = fs::symlink_metadata(path)?;
    if metadata.file_type().is_symlink() || !metadata.is_dir() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "protected path is not a directory",
        ));
    }
    #[cfg(unix)]
    fs::set_permissions(path, fs::Permissions::from_mode(0o700))?;
    Ok(())
}

pub(crate) fn ensure_private_file(path: &Path) -> io::Result<()> {
    ensure_not_symlink(path)?;
    #[cfg(unix)]
    fs::set_permissions(path, fs::Permissions::from_mode(0o600))?;
    #[cfg(not(unix))]
    let _ = path;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(unix)]
    use std::os::unix::fs::{symlink, PermissionsExt};
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::time::{SystemTime, UNIX_EPOCH};

    static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn temporary_home() -> PathBuf {
        loop {
            let stamp = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("clock")
                .as_nanos();
            let counter = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
            let path = std::env::temp_dir().join(format!(
                "lucy-config-{stamp}-{}-{counter}",
                std::process::id()
            ));
            match fs::create_dir(&path) {
                Ok(()) => return path,
                Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
                Err(error) => panic!("temp home: {error}"),
            }
        }
    }

    #[test]
    fn bootstraps_config_without_overwriting_existing_bytes() {
        let home = temporary_home();
        let first = Config::load_or_create(&home).expect("create config");
        assert_eq!(first.llm.model, "");
        assert_eq!(first.llm.base_url, DEFAULT_BASE_URL);
        assert_eq!(
            first.llm.api_key_env.as_deref(),
            Some(GENERATED_API_KEY_ENV)
        );

        let path = config_path(&home);
        let generated = fs::read(&path).expect("generated bytes");
        #[cfg(unix)]
        assert_eq!(
            fs::metadata(&path)
                .expect("config metadata")
                .permissions()
                .mode()
                & 0o777,
            0o600
        );
        #[cfg(unix)]
        assert_eq!(
            fs::metadata(config_dir(&home))
                .expect("Lucy directory metadata")
                .permissions()
                .mode()
                & 0o777,
            0o700
        );
        let custom = b"system_prompt = \"custom\"\n[llm]\nmodel = \"local\"\n";
        fs::write(&path, custom).expect("custom config");
        let loaded = Config::load_or_create(&home).expect("load custom config");
        assert_eq!(loaded.system_prompt, "custom");
        assert_eq!(loaded.llm.model, "local");
        assert_ne!(generated, custom);
        assert_eq!(fs::read(path).expect("bytes after load"), custom);

        fs::remove_dir_all(home).expect("remove temp home");
    }

    #[cfg(unix)]
    #[test]
    fn rejects_symlinked_config_files_and_directories() {
        let home = temporary_home();
        let lucy = config_dir(&home);
        fs::create_dir_all(&lucy).expect("Lucy directory");
        let target = home.join("config-target.toml");
        fs::write(&target, "system_prompt = \"target\"\n").expect("target config");
        let path = config_path(&home);
        symlink(&target, &path).expect("config symlink");
        assert!(Config::load_or_create(&home).is_err());
        assert!(Config::load_from_path(&path).is_err());
        fs::remove_file(path).expect("remove config symlink");
        fs::remove_dir(lucy).expect("remove Lucy directory");
        fs::remove_file(target).expect("remove target config");
        fs::remove_dir_all(&home).expect("remove temp home");

        let home = temporary_home();
        let target = home.join("lucy-target");
        fs::create_dir(&target).expect("target directory");
        fs::create_dir_all(config_dir(&home).parent().expect("config parent"))
            .expect("config parent");
        symlink(&target, config_dir(&home)).expect("Lucy directory symlink");
        assert!(Config::ensure_exists(&home).is_err());
        fs::remove_file(config_dir(&home)).expect("remove Lucy directory symlink");
        fs::remove_dir(target).expect("remove target directory");
        fs::remove_dir_all(home).expect("remove temp home");
    }

    #[test]
    fn migrates_a_legacy_config_once_without_overwriting_xdg_config() {
        let home = temporary_home();
        let legacy = legacy_config_path(&home);
        fs::create_dir_all(legacy.parent().expect("legacy parent")).expect("legacy parent");
        let legacy_bytes = b"system_prompt = \"legacy\"\n[llm]\nmodel = \"old-model\"\n";
        fs::write(&legacy, legacy_bytes).expect("legacy config");

        let config = Config::load_or_create(&home).expect("migrate legacy config");
        assert_eq!(config.system_prompt, "legacy");
        assert_eq!(
            fs::read(config_path(&home)).expect("migrated bytes"),
            legacy_bytes
        );
        assert!(!legacy.exists());

        fs::create_dir_all(legacy.parent().expect("legacy parent")).expect("legacy parent");
        fs::write(&legacy, b"system_prompt = \"stale\"\n").expect("stale legacy config");
        let loaded = Config::load_or_create(&home).expect("retain XDG config");
        assert_eq!(loaded.system_prompt, "legacy");
        assert_eq!(
            fs::read(config_path(&home)).expect("XDG bytes"),
            legacy_bytes
        );
        assert_eq!(
            fs::read(&legacy).expect("legacy bytes"),
            b"system_prompt = \"stale\"\n"
        );

        fs::remove_dir_all(home).expect("remove temp home");
    }

    #[test]
    fn config_dir_uses_absolute_xdg_home_and_defaults_otherwise() {
        let home = temporary_home();
        let xdg_home = home.join("custom-xdg");
        assert_eq!(
            config_dir_from_xdg_home(&home, Some(xdg_home.as_os_str())),
            xdg_home.join("lucy")
        );
        assert_eq!(
            config_dir_from_xdg_home(&home, None),
            home.join(".config/lucy")
        );
        assert_eq!(
            config_dir_from_xdg_home(&home, Some(std::ffi::OsStr::new("relative"))),
            home.join(".config/lucy")
        );
        fs::remove_dir_all(home).expect("remove temp home");
    }

    #[test]
    fn malformed_toml_error_does_not_include_source_details() {
        let home = temporary_home();
        let path = config_path(&home);
        fs::create_dir_all(path.parent().expect("config parent")).expect("config parent");
        fs::write(
            &path,
            "system_prompt = \"provider-secret\n[llm]\nmodel = [\n",
        )
        .expect("malformed config");

        let error = Config::load_from_path(&path).expect_err("malformed TOML");
        let message = error.to_string();
        assert!(message.contains("invalid TOML"));
        assert!(!message.contains("provider-secret"));
        assert!(!message.contains("system_prompt"));
        assert!(!message.contains(&path.display().to_string()));
        fs::remove_dir_all(home).expect("remove temp home");
    }

    #[test]
    fn omitted_api_key_environment_uses_openai_default() {
        let config = Config {
            system_prompt: "prompt".to_owned(),
            llm: LlmConfig {
                base_url: "http://localhost".to_owned(),
                model: "model".to_owned(),
                api_key_env: None,
                effort: None,
            },
        };
        assert_eq!(
            config.resolved_llm().expect("settings").api_key_env,
            DEFAULT_API_KEY_ENV
        );
    }

    #[test]
    fn resolved_effort_passes_through_and_trims() {
        let config = |effort: Option<&str>| Config {
            system_prompt: "prompt".to_owned(),
            llm: LlmConfig {
                base_url: "http://localhost".to_owned(),
                model: "model".to_owned(),
                api_key_env: Some("LUCY_KEY".to_owned()),
                effort: effort.map(str::to_owned),
            },
        };
        assert_eq!(config(None).resolved_llm().expect("none").effort, None);
        assert_eq!(
            config(Some("high"))
                .resolved_llm()
                .expect("set")
                .effort
                .as_deref(),
            Some("high")
        );
        assert_eq!(
            config(Some("  medium  "))
                .resolved_llm()
                .expect("trim")
                .effort
                .as_deref(),
            Some("medium")
        );
    }
}