gunk-dev/the-valley

verification.cue

sourcef1e680d2de7cmain's published tip history
16.2 KiB
1// The verification policy of a valley project: which checks a change must
2// carry, derived from the paths its diff touches. Policy is data, so it is
3// a CUE document like the host config and the event vocabulary
4// (ida-1ec03b1).
5//
6// A policy is composed from two documents, never one. The instance layer —
7// the group's floor and its project-type templates — is carried in the
8// instance repository. The project layer is carried in the project's own
9// tree. The effective policy is the unification of the two, and because
10// unification can only narrow, the instance layer can state things a
11// project cannot take back. That is the whole of the mandatory-versus-
12// overridable distinction; #Requires spells it out.
13//
14// Two tools read this file. `valley checks` composes the two layers with it
15// and prints the checks the policy requires of a change, and the change's
16// carry mode (dcr-f41f718); that deriver only reports. The Phase 3
17// integrator asks the deriver the same question and acts on the answer,
18// which is the enforcement point (archive/design/integration.md).
19//
20// Like the host and event schemas, this file is deliberately not Nix. A
21// policy names checks; it never names derivations, systems, or store
22// paths, so it travels between machines unchanged.
23//
24// Compose an instance floor with a project policy and validate both:
25// cue vet -c schema/verification.cue \
26// examples/policy/instance/*.cue examples/policy/project/*.cue
27package verification
28
29import "list"
30
31// #Policy is one layer's contribution to a verification policy: the checks
32// it can require, the path classes that require them, what a path no class
33// covers costs, and the restacker's cap. Both layers are the same shape,
34// because the composition is unification and unification needs both sides
35// to agree on structure.
36#Policy: {
37 // checks maps a check's name to how that check is run. Classes
38 // require checks by name; a name that is not a key here fails vet
39 // (see the cross-check below), because a required check nobody can
40 // run is the one failure this document must never have. Either layer
41 // may contribute entries; two layers describing the same check must
42 // describe it identically, or unification conflicts.
43 checks: [#Name]: #Check
44
45 // The path classes, keyed by class name. Matching is a set operation,
46 // never first-match: a change's required checks are the union of
47 // `requires` over every class that at least one changed path matches.
48 // Classes may overlap on purpose — a knowledge node is both knowledge
49 // and prose — and overlapping classes compose to the maximum of what
50 // they ask for. That union is the whole of the "mixed commits take the
51 // max" rule (ida-1ec03b1): there is no separate mixed case to get
52 // wrong.
53 //
54 // The name is the key rather than a field, because classes compose
55 // across the two layers and CUE unifies lists positionally — two lists
56 // of different lengths simply conflict. Keying by name makes the
57 // composition field-wise instead: when both layers name the same
58 // class, it is one class, and its `paths` and `requires` are the union
59 // of what each layer contributed.
60 classes: [#Name]: #Class
61
62 // The checks required when a changed path matches no class at all.
63 // A path outside every class means the policy is behind the tree, so
64 // the safe answer is the strictest set the project has rather than
65 // the empty one. An empty set is allowed, and is a deliberate
66 // statement that uncovered paths are unconstrained.
67 unclassified: #Requires
68
69 // The restacker rebases a ready change when its target moves, and adds
70 // each rebased result to the change as a new version. Those are the
71 // change's automatic versions. maxAutomaticVersions caps how many the
72 // restacker adds to one change over the change's life. The cap is
73 // policy so that it is composed, and hashed into the policy digest,
74 // with everything else here. A layer that sets the cap writes a plain
75 // number. A second default would leave the cap with no default, and vet
76 // would fail.
77 restack: {maxAutomaticVersions: int & >=1 | *64}
78}
79
80// #Class is one path class and what touching it requires.
81#Class: {
82 // The paths this class covers, as a set of globs. Patterns are matched
83 // against each changed path of the diff, relative to the repository
84 // root: `*` matches within one path segment, `**` matches any number
85 // of segments including none. Every side of the diff counts — an
86 // added, modified, or deleted path, and both the old and the new path
87 // of a rename — because a check's scope is what the tree looked like
88 // as much as what it looks like now.
89 //
90 // A pattern's value says whether the class covers it, and it carries
91 // the same mandatory bit as #Requires: a pattern the instance layer
92 // pinned to `true` cannot be dropped by a project. Without that, a
93 // project could narrow a floor class's coverage down to a path it
94 // never touches and escape the floor everywhere else.
95 paths: #Paths
96
97 // The checks a change touching this class must carry. Every name must
98 // be a key of the composed policy's `checks`.
99 requires: #Requires
100
101 // How far an approval of a change touching this class carries when the
102 // change is rebased onto a target that has moved. #Carry spells out the
103 // modes.
104 carry: #Carry
105}
106
107// #Carry is the set of carry modes a class asks for, in the same
108// set-as-struct encoding as #Requires. A class's resolved mode is the
109// strictest mode whose key is `true`, and `same-paths` when no key is
110// (#Resolve).
111//
112// An approval carries across a rebase only when the rebased change has
113// exactly the files the approval saw. The mode adds a condition on the
114// landings since the approval. From the strictest:
115//
116// - `exact` — there were none: the target has not moved.
117// - `same-class` — none of them touched a path in any class the change
118// touches.
119// - `same-paths` — no condition. It is not a key: it is what a class with
120// no key `true` asks for.
121//
122// The layers compose the way #Requires does, and the value's form is the
123// mandatory bit:
124//
125// - The floor writes a concrete `true`, which no project can drop: a
126// project writing `false` conflicts, and `cue vet` names the field.
127// - A template writes `bool | *true`, a default a project may turn off.
128// - A project may always add a stricter key.
129//
130// The modes are keys rather than one string field because the three layers
131// unify. Two different strings conflict, so a string field could not say
132// "the strictest of the layers".
133#Carry: [#Mode]: bool
134
135// #Mode is a carry mode a class can ask for: either mode stricter than the
136// default `same-paths`.
137#Mode: "same-class" | "exact"
138
139// #Resolve reads a carry set as the one mode it means.
140#Resolve: {
141 carry: #Carry
142 let on = [for m, v in carry if v {m}]
143 mode: [
144 if list.Contains(on, "exact") {"exact"},
145 if list.Contains(on, "same-class") {"same-class"},
146 "same-paths",
147 ][0]
148}
149
150// #Requires is a set of check names written as a struct, with the value
151// saying whether the check is required. The value's *form* is the one bit
152// that separates a mandatory requirement from a suggested one, expressed
153// in CUE's own semantics rather than in a flag this schema would have to
154// interpret:
155//
156// - `true` — concrete, and unification can only narrow, so a project
157// supplying `false` conflicts and `cue vet` fails naming the field.
158// This is what the instance floor writes for a check that is not
159// negotiable.
160// - `bool | *true` — a disjunction with a marked default, so it reads as
161// `true` when nobody says otherwise and unifies cleanly with a
162// project's `false`. This is what a project-type template writes for a
163// check that is a sensible starting point rather than a rule.
164//
165// A project may always ADD names here, because the struct is open below
166// the keys the instance layer pinned; it can never remove one the floor
167// made concrete.
168#Requires: [#Name]: bool
169
170// #Paths is the same set-as-struct encoding, keyed by glob rather than by
171// check name, and read the same way: `true` pins the coverage, `bool |
172// *true` offers it.
173#Paths: [#Pattern]: bool
174
175// #Check is how one named check is run. In the reference implementation a
176// check is a flake check: `nix build .#checks.<system>.<attribute>`, where
177// the system is the runner's own and never the policy's — a policy that
178// named a system would stop travelling.
179#Check: {
180 // The runner kind, named the way schema/attestation.cue's #CheckRun
181 // names it, so a policy and the statement satisfying it agree on what
182 // a check is. "nix" builds a flake check attribute and claims purity
183 // strongly; "command" runs a command line and claims nothing beyond
184 // having run it, which is the effectful class of
185 // archive/design/verification.md.
186 runner: "nix" | "command"
187
188 // The flake check attribute, for the nix runner.
189 attribute?: #Name
190
191 // The command line, for the command runner. One line, because a
192 // statement about it is written as lines and the form has no escapes.
193 command?: =~"^[^\\x00-\\x1f\\x7f]+$"
194
195 if runner == "nix" {
196 attribute: #Name
197 command?: _|_
198 // A pure check's evidence does not age, because time is not among
199 // its inputs. A window declared over one would be a window nothing
200 // reads, so it is refused rather than ignored — and the refusal is
201 // a sentinel rather than bottom, so `cue vet` names the field.
202 validity?: "INVALID: a pure check's evidence does not age; a validity window is for an effectful check"
203 }
204 if runner == "command" {
205 command: =~"^[^\\x00-\\x1f\\x7f]+$"
206 attribute?: _|_
207 }
208
209 // Δ: how long an observation of this check stays good once made
210 // (dcr-439b771). It exists for effectful checks alone. A pure check's
211 // evidence is content-addressed and does not age, because time is not
212 // among its inputs; an effectful check reads mutable world state,
213 // which has no digest, so the honest semantics are bounded staleness
214 // — an observation transfers while nothing relevant has changed under
215 // it and it is recent enough.
216 //
217 // Optional, and its absence is not a default. Absent a declared
218 // window the integrator re-demands the check: a window nobody wrote
219 // down is not a window a tool may invent. Like every other field
220 // here it composes across the two layers, so a group can floor a
221 // window and a project can only narrow it.
222 validity?: #Duration
223
224 // What running the check costs its runner. A "quick" check is cheap.
225 // A "slow" check takes minutes, or needs special hardware such as
226 // virtualisation support.
227 //
228 // The cost is a declared property. `valley checks` groups the required
229 // checks by it and can filter them by it. The integrator does not
230 // read it: approving runs every required check, and the integrator
231 // requires evidence for every required check, whatever its cost.
232 cost: *"quick" | "slow"
233}
234
235// A validity window: a whole number of seconds, minutes, hours or days.
236// Written as text rather than as a number so that the unit is in the
237// document a person reads, and bounded to units a policy has a use for —
238// a window in milliseconds is not a policy, and one in months is not a
239// window.
240#Duration: =~"^[1-9][0-9]*(s|m|h|d)$"
241
242// #Templates is the instance layer's set of project types. A project
243// selects one by name and then narrows it, so a project that is an
244// ordinary instance of its type needs almost no configuration of its own.
245#Templates: [#Name]: #Policy
246
247// Names are lowercase, dash-separated, and short. They are quoted in
248// integrator output and carried in attestation payloads, so they are held
249// to the same shape as project names in schema/valley.cue. The shape also
250// keeps every control character out of a class or check name, which
251// `valley checks` relies on (see #Pattern).
252#Name: =~"^[a-z0-9][a-z0-9-]*$"
253
254// A path glob, relative to the repository root. A leading slash and a `..`
255// segment are both rejected: a pattern that reaches outside the tree is
256// describing something a diff cannot contain, and silently matching
257// nothing is the expensive way to find that out.
258//
259// A control character is rejected too. `valley checks` reads the composed
260// policy as lines of tab-separated fields, and a pattern is one of those
261// fields. A newline in a pattern would end its line and start a forged one,
262// such as a carry mode for a class the floor pinned. A tab would add a
263// field. Git allows both in a path, but a policy has no use for a pattern
264// that matches one.
265#Pattern: =~"^[^/]" & !~"(^|/)\\.\\.(/|$)" & !~"[\\x00-\\x1f\\x7f]"
266
267// ----------------------------------------------------------------------
268// The composition.
269//
270// The two layers meet here. The schema cannot tell which file a field came
271// from, so the tool that composes a policy is what makes the floor a
272// floor: `floor` and `templates` must be read from the instance
273// repository's integrated tip, never from the project's tree. A project
274// that vendored a copy of the floor and edited the copy would unify
275// perfectly well — unification forces nothing on its own. What forces is
276// provenance: changing the floor is a change landing in the instance
277// repository, under that repository's own policy.
278//
279// The two halves resolve at different times, on purpose. The floor
280// resolves at head, because a floor a project could pin is not a floor.
281// Templates may be pinned, because they are defaults and a stale default
282// is harmless.
283
284// The instance layer, read from the instance repository's integrated tip.
285floor: #Policy
286templates: #Templates
287
288// The project type that adds nothing, so that a project which declares no
289// type still composes. It is defined here rather than left to each
290// instance because it is a property of the composition, not of any group.
291templates: none: {}
292
293// The project layer, read from the project's own tree. Neither field has
294// to be written: `type` defaults to the project type that adds nothing,
295// and an unwritten `project` is three empty sets. A project with no policy
296// document at all therefore composes — to the floor exactly, which is the
297// point of having one.
298type: #Name | *"none"
299project: #Policy
300
301// The effective policy for this project: what the integrator derives a
302// change's required checks from. Naming an unknown project type is an
303// evaluation error rather than a silent fallback. The `let` exists so the
304// cross-check below can read the composition without `policy` referring to
305// itself; every error still surfaces under `policy`, where a reader is
306// looking.
307let composed = floor & templates[type] & project
308policy: composed
309
310// Every check a class requires, and every check `unclassified` names, must
311// exist in the composed catalogue. A name that does not lands in `checks`
312// as an incomplete #Check, so `cue vet -c` fails with an error naming the
313// missing check. Without this, a typo is not a vet error — it is a check
314// that silently stops being required. The cross-check runs against the
315// composition rather than either layer, because a floor may legitimately
316// require a check whose definition a template supplies.
317policy: {
318 for _, c in composed.classes for n, _ in c.requires {
319 checks: (n): _
320 }
321 for n, _ in composed.unclassified {
322 checks: (n): _
323 }
324}
325
326// The resolved carry mode of every class of the composed policy, keyed by
327// class name. A change's carry mode is the strictest of these among the
328// classes its paths match, and an unclassified path is `same-paths`.
329// `valley checks` reads this field and reports that mode. The field is
330// hidden because the sentinel below rejects every other top-level field.
331//
332// It reads `policy` rather than `composed`, because a document may refine
333// `policy` directly. A key written there is in `policy` and not in
334// `composed`, and the deriver matches paths and reads requirements from
335// `policy`. Reading `composed` here would miss a mode added that way, or a
336// whole class.
337_mode: {for n, c in policy.classes {(n): (#Resolve & {carry: c.carry}).mode}}
338
339// A file's top level cannot be closed (embedding a definition would open
340// it instead — embedding lifts closedness), so reject stray top-level
341// fields explicitly: anything but the composition's fields — a `class:`
342// typo, a host-side concern like `enable:` — conflicts with this sentinel
343// and fails vet with an error naming the field.
344[!="floor" & !="templates" & !="type" & !="project" & !="policy"]: "INVALID: unknown top-level field; only \"floor\", \"templates\", \"type\", \"project\" and \"policy\" are allowed"