sprawling-storage 0.0.10

The append-only, hash-chained Ledger of a sprawling city, its content store, checkpoints and worktrees.
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
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Copyright (c) 2026 2youg1 and the sprawling contributors

//! A tree kept for its node between runs: whether somebody holds it,
//! and how it is taken back into use.

use super::super::lease::{FileWork, WorktreeLease};
use super::super::name::WorktreeName;
use super::super::weight::measure;
use super::Worktrees;
use super::open_tree;
use crate::error::StorageError;

/// Why a tree is locked: a run holds it.
const LEASE_REASON: &str = "held by a sprawling run";

/// What [`Worktrees::claim`] finds under a name.
pub(super) enum Standing {
    Held,
    Kept(git2::Worktree),
    Absent,
}

impl Worktrees {
    /// Lifts every lease a previous writer of this city left behind.
    ///
    /// A city has one writer, and `_writer` is the proof that the caller
    /// is it: while it is held, no lock can belong to a run that is
    /// still alive, so every lock is one a dead process never gave
    /// back. A city with no repository has no trees to free.
    ///
    /// # Errors
    /// Propagates a repository that cannot list, read or unlock its
    /// trees.
    pub fn lift_abandoned_leases(
        city_root: &std::path::Path,
        _writer: &crate::JsonlLedger,
    ) -> Result<(), StorageError> {
        let refuse = |op: &'static str, err: git2::Error| StorageError::Worktree {
            op,
            detail: format!("{}: {err}", city_root.display()),
        };
        let repo = match git2::Repository::open(city_root) {
            Ok(repo) => repo,
            Err(err) if err.code() == git2::ErrorCode::NotFound => return Ok(()),
            Err(err) => return Err(refuse("open the city repository", err)),
        };
        let names = repo
            .worktrees()
            .map_err(|err| refuse("list worktrees", err))?;
        for name in names.iter().flatten().flatten() {
            let tree = repo
                .find_worktree(name)
                .map_err(|err| refuse("find a worktree", err))?;
            let lock = tree
                .is_locked()
                .map_err(|err| refuse("read a worktree lock", err))?;
            match lock {
                git2::WorktreeLockStatus::Locked(_) => tree
                    .unlock()
                    .map_err(|err| refuse("unlock a worktree", err))?,
                git2::WorktreeLockStatus::Unlocked => {}
            }
        }
        Ok(())
    }

    /// Where `name` stands: held under a lock, kept on disk for its
    /// node, or not there at all.
    ///
    /// A registration with no directory under it is what an interrupted
    /// placement or a person deleting the tree by hand leaves, and it is
    /// taken back here - the same reflex the index has about a cache it
    /// doubts - so that `E_WORKTREE_BUSY` keeps meaning that somebody is
    /// working.
    pub(super) fn standing(&self, name: &WorktreeName) -> Result<Standing, StorageError> {
        let tree = match self.repo.find_worktree(name.as_str()) {
            Ok(tree) => tree,
            Err(err) if err.code() == git2::ErrorCode::NotFound => return Ok(Standing::Absent),
            Err(err) => {
                return Err(StorageError::Worktree {
                    op: "find a worktree",
                    detail: format!("{}: {err}", name.as_str()),
                });
            }
        };
        if !tree.path().exists() {
            self.forget(name.as_str())?;
            return Ok(Standing::Absent);
        }
        let lock = tree.is_locked().map_err(|err| StorageError::Worktree {
            op: "read a worktree lock",
            detail: format!("{}: {err}", name.as_str()),
        })?;
        Ok(match lock {
            git2::WorktreeLockStatus::Locked(_) => Standing::Held,
            git2::WorktreeLockStatus::Unlocked => Standing::Kept(tree),
        })
    }

    /// Takes a kept tree back into use: the node's branch moved up to
    /// the trunk when the trunk already holds all of it, the index read
    /// back from that branch head, every tracked file under `scopes`
    /// forced to it and every untracked one there removed, then the
    /// lock. Git rewrites only the files that differ, and only inside
    /// the scope, so a node's second run costs what it left behind in
    /// its own scope, not the size of the city.
    ///
    /// The index is read back whole before the narrowed checkout, unless
    /// it already writes the branch head's tree, because that checkout
    /// moves only the entries it writes: an entry outside the scope that
    /// names anything but the branch head - the last run's commit after
    /// the branch followed the trunk, or a path a run staged and never
    /// offered - would be committed by the next scoped offer. The
    /// comparison comes first because reading back walks the whole tree,
    /// and a reclaim that finds the branch and the index where the last
    /// run left them is the common case.
    pub(super) fn reattach(
        &self,
        name: &WorktreeName,
        tree: &git2::Worktree,
        scopes: &[String],
    ) -> Result<WorktreeLease, StorageError> {
        self.follow_trunk(name)?;
        let work = restore(tree, name.as_str(), scopes)?;
        tree.lock(Some(LEASE_REASON))
            .map_err(|err| StorageError::Worktree {
                op: "lock a worktree",
                detail: format!("{}: {err}", name.as_str()),
            })?;
        let weight = measure(tree.path())?;
        Ok(WorktreeLease {
            name: name.clone(),
            path: tree.path().to_path_buf(),
            disk: weight.bytes,
            work: FileWork {
                walked: weight.walked,
                ..work
            },
        })
    }

    /// Moves `name`'s branch to the trunk when the trunk descends from
    /// it. A branch with work the trunk lacks stays where it is: that
    /// work is still waiting for its merge.
    fn follow_trunk(&self, name: &WorktreeName) -> Result<(), StorageError> {
        let refuse = |op: &'static str, err: git2::Error| StorageError::Worktree {
            op,
            detail: format!("{}: {err}", name.as_str()),
        };
        let mut branch = self
            .repo
            .find_branch(name.as_str(), git2::BranchType::Local)
            .map_err(|err| refuse("find a node branch", err))?;
        let tip = branch
            .get()
            .peel_to_commit()
            .map_err(|err| refuse("read a node branch", err))?
            .id();
        let trunk = self
            .repo
            .head()
            .and_then(|head| head.peel_to_commit())
            .map_err(|err| refuse("read the city trunk", err))?
            .id();
        let behind = tip != trunk
            && self
                .repo
                .graph_descendant_of(trunk, tip)
                .map_err(|err| refuse("judge a fast-forward", err))?;
        if behind {
            branch
                .get_mut()
                .set_target(trunk, "follow the trunk")
                .map_err(|err| refuse("move a node branch", err))?;
        }
        Ok(())
    }
}

/// Puts `tree` on `branch` and forces every tracked file under `scopes`
/// to its head, removing what is untracked there, and counts what that
/// wrote (`crates/storage/spec/Worktree.lean` §8-31). The index is read back from the head first
/// when it does not already write the head's tree, for the reason
/// [`Worktrees::reattach`] gives.
///
/// The worktree's HEAD is pointed at `branch` when it names anything
/// else: a stock taken over by a node still names the stock's branch
/// (`crates/storage/spec/Worktree/Trees/Stock.lean` §8-35), and a checkout of the wrong head would hand the
/// node another line of work.
///
/// # Errors
/// Propagates a tree whose repository, head, index or checkout git
/// refuses.
pub(super) fn restore(
    tree: &git2::Worktree,
    branch: &str,
    scopes: &[String],
) -> Result<FileWork, StorageError> {
    let refuse = |op: &'static str, err: git2::Error| StorageError::Worktree {
        op,
        detail: format!("{branch}: {err}"),
    };
    let repo = open_tree(tree).map_err(|err| refuse("open a kept worktree", err))?;
    let wanted = format!("refs/heads/{branch}");
    let pointed = repo
        .find_reference("HEAD")
        .map_err(|err| refuse("read a kept worktree's HEAD", err))?;
    if pointed.symbolic_target_bytes() != Some(wanted.as_bytes()) {
        repo.reference_symbolic("HEAD", &wanted, true, "point the tree at its node's branch")
            .map_err(|err| refuse("point a kept worktree at its branch", err))?;
    }
    let head = repo
        .head()
        .and_then(|head| head.peel_to_tree())
        .map_err(|err| refuse("read a kept worktree's branch", err))?;
    let mut index = repo
        .index()
        .map_err(|err| refuse("read a kept worktree's index", err))?;
    if !index_writes_tree(&mut index, head.id())
        .map_err(|err| refuse("compare a kept worktree's index", err))?
    {
        index
            .read_tree(&head)
            .and_then(|()| index.write())
            .map_err(|err| refuse("reset a kept worktree's index", err))?;
    }
    let mut work = FileWork::default();
    let mut checkout = git2::build::CheckoutBuilder::new();
    checkout
        .force()
        .remove_untracked(true)
        .notify_on(
            git2::CheckoutNotificationType::UPDATED | git2::CheckoutNotificationType::UNTRACKED,
        )
        .notify(|kind, _, _, target, workdir| {
            count_change(&mut work, kind, (target.is_some(), workdir.is_some()));
            true
        });
    for spec in crate::checkpoint::scan::pathspec::of(scopes) {
        checkout.path(spec);
    }
    repo.checkout_head(Some(&mut checkout))
        .map_err(|err| refuse("reset a kept worktree", err))?;
    drop(checkout);
    Ok(work)
}

/// Counts one change a reattaching checkout announced before making it
/// (`crates/storage/spec/Worktree.lean` §8-31). `present` says whether the branch head holds the
/// path and whether the disk does: a tracked file the head lacks is
/// removed, one the disk lacks is created, and any other is rewritten;
/// an untracked entry is removed.
fn count_change(work: &mut FileWork, kind: git2::CheckoutNotificationType, present: (bool, bool)) {
    let count = if kind.contains(git2::CheckoutNotificationType::UNTRACKED) {
        &mut work.removed
    } else {
        match present {
            (false, _) => &mut work.removed,
            (true, false) => &mut work.created,
            (true, true) => &mut work.rewritten,
        }
    };
    *count = count.saturating_add(1);
}

/// Writes the tree `index` holds into the object store and says whether
/// it is `tree`.
///
/// Where no entry changed since the index last wrote or read a tree, the
/// index's own tree cache answers without hashing anything; otherwise
/// only the subtrees whose entries changed are written. An index holding
/// a conflict writes no tree, and so is not `tree`.
fn index_writes_tree(index: &mut git2::Index, tree: git2::Oid) -> Result<bool, git2::Error> {
    match index.write_tree() {
        Ok(written) => Ok(written == tree),
        Err(err) if err.code() == git2::ErrorCode::Unmerged => Ok(false),
        Err(err) => Err(err),
    }
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::arithmetic_side_effects,
    reason = "test code"
)]
mod tests {
    use super::super::super::lease::FileWork;
    use super::super::Landing;
    use super::super::tests::{bulk_city, city, entries, name, owner};
    use crate::checkpoint::Checkpoint;
    use kernel::TimeMs;
    use std::path::Path;

    fn read(path: &Path) -> String {
        std::fs::read_to_string(path).unwrap()
    }

    /// `crates/storage/spec/Worktree.lean` §8-31: a placement writes the tree's files once and walks
    /// the city once; a reclaim writes what the last run changed in its
    /// scope and nothing else. Judged at two sizes, so a cost that grows
    /// with the tree where it should not is a failure here rather than a
    /// slower reading somewhere else.
    #[test]
    fn claiming_a_tree_costs_what_it_places_and_restores() {
        for files in [32, 64] {
            let dir = tempfile::tempdir().unwrap();
            let trees = bulk_city(dir.path(), files);
            let scope = ["bulk".to_owned()];
            let city = entries(dir.path());

            let placed = trees.claim(&name("node-1"), &scope).unwrap();
            let tree = placed.path().to_path_buf();
            assert_eq!(
                placed.work(),
                FileWork {
                    created: files,
                    rewritten: 0,
                    removed: 0,
                    walked: city,
                },
                "a placement at {files} files"
            );

            std::fs::write(tree.join("bulk").join("file-0000.txt"), b"changed by a run").unwrap();
            std::fs::write(tree.join("bulk").join("stray.txt"), b"left by a run").unwrap();
            trees.release(placed).unwrap();
            let restored = trees.claim(&name("node-1"), &scope).unwrap();
            assert_eq!(
                restored.work(),
                FileWork {
                    created: 0,
                    rewritten: 1,
                    removed: 1,
                    walked: entries(&tree),
                },
                "a reclaim after a run at {files} files"
            );

            trees.release(restored).unwrap();
            let unchanged = trees.claim(&name("node-1"), &scope).unwrap();
            assert_eq!(
                unchanged.work(),
                FileWork {
                    walked: entries(&tree),
                    ..FileWork::default()
                },
                "a reclaim with nothing to restore at {files} files"
            );
        }
    }

    /// A kept tree claimed again writes only its scope, and what the node
    /// offers from it moves only that scope: the trunk's work elsewhere,
    /// which this tree never checked out, lands in the city untouched.
    #[test]
    fn a_kept_tree_claimed_again_checks_out_only_its_scope_and_offers_only_that() {
        let dir = tempfile::tempdir().unwrap();
        let trees = city(dir.path());
        let city_docs = dir.path().join("docs").join("plan.md");
        let land_city = |t: u64| {
            Checkpoint::open(dir.path())
                .unwrap()
                .land(&[], TimeMs::new(t), &owner(), "checkpoint: city")
                .unwrap()
        };
        std::fs::create_dir_all(city_docs.parent().unwrap()).unwrap();
        std::fs::write(
            &city_docs, b"v1
",
        )
        .unwrap();
        land_city(1_500);
        let lab = ["lab".to_owned()];
        trees
            .release(trees.claim(&name("node-1"), &lab).unwrap())
            .unwrap();
        std::fs::write(
            &city_docs, b"v2
",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("lab").join("notes.md"),
            b"second
",
        )
        .unwrap();
        let trunk = land_city(2_000);

        let again = trees.claim(&name("node-1"), &lab).unwrap();
        assert_eq!(
            (
                read(&again.path().join("lab").join("notes.md")),
                read(&again.path().join("docs").join("plan.md"))
            ),
            (
                "second
"
                .to_owned(),
                "v1
"
                .to_owned()
            ),
            "the scope follows the trunk and nothing outside it is written"
        );

        std::fs::write(
            again.path().join("lab").join("notes.md"),
            b"from the node
",
        )
        .unwrap();
        Checkpoint::open(again.path())
            .unwrap()
            .land(&lab, TimeMs::new(3_000), &owner(), "offer: lab")
            .unwrap();
        let of = owner();
        trees
            .plan_merge(again.name())
            .unwrap()
            .apply(&Landing {
                t: TimeMs::new(4_000),
                of: &of,
                subject: "merge: node-1",
                reviewed_by_person: false,
            })
            .unwrap();
        let repo = git2::Repository::open(dir.path()).unwrap();
        let before = repo
            .find_commit(git2::Oid::from_str(&trunk).unwrap())
            .unwrap()
            .tree()
            .unwrap();
        let after = repo.head().unwrap().peel_to_tree().unwrap();
        let moved: Vec<String> = repo
            .diff_tree_to_tree(Some(&before), Some(&after), None)
            .unwrap()
            .deltas()
            .filter_map(|delta| delta.new_file().path().map(|p| p.display().to_string()))
            .collect();
        assert_eq!(
            (moved, read(&city_docs)),
            (
                vec!["lab/notes.md".to_owned()],
                "v2
"
                .to_owned()
            ),
            "the merge moves the scope alone"
        );
    }

    /// A path a run staged outside its scope and never offered does not
    /// ride along with the node's next offer, although the trunk did not
    /// move in between: the next claim finds an index that no longer
    /// names the branch head, and reads it back.
    #[test]
    fn a_path_staged_outside_the_scope_does_not_ride_along_with_the_next_offer() {
        let dir = tempfile::tempdir().unwrap();
        let trees = city(dir.path());
        let lab = ["lab".to_owned()];
        let first = trees.claim(&name("node-1"), &lab).unwrap();
        let stray = first.path().join("docs").join("stray.md");
        std::fs::create_dir_all(stray.parent().unwrap()).unwrap();
        std::fs::write(&stray, b"not this node's").unwrap();
        let staging = git2::Repository::open(first.path()).unwrap();
        let mut index = staging.index().unwrap();
        index.add_path(Path::new("docs/stray.md")).unwrap();
        index.write().unwrap();
        trees.release(first).unwrap();

        let again = trees.claim(&name("node-1"), &lab).unwrap();
        std::fs::write(again.path().join("lab").join("notes.md"), b"from the node").unwrap();
        Checkpoint::open(again.path())
            .unwrap()
            .land(&lab, TimeMs::new(3_000), &owner(), "offer: lab")
            .unwrap();
        let offered = git2::Repository::open(again.path()).unwrap();
        let head = offered.head().unwrap().peel_to_commit().unwrap();
        let moved: Vec<String> = offered
            .diff_tree_to_tree(
                Some(&head.parent(0).unwrap().tree().unwrap()),
                Some(&head.tree().unwrap()),
                None,
            )
            .unwrap()
            .deltas()
            .filter_map(|delta| delta.new_file().path().map(|p| p.display().to_string()))
            .collect();
        assert_eq!(
            moved,
            vec!["lab/notes.md".to_owned()],
            "the offer moves the scope alone"
        );
    }

    /// A node's next run finds its tree where it left it: releasing gives
    /// the lease back and keeps the files, so the next claim neither
    /// measures the city nor checks the whole tree out again.
    #[test]
    fn a_released_tree_stays_on_disk_for_the_nodes_next_run() {
        let dir = tempfile::tempdir().unwrap();
        let trees = city(dir.path());
        let lease = trees.claim(&name("node-1"), &[]).unwrap();
        let path = lease.path().to_path_buf();
        let notes = path.join("lab").join("notes.md");
        let written = std::fs::metadata(&notes).unwrap().modified().unwrap();

        trees.release(lease).unwrap();
        assert!(notes.exists(), "a released tree keeps its files");
        assert_eq!(trees.live().unwrap(), vec![name("node-1")]);

        let again = trees.claim(&name("node-1"), &[]).unwrap();
        // The same directory, spelled two ways: git answers with the long
        // name, and a Windows runner's temp directory hands out its 8.3 one.
        assert_eq!(
            std::fs::canonicalize(again.path()).unwrap(),
            std::fs::canonicalize(&path).unwrap()
        );
        assert_eq!(
            std::fs::metadata(&notes).unwrap().modified().unwrap(),
            written,
            "a file the node's branch already holds is not written again"
        );
        assert!(dir.path().join("lab").join("notes.md").exists());
    }

    /// A kept tree whose node has nothing unmerged follows the trunk: a
    /// node that starts from where the city was when it last ran would do
    /// its next work on files somebody has since changed, and every merge
    /// of it would be refused as stale.
    #[test]
    fn a_kept_tree_with_nothing_unmerged_starts_from_the_trunk_as_it_now_stands() {
        let dir = tempfile::tempdir().unwrap();
        let trees = city(dir.path());
        trees
            .release(trees.claim(&name("node-1"), &[]).unwrap())
            .unwrap();
        std::fs::write(
            dir.path().join("lab").join("notes.md"),
            b"second
    ",
        )
        .unwrap();
        Checkpoint::open(dir.path())
            .unwrap()
            .land(&[], TimeMs::new(2_000), &owner(), "checkpoint: lab")
            .unwrap();

        let again = trees.claim(&name("node-1"), &[]).unwrap();
        assert_eq!(
            std::fs::read_to_string(again.path().join("lab").join("notes.md")).unwrap(),
            "second
    "
        );
    }
}