[codex] Fix PR worktree checkout materialization
Merged
KI
6mo ago+2637+2637+0123456789012345678901234567890123456789-828-828-012345678901234567890123456789
Summary
- Replace the v2 PR workspace flow that created a detached placeholder worktree and then ran
gh pr checkout. - Add a materialize-first helper that fetches and verifies the PR head against GitHub's
headRefOid, creates/configures the local branch, then letsworkspaces.createadd the worktree from that branch. - Start materialized PR branches from verified commit OIDs, not mutable fetched refs; synthetic fallback fetches reuse one stable internal ref per PR.
- Remove the now-unused
gh pr checkoutrecovery helper/tests. - Configure cross-repo PR branches with a Superset-managed fork remote and push refspec so plain
git pushtargets the contributor branch instead ofrefs/pull/<n>/head. - Return materialization warnings from
workspaces.createand surface them in the desktop workspace-create flow. - Serialize concurrent creates for the same project/PR so repeated clicks reuse the first workspace instead of racing git worktree creation.
- Normalize adopted local PR branches through the same tracking/push setup as freshly materialized branches, and roll back newly created PR branches if later workspace registration fails.
- Update CLI/MCP/SDK/docs text so
--prno longer claims the host runsgh pr checkout. - Document the current setup, Worktrunk/T3Code comparison, implemented fix, and remaining follow-ups in
plans/done/20260516-pr-worktree-checkout-flow.md.
Why
The old flow performed two checkouts in the target path. git worktree add --detach can run hooks, filters, or watchers that dirty tracked files such as package-lock.json; the later gh pr checkout --force can still fail with local changes would be overwritten by checkout.
The new flow avoids the dirty-placeholder state entirely: it materializes the verified branch first, then performs one worktree checkout.
Known follow-ups
- v1 desktop still uses the old detached worktree +
gh pr checkoutpath and should get a separate backport. - Add richer fork permission handling (
maintainerCanModify, deleted forks, inaccessible forks) if exact GitHub CLI parity is needed for every fork push edge case. - Renderer surfacing for hook-induced dirty-status warnings is still deferred; materialization/setup warnings now reach the desktop flow.
Validation
bun test packages/host-service/src/trpc/router/workspace-creation/utils/pr-branch-materialize.test.ts packages/host-service/test/integration/workspace-create-pr.integration.test.ts packages/host-service/test/integration/pr-branch-materialize.integration.test.ts(21 pass, 0 fail)bun test packages/host-service/test/integration(206 pass, 8 existing todos, 0 fail)bun run --cwd packages/host-service test:e2e(12 pass, 0 fail)bun run typecheckbun run lint
Summary by cubic
Fix PR workspace creation by materializing the verified PR head before creating the worktree. This removes the detached worktree + gh pr checkout flow, prevents “local changes would be overwritten by checkout” errors, and configures fork pushes for cross‑repo PRs.
-
Bug Fixes
- Fetch and verify the PR head against
headRefOid, create/configure the local branch, then run onegit worktree add <path> <branch>. - Support same‑repo PRs via
refs/heads/<branch>and cross‑repo or deleted head branches viarefs/pull/<n>/head; abort on OID mismatch. For cross‑repo PRs with fork metadata, add asuperset-pr-<n>remote and setpushRemotewithHEAD:refs/heads/<headRefName>; warn when fork metadata is missing or when falling back to the synthetic PR ref. - Handle edge cases: normalize tracking for existing branches, detect conflicts, and abort with a clear error; on failure, delete only a newly created PR branch that still points at the verified head. Return warnings from the host; desktop now surfaces them as toasts.
- Fetch and verify the PR head against
-
Refactors
- Added
materializePrBranch,normalizePrBranchTracking, and cleanup utilities with unit/integration tests covering edge cases;workspaces.createnow materializes the branch then runsgit worktree add.fetchPrMetadatauses injectedctx.execGh; integration tests confirm the host never runsgh pr checkoutin worktree mode. - Updated CLI/
@mcp-v2/@superset/sdk/docs:--pr“checks out the verified PR head” andworkspaces.createreturns awarningsarray. Added design docplans/done/20260516-pr-worktree-checkout-flow.md. Removed the oldgh pr checkoutrecovery helper and its tests.
- Added
Summary by CodeRabbit
-
New Features
- Server now materializes and checks out the verified PR head, derives the workspace branch, returns optional warnings, and serializes concurrent PR workspace creation.
-
Bug Fixes
- Verifies PR head before adding worktrees to avoid dirty-file/checkout failures and improves safe rollback and conflict handling.
-
Documentation
- Updated CLI, SDK, tooling and help text to reflect the new PR checkout behavior.
-
Tests
- Added unit and integration tests for PR branch materialization and worktree flows; removed legacy checkout-recovery tests.
-
UI
- Warnings from the server are surfaced to users via toast notifications.