{"record":{"id":"af40d459d43ea943","repo":"coleam00/Archon","slug":"cannot-verify-worktree-ownership-at-worktreepath","errorCode":null,"errorMessage":"Cannot verify worktree ownership at ${worktreePath}: ${(error as Error).message}","messagePattern":"Cannot verify worktree ownership at (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/git/src/worktree.ts","lineNumber":469,"sourceCode":"    // a TOCTOU race or filesystem corruption. Fail fast.\n    // EACCES/EIO/etc.: cannot verify ownership — fail fast rather than\n    // defaulting to permissive adoption.\n    throw wrap(`Cannot verify worktree ownership at ${worktreePath}: ${err.message}`);\n  }\n\n  if (!gitContent.startsWith('gitdir:')) {\n    throw new Error(`Cannot adopt ${worktreePath}: .git pointer is not a git-worktree reference.`);\n  }\n\n  let worktreeIdentity: GitCheckoutIdentity;\n  let expectedIdentity: GitCheckoutIdentity;\n  try {\n    [worktreeIdentity, expectedIdentity] = await Promise.all([\n      getGitCheckoutIdentity(worktreePath),\n      getGitCheckoutIdentity(expectedRepo),\n    ]);\n  } catch (error) {\n    throw new Error(\n      `Cannot verify worktree ownership at ${worktreePath}: ${(error as Error).message}`,\n      {\n        cause: error,\n      }\n    );\n  }\n  if (!worktreeIdentity.linkedWorktree) {\n    // Not a git-worktree pointer (e.g., submodule pointer, or malformed).\n    // We cannot confirm this is our worktree, so refuse adoption.\n    throw new Error(`Cannot adopt ${worktreePath}: .git pointer is not a git-worktree reference.`);\n  }\n\n  // Compare resolved common-directory paths: the primary checkout and every\n  // linked worktree share this Git identity, while separate clones differ.\n  if (resolve(worktreeIdentity.commonGitDir) !== resolve(expectedIdentity.commonGitDir)) {\n    throw new Error(\n      `Worktree at ${worktreePath} belongs to a different clone (${worktreeIdentity.commonGitDir}). ` +\n        'Remove it from that clone or use a different codebase registration.'","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/coleam00/Archon/blob/0773b9745896ef0612e709c80845a0f7db315b19/packages/git/src/worktree.ts#L451-L487","documentation":"verifyWorktreeOwnership proves a directory at worktreePath is a linked worktree belonging to expectedRepo by comparing Git checkout identities of both. If either `git` identity query fails (not a git dir, corrupt .git pointer, git binary error), it throws wrapping the underlying message with `cause` preserved. Adoption is refused because ownership cannot be proven.","triggerScenarios":"Calling assertWorktreeOwnership or findExisting against a path where getGitCheckoutIdentity fails — the path is not a git worktree/checkout, the .git file is malformed, the expected repo path is missing or not a git repo, or git itself errors (permissions, broken install).","commonSituations":"Reusing a directory that was never a worktree (plain folder with a leftover .git file); the expected repo was moved/deleted so its identity can't be read; a partially-deleted or corrupted worktree; running as a user without read access to the git metadata.","solutions":["Run `git worktree list` in the expected repo and `git rev-parse --git-common-dir` in worktreePath to see which side failed.","Recreate the worktree with `git worktree add <path> <branch>` if it is corrupted or was never a real worktree.","Fix the expectedRepo registration/path so it points at an existing git clone.","Remove the orphaned directory and let isolation provisioning create a fresh worktree."],"exampleFix":"// before: adopting a directory that is not a worktree\nawait assertWorktreeOwnership('/srv/app', '/srv/clone');\n// after: recreate it as a linked worktree of the expected repo\ncd /srv/clone && git worktree add /srv/app feat-branch\nawait assertWorktreeOwnership('/srv/app', '/srv/clone');","handlingStrategy":"try-catch","validationCode":"import { existsSync } from 'node:fs';\nif (!existsSync(expectedRepo) || !existsSync(`${expectedRepo}/.git`)) {\n  throw new Error(`expected repo ${expectedRepo} is not an existing git checkout`);\n}\nif (!existsSync(`${worktreePath}/.git`)) {\n  throw new Error(`${worktreePath} has no .git entry; not a worktree candidate`);\n}","typeGuard":"function looksLikeWorktree(p: string): boolean {\n  try { return readFileSync(`${p}/.git`, 'utf8').startsWith('gitdir:'); } catch { return false; }\n}","tryCatchPattern":"try {\n  await assertWorktreeOwnership(worktreePath, expectedRepo);\n} catch (e) {\n  if (e.message.startsWith('Cannot verify worktree ownership')) {\n    console.error('Recreate the worktree:', e.cause);\n  } else throw e;\n}","preventionTips":["Create directories only via `git worktree add`, never by hand-copying.","Verify `git worktree list` shows the path before attempting adoption.","Keep the parent clone present and readable; don't delete it while worktrees exist."],"tags":["git","worktree","ownership","verification"],"backgroundTag":"worktree-ownership-unverifiable","analyzedSha":"0773b9745896ef0612e709c80845a0f7db315b19","analyzedAt":"2026-09-01T02:28:07.064Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}