From 7ee493b9cc91225a2b9bff6f379863cd0cde81d4 Mon Sep 17 00:00:00 2001 From: Alexei Ledenev Date: Tue, 24 Nov 2026 14:55:11 +0200 Subject: [PATCH 1/4] feat(replay): name up to three changes to look at, with their status --- internal/application/replay/replay.go | 59 ++++++++++++++++++---- internal/application/replay/replay_test.go | 49 ++++++++++++++++++ 2 files changed, 99 insertions(+), 9 deletions(-) diff --git a/internal/application/replay/replay.go b/internal/application/replay/replay.go index b5a9017f..99bc514e 100644 --- a/internal/application/replay/replay.go +++ b/internal/application/replay/replay.go @@ -34,13 +34,20 @@ package replay import ( "context" "fmt" + "regexp" + "strconv" "github.com/coupling-dev/archfit/internal/model/result" ) -// maxExamples is how many changes that would ask the result names. +// maxExamples is how many changes of each named class the result names. const maxExamples = 3 +// subjectNumber finds the pull request number GitHub writes into the subject +// of a squash merge, "Title (#12)", or of a merge commit, "Merge pull request +// #12 from ...". +var subjectNumber = regexp.MustCompile(`\(#([0-9]+)\)\s*$|^Merge pull request #([0-9]+)\b`) + // Change is one first-parent commit of the default branch. type Change struct { // Commit is the full commit id. @@ -64,11 +71,26 @@ func (c Change) Label() string { if c.PR > 0 { return fmt.Sprintf("#%d %s", c.PR, c.Title) } - short := c.Commit - if len(short) > 7 { - short = short[:7] + return c.short() + " " + c.Subject +} + +// Name names the change in a list: "#12", the number GitHub wrote into the +// subject when no pull request is known, or else the short commit id. +func (c Change) Name() string { + if c.PR > 0 { + return "#" + strconv.Itoa(c.PR) + } + if m := subjectNumber.FindStringSubmatch(c.Subject); m != nil { + return "#" + m[1] + m[2] + } + return c.short() +} + +func (c Change) short() string { + if len(c.Commit) > 7 { + return c.Commit[:7] } - return short + " " + c.Subject + return c.Commit } // Source is the history a replay reads. @@ -111,6 +133,15 @@ type Example struct { What string } +// Look is a change to look at first. +type Look struct { + // Name is Change.Name. + Name string + // Status is what an owner would see on it: fail, waiting or unknown. + // A change with no parent, or a check that could not run, is unknown. + Status string +} + // Result is what a replay found. type Result struct { // Source is the branch the changes were read from. @@ -123,6 +154,8 @@ type Result struct { Passed, Asked, Look int // Examples are up to three of the changes that would ask, newest first. Examples []Example + // LookAt are up to three of the changes to look at first, newest first. + LookAt []Look // Stopped is true when the context ended before every change was read. Stopped bool // Incomplete is true when the clone is shallow and the history it holds @@ -165,6 +198,9 @@ func (s Service) Run(ctx context.Context, n int) (Result, error) { } default: res.Look++ + if len(res.LookAt) < maxExamples { + res.LookAt = append(res.LookAt, Look{Name: c.Name(), Status: what}) + } } } return res, nil @@ -178,14 +214,16 @@ const ( classAsked ) -// judge classifies one change. what is the first error that asks an owner. +// judge classifies one change. what is the first error that asks an owner, +// or the status of a change to look at. func (s Service) judge(ctx context.Context, c Change) (cl class, what string, err error) { + unknown := string(result.StatusUnknown) if c.Parent == "" { - return classLook, "", nil + return classLook, unknown, nil } r, err := s.Checker.Check(ctx, c) if err != nil { - return classLook, "", err + return classLook, unknown, err } switch r.Status { case result.StatusPass: @@ -197,7 +235,10 @@ func (s Service) judge(ctx context.Context, c Change) (cl class, what string, er } } } - return classLook, "", nil + if r.Status == "" { + return classLook, unknown, nil + } + return classLook, string(r.Status), nil } // linear reports whether the changes hold no merge commit. diff --git a/internal/application/replay/replay_test.go b/internal/application/replay/replay_test.go index fddd8ed4..b5750f52 100644 --- a/internal/application/replay/replay_test.go +++ b/internal/application/replay/replay_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "slices" "testing" "time" @@ -274,3 +275,51 @@ func TestRunStopsWhenTheContextEndsInTheHistory(t *testing.T) { t.Errorf("got %+v, %v; want a stopped result with nothing read", got, err) } } + +func TestRunNamesUpToThreeChangesToLookAt(t *testing.T) { + fixOnly := result.Result{Status: result.StatusFail, Errors: []result.Error{{What: "x", Next: result.Next{Kind: result.NextFix}}}} + changes := []replay.Change{ + {Commit: "c1", Parent: "p", PR: 12}, + {Commit: "c2", Parent: "p", Subject: "Add billing (#11)"}, + {Commit: "c3", Parent: "p", Subject: "Merge pull request #9 from a/b", Merge: true}, + {Commit: "c4", Parent: "p", PR: 8}, + {Commit: "c5abcdef01"}, + } + svc := replay.Service{ + History: &history{src: replay.Source{Changes: changes}}, + Checker: &checker{ + results: map[string]result.Result{"c1": fixOnly, "c3": {Status: result.StatusWaiting}, "c4": {}}, + errs: map[string]error{"c2": errors.New("git worktree add failed")}, + }, + } + got, err := svc.Run(context.Background(), 5) + if err != nil { + t.Fatal(err) + } + want := []replay.Look{{Name: "#12", Status: "fail"}, {Name: "#11", Status: "unknown"}, {Name: "#9", Status: "waiting"}} + if got.Look != 5 || !slices.Equal(got.LookAt, want) { + t.Errorf("got %d to look at, named %+v; want 5, named %+v", got.Look, got.LookAt, want) + } +} + +func TestChangeName(t *testing.T) { + cases := []struct { + name string + c replay.Change + want string + }{ + {"pull request from GitHub", replay.Change{Commit: "abcdef0123", PR: 7, Subject: "Fix (#3)"}, "#7"}, + {"squash subject", replay.Change{Commit: "abcdef0123", Subject: "Add the tc shaper (#431)"}, "#431"}, + {"merge subject", replay.Change{Commit: "abcdef0123", Subject: "Merge pull request #12 from a/feature"}, "#12"}, + {"number inside the subject", replay.Change{Commit: "abcdef0123", Subject: "Revert (#5) and more"}, "abcdef0"}, + {"no number", replay.Change{Commit: "abcdef0123", Subject: "Initial commit"}, "abcdef0"}, + {"short commit", replay.Change{Commit: "abc"}, "abc"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := tc.c.Name(); got != tc.want { + t.Errorf("Name() = %q, want %q", got, tc.want) + } + }) + } +} From 554ef50f51eec66f57f5e9f4e3fc957df45c9b0d Mon Sep 17 00:00:00 2001 From: Alexei Ledenev Date: Tue, 24 Nov 2026 14:55:16 +0200 Subject: [PATCH 2/4] fix(setup): name the changes to show an owner, say what setup commits, explain a candidate pin --- cmd/archfit/setup_command_test.go | 8 +++- cmd/archfit/setup_files.go | 18 +++++++- cmd/archfit/setup_wiring.go | 6 ++- cmd/archfit/setup_workflow_test.go | 41 ++++++++++++++++++ internal/application/setup/run_test.go | 54 +++++++++++++++++++++++- internal/application/setup/setup.go | 37 ++++++++++++++-- internal/application/setup/setup_test.go | 37 ++++++++++++++-- 7 files changed, 188 insertions(+), 13 deletions(-) diff --git a/cmd/archfit/setup_command_test.go b/cmd/archfit/setup_command_test.go index 6888f765..c20a13c5 100644 --- a/cmd/archfit/setup_command_test.go +++ b/cmd/archfit/setup_command_test.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "slices" "strings" "testing" @@ -346,11 +347,14 @@ func TestSetupFirstPublication(t *testing.T) { //nolint:gocyclo // the process b t.Fatal(out) } } - if out := run(); !strings.Contains(out, "opened setup pull request #7") { + announce := regexp.MustCompile(`(?m)^setup commits (\S+, )*\.github/workflows/archfit\.yaml(, \S+)* on archfit/setup and pushes the branch\ncheck: `) + if out := run(); !strings.Contains(out, "opened setup pull request #7") || !announce.MatchString(out) || + !strings.Contains(out, "committed as Setup Author ") || strings.Contains(out, "--no-hooks leaves out") { t.Fatal(out) } body, err := os.ReadFile(filepath.Join(dir, ".archfit-cache/setup-pull-request.md")) //nolint:gosec // dir is a test fixture - if err != nil || !strings.Contains(string(body), "## Last 30 merged pull requests\n\n1 passed, 0 needed an owner, 1 to show an owner.") { + root := strings.TrimSpace(gitOutput(t, dir, "rev-list", "--max-parents=0", "--abbrev-commit", "--abbrev=7", headRef)) + if err != nil || !strings.Contains(string(body), "## Last 30 merged pull requests\n\n1 passed, 0 needed an owner, 1 to show an owner ("+root+": unknown).") { t.Fatalf("missing replay in PR body: %s, %v", body, err) } if words := testutil.StoryNotUsedIn(string(body)); len(words) > 0 { diff --git a/cmd/archfit/setup_files.go b/cmd/archfit/setup_files.go index b220f1b7..e388f7cd 100644 --- a/cmd/archfit/setup_files.go +++ b/cmd/archfit/setup_files.go @@ -162,7 +162,16 @@ func (f setupFiles) ValidPolicy(data []byte) (valid, required bool) { return err == nil, err == nil && policy.Gate == policyfile.GateRequired } -func (f setupFiles) PinNote() string { return doctorPin(f.co.root, version) } +// PinNote compares the local engine with the workflow pin. Setup leaves out +// "cannot tell": nothing in setup changes it, and its causes are a candidate +// pin setup wrote itself, a development build, or a workflow without a version +// comment. doctor still reports it. +func (f setupFiles) PinNote() string { + if note := doctorPin(f.co.root, version); note != unknownWorkflowPin { + return note + } + return "" +} func (f setupFiles) Exceptions() ([]string, error) { data, _, err := f.co.policyAt("") @@ -222,6 +231,13 @@ func (f setupFiles) Paths() []string { return paths } +func (f setupFiles) HookPaths() []string { + if f.noHooks { + return nil + } + return []string{harnessConfigFile(harnessClaude), harnessConfigFile(harnessCodex)} +} + func setupUsesAction(data []byte) bool { var workflow struct { Jobs map[string]struct { diff --git a/cmd/archfit/setup_wiring.go b/cmd/archfit/setup_wiring.go index 033fd897..3093ba18 100644 --- a/cmd/archfit/setup_wiring.go +++ b/cmd/archfit/setup_wiring.go @@ -46,7 +46,11 @@ func setupDependencies(deps *appDeps, dir string, noHooks bool) (setup.Dependenc svc := co.replay(&local) svc.History = replayHistory{co: co, ref: setupgit.OriginRef(branch), noLabels: true} r, err := svc.Run(ctx, 30) - return setup.ReplaySummary{Read: r.Read, Passed: r.Passed, NeededOwner: r.Asked, ToShowOwner: r.Look}, err + summary := setup.ReplaySummary{Read: r.Read, Passed: r.Passed, NeededOwner: r.Asked, ToShowOwner: r.Look} + for _, l := range r.LookAt { + summary.ToShow = append(summary.ToShow, l.Name+": "+l.Status) + } + return summary, err }, Init: func(ctx context.Context) (string, error) { return runInit(ctx, &local, co, noHooks, true) diff --git a/cmd/archfit/setup_workflow_test.go b/cmd/archfit/setup_workflow_test.go index b2adf2ef..d4588a59 100644 --- a/cmd/archfit/setup_workflow_test.go +++ b/cmd/archfit/setup_workflow_test.go @@ -177,3 +177,44 @@ func TestSetupPathReadFailureSuggestsRegularFile(t *testing.T) { t.Fatalf("next %q error %v", next, err) } } + +func TestSetupHookPathsFollowNoHooks(t *testing.T) { + hooks := []string{harnessConfigFile(harnessClaude), harnessConfigFile(harnessCodex)} + if got := (setupFiles{workflow: fixtureSetupWorkflow}).HookPaths(); !slices.Equal(got, hooks) { + t.Errorf("HookPaths() = %v, want %v", got, hooks) + } + if got := (setupFiles{workflow: fixtureSetupWorkflow, noHooks: true}).HookPaths(); got != nil { + t.Errorf("HookPaths() with --no-hooks = %v, want none", got) + } +} + +// TestSetupPinNoteLeavesOutCannotTell: setup prints the pin line only when it +// can compare; a candidate pin is "cannot tell", which only doctor reports. +func TestSetupPinNoteLeavesOutCannotTell(t *testing.T) { + old := version + version = "v0.10.0" + t.Cleanup(func() { version = old }) + sha := strings.Repeat("b", 40) + for _, tc := range []struct { + name, comment, want string + }{ + {"candidate", "candidate", ""}, + {"release", "v0.10.0", "the local engine matches the workflow pin"}, + {"other release", "v0.9.0", "the local engine does not match the workflow pin (v0.10.0 vs v0.9.0)"}, + } { + t.Run(tc.name, func(t *testing.T) { + root := t.TempDir() + path := filepath.Join(root, fixtureSetupWorkflow) + if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(setupWorkflow("main", tc.comment, sha)), 0o600); err != nil { + t.Fatal(err) + } + f := setupFiles{co: checkout{root: root}, workflow: fixtureSetupWorkflow} + if got := f.PinNote(); got != tc.want { + t.Errorf("PinNote() = %q, want %q", got, tc.want) + } + }) + } +} diff --git a/internal/application/setup/run_test.go b/internal/application/setup/run_test.go index 60739ed4..00e4ef7e 100644 --- a/internal/application/setup/run_test.go +++ b/internal/application/setup/run_test.go @@ -6,6 +6,8 @@ import ( "slices" "strings" "testing" + + "github.com/coupling-dev/archfit/internal/testutil" ) const ( @@ -203,6 +205,7 @@ type setupFixture struct { ssh bool actions []string workflowTag, workflowSHA string + hooks []string } func (f *setupFixture) deps(t *testing.T) Dependencies { @@ -245,7 +248,10 @@ func (f *setupFixture) WriteBody(body string) error { return nil } func (f *setupFixture) ValidationPaths() []string { return f.Paths() } -func (f *setupFixture) Paths() []string { return []string{".archfit.yaml", WorkflowPath} } +func (f *setupFixture) Paths() []string { + return slices.Concat([]string{".archfit.yaml", WorkflowPath}, f.hooks) +} +func (f *setupFixture) HookPaths() []string { return f.hooks } type fixtureGit struct{ *setupFixture } @@ -430,6 +436,52 @@ func TestCandidateActionPin(t *testing.T) { if f.workflowSHA != tc.ref || f.workflowTag != tc.note { t.Fatalf("workflow pinned %q@%q, want %q@%q", f.workflowTag, f.workflowSHA, tc.note, tc.ref) } + if want := "marked `# " + note + "` instead of a release tag"; !strings.Contains(f.body, want) { + t.Fatalf("body does not explain the candidate pin %q:\n%s", want, f.body) + } + }) + } +} + +// TestSetupSaysWhatItCommits: the run that commits names the files and the +// branch before the check, and the run before it says how to leave out the +// hooks, while --no-hooks can still do that. +func TestSetupSaysWhatItCommits(t *testing.T) { + hooks := []string{".claude/settings.json", ".codex/hooks.json"} + for _, tc := range []struct { + name string + hooks []string + firstRun string + committed string + }{ + {"with hooks", hooks, + "the next archfit setup commits the setup files, with .claude/settings.json and .codex/hooks.json, on archfit/setup and pushes the branch; archfit setup --no-hooks leaves out the hook files", + "setup commits .archfit.yaml, .github/workflows/archfit.yaml, .claude/settings.json, .codex/hooks.json on archfit/setup and pushes the branch"}, + {"no hooks", nil, "", "setup commits .archfit.yaml, .github/workflows/archfit.yaml on archfit/setup and pushes the branch"}, + } { + t.Run(tc.name, func(t *testing.T) { + f := setupFixture{branch: fixtureBranch, ready: true, scope: true, hooks: tc.hooks} + deps := f.deps(t) + first := strings.Join(Run(t.Context(), deps).Lines, "\n") + if tc.firstRun == "" && strings.Contains(first, "--no-hooks") { + t.Errorf("hook line without hooks:\n%s", first) + } + if !strings.Contains(first, tc.firstRun) { + t.Errorf("missing %q in:\n%s", tc.firstRun, first) + } + second := strings.Join(Run(t.Context(), deps).Lines, "\n") + says, check := strings.Index(second, tc.committed), strings.Index(second, "check: fail against origin/main") + if says < 0 || check < says { + t.Errorf("want %q before the check in:\n%s", tc.committed, second) + } + if !strings.Contains(second, "committed as Setup Author ") { + t.Errorf("missing the commit line in:\n%s", second) + } + for _, text := range []string{first, second} { + if words := testutil.StoryNotUsedIn(text); len(words) > 0 { + t.Errorf("vocabulary %v in:\n%s", words, text) + } + } }) } } diff --git a/internal/application/setup/setup.go b/internal/application/setup/setup.go index ba87885b..99fef5a1 100644 --- a/internal/application/setup/setup.go +++ b/internal/application/setup/setup.go @@ -41,6 +41,8 @@ type Files interface { WriteBody(body string) error Paths() []string ValidationPaths() []string + // HookPaths are the hook settings files among Paths; none with --no-hooks. + HookPaths() []string } type unsafeDestination interface{ UnsafeDestination() } @@ -117,10 +119,21 @@ type Dependencies struct { // ReplaySummary counts where the current policy would have asked for help. type ReplaySummary struct { Read, Passed, NeededOwner, ToShowOwner int + // ToShow names up to three of the changes to show an owner, newest + // first, each with what the owner would see: "#12: fail". + ToShow []string } func (r ReplaySummary) counts() string { - return fmt.Sprintf("%d passed, %d needed an owner, %d to show an owner", r.Passed, r.NeededOwner, r.ToShowOwner) + counts := fmt.Sprintf("%d passed, %d needed an owner, %d to show an owner", r.Passed, r.NeededOwner, r.ToShowOwner) + if len(r.ToShow) == 0 { + return counts + } + names := slices.Clone(r.ToShow) + if more := r.ToShowOwner - len(names); more > 0 { + names = append(names, fmt.Sprintf("%d more", more)) + } + return counts + " (" + strings.Join(names, ", ") + ")" } // CheckResult carries the local verdict and its continuation, if any. @@ -239,9 +252,14 @@ func installLink(owner, repo uint64) string { } // Body constructs the setup pull request text without local checkout paths. -func Body(version, link string, replay *ReplaySummary, decisions []string) string { +// pinNote is the workflow comment of an Action pinned by commit in a candidate +// build; it is empty for a release. +func Body(version, link, pinNote string, replay *ReplaySummary, decisions []string) string { var b strings.Builder fmt.Fprintf(&b, "This pull request sets up archfit %s: the policy, the workflow and the instructions for agents.\nChecks are advisory: nothing blocks a merge until an owner makes the check required.\n", strings.TrimPrefix(version, "v")) + if pinNote != "" { + fmt.Fprintf(&b, "The workflow pins a candidate build of the Action: a commit made for testing before a release, marked `# %s` instead of a release tag.\n", pinNote) + } if replay != nil { fmt.Fprintf(&b, "\n## Last 30 merged pull requests\n\n%s. Read them with `archfit replay --last 30`.\n", replay.counts()) } @@ -384,6 +402,12 @@ func Run(ctx context.Context, d Dependencies) Outcome { out.add(" %s", line) } } + // The run that commits prints its lines only at its end, so this is + // the last point where --no-hooks keeps the hooks out of the branch. + if hooks := d.Files.HookPaths(); len(hooks) > 0 { + out.add("the next %s commits the setup files, with %s, on %s and pushes the branch; %s --no-hooks leaves out the hook files", + Command, strings.Join(hooks, " and "), Branch, Command) + } default: publish(ctx, d, s, &out) } @@ -496,6 +520,7 @@ func commit(ctx context.Context, d Dependencies, s *Detection, out *Outcome, pat out.stop(errors.New("git author name or email is missing"), "set git user.name and user.email, then archfit setup") return false } + out.add("setup commits %s on %s and pushes the branch", strings.Join(paths, ", "), Branch) base := "origin/" + s.Default check, err := d.Check(ctx, base) if err != nil { @@ -522,7 +547,7 @@ func commit(ctx context.Context, d Dependencies, s *Detection, out *Outcome, pat out.stop(err, "fix the commit error, then archfit setup") return false } - out.add("committed %s as %s", strings.Join(paths, ", "), identity) + out.add("committed as %s", identity) s.pushed = false return true } @@ -566,7 +591,11 @@ func publishPR(ctx context.Context, d Dependencies, s Detection, out *Outcome) { out.stop(err, "fix .archfit.yaml") return } - if err := d.Files.WriteBody(Body(d.Version, out.InstallURL, out.replay(ctx, s.Default, d.Replay), decisions)); err != nil { + var pinNote string + if d.ActionRef != "" { + pinNote = d.ActionRefNote + } + if err := d.Files.WriteBody(Body(d.Version, out.InstallURL, pinNote, out.replay(ctx, s.Default, d.Replay), decisions)); err != nil { out.stop(err, "fix the pull request body file") return } diff --git a/internal/application/setup/setup_test.go b/internal/application/setup/setup_test.go index 256ea30c..c0605e7d 100644 --- a/internal/application/setup/setup_test.go +++ b/internal/application/setup/setup_test.go @@ -63,7 +63,7 @@ func TestTag(t *testing.T) { } func TestBody(t *testing.T) { - bare := Body("v0.10.0", InstallURL, nil, nil) + bare := Body("v0.10.0", InstallURL, "", nil, nil) for _, line := range []string{ "This pull request sets up archfit 0.10.0:", "Checks are advisory: nothing blocks a merge until an owner makes the check required.", @@ -74,17 +74,46 @@ func TestBody(t *testing.T) { t.Errorf("missing %q in %q", line, bare) } } - if strings.Contains(bare, "## Last 30") || strings.Contains(bare, "## For the owner") { + if strings.Contains(bare, "## Last 30") || strings.Contains(bare, "## For the owner") || strings.Contains(bare, "candidate") { t.Errorf("empty sections rendered: %q", bare) } - full := Body("0.10.0", InstallURL, &ReplaySummary{Passed: 22, NeededOwner: 5, ToShowOwner: 3}, []string{"exception legacy: expires 2027-02-20"}) - for _, section := range []string{"## Last 30 merged pull requests\n\n22 passed, 5 needed an owner", "## For the owner to decide\n\n- exception legacy: expires 2027-02-20"} { + full := Body("0.10.0", InstallURL, "candidate", &ReplaySummary{Passed: 22, NeededOwner: 5, ToShowOwner: 3, ToShow: []string{"#12: fail"}}, []string{"exception legacy: expires 2027-02-20"}) + for _, section := range []string{ + "advisory: nothing blocks a merge until an owner makes the check required.\nThe workflow pins a candidate build of the Action: a commit made for testing before a release, marked `# candidate` instead of a release tag.\n", + "## Last 30 merged pull requests\n\n22 passed, 5 needed an owner, 3 to show an owner (#12: fail, 2 more). Read them", + "## For the owner to decide\n\n- exception legacy: expires 2027-02-20", + } { if !strings.Contains(full, section) { t.Errorf("missing %q in %q", section, full) } } } +func TestReplayCountsNameTheChangesToShow(t *testing.T) { + for _, tc := range []struct { + name string + r ReplaySummary + want string + }{ + {"none to show", ReplaySummary{Passed: 3}, "3 passed, 0 needed an owner, 0 to show an owner"}, + {"one", ReplaySummary{Passed: 2, ToShowOwner: 1, ToShow: []string{"#1: unknown"}}, "2 passed, 0 needed an owner, 1 to show an owner (#1: unknown)"}, + {"three", ReplaySummary{ToShowOwner: 3, ToShow: []string{"#12: fail", "#11: unknown", "4f2a9c1: unknown"}}, + "0 passed, 0 needed an owner, 3 to show an owner (#12: fail, #11: unknown, 4f2a9c1: unknown)"}, + {"more than three", ReplaySummary{NeededOwner: 1, ToShowOwner: 5, ToShow: []string{"#12: fail", "#11: unknown", "#9: waiting"}}, + "0 passed, 1 needed an owner, 5 to show an owner (#12: fail, #11: unknown, #9: waiting, 2 more)"}, + } { + t.Run(tc.name, func(t *testing.T) { + got := tc.r.counts() + if got != tc.want { + t.Errorf("counts() = %q, want %q", got, tc.want) + } + if words := testutil.StoryNotUsedIn(got); len(words) > 0 { + t.Errorf("vocabulary %v in %q", words, got) + } + }) + } +} + func TestReplayTimeout(t *testing.T) { var out Outcome got := out.replay(t.Context(), "main", func(ctx context.Context, _ string) (ReplaySummary, error) { From 290e3a676d4088c35eba1986f875e1e106c0f4b3 Mon Sep 17 00:00:00 2001 From: Alexei Ledenev Date: Tue, 24 Nov 2026 14:55:19 +0200 Subject: [PATCH 3/4] docs(setup): describe the named replay line, the commit line and the candidate pin --- CHANGELOG.md | 1 + docs/commands.md | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cb871df..c926650d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ History before 0.9.0 is in [docs/archived/CHANGELOG-v0-v5.md](docs/archived/CHAN ## Unreleased - Add `archfit setup` to write the first policy and pin the Action, then open an advisory setup pull request. +- `archfit setup` names the changes to show an owner, says what it commits and pushes before the commit, explains a `# candidate` pin in the pull request body, and no longer prints a pin line it cannot act on. ## v0.9.0 diff --git a/docs/commands.md b/docs/commands.md index 9cf91c48..1e4b144f 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -239,11 +239,18 @@ Use `--no-hooks` to skip Claude Code and Codex hooks, or `-C DIR` to select a ch | Required checks on | The same, and the policy has `gate: required`. | Check that a branch ruleset requires `archfit/gate`: without it, a fail does not block a merge. | After writing a new policy, setup shows how it would judge up to 30 recent first-parent changes. It also includes the counts in the first setup pull request body. Replay uses the local remote-tracking default branch, does not fetch PR labels or start CI, and stops after 60 seconds. If the local ref is unavailable, run `archfit replay` later. +The replay line names up to three of the changes to show an owner, newest first, each with the status an owner would see on it, for example `1 to show an owner (#1: unknown)`. +Setup asks GitHub for no labels, so it takes the pull request number from the commit subject (`(#12)` or `Merge pull request #12`), or else names the short commit id. -Before a commit, setup runs the check against the remote default branch. A `fail` does not stop setup. +Before a commit, setup prints the files it commits on `archfit/setup` and says that it pushes the branch. It does not ask first. +The first run, which writes the policy, also says that the next run commits the Claude Code and Codex hook files. +To leave them out, run that next `archfit setup` with `--no-hooks`: once they are committed on `archfit/setup`, `--no-hooks` stops until you delete the branch. +Then setup runs the check against the remote default branch. A `fail` does not stop setup. An `unknown` stops setup before the commit and shows the check's next step. Setup commits only changed setup paths, but includes any existing edits in those paths. Review the pull request before merging. +A candidate build, which pins the Action by commit, adds one sentence to the pull request body that explains its `# candidate` pin. +Setup says whether the local engine matches the workflow pin only when it can tell; [`doctor`](#doctor) also reports when it cannot. SSH pushes use the SSH key and do not require the `gh` `workflow` scope. When `gh` does not report classic scopes, setup attempts the push and reports a permission error if Git rejects it. From 04d3621eee0ad5bbb8112a1dd62f51748cfd5695 Mon Sep 17 00:00:00 2001 From: Alexei Ledenev Date: Tue, 24 Nov 2026 14:58:44 +0200 Subject: [PATCH 4/4] fix(setup): print the commit line after the check, keep the hook hint free of the command, name test literals --- cmd/archfit/setup_command_test.go | 6 +++--- cmd/archfit/setup_workflow_test.go | 7 +++++-- docs/commands.md | 8 ++++---- internal/application/replay/replay_test.go | 11 ++++++----- internal/application/setup/run_test.go | 16 ++++++++-------- internal/application/setup/setup.go | 6 +++--- internal/application/setup/setup_test.go | 9 ++++++--- 7 files changed, 35 insertions(+), 28 deletions(-) diff --git a/cmd/archfit/setup_command_test.go b/cmd/archfit/setup_command_test.go index c20a13c5..3ad0177a 100644 --- a/cmd/archfit/setup_command_test.go +++ b/cmd/archfit/setup_command_test.go @@ -242,7 +242,7 @@ func TestSetupResumesPublishedBranchWithoutMutations(t *testing.T) { //nolint:go func TestSetupFirstPublication(t *testing.T) { //nolint:gocyclo // the process boundary enumerates each external response old := version - version = "v0.10.0" + version = setupRelease t.Cleanup(func() { version = old }) for _, tc := range []struct { name string @@ -347,9 +347,9 @@ func TestSetupFirstPublication(t *testing.T) { //nolint:gocyclo // the process b t.Fatal(out) } } - announce := regexp.MustCompile(`(?m)^setup commits (\S+, )*\.github/workflows/archfit\.yaml(, \S+)* on archfit/setup and pushes the branch\ncheck: `) + announce := regexp.MustCompile(`(?m)^check: .*\nsetup commits (\S+, )*\.github/workflows/archfit\.yaml(, \S+)* on archfit/setup and pushes the branch\n`) if out := run(); !strings.Contains(out, "opened setup pull request #7") || !announce.MatchString(out) || - !strings.Contains(out, "committed as Setup Author ") || strings.Contains(out, "--no-hooks leaves out") { + !strings.Contains(out, "committed as Setup Author ") || strings.Contains(out, "add --no-hooks") { t.Fatal(out) } body, err := os.ReadFile(filepath.Join(dir, ".archfit-cache/setup-pull-request.md")) //nolint:gosec // dir is a test fixture diff --git a/cmd/archfit/setup_workflow_test.go b/cmd/archfit/setup_workflow_test.go index d4588a59..89402274 100644 --- a/cmd/archfit/setup_workflow_test.go +++ b/cmd/archfit/setup_workflow_test.go @@ -190,16 +190,19 @@ func TestSetupHookPathsFollowNoHooks(t *testing.T) { // TestSetupPinNoteLeavesOutCannotTell: setup prints the pin line only when it // can compare; a candidate pin is "cannot tell", which only doctor reports. +// setupRelease is the engine version these setup tests pin. +const setupRelease = "v0.10.0" + func TestSetupPinNoteLeavesOutCannotTell(t *testing.T) { old := version - version = "v0.10.0" + version = setupRelease t.Cleanup(func() { version = old }) sha := strings.Repeat("b", 40) for _, tc := range []struct { name, comment, want string }{ {"candidate", "candidate", ""}, - {"release", "v0.10.0", "the local engine matches the workflow pin"}, + {"release", setupRelease, "the local engine matches the workflow pin"}, {"other release", "v0.9.0", "the local engine does not match the workflow pin (v0.10.0 vs v0.9.0)"}, } { t.Run(tc.name, func(t *testing.T) { diff --git a/docs/commands.md b/docs/commands.md index 1e4b144f..081faa63 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -238,15 +238,15 @@ Use `--no-hooks` to skip Claude Code and Codex hooks, or `-C DIR` to select a ch | Advisory checks on | The default branch has the policy and an Archfit workflow. | Open the setup page: an owner approves the first policy there, then makes the check required. | | Required checks on | The same, and the policy has `gate: required`. | Check that a branch ruleset requires `archfit/gate`: without it, a fail does not block a merge. | -After writing a new policy, setup shows how it would judge up to 30 recent first-parent changes. It also includes the counts in the first setup pull request body. Replay uses the local remote-tracking default branch, does not fetch PR labels or start CI, and stops after 60 seconds. If the local ref is unavailable, run `archfit replay` later. +After writing a new policy, setup shows how it would judge up to 30 recent first-parent changes. It also includes the counts in the first setup pull request body. Replay uses the local remote-tracking default branch, does not fetch pull request labels or start CI, and stops after 60 seconds. If the local ref is unavailable, run `archfit replay` later. The replay line names up to three of the changes to show an owner, newest first, each with the status an owner would see on it, for example `1 to show an owner (#1: unknown)`. Setup asks GitHub for no labels, so it takes the pull request number from the commit subject (`(#12)` or `Merge pull request #12`), or else names the short commit id. -Before a commit, setup prints the files it commits on `archfit/setup` and says that it pushes the branch. It does not ask first. +Before a commit, setup runs the check against the remote default branch. A `fail` does not stop setup. +An `unknown` stops setup before the commit and shows the check's next step. +Then setup prints the files it commits on `archfit/setup` and says that it pushes the branch. It does not ask first. The first run, which writes the policy, also says that the next run commits the Claude Code and Codex hook files. To leave them out, run that next `archfit setup` with `--no-hooks`: once they are committed on `archfit/setup`, `--no-hooks` stops until you delete the branch. -Then setup runs the check against the remote default branch. A `fail` does not stop setup. -An `unknown` stops setup before the commit and shows the check's next step. Setup commits only changed setup paths, but includes any existing edits in those paths. Review the pull request before merging. A candidate build, which pins the Action by commit, adds one sentence to the pull request body that explains its `# candidate` pin. diff --git a/internal/application/replay/replay_test.go b/internal/application/replay/replay_test.go index b5750f52..c93d40e7 100644 --- a/internal/application/replay/replay_test.go +++ b/internal/application/replay/replay_test.go @@ -303,16 +303,17 @@ func TestRunNamesUpToThreeChangesToLookAt(t *testing.T) { } func TestChangeName(t *testing.T) { + const fullCommit = "abcdef0123" cases := []struct { name string c replay.Change want string }{ - {"pull request from GitHub", replay.Change{Commit: "abcdef0123", PR: 7, Subject: "Fix (#3)"}, "#7"}, - {"squash subject", replay.Change{Commit: "abcdef0123", Subject: "Add the tc shaper (#431)"}, "#431"}, - {"merge subject", replay.Change{Commit: "abcdef0123", Subject: "Merge pull request #12 from a/feature"}, "#12"}, - {"number inside the subject", replay.Change{Commit: "abcdef0123", Subject: "Revert (#5) and more"}, "abcdef0"}, - {"no number", replay.Change{Commit: "abcdef0123", Subject: "Initial commit"}, "abcdef0"}, + {"pull request from GitHub", replay.Change{Commit: fullCommit, PR: 7, Subject: "Fix (#3)"}, "#7"}, + {"squash subject", replay.Change{Commit: fullCommit, Subject: "Add the tc shaper (#431)"}, "#431"}, + {"merge subject", replay.Change{Commit: fullCommit, Subject: "Merge pull request #12 from a/feature"}, "#12"}, + {"number inside the subject", replay.Change{Commit: fullCommit, Subject: "Revert (#5) and more"}, "abcdef0"}, + {"no number", replay.Change{Commit: fullCommit, Subject: "Initial commit"}, "abcdef0"}, {"short commit", replay.Change{Commit: "abc"}, "abc"}, } for _, tc := range cases { diff --git a/internal/application/setup/run_test.go b/internal/application/setup/run_test.go index 00e4ef7e..7182799f 100644 --- a/internal/application/setup/run_test.go +++ b/internal/application/setup/run_test.go @@ -169,6 +169,9 @@ func TestSetupStopsWithoutPublishing(t *testing.T) { out := Run(t.Context(), deps) assertOutcome(t, out, Blocked, 2, tc.next, tc.line) assertActions(t, f.actions, actions...) + if text := strings.Join(out.Lines, "\n"); strings.Contains(text, "setup commits") { + t.Errorf("a stop before the commit says setup commits:\n%s", text) + } } }) } @@ -444,7 +447,7 @@ func TestCandidateActionPin(t *testing.T) { } // TestSetupSaysWhatItCommits: the run that commits names the files and the -// branch before the check, and the run before it says how to leave out the +// branch after the check and before the commit, and the run before it says how to leave out the // hooks, while --no-hooks can still do that. func TestSetupSaysWhatItCommits(t *testing.T) { hooks := []string{".claude/settings.json", ".codex/hooks.json"} @@ -455,7 +458,7 @@ func TestSetupSaysWhatItCommits(t *testing.T) { committed string }{ {"with hooks", hooks, - "the next archfit setup commits the setup files, with .claude/settings.json and .codex/hooks.json, on archfit/setup and pushes the branch; archfit setup --no-hooks leaves out the hook files", + "the next archfit setup commits the setup files, with .claude/settings.json and .codex/hooks.json, on archfit/setup and pushes the branch; add --no-hooks to that run to leave out the hook files", "setup commits .archfit.yaml, .github/workflows/archfit.yaml, .claude/settings.json, .codex/hooks.json on archfit/setup and pushes the branch"}, {"no hooks", nil, "", "setup commits .archfit.yaml, .github/workflows/archfit.yaml on archfit/setup and pushes the branch"}, } { @@ -470,12 +473,9 @@ func TestSetupSaysWhatItCommits(t *testing.T) { t.Errorf("missing %q in:\n%s", tc.firstRun, first) } second := strings.Join(Run(t.Context(), deps).Lines, "\n") - says, check := strings.Index(second, tc.committed), strings.Index(second, "check: fail against origin/main") - if says < 0 || check < says { - t.Errorf("want %q before the check in:\n%s", tc.committed, second) - } - if !strings.Contains(second, "committed as Setup Author ") { - t.Errorf("missing the commit line in:\n%s", second) + check, says, done := strings.Index(second, "check: fail against origin/main"), strings.Index(second, tc.committed), strings.Index(second, "committed as Setup Author ") + if check < 0 || says < check || done < says { + t.Errorf("want the check, then %q, then the commit, in:\n%s", tc.committed, second) } for _, text := range []string{first, second} { if words := testutil.StoryNotUsedIn(text); len(words) > 0 { diff --git a/internal/application/setup/setup.go b/internal/application/setup/setup.go index 99fef5a1..8b71d673 100644 --- a/internal/application/setup/setup.go +++ b/internal/application/setup/setup.go @@ -405,8 +405,8 @@ func Run(ctx context.Context, d Dependencies) Outcome { // The run that commits prints its lines only at its end, so this is // the last point where --no-hooks keeps the hooks out of the branch. if hooks := d.Files.HookPaths(); len(hooks) > 0 { - out.add("the next %s commits the setup files, with %s, on %s and pushes the branch; %s --no-hooks leaves out the hook files", - Command, strings.Join(hooks, " and "), Branch, Command) + out.add("the next %s commits the setup files, with %s, on %s and pushes the branch; add --no-hooks to that run to leave out the hook files", + Command, strings.Join(hooks, " and "), Branch) } default: publish(ctx, d, s, &out) @@ -520,7 +520,6 @@ func commit(ctx context.Context, d Dependencies, s *Detection, out *Outcome, pat out.stop(errors.New("git author name or email is missing"), "set git user.name and user.email, then archfit setup") return false } - out.add("setup commits %s on %s and pushes the branch", strings.Join(paths, ", "), Branch) base := "origin/" + s.Default check, err := d.Check(ctx, base) if err != nil { @@ -535,6 +534,7 @@ func commit(ctx context.Context, d Dependencies, s *Detection, out *Outcome, pat } return false } + out.add("setup commits %s on %s and pushes the branch", strings.Join(paths, ", "), Branch) if s.branch != Branch { if err := d.Git.Switch(ctx, Branch, s.Default); err != nil { out.stop(err, "sync the default branch or switch to "+Branch+", then archfit setup") diff --git a/internal/application/setup/setup_test.go b/internal/application/setup/setup_test.go index c0605e7d..dda5ab03 100644 --- a/internal/application/setup/setup_test.go +++ b/internal/application/setup/setup_test.go @@ -77,7 +77,7 @@ func TestBody(t *testing.T) { if strings.Contains(bare, "## Last 30") || strings.Contains(bare, "## For the owner") || strings.Contains(bare, "candidate") { t.Errorf("empty sections rendered: %q", bare) } - full := Body("0.10.0", InstallURL, "candidate", &ReplaySummary{Passed: 22, NeededOwner: 5, ToShowOwner: 3, ToShow: []string{"#12: fail"}}, []string{"exception legacy: expires 2027-02-20"}) + full := Body("0.10.0", InstallURL, "candidate", &ReplaySummary{Passed: 22, NeededOwner: 5, ToShowOwner: 3, ToShow: []string{toShowFail}}, []string{"exception legacy: expires 2027-02-20"}) for _, section := range []string{ "advisory: nothing blocks a merge until an owner makes the check required.\nThe workflow pins a candidate build of the Action: a commit made for testing before a release, marked `# candidate` instead of a release tag.\n", "## Last 30 merged pull requests\n\n22 passed, 5 needed an owner, 3 to show an owner (#12: fail, 2 more). Read them", @@ -89,6 +89,9 @@ func TestBody(t *testing.T) { } } +// toShowFail names a change to show an owner and what the owner sees. +const toShowFail = "#12: fail" + func TestReplayCountsNameTheChangesToShow(t *testing.T) { for _, tc := range []struct { name string @@ -97,9 +100,9 @@ func TestReplayCountsNameTheChangesToShow(t *testing.T) { }{ {"none to show", ReplaySummary{Passed: 3}, "3 passed, 0 needed an owner, 0 to show an owner"}, {"one", ReplaySummary{Passed: 2, ToShowOwner: 1, ToShow: []string{"#1: unknown"}}, "2 passed, 0 needed an owner, 1 to show an owner (#1: unknown)"}, - {"three", ReplaySummary{ToShowOwner: 3, ToShow: []string{"#12: fail", "#11: unknown", "4f2a9c1: unknown"}}, + {"three", ReplaySummary{ToShowOwner: 3, ToShow: []string{toShowFail, "#11: unknown", "4f2a9c1: unknown"}}, "0 passed, 0 needed an owner, 3 to show an owner (#12: fail, #11: unknown, 4f2a9c1: unknown)"}, - {"more than three", ReplaySummary{NeededOwner: 1, ToShowOwner: 5, ToShow: []string{"#12: fail", "#11: unknown", "#9: waiting"}}, + {"more than three", ReplaySummary{NeededOwner: 1, ToShowOwner: 5, ToShow: []string{toShowFail, "#11: unknown", "#9: waiting"}}, "0 passed, 1 needed an owner, 5 to show an owner (#12: fail, #11: unknown, #9: waiting, 2 more)"}, } { t.Run(tc.name, func(t *testing.T) {