gunk-dev/the-valley

versionchecks.go

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