gunk-dev/the-valley

plan.go

source3ab28cc3b62emain's published tip history
11.1 KiB
1package main
2
3import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "math"
8 "strconv"
9 "strings"
10
11 "the-valley/change"
12 "the-valley/rebase"
13)
14
15// The restacker's own limits on a record it would change. Each is below
16// the push hook's (change/record.go), so a record the restacker leaves at
17// its limit still has room for its owner's own updates.
18const (
19 maxVersions = 900 // versions of one change
20 maxSeq = 99000 // record updates in a change's life
21 maxBytes = 48 << 10 // bytes of the record's encoded JSON
22)
23
24// What the planner would do with a family.
25const (
26 restack = "restack" // rebase every layer onto main's tip
27 hold = "hold" // a layer, a request or a limit stops it; Reasons say which
28 current = "current" // the family already descends from main's tip
29 leave = "leave" // no layer is a ready change, so the restacker has no business with it
30)
31
32// Plan is what one pass would do, from one snapshot of origin.
33type Plan struct {
34 Target string `json:"target"`
35 Tip string `json:"tip"`
36 Evidence string `json:"evidence"` // change.EvidenceFingerprint of every attestation ref
37 Cap int `json:"maxAutomaticVersions"` // the policy's cap as CapOf counts it
38 Operation string `json:"operation"` // the name the pass's writes are built under
39 Recovery *Recovery `json:"recovery"` // what recovery decided of the journal in the state directory; null for none
40 Families []FamilyPlan `json:"families"`
41
42 families []*rebase.Family // each family as captured, as Families
43}
44
45// FamilyPlan is one family of layers, bottom first, and what would become
46// of it.
47type FamilyPlan struct {
48 Layers []LayerPlan `json:"layers"`
49 Action string `json:"action"`
50 Reasons []string `json:"reasons"`
51 Notes []string `json:"notes"`
52 Requests []RequestPlan `json:"requests"`
53 Writes *Writes `json:"writes,omitempty"` // what a pass would push for it; absent for a family left alone or current
54}
55
56// LayerPlan is one layer as the snapshot shows it, with the capacity of
57// the record a restack would change; Capacity is nil for a plain branch.
58type LayerPlan struct {
59 Name string `json:"name"`
60 ID string `json:"id,omitempty"`
61 Branch string `json:"branch"`
62 Head string `json:"head"`
63 State string `json:"state"`
64 Below string `json:"below,omitempty"`
65 Capacity *Capacity `json:"capacity,omitempty"`
66}
67
68// Capacity is how close a record is to each limit, counted as it would be
69// once the restack added a version.
70type Capacity struct {
71 Uploads int `json:"uploads"` // versions the principal uploaded, the new one included
72 Cap int `json:"maxAutomaticVersions"`
73 Versions int `json:"versions"`
74 MaxVersions int `json:"maxVersions"`
75 Seq int `json:"seq"`
76 MaxSeq int `json:"maxSeq"`
77 Bytes int `json:"bytes"`
78 MaxBytes int `json:"maxBytes"`
79 Over []string `json:"over"` // each limit the record would pass, as a sentence
80}
81
82// RequestPlan is an integration request related to a family: how it is
83// related, and whether its outcome is current.
84type RequestPlan struct {
85 Name string `json:"name"`
86 Head string `json:"head"`
87 By []string `json:"by"` // name, head, range
88 Decision string `json:"decision,omitempty"`
89 Current bool `json:"current"`
90 Releases bool `json:"releases"`
91 Why string `json:"why"`
92}
93
94// Request is an integration request on origin: its name under
95// rebase.RequestPrefix and the head it asks to land.
96type Request struct {
97 Name, Head string
98}
99
100// Members is what a request may be related to a family by: the request
101// names of its changes, and its heads. A change's heads are every version
102// head it has ever had, not only the active one, so a request filed for an
103// older version under any name still holds the family. A plain branch's
104// head is its tip.
105type Members struct {
106 Names map[string]bool
107 Heads []string
108}
109
110// membersOf collects a family's request names and heads. A change's names
111// are c-<change ID> and the branch-derived name of every version it has
112// ever had, since a request filed under an earlier version's branch is
113// still about the change. A plain branch's name is its own.
114func membersOf(f *rebase.Family) Members {
115 m := Members{Names: map[string]bool{}}
116 seen := map[string]bool{}
117 head := func(h string) {
118 if h != "" && !seen[h] {
119 seen[h] = true
120 m.Heads = append(m.Heads, h)
121 }
122 }
123 for _, l := range f.Layers {
124 if l.ID == "" {
125 m.Names[rebase.ChangeName(l.Branch)] = true
126 head(l.Head)
127 continue
128 }
129 m.Names[change.RequestName(l.ID)] = true
130 head(l.Head)
131 for _, v := range l.Record.Versions {
132 if v.Branch != "" {
133 m.Names[rebase.ChangeName(v.Branch)] = true
134 }
135 head(rebase.HeadOf(v))
136 }
137 }
138 return m
139}
140
141// Incoming is the commits a request's head would bring to the target: the
142// commits it reaches that the target's tip does not. An error is a head
143// whose commits could not be read, as for an object that is missing or is
144// not a commit.
145type Incoming func(head string) (map[string]bool, error)
146
147// Ancestry answers whether a is an ancestor of b, or b itself. An error is
148// an answer that could not be had, as for an object that is missing or is
149// not a commit.
150type Ancestry func(a, b string) (bool, error)
151
152// Relate says how a request is related to a family: by name, by an exact
153// head, and by an incoming range that contains a head of the family. No
154// answer and a nil error is unrelated. An error is a relation that could
155// not be determined, which the caller must treat as related.
156func Relate(r Request, m Members, in Incoming) ([]string, error) {
157 var by []string
158 if m.Names[r.Name] {
159 by = append(by, "name")
160 }
161 for _, h := range m.Heads {
162 if h == r.Head {
163 by = append(by, "head")
164 break
165 }
166 }
167 if len(by) > 0 {
168 return by, nil
169 }
170 commits, err := in(r.Head)
171 if err != nil {
172 return nil, err
173 }
174 for _, h := range m.Heads {
175 if commits[h] {
176 return []string{"range"}, nil
177 }
178 }
179 return nil, nil
180}
181
182// Currency judges a related request's outcome: whether it lets the family
183// proceed, and why. An outcome is current when its head, its target tip
184// and its evidence fingerprint all equal what the planner sees. A current
185// stale releases the family, since the integrator will not land the
186// request at that head on that tip. So does a current reject, which the
187// caller notes. Anything else holds: no outcome, a record that does not
188// read, an outcome about another head, tip or evidence, which the
189// integrator will judge again, and a failure or a refusal.
190func Currency(o rebase.Outcome, found bool, head, tip, evidence string) (current, releases bool, why string) {
191 switch {
192 case !found:
193 return false, false, "no outcome yet, so the integrator has still to judge it"
194 case o.Decision == "unknown":
195 return false, false, "its outcome record does not read"
196 case o.Head != head:
197 return false, false, fmt.Sprintf("its outcome is about head %s, not %s", short(o.Head), short(head))
198 case o.Tip != tip:
199 return false, false, fmt.Sprintf("its outcome was judged on main at %s, not %s", short(o.Tip), short(tip))
200 case o.Evidence != evidence:
201 return false, false, fmt.Sprintf("its outcome was judged on evidence %s, not %s", o.Evidence, evidence)
202 }
203 switch o.Decision {
204 case "stale":
205 return true, true, "a current stale outcome: it cannot land as it stands"
206 case "reject":
207 return true, true, "a current reject outcome: the integrator rejected it"
208 case "failed":
209 return true, false, "the integrator failed on it at " + o.FailedAt
210 case "land", "voided-withdrawn":
211 return true, false, fmt.Sprintf("a current %s outcome with the request still filed", o.Decision)
212 }
213 return true, false, "the integrator refused it (" + o.Decision + ")"
214}
215
216// CapOf reads the policy's cap on automatic versions as the count the
217// planner compares uploads against. The schema admits any whole number of
218// at least 1, with no upper bound. A cap larger than an int holds is no
219// practical cap: it is counted as the largest int, and no record holds
220// that many versions. An absent cap, or one that is not a whole number of
221// at least 1, is refused, since the schema admits neither.
222func CapOf(stated json.Number) (int, error) {
223 if stated == "" {
224 return 0, errors.New("the composed policy states no restack.maxAutomaticVersions")
225 }
226 n, err := strconv.Atoi(string(stated))
227 switch {
228 case errors.Is(err, strconv.ErrRange) && n > 0:
229 return math.MaxInt, nil
230 case err != nil || n < 1:
231 return 0, fmt.Errorf("the composed policy's restack.maxAutomaticVersions is %s, not a whole number of at least 1", stated)
232 }
233 return n, nil
234}
235
236// CapacityOf counts a record against every limit as it would stand with
237// one more version uploaded by principal. size is the length of that
238// record's encoded JSON.
239func CapacityOf(r *change.Record, principal string, cap, size int) *Capacity {
240 c := &Capacity{
241 Uploads: 1, Cap: cap,
242 Versions: len(r.Versions) + 1, MaxVersions: maxVersions,
243 Seq: r.Seq + 1, MaxSeq: maxSeq,
244 Bytes: size, MaxBytes: maxBytes,
245 }
246 for _, v := range r.Versions {
247 if v.Uploader == principal {
248 c.Uploads++
249 }
250 }
251 if c.Uploads > c.Cap {
252 c.Over = append(c.Over, fmt.Sprintf("%s would have %d versions uploaded by %s, over the cap of %d", r.ChangeID, c.Uploads, principal, c.Cap))
253 }
254 if c.Versions > c.MaxVersions {
255 c.Over = append(c.Over, fmt.Sprintf("%s would have %d versions, over the %d the restacker allows", r.ChangeID, c.Versions, c.MaxVersions))
256 }
257 if c.Seq > c.MaxSeq {
258 c.Over = append(c.Over, fmt.Sprintf("%s's record would be at update %d, over the %d the restacker allows", r.ChangeID, c.Seq, c.MaxSeq))
259 }
260 if c.Bytes > c.MaxBytes {
261 c.Over = append(c.Over, fmt.Sprintf("%s's record would be %d bytes, over the %d the restacker allows", r.ChangeID, c.Bytes, c.MaxBytes))
262 }
263 return c
264}
265
266// nextSize is the length of a record's encoded JSON once a restack adds a
267// version. The rebased head is not known yet, so the version names a
268// stand-in of the same length; the title, the branch and the uploader are
269// the ones the restack would write. 0 is a record the engine would not
270// extend at all, which the version and update limits already hold.
271func nextSize(handle string, r *change.Record, principal string) int {
272 active := r.ActiveVersion()
273 stand := change.HeadPrefix + strings.Repeat("0", len(rebase.HeadOf(active)))
274 next, err := rebase.NextRecord(handle, r, change.Version{Head: stand, Title: active.Title, Branch: active.Branch, Uploader: principal})
275 if err != nil {
276 return 0
277 }
278 b, err := change.Encode(next)
279 if err != nil {
280 return 0
281 }
282 return len(b)
283}
284
285func short(oid string) string {
286 if len(oid) > 12 {
287 return oid[:12]
288 }
289 return oid
290}
291
292// stateLine names a layer's state for a reason, as a change is ready,
293// approved or submitted.
294func stateLine(l *rebase.Layer) string {
295 switch s := l.State(); s {
296 case rebase.Plain:
297 return l.Name + " is a plain branch, which no record is on"
298 case rebase.Draft:
299 return l.Name + " is a draft"
300 case rebase.Withdrawn:
301 return l.Name + " is withdrawn"
302 case rebase.Uncertain:
303 return l.Name + " may have landed already, with no evidence either way"
304 default:
305 return fmt.Sprintf("%s is %s", l.Name, s)
306 }
307}