| 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 decision, in the node |
| 4 | // format the archived knowledge graph uses (schema/node.cue). The ref is a |
| 5 | // cord, and each knot is tied 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 | // decisions/<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. |
| 26 | package knot |
| 27 | |
| 28 | import ( |
| 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. |
| 35 | const 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. |
| 39 | const ( |
| 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. |
| 72 | const ( |
| 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. |
| 92 | const ( |
| 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. |
| 109 | const IDRuleCutoff = "2026-08-05" |
| 110 | |
| 111 | // The shapes of names and references. |
| 112 | var ( |
| 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|dcr)-[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|dcr)-[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|dcr)-[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 | // opID is the ID an operation carries in its Valley-Op trailer. |
| 137 | opID = regexp.MustCompile(`^[0-9a-f]{16}$`) |
| 138 | ) |
| 139 | |
| 140 | // CommentStamp is the layout of the stamp a comment's filename opens with: |
| 141 | // the UTC time the comment was written. |
| 142 | const CommentStamp = "20060102T150405Z" |
| 143 | |
| 144 | // Rule names one admission rule. Every refusal the validator gives is |
| 145 | // under one rule, so a test or a check can say which rule a graph breaks. |
| 146 | type Rule string |
| 147 | |
| 148 | // The rules both validators enforce: the Go validator in this package, |
| 149 | // and the CUE schema with the knowledge lint (schema/node.cue, |
| 150 | // nix/knowledge-lint.py). The knot-agree check runs both over the same |
| 151 | // graphs and fails when they disagree on any of these. |
| 152 | const ( |
| 153 | RuleType Rule = "type" // type is one of the four |
| 154 | RuleStatus Rule = "status" // status is in the type's enum |
| 155 | RuleIDShape Rule = "id-shape" // the ID has its type's prefix and 7 hex digits |
| 156 | RuleIDDerived Rule = "id-derived" // the ID's digits are derived from the slug |
| 157 | RuleFilename Rule = "filename" // the filename's ID is the frontmatter's |
| 158 | RuleDirectory Rule = "directory" // a knot lives in its type's directory |
| 159 | RuleUniqueID Rule = "unique-id" // no two knots share an ID |
| 160 | RuleFieldOnType Rule = "field-on-type" // a field appears only on the types that carry it |
| 161 | RuleFieldShape Rule = "field-shape" // names, references, links, dates and times have their shapes |
| 162 | RuleRequired Rule = "required" // the fields every knot of a type carries are there |
| 163 | RuleEdgeResolves Rule = "edge-resolves" // a local edge names a knot in the graph |
| 164 | RuleParentOutcome Rule = "parent-outcome" // parent names an outcome |
| 165 | RuleGraduated Rule = "graduated" // graduated_into is there exactly when an idea is graduated |
| 166 | ) |
| 167 | |
| 168 | // The rules the Go validator alone enforces. The canonical form, the |
| 169 | // layout, cycles and transitions are the live graph's, and the archived |
| 170 | // graph the lint was written for has none of them. |
| 171 | const ( |
| 172 | RuleCanonical Rule = "canonical" // the file is exactly as Render writes it |
| 173 | RuleText Rule = "text" // UTF-8, no control character but newline and tab |
| 174 | RuleLayout Rule = "layout" // only the allowed paths, as plain files |
| 175 | RuleLimit Rule = "limit" // within the limits above |
| 176 | RuleCycle Rule = "cycle" // parent and blocked_by edges form no cycle |
| 177 | RuleTransition Rule = "transition" // what a push may change |
| 178 | RuleProvenance Rule = "provenance" // created_by and author name the pusher |
| 179 | RuleClaim Rule = "claim" // claimed_by and claimed_at come together |
| 180 | RuleCutoff Rule = "cutoff" // created after IDRuleCutoff |
| 181 | ) |
| 182 | |
| 183 | // SharedRules are the rules both validators enforce, in a fixed order. |
| 184 | var SharedRules = []Rule{ |
| 185 | RuleType, RuleStatus, RuleIDShape, RuleIDDerived, RuleFilename, |
| 186 | RuleDirectory, RuleUniqueID, RuleFieldOnType, RuleFieldShape, |
| 187 | RuleRequired, RuleEdgeResolves, RuleParentOutcome, RuleGraduated, |
| 188 | } |
| 189 | |
| 190 | // GoRules are the rules the Go validator alone enforces. |
| 191 | var GoRules = []Rule{ |
| 192 | RuleCanonical, RuleText, RuleLayout, RuleLimit, RuleCycle, |
| 193 | RuleTransition, RuleProvenance, RuleClaim, RuleCutoff, |
| 194 | } |
| 195 | |
| 196 | // Error is a refusal: the rule it is under, the path it is about, if any, |
| 197 | // and what is wrong, as a sentence that names the valid values. |
| 198 | type Error struct { |
| 199 | Rule Rule |
| 200 | Path string |
| 201 | Msg string |
| 202 | } |
| 203 | |
| 204 | func (e *Error) Error() string { |
| 205 | if e.Path == "" { |
| 206 | return e.Msg |
| 207 | } |
| 208 | return e.Path + ": " + e.Msg |
| 209 | } |
| 210 | |
| 211 | func refuse(rule Rule, path, msg string) *Error { |
| 212 | return &Error{Rule: rule, Path: path, Msg: msg} |
| 213 | } |