gunk-dev/the-valley

versionchecks.go

source7309e1d6b8bbmain's published tip history
11.0 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. 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 reading := c.start(l.g)
125 if _, err := l.snap.layers(); err != nil {
126 warn("the policy at main did not read, so no ready change lists its checks: %v", err)
127 return
128 }
129 if _, err := schemaFile(l.g, ""); err != nil {
130 warn("%v, so no ready change lists its checks", err)
131 return
132 }
133 model, err := l.snap.model(true)
134 if err != nil {
135 warn("the policy at main did not read, so no ready change lists its checks: %v", err)
136 return
137 }
138 repo := l.g.dir
139 if repo == "" {
140 repo = "."
141 }
142
143 // Each head's required checks and tree digest.
144 type head struct {
145 v *jsonVersion
146 change string
147 required []jsonRequired
148 asked []nixCheck
149 digest string
150 }
151 var read []head
152 var patterns []string
153 for _, v := range heads {
154 say := func(format string, args ...any) {
155 warn("v%d of %s lists no checks: %s", v.N, v.Head, fmt.Sprintf(format, args...))
156 }
157 base, err := l.g.output(true, "merge-base", l.snap.tip, v.Head)
158 if err != nil {
159 say("it shares no history with main")
160 continue
161 }
162 doc, err := deriveUnder(l.g, model, base, v.Head)
163 if err != nil {
164 say("its checks did not derive: %v", err)
165 continue
166 }
167 digest, err := treeDigest(l.g, repo, v.Head)
168 if err != nil {
169 say("%v", err)
170 continue
171 }
172 h := head{v: v, change: changeOf[v], required: doc.Required, digest: digest}
173 for _, q := range doc.Required {
174 if q.Runner == "nix" {
175 h.asked = append(h.asked, nixCheck{q.Name, q.Attribute})
176 }
177 }
178 read = append(read, h)
179 patterns = append(patterns, attestationRefs+digest+"/*")
180 }
181 if len(read) == 0 {
182 return
183 }
184
185 // The evidence origin holds for every head, listed and fetched at once.
186 o, err := listOrigin(l.g, patterns...)
187 if err != nil {
188 warn("could not list the evidence origin holds, so no ready change lists its checks")
189 return
190 }
191 var oids []string
192 for _, oid := range o.refs {
193 oids = append(oids, oid)
194 }
195 if len(oids) > 0 {
196 if err := fetch(l.g, oids...); err != nil {
197 warn("could not fetch the evidence origin holds, so no ready change lists its checks")
198 return
199 }
200 }
201 keys, p, unsigned := hostSigners(l.g)
202 if unsigned == "" {
203 defer os.Remove(keys)
204 }
205 var here counting
206 if p != nil {
207 here = counting{project: project(l.g), integrator: p.name, projects: p.projects}
208 }
209 builds, timed := c.wait(reading, warn)
210
211 for _, h := range read {
212 stored := storedFor(h.digest, o.refs)
213 var judged map[string]judgment
214 unjudged := unsigned
215 if unjudged == "" && len(stored) > 0 && len(h.asked) > 0 {
216 judged, unjudged, err = judge(l.g, repo, h.v.Head, keys, "", here, stored, o.refs, h.asked)
217 if err != nil {
218 warn("v%d of %s lists no checks: %v", h.v.N, h.v.Head, err)
219 continue
220 }
221 }
222 h.v.Checks = checkStates(h.digest, h.required, judged, unsigned, unjudged)
223 if timed {
224 addCosts(h.v.Checks, h.required, l.name, h.change, h.v.N, builds)
225 }
226 }
227}
228
229// checkStates is a version's check state, from the note judged for each
230// nix check. unsigned says why no note could be verified, and unjudged why
231// the integrator cannot judge the tree's evidence at all.
232func checkStates(digest string, required []jsonRequired, judged map[string]judgment, unsigned, unjudged string) *jsonVersionChecks {
233 out := &jsonVersionChecks{State: checkPassed, Tree: digest, Required: []jsonCheckState{}}
234 for _, q := range required {
235 s := jsonCheckState{Name: q.Name}
236 j, ok := judged[q.Name]
237 switch {
238 case q.Runner != "nix":
239 s.State = checkOther
240 case unsigned != "":
241 s.State, s.Why = checkPending, unsigned
242 case unjudged != "":
243 s.State, s.Why = checkFailed, unjudged+", so the integrator cannot judge this tree's evidence"
244 case !ok:
245 s.State = checkPending
246 case j.note.refusal != "":
247 s.State, s.Why = checkPending, j.note.refusal
248 case j.why != "":
249 s.State, s.Signer, s.Why = checkFailed, j.note.signer, j.why
250 default:
251 s.State, s.Signer = checkPassed, j.note.signer
252 }
253 switch {
254 case s.State == checkFailed:
255 out.State = checkFailed
256 case s.State == checkPending && out.State == checkPassed:
257 out.State = checkPending
258 }
259 out.Required = append(out.Required, s)
260 }
261 return out
262}
263
264// checksLine is valley pending's line for a version's check state, with
265// what its checks cost to build when that is known (costLine):
266// "checks: 18 passed, 1 failed (valley-request) in 9m10s (eval 24s)".
267func checksLine(c *jsonVersionChecks) string {
268 if len(c.Required) == 0 {
269 return "checks: none required"
270 }
271 count := map[string]int{}
272 var failed []string
273 for _, q := range c.Required {
274 count[q.State]++
275 if q.State == checkFailed {
276 failed = append(failed, q.Name)
277 }
278 }
279 var parts []string
280 for _, state := range []string{checkPassed, checkFailed, checkPending, checkOther} {
281 if count[state] == 0 {
282 continue
283 }
284 part := fmt.Sprintf("%d %s", count[state], state)
285 if state == checkFailed {
286 part += " (" + strings.Join(failed, ", ") + ")"
287 }
288 parts = append(parts, part)
289 }
290 return "checks: " + strings.Join(parts, ", ") + costLine(c)
291}
292
293// runCheckLine prints valley pending's checks line for the active version
294// of each change named, for review to show above its diff, one line each:
295//
296// <change ID> checks: 21 passed in 9m10s (eval 24s)
297//
298// A change that is not ready, or whose active version has landed, has no
299// check state, and so no line. What could not be read is said on stderr,
300// as valley pending --checks says it, and leaves the line out.
301func runCheckLine(g git, args []string, stdout, stderr io.Writer) error {
302 wanted := map[string]bool{}
303 for _, a := range args {
304 if !change.ValidID(a) {
305 return failure("usage: valley-go checkline <change ID>...")
306 }
307 wanted[a] = true
308 }
309 if !g.succeeds(true, "rev-parse", "--git-dir") {
310 return failure("not a git repository")
311 }
312 list, _, err := sources(g, listing{}, stderr)
313 if err != nil {
314 return err
315 }
316 repos := readSources(list, newVerifier())
317 defer closeSources(repos)
318 r := repos[0]
319 if r.err != nil {
320 return r.err
321 }
322 named := func(s *snapshot, n recorded) bool { return wanted[n.id] && pendingChange(s, n) }
323 view := r.repoView(handles(repos), named, unlanded)
324 r.addChecks(&view, newCosts(), stderr)
325 var b strings.Builder
326 for _, c := range view.Changes {
327 if v := c.Versions[c.Active-1]; v.Checks != nil {
328 fmt.Fprintf(&b, "%s\t%s\n", c.ChangeID, readable(checksLine(v.Checks)))
329 }
330 }
331 _, err = io.WriteString(stdout, b.String())
332 return err
333}