gunk-dev/the-valley

plan.go

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