prepareWorkspace chose between Manager.Prepare (refresh onto base) and a separate Manager.Resume, and Manager.Resume only opened an already-prepared clone. A remediation whose local worktree the terminal cleanup removed, an expired volume dropped, or a different host never had could therefore never be resumed, and the retry path had no way to select the mode that continues pushed work. Unify the two matched methods into Prepare(..., Target): Fresh resets onto origin/<base>, a non-empty Target resets onto origin/<that branch>, and either target clones a missing worktree. The daemon's retry path selects the target from the task row and passes the persisted branch, so a resume lands on the branch the open PR lives on and fails closed when it is not on the remote. Delete the container-mode no-op resume (hybridTrees.Resume and the resumableTrees capability) now that the daemon owns it. The remediate workflow no longer stages a resume, and the retired remediate.resume step word stays resolvable as an inert stage: a stored definition is validated as a whole collection, so a removed word would fail every workflow to decode, not just its own.
264 lines
8.7 KiB
Go
264 lines
8.7 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/worktree"
|
|
)
|
|
|
|
// runGit runs git in dir, failing the test on error.
|
|
func runGit(t *testing.T, dir string, args ...string) {
|
|
t.Helper()
|
|
ctx := t.Context()
|
|
cmd := exec.CommandContext(ctx, "git", args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v: %v\n%s", args, err, out)
|
|
}
|
|
}
|
|
|
|
// gitRepoWithOriginRef creates a repo with a base commit, then labels
|
|
// that commit "origin/<base>" (a plain local branch standing in for a
|
|
// remote-tracking ref) so Manager.Diff/ChangedFiles -- which always diff
|
|
// against "origin/<base>" -- resolve without a real clone/push round trip.
|
|
func gitRepoWithOriginRef(t *testing.T, base string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
runGit(t, dir, "init", "-b", base)
|
|
runGit(t, dir, "config", "user.name", "archie-bot")
|
|
runGit(t, dir, "config", "user.email", "archie-bot@example.com")
|
|
runGit(t, dir, "config", "commit.gpgsign", "false")
|
|
if err := os.WriteFile(filepath.Join(dir, "README.md"), []byte("seed\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, dir, "add", "-A")
|
|
runGit(t, dir, "commit", "-m", "seed")
|
|
runGit(t, dir, "branch", "origin/"+base, base)
|
|
return dir
|
|
}
|
|
|
|
// hostileSigningConfig writes a global git config that makes every commit
|
|
// fail unless the fixture disables signing in the repository's own config.
|
|
func hostileSigningConfig(t *testing.T) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "hostile.gitconfig")
|
|
const cfg = "[user]\n\tname = Hostile\n\temail = hostile@example.com\n\tsigningkey = /nonexistent/signing-key\n[gpg]\n\tformat = ssh\n[commit]\n\tgpgsign = true\n"
|
|
if err := os.WriteFile(path, []byte(cfg), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
// TestGitRepoWithOriginRefIgnoresHostSigningConfig pins the fixture's
|
|
// hermeticity: it must not depend on the developer's global git identity or a
|
|
// signing agent.
|
|
func TestGitRepoWithOriginRefIgnoresHostSigningConfig(t *testing.T) {
|
|
t.Setenv("GIT_CONFIG_GLOBAL", hostileSigningConfig(t))
|
|
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
|
gitRepoWithOriginRef(t, "main")
|
|
}
|
|
|
|
func newYaegiGateTaskContext(t *testing.T, dir string) *TaskContext {
|
|
t.Helper()
|
|
return &TaskContext{
|
|
Task: &Task{ID: 1, Owner: "acme", Repo: "todo", IssueNumber: 42},
|
|
Repo: config.Repo{Owner: "acme", Name: "todo", Base: "main"},
|
|
Trees: &realTrees{Manager: &worktree.Manager{WorkDir: t.TempDir()}},
|
|
Dir: dir,
|
|
Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
}
|
|
|
|
func writeGateGo(t *testing.T, dir, src string) {
|
|
t.Helper()
|
|
archieDir := filepath.Join(dir, ".archie")
|
|
if err := os.MkdirAll(archieDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(archieDir, "gate.go"), []byte(src), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestStageYaegiGateNoScriptIsNoop(t *testing.T) {
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
t.Skip("git not installed")
|
|
}
|
|
dir := gitRepoWithOriginRef(t, "main")
|
|
tc := newYaegiGateTaskContext(t, dir)
|
|
|
|
if err := StageYaegiGate().Run(context.Background(), tc); err != nil {
|
|
t.Fatalf("StageYaegiGate() = %v, want nil (no .archie/gate.go)", err)
|
|
}
|
|
}
|
|
|
|
func TestStageYaegiGateBlocksOnErrorFinding(t *testing.T) {
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
t.Skip("git not installed")
|
|
}
|
|
dir := gitRepoWithOriginRef(t, "main")
|
|
writeGateGo(t, dir, `package gate
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/gate"
|
|
)
|
|
|
|
func Check(ctx gate.GateContext) []gate.Finding {
|
|
var findings []gate.Finding
|
|
for _, line := range strings.Split(ctx.Diff, "\n") {
|
|
if strings.HasPrefix(line, "+") && strings.Contains(line, "panic(") {
|
|
findings = append(findings, gate.Finding{Level: "error", Message: "new panic() call"})
|
|
}
|
|
}
|
|
return findings
|
|
}
|
|
`)
|
|
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n\nfunc main() { panic(\"boom\") }\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, dir, "add", "-A")
|
|
runGit(t, dir, "commit", "-m", "add panic")
|
|
|
|
tc := newYaegiGateTaskContext(t, dir)
|
|
err := StageYaegiGate().Run(context.Background(), tc)
|
|
if err == nil {
|
|
t.Fatal("StageYaegiGate() = nil, want an error for the blocking finding")
|
|
}
|
|
}
|
|
|
|
func TestStageYaegiGateAllowsWarnFinding(t *testing.T) {
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
t.Skip("git not installed")
|
|
}
|
|
dir := gitRepoWithOriginRef(t, "main")
|
|
writeGateGo(t, dir, `package gate
|
|
|
|
import "github.com/samcharles93/archie-core/internal/gate"
|
|
|
|
func Check(ctx gate.GateContext) []gate.Finding {
|
|
return []gate.Finding{{Level: "warn", Message: "advisory only"}}
|
|
}
|
|
`)
|
|
if err := os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("hi\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, dir, "add", "-A")
|
|
runGit(t, dir, "commit", "-m", "add notes")
|
|
|
|
tc := newYaegiGateTaskContext(t, dir)
|
|
if err := StageYaegiGate().Run(context.Background(), tc); err == nil {
|
|
t.Fatal("StageYaegiGate() = nil, want legacy gate rejection")
|
|
}
|
|
}
|
|
|
|
func TestStageYaegiGateRejectsUnknownLevel(t *testing.T) {
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
t.Skip("git not installed")
|
|
}
|
|
dir := gitRepoWithOriginRef(t, "main")
|
|
writeGateGo(t, dir, `package gate
|
|
|
|
import "github.com/samcharles93/archie-core/internal/gate"
|
|
|
|
func Check(ctx gate.GateContext) []gate.Finding {
|
|
return []gate.Finding{{Level: "fatal", Message: "mis-typed level"}}
|
|
}
|
|
`)
|
|
if err := os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("hi\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runGit(t, dir, "add", "-A")
|
|
runGit(t, dir, "commit", "-m", "add notes")
|
|
|
|
tc := newYaegiGateTaskContext(t, dir)
|
|
if err := StageYaegiGate().Run(context.Background(), tc); err == nil {
|
|
t.Fatal("StageYaegiGate() = nil, want an error for an unrecognized finding level (fail closed)")
|
|
}
|
|
}
|
|
|
|
func TestStageRepoStagesNilLoaderIsNoop(t *testing.T) {
|
|
tc := &TaskContext{Log: slog.New(slog.DiscardHandler)}
|
|
if err := StageRepoStages().Run(context.Background(), tc); err != nil {
|
|
t.Fatalf("StageRepoStages() = %v, want nil (no loader wired up)", err)
|
|
}
|
|
}
|
|
|
|
func TestStageRepoStagesRunsAllInOrder(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.MkdirAll(filepath.Join(dir, ".archie", "stages"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, ".archie", "stages", "first.go"), []byte("package stages"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tc := &TaskContext{Dir: dir, Log: slog.New(slog.DiscardHandler)}
|
|
if err := StageRepoStages().Run(context.Background(), tc); err == nil {
|
|
t.Fatal("StageRepoStages() = nil, want legacy stage rejection")
|
|
}
|
|
}
|
|
|
|
func TestStageRepoStagesStopsOnOutcome(t *testing.T) {
|
|
t.Skip("repository-authored stages are no longer executable")
|
|
}
|
|
|
|
func TestStageRepoStagesPropagatesLoaderError(t *testing.T) {
|
|
t.Skip("repository-authored stage loaders are no longer called")
|
|
}
|
|
|
|
// TestStageDiffCapUnlimitedWhenCapIsZero pins that an explicit 0 switches the
|
|
// cap off. The dashboard schema documents 0 that way, and this is the step that
|
|
// has to honour it -- for a long time it could not, because the config loader
|
|
// rewrote an explicit 0 to the default before the step ever saw it.
|
|
func TestStageDiffCapUnlimitedWhenCapIsZero(t *testing.T) {
|
|
tc := &TaskContext{
|
|
Cfg: config.Config{DiffCapLines: new(0)},
|
|
Trees: &fakeTrees{changedLines: 10_000},
|
|
Repo: config.Repo{Owner: "acme", Name: "app"},
|
|
}
|
|
|
|
if err := StageDiffCap().Run(context.Background(), tc); err != nil {
|
|
t.Fatalf("StageDiffCap: %v", err)
|
|
}
|
|
if tc.Outcome.Status == StatusParked {
|
|
t.Fatalf("task parked with the cap switched off: %q", tc.Outcome.Detail)
|
|
}
|
|
}
|
|
|
|
// TestStageDiffCapParksWithAnActionableReason is the regression case for a park
|
|
// reason that told the operator to do something impossible. A parked task's
|
|
// actions are retry, abandon and reject (internal/taskstate), so "approve
|
|
// manually" sent them looking for a button that does not exist -- which is
|
|
// exactly what happened on task 11.
|
|
func TestStageDiffCapParksWithAnActionableReason(t *testing.T) {
|
|
tc := &TaskContext{
|
|
Cfg: config.Config{DiffCapLines: new(400)},
|
|
Trees: &fakeTrees{changedLines: 789},
|
|
Repo: config.Repo{Owner: "acme", Name: "app"},
|
|
}
|
|
|
|
if err := StageDiffCap().Run(context.Background(), tc); err != nil {
|
|
t.Fatalf("StageDiffCap: %v", err)
|
|
}
|
|
if tc.Outcome.Status != StatusParked {
|
|
t.Fatalf("Outcome.Status = %q, want parked at 789 lines over a 400 cap", tc.Outcome.Status)
|
|
}
|
|
if strings.Contains(tc.Outcome.Detail, "approve") {
|
|
t.Errorf("park reason offers approve, which is not an action a parked task has: %q", tc.Outcome.Detail)
|
|
}
|
|
for _, want := range []string{"789", "400", "diff_cap_lines"} {
|
|
if !strings.Contains(tc.Outcome.Detail, want) {
|
|
t.Errorf("park reason %q is missing %q", tc.Outcome.Detail, want)
|
|
}
|
|
}
|
|
}
|