package workflow import ( "context" "fmt" "os/exec" "slices" "strings" "github.com/samcharles93/archie-core/internal/agentexec" "github.com/samcharles93/archie-core/internal/config" ) // TDD is the bugfix workflow: prove the bug with failing tests before // fixing it. The repro stage's gate INVERTS the test command // (ExpectFailure) -- the run cannot proceed until the new tests fail for // the right reason -- and the fix stage restores the repo's full gate. // Routed via the "bug" label. func TDD() Workflow { return Workflow{ Name: "tdd", Stages: []Stage{ StagePrepareWorktree(), StageRepoStages(), StageBaselineGate(), tddAnalyseStage(), tddReproTestsStage(), tddCaptureProofStage(), StageCommit("commit-repro", func(tc *TaskContext) string { return "test: failing repro" + commitIssueReference("Refs", tc.Task) }), tddFixStage(), StageCommitPush(func(tc *TaskContext) string { return fmt.Sprintf("fix: %s (archie)", tc.Task.Title) + commitIssueReference("Fixes", tc.Task) }), StageYaegiGate(), StageDiffCap(), tddOpenPRStage(), }, } } // tddAnalyseStage explores the bug read-only and records the root cause and // reproduction plan on tc.Task.Plan. func tddAnalyseStage() Stage { return AgentStage{ Name: "analyse", Role: "planner", ReadOnly: true, Mission: func(tc *TaskContext) string { return fmt.Sprintf( "Analyse this %s for the repository %s and determine the problem surface.\n\n"+ "%s\n\n"+ "Explore the code read-only and call finish with status \"passed\" and a summary "+ "containing: the root cause (file and function), the exact conditions that trigger it, "+ "the expected vs actual behaviour, and which test cases would prove the bug. "+ "Call finish with status \"blocked\" if you cannot locate a plausible cause.", taskKind(tc.Task), tc.Repo.FullName(), taskPromptBlock(tc.Task), ) }, OnResult: func(tc *TaskContext, res agentexec.Result) error { tc.Task.Plan = res.Summary return nil }, }.Stage() } // tddReproTestsStage writes tests that reproduce the bug, under the // inverted gate: code-quality commands must still pass, but the test // command (last in repo.Gate) MUST fail -- proof the repro captures the bug. func tddReproTestsStage() Stage { return AgentStage{ Name: "repro-tests", Role: "builder", Gate: func(tc *TaskContext) agentexec.Gate { return tddReproGate(tc.Repo, tc.Cfg.Budgets) }, Mission: func(tc *TaskContext) string { cmd := testCommand(tc.Repo) return fmt.Sprintf( "Write tests that REPRODUCE this bug in the repository %s. Do NOT fix the bug yet.\n\n"+ "%s\n\n\n%s\n\n\n"+ "Add tests that pass once the bug is fixed but FAIL today because of it. The gate is "+ "inverted for this stage: it requires the full gate to pass EXCEPT %s which is required "+ "to FAIL. Do not touch non-test files. Call finish with status \"passed\" once the failing "+ "repro is in place, summarising which tests capture the bug.", tc.Repo.FullName(), taskPromptBlock(tc.Task), tc.Task.Plan, cmd, ) }, }.Stage() } // tddCaptureProofStage captures the failing test run -- the proof the repro // reproduces the bug, posted on the PR later. func tddCaptureProofStage() Stage { return Stage{Name: "capture-proof", Run: func(ctx context.Context, tc *TaskContext) error { argv := testCommandArgv(tc.Repo) if len(argv) == 0 { return fmt.Errorf("tdd: repo %s has no gate configured -- cannot capture repro proof (add at least one [[repos.gate]] command, with the test runner last)", tc.Repo.FullName()) } cmd := exec.CommandContext(ctx, argv[0], argv[1:]...) cmd.Dir = tc.Dir out, err := cmd.CombinedOutput() if err == nil { return fmt.Errorf("repro tests unexpectedly pass after commit -- the bug is not captured") } tc.ReproProof = clip(string(out), 4000) return nil }} } // tddFixStage fixes the bug. Test files are write-protected: the committed // repro is the spec, and the fix stage cannot touch it. func tddFixStage() Stage { return AgentStage{ Name: "fix", Role: "builder", Gate: func(tc *TaskContext) agentexec.Gate { return GateFromRepo(tc.Repo, tc.Cfg.Budgets) }, ProtectGlobs: func(tc *TaskContext) []string { glob := tc.Repo.ResolvedTestGlob() if glob == "" { return nil } return []string{glob} }, ExtraRules: "The repro tests written in the previous stage are the bug's specification. " + "Test files are write-protected in this stage -- make them pass by fixing the code they exercise.", Mission: func(tc *TaskContext) string { return fmt.Sprintf( "Fix the bug in the repository %s. Failing repro tests are already committed; make them pass.\n\n"+ "%s\n\n\n%s\n\n\n"+ "The full quality gate (including 'go test ./...') must pass. Make the smallest fix "+ "that makes the repro tests pass without changing them. Call finish with status "+ "\"passed\" and a summary for the PR reviewer: root cause, the fix, and verification.", tc.Repo.FullName(), taskPromptBlock(tc.Task), tc.Task.Plan, ) }, OnResult: func(tc *TaskContext, res agentexec.Result) error { tc.BuildSummary = res.Summary return nil }, }.Stage() } // tddOpenPRStage opens the PR, then posts the captured failing-test output // as evidence the bug was reproduced before the fix. func tddOpenPRStage() Stage { return Stage{Name: "open-pr", Run: func(ctx context.Context, tc *TaskContext) error { body := fmt.Sprintf("%s\n\n---\n*workflow: tdd (failing repro committed first) · %d iterations · %s*", tc.BuildSummary, tc.Task.Iterations, formatTokenUsage(tc.Task.TokensUsed, tc.RunUsage)) return OpenPR(ctx, tc, body) }} } // tddReproGate builds the inverted gate for the repro-tests stage: // every command from repo.Gate runs normally except the last one (the // test runner, by convention), which gets ExpectFailure -- the repro // must fail the tests to prove the bug exists. func tddReproGate(repo config.Repo, budgets config.Budgets) agentexec.Gate { if len(repo.Gate) == 0 { return agentexec.Gate{} } cmds := make([]agentexec.Command, 0, len(repo.Gate)) for _, argv := range repo.Gate { if len(argv) == 0 { continue } gc := agentexec.Command{Name: argv[0], Argv: argv} cmds = append(cmds, gc) } if len(cmds) > 0 { cmds[len(cmds)-1].ExpectFailure = true } return agentexec.Gate{ Commands: cmds, MaxConsecutiveFailures: budgets.GateMaxFailures, } } // testCommand returns a human-readable representation of the test // command (the last entry in repo.Gate). func testCommand(repo config.Repo) string { last := testCommandArgv(repo) if len(last) == 0 { return "(no gate configured)" } return strings.Join(last, " ") } // testCommandArgv returns the test command argv (the last entry in // repo.Gate), or nil when no gate is configured. func testCommandArgv(repo config.Repo) []string { for _, v := range slices.Backward(repo.Gate) { if len(v) > 0 { return v } } return nil }