gunk-dev/the-valley

failed_checks.go

source672eff5ffaecmain's published tip history
4.8 KiB
1package cmd
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "time"
8
9 "the-valley/flow/internal/checksfix"
10 "the-valley/flow/internal/event"
11 "the-valley/flow/internal/git"
12 "the-valley/flow/internal/restack"
13 "the-valley/flow/internal/run"
14 "the-valley/flow/internal/valley"
15 "github.com/spf13/cobra"
16)
17
18var failedChecksCmd = &cobra.Command{
19 Use: "failed-checks",
20 Short: "Dispatch runs to fix the valley checks that failed on ready changes klaus owns",
21 Long: `Reads the checks of the ready changes in the valley repos the session's
22runs deliver to, and launches a run to fix each failed head of a change
23klaus owns, as the pipeline dispatches a fix agent for a PR's failing CI.
24The dashboard does this after each integration check, about once a minute;
25run it by hand where no dashboard runs.
26
27The valley lists, on a ready change's active version, where the host's
28check worker stands on its required checks: pending, failed or passed. A
29valley from before checks were listed lists none, and klaus does nothing.
30A change is klaus's when its record's owner is klaus's principal, the
31repo's git config valley.principal.
32
33For each failed head, klaus:
34
35 - does nothing while a run, in any session, is working on the change or
36 its branch;
37 - acts once on each failed head, whatever came of it;
38 - counts a fix run whose change's next version still fails as a failed
39 attempt, and stops after 3 attempts, until the change's checks pass;
40 - records the dispatch, and the ID of the run it is about to launch, in
41 the session's checks-dispatches.json, and launches nothing if it can't;
42 - launches 'klaus launch [--repo <project>] --change <change-id>
43 --run-id <id> --expect-owner <principal> --expect-branch <branch>
44 --expect-head <head> --reason checks-failed <brief>' from a clone of the
45 repo. Launch returns the change to draft before the run starts, and
46 refuses a change that moved since klaus read it.
47
48The brief names the failed checks and what the worker said of each, how to
49read the worker's build output ('valley telemetry --json read --source
50worker --logs'), that the run amends the change's single commit, and that
51it builds every check 'valley checks --names origin/main HEAD' lists before
52it pushes.
53
54The dashboard reports change:checks-failed and change:checks-passed for
55each verdict; klaus status and the dashboard show the checks in the CI
56column. Prints one line per change klaus owns whose checks failed.`,
57 Args: cobra.NoArgs,
58 RunE: func(cmd *cobra.Command, args []string) error {
59 store, err := sessionStore()
60 if err != nil {
61 return err
62 }
63 hds, ok := store.(*run.HomeDirStore)
64 if !ok {
65 return fmt.Errorf("failed-checks needs a session directory")
66 }
67 lines, err := checksPass(cmd.Context(), hds, nil)
68 if err != nil {
69 return err
70 }
71 if len(lines) == 0 {
72 fmt.Println("No failed checks on ready changes klaus owns.")
73 }
74 for _, l := range lines {
75 fmt.Println(l)
76 }
77 return nil
78 },
79}
80
81func init() {
82 rootCmd.AddCommand(failedChecksCmd)
83}
84
85// checksPass runs a checks pass over the valley repos of the session's
86// runs, reading their checks in listed, or with nil, listing each repo,
87// and dispatching a fix run for each failed head of a change klaus owns.
88func checksPass(ctx context.Context, store *run.HomeDirStore, listed *valley.Changes) ([]string, error) {
89 states, err := store.List()
90 if err != nil {
91 return nil, err
92 }
93 root, _ := git.RepoRoot()
94 repos := restack.Repos(states, root)
95 if len(repos) == 0 {
96 return nil, nil
97 }
98 unlock, err := checksfix.Lock(store.BaseDir())
99 if err != nil {
100 return nil, err
101 }
102 defer unlock()
103 // The ledger holds the attempts: a pass that can't read it dispatches
104 // nothing, rather than start them over.
105 ledger, err := checksfix.LoadLedger(store.BaseDir())
106 if err != nil {
107 return nil, fmt.Errorf("reading the checks ledger: %w", err)
108 }
109 all, err := run.ListAllSessions()
110 if err != nil {
111 all = states
112 }
113 d := checksfix.Deps{
114 List: valley.ListRepoChecks,
115 Principal: valley.Principal,
116 NewRunID: run.GenID,
117 Launch: launchChecksFix,
118 Running: restack.RunningOn(all, run.DefaultTmuxDeps()),
119 Now: time.Now,
120 }
121 return checksfix.Pass(ctx, d, ledger, repos, listed, true), nil
122}
123
124// launchChecksFix launches a fix run with this klaus binary, under the run
125// ID klaus recorded, expecting the change at the owner, branch and head
126// klaus read.
127func launchChecksFix(ctx context.Context, l checksfix.Launch) error {
128 self, err := os.Executable()
129 if err != nil {
130 self = "klaus"
131 }
132 args := []string{"launch"}
133 if l.Project != "" {
134 args = append(args, "--repo", l.Project)
135 }
136 args = append(args, "--change", l.ChangeID, "--run-id", l.RunID,
137 "--expect-owner", l.Owner, "--expect-branch", l.Branch, "--expect-head", l.Head,
138 "--reason", event.DispatchChecksFailed, "--", l.Prompt)
139 return runLaunch(ctx, self, l.Dir, args)
140}