gunk-dev/the-valley

versionchecks.go

source6bf8ca46b311main's published tip history
12.5 KiB
1package main
2
3// The check state valley changes --checks and valley pending --checks
4// list for the active version of each ready change and each draft that
5// has not landed as any version: the checks the policy requires of it,
6// and for each the state of the note the integrator would judge it by.
7// valley pending reads a draft's only when it lists the drafts, with
8// --drafts, and valley review reads it for the change it shows.
9//
10// A draft's checks are read exactly as a ready change's. The required
11// checks are derived as the check worker derives them (worker.go): over
12// the head's merge base with main, under both policy layers read at
13// main's tip on origin. Each nix check's note is read as coverage reads
14// it (coverage.go), from origin's attestation refs for the head's tree,
15// verified under the host's published known-signers file. A note is
16// about a tree, not a change's state, so a draft made ready at the same
17// head lists the same states:
18//
19// - passed: the note judged is one coverage would reuse;
20// - failed: the note judged verifies, is signed by a signer who counts
21// in this project, and coverage would not reuse it, because it
22// records the check as failed or because it is the wrong kind, runner
23// or attribute. why says which;
24// - pending: no note about the check verifies and is signed by a signer
25// who counts. why says why a note origin holds does not count, when
26// one is there.
27//
28// A check with a runner other than nix is listed as other, since the host
29// worker does not run it, and it does not count toward the overall state.
30// VALLEY_KNOWN_SIGNERS does not narrow what is read here: the listing says
31// what the integrator would judge.
32//
33// It is behind --checks because it costs each repository a read of the
34// policy with cue and an attest verify for each note. The reads are
35// batched by repository, over the ready changes and drafts together: the
36// policy is read once, without asking cue which requirements are
37// mandatory, since nothing here shows or decides by a pin; origin's
38// attestation refs for every head are listed in one ls-remote and fetched
39// in one fetch; and the host's signers are read once. A read that fails
40// leaves the changes it was for without check state, and says so on
41// stderr; it never reads as no evidence.
42//
43// Each nix check also says what it cost the host's check worker to build
44// for this version, against what it recently cost (checkcost.go), when
45// the host's worker telemetry can be read. That read runs while the
46// policy is read. When it cannot be read, the fields are left out, stderr
47// says why once, and the check states are listed all the same.
48
49import (
50 "fmt"
51 "io"
52 "os"
53 "strings"
54
55 "the-valley/change"
56)
57
58// The check states of a version's checks.
59const (
60 checkPassed = "passed"
61 checkFailed = "failed"
62 checkPending = "pending"
63 checkOther = "other"
64)
65
66// jsonVersionChecks is a version's check state, as valley-changes/v1
67// writes it under checks.
68type jsonVersionChecks struct {
69 State string `json:"state"`
70 Tree string `json:"tree"`
71 Required []jsonCheckState `json:"required"`
72 // BuildMS is the sum of the build times of the checks built for this
73 // version, and EvalMS the sum of the evaluations of the runs that
74 // built them, when the worker's telemetry holds any (checkcost.go).
75 BuildMS *int64 `json:"build_ms,omitempty"`
76 EvalMS *int64 `json:"eval_ms,omitempty"`
77}
78
79// jsonCheckState is one required check and the state of its note.
80type jsonCheckState struct {
81 Name string `json:"name"`
82 State string `json:"state"`
83 Signer string `json:"signer,omitempty"`
84 Why string `json:"why,omitempty"`
85 // What the check cost to build, from the worker's telemetry
86 // (checkcost.go): this version's build, whether its output was
87 // cached, the median of the check's recent builds, whether it has
88 // none, and whether this build grew against them.
89 BuildMS *int64 `json:"build_ms,omitempty"`
90 Cached *bool `json:"cached,omitempty"`
91 BaselineMS *int64 `json:"baseline_ms,omitempty"`
92 New bool `json:"new,omitempty"`
93 // OtherCapacity says the check is new because its builds so far ran on
94 // other capacity.
95 OtherCapacity bool `json:"other_capacity,omitempty"`
96 Grown bool `json:"grown,omitempty"`
97}
98
99// unlandedDraft is a draft that has not landed as any version.
100func unlandedDraft(s *snapshot, n recorded) bool {
101 return n.record.State == change.Draft && !s.isLanded(n)
102}
103
104// addChecks gives the active version of each pending change in view its
105// check state, and, with drafts, that of each draft in view that has not
106// landed, whether view lists it in changes or in drafts. Each also gets
107// its build times from the host's worker telemetry, read through c, or
108// none when c is nil. view is this repository's.
109func (l listed) addChecks(view *jsonRepo, drafts bool, c *costs, stderr io.Writer) {
110 if l.err != nil {
111 return
112 }
113 wanted := map[[2]string]bool{}
114 for _, n := range l.snap.all {
115 if pendingChange(l.snap, n) || drafts && unlandedDraft(l.snap, n) {
116 wanted[[2]string{n.record.Target, n.id}] = true
117 }
118 }
119 lists := []*[]jsonChange{&view.Changes}
120 if view.Drafts != nil {
121 lists = append(lists, view.Drafts)
122 }
123 var heads []*jsonVersion
124 changeOf := map[*jsonVersion]string{}
125 for _, list := range lists {
126 for i := range *list {
127 ch := &(*list)[i]
128 if wanted[[2]string{ch.Target, ch.ChangeID}] {
129 heads = append(heads, &ch.Versions[ch.Active-1])
130 changeOf[heads[len(heads)-1]] = ch.ChangeID
131 }
132 }
133 }
134 if len(heads) == 0 {
135 return
136 }
137 warn := func(format string, args ...any) {
138 fmt.Fprintf(stderr, "valley: %s: %s\n", l.name, fmt.Sprintf(format, args...))
139 }
140 if l.snap.tip == "" {
141 warn("origin has no main to read the policy at, so no change lists its checks")
142 return
143 }
144 var reading *costRead
145 if c != nil {
146 reading = c.start(l.g)
147 }
148 if _, err := l.snap.layers(); err != nil {
149 warn("the policy at main did not read, so no change lists its checks: %v", err)
150 return
151 }
152 if _, err := schemaFile(l.g, ""); err != nil {
153 warn("%v, so no change lists its checks", err)
154 return
155 }
156 model, err := l.snap.model()
157 if err != nil {
158 warn("the policy at main did not read, so no change lists its checks: %v", err)
159 return
160 }
161 repo := l.g.dir
162 if repo == "" {
163 repo = "."
164 }
165
166 // Each head's required checks and tree digest.
167 type head struct {
168 v *jsonVersion
169 change string
170 required []jsonRequired
171 asked []nixCheck
172 digest string
173 }
174 var read []head
175 var patterns []string
176 for _, v := range heads {
177 say := func(format string, args ...any) {
178 warn("v%d of %s lists no checks: %s", v.N, v.Head, fmt.Sprintf(format, args...))
179 }
180 base, err := l.g.output(true, "merge-base", l.snap.tip, v.Head)
181 if err != nil {
182 say("it shares no history with main")
183 continue
184 }
185 doc, err := deriveUnder(l.g, model, base, v.Head)
186 if err != nil {
187 say("its checks did not derive: %v", err)
188 continue
189 }
190 digest, err := treeDigest(l.g, repo, v.Head)
191 if err != nil {
192 say("%v", err)
193 continue
194 }
195 h := head{v: v, change: changeOf[v], required: doc.Required, digest: digest}
196 for _, q := range doc.Required {
197 if q.Runner == "nix" {
198 h.asked = append(h.asked, nixCheck{q.Name, q.Attribute})
199 }
200 }
201 read = append(read, h)
202 patterns = append(patterns, attestationRefs+digest+"/*")
203 }
204 if len(read) == 0 {
205 return
206 }
207
208 // The evidence origin holds for every head, listed and fetched at once.
209 o, err := listOrigin(l.g, patterns...)
210 if err != nil {
211 warn("could not list the evidence origin holds, so no change lists its checks")
212 return
213 }
214 var oids []string
215 for _, oid := range o.refs {
216 oids = append(oids, oid)
217 }
218 if len(oids) > 0 {
219 if err := fetch(l.g, oids...); err != nil {
220 warn("could not fetch the evidence origin holds, so no change lists its checks")
221 return
222 }
223 }
224 keys, p, unsigned := hostSigners(l.g)
225 if unsigned == "" {
226 defer os.Remove(keys)
227 }
228 var here counting
229 if p != nil {
230 here = counting{project: project(l.g), integrator: p.name, projects: p.projects}
231 }
232 var builds []checkBuild
233 timed := false
234 if c != nil {
235 builds, timed = c.wait(reading, warn)
236 }
237
238 for _, h := range read {
239 stored := storedFor(h.digest, o.refs)
240 var judged map[string]judgment
241 unjudged := unsigned
242 if unjudged == "" && len(stored) > 0 && len(h.asked) > 0 {
243 judged, unjudged, err = judge(l.g, repo, h.v.Head, keys, "", here, stored, o.refs, h.asked)
244 if err != nil {
245 warn("v%d of %s lists no checks: %v", h.v.N, h.v.Head, err)
246 continue
247 }
248 }
249 h.v.Checks = checkStates(h.digest, h.required, judged, unsigned, unjudged)
250 if timed {
251 addCosts(h.v.Checks, h.required, l.name, h.change, h.v.N, builds)
252 }
253 }
254}
255
256// checkStates is a version's check state, from the note judged for each
257// nix check. unsigned says why no note could be verified, and unjudged why
258// the integrator cannot judge the tree's evidence at all.
259func checkStates(digest string, required []jsonRequired, judged map[string]judgment, unsigned, unjudged string) *jsonVersionChecks {
260 out := &jsonVersionChecks{State: checkPassed, Tree: digest, Required: []jsonCheckState{}}
261 for _, q := range required {
262 s := jsonCheckState{Name: q.Name}
263 j, ok := judged[q.Name]
264 switch {
265 case q.Runner != "nix":
266 s.State = checkOther
267 case unsigned != "":
268 s.State, s.Why = checkPending, unsigned
269 case unjudged != "":
270 s.State, s.Why = checkFailed, unjudged+", so the integrator cannot judge this tree's evidence"
271 case !ok:
272 s.State = checkPending
273 case j.note.refusal != "":
274 s.State, s.Why = checkPending, j.note.refusal
275 case j.why != "":
276 s.State, s.Signer, s.Why = checkFailed, j.note.signer, j.why
277 default:
278 s.State, s.Signer = checkPassed, j.note.signer
279 }
280 switch {
281 case s.State == checkFailed:
282 out.State = checkFailed
283 case s.State == checkPending && out.State == checkPassed:
284 out.State = checkPending
285 }
286 out.Required = append(out.Required, s)
287 }
288 return out
289}
290
291// checksLine is valley pending's line for a version's check state, with
292// what its checks cost to build when that is known (costLine):
293// "checks: 18 passed, 1 failed (valley-request) in 9m10s (eval 24s)".
294func checksLine(c *jsonVersionChecks) string {
295 if len(c.Required) == 0 {
296 return "checks: none required"
297 }
298 count := map[string]int{}
299 var failed []string
300 for _, q := range c.Required {
301 count[q.State]++
302 if q.State == checkFailed {
303 failed = append(failed, q.Name)
304 }
305 }
306 var parts []string
307 for _, state := range []string{checkPassed, checkFailed, checkPending, checkOther} {
308 if count[state] == 0 {
309 continue
310 }
311 part := fmt.Sprintf("%d %s", count[state], state)
312 if state == checkFailed {
313 part += " (" + strings.Join(failed, ", ") + ")"
314 }
315 parts = append(parts, part)
316 }
317 return "checks: " + strings.Join(parts, ", ") + costLine(c)
318}
319
320// runCheckLine prints valley pending's checks line for the active version
321// of each change named, for review to show above its diff, one line each:
322//
323// <change ID> checks: 21 passed in 9m10s (eval 24s)
324//
325// A ready change and a draft each have one. A withdrawn change, or one
326// that has landed as any version, has no check state, and so no line. What could not be read is said on stderr,
327// as valley pending --checks says it, and leaves the line out.
328func runCheckLine(g git, args []string, stdout, stderr io.Writer) error {
329 for _, a := range args {
330 if !change.ValidID(a) {
331 return failure("usage: valley checkline <change ID>...")
332 }
333 }
334 if !g.succeeds(true, "rev-parse", "--git-dir") {
335 return failure("not a git repository")
336 }
337 changes, err := activeChecks(g, args, newCosts(), stderr)
338 if err != nil {
339 return err
340 }
341 var b strings.Builder
342 for _, c := range changes {
343 if v := c.Versions[c.Active-1]; v.Checks != nil {
344 fmt.Fprintf(&b, "%s\t%s\n", c.ChangeID, readable(checksLine(v.Checks)))
345 }
346 }
347 _, err = io.WriteString(stdout, b.String())
348 return err
349}
350
351// activeChecks is each change named that is ready or a draft and has not
352// landed, in this repository, with its active version's check state, as
353// valley pending --drafts --checks reads it, and with build times read
354// through c when it is given.
355func activeChecks(g git, ids []string, c *costs, stderr io.Writer) ([]jsonChange, error) {
356 wanted := map[string]bool{}
357 for _, id := range ids {
358 wanted[id] = true
359 }
360 list, _, err := sources(g, listing{}, stderr)
361 if err != nil {
362 return nil, err
363 }
364 repos := readSources(list, newVerifier())
365 defer closeSources(repos)
366 r := repos[0]
367 if r.err != nil {
368 return nil, r.err
369 }
370 named := func(s *snapshot, n recorded) bool {
371 return wanted[n.id] && (pendingChange(s, n) || unlandedDraft(s, n))
372 }
373 view := r.repoView(handles(repos), named, unlanded)
374 r.addChecks(&view, true, c, stderr)
375 return view.Changes, nil
376}