fix: recover failed task launches safely (#3437)

This commit is contained in:
Carlos Florêncio
2026-09-07 11:19:09 +01:00
committed by GitHub
parent 3c6af41063
commit 3fba828e2f
69 changed files with 2280 additions and 277 deletions
+18 -7
View File
@@ -15,6 +15,13 @@ import (
"github.com/kandev/kandev/internal/common/subproc"
)
// pullRequestSnapshotRef is internal state owned by Kandev. Keeping PR heads
// outside refs/remotes/origin prevents ordinary remote pruning or a user branch
// named pr/<N> from changing the commit selected for a task launch.
func pullRequestSnapshotRef(prNumber int) string {
return fmt.Sprintf("refs/kandev/pull/%d/head", prNumber)
}
// isGitRepo checks if a path is a Git repository.
func (m *Manager) isGitRepo(path string) bool {
gitDir := filepath.Join(path, ".git")
@@ -212,18 +219,16 @@ func (m *Manager) prepareCheckoutFromRefreshedOrigin(ctx context.Context, repoPa
}
// prepareBranchFromRefreshedOrigin selects a provider-refreshed source branch
// without contacting origin. A PR number selects the dedicated origin/pr/<N>
// ref, which is also how fork PR heads are kept available after the
// authenticated refresh. When both refs exist, the selected ref is the one
// that contains the other. A local-only ref is preserved, a refreshed remote
// ref is returned to the caller as the worktree start point, and divergence or
// an unverified relationship fails closed.
// without contacting origin. A PR number selects the dedicated Kandev-owned
// snapshot ref, which is also how fork PR heads are kept available after the
// authenticated refresh. PR preparation never creates or resets a local
// branch with the PR's source name.
func (m *Manager) prepareBranchFromRefreshedOrigin(
ctx context.Context, repoPath, localBranch, sourceBranch string, prNumber int,
) (string, error) {
remoteRef := "origin/" + sourceBranch
if prNumber > 0 {
remoteRef = fmt.Sprintf("origin/pr/%d", prNumber)
remoteRef = pullRequestSnapshotRef(prNumber)
}
localExists, err := m.branchExists(ctx, repoPath, localBranch)
if err != nil {
@@ -233,6 +238,12 @@ func (m *Manager) prepareBranchFromRefreshedOrigin(
if err != nil {
return "", err
}
if prNumber > 0 {
if !remoteExists {
return "", fmt.Errorf("required fetched remote ref %q is missing: %w", remoteRef, ErrWorkspaceCheckoutFailed)
}
return remoteRef, nil
}
if !localExists && !remoteExists {
return "", nil
}