package workflow import ( "context" "fmt" "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, after the // conversation entries it reports, and keeps the requests it was given. type sequenceRunner struct { results []agentrun.Result requests []agentrun.Request } func (s *sequenceRunner) Run(_ context.Context, _ string, req agentrun.Request, report agentrun.Reporter) (agentrun.Result, error) { s.requests = append(s.requests, req) report.ReportTranscript(agentrun.TranscriptEntry{Kind: agentrun.TranscriptUser, Text: "the mission"}) report.ReportTranscript(agentrun.TranscriptEntry{Kind: agentrun.TranscriptTool, Tool: "read", Output: fmt.Sprintf("run %d output", len(s.requests))}) 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"}}, } } setups := 0 wf := Workflow{Name: "implement", Stages: []Stage{ {Name: "setup", Run: func(context.Context, *TaskContext) error { setups++; return nil }}, 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 tk.ResumeFrom != "build" { t.Errorf("resume point = %q, want the stopped step", tk.ResumeFrom) } 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) } if setups != 1 { t.Errorf("setup ran %d times, want once: the continuation skips completed steps", setups) } if tk.ResumeFrom != "" || len(tk.ResumeHistory) != 0 { t.Errorf("resume point %q and history survive the continuation", tk.ResumeFrom) } if len(runner.requests) != 2 { t.Fatalf("agent ran %d times, want 2", len(runner.requests)) } if first := runner.requests[0].History; len(first) != 0 { t.Errorf("first run got history %v", first) } history := runner.requests[1].History if len(history) != 1 || history[0].Output != "run 1 output" { t.Errorf("continued run history = %+v, want the stopped run's tool output without its mission", history) } }) } }