gunk-dev/the-valley

mainstate.go

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