| 1 | package 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 one attest verify for each head, over all of its |
| 35 | // notes (verifyEach), the heads side by side. The reads are |
| 36 | // batched by repository, over the ready changes and drafts together: the |
| 37 | // policy is read once, without asking cue which requirements are |
| 38 | // mandatory, since nothing here shows or decides by a pin; origin's |
| 39 | // attestation refs for every head come from the snapshot's own listing of |
| 40 | // origin and are fetched in one fetch; and the host's signers are read |
| 41 | // once, as the caller's use says (evidenceUse). A read that fails |
| 42 | // leaves the changes it was for without check state, and says so on |
| 43 | // stderr; it never reads as no evidence. |
| 44 | // |
| 45 | // Each nix check also says what it cost the host's check worker to build |
| 46 | // for this version, against what it recently cost (checkcost.go), when |
| 47 | // the host's worker telemetry can be read. That read runs while the |
| 48 | // policy is read. When it cannot be read, the fields are left out, stderr |
| 49 | // says why once, and the check states are listed all the same. |
| 50 | // |
| 51 | // The same read tells a check the worker built and that failed from one |
| 52 | // not built yet. attest publishes no note when any check of its batch |
| 53 | // fails, so such a check reads as pending. When the worker's newest build |
| 54 | // of a check for the head's tree failed and no note makes the check |
| 55 | // passed, the check carries the worker's report: its result, when it |
| 56 | // ended, and its attest run (checkcost.go). When a check is pending and |
| 57 | // the worker's newest batch for the tree failed before any check ran, the |
| 58 | // version carries that batch's report. A row and the checks line name a |
| 59 | // pending check as failed in the worker's build, in words apart from a |
| 60 | // failure a note records, and with --checks a see: line names the command |
| 61 | // that reads the worker's records and logs. This is diagnostic, never |
| 62 | // evidence: nothing in it is signed, a check's state and the version's |
| 63 | // come from notes alone, and neither approve nor the integrator reads it. |
| 64 | // When the telemetry cannot be read, nothing is claimed. |
| 65 | |
| 66 | import ( |
| 67 | "fmt" |
| 68 | "io" |
| 69 | "os" |
| 70 | "runtime" |
| 71 | "strings" |
| 72 | "sync" |
| 73 | "time" |
| 74 | |
| 75 | "the-valley/change" |
| 76 | ) |
| 77 | |
| 78 | // The check states of a version's checks. |
| 79 | const ( |
| 80 | checkPassed = "passed" |
| 81 | checkFailed = "failed" |
| 82 | checkPending = "pending" |
| 83 | checkOther = "other" |
| 84 | ) |
| 85 | |
| 86 | // jsonVersionChecks is a version's check state, as valley-changes/v1 |
| 87 | // writes it under checks. |
| 88 | type jsonVersionChecks struct { |
| 89 | State string `json:"state"` |
| 90 | Tree string `json:"tree"` |
| 91 | Required []jsonCheckState `json:"required"` |
| 92 | // BuildMS is the sum of the build times of the checks built for this |
| 93 | // version, and EvalMS the sum of the evaluations of the runs that |
| 94 | // built them, when the worker's telemetry holds any (checkcost.go). |
| 95 | BuildMS *int64 `json:"build_ms,omitempty"` |
| 96 | EvalMS *int64 `json:"eval_ms,omitempty"` |
| 97 | // Worker is the worker's report of its newest batch for this tree, |
| 98 | // when a nix check is pending and that batch failed before any check |
| 99 | // ran (addWorkerReports). It is diagnostic: State does not count it. |
| 100 | Worker *jsonWorkerReport `json:"worker,omitempty"` |
| 101 | } |
| 102 | |
| 103 | // jsonWorkerReport is what the worker's telemetry records of a build or a |
| 104 | // batch of its that failed: its result, when it ended, and the attest run |
| 105 | // whose records and logs say why. |
| 106 | type jsonWorkerReport struct { |
| 107 | Result string `json:"result"` |
| 108 | Time string `json:"time"` |
| 109 | Run string `json:"run"` |
| 110 | } |
| 111 | |
| 112 | // jsonCheckState is one required check and the state of its note. |
| 113 | type jsonCheckState struct { |
| 114 | Name string `json:"name"` |
| 115 | State string `json:"state"` |
| 116 | Signer string `json:"signer,omitempty"` |
| 117 | Why string `json:"why,omitempty"` |
| 118 | // What the check cost to build, from the worker's telemetry |
| 119 | // (checkcost.go): this version's build, whether its output was |
| 120 | // cached, the median of the check's recent builds, whether it has |
| 121 | // none, and whether this build grew against them. |
| 122 | BuildMS *int64 `json:"build_ms,omitempty"` |
| 123 | Cached *bool `json:"cached,omitempty"` |
| 124 | BaselineMS *int64 `json:"baseline_ms,omitempty"` |
| 125 | New bool `json:"new,omitempty"` |
| 126 | // OtherCapacity says the check is new because its builds so far ran on |
| 127 | // other capacity. |
| 128 | OtherCapacity bool `json:"other_capacity,omitempty"` |
| 129 | Grown bool `json:"grown,omitempty"` |
| 130 | // Worker is the worker's report of its newest build of the check for |
| 131 | // this tree, when that build failed and no note makes the check |
| 132 | // passed (addWorkerReports). It is diagnostic: State does not count |
| 133 | // it. |
| 134 | Worker *jsonWorkerReport `json:"worker,omitempty"` |
| 135 | } |
| 136 | |
| 137 | // unlandedDraft is a draft that has not landed as any version. |
| 138 | func unlandedDraft(s *snapshot, n recorded) bool { |
| 139 | return n.record.State == change.Draft && !s.isLanded(n) |
| 140 | } |
| 141 | |
| 142 | // addChecks gives the active version of each pending change in view its |
| 143 | // check state, and, with drafts, that of each draft in view that has not |
| 144 | // landed, whether view lists it in changes or in drafts. Each also gets |
| 145 | // its build times from the host's worker telemetry, read through c, or |
| 146 | // none when c is nil. view is this repository's. The host's signers are |
| 147 | // read as use says (evidenceUse). |
| 148 | func (l listed) addChecks(view *jsonRepo, drafts bool, c *costs, use evidenceUse, stderr io.Writer) { |
| 149 | if l.err != nil { |
| 150 | return |
| 151 | } |
| 152 | wanted := map[[2]string]bool{} |
| 153 | for _, n := range l.snap.all { |
| 154 | if pendingChange(l.snap, n) || drafts && unlandedDraft(l.snap, n) { |
| 155 | wanted[[2]string{n.record.Target, n.id}] = true |
| 156 | } |
| 157 | } |
| 158 | lists := []*[]jsonChange{&view.Changes} |
| 159 | if view.Drafts != nil { |
| 160 | lists = append(lists, view.Drafts) |
| 161 | } |
| 162 | var heads []*jsonVersion |
| 163 | changeOf := map[*jsonVersion]string{} |
| 164 | for _, list := range lists { |
| 165 | for i := range *list { |
| 166 | ch := &(*list)[i] |
| 167 | if wanted[[2]string{ch.Target, ch.ChangeID}] { |
| 168 | heads = append(heads, &ch.Versions[ch.Active-1]) |
| 169 | changeOf[heads[len(heads)-1]] = ch.ChangeID |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | if len(heads) == 0 { |
| 174 | return |
| 175 | } |
| 176 | warn := func(format string, args ...any) { |
| 177 | fmt.Fprintf(stderr, "valley: %s: %s\n", l.name, fmt.Sprintf(format, args...)) |
| 178 | } |
| 179 | if l.snap.tip == "" { |
| 180 | warn("origin has no main to read the policy at, so no change lists its checks") |
| 181 | return |
| 182 | } |
| 183 | var reading *costRead |
| 184 | if c != nil { |
| 185 | reading = c.start(l.g) |
| 186 | } |
| 187 | if _, err := l.snap.layers(); err != nil { |
| 188 | warn("the policy at main did not read, so no change lists its checks: %v", err) |
| 189 | return |
| 190 | } |
| 191 | if _, err := schemaFile(l.g, ""); err != nil { |
| 192 | warn("%v, so no change lists its checks", err) |
| 193 | return |
| 194 | } |
| 195 | model, err := l.snap.model() |
| 196 | if err != nil { |
| 197 | warn("the policy at main did not read, so no change lists its checks: %v", err) |
| 198 | return |
| 199 | } |
| 200 | repo := l.g.dir |
| 201 | if repo == "" { |
| 202 | repo = "." |
| 203 | } |
| 204 | |
| 205 | // Each head's required checks and tree digest. |
| 206 | type head struct { |
| 207 | v *jsonVersion |
| 208 | change string |
| 209 | required []jsonRequired |
| 210 | asked []nixCheck |
| 211 | digest string |
| 212 | } |
| 213 | var read []head |
| 214 | var patterns []string |
| 215 | for _, v := range heads { |
| 216 | say := func(format string, args ...any) { |
| 217 | warn("v%d of %s lists no checks: %s", v.N, v.Head, fmt.Sprintf(format, args...)) |
| 218 | } |
| 219 | base, err := l.g.output(true, "merge-base", l.snap.tip, v.Head) |
| 220 | if err != nil { |
| 221 | say("it shares no history with main") |
| 222 | continue |
| 223 | } |
| 224 | doc, err := deriveUnder(l.g, model, base, v.Head) |
| 225 | if err != nil { |
| 226 | say("its checks did not derive: %v", err) |
| 227 | continue |
| 228 | } |
| 229 | digest, err := rememberedDigest(l.g, v.Head) |
| 230 | if err != nil { |
| 231 | say("%v", err) |
| 232 | continue |
| 233 | } |
| 234 | h := head{v: v, change: changeOf[v], required: doc.Required, digest: digest} |
| 235 | for _, q := range doc.Required { |
| 236 | if q.Runner == "nix" { |
| 237 | h.asked = append(h.asked, nixCheck{q.Name, q.Attribute}) |
| 238 | } |
| 239 | } |
| 240 | read = append(read, h) |
| 241 | patterns = append(patterns, attestationRefs+digest+"/*") |
| 242 | } |
| 243 | if len(read) == 0 { |
| 244 | return |
| 245 | } |
| 246 | |
| 247 | // The evidence origin holds for every head, as the snapshot listed it, |
| 248 | // or else listed now, and fetched at once. |
| 249 | refs := l.snap.evidence() |
| 250 | if refs == nil { |
| 251 | o, err := listOrigin(l.g, patterns...) |
| 252 | if err != nil { |
| 253 | warn("could not list the evidence origin holds, so no change lists its checks") |
| 254 | return |
| 255 | } |
| 256 | refs = o.refs |
| 257 | } |
| 258 | var oids []string |
| 259 | for _, h := range read { |
| 260 | for _, ref := range storedFor(h.digest, refs) { |
| 261 | oids = append(oids, refs[ref]) |
| 262 | } |
| 263 | } |
| 264 | if len(oids) > 0 { |
| 265 | if err := fetch(l.g, oids...); err != nil { |
| 266 | warn("could not fetch the evidence origin holds, so no change lists its checks") |
| 267 | return |
| 268 | } |
| 269 | } |
| 270 | keys, p, unsigned := hostSignersFrom(l.g, use.signers()) |
| 271 | if unsigned == "" { |
| 272 | defer os.Remove(keys) |
| 273 | } |
| 274 | var here counting |
| 275 | if p != nil { |
| 276 | here = counting{project: project(l.g), integrator: p.name, projects: p.projects} |
| 277 | } |
| 278 | // Each head's evidence is judged beside the others', one attest |
| 279 | // verify at a time for each CPU, and every judging ends before what |
| 280 | // each found is taken, in the heads' order. |
| 281 | type judging struct { |
| 282 | judged map[string]judgment |
| 283 | unjudged string |
| 284 | err error |
| 285 | } |
| 286 | judgings := make([]judging, len(read)) |
| 287 | slots := make(chan struct{}, runtime.NumCPU()) |
| 288 | var wg sync.WaitGroup |
| 289 | for i, h := range read { |
| 290 | stored := storedFor(h.digest, refs) |
| 291 | judgings[i].unjudged = unsigned |
| 292 | if unsigned != "" || len(stored) == 0 || len(h.asked) == 0 { |
| 293 | continue |
| 294 | } |
| 295 | wg.Add(1) |
| 296 | go func() { |
| 297 | defer wg.Done() |
| 298 | slots <- struct{}{} |
| 299 | defer func() { <-slots }() |
| 300 | j := &judgings[i] |
| 301 | j.judged, j.unjudged, j.err = judge(l.g, repo, h.v.Head, keys, "", here, stored, refs, h.asked) |
| 302 | }() |
| 303 | } |
| 304 | wg.Wait() |
| 305 | timed := c != nil && c.wait(reading, warn) |
| 306 | |
| 307 | for i, h := range read { |
| 308 | j := judgings[i] |
| 309 | if j.err != nil { |
| 310 | warn("v%d of %s lists no checks: %v", h.v.N, h.v.Head, j.err) |
| 311 | continue |
| 312 | } |
| 313 | h.v.Checks = checkStates(h.digest, h.required, j.judged, unsigned, j.unjudged) |
| 314 | if timed { |
| 315 | addCosts(h.v.Checks, h.required, l.name, h.change, h.v.N, reading.builds) |
| 316 | addWorkerReports(h.v.Checks, h.required, l.name, h.change, h.v.N, reading.builds, reading.batches) |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // checkStates is a version's check state, from the note judged for each |
| 322 | // nix check. unsigned says why no note could be verified, and unjudged why |
| 323 | // the integrator cannot judge the tree's evidence at all. |
| 324 | func checkStates(digest string, required []jsonRequired, judged map[string]judgment, unsigned, unjudged string) *jsonVersionChecks { |
| 325 | out := &jsonVersionChecks{State: checkPassed, Tree: digest, Required: []jsonCheckState{}} |
| 326 | for _, q := range required { |
| 327 | s := jsonCheckState{Name: q.Name} |
| 328 | j, ok := judged[q.Name] |
| 329 | switch { |
| 330 | case q.Runner != "nix": |
| 331 | s.State = checkOther |
| 332 | case unsigned != "": |
| 333 | s.State, s.Why = checkPending, unsigned |
| 334 | case unjudged != "": |
| 335 | s.State, s.Why = checkFailed, unjudged+", so the integrator cannot judge this tree's evidence" |
| 336 | case !ok: |
| 337 | s.State = checkPending |
| 338 | case j.note.refusal != "": |
| 339 | s.State, s.Why = checkPending, j.note.refusal |
| 340 | case j.why != "": |
| 341 | s.State, s.Signer, s.Why = checkFailed, j.note.signer, j.why |
| 342 | default: |
| 343 | s.State, s.Signer = checkPassed, j.note.signer |
| 344 | } |
| 345 | switch { |
| 346 | case s.State == checkFailed: |
| 347 | out.State = checkFailed |
| 348 | case s.State == checkPending && out.State == checkPassed: |
| 349 | out.State = checkPending |
| 350 | } |
| 351 | out.Required = append(out.Required, s) |
| 352 | } |
| 353 | return out |
| 354 | } |
| 355 | |
| 356 | // checksLine is valley pending's line for a version's check state, with |
| 357 | // what the worker reports of the pending checks' failed builds |
| 358 | // (workerSays), and what its checks cost to build when that is known |
| 359 | // (costLine): "checks: 18 passed, 1 failed (valley-request) in 9m10s |
| 360 | // (eval 24s)", or "checks: 18 passed, 1 pending (valley-request failed in |
| 361 | // the worker's build, 5m ago)". |
| 362 | func checksLine(c *jsonVersionChecks) string { |
| 363 | if len(c.Required) == 0 { |
| 364 | return "checks: none required" |
| 365 | } |
| 366 | count := map[string]int{} |
| 367 | var failed []string |
| 368 | for _, q := range c.Required { |
| 369 | count[q.State]++ |
| 370 | if q.State == checkFailed { |
| 371 | failed = append(failed, q.Name) |
| 372 | } |
| 373 | } |
| 374 | worker := workerSays(c) |
| 375 | var parts []string |
| 376 | for _, state := range []string{checkPassed, checkFailed, checkPending, checkOther} { |
| 377 | if count[state] == 0 { |
| 378 | continue |
| 379 | } |
| 380 | part := fmt.Sprintf("%d %s", count[state], state) |
| 381 | if state == checkFailed { |
| 382 | part += " (" + strings.Join(failed, ", ") + ")" |
| 383 | } |
| 384 | if state == checkPending && worker != "" { |
| 385 | part += " (" + worker + ")" |
| 386 | } |
| 387 | parts = append(parts, part) |
| 388 | } |
| 389 | return "checks: " + strings.Join(parts, ", ") + costLine(c) |
| 390 | } |
| 391 | |
| 392 | // workerSays is what the worker reports of a version's pending checks, in |
| 393 | // words that tell it from a failure a note records: the checks whose |
| 394 | // newest build failed, and when the newest of those builds ended, then |
| 395 | // the batch that failed before any check ran, and when: "valley-request |
| 396 | // failed in the worker's build, 5m ago; the worker's batch failed, 1m |
| 397 | // ago". It is "" when the worker reports neither. |
| 398 | func workerSays(c *jsonVersionChecks) string { |
| 399 | var names []string |
| 400 | newest := "" |
| 401 | for _, q := range c.Required { |
| 402 | if q.State == checkPending && q.Worker != nil { |
| 403 | names = append(names, q.Name) |
| 404 | newest = max(newest, q.Worker.Time) |
| 405 | } |
| 406 | } |
| 407 | var says []string |
| 408 | switch len(names) { |
| 409 | case 0: |
| 410 | case 1: |
| 411 | says = append(says, names[0]+" failed in the worker's build"+agoText(newest)) |
| 412 | default: |
| 413 | says = append(says, firstFew(names)+" failed in the worker's builds"+agoText(newest)) |
| 414 | } |
| 415 | if c.Worker != nil { |
| 416 | says = append(says, "the worker's batch failed"+agoText(c.Worker.Time)) |
| 417 | } |
| 418 | return strings.Join(says, "; ") |
| 419 | } |
| 420 | |
| 421 | // agoText is ", 5m ago" for a record's time, or "" when it does not read. |
| 422 | func agoText(stamp string) string { |
| 423 | at, err := time.Parse(time.RFC3339, stamp) |
| 424 | if err != nil { |
| 425 | return "" |
| 426 | } |
| 427 | return ", " + ago(time.Now().Unix(), at.Unix()) + " ago" |
| 428 | } |
| 429 | |
| 430 | // rowChecks is a version's checks as a row says them: in checkCount's |
| 431 | // words, or, when no note records a failure and the worker reports one, |
| 432 | // with the glyph ✗, what it reports (workerSays). |
| 433 | func rowChecks(c *jsonVersionChecks) (glyph, says string) { |
| 434 | if c != nil && c.State == checkPending { |
| 435 | if w := workerSays(c); w != "" { |
| 436 | return "✗", w |
| 437 | } |
| 438 | } |
| 439 | return "", checkCount(c) |
| 440 | } |
| 441 | |
| 442 | // workerSee is the line under a version's checks line that reads what the |
| 443 | // worker reports, when it reports a failure (workerSays), or "". |
| 444 | func workerSee(c *jsonVersionChecks) string { |
| 445 | if c == nil || workerSays(c) == "" { |
| 446 | return "" |
| 447 | } |
| 448 | return "see: valley telemetry read --source worker --logs" |
| 449 | } |
| 450 | |
| 451 | // runCheckLine prints valley pending's checks line for the active version |
| 452 | // of each change named, for review to show above its diff, one line each: |
| 453 | // |
| 454 | // <change ID> checks: 21 passed in 9m10s (eval 24s) |
| 455 | // |
| 456 | // A ready change and a draft each have one. A withdrawn change, or one |
| 457 | // that has landed as any version, has no check state, and so no line. What could not be read is said on stderr, |
| 458 | // as valley pending --checks says it, and leaves the line out. |
| 459 | func runCheckLine(g git, args []string, stdout, stderr io.Writer) error { |
| 460 | for _, a := range args { |
| 461 | if !change.ValidID(a) { |
| 462 | return failure("usage: valley checkline <change ID>...") |
| 463 | } |
| 464 | } |
| 465 | if !g.succeeds(true, "rev-parse", "--git-dir") { |
| 466 | return failure("not a git repository") |
| 467 | } |
| 468 | c := newCosts() |
| 469 | defer c.close() |
| 470 | changes, err := activeChecks(g, args, c, stderr) |
| 471 | if err != nil { |
| 472 | return err |
| 473 | } |
| 474 | var b strings.Builder |
| 475 | for _, c := range changes { |
| 476 | if v := c.Versions[c.Active-1]; v.Checks != nil { |
| 477 | fmt.Fprintf(&b, "%s\t%s\n", c.ChangeID, readable(checksLine(v.Checks))) |
| 478 | } |
| 479 | } |
| 480 | _, err = io.WriteString(stdout, b.String()) |
| 481 | return err |
| 482 | } |
| 483 | |
| 484 | // activeChecks is each change named that is ready or a draft and has not |
| 485 | // landed, in this repository, with its active version's check state, as |
| 486 | // valley pending --drafts --checks reads it, and with build times read |
| 487 | // through c when it is given. |
| 488 | func activeChecks(g git, ids []string, c *costs, stderr io.Writer) ([]jsonChange, error) { |
| 489 | wanted := map[string]bool{} |
| 490 | for _, id := range ids { |
| 491 | wanted[id] = true |
| 492 | } |
| 493 | list, _, err := sources(g, listing{}, stderr) |
| 494 | if err != nil { |
| 495 | return nil, err |
| 496 | } |
| 497 | repos := readSources(list, newVerifier()) |
| 498 | defer closeSources(repos) |
| 499 | r := repos[0] |
| 500 | if r.err != nil { |
| 501 | return nil, r.err |
| 502 | } |
| 503 | named := func(s *snapshot, n recorded) bool { |
| 504 | return wanted[n.id] && (pendingChange(s, n) || unlandedDraft(s, n)) |
| 505 | } |
| 506 | view := r.repoView(handles(repos), named, unlanded) |
| 507 | r.addChecks(&view, true, c, assessEvidence, stderr) |
| 508 | return view.Changes, nil |
| 509 | } |