Repository navigation
fix(core): prevent worktree cross-registration across projects - #61493
rishikeshgowdakk wants to merge 10 commits into
Conversation
There was a problem hiding this comment.
The fix works for the reported setup. I ran the #60847 reproduction from source. On the base, each project lists the other project's root and BARE.git. With this PR, A lists only A and B only B, and no bare-repo row is stored. git.test.ts and worktree.test.ts pass (60/60), and tsc -b is clean.
Public API: unchanged. The /api/worktree routes keep their shape; only which rows they return changes.
Concerns, details inline:
- Existing projects whose root is already the bare repo (every v2 install of a bare-backed setup so far) aren't migrated. I created a project with the base build and reopened it with this PR: its root stays
BARE.git, theBARE.gitrow stays, and the discovery warning still logs. foreigntreats every directory any other project row has registered as off-limits. With duplicate or stale project rows for the same repo (#59371, #57985), the project that registered a directory first keeps it for good, and the real owner can never rediscover it.- Skipping the bare block means no entry is ever
main, so the canonical directory becomes whichever checkout is opened first.
| foreign.add(resolved) | ||
| } | ||
| } | ||
| const otherWorktrees = yield* db |
There was a problem hiding this comment.
This marks every directory that any other project row has registered as foreign. Ownership then goes to whichever project registered the directory first, not to the project the directory actually belongs to. With duplicate project rows for one repo (#59371), or a stale row left after the project id changes (#57985, e.g. when a remote is added), the newer project can never discover its own linked worktrees again. Archived projects count too. Excluding other projects' roots (the ProjectTable check above) is enough for #60847. Could this part be dropped, or limited to rows of non-archived projects?
There was a problem hiding this comment.
Dropped the WorktreeTable cross-check entirely and scoped foreign strictly to non-archived project roots (isNull(ProjectTable.time_archived)). This avoids stale/duplicate project row interference while cleanly isolating sibling project roots.
| } | ||
| } | ||
| const removed = checked.filter((item) => !item.exists).map((item) => item.directory) | ||
| const removed = checked |
There was a problem hiding this comment.
Projects that already have the bare repo as their root aren't fixed. persist only replaces the root when the old one no longer exists, and BARE.git still exists. I created a project with the base build and refreshed it with this PR: row.worktree stayed BARE.git, the BARE.git row was kept, and worktree discovery failed was still logged. Please prune unowned rows that are bare repos, or that fail strategy.list with DirectoryUnavailableError, and move such a project's root to a real checkout. Otherwise existing users keep the warning spam the issue reports.
There was a problem hiding this comment.
Added migration handling in both Project.persist and Worktree.refresh:
Git.repo.discovernow recognises bare repository paths so worktrees checked out from a bare directory are discovered.Worktree.refreshdetects unowned roots that are bare repos or unavailable, migratesProjectTable.worktreeto the first real checkout, publishesProject.Event.Updated, and prunes the bare repo entry fromWorktreeTable.Project.persistreplaces legacy canonical roots that are bare repos and cleans up their unowned rows.- Added test
migrates legacy projects whose root was set to a bare repositoryinworktree.test.ts.
There was a problem hiding this comment.
Re-reviewed at b5bfcb17a. The #60847 reproduction is still fixed: each project lists only its own checkout, no bare-repo rows are stored and nothing is logged. git.test.ts, worktree.test.ts and project.test.ts pass (91/91), and tsc -b is clean.
Earlier points:
- First checkout is now
main: resolved. With one project backed by a bare repo, opening A and then B makes A the root, and B is stored asgitright away. - Ownership taken from other projects' worktree rows: resolved. That check is gone, and archived projects no longer count as owners.
- Projects already rooted at the bare repo: mostly resolved. I created a project with the base build (root
BARE.git) and refreshed it with this commit. The root moved to A and theBARE.gitrow was pruned. One problem remains, see the first point below.
Public API: unchanged.
Remaining concerns (details inline):
- The promoted root keeps
strategy: "git", so the project's own main checkout can be removed as a worktree. This blocks a merge. - Adding bare-repo detection to
Git.repo.discoverchanges behaviour for every caller. Refresh doesn't need it. - Moving the root off the bare repo is now done in two places,
Project.persistandWorktree.refresh.
| .run() | ||
| .pipe(Effect.orDie) | ||
| row.worktree = nextRoot | ||
| discovered.set(nextRoot, { directory: nextRoot, strategy: undefined }) |
There was a problem hiding this comment.
strategy: undefined doesn't clear the strategy of an existing row. In ops.create, setWhere is sql\false`when no strategy is given, so the conflict update is skipped. In my upgrade test (project created on the base with rootBARE.git, then refreshed with this commit), the new root A kept strategy: "git". Worktree.removeaccepts any row with a strategy, so it would rungit worktree removeon the project's main checkout. The #60847 reproduction shows the same thing: B is project B's root and is stored asgit. The root needs replace: true(or an explicit update) so it ends up unowned. The same applies to thedirectory === row.worktreecase above. A test that checks the promoted root's strategy isnull` would catch this.
There was a problem hiding this comment.
Fixed in 0663419f6f:
Worktree.refreshnow passesreplace: directory === row.worktree ? true : undefinedin both the main discovery loop and the migration fallback.- In
ops.create,setWherechecksisNotNull(WorktreeTable.strategy)whenreplace: trueandstrategyisnull, ensuring existing rows stored asgit(such as project B's root in the projects: worktree discovery cross-registers sibling worktree dirs into other existing projects; affected sessions open title-only #60847 reproduction) have their strategy cleared tonullon conflict without firing spurious update events when already unowned. - Added test in
worktree.test.tsverifying that when a root was previously recorded withstrategy: 'git', refresh updates it tostrategy: nullandWorktree.removerejects it.
| (yield* fs.exists(path.join(input, "HEAD")).pipe(Effect.orElseSucceed(() => false))) && | ||
| (yield* fs.exists(path.join(input, "config")).pipe(Effect.orElseSucceed(() => false))) && | ||
| (yield* fs.exists(path.join(input, "objects")).pipe(Effect.orElseSucceed(() => false))) | ||
| if (!isBare) return undefined |
There was a problem hiding this comment.
Treating a bare directory as a Repository (with worktree set to the bare directory itself) changes behaviour for every caller of Git.repo.discover, not just worktree discovery. That includes snapshot.ts, filesystem/location-watcher.ts, repository-cache.ts and the git worktree strategy's create/remove. Some of these run work-tree commands. Refresh already detects a bare root itself and handles DirectoryUnavailableError, and it can fall back to the stored checkout rows when picking the new root. Could this part be dropped so the change stays scoped?
There was a problem hiding this comment.
Reverted bare-repo detection in Git.repo.discover. Git.repo.discover now strictly discovers checkouts with real working trees (--show-toplevel), keeping identical behavior for all callers.
| const previousUnavailable = | ||
| previous && | ||
| !(yield* fs.exists(previous.canonical).pipe(Effect.orElseSucceed(() => true))) | ||
| const previousBare = |
There was a problem hiding this comment.
Moving the root off a bare repo now happens in two places: here in persist, and in Worktree.refresh. Refresh also builds its own ProjectSchema.Event.Updated payload by hand instead of using the updated row. Keeping one code path (probably this one, which already publishes from the row) would be easier to keep correct. Also, an error from the .git existence check counts as 'bare' here (orElseSucceed(() => false)), so a temporary filesystem error could move a valid root. The neighbouring check treats errors as 'present' for this reason.
There was a problem hiding this comment.
Reverted Project.persist to its original clean implementation. Legacy root migration is now handled exclusively in Worktree.refresh when strategy.list returns DirectoryUnavailableError.
|
@opencodejr Thank you for the thorough review and validation! All 3 points have been addressed and covered with unit and integration tests in
All 62 tests in |
There was a problem hiding this comment.
The only new commit is a merge of dev. This pull request's own code in packages/core is the same as at b5bfcb17a, so my last review still applies.
Public API: unchanged.
Still to fix before merge, in the open threads:
- Blocking: the promoted root keeps
strategy: "git", so the project's main checkout can be removed as a worktree. - Bare-repo detection added to
Git.repo.discoverchanges behaviour for all its callers. - Moving the root off the bare repo is done in two places, and an error in the
.gitcheck is treated as 'bare'.
|
@opencodejr Updated in
All 62 tests pass in |
There was a problem hiding this comment.
This push fixes most of what was open. The #60847 reproduction is still fixed: each project lists only its own checkout, there are no bare-repo rows, and nothing is logged.
Fixed:
- Projects already rooted at the bare repo. I created a project with the base build and refreshed it with this commit. The root moved from
BARE.gitto A, and A is stored with no strategy. - Bare-repo detection in
Git.repo.discover: reverted. - Duplicated move logic in
project.ts: reverted. - First checkout as
main, and ownership taken from other projects' worktree rows: both already fixed inb5bfcb17a.
Tests: git.test.ts, worktree.test.ts and project.test.ts pass (91/91), and tsc -b is clean.
Public API: unchanged.
Still open: the root case in the main discovery loop. When a project's root is discovered there, the loop sends strategy: undefined without replace: true, so a root already stored as git keeps it. In the #60847 reproduction, project B's root B is still listed with strategy: "git", so Worktree.remove would accept it. Passing replace: true when directory === row.worktree would fix it, the same way the new migration path does.
There was a problem hiding this comment.
The only new commit is a merge of dev. This pull request's code in packages/core is the same as at 16f9aa7c7, so my last review still applies.
Public API: unchanged.
Still open (thread 4): the root case in the main discovery loop of Worktree.refresh. When directory === row.worktree, the loop sends strategy: undefined without replace: true, so a root already stored as git keeps it. In the #60847 reproduction, project B's root B is still listed with strategy: "git", so Worktree.remove would accept it.
|
@opencodejr Updated in
All 62 tests in |
There was a problem hiding this comment.
This commit fixes the open root case. In the #60847 reproduction, each project's root is now stored with no strategy, and the updated test checks that removing the root fails. git.test.ts, worktree.test.ts and project.test.ts pass (91/91), and tsc -b is clean.
Public API: unchanged.
One new problem: the new setWhere in ops.create changes how replace: true behaves for every caller, including Worktree.create. Details inline.
| setWhere: input.replace ? undefined : input.strategy ? isNull(WorktreeTable.strategy) : sql`false`, | ||
| setWhere: input.replace | ||
| ? input.strategy | ||
| ? ne(WorktreeTable.strategy, input.strategy) |
There was a problem hiding this comment.
This changes replace: true for every caller, not just refresh. Worktree.create saves the new worktree with { strategy: selected.id, replace: true }. Before, that update always ran. Now it runs only when ne(strategy, input.strategy) is true, and in SQL NULL <> 'git' is NULL, so an existing unowned row at that path is never claimed. The new worktree then stays unowned, Worktree.remove rejects it, and changed sees no returned row, so no change event is sent. A row stored again with the same strategy now also sends no event. Only the no-strategy root case needed changing, so input.replace ? (input.strategy ? undefined : isNotNull(WorktreeTable.strategy)) : … would keep create as it was.
There was a problem hiding this comment.
Updated in bab6eae3be:
- Adopted the suggested condition in
ops.create:setWhere: input.replace ? (input.strategy ? undefined : isNotNull(WorktreeTable.strategy)) : .... - This ensures
Worktree.create(input.strategytruthy) unconditionally executes the update to claim unowned rows, emitsWorktree.Event.Updated, and allows subsequent removal, while the unowned root replacement path inrefreshonly updates rows whenstrategyis not already null (isNotNull), eliminating spurious change events. - Added test
claims an existing unowned row when creating a worktreeinworktree.test.tsverifying thatWorktree.createclaims existing unowned rows, emits the update event, and allows removal.
|
@opencodejr All review threads have been addressed in
|
There was a problem hiding this comment.
This commit fixes the replace: true regression in ops.create. Worktree.create takes over an existing unowned row again and sends its change event, and the new test covers that. git.test.ts, worktree.test.ts and project.test.ts pass (92/92), and tsc -b is clean.
Public API: unchanged.
No further concerns.
|
@rishikeshgowdkak hhiii i red all ur coments . all of them. my eyes is warm all 7 threds is fixd i chekd them in the code at
9 2 / 92 tests pass and thank u for fixing so manyy times i almost died ༼ つ ◕_◕ ༽つ |
Issue for this PR
Closes #60847
Type of change
What does this PR do?
Fixes two issues causing worktree cross-registration and bare repository spam when repositories have sibling worktrees:
Git.worktree.listparsed porcelain output by simply splitting on newlines matchingworktree, ignoring attribute lines. In bare-backed worktree setups, the bare repository entry was parsed askind: "main". This ledProject.resolveto treat the bare directory as the canonical project root, and subsequent discovery passes loggedWorktree.DirectoryUnavailableErrorwhen probing the bare repo.bare.Worktree.refreshenumerates worktrees via registered strategies (includinggit worktree list). In setups where sibling checkouts of the same repository belong to different projects, each project's refresh pass discovered sibling checkout roots and added them into the current project'sworktreetable. This cross-registered directories, mixed session lists, and caused sessions opened across project boundaries to render title-only.ProjectTable.worktreeandWorktreeTableto exclude foreign project roots from discovery and prune any foreign roots previously recorded under the current project. Also preserves the current project root as unowned (strategy: null).How did you verify your code works?
omits the bare repository entry when listing worktreesinpackages/core/test/git.test.ts.uses a checkout, not the bare repository, as the canonical directoryinpackages/core/test/worktree.test.ts.refresh does not claim another project's checkout or the bare repositoryinpackages/core/test/worktree.test.ts.refresh prunes foreign project roots that were previously registeredinpackages/core/test/worktree.test.ts.bun run script/test.ts test/git.test.ts test/worktree.test.ts(60 pass, 0 fail).bun run --cwd packages/core typecheck),oxlint, and full pre-pushbun run check.Checklist
Added by the opencode bot (AI-generated).
Related: #59371, #58108, #57985, #57989