Repository navigation
fix(worktrees): keep an unregistered task directory's content without --force - #1986
Conversation
… --force An unregistered task directory was deleted with `remove_dir_all` even without `--force`, so ordinary files and symlinks inside it were removed too. The "not empty" refusal covered only plain subdirectories, and it ran after earlier subdirectories had already been deleted. The final deletion dropped its error and still reported `removed: true`. Without `--force`, refuse any child that is not a plain directory, and any plain subdirectory that is not empty, with `worktree_in_use`. An empty `<repo-name>` subdirectory that `worktree.list` reports as unregistered is still cleaned. The first pass only checks every child; the directory is deleted once, only when all checks pass, and a failed deletion reports `worktree_store_failed`. A `.git` entry directly in the task directory now refuses even with `--force`, as the managed-worktree contract requires; the message names the path and leaves it to `git worktree remove`. Add flow tests for the file, empty-subdirectory, all-children, deletion-failure, symlink and `.git` cases. Fixes #1981
The removal checked every child and then deleted the task directory recursively, so content written after the check was also deleted without `--force`. Without `--force`, remove only the empty subdirectories the check recorded and the task directory itself, each with a non-recursive delete, so content that appears after the check returns `worktree_in_use` instead of being deleted. A `.git` entry is detected with `symlink_metadata`, so a dangling symlink also refuses the removal. Cover the deletion step directly with unit tests for content that appears after the check, restore the permission of the failed-deletion test on panic and skip it where file permissions do not bind the test user, and refuse a dangling `.git` symlink with `--force`. Fixes #1981
eric8810
left a comment
There was a problem hiding this comment.
The preflight and non-recursive no-force deletion address the original file-loss case. The new tests also cover dangling .git and make a failed deletion visible to callers.
[P2] Refuse deletion when a stored Session uses the task directory itself. crates/dim-app-server/src/worktrees.rs:857–866 checks sessions_under_workspace once per plain child, but the directory removed at :884–885 is the parent entry. A Session with cwd exactly <managed-root>/<task-id> is not under any child, so an empty task directory can still be removed under that Session, even without --force. This also violates the worktree removal contract, which requires the directory to have no Session workspace and treats force as a content override only. Query sessions_under_workspace(&entry) once to cover the root and all descendants; ideally use the same store ownership exclusion as the recorded path so a new Session cannot appear between the check and removal. Add a fixture that creates a Session at the task root and checks that both ordinary and forced removal leave it intact.
This is a source finding; I did not run the Rust fixtures locally. Exact CI was in progress.
The removal checked `sessions_under_workspace` once per plain child but then deleted the parent task directory, so a Session whose cwd is the task directory itself was not under any child and an empty task directory could still be removed under it, with or without `--force`. Query the task directory once before inspecting any child, which covers the root and every descendant, and refuse with `worktree_in_use` for both the ordinary and the forced removal. The check is a store read rather than the ownership-locked removal that the recorded path uses, because an unregistered directory has no record to remove; a Session created between the check and the deletion is therefore not excluded. Add a flow test that places a Session at the task root and checks that both removals fail and leave the directory and its content intact. Fixes #1981
|
Fixed in a79a2b2. The check is a SessionStore read, not the ownership-locked removal that the recorded path uses: an unregistered directory has no directory record, and that removal requires one. A Session created between the read and the deletion is therefore not excluded by it; without The new test |
eric8810
left a comment
There was a problem hiding this comment.
The task-root lookup fixes the previous sequential case, and the new fixture covers ordinary and forced removal.
[P2] Serialize unregistered removal with Session creation. worktrees.rs:819–831,885–887 snapshots Session ownership before the asynchronous directory preflight, then deletes outside the ownership lock. A normal session.create can validate this cwd and commit a Session in between (storage/timeline.rs:1524–1557). The remove then reports success and leaves the new Session pointing at a deleted workspace. Nonrecursive removal protects files, not an empty workspace; --force can also remove its contents. The new task-wide lookup means every descendant is now checked before the entire preflight, rather than immediately before handling that child.
Please add an unregistered-removal operation to the SessionStore owner that performs the final cwd check and the removal under the same ownership lock as creation, without requiring a worktree record. That lets either creation win and block removal, or removal win and make creation's existing cwd recheck fail. A gated test for both orderings would complement the new sequential fixture.
Source review only. Exact CI was still running.
The removal checked stored Session workspaces before its asynchronous content preflight and deleted after that check, outside the ownership lock that `createOwnedSession` takes. A Session creation could validate the same cwd and commit between the two, and the removal then reported success while the new Session pointed at a deleted workspace. Add `SessionStore::remove_unregistered_directory`, exposed on the store face: it holds the ownership lock from the final session-workspace check through the caller's removal, so creation that commits first makes the removal fail with `worktree_in_use`, and a removal that runs first makes creation's existing workspace check fail. An unregistered directory needs no worktree record. The content preflight and the stored empty children stay outside the lock. Add two flow tests: one holds the removal after its content check and creates a Session at the task root, the other holds the removal while it owns the lock and queues a creation for the same cwd. Fixes #1981
The hold has no dependency on the connection holds; keeping it before them leaves the neighboring insertion points free for other testing hooks. Fixes #1981
…ed removal The store operation behind an unregistered removal accepted relative paths and paths with `..`, which compare lexically against stored Session workspaces while the filesystem resolves them. Reject any path that is not absolute and normal before taking the ownership lock. A second concurrent `--force` removal that passed its content check re-entered the lock after the first one deleted the directory and reported a storage failure from the missing path. Re-check under the lock and answer not found when another removal already finished. Dropping the caller of a removal released the lock while the deletion still ran. Run the check and the removal in a task that owns the lock, so the lock is held until the deletion finishes, and report the owner's removed flag on the store face. Fixes #1981
|
Fixed in 99586c0. The unregistered removal runs under the ownership mutex that The store operation now rejects a root that is not absolute and normal, so a relative path or one with New tests in |
eric8810
left a comment
There was a problem hiding this comment.
Re-reviewed 99586c0a. The final Session workspace check and removal now share the ownership mutex with normal Session creation, and the detached removal keeps that exclusion after caller cancellation. That addresses the previous finding.
[P2] Revalidate that this is still an unregistered directory under the ownership lock. crates/dim-app-server/src/worktrees.rs:706–724 selects the unregistered path from a directory-record snapshot before the lock. Its Git checks are also before the lock (:835–892). The locked owner method only rechecks existence and Session cwd (storage/timeline.rs:1815–1833); the forced deletion does not repeat registration/Git checks (worktrees.rs:916–925,1144–1160). A normal association can register an existing sibling worktree while the Session's cwd remains in its source checkout (file_access.rs:425–501), so an association can take effect before removal acquires the lock without the cwd check detecting it. The removal would delete the worktree while retaining its records, although force only overrides the content check.
Under the same ownership exclusion, reject a newly recorded/associated directory at or beneath the task root, and recheck that the task directory meets the unregistered Git and path conditions before deletion. The non-destructive regression can gate removal before lock acquisition, commit an association, and assert the removal callback never runs. Static finding; exact CI was still running.
An unregistered removal took its task directory from a directory-record snapshot and checked its Git facts before it acquired the store's ownership lock. An association committed in between recorded a worktree under the task root while its Session's cwd stayed in the source checkout, so the final cwd check passed and a forced removal deleted the worktree but kept its records. The store now refuses a directory record at or under the task root inside the ownership lock. The removal step repeats the pre-check's filesystem facts under the same lock before it deletes: the task directory is a plain directory without a `.git` entry, and every immediate child is a plain directory at its own canonical path under the managed root without a `.git` entry. Only filesystem checks run under the lock; no Git subprocess starts. Three regression tests gate the removal before the lock and then commit an association, add a `.git` entry, or associate a recorded directory: each is refused with `worktree_in_use` and leaves the directory, its file and the associated worktree intact. A store-level test asserts the removal callback never runs for a recorded directory. Fixes #1981
|
Fixed in 52defcf.
Regression tests in |
eric8810
left a comment
There was a problem hiding this comment.
Reviewed at head 52defcf.
This fixes three real holes in one path, and fixes them where the state lives. The Session check moved from per-child to once for the directory itself, so a Session whose cwd is the task directory is now seen; every child is inspected before anything is deleted, so a refusal no longer leaves earlier subdirectories already gone; and the final deletion's error is no longer dropped while reporting removed: true. Checking again inside the store's removal operation is the right second gate rather than a duplicated one — the first check is about what the tree looks like, the second about what the store knows, and they can disagree by the time the delete runs.
The face change is handled the way §2 asks: sessions_under_workspace on the storage face delegates to the owner, resource-faces.md and the generated catalog move with it, and the design page states the refusal. The testing-gated removal_hold is the part I would have reached for too — a check/delete race that only shows up under load needs a way to be stopped at the boundary, and gating it behind a feature keeps it out of the product.
verify (linux) and ci-ok failed on this head, outside this PR's path:
staged_update::tui_update::update_now_refuses_a_busy_daemon_and_prints_manual_command
228 passed; 1 failed
The update_now cluster has been red on many heads this week (fifteen cases, then one, now this), and it is a TUI frame assertion against the updater, not worktree removal. Not approving while a required check is red.
Non-blocking: none.
Verification: not run — I read the worktrees.rs delta, the file list and one failing test name; I did not read the storage face body, the new flow tests, or the docs deltas.
zerob13
left a comment
There was a problem hiding this comment.
Re-reviewed 52defcf724ca4cfed494d2220a47997b33750f60. The SessionStore registration check now runs under the ownership lock and its targeted CI regression passes.
[P2] Match the repository protection check to the deletion scope. The design says the unregistered directory must not contain Git metadata. The final guard in crates/dim-app-server/src/worktrees.rs:1078–1186 does not cover the full tree that forced removal may delete. Force should waive the ordinary-content check, not the repository protection. Preserve that protection for the full deletion scope, or narrow what the operation removes. This inherited mismatch is a source finding; no destructive probe was run.
Linux CI passed the worktree cases but failed the separate update prompt test. It ran against the prior main, not the current combined tree.
The final unregistered-removal check looked only at the task directory and its immediate children, while --force deletes the entire tree, so a deeper .git directory, gitdir file or symlink was deleted with it. The removal now walks every directory in the tree it is about to delete, under the same ownership lock and before any deletion. A .git entry in any form refuses the whole removal with worktree_in_use; the walk uses symlink_metadata, does not follow symlinks and stays inside the task directory. --force still skips only the ordinary-content checks, and the non-force path is unchanged. The design records that the traversal and the recursive deletion both run under the lock with no time limit. Fixes #1981
|
Fixed in fe6738f. The removal step that runs under the ownership mutex now walks every directory in the tree it is about to delete before deleting anything. A New tests in |
eric8810
left a comment
There was a problem hiding this comment.
Reviewed at head fe6738f.
The head carries 77 commits since the one I read, almost all of it main merged in; this PR's own delta is one addition, and it strengthens the same rule. check_unregistered_tree walks the whole task directory before anything is deleted, refusing when any directory in it holds a .git entry in any form — directory, gitdir file or dangling link — and it reads with symlink_metadata so the walk cannot follow a link out of the task directory. That matches the CLI contract's "no .git entry and empty" more literally than the earlier per-child check: a nested worktree used to be caught (or not) child by child, and is now refused as a whole before the first deletion. My earlier reading of the ordering fix and the store-side second gate still applies unchanged.
verify (linux) and ci-ok failed on this head, outside this PR's path:
crates/dim-app-server/tests/storage_flow.rs:8379
storage_flow::a_slow_store_upgrade_reports_progress_and_keeps_the_opening_alive
reported progress keeps the opening inside its inactivity bound:
Build("initialization timed out: SqliteStorage Loading []")
486 passed; 1 failed
That is the store-upgrade opening path, in the inactivity-bound and progress-reporting machinery — the area #1997 and its rutis change touch, not worktree removal. Because 77 commits of main arrived with this head, I cannot tell from here whether this failure came in with main or with this branch; a check against main would settle it. Not approving while a required check is red.
Non-blocking: none.
Verification: not run — I read the worktrees.rs addition, the stat of the delta and the failure message; I did not read the merged main changes or the flow tests behind them.
|
The same test failure with the same message appears in these runs:
The pages for the two earliest runs are no longer accessible on GitHub; their failure record comes from logs saved at the time, and the test name and message match the other two runs listed. Each one reports The earliest of these runs predates this branch's first commit. PR #1550 changed only files under This pull request changes the worktree removal path, its directory-record checks and the store-side method that runs them under the ownership lock, and does not touch the store opening or upgrade path. |
|
The The same message failed run 45028209559 attempt 1 on a different head and passed on attempt 2 at the same head, which identifies it as intermittent. This pull request's changes are in the worktree removal path and its store operations; they do not change the store opening or upgrade progress reporting. |
eric8810
left a comment
There was a problem hiding this comment.
Reviewed at head 124d4ba.
This is a data-loss fix, so the thing to check is not that it deletes less but that it decides before it deletes, and it does. remove_unregistered now runs check_unregistered_entry, then sessions_under_workspace on the directory itself, then check_unregistered_children, and only then enters the store's removal operation — where the same three checks run again under the lock and remove_checked_entry deletes nothing that a check has not already passed. Re-checking inside the lock rather than trusting the earlier pass is the right shape for a path where the directory can change between the two.
The checks themselves are written against the ways this goes wrong: symlink_metadata and never a following call, so a symlink is refused rather than walked; a canonical path that must equal the checked path and stay under the managed root; .git refused at any depth in any form — directory, gitdir file, or a dangling link — with the walk refusing to follow links at all; and without --force, every child must be empty, with the verified empty children returned so the deletion acts on that list instead of re-reading the directory. Refusing a Session whose workspace is the task directory or below it, checked once for the directory and again in the store, closes the hole where a per-child check missed a cwd on the directory itself.
cleanup_unregistered_root carries the same discipline for the no-longer-a-worktree case: canonical check, emptiness decided before the delete, and a cancellation check on both sides of it.
Blocking: none.
Checks are green at this head. I read remove_unregistered, check_unregistered_children, check_unregistered_tree, cleanup_unregistered_root and the error taxonomy in worktrees.rs; I did not read the 779 lines of worktrees.rs flow tests, the storage/face.rs and timeline.rs changes that implement remove_unregistered_directory and sessions_under_workspace, or the design deltas, and ran nothing.
Non-blocking: none.
zerob13
left a comment
There was a problem hiding this comment.
The prior blocking concern is resolved. The final unregistered cleanup now checks the full deletion tree for Git metadata without following symlinks, including on the forced path, after revalidating directory/Session ownership. No-force cleanup remains non-recursive. The exact merge with current main passed Linux (including nested Git directory, gitdir file, dangling link and concurrent ownership coverage), macOS, Windows and ci-ok. Static review and hosted CI only; no destructive local checks.
dimcode worktree remove TASK_IDdeletes an unregistered task directory below the managed root. Without--force, ordinary files and symlinks in that directory were removed with it, although the CLI contract removes such a directory only when it has no.gitentry and is empty, or when--forceis given. The refusal ran only for plain subdirectories and was checked while earlier subdirectories had already been deleted, the final deletion dropped its error and still reportedremoved: true, and a Session whosecwdis the task directory itself was not checked at all because the Session check ran per child. See #1981.The removal now refuses with
worktree_in_usewhen a stored Session workspace is at or under the task directory, checked once for the directory itself before it inspects children and again in the store's removal operation, with and without--force. It then checks every child before deleting anything, and refuses a non-directory child (files, symlinks) or a non-empty plain subdirectory without--forcewithworktree_in_use. An empty<repo-name>subdirectory under the task directory is still cleaned without--force, which is whatworktree.listreports for a failed creation. A.gitentry anywhere in the tree the removal deletes, whether directly in the task directory or deeper, as a directory, a gitdir file or a symlink including a dangling one, refuses even with--forceand names the path, as the removal contract requires. Without--force, the deletion then removes only those checked empty directories one by one without recursion, so content that appears after the check makes the removal fail withworktree_in_useinstead of being deleted. With--forcethe directory is removed recursively as before, and a failed deletion returnsworktree_store_failedinstead ofremoved: true.worktree.prune --applystill records each failure infailedand continues with the remaining candidates, and its selection still never includes unregistered entries.The final directory-record and Session checks and the deletion run in the same critical section as Session creation and worktree association. The SessionStore owner's
removeUnregisteredDirectoryoperation holds the ownership mutex thatcreateOwnedSessionandassociate_worktreeuse, refuses withworktree_in_usewhen a directory record's root is at or under the task directory, re-checks for a stored Session workspace at or under it, and runs the removal before releasing the mutex; an unregistered directory needs no worktree record of its own. The removal repeats the pre-check's filesystem facts under the same mutex before it deletes: the task directory is a plain directory without a.gitentry, each immediate child is a plain directory at its own canonical path under the managed root without a.gitentry, and every directory in the tree has no.gitentry in any form (a directory, a gitdir file or a symlink, including a dangling one). The tree walk usessymlink_metadata, does not follow symlinks and stays inside the task directory. These are filesystem checks only, so no Git subprocess runs under the mutex. The mutex has no time limit: the tree walk and a--forcedeletion recurse inside it, and while it runs Session creation, fork and deletion and worktree association or removal wait for it. A Session created first makes the removal fail withworktree_in_use; a removal that wins makes the creation's existing workspace check fail withthe Session workspace ... no longer exists. Neither ordering leaves a Session pointing at a deleted workspace. Dropping the caller does not release the mutex while the deletion still runs. The content check runs before the mutex; without--forcethe lock-time recheck repeats it, and content that appeared in a checked child makes the removal fail withworktree_in_use. The operation takes{root}, rejects a root that is not absolute and normal, and answers{removed}; a removal whose directory another removal already deleted answersworktree_not_foundinstead of a storage failure.Tests in
crates/dim-app-server/tests/transport_flow/worktrees.rscover an ordinary file refused without--force, the same directory removed with--force, an empty repository subdirectory cleaned without--force, two children rejected together with both intact, a failed deletion reported asworktree_store_failed, a symlink refused with its target untouched, a task directory with a.gitentry refused even with--force, a dangling.gitsymlink refused with the link intact, a.gitdirectory, a gitdir file and a dangling.gitsymlink below a direct child each refusing with--forceand leaving the tree intact, a deep tree without Git metadata removed with--force, a Session at the task root keeping an empty directory without--forceand a filled one with--force, a Session created after the content check keeping the directory, a worktree associated after the content check keeping the task directory and the worktree, a.gitentry added after the content check keeping the directory, a recorded directory refused before the removal callback runs, a creation queued behind a removal failing its own workspace check without leaving a Session row, a relative or..root refused with the directory intact, a second concurrent removal answeringworktree_not_foundafter the first one deleted the directory, and a dropped caller keeping the mutex held until the deletion finishes. Unit tests incrates/dim-app-server/src/worktrees.rsplace content in the task directory and in a checked subdirectory after the check and assert that the removal returnsworktree_in_usewith the content intact. The cross-language store fixture covers the operation's refusal while a Session uses the directory and a creation blocked while the callback holds the mutex. The nested Git-metadata tests fail without the tree walk and pass with it; the associated-worktree, added-.gitand recorded-directory tests fail without the lock-time record and Git rechecks and pass with them. On main, the file, all-children, deletion-failure, symlink,.git, dangling.git, nested Git-metadata, path-shape, concurrent-not-found and dropped-caller tests fail; the force, empty-subdirectory and deep-tree tests pass there and remain as regression protection.Fixes #1981