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" ) // fakeBudgetRunner is an agent runner whose stage exhausted its budget. type fakeBudgetRunner struct{ res agentrun.Result } func (f fakeBudgetRunner) Run(context.Context, string, agentrun.Request, agentrun.Reporter) (agentrun.Result, error) { return f.res, nil } // A stage that exhausts its budget closes the run with its partial findings // as the outcome: the notes it appended reach tc.Outcome -- where a // completed run's result would go -- and not only the task's notes. The // outcome stays parked, so a budget stop is distinguishable from a // gate-passed completion, and the stage's step is not recorded as failed. func TestBudgetStoppedStageDeliversFindingsAsOutcome(t *testing.T) { tests := []struct { name string stopReason string }{ {name: "step budget", stopReason: "budget_exhausted"}, {name: "wall clock", stopReason: "timed_out"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { res := agentrun.Result{ Version: agentrun.ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "build", Status: "parked", StopReason: tt.stopReason, Detail: "stopped after 60/60 steps, 900 tokens", Changes: []string{"internal/auth/token.go"}, AppendedNotes: []string{"auth lives in internal/auth", "migration needs a backfill"}, } store := &fakeStepStore{} tc := &TaskContext{ Task: &task.Task{ID: 1, Attempt: 1}, Store: store, Log: slog.Default(), Agent: fakeBudgetRunner{res: res}, Cfg: config.Config{Models: map[string]string{"default": "test/model"}}, } wf := Workflow{Name: "implement", Stages: []Stage{ AgentStage{Name: "build", Mission: func(*TaskContext) string { return "do it" }}.Stage(), }} Run(context.Background(), wf, tc) if tc.Outcome.Status != StatusParked { t.Fatalf("outcome status = %q, want %q (a budget stop is not a success)", tc.Outcome.Status, StatusParked) } for _, want := range []string{ "stopped after 60/60 steps", "auth lives in internal/auth", "migration needs a backfill", "internal/auth/token.go", } { if !strings.Contains(tc.Outcome.Detail, want) { t.Errorf("outcome detail %q lacks %q", tc.Outcome.Detail, want) } } // The findings reach the outcome, not only the appended notes. for _, want := range res.AppendedNotes { if !strings.Contains(tc.Task.Notes, want) { t.Errorf("task notes %q lack finding %q", tc.Task.Notes, want) } } // The outcome is delivered as the run's terminal transition, // where a completed run's result would land. if len(store.transitions) != 1 { t.Fatalf("transitions = %d, want 1", len(store.transitions)) } got := store.transitions[0] if got.from != StatusRunning || got.to != StatusParked { t.Errorf("transition = %s -> %s, want %s -> %s", got.from, got.to, StatusRunning, StatusParked) } if !strings.Contains(got.detail, "auth lives in internal/auth") { t.Errorf("transition detail %q lacks the findings", got.detail) } // A budget stop is an outcome, not a runner error: no step is // recorded as failed. for _, f := range store.finished { if f.To == taskstate.StepFailed { t.Errorf("step %d recorded failed (%q); a budget stop is an outcome, not a failure", f.StepID, f.Detail) } } }) } } // A stage that stops without passing for a reason other than its budget // still parks through the error path, notes included. func TestDeliverResultNonBudgetStopCarriesFindings(t *testing.T) { res := agentrun.Result{ Status: "parked", StopReason: "gate_failed", Detail: "gate go test: FAIL", AppendedNotes: []string{"auth lives in internal/auth"}, } err := AgentStage{Name: "build"}.deliverResult(&TaskContext{}, res) if err == nil { t.Fatal("want error for a stop that is not a budget stop") } if !strings.Contains(err.Error(), "auth lives in internal/auth") { t.Errorf("error %q lacks the finding", err) } }