gunk-dev/the-valley

versionchecks.go

sourceb1cafbe3a404main'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 instance, layer, done, err := policyLayers(l.g, l.snap.tip)
126 defer done()
127 if err != nil {
128 warn("the policy at main did not read, so no ready change lists its checks: %v", err)
129 return
130 }
131 schema, err := schemaFile(l.g, "")
132 if err != nil {
133 warn("%v, so no ready change lists its checks", err)
134 return
135 }
136 model, err := policyModel(schema, instance, layer)
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 builds, timed := c.wait(reading, warn)
213
214 for _, h := range read {
215 stored := storedFor(h.digest, o.refs)
216 var judged map[string]judgment
217 unjudged := unsigned
218 if unjudged == "" && len(stored) > 0 && len(h.asked) > 0 {
219 judged, unjudged, err = judge(l.g, repo, h.v.Head, keys, "", here, stored, o.refs, h.asked)
220 if err != nil {
221 warn("v%d of %s lists no checks: %v", h.v.N, h.v.Head, err)
222 continue
223 }
224 }
225 h.v.Checks = checkStates(h.digest, h.required, judged, unsigned, unjudged)
226 if timed {
227 addCosts(h.v.Checks, h.required, l.name, h.change, h.v.N, builds)
228 }
229 }
230}
231
232// checkStates is a version's check state, from the note judged for each
233// nix check. unsigned says why no note could be verified, and unjudged why
234// the integrator cannot judge the tree's evidence at all.
235func checkStates(digest string, required []jsonRequired, judged map[string]judgment, unsigned, unjudged string) *jsonVersionChecks {
236 out := &jsonVersionChecks{State: checkPassed, Tree: digest, Required: []jsonCheckState{}}
237 for _, q := range required {
238 s := jsonCheckState{Name: q.Name}
239 j, ok := judged[q.Name]
240 switch {
241 case q.Runner != "nix":
242 s.State = checkOther
243 case unsigned != "":
244 s.State, s.Why = checkPending, unsigned
245 case unjudged != "":
246 s.State, s.Why = checkFailed, unjudged+", so the integrator cannot judge this tree's evidence"
247 case !ok:
248 s.State = checkPending
249 case j.note.refusal != "":
250 s.State, s.Why = checkPending, j.note.refusal
251 case j.why != "":
252 s.State, s.Signer, s.Why = checkFailed, j.note.signer, j.why
253 default:
254 s.State, s.Signer = checkPassed, j.note.signer
255 }
256 switch {
257 case s.State == checkFailed:
258 out.State = checkFailed
259 case s.State == checkPending && out.State == checkPassed:
260 out.State = checkPending
261 }
262 out.Required = append(out.Required, s)
263 }
264 return out
265}
266
267// checksLine is valley pending's line for a version's check state, with
268// what its checks cost to build when that is known (costLine):
269// "checks: 18 passed, 1 failed (valley-request) in 9m10s (eval 24s)".
270func checksLine(c *jsonVersionChecks) string {
271 if len(c.Required) == 0 {
272 return "checks: none required"
273 }
274 count := map[string]int{}
275 var failed []string
276 for _, q := range c.Required {
277 count[q.State]++
278 if q.State == checkFailed {
279 failed = append(failed, q.Name)
280 }
281 }
282 var parts []string
283 for _, state := range []string{checkPassed, checkFailed, checkPending, checkOther} {
284 if count[state] == 0 {
285 continue
286 }
287 part := fmt.Sprintf("%d %s", count[state], state)
288 if state == checkFailed {
289 part += " (" + strings.Join(failed, ", ") + ")"
290 }
291 parts = append(parts, part)
292 }
293 return "checks: " + strings.Join(parts, ", ") + costLine(c)
294}
295
296// runCheckLine prints valley pending's checks line for the active version
297// of each change named, for review to show above its diff, one line each:
298//
299// <change ID> checks: 21 passed in 9m10s (eval 24s)
300//
301// A change that is not ready, or whose active version has landed, has no
302// check state, and so no line. What could not be read is said on stderr,
303// as valley pending --checks says it, and leaves the line out.
304func runCheckLine(g git, args []string, stdout, stderr io.Writer) error {
305 wanted := map[string]bool{}
306 for _, a := range args {
307 if !change.ValidID(a) {
308 return failure("usage: valley-go checkline <change ID>...")
309 }
310 wanted[a] = true
311 }
312 if !g.succeeds(true, "rev-parse", "--git-dir") {
313 return failure("not a git repository")
314 }
315 list, _, err := sources(g, listing{}, stderr)
316 if err != nil {
317 return err
318 }
319 repos := readSources(list, newVerifier())
320 r := repos[0]
321 if r.err != nil {
322 return r.err
323 }
324 named := func(s *snapshot, n recorded) bool { return wanted[n.id] && pendingChange(s, n) }
325 view := r.repoView(handles(repos), named, unlanded)
326 r.addChecks(&view, newCosts(), stderr)
327 var b strings.Builder
328 for _, c := range view.Changes {
329 if v := c.Versions[c.Active-1]; v.Checks != nil {
330 fmt.Fprintf(&b, "%s\t%s\n", c.ChangeID, readable(checksLine(v.Checks)))
331 }
332 }
333 _, err = io.WriteString(stdout, b.String())
334 return err
335}