gunk-dev/the-valley

verdict.go

source5820f67029d8main's published tip history
13.6 KiB
1package main
2
3// The verdict: the whole of what integration decides, as a function of
4// data. Nothing here reads git, runs nix, or looks at a clock — a transport
5// gathers all of that into a Snapshot first, and this file turns the
6// Snapshot into a decision. That split is what makes the rules of
7// dcr-439b771 testable without a repository, and it is why the refs
8// transport below is the only thing that has to change when a second one
9// arrives.
10//
11// Integration is optimistic concurrency control over content-addressed
12// evidence. A change is a transaction; its snapshot is the base tree its
13// attestations were produced over; its read set is the union of its
14// required checks' input closures; its write set is its delta. The
15// integrator is the commit point, and the commit rule has two parts: the
16// delta must apply cleanly to the current tip, and per required check the
17// evidence must transfer.
18
19import (
20 "fmt"
21 "sort"
22 "strings"
23 "time"
24)
25
26// Outcome is what the integrator does with a change.
27type Outcome string
28
29const (
30 // Land: the delta applies and every required check's evidence stands.
31 Land Outcome = "land"
32
33 // Stale: the change is behind, not wrong. Either the delta no longer
34 // applies or some evidence no longer transfers. The request stays put
35 // and retry is partial — only the named checks need new evidence.
36 Stale Outcome = "stale"
37
38 // Reject: the evidence is not evidence. A note whose signature does
39 // not check out, text that is not the written form of what it says, or
40 // a statement about a different tree is not a staleness problem, and
41 // re-attesting would not fix it.
42 Reject Outcome = "reject"
43)
44
45// Kind is the two kinds of claim of design/verification.md, which decide
46// which transfer rule applies.
47type Kind string
48
49const (
50 Pure Kind = "pure"
51 Effectful Kind = "effectful"
52)
53
54// Admissibility is what a verifier made of an attestation before any
55// transfer rule was applied.
56type Admissibility string
57
58const (
59 // Admissible: the note verifies under a key the integrator holds, its
60 // text is the written form of what it says, and its subject is the
61 // tree the change produces.
62 Admissible Admissibility = "admissible"
63
64 // UnknownSigner: the note is well formed but nobody who signed it is
65 // in the known-signers file. Evidence from a signer the integrator
66 // does not hold does not transfer. Deciding what to do about an
67 // unknown attester — re-verification, trust measurement — belongs to
68 // the trust backstop and is deliberately not here.
69 UnknownSigner Admissibility = "unknown-signer"
70
71 // Tampered: everything else a verifier can refuse — a signature that
72 // does not check out over the text, text that is not its own written
73 // form, a statement whose subject digest is not the change's tree.
74 Tampered Admissibility = "tampered"
75)
76
77// Change is the integration primitive (ida-93e4f91): a delta targeting a
78// version stream, with the provenance and the attestations produced over
79// the tree it results in. The branch it arrived on is transport.
80type Change struct {
81 // ID names the change. The refs transport takes it from the last
82 // segment of the request ref.
83 ID string
84
85 // Target is the protected ref the delta targets, in full.
86 Target string
87
88 // Base is the commit the delta was authored against, and Head the
89 // commit whose tree the attestations name.
90 Base string
91 Head string
92
93 // Attested is the valley-tree-v1 digest of Head's tree — the snapshot
94 // the transaction read, and the subject every attestation names.
95 Attested string
96
97 // Evidence is the attestations found for this change, by check name.
98 Evidence map[string]Evidence
99
100 // Provenance is the run record the attestations carry, as the first
101 // statement stated it. Phase 2 records a delegation chain and Phase 3
102 // does not yet check one, so this is carried into the outcome for a
103 // reader and is not consulted by any rule below.
104 Provenance string
105}
106
107// Evidence is one attestation as the integrator read it back.
108type Evidence struct {
109 Check string
110 Kind Kind
111 Admissible Admissibility
112
113 // Result is what the statement recorded. A statement recording a
114 // failure is not evidence that a check passed.
115 Result string
116
117 // Inputs is the input-closure digest the statement recorded, for a
118 // pure check. This is the read set the transfer rule compares.
119 Inputs string
120
121 // ObservedAt is when an effectful check's result was observed.
122 ObservedAt time.Time
123
124 // TextDigest is a SHA-256 over the bytes the signature covers. The
125 // transfer statement cites the original by this digest.
126 TextDigest string
127
128 // Signer names the key whose signature the integrator accepted, for
129 // the reader of an outcome. Empty when none was accepted.
130 Signer string
131
132 // Carriage: the note as stored, the bytes its signature covers, the
133 // ref and path it was found at, and what a verifier refused it with.
134 // No rule below reads any of it; what lands does.
135 note []byte
136 text []byte
137 ref string
138 entry string
139 refusal string
140}
141
142// Required is one check the policy demands of this change, as the deriver
143// named it.
144type Required struct {
145 Name string
146
147 // Classes are the path classes that made this check required. An
148 // effectful check's scope is these classes' paths: an intervening
149 // landing inside one of them is what ends the observation's validity.
150 Classes []string
151
152 // Runner and Attribute are how the check is run, from the composed
153 // policy's catalogue.
154 Runner string
155 Attribute string
156
157 // Validity is Δ: how long an effectful observation of this check stays
158 // good. Zero means the policy declares no window, and absent a
159 // declared window the check is re-demanded — a window nobody wrote
160 // down is not a window the integrator may invent.
161 Validity time.Duration
162}
163
164// Applied is what happened when the delta was applied to the current tip.
165type Applied struct {
166 // Clean says the delta applied with no conflict. A conflict means
167 // resolving it would produce a tree nobody has authored, and new
168 // authorship needs new evidence.
169 Clean bool
170
171 // Conflict is what the merge said, for the reader of the outcome.
172 Conflict string
173
174 // Tree is the valley-tree-v1 digest of the resulting tree. Equal to
175 // the change's Attested digest exactly when nothing the change reads
176 // has moved, whatever else landed in between.
177 Tree string
178}
179
180// Snapshot is everything about a change that had to be computed by
181// touching git, nix, or the clock. A transport fills it in; the verdict is
182// a function of it.
183type Snapshot struct {
184 Apply Applied
185
186 // Required is the check set derived from the diff by the policy
187 // machinery, read at the target tip and never from the submitted tree.
188 Required []Required
189
190 // Closures holds each pure check's input-closure digest recomputed on
191 // the rebased tree, keyed by check name. A check missing from both
192 // this map and ClosureErrors was not recomputed because it did not
193 // need to be.
194 Closures map[string]string
195
196 // ClosureErrors holds why a recomputation could not be done. A closure
197 // the integrator cannot compute is a check it cannot transfer.
198 ClosureErrors map[string]string
199
200 // Touched are the policy classes that intervening landed changes
201 // touched, between the change's base and the current tip.
202 Touched map[string]bool
203
204 // Now is when the judgement is being made, for the effectful window.
205 Now time.Time
206}
207
208// CheckVerdict is one check's outcome at the commit point.
209type CheckVerdict struct {
210 Name string
211
212 // Transferred says the evidence stands on the landed tree.
213 Transferred bool
214
215 // Rule is which transfer rule carried it: "unmoved" when the landed
216 // tree is the attested tree, "closure" when the recomputed input
217 // closure equalled the attested one, "untouched" when an effectful
218 // observation's scope was untouched and it was inside its window.
219 Rule string
220
221 // Reason says why it did not transfer, in words a contributor can act
222 // on. Empty when it did.
223 Reason string
224
225 // Evidence is the digest of the statement text this verdict is about,
226 // and Inputs the closure digest recomputed under the closure rule.
227 Evidence string
228 Inputs string
229
230 Signer string
231}
232
233// Verdict is what the integrator decided, and everything the outcome event
234// and the transfer statement are written from.
235type Verdict struct {
236 Outcome Outcome
237
238 // Reason is the one-line summary a reader gets. For a rejection it is
239 // what was wrong; for staleness, which rule the change fell to.
240 Reason string
241
242 // StaleReason distinguishes the two ways a change goes stale, as
243 // #RequestStale carries it: "conflict" or "evidence".
244 StaleReason string
245
246 // Checks is the per-check verdict, in the order the checks were
247 // required.
248 Checks []CheckVerdict
249
250 // Invalidated is exactly the checks that need new evidence — the
251 // event's payload, and the whole of what a contributor has to redo.
252 Invalidated []string
253}
254
255// Decide applies the commit rule of dcr-439b771 to a change.
256func Decide(ch Change, s Snapshot) Verdict {
257 required := append([]Required(nil), s.Required...)
258 sort.Slice(required, func(i, j int) bool { return required[i].Name < required[j].Name })
259
260 // First part of the commit rule: the delta must apply cleanly. A
261 // conflict is new authorship, so every check is re-demanded rather
262 // than a subset — there is no partial retry from a tree nobody wrote.
263 if !s.Apply.Clean {
264 v := Verdict{
265 Outcome: Stale,
266 StaleReason: "conflict",
267 Reason: "the delta does not apply cleanly to the tip; resolving it is new authorship, so every check needs new evidence",
268 }
269 for _, r := range required {
270 v.Checks = append(v.Checks, CheckVerdict{Name: r.Name, Reason: "the delta conflicts, so the tree this would be evidence about does not exist yet"})
271 v.Invalidated = append(v.Invalidated, r.Name)
272 }
273 return v
274 }
275
276 unmoved := s.Apply.Tree != "" && s.Apply.Tree == ch.Attested
277
278 v := Verdict{Outcome: Land}
279 for _, r := range required {
280 cv := judge(ch, s, r, unmoved)
281 v.Checks = append(v.Checks, cv)
282 switch {
283 case cv.Rule == "rejected":
284 // A forgery ends the judgement: the change is not behind, its
285 // evidence is not evidence.
286 return Verdict{
287 Outcome: Reject,
288 Reason: fmt.Sprintf("%s: %s", r.Name, cv.Reason),
289 Checks: v.Checks,
290 }
291 case !cv.Transferred:
292 v.Invalidated = append(v.Invalidated, r.Name)
293 }
294 }
295 if len(v.Invalidated) > 0 {
296 v.Outcome = Stale
297 v.StaleReason = "evidence"
298 v.Reason = fmt.Sprintf("%d of %d required checks no longer carry evidence that transfers", len(v.Invalidated), len(required))
299 return v
300 }
301 if len(required) == 1 {
302 v.Reason = "the one required check carries evidence that transfers"
303 } else {
304 v.Reason = fmt.Sprintf("all %d required checks carry evidence that transfers", len(required))
305 }
306 return v
307}
308
309// refusalLine is the sentence a verifier refused with. attest reports what
310// it checked and then the refusal, prefixed with its own name; the lines
311// after the refusal elaborate it and the reader of a verdict wants the
312// sentence, not the elaboration.
313func refusalLine(s string) string {
314 var last string
315 for _, line := range strings.Split(strings.TrimSpace(s), "\n") {
316 if why, ok := strings.CutPrefix(line, "attest: "); ok {
317 return strings.TrimSpace(why)
318 }
319 if strings.TrimSpace(line) != "" {
320 last = strings.TrimSpace(line)
321 }
322 }
323 return last
324}
325
326// judge is the second part of the commit rule, for one check.
327func judge(ch Change, s Snapshot, r Required, unmoved bool) CheckVerdict {
328 cv := CheckVerdict{Name: r.Name}
329 ev, ok := ch.Evidence[r.Name]
330 if !ok {
331 cv.Reason = "no attestation for this check accompanies the change"
332 return cv
333 }
334 cv.Evidence, cv.Signer = ev.TextDigest, ev.Signer
335
336 switch ev.Admissible {
337 case Tampered:
338 cv.Rule = "rejected"
339 cv.Reason = "the attestation does not verify as the statement it claims to be"
340 if why := refusalLine(ev.refusal); why != "" {
341 cv.Reason += ": " + why
342 }
343 return cv
344 case UnknownSigner:
345 cv.Reason = "signed by no signer the integrator holds, so its evidence does not transfer"
346 return cv
347 }
348 if ev.Result != "passed" {
349 cv.Reason = fmt.Sprintf("the attestation records the check as %s", ev.Result)
350 return cv
351 }
352
353 if ev.Kind == Pure {
354 // Content-addressed evidence does not age: a pure attestation
355 // carries no expiry, because time is not among its inputs.
356 if unmoved {
357 cv.Transferred, cv.Rule = true, "unmoved"
358 return cv
359 }
360 if why, bad := s.ClosureErrors[r.Name]; bad {
361 cv.Reason = "the input closure could not be recomputed on the landed tree: " + why
362 return cv
363 }
364 got, ok := s.Closures[r.Name]
365 if !ok {
366 cv.Reason = "the input closure was not recomputed on the landed tree"
367 return cv
368 }
369 if got != ev.Inputs {
370 cv.Reason = fmt.Sprintf("its input closure changed: attested %s, landed tree %s", short(ev.Inputs), short(got))
371 return cv
372 }
373 cv.Transferred, cv.Rule, cv.Inputs = true, "closure", got
374 return cv
375 }
376
377 // An effectful observation transfers while its scope is untouched and
378 // it is recent enough. Serializable semantics against mutable world
379 // state are unattainable in principle, so the honest semantics are
380 // bounded staleness.
381 for _, class := range r.Classes {
382 if s.Touched[class] {
383 cv.Reason = fmt.Sprintf("an intervening landed change touched the %s class, which is what this check reads", class)
384 return cv
385 }
386 }
387 if r.Validity == 0 {
388 cv.Reason = "the policy declares no validity window for this effectful check, so its observation is re-demanded"
389 return cv
390 }
391 if age := s.Now.Sub(ev.ObservedAt); age >= r.Validity {
392 cv.Reason = fmt.Sprintf("the observation is %s old and the declared window is %s", age.Round(time.Second), r.Validity)
393 return cv
394 }
395 cv.Transferred, cv.Rule = true, "untouched"
396 return cv
397}
398
399func short(digest string) string {
400 if len(digest) > 12 {
401 return digest[:12]
402 }
403 return digest
404}