Repository navigation
fix(host-service): workspace delete re-checks the disk after git unregisters the worktree #6785
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
abab14d
ba91e8f
19d3510
010b5a5
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { existsSync, statSync } from "node:fs"; | ||
| import { existsSync, lstatSync, statSync } from "node:fs"; | ||
| import { rm } from "node:fs/promises"; | ||
| import { sanitizePromptForPty } from "@superset/shared/agent-prompt-launch"; | ||
| import { TRPCError } from "@trpc/server"; | ||
|
|
@@ -365,6 +365,16 @@ function isMissingDirectory(path: string): boolean { | |
| } | ||
| } | ||
|
|
||
| /** Like isMissingDirectory, but does not follow a final symlink: a dangling | ||
| * link at the worktree path is still an entry to remove, not an absence. */ | ||
| function isMissingPath(path: string): boolean { | ||
| try { | ||
| return lstatSync(path, { throwIfNoEntry: false }) === undefined; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function archiveReasonFor( | ||
| ctx: HostServiceContext, | ||
| local: { pullRequestId: string | null }, | ||
|
|
@@ -514,8 +524,9 @@ async function runDestroyPhases( | |
| // treat that like "still registered" and block rather than risk | ||
| // orphaning disk past the archive commit point. | ||
| let stillRegistered = true; | ||
| let removeError: string | undefined; | ||
| try { | ||
| ({ stillRegistered } = await cleanupGitOps.removeWorktree({ | ||
| ({ stillRegistered, removeError } = await cleanupGitOps.removeWorktree({ | ||
| repoPath: project.repoPath, | ||
| worktreePath: local.worktreePath, | ||
| gitEnv: repoGitEnv, | ||
|
|
@@ -533,10 +544,53 @@ async function runDestroyPhases( | |
| // retryable instead of orphaning disk past the commit point. | ||
| throw new TRPCError({ | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| message: `Failed to remove worktree at ${local.worktreePath}`, | ||
| message: `Failed to remove worktree at ${local.worktreePath}${ | ||
| removeError ? `: ${removeError}` : "" | ||
| }`, | ||
| }); | ||
| } | ||
| worktreeRemoved = true; | ||
| if (!isMissingPath(local.worktreePath)) { | ||
| // Unregistered is not removed: git's unregistration and its | ||
| // recursive delete are not atomic, so `remove --force --force` | ||
| // can drop the registration and still fail partway through | ||
| // deleting files (locked file, live writer). Trusting the | ||
| // registry alone silently orphaned the folder — no list shows | ||
| // it, and a retry reports success without touching it (#6730). | ||
| // Fall back to the same guarded direct removal the other | ||
| // branches use. | ||
| const worktreeBaseDir = | ||
| project.worktreeBaseDir ?? getHostWorktreeBaseDir(ctx); | ||
| if ( | ||
| !isInsideProjectWorktreesRoot( | ||
| local.worktreePath, | ||
| project.id, | ||
| worktreeBaseDir, | ||
| ) | ||
| ) { | ||
| warnings.push( | ||
| `Worktree at ${local.worktreePath} is no longer registered with git, but its folder is outside the managed worktrees root and was left on disk`, | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } else { | ||
| try { | ||
| await rm(local.worktreePath, { recursive: true, force: true }); | ||
|
Contributor
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. P1: Protect this fallback deletion from ancestor replacement between the root check and Prompt for AI agents |
||
| } catch (err) { | ||
| const message = err instanceof Error ? err.message : String(err); | ||
| throw new TRPCError({ | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| message: `Worktree at ${local.worktreePath} is no longer registered with git, but its folder could not be removed: ${message}${ | ||
| removeError ? ` (git worktree remove: ${removeError})` : "" | ||
| }`, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
| } | ||
| // The outside-root branch above leaves the folder in place, so | ||
| // report removal from the final disk state rather than assuming | ||
| // this path always cleared it (#6785 review). `isMissingPath` | ||
| // rather than `existsSync`: a leftover this process cannot read, | ||
| // or a dangling symlink, still exists and must not be reported | ||
| // as removed. | ||
| worktreeRemoved = isMissingPath(local.worktreePath); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
P2: When the worktree path is a dangling symlink, this branch's entry check and the new isMissingPath disagree. existsSync() follows the link and returns false, so
worktreeRemoved = !existsSync(local.worktreePath)(line ~470) reports the symlink as already gone, while the new isMissingPath correctly treats it as a leftover. IfresolveGitEnvthen fails andworktreeRemovedis already true, the code only pushes a warning,repoGitEnvstays null, and theif (repoGitEnv)block that would remove the link never runs — destroy returnsworktreeRemoved: truewith the symlink still present inside the managed root. That contradicts the PR's goal of no orphaned entries, and the case is exactly the dangling-symlink scenario commit 010b5a5 targets. Make the entry check use the same non-following semantics as isMissingPath.Prompt for AI agents