lific 2.4.0

Local-first, lightweight issue tracker. Single binary, SQLite-backed, MCP-native.
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
//! Format-native, non-destructive config writers for `lific connect`.
//!
//! The cardinal rule: **never destroy a user's config.** Each writer reads the
//! existing file (if any), inserts or replaces only Lific's own entry under the
//! client's top-level key, and writes the rest back untouched. If a file exists
//! but doesn't parse (e.g. JSONC with comments, which OpenCode/Crush/Zed allow),
//! we refuse to modify it and hand back the snippet to merge by hand.
//!
//! JSON: `serde_json` round-trip, pretty-printed with a trailing newline.
//! TOML: `toml_edit` document round-trip — preserves the user's existing
//!       formatting and comments, setting only `[mcp_servers.lific]`.
//! YAML: `serde_yaml` round-trip (YAML comments are lost — this is called out
//!       in the per-client notes surfaced to the user).

use std::path::Path;

use super::clients::{CompiledEntry, Format};

/// What a write did.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
    /// The file did not exist and was created.
    Created,
    /// The file existed and Lific's entry was inserted or replaced in place.
    Updated,
}

impl Action {
    pub fn as_str(self) -> &'static str {
        match self {
            Action::Created => "created",
            Action::Updated => "updated",
        }
    }
}

/// The result of a successful compile-to-text step (used by `--dry-run`, which
/// renders the *whole* file that would be written without touching disk).
pub struct Rendered {
    pub contents: String,
    pub action: Action,
}

/// Error from a writer that a caller should surface as a per-client failure
/// (and keep going with the other clients).
#[derive(Debug)]
pub struct WriteError {
    pub message: String,
    /// A snippet the user can paste to merge manually, when we refused to touch
    /// an unparseable file.
    pub manual_snippet: Option<String>,
}

impl WriteError {
    fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            manual_snippet: None,
        }
    }

    fn with_snippet(message: impl Into<String>, snippet: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            manual_snippet: Some(snippet.into()),
        }
    }
}

impl std::fmt::Display for WriteError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

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

/// Render the full file contents that *would* be written for `entry` merged
/// into whatever currently exists at `path`, without writing anything. Used by
/// both `--dry-run` and the real write path (which then just writes the result).
pub fn render(path: &Path, format: Format, entry: &CompiledEntry) -> Result<Rendered, WriteError> {
    let existing = read_existing(path)?;
    let action = if existing.is_some() {
        Action::Updated
    } else {
        Action::Created
    };
    let existing_str = existing.as_deref().unwrap_or("");
    let contents = match format {
        Format::Json => render_json(existing_str, entry)?,
        Format::Toml => render_toml(existing_str, entry)?,
        Format::Yaml => render_yaml(existing_str, entry)?,
    };
    Ok(Rendered { contents, action })
}

/// Merge `entry` into the config at `path` and write it back, creating parent
/// directories as needed. Returns whether the file was created or updated.
pub fn write(path: &Path, format: Format, entry: &CompiledEntry) -> Result<Action, WriteError> {
    let rendered = render(path, format, entry)?;
    if let Some(parent) = path.parent()
        && !parent.as_os_str().is_empty()
    {
        std::fs::create_dir_all(parent)
            .map_err(|e| WriteError::new(format!("failed to create {}: {e}", parent.display())))?;
    }
    std::fs::write(path, &rendered.contents)
        .map_err(|e| WriteError::new(format!("failed to write {}: {e}", path.display())))?;
    Ok(rendered.action)
}

/// Read the file if present. `Ok(None)` = doesn't exist. An unreadable file is
/// an error (permissions, etc.).
fn read_existing(path: &Path) -> Result<Option<String>, WriteError> {
    match std::fs::read_to_string(path) {
        Ok(s) => Ok(Some(s)),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(e) => Err(WriteError::new(format!(
            "failed to read {}: {e}",
            path.display()
        ))),
    }
}

// ── JSON ─────────────────────────────────────────────────────

fn render_json(existing: &str, entry: &CompiledEntry) -> Result<String, WriteError> {
    // `existing` is "" when the file is absent; treat that as an empty object.
    let mut root: serde_json::Value = if existing.trim().is_empty() {
        serde_json::json!({})
    } else {
        serde_json::from_str(existing).map_err(|e| {
            WriteError::with_snippet(
                format!(
                    "existing config is not valid JSON ({e}); not modifying it. Merge this in by hand:"
                ),
                manual_json_snippet(entry),
            )
        })?
    };

    let serde_json::Value::Object(root_map) = &mut root else {
        return Err(WriteError::with_snippet(
            "existing config is not a JSON object; not modifying it. Merge this in by hand:",
            manual_json_snippet(entry),
        ));
    };

    // Get-or-create the top-level object (mcpServers / servers / mcp / ...).
    let top = root_map
        .entry(entry.top_key.clone())
        .or_insert_with(|| serde_json::json!({}));
    let serde_json::Value::Object(top_map) = top else {
        return Err(WriteError::with_snippet(
            format!(
                "existing `{}` is not an object; not modifying it. Merge this in by hand:",
                entry.top_key
            ),
            manual_json_snippet(entry),
        ));
    };

    // Insert/replace only our own server entry, preserving siblings.
    top_map.insert(entry.name.clone(), entry.value.clone());

    let mut out = serde_json::to_string_pretty(&root)
        .map_err(|e| WriteError::new(format!("failed to serialize JSON: {e}")))?;
    out.push('\n');
    Ok(out)
}

/// The minimal `{ "<top_key>": { "<name>": <value> } }` snippet for manual merge.
fn manual_json_snippet(entry: &CompiledEntry) -> String {
    let snippet = serde_json::json!({
        entry.top_key.clone(): { entry.name.clone(): entry.value.clone() }
    });
    serde_json::to_string_pretty(&snippet).unwrap_or_default()
}

// ── TOML (Codex) ─────────────────────────────────────────────

fn render_toml(existing: &str, entry: &CompiledEntry) -> Result<String, WriteError> {
    use toml_edit::{DocumentMut, Item, Table};

    let mut doc: DocumentMut = if existing.trim().is_empty() {
        DocumentMut::new()
    } else {
        existing.parse::<DocumentMut>().map_err(|e| {
            WriteError::with_snippet(
                format!("existing config.toml does not parse ({e}); not modifying it. Merge by hand:"),
                manual_toml_snippet(entry),
            )
        })?
    };

    // entry.top_key is the dotted table path, e.g. "mcp_servers.lific".
    let parts: Vec<&str> = entry.top_key.split('.').collect();

    // Build our leaf table from the compiled JSON object.
    let mut leaf = Table::new();
    let obj = entry
        .value
        .as_object()
        .ok_or_else(|| WriteError::new("internal: codex compiled value must be an object"))?;
    for (k, v) in obj {
        leaf.insert(k, json_to_toml_value(v)?);
    }

    // Descend/create the parent tables, marking intermediate tables implicit so
    // they render as `[mcp_servers.lific]` rather than an empty `[mcp_servers]`.
    let mut current = doc.as_table_mut();
    let last = parts.len() - 1;
    for part in &parts[..last] {
        let sub = current.entry(part).or_insert_with(|| {
            let mut t = Table::new();
            t.set_implicit(true);
            Item::Table(t)
        });
        current = sub.as_table_mut().ok_or_else(|| {
            WriteError::with_snippet(
                format!("existing `{part}` in config.toml is not a table; not modifying it. Merge by hand:"),
                manual_toml_snippet(entry),
            )
        })?;
    }
    current.insert(parts[last], Item::Table(leaf));

    Ok(doc.to_string())
}

/// Convert a JSON scalar/array (the compiled entry's shape) into a toml_edit
/// value. Codex entries only ever contain strings and string arrays.
fn json_to_toml_value(v: &serde_json::Value) -> Result<toml_edit::Item, WriteError> {
    use toml_edit::{Array, Item, Value, value};
    match v {
        serde_json::Value::String(s) => Ok(value(s.as_str())),
        serde_json::Value::Bool(b) => Ok(value(*b)),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Ok(value(i))
            } else if let Some(f) = n.as_f64() {
                Ok(value(f))
            } else {
                Err(WriteError::new("unsupported TOML number"))
            }
        }
        serde_json::Value::Array(arr) => {
            let mut a = Array::new();
            for item in arr {
                match item {
                    serde_json::Value::String(s) => a.push(s.as_str()),
                    serde_json::Value::Bool(b) => a.push(*b),
                    other => {
                        return Err(WriteError::new(format!(
                            "unsupported TOML array element: {other}"
                        )));
                    }
                }
            }
            Ok(Item::Value(Value::Array(a)))
        }
        other => Err(WriteError::new(format!("unsupported TOML value: {other}"))),
    }
}

fn manual_toml_snippet(entry: &CompiledEntry) -> String {
    use toml_edit::{DocumentMut, Item, Table};
    // Render a standalone document containing only our table.
    let mut doc = DocumentMut::new();
    let parts: Vec<&str> = entry.top_key.split('.').collect();

    let mut leaf = Table::new();
    if let Some(obj) = entry.value.as_object() {
        for (k, v) in obj {
            if let Ok(item) = json_to_toml_value(v) {
                leaf.insert(k, item);
            }
        }
    }

    let mut current = doc.as_table_mut();
    let last = parts.len() - 1;
    for part in &parts[..last] {
        let sub = current.entry(part).or_insert_with(|| {
            let mut t = Table::new();
            t.set_implicit(true);
            Item::Table(t)
        });
        match sub.as_table_mut() {
            Some(t) => current = t,
            None => return doc.to_string(),
        }
    }
    current.insert(parts[last], Item::Table(leaf));
    doc.to_string()
}

// ── YAML (Goose) ─────────────────────────────────────────────

fn render_yaml(existing: &str, entry: &CompiledEntry) -> Result<String, WriteError> {
    let mut root: serde_yaml::Value = if existing.trim().is_empty() {
        serde_yaml::Value::Mapping(serde_yaml::Mapping::new())
    } else {
        serde_yaml::from_str(existing).map_err(|e| {
            WriteError::with_snippet(
                format!("existing config.yaml does not parse ({e}); not modifying it. Merge by hand:"),
                manual_yaml_snippet(entry),
            )
        })?
    };

    let serde_yaml::Value::Mapping(root_map) = &mut root else {
        return Err(WriteError::with_snippet(
            "existing config.yaml is not a mapping; not modifying it. Merge by hand:",
            manual_yaml_snippet(entry),
        ));
    };

    let top_key = serde_yaml::Value::String(entry.top_key.clone());
    let top = root_map
        .entry(top_key)
        .or_insert_with(|| serde_yaml::Value::Mapping(serde_yaml::Mapping::new()));
    let serde_yaml::Value::Mapping(top_map) = top else {
        return Err(WriteError::with_snippet(
            format!(
                "existing `{}` is not a mapping; not modifying it. Merge by hand:",
                entry.top_key
            ),
            manual_yaml_snippet(entry),
        ));
    };

    let value_yaml: serde_yaml::Value = serde_yaml::to_value(&entry.value)
        .map_err(|e| WriteError::new(format!("failed to convert value to YAML: {e}")))?;
    top_map.insert(serde_yaml::Value::String(entry.name.clone()), value_yaml);

    serde_yaml::to_string(&root)
        .map_err(|e| WriteError::new(format!("failed to serialize YAML: {e}")))
}

fn manual_yaml_snippet(entry: &CompiledEntry) -> String {
    let mut top = serde_yaml::Mapping::new();
    let value_yaml =
        serde_yaml::to_value(&entry.value).unwrap_or(serde_yaml::Value::Null);
    top.insert(serde_yaml::Value::String(entry.name.clone()), value_yaml);
    let mut root = serde_yaml::Mapping::new();
    root.insert(
        serde_yaml::Value::String(entry.top_key.clone()),
        serde_yaml::Value::Mapping(top),
    );
    serde_yaml::to_string(&serde_yaml::Value::Mapping(root)).unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::connect::clients::{ServerConfig, find_client};

    fn remote() -> ServerConfig {
        ServerConfig::remote("http://127.0.0.1:3456/mcp", "lific_sk-live-K")
    }

    fn tmp() -> std::path::PathBuf {
        std::env::temp_dir().join(format!(
            "lific-connect-writer-{}-{}",
            std::process::id(),
            // A per-call nonce so parallel tests don't collide.
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ))
    }

    #[test]
    fn json_creates_file_when_absent() {
        let dir = tmp();
        let path = dir.join("opencode.json");
        let entry = find_client("opencode").unwrap().compile(&remote());
        let action = write(&path, Format::Json, &entry).unwrap();
        assert_eq!(action, Action::Created);

        let written = std::fs::read_to_string(&path).unwrap();
        assert!(written.ends_with('\n'), "must end with a trailing newline");
        let v: serde_json::Value = serde_json::from_str(&written).unwrap();
        assert_eq!(v["mcp"]["lific"]["type"], "remote");
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn json_preserves_sibling_servers_and_unrelated_keys() {
        let dir = tmp();
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("opencode.json");
        // Pre-existing config with another MCP server and unrelated top keys.
        std::fs::write(
            &path,
            serde_json::to_string_pretty(&serde_json::json!({
                "theme": "dark",
                "mcp": {
                    "other": { "type": "remote", "url": "http://other" }
                }
            }))
            .unwrap(),
        )
        .unwrap();

        let entry = find_client("opencode").unwrap().compile(&remote());
        let action = write(&path, Format::Json, &entry).unwrap();
        assert_eq!(action, Action::Updated);

        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        // Unrelated key preserved verbatim.
        assert_eq!(v["theme"], "dark");
        // Sibling server preserved verbatim.
        assert_eq!(v["mcp"]["other"]["url"], "http://other");
        // Our entry added.
        assert_eq!(v["mcp"]["lific"]["url"], "http://127.0.0.1:3456/mcp");
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn json_replaces_existing_lific_entry_not_duplicates() {
        let dir = tmp();
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("opencode.json");
        std::fs::write(
            &path,
            serde_json::to_string_pretty(&serde_json::json!({
                "mcp": { "lific": { "type": "remote", "url": "http://stale" } }
            }))
            .unwrap(),
        )
        .unwrap();

        let entry = find_client("opencode").unwrap().compile(&remote());
        write(&path, Format::Json, &entry).unwrap();

        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(v["mcp"]["lific"]["url"], "http://127.0.0.1:3456/mcp");
        // Exactly one lific key (object semantics guarantee no dup, but assert
        // the map has just the one server we expect).
        assert_eq!(v["mcp"].as_object().unwrap().len(), 1);
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn json_refuses_to_touch_unparseable_jsonc_and_returns_snippet() {
        let dir = tmp();
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("opencode.json");
        // JSONC with a comment — valid for OpenCode but not strict JSON.
        let original = "{\n  // my config\n  \"mcp\": {}\n}\n";
        std::fs::write(&path, original).unwrap();

        let entry = find_client("opencode").unwrap().compile(&remote());
        let err = write(&path, Format::Json, &entry).unwrap_err();
        assert!(err.manual_snippet.is_some(), "must hand back a snippet");
        let snippet = err.manual_snippet.unwrap();
        assert!(snippet.contains("lific"));
        // File must be byte-for-byte unchanged.
        assert_eq!(std::fs::read_to_string(&path).unwrap(), original);
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn toml_sets_only_our_table_and_preserves_comments() {
        let dir = tmp();
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("config.toml");
        let original = "# Codex config\nmodel = \"gpt-5\"\n\n[mcp_servers.other]\nurl = \"http://other\"\n";
        std::fs::write(&path, original).unwrap();

        let entry = find_client("codex").unwrap().compile(&remote());
        let action = write(&path, Format::Toml, &entry).unwrap();
        assert_eq!(action, Action::Updated);

        let written = std::fs::read_to_string(&path).unwrap();
        // Comment preserved.
        assert!(written.contains("# Codex config"), "comment must survive: {written}");
        // Unrelated top-level key preserved.
        assert!(written.contains("model = \"gpt-5\""));
        // Sibling server preserved.
        assert!(written.contains("[mcp_servers.other]"));
        // Our table added with the env-var token and no inline key.
        let doc: toml_edit::DocumentMut = written.parse().unwrap();
        assert_eq!(
            doc["mcp_servers"]["lific"]["url"].as_str(),
            Some("http://127.0.0.1:3456/mcp")
        );
        assert_eq!(
            doc["mcp_servers"]["lific"]["bearer_token_env_var"].as_str(),
            Some("LIFIC_API_KEY")
        );
        assert!(!written.contains("lific_sk-live-K"), "must not inline the key");
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn toml_creates_fresh_when_absent() {
        let dir = tmp();
        let path = dir.join("config.toml");
        let entry = find_client("codex").unwrap().compile(&remote());
        let action = write(&path, Format::Toml, &entry).unwrap();
        assert_eq!(action, Action::Created);
        let doc: toml_edit::DocumentMut =
            std::fs::read_to_string(&path).unwrap().parse().unwrap();
        assert_eq!(
            doc["mcp_servers"]["lific"]["bearer_token_env_var"].as_str(),
            Some("LIFIC_API_KEY")
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn toml_refuses_unparseable_and_returns_snippet() {
        let dir = tmp();
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("config.toml");
        let original = "this is = = not valid toml [[[\n";
        std::fs::write(&path, original).unwrap();
        let entry = find_client("codex").unwrap().compile(&remote());
        let err = write(&path, Format::Toml, &entry).unwrap_err();
        assert!(err.manual_snippet.is_some());
        assert_eq!(std::fs::read_to_string(&path).unwrap(), original);
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn yaml_sets_extensions_lific_and_preserves_other_extensions() {
        let dir = tmp();
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("config.yaml");
        std::fs::write(
            &path,
            "GOOSE_MODEL: gpt-5\nextensions:\n  other:\n    type: stdio\n    cmd: foo\n",
        )
        .unwrap();

        let entry = find_client("goose").unwrap().compile(&remote());
        let action = write(&path, Format::Yaml, &entry).unwrap();
        assert_eq!(action, Action::Updated);

        let v: serde_yaml::Value =
            serde_yaml::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        // Unrelated top key preserved.
        assert_eq!(v["GOOSE_MODEL"].as_str(), Some("gpt-5"));
        // Sibling extension preserved.
        assert_eq!(v["extensions"]["other"]["cmd"].as_str(), Some("foo"));
        // Ours added.
        assert_eq!(
            v["extensions"]["lific"]["type"].as_str(),
            Some("streamable_http")
        );
        assert_eq!(v["extensions"]["lific"]["uri"].as_str(), Some("http://127.0.0.1:3456/mcp"));
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn render_does_not_write_to_disk() {
        let dir = tmp();
        let path = dir.join("opencode.json");
        let entry = find_client("opencode").unwrap().compile(&remote());
        let rendered = render(&path, Format::Json, &entry).unwrap();
        assert_eq!(rendered.action, Action::Created);
        assert!(rendered.contents.contains("lific"));
        // Nothing was written.
        assert!(!path.exists());
    }
}