gunk-dev/the-valley

mainstate.go

source1cb0f4ba6ed2main's published tip history
9.2 KiB
1package main
2
3// Main's state: whether the checks that follow a project's classes have
4// passed on main since the landings that set them due.
5//
6// A landing that touches a class sets the checks that follow it due on
7// main, without requiring them of the change (../derive). A follow check
8// is due when a landing since the newest main commit whose tree holds a
9// pass for it touched a class it follows. mainFollows finds that under the
10// policy at main's tip, read without asking which requirements are
11// mandatory (composed.ReadUnpinned), by walking main's first parent from
12// the tip, at most followWalk commits. At each commit it reads the
13// evidence origin holds for the commit's tree, from one listing of
14// origin's attestation refs for the whole walk, judged as coverage judges
15// it (coverage.go),
16// and then derives the commit's own follows from its diff against its
17// first parent, as valley checks does, and the change ID of each landing
18// that sets a check due. A check whose pass the walk does
19// not reach is due when a landing the walk read set it due, and when the
20// walk stopped at its bound rather than at main's first commit, since a
21// landing past the bound may have. The bound keeps a read of main's state
22// to about two seconds however long main is, most of it a tree digest per
23// commit; only follow checks of the nix runner are read.
24//
25// The check worker (worker.go) runs the due checks on main's tip, and
26// valley pending shows the same walk, with the worker's telemetry, as one
27// line (mainLines). Main is clear when no follow check is due, clearing
28// when some are due or running and none failed, and muddy when a follow
29// check's newest run on main failed since its last pass, in its build or
30// its evaluation. The line names the landings the due checks cover as
31// valley pending names a change: by handle, or by short commit ID for a
32// main commit that carries no change ID. Nothing of this
33// is stored: a pass is an attestation on a main tree, and a run and its
34// result are the worker's telemetry (checkcost.go reads it).
35
36import (
37 "cmp"
38 "fmt"
39 "io"
40 "sort"
41 "strconv"
42 "strings"
43
44 "the-valley/composed"
45)
46
47// followWalk is how many of main's commits a walk reads at most.
48const followWalk = 20
49
50// followCheck is one nix check that follows a class of the policy at
51// main's tip, as the walk found it.
52type followCheck struct {
53 nixCheck
54 due bool
55 covers []string // the landings since its newest pass that set it due, newest first
56 passed string // the main commit whose tree holds its newest pass, or "" when the walk reached none
57}
58
59// mainWalk is what mainFollows read of main.
60type mainWalk struct {
61 checks []followCheck // every nix follow check the policy declares, by name
62 walked []string // the commits read, newest first
63 trees map[string]string // the newest commit read at each tree digest
64 ids map[string]string // the change ID each landing in a check's covers carries
65 tip string // the tip's tree digest
66}
67
68// mainFollows walks main from tip under the policy model. It returns no
69// checks when the policy declares no nix follow check.
70func mainFollows(g git, tip string, model *composed.Model, stderr io.Writer) (mainWalk, error) {
71 declared := map[string]bool{}
72 for _, c := range model.Classes {
73 for _, name := range c.Follows {
74 declared[name] = true
75 }
76 }
77 var names []string
78 for name := range declared {
79 names = append(names, name)
80 }
81 sort.Strings(names)
82 w := mainWalk{trees: map[string]string{}, ids: map[string]string{}}
83 open := map[string]int{} // each check whose pass the walk has not reached, by name, to its index
84 for _, name := range names {
85 if q := model.Checks[name]; q.Runner == "nix" {
86 open[name] = len(w.checks)
87 w.checks = append(w.checks, followCheck{nixCheck: nixCheck{name, q.Attribute}})
88 }
89 }
90 if len(w.checks) == 0 {
91 return w, nil
92 }
93 out, err := g.output(true, "rev-list", "--first-parent", "--max-count="+strconv.Itoa(followWalk), tip)
94 if err != nil {
95 return mainWalk{}, failure("could not walk main from " + tip)
96 }
97 // One listing of origin's attestation refs, and one read of the host's
98 // signers, serve every tree the walk judges.
99 o, err := listOrigin(g, attestationRefs+"*")
100 if err != nil {
101 return mainWalk{}, failure("could not list the evidence origin holds")
102 }
103 var keys hostKeys
104 defer keys.done()
105 rooted := false // whether the walk reached main's first commit
106 for _, commit := range strings.Fields(out) {
107 var asked []nixCheck
108 for _, q := range w.checks {
109 if _, ok := open[q.name]; ok {
110 asked = append(asked, q.nixCheck)
111 }
112 }
113 e, err := coverageIn(g, commit, o.refs, asked, &keys, stderr)
114 if err != nil {
115 return mainWalk{}, err
116 }
117 if len(w.walked) == 0 {
118 w.tip = e.digest
119 }
120 w.walked = append(w.walked, commit)
121 if _, ok := w.trees[e.digest]; !ok {
122 w.trees[e.digest] = commit // the newest commit at a tree a revert brought back
123 }
124 for name, i := range open {
125 if _, ok := e.covered[name]; ok {
126 w.checks[i].passed = commit
127 delete(open, name)
128 }
129 }
130 parent, err := g.output(true, "rev-parse", "--verify", "--quiet", commit+"^1")
131 rooted = err != nil // a root commit lands nothing
132 if len(open) == 0 || rooted {
133 break
134 }
135 doc, err := deriveUnder(g, model, parent, commit)
136 if err != nil {
137 return mainWalk{}, err
138 }
139 for _, f := range doc.Follows {
140 if i, ok := open[f.Name]; ok {
141 w.checks[i].due = true
142 w.checks[i].covers = append(w.checks[i].covers, commit)
143 if _, read := w.ids[commit]; !read {
144 // A commit whose IDs do not read is named by its
145 // commit ID, as one that carries none is.
146 header, trailer, _ := headIDs(g, commit)
147 w.ids[commit] = cmp.Or(header, trailer)
148 }
149 }
150 }
151 }
152 if !rooted && len(w.walked) == followWalk {
153 for _, i := range open {
154 w.checks[i].due = true
155 }
156 }
157 return w, nil
158}
159
160// landings are the landings the due checks cover, newest first, each
161// named once as valley pending names a change: by the handle h gives the
162// change ID its commit carries, and otherwise by its short commit ID.
163func (w mainWalk) landings(h handleMap, project string) []string {
164 index := map[string]int{}
165 for i, c := range w.walked {
166 index[c] = i
167 }
168 var covers []string
169 for _, q := range w.checks {
170 covers = append(covers, q.covers...)
171 }
172 sort.SliceStable(covers, func(a, b int) bool { return index[covers[a]] < index[covers[b]] })
173 var named []string
174 for i, c := range covers {
175 if i > 0 && c == covers[i-1] {
176 continue
177 }
178 name := h[handleKey{project, recordTarget, w.ids[c]}]
179 if name == "" {
180 name = short(c)
181 }
182 named = append(named, name)
183 }
184 return named
185}
186
187// mainRun is one batch the worker ran on a main tip, as its telemetry
188// records it: its start, or its end.
189type mainRun struct {
190 project, tree, run string
191 checks []string
192 ended bool
193}
194
195// mainLines is a project's main state as valley pending's first lines,
196// the first led by lead: the state, and a see: line when a check failed.
197// h names the landings covered, and builds and runs are the worker's
198// telemetry, newest first.
199func (t terminal) mainLines(w mainWalk, lead, project string, h handleMap, builds []checkBuild, runs []mainRun) []string {
200 index := map[string]int{}
201 for i, c := range w.walked {
202 index[c] = i
203 }
204 ended := map[string]bool{}
205 for _, r := range runs {
206 if r.ended {
207 ended[r.run] = true
208 }
209 }
210 running := map[string]bool{}
211 for _, r := range runs {
212 if !r.ended && !ended[r.run] && r.project == project && r.tree == w.tip {
213 for _, c := range r.checks {
214 running[c] = true
215 }
216 }
217 }
218 var failed, due []string
219 var failedAt string
220 newest := ""
221 for _, q := range w.checks {
222 if q.passed != "" && (newest == "" || index[q.passed] < index[newest]) {
223 newest = q.passed
224 }
225 for _, b := range builds {
226 if b.project != project || b.change != "" || b.check != q.name {
227 continue
228 }
229 at, ok := w.trees[b.tree]
230 if b.result != "passed" && ok && (q.passed == "" || index[at] < index[q.passed]) {
231 failed = append(failed, q.name)
232 if failedAt == "" || index[at] < index[failedAt] {
233 failedAt = at
234 }
235 }
236 break // the check's newest run on main
237 }
238 if q.due {
239 state := " due"
240 if running[q.name] {
241 state = " running"
242 }
243 due = append(due, q.name+state)
244 }
245 }
246 shown := w.landings(h, project)
247 switch {
248 case len(failed) > 0:
249 line := t.coverage(lead+t.tint(colourRefused, "✗")+" muddy · "+strings.Join(failed, ", ")+" failed at "+short(failedAt), shown)
250 return []string{line, " see: valley telemetry read --source worker"}
251 case len(due) > 0:
252 return []string{t.coverage(lead+t.tint(colourWorking, "◐")+" clearing · "+strings.Join(due, ", "), shown)}
253 case newest != "":
254 return []string{lead + t.tint(colourDone, "✓") + " clear · checks passed at " + short(newest)}
255 }
256 return []string{lead + t.tint(colourDone, "✓") + " clear"}
257}
258
259// coverage is line with the landings it covers, as many as fit the
260// terminal, and how many more there are.
261func (t terminal) coverage(line string, covers []string) string {
262 if len(covers) == 0 {
263 return line
264 }
265 for n := len(covers); n > 0; n-- {
266 s := line + " · covers " + strings.Join(covers[:n], ", ")
267 if n < len(covers) {
268 s += fmt.Sprintf(" and %d more", len(covers)-n)
269 }
270 if n == 1 || columns(s) <= t.width {
271 return s
272 }
273 }
274 return line
275}