gunk-dev/the-valley

mainstate.go

sourcebc00697b19b5main's published tip history
10.7 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 one
23// attest verify of the notes 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, by the attestation refs
72// listed, or by a listing of its own when listed is nil. It returns no
73// checks when the policy declares no nix follow check.
74func mainFollows(g git, tip string, model *composed.Model, keys *hostKeys, listed map[string]string, stderr io.Writer) (mainWalk, error) {
75 declared := map[string]bool{}
76 for _, c := range model.Classes {
77 for _, name := range c.Follows {
78 declared[name] = true
79 }
80 }
81 var names []string
82 for name := range declared {
83 names = append(names, name)
84 }
85 sort.Strings(names)
86 w := mainWalk{trees: map[string]string{}, ids: map[string]string{}}
87 open := map[string]int{} // each check whose pass the walk has not reached, by name, to its index
88 for _, name := range names {
89 if q := model.Checks[name]; q.Runner == "nix" {
90 open[name] = len(w.checks)
91 w.checks = append(w.checks, followCheck{nixCheck: nixCheck{name, q.Attribute}})
92 }
93 }
94 if len(w.checks) == 0 {
95 return w, nil
96 }
97 out, err := g.output(true, "rev-list", "--first-parent", "--max-count="+strconv.Itoa(followWalk), tip)
98 if err != nil {
99 return mainWalk{}, failure("could not walk main from " + tip)
100 }
101 // One listing of origin's attestation refs, and one read of the host's
102 // signers, serve every tree the walk judges.
103 if listed == nil {
104 o, err := listOrigin(g, attestationRefs+"*")
105 if err != nil {
106 return mainWalk{}, failure("could not list the evidence origin holds")
107 }
108 listed = o.refs
109 }
110 digest := func(_, commit string) (string, error) { return rememberedDigest(g, commit) }
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, listed, asked, keys, digest, 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 for no change, as its telemetry
194// records it: its start, or its end. It is a follow run on a main tip,
195// or a check request's batch, as its class says.
196type mainRun struct {
197 project, tree, run string
198 checks []string
199 ended bool
200 class string // "follow" or "request", or "" from a worker that names no class
201}
202
203// follow reports whether the run is a follow run. A worker that named no
204// class ran no request, so a run with no class is a follow run.
205func (r mainRun) follow() bool { return r.class != classNames[classRequest] }
206
207// mainLines is a project's main state as valley pending's first lines,
208// the first led by lead: the state, and a see: line when a check failed.
209// h names the landings covered, and builds and runs are the worker's
210// telemetry, newest first. A check's build counts by the tree it is
211// about: one at a tree the walk read is a run on main, whichever batch
212// built it, and any other is not. A check.result names no batch class,
213// and the batch records that do can fall outside what was read. Each
214// check is judged tree by tree, by its newest run at each tree, so a run
215// at an older commit's tree, however recent, hides no failure at a newer
216// one.
217func (t terminal) mainLines(w mainWalk, lead, project string, h handleMap, builds []checkBuild, runs []mainRun) []string {
218 index := map[string]int{}
219 for i, c := range w.walked {
220 index[c] = i
221 }
222 ended := map[string]bool{}
223 for _, r := range runs {
224 if r.ended {
225 ended[r.run] = true
226 }
227 }
228 running := map[string]bool{}
229 for _, r := range runs {
230 if r.follow() && !r.ended && !ended[r.run] && r.project == project && r.tree == w.tip {
231 for _, c := range r.checks {
232 running[c] = true
233 }
234 }
235 }
236 var failed, due []string
237 var failedAt string
238 newest := ""
239 for _, q := range w.checks {
240 if q.passed != "" && (newest == "" || index[q.passed] < index[newest]) {
241 newest = q.passed
242 }
243 judged := map[string]bool{} // the trees whose newest run of the check has been read
244 failing := "" // the newest commit whose tree's newest run of the check failed since its pass
245 for _, b := range builds {
246 at, ok := w.trees[b.tree]
247 if b.project != project || b.change != "" || b.check != q.name || !ok || judged[b.tree] {
248 continue
249 }
250 judged[b.tree] = true
251 if b.result != "passed" && (q.passed == "" || index[at] < index[q.passed]) && (failing == "" || index[at] < index[failing]) {
252 failing = at
253 }
254 }
255 if failing != "" {
256 failed = append(failed, q.name)
257 if failedAt == "" || index[failing] < index[failedAt] {
258 failedAt = failing
259 }
260 }
261 if q.due {
262 state := " due"
263 if running[q.name] {
264 state = " running"
265 }
266 due = append(due, q.name+state)
267 }
268 }
269 shown := w.landings(h, project)
270 switch {
271 case len(failed) > 0:
272 line := t.coverage(lead+t.tint(colourRefused, "✗")+" muddy · "+strings.Join(failed, ", ")+" failed at "+short(failedAt), shown)
273 return []string{line, " see: valley telemetry read --source worker"}
274 case len(due) > 0:
275 return []string{t.coverage(lead+t.tint(colourWorking, "◐")+" clearing · "+strings.Join(due, ", "), shown)}
276 case newest != "":
277 return []string{lead + t.tint(colourDone, "✓") + " clear · checks passed at " + short(newest)}
278 }
279 return []string{lead + t.tint(colourDone, "✓") + " clear"}
280}
281
282// coverage is line with the landings it covers, as many as fit the
283// terminal, and how many more there are.
284func (t terminal) coverage(line string, covers []string) string {
285 if len(covers) == 0 {
286 return line
287 }
288 for n := len(covers); n > 0; n-- {
289 s := line + " · covers " + strings.Join(covers[:n], ", ")
290 if n < len(covers) {
291 s += fmt.Sprintf(" and %d more", len(covers)-n)
292 }
293 if n == 1 || columns(s) <= t.width {
294 return s
295 }
296 }
297 return line
298}