gunk-dev/the-valley

versionchecks.go

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