gunk-dev/the-valley

plan.go

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