|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "strconv" |
| 8 | +) |
| 9 | + |
| 10 | +// RegisterAutofixCommands adds the /autofix command. |
| 11 | +func RegisterAutofixCommands(r *Registry) { |
| 12 | + r.Register(Command{ |
| 13 | + Name: "/autofix", |
| 14 | + Aliases: []string{"/af"}, |
| 15 | + Description: "run tests, ask agent to fix failures, repeat up to N times (default 3)", |
| 16 | + Category: "dev", |
| 17 | + Handler: cmdAutofix, |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +// detectTestCmd returns the test command and args appropriate for the repo. |
| 22 | +func detectTestCmd(repoPath string) (string, []string) { |
| 23 | + // Check for go.mod (most common for this project) |
| 24 | + if _, err := exec.LookPath("go"); err == nil { |
| 25 | + gomod := exec.Command("go", "env", "GOMOD") |
| 26 | + gomod.Dir = repoPath |
| 27 | + if out, err := gomod.Output(); err == nil && len(out) > 0 { |
| 28 | + return "go", []string{"test", "./..."} |
| 29 | + } |
| 30 | + } |
| 31 | + if _, err := exec.LookPath("npm"); err == nil { |
| 32 | + pkg := exec.Command("npm", "run", "test", "--if-present") |
| 33 | + pkg.Dir = repoPath |
| 34 | + return "npm", []string{"test"} |
| 35 | + } |
| 36 | + if _, err := exec.LookPath("pytest"); err == nil { |
| 37 | + return "pytest", []string{} |
| 38 | + } |
| 39 | + // Default fallback |
| 40 | + return "go", []string{"test", "./..."} |
| 41 | +} |
| 42 | + |
| 43 | +func cmdAutofix(ctx Context) Result { |
| 44 | + maxAttempts := 3 |
| 45 | + if ctx.HasArg(1) { |
| 46 | + if v, err := strconv.Atoi(ctx.Arg(1)); err == nil && v > 0 { |
| 47 | + maxAttempts = v |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if ctx.REPL.StreamAndPrint == nil { |
| 52 | + PrintError("agent not available for autofix") |
| 53 | + return Result{Handled: true} |
| 54 | + } |
| 55 | + |
| 56 | + testCmd, testArgs := detectTestCmd(ctx.RepoPath) |
| 57 | + |
| 58 | + for attempt := 1; attempt <= maxAttempts; attempt++ { |
| 59 | + fmt.Printf("%s[attempt %d/%d]%s running %s %v\n", |
| 60 | + ColorDim, attempt, maxAttempts, ColorReset, testCmd, testArgs) |
| 61 | + |
| 62 | + cmd := exec.Command(testCmd, testArgs...) |
| 63 | + cmd.Dir = ctx.RepoPath |
| 64 | + output, err := cmd.CombinedOutput() |
| 65 | + |
| 66 | + if err == nil { |
| 67 | + PrintSuccess("tests passed on attempt %d/%d", attempt, maxAttempts) |
| 68 | + return Result{Handled: true} |
| 69 | + } |
| 70 | + |
| 71 | + // Tests failed — print failure output |
| 72 | + fmt.Printf("%s── Test failures ───────────────────%s\n", ColorDim, ColorReset) |
| 73 | + fmt.Println(string(output)) |
| 74 | + fmt.Printf("%s──────────────────────────────────%s\n\n", ColorDim, ColorReset) |
| 75 | + |
| 76 | + if attempt == maxAttempts { |
| 77 | + break |
| 78 | + } |
| 79 | + |
| 80 | + // Ask agent to fix |
| 81 | + prompt := fmt.Sprintf("The following tests are failing. Fix them:\n\n%s", string(output)) |
| 82 | + ctx.REPL.StreamAndPrint(context.Background(), ctx.Agent, prompt, ctx.RepoPath) |
| 83 | + } |
| 84 | + |
| 85 | + fmt.Printf("%sautofix gave up after %d attempts%s\n\n", ColorRed, maxAttempts, ColorReset) |
| 86 | + return Result{Handled: true} |
| 87 | +} |
0 commit comments