Skip to content

fix: sweep untracked data-free scaffold dirs when removing a directory - #347

Open
vosbek wants to merge 1 commit into
atomicdotdev:devfrom
vosbek:fix/switch-untracked-dir-removal
Open

vosbek wants to merge 1 commit into
atomicdotdev:devfrom
vosbek:fix/switch-untracked-dir-removal

Conversation

@vosbek

@vosbek vosbek commented Nov 24, 2026

Copy link
Copy Markdown

fix: sweep untracked data-free scaffold dirs when removing a directory

The bug

The plain quick-start flow is broken on every platform:

atomic init
atomic add hello.rs && atomic record -m "Initial commit"
atomic view create feature-x
atomic view switch feature-x
✗ Internal error: atomic:REPOSITORY: Invalid operation: cannot remove directory '.vault':
  untracked children are never removed recursively

Reproduced identically on Linux (WSL2, Ubuntu, rustc 1.98.0) and Windows
(x86_64-pc-windows-msvc, NTFS), with and without --force.

Root cause

init_vault() creates six scaffold subdirectories on disk
(goals/, memory/, skills/, intents/, scratch/, prompts/), but only the
four default files are tracked and recorded. goals/, intents/, and scratch/
hold no files, so they never get path claims.

When a switch targets a view that no longer projects .vault (e.g. a default
view create overlay, which anchors on the shared root with an empty own log),
the removal plan includes the recorded files, their recorded parent dirs, and
.vault itself — but nothing for the untracked scaffolds. The executor's
fs::remove_dir(".vault") then fails with DirectoryNotEmpty, and the operation
correctly fails closed ("untracked children are never removed recursively"),
leaving the switch aborted and recovered.

The bash harness never caught this: CI runs only suites 09 and 46
(ci.yml), and 01_single_file.sh — which exercises exactly this flow — is not
in the CI set.

The fix

Untracked directories that hold no files and no symlinks anywhere beneath
them
carry no user bytes, so their removal is safe to plan:

  • expand_removals_with_data_free_dirs (switch planner): expands the planned
    removal set with data-free untracked subdirectories, deepest first, before the
    removal scope is cloned — so every swept directory gets its own journaled
    effect, observation, and receipt.
  • remove_data_free_untracked_children (direct, unjournaled materialize path):
    sweeps the same class bottom-up immediately before the parent's remove_dir.

The fail-closed contract is unchanged for everything that matters: an untracked
file or symlink beneath a removed directory still blocks the removal
with the same guard, and directories with non-UTF-8 names are left for it too.
Mid-flight races degrade to the same DirectoryNotEmpty failure rather than
data loss. A Windows junction encountered as a "directory" is removed with
remove_dir, which unlinks the reparse point without touching the target.

Vault commands already create their directories on demand, so the scaffolds do
not need to survive a view round-trip; after switching back only the recorded
vault content returns.

Tests

atomic-repository/tests/it/switch_untracked_dir_removal_test.rs:

  • switch_to_empty_view_sweeps_untracked_scaffold_dirs — scaffold dirs
    (including nested ones) are planned and removed; fails on unfixed dev.
  • switch_to_empty_view_still_fails_closed_on_untracked_files — an untracked
    file beneath the removed directory keeps the fail-closed error and its bytes
    survive.
  • vault_scaffold_dirs_do_not_block_switching_away — the exact atomic init
    vault shape; switching away and back round-trips the recorded vault content.

Verified: with the fix, all three pass on Windows (MSVC/NTFS) and Linux
(Ubuntu/ext4); without the fix, the two sweep tests fail on both platforms.
cargo fmt --check and cargo clippy -p atomic-repository --all-features are
clean for the touched files (the three remaining warnings are pre-existing
#[cfg(windows)] sites never linted by the ubuntu clippy job).

`atomic init` records the vault defaults but leaves `goals/`, `intents/`,
and `scratch/` as untracked empty scaffolds on disk. Switching to a view
that no longer projects `.vault` planned the directory's removal while
those scaffolds remained, so `fs::remove_dir` failed with
DirectoryNotEmpty and the switch failed closed with "untracked children
are never removed recursively" -- on every platform, in the plain
quick-start flow (init -> view create -> view switch).

Plan the removal of untracked directories that hold no files and no
symlinks anywhere beneath them (they carry no user bytes) alongside the
parent directory, deepest first, in both the journaled switch planner
(`expand_removals_with_data_free_dirs`) and the direct materialize path
(`remove_data_free_untracked_children`). Untracked files and symlinks
beneath a removed directory still block the removal with the same
fail-closed guard, and non-UTF-8 directory names are left for it as
well. Vault commands already create their directories on demand, so the
scaffolds do not need to survive a view round-trip.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant