Skip to content

fix(server): refreshing a merge request worktree keeps other branches' upstreams - #47593

Merged
juliusmarminge merged 4 commits into
pingdotgg:mainfrom
StiensWout:wf/find-b3a2f2b2040f940863ed99f7
Dec 2, 2026
Merged

juliusmarminge merged 4 commits into
pingdotgg:mainfrom
StiensWout:wf/find-b3a2f2b2040f940863ed99f7

Conversation

@StiensWout

Copy link
Copy Markdown
Contributor

When a worktree was reused for a pull or merge request checkout, T3 Code set the upstream on whatever branch that worktree had checked out at that moment, read again partway through. If someone switched the worktree to another branch during the checkout, that branch was left tracking the request's source branch. T3 Code now sets the upstream on the request branch it already found for the worktree. Newly created worktrees use the branch they were created for.

Related: #23368

Reproduction on main

Commit ca36afaeb1 (main). T3 Code makes the other branch track the merge request's source branch. Expected: The other branch keeps its configured upstream.

  1. Keep a worktree on the merge request's source branch.
  2. Set up another local branch to track a different remote branch.
  3. Open the merge request from the branch picker.
  4. Choose Worktree.
  5. Switch the reused worktree to the other branch from a terminal as the checkout starts.
  6. Check which upstream the other branch tracks.

The cause is at apps/server/src/git/GitManager.ts:4109. Ran pnpm scenarios:prove scripts/scenarios/pr-worktree-keeps-switched-branch-upstream.e2e.ts --target web with the repro scenario added: it failed in 2 of 2 runs on a clean boat.dev VM, with expected "origin/review-source" to be "origin/separate-work" in the output.

Evidence

Environment: The web app in Chromium on Linux x64, a fresh checkout on a boat.dev VM reset before each run.

Proven: the repro fails at the test commit 5d7fb578dc and passes on the head ae2ea9d132, both on main 54e81819cb.

Before After
Commit 5d7fb578dc ae2ea9d132
Result ASSERTION_FAILED: expected "origin/review-source" to be "origin/separate-work" passed
merge request checkout after switch Before: merge request checkout after switch Before: merge request checkout after switch (changed region)
T3 Code makes the other branch track the merge request's source branch.
After: merge request checkout after switch After: merge request checkout after switch (changed region)
The other branch keeps its configured upstream.
failure Before: failure
Recording Before: recording After: recording

Tests

  • 5d7fb578dc adds the failing test, alone, before the fix.
  • pnpm exec vp run check:changed 54e81819cbe825bfa36808c783c84758890e4ca0 passed on head.

Not in this change

  • The local checkout path still reads the current branch from status after checkoutChangeRequest, because the provider CLI picks the branch name there.
  • A switch that happens while the later refresh of the reused worktree is running is still handled by refreshCheckedOutBranch's own branch check, which this change does not touch.
  • The scenario still intercepts Git through a PATH shim. It now checks that the switch really happened, but it does not stop a cached absolute Git path from bypassing the shim.

Who did the work

  • Repro written by GPT 6.2 Sol, in Codex
  • Duplicate check judged by Claude Opus 5.5, in Claude Code
  • Written by Claude Opus 5.5, in Claude Code
  • Reviewed by Claude Opus 5.5, in Claude Code, and by GPT 6.2 Sol, in Codex
  • Reproduction, checks and proof run by a program on base and head, not by an agent

Made in T3 Code by GPT 6.2 Sol and Claude Opus 5.5

StiensWout and others added 2 commits December 2, 2026 16:47
…' upstreams

Configure the head upstream on the branch found for the worktree instead of
re-reading whichever branch the checkout holds at that moment.

Co-authored-by: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Dec 2, 2026
macroscopeapp[bot]
macroscopeapp Bot previously approved these changes Dec 2, 2026
@macroscopeapp

macroscopeapp Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Would Approve

Macroscope's review found this PR approvable — Worktree preparation now configures the intended request branch’s upstream instead of whichever branch happens to be checked out. Existing fetch and refresh safeguards remain unchanged, with no obvious material risk requiring human judgment.

Needs human approval because:

  • Macroscope's correctness review did not reach a verdict

Notes:

  • The correctness review was unavailable. Eligibility was assessed without its result.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Worktree preparation now configures pull-request upstreams using explicit branch names for reused and new worktrees. A test switches a reused worktree’s branch during upstream lookup and checks that both branches retain their respective remote tracking branches.

Changes: Worktree upstream selection

Layer / File(s) Summary
Configure upstreams with explicit branch names
apps/server/src/git/GitManager.ts, apps/server/src/git/GitManager.test.ts
Removes the helper that used the checked-out branch for reused worktrees. Reused and new worktrees now pass explicit pull-request branch names. The test checks tracking state after a branch switch during upstream lookup.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix

Suggested reviewers: iamshadmantaqi, juliusmarminge

Merge Risk: Minimal · up to df439

This fix keeps each branch's upstream correct when a merge request worktree is refreshed after its branch was switched. No actionable merge risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix and its scope: a merge request worktree refresh no longer changes other branches' upstreams. The raw summary says the reused worktree now sets upstream on the resolved PR branc…
Description check Passed The description follows the template's main sections: Problem, Change, Scope, and Verification. It explains the bug, the reused-worktree fix, and the new-worktree behavior. It gives a reproduction on …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the fix and its scope: a merge request worktree refresh no longer changes other branches' upstreams. The raw summary says the reused worktree now sets upstream on the resolved PR branch rather than the currently checked-out branch, which matches the title. The title says "merge request" while the code and description use "pull request" wording, but the meaning is the same. The title is 80 characters, and the 10-word count is within the expected concise form.

Full details: Description check

Explanation

The description follows the template's main sections: Problem, Change, Scope, and Verification. It explains the bug, the reused-worktree fix, and the new-worktree behavior. It gives a reproduction on main, failing and passing commits, and a test that fails before the fix and passes after. The Scope section links related issue #23368 but does not clearly say whether that issue shows this bug, and it does not explain why no issue is needed. The description also has a long reproduction table and an extra attribution section, but its verification is specific and it states known limits. Overall it is mostly complete.

Finishing Touches

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between ae2ea9d and df439d7.

Files selected for processing: 1

  • apps/server/src/git/GitManager.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@juliusmarminge juliusmarminge added the takeover:in-progress A takeover lead is working on this PR label Dec 2, 2026
juliusmarminge and others added 2 commits December 2, 2026 17:26
…ests

Replace the PATH-shim scenario with a GitManager unit test that pauses
after the reused worktree's upstream read and switches its branch, which
fails on main and passes with the fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@macroscopeapp
macroscopeapp Bot dismissed their stale review December 2, 2026 18:15

Dismissing prior approval to re-evaluate df439d7

@juliusmarminge

Copy link
Copy Markdown
Member

Evidence

Real web app, Chromium 156 on Linux, 1280×800, light theme for every capture. Fresh per-commit databases; Replay is used only to seed threads. GitLab requests, fetches, worktree creation, branch switches, and configuration queries are real.

  1. Seed a real GitLab project with main, mr-source (one new commit), other-branch (at main), and an open merge request (fixture MR 1). Create fresh local projects through t3.project and Replay threads through t3.thread. Fetch the real origin and set other-branch to track origin/other-branch.
  2. Reused case: create mr-source at main, set its upstream to origin/mr-source, and add an existing worktree on mr-source. Open a new-thread draft, open Branch, search oxc stack #1, choose Checkout merge request, and click Worktree. A Git pass-through driver runs the real git switch other-branch after the real rev-parse --verify @{upstream}^{commit} returns in that worktree.
  3. New case: repeat with a fresh project and no local mr-source branch or existing request worktree. The same UI action creates the worktree; the driver runs the real git switch other-branch immediately after successful git worktree add, before preparation continues.
  4. Normal control: repeat the new-worktree action in a fresh project without a concurrent switch. Capture the completed draft, then inspect both branch upstreams.
  5. After each case, reopen the original local thread and show the shared repository configuration in the app terminal, including a live git for-each-ref query. Compare other-branch and mr-source upstreams. Run this identical scenario on base and head, each with its own fresh database.

Scenario: Merge request preparation preserves unrelated upstreams in reused and new worktrees

Proven: the scenario fails on base 9e0de5137b and passes on head df439d76a3.

| | Base 9e0de5137b | Head df439d76a3 |
| Result | ❌ failed
ASSERTION_FAILED: expected ["reused: origin/mr-source | mr-source: origin/mr-source","new: origin/mr-source | mr-source: ","normal: origin/other-branch | mr-source: origin/mr-source"] to equal ["reused: origin/other-branch | mr-source: origin/mr-source","new: origin/other-branch | mr-source: origin/mr-source","normal: origin/other-branch | mr-source: origin/mr-source"] | ✅ passed |
| reused upstream result | Base 9e0de5137b: reused upstream result | Head df439d76a3: reused upstream result |
| new upstream result | Base 9e0de5137b: new upstream result | Head df439d76a3: new upstream result |
| normal checkout complete | Base 9e0de5137b: normal checkout complete | Head df439d76a3: normal checkout complete |
| normal upstream result | Base 9e0de5137b: normal upstream result | Head df439d76a3: normal upstream result |

The reused-worktree race produces the warning on both commits; that warning is expected after switching away and is not the verdict. The Git configuration captures show the difference. The normal control shows mr-source selected without the warning on both commits. The automatic duplicate failure screenshot is omitted.

Recordings retain the checkout interaction, followed by the completion screenshot for two seconds and the terminal result screenshot for three seconds:

Case Before After
reused Before recording After recording
new Before recording After recording
normal Before recording After recording

Scenario · GitLab fixture seed · Recording extraction

@juliusmarminge juliusmarminge added takeover:ready Takeover done; auto-lands when gates pass and removed takeover:in-progress A takeover lead is working on this PR labels Dec 2, 2026
@juliusmarminge
juliusmarminge merged commit b2930e0 into pingdotgg:main Dec 2, 2026
6 checks passed
@juliusmarminge juliusmarminge removed the takeover:ready Takeover done; auto-lands when gates pass label Dec 2, 2026
@StiensWout
StiensWout deleted the wf/find-b3a2f2b2040f940863ed99f7 branch December 3, 2026 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants