gunk-dev/the-valley

mainstate.go

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