| 1 | package main |
| 2 | |
| 3 | // The refs transport: a reconciliation loop over the integration requests |
| 4 | // durable in the served repository. |
| 5 | // |
| 6 | // It polls, deliberately. The bus is unauthenticated (bd-d853d9c), which |
| 7 | // gates automated consumers, and the requests are durable in git with the |
| 8 | // bus as a projection over them (architecture.md). So the controller |
| 9 | // level-triggers over the request refs themselves and converges: each pass |
| 10 | // recomputes the verdict for every request against the current tip, acts |
| 11 | // only where the answer has changed since the last pass, and needs no |
| 12 | // subscription at all. It publishes outcomes to the bus exactly as the |
| 13 | // post-receive hook publishes ref updates. Consuming events arrives in a |
| 14 | // later phase. |
| 15 | // |
| 16 | // # The request ref |
| 17 | // |
| 18 | // A request is a ref: |
| 19 | // |
| 20 | // refs/the-valley/integration-requests/<target>/<change> -> commit |
| 21 | // |
| 22 | // The target segment names the protected branch, so the change's target |
| 23 | // stream is refs/heads/<target>; the last segment names the change. The |
| 24 | // commit is the change's head, its tree is what the attestations are over, |
| 25 | // and the delta is the merge base with the target's tip to that head. |
| 26 | // Nothing else is encoded: a change is a diff targeting a stream |
| 27 | // (ida-93e4f91), and every part of that is already in git. A request |
| 28 | // whose last segment is c-<change ID> is the request of the change with a |
| 29 | // record under that ID, and the records decide whether it may land at all |
| 30 | // (records.go). |
| 31 | |
| 32 | import ( |
| 33 | "the-valley/integrator/verdict" |
| 34 | |
| 35 | "fmt" |
| 36 | "sort" |
| 37 | "strings" |
| 38 | "time" |
| 39 | ) |
| 40 | |
| 41 | const ( |
| 42 | requestPrefix = "refs/the-valley/integration-requests" |
| 43 | outcomePrefix = "refs/the-valley/integration-outcomes" |
| 44 | ) |
| 45 | |
| 46 | // request is one integration request as the refs transport found it. |
| 47 | type request struct { |
| 48 | ref string |
| 49 | change string |
| 50 | target string |
| 51 | head string |
| 52 | } |
| 53 | |
| 54 | // requests lists the pending requests, in ref order so a pass is |
| 55 | // deterministic, along with the refs under the namespace that are not |
| 56 | // requests at all. |
| 57 | // |
| 58 | // Any holder of the request grant can write this namespace, and anyone at all |
| 59 | // where a repository is served without the pre-receive hook, so a name that |
| 60 | // does not parse is an ordinary thing to find there and not a reason to stop: one |
| 61 | // unreadable ref must not hold up every readable one. It is reported and |
| 62 | // skipped. |
| 63 | func (in *integrator) requests() ([]request, []string, error) { |
| 64 | out, err := git(in.repo, "for-each-ref", "--format=%(refname) %(objectname)", requestPrefix) |
| 65 | if err != nil { |
| 66 | return nil, nil, err |
| 67 | } |
| 68 | var found []request |
| 69 | var malformed []string |
| 70 | for _, line := range strings.Split(strings.TrimSpace(out), "\n") { |
| 71 | if line == "" { |
| 72 | continue |
| 73 | } |
| 74 | ref, head, ok := strings.Cut(line, " ") |
| 75 | if !ok { |
| 76 | continue |
| 77 | } |
| 78 | rest := strings.TrimPrefix(ref, requestPrefix+"/") |
| 79 | target, change, ok := strings.Cut(rest, "/") |
| 80 | if !ok || strings.Contains(change, "/") || target == "" || change == "" { |
| 81 | malformed = append(malformed, ref) |
| 82 | continue |
| 83 | } |
| 84 | found = append(found, request{ref: ref, change: change, target: "refs/heads/" + target, head: head}) |
| 85 | } |
| 86 | sort.Slice(found, func(i, j int) bool { return found[i].ref < found[j].ref }) |
| 87 | sort.Strings(malformed) |
| 88 | return found, malformed, nil |
| 89 | } |
| 90 | |
| 91 | // reconcile is one pass: judge every request against the current tip of its |
| 92 | // target, and act where the answer has changed. |
| 93 | // |
| 94 | // The change records are read once, before any request is judged. A pass |
| 95 | // that cannot read them judges nothing: which heads are versions of |
| 96 | // changes that are not ready is what keeps those heads from landing. The |
| 97 | // requests a withdrawn change voids are deleted before the rest are |
| 98 | // judged. |
| 99 | func (in *integrator) reconcile() error { |
| 100 | requests, malformed, err := in.requests() |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | for _, ref := range malformed { |
| 105 | fmt.Fprintf(in.out, "%s: not a request ref; one is %s/<target>/<change>. Skipped.\n", ref, requestPrefix) |
| 106 | } |
| 107 | if len(requests) == 0 { |
| 108 | return nil |
| 109 | } |
| 110 | idx, err := in.readRecords() |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | for _, u := range idx.unreadable { |
| 115 | fmt.Fprintf(in.out, "WARNING: %s is a change record that does not read: %s. Requests are judged as though it did not exist, so a head it names is not held back by it. The hook refuses such a record, so the repository's storage is corrupt; restore the record or delete it.\n", u.ref, u.why) |
| 116 | } |
| 117 | requests = in.voidWithdrawn(requests, idx) |
| 118 | for _, r := range requests { |
| 119 | if err := in.handle(r, idx); err != nil { |
| 120 | // One bad request must not stop the pass: the integrator is a |
| 121 | // controller, and a request it cannot judge is one request's |
| 122 | // problem. |
| 123 | fmt.Fprintf(in.out, "%s: %v\n", r.change, err) |
| 124 | } |
| 125 | } |
| 126 | return nil |
| 127 | } |
| 128 | |
| 129 | // handle judges one request and acts on the verdict. |
| 130 | func (in *integrator) handle(r request, idx *recordIndex) error { |
| 131 | tip, err := gitLine(in.repo, "rev-parse", "--verify", "--quiet", r.target) |
| 132 | if err != nil { |
| 133 | return fmt.Errorf("no target %s to integrate onto", r.target) |
| 134 | } |
| 135 | // Level-triggering: the verdict is a function of the request's head, |
| 136 | // the target's tip, and the evidence stored for the change, so an |
| 137 | // unchanged answer is one where none of the three has moved. |
| 138 | // Re-announcing an unchanged answer would be a retry storm; missing a |
| 139 | // moved one would strand a contributor who pushed the attestation a |
| 140 | // previous pass said was absent. |
| 141 | evidence, err := in.evidenceState() |
| 142 | if err != nil { |
| 143 | return err |
| 144 | } |
| 145 | memo := fmt.Sprintf("%s %s %s", r.head, tip, evidence) |
| 146 | last, err := in.readMemo(r) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | if in.answered(last, memo) { |
| 151 | return nil |
| 152 | } |
| 153 | // From here on the request has been taken up, so whatever happens is |
| 154 | // this pass's answer to it — including a failure. Recording that is |
| 155 | // what keeps the next tick from asking the identical question. |
| 156 | outcome, err := in.refuse(r, idx, tip, last, memo) |
| 157 | if err == nil && outcome == "" { |
| 158 | outcome, err = in.act(r, tip) |
| 159 | } |
| 160 | if err != nil { |
| 161 | if memoErr := in.writeMemo(r, memo+" "+failedMemo+" "+in.now().Format(time.RFC3339)); memoErr != nil { |
| 162 | return fmt.Errorf("%v (and the failure could not be recorded: %v)", err, memoErr) |
| 163 | } |
| 164 | return err |
| 165 | } |
| 166 | if last == memo+" "+string(outcome) { |
| 167 | return nil |
| 168 | } |
| 169 | return in.writeMemo(r, memo+" "+string(outcome)) |
| 170 | } |
| 171 | |
| 172 | // refuse applies the rules that refuse a request before it is judged |
| 173 | // (records.go), and reports a refusal. It reports one only when it is new: |
| 174 | // a refusal is decided again on every pass, and saying the same one every |
| 175 | // tick would bury it. |
| 176 | func (in *integrator) refuse(r request, idx *recordIndex, tip, last, memo string) (verdict.Outcome, error) { |
| 177 | outcome, why, err := in.recordRefusal(r, idx, tip) |
| 178 | if err == nil && outcome == "" { |
| 179 | outcome, why, err = in.mergeRefusal(r, tip) |
| 180 | } |
| 181 | if err != nil { |
| 182 | return "", err |
| 183 | } |
| 184 | if outcome != "" && last != memo+" "+string(outcome) { |
| 185 | in.reportRefusal(r, tip, outcome, why) |
| 186 | } |
| 187 | // A record that does not read may name a commit this request brings, |
| 188 | // and whether it holds that commit back cannot be told. That is said |
| 189 | // with the request, whatever is decided about it. |
| 190 | if outcome == "" || last != memo+" "+string(outcome) { |
| 191 | for _, u := range in.uncheckable(r, idx, tip) { |
| 192 | fmt.Fprintf(in.out, "WARNING: %s may name a commit %s brings, and does not read, so whether it holds that commit back cannot be checked\n", u.ref, r.change) |
| 193 | } |
| 194 | } |
| 195 | return outcome, nil |
| 196 | } |
| 197 | |
| 198 | // reportRefusal prints a refusal the way report prints a verdict. |
| 199 | func (in *integrator) reportRefusal(r request, tip string, outcome verdict.Outcome, why string) { |
| 200 | fmt.Fprintf(in.out, "%s -> %s @ %s: %s\n", r.change, r.target, verdict.Short(tip), outcome) |
| 201 | fmt.Fprintf(in.out, " %s\n", why) |
| 202 | fmt.Fprintf(in.out, " no event: a refusal is not staleness, and the request stays where it is\n") |
| 203 | } |
| 204 | |
| 205 | // failedMemo marks a memo written for a pass that could not act on its own |
| 206 | // verdict, and retryAfter is how long that answer stands. |
| 207 | // |
| 208 | // A failure here is a function of the same three things the verdict is, so |
| 209 | // the identical attempt fails identically until one of them moves — and |
| 210 | // moving any of them already re-judges the request, whatever the memo says. |
| 211 | // The window exists for the other kind of failure: a bus, a disk, a lock, |
| 212 | // something outside the three that will be different later. Minutes rather |
| 213 | // than ticks, because a controller that reports the same refusal every |
| 214 | // fifteen seconds has buried the report it was making. |
| 215 | const ( |
| 216 | failedMemo = "failed" |
| 217 | retryAfter = 10 * time.Minute |
| 218 | ) |
| 219 | |
| 220 | // answered says whether this pass already has this request's answer, given |
| 221 | // the last memo written for it. An outcome stands until the request, the |
| 222 | // tip, or the evidence moves; a recorded failure stands the same way, and |
| 223 | // additionally expires, so a transient one is retried without being |
| 224 | // retried at tick rate. A refusal never stands: what refused it can change |
| 225 | // while the three stay where they are, as when a draft is readied again, |
| 226 | // so it is decided again on every pass. Nor does a voiding: a request |
| 227 | // filed again at the same head is a fresh approval. |
| 228 | func (in *integrator) answered(last, memo string) bool { |
| 229 | rest, ok := strings.CutPrefix(last, memo+" ") |
| 230 | if !ok { |
| 231 | return false |
| 232 | } |
| 233 | switch verdict.Outcome(rest) { |
| 234 | case refusedNotReady, refusedMerge, voidedWithdrawn: |
| 235 | return false |
| 236 | } |
| 237 | when, failed := strings.CutPrefix(rest, failedMemo+" ") |
| 238 | if !failed { |
| 239 | return true |
| 240 | } |
| 241 | at, err := time.Parse(time.RFC3339, when) |
| 242 | if err != nil { |
| 243 | return false |
| 244 | } |
| 245 | return in.now().Sub(at) < retryAfter |
| 246 | } |
| 247 | |
| 248 | // act judges one request and does what the verdict says, returning the |
| 249 | // outcome so the memo can record it. |
| 250 | func (in *integrator) act(r request, tip string) (verdict.Outcome, error) { |
| 251 | ch, snap, landed, err := in.materialize(r, tip) |
| 252 | if err != nil { |
| 253 | return "", err |
| 254 | } |
| 255 | v := verdict.Decide(ch, snap) |
| 256 | in.report(r, tip, v) |
| 257 | if landed.composition != "" { |
| 258 | fmt.Fprintf(in.out, " policy %s\n", landed.composition) |
| 259 | } |
| 260 | |
| 261 | switch v.Outcome { |
| 262 | case verdict.Land: |
| 263 | outcome, err := in.land(ch, v, r, tip, landed) |
| 264 | if outcome != "" || err != nil { |
| 265 | if outcome == "" { |
| 266 | outcome = v.Outcome |
| 267 | } |
| 268 | return outcome, err |
| 269 | } |
| 270 | case verdict.Stale: |
| 271 | in.publishStale(ch, v, tip) |
| 272 | case verdict.Reject: |
| 273 | // There is no rejection event. The failure model is unified on |
| 274 | // staleness (architecture.md), and a change whose evidence is |
| 275 | // forged is not stale — so rather than stretch either event to |
| 276 | // cover it, the vocabulary stays as it is and the refusal is the |
| 277 | // integrator's own output. The request stays put; nothing about |
| 278 | // it will change on its own. |
| 279 | fmt.Fprintf(in.out, " no event: a forged attestation is not staleness, and the vocabulary has no other word\n") |
| 280 | } |
| 281 | return v.Outcome, nil |
| 282 | } |
| 283 | |
| 284 | // landing is the commit a verdict would move the target to, and what the |
| 285 | // verdict was computed under. |
| 286 | type landing struct { |
| 287 | commit string // the commit that would be the target's new tip |
| 288 | digest string // the valley-tree-v1 digest of the resulting tree |
| 289 | policy string // a digest of the composed policy the verdict used |
| 290 | |
| 291 | // composition is what the pass has to say about how the policy layers |
| 292 | // resolved, or empty when both were where they are configured to be. |
| 293 | composition string |
| 294 | |
| 295 | // notes are the contributor's attestations as stored, by check name. |
| 296 | // The verdict does not read them; what lands countersigns them. |
| 297 | notes map[string][]byte |
| 298 | } |
| 299 | |
| 300 | // materialize gathers everything the verdict is a function of: the applied |
| 301 | // delta, the required checks, the recomputed closures, and what intervening |
| 302 | // landings touched. |
| 303 | func (in *integrator) materialize(r request, tip string) (verdict.Change, verdict.Snapshot, landing, error) { |
| 304 | var land landing |
| 305 | wt, err := addWorktree(in.repo, tip) |
| 306 | if err != nil { |
| 307 | return verdict.Change{}, verdict.Snapshot{}, land, err |
| 308 | } |
| 309 | defer wt.remove() |
| 310 | |
| 311 | base, err := gitLine(in.repo, "merge-base", tip, r.head) |
| 312 | if err != nil { |
| 313 | return verdict.Change{}, verdict.Snapshot{}, land, fmt.Errorf("%s and %s share no history", verdict.Short(r.head), r.target) |
| 314 | } |
| 315 | attested, err := in.treeDigest(wt, r.head) |
| 316 | if err != nil { |
| 317 | return verdict.Change{}, verdict.Snapshot{}, land, err |
| 318 | } |
| 319 | ch := verdict.Change{ID: r.change, Target: r.target, Base: base, Head: r.head, Attested: attested} |
| 320 | if ch.Evidence, land.notes, err = in.collectEvidence(wt, r.head, attested); err != nil { |
| 321 | return ch, verdict.Snapshot{}, land, err |
| 322 | } |
| 323 | |
| 324 | // The policy is read from the integrator's own side and never from the |
| 325 | // tree being gated: the project layer from this checkout of the target |
| 326 | // tip, the floor from the instance repository's integrated tip. Both |
| 327 | // are resolved once and held for the whole pass. |
| 328 | pol, closePolicy, err := in.policy.open(wt) |
| 329 | if err != nil { |
| 330 | return ch, verdict.Snapshot{}, land, err |
| 331 | } |
| 332 | defer closePolicy() |
| 333 | land.composition = pol.note |
| 334 | cat, policyDigest, err := pol.catalogue() |
| 335 | if err != nil { |
| 336 | return ch, verdict.Snapshot{}, land, err |
| 337 | } |
| 338 | land.policy = policyDigest |
| 339 | owed, err := pol.derive(wt, base, r.head) |
| 340 | if err != nil { |
| 341 | return ch, verdict.Snapshot{}, land, err |
| 342 | } |
| 343 | required, err := pol.required(owed, cat) |
| 344 | if err != nil { |
| 345 | return ch, verdict.Snapshot{}, land, err |
| 346 | } |
| 347 | |
| 348 | snap := verdict.Snapshot{Required: required, Touched: map[string]bool{}, Now: in.now()} |
| 349 | |
| 350 | // Apply the delta. A base that is already the tip is a fast-forward: |
| 351 | // the head lands as it stands and its tree is the tree that was |
| 352 | // attested. |
| 353 | if base == tip { |
| 354 | land.commit = r.head |
| 355 | land.digest = attested |
| 356 | snap.Apply = verdict.Applied{Clean: true, Tree: attested} |
| 357 | } else { |
| 358 | tree, conflict, err := mergeDelta(in.repo, base, tip, r.head) |
| 359 | if err != nil { |
| 360 | return ch, snap, land, err |
| 361 | } |
| 362 | if conflict != "" { |
| 363 | snap.Apply = verdict.Applied{Clean: false, Conflict: conflict} |
| 364 | return ch, snap, land, nil |
| 365 | } |
| 366 | if land.commit, err = in.commitTree(tree, tip, r); err != nil { |
| 367 | return ch, snap, land, err |
| 368 | } |
| 369 | if land.digest, err = in.treeDigest(wt, tree); err != nil { |
| 370 | return ch, snap, land, err |
| 371 | } |
| 372 | snap.Apply = verdict.Applied{Clean: true, Tree: land.digest} |
| 373 | |
| 374 | // What intervening landings touched, in the policy's own terms: |
| 375 | // the classes the deriver matches over base..tip. Asking the |
| 376 | // deriver rather than matching globs here keeps one implementation |
| 377 | // of what a class covers. |
| 378 | intervening, err := pol.derive(wt, base, tip) |
| 379 | if err != nil { |
| 380 | return ch, snap, land, err |
| 381 | } |
| 382 | snap.Touched = intervening.classes |
| 383 | } |
| 384 | |
| 385 | // The closures only have to be recomputed when the tree moved: an |
| 386 | // unmoved tree presents the identical computation by construction. |
| 387 | if land.digest != attested { |
| 388 | snap.Closures, snap.ClosureErrors = in.closures(wt, land.commit, required) |
| 389 | } |
| 390 | return ch, snap, land, nil |
| 391 | } |
| 392 | |
| 393 | // committerEmail is the address on the commits the integrator makes. git |
| 394 | // wants an address and there is none to give: the integrator is a service, |
| 395 | // not a correspondent. .invalid never resolves, which says so in the address |
| 396 | // itself. Which integrator committed is carried by the name beside it. |
| 397 | const committerEmail = "integrator@the-valley.invalid" |
| 398 | |
| 399 | // commitTree turns the merged tree into the commit that would become the |
| 400 | // target's new tip. The tip is its only parent, so the ref write is a |
| 401 | // fast-forward; the change's authorship is carried over from its head. |
| 402 | // |
| 403 | // Both identities are stated here rather than left to git. The author is the |
| 404 | // asker's, because re-parenting a delta is not authoring it. The committer is |
| 405 | // the integrator, under the name it signs its transfer statement with: the |
| 406 | // commit and the statement about it then name the same actor. Leaving either |
| 407 | // to git would ask git to guess an identity from the environment, and under |
| 408 | // the unit there is nothing to guess from — which is a landing that fails for |
| 409 | // a reason that has nothing to do with the change. |
| 410 | func (in *integrator) commitTree(tree, tip string, r request) (string, error) { |
| 411 | message, err := git(in.repo, "log", "-1", "--format=%B", r.head) |
| 412 | if err != nil { |
| 413 | return "", err |
| 414 | } |
| 415 | author, err := git(in.repo, "log", "-1", "--format=%an%n%ae%n%aI", r.head) |
| 416 | if err != nil { |
| 417 | return "", err |
| 418 | } |
| 419 | fields := strings.Split(strings.TrimRight(author, "\n"), "\n") |
| 420 | if len(fields) != 3 { |
| 421 | return "", fmt.Errorf("cannot read the authorship of %s", verdict.Short(r.head)) |
| 422 | } |
| 423 | cmd := []string{"commit-tree", tree, "-p", tip, "-m", strings.TrimRight(message, "\n")} |
| 424 | // The committer date is the author's, so that the commit id is a function |
| 425 | // of what the pass decided from — the tree, the tip, the message and the |
| 426 | // authorship — and of nothing else. A pass that dies between here and the |
| 427 | // ref write recomputes the identical commit next time round instead of |
| 428 | // leaving another unreferenced one behind. Nothing reads this date: when |
| 429 | // the landing happened is recorded, signed, in the transfer statement. |
| 430 | out, err := in.gitEnv(map[string]string{ |
| 431 | "GIT_AUTHOR_NAME": fields[0], |
| 432 | "GIT_AUTHOR_EMAIL": fields[1], |
| 433 | "GIT_AUTHOR_DATE": fields[2], |
| 434 | "GIT_COMMITTER_NAME": in.signerName, |
| 435 | "GIT_COMMITTER_EMAIL": committerEmail, |
| 436 | "GIT_COMMITTER_DATE": fields[2], |
| 437 | }, cmd...) |
| 438 | return strings.TrimSpace(out), err |
| 439 | } |
| 440 | |
| 441 | // land moves the protected ref, stores the evidence, and says so on the |
| 442 | // bus. The ref write is a compare-and-swap against the tip the verdict was |
| 443 | // computed over, so a request that lost a race to another lands nothing and |
| 444 | // is judged again next pass against the tip that won. |
| 445 | // |
| 446 | // The order is the commit point's. Everything that can fail while the |
| 447 | // integrator is still deciding happens first, and the ref write is the |
| 448 | // commit. Composing the transfer statement — vetting it, writing it down, |
| 449 | // signing it — is part of deciding: a claim the integrator cannot write |
| 450 | // down is a claim it has not made, and a stream moved on a decision that |
| 451 | // cannot be stated is a landing nothing records. A failure before the |
| 452 | // commit point leaves the stream where it was and the request where it was, |
| 453 | // which is a transaction abandoned before it committed and costs one pass |
| 454 | // to redo. |
| 455 | // |
| 456 | // Past the commit point the landing is a fact in the stream, and what is |
| 457 | // left is recording it: handing the ref move to the host to publish, |
| 458 | // storing the evidence, consuming the request, and saying so on the bus. |
| 459 | // None of those may be skipped because an earlier one failed, and |
| 460 | // consuming the request in particular may not be reachable only through |
| 461 | // something that can fail — a request left pending for a change |
| 462 | // already in the stream is re-judged against an empty delta on every pass, |
| 463 | // forever. So each step runs, each failure is reported, and the landing is |
| 464 | // reported as having happened, because it did. |
| 465 | func (in *integrator) land(ch verdict.Change, v verdict.Verdict, r request, tip string, l landing) (verdict.Outcome, error) { |
| 466 | transfer, err := in.composeTransfer(ch, v, l.digest, l.commit, tip, l.policy) |
| 467 | if err != nil { |
| 468 | return "", err |
| 469 | } |
| 470 | evidence, err := in.countersign(ch, v, l, transfer) |
| 471 | if err != nil { |
| 472 | return "", err |
| 473 | } |
| 474 | |
| 475 | // Judging and signing can take minutes, and a change can be unreadied |
| 476 | // or withdrawn meanwhile, or a record can come to name a commit this |
| 477 | // request brings. So every record is read again here, as the last |
| 478 | // thing before the commit point, and a refusal lands nothing. A record |
| 479 | // written between this read and the ref write is still missed: refs |
| 480 | // offer no way to make the two one step. What lands then is still the |
| 481 | // head the operator approved. |
| 482 | if outcome, why, err := in.finalRefusal(r, tip); err != nil || outcome != "" { |
| 483 | if outcome != "" { |
| 484 | in.reportRefusal(r, tip, outcome, why) |
| 485 | } |
| 486 | return outcome, err |
| 487 | } |
| 488 | |
| 489 | // The commit point. |
| 490 | if _, err := git(in.repo, "update-ref", ch.Target, l.commit, tip); err != nil { |
| 491 | return "", fmt.Errorf("%s moved under the verdict; nothing landed: %w", ch.Target, err) |
| 492 | } |
| 493 | fmt.Fprintf(in.out, " landed %s %s -> %s\n", ch.Target, verdict.Short(tip), verdict.Short(l.commit)) |
| 494 | in.queueRefUpdate(ch.Target, tip, l.commit) |
| 495 | |
| 496 | var unrecorded []string |
| 497 | refs, err := in.store(evidence) |
| 498 | if err != nil { |
| 499 | unrecorded = append(unrecorded, fmt.Sprintf("the evidence was not stored (%v)", err)) |
| 500 | } |
| 501 | for _, ref := range refs { |
| 502 | fmt.Fprintf(in.out, " evidence %s\n", ref) |
| 503 | } |
| 504 | // The request is consumed only if it still names the head that landed. |
| 505 | // A second approval can replace it while this pass works — the asker's |
| 506 | // write is leased against what it read, and nothing stops it reading |
| 507 | // the request this pass is about to land — and a delete that did not |
| 508 | // compare would take the new request with the old one. A request that |
| 509 | // moved is a new request, and it stands for the next pass. |
| 510 | if _, err := git(in.repo, "update-ref", "-d", r.ref, r.head); err != nil { |
| 511 | if now, found := gitTry(in.repo, "rev-parse", "--verify", "--quiet", r.ref); found && now != r.head { |
| 512 | fmt.Fprintf(in.out, " request %s moved to %s while %s landed; it stands for the next pass\n", |
| 513 | r.ref, verdict.Short(now), verdict.Short(r.head)) |
| 514 | } else { |
| 515 | unrecorded = append(unrecorded, fmt.Sprintf("the request ref %s was not consumed (%v)", r.ref, err)) |
| 516 | } |
| 517 | } |
| 518 | // The bus comes last. Everything above is the landing's own record; an |
| 519 | // event is a best-effort notice of it, and a bus that fails or hangs |
| 520 | // must not leave any of that record undone. |
| 521 | in.publishRefUpdated(ch.Target, tip, l.commit) |
| 522 | in.publishLanded(ch, v, tip, l.commit) |
| 523 | if len(unrecorded) > 0 { |
| 524 | return "", fmt.Errorf("%s landed as %s and the landing is not fully recorded: %s", |
| 525 | ch.ID, verdict.Short(l.commit), strings.Join(unrecorded, "; ")) |
| 526 | } |
| 527 | return "", nil |
| 528 | } |
| 529 | |
| 530 | // report prints the verdict, per check, in the order it was judged. |
| 531 | func (in *integrator) report(r request, tip string, v verdict.Verdict) { |
| 532 | fmt.Fprintf(in.out, "%s -> %s @ %s: %s\n", r.change, r.target, verdict.Short(tip), v.Outcome) |
| 533 | fmt.Fprintf(in.out, " %s\n", v.Reason) |
| 534 | for _, cv := range v.Checks { |
| 535 | if cv.Transferred { |
| 536 | fmt.Fprintf(in.out, " check %-24s transferred (%s)\n", cv.Name, cv.Rule) |
| 537 | continue |
| 538 | } |
| 539 | fmt.Fprintf(in.out, " check %-24s %s\n", cv.Name, cv.Reason) |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // The memo is the level-trigger's memory: what this integrator last decided |
| 544 | // about a request, keyed by the request. It is durable in git like |
| 545 | // everything else the transport reads, so a restarted controller does not |
| 546 | // re-announce a judgement it has already made. |
| 547 | func (in *integrator) memoRef(r request) string { |
| 548 | return outcomePrefix + "/" + strings.TrimPrefix(r.ref, requestPrefix+"/") |
| 549 | } |
| 550 | |
| 551 | func (in *integrator) readMemo(r request) (string, error) { |
| 552 | out, ok := gitTry(in.repo, "cat-file", "blob", in.memoRef(r)) |
| 553 | if !ok { |
| 554 | return "", nil |
| 555 | } |
| 556 | return strings.TrimSpace(out), nil |
| 557 | } |
| 558 | |
| 559 | func (in *integrator) writeMemo(r request, value string) error { |
| 560 | blob, err := in.writeBlob([]byte(value + "\n")) |
| 561 | if err != nil { |
| 562 | return err |
| 563 | } |
| 564 | _, err = git(in.repo, "update-ref", in.memoRef(r), blob) |
| 565 | return err |
| 566 | } |
| 567 | |
| 568 | // evidenceState digests the attestation namespace: every ref under it and |
| 569 | // the object it points at. Attestation refs are create-only, so this moves |
| 570 | // exactly when evidence arrives. |
| 571 | // |
| 572 | // It is deliberately the whole namespace rather than the refs for one |
| 573 | // change's subject digest, which would take a checkout to compute and would |
| 574 | // have to be recomputed before the memo could rule the work out. So an |
| 575 | // attestation for one change re-judges every pending request. That is the |
| 576 | // conservative direction — a pass that runs and changes nothing costs a |
| 577 | // pass; a verdict never revisited strands a change — and it is bounded by |
| 578 | // the number of requests pending. |
| 579 | func (in *integrator) evidenceState() (string, error) { |
| 580 | out, err := git(in.repo, "for-each-ref", "--format=%(refname) %(objectname)", attestationPrefix) |
| 581 | if err != nil { |
| 582 | return "", err |
| 583 | } |
| 584 | return sha256hex([]byte(out))[:16], nil |
| 585 | } |
| 586 | |
| 587 | func (in *integrator) now() time.Time { |
| 588 | if !in.clock.IsZero() { |
| 589 | return in.clock |
| 590 | } |
| 591 | return time.Now().UTC() |
| 592 | } |