Skip to content

fix(server): reconcile detached-HEAD worktrees to null branch - #216

Open
nullStack65 wants to merge 1 commit into
release/native-0.1.10from
fix/clo-1102-detached-head-worktree-sync
Open

nullStack65 wants to merge 1 commit into
release/native-0.1.10from
fix/clo-1102-detached-head-worktree-sync

Conversation

@nullStack65

Copy link
Copy Markdown
Owner

When a worktree enters detached HEAD (refName === null), WorktreeBranchSync returned early and left the stale branch name on the thread record.

This change:

  • Reconciles refName === null to branch: null in WorktreeBranchSync after confirming the worktree directory still exists on disk and git.currentBranch(cwd) is null.
  • Adds detached: boolean to WorktreeMcpStatusResult and t3_worktree_status (attached && branch === null).
  • Adds focused tests in WorktreeBranchSync.test.ts and WorktreeMcpService.test.ts.

Deploy: fleet
Model: gemini-4-hydrogen
Task: fix

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

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

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@nullStack65

Copy link
Copy Markdown
Owner Author

Independent full review: the fix that clears the stale branch name when a worktree's HEAD becomes detached. Reviewed exact head 003e816e96cb983865f083d9914b0056644648da against base release/native-0.1.10 at caf9ebeb0cae23bee584f7b0acf3856fb6f6cada. The head is a merge of the base, so this reviews only the 5-file PR delta.

Verdict: one important finding and one nit. The detached-HEAD path works and its tests pass. The problem is that the new guard only checks that the folder exists, so a folder that exists but is no longer a git repository also gets its branch wiped.

F1 (important), apps/server/src/orchestration-v2/WorktreeBranchSync.ts:45. The only thing that stops a refName === null event from clearing the branch is fs.exists(input.cwd). GitWorkflowService.currentBranch also returns null when the folder is not a git repository at all (GitWorkflowService.ts:358). Examples: a worktree folder whose .git link points at a pruned or moved admin dir, or a folder still on disk part-way through removal. VcsStatusBroadcaster already knows the difference. It publishes isRepo, and its comment says a null branch can mean "a checkout that is gone" (VcsStatusBroadcaster.ts:1065-1066). But reconcile takes only {cwd, refName} and drops isRepo. The idle sweep (storageCleanup.ts:815, followBranch) passes the bare currentBranch result in the same way.

What goes wrong: the thread's last real branch name is overwritten with null, so the branch the thread was on is no longer recorded. t3_worktree_status then reports detached: true for a broken checkout. Fix: before reconciling to null, require that the folder is still a repository. Either pass isRepo from the broadcaster event, or check gitWorkflow.repositoryRoot(cwd) !== null inside reconcile, which covers both callers. Add a test where the folder exists but is not a repo.

F2 (nit), packages/contracts/src/worktreeMcp.ts:81. The new detached field changes the output schema and description of the t3_worktree_status MCP tool. The repo instructions ask for a run of the matching evals/ suite when model-facing tool text changes. The PR shows no eval run.

Checked

  • Obligation: the PR does what its body says (reconcile detached HEAD to branch: null, add detached to worktree status). No scope creep.
  • Effect on storage cleanup: a project worktree whose branch becomes null is kept with the reason "thread has no branch" (storageCleanup.ts:1463), so this change cannot cause a worktree to be deleted.
  • A temporary detached HEAD during a rebase clears the branch, and the next branch event restores it. That is acceptable.
  • Surfaces: server and contracts only, with no web or mobile consumer of WorktreeMcpStatusResult. Detached state comes from provider-independent git status events.
  • Ran vp test run src/orchestration-v2/WorktreeBranchSync.test.ts src/mcp/WorktreeMcpService.test.ts (apps/server) on the head after vp i: 26 passed.
  • CI on the head is all green: the deploy-line check, the 4 ARC CI jobs and CodeRabbit.

Not verified: a live run on a real detached worktree, and evals.

{
  "reviewed_base": "caf9ebeb0cae23bee584f7b0acf3856fb6f6cada",
  "reviewed_head": "003e816e96cb983865f083d9914b0056644648da",
  "reviewer": "claude-opus-5-5, Claude Code via T3 Code",
  "author_session": "unknown: PR author nullStack65, body says Model gemini-4-hydrogen; native author session not supplied to reviewer",
  "reviewer_session": "t3-23264413-41298ae1fc704426",
  "fresh_context": true,
  "review_kind": "full",
  "round_id": "t3-23264413-41298ae1fc704426",
  "intent": [
    "Reconcile refName === null to branch: null in WorktreeBranchSync after confirming the worktree directory exists and git.currentBranch(cwd) is null",
    "Add detached: boolean (attached && branch === null) to WorktreeMcpStatusResult and t3_worktree_status",
    "Focused tests in WorktreeBranchSync.test.ts and WorktreeMcpService.test.ts"
  ],
  "prior_findings": [],
  "findings": [
    {
      "id": "F1",
      "severity": "important",
      "outcome": "open",
      "file": "apps/server/src/orchestration-v2/WorktreeBranchSync.ts",
      "line": 45,
      "summary": "Null-branch reconcile only checks that the folder exists. A folder that exists but is no longer a git repo (currentBranch returns null at GitWorkflowService.ts:358) also clears the thread's branch, and status reports detached: true. The broadcaster's isRepo is dropped. Require repositoryRoot(cwd) !== null or pass isRepo."
    },
    {
      "id": "F2",
      "severity": "nit",
      "outcome": "open",
      "file": "packages/contracts/src/worktreeMcp.ts",
      "line": 81,
      "summary": "The t3_worktree_status MCP output schema changed with no evals/ run, which the repo instructions ask for when model-facing tool text changes."
    }
  ]
}

@nullStack65

nullStack65 commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner Author

Repair update for the fix that clears the stale branch name when a worktree's HEAD becomes detached. F1 is fixed on head 857bc85b50f6c3897af7a6c1afc295a832ab9756; a focused re-check still needs to confirm it there. F2 (nit) was outside this repair and is left open.

F1 fix (apps/server/src/orchestration-v2/WorktreeBranchSync.ts): before reconciling to null, reconcile now also requires git.repositoryRoot(cwd) !== null. That uses the same registry.detect check that makes currentBranch return null for a folder that is not a repository, so a pruned or half-removed worktree keeps its last branch. Because the check is inside reconcile, it covers both callers: the broadcaster stream (WorktreeBranchSync.layer) and the idle sweep (storageCleanup.ts:340 builds makeReconcile).

Test: new case "ignores detached HEAD events when the worktree directory is no longer a repository". It fails without the fix (1 failed, 4 passed) and passes with it. vp test run src/orchestration-v2/WorktreeBranchSync.test.ts src/mcp/WorktreeMcpService.test.ts (apps/server): 27 passed. Server typecheck: clean. vp run check:changed could not run on this host (Node 22 rejects the .ts script, ERR_UNKNOWN_FILE_EXTENSION); CI owns the rest.

Fixer outcomes for the re-check: F1 important fixed at 857bc85b50f6c3897af7a6c1afc295a832ab9756. F2 nit left open (out of scope for this repair). The original review record above stays the gate record; a focused re-check in the same round should mark F1 confirmed on the new head.

Squash-rebase of PR #216 onto current release/native-0.1.10; no behavior change from the original
commits. Original authoring trailers: Model gemini-4-hydrogen / Task fix / Agent antigravity

Model: claude-opus-5-5
Task: fix
Agent: Builder drain parked-PR lane (Claude Code worker F3)
Lane: magpie
Harness: t3/claude-code
Model-family: Claude
Model-generation: Opus 5.5
Skills-SHA: unknown (source unavailable)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nullStack65
nullStack65 force-pushed the fix/clo-1102-detached-head-worktree-sync branch from 857bc85 to 4bd79ae Compare November 28, 2026 05:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant