Repository navigation
fix(doctor): race-free branch delete + ignored-secret guard (review findings on #2712) #2713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,7 +115,15 @@ function gitFailure(stderr: string): GitOutcome { | |
| */ | ||
| function git(cwd: string, args: string[]): GitOutcome { | ||
| try { | ||
| const res = spawnSync('git', args, { cwd, encoding: 'utf-8', stdio: ['ignore', 'pipe', 'pipe'] }); | ||
| const res = spawnSync('git', args, { | ||
| cwd, | ||
| encoding: 'utf-8', | ||
| stdio: ['ignore', 'pipe', 'pipe'], | ||
| // Bounded: a probe hanging on a lock, credential prompt, or network mount | ||
| // must not wedge `genie doctor`; a timeout surfaces as a refusal. | ||
| timeout: 10_000, | ||
| maxBuffer: 8 * 1024 * 1024, | ||
| }); | ||
| if (res.error) return gitFailure(res.error.message); | ||
| return { ok: res.status === 0, status: res.status, stdout: res.stdout ?? '', stderr: (res.stderr ?? '').trim() }; | ||
| } catch (error) { | ||
|
|
@@ -190,7 +198,7 @@ export function parseWorktreePorcelain(text: string): WorktreeRecord[] { | |
| * takes `ref`; only human output takes `name`. See {@link resolveIntegration} | ||
| * for why the distinction is load-bearing. | ||
| */ | ||
| interface IntegrationBranch { | ||
| export interface IntegrationBranch { | ||
| /** Short form, e.g. `dev` or `origin/main`. Display only — never a git argument. */ | ||
| name: string; | ||
| /** Fully-qualified ref, e.g. `refs/heads/dev`. The only form a probe may use. */ | ||
|
|
@@ -235,6 +243,31 @@ export function resolveIntegrationBranch(root: string): string | null { | |
| * with a launch branch would prove ITS containment while the `git branch -D` | ||
| * below still deletes the branch — orphaning every commit the branch carried. | ||
| */ | ||
| /** | ||
| * Gitignored files are deleted by `git worktree remove` (git's documented | ||
| * semantics) and are usually the point of the reclaim — node_modules, build | ||
| * output. The exception is user-owned secret material, which must never ride a | ||
| * cleanup. The probe refuses removal when an ignored file's basename matches a | ||
| * small sensitive-pattern set; everything else stays disclosed-but-removable. | ||
| * A probe failure (timeout, oversized listing) refuses via the caller's | ||
| * fail-closed error class — an unanswered probe proves nothing. | ||
| */ | ||
| const SENSITIVE_IGNORED = [/^\.env(\..+)?$/, /\.pem$/, /\.key$/, /^id_(rsa|ed25519|ecdsa)/, /credential/i, /secret/i]; | ||
|
|
||
| function findIgnoredSecret(path: string): string | null { | ||
| // `traditional` collapses a fully-ignored directory (node_modules/) to one | ||
| // line, keeping the listing bounded on exactly the worktrees worth reclaiming. | ||
| const listing = git(path, ['status', '--porcelain', '--ignored=traditional']); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a secret is inside an ignored directory whose own basename is not sensitive, such as AGENTS.md reference: AGENTS.md:L31-L31 Useful? React with 👍 / 👎. |
||
| if (!listing.ok) return 'unlistable ignored files'; | ||
| for (const line of listing.stdout.split('\n')) { | ||
| if (!line.startsWith('!!')) continue; | ||
| const rel = line.slice(2).trim().replace(/\/$/, ''); | ||
| const base = rel.split('/').pop() ?? rel; | ||
| if (SENSITIVE_IGNORED.some((re) => re.test(base))) return rel; | ||
|
Comment on lines
+264
to
+266
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a sensitive ignored filename requires Git quoting, such as AGENTS.md reference: AGENTS.md:L31-L31 Useful? React with 👍 / 👎. |
||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function classifyEntry( | ||
| root: string, | ||
| path: string, | ||
|
|
@@ -246,6 +279,8 @@ function classifyEntry( | |
| const status = git(path, ['status', '--porcelain']); | ||
| if (!status.ok) return { ...entry, disposition: 'error', reason: firstLine(status.stderr, 'git status failed') }; | ||
| if (status.stdout.trim() !== '') return { ...entry, disposition: 'dirty', reason: 'uncommitted changes' }; | ||
| const secret = findIgnoredSecret(path); | ||
| if (secret !== null) return { ...entry, disposition: 'dirty', reason: `ignored secret present (${secret})` }; | ||
|
Comment on lines
+282
to
+283
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a process writes an ignored AGENTS.md reference: AGENTS.md:L31-L31 Useful? React with 👍 / 👎. |
||
| const ancestry = git(root, ['merge-base', '--is-ancestor', `refs/heads/${branch}`, integration.ref]); | ||
| if (ancestry.ok) return { ...entry, disposition: 'removable', reason: 'merged+clean', sizeBytes: safeSizeOf(path) }; | ||
| // `--is-ancestor` exits 1 for a definitive "no"; anything else is a broken | ||
|
|
@@ -410,18 +445,26 @@ export function removeLaunchWorktree( | |
| entry: LaunchWorktreeEntry, | ||
| integration: IntegrationBranch, | ||
| ): string | null { | ||
| // Re-prove ancestry at removal time: a commit landing on the branch between | ||
| // the scan and this call would leave the tree clean (so `worktree remove` | ||
| // would still succeed) while making `-D` an orphaning force-delete. Once the | ||
| // worktree is gone the branch can gain no commits — git refuses a second | ||
| // checkout of the same branch — so re-proving HERE closes the whole window. | ||
| // Early re-proof: a commit landed since the scan refuses BEFORE the worktree | ||
| // is touched, preserving it intact. This probe alone cannot close the race — | ||
| // a commit can still land after it — so it is an ergonomic guard, not the | ||
| // authoritative one. | ||
| if (entry.branch !== null) { | ||
| const ancestry = git(root, ['merge-base', '--is-ancestor', `refs/heads/${entry.branch}`, integration.ref]); | ||
| if (!ancestry.ok) return `kept: branch advanced past ${integration.name} after the scan (ancestry re-proof failed)`; | ||
| } | ||
| const removed = git(root, ['worktree', 'remove', entry.path]); | ||
| if (!removed.ok) return `git worktree remove refused: ${firstLine(removed.stderr, 'unknown git error')}`; | ||
| if (entry.branch === null) return null; | ||
| // AUTHORITATIVE re-proof, after removal and before the forced delete: with | ||
| // the worktree gone the branch is frozen (git refuses a second checkout of a | ||
| // checked-out branch, and no checkout holds it now), so an ancestry answer | ||
| // taken HERE cannot be invalidated before `-D` — this is the only point in | ||
| // the sequence where the proof and the delete are race-free. | ||
| const frozen = git(root, ['merge-base', '--is-ancestor', `refs/heads/${entry.branch}`, integration.ref]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When another process advances this now-unchecked-out branch after the ancestry probe but before Useful? React with 👍 / 👎. |
||
| if (!frozen.ok) { | ||
| return `worktree removed; branch ${entry.branch} kept: commits landed mid-removal (post-removal ancestry re-proof failed)`; | ||
| } | ||
| const deleted = git(root, ['branch', '-D', entry.branch]); | ||
| if (!deleted.ok) { | ||
| return `worktree removed; branch ${entry.branch} kept: ${firstLine(deleted.stderr, 'unknown git error')}`; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When removing a large worktree or one on a slow filesystem takes more than ten seconds, this shared wrapper terminates
git worktree removemidway because the timeout applies to mutations as well as probes. That can leave a partially deleted checkout and stale worktree registration while reporting that removal was refused. Keep the bound on read-only probes, but do not kill an in-progress destructive operation at the same fixed deadline.Useful? React with 👍 / 👎.