{"record":{"id":"05059277b6c3eebd","repo":"coleam00/Archon","slug":"failed-to-create-worktree-for-pr-prnumber-e","errorCode":null,"errorMessage":"Failed to create worktree for PR #${prNumber}: ${err.message}","messagePattern":"Failed to create worktree for PR #(.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/isolation/src/providers/worktree.ts","lineNumber":1086,"sourceCode":"    await this.cleanOrphanDirectoryIfExists(worktreePath);\n\n    const repoPath = request.canonicalRepoPath;\n    const prNumber = request.identifier;\n\n    try {\n      if (!request.isForkPR) {\n        // Same-repo PR: Use the actual branch so changes push directly to PR\n        await this.createFromSameRepoPR(repoPath, worktreePath, request.prBranch, remote);\n      } else {\n        // Fork PR: Use synthetic review branch\n        await this.createFromForkPR(repoPath, worktreePath, prNumber, remote, request.prSha);\n      }\n    } catch (error) {\n      // Clean up orphaned git-registered worktree from partial failure\n      // (e.g., worktree add succeeded but createBranchWithStaleRetry failed)\n      await this.cleanOrphanWorktreeIfExists(repoPath, worktreePath);\n      const err = error as Error;\n      throw new Error(`Failed to create worktree for PR #${prNumber}: ${err.message}`);\n    }\n  }\n\n  /**\n   * Create worktree for same-repo PR using the actual branch\n   */\n  private async createFromSameRepoPR(\n    repoPath: string,\n    worktreePath: string,\n    prBranch: string,\n    remote = 'origin'\n  ): Promise<void> {\n    // Fetch the PR's actual branch\n    await execFileAsync('git', ['-C', repoPath, 'fetch', remote, prBranch], {\n      timeout: GIT_OPERATION_TIMEOUT_MS,\n    });\n\n    // Try to create worktree with the branch","sourceCodeStart":1068,"sourceCodeEnd":1104,"githubUrl":"https://github.com/coleam00/Archon/blob/0773b9745896ef0612e709c80845a0f7db315b19/packages/isolation/src/providers/worktree.ts#L1068-L1104","documentation":"createFromPR wraps all PR worktree creation (same-repo branch checkout or fork synthetic branch). If any step fails partway — e.g. `git worktree add` succeeded but creating the local tracking branch failed — the orphaned worktree is cleaned up and the original error is rewrapped with the PR number for context.","triggerScenarios":"WorktreeProvider handling a PRIsolationRequest where the underlying git operations fail: fetch of the PR branch fails, `git worktree add` fails (path exists, branch collision), or createBranchWithStaleRetry cannot create the tracking branch (stale branch name, ref conflict).","commonSituations":"PR branch deleted or force-pushed between listing and checkout; leftover worktree/branch from a previous failed run with the same pr-N name; fork PR whose SHA is unreachable because the fork remote was never fetched; network failure mid-creation.","solutions":["Read the inner message after 'Failed to create worktree for PR #N:' — it names the failing git step","Run `git worktree prune` and delete stale `pr-N-review` branches, then retry","Re-fetch the PR (`git fetch origin pull/N/head` or the PR branch) to confirm it still exists and is reachable","For fork PRs, ensure the fork's commits are fetched/available locally before creation","Check disk space and that the target worktree path is not locked by another process"],"exampleFix":"# before\ngit branch -a | grep pr-42-review  # stale branch blocks creation\n# after\ngit branch -D pr-42-review && git worktree prune  # then rerun","handlingStrategy":"try-catch","validationCode":"import { execFile } from 'node:child_process';\nimport { promisify } from 'node:util';\nconst execFileAsync = promisify(execFile);\nexport async function assertPrStateReady(repoPath: string, prBranch: string): Promise<void> {\n  await execFileAsync('git', ['-C', repoPath, 'worktree', 'prune']);\n  await execFileAsync('git', ['-C', repoPath, 'fetch', 'origin', prBranch]); // throws if branch is gone/unreachable\n}","typeGuard":null,"tryCatchPattern":"try {\n  const env = await provider.create(prRequest);\n} catch (e) {\n  const msg = (e as Error).message;\n  if (msg.startsWith('Failed to create worktree for PR #')) {\n    console.error('Prune stale worktrees/branches and confirm the PR branch is fetchable, then retry:', msg);\n  }\n  throw e;\n}","preventionTips":["Run `git worktree prune` regularly to remove stale registrations","Delete stale `pr-N-review` branches after reviews complete","Confirm PR branches are still open and fetchable before creating review worktrees","For fork PRs, fetch the fork ref/SHA before creation"],"tags":["git","worktree","pr","partial-failure","cleanup"],"backgroundTag":"worktree-creation-failed","analyzedSha":"0773b9745896ef0612e709c80845a0f7db315b19","analyzedAt":"2026-09-01T02:28:07.064Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}