gunk-dev/the-valley

generate.go

source822cb119f57bmain's published tip history
6.6 KiB
1package main
2
3import (
4 "fmt"
5 "math/rand/v2"
6 "slices"
7 "strings"
8
9 "the-valley/knot"
10)
11
12var (
13 words = []string{"plan", "graph", "hook", "ref", "knot", "cord", "lease", "claim", "slice", "seed", "view", "push", "tree", "walk", "bound"}
14 principals = []string{"patrick", "klaus-env", "integrator", "a.b_c"}
15)
16
17// Generate is n graphs from seed: each valid graph the generator makes,
18// and, after it, the same graph broken by one shared rule. A broken graph
19// is kept only when the Go validator refuses it under a shared rule, so
20// every case is one the CUE schema and the lint must refuse too.
21func Generate(n int, seed uint64) []Case {
22 r := rand.New(rand.NewPCG(seed, seed^0x9e3779b97f4a7c15))
23 var cases []Case
24 for i := 0; len(cases) < n; i++ {
25 knots := graph(r)
26 name := fmt.Sprintf("generated-%03d", i)
27 valid := Case{Name: name, Accept: true, Files: withComments(r, knots)}
28 if err := validate(asFiles(valid.Files)); err != nil {
29 panic(fmt.Sprintf("generated graph %s is invalid: %v", name, err))
30 }
31 cases = append(cases, valid)
32 if len(cases) == n {
33 break
34 }
35 broken, rule := mutate(r, knots)
36 err := validate(asFiles(broken))
37 e, ok := err.(*knot.Error)
38 if err == nil || !ok {
39 continue
40 }
41 if !slices.Contains(knot.SharedRules, e.Rule) {
42 continue
43 }
44 cases = append(cases, Case{Name: name + "-" + string(rule), Rule: e.Rule, Files: broken})
45 }
46 return cases
47}
48
49func asFiles(fs map[string]string) map[string]file {
50 out := map[string]file{}
51 for p, s := range fs {
52 out[p] = file{mode: "100644", data: []byte(s)}
53 }
54 return out
55}
56
57// graph is a random valid graph of 2 to 24 knots. Every edge points at a
58// knot made earlier, so neither parent nor blocked_by edges form a cycle.
59func graph(r *rand.Rand) []*knot.Knot {
60 var knots []*knot.Knot
61 taken := map[string]bool{}
62 pick := func(list []string) string { return list[r.IntN(len(list))] }
63 for range 2 + r.IntN(23) {
64 t := knot.Types[r.IntN(len(knot.Types))]
65 var title []string
66 for range 1 + r.IntN(4) {
67 title = append(title, pick(words))
68 }
69 title[0] = strings.ToUpper(title[0][:1]) + title[0][1:]
70 id, slug := knot.NewID(t, strings.Join(title, " "), func(id string) bool { return taken[id] })
71 taken[id] = true
72 k := &knot.Knot{Type: t.Name, ID: id, Slug: slug, Status: pick(t.Statuses()), Title: strings.Join(title, " "),
73 Created: fmt.Sprintf("2026-%02d-%02d", 9+r.IntN(3), 1+r.IntN(28)), CreatedBy: pick(principals)}
74 var outcomes, decisions, earlier []string
75 for _, e := range knots {
76 earlier = append(earlier, e.ID)
77 switch e.Type {
78 case "outcome":
79 outcomes = append(outcomes, e.ID)
80 case "decision":
81 decisions = append(decisions, e.ID)
82 }
83 }
84 if k.Status == "graduated" {
85 if len(decisions) == 0 {
86 k.Status = "raw"
87 } else {
88 k.GraduatedInto = pick(decisions)
89 }
90 }
91 if len(outcomes) > 0 && r.IntN(2) == 0 {
92 k.Parent = pick(outcomes)
93 }
94 if r.IntN(3) == 0 {
95 k.Owner = pick(principals)
96 }
97 if r.IntN(4) == 0 {
98 k.Source = "2026-10-15, a generated graph"
99 }
100 if t.Name == "outcome" || t.Name == "bug" {
101 k.BlockedBy = []string{}
102 for range r.IntN(3) {
103 if len(earlier) > 0 && !slices.Contains(k.BlockedBy, earlier[len(earlier)-1]) {
104 b := pick(earlier)
105 if !slices.Contains(k.BlockedBy, b) {
106 k.BlockedBy = append(k.BlockedBy, b)
107 }
108 }
109 }
110 if r.IntN(5) == 0 {
111 k.BlockedBy = append(k.BlockedBy, "cosmo:oc-0123456")
112 }
113 slices.Sort(k.BlockedBy)
114 if r.IntN(4) == 0 {
115 k.ClaimedBy, k.ClaimedAt = "run-"+fmt.Sprint(r.IntN(100)), "2026-10-15T01:02:03Z"
116 }
117 if r.IntN(4) == 0 {
118 k.Changes = []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"}
119 }
120 if r.IntN(6) == 0 {
121 k.Target = pick([]string{"klaus", "cosmo", "cosmo:bd-0123456"})
122 }
123 }
124 // A decided decision links the change that records it. This draws
125 // nothing, so the graphs a seed makes stay the same.
126 if k.Status == "decided" {
127 k.Changes = []string{"https://github.com/gunk-dev/klaus/pull/12"}
128 }
129 if r.IntN(2) == 0 {
130 k.Body = "A body for " + k.Title + ".\n"
131 }
132 knots = append(knots, k)
133 }
134 return knots
135}
136
137func withComments(r *rand.Rand, knots []*knot.Knot) map[string]string {
138 fs := files(knots...)
139 for i := range r.IntN(4) {
140 k := knots[r.IntN(len(knots))]
141 p := fmt.Sprintf("%s/%s/20261015T0%d0000Z-%016x.md", knot.CommentsDirectory, k.ID, i, r.Uint64())
142 fs[p] = string(knot.RenderComment(&knot.Comment{Author: principals[r.IntN(len(principals))], Body: "A comment.\n"}))
143 }
144 return fs
145}
146
147// mutate is the graph broken by one shared rule, picked at random, and the
148// rule it means to break.
149func mutate(r *rand.Rand, knots []*knot.Knot) (map[string]string, knot.Rule) {
150 knots = slices.Clone(knots)
151 i := r.IntN(len(knots))
152 k := knots[i].Clone()
153 knots[i] = k
154 t := k.TypeOf()
155 at := slices.IndexFunc(knot.Types, func(x knot.Type) bool { return x.Name == t.Name })
156 other := knot.Types[(at+1+r.IntN(3))%4]
157 rules := []knot.Rule{knot.RuleType, knot.RuleStatus, knot.RuleIDShape, knot.RuleIDDerived, knot.RuleDirectory,
158 knot.RuleFieldOnType, knot.RuleFieldShape, knot.RuleRequired, knot.RuleEdgeResolves, knot.RuleParentOutcome, knot.RuleGraduated}
159 rule := rules[r.IntN(len(rules))]
160 switch rule {
161 case knot.RuleType:
162 k.Type = "task"
163 fs := files(knots...)
164 delete(fs, knots[i].Path())
165 fs[t.Directory+"/"+k.ID+"-"+k.Slug+".md"] = string(knot.Render(k))
166 return fs, rule
167 case knot.RuleStatus:
168 k.Status = other.Terminal[0]
169 if slices.Contains(t.Statuses(), k.Status) {
170 k.Status = "blocked"
171 }
172 case knot.RuleIDShape:
173 fs := files(knots...)
174 fs[k.Path()] = strings.Replace(fs[k.Path()], "id: "+k.ID, "id: "+other.Prefix+k.ID[len(t.Prefix):], 1)
175 return fs, rule
176 case knot.RuleIDDerived:
177 k.ID = t.Prefix + "-0000000"
178 case knot.RuleDirectory:
179 fs := files(knots...)
180 delete(fs, k.Path())
181 fs[other.Directory+"/"+k.ID+"-"+k.Slug+".md"] = string(knot.Render(k))
182 return fs, rule
183 case knot.RuleFieldOnType:
184 switch t.Name {
185 case "outcome", "bug":
186 k.GraduatedInto = "dcr-0000000"
187 case "idea":
188 k.Changes = []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"}
189 default:
190 k.Target = "klaus"
191 }
192 case knot.RuleFieldShape:
193 k.Owner = "an owner"
194 case knot.RuleRequired:
195 k.Title = ""
196 case knot.RuleEdgeResolves:
197 k.Parent = "oc-0000000"
198 case knot.RuleParentOutcome:
199 for _, o := range knots {
200 if o.Type != "outcome" {
201 k.Parent = o.ID
202 }
203 }
204 if k.Parent == "" || strings.HasPrefix(k.Parent, "oc-") {
205 k.Parent = "bd-0000000"
206 }
207 case knot.RuleGraduated:
208 if t.Name == "idea" {
209 k.Status, k.GraduatedInto = "graduated", ""
210 } else {
211 k.Title = ""
212 rule = knot.RuleRequired
213 }
214 }
215 return files(knots...), rule
216}