gunk-dev/the-valley

recover.go

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