Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .claude/skills/disk-reduction/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ python3 .claude/skills/disk-reduction/scripts/clean-shared-buckets.py
python3 .claude/skills/disk-reduction/scripts/clean-shared-buckets.py --apply
```

**Why buckets orphan so fast.** A bucket is `~/.cache/nub/shared-target-<key>`, where `<key>` hashes the content of the depended-on crates (`vendor/aube` and `crates`, excluding the leaves `nub-cli`/`nub-native`/`nub-phantom`). Two facts compound:
**Why buckets orphan so fast.** A bucket is `~/.cache/nub/shared-target-<key>`, where `<key>` hashes the content of the depended-on crates plus `runtime/` (`vendor/aube`, `crates` excluding the leaves `nub-cli`/`nub-native`/`nub-phantom`, and `runtime`). Two facts compound:

- **Only a NON-diverged worktree resolves to a bucket at all.** The moment a branch touches a depended-on crate, `rust-build.sh` sends it to a private `$root/target` instead — so most feature branches reference no bucket, and a bucket's referrers are only ever the worktrees sitting at some exact content state.
- **`main` advancing moves the key.** Every merge that touches `vendor/aube` or a non-leaf crate strands the previous bucket.
- **`main` advancing moves the key.** Every merge that touches `vendor/aube`, a non-leaf crate, or `runtime/` strands the previous bucket.

`rust-build.sh` has its own GC, but it only retires a bucket after **14 days** of untouched mtime, so a fortnight of churn accumulates first. On 2026-08-14, 13 of 15 buckets proved orphaned — 12 in the first pass, holding ~135 G by `du` and returning 73 GiB of `df`, plus a 15 G bucket that orphaned the moment its last worktree was removed. Re-run the audit after removing any worktree.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
":(exclude)crates/nub-native",
":(exclude)crates/nub-phantom",
]
CRATE_PATHS = ["vendor/aube", "crates"]
# `runtime` rides along for the same reason it joined rust-build.sh: a dev
# binary resolves runtime/*.cjs from the tree that compiled nub-core, so the
# hash covers it and a runtime-divergent worktree isolates.
CRATE_PATHS = ["vendor/aube", "crates", "runtime"]
BUCKET_RE = re.compile(r"^shared-target-([0-9a-f]+)$")


Expand Down
8 changes: 4 additions & 4 deletions .claude/skills/rust-build/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: >-
incremental builds, the cross-worktree artifact-contamination hazard that
sharing creates (the phantom `E0063: missing field` on correct source), and the
wrapper (`scripts/rust-build.sh`) that shares by default and auto-isolates the
moment a worktree diverges a depended-on crate. Auto-triggers on a spurious
moment a worktree diverges a depended-on crate or `runtime/`. Auto-triggers on a spurious
cargo compile error that names a field/symbol absent from your checkout, on
"worktree build contamination", and on setting CARGO_TARGET_DIR for a worktree.
metadata:
Expand Down Expand Up @@ -57,12 +57,12 @@ error[E0063]: missing field `lockfile_legacy_basenames` in initializer of `aube_

Sharers are grouped by the **content** of their depended-on crates, hashed into the bucket name (`shared-target-<hash>`):

- **Depended-on crate sources unmodified → share** that content's bucket. Everyone in it agrees by construction. The common case: feature work in `nub-cli`, integration tests, docs, non-Rust files.
- **Diverged a depended-on crate → isolate** to a private per-worktree `target/` (removed with the worktree), CoW-seeded from the matching bucket so you rebuild only what differs.
- **Depended-on crate sources and `runtime/` unmodified → share** that content's bucket. Everyone in it agrees by construction. The common case: feature work in `nub-cli`, integration tests, docs.
- **Diverged a depended-on crate or `runtime/` → isolate** to a private per-worktree `target/` (removed with the worktree), CoW-seeded from the matching bucket so you rebuild only what differs. `runtime/` is hashed for a different reason than the crates: a dev binary resolves `runtime/*.cjs` from the tree that compiled `nub-core` (baked `CARGO_MANIFEST_DIR`), so a shared-bucket binary loads whichever sharer compiled last — a worktree with edited runtime files would silently test a sibling's copy.

**Content, not merge-base:** a merge-base proves only that *this* worktree made no local changes against *its own* base, so two worktrees whose bases straddle a `nub-core`/`aube` commit both pass while disagreeing on content. The key hashes the git **index**, which only matters on the shared branch (no local changes by definition), so it moves on rebase, never mid-edit.

Depended-on crates = every workspace/vendored crate **except leaf artifacts nothing links**: `crates/nub-cli` (bin), `crates/nub-native` (cdylib, own workspace), `crates/nub-phantom` (bin, own workspace). So: `crates/nub-core`, `crates/nub-cache-key`, `crates/nub-phantom-core`, `crates/nub-phantom-scan`, and all of `vendor/aube`. `nub-phantom-core`/`nub-phantom-scan` are **not** leaves — `nub-cli` depends on both — and the pathspec `:(exclude)crates/nub-phantom` matches only that directory, not those siblings.
The hashed set = every workspace/vendored crate **except leaf artifacts nothing links** — `crates/nub-cli` (bin), `crates/nub-native` (cdylib, own workspace), `crates/nub-phantom` (bin, own workspace) — **plus `runtime/`**. So: `crates/nub-core`, `crates/nub-cache-key`, `crates/nub-phantom-core`, `crates/nub-phantom-scan`, all of `vendor/aube`, and `runtime/`. `nub-phantom-core`/`nub-phantom-scan` are **not** leaves — `nub-cli` depends on both — and the pathspec `:(exclude)crates/nub-phantom` matches only that directory, not those siblings.

## Letting the wrapper choose IS the caching strategy

Expand Down
52 changes: 32 additions & 20 deletions scripts/rust-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ leaves=":(exclude)crates/nub-cli :(exclude)crates/nub-native :(exclude)crates/nu
# Both checks are deliberately broad (any path under a depended-on crate, not just
# *.rs): over-isolating on an irrelevant file costs one cold build; under-isolating
# risks the clobber. Depended-on = every workspace/vendored crate except nub-cli.
# `runtime/` is in the set for a different reason than the crates: the binary
# resolves `runtime/*.cjs` at run time from the tree that compiled nub-core (its
# baked CARGO_MANIFEST_DIR), so a shared-bucket binary loads whichever SHARER
# compiled last — a worktree with edited runtime files would test a sibling's
# copy, silently. Isolating on runtime divergence keeps every shared-bucket
# binary pointed at base-identical runtime content. (Observed: three worktrees'
# red/green verdicts flipped as siblings rebuilt the common bucket.)
# `-C "$root"` on the git queries so the pathspecs resolve from the repo root
# regardless of the CWD the wrapper was invoked from (a subdir would otherwise
# misread them). The final `exec cargo` still runs in the original CWD.
Expand All @@ -105,17 +112,17 @@ diverged=""
if [ -n "$base" ]; then
# shellcheck disable=SC2086 # $leaves must word-split into separate pathspecs
diverged=$(git -C "$root" diff --name-only "$base" -- \
vendor/aube crates $leaves 2>/dev/null || true)
vendor/aube crates runtime $leaves 2>/dev/null || true)
fi
# shellcheck disable=SC2086
untracked=$(git -C "$root" ls-files --others --exclude-standard -- \
vendor/aube crates $leaves 2>/dev/null || true)
vendor/aube crates runtime $leaves 2>/dev/null || true)

# The content key names the bucket AND, when isolating, names the seed to clone
# from — so it is computed unconditionally. `ls-files -s` emits the staged blob
# OIDs, so this is a pure content hash of the depended-on crates. ~0.2s.
# shellcheck disable=SC2086
key=$(git -C "$root" ls-files -s -- vendor/aube crates $leaves 2>/dev/null \
key=$(git -C "$root" ls-files -s -- vendor/aube crates runtime $leaves 2>/dev/null \
Comment thread
pullfrog[bot] marked this conversation as resolved.
Resolved
| shasum 2>/dev/null | cut -c1-12 || true)
if [ "$keyed" = 1 ]; then
bucket="$shared${key:+-$key}"
Expand Down Expand Up @@ -188,14 +195,28 @@ if [ -n "$diverged" ] || [ -n "$untracked" ]; then
else
target="$bucket" # shared fast path, content-keyed
why="shared bucket ${key:-none} — depended-on crate content"
# MIGRATION: content-keying renames the bucket, so on the first run after this
# change lands the warm legacy dir would be orphaned and every worktree would
# eat one cold build. Seed the new bucket from it (same CoW clone, ~0 cost).
# Self-retiring: the legacy dir is covered by the GC below, so once every live
# worktree has moved to a keyed bucket it ages out on its own.
if [ "$target" != "$shared" ] && seed_from "$shared" "$target"; then
why="$why (migrated from the legacy shared dir)"
fi
# A SHARED bucket is NEVER seeded from another bucket. Any other bucket's
# hashed content differs by construction (a matching key would BE this bucket),
# CoW clones preserve mtimes, and cargo's path-dep freshness check
# (LocalFingerprint::CheckDepInfo) compares mtimes rather than content — so a
# sharer whose checkout is older than the seeded dep-info would accept a
# foreign rlib as fresh: the phantom E0063 this keying exists to prevent,
# persisting inside a bucket other worktrees join. A key move therefore costs
# the FIRST worktree one cold build and everyone else joins warm — that is the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cost accounting covers sharers but not isolated worktrees. mkdir -p (L222) and touch (L227) publish the new bucket before cargo writes anything into it, and newest_bucket() (L174-177) ranks purely by mtime with no warmth check — so for the duration of that first cold build, every isolated worktree seeds from an empty or half-built tree and prints (seeded from shared-target-…) while getting a cold build of its own.

Technical details
# The re-key cold build is not confined to the first sharer

## Affected sites
- `scripts/rust-build.sh:204-207` — "A key move therefore costs the FIRST worktree one cold build and everyone else joins warm" accounts only for the shared branch.
- `scripts/rust-build.sh:222,227` — `mkdir -p "$target"` then `touch "$target"` make the brand-new, still-empty bucket the newest by mtime before cargo has produced a single artifact.
- `scripts/rust-build.sh:174-177` — `newest_bucket()` sorts `ls -dt` and filters only `.seeding`, so that empty bucket is the top hit.
- `scripts/rust-build.sh:189-194` — a diverged worktree's own `$bucket` is keyed by its index and therefore never exists on disk, so `newest_bucket()` is the seed source for essentially every isolated worktree.
- `scripts/rust-build.sh:140-166` — `seed_from` succeeds on an empty source (`cp -a src/. dst/` copies nothing and exits 0), so `$why` reports a successful seed that inherited nothing.

## Required outcome
- An isolated worktree does not adopt a bucket that has no artifacts in it yet, and the `(seeded from ...)` message is not printed when nothing was actually inherited.

## Open questions for the human
- Seeding from a bucket a sibling is *actively building into* pairs a fresh-looking fingerprint with a partial rlib — the same hazard `newest_bucket()`'s `.seeding` filter already names in its own comment. That exposure is pre-existing in kind, but deleting the shared-branch seed makes a from-scratch bucket the guaranteed state after every merge that moves the key. Is bounding it worth a warmth check (e.g. requiring a non-empty `deps/`) here, or is the window short enough to accept?

# designed price. (The isolated branch may seed foreign content because its
# target is PRIVATE and its just-edited divergent files carry fresh mtimes.)
fi

# `--print-target` resolves the target dir the SAME way a real build would and
# prints it, so callers that must locate the artifact afterwards (make
# install-dev symlinking nub-dev) follow the shared/isolated decision instead of
# hardcoding a path that is only right in one of the two cases. It exits HERE,
# before the mkdir/GC below, so it is a pure read — tooling that compares
Comment thread
pullfrog[bot] marked this conversation as resolved.
Resolved
# against it (the disk-reduction bucket sweep's positive control) must not
# materialize an empty bucket as a side effect of asking.
if [ "${1:-}" = "--print-target" ]; then
printf '%s\n' "$target"
exit 0
fi

mkdir -p "$target"
Expand Down Expand Up @@ -243,15 +264,6 @@ if [ "${NUB_BUILD_FG:-}" != "1" ] && [ "$(uname)" = "Darwin" ] \
qos="taskpolicy -c utility"
fi

# `--print-target` resolves the target dir the SAME way a real build would and
# prints it, so callers that must locate the artifact afterwards (make
# install-dev symlinking nub-dev) follow the shared/isolated decision instead of
# hardcoding a path that is only right in one of the two cases.
if [ "${1:-}" = "--print-target" ]; then
printf '%s\n' "$target"
exit 0
fi

# The machine-global rustc wrapper (make qos-global) now carries the GLOBAL
# CONCURRENCY SEMAPHORE, not just a QoS clamp, so this invocation must let it
# run. It previously blanked RUSTC_WRAPPER — sound when the wrapper only
Expand Down
Loading