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