test(workflow): budget stop asks to continue and resumes on approval (#1832)

This commit is contained in:
sam committed 2026-11-09 09:41:46 +11:00
1 parent 561863613f
commit f27e9853cc
1 file changed
+89
@@ -0,0 +1,89 @@
package workflow
import (
"context"
"log/slog"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/agentrun"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// sequenceRunner answers each agent run with the next result.
type sequenceRunner struct{ results []agentrun.Result }
func (s *sequenceRunner) Run(context.Context, string, agentrun.Request, agentrun.Reporter) (agentrun.Result, error) {
res := s.results[0]
s.results = s.results[1:]
return res, nil
}
// A stage that opts in asks for approval when it reaches its step budget and
// keeps its worktree for the continuation; approval runs it again from there
// to completion. Any other stop parks as before.
func TestBudgetStopAsksToContinue(t *testing.T) {
stopped := func(reason string) agentrun.Result {
return agentrun.Result{
Version: agentrun.ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "build",
Status: "parked", StopReason: reason, Detail: "stopped after 60/60 steps",
Summary: "halfway through the migration", Changes: []string{"internal/auth/token.go"},
}
}
passed := agentrun.Result{Version: agentrun.ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "build", Status: agentrun.StatusPassed}
tests := []struct {
name string
approve bool
reason string
wantStatus string
wantKeep bool
}{
{name: "step budget asks", approve: true, reason: "budget_exhausted", wantStatus: StatusWaitingApproval, wantKeep: true},
{name: "not opted in parks", reason: "budget_exhausted", wantStatus: StatusParked},
{name: "wall clock parks", approve: true, reason: "timed_out", wantStatus: StatusParked},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
runner := &sequenceRunner{results: []agentrun.Result{stopped(tt.reason), passed}}
tk := &task.Task{ID: 1, Attempt: 1}
newContext := func() *TaskContext {
return &TaskContext{
Task: tk, Store: &fakeStepStore{}, Log: slog.Default(), Agent: runner,
Cfg: config.Config{Models: map[string]string{"default": "test/model"}},
}
}
wf := Workflow{Name: "implement", Stages: []Stage{
AgentStage{Name: "build", ApproveBudget: tt.approve, Mission: func(*TaskContext) string { return "do it" }}.Stage(),
}}
tc := newContext()
Run(context.Background(), wf, tc)
if tc.Outcome.Status != tt.wantStatus {
t.Fatalf("outcome = %q, want %q", tc.Outcome.Status, tt.wantStatus)
}
if keeps := tk.RetryMode == string(taskstate.RetryKeepWorktree); keeps != tt.wantKeep {
t.Fatalf("keeps worktree = %v, want %v", keeps, tt.wantKeep)
}
if !tt.wantKeep {
return
}
if kind := taskstate.ApprovalKind(StatusWaitingApproval, "", tk.RetryMode); kind != taskstate.ApprovalBudget {
t.Errorf("approval kind = %q, want %q", kind, taskstate.ApprovalBudget)
}
for _, want := range []string{"halfway through the migration", "internal/auth/token.go"} {
if !strings.Contains(tk.Notes, want) {
t.Errorf("notes %q lack %q for the continuation", tk.Notes, want)
}
}
tc = newContext()
Run(context.Background(), wf, tc)
if tc.Outcome.Status != StatusCompleted {
t.Errorf("continued outcome = %q, want %q", tc.Outcome.Status, StatusCompleted)
}
})
}
}