feat(taskactions): archive reaps the task's worktree; cleanup test models a completed run
A worktree terminal cleanup keeps (uncaptured or unreadable work) now has a reap path: archiving calls Daemon.RemoveWorktree for the owning identity's trees, and the keep logs name archive as the way out. The no-change cleanup test reports completed through one direct transition instead of reaching merged via a faked pr_open hop.
This commit is contained in:
6 files changed
+73
-29
No files matched your search
@@ -1474,16 +1474,16 @@ func (d *Daemon) worktreeHoldsUncapturedWork(ctx context.Context, trees *worktre
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return false
|
||||
}
|
||||
d.Log.Warn("terminal worktree kept: it could not be read", "task", task.ID, "dir", dir, "err", err)
|
||||
d.Log.Warn("terminal worktree kept: it could not be read; archive the task to remove it", "task", task.ID, "dir", dir, "err", err)
|
||||
return true
|
||||
}
|
||||
uncommitted, err := trees.HasUncommittedChanges(ctx, dir)
|
||||
if err != nil {
|
||||
d.Log.Warn("terminal worktree kept: uncommitted work could not be read", "task", task.ID, "dir", dir, "err", err)
|
||||
d.Log.Warn("terminal worktree kept: uncommitted work could not be read; archive the task to remove it", "task", task.ID, "dir", dir, "err", err)
|
||||
return true
|
||||
}
|
||||
if uncommitted {
|
||||
d.Log.Info("terminal worktree kept: it holds uncommitted work", "task", task.ID, "dir", dir)
|
||||
d.Log.Info("terminal worktree kept: it holds uncommitted work; archive the task to remove it", "task", task.ID, "dir", dir)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@@ -2280,3 +2280,14 @@ func withTaskTimeLimit(ctx context.Context, limit time.Duration) (context.Contex
|
||||
}
|
||||
return context.WithTimeoutCause(ctx, limit, fmt.Errorf("%w (%s)", errTaskTimeLimit, limit))
|
||||
}
|
||||
|
||||
// RemoveWorktree deletes a task's clone from the trees of the identity that
|
||||
// owns it. Archiving calls it: terminal cleanup keeps a worktree that may hold
|
||||
// uncaptured work, and the operator's archive is the decision to discard it.
|
||||
func (d *Daemon) RemoveWorktree(owner, repo, identity string, issue int) error {
|
||||
trees := d.treesFor(&workflow.Task{Owner: owner, Repo: repo, Identity: identity})
|
||||
if trees == nil {
|
||||
return nil
|
||||
}
|
||||
return trees.Cleanup(owner, repo, issue)
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package daemon
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -274,14 +276,14 @@ func TestProcessCleansWorktreeOnTerminalNoChange(t *testing.T) {
|
||||
wantTreeExists bool
|
||||
}{
|
||||
{
|
||||
name: "process removes worktree when worker reports merged without PR",
|
||||
workerStatus: workflow.StatusMerged,
|
||||
name: "process removes worktree when worker completes without a PR",
|
||||
workerStatus: workflow.StatusCompleted,
|
||||
taskIdentity: "",
|
||||
wantTreeExists: false,
|
||||
},
|
||||
{
|
||||
name: "process removes identity worktree when worker reports merged without PR",
|
||||
workerStatus: workflow.StatusMerged,
|
||||
name: "process removes identity worktree when worker completes without a PR",
|
||||
workerStatus: workflow.StatusCompleted,
|
||||
taskIdentity: "winter",
|
||||
wantTreeExists: false,
|
||||
},
|
||||
@@ -373,21 +375,9 @@ func TestProcessCleansWorktreeOnTerminalNoChange(t *testing.T) {
|
||||
targetTrees := d.treesFor(task)
|
||||
workDir := targetTrees.Dir(task.Owner, task.Repo, task.IssueNumber)
|
||||
|
||||
// The route the worker's completion report takes to its status,
|
||||
// computed here because t.Fatalf is not callable from the
|
||||
// subscription goroutine below.
|
||||
route, err := taskstatetest.Path(workflow.StatusRunning, tt.workerStatus)
|
||||
if err != nil {
|
||||
t.Fatalf("no legal route to %s: %v", tt.workerStatus, err)
|
||||
}
|
||||
|
||||
sub, err := mustCoreConn(t, busClient).Subscribe(agentnats.SubjectForTask(task.ID), func(msg *natsio.Msg) {
|
||||
// Transition task in store as archie-agent would do over the gRPC State Store
|
||||
at := workflow.StatusRunning
|
||||
for _, to := range route {
|
||||
_ = s.Transition(ctx, task.ID, at, to, "worker completion")
|
||||
at = to
|
||||
}
|
||||
_ = s.Transition(ctx, task.ID, workflow.StatusRunning, tt.workerStatus, "worker completion")
|
||||
resp, _ := json.Marshal(taskrun.Response{
|
||||
Status: tt.workerStatus,
|
||||
})
|
||||
@@ -408,3 +398,27 @@ func TestProcessCleansWorktreeOnTerminalNoChange(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRemoveWorktreeReapsTheOwningIdentitysClone is the operator's reap path
|
||||
// for a worktree terminal cleanup kept: archiving removes the clone under the
|
||||
// trees of the identity that owns the task, and only that clone.
|
||||
func TestRemoveWorktreeReapsTheOwningIdentitysClone(t *testing.T) {
|
||||
root := &worktree.Manager{WorkDir: t.TempDir()}
|
||||
winter := &worktree.Manager{WorkDir: t.TempDir()}
|
||||
d := &Daemon{Trees: root, Identities: []*IdentityRunner{{Name: "winter", Trees: winter}}}
|
||||
for _, m := range []*worktree.Manager{root, winter} {
|
||||
if err := os.MkdirAll(m.Dir("acme", "widget", 7), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := d.RemoveWorktree("acme", "widget", "winter", 7); err != nil {
|
||||
t.Fatalf("RemoveWorktree: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(winter.Dir("acme", "widget", 7)); !errors.Is(err, fs.ErrNotExist) {
|
||||
t.Errorf("identity worktree still on disk: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(root.Dir("acme", "widget", 7)); err != nil {
|
||||
t.Errorf("root worktree removed: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user