gunk-dev/the-valley

contract.go

source88a5a486b52fmain's published tip history
9.6 KiB
1// Package knot is the plan graph: a project's plan kept as data, at one
2// ref of the project's repository, refs/the-valley/knots. Each item on the
3// ref is a knot: an outcome, a bug, an idea or a question, in the node
4// format schema/node.cue states. The ref is a cord, and each knot is tied
5// on it.
6//
7// The ref has its own root history. Its first commit has no parent, and
8// every later commit has the previous tip as its one parent. Each commit
9// is one operation, and its tree is the whole graph:
10//
11// outcomes/<id>-<slug>.md
12// bugs/<id>-<slug>.md
13// ideas/<id>-<slug>.md
14// questions/<id>-<slug>.md
15// comments/<knot id>/<stamp>-<op>.md
16//
17// This package is the one reading of the graph. The valley command reads
18// and writes it through this package, and the push hook validates every
19// push to the ref with it, so a graph one of them accepts is a graph the
20// other accepts. It depends on the standard library alone.
21//
22// This file is the admission contract: every limit the graph is held to,
23// the shapes of its names and references, and which rules the Go
24// validator shares with the CUE schema and the knowledge lint. Each is a
25// named constant, and contract_test.go tests each limit at its edge.
26package knot
27
28import (
29 "regexp"
30 "time"
31)
32
33// Ref is the one ref the graph lives at. The hook matches it exactly: a
34// ref that only begins with this name is no graph.
35const Ref = "refs/the-valley/knots"
36
37// The limits. A push that would take the graph past one is refused, and
38// the valley command refuses to write it.
39const (
40 // MaxKnots is the most knots a graph holds.
41 MaxKnots = 5000
42 // MaxComments is the most comments a graph holds, across every knot.
43 MaxComments = 20000
44 // MaxKnotSize is the most bytes one knot's file holds.
45 MaxKnotSize = 64 << 10
46 // MaxCommentSize is the most bytes one comment's file holds.
47 MaxCommentSize = 16 << 10
48 // MaxChangedPaths is the most files one commit adds or changes. One
49 // operation touches one or two, so this bounds what the hook reads
50 // for a push without bounding any operation.
51 MaxChangedPaths = 32
52 // MaxCommitSize is the most bytes a graph commit object holds.
53 MaxCommitSize = 16 << 10
54 // MaxTitle is the most bytes a title or a source holds.
55 MaxTitle = 200
56 // MaxSlug is the most bytes a slug holds.
57 MaxSlug = 48
58 // MaxName is the most bytes a principal, a project or a run ID holds.
59 MaxName = 64
60 // MaxListItems is the most entries one list field holds.
61 MaxListItems = 64
62 // MaxLink is the most bytes one entry of changes holds.
63 MaxLink = 256
64)
65
66// The bounds on the hook's walk of a graph's tree. They follow from the
67// limits above: the widest tree is one knot's comments, and the walk
68// visits the root's five directories, every knot, one comments directory
69// per knot, and every comment. The walk counts a subtree each time a tree
70// names it, so a tree that names one subtree many times is refused at
71// these bounds before it is read.
72const (
73 // MaxDepth is how deep trees nest, the root's tree counted as 1:
74 // comments/<id>/ is at 3.
75 MaxDepth = 3
76 // MaxTreeEntries is the most entries one tree holds.
77 MaxTreeEntries = MaxComments
78 // MaxEntries is the most entries the walk visits in all.
79 MaxEntries = 5 + 2*MaxKnots + MaxComments
80 // MaxTreeSize is the most bytes one tree object holds: MaxTreeEntries
81 // entries of the longest name, with a 32-byte object ID.
82 MaxTreeSize = 2 << 20
83 // MaxReadBytes is the most bytes the hook reads for one push: trees,
84 // the commits and the knots it parses.
85 MaxReadBytes = 64 << 20
86 // HookDeadline is how long the hook spends on one push's graph before
87 // it refuses the push.
88 HookDeadline = 5 * time.Second
89)
90
91// What the valley command does when a write does not land.
92const (
93 // Attempts is how many times the command tries one operation: the
94 // first try and four retries, each after a random pause.
95 Attempts = 5
96 // OpSearch is how many of the ref's latest commits the command
97 // searches for an operation's ID after a push whose outcome it cannot
98 // tell, before it tries the operation again.
99 OpSearch = 64
100 // StaleClaim is the age past which a claim is flagged as stale.
101 StaleClaim = 24 * time.Hour
102)
103
104// IDRuleCutoff is the date after which every node's ID is derived from its
105// slug (archive/.the-valley/README.md). The knowledge lint checks the
106// derivation only for nodes created after it. Every knot is: a knot
107// created on or before the cutoff is refused, so the derivation rule is
108// the same in both validators for every graph the Go validator accepts.
109const IDRuleCutoff = "2026-08-05"
110
111// The shapes of names and references.
112var (
113 // localID is a knot's ID: a type prefix and the first 7 hex digits of
114 // the SHA-256 of its slug.
115 localID = regexp.MustCompile(`^(oc|bd|ida|qn)-[0-9a-f]{7}$`)
116 // qualifiedID names a knot in another project's graph:
117 // <project>:<id>.
118 qualifiedID = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]*:(oc|bd|ida|qn)-[0-9a-f]{7}$`)
119 // name is a principal, a project or a run ID, as schema/valley.cue
120 // names a principal (#PrincipalName).
121 name = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]*$`)
122 // slug is the part of a knot's filename after its ID.
123 slugShape = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`)
124 // date is a YYYY-MM-DD date, and timestamp a UTC time to the second.
125 date = regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}$`)
126 timestamp = regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$`)
127 // valleyChange links a change on a valley host:
128 // valley:<project>/<change ID>.
129 valleyChange = regexp.MustCompile(`^valley:[a-zA-Z0-9][a-zA-Z0-9._-]*/[k-z]{32}$`)
130 // githubPR links a pull request on GitHub.
131 githubPR = regexp.MustCompile(`^https://github\.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/pull/[1-9][0-9]*$`)
132 // knotFile, commentDir and commentFile are the names the layout
133 // allows below the root.
134 knotFile = regexp.MustCompile(`^((oc|bd|ida|qn)-[0-9a-f]{7})-([a-z0-9-]+)\.md$`)
135 commentFile = regexp.MustCompile(`^([0-9]{8}T[0-9]{6}Z)-([0-9a-f]{16})\.md$`)
136 // commentID is a comment's ID: its file's name without .md.
137 commentID = regexp.MustCompile(`^[0-9]{8}T[0-9]{6}Z-[0-9a-f]{16}$`)
138 // opID is the ID an operation carries in its Valley-Op trailer.
139 opID = regexp.MustCompile(`^[0-9a-f]{16}$`)
140)
141
142// CommentStamp is the layout of the stamp a comment's filename opens with:
143// the UTC time the comment was written.
144const CommentStamp = "20060102T150405Z"
145
146// Rule names one admission rule. Every refusal the validator gives is
147// under one rule, so a test or a check can say which rule a graph breaks.
148type Rule string
149
150// The rules both validators enforce: the Go validator in this package,
151// and the CUE schema with the knowledge lint (schema/node.cue,
152// nix/knowledge-lint.py). The knot-agree check runs both over the same
153// graphs and fails when they disagree on any of these.
154const (
155 RuleType Rule = "type" // type is one of the four
156 RuleStatus Rule = "status" // status is in the type's enum
157 RuleIDShape Rule = "id-shape" // the ID has its type's prefix and 7 hex digits
158 RuleIDDerived Rule = "id-derived" // the ID's digits are derived from the slug
159 RuleFilename Rule = "filename" // the filename's ID is the frontmatter's
160 RuleDirectory Rule = "directory" // a knot lives in its type's directory
161 RuleUniqueID Rule = "unique-id" // no two knots share an ID
162 RuleFieldOnType Rule = "field-on-type" // a field appears only on the types that carry it
163 RuleFieldShape Rule = "field-shape" // names, references, links, dates and times have their shapes
164 RuleRequired Rule = "required" // the fields every knot of a type carries are there
165 RuleEdgeResolves Rule = "edge-resolves" // a local edge names a knot in the graph
166 RuleParentOutcome Rule = "parent-outcome" // parent names an outcome
167 RuleGraduated Rule = "graduated" // graduated_into is there exactly when an idea is graduated
168)
169
170// The rules the Go validator alone enforces. The canonical form, the
171// layout, cycles and transitions are the live graph's, and the archived
172// graph the lint was written for has none of them.
173const (
174 RuleCanonical Rule = "canonical" // the file is exactly as Render writes it
175 RuleText Rule = "text" // UTF-8, no control character but newline and tab
176 RuleLayout Rule = "layout" // only the allowed paths, as plain files
177 RuleLimit Rule = "limit" // within the limits above
178 RuleCycle Rule = "cycle" // parent and blocked_by edges form no cycle
179 RuleTransition Rule = "transition" // what a push may change
180 RuleProvenance Rule = "provenance" // created_by, author and edited_by name the pusher
181 RuleClaim Rule = "claim" // claimed_by and claimed_at come together
182 RuleCutoff Rule = "cutoff" // created after IDRuleCutoff
183)
184
185// SharedRules are the rules both validators enforce, in a fixed order.
186var SharedRules = []Rule{
187 RuleType, RuleStatus, RuleIDShape, RuleIDDerived, RuleFilename,
188 RuleDirectory, RuleUniqueID, RuleFieldOnType, RuleFieldShape,
189 RuleRequired, RuleEdgeResolves, RuleParentOutcome, RuleGraduated,
190}
191
192// GoRules are the rules the Go validator alone enforces.
193var GoRules = []Rule{
194 RuleCanonical, RuleText, RuleLayout, RuleLimit, RuleCycle,
195 RuleTransition, RuleProvenance, RuleClaim, RuleCutoff,
196}
197
198// Error is a refusal: the rule it is under, the path it is about, if any,
199// and what is wrong, as a sentence that names the valid values.
200type Error struct {
201 Rule Rule
202 Path string
203 Msg string
204}
205
206func (e *Error) Error() string {
207 if e.Path == "" {
208 return e.Msg
209 }
210 return e.Path + ": " + e.Msg
211}
212
213func refuse(rule Rule, path, msg string) *Error {
214 return &Error{Rule: rule, Path: path, Msg: msg}
215}