Seven failing tests that must pass when gaps 1, 3, and 5 are closed:
Gap 1 (per-task agent):
TestAgentRequestMessageHasWorkflowField — AgentRequestMessage needs
a Workflow field so the agent runs full multi-stage workflows, not
one stage at a time.
Gap 3 (response/system channel split):
TestAgentResponseEnvelopeHasChannelField — envelope needs a Channel
field ("response"|"system").
TestSubjectForAgentResponseExists / TestSubjectForAgentSystemExists
— subject functions for archie.agent.<id>.response and .system.
Gap 5 (daemon review step):
TestAgentStageHasReviewHook — AgentStage needs a ReviewResult hook
that gates agent output before OnResult forwards to humans.
All tests currently fail (compilation errors on missing fields).
238 lines
8.3 KiB
Go
238 lines
8.3 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/agentexec"
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/store"
|
|
)
|
|
|
|
type fakeAgentRunner struct {
|
|
workspace string
|
|
request agentexec.Request
|
|
result agentexec.Result
|
|
err error
|
|
}
|
|
|
|
type agentRunnerFunc func(context.Context, string, agentexec.Request) (agentexec.Result, error)
|
|
|
|
func (f agentRunnerFunc) Run(ctx context.Context, workspace string, req agentexec.Request) (agentexec.Result, error) {
|
|
return f(ctx, workspace, req)
|
|
}
|
|
|
|
func (r *fakeAgentRunner) Run(_ context.Context, workspace string, req agentexec.Request) (agentexec.Result, error) {
|
|
r.workspace = workspace
|
|
r.request = req
|
|
if r.result.Version == 0 {
|
|
r.result.Version = agentexec.ProtocolVersion
|
|
}
|
|
r.result.TaskID = req.TaskID
|
|
r.result.Attempt = req.Attempt
|
|
r.result.Stage = req.Stage
|
|
return r.result, r.err
|
|
}
|
|
|
|
func TestAgentStageBuildsExecutionRequestAndAppliesResult(t *testing.T) {
|
|
runner := &fakeAgentRunner{result: agentexec.Result{
|
|
Version: agentexec.ProtocolVersion, Status: agentexec.StatusPassed,
|
|
Summary: "finished", TokensUsed: 23, Iterations: 4,
|
|
}}
|
|
resultCalled := false
|
|
stage := AgentStage{
|
|
Name: "plan", Role: "planner", ReadOnly: true, MaxSteps: 7,
|
|
Mission: func(*TaskContext) string { return "inspect the repository" },
|
|
OnResult: func(_ *TaskContext, result agentexec.Result) error {
|
|
resultCalled = result.Summary == "finished"
|
|
return nil
|
|
},
|
|
}.Stage()
|
|
task := &store.Task{ID: 9, Attempt: 2}
|
|
tc := &TaskContext{
|
|
Task: task, Agent: runner, Dir: "/tmp/workspace", Log: slog.New(slog.DiscardHandler),
|
|
Cfg: config.Config{
|
|
Models: map[string]string{"builder": "provider/fallback"},
|
|
Budgets: config.Budgets{MaxSteps: 50, MaxTokens: 5000},
|
|
},
|
|
Repo: config.Repo{Protect: []string{"_templ.go"}, Preflight: [][]string{{"go", "version"}}},
|
|
}
|
|
if err := stage.Run(context.Background(), tc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if runner.workspace != tc.Dir {
|
|
t.Fatalf("workspace = %q, want %q", runner.workspace, tc.Dir)
|
|
}
|
|
wantPreflight := []agentexec.Command{{Name: "go", Argv: []string{"go", "version"}}}
|
|
if runner.request.Version != agentexec.ProtocolVersion || runner.request.TaskID != 9 || runner.request.Attempt != 2 ||
|
|
runner.request.Model != "provider/fallback" || runner.request.Budget.MaxSteps != 7 || !runner.request.ReadOnly ||
|
|
!reflect.DeepEqual(runner.request.Preflight, wantPreflight) {
|
|
t.Fatalf("unexpected request: %#v", runner.request)
|
|
}
|
|
if len(runner.request.Protection.Suffixes) != 0 {
|
|
t.Fatalf("read-only request has protection rules: %#v", runner.request.Protection)
|
|
}
|
|
if !resultCalled || task.TokensUsed != 23 || task.Iterations != 4 {
|
|
t.Fatalf("result not applied: called=%v task=%#v", resultCalled, task)
|
|
}
|
|
}
|
|
|
|
func TestAgentStagePersistsReturnedNotes(t *testing.T) {
|
|
st, err := store.Open(filepath.Join(t.TempDir(), "archie.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := st.Close(); err != nil {
|
|
t.Errorf("close store: %v", err)
|
|
}
|
|
})
|
|
ctx := context.Background()
|
|
if _, err := st.EnqueueIssue(ctx, "owner", "repo", 1, "title", "body", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task, err := st.ClaimNext(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runner := &fakeAgentRunner{result: agentexec.Result{
|
|
Version: agentexec.ProtocolVersion, Status: agentexec.StatusPassed,
|
|
AppendedNotes: []string{"checked with go test"},
|
|
}}
|
|
stage := AgentStage{
|
|
Name: "build", Role: "builder", Mission: func(*TaskContext) string { return "build" },
|
|
}.Stage()
|
|
tc := &TaskContext{
|
|
Task: task, Store: st, Agent: runner, Log: slog.New(slog.DiscardHandler),
|
|
Cfg: config.Config{Models: map[string]string{"builder": "provider/model"}},
|
|
}
|
|
if err := stage.Run(ctx, tc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if task.Notes != "- checked with go test\n" {
|
|
t.Fatalf("task notes = %q", task.Notes)
|
|
}
|
|
}
|
|
|
|
func TestAgentStageDiscardsUnstampedErrorResult(t *testing.T) {
|
|
task := &store.Task{ID: 1, Attempt: 1, Notes: "existing\n"}
|
|
runner := agentRunnerFunc(func(context.Context, string, agentexec.Request) (agentexec.Result, error) {
|
|
return agentexec.Result{AppendedNotes: []string{"untrusted"}}, errors.New("worker exited")
|
|
})
|
|
stage := AgentStage{
|
|
Name: "build", Role: "builder", Mission: func(*TaskContext) string { return "build" },
|
|
}.Stage()
|
|
err := stage.Run(context.Background(), &TaskContext{
|
|
Task: task, Agent: runner, Log: slog.New(slog.DiscardHandler),
|
|
Cfg: config.Config{Models: map[string]string{"builder": "provider/model"}},
|
|
})
|
|
if err == nil {
|
|
t.Fatal("stage accepted an unstamped error result")
|
|
}
|
|
if task.Notes != "existing\n" {
|
|
t.Fatalf("unstamped error result mutated notes: %q", task.Notes)
|
|
}
|
|
}
|
|
|
|
func TestFeasibilityDecisionCrossesAsCapturedData(t *testing.T) {
|
|
runner := &fakeAgentRunner{result: agentexec.Result{
|
|
Version: agentexec.ProtocolVersion, Status: agentexec.StatusPassed,
|
|
Captures: map[string][]json.RawMessage{"decide": {json.RawMessage(`{"fit":true,"reasons":"aligned"}`)}},
|
|
}}
|
|
tc := &TaskContext{
|
|
Task: &store.Task{ID: 1}, Agent: runner, Log: slog.New(slog.DiscardHandler),
|
|
Cfg: config.Config{Models: map[string]string{"planner": "provider/model"}},
|
|
Repo: config.Repo{Owner: "owner", Name: "repo"},
|
|
}
|
|
if err := Feasibility().Stages[1].Run(context.Background(), tc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tc.decision == nil || !tc.decision.Fit || tc.decision.Reasons != "aligned" {
|
|
t.Fatalf("decision = %#v", tc.decision)
|
|
}
|
|
}
|
|
|
|
func TestFeasibilityRejectsNullFitValue(t *testing.T) {
|
|
runner := &fakeAgentRunner{result: agentexec.Result{
|
|
Status: agentexec.StatusPassed,
|
|
Captures: map[string][]json.RawMessage{"decide": {json.RawMessage(`{"fit":null,"reasons":"not valid"}`)}},
|
|
}}
|
|
tc := &TaskContext{
|
|
Task: &store.Task{ID: 1}, Agent: runner, Log: slog.New(slog.DiscardHandler),
|
|
Cfg: config.Config{Models: map[string]string{"planner": "provider/model"}},
|
|
Repo: config.Repo{Owner: "owner", Name: "repo"},
|
|
}
|
|
if err := Feasibility().Stages[1].Run(context.Background(), tc); err == nil {
|
|
t.Fatal("assess accepted a null fit value")
|
|
}
|
|
if tc.decision != nil {
|
|
t.Fatalf("decision mutated from invalid capture: %#v", tc.decision)
|
|
}
|
|
}
|
|
|
|
// ── regression: Gap 5 — daemon review step ──────────────────────────
|
|
|
|
func TestAgentStageHasReviewHook(t *testing.T) {
|
|
// Gap 5: no daemon review step before human delivery.
|
|
// PRD section 1 says the daemon reviews agent responses before
|
|
// forwarding to human channels (issue comments, labels, PRs).
|
|
// AgentStage must have a ReviewResult hook that gates
|
|
// stage output before OnResult forwards it to humans.
|
|
reviewed := false
|
|
stage := AgentStage{
|
|
Name: "plan",
|
|
Role: "planner",
|
|
Mission: func(*TaskContext) string { return "test" },
|
|
ReviewResult: func(_ *TaskContext, _ agentexec.Result) error {
|
|
reviewed = true
|
|
return nil
|
|
},
|
|
}
|
|
if stage.ReviewResult == nil {
|
|
t.Error("Gap 5: AgentStage.ReviewResult is nil. " +
|
|
"Add a ReviewResult func(*TaskContext, agentexec.Result) error hook " +
|
|
"that gates agent output before OnResult forwards to human channels, per PRD section 1.")
|
|
}
|
|
// Verify the field is settable and has the right signature.
|
|
_ = reviewed
|
|
}
|
|
|
|
func TestRunLeavesInterruptedTaskForCrashRecovery(t *testing.T) {
|
|
st, err := store.Open(filepath.Join(t.TempDir(), "archie.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := st.Close(); err != nil {
|
|
t.Errorf("close store: %v", err)
|
|
}
|
|
})
|
|
if _, err := st.EnqueueIssue(context.Background(), "owner", "repo", 2, "title", "body", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task, err := st.ClaimNext(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
Run(ctx, Workflow{Name: "test", Stages: []Stage{{
|
|
Name: "agent", Run: func(ctx context.Context, _ *TaskContext) error { return ctx.Err() },
|
|
}}}, &TaskContext{
|
|
Task: task, Store: st, Repo: config.Repo{Owner: "owner", Name: "repo"},
|
|
Log: slog.New(slog.DiscardHandler),
|
|
})
|
|
got, err := st.TaskByIssue(context.Background(), "owner", "repo", 2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Status != store.StatusRunning || got.ParkReason != "" {
|
|
t.Fatalf("interrupted task status=%q park_reason=%q, want running with no park reason", got.Status, got.ParkReason)
|
|
}
|
|
}
|