gunk-dev/the-valley

generate.go

sourcedee3830391efmain'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, destinations, 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 destinations = append(destinations, e.ID)
81 case "question":
82 destinations = append(destinations, e.ID)
83 }
84 }
85 if k.Status == "graduated" {
86 if len(destinations) == 0 {
87 k.Status = "raw"
88 } else {
89 k.GraduatedInto = pick(destinations)
90 }
91 }
92 if len(outcomes) > 0 && r.IntN(2) == 0 {
93 k.Parent = pick(outcomes)
94 }
95 if r.IntN(3) == 0 {
96 k.Owner = pick(principals)
97 }
98 if r.IntN(4) == 0 {
99 k.Source = "2026-10-15, a generated graph"
100 }
101 if t.Name == "outcome" || t.Name == "bug" {
102 k.BlockedBy = []string{}
103 for range r.IntN(3) {
104 if len(earlier) > 0 && !slices.Contains(k.BlockedBy, earlier[len(earlier)-1]) {
105 b := pick(earlier)
106 if !slices.Contains(k.BlockedBy, b) {
107 k.BlockedBy = append(k.BlockedBy, b)
108 }
109 }
110 }
111 if r.IntN(5) == 0 {
112 k.BlockedBy = append(k.BlockedBy, "cosmo:oc-0123456")
113 }
114 slices.Sort(k.BlockedBy)
115 if r.IntN(4) == 0 {
116 k.ClaimedBy, k.ClaimedAt = "run-"+fmt.Sprint(r.IntN(100)), "2026-10-15T01:02:03Z"
117 }
118 if r.IntN(4) == 0 {
119 k.Changes = []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"}
120 }
121 if r.IntN(6) == 0 {
122 k.Target = pick([]string{"klaus", "cosmo", "cosmo:bd-0123456"})
123 }
124 }
125 // An answered question links the change that records its answer.
126 // This draws nothing, so the graphs a seed makes stay the same.
127 if k.Status == "answered" {
128 k.Changes = []string{"https://github.com/gunk-dev/klaus/pull/12"}
129 }
130 if r.IntN(2) == 0 {
131 k.Body = "A body for " + k.Title + ".\n"
132 }
133 knots = append(knots, k)
134 }
135 return knots
136}
137
138func withComments(r *rand.Rand, knots []*knot.Knot) map[string]string {
139 fs := files(knots...)
140 for i := range r.IntN(4) {
141 k := knots[r.IntN(len(knots))]
142 p := fmt.Sprintf("%s/%s/20261015T0%d0000Z-%016x.md", knot.CommentsDirectory, k.ID, i, r.Uint64())
143 fs[p] = string(knot.RenderComment(&knot.Comment{Author: principals[r.IntN(len(principals))], Body: "A comment.\n"}))
144 }
145 return fs
146}
147
148// mutate is the graph broken by one shared rule, picked at random, and the
149// rule it means to break.
150func mutate(r *rand.Rand, knots []*knot.Knot) (map[string]string, knot.Rule) {
151 knots = slices.Clone(knots)
152 i := r.IntN(len(knots))
153 k := knots[i].Clone()
154 knots[i] = k
155 t := k.TypeOf()
156 at := slices.IndexFunc(knot.Types, func(x knot.Type) bool { return x.Name == t.Name })
157 other := knot.Types[(at+1+r.IntN(3))%4]
158 rules := []knot.Rule{knot.RuleType, knot.RuleStatus, knot.RuleIDShape, knot.RuleIDDerived, knot.RuleDirectory,
159 knot.RuleFieldOnType, knot.RuleFieldShape, knot.RuleRequired, knot.RuleEdgeResolves, knot.RuleParentOutcome, knot.RuleGraduated}
160 rule := rules[r.IntN(len(rules))]
161 switch rule {
162 case knot.RuleType:
163 k.Type = "task"
164 fs := files(knots...)
165 delete(fs, knots[i].Path())
166 fs[t.Directory+"/"+k.ID+"-"+k.Slug+".md"] = string(knot.Render(k))
167 return fs, rule
168 case knot.RuleStatus:
169 k.Status = other.Terminal[0]
170 if slices.Contains(t.Statuses(), k.Status) {
171 k.Status = "blocked"
172 }
173 case knot.RuleIDShape:
174 fs := files(knots...)
175 fs[k.Path()] = strings.Replace(fs[k.Path()], "id: "+k.ID, "id: "+other.Prefix+k.ID[len(t.Prefix):], 1)
176 return fs, rule
177 case knot.RuleIDDerived:
178 k.ID = t.Prefix + "-0000000"
179 case knot.RuleDirectory:
180 fs := files(knots...)
181 delete(fs, k.Path())
182 fs[other.Directory+"/"+k.ID+"-"+k.Slug+".md"] = string(knot.Render(k))
183 return fs, rule
184 case knot.RuleFieldOnType:
185 switch t.Name {
186 case "outcome", "bug":
187 k.GraduatedInto = "qn-0000000"
188 case "idea":
189 k.Changes = []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"}
190 default:
191 k.Target = "klaus"
192 }
193 case knot.RuleFieldShape:
194 k.Owner = "an owner"
195 case knot.RuleRequired:
196 k.Title = ""
197 case knot.RuleEdgeResolves:
198 k.Parent = "oc-0000000"
199 case knot.RuleParentOutcome:
200 for _, o := range knots {
201 if o.Type != "outcome" {
202 k.Parent = o.ID
203 }
204 }
205 if k.Parent == "" || strings.HasPrefix(k.Parent, "oc-") {
206 k.Parent = "bd-0000000"
207 }
208 case knot.RuleGraduated:
209 if t.Name == "idea" {
210 k.Status, k.GraduatedInto = "graduated", ""
211 } else {
212 k.Title = ""
213 rule = knot.RuleRequired
214 }
215 }
216 return files(knots...), rule
217}