From 5465442cb302576cbe5a459607e8d5a652783647 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 21 Jul 2026 04:07:21 +1000 Subject: [PATCH] =?UTF-8?q?feat:=20TDD=20workflow=20for=20bugfixes=20?= =?UTF-8?q?=E2=80=94=20failing=20repro=20before=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Routed via the bug label. Stages: analyse (read-only problem-surface analysis) -> repro-tests behind the INVERTED gate (go vet must pass, go test ./... must FAIL — GateCommand.ExpectFailure proves the repro captures the bug) -> repro committed separately -> fix behind the repo's full gate with the repro declared immutable in the rules -> push, diff cap, PR. Two-commit PRs tell the story: test first, fix second. Zero engine or step-library changes — the workflow is pure composition, per the reusability requirement. Proven live on samcharles93/todo#5 (empty todos bug): repro gate pushed back once, fix stage blocked twice by the full gate before green; PR #6 (~103k tokens total). --- internal/workflow/steps.go | 21 ++++++-- internal/workflow/tdd.go | 108 +++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 internal/workflow/tdd.go diff --git a/internal/workflow/steps.go b/internal/workflow/steps.go index 5ff54a706..47bd9fedc 100644 --- a/internal/workflow/steps.go +++ b/internal/workflow/steps.go @@ -27,10 +27,11 @@ func StagePrepareWorktree() Stage { }} } -// StageCommitPush commits everything in the worktree and pushes the -// branch; an empty tree parks (the workflow produced nothing). -func StageCommitPush(message func(*TaskContext) string) Stage { - return Stage{Name: "commit-push", Run: func(ctx context.Context, tc *TaskContext) error { +// StageCommit commits everything in the worktree without pushing — +// used mid-workflow so a PR tells its story in multiple commits (TDD: +// failing tests first, fix second). An empty tree parks. +func StageCommit(name string, message func(*TaskContext) string) Stage { + return Stage{Name: name, Run: func(ctx context.Context, tc *TaskContext) error { changed, err := tc.Trees.CommitAll(ctx, tc.Dir, message(tc)) if err != nil { return err @@ -38,6 +39,18 @@ func StageCommitPush(message func(*TaskContext) string) Stage { if !changed { return fmt.Errorf("worktree has no changes to commit") } + return nil + }} +} + +// StageCommitPush commits everything in the worktree and pushes the +// branch; an empty tree parks (the workflow produced nothing). +func StageCommitPush(message func(*TaskContext) string) Stage { + commit := StageCommit("commit-push", message) + return Stage{Name: "commit-push", Run: func(ctx context.Context, tc *TaskContext) error { + if err := commit.Run(ctx, tc); err != nil { + return err + } return tc.Trees.Push(ctx, tc.Dir, tc.Branch) }} } diff --git a/internal/workflow/tdd.go b/internal/workflow/tdd.go new file mode 100644 index 000000000..cee302ab9 --- /dev/null +++ b/internal/workflow/tdd.go @@ -0,0 +1,108 @@ +package workflow + +import ( + "fmt" + + "github.com/samcharles93/ai-sdk/agentloop" +) + +// 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(), + StageBaselineGate(), + + AgentStage{ + Name: "analyse", + Role: "planner", + ReadOnly: true, + MaxSteps: 15, + Mission: func(tc *TaskContext) string { + return fmt.Sprintf( + "Analyse this bug report for the repository %s and determine the problem surface.\n\n"+ + "\n# %s\n\n%s\n\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.", + tc.Repo.FullName(), tc.Task.IssueNumber, tc.Task.Title, tc.Task.Body, + ) + }, + OnResult: func(tc *TaskContext, res agentloop.Result) error { + tc.Task.Plan = res.Summary + return nil + }, + }.Stage(), + + AgentStage{ + Name: "repro-tests", + Role: "builder", + // The inverted gate: code must still vet, but the test + // suite MUST fail — proof the repro captures the bug. + Gate: func(tc *TaskContext) agentloop.GateConfig { + return agentloop.GateConfig{ + Commands: []agentloop.GateCommand{ + {Name: "vet", Argv: []string{"go", "vet", "./..."}}, + {Name: "repro-must-fail", Argv: []string{"go", "test", "./...", "-count=1"}, ExpectFailure: true}, + }, + MaxConsecutiveFailures: tc.Cfg.Budgets.GateMaxFailures, + } + }, + Mission: func(tc *TaskContext) string { + return fmt.Sprintf( + "Write tests that REPRODUCE this bug in the repository %s. Do NOT fix the bug yet.\n\n"+ + "\n# %s\n\n%s\n\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 'go vet' to pass and 'go test ./...' 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(), tc.Task.IssueNumber, tc.Task.Title, tc.Task.Body, tc.Task.Plan, + ) + }, + }.Stage(), + + StageCommit("commit-repro", func(tc *TaskContext) string { + return fmt.Sprintf("test: failing repro for #%d", tc.Task.IssueNumber) + }), + + AgentStage{ + Name: "fix", + Role: "builder", + Gate: func(tc *TaskContext) agentloop.GateConfig { + return GateFromRepo(tc.Repo, tc.Cfg.Budgets) + }, + ExtraRules: "The repro tests written in the previous stage are the bug's specification. " + + "Never weaken, skip, or delete them — 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"+ + "\n# %s\n\n%s\n\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(), tc.Task.IssueNumber, tc.Task.Title, tc.Task.Body, tc.Task.Plan, + ) + }, + OnResult: func(tc *TaskContext, res agentloop.Result) error { + tc.BuildSummary = res.Summary + return nil + }, + }.Stage(), + + StageCommitPush(func(tc *TaskContext) string { + return fmt.Sprintf("fix: %s (archie)\n\nFixes #%d", tc.Task.Title, tc.Task.IssueNumber) + }), + StageDiffCap(), + StageOpenPR(func(tc *TaskContext) string { + return fmt.Sprintf("%s\n\n---\n*workflow: tdd (failing repro committed first) · %d iterations · %d tokens*\n\nCloses #%d", + tc.BuildSummary, tc.Task.Iterations, tc.Task.TokensUsed, tc.Task.IssueNumber) + }), + }, + } +}