| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | "the-valley/change" |
| 8 | "the-valley/rebase" |
| 9 | ) |
| 10 | |
| 11 | // Recovery reads origin and a journal, and decides each write the journal |
| 12 | // names, in the order a pass makes them: a family's branch push, then its |
| 13 | // versions, lower layers first, then its reports and its replacements. |
| 14 | // Each write is one of: |
| 15 | // |
| 16 | // - done: origin holds it, or has moved on from it; |
| 17 | // - finish: origin does not hold it, and everything the write was built |
| 18 | // on still holds, so it is still valid to push; |
| 19 | // - stop: it must not be pushed. A stop is final when no later pass could |
| 20 | // push it either, and terminal when it is also a local failure whose |
| 21 | // diagnostics the journal keeps. |
| 22 | // |
| 23 | // A branch push origin did not take is finished only while nothing it was |
| 24 | // prepared from has moved: main is at the tip it was rebased onto, every |
| 25 | // branch at the tip its lease names, and every record at the record |
| 26 | // commit read. Otherwise it stops, and the next pass prepares the family |
| 27 | // again. A version is finished only while its branch is at the prepared |
| 28 | // head, or the branch push is to be finished first, and its record is |
| 29 | // still the record commit read, so the change is still ready at the head |
| 30 | // the capture read. A version stops on its author's movement, a |
| 31 | // withdrawal, an unready, a submission, or a main that no longer descends |
| 32 | // from the tip the family was rebased onto. A deleted record is a |
| 33 | // terminal failure: the version cannot be added, and the hook takes no |
| 34 | // report about a change it does not record. A family whose writes stop |
| 35 | // does not stop another's. |
| 36 | // |
| 37 | // A related integration request that might still land holds the branch |
| 38 | // push and every version, which wait for a later pass: requests are read |
| 39 | // again, by name and by every version head the changes have had, the |
| 40 | // prepared heads included. A report is finished once what it says |
| 41 | // published is on origin, or will be once the writes before it are |
| 42 | // finished. A report whose claims no longer can be, because a version it |
| 43 | // names stopped, stops, and a report of what did happen is built in its |
| 44 | // place: a replacement. A replacement is finished while origin's report |
| 45 | // is still the one it was built on and it still says what did happen, as |
| 46 | // the family's versions are decided now. One that no longer does stops, |
| 47 | // and another is built in its place. |
| 48 | // |
| 49 | // In --report-only mode recovery decides and pushes nothing. |
| 50 | |
| 51 | // What recovery decides about a write. |
| 52 | const ( |
| 53 | writeDone = "done" |
| 54 | writeFinish = "finish" |
| 55 | writeStop = "stop" |
| 56 | ) |
| 57 | |
| 58 | // Recovery is what recovery decided about a journal. |
| 59 | type Recovery struct { |
| 60 | Journal string `json:"journal"` // the journal's path |
| 61 | Error string `json:"error,omitempty"` // why it was not read, so nothing in it is decided |
| 62 | Settled bool `json:"settled"` // every write is done or stopped for good |
| 63 | Decisions []Decision `json:"decisions"` |
| 64 | } |
| 65 | |
| 66 | // Decision is what recovery decided about one write. |
| 67 | type Decision struct { |
| 68 | Family int `json:"family"` // the family's place in the journal |
| 69 | Operation string `json:"operation"` // the operation that built the family |
| 70 | Write string `json:"write"` // branches, version, report or replacement |
| 71 | ID string `json:"change_id,omitempty"` // the change a version or a report is about |
| 72 | Commit string `json:"commit,omitempty"` // the record or report commit it pushes |
| 73 | Action string `json:"action"` // done, finish or stop |
| 74 | Final bool `json:"final"` // a stop no later pass can finish |
| 75 | Terminal bool `json:"terminal"` // a final stop that is a local failure, kept with its diagnostics |
| 76 | Why string `json:"why"` |
| 77 | } |
| 78 | |
| 79 | func (d Decision) settled() bool { return d.Action == writeDone || d.Action == writeStop && d.Final } |
| 80 | |
| 81 | // reading is what recovery reads origin through. |
| 82 | type reading struct { |
| 83 | s *rebase.Snapshot |
| 84 | requests []Request |
| 85 | evidence string // the evidence fingerprint origin shows now |
| 86 | in Incoming // against origin's tip now |
| 87 | is Ancestry // in the scratch repository |
| 88 | have func(oid string) bool // whether the scratch repository holds an object |
| 89 | landed func(id string, v change.Version) bool // whether a version has landed |
| 90 | } |
| 91 | |
| 92 | // decide decides every write the journal names. |
| 93 | func decide(j *Journal, rd reading) *Recovery { |
| 94 | rec := &Recovery{Decisions: []Decision{}, Settled: true} |
| 95 | for i, f := range j.Families { |
| 96 | ds := rd.family(i, f) |
| 97 | for k := range ds { |
| 98 | ds[k].Operation = f.Operation |
| 99 | } |
| 100 | rec.Decisions = append(rec.Decisions, ds...) |
| 101 | } |
| 102 | for _, d := range rec.Decisions { |
| 103 | rec.Settled = rec.Settled && d.settled() |
| 104 | } |
| 105 | return rec |
| 106 | } |
| 107 | |
| 108 | func (rd reading) family(i int, f JournalFamily) []Decision { |
| 109 | var out []Decision |
| 110 | stopped := func(d *Decision, final bool, why string, a ...any) { |
| 111 | d.Action, d.Final, d.Why = writeStop, final, fmt.Sprintf(why, a...) |
| 112 | } |
| 113 | layers := map[string]ObservedLayer{} |
| 114 | for _, l := range f.Observed.Layers { |
| 115 | layers[l.ID] = l |
| 116 | } |
| 117 | |
| 118 | // Whether main still descends from the tip the family was rebased onto. |
| 119 | compatible, final, target := true, true, "" |
| 120 | switch descends, err := rd.is(f.Tip, rd.s.Tip); { |
| 121 | case rd.s.Tip == "": |
| 122 | compatible, target = false, "origin has no "+rebase.Target |
| 123 | case err != nil: |
| 124 | compatible, final, target = false, false, fmt.Sprintf("whether %s at %s descends from %s cannot be told", rebase.Target, short(rd.s.Tip), short(f.Tip)) |
| 125 | case !descends: |
| 126 | compatible, target = false, fmt.Sprintf("%s at %s no longer descends from %s, the tip the family was rebased onto", rebase.Target, short(rd.s.Tip), short(f.Tip)) |
| 127 | } |
| 128 | held := rd.holding(f) |
| 129 | var submitted []string |
| 130 | for _, v := range f.Versions { |
| 131 | if cv, ok := capturedVersion(v, layers[v.ID]); ok && rd.landed(v.ID, cv) { |
| 132 | submitted = append(submitted, fmt.Sprintf("%s v%d has been submitted", v.Handle, cv.N)) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // The branch push. |
| 137 | branches := Decision{Action: ""} |
| 138 | if b := f.Branches; b != nil { |
| 139 | d := Decision{Family: i, Write: "branches"} |
| 140 | var moved, other, missing []string |
| 141 | for _, u := range b.Updates { |
| 142 | at := rd.s.Refs["refs/heads/"+u.Branch] |
| 143 | switch { |
| 144 | case at == u.New || at != "" && at != u.Old && rd.reaches(u.New, at): |
| 145 | moved = append(moved, u.Branch) |
| 146 | case at == u.Old: |
| 147 | case at == "": |
| 148 | other = append(other, "origin has no "+u.Branch) |
| 149 | default: |
| 150 | other = append(other, fmt.Sprintf("origin's %s is at %s, not %s", u.Branch, short(at), short(u.Old))) |
| 151 | } |
| 152 | if !rd.have(u.New) { |
| 153 | missing = append(missing, short(u.New)) |
| 154 | } |
| 155 | } |
| 156 | recordMoved := "" |
| 157 | for _, v := range f.Versions { |
| 158 | if rd.s.Refs[v.Ref] != v.Old { |
| 159 | recordMoved = rd.recordNow(v, layers[v.ID].Head) |
| 160 | break |
| 161 | } |
| 162 | } |
| 163 | switch { |
| 164 | case len(moved) > 0: |
| 165 | d.Action, d.Why = writeDone, "origin took the branch push: "+strings.Join(moved, ", ")+" at the prepared heads, or past them" |
| 166 | case len(other) > 0: |
| 167 | stopped(&d, true, "the branches were not pushed, and %s since the family was prepared", strings.Join(other, "; ")) |
| 168 | case len(missing) > 0: |
| 169 | stopped(&d, true, "the scratch repository no longer holds the prepared head %s", strings.Join(missing, ", ")) |
| 170 | case !compatible: |
| 171 | stopped(&d, final, "the branches were not pushed, and %s", target) |
| 172 | case rd.s.Tip != f.Tip: |
| 173 | stopped(&d, true, "%s moved from %s to %s before the branches were pushed, so the next pass prepares the family again", rebase.Target, short(f.Tip), short(rd.s.Tip)) |
| 174 | case recordMoved != "": |
| 175 | stopped(&d, true, "the branches were not pushed, and %s", recordMoved) |
| 176 | case len(submitted) > 0: |
| 177 | stopped(&d, true, "the branches were not pushed, and %s", strings.Join(submitted, "; ")) |
| 178 | case len(held) > 0: |
| 179 | stopped(&d, false, "the branches wait: %s", strings.Join(held, "; ")) |
| 180 | default: |
| 181 | d.Action, d.Why = writeFinish, "origin has not moved since the family was prepared" |
| 182 | } |
| 183 | branches = d |
| 184 | out = append(out, d) |
| 185 | } |
| 186 | |
| 187 | // Each version, lower layers first. |
| 188 | versions := map[string]Decision{} |
| 189 | for _, v := range f.Versions { |
| 190 | d := Decision{Family: i, Write: "version", ID: v.ID, Commit: v.Commit} |
| 191 | at := rd.s.Refs[v.Ref] |
| 192 | branch := rd.s.Refs["refs/heads/"+v.Branch] |
| 193 | switch { |
| 194 | case at == v.Commit || at != "" && at != v.Old && rd.reaches(v.Commit, at): |
| 195 | d.Action, d.Why = writeDone, fmt.Sprintf("origin's record of %s holds v%d", v.Handle, v.Version) |
| 196 | case at == "": |
| 197 | stopped(&d, true, "origin no longer holds a record of %s, so v%d cannot be added", v.Handle, v.Version) |
| 198 | d.Terminal = true |
| 199 | case at != v.Old: |
| 200 | stopped(&d, true, "%s, so v%d is not added", rd.recordNow(v, layers[v.ID].Head), v.Version) |
| 201 | case !rd.have(v.Commit): |
| 202 | stopped(&d, true, "the scratch repository no longer holds the record commit %s", short(v.Commit)) |
| 203 | case branches.Action == writeStop: |
| 204 | stopped(&d, branches.Final, "its branch push stopped: %s", branches.Why) |
| 205 | case !compatible: |
| 206 | stopped(&d, final, "%s", target) |
| 207 | case len(submitted) > 0: |
| 208 | stopped(&d, true, "%s", strings.Join(submitted, "; ")) |
| 209 | case branches.Action == writeFinish: |
| 210 | d.Action, d.Why = writeFinish, "once the branches are pushed: the record is ready at the head the capture read" |
| 211 | case branch == "": |
| 212 | stopped(&d, true, "origin no longer has %s, so v%d is not added", v.Branch, v.Version) |
| 213 | case branch != v.Head: |
| 214 | stopped(&d, true, "origin's %s is at %s, not the prepared head %s: its author moved it, so v%d is not added", v.Branch, short(branch), short(v.Head), v.Version) |
| 215 | case len(held) > 0: |
| 216 | stopped(&d, false, "paused: %s", strings.Join(held, "; ")) |
| 217 | default: |
| 218 | d.Action, d.Why = writeFinish, fmt.Sprintf("origin's %s is at the prepared head, and the record is ready at the head the capture read", v.Branch) |
| 219 | } |
| 220 | versions[v.ID] = d |
| 221 | out = append(out, d) |
| 222 | } |
| 223 | |
| 224 | // Each report. |
| 225 | for _, r := range f.Reports { |
| 226 | p := r.Pending |
| 227 | d := Decision{Family: i, Write: "report", ID: p.ID, Commit: p.Commit} |
| 228 | at := rd.s.Refs[p.Ref] |
| 229 | current, found := rd.s.Report(p.ID) |
| 230 | // What the report says was published, against what became of it. |
| 231 | // The journal was checked as it was read (loadJournal), so each |
| 232 | // version the report names is one the family adds, decided above. |
| 233 | claims, claimFinal, pending := "", false, false |
| 234 | for _, l := range p.Report.Layers { |
| 235 | if l.Published == nil { |
| 236 | continue |
| 237 | } |
| 238 | switch v := versions[l.ChangeID]; { |
| 239 | case branches.Action == writeStop: |
| 240 | claims, claimFinal = "its branches were not pushed: "+branches.Why, branches.Final |
| 241 | case l.Published.Record != nil && v.Action == writeStop: |
| 242 | claims, claimFinal = fmt.Sprintf("%s's version was not added: %s", l.ChangeID, v.Why), v.Final |
| 243 | case l.Published.Record != nil && v.Action == writeFinish || branches.Action == writeFinish: |
| 244 | pending = true |
| 245 | } |
| 246 | if claims != "" { |
| 247 | break |
| 248 | } |
| 249 | } |
| 250 | switch { |
| 251 | case at == p.Commit || at != "" && at != p.Old && rd.reaches(p.Commit, at): |
| 252 | d.Action, d.Why = writeDone, "origin holds the report" |
| 253 | case rd.s.Refs[change.Ref(rebase.Target, p.ID)] == "": |
| 254 | stopped(&d, true, "origin no longer holds a record of %s, and the hook takes no report about a change it does not record. The report is kept here: %s, %s", |
| 255 | p.ID, p.Report.Result, oneLine(p.Report)) |
| 256 | d.Terminal = true |
| 257 | case found && sameReport(current.Report, p.Report): |
| 258 | d.Action, d.Why = writeDone, "origin's report already says the same" |
| 259 | case at != p.Old: |
| 260 | stopped(&d, true, "origin's report moved to %s since this one was built on %s", short(at), orNone(p.Old)) |
| 261 | case !rd.have(p.Commit): |
| 262 | stopped(&d, true, "the scratch repository no longer holds the report commit %s", short(p.Commit)) |
| 263 | case claims != "": |
| 264 | stopped(&d, claimFinal, "the attempt did not end as this report says, since %s; a report of what did happen is built in its place", claims) |
| 265 | case pending: |
| 266 | d.Action, d.Why = writeFinish, "once the writes it reports are pushed" |
| 267 | default: |
| 268 | d.Action, d.Why = writeFinish, "origin's report is still the one it was built on" |
| 269 | } |
| 270 | out = append(out, d) |
| 271 | } |
| 272 | |
| 273 | // Each replacement, against what did happen as the versions are |
| 274 | // decided now. |
| 275 | var happened Observation |
| 276 | var published map[string]*change.RestackPublished |
| 277 | if branches.Action == writeDone { |
| 278 | happened, published = whatHappened(f, func(id string) Decision { return versions[id] }) |
| 279 | } |
| 280 | for _, r := range f.Replacements { |
| 281 | p := r.Pending |
| 282 | d := Decision{Family: i, Write: "replacement", ID: p.ID, Commit: p.Commit} |
| 283 | at := rd.s.Refs[p.Ref] |
| 284 | current, found := rd.s.Report(p.ID) |
| 285 | says := "" |
| 286 | if branches.Action == writeDone { |
| 287 | if want, _, err := bound(fullReport(f.Operation, f.Tip, p.ID, happened, published), happened.Digest()); err != nil { |
| 288 | says = err.Error() |
| 289 | } else if !sameReport(want, p.Report) { |
| 290 | says = "it no longer says what did happen" |
| 291 | } |
| 292 | } |
| 293 | switch { |
| 294 | case at == p.Commit || at != "" && at != p.Old && rd.reaches(p.Commit, at): |
| 295 | d.Action, d.Why = writeDone, "origin holds the report" |
| 296 | case rd.s.Refs[change.Ref(rebase.Target, p.ID)] == "": |
| 297 | stopped(&d, true, "origin no longer holds a record of %s, and the hook takes no report about a change it does not record. The report is kept here: %s, %s", |
| 298 | p.ID, p.Report.Result, oneLine(p.Report)) |
| 299 | d.Terminal = true |
| 300 | case found && sameReport(current.Report, p.Report): |
| 301 | d.Action, d.Why = writeDone, "origin's report already says the same" |
| 302 | case at != p.Old: |
| 303 | stopped(&d, true, "origin's report moved to %s since this one was built on %s; a report of what did happen is built in its place", short(at), orNone(p.Old)) |
| 304 | case !rd.have(p.Commit): |
| 305 | stopped(&d, true, "the scratch repository no longer holds the report commit %s; a report of what did happen is built in its place", short(p.Commit)) |
| 306 | case branches.Action != writeDone: |
| 307 | stopped(&d, true, "origin no longer shows the family's branch push: %s", branches.Why) |
| 308 | case says != "": |
| 309 | stopped(&d, true, "%s; a report of what did happen is built in its place", says) |
| 310 | default: |
| 311 | d.Action, d.Why = writeFinish, "origin's report is still the one it was built on, and this one says what did happen" |
| 312 | } |
| 313 | out = append(out, d) |
| 314 | } |
| 315 | return out |
| 316 | } |
| 317 | |
| 318 | // reaches reports whether a commit is in the history of another: whether |
| 319 | // a ref moved on from a write origin took. |
| 320 | func (rd reading) reaches(commit, at string) bool { |
| 321 | in, err := rd.is(commit, at) |
| 322 | return err == nil && in |
| 323 | } |
| 324 | |
| 325 | // holding is why the requests origin holds now might still land one of |
| 326 | // the family's heads: each related request whose outcome does not release |
| 327 | // it, as the planner judges them. A family's heads are every version head |
| 328 | // its changes have had, and the prepared heads. |
| 329 | func (rd reading) holding(f JournalFamily) []string { |
| 330 | if len(f.Versions) == 0 { |
| 331 | return nil |
| 332 | } |
| 333 | m := Members{Names: map[string]bool{}} |
| 334 | seen := map[string]bool{} |
| 335 | for _, v := range f.Versions { |
| 336 | m.Names[change.RequestName(v.ID)] = true |
| 337 | for _, version := range v.Record.Versions { |
| 338 | if version.Branch != "" { |
| 339 | m.Names[rebase.ChangeName(version.Branch)] = true |
| 340 | } |
| 341 | if h := rebase.HeadOf(version); !seen[h] { |
| 342 | seen[h] = true |
| 343 | m.Heads = append(m.Heads, h) |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | var held []string |
| 348 | for _, r := range rd.requests { |
| 349 | by, err := Relate(r, m, rd.in) |
| 350 | if err != nil { |
| 351 | held = append(held, fmt.Sprintf("request %s: whether it is related cannot be determined: %v", r.Name, err)) |
| 352 | continue |
| 353 | } |
| 354 | if by == nil { |
| 355 | continue |
| 356 | } |
| 357 | o, found := rd.s.OutcomeOf(r.Name) |
| 358 | if _, releases, why := Currency(o, found, r.Head, rd.s.Tip, rd.evidence); !releases { |
| 359 | held = append(held, fmt.Sprintf("request %s might still land: %s", r.Name, why)) |
| 360 | } |
| 361 | } |
| 362 | return held |
| 363 | } |
| 364 | |
| 365 | // recordNow says how the change's record moved from the record commit a |
| 366 | // version was built on. captured is the head the capture read the change |
| 367 | // at. |
| 368 | func (rd reading) recordNow(v *rebase.PendingVersion, captured string) string { |
| 369 | at := rd.s.Refs[v.Ref] |
| 370 | if at == "" { |
| 371 | return "origin no longer holds a record of " + v.Handle |
| 372 | } |
| 373 | for _, n := range rd.s.Changes { |
| 374 | if n.ID != v.ID { |
| 375 | continue |
| 376 | } |
| 377 | r, active := n.Record, n.Record.ActiveVersion() |
| 378 | switch { |
| 379 | case r.State == change.Withdrawn: |
| 380 | return v.Handle + " was withdrawn" |
| 381 | case r.State != change.Ready: |
| 382 | return fmt.Sprintf("%s is %s, not ready", v.Handle, r.State) |
| 383 | case rebase.HeadOf(active) != captured: |
| 384 | return fmt.Sprintf("%s is at v%d, %s: its author moved it", v.Handle, active.N, short(rebase.HeadOf(active))) |
| 385 | } |
| 386 | return fmt.Sprintf("the record of %s moved to %s since it was read, so the lease is gone", v.Handle, short(at)) |
| 387 | } |
| 388 | return fmt.Sprintf("the record of %s at %s does not read", v.Handle, short(at)) |
| 389 | } |
| 390 | |
| 391 | // capturedVersion is the version a family's layer was at when the capture |
| 392 | // read it, from the record the version was built on. |
| 393 | func capturedVersion(v *rebase.PendingVersion, l ObservedLayer) (change.Version, bool) { |
| 394 | for _, version := range v.Record.Versions { |
| 395 | if version.N == l.Version { |
| 396 | return version, true |
| 397 | } |
| 398 | } |
| 399 | return change.Version{}, false |
| 400 | } |
| 401 | |
| 402 | // oneLine is a report as one line, for a diagnostic. |
| 403 | func oneLine(r *change.RestackReport) string { |
| 404 | var layers []string |
| 405 | for _, l := range r.Layers { |
| 406 | s := fmt.Sprintf("%s v%d at %s", l.ChangeID, l.Version, short(strings.TrimPrefix(l.Head, change.HeadPrefix))) |
| 407 | if l.Published != nil { |
| 408 | s += " published at " + short(strings.TrimPrefix(l.Published.Head, change.HeadPrefix)) |
| 409 | } |
| 410 | layers = append(layers, s) |
| 411 | } |
| 412 | return fmt.Sprintf("on %s at %s, operation %s: %s", r.Target, short(strings.TrimPrefix(r.TargetTip, change.HeadPrefix)), r.Operation, strings.Join(layers, "; ")) |
| 413 | } |
| 414 | |
| 415 | func orNone(oid string) string { |
| 416 | if oid == "" { |
| 417 | return "no report" |
| 418 | } |
| 419 | return short(oid) |
| 420 | } |