scrum4me-mcp/src/git/worktree.ts
Janpeter Visser 70cb4ad2b0 fix(cross-repo): per-repo worktree-branch + PR resolutie (IDEA-062)
Cross-repo sprints (sprint-product = repo X, maar een taak heeft
task.repo_url naar repo Y) faalden op twee plekken omdat sprint-brede
beslissingen werden toegepast op per-repo git-state.

1. createWorktreeForJob (src/git/worktree.ts)
   reuseBranch wordt sprint-breed bepaald in wait-for-job.ts. De eerste
   job die repo Y target krijgt reuseBranch=true terwijl de branch daar
   nooit is aangemaakt -> `git worktree add <path> <branch>` faalt met
   "invalid reference" -> job vast, worker UNHEALTHY. Idem na een
   container-recreate (clone is dan vers).
   Fix: 3-weg fallback in het reuseBranch-pad:
   - lokale branch bestaat   -> hergebruik
   - alleen op origin        -> recreate lokaal vanaf origin/<branch>
   - nergens                 -> fresh vanaf baseRef
   Lost ook het container-recreate-verlies op.

2. maybeCreateAutoPr (src/tools/update-job-status.ts)
   De sprint/story sibling-lookup voor pr_url-hergebruik filterde niet
   op repo. Een repo-Y-job erfde de pr_url van een repo-X-sibling ->
   job.pr_url wees naar de verkeerde repo en er werd nooit een PR voor
   de repo-Y-branch aangemaakt (branch wel gepusht, maar PR-loos).
   Fix: siblings groeperen per repo-bucket ((task.repo_url ?? null));
   alleen een sibling uit dezelfde bucket levert een herbruikbare
   pr_url. Geldt voor SPRINT- en STORY-mode. createPullRequest zelf was
   al repo-correct (gh pr create draait in de worktree).

Tests: 3 nieuwe in worktree.test.ts (reuse-local / recreate-from-origin
/ fresh-fallback), 2 nieuwe in update-job-status-auto-pr.test.ts
(cross-repo story + sprint). update-job-status-mock omgezet naar
findMany. Alle 373 tests groen, build groen.

package-lock.json: version 0.7.0 -> 0.8.0 (was niet mee-gesynced in de
v0.8.0-bump commit 55fa133).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 19:13:38 +02:00

184 lines
6.2 KiB
TypeScript

import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
import * as path from 'node:path'
import * as fs from 'node:fs/promises'
import { getWorktreeRoot } from './worktree-paths.js'
const exec = promisify(execFile)
async function branchExists(repoRoot: string, name: string): Promise<boolean> {
try {
await exec('git', ['show-ref', '--verify', '--quiet', `refs/heads/${name}`], { cwd: repoRoot })
return true
} catch {
return false
}
}
async function remoteBranchExists(repoRoot: string, name: string): Promise<boolean> {
try {
await exec(
'git',
['show-ref', '--verify', '--quiet', `refs/remotes/origin/${name}`],
{ cwd: repoRoot },
)
return true
} catch {
return false
}
}
async function findWorktreeForBranch(
repoRoot: string,
branchName: string,
): Promise<string | null> {
try {
const { stdout } = await exec('git', ['worktree', 'list', '--porcelain'], { cwd: repoRoot })
// Porcelain blocks: worktree <path>\nHEAD <sha>\nbranch refs/heads/<name>\n\n
const blocks = stdout.split('\n\n').filter(Boolean)
for (const block of blocks) {
const lines = block.split('\n')
const wt = lines.find((l) => l.startsWith('worktree '))?.slice(9)
const br = lines.find((l) => l.startsWith('branch '))?.slice(7) // refs/heads/<name>
if (wt && br && br === `refs/heads/${branchName}`) return wt
}
return null
} catch {
return null
}
}
export async function createWorktreeForJob(opts: {
repoRoot: string
jobId: string
branchName: string
baseRef?: string
/**
* When true the branch is expected to exist already (sibling job created it).
* If a stale sibling worktree still occupies the branch, it is removed first
* — siblings are sequential, so this is safe.
*/
reuseBranch?: boolean
}): Promise<{ worktreePath: string; branchName: string }> {
const { repoRoot, jobId, baseRef = 'origin/main', reuseBranch = false } = opts
let { branchName } = opts
const parent = getWorktreeRoot()
await fs.mkdir(parent, { recursive: true })
const worktreePath = path.join(parent, jobId)
// Reject if worktree path already exists — caller must remove it first
try {
await fs.access(worktreePath)
throw new Error(
`Worktree path already exists: ${worktreePath}. Call removeWorktreeForJob first.`,
)
} catch (err: unknown) {
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err
}
await exec('git', ['fetch', 'origin', '--prune'], { cwd: repoRoot })
if (reuseBranch) {
// Sibling task already created the branch; check it out into a fresh worktree.
// If the branch is still attached to a stale sibling worktree, drop that first.
const occupant = await findWorktreeForBranch(repoRoot, branchName)
if (occupant) {
await exec('git', ['worktree', 'remove', '--force', occupant], { cwd: repoRoot })
}
// reuseBranch is decided sprint-wide, but git branches are per-repo. For a
// cross-repo sprint the first job targeting THIS repo gets reuseBranch=true
// even though the branch was never created here; a container recreate also
// wipes the local clone. Fall back gracefully instead of failing with
// "invalid reference":
// - local branch exists → reuse it
// - exists on origin only → recreate the local branch tracking origin
// - nowhere → create it fresh from baseRef
if (await branchExists(repoRoot, branchName)) {
await exec('git', ['worktree', 'add', worktreePath, branchName], { cwd: repoRoot })
} else if (await remoteBranchExists(repoRoot, branchName)) {
await exec(
'git',
['worktree', 'add', '-b', branchName, worktreePath, `origin/${branchName}`],
{ cwd: repoRoot },
)
} else {
await exec('git', ['worktree', 'add', '-b', branchName, worktreePath, baseRef], {
cwd: repoRoot,
})
}
return { worktreePath, branchName }
}
// Fresh branch: if a local branch with this name already exists, it is an
// orphan from a prior failed run (the agent didn't push or branch was
// never tied to a worktree). Remove the orphan so the new worktree gets
// the predictable `feat/story-<id>`-name; this prevents the kind of
// 2-May-2026 failure where the agent inherited an unrelated suffix and
// pushed to a non-existent remote ref.
if (await branchExists(repoRoot, branchName)) {
const occupant = await findWorktreeForBranch(repoRoot, branchName)
if (occupant) {
// Branch is currently checked out elsewhere — likely a sibling worktree
// that should have been cleaned up. Remove it before reusing the name.
try {
await exec('git', ['worktree', 'remove', '--force', occupant], { cwd: repoRoot })
} catch {
// ignore — fall through to deletion below
}
}
try {
await exec('git', ['branch', '-D', branchName], { cwd: repoRoot })
console.warn(`[createWorktreeForJob] removed orphan branch ${branchName} before recreate`)
} catch {
// last resort: timestamp-suffix to avoid collision rather than fail
branchName = `${branchName}-${Date.now()}`
}
}
await exec('git', ['worktree', 'add', '-b', branchName, worktreePath, baseRef], {
cwd: repoRoot,
})
return { worktreePath, branchName }
}
export async function removeWorktreeForJob(opts: {
repoRoot: string
jobId: string
keepBranch?: boolean
}): Promise<{ removed: boolean }> {
const { repoRoot, jobId, keepBranch = false } = opts
const parent = getWorktreeRoot()
const worktreePath = path.join(parent, jobId)
try {
await fs.access(worktreePath)
} catch {
return { removed: false }
}
let branchName: string | undefined
if (!keepBranch) {
try {
const { stdout } = await exec('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
cwd: worktreePath,
})
branchName = stdout.trim()
} catch {
// worktree HEAD unreadable — skip branch deletion
}
}
await exec('git', ['worktree', 'remove', '--force', worktreePath], { cwd: repoRoot })
if (!keepBranch && branchName && (await branchExists(repoRoot, branchName))) {
await exec('git', ['branch', '-D', branchName], { cwd: repoRoot })
}
return { removed: true }
}