feat(workflow): repository step types; implement and tdd ship as YAML in general steps

This commit is contained in:
sam committed 2026-10-05 00:04:34 +11:00
1 parent 880a898ecb
commit 4efb835eed
9 files changed
+407 -266

No files matched your search

+43 -14
View File
@@ -29,9 +29,14 @@ type agentRunSettings struct {
Role string `yaml:"role"`
// ReadOnly restricts the agent to read-only tools.
ReadOnly bool `yaml:"read_only"`
// Gate is "repository" to hold the agent to the repository's gate
// commands before it may finish; empty means ungated.
// Gate holds the agent to the repository's gate before it may finish:
// "repository", or "test-failure" where the test command must fail
// (writing a failing reproduction). Empty means ungated.
Gate string `yaml:"gate"`
// ProtectTests write-blocks the repository's test files.
ProtectTests bool `yaml:"protect_tests"`
// ExtraRules is appended to the agent's system prompt.
ExtraRules string `yaml:"extra_rules"`
// Result is a JSON Schema object. When set, the agent must return a value
// matching it, which later steps read as steps.<id>.result.
Result map[string]any `yaml:"result"`
@@ -50,11 +55,8 @@ func newAgentRunStage(settings yaml.Node) (Stage, error) {
if err := settings.Decode(&s); err != nil {
return Stage{}, fmt.Errorf("%s: %w", AgentRunStepName, err)
}
if strings.TrimSpace(s.Mission) == "" {
return Stage{}, fmt.Errorf("%s: settings.mission is required", AgentRunStepName)
}
if s.Gate != "" && s.Gate != "repository" {
return Stage{}, fmt.Errorf("%s: settings.gate is repository or empty, not %q", AgentRunStepName, s.Gate)
if err := s.check(); err != nil {
return Stage{}, fmt.Errorf("%s: %w", AgentRunStepName, err)
}
resultTool, err := agentResultTool(s.Result)
if err != nil {
@@ -65,10 +67,11 @@ func newAgentRunStage(settings yaml.Node) (Stage, error) {
role = "builder"
}
agent := AgentStage{
Name: AgentRunStepName,
Role: role,
ReadOnly: s.ReadOnly,
Mission: func(*TaskContext) string { return s.Mission },
Name: AgentRunStepName,
Role: role,
ReadOnly: s.ReadOnly,
Mission: func(*TaskContext) string { return s.Mission },
ExtraRules: s.ExtraRules,
OnResult: func(tc *TaskContext, res agentrun.Result) error {
tc.stepResult.Summary = res.Summary
if resultTool == nil {
@@ -81,9 +84,7 @@ func newAgentRunStage(settings yaml.Node) (Stage, error) {
return json.Unmarshal(calls[0], &tc.stepResult.Result)
},
}
if s.Gate == "repository" {
agent.Gate = func(tc *TaskContext) agentrun.Gate { return GateFromRepo(tc.Repo, tc.Cfg.Budgets) }
}
applyRepositoryControls(&agent, s)
if resultTool != nil {
agent.CaptureTools = func(*TaskContext) []agentrun.CaptureTool { return []agentrun.CaptureTool{*resultTool} }
}
@@ -100,6 +101,34 @@ func newAgentRunStage(settings yaml.Node) (Stage, error) {
}}, nil
}
func (s agentRunSettings) check() error {
if strings.TrimSpace(s.Mission) == "" {
return fmt.Errorf("settings.mission is required")
}
if s.Gate != "" && s.Gate != "repository" && s.Gate != gateExpectTestFailure {
return fmt.Errorf("settings.gate is repository, test-failure or empty, not %q", s.Gate)
}
return nil
}
// applyRepositoryControls sets the agent's gate and write protection.
func applyRepositoryControls(agent *AgentStage, s agentRunSettings) {
switch s.Gate {
case "repository":
agent.Gate = func(tc *TaskContext) agentrun.Gate { return GateFromRepo(tc.Repo, tc.Cfg.Budgets) }
case gateExpectTestFailure:
agent.Gate = func(tc *TaskContext) agentrun.Gate { return tddReproGate(tc.Repo, tc.Cfg.Budgets) }
}
if s.ProtectTests {
agent.ProtectGlobs = func(tc *TaskContext) []string {
if glob := tc.Repo.ResolvedTestGlob(); glob != "" {
return []string{glob}
}
return nil
}
}
}
// agentResultTool is the capture tool for a result schema, or nil for none.
func agentResultTool(schema map[string]any) (*agentrun.CaptureTool, error) {
if schema == nil {
+24 -2
View File
@@ -4,9 +4,13 @@ import (
"bytes"
"context"
"crypto/sha256"
"embed"
"encoding/hex"
"encoding/json"
"fmt"
"io/fs"
"path"
"slices"
"sort"
"strings"
@@ -262,7 +266,7 @@ func ShippedDefinitions() WorkflowDefinitionCollection {
ids = append(ids, id)
}
sort.Strings(ids)
collection := WorkflowDefinitionCollection{Definitions: make([]WorkflowDefinitionEntry, 0, len(ids))}
collection := WorkflowDefinitionCollection{Definitions: shippedYAML()}
for _, id := range ids {
wf := workflows[id]
var builder strings.Builder
@@ -281,9 +285,28 @@ func ShippedDefinitions() WorkflowDefinitionCollection {
}
collection.Definitions = append(collection.Definitions, WorkflowDefinitionEntry{ID: id, YAML: builder.String()})
}
slices.SortFunc(collection.Definitions, func(a, b WorkflowDefinitionEntry) int { return strings.Compare(a.ID, b.ID) })
return collection
}
//go:embed shipped/*.yaml
var shippedFiles embed.FS
// shippedYAML is the shipped workflows written in the general step types.
func shippedYAML() []WorkflowDefinitionEntry {
files, _ := fs.Glob(shippedFiles, "shipped/*.yaml")
entries := make([]WorkflowDefinitionEntry, 0, len(files))
for _, name := range files {
data, err := shippedFiles.ReadFile(name)
if err != nil {
panic(err) // embedded at build time; unreadable means a broken binary
}
id := strings.TrimSuffix(path.Base(name), ".yaml")
entries = append(entries, WorkflowDefinitionEntry{ID: id, YAML: string(data)})
}
return entries
}
// sortedInputNames returns a Workflow.Interface's declared input names in a
// deterministic order, so the generated YAML (and its digest) never depends
// on map iteration order.
@@ -298,7 +321,6 @@ func sortedInputNames(inputs map[string]task.InputSpec) []string {
func legacyBuiltinWorkflows() Registry {
return Registry{
"implement": Implement(), "tdd": TDD(),
"feasibility": Feasibility(), "triage": Triage(), "remediate": Remediate(),
"pr-review": PRReview(),
}
-80
View File
@@ -123,83 +123,3 @@ func runBaselineFix(ctx context.Context, tc *TaskContext, argv []string, out []b
}
return nil
}
// Implement is the default workflow: plan read-only, post the plan to
// the issue for visibility, build behind the repo's gate, open the PR
// with the builder's own summary.
func Implement() Workflow {
return Workflow{
Name: "implement",
Stages: []Stage{
StagePrepareWorktree(),
StageBaselineGate(),
AgentStage{
Name: "plan",
Role: "planner",
ReadOnly: true,
Mission: func(tc *TaskContext) string {
prd := ""
if tc.Task.Plan != "" {
prd = "\n<approved_prd>\n" + tc.Task.Plan + "\n</approved_prd>\n"
}
return fmt.Sprintf(
"Produce a concrete implementation plan for this %s on the repository %s.\n"+prd+"\n"+
"%s\n\n"+
"Explore the codebase with your read-only tools, then call finish with status "+
"\"passed\" and the plan as the summary: files to touch, the approach, acceptance "+
"criteria, and what tests should prove it. Keep the plan tightly scoped to the task -- "+
"call finish with status \"blocked\" if the task is too vague or too large for one PR.",
taskKind(tc.Task), tc.Repo.FullName(), taskPromptBlock(tc.Task),
)
},
OnResult: func(tc *TaskContext, res agentrun.Result) error {
tc.Task.Plan = res.Summary
return nil
},
}.Stage(),
AgentStage{
Name: "build",
Role: "builder",
Gate: func(tc *TaskContext) agentrun.Gate {
return GateFromRepo(tc.Repo, tc.Cfg.Budgets)
},
ExtraRules: "Files matching the repository's protected suffixes (e.g. generated code) are " +
"write-blocked -- edit their sources instead; the gate regenerates them.",
Mission: func(tc *TaskContext) string {
return fmt.Sprintf(
"Implement this %s on the repository %s, following the plan below.\n\n"+
"%s\n\n<plan>\n%s\n</plan>\n\n"+
"Make the smallest change that satisfies the task and the plan's acceptance criteria. "+
"Do not run git -- the orchestrator commits and pushes for you. When done, call finish "+
"with status \"passed\" and a summary written for the human who will review the pull "+
"request: what changed, why, and how it was verified.",
taskKind(tc.Task), tc.Repo.FullName(), taskPromptBlock(tc.Task), tc.Task.Plan,
)
},
OnResult: func(tc *TaskContext, res agentrun.Result) error {
tc.BuildSummary = res.Summary
if res.Status == agentrun.StatusPassed && len(res.Changes) == 0 && !tc.BaselineFixed {
tc.BuildNoChanges = true
}
return nil
},
}.Stage(),
StageCommitPush(func(tc *TaskContext) string {
return fmt.Sprintf("%s (archie)", tc.Task.Title) +
commitIssueReference("Implements", tc.Task)
}),
StageDiffCap(),
StagePRReviewAndOpenPR(implementPRBody),
},
}
}
// implementPRBody is the implement workflow's PR footer: token usage split
// into fresh and cached.
func implementPRBody(tc *TaskContext) string {
return fmt.Sprintf("%s\n\n---\n*workflow: implement · %d iterations · %s*",
tc.BuildSummary, tc.Task.Iterations, formatTokenUsage(tc.Task.TokensUsed, tc.RunUsage))
}
+212
View File
@@ -0,0 +1,212 @@
package workflow
import (
"context"
"fmt"
"os/exec"
"strings"
"gopkg.in/yaml.v3"
"github.com/samcharles93/archie-core/internal/domain/agentrun"
)
// The general repository step types. Each needs a repository.
const (
RepoPrepareStepName = "repo.prepare"
RepoCommitStepName = "repo.commit"
RepoOpenPRStepName = "repo.open-pr"
GateRepositoryName = "gate.repository"
GateDiffSizeStepName = "gate.diff-size"
repoBranchTask = "task"
gateExpectTestFailure = "test-failure"
)
// RepoStepTypes contributes the general repository step types.
func RepoStepTypes() []StepType {
return []StepType{
{Name: RepoPrepareStepName, Factory: newRepoPrepareStage},
{Name: RepoCommitStepName, Factory: newRepoCommitStage},
{Name: RepoOpenPRStepName, Factory: newRepoOpenPRStage},
{Name: GateRepositoryName, Factory: newGateRepositoryStage},
{Name: GateDiffSizeStepName, Factory: func(yaml.Node) (Stage, error) {
stage := StageDiffCap()
stage.Name = GateDiffSizeStepName
return stage, nil
}},
}
}
// decodeSettings decodes optional settings strictly.
func decodeSettings(step string, settings yaml.Node, into any) error {
if settings.Kind == 0 {
return nil
}
if err := settings.Decode(into); err != nil {
return fmt.Errorf("%s: %w", step, err)
}
return nil
}
type repoPrepareSettings struct {
// Branch is "fresh" (the default, a new branch from the base) or "task",
// the branch an earlier run of this task pushed.
Branch string `yaml:"branch"`
}
func newRepoPrepareStage(settings yaml.Node) (Stage, error) {
var s repoPrepareSettings
if err := decodeSettings(RepoPrepareStepName, settings, &s); err != nil {
return Stage{}, err
}
var stage Stage
switch s.Branch {
case "", "fresh":
stage = StagePrepareWorktree()
case repoBranchTask:
stage = StagePrepareWorktreeOnBranch()
default:
return Stage{}, fmt.Errorf("%s: settings.branch is fresh or task, not %q", RepoPrepareStepName, s.Branch)
}
stage.Name = RepoPrepareStepName
return stage, nil
}
type repoCommitSettings struct {
Message string `yaml:"message"`
// Reference is the verb that links the commit to the task's issue
// ("Fixes", "Implements", "Refs"); empty links nothing.
Reference string `yaml:"reference"`
Push bool `yaml:"push"`
// IfEmpty is what an empty worktree does: "fail" (the default), "skip",
// or "complete", which closes the issue and completes the workflow.
IfEmpty string `yaml:"if_empty"`
}
func newRepoCommitStage(settings yaml.Node) (Stage, error) {
var s repoCommitSettings
if err := decodeSettings(RepoCommitStepName, settings, &s); err != nil {
return Stage{}, err
}
if strings.TrimSpace(s.Message) == "" {
return Stage{}, fmt.Errorf("%s: settings.message is required", RepoCommitStepName)
}
switch s.IfEmpty {
case "", "fail", "skip", "complete":
default:
return Stage{}, fmt.Errorf("%s: settings.if_empty is fail, skip or complete, not %q", RepoCommitStepName, s.IfEmpty)
}
return Stage{Name: RepoCommitStepName, Run: func(ctx context.Context, tc *TaskContext) error {
message := s.Message
if s.Reference != "" {
message += commitIssueReference(s.Reference, tc.Task)
}
changed, err := tc.Trees.CommitAll(ctx, tc.Dir, message)
if err != nil {
return err
}
if !changed && !tc.BaselineFixed {
switch s.IfEmpty {
case "skip":
return nil
case "complete":
return closeNoChangesIssue(ctx, tc)
default:
return fmt.Errorf("worktree has no changes to commit")
}
}
if s.Push {
if err := tc.Trees.Push(ctx, tc.Dir, tc.Branch); err != nil {
return err
}
tc.captureChanges(ctx, capturedAfterCommitPush)
return nil
}
tc.captureChanges(ctx, capturedAfterCommit)
return nil
}}, nil
}
type repoOpenPRSettings struct {
Body string `yaml:"body"`
// Review runs the PR review pipeline on the change first; a blocking
// finding parks the task instead of opening the PR.
Review bool `yaml:"review"`
}
func newRepoOpenPRStage(settings yaml.Node) (Stage, error) {
var s repoOpenPRSettings
if err := decodeSettings(RepoOpenPRStepName, settings, &s); err != nil {
return Stage{}, err
}
body := func(tc *TaskContext) string {
return fmt.Sprintf("%s\n\n---\n*workflow: %s · %d iterations · %s*",
s.Body, tc.Task.Workflow, tc.Task.Iterations, formatTokenUsage(tc.Task.TokensUsed, tc.RunUsage))
}
stage := StageOpenPR(body)
if s.Review {
stage = StagePRReviewAndOpenPR(body)
}
stage.Name = RepoOpenPRStepName
return stage, nil
}
type gateRepositorySettings struct {
// Expect is "pass" (the default) or "test-failure": every gate command
// passes except the test command, which must fail.
Expect string `yaml:"expect"`
// Repair lets the builder fix a failing gate before the step fails.
Repair bool `yaml:"repair"`
}
// newGateRepositoryStage runs the repository's gate commands in the worktree.
// Its summary is the test command's output, so a later step can quote it.
func newGateRepositoryStage(settings yaml.Node) (Stage, error) {
var s gateRepositorySettings
if err := decodeSettings(GateRepositoryName, settings, &s); err != nil {
return Stage{}, err
}
switch {
case s.Expect != "" && s.Expect != "pass" && s.Expect != gateExpectTestFailure:
return Stage{}, fmt.Errorf("%s: settings.expect is pass or test-failure, not %q", GateRepositoryName, s.Expect)
case s.Repair && s.Expect == gateExpectTestFailure:
return Stage{}, fmt.Errorf("%s: repair applies only when the gate is expected to pass", GateRepositoryName)
}
if s.Repair {
stage := StageBaselineGate()
stage.Name = GateRepositoryName
return stage, nil
}
expectFailure := s.Expect == gateExpectTestFailure
return Stage{Name: GateRepositoryName, Run: func(ctx context.Context, tc *TaskContext) error {
gate := GateFromRepo(tc.Repo, tc.Cfg.Budgets)
if expectFailure {
gate = tddReproGate(tc.Repo, tc.Cfg.Budgets)
}
if len(gate.Commands) == 0 {
return fmt.Errorf("repository %s has no gate commands", tc.Repo.FullName())
}
for _, command := range gate.Commands {
out, err := runGateCommand(ctx, tc.Dir, command)
tc.stepResult.Summary = clip(out, 4000)
if err != nil {
return err
}
}
return nil
}}, nil
}
func runGateCommand(ctx context.Context, dir string, command agentrun.Command) (string, error) {
cmd := exec.CommandContext(ctx, command.Argv[0], command.Argv[1:]...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
name := strings.Join(command.Argv, " ")
switch {
case command.ExpectFailure && err == nil:
return string(out), fmt.Errorf("%s passed, but it was expected to fail", name)
case !command.ExpectFailure && err != nil:
return string(out), fmt.Errorf("%s failed: %w", name, err)
}
return string(out), nil
}
@@ -0,0 +1,45 @@
id: implement
steps:
- type: repo.prepare
- type: gate.repository
settings:
repair: true
- id: plan
type: agent.run
settings:
role: planner
read_only: true
mission: |
Produce a concrete implementation plan for this {{ task.kind }} on the repository {{ task.repository }}.
{{ task.plan }}
{{ task.prompt }}
Explore the codebase with your read-only tools, then call finish with status "passed" and the plan as the summary: files to touch, the approach, acceptance criteria, and what tests should prove it. Keep the plan tightly scoped to the task. Call finish with status "blocked" if the task is too vague or too large for one PR.
- id: build
type: agent.run
settings:
gate: repository
extra_rules: Files matching the repository's protected suffixes (e.g. generated code) are write-blocked. Edit their sources instead; the gate regenerates them.
mission: |
Implement this {{ task.kind }} on the repository {{ task.repository }}, following the plan below.
{{ task.prompt }}
<plan>
{{ steps.plan.summary }}
</plan>
Make the smallest change that satisfies the task and the plan's acceptance criteria. Do not run git: the orchestrator commits and pushes for you. When done, call finish with status "passed" and a summary written for the human who will review the pull request: what changed, why, and how it was verified.
- type: repo.commit
settings:
message: "{{ task.title }} (archie)"
reference: Implements
push: true
if_empty: complete
- type: gate.diff-size
- type: repo.open-pr
settings:
review: true
body: "{{ steps.build.summary }}"
+64
View File
@@ -0,0 +1,64 @@
id: tdd
steps:
- type: repo.prepare
- type: gate.repository
settings:
repair: true
- id: analyse
type: agent.run
settings:
role: planner
read_only: true
mission: |
Analyse this {{ task.kind }} for the repository {{ task.repository }} and determine the problem surface.
{{ task.prompt }}
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.
- id: repro
type: agent.run
settings:
gate: test-failure
mission: |
Write tests that REPRODUCE this bug in the repository {{ task.repository }}. Do NOT fix the bug yet.
{{ task.prompt }}
<analysis>
{{ steps.analyse.summary }}
</analysis>
Add tests that pass once the bug is fixed but FAIL today because of it. The gate is inverted for this step: every gate command must pass except the test command, which must 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.
- id: proof
type: gate.repository
settings:
expect: test-failure
- type: repo.commit
settings:
message: "test: failing repro"
reference: Refs
- id: fix
type: agent.run
settings:
gate: repository
protect_tests: true
extra_rules: The repro tests written earlier are the bug's specification. Test files are write-protected in this step; make them pass by fixing the code they exercise.
mission: |
Fix the bug in the repository {{ task.repository }}. Failing repro tests are already committed; make them pass.
{{ task.prompt }}
<analysis>
{{ steps.analyse.summary }}
</analysis>
The full quality gate 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.
- type: repo.commit
settings:
message: "fix: {{ task.title }} (archie)"
reference: Fixes
push: true
- type: gate.diff-size
- type: repo.open-pr
settings:
body: "{{ steps.fix.summary }}"
+10 -2
View File
@@ -63,7 +63,7 @@ func checkReference(path string, earlier map[string]bool) error {
return fmt.Errorf("reference {{ %s }} must start with task, inputs or steps", path)
}
var taskFields = map[string]bool{"title": true, "body": true, "prompt": true, "repository": true, "kind": true, "issue": true, "pr": true}
var taskFields = map[string]bool{"plan": true, "title": true, "body": true, "prompt": true, "repository": true, "kind": true, "issue": true, "pr": true}
// renderSettings returns a copy of settings with every reference replaced by
// its value in this run. A reference to an absent value renders empty: a
@@ -90,7 +90,7 @@ func referenceScope(tc *TaskContext) map[string]any {
maps.Copy(inputs, t.Inputs)
return map[string]any{
"task": map[string]any{
"title": t.Title, "body": t.Body, "prompt": taskPromptBlock(t),
"plan": approvedPlan(t), "title": t.Title, "body": t.Body, "prompt": taskPromptBlock(t),
"repository": tc.Repo.FullName(), "kind": taskKind(t),
"issue": t.IssueNumber, "pr": t.PRNumber,
},
@@ -158,3 +158,11 @@ func checkStepID(id string, seen map[string]bool) error {
}
return nil
}
// approvedPlan is the PRD a feasibility run handed over, framed for a mission.
func approvedPlan(t *Task) string {
if t.Plan == "" {
return ""
}
return "<approved_prd>\n" + t.Plan + "\n</approved_prd>"
}
-167
View File
@@ -1,156 +1,10 @@
package workflow
import (
"context"
"fmt"
"os/exec"
"slices"
"strings"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/agentrun"
)
// TDD is the bugfix workflow: the repro stage's gate expects the test command
// to fail, and the fix stage restores the full gate. Routed via the "bug"
// label.
func TDD() Workflow {
return Workflow{
Name: "tdd",
Stages: []Stage{
StagePrepareWorktree(),
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)
}),
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 agentrun.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) agentrun.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<analysis>\n%s\n</analysis>\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) agentrun.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<analysis>\n%s\n</analysis>\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 agentrun.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
@@ -175,24 +29,3 @@ func tddReproGate(repo config.Repo, budgets config.Budgets) agentrun.Gate {
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
}
@@ -56,9 +56,17 @@ func (commandSteps) StepTypes() []workflow.StepType {
return []workflow.StepType{workflow.CommandRunStepType()}
}
// repositorySteps provides the general repository step types: prepare,
// commit, open a PR, and the repository and diff-size gates.
type repositorySteps struct{}
func (repositorySteps) Name() string { return "repository" }
func (repositorySteps) StepTypes() []workflow.StepType { return workflow.RepoStepTypes() }
// Providers returns the step-type providers to register.
func Providers() []workflow.StepTypeProvider {
return []workflow.StepTypeProvider{shippedStages{}, repoHooks{}, eventSteps{}, commandSteps{}}
return []workflow.StepTypeProvider{shippedStages{}, repoHooks{}, eventSteps{}, commandSteps{}, repositorySteps{}}
}
// NewManager returns a new manager with Providers registered.