gunk-dev/the-valley

failed_checks.go

source6a6791929c36main's published tip history
5.1 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 flow 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
23flow 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 flow does nothing.
30A change is flow's when its record's owner is flow's principal, the
31repo's git config valley.principal.
32
33For each failed head, flow:
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 'valley flow 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 flow 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; valley flow status and the dashboard show the checks in the CI
56column. Prints one line per change flow 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 views, err := checksPass(cmd.Context(), hds, nil, true)
68 if err != nil {
69 return err
70 }
71 if len(views) == 0 {
72 fmt.Println("No failed checks on ready changes flow owns.")
73 }
74 for _, v := range views {
75 fmt.Println(v)
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 with act dispatching a fix run for each failed head of a change klaus
88// owns. A pass that doesn't act only reads.
89func checksPass(ctx context.Context, store *run.HomeDirStore, listed *valley.Changes, act bool) ([]checksfix.View, error) {
90 states, err := store.List()
91 if err != nil {
92 return nil, err
93 }
94 root, _ := git.RepoRoot()
95 repos := restack.Repos(states, root)
96 if len(repos) == 0 {
97 return nil, nil
98 }
99 if act {
100 unlock, err := checksfix.Lock(store.BaseDir())
101 if err != nil {
102 return nil, err
103 }
104 defer unlock()
105 }
106 // The ledger holds the attempts: a pass that can't read it dispatches
107 // nothing, rather than start them over.
108 ledger, err := checksfix.LoadLedger(store.BaseDir())
109 if err != nil {
110 return nil, fmt.Errorf("reading the checks ledger: %w", err)
111 }
112 events := event.NewLog(store.BaseDir())
113 all, err := run.ListAllSessions()
114 if err != nil {
115 all = states
116 }
117 d := checksfix.Deps{
118 List: valley.ListRepoChecks,
119 Principal: valley.Principal,
120 NewRunID: run.GenID,
121 Launch: launchChecksFix,
122 Running: restack.RunningOn(all, run.DefaultTmuxDeps()),
123 Now: time.Now,
124 Emit: func(runID, typ string, data map[string]interface{}) {
125 if err := events.Emit(event.New(runID, typ, data)); err != nil {
126 fmt.Fprintf(os.Stderr, "warning: emitting %s: %v\n", typ, err)
127 }
128 },
129 }
130 return checksfix.Pass(ctx, d, ledger, repos, listed, act)
131}
132
133// launchChecksFix launches a fix run with this klaus binary, under the run
134// ID klaus recorded, expecting the change at the owner, branch and head
135// klaus read.
136func launchChecksFix(ctx context.Context, l checksfix.Launch) error {
137 self, err := os.Executable()
138 if err != nil {
139 self = "klaus"
140 }
141 args := []string{"launch"}
142 if l.Project != "" {
143 args = append(args, "--repo", l.Project)
144 }
145 args = append(args, "--change", l.ChangeID, "--run-id", l.RunID,
146 "--expect-owner", l.Owner, "--expect-branch", l.Branch, "--expect-head", l.Head,
147 "--reason", event.DispatchChecksFailed, "--", l.Prompt)
148 return runLaunch(ctx, self, l.Dir, args)
149}