lean-ctx-engine 3.12.1

LeanCTX | OSS library: the open-source runtime for the Context Gateway for AI agents behind the lean-ctx CLI, MCP server and proxy integrations.
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
//! Generic config setter that works for ALL schema-known keys.
//!
//! Instead of a hardcoded match arm per key, this module:
//! 1. Validates the key against `ConfigSchema`
//! 2. Parses the value according to the schema type
//! 3. Performs a TOML round-trip to set the value
//! 4. Deserializes back into `Config` for full serde validation

use super::Config;
use super::schema::{ConfigSchema, KeySchema};

/// Attempts to set a config key generically via schema-validated TOML round-trip.
///
/// Returns the updated `Config` on success, or a user-friendly error message.
pub fn set_by_key(key: &str, value: &str) -> Result<Config, crate::core::error::ConfigError> {
    set_many_by_key(&[(key, value)])
}

/// Atomically validates and persists several schema-known keys.
///
/// The config file is written only after every key and value has passed schema
/// validation and the resulting document deserializes into [`Config`].
///
/// The read, the merge, and the write all happen under the one config write
/// lock (LR-TEL-01). Previously the on-disk table was read unlocked and saved
/// later, so a concurrent writer could commit between the two and have its
/// value silently overwritten by this document.
pub fn set_many_by_key(
    updates: &[(&str, &str)],
) -> Result<Config, crate::core::error::ConfigError> {
    let schema = ConfigSchema::generate();
    let path = Config::path().ok_or(crate::core::error::ConfigError::MissingPath)?;
    let guard = crate::config_io::ConfigWriteGuard::acquire(&path).map_err(|message| {
        crate::core::error::ConfigError::Save {
            source: Box::new(crate::core::error::LeanCtxError::Config(message)),
        }
    })?;
    // Read the same resolved file the guard excludes writes to.
    let mut table = load_config_as_table_at(guard.path())?;
    for (key, value) in updates {
        let key_schema =
            schema
                .lookup(key)
                .ok_or_else(|| crate::core::error::ConfigError::UnknownKey {
                    key: (*key).to_string(),
                })?;
        let toml_value = parse_value(value, key_schema)?;
        set_nested(&mut table, key, toml_value)?;
    }
    record_telemetry_choice(&mut table, updates)?;

    let cfg: Config = toml::Value::Table(table)
        .try_into()
        .map_err(
            |e: toml::de::Error| crate::core::error::ConfigError::InvalidValue {
                key: updates
                    .iter()
                    .map(|(key, _)| *key)
                    .collect::<Vec<_>>()
                    .join(","),
                message: e.to_string(),
            },
        )?;
    // Already holding the guard: write through it so the non-reentrant lock is
    // not acquired a second time on this path.
    cfg.save_to_locked(&guard)
        .map_err(|e| crate::core::error::ConfigError::Save {
            source: Box::new(e),
        })?;
    // K2 review 3 (NEW-2): a Hive off switch stops local recording at
    // once, not at the scheduler's next tick (or the next restart).
    if updates.iter().any(|(key, _)| key.starts_with("hive.")) {
        super::hive::apply_off_switches(&cfg.hive);
    }
    Ok(cfg)
}

/// Returns the current value of `key` from the on-disk `config.toml`, rendered
/// as the string a user would type (no TOML quoting), or `None` if the key is
/// unset (still at its schema default).
///
/// Powers the before→after review for consequential `config set` writes (#852).
/// Reads only the user's file, so an unset key is reported as `None` rather than
/// the default — the review then shows `(default) → <new>`.
#[must_use]
pub fn current_value(key: &str) -> Option<String> {
    let table = load_config_as_table().ok()?;
    let parts: Vec<&str> = key.split('.').collect();
    let (parents, leaf) = parts.split_at(parts.len() - 1);

    let mut current = &table;
    for part in parents {
        current = current.get(*part)?.as_table()?;
    }
    current.get(leaf[0]).map(display_toml_value)
}

/// Appends the new `entries` to the string array `key` of the GLOBAL config,
/// through the schema-validated setter. Returns the entries that were new.
pub(crate) fn add_to_global_list(key: &str, entries: &[String]) -> Result<Vec<String>, String> {
    let mut list = super::global_string_list(key);
    let added: Vec<String> = entries
        .iter()
        .filter(|p| !p.is_empty() && !list.contains(p))
        .cloned()
        .collect();
    if added.is_empty() {
        return Ok(added);
    }
    list.extend(added.iter().cloned());
    set_by_key(key, &list.join(","))
        .map(|_| ())
        .map_err(|e| e.to_string())?;
    Ok(added)
}

/// Renders a scalar `toml::Value` the way a user types it on the CLI: strings
/// without quotes, arrays comma-joined, everything else via its TOML form.
fn display_toml_value(value: &toml::Value) -> String {
    match value {
        toml::Value::String(s) => s.clone(),
        toml::Value::Array(items) => items
            .iter()
            .map(display_toml_value)
            .collect::<Vec<_>>()
            .join(", "),
        other => other.to_string(),
    }
}

fn load_config_as_table() -> Result<toml::Table, crate::core::error::ConfigError> {
    let path = Config::path().ok_or(crate::core::error::ConfigError::MissingPath)?;
    load_config_as_table_at(&path)
}

/// Path-parameterized core of [`load_config_as_table`], so a caller that has
/// already resolved and locked the config path reads exactly that file.
fn load_config_as_table_at(
    path: &std::path::Path,
) -> Result<toml::Table, crate::core::error::ConfigError> {
    if !path.exists() {
        return Ok(toml::Table::new());
    }
    let raw = std::fs::read_to_string(path)
        .map_err(|source| crate::core::error::ConfigError::Read { source })?;
    raw.parse::<toml::Table>()
        .map_err(|source| crate::core::error::ConfigError::ParseToml { source })
}
fn parse_value(
    value: &str,
    schema: &KeySchema,
) -> Result<toml::Value, crate::core::error::ConfigError> {
    match schema.ty.as_str() {
        "bool" | "bool?" => match value {
            "true" | "1" | "yes" => Ok(toml::Value::Boolean(true)),
            "false" | "0" | "no" => Ok(toml::Value::Boolean(false)),
            _ => Err(crate::core::error::ConfigError::ExpectedBool {
                value: value.to_string(),
            }),
        },
        "u8" | "u16" | "u32" | "u64" | "usize" | "u64?" => {
            let n: i64 =
                value
                    .parse()
                    .map_err(|_| crate::core::error::ConfigError::ExpectedInteger {
                        value: value.to_string(),
                    })?;
            if n < 0 {
                return Err(crate::core::error::ConfigError::ExpectedUnsignedInteger {
                    value: value.to_string(),
                });
            }
            Ok(toml::Value::Integer(n))
        }
        "f32" | "f64" => {
            let n: f64 =
                value
                    .parse()
                    .map_err(|_| crate::core::error::ConfigError::ExpectedNumber {
                        value: value.to_string(),
                    })?;
            Ok(toml::Value::Float(n))
        }
        "string" | "string?" => Ok(toml::Value::String(value.to_string())),
        "enum" => {
            if let Some(ref allowed) = schema.values
                && !allowed.iter().any(|v| v == value)
            {
                return Err(crate::core::error::ConfigError::InvalidEnumValue {
                    value: value.to_string(),
                    allowed: allowed.join(", "),
                });
            }
            Ok(toml::Value::String(value.to_string()))
        }
        "string[]" | "array" => {
            let items: Vec<toml::Value> = value
                .split(',')
                .map(|s| toml::Value::String(s.trim().to_string()))
                .filter(|v| v.as_str() != Some(""))
                .collect();
            Ok(toml::Value::Array(items))
        }
        "table" => Err(crate::core::error::ConfigError::CannotSetTable {
            value: value.to_string(),
        }),
        other => {
            // Fallback: treat as string (covers unknown future types gracefully)
            tracing::debug!("Unknown schema type '{other}', treating value as string");
            Ok(toml::Value::String(value.to_string()))
        }
    }
}

/// A user who sets `telemetry.enabled` by hand (`config set`) made an explicit
/// choice. Record it as one, so upgrades that re-enable the ambiguous legacy
/// `enabled = false` never override an opt-out made after telemetry v2.
fn record_telemetry_choice(
    table: &mut toml::Table,
    updates: &[(&str, &str)],
) -> Result<(), crate::core::error::ConfigError> {
    if updates
        .iter()
        .any(|(key, _)| *key == "telemetry.preference")
    {
        return Ok(());
    }
    let enabled = table
        .get("telemetry")
        .and_then(|telemetry| telemetry.get("enabled"))
        .and_then(toml::Value::as_bool);
    let Some(enabled) =
        enabled.filter(|_| updates.iter().any(|(key, _)| *key == "telemetry.enabled"))
    else {
        return Ok(());
    };
    let preference = if enabled {
        "explicitly_enabled"
    } else {
        "explicitly_disabled"
    };
    set_nested(
        table,
        "telemetry.preference",
        toml::Value::String(preference.to_string()),
    )
}

fn set_nested(
    table: &mut toml::Table,
    key: &str,
    value: toml::Value,
) -> Result<(), crate::core::error::ConfigError> {
    let parts: Vec<&str> = key.split('.').collect();
    let (parents, leaf) = parts.split_at(parts.len() - 1);

    let mut current = table;
    for part in parents {
        current = current
            .entry(*part)
            .or_insert_with(|| toml::Value::Table(toml::Table::new()))
            .as_table_mut()
            .ok_or_else(|| crate::core::error::ConfigError::NonTableParent {
                key: key.to_string(),
                part: (*part).to_string(),
            })?;
    }
    current.insert(leaf[0].to_string(), value);
    Ok(())
}

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

    #[test]
    fn parse_bool_values() {
        let schema = KeySchema {
            ty: "bool".to_string(),
            default: serde_json::json!(false),
            description: String::new(),
            values: None,
            env_override: None,
        };
        assert_eq!(
            parse_value("true", &schema).unwrap(),
            toml::Value::Boolean(true)
        );
        assert_eq!(
            parse_value("false", &schema).unwrap(),
            toml::Value::Boolean(false)
        );
        assert!(parse_value("maybe", &schema).is_err());
    }

    #[test]
    fn parse_integer_values() {
        let schema = KeySchema {
            ty: "u32".to_string(),
            default: serde_json::json!(0),
            description: String::new(),
            values: None,
            env_override: None,
        };
        assert_eq!(
            parse_value("42", &schema).unwrap(),
            toml::Value::Integer(42)
        );
        assert!(parse_value("-1", &schema).is_err());
        assert!(parse_value("abc", &schema).is_err());
    }

    #[test]
    fn parse_enum_validates_allowed() {
        let schema = KeySchema {
            ty: "enum".to_string(),
            default: serde_json::json!("off"),
            description: String::new(),
            values: Some(vec!["off".into(), "lite".into(), "full".into()]),
            env_override: None,
        };
        assert_eq!(
            parse_value("lite", &schema).unwrap(),
            toml::Value::String("lite".into())
        );
        assert!(parse_value("invalid", &schema).is_err());
    }

    #[test]
    fn parse_string_array() {
        let schema = KeySchema {
            ty: "string[]".to_string(),
            default: serde_json::json!([]),
            description: String::new(),
            values: None,
            env_override: None,
        };
        let result = parse_value("a, b, c", &schema).unwrap();
        let arr = result.as_array().unwrap();
        assert_eq!(arr.len(), 3);
        assert_eq!(arr[0].as_str().unwrap(), "a");
        assert_eq!(arr[2].as_str().unwrap(), "c");
    }

    #[test]
    fn set_nested_creates_intermediate_tables() {
        let mut table = toml::Table::new();
        set_nested(
            &mut table,
            "proxy.anthropic_upstream",
            toml::Value::String("https://example.com".into()),
        )
        .unwrap();
        let proxy = table["proxy"].as_table().unwrap();
        assert_eq!(
            proxy["anthropic_upstream"].as_str().unwrap(),
            "https://example.com"
        );
    }

    #[test]
    fn decision_loop_keys_round_trip_through_cli_setter() {
        let schema = ConfigSchema::generate();
        let mut table = toml::Table::new();
        for (key, value) in [
            ("decision_loop.enabled", "false"),
            ("decision_loop.max_filter_level", "0"),
        ] {
            let key_schema = schema.lookup(key).expect("decision-loop key in schema");
            set_nested(&mut table, key, parse_value(value, key_schema).unwrap()).unwrap();
        }

        let cfg: Config = toml::Value::Table(table).try_into().unwrap();
        assert!(!cfg.decision_loop.enabled);
        assert_eq!(cfg.decision_loop.max_filter_level, 0);
    }

    #[test]
    fn manual_telemetry_switch_is_recorded_as_explicit_choice() {
        for (value, preference) in [
            ("false", "explicitly_disabled"),
            ("true", "explicitly_enabled"),
        ] {
            let mut table = toml::Table::new();
            set_nested(
                &mut table,
                "telemetry.enabled",
                toml::Value::Boolean(value == "true"),
            )
            .unwrap();
            record_telemetry_choice(&mut table, &[("telemetry.enabled", value)]).unwrap();
            assert_eq!(table["telemetry"]["preference"].as_str(), Some(preference));
        }

        // An explicit preference in the same write wins; unrelated writes add nothing.
        let mut table = toml::Table::new();
        set_nested(&mut table, "telemetry.enabled", toml::Value::Boolean(false)).unwrap();
        record_telemetry_choice(&mut table, &[("profile", "coder")]).unwrap();
        assert!(table["telemetry"].get("preference").is_none());
        set_nested(
            &mut table,
            "telemetry.preference",
            toml::Value::String("default_on".into()),
        )
        .unwrap();
        record_telemetry_choice(
            &mut table,
            &[
                ("telemetry.enabled", "false"),
                ("telemetry.preference", "default_on"),
            ],
        )
        .unwrap();
        assert_eq!(
            table["telemetry"]["preference"].as_str(),
            Some("default_on")
        );
    }

    #[test]
    fn set_nested_flat_key() {
        let mut table = toml::Table::new();
        set_nested(&mut table, "ultra_compact", toml::Value::Boolean(true)).unwrap();
        assert!(table["ultra_compact"].as_bool().unwrap());
    }

    #[test]
    fn set_nested_rejects_non_table_intermediate() {
        let mut table = toml::Table::new();
        table.insert("proxy".into(), toml::Value::String("oops".into()));
        let err = set_nested(&mut table, "proxy.port", toml::Value::Integer(8080)).unwrap_err();
        assert!(err.to_string().contains("non-table"), "got: {err}");
    }

    #[test]
    fn display_toml_value_renders_user_facing_form() {
        // Strings drop their quotes (what a user would type on the CLI).
        assert_eq!(
            display_toml_value(&toml::Value::String("enforce".into())),
            "enforce"
        );
        assert_eq!(display_toml_value(&toml::Value::Boolean(false)), "false");
        assert_eq!(display_toml_value(&toml::Value::Integer(8080)), "8080");
        assert_eq!(
            display_toml_value(&toml::Value::Array(vec![
                toml::Value::String("a".into()),
                toml::Value::String("b".into()),
            ])),
            "a, b"
        );
    }
}