Skip to content
zit Artifact [3e69eb19f7]
Login

Artifact [3e69eb19f7]

Artifact 3e69eb19f7ce441e6661f5566b28c285053f4d6bef1895a02296709c11a3325b:


const std = @import("std");
const artifact = @import("zit").artifact;
const zit = @import("zit");
const support = @import("support.zig");
const ConeTreeStore = support.ConeTreeStore;
const addConeTreeChild = support.addConeTreeChild;
const addManaged = support.addManaged;
const adoptStore = support.adoptStore;
const appendArtifact = support.appendArtifact;
const b_seed_len = support.b_seed_len;
const buildConeTree = support.buildConeTree;
const collectArtifactDigests = support.collectArtifactDigests;
const commitHashDup = support.commitHashDup;
const coneTreeServer = support.coneTreeServer;
const cone_tree_flags = support.cone_tree_flags;
const erasureCloneOf = support.erasureCloneOf;
const erasureOk = support.erasureOk;
const erasureOrigin = support.erasureOrigin;
const erasure_seed_hex = support.erasure_seed_hex;
const exchangeWith = support.exchangeWith;
const expectArtifactReadsBack = support.expectArtifactReadsBack;
const expectContains = support.expectContains;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectFileContent = support.expectFileContent;
const expectSameDigestSet = support.expectSameDigestSet;
const fileExists = support.fileExists;
const forksOfJson = support.forksOfJson;
const freeDigests = support.freeDigests;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const holdRewriteLock = support.holdRewriteLock;
const initRepo = support.initRepo;
const leafSubManifest = support.leafSubManifest;
const runCommitEnv = support.runCommitEnv;
const runErasureZit = support.runErasureZit;
const runZit = support.runZit;
const sha3HexAlloc = support.sha3HexAlloc;
const shunDigestInRepo = support.shunDigestInRepo;
const spawnServe = support.spawnServe;
const storeHoldsArtifact = support.storeHoldsArtifact;
const storeShunRowCount = support.storeShunRowCount;
const waitForListen = support.waitForListen;
const withZCard = support.withZCard;
const writeFile = support.writeFile;

test "cli pull sync §4: a saved remote is resolved and used verbatim when the URL arg is omitted" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);

    // Save a remote nothing listens on — stored verbatim. `zit pull` with no
    // URL must resolve THIS URL and hand it to the transport, whose connect
    // failure quotes it back — proving verbatim use (not a usage error, not
    // "not a repo"). Port 1 is reserved and closed on every host.
    {
        var r = try runZit(gpa, io, tmp.dir, &.{ "remote", "http://127.0.0.1:1/xfer" });
        defer freeResult(gpa, &r);
        try expectExit(r.term, 0);
    }
    var r = try runZit(gpa, io, tmp.dir, &.{"pull"});
    defer freeResult(gpa, &r);
    try expectExit(r.term, 1); // a connect failure, not a usage error
    try std.testing.expect(std.mem.indexOf(u8, r.stderr, "http://127.0.0.1:1/xfer") != null);
}

// --- pull interop (sync §3.2/§3.3/§3.7) ------------------------------------
//
// The pull milestone e2e: an incremental `zit pull` into an EXISTING repo B over
// loopback HTTP. B is cloned from A, then A grows a second check-in, then B pulls
// A's new artifacts and its on-disk set must equal A's new full set — proving the
// pull client fetched exactly what B lacked and converged (sync §3.7), over the
// wire, not from any fossil oracle. `zit serve` reads a fixed snapshot of the
// repository at startup, so A is re-served after the second commit; the restart
// is a harness detail, not part of what pull is asked to prove.

test "cli pull: an incremental pull fetches a repo's new artifacts over HTTP (sync §3.2/§3.3/§3.7)" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;

    // Repo A: init + a committed file — A's first check-in, real artifacts to seed.
    var tmp_a = std.testing.tmpDir(.{});
    defer tmp_a.cleanup();
    try initRepo(gpa, io, tmp_a.dir);
    try writeFile(io, tmp_a.dir, "one.txt", "first artifact over http\n");
    try addManaged(gpa, io, tmp_a.dir, &.{"one.txt"});
    var commit1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
    defer freeResult(gpa, &commit1);
    try expectExit(commit1.term, 0);

    // Serve A's seed state and clone it into B; B now shares A's project-code and
    // holds A's first-check-in artifacts (the pull starts from a real subset).
    const clone_port = try freshLoopbackPort(io);
    var clone_server = try spawnServe(gpa, io, tmp_a.dir, clone_port);
    try waitForListen(io, clone_port);
    const clone_url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{clone_port});
    defer gpa.free(clone_url);
    var tmp_b = std.testing.tmpDir(.{});
    defer tmp_b.cleanup();
    var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", clone_url, "repo" });
    defer freeResult(gpa, &clone);
    try expectExitDiag(clone.term, 0, clone);
    // The seed server has served its purpose; stop it before A grows so the next
    // server starts from a snapshot that includes the second check-in.
    clone_server.kill(io);

    // A grows: a second committed file, so A now holds artifacts B does not.
    try writeFile(io, tmp_a.dir, "two.txt", "second artifact B has never seen\n");
    try addManaged(gpa, io, tmp_a.dir, &.{"two.txt"});
    var commit2 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "grow" });
    defer freeResult(gpa, &commit2);
    try expectExit(commit2.term, 0);

    // A's new full artifact-digest set is the convergence oracle for the pull.
    const a_digests = try collectArtifactDigests(gpa, io, tmp_a.dir);
    defer freeDigests(gpa, a_digests);
    try std.testing.expect(a_digests.len > b_seed_len(gpa, io, tmp_b.dir));

    // Re-serve A (now two check-ins) on a fresh port; pull from within B.
    const pull_port = try freshLoopbackPort(io);
    var pull_server = try spawnServe(gpa, io, tmp_a.dir, pull_port);
    defer pull_server.kill(io);
    try waitForListen(io, pull_port);
    const pull_url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{pull_port});
    defer gpa.free(pull_url);

    var b_repo = try tmp_b.dir.openDir(io, "repo", .{});
    defer b_repo.close(io);
    var pull = try runZit(gpa, io, b_repo, &.{ "pull", pull_url });
    defer freeResult(gpa, &pull);
    try expectExitDiag(pull.term, 0, pull);
    try std.testing.expectEqualStrings("", pull.stderr);

    // THE PROOF: B's on-disk artifact set now equals A's NEW full set — B received
    // the second check-in's artifacts over the wire and converged (sync §3.7). A
    // pull that fetched nothing (or only part) fails here; the assertion is not weakened.
    const b_digests = try collectArtifactDigests(gpa, io, b_repo);
    defer freeDigests(gpa, b_digests);
    try expectSameDigestSet(a_digests, b_digests);
}

// --- incremental pull, no server restart (sync §3.8) -----------------------
//
// Direction A of the fossil incremental-pull interop: a `zit serve` must
// advertise the CURRENT on-disk repository per request, so a check-in committed
// to A *while the server is live* is pulled by B without restarting serve. The
// server is stateless per request (§3.8), so it re-reads A's log every request
// rather than snapshotting its holdings once at startup. One server, spawned
// before the second commit and never restarted, is the whole point.

test "cli pull: a commit made while the server runs is pulled without a restart (sync §3.8)" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;

    // Repo A: init + a first committed file — real seed artifacts.
    var tmp_a = std.testing.tmpDir(.{});
    defer tmp_a.cleanup();
    try initRepo(gpa, io, tmp_a.dir);
    try writeFile(io, tmp_a.dir, "one.txt", "first artifact over http\n");
    try addManaged(gpa, io, tmp_a.dir, &.{"one.txt"});
    var commit1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
    defer freeResult(gpa, &commit1);
    try expectExit(commit1.term, 0);

    // ONE server for A, spawned now and never restarted — the invariant under test.
    const port = try freshLoopbackPort(io);
    var server = try spawnServe(gpa, io, tmp_a.dir, port);
    defer server.kill(io);
    try waitForListen(io, port);
    const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{port});
    defer gpa.free(url);

    // Clone A into B over the live server; B holds A's seed check-in.
    var tmp_b = std.testing.tmpDir(.{});
    defer tmp_b.cleanup();
    var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url, "repo" });
    defer freeResult(gpa, &clone);
    try expectExitDiag(clone.term, 0, clone);
    var b_repo = try tmp_b.dir.openDir(io, "repo", .{});
    defer b_repo.close(io);
    const seed_digests = try collectArtifactDigests(gpa, io, b_repo);
    const seed_len = seed_digests.len;
    freeDigests(gpa, seed_digests);

    // A grows a SECOND check-in while the SAME server keeps running — no restart.
    try writeFile(io, tmp_a.dir, "two.txt", "second artifact committed with serve still live\n");
    try addManaged(gpa, io, tmp_a.dir, &.{"two.txt"});
    var commit2 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "grow" });
    defer freeResult(gpa, &commit2);
    try expectExit(commit2.term, 0);

    // A's new full digest set is the convergence oracle.
    const a_digests = try collectArtifactDigests(gpa, io, tmp_a.dir);
    defer freeDigests(gpa, a_digests);
    try std.testing.expect(a_digests.len > seed_len);

    // Pull from B against the still-live server. It MUST see the second check-in.
    var pull = try runZit(gpa, io, b_repo, &.{ "pull", url });
    defer freeResult(gpa, &pull);
    try expectExitDiag(pull.term, 0, pull);
    try std.testing.expectEqualStrings("", pull.stderr);

    // THE PROOF: B's on-disk artifact set equals A's NEW full set — the server
    // advertised the live commit and B converged (§3.7), no restart involved.
    const b_digests = try collectArtifactDigests(gpa, io, b_repo);
    defer freeDigests(gpa, b_digests);
    try expectSameDigestSet(a_digests, b_digests);
}

test "cli pull submanifests §7.1/§7.3: two peers converge a tree across pull rounds" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;

    // Repo A: a real repository with a real check-in, so clone has a project
    // code and a checkout to work from.
    var tmp_a = std.testing.tmpDir(.{});
    defer tmp_a.cleanup();
    try initRepo(gpa, io, tmp_a.dir);
    try writeFile(io, tmp_a.dir, "one.txt", "seed\n");
    try addManaged(gpa, io, tmp_a.dir, &.{"one.txt"});
    var commit1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
    defer freeResult(gpa, &commit1);
    try expectExit(commit1.term, 0);

    // The tree: two leaves holding contiguous slices of one sorted namespace
    // (submanifests §3.6), and the check-in manifest that references them. Each
    // S card's filename is the first filename of its subtree (§5.1 steps 4/5)
    // and the run strictly ascends (§3.4).
    const leaf_ab = try leafSubManifest(gpa, &.{ "a.txt", "b.txt" });
    defer gpa.free(leaf_ab);
    const leaf_mn = try leafSubManifest(gpa, &.{ "m.txt", "n.txt" });
    defer gpa.free(leaf_mn);
    var name_buf_ab: [64]u8 = undefined;
    var name_buf_mn: [64]u8 = undefined;
    const hash_ab = artifact.artifactName(.sha3_256, leaf_ab, &name_buf_ab);
    const hash_mn = artifact.artifactName(.sha3_256, leaf_mn, &name_buf_mn);

    const tree_body = try std.fmt.allocPrint(
        gpa,
        "C tree\nD 2026-07-27T12:00:00.000\nS a.txt {s}\nS m.txt {s}\nU u\n",
        .{ hash_ab, hash_mn },
    );
    defer gpa.free(tree_body);
    const tree = try withZCard(gpa, tree_body);
    defer gpa.free(tree);

    // ROUND 0: A holds the tree manifest and NEITHER leaf — the §7.1 phantom
    // state, staged deliberately so B must converge across two pulls.
    const tree_hash = try appendArtifact(gpa, io, tmp_a.dir, tree);
    defer gpa.free(tree_hash);

    const port0 = try freshLoopbackPort(io);
    var server0 = try spawnServe(gpa, io, tmp_a.dir, port0);
    defer server0.kill(io);
    try waitForListen(io, port0);
    const url0 = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{port0});
    defer gpa.free(url0);

    var tmp_b = std.testing.tmpDir(.{});
    defer tmp_b.cleanup();
    var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url0, "repo" });
    defer freeResult(gpa, &clone);

    // §7.1 ON THE CLONE PATH. A clone converges on the cfile cursor, so it
    // faithfully copies what A holds — INCLUDING A's own incomplete closure.
    // "Storing a structural artifact that carries S cards whose referenced
    // sub-manifests are absent MUST create a phantom for each missing
    // reference"; the reference the phantom stands for is precisely what B does
    // not have, so a clone that reports success here would hand the operator a
    // repository whose gap it recorded nowhere and would never ask for. The
    // repository is still written in full — a partial clone is progress — but
    // the exit status says the closure is open, exactly as `pull` already does
    // for artifacts it did not obtain.
    try std.testing.expect(clone.term != .exited or clone.term.exited != 0);
    try std.testing.expect(std.mem.indexOf(u8, clone.stderr, "sub-manifest") != null);
    try std.testing.expect(std.mem.indexOf(u8, clone.stderr, "2 ") != null); // both leaves, not one

    var b_repo = try tmp_b.dir.openDir(io, "repo", .{});
    defer b_repo.close(io);

    // MID-CONVERGENCE, §5.2: B holds the check-in but not its sub-manifests, so
    // its file set MUST NOT be produced. `ls` fails rather than printing the
    // part of the tree B can already reach — a partial enumeration presented as
    // a file set is the exact failure the section forbids.
    var partial = try runZit(gpa, io, b_repo, &.{ "ls", tree_hash });
    defer freeResult(gpa, &partial);
    try std.testing.expect(partial.term != .exited or partial.term.exited != 0);
    try std.testing.expectEqualStrings("", partial.stdout);
    try std.testing.expect(partial.stderr.len != 0);

    // The same holds on A, which is missing the same two artifacts: the gap is
    // a property of the repository's holdings, not of having synced.
    var partial_a = try runZit(gpa, io, tmp_a.dir, &.{ "ls", tree_hash });
    defer freeResult(gpa, &partial_a);
    try std.testing.expectEqualStrings("", partial_a.stdout);

    // ROUND 1: the sub-manifests arrive at A, and B pulls again.
    const got_ab = try appendArtifact(gpa, io, tmp_a.dir, leaf_ab);
    defer gpa.free(got_ab);
    const got_mn = try appendArtifact(gpa, io, tmp_a.dir, leaf_mn);
    defer gpa.free(got_mn);
    try std.testing.expectEqualStrings(hash_ab, got_ab); // the S card names what arrived
    try std.testing.expectEqualStrings(hash_mn, got_mn);

    const port1 = try freshLoopbackPort(io);
    var server1 = try spawnServe(gpa, io, tmp_a.dir, port1);
    defer server1.kill(io);
    try waitForListen(io, port1);
    const url1 = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{port1});
    defer gpa.free(url1);

    var pull = try runZit(gpa, io, b_repo, &.{ "pull", url1 });
    defer freeResult(gpa, &pull);
    try expectExitDiag(pull.term, 0, pull);

    // THE PROOF: B's holdings equal A's, and the tree now enumerates to its
    // complete file set — the concatenation of the two leaves' F runs in S card
    // order (submanifests §5.1), which is also the sorted namespace.
    const a_digests = try collectArtifactDigests(gpa, io, tmp_a.dir);
    defer freeDigests(gpa, a_digests);
    const b_digests = try collectArtifactDigests(gpa, io, b_repo);
    defer freeDigests(gpa, b_digests);
    try expectSameDigestSet(a_digests, b_digests);

    var listed = try runZit(gpa, io, b_repo, &.{ "ls", tree_hash });
    defer freeResult(gpa, &listed);
    try expectExitDiag(listed.term, 0, listed);

    // Built from the same content hashes the leaves' F cards carry, so the
    // expectation is derived from the fixture rather than transcribed from a
    // passing run.
    var expected: std.ArrayList(u8) = .empty;
    defer expected.deinit(gpa);
    for ([_][]const u8{ "a.txt", "b.txt", "m.txt", "n.txt" }) |name| {
        const blob = try sha3HexAlloc(gpa, name);
        defer gpa.free(blob);
        try expected.print(gpa, "{s}\n", .{name}); // names only; `blob` is what --long would add
    }
    try std.testing.expectEqualStrings(expected.items, listed.stdout);
}

fn digestSetContains(set: [][]u8, needle: []const u8) bool {
    for (set) |d| {
        if (std.mem.eql(u8, d, needle)) return true;
    }
    return false;
}

test "cli pull: an artifact in the repository shun table is not fetched from a peer that holds it (sync §3.3 L553-556)" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;

    // Repo A: init + a first check-in.
    var tmp_a = std.testing.tmpDir(.{});
    defer tmp_a.cleanup();
    try initRepo(gpa, io, tmp_a.dir);
    try writeFile(io, tmp_a.dir, "one.txt", "first artifact\n");
    try addManaged(gpa, io, tmp_a.dir, &.{"one.txt"});
    var commit1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
    defer freeResult(gpa, &commit1);
    try expectExit(commit1.term, 0);

    // Clone A's seed state into B, then let A grow past it.
    const clone_port = try freshLoopbackPort(io);
    var clone_server = try spawnServe(gpa, io, tmp_a.dir, clone_port);
    try waitForListen(io, clone_port);
    const clone_url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{clone_port});
    defer gpa.free(clone_url);
    var tmp_b = std.testing.tmpDir(.{});
    defer tmp_b.cleanup();
    var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", clone_url, "repo" });
    defer freeResult(gpa, &clone);
    try expectExitDiag(clone.term, 0, clone);
    clone_server.kill(io);

    try writeFile(io, tmp_a.dir, "two.txt", "an artifact B will shun\n");
    try addManaged(gpa, io, tmp_a.dir, &.{"two.txt"});
    var commit2 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "grow" });
    defer freeResult(gpa, &commit2);
    try expectExit(commit2.term, 0);

    var b_repo = try tmp_b.dir.openDir(io, "repo", .{});
    defer b_repo.close(io);

    // Everything A gained past the clone — exactly what an unshunned pull would
    // fetch — is shunned in B.
    const a_digests = try collectArtifactDigests(gpa, io, tmp_a.dir);
    defer freeDigests(gpa, a_digests);
    const b_before = try collectArtifactDigests(gpa, io, b_repo);
    defer freeDigests(gpa, b_before);
    var shunned: usize = 0;
    for (a_digests) |d| {
        if (digestSetContains(b_before, d)) continue;
        try shunDigestInRepo(gpa, io, b_repo, d);
        shunned += 1;
    }
    try std.testing.expect(shunned > 0); // the pull has something to decline

    // Pull from A. A announces every one of those hashes, B mints their phantoms
    // and asks for none of them.
    const pull_port = try freshLoopbackPort(io);
    var pull_server = try spawnServe(gpa, io, tmp_a.dir, pull_port);
    defer pull_server.kill(io);
    try waitForListen(io, pull_port);
    const pull_url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{pull_port});
    defer gpa.free(pull_url);
    var pull = try runZit(gpa, io, b_repo, &.{ "pull", pull_url });
    defer freeResult(gpa, &pull);

    // Exit 0 and a silent stderr: a deliberately-suppressed phantom is not an
    // unmet expectation, so the §3.7 clean terminal is a SUCCESS. Were it counted,
    // `zit pull` would report "the peer holds N artifacts this pull did not
    // obtain" here and on every later pull, forever, for obeying the shun rows.
    try expectExitDiag(pull.term, 0, pull);
    try std.testing.expectEqualStrings("", pull.stderr);

    // THE PROOF: B's on-disk artifact set is unchanged — not one shunned artifact
    // came over the wire.
    const b_after = try collectArtifactDigests(gpa, io, b_repo);
    defer freeDigests(gpa, b_after);
    try expectSameDigestSet(b_before, b_after);

    // The same guard for `zit sync`, whose pull half runs the very same §3.3 scan
    // — the filter is a property of the operation, not of one verb's plumbing.
    var sync_run = try runZit(gpa, io, b_repo, &.{ "sync", pull_url });
    defer freeResult(gpa, &sync_run);
    try expectExitDiag(sync_run.term, 0, sync_run);
    const b_after_sync = try collectArtifactDigests(gpa, io, b_repo);
    defer freeDigests(gpa, b_after_sync);
    try expectSameDigestSet(b_before, b_after_sync);
}

test "cli pull on a sparse store: elided phantoms are never spontaneously re-requested" {
    // Sparse-cone RFC section 5.2 rule 3 over the real wire: after a sparse
    // clone, a pull against the same dense server must not re-download the
    // elided blob (the store's byte size is the witness), while a NEW commit
    // on the server -- touching the cone -- still arrives. Suppression
    // narrows requests, never freshness.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp_a = std.testing.tmpDir(.{});
    defer tmp_a.cleanup();
    try initRepo(gpa, io, tmp_a.dir);
    try tmp_a.dir.createDir(io, "src", .default_dir);
    try tmp_a.dir.createDir(io, "tools", .default_dir);
    try writeFile(io, tmp_a.dir, "src/a.zig", "one\n");
    try writeFile(io, tmp_a.dir, "tools/big.bin", "elidable payload\n");
    try addManaged(gpa, io, tmp_a.dir, &.{ "src/a.zig", "tools/big.bin" });
    var c1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
    defer freeResult(gpa, &c1);
    try expectExit(c1.term, 0);

    const port = try freshLoopbackPort(io);
    var server = try spawnServe(gpa, io, tmp_a.dir, port);
    defer server.kill(io);
    try waitForListen(io, port);
    const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
    defer gpa.free(url);

    var tmp_b = std.testing.tmpDir(.{});
    defer tmp_b.cleanup();
    var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--cone=src", url, "repo" });
    defer freeResult(gpa, &clone);
    try expectExitDiag(clone.term, 0, clone);
    var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
    defer b_dir.close(io);

    const size_after_clone = blk: {
        const st = try b_dir.statFile(io, ".zit", .{});
        break :blk st.size;
    };

    // An idle pull: nothing new on the server, and the elided phantom must
    // stay suppressed -- the store may not grow.
    var idle = try runZit(gpa, io, b_dir, &.{"pull"});
    defer freeResult(gpa, &idle);
    try expectExitDiag(idle.term, 0, idle);
    const size_after_idle = blk: {
        const st = try b_dir.statFile(io, ".zit", .{});
        break :blk st.size;
    };
    try std.testing.expectEqual(size_after_clone, size_after_idle);

    // Freshness intact: a new in-cone commit on the server arrives on pull.
    // (The server child holds its own snapshot; restart it to serve the new
    // commit.)
    server.kill(io);
    try writeFile(io, tmp_a.dir, "src/a.zig", "two\n");
    var c2 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "more" });
    defer freeResult(gpa, &c2);
    try expectExit(c2.term, 0);
    server = try spawnServe(gpa, io, tmp_a.dir, port);
    try waitForListen(io, port);

    var fresh = try runZit(gpa, io, b_dir, &.{"pull"});
    defer freeResult(gpa, &fresh);
    try expectExitDiag(fresh.term, 0, fresh);
    const size_after_fresh = blk: {
        const st = try b_dir.statFile(io, ".zit", .{});
        break :blk st.size;
    };
    try std.testing.expect(size_after_fresh > size_after_idle);
}

test "cli pull under a store cone takes a check-in whose out-of-cone sub-manifest nodes the server omits" {
    // Goal: the pull half of sparse-cone section 5.1, apart from clone. A new
    // check-in's out-of-cone nodes can be classified only once its tree has
    // arrived, so the run must settle them as elided rather than as nodes the
    // peer failed to send. Method: a sparse store adopted from the server's
    // own, then a pull of a child check-in and an open of it.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var src_tmp = std.testing.tmpDir(.{});
    defer src_tmp.cleanup();
    try initRepo(gpa, io, src_tmp.dir);
    var en = try runZit(gpa, io, src_tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
    defer freeResult(gpa, &en);
    try expectExitDiag(en.term, 0, en);
    var t: ConeTreeStore = .{ .gpa = gpa, .io = io, .dir = src_tmp.dir };
    defer t.deinit();
    const tree = try buildConeTree(&t, false);

    var sparse = std.testing.tmpDir(.{});
    defer sparse.cleanup();
    try initRepo(gpa, io, sparse.dir);
    try adoptStore(gpa, io, src_tmp.dir, sparse.dir);
    var store_cone = try zit.cone.Cone.init(gpa, &.{ "lib", "src" }, &.{"src/gen"});
    defer store_cone.deinit(gpa);
    zit.repocache.writeStoreCone(gpa, io, sparse.dir, &store_cone);

    const tip2 = try addConeTreeChild(&t, tree);
    var server = try coneTreeServer(gpa, io, src_tmp.dir);
    defer gpa.free(server.url);
    defer server.child.kill(io);

    var pull = try runZit(gpa, io, sparse.dir, &.{ "pull", server.url });
    defer freeResult(gpa, &pull);
    try expectExitDiag(pull.term, 0, pull);
    var op = try runZit(gpa, io, sparse.dir, &(.{"open"} ++ cone_tree_flags ++ .{tip2}));
    defer freeResult(gpa, &op);
    try expectExitDiag(op.term, 0, op);
    try expectFileContent(gpa, io, sparse.dir, "src/y", "src/y");
    try std.testing.expect(!fileExists(io, sparse.dir, "docs/a"));
}

test "cli pull and open under a horizon and a cone take a check-in whose out-of-cone sub-manifest nodes the server omits" {
    // Goal: slot elision under a horizon. The slot walk runs over K's trees,
    // so a pull settles a new check-in's omitted out-of-cone nodes as elided
    // and an open inside the cone reaches the check-in; an open that widens
    // the cone past them fetches the nodes while the store keeps its
    // horizon. Method: the store cone pull test's sparse store, its cone
    // given a horizon at the first check-in, opened narrow and then, in a
    // second store once the server holds the node, wide.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var src_tmp = std.testing.tmpDir(.{});
    defer src_tmp.cleanup();
    try initRepo(gpa, io, src_tmp.dir);
    var en = try runZit(gpa, io, src_tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
    defer freeResult(gpa, &en);
    try expectExitDiag(en.term, 0, en);
    var t: ConeTreeStore = .{ .gpa = gpa, .io = io, .dir = src_tmp.dir };
    defer t.deinit();
    const tree = try buildConeTree(&t, false);
    const tip2 = try addConeTreeChild(&t, tree);
    var store_cone = try zit.cone.Cone.initHorizon(gpa, &.{ "lib", "src" }, &.{"src/gen"}, .{ .hash = tree.tip, .rule = .descendants });
    defer store_cone.deinit(gpa);
    var server = try coneTreeServer(gpa, io, src_tmp.dir);
    defer gpa.free(server.url);
    defer server.child.kill(io);

    for ([_]bool{ false, true }) |widen| {
        var sparse = std.testing.tmpDir(.{});
        defer sparse.cleanup();
        try initRepo(gpa, io, sparse.dir);
        try adoptStore(gpa, io, src_tmp.dir, sparse.dir);
        zit.repocache.writeStoreCone(gpa, io, sparse.dir, &store_cone);
        var remote = try runZit(gpa, io, sparse.dir, &.{ "remote", server.url });
        defer freeResult(gpa, &remote);
        try expectExitDiag(remote.term, 0, remote);
        var pull = try runZit(gpa, io, sparse.dir, &.{"pull"});
        defer freeResult(gpa, &pull);
        try expectExitDiag(pull.term, 0, pull);
        if (!widen) {
            // A dense open of that check-in needs the omitted nodes; with no
            // remote to supply them it must say the sparse cone holds them
            // back, not that the store lost a baseline.
            var unset = try runZit(gpa, io, sparse.dir, &.{ "remote", "--unset" });
            defer freeResult(gpa, &unset);
            try expectExitDiag(unset.term, 0, unset);
            var dense = try runZit(gpa, io, sparse.dir, &.{ "open", tip2 });
            defer freeResult(gpa, &dense);
            try expectExit(dense.term, 1);
            try std.testing.expect(std.mem.indexOf(u8, dense.stderr, "sparse cone") != null);
            try std.testing.expect(std.mem.indexOf(u8, dense.stderr, "baseline is not found") == null);
            var op = try runZit(gpa, io, sparse.dir, &(.{"open"} ++ cone_tree_flags ++ .{tip2}));
            defer freeResult(gpa, &op);
            try expectExitDiag(op.term, 0, op);
            try expectFileContent(gpa, io, sparse.dir, "src/y", "src/y");
            try std.testing.expect(!fileExists(io, sparse.dir, "docs/a"));
            continue;
        }
        _ = try t.leaf(&.{ "docs/a", "docs/b" }, true);
        try std.testing.expectEqualStrings(tree.docs, t.names.items[t.names.items.len - 1]);
        var op = try runZit(gpa, io, sparse.dir, &(.{ "open", "--cone=docs" } ++ cone_tree_flags ++ .{tip2}));
        defer freeResult(gpa, &op);
        try expectExitDiag(op.term, 0, op);
        try expectFileContent(gpa, io, sparse.dir, "docs/a", "docs/a");
        try expectFileContent(gpa, io, sparse.dir, "src/y", "src/y");
        var after = zit.repocache.readStoreCone(gpa, io, sparse.dir) orelse return error.TestUnexpectedResult;
        defer after.deinit(gpa);
        try std.testing.expect(after.horizon != null);
        try std.testing.expectEqualStrings(tree.tip, after.horizon.?.hash);
        try std.testing.expect(after.covers("docs/a"));
    }
}

test "cli pull: an artifact stored as a delta of a shunned basis is sent whole to a peer that never held the basis" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.createDir(io, "a", .default_dir);
    var a = try tmp.dir.openDir(io, "a", .{});
    defer a.close(io);
    try initRepo(gpa, io, a);
    try writeFile(io, a, "other.txt", "other\n");
    try addManaged(gpa, io, a, &.{"other.txt"});
    const c0 = try commitHashDup(gpa, io, a, "seed");
    gpa.free(c0);

    // b is cloned before v1 exists, so it can only get v1 from a, and a will
    // never serve it.
    {
        const port = try freshLoopbackPort(io);
        var server = try spawnServe(gpa, io, a, port);
        defer server.kill(io);
        try waitForListen(io, port);
        const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
        defer gpa.free(url);
        var clone = try runZit(gpa, io, tmp.dir, &.{ "clone", url, "b" });
        defer freeResult(gpa, &clone);
        try expectExitDiag(clone.term, 0, clone);
    }
    var b = try tmp.dir.openDir(io, "b", .{});
    defer b.close(io);

    // v2 is stored as a delta of v1; v1 is then shunned with its frame kept.
    var body: std.ArrayList(u8) = .empty;
    defer body.deinit(gpa);
    for (0..200) |i| try body.print(gpa, "line {d} of a file long enough to be stored as a delta\n", .{i});
    try writeFile(io, a, "big.txt", body.items);
    try addManaged(gpa, io, a, &.{"big.txt"});
    const c1 = try commitHashDup(gpa, io, a, "v1");
    gpa.free(c1);
    var v1_name: [64]u8 = undefined;
    const v1 = artifact.artifactName(.sha3_256, body.items, &v1_name);
    try body.appendSlice(gpa, "one more line\n");
    try writeFile(io, a, "big.txt", body.items);
    const c2 = try commitHashDup(gpa, io, a, "v2");
    gpa.free(c2);
    {
        var sh = try runZit(gpa, io, a, &.{ "shun", "--defer", "--force", v1 });
        defer freeResult(gpa, &sh);
        try expectExitDiag(sh.term, 0, sh);
    }

    // Sent as a delta, v2 would sit in b on a basis b can never get.
    _ = try exchangeWith(gpa, io, a, b, "pull");
    var name_buf: [64]u8 = undefined;
    const v2 = artifact.artifactName(.sha3_256, body.items, &name_buf);
    try expectArtifactReadsBack(gpa, io, b, v2);
}

test "cli pull: latched erasures a held rewrite lock keeps out are named on stderr, and the next pull records them" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    const names = try erasureOrigin(gpa, io, tmp.dir);
    defer gpa.free(names.secret);
    defer gpa.free(names.other);
    var a = try tmp.dir.openDir(io, "a", .{});
    defer a.close(io);
    try erasureCloneOf(gpa, io, tmp.dir, a, &.{"b"});
    var b = try tmp.dir.openDir(io, "b", .{});
    defer b.close(io);
    try erasureOk(gpa, io, a, .{ .key = erasure_seed_hex }, &.{ "shun", "--force", "--sign", "--now", names.secret });
    const port = try freshLoopbackPort(io);
    var server = try spawnServe(gpa, io, a, port);
    defer server.kill(io);
    try waitForListen(io, port);
    const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
    defer gpa.free(url);
    {
        var held = try holdRewriteLock(io, b);
        defer held.release(io);
        var r = try runErasureZit(gpa, io, b, .{}, &.{ "pull", url });
        defer freeResult(gpa, &r);
        // The exchange succeeded, so its exit stands; what did not land is said.
        try expectExitDiag(r.term, 0, r);
        try expectContains(r.stderr, "were not recorded: another rewrite of this store is in progress");
    }
    try std.testing.expectEqual(@as(usize, 0), try storeShunRowCount(gpa, io, b));
    try erasureOk(gpa, io, b, .{}, &.{ "pull", url });
    try std.testing.expect(!try storeHoldsArtifact(gpa, io, b, names.secret));
}

test "cli pull and sync -F json: erasures a held rewrite lock keeps out are counted in erasures_deferred, and the next session reports 0" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    const names = try erasureOrigin(gpa, io, tmp.dir);
    defer gpa.free(names.secret);
    defer gpa.free(names.other);
    var a = try tmp.dir.openDir(io, "a", .{});
    defer a.close(io);
    try erasureCloneOf(gpa, io, tmp.dir, a, &.{ "b", "c" });
    try erasureOk(gpa, io, a, .{ .key = erasure_seed_hex }, &.{ "shun", "--force", "--sign", "--now", names.secret });
    const port = try freshLoopbackPort(io);
    var server = try spawnServe(gpa, io, a, port);
    defer server.kill(io);
    try waitForListen(io, port);
    const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
    defer gpa.free(url);
    for ([_][]const u8{ "pull", "sync" }, [_][]const u8{ "b", "c" }) |verb, sub| {
        var d = try tmp.dir.openDir(io, sub, .{});
        defer d.close(io);
        {
            var held = try holdRewriteLock(io, d);
            defer held.release(io);
            var r = try runErasureZit(gpa, io, d, .{}, &.{ verb, "-F", "json", url });
            defer freeResult(gpa, &r);
            try expectExitDiag(r.term, 0, r);
            try expectContains(r.stderr, "were not recorded: another rewrite of this store is in progress");
            var parsed: std.json.Parsed(std.json.Value) = undefined;
            _ = try forksOfJson(gpa, r.stdout, &parsed);
            defer parsed.deinit();
            try std.testing.expect(parsed.value.object.get("erasures_deferred").?.integer > 0);
        }
        try std.testing.expect(try storeHoldsArtifact(gpa, io, d, names.secret));
        var r = try runErasureZit(gpa, io, d, .{}, &.{ verb, "-F", "json", url });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
        var parsed: std.json.Parsed(std.json.Value) = undefined;
        _ = try forksOfJson(gpa, r.stdout, &parsed);
        defer parsed.deinit();
        try std.testing.expectEqual(@as(i64, 0), parsed.value.object.get("erasures_deferred").?.integer);
        try std.testing.expect(!try storeHoldsArtifact(gpa, io, d, names.secret));
    }
}