Files
archie/internal/workflow/tdd.go
T
sam 170fe0250b fix: close the duplicate-work loop and the dead paths around it
Applies the four-reviewer pass over the forge/dashboard-actions work.
The through-line is that several capabilities were wired but inert, and
one of them could cost real work.

DUPLICATE WORK. "Closes #N" was dropped from PR bodies in favour of
LinkBranch, which is sidebar linkage on Gitea and a no-op stub on
GitHub -- neither closes anything, and nothing else did either. So a
merged task left its issue open, labelled and assigned; the next poll
re-enqueued it, and once the dashboard's Clear removed the task row that
became a second implementation and a second PR for work already merged.
reconcilePRs now closes the issue on merge, and a rejected task closes
its issue too. A PR closed without merging deliberately leaves the issue
open: that usually means "try again".

MAX RETRIES. Configured, defaulted and reported by /api/config, and
enforced nowhere -- markDead was unreachable and EffectiveMaxRetries had
no callers, so the dashboard's retry incremented retry_count forever.
The retry action now retires a task to dead at the cap.

DEAD ON ARRIVAL. Tasks() never selected plan or source, so the
"Decision required" panel could not render and chat tasks -- whose issue
number is synthetic -- linked to forge issues that do not exist.

COSMETIC FAILURE, FATAL RESULT. OpenPR failed the whole stage when
LinkBranch errored, so the pull request was never opened and the task
parked; a retry re-links the same branch, which Gitea answers with a
conflict, so every retry parked again. It is best-effort now.

INVISIBLE ACTIONS. Approve/retry/reject wrote store rows and nothing
else, so the timeline and activity stream never showed that a human had
intervened. They emit events now.

Also: store write failures answer 500 rather than 409; the dashboard
surfaces the server's actual reason instead of "409 Conflict"; action
buttons stop propagation (acting on a task no longer also expands it),
disable in flight, keep per-task errors instead of one shared string,
and confirm before rejecting.

Removed: the maybeRetryParked shim and markDead with their now-vacuous
tests, WaitingTasks (no consumers; its tests were rewritten to assert
the same routing through TaskByID), write-only Daemon.Runtime, and
dir/dirs response bloat from /api/skills. skillSource checked workDir
before sharedDir, mislabelling every shared skill when the two are equal.
DefaultRoots now dedupes roots differing only by a trailing separator.

The air build rule claiming to keep ui/dist current never ran -- it
required one file to satisfy include_dir and include_file together, and
ui/ is excluded from the walk. Deleted with the comment that promised it.
task test:ui joins task check and CI; it ran nowhere before, so the
dashboard suite could rot silently.

Docs corrected where they described behaviour that no longer exists:
parks no longer comment on the issue, state labels no longer drive
retries, and forgerpc's doc claimed four proxied methods while listing
three of five.
2026-08-05 03:54:25 +10:00

190 lines
6.7 KiB
Go

package workflow
import (
"context"
"fmt"
"os/exec"
"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(),
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(),
AgentStage{
Name: "repro-tests",
Role: "builder",
// 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.
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<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(),
// Capture the failing test run -- the proof the repro
// reproduces the bug, posted on the PR later.
{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
}},
StageCommit("commit-repro", func(tc *TaskContext) string {
return "test: failing repro" + commitIssueReference("Refs", tc.Task)
}),
AgentStage{
Name: "fix",
Role: "builder",
Gate: func(tc *TaskContext) agentexec.Gate {
return GateFromRepo(tc.Repo, tc.Cfg.Budgets)
},
// Environmental, not advisory: the fix stage cannot touch
// test files at all -- the committed repro is the spec.
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 agentexec.Result) error {
tc.BuildSummary = res.Summary
return nil
},
}.Stage(),
StageCommitPush(func(tc *TaskContext) string {
return fmt.Sprintf("fix: %s (archie)", tc.Task.Title) +
commitIssueReference("Fixes", tc.Task)
}),
StageYaegiGate(),
StageDiffCap(),
// Open the PR, then post the captured failing-test output as
// evidence the bug was reproduced before the fix.
{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 · %d tokens*",
tc.BuildSummary, tc.Task.Iterations, tc.Task.TokensUsed)
if err := OpenPR(ctx, tc, body); err != nil {
return err
}
return nil
}},
},
}
}
// 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 i := len(repo.Gate) - 1; i >= 0; i-- {
if len(repo.Gate[i]) > 0 {
return repo.Gate[i]
}
}
return nil
}