| 1 | // Every change a repository holds a record of, and how far each version |
| 2 | // has landed. |
| 3 | // |
| 4 | // changes and pending read one snapshot of origin for each repository |
| 5 | // they list: every branch, every record, every integration request and |
| 6 | // outcome, and every attestation ref. From it they decide, for each |
| 7 | // version of each change, the landing evidence it has. The first rule |
| 8 | // that applies is the answer: |
| 9 | // |
| 10 | // 1. landed: the version's head is an ancestor of main. |
| 11 | // 2. landed-recorded: a commit on main carries a transfer statement, |
| 12 | // signed by the integrator, whose change is the version's request |
| 13 | // name and whose base is the valley-tree-v1 digest of the version's |
| 14 | // tree. The integrator writes one when it lands a change as a new |
| 15 | // commit (integrator/transfer.go). |
| 16 | // 3. uncertain: a request for the version was consumed, or the outcome |
| 17 | // record says the integrator failed on it, and neither of the above |
| 18 | // holds. The integrator moves main before it stores its evidence |
| 19 | // (integrator/refs.go), so a landing can exist without its evidence. |
| 20 | // 4. not-landed: otherwise. |
| 21 | // |
| 22 | // The outcome record alone is never proof of a landing, because a later |
| 23 | // pass can overwrite it. |
| 24 | |
| 25 | package main |
| 26 | |
| 27 | import ( |
| 28 | "bytes" |
| 29 | "errors" |
| 30 | "fmt" |
| 31 | "os" |
| 32 | "os/exec" |
| 33 | "sort" |
| 34 | "strings" |
| 35 | |
| 36 | "the-valley/change" |
| 37 | ) |
| 38 | |
| 39 | // The landing evidence a version can have. |
| 40 | const ( |
| 41 | landed = "landed" |
| 42 | landedRecorded = "landed-recorded" |
| 43 | uncertain = "uncertain" |
| 44 | notLanded = "not-landed" |
| 45 | ) |
| 46 | |
| 47 | const ( |
| 48 | transferNote = "transfer.note" |
| 49 | predicateTransfer = "the-valley/integration/transfer/v1" |
| 50 | ) |
| 51 | |
| 52 | // inventoryPatterns are the ls-remote patterns of every ref a snapshot |
| 53 | // reads. |
| 54 | var inventoryPatterns = []string{ |
| 55 | "refs/heads/*", |
| 56 | change.RefPrefix + recordTarget + "/*", |
| 57 | requestPrefix + "*", |
| 58 | outcomePrefix + "*", |
| 59 | attestationRefs + "*", |
| 60 | } |
| 61 | |
| 62 | // verifier is what a transfer statement is checked with: the verifier |
| 63 | // keys of the identity registry's known-signers file, the name the |
| 64 | // integrator signs under, and the attest helper that checks a note. |
| 65 | // VALLEY_KNOWN_SIGNERS and VALLEY_INTEGRATOR_SIGNER name the first two. |
| 66 | // Without both, no transfer statement is believed, and a version that |
| 67 | // only one could show landed reads as uncertain or not-landed. |
| 68 | type verifier struct { |
| 69 | keys, signer, attest string |
| 70 | } |
| 71 | |
| 72 | func verifierFromEnv() verifier { |
| 73 | return verifier{ |
| 74 | keys: os.Getenv("VALLEY_KNOWN_SIGNERS"), |
| 75 | signer: os.Getenv("VALLEY_INTEGRATOR_SIGNER"), |
| 76 | attest: "attest", |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // transferStatement is one transfer statement found under an attestation |
| 81 | // ref, with the values a landing is matched on, read before it is |
| 82 | // verified. |
| 83 | type transferStatement struct { |
| 84 | note []byte |
| 85 | change string // predicate.change: the request's change name |
| 86 | base string // predicate.base.valley-tree-v1: the tree the evidence was over |
| 87 | landed string // subject.digest.git-sha1: the commit that landed |
| 88 | } |
| 89 | |
| 90 | // snapshot is one read of a repository's origin. |
| 91 | type snapshot struct { |
| 92 | g git |
| 93 | o origin |
| 94 | tip string // main on origin; "" when origin has none |
| 95 | all []recorded |
| 96 | verify verifier |
| 97 | |
| 98 | outcomes map[string]memo // outcome ref → the record it holds |
| 99 | landings map[string][2]string // "<change ID> <n>" → landing and commit |
| 100 | onMainOf map[string]bool |
| 101 | reachOf map[string]map[string]bool |
| 102 | digestOf map[string]string |
| 103 | transfers []transferStatement |
| 104 | read bool // whether transfers has been read |
| 105 | |
| 106 | // unverified is set when a transfer statement named a version and |
| 107 | // could not be checked, so its landing may be stronger than shown. |
| 108 | unverified bool |
| 109 | } |
| 110 | |
| 111 | // readSnapshot lists origin once and fetches every object the snapshot |
| 112 | // reads: main, every branch tip, every record commit and every outcome |
| 113 | // record. A record commit has its version heads as parents, so they come |
| 114 | // with it. |
| 115 | func readSnapshot(g git, v verifier) (*snapshot, error) { |
| 116 | o, err := listOrigin(g, inventoryPatterns...) |
| 117 | if err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | var want []string |
| 121 | for ref, oid := range o.refs { |
| 122 | if strings.HasPrefix(ref, "refs/heads/") || strings.HasPrefix(ref, outcomePrefix) { |
| 123 | want = append(want, oid) |
| 124 | } |
| 125 | } |
| 126 | if err := fetch(g, want...); err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | all, err := readAll(g, o) |
| 130 | if err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | return newSnapshot(g, o, all, v), nil |
| 134 | } |
| 135 | |
| 136 | // newSnapshot is a snapshot of what o lists, with every record in all |
| 137 | // already read and every object it names already fetched. |
| 138 | func newSnapshot(g git, o origin, all []recorded, v verifier) *snapshot { |
| 139 | s := &snapshot{ |
| 140 | g: g, o: o, tip: o.refs["refs/heads/"+recordTarget], all: all, verify: v, |
| 141 | outcomes: map[string]memo{}, landings: map[string][2]string{}, onMainOf: map[string]bool{}, |
| 142 | reachOf: map[string]map[string]bool{}, digestOf: map[string]string{}, |
| 143 | } |
| 144 | for ref, oid := range o.refs { |
| 145 | if strings.HasPrefix(ref, outcomePrefix) { |
| 146 | data, err := g.output(true, "cat-file", "blob", oid) |
| 147 | if err != nil { |
| 148 | data = "" |
| 149 | } |
| 150 | s.outcomes[ref] = parseMemo(data) |
| 151 | } |
| 152 | } |
| 153 | return s |
| 154 | } |
| 155 | |
| 156 | // headOf is a version's head as an object ID. |
| 157 | func headOf(v change.Version) string { |
| 158 | return strings.TrimPrefix(v.Head, change.HeadPrefix) |
| 159 | } |
| 160 | |
| 161 | // onMain reports whether a commit is an ancestor of main, or main itself. |
| 162 | func (s *snapshot) onMain(oid string) bool { |
| 163 | if s.tip == "" || oid == "" { |
| 164 | return false |
| 165 | } |
| 166 | on, seen := s.onMainOf[oid] |
| 167 | if !seen { |
| 168 | on = s.g.succeeds(true, "merge-base", "--is-ancestor", oid, s.tip) |
| 169 | s.onMainOf[oid] = on |
| 170 | } |
| 171 | return on |
| 172 | } |
| 173 | |
| 174 | // requestNames are the names a request for a version is filed under: its |
| 175 | // branch, the way approve names one, and c-<change ID>. |
| 176 | func requestNames(id string, v change.Version) []string { |
| 177 | var names []string |
| 178 | if v.Branch != "" { |
| 179 | names = append(names, changeName(v.Branch)) |
| 180 | } |
| 181 | return append(names, "c-"+id) |
| 182 | } |
| 183 | |
| 184 | // request is the integration request origin holds for a version: one |
| 185 | // filed under either of its names, at its head. |
| 186 | func (s *snapshot) request(id string, v change.Version) *jsonRequest { |
| 187 | for _, name := range requestNames(id, v) { |
| 188 | if ref := requestPrefix + name; s.o.refs[ref] == headOf(v) { |
| 189 | return &jsonRequest{Ref: ref, Commit: headOf(v)} |
| 190 | } |
| 191 | } |
| 192 | return nil |
| 193 | } |
| 194 | |
| 195 | // outcome is the integrator's last decision about a version: the outcome |
| 196 | // record under either of its names whose head is the version's head. |
| 197 | func (s *snapshot) outcome(id string, v change.Version) (name string, m memo, found bool) { |
| 198 | for _, name := range requestNames(id, v) { |
| 199 | if m, ok := s.outcomes[outcomePrefix+name]; ok && m.head == headOf(v) { |
| 200 | return name, m, true |
| 201 | } |
| 202 | } |
| 203 | return "", memo{}, false |
| 204 | } |
| 205 | |
| 206 | // landing is the landing evidence a version has, and the commit on main it |
| 207 | // landed as, when it has. Each version is decided once per snapshot. |
| 208 | func (s *snapshot) landing(id string, v change.Version) (kind, commit string) { |
| 209 | key := fmt.Sprintf("%s %d", id, v.N) |
| 210 | if l, ok := s.landings[key]; ok { |
| 211 | return l[0], l[1] |
| 212 | } |
| 213 | kind, commit = s.decideLanding(id, v) |
| 214 | s.landings[key] = [2]string{kind, commit} |
| 215 | return kind, commit |
| 216 | } |
| 217 | |
| 218 | func (s *snapshot) decideLanding(id string, v change.Version) (kind, commit string) { |
| 219 | head := headOf(v) |
| 220 | if s.onMain(head) { |
| 221 | return landed, head |
| 222 | } |
| 223 | if commit, ok := s.recordedLanding(id, v); ok { |
| 224 | return landedRecorded, commit |
| 225 | } |
| 226 | if name, m, ok := s.outcome(id, v); ok { |
| 227 | consumed := m.decision == "land" && s.o.refs[requestPrefix+name] != head |
| 228 | if consumed || m.decision == "failed" { |
| 229 | return uncertain, "" |
| 230 | } |
| 231 | } |
| 232 | return notLanded, "" |
| 233 | } |
| 234 | |
| 235 | // isLanded reports whether a version has landed by the first two rules. |
| 236 | func (s *snapshot) isLanded(id string, v change.Version) bool { |
| 237 | kind, _ := s.landing(id, v) |
| 238 | return kind == landed || kind == landedRecorded |
| 239 | } |
| 240 | |
| 241 | // recordedLanding looks for a transfer statement that records the version |
| 242 | // landing as a commit on main, and verifies the first that matches. |
| 243 | func (s *snapshot) recordedLanding(id string, v change.Version) (string, bool) { |
| 244 | names := map[string]bool{} |
| 245 | for _, n := range requestNames(id, v) { |
| 246 | names[n] = true |
| 247 | } |
| 248 | for _, t := range s.transferStatements() { |
| 249 | if !names[t.change] || !isObjectID(t.landed) || !s.onMain(t.landed) { |
| 250 | continue |
| 251 | } |
| 252 | digest, ok := s.digest(headOf(v)) |
| 253 | if !ok { |
| 254 | s.unverified = true |
| 255 | continue |
| 256 | } |
| 257 | if t.base != digest { |
| 258 | continue |
| 259 | } |
| 260 | if s.verified(t) { |
| 261 | return t.landed, true |
| 262 | } |
| 263 | } |
| 264 | return "", false |
| 265 | } |
| 266 | |
| 267 | // transferStatements reads every transfer statement under origin's |
| 268 | // attestation refs, once. A transfer statement is the transfer.note at |
| 269 | // the top of an attestation ref's tree. Anything unreadable is skipped: |
| 270 | // what is not read is not believed. |
| 271 | func (s *snapshot) transferStatements() []transferStatement { |
| 272 | if s.read { |
| 273 | return s.transfers |
| 274 | } |
| 275 | s.read = true |
| 276 | var trees []string |
| 277 | for ref, oid := range s.o.refs { |
| 278 | if strings.HasPrefix(ref, attestationRefs) { |
| 279 | trees = append(trees, oid) |
| 280 | } |
| 281 | } |
| 282 | if len(trees) == 0 { |
| 283 | return nil |
| 284 | } |
| 285 | sort.Strings(trees) |
| 286 | if err := fetch(s.g, trees...); err != nil { |
| 287 | s.unverified = true |
| 288 | return nil |
| 289 | } |
| 290 | var query bytes.Buffer |
| 291 | for _, tree := range trees { |
| 292 | fmt.Fprintf(&query, "%s:%s\n", tree, transferNote) |
| 293 | } |
| 294 | out, err := s.g.input(true, &query, "cat-file", "--batch-check=%(objectname) %(objecttype)") |
| 295 | if err != nil { |
| 296 | s.unverified = true |
| 297 | return nil |
| 298 | } |
| 299 | for _, line := range strings.Split(out, "\n") { |
| 300 | oid, kind, _ := strings.Cut(line, " ") |
| 301 | if kind != "blob" { |
| 302 | continue |
| 303 | } |
| 304 | note, err := s.g.output(true, "cat-file", "blob", oid) |
| 305 | if err != nil { |
| 306 | continue |
| 307 | } |
| 308 | note += "\n" |
| 309 | text, ok := noteText(note) |
| 310 | if !ok || statementValue(text, "predicateType") != predicateTransfer { |
| 311 | continue |
| 312 | } |
| 313 | s.transfers = append(s.transfers, transferStatement{ |
| 314 | note: []byte(note), |
| 315 | change: statementValue(text, "predicate.change"), |
| 316 | base: statementValue(text, "predicate.base.valley-tree-v1"), |
| 317 | landed: statementValue(text, "subject.digest.git-sha1"), |
| 318 | }) |
| 319 | } |
| 320 | return s.transfers |
| 321 | } |
| 322 | |
| 323 | // noteText is the statement text of a note: everything before the last |
| 324 | // blank line, which separates the text from its signatures. |
| 325 | func noteText(note string) (string, bool) { |
| 326 | split := strings.LastIndex(note, "\n\n") |
| 327 | if split < 0 { |
| 328 | return "", false |
| 329 | } |
| 330 | return note[:split+1], true |
| 331 | } |
| 332 | |
| 333 | // statementValue reads one key's value from statement text, where each |
| 334 | // line is a key, one space and a value. It is a lookup, and it decides |
| 335 | // nothing: what is read here only chooses which statement to verify. |
| 336 | func statementValue(text, key string) string { |
| 337 | for _, line := range strings.Split(text, "\n") { |
| 338 | if k, v, ok := strings.Cut(line, " "); ok && k == key { |
| 339 | return v |
| 340 | } |
| 341 | } |
| 342 | return "" |
| 343 | } |
| 344 | |
| 345 | // repoDir is the directory attest is pointed at. |
| 346 | func (s *snapshot) repoDir() string { |
| 347 | if s.g.dir == "" { |
| 348 | return "." |
| 349 | } |
| 350 | return s.g.dir |
| 351 | } |
| 352 | |
| 353 | // digest is the valley-tree-v1 digest of a commit's tree, from attest, |
| 354 | // which defines the scheme. |
| 355 | func (s *snapshot) digest(oid string) (string, bool) { |
| 356 | if d, ok := s.digestOf[oid]; ok { |
| 357 | return d, d != "" |
| 358 | } |
| 359 | cmd := exec.Command(s.verify.attest, "digest", "--repo", s.repoDir(), "--rev", oid) |
| 360 | cmd.Env = s.g.env |
| 361 | out, err := cmd.Output() |
| 362 | d := "" |
| 363 | if scheme, value, ok := strings.Cut(strings.TrimSpace(string(out)), ":"); err == nil && ok && scheme == "valley-tree-v1" { |
| 364 | d = value |
| 365 | } |
| 366 | s.digestOf[oid] = d |
| 367 | return d, d != "" |
| 368 | } |
| 369 | |
| 370 | // verified runs attest verify over a transfer statement: its signature |
| 371 | // must be the integrator's, by a key the known-signers file holds, and |
| 372 | // its subject must be the tree of the commit it says landed. |
| 373 | func (s *snapshot) verified(t transferStatement) bool { |
| 374 | if s.verify.keys == "" || s.verify.signer == "" { |
| 375 | s.unverified = true |
| 376 | return false |
| 377 | } |
| 378 | file, err := os.CreateTemp("", "valley-transfer-*.note") |
| 379 | if err != nil { |
| 380 | s.unverified = true |
| 381 | return false |
| 382 | } |
| 383 | defer os.Remove(file.Name()) |
| 384 | _, err = file.Write(t.note) |
| 385 | if cerr := file.Close(); err != nil || cerr != nil { |
| 386 | s.unverified = true |
| 387 | return false |
| 388 | } |
| 389 | cmd := exec.Command(s.verify.attest, "verify", "--note", file.Name(), |
| 390 | "--known-keys", s.verify.keys, "--signer", s.verify.signer, |
| 391 | "--repo", s.repoDir(), "--rev", t.landed) |
| 392 | cmd.Env = s.g.env |
| 393 | if err := cmd.Run(); err != nil { |
| 394 | // attest refusing the note is an answer. Failing to run it is not. |
| 395 | var exit *exec.ExitError |
| 396 | if !errors.As(err, &exit) { |
| 397 | s.unverified = true |
| 398 | } |
| 399 | return false |
| 400 | } |
| 401 | return true |
| 402 | } |
| 403 | |
| 404 | // reach is every commit reachable from oid that main does not reach. |
| 405 | func (s *snapshot) reach(oid string) map[string]bool { |
| 406 | if r, ok := s.reachOf[oid]; ok { |
| 407 | return r |
| 408 | } |
| 409 | args := []string{"rev-list", oid} |
| 410 | if s.tip != "" { |
| 411 | args = append(args, "^"+s.tip) |
| 412 | } |
| 413 | r := map[string]bool{} |
| 414 | if out, err := s.g.output(true, args...); err == nil { |
| 415 | for _, c := range strings.Fields(out) { |
| 416 | r[c] = true |
| 417 | } |
| 418 | } |
| 419 | s.reachOf[oid] = r |
| 420 | return r |
| 421 | } |
| 422 | |
| 423 | // strictAncestor reports whether a is an ancestor of b, and not b itself. |
| 424 | // A commit main does not reach is an ancestor of b exactly when it is in |
| 425 | // reach(b), so git is asked only about a commit on main. |
| 426 | func (s *snapshot) strictAncestor(a, b string) bool { |
| 427 | switch { |
| 428 | case a == b: |
| 429 | return false |
| 430 | case s.reach(b)[a]: |
| 431 | return true |
| 432 | case s.onMain(a): |
| 433 | return s.g.succeeds(true, "merge-base", "--is-ancestor", a, b) |
| 434 | } |
| 435 | return false |
| 436 | } |
| 437 | |
| 438 | // includes is the change IDs among candidates whose active head is a |
| 439 | // strict ancestor of n's active head, nearest only: one that is itself an |
| 440 | // ancestor of another such change's head is left out. It is ancestry |
| 441 | // alone, so a change that includes another lands the other's content too. |
| 442 | func (s *snapshot) includes(n recorded, candidates []recorded) []string { |
| 443 | head := headOf(n.record.ActiveVersion()) |
| 444 | var below []recorded |
| 445 | for _, c := range candidates { |
| 446 | if c.id != n.id && s.strictAncestor(headOf(c.record.ActiveVersion()), head) { |
| 447 | below = append(below, c) |
| 448 | } |
| 449 | } |
| 450 | ids := []string{} |
| 451 | for _, c := range below { |
| 452 | nearest := true |
| 453 | for _, d := range below { |
| 454 | if s.strictAncestor(headOf(c.record.ActiveVersion()), headOf(d.record.ActiveVersion())) { |
| 455 | nearest = false |
| 456 | break |
| 457 | } |
| 458 | } |
| 459 | if nearest { |
| 460 | ids = append(ids, c.id) |
| 461 | } |
| 462 | } |
| 463 | sort.Strings(ids) |
| 464 | return ids |
| 465 | } |
| 466 | |
| 467 | // unrecorded is every branch on origin, other than main, whose tip main |
| 468 | // does not reach and that is not the head of any change's active version, |
| 469 | // in the order of their names. |
| 470 | func (s *snapshot) unrecorded() []jsonBranch { |
| 471 | active := map[string]bool{} |
| 472 | for _, n := range s.all { |
| 473 | active[headOf(n.record.ActiveVersion())] = true |
| 474 | } |
| 475 | branches := []jsonBranch{} |
| 476 | for ref, oid := range s.o.refs { |
| 477 | name, ok := strings.CutPrefix(ref, "refs/heads/") |
| 478 | if !ok || name == recordTarget || active[oid] || s.onMain(oid) { |
| 479 | continue |
| 480 | } |
| 481 | branches = append(branches, jsonBranch{Branch: name, Head: oid}) |
| 482 | } |
| 483 | sort.Slice(branches, func(i, j int) bool { return branches[i].Branch < branches[j].Branch }) |
| 484 | return branches |
| 485 | } |