vivac 0.10.0

Provenance tree for work: every node knows which node it was born from
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
//! `vivac setup` — writes what a harness needs, after showing it.
//!
//! `t565` §7. This module holds what every harness shares: finding the
//! root, the all-or-nothing commit with its verification and rollback, the
//! confirmation prompt, and the fingerprint that tells an untouched
//! generated file apart from an edited one. `claude_code` is the one harness
//! that exists today, with its own files, its own hook shape and its own
//! rules for "already there" versus "conflict" (`t565` §9 on
//! `INTEGRATION.md`).
//!
//! Not exposed over MCP: setup reforms the environment rather than
//! recording work, which is for whoever has a terminal, the same reason
//! `abandon` and `restore` stay off that server.

mod claude_code;
pub mod json;

use crate::args::Args;
use crate::failure::Failure;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};

const HARNESSES: &[&str] = &["claude-code"];

pub fn dispatch(cwd: &Path, a: &Args) -> Result<i32, Failure> {
    if a.has("dry-run") && a.has("yes") {
        return Err(Failure::usage(
            "--dry-run writes nothing, so there is nothing for --yes to confirm.\n\n  \
             Give one or the other.",
        ));
    }
    if let [first, ..] = a.extra(1) {
        return Err(Failure::usage(format!(
            "setup does not take \"{first}\".\n\n  It takes one word of its own: the harness to set up."
        )));
    }
    let Some(harness) = a.positional(0) else {
        return Err(Failure::usage(
            "vivac setup needs the harness to set up:  vivac setup claude-code\n  \
             It knows claude-code today.",
        ));
    };
    match harness {
        "claude-code" => claude_code::run(&resolve_root(cwd)?, a),
        other => Err(Failure::usage(format!(
            "vivac setup does not know \"{other}\" yet. It knows: {}",
            HARNESSES.join(", ")
        ))),
    }
}

/// The root a harness gets set up in: the same upward search every other
/// command uses, falling back to the current directory when nothing is
/// found, so a fresh project gets its tree planted right there. Refuses if
/// that root turns out to be the very folder that holds the registry of
/// every tree on the machine (`t565` §7.2).
pub fn resolve_root(cwd: &Path) -> Result<PathBuf, Failure> {
    let root = crate::store::find_root(cwd).unwrap_or_else(|| cwd.to_path_buf());
    let canon = |p: &Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf());
    let is_registry = crate::store::store_dir().is_some_and(|d| canon(&d) == canon(&root));
    if is_registry {
        return Err(Failure::Model(format!(
            "  {} holds the registry of the trees on this machine, so it cannot\n  \
             hold a tree too. Run setup inside a project.",
            root.display()
        )));
    }
    Ok(root)
}

/// What a `PlannedWrite` does to its file.
pub enum Action {
    Write(String),
    /// Removing the file entirely -- an `--undo` that empties a JSON file
    /// out, or a skill no longer wanted. Goes through the same commit as a
    /// write, so it shares its rollback.
    Delete,
}

/// A structural check on a JSON write, run against the file re-read and
/// re-parsed after writing: see [`PlannedWrite::preserved`].
type PreservedCheck = Box<dyn Fn(&json::Value) -> bool>;

/// One file `setup` is about to change, with what was there before so a
/// failed commit can put it back exactly.
pub struct PlannedWrite {
    pub path: PathBuf,
    pub action: Action,
    /// `None` for a file that does not exist yet: rollback removes it
    /// instead of restoring bytes that never existed.
    pub original: Option<Vec<u8>>,
    /// For a JSON write: checked against the file re-read and re-parsed
    /// after writing, in whichever direction this call goes -- a superset
    /// of the original for `apply`, a subsequence of it for `--undo`.
    /// `t565` §7.6's second check, on top of the byte-for-byte one every
    /// write already gets. `None` for the skill file and for every
    /// `Delete`, neither of which this applies to.
    pub preserved: Option<PreservedCheck>,
}

impl PlannedWrite {
    pub fn write(path: PathBuf, content: String, original: Option<Vec<u8>>) -> PlannedWrite {
        PlannedWrite {
            path,
            action: Action::Write(content),
            original,
            preserved: None,
        }
    }

    pub fn delete(path: PathBuf, original: Vec<u8>) -> PlannedWrite {
        PlannedWrite {
            path,
            action: Action::Delete,
            original: Some(original),
            preserved: None,
        }
    }
}

fn sibling_temp(path: &Path) -> PathBuf {
    let mut name = path.file_name().unwrap_or_default().to_os_string();
    name.push(".tmp");
    path.with_file_name(name)
}

/// A temporary file next to `path`, renamed over it -- `write_config_atomic`'s
/// own shape, so a process that dies mid-write leaves the original untouched.
///
/// A temporary left behind by a failed write or rename is removed before the
/// error goes up. It holds the whole new file, and a settings file can carry
/// credentials in its `env` block: a `settings.json.tmp` lying next to the
/// original is exactly the copy under another name that §7.6 keeps setup
/// from ever making.
fn write_atomic(path: &Path, content: &[u8]) -> std::io::Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let tmp = sibling_temp(path);
    let written = std::fs::write(&tmp, content).and_then(|()| std::fs::rename(&tmp, path));
    if written.is_err() {
        let _ = std::fs::remove_file(&tmp);
    }
    written
}

fn remove_if_present(path: &Path) -> std::io::Result<()> {
    match std::fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(e) => Err(e),
    }
}

fn apply_action(w: &PlannedWrite) -> std::io::Result<()> {
    match &w.action {
        Action::Write(content) => write_atomic(&w.path, content.as_bytes()),
        Action::Delete => remove_if_present(&w.path),
    }
}

/// Puts `w` back exactly as it was: whether it was written or deleted makes
/// no difference to undoing it, only what `original` says was there.
fn restore_one(w: &PlannedWrite) -> std::io::Result<()> {
    match &w.original {
        Some(bytes) => write_atomic(&w.path, bytes),
        None => remove_if_present(&w.path),
    }
}

fn verify_one(w: &PlannedWrite) -> bool {
    match &w.action {
        Action::Write(content) => {
            let Ok(actual) = std::fs::read(&w.path) else {
                return false;
            };
            if actual != content.as_bytes() {
                return false;
            }
            match &w.preserved {
                Some(check) => std::str::from_utf8(&actual)
                    .ok()
                    .and_then(|s| json::parse(s).ok())
                    .is_some_and(|v| check(&v)),
                None => true,
            }
        }
        Action::Delete => !w.path.exists(),
    }
}

/// Rolls every one of `writes` back, in reverse, and returns the paths it
/// could not restore. `t565` §7.6: the order matters because a later write
/// can depend on an earlier one existing (a directory it lives in).
///
/// Public so a caller whose own next step fails *after* a successful commit
/// -- planting `.vivac/`, say -- can undo the commit by hand and report it
/// with [`failure_with_rollback`], the same way a failure inside the commit
/// itself does.
pub fn rollback(writes: &[PlannedWrite]) -> Vec<PathBuf> {
    let mut failed = Vec::new();
    for w in writes.iter().rev() {
        if restore_one(w).is_err() {
            failed.push(w.path.clone());
        }
    }
    failed
}

enum Cause {
    Write(std::io::Error),
    Verify,
}

/// The exit-5 text for a rollback that just ran: `clause` names what went
/// wrong, and `unrestored` lists whatever the rollback itself could not put
/// back -- empty in the common case, where it says so plainly instead.
pub fn failure_with_rollback(clause: String, unrestored: &[PathBuf]) -> Failure {
    let message = if unrestored.is_empty() {
        format!("{clause}, so setup put every file it\n  touched back as it was.")
    } else {
        let mut m = format!("{clause}, and setup could not put these back as they were:\n");
        for p in unrestored {
            m.push_str(&format!("      {}\n", p.display()));
        }
        m.push_str(
            "  setup keeps no copy on disk, so the only other copy is whatever\n  \
             version control holds.",
        );
        m
    };
    Failure::Io(std::io::Error::other(message))
}

fn rollback_failure(path: &Path, cause: Cause, writes: &[PlannedWrite]) -> Failure {
    let unrestored = rollback(writes);
    let clause = match cause {
        Cause::Write(e) => format!("{} could not be written ({e})", path.display()),
        Cause::Verify => format!("{} did not read back as written", path.display()),
    };
    failure_with_rollback(clause, &unrestored)
}

/// Applies every one of `writes`, in order; on any failure to write or to
/// verify, rolls every one of them back and fails instead of leaving a
/// partial result. `t565` §7.6: this is what makes the four pieces all or
/// nothing, the same promise the security pillar was given.
pub fn commit(writes: &[PlannedWrite]) -> Result<(), Failure> {
    for (i, w) in writes.iter().enumerate() {
        if let Err(e) = apply_action(w) {
            // `w` itself never changed -- a failed write leaves its target
            // exactly as it was, since `write_atomic`'s last step is one
            // rename -- so only the entries before it need rolling back.
            return Err(rollback_failure(&w.path, Cause::Write(e), &writes[..i]));
        }
    }
    for w in writes {
        if !verify_one(w) {
            return Err(rollback_failure(&w.path, Cause::Verify, writes));
        }
    }
    Ok(())
}

/// Whether typing `y` or `yes`, in any mix of case, answered a question.
/// Split out so the interpretation has a test that never has to fake a
/// terminal.
pub fn is_yes(answer: &str) -> bool {
    matches!(answer.trim().to_ascii_lowercase().as_str(), "y" | "yes")
}

/// Prints `prompt`, flushes it out with no line of its own, and reads one
/// line of the answer. Only ever called once a terminal is known to be
/// there -- `--yes` and the no-terminal refusal both skip this entirely.
pub fn ask(prompt: &str) -> bool {
    use std::io::Write;
    print!("{prompt}");
    let _ = std::io::stdout().flush();
    let mut line = String::new();
    let _ = std::io::stdin().read_line(&mut line);
    is_yes(&line)
}

pub fn stdin_is_terminal() -> bool {
    std::io::stdin().is_terminal()
}

/// 64-bit FNV-1a over `data`. Not a cryptographic hash and not meant to be
/// one: it only has to tell an untouched generated file apart from an
/// edited one, and that is what keeps this from pulling in a dependency
/// (`t565` §7.4).
pub fn fnv1a64(data: &[u8]) -> u64 {
    const OFFSET_BASIS: u64 = 0xcbf29ce484222325;
    const PRIME: u64 = 0x100000001b3;
    let mut hash = OFFSET_BASIS;
    for &byte in data {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(PRIME);
    }
    hash
}

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

    #[test]
    fn fnv1a64_matches_the_reference_vectors() {
        // The two vectors fnvhash.info publishes for FNV-1a/64.
        assert_eq!(fnv1a64(b""), 0xcbf29ce484222325);
        assert_eq!(fnv1a64(b"a"), 0xaf63dc4c8601ec8c);
    }

    #[test]
    fn is_yes_takes_any_case_of_y_or_yes() {
        for s in ["y", "Y", "yes", "YES", "Yes", "  y  ", "y\n"] {
            assert!(is_yes(s), "{s:?} should count as yes");
        }
        for s in ["n", "no", "", "yep", "sure"] {
            assert!(!is_yes(s), "{s:?} should not count as yes");
        }
    }

    fn temp_dir(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("vivac-setup-{name}-{}", crate::id::ulid()));
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    /// A rename that fails must not leave the temporary behind: it holds the
    /// whole new file, credentials in `env` included, under another name.
    /// A directory where the file should go makes the rename fail on every
    /// platform.
    #[test]
    fn a_failed_rename_leaves_no_temporary_behind() {
        let dir = temp_dir("rename-fails");
        let target = dir.join("settings.json");
        std::fs::create_dir_all(target.join("occupied")).unwrap();
        assert!(write_atomic(&target, b"{\"env\":{}}").is_err());
        assert!(
            !sibling_temp(&target).exists(),
            "the temporary survived the failed rename"
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn a_committed_write_leaves_every_file_as_written() {
        let dir = temp_dir("commit-ok");
        let writes = vec![
            PlannedWrite::write(dir.join("a.txt"), "A".to_string(), None),
            PlannedWrite::write(dir.join("b.txt"), "B".to_string(), None),
        ];
        commit(&writes).unwrap();
        assert_eq!(std::fs::read_to_string(dir.join("a.txt")).unwrap(), "A");
        assert_eq!(std::fs::read_to_string(dir.join("b.txt")).unwrap(), "B");
        std::fs::remove_dir_all(&dir).ok();
    }

    /// `t565` §11 test 15, and the coordinator's point A: a write that fails
    /// on the second file rolls the first one back too, exactly like a
    /// verification failure would. A directory sitting where the second
    /// file needs to be a file is the fault: `write_atomic`'s rename onto it
    /// fails on every platform this runs on, unlike a permission bit.
    #[test]
    fn a_write_failure_rolls_the_earlier_files_back() {
        let dir = temp_dir("commit-write-fails");
        let first = dir.join("first.txt");
        let second = dir.join("second.txt");
        std::fs::write(&first, "original first").unwrap();
        std::fs::create_dir(&second).unwrap(); // the fault

        let writes = vec![
            PlannedWrite::write(
                first.clone(),
                "new first".into(),
                Some(b"original first".to_vec()),
            ),
            PlannedWrite::write(second.clone(), "new second".into(), None),
        ];
        let err = commit(&writes).unwrap_err();
        let msg = err.message();
        assert!(msg.contains("could not be written"), "{msg}");
        assert!(msg.contains("put every file it"), "{msg}");
        assert_eq!(std::fs::read_to_string(&first).unwrap(), "original first");
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn a_verification_failure_rolls_every_file_back() {
        let dir = temp_dir("commit-verify-fails");
        let first = dir.join("first.txt");
        let second = dir.join("second.txt");
        std::fs::write(&first, "original first").unwrap();

        // `commit` only ever writes `content` itself, so the only way to
        // reach the verification path is a write whose declared content
        // does not match what actually lands on disk -- the shape of a
        // `preserved` check failing.
        let writes = vec![
            PlannedWrite::write(
                first.clone(),
                "new first".into(),
                Some(b"original first".to_vec()),
            ),
            PlannedWrite {
                path: second.clone(),
                action: Action::Write("new second".into()),
                original: None,
                preserved: Some(Box::new(|_| false)),
            },
        ];
        let err = commit(&writes).unwrap_err();
        assert!(err.message().contains("did not read back as written"));
        assert_eq!(std::fs::read_to_string(&first).unwrap(), "original first");
        assert!(
            !second.exists(),
            "a file with no original should be removed"
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    /// Point A's other half: `--undo` deleting a file is part of the same
    /// transaction, and a delete that cannot complete rolls a write next to
    /// it back too.
    #[test]
    fn a_failed_delete_rolls_a_sibling_write_back() {
        let dir = temp_dir("commit-delete-fails");
        let written = dir.join("written.txt");
        let undone = dir.join("undone.txt");
        std::fs::write(&written, "original written").unwrap();
        std::fs::create_dir(&undone).unwrap(); // remove_file fails on a directory

        let writes = vec![
            PlannedWrite::write(
                written.clone(),
                "new written".into(),
                Some(b"original written".to_vec()),
            ),
            PlannedWrite::delete(undone.clone(), b"{}".to_vec()),
        ];
        let err = commit(&writes).unwrap_err();
        assert!(
            err.message().contains("could not be written")
                || err.message().contains("Input/output")
        );
        assert_eq!(
            std::fs::read_to_string(&written).unwrap(),
            "original written"
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    /// When rollback itself cannot put a file back, the failure says so and
    /// names it, instead of a false "put back as it was".
    #[test]
    fn an_unrestorable_file_is_named_rather_than_claimed_fixed() {
        let dir = temp_dir("commit-unrestorable");
        let first = dir.join("first.txt");
        let second = dir.join("second.txt");
        std::fs::write(&second, "second").unwrap();
        // `first`'s rollback target cannot be written to at all: a
        // directory sits where the restored file would need to be.
        std::fs::create_dir(&first).unwrap();

        let writes = vec![
            PlannedWrite::write(
                first.clone(),
                "new first".into(),
                Some(b"original first".to_vec()),
            ),
            PlannedWrite::write(
                second.clone(),
                "new second".into(),
                Some(b"second".to_vec()),
            ),
        ];
        let unrestored = rollback(&writes);
        assert_eq!(unrestored, vec![first.clone()]);

        let msg = failure_with_rollback("something failed".to_string(), &unrestored).message();
        assert!(
            msg.contains("could not put these back as they were"),
            "{msg}"
        );
        assert!(msg.contains(&first.display().to_string()), "{msg}");
        assert!(msg.contains("version control"), "{msg}");
        std::fs::remove_dir_all(&dir).ok();
    }
}