Repository navigation
fix(worktree): refuse to reuse a worktree that is in use - #19013
Conversation
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Check whether a closed or idle chat slot stays in [DESIGN-REVIEWED] 758e274 Description read[DESCRIPTION-READ] ca14130ea7f469641aac54a2721265e0090b31a8d3ef4631e3051862e7d02ebe That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5, fork) — 🟡 CONCERNSPremise-level review of First-Principles-Verdict: CONCERNS The new Not justified as shipped
What this change shipsInventory (6 items) — 5 justifiedIntent: stop "Start in new worktree" from opening a second session in a worktree another session is using. FIX.
[FIRST-PRINCIPLES-REVIEWED] 758e274 Description read[DESCRIPTION-READ] c00bf135bb935f5de54b3548fdc453cf82015077ae66c9b19c68cfcb6cefa314 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsFINDING -- src/kiro_crew/dashboard/handlers/worktree.py:662 -- With |
|
Thanks, the finding holds. Fixed in a second commit, fe882f5, on top of 01883fc.
Runs on fe882f5:
|
fe882f5 to
55ff463
Compare
|
Thanks, the finding holds. I reproduced it before fixing: a scratch superproject with an initialized submodule whose own config set Fix: I chose "do not recurse" over "screen each submodule's filters" because the in-use check holds without recursion. It asks whether the SUPERPROJECT worktree is as Red: Scope: this addresses the blocking submodule-filter execution. The separate non-blocking note about |
"Start in new worktree" reused the existing worktree for any request whose branch already had one at the derived path. The rule exists for a retry whose session never opened, but it also matched a second request for the same branch, so the card offered again put a second session into a tree another session was working in, uncommitted edits and new commits included. _create_worktree_sync now reuses the tree only while it is exactly as "worktree add" left it: a new helper, _worktree_in_use, checks that "git status --porcelain" is empty and that the branch has no commit beyond the base, and refuses when either probe cannot answer. The endpoint also refuses when a session is already scoped to the tree or to a directory inside it. Both answers are a 409 with code worktree_in_use, built by _in_use_refusal, which the card shows inline. The follow-up trust-model doc states the rule.
The in-use probe's `git status` recursed into submodules, so a content filter a submodule declares in its own config (.git/modules/<name>/config) ran to compare a modified tracked file. `_checkout_filter` screens only the superproject's config. The probe now passes --ignore-submodules=all: the in-use check asks whether the superproject tree and branch are as `worktree add` left them, and a fresh add checks no submodule out. The real-git reuse checks also run without a skip: a newly added skipif loosened the test file's ratchet, and every CI host has git. A new test drives the real probe against a superproject whose submodule declares a filter, and asserts its marker file is never written.
55ff463 to
758e274
Compare
iamwhatever
left a comment
There was a problem hiding this comment.
This PR fixes "Start in new worktree" so it no longer hands one worktree to two sessions. Before, POST /api/worktree/create reused any registered worktree on the same branch. Now it reuses one only when the tree is as worktree add left it, and it answers 409 worktree_in_use when the tree has uncommitted changes, when its branch has commits beyond the base, or when a slot is already scoped to the tree or a directory inside it. That is the root cause, because the old check treated "path is registered on this branch" as proof of a retry. I read the full production diff in dashboard/handlers/worktree.py and tried to break it. Every probe that git cannot answer fails closed. The filter screen runs before status, and --ignore-submodules=all stops a submodule's own clean filter from running; the real-git test reproduces that filter running on base. The branch is validated by is_valid_followup_branch before it reaches rev-list. The base ref comes from the same _resolve_base_ref the create path cuts from. The slot-prefix check adds os.sep, so proj-wt-x2 does not match proj-wt-x. Base is main, all checks are green, and there are no change requests. Two notes, neither blocking: the First Principles CONCERNS about the zero-reader code key still has no disposition, and a repo with status.showUntrackedFiles=no would hide a closed session's untracked files from the probe (adding --untracked-files=normal is a one-line follow-up). — crew-pr-reviewer (deep re-check)
Problem / Motivation
Goal: "Start in new worktree" never opens a second session in a worktree that another session is using.
POST /api/worktree/createreuses an existing worktree when the derived directory is already registered on the requested branch. The rule exists for one case: a retry after the new session failed to open. The check cannot tell that retry from a second request for the same branch. The follow-up card derives the branch from its title (followupBranchForinwebsite/src/pages/ChatPage.tsx), reads onlypathfrom the answer, and opens a new session scoped to it. So when the same follow-up is offered again, or another session in the repository offers one with the same title, the second press opens a second session in the first session's working tree, with its uncommitted edits and new commits.Why it matters
Two agents then edit one checkout without knowing about each other. One can overwrite or commit the other's half-done work. The endpoint even marks the answer
reused: true, but nothing in the client reads that field (git grep -n reused -- website/src/pages/ChatPage.tsx website/src/api/client/files.tsfinds only two retry messages and an unrelated comment).Not a goal
errorof a 409 inline. Its copy after a failed session switch says "Switch to it in the sidebar, or press the button again"; pressing again now returns the 409 that says a session is open there, instead of opening a second session.busyIndexinwebsite/src/components/FollowUpCard.tsx)..gitignore) do not count as changes._create_worktree(src/kiro_crew/apps/builtins/spec_builder/backend/repository.py) and the task runner'sinit_workspace(src/kiro_crew/git_coord.py) never reuse a worktree: they refuse an existing path or branch, so they do not have this rule.What changed (motivation → approach → change)
200withreused: true, and the client opens a second session in that tree._create_worktree_syncinsrc/kiro_crew/dashboard/handlers/worktree.pytreats "the path is registered on this branch" as proof of a retry. A retry's tree is untouched by construction, because the failed session never ran; the check never looks at the tree._worktree_in_use, runs before a reuse. It screens the worktree's own config for a filter driver with the existing_checkout_filter, runsgit --no-optional-locks status --porcelain --ignore-submodules=allin the worktree (no index lock is taken, and no recursion into submodules), and counts the branch's commits beyond the base withgit rev-list --count <base>..refs/heads/<branch>. Any change, any such commit, or any probe git cannot answer refuses the reuse. An advanced base still reuses an untouched tree, because its old tip is then an ancestor.--ignore-submodules=allon that status._checkout_filterscreens only the superproject's config, so without it the recursivestatuswould run a content filter a submodule declares in its OWN config (.git/modules/<name>/config), whichgit statusexecutes to compare a modified tracked file — repository-controlled code the superproject screen never saw. A freshworktree adddoes not check submodules out, and a session that advanced a submodule POINTER did so in a commit the branch-ahead count still catches, so ignoring submodule working state loses no signal this check needs.api_worktree_createalso refuses a reuse when a session is already scoped to the worktree or to a directory inside it. That catches a session that opened and has not changed anything yet. It compares against the realpath'd slot projects the handler already collects for its allow-list.409built by a new helper,_in_use_refusal, withcode: worktree_in_useand anerrorthat names the reason and says to continue in the session working there.docs/architecture/followup-suggestions-trust-model.mdstates the rule.reusedin the client. Only the server can see the tree and the sessions, and it would be a website change.Backwards compatibility
Compatible: a retry on an untouched worktree is still reused, pinned by
test_our_own_worktree_at_the_destination_is_reusedandtest_an_untouched_worktree_is_reused_on_retry. A fresh create is unchanged. The 409 body keeps theerrorkey the card reads and addscode. The only caller is the follow-up card:git grep -n "worktree/create" -- src website/srcfinds the route, the handler docstrings andapi.createWorktree.Removes nothing: a
200for a worktree in use becomes a409; no route, key or retry path is removed.Not a reuse signal: a submodule's own working-tree state. The probe ignores submodules (
--ignore-submodules=all), so a reused worktree is judged by its superproject tree and its branch commits, not by edits inside a submodule's nested checkout. A session's real work still shows as a superproject edit or a branch commit.Tests
Added to
test/test_dashboard_worktree_coverage.py(thesync_envfixture gains programmablestatus,aheadanddest_filterprobes):TestCreateWorktreeSync::test_the_retry_inspects_the_tree_without_writing_its_index: the retry still answersreused, and the probes run as--no-optional-locks status --porcelain --ignore-submodules=allin the worktree andrev-list --count <base>..refs/heads/<branch>in the repository.test_a_reused_worktree_with_uncommitted_changes_is_a_409andtest_a_reused_worktree_whose_branch_has_new_commits_is_a_409: refused withworktree_in_use; the worktree and branch are not touched.test_a_reused_worktree_git_cannot_inspect_is_a_409(5 cases): a filter in the worktree's config, a failedstatus, an unresolvable base, a failedrev-list, and unparseable output.TestCreateWorktreeSyncRealGit(3 tests): real git on a synthetic repository, with only the OS-sandbox wrapper replaced by a plaingitcall. An untouched worktree is reused on retry; one with uncommitted edits, and one holding another session's commits, are refused.TestWorktreeInUseIgnoresSubmodules::test_the_reuse_probe_does_not_run_a_submodule_filter: a real superproject with an initialized submodule whose own config declaresfilter.evil.clean(its driver touches a marker file) and a modified tracked file. The reuse probe'sstatusmust not run that filter. With only the OS-sandbox wrapper replaced by a plaingitcall,_worktree_in_useis driven directly: on the base (b2b49857ae, no--ignore-submodules) the recursivestatuscreates the marker (AssertionError: the reuse probe recursed into the submodule and ran its clean filter), red in 3 of 3 runs; with the fix the marker is never created and the superproject tree reads as reusable, green in 3 of 3 runs.TestEndpointCreateOutcomes::test_a_reused_worktree_open_in_a_session_is_refused(3 cases): a session scoped to the worktree or to a directory inside it gets the 409, audited as an error; with no such session the reuse is returned.Before the fix (on origin/main 3dc503d, with only this test file copied in:
pytest -n 0 test/test_dashboard_worktree_coverage.py -k "inspects_the_tree or uncommitted or new_commits or cannot_inspect or RealGit or open_in_a_session or our_own_worktree"), RC=1 in 3 of 3 serial runs:The three that passed are the controls: the untouched retry, at the unit level and with real git, and the endpoint with no session on the worktree.
After the fix, RC=0:
test/test_dashboard_worktree_coverage.py141 passed in 3 of 3 serial runs; the six test files that name the module 419 passed, 8 skipped. The 8 skips are git-version checks intest/test_git_config_hooks.py(6, git < 2.54) andtest/test_governance_updates.py(2);test/test_worktree_create.pyruns whole here (91 passed), includingtest_retry_reuses_our_own_worktreethrough the real sandboxed spawn. mypy, flake8 and isort are clean, black is clean onworktree.py(the test file is in.github/black-baseline.txt), andscripts/check_black_formatting.py,scripts/check_subprocess_encoding.py(the real-git helper pinsencoding="utf-8"),scripts/docs-lint.shandscripts/check_comment_history.py(withCOMMENT_HISTORY_BASE_REF=origin/main) pass.Manual verification
No UI change.
TestCreateWorktreeSyncRealGitdrives_create_worktree_syncwith real git output on a synthetic repository, including a second session's edits and commits in the worktree.Related Issues
None found. The reuse rule comes from #461. Open #17169 and #17628 touch other hunks of
worktree.py(the non-repo message and_run_git). Open #19012 edits the same reuse branch of_create_worktree_sync: it refuses a worktree whosegit worktree addnever finished, and reads the worktree list with each entry's lock state instead of only its branch. The two conflict in that one hunk. Whichever lands second keeps both checks: the unfinished-add refusal first, then this change's in-use check (_worktree_in_use,_in_use_refusal) before the reuse is returned.Pattern harvest
Rule candidate: review-prompt
Pattern: an idempotent retry keyed only on a resource's identity (here a path and a branch name) must also check that the resource is still in the state the failed attempt left it; otherwise it hands one resource to two owners.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)docs/architecture/followup-suggestions-trust-model.md