scrum4me-mcp/src/git/worktree.ts
Madhura68 f7f5a487ec PBI-9 + PBI-47: worktree foundation, product-worktrees, P0 fixes, PAUSED flow
Adds two interlocking PBIs:

PBI-9 — Worktree foundation + persistent product-worktrees for idea-jobs
  - src/git/worktree-paths.ts: centralised root + skip-set + lock-path helpers
  - src/git/file-lock.ts: proper-lockfile wrapper, deadlock-safe ordered acquire
  - src/git/product-worktree.ts: detached-HEAD worktree per product, .scratch/
    excluded via git rev-parse --git-path (handles linked .git file)
  - src/git/job-locks.ts: setupProductWorktrees + releaseLocksOnTerminal
  - wait-for-job.ts: idea-branch wires product-worktrees for IDEA_GRILL/MAKE_PLAN
  - update-job-status.ts + pbi-cascade.ts + stale-reset: release on all four
    server-side terminal transitions (DONE/FAILED/CANCELLED/stale)
  - cleanup-my-worktrees: skip _products/ + *.lock
  - README: worktrees section with single-host invariant + advisory-lock path

PBI-47 — Sprint-flow P0 corrections + PAUSED flow with rich pause_context
  - prisma schema: ClaudeJob.{base_sha,head_sha} + SprintRun.pause_context
  - tryClaimJob captures base_sha; prepareDoneUpdate captures head_sha
  - verify-task-against-plan diffs vs base_sha (no more origin/main fallback);
    rejects with MISSING_BASE_SHA when null — fixes per-task verify-scope P0
  - pr.ts: createPullRequest enableAutoMerge default false; new
    enableAutoMergeOnPr with --match-head-commit guard + 5-category typed
    EnableAutoMergeResult — fixes STORY auto-merge timing P0
  - src/flow/{effects,worktree-lease,pr-flow,sprint-run}.ts: pure transition
    modules + idempotent declarative effects executor
  - update-job-status: STORY auto-merge fires only on the last task of the
    story (story.status === DONE), with head_sha as merge guard; MERGE_CONFLICT
    routes to sprint-run flow which produces CREATE_CLAUDE_QUESTION +
    SET_SPRINT_RUN_STATUS effects with rich pause_context

Tests: 31 test files, 242 passing. Pure-transition tests cover STORY 3-tasks
auto-merge timing, SPRINT draft→ready, MERGE_CONFLICT pause/resume, file-lock
deadlock prevention, worktree-lease lifecycle, delete-only verify (ALIGNED),
per-job verify scope (base_sha isolation), 5-category auto-merge errors.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 21:09:48 +02:00

151 lines
5 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 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 })
}
await exec('git', ['worktree', 'add', worktreePath, branchName], { 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 }
}