quilt-cli 0.31.1

Local-first data versioning for AI agent workflows: commit immutable data packages on your own machine, then sync them with S3 to share.
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
use quilt_rs::lineage::CommitState;
use quilt_uri::Namespace;

use crate::cli::Error;
use crate::cli::model::Commands;
use crate::cli::output::Std;

#[derive(Debug)]
pub struct Input {
    pub namespace: Namespace,
}

#[derive(Debug)]
pub struct Output {
    pub commit: CommitState,
}

impl std::fmt::Display for Output {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Undid the last commit; now at \"{}\"", self.commit.hash)
    }
}

pub async fn command(m: impl Commands, args: Input) -> Std {
    Std::from_result(m.undo_commit(args).await)
}

pub async fn model(
    local_domain: &quilt_rs::LocalDomain,
    Input { namespace }: Input,
) -> Result<Output, Error> {
    let package = local_domain
        .get_installed_package(&namespace)
        .await?
        .ok_or_else(|| Error::NamespaceNotFound(namespace))?;
    let commit = package.undo_commit().await?;
    Ok(Output { commit })
}

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

    use std::path::PathBuf;

    use tempfile::TempDir;

    use crate::cli::commit;
    use crate::cli::create;
    use crate::cli::model::Model;
    use crate::cli::model::create_model_in_temp_dir;
    use quilt_rs::flow::UserMeta;
    use quilt_rs::io::storage::ByteStream;
    use quilt_rs::io::storage::LocalStorage;
    use quilt_rs::io::storage::Storage;

    /// A local-only package with `revisions` commits after its initial one,
    /// each writing `value.txt`. Returns the model, the namespace, and the
    /// package's working directory.
    async fn package_with(
        revisions: &[&str],
    ) -> Result<(Model, Namespace, PathBuf, TempDir), Error> {
        let (cli_model, temp_dir) = create_model_in_temp_dir().await?;
        let namespace: Namespace = ("demo", "local").into();
        let created = cli_model
            .create(create::Input {
                namespace: namespace.clone(),
                source: None,
                message: Some("initial".to_string()),
            })
            .await?;
        let package_home = created.installed_package.package_home().await?;
        let storage = LocalStorage::new();

        for message in revisions {
            storage
                .write_byte_stream(
                    package_home.join("value.txt"),
                    ByteStream::from(message.as_bytes().to_vec()),
                )
                .await?;
            cli_model
                .commit(commit::Input {
                    message: (*message).to_string(),
                    namespace: namespace.clone(),
                    user_meta: UserMeta::Keep,
                    workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                    host_config: None,
                })
                .await?;
        }
        Ok((cli_model, namespace, package_home, temp_dir))
    }

    /// The changeset the engine reports for the package — what `quilt status`
    /// would print.
    async fn changes(cli_model: &Model, namespace: &Namespace) -> Result<Vec<String>, Error> {
        let package = cli_model
            .get_local_domain()
            .get_installed_package(namespace)
            .await?
            .expect("installed");
        let status = package.status(None).await?;
        Ok(status
            .changes
            .keys()
            .map(|path| path.display().to_string())
            .collect())
    }

    /// The package's **tracked-path map** as persisted in the domain lineage.
    ///
    /// Deliberately not `status`: the changeset is recomputed from the
    /// manifest and the working tree, so it is blind to this map being wrong.
    /// The map is what `pull`'s touch set, `install_paths`, `uninstall_paths`
    /// and undo's own guard read, so it has to be asserted on its own terms.
    async fn tracked(cli_model: &Model, namespace: &Namespace) -> Result<Vec<String>, Error> {
        let lineage = cli_model.get_local_domain().get_lineage().await?;
        let mut keys: Vec<String> = lineage
            .packages
            .get(namespace)
            .expect("installed")
            .paths
            .keys()
            .map(|path| path.display().to_string())
            .collect();
        keys.sort();
        Ok(keys)
    }

    async fn undo(cli_model: &Model, namespace: &Namespace) -> Result<Output, Error> {
        super::model(
            cli_model.get_local_domain(),
            Input {
                namespace: namespace.clone(),
            },
        )
        .await
    }

    #[tokio::test]
    async fn undoes_the_newest_commit() -> Result<(), Error> {
        let (cli_model, _temp_dir) = create_model_in_temp_dir().await?;
        let namespace: Namespace = ("demo", "local").into();
        let created = cli_model
            .create(create::Input {
                namespace: namespace.clone(),
                source: None,
                message: Some("initial".to_string()),
            })
            .await?;
        let package_home = created.installed_package.package_home().await?;
        let storage = LocalStorage::new();

        storage
            .write_byte_stream(
                package_home.join("value.txt"),
                ByteStream::from_static(b"first"),
            )
            .await?;
        cli_model
            .commit(commit::Input {
                message: "first".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;

        storage
            .write_byte_stream(
                package_home.join("value.txt"),
                ByteStream::from_static(b"second"),
            )
            .await?;
        cli_model
            .commit(commit::Input {
                message: "second".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;

        let output = super::model(
            cli_model.get_local_domain(),
            Input {
                namespace: namespace.clone(),
            },
        )
        .await?;
        assert_eq!(output.commit.prev_hashes.len(), 1);
        assert_eq!(
            tokio::fs::read(package_home.join("value.txt")).await?,
            b"first"
        );
        Ok(())
    }

    /// The floor: a package's first revision has no parent, so there is
    /// nothing to undo. This is the state `create` leaves behind, not an edge
    /// case a user has to reach for.
    #[tokio::test]
    async fn refuses_at_the_initial_commit() -> Result<(), Error> {
        let (cli_model, namespace, _home, _temp) = package_with(&[]).await?;

        let err = undo(&cli_model, &namespace).await.unwrap_err();

        assert!(
            err.to_string().contains("first revision"),
            "should name the floor, got: {err}"
        );
        Ok(())
    }

    /// Undoing a *commit* must not be how an uncommitted edit is lost: the
    /// object store holds committed content only, so there would be nothing to
    /// restore it from.
    #[tokio::test]
    async fn refuses_a_dirty_tree_and_writes_nothing() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&["first", "second"]).await?;
        LocalStorage::new()
            .write_byte_stream(
                home.join("value.txt"),
                ByteStream::from_static(b"uncommitted"),
            )
            .await?;

        let err = undo(&cli_model, &namespace).await.unwrap_err();

        assert!(
            err.to_string().contains("value.txt"),
            "should name the dirty path, got: {err}"
        );
        // The refusal is only worth anything if it left the edit alone.
        assert_eq!(
            tokio::fs::read(home.join("value.txt")).await?,
            b"uncommitted"
        );
        Ok(())
    }

    /// The message must not send the user to a verb that does not exist:
    /// nothing discards uncommitted work on a package with no remote
    /// (<https://github.com/quiltdata/quilt-rs/issues/880>).
    #[tokio::test]
    async fn dirty_refusal_names_no_discard_verb() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&["first", "second"]).await?;
        LocalStorage::new()
            .write_byte_stream(home.join("value.txt"), ByteStream::from_static(b"edited"))
            .await?;

        let msg = undo(&cli_model, &namespace).await.unwrap_err().to_string();

        assert!(msg.contains("by hand"), "got: {msg}");
        for absent in ["discard", "reset", "revert", "quilt clean"] {
            assert!(!msg.contains(absent), "must not offer `{absent}`: {msg}");
        }
        Ok(())
    }

    /// A file the undone commit *added* is removed, and one it *deleted* comes
    /// back. Both are the delta arms the old rebuild-everything loop got right
    /// only by accident.
    #[tokio::test]
    async fn restores_added_and_removed_paths() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&["first"]).await?;
        let storage = LocalStorage::new();
        // Second commit: drop value.txt, add other.txt.
        tokio::fs::remove_file(home.join("value.txt")).await?;
        storage
            .write_byte_stream(home.join("other.txt"), ByteStream::from_static(b"new"))
            .await?;
        cli_model
            .commit(commit::Input {
                message: "swap".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;

        undo(&cli_model, &namespace).await?;

        assert_eq!(tokio::fs::read(home.join("value.txt")).await?, b"first");
        assert!(
            !home.join("other.txt").exists(),
            "the added file should be gone again"
        );
        Ok(())
    }

    /// Undo moves the pointer and prunes nothing, so the discarded revision is
    /// still listed. Pinning it because it is surprising, not because it is
    /// settled — see the open question on whether the log should hide it.
    #[tokio::test]
    async fn undone_revision_still_appears_in_the_log() -> Result<(), Error> {
        let (cli_model, namespace, _home, _temp) = package_with(&["first", "second"]).await?;
        let package = cli_model
            .get_local_domain()
            .get_installed_package(&namespace)
            .await?
            .unwrap();
        let before = package.revisions().await?.len();

        undo(&cli_model, &namespace).await?;

        assert_eq!(package.revisions().await?.len(), before);
        Ok(())
    }

    /// The case the review raised: a path that is a file in one revision and a
    /// directory in the other. Writing before deleting is exactly wrong here —
    /// the file at `a` blocks creating `a/b`, and vice versa — so the removal
    /// that makes room has to happen first for that path.
    #[tokio::test]
    async fn handles_a_file_becoming_a_directory() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&["first"]).await?;
        let storage = LocalStorage::new();
        // Second commit turns the file `a` into the directory `a/`.
        storage
            .write_byte_stream(home.join("a"), ByteStream::from_static(b"a-as-file"))
            .await?;
        cli_model
            .commit(commit::Input {
                message: "a is a file".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;
        tokio::fs::remove_file(home.join("a")).await?;
        storage
            .write_byte_stream(home.join("a").join("b"), ByteStream::from_static(b"nested"))
            .await?;
        cli_model
            .commit(commit::Input {
                message: "a is a directory".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;

        undo(&cli_model, &namespace).await?;

        assert_eq!(tokio::fs::read(home.join("a")).await?, b"a-as-file");
        assert!(
            !home.join("a").join("b").exists(),
            "the nested path should be gone"
        );
        Ok(())
    }
    /// A failure part-way is **not** resumable, and that is the accepted
    /// limitation rather than an oversight — pinned so nobody re-widens the
    /// guard to admit it.
    ///
    /// Undo cannot tell its own half-finished work from a user's edits;
    /// nothing on disk records how far the last attempt got. Every rule that
    /// admits a state undo can produce also admits the user's version of the
    /// same state, and one of those is silent data loss. So the mixed tree is
    /// reported instead. Nothing is lost either way: all of it is committed
    /// content the object store still holds.
    #[tokio::test]
    async fn a_failed_undo_reports_the_mixed_tree_rather_than_resuming() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&[]).await?;
        let storage = LocalStorage::new();
        for (name, body) in [("one.txt", "one-v1"), ("two.txt", "two-v1")] {
            storage
                .write_byte_stream(home.join(name), ByteStream::from(body.as_bytes().to_vec()))
                .await?;
        }
        cli_model
            .commit(commit::Input {
                message: "v1".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;
        for (name, body) in [("one.txt", "one-v2"), ("two.txt", "two-v2")] {
            storage
                .write_byte_stream(home.join(name), ByteStream::from(body.as_bytes().to_vec()))
                .await?;
        }
        cli_model
            .commit(commit::Input {
                message: "v2".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;

        // Break the second write, so the first has already landed when it fails.
        let package = cli_model
            .get_local_domain()
            .get_installed_package(&namespace)
            .await?
            .unwrap();
        let mut entries = tokio::fs::read_dir(package.paths.objects_dir()).await?;
        let mut victim = None;
        while let Some(entry) = entries.next_entry().await? {
            if entry.file_type().await?.is_file()
                && tokio::fs::read(entry.path()).await? == b"two-v1"
            {
                victim = Some(entry.path());
            }
        }
        let victim = victim.expect("the previous content of two.txt is in the object store");
        let bytes = tokio::fs::read(&victim).await?;
        tokio::fs::remove_file(&victim).await?;

        assert!(undo(&cli_model, &namespace).await.is_err());
        assert_eq!(
            tokio::fs::read(home.join("one.txt")).await?,
            b"one-v1",
            "the first write landed before the failure"
        );

        // Even with the cause repaired, the mixed tree is reported rather than
        // silently completed — and the path it names is the one undo itself
        // moved, which is exactly the ambiguity it refuses to guess about.
        tokio::fs::write(&victim, &bytes).await?;
        let err = undo(&cli_model, &namespace).await.unwrap_err();
        assert!(err.to_string().contains("one.txt"), "got: {err}");
        assert_eq!(
            tokio::fs::read(home.join("two.txt")).await?,
            b"two-v2",
            "nothing further was written"
        );
        Ok(())
    }

    /// The other side of admitting "already removed": a path the undone commit
    /// added, which the user has since *edited*, is real uncommitted work even
    /// though the undo was going to delete it. It must still refuse.
    #[tokio::test]
    async fn refuses_an_edit_to_a_path_it_would_remove() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&["first"]).await?;
        let storage = LocalStorage::new();
        storage
            .write_byte_stream(
                home.join("added.txt"),
                ByteStream::from_static(b"committed"),
            )
            .await?;
        cli_model
            .commit(commit::Input {
                message: "adds a file".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;
        storage
            .write_byte_stream(
                home.join("added.txt"),
                ByteStream::from_static(b"then edited"),
            )
            .await?;

        let err = undo(&cli_model, &namespace).await.unwrap_err();

        assert!(err.to_string().contains("added.txt"), "got: {err}");
        assert_eq!(
            tokio::fs::read(home.join("added.txt")).await?,
            b"then edited"
        );
        Ok(())
    }

    /// A path the undo would remove, which cannot be *read* to tell whether it
    /// holds uncommitted work. Unlinking needs only the parent directory's
    /// permission, so classifying an unreadable file as clean would delete
    /// contents nobody verified. A symlink loop is the portable way to make a
    /// read fail as neither not-found nor a directory — a `chmod`-based test
    /// would pass vacuously as root.
    #[cfg(unix)]
    #[tokio::test]
    async fn refuses_when_a_path_it_would_remove_cannot_be_read() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&["first"]).await?;
        LocalStorage::new()
            .write_byte_stream(
                home.join("added.txt"),
                ByteStream::from_static(b"committed"),
            )
            .await?;
        cli_model
            .commit(commit::Input {
                message: "adds a file".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;

        tokio::fs::remove_file(home.join("added.txt")).await?;
        std::os::unix::fs::symlink("added.txt", home.join("added.txt"))?;

        let err = undo(&cli_model, &namespace).await.unwrap_err();

        // Surfaced as the I/O failure it is, not reported as a clean removal
        // and not dressed up as an uncommitted change.
        assert!(
            !err.to_string().contains("uncommitted changes"),
            "an unreadable path is not a known edit: {err}"
        );
        assert!(
            home.join("added.txt").symlink_metadata().is_ok(),
            "nothing should have been removed"
        );
        Ok(())
    }

    /// The restore writes paths the *previous* revision carries, which is not
    /// the set the dirty check walks (`lineage.paths` is the current
    /// revision's). A destination the current revision does not track is
    /// therefore never checked — so an untracked file sitting there is
    /// overwritten without a word.
    ///
    /// Minimal shape: the undone commit *deleted* a file, so undo restores it,
    /// and the user has since created their own file at that path.
    #[tokio::test]
    async fn refuses_to_overwrite_an_untracked_file_at_a_restore_destination() -> Result<(), Error>
    {
        let (cli_model, namespace, home, _temp) = package_with(&[]).await?;
        let storage = LocalStorage::new();
        storage
            .write_byte_stream(home.join("x"), ByteStream::from_static(b"committed-x"))
            .await?;
        cli_model
            .commit(commit::Input {
                message: "adds x".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;
        tokio::fs::remove_file(home.join("x")).await?;
        cli_model
            .commit(commit::Input {
                message: "deletes x".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;

        // The user makes their own file where the restore wants to write.
        storage
            .write_byte_stream(
                home.join("x"),
                ByteStream::from_static(b"mine, never committed"),
            )
            .await?;

        let err = undo(&cli_model, &namespace).await.unwrap_err();

        assert!(err.to_string().contains('x'), "should name the path: {err}");
        assert_eq!(
            tokio::fs::read(home.join("x")).await?,
            b"mine, never committed",
            "the user file must survive"
        );
        Ok(())
    }

    /// The rebuilt **tracked-path map** must describe the tree undo just
    /// wrote — gaining what it restored and losing what it removed.
    ///
    /// Every other test here reads file contents, and `status` cannot stand in
    /// for this: it recomputes the changeset from the manifest and the tree, so
    /// it stays green with the map entirely wrong. That map is what `pull`'s
    /// touch set, the `install_paths` / `uninstall_paths` pair and undo's own
    /// guard read, so a stale or missing entry is a real defect that nothing
    /// else here would notice.
    #[tokio::test]
    async fn the_tracked_path_map_matches_the_restored_revision() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&["first"]).await?;
        let storage = LocalStorage::new();
        // A revision that touches all three delta arms, so the rebuilt path
        // states cover a modification, an addition and a removal.
        storage
            .write_byte_stream(home.join("kept.txt"), ByteStream::from_static(b"kept-v1"))
            .await?;
        cli_model
            .commit(commit::Input {
                message: "v1".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;
        storage
            .write_byte_stream(home.join("kept.txt"), ByteStream::from_static(b"kept-v2"))
            .await?;
        storage
            .write_byte_stream(home.join("gained.txt"), ByteStream::from_static(b"gained"))
            .await?;
        tokio::fs::remove_file(home.join("value.txt")).await?;
        cli_model
            .commit(commit::Input {
                message: "v2".to_string(),
                namespace: namespace.clone(),
                user_meta: UserMeta::Keep,
                workflow: quilt_rs::io::remote::WorkflowIntent::BucketDefault,
                host_config: None,
            })
            .await?;
        assert_eq!(
            tracked(&cli_model, &namespace).await?,
            ["gained.txt", "kept.txt"],
            "v2 tracks the addition and the modification, not the removal"
        );

        undo(&cli_model, &namespace).await?;

        // v1's rows exactly: `value.txt` comes back, `gained.txt` goes, and
        // `kept.txt` stays but at its earlier content.
        assert_eq!(
            tracked(&cli_model, &namespace).await?,
            ["kept.txt", "value.txt"]
        );
        assert!(changes(&cli_model, &namespace).await?.is_empty());
        Ok(())
    }

    /// Undoing *to* the initial commit empties the working tree — the initial
    /// revision has no rows, so every tracked file is a trailing removal.
    /// `refuses_at_the_initial_commit` covers the refusal one step later; this
    /// covers getting there.
    #[tokio::test]
    async fn undoing_to_the_initial_commit_empties_the_tree() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&["first"]).await?;
        assert!(home.join("value.txt").exists());

        undo(&cli_model, &namespace).await?;

        let mut entries = tokio::fs::read_dir(&home).await?;
        let mut left = Vec::new();
        while let Some(entry) = entries.next_entry().await? {
            left.push(entry.file_name().to_string_lossy().into_owned());
        }
        left.sort();
        assert!(left.is_empty(), "files left behind: {left:?}");
        assert!(
            tracked(&cli_model, &namespace).await?.is_empty(),
            "the tracked-path map should be empty too"
        );
        Ok(())
    }

    /// The guard is a gate, not a wall: clearing what it named lets the undo
    /// through. Every other refusal test stops at the refusal, which would
    /// also pass if undo had become permanently stuck.
    #[tokio::test]
    async fn clearing_the_obstruction_lets_the_undo_through() -> Result<(), Error> {
        let (cli_model, namespace, home, _temp) = package_with(&["first", "second"]).await?;
        LocalStorage::new()
            .write_byte_stream(home.join("value.txt"), ByteStream::from_static(b"edited"))
            .await?;

        assert!(undo(&cli_model, &namespace).await.is_err());

        // Put back what the revision being undone committed, and retry.
        LocalStorage::new()
            .write_byte_stream(home.join("value.txt"), ByteStream::from_static(b"second"))
            .await?;
        undo(&cli_model, &namespace).await?;

        assert_eq!(tokio::fs::read(home.join("value.txt")).await?, b"first");
        assert!(changes(&cli_model, &namespace).await?.is_empty());
        Ok(())
    }
}