fur-cli 2.1.1

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
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
//! Reconstruct `.fur/` from `chats/`.
//!
//! This is the test of whether the architecture actually holds:
//!
//! ```text
//! rm -rf .fur && fur convo
//! ```
//!
//! If the archive comes back, `chats/` has genuinely become the durable format
//! and `.fur/` is disposable operational state.
//!
//! Rebuild is deliberately conservative. It runs when `.fur/` is *absent*. It
//! refuses to run over an existing `.fur/` without `--force`, because a stale
//! or partially-synced `chats/` would otherwise destroy live local state
//! (active conversation, cursor position, avatar emoji) on an ordinary command.

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

use serde_json::{json, Value};

use crate::schema::document::{parse, FurDocument};
use crate::schema::SCHEMA_VERSION;

/// Written by `fur lock`, removed by `fur unlock`, never itself encrypted.
///
/// It lives in `chats/` rather than `.fur/` on purpose: once `.fur/` is
/// disposable, a lock flag stored there could be deleted while the ciphertext
/// remained, leaving rebuild to parse encrypted bytes as front matter.
pub const LOCK_SENTINEL: &str = ".fur-locked";

/// Encrypted password verifier, kept beside the data rather than inside the
/// disposable index.
pub const LOCK_CHECK: &str = ".fur-lockcheck";

/// What a directory looks like before any command touches it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectState {
    /// Neither `.fur/` nor any conversation document.
    Empty,
    /// `.fur/` present and usable.
    Indexed,
    /// Conversation documents exist but `.fur/` does not — a copied archive.
    Unindexed,
    /// `chats/` is encrypted; nothing may be parsed until unlocked.
    Locked,
}

#[derive(Debug, Default)]
pub struct RebuildSummary {
    pub conversations: usize,
    pub messages: usize,
    pub avatars: Vec<String>,
    pub guessed_main: Option<String>,
    pub skipped: Vec<String>,
}

pub fn is_locked_archive(root: &Path) -> bool {
    root.join("chats").join(LOCK_SENTINEL).exists()
}

pub fn detect_state(root: &Path) -> ProjectState {
    if is_locked_archive(root) {
        return ProjectState::Locked;
    }

    if root.join(".fur").join("index.json").exists() {
        return ProjectState::Indexed;
    }

    if spine_paths(root).is_empty() {
        ProjectState::Empty
    } else {
        ProjectState::Unindexed
    }
}

/// Every `chats/<slug>/convo.md` in the project.
///
/// Spines are found by *content*, not by filename: any `.md` directly inside a
/// conversation folder that parses as a document counts. Filenames stay
/// cosmetic so a user can rename freely without breaking rebuild.
pub fn spine_paths(root: &Path) -> Vec<PathBuf> {
    let chats = root.join("chats");

    let Ok(entries) = fs::read_dir(&chats) else {
        return Vec::new();
    };

    let mut out = Vec::new();

    for entry in entries.flatten() {
        let dir = entry.path();
        if !dir.is_dir() {
            continue;
        }

        let Ok(files) = fs::read_dir(&dir) else {
            continue;
        };

        let mut found: Vec<PathBuf> = files
            .flatten()
            .map(|f| f.path())
            .filter(|p| p.is_file())
            .filter(|p| p.extension().and_then(|e| e.to_str()) == Some("md"))
            .filter(|p| looks_like_spine(p))
            .collect();

        found.sort();
        out.append(&mut found);
    }

    out.sort();
    out
}

/// Cheap check: front matter opening plus a conversation id, without paying to
/// parse every long-form document in the folder.
fn looks_like_spine(path: &Path) -> bool {
    let Ok(content) = fs::read_to_string(path) else {
        return false;
    };

    content.starts_with("---\n") && content.contains("\nconversation_id:")
}

/// Rebuild `.fur/` from every conversation document under `chats/`.
pub fn rebuild(root: &Path, force: bool) -> Result<RebuildSummary, String> {
    if is_locked_archive(root) {
        return Err("🔒 Archive is locked. Run `fur unlock` first.".to_string());
    }

    let fur_dir = root.join(".fur");

    if fur_dir.join("index.json").exists() && !force {
        return Err(
            ".fur/ already exists — refusing to overwrite live state (use --force)".to_string(),
        );
    }

    let spines = spine_paths(root);
    if spines.is_empty() {
        return Err("no conversation documents found under chats/".to_string());
    }

    fs::create_dir_all(fur_dir.join("threads"))
        .map_err(|e| format!("cannot create .fur/threads: {}", e))?;
    fs::create_dir_all(fur_dir.join("messages"))
        .map_err(|e| format!("cannot create .fur/messages: {}", e))?;

    let mut summary = RebuildSummary::default();
    let mut thread_ids: Vec<String> = Vec::new();
    let mut avatar_counts: BTreeMap<String, usize> = BTreeMap::new();

    for spine in &spines {
        let text = match fs::read_to_string(spine) {
            Ok(t) => t,
            Err(e) => {
                summary
                    .skipped
                    .push(format!("{} ({})", spine.display(), e));
                continue;
            }
        };

        let doc = match parse(&text) {
            Ok(d) => d,
            Err(e) => {
                // One malformed document costs that document, not the archive.
                summary
                    .skipped
                    .push(format!("{} ({})", spine.display(), e));
                continue;
            }
        };

        let folder = spine.parent().unwrap_or(root);

        for msg in &doc.messages {
            *avatar_counts.entry(msg.avatar.clone()).or_insert(0) += 1;
            write_message(&fur_dir, root, folder, msg)?;
            summary.messages += 1;
        }

        write_thread(&fur_dir, &doc)?;
        thread_ids.push(doc.conversation_id.clone());
        summary.conversations += 1;
    }

    if thread_ids.is_empty() {
        return Err("no conversation documents could be parsed".to_string());
    }

    write_index(&fur_dir, &thread_ids)?;

    summary.avatars = avatar_counts.keys().cloned().collect();
    summary.guessed_main = avatar_counts
        .iter()
        .max_by_key(|(_, count)| **count)
        .map(|(name, _)| name.clone());

    write_avatars(&fur_dir, &summary)?;

    Ok(summary)
}

/// Add one already-validated canonical conversation document to the operational
/// `.fur/` index without rebuilding or disturbing the rest of the project.
///
/// viceroy: this is the narrow import seam used by publication registries. The conversation
/// directory remains authoritative; the JSON written here is only its local
/// projection.
pub fn import_document(
    root: &Path,
    folder: &Path,
    doc: &FurDocument,
) -> Result<(), String> {
    let fur_dir = root.join(".fur");
    let thread_path = fur_dir
        .join("threads")
        .join(format!("{}.json", doc.conversation_id));

    if thread_path.exists() {
        return Err(format!(
            "conversation {} is already indexed",
            doc.conversation_id
        ));
    }

    for msg in &doc.messages {
        let message_path = fur_dir.join("messages").join(format!("{}.json", msg.id));
        if message_path.exists() {
            return Err(format!("message id {} is already indexed", msg.id));
        }
    }

    fs::create_dir_all(fur_dir.join("threads"))
        .map_err(|e| format!("cannot create .fur/threads: {}", e))?;
    fs::create_dir_all(fur_dir.join("messages"))
        .map_err(|e| format!("cannot create .fur/messages: {}", e))?;

    for msg in &doc.messages {
        write_message(&fur_dir, root, folder, msg)?;
    }
    write_thread(&fur_dir, doc)?;
    append_to_index(&fur_dir, &doc.conversation_id)?;
    merge_avatars(&fur_dir, doc)?;

    Ok(())
}

fn append_to_index(fur_dir: &Path, conversation_id: &str) -> Result<(), String> {
    let path = fur_dir.join("index.json");
    let mut value: Value = if path.exists() {
        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))?
    } else {
        json!({
            "threads": [],
            "active_thread": Value::Null,
            "current_message": Value::Null,
            "created_at": chrono::Utc::now().to_rfc3339(),
            "schema_version": SCHEMA_VERSION
        })
    };

    let threads = value["threads"]
        .as_array_mut()
        .ok_or("index.json has no threads array")?;
    if threads.iter().any(|item| item.as_str() == Some(conversation_id)) {
        return Err(format!("conversation {} is already indexed", conversation_id));
    }
    threads.push(json!(conversation_id));
    value["active_thread"] = json!(conversation_id);
    value["current_message"] = Value::Null;

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

fn merge_avatars(fur_dir: &Path, doc: &FurDocument) -> Result<(), String> {
    let path = fur_dir.join("avatars.json");
    let mut value: Value = if path.exists() {
        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))?
    } else {
        json!({})
    };

    let mut counts: BTreeMap<String, usize> = BTreeMap::new();
    for msg in &doc.messages {
        *counts.entry(msg.avatar.clone()).or_insert(0) += 1;
        if value.get(&msg.avatar).is_none() {
            value[msg.avatar.as_str()] = json!("🐾");
        }
    }

    if value.get("main").and_then(|item| item.as_str()).is_none() {
        if let Some((name, _)) = counts.iter().max_by_key(|(_, count)| **count) {
            value["main"] = json!(name);
        }
    }

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

fn write_message(
    fur_dir: &Path,
    root: &Path,
    folder: &Path,
    msg: &crate::schema::document::FurMessage,
) -> Result<(), String> {
    // Links are stored as basenames relative to the conversation folder;
    // `.fur/` wants a path relative to the project root.
    let markdown = msg
        .link
        .as_ref()
        .map(|name| relative_to_root(root, &folder.join(name)));

    let markdown_meta = match (&markdown, &msg.sha256) {
        (Some(path), hash) => {
            let size = fs::metadata(root.join(path)).map(|m| m.len()).ok();
            json!({
                "hash": hash,
                "size": size,
                "filename": Path::new(path).file_name().and_then(|f| f.to_str())
            })
        }
        _ => Value::Null,
    };

    let attachment = msg
        .img
        .as_ref()
        .map(|name| relative_to_root(root, &folder.join(name)));

    let value = json!({
        "id": msg.id,
        "avatar": msg.avatar,
        "timestamp": msg.ts,
        "text": if msg.body.is_empty() { Value::Null } else { json!(msg.body) },
        "markdown": markdown,
        "markdown_meta": markdown_meta,
        "attachment": attachment,
        "parent": Value::Null,
        "children": [],
        "branches": [],
        "schema_version": SCHEMA_VERSION
    });

    let path = fur_dir.join("messages").join(format!("{}.json", msg.id));

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

fn write_thread(fur_dir: &Path, doc: &FurDocument) -> Result<(), String> {
    let value = json!({
        "id": doc.conversation_id,
        "created_at": doc.created_at,
        "messages": doc.messages.iter().map(|m| m.id.clone()).collect::<Vec<_>>(),
        "tags": doc.tags,
        "parents": doc.parents,
        "children": doc.children,
        "title": doc.title,
        "schema_version": SCHEMA_VERSION
    });

    let path = fur_dir
        .join("threads")
        .join(format!("{}.json", doc.conversation_id));

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

fn write_index(fur_dir: &Path, thread_ids: &[String]) -> Result<(), String> {
    let value = json!({
        "threads": thread_ids,
        "active_thread": thread_ids.last(),
        "current_message": Value::Null,
        "created_at": chrono::Utc::now().to_rfc3339(),
        "schema_version": SCHEMA_VERSION
    });

    let path = fur_dir.join("index.json");

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

/// Avatar *names* come from the documents; the emoji mapping and `main` are
/// reader preferences that no document can carry. A placeholder is written so
/// nothing crashes, and onboarding refines it later.
fn write_avatars(fur_dir: &Path, summary: &RebuildSummary) -> Result<(), String> {
    let path = fur_dir.join("avatars.json");

    if path.exists() {
        return Ok(());
    }

    let mut map = serde_json::Map::new();

    if let Some(main) = &summary.guessed_main {
        map.insert("main".to_string(), json!(main));
    }

    for name in &summary.avatars {
        map.insert(name.clone(), json!("🐾"));
    }

    fs::write(&path, pretty(&Value::Object(map)))
        .map_err(|e| format!("cannot write {}: {}", path.display(), e))
}

fn relative_to_root(root: &Path, path: &Path) -> String {
    path.strip_prefix(root)
        .unwrap_or(path)
        .to_string_lossy()
        .replace('\\', "/")
}

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

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

    fn scratch(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(name);
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(dir.join("chats")).unwrap();
        dir
    }

    fn spine(root: &Path, slug: &str, body: &str) {
        let folder = root.join("chats").join(slug);
        fs::create_dir_all(&folder).unwrap();
        fs::write(folder.join("convo.md"), body).unwrap();
    }

    const DOC: &str = "---\nfur_schema: 1\nconversation_id: 14e87a09-d24e-4f4e-be73-7c76b4a15f5f\ntitle: Hello\ncreated_at: 2026-08-09T01:23:14Z\ntags: []\n---\n\n<!-- fur:msg id=m1 avatar=andrew ts=2026-08-09T01:23:58Z -->\n\nfirst\n\n<!-- fur:msg id=m2 avatar=andrew ts=2026-08-09T01:24:58Z -->\n\nsecond\n";

    #[test]
    fn detects_an_unindexed_archive() {
        let root = scratch("fur_rb_state");
        spine(&root, "hello-14e87a09", DOC);
        assert_eq!(detect_state(&root), ProjectState::Unindexed);
    }

    #[test]
    fn detects_empty_and_locked() {
        let root = scratch("fur_rb_empty");
        assert_eq!(detect_state(&root), ProjectState::Empty);

        fs::write(root.join("chats").join(LOCK_SENTINEL), "1").unwrap();
        assert_eq!(detect_state(&root), ProjectState::Locked);
    }

    #[test]
    fn rebuilds_threads_messages_and_index() {
        let root = scratch("fur_rb_build");
        spine(&root, "hello-14e87a09", DOC);

        let summary = rebuild(&root, false).unwrap();

        assert_eq!(summary.conversations, 1);
        assert_eq!(summary.messages, 2);
        assert_eq!(summary.guessed_main.as_deref(), Some("andrew"));

        assert!(root
            .join(".fur/threads/14e87a09-d24e-4f4e-be73-7c76b4a15f5f.json")
            .exists());
        assert!(root.join(".fur/messages/m1.json").exists());
        assert!(root.join(".fur/messages/m2.json").exists());

        let index: Value =
            serde_json::from_str(&fs::read_to_string(root.join(".fur/index.json")).unwrap())
                .unwrap();
        assert_eq!(index["threads"].as_array().unwrap().len(), 1);
    }

    #[test]
    fn refuses_to_clobber_live_state() {
        let root = scratch("fur_rb_guard");
        spine(&root, "hello-14e87a09", DOC);
        rebuild(&root, false).unwrap();

        assert!(rebuild(&root, false).is_err());
        assert!(rebuild(&root, true).is_ok());
    }

    #[test]
    fn refuses_to_parse_a_locked_archive() {
        let root = scratch("fur_rb_locked");
        spine(&root, "hello-14e87a09", DOC);
        fs::write(root.join("chats").join(LOCK_SENTINEL), "1").unwrap();

        let err = rebuild(&root, false).unwrap_err();
        assert!(err.contains("locked"), "got: {}", err);
    }

    #[test]
    fn one_bad_document_does_not_sink_the_archive() {
        let root = scratch("fur_rb_partial");
        spine(&root, "hello-14e87a09", DOC);
        spine(&root, "broken-deadbeef", "---\nconversation_id: x\n---\n\nnope\n");

        let summary = rebuild(&root, false).unwrap();

        assert_eq!(summary.conversations, 1);
        assert_eq!(summary.skipped.len(), 1);
    }
}