fix(daemon): resume a retry whose worktree is gone
prepareWorkspace chose between Manager.Prepare (refresh onto base) and a separate Manager.Resume, and Manager.Resume only opened an already-prepared clone. A remediation whose local worktree the terminal cleanup removed, an expired volume dropped, or a different host never had could therefore never be resumed, and the retry path had no way to select the mode that continues pushed work. Unify the two matched methods into Prepare(..., Target): Fresh resets onto origin/<base>, a non-empty Target resets onto origin/<that branch>, and either target clones a missing worktree. The daemon's retry path selects the target from the task row and passes the persisted branch, so a resume lands on the branch the open PR lives on and fails closed when it is not on the remote. Delete the container-mode no-op resume (hybridTrees.Resume and the resumableTrees capability) now that the daemon owns it. The remediate workflow no longer stages a resume, and the retired remediate.resume step word stays resolvable as an inert stage: a stored definition is validated as a whole collection, so a removed word would fail every workflow to decode, not just its own.
This commit is contained in:
1 parent
c70b144782
commit
6ff239ee70
22 files changed
+363
-246
No files matched your search
@@ -136,7 +136,9 @@ func TestDaemonPreparesWorktreesFromTheMappedBaseBranch(t *testing.T) {
|
||||
}
|
||||
ast.Inspect(file, func(node ast.Node) bool {
|
||||
call, ok := node.(*ast.CallExpr)
|
||||
if !ok || len(call.Args) != 8 {
|
||||
// Prepare carries owner, repo, base, issue, title, body, labels and
|
||||
// the daemon's prepare target: nine arguments.
|
||||
if !ok || len(call.Args) != 9 {
|
||||
return true
|
||||
}
|
||||
selector, ok := call.Fun.(*ast.SelectorExpr)
|
||||
|
||||
+28
-35
@@ -1485,25 +1485,42 @@ func (d *Daemon) prepareWorkspace(ctx context.Context, task *workflow.Task, tree
|
||||
}
|
||||
return dir, true
|
||||
}
|
||||
// A task that continues an already-open PR branch -- the remediate
|
||||
// workflow -- resumes that branch's remote tip instead of resetting onto
|
||||
// base. Preparing one the ordinary way discarded the commits the implement
|
||||
// run had already pushed, so the agent remediated a PR from a tree that no
|
||||
// longer contained it. This runs before Acquire, because the daemon holds
|
||||
// the forge credential that fetches the branch and the container must find
|
||||
// the PR's work already in place.
|
||||
// The retry path selects the worktree mode from the task row. A task that
|
||||
// continues an already-open PR branch -- the remediate workflow -- resumes
|
||||
// that branch's remote tip instead of resetting onto base: preparing one
|
||||
// the ordinary way discarded the commits the implement run had already
|
||||
// pushed, so the agent remediated a PR from a tree that no longer contained
|
||||
// it. Every other task starts fresh on base. This runs before Acquire,
|
||||
// because the daemon holds the forge credential that fetches the branch and
|
||||
// the container must find the PR's work already in place. It also runs on a
|
||||
// retry whose local worktree is gone: Prepare clones a missing worktree for
|
||||
// either target, which is how a resume reaches a task whose clone the
|
||||
// terminal cleanup removed.
|
||||
target := worktree.Fresh
|
||||
if continuesOpenPRBranch(task) {
|
||||
return d.resumePRBranchWorkspace(ctx, task, trees)
|
||||
if task.Branch == "" {
|
||||
// A remediation task is queued against the branch its implement
|
||||
// run pushed, so an empty branch is a dispatch bug upstream and no
|
||||
// retry can fix it.
|
||||
d.Log.Error("remediate task has no persisted branch", "task", task.ID)
|
||||
d.parkRunningTask(ctx, task.ID, "worktree resume failed: remediate task has no branch to resume", taskstate.ParkNeedsHuman)
|
||||
return "", false
|
||||
}
|
||||
target = worktree.Target(task.Branch)
|
||||
}
|
||||
// Every task gets an independent full clone. The former
|
||||
// PreparePersistent path shared objects with a per-repo bare cache;
|
||||
// go-git has no --dissociate, so a shared cache would stay a live
|
||||
// dependency of each worktree and expiring one would corrupt running
|
||||
// tasks. repo.PersistentStorage still governs the container volume.
|
||||
dir, branch, err := trees.Prepare(ctx, task.Owner, task.Repo, repo.BaseBranch(), task.IssueNumber, task.Title, task.Body, task.Labels)
|
||||
dir, branch, err := trees.Prepare(ctx, task.Owner, task.Repo, repo.BaseBranch(), task.IssueNumber, task.Title, task.Body, task.Labels, target)
|
||||
if err != nil {
|
||||
d.Log.Error("worktree prepare failed", "err", err)
|
||||
d.parkRunningTask(ctx, task.ID, "worktree prepare failed: "+err.Error(), taskstate.ParkTransient)
|
||||
reason := "worktree prepare failed: " + err.Error()
|
||||
if target != worktree.Fresh {
|
||||
reason = "worktree resume failed: " + err.Error()
|
||||
}
|
||||
d.Log.Error("worktree prepare failed", "task", task.ID, "err", err)
|
||||
d.parkRunningTask(ctx, task.ID, reason, taskstate.ParkTransient)
|
||||
return "", false
|
||||
}
|
||||
task.Branch = branch
|
||||
@@ -1524,30 +1541,6 @@ func continuesOpenPRBranch(task *workflow.Task) bool {
|
||||
return task.Workflow == "remediate"
|
||||
}
|
||||
|
||||
// resumePRBranchWorkspace prepares a task's worktree on the branch its row
|
||||
// names, never on a branch recomputed from the title or labels: the PR was
|
||||
// opened from the persisted branch, and a retitled issue or relabelled task
|
||||
// would otherwise name a branch that does not exist. It fails closed -- there
|
||||
// is deliberately no fallback to preparing base, because that fallback is the
|
||||
// defect this route removes.
|
||||
func (d *Daemon) resumePRBranchWorkspace(ctx context.Context, task *workflow.Task, trees *worktree.Manager) (string, bool) {
|
||||
if task.Branch == "" {
|
||||
// A remediation task is queued against the branch its implement run
|
||||
// pushed, so an empty branch is a dispatch bug upstream and no retry
|
||||
// can fix it.
|
||||
d.Log.Error("remediate task has no persisted branch", "task", task.ID)
|
||||
d.parkRunningTask(ctx, task.ID, "worktree resume failed: remediate task has no branch to resume", taskstate.ParkNeedsHuman)
|
||||
return "", false
|
||||
}
|
||||
dir := trees.Dir(task.Owner, task.Repo, task.IssueNumber)
|
||||
if err := trees.Resume(ctx, dir, task.Branch); err != nil {
|
||||
d.Log.Error("worktree resume failed", "task", task.ID, "branch", task.Branch, "err", err)
|
||||
d.parkRunningTask(ctx, task.ID, "worktree resume failed: "+err.Error(), taskstate.ParkTransient)
|
||||
return "", false
|
||||
}
|
||||
return dir, true
|
||||
}
|
||||
|
||||
// cleanupTerminalTaskWorktree removes a task's worktree once its forge side is
|
||||
// settled, unless the worktree still holds work no commit captured.
|
||||
//
|
||||
|
||||
@@ -23,7 +23,7 @@ import (
|
||||
func pushedImplementRun(t *testing.T, trees *worktree.Manager, title string) (dir, branch string, tip plumbing.Hash) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
dir, branch, err := trees.Prepare(ctx, "acme", "widget", "main", 7, title, "", "bug")
|
||||
dir, branch, err := trees.Prepare(ctx, "acme", "widget", "main", 7, title, "", "bug", worktree.Fresh)
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v", err)
|
||||
}
|
||||
@@ -116,6 +116,42 @@ func TestPrepareWorkspaceResumesThePersistedBranchForARemediation(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPrepareWorkspaceResumesAPRBranchAfterTheWorktreeWasRemoved pins the
|
||||
// retry failure archie-core-866m reports. Manager.Resume opens an
|
||||
// already-prepared worktree, so a remediation whose local clone is gone -- a
|
||||
// first run on this host, an expired volume, a worktree the terminal cleanup
|
||||
// removed before a retry -- can never be resumed, and the retry path offers
|
||||
// only fresh-from-base or that unrunnable resume. Preparing a resume must
|
||||
// clone when the worktree is missing, exactly as a fresh prepare does, and
|
||||
// land on the PR branch tip rather than base.
|
||||
func TestPrepareWorkspaceResumesAPRBranchAfterTheWorktreeWasRemoved(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
trees := newTestTrees(t, newLocalRemote(t, "acme", "widget"))
|
||||
dir, branch, tip := pushedImplementRun(t, trees, "fix: original issue")
|
||||
|
||||
// The clone is gone before the retry reaches prepareWorkspace.
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatalf("remove worktree: %v", err)
|
||||
}
|
||||
|
||||
d, st, _ := testDaemon(t, 3, 0)
|
||||
task := claimedTask(t, st, "fix: original issue", "remediate", branch)
|
||||
|
||||
gotDir, ok := d.prepareWorkspace(ctx, task, trees, config.Repo{Owner: "acme", Name: "widget", Base: "main"})
|
||||
if !ok {
|
||||
t.Fatal("prepareWorkspace() = false, want the remediation prepared on its PR branch after the worktree was removed")
|
||||
}
|
||||
if gotDir != dir {
|
||||
t.Errorf("prepareWorkspace() dir = %q, want the task's worktree %q", gotDir, dir)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(gotDir, "widget.go")); err != nil {
|
||||
t.Fatalf("widget.go missing after prepare: %v (the PR branch's work was not restored)", err)
|
||||
}
|
||||
if got := headOf(t, gotDir); got != tip {
|
||||
t.Errorf("worktree HEAD = %s, want the PR branch tip %s", got, tip)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPrepareWorkspaceFailsClosedWhenTheRemediationBranchIsGone pins the other
|
||||
// half of the same routing: a remediation whose branch is not on the remote
|
||||
// must park, not quietly fall back to preparing base -- the fallback is exactly
|
||||
|
||||
@@ -276,7 +276,7 @@ func TestCleanupTerminalTaskWorktreeWithCancelledContext(t *testing.T) {
|
||||
// A prepared clone: the cancelled context is the thing under test,
|
||||
// so the worktree itself is readied with a live one.
|
||||
if _, _, err := trees.Prepare(context.Background(), task.Owner, task.Repo, "main",
|
||||
task.IssueNumber, task.Title, task.Body, task.Labels); err != nil {
|
||||
task.IssueNumber, task.Title, task.Body, task.Labels, worktree.Fresh); err != nil {
|
||||
t.Fatalf("prepare worktree: %v", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -248,7 +248,7 @@ func TestCleanupTerminalTaskWorktreeLifecycle(t *testing.T) {
|
||||
workDir := targetTrees.Dir(claimed.Owner, claimed.Repo, claimed.IssueNumber)
|
||||
// A prepared clone, the only shape the daemon ever cleans up.
|
||||
if _, _, err := targetTrees.Prepare(ctx, claimed.Owner, claimed.Repo, "main",
|
||||
claimed.IssueNumber, claimed.Title, claimed.Body, claimed.Labels); err != nil {
|
||||
claimed.IssueNumber, claimed.Title, claimed.Body, claimed.Labels, worktree.Fresh); err != nil {
|
||||
t.Fatalf("prepare worktree: %v", err)
|
||||
}
|
||||
if tt.uncommitted {
|
||||
|
||||
Reference in new issue
Block a user