fur-cli 2.1.0

Turn your AI chats into a durable, local-first diary. Save messages, attach notes, organize conversations, and stop losing context every time the model forgets you exist.
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
//! Conversation-level lineage: `fur link` / `fur unlink`.
//!
//! Edges are stored in `.fur/threads/<id>.json` under `parents` / `children`
//! and carried out to `chats/<slug>/convo.md` by the usual sync. `.fur/` is
//! written first and remains authoritative; the document is regenerated from
//! it, exactly as `jot` does.
//!
//! # Reciprocity
//!
//! `parents` and `children` are *assertions*, not two halves of one record.
//! When both conversations are local, linking writes both ends. When the other
//! end is not local — imported from a registry, or simply not pulled yet — only
//! this end is written, and that is a complete, valid state rather than a
//! failure. `fur doctor` reports asymmetry; it does not treat it as damage.

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

use clap::Parser;
use colored::*;
use serde_json::{json, Value};

use crate::schema::bridge::sync_conversation;

#[derive(Parser, Debug)]
pub struct LinkArgs {
    /// Conversation to modify (id or unique prefix). Defaults to the active one.
    pub id: Option<String>,

    /// Conversation this one derives from
    #[arg(long, conflicts_with = "child")]
    pub parent: Option<String>,

    /// Conversation that derives from this one
    #[arg(long)]
    pub child: Option<String>,
}

/// Which side of the edge the user named.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Edge {
    Parent,
    Child,
}

impl Edge {
    /// Field on the conversation being modified.
    fn field(self) -> &'static str {
        match self {
            Edge::Parent => "parents",
            Edge::Child => "children",
        }
    }

    /// Field on the conversation at the far end of the edge.
    fn inverse_field(self) -> &'static str {
        match self {
            Edge::Parent => "children",
            Edge::Child => "parents",
        }
    }

    fn describe(self) -> &'static str {
        match self {
            Edge::Parent => "parent",
            Edge::Child => "child",
        }
    }
}

/// What a single-sided edit actually did.
#[derive(Debug, PartialEq, Eq)]
pub enum Outcome {
    Added,
    Removed,
    AlreadyPresent,
    NotPresent,
}

pub fn run_link(args: LinkArgs) {
    dispatch(args, false);
}

pub fn run_unlink(args: LinkArgs) {
    dispatch(args, true);
}

fn dispatch(args: LinkArgs, remove: bool) {
    let edge = match (&args.parent, &args.child) {
        (Some(_), None) => Edge::Parent,
        (None, Some(_)) => Edge::Child,
        (None, None) => {
            eprintln!("❌ Specify --parent <id> or --child <id>.");
            return;
        }
        (Some(_), Some(_)) => {
            eprintln!("❌ Pass one of --parent or --child, not both.");
            return;
        }
    };

    let other_ref = args
        .parent
        .as_deref()
        .or(args.child.as_deref())
        .unwrap_or_default();

    match apply(Path::new("."), args.id.as_deref(), other_ref, edge, remove) {
        Ok(report) => report.print(),
        Err(e) => eprintln!("❌ {}", e),
    }
}

#[derive(Debug)]
pub struct LinkReport {
    edge: &'static str,
    target: String,
    target_title: String,
    other: String,
    other_title: Option<String>,
    near: Outcome,
    far: Option<Outcome>,
}

impl LinkReport {
    fn print(&self) {
        let other_label = match &self.other_title {
            Some(title) => format!("{} \"{}\"", short(&self.other), title),
            None => format!("{} {}", short(&self.other), "(no local copy)".bright_black()),
        };

        match self.near {
            Outcome::Added => println!(
                "🔗 {} \"{}\" — {} → {}",
                short(&self.target).bright_yellow(),
                self.target_title,
                self.edge.bright_cyan(),
                other_label
            ),
            Outcome::Removed => println!(
                "✂️  {} \"{}\" — removed {} {}",
                short(&self.target).bright_yellow(),
                self.target_title,
                self.edge.bright_cyan(),
                other_label
            ),
            Outcome::AlreadyPresent => println!(
                "• Already linked: {} is a {} of {}",
                short(&self.other),
                self.edge,
                short(&self.target)
            ),
            Outcome::NotPresent => println!(
                "• Not linked: {} is not a {} of {}",
                short(&self.other),
                self.edge,
                short(&self.target)
            ),
        }

        match &self.far {
            Some(Outcome::Added) | Some(Outcome::Removed) => {
                println!("   {}", "reciprocal edge updated".bright_black())
            }
            None => println!(
                "   {}",
                "one-sided: the other conversation is not in this project".bright_black()
            ),
            _ => {}
        }
    }
}

/// Add or remove one edge, writing both ends when both are local.
///
/// `root` is explicit so this is testable without changing the process
/// directory.
pub fn apply(
    root: &Path,
    target_ref: Option<&str>,
    other_ref: &str,
    edge: Edge,
    remove: bool,
) -> Result<LinkReport, String> {
    let fur_dir = root.join(".fur");
    let index = read_json(&fur_dir.join("index.json"))?;
    let ids = thread_ids(&index);

    let target = match target_ref {
        Some(prefix) => resolve_local(&ids, prefix)?
            .ok_or_else(|| format!("no conversation matches '{}'", prefix))?,
        None => index["active_thread"]
            .as_str()
            .filter(|s| !s.is_empty())
            .ok_or("no active conversation; pass a conversation id")?
            .to_string(),
    };

    let (other, other_is_local) = match resolve_local(&ids, other_ref)? {
        Some(id) => (id, true),
        None if looks_like_conversation_id(other_ref) => (other_ref.to_string(), false),
        None => {
            return Err(format!(
                "no conversation matches '{}'; pass a full conversation id to link one that is not in this project",
                other_ref
            ))
        }
    };

    if target == other {
        return Err(format!(
            "{} \"{}\" cannot be its own parent or child — name the other conversation explicitly, e.g. `fur link <id> --child {}`",
            short(&target),
            title_of(&fur_dir, &target).unwrap_or_else(|| "Untitled".to_string()),
            short(&other)
        ));
    }

    // A loop can only be *created* locally, so it is refused here rather than in
    // the format. An archive can still contain one after importing two halves
    // published independently, which is why every walk keeps a visited set.
    if !remove {
        let (parent, child) = match edge {
            Edge::Parent => (other.as_str(), target.as_str()),
            Edge::Child => (target.as_str(), other.as_str()),
        };

        if let Ok(lineage) = crate::schema::lineage::Lineage::load(&fur_dir) {
            if lineage.would_cycle(parent, child) {
                return Err(format!(
                    "that would loop: {} already leads back to {}",
                    short(child),
                    short(parent)
                ));
            }
        }
    }

    let near = edit_edge(&fur_dir, &target, edge.field(), &other, remove)?;
    let far = if other_is_local {
        Some(edit_edge(
            &fur_dir,
            &other,
            edge.inverse_field(),
            &target,
            remove,
        )?)
    } else {
        None
    };

    // Regenerate only the documents that actually changed.
    if matches!(near, Outcome::Added | Outcome::Removed) {
        sync_conversation(root, &fur_dir, &target)?;
    }
    if matches!(far, Some(Outcome::Added) | Some(Outcome::Removed)) {
        sync_conversation(root, &fur_dir, &other)?;
    }

    Ok(LinkReport {
        edge: edge.describe(),
        target_title: title_of(&fur_dir, &target).unwrap_or_else(|| "Untitled".to_string()),
        other_title: title_of(&fur_dir, &other),
        target,
        other,
        near,
        far,
    })
}

/// Mutate one conversation's edge list in `.fur/`.
fn edit_edge(
    fur_dir: &Path,
    tid: &str,
    field: &str,
    value: &str,
    remove: bool,
) -> Result<Outcome, String> {
    let path = thread_path(fur_dir, tid);
    let mut convo = read_json(&path)?;

    let mut list: Vec<String> = convo[field]
        .as_array()
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect()
        })
        .unwrap_or_default();

    let present = list.iter().any(|item| item == value);

    if remove {
        if !present {
            return Ok(Outcome::NotPresent);
        }
        list.retain(|item| item != value);
    } else {
        if present {
            return Ok(Outcome::AlreadyPresent);
        }
        list.push(value.to_string());
    }

    // Canonical on disk as well as in the document, so `.fur/` and `convo.md`
    // never disagree about ordering.
    list.sort();
    list.dedup();
    convo[field] = json!(list);

    fs::write(&path, pretty(&convo))
        .map_err(|e| format!("cannot write {}: {}", path.display(), e))?;

    Ok(if remove { Outcome::Removed } else { Outcome::Added })
}

fn thread_ids(index: &Value) -> Vec<String> {
    index["threads"]
        .as_array()
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect()
        })
        .unwrap_or_default()
}

/// Exact match first, then unique prefix — the same rule `fur convo` uses.
fn resolve_local(ids: &[String], needle: &str) -> Result<Option<String>, String> {
    if needle.trim().is_empty() {
        return Err("empty conversation reference".to_string());
    }

    if let Some(hit) = ids.iter().find(|id| id.as_str() == needle) {
        return Ok(Some(hit.clone()));
    }

    let matches: Vec<&String> = ids.iter().filter(|id| id.starts_with(needle)).collect();

    match matches.as_slice() {
        [] => Ok(None),
        [single] => Ok(Some((*single).clone())),
        _ => Err(format!(
            "ambiguous conversation prefix '{}': {:?}",
            needle,
            matches.iter().map(|m| short(m)).collect::<Vec<_>>()
        )),
    }
}

/// A reference to a conversation this project does not hold is only accepted
/// in full, so a typo cannot quietly become a dangling edge.
fn looks_like_conversation_id(raw: &str) -> bool {
    raw.len() >= 32 && raw.chars().all(|c| c.is_ascii_hexdigit() || c == '-')
}

fn title_of(fur_dir: &Path, tid: &str) -> Option<String> {
    let convo = read_json(&thread_path(fur_dir, tid)).ok()?;
    convo["title"].as_str().map(|s| s.to_string())
}

fn thread_path(fur_dir: &Path, tid: &str) -> PathBuf {
    fur_dir.join("threads").join(format!("{}.json", tid))
}

fn read_json(path: &Path) -> Result<Value, String> {
    let content = fs::read_to_string(path)
        .map_err(|e| format!("cannot read {}: {}", path.display(), e))?;
    serde_json::from_str(&content)
        .map_err(|e| format!("invalid JSON in {}: {}", path.display(), e))
}

fn pretty(value: &Value) -> String {
    serde_json::to_string_pretty(value).unwrap_or_else(|_| "{}".to_string())
}

fn short(id: &str) -> &str {
    if id.len() >= 8 {
        &id[..8]
    } else {
        id
    }
}

// --- test seam -------------------------------------------------------------
//
// `Edge` is an implementation detail of argument parsing, but the
// integration test needs to name a direction without going through clap.
//
// The binary compiles the command modules separately from the library, where
// this seam is intentionally unused — the same `#[allow(dead_code)]` situation
// as `registry::install_pull_value`.

#[allow(dead_code)]
pub fn edge_parent() -> Edge {
    Edge::Parent
}

#[allow(dead_code)]
pub fn edge_child() -> Edge {
    Edge::Child
}

#[allow(dead_code)]
impl LinkReport {
    pub fn near_outcome(&self) -> &Outcome {
        &self.near
    }

    pub fn far_outcome(&self) -> Option<&Outcome> {
        self.far.as_ref()
    }
}