-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuggest.go
More file actions
231 lines (212 loc) · 7.54 KB
/
Copy pathsuggest.go
File metadata and controls
231 lines (212 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"sort"
"strings"
)
// suggestSystem is the advisor's brief. It is deliberately narrow: the model
// classifies documented lines and never decides whether anything passed.
const suggestSystem = `You help configure kibble, a tool that runs a README's documented commands in a
clean Linux container to check the docs still work.
You will get the repository name and a list of documented command lines that kibble could
not classify on its own. For each line, decide which single category fits:
run the line should run as written and is expected to succeed
nonzero the line runs, but exiting nonzero is its documented behavior, as a linter or
scanner that fails when it finds something
skip the line cannot honestly run in a clean container with no credentials, no
terminal, no network services, and only the repository's own files
Judge only from the line and its section heading. Prefer skip when unsure, because a
wrong run costs a false failure while a wrong skip only costs coverage.
Reply with JSON only, no prose and no code fence:
{"lines":[{"cmd":"<the line verbatim>","verdict":"run|nonzero|skip","reason":"<short reason>"}]}
The reason is one clause a maintainer will read in a config file. Do not restate the
command. For skip, say what the container lacks.`
// suggestion is one classified line returned by the advisor.
type suggestion struct {
// Cmd is the documented line, echoed back for matching.
Cmd string `json:"cmd"`
// Verdict is run, nonzero, or skip.
Verdict string `json:"verdict"`
// Reason is the short justification written into the config.
Reason string `json:"reason"`
}
// suggestReply is the advisor's whole answer.
type suggestReply struct {
// Lines are the classified lines.
Lines []suggestion `json:"lines"`
}
// candidate is a documented line kibble wants a second opinion on, carried
// with the heading that gives it context.
type candidate struct {
// Cmd is the documented line.
Cmd string
// Heading is the section the line appears under.
Heading string
// Skip is kibble's own reason for skipping, empty when it planned to run.
Skip string
}
// suggestCandidates returns the lines worth asking about: every line kibble
// skipped for a reason a maintainer might overrule, and every line it planned
// to run whose exit code it cannot predict. Lines skipped for reasons that are
// certain, such as a placeholder the reader must fill in, are left alone.
func suggestCandidates(plan *Plan) []candidate {
var out []candidate
seen := map[string]bool{}
for _, step := range plan.Steps {
for _, line := range step.Lines {
cmd := flatten(line.Cmd)
if cmd == "" || seen[cmd] {
continue
}
seen[cmd] = true
if line.Skip == "" && !line.NonzeroOK {
out = append(out, candidate{Cmd: cmd, Heading: step.Heading})
continue
}
if line.Skip != "" && !certainSkip(line.Skip) {
out = append(out, candidate{Cmd: cmd, Heading: step.Heading, Skip: line.Skip})
}
}
}
return out
}
// certainSkipReasons are skip reasons the engine is sure about, so a model is
// never asked to second-guess them.
var certainSkipReasons = []string{
"placeholder", "never create", "which the docs never set", "interactive sign-in",
"another shell", "kernel interfaces", "only the reader's system",
"which was skipped", "session ended", "follows a skipped cd",
}
// certainSkip reports whether a skip reason is one the engine decides on its
// own evidence rather than a judgment call.
func certainSkip(reason string) bool {
for _, r := range certainSkipReasons {
if strings.Contains(reason, r) {
return true
}
}
return false
}
// askAdvisor sends the candidates and returns the advisor's classifications,
// keyed by the command line. A reply that does not parse is an error, never a
// silent empty result.
func askAdvisor(ctx context.Context, a Advisor, repo string, cands []candidate) (
map[string]suggestion, error) {
var b strings.Builder
fmt.Fprintf(&b, "Repository: %s\n\nLines:\n", repo)
for _, c := range cands {
fmt.Fprintf(&b, "- cmd: %s\n", c.Cmd)
if c.Heading != "" {
fmt.Fprintf(&b, " section: %s\n", c.Heading)
}
if c.Skip != "" {
fmt.Fprintf(&b, " kibble would skip because: %s\n", c.Skip)
}
}
reply, err := a.Chat(ctx, suggestSystem, b.String())
if err != nil {
return nil, err
}
obj, err := firstJSONObject(reply)
if err != nil {
return nil, err
}
var parsed suggestReply
if err := json.Unmarshal([]byte(obj), &parsed); err != nil {
return nil, fmt.Errorf("%w: reply was not the requested JSON: %w", ErrAdvisor, err)
}
out := map[string]suggestion{}
for _, s := range parsed.Lines {
if cmd := strings.TrimSpace(s.Cmd); cmd != "" {
out[cmd] = s
}
}
return out, nil
}
// writeSuggestedConfig renders the advisor's classifications as a .kibble.yml
// for a human to read and commit. Only lines where the advisor disagrees with
// the engine become rules, so the file stays small and every entry earns its
// place. It reports whether any rule was written.
func writeSuggestedConfig(w io.Writer, repo string, cands []candidate,
got map[string]suggestion) bool {
type rule struct {
// cmd is the documented line the rule matches.
cmd string
// verdict is the advisor's classification.
verdict string
// reason is the advisor's justification.
reason string
}
var rules []rule
for _, c := range cands {
s, ok := got[c.Cmd]
if !ok {
continue
}
switch s.Verdict {
case "nonzero":
rules = append(rules, rule{c.Cmd, "nonzero", s.Reason})
case "skip":
if c.Skip == "" {
rules = append(rules, rule{c.Cmd, "skip", s.Reason})
}
case "run":
if c.Skip != "" {
rules = append(rules, rule{c.Cmd, "run", s.Reason})
}
}
}
if len(rules) == 0 {
return false
}
sort.SliceStable(rules, func(i, j int) bool { return rules[i].cmd < rules[j].cmd })
_, _ = fmt.Fprintf(w, "# .kibble.yml proposed for %s.\n", repo)
_, _ = fmt.Fprintln(w, "# Every entry is a judgment call kibble's heuristics could not make on their")
_, _ = fmt.Fprintln(w, "# own. Read each one, delete what you disagree with, and commit the rest.")
_, _ = fmt.Fprintln(w, "# Once committed, runs stay deterministic: no model is consulted again.")
_, _ = fmt.Fprintln(w, "version: 1")
_, _ = fmt.Fprintln(w, "examples:")
_, _ = fmt.Fprintln(w, " steps:")
for _, r := range rules {
_, _ = fmt.Fprintf(w, " - match: %s\n", yamlScalar(r.cmd))
switch r.verdict {
case "nonzero":
_, _ = fmt.Fprintln(w, " nonzeroOk: true")
case "skip":
_, _ = fmt.Fprintf(w, " skip: %s\n", yamlScalar(reasonText(r.reason)))
case "run":
_, _ = fmt.Fprintln(w, " run: true")
}
if r.verdict != "skip" && r.reason != "" {
_, _ = fmt.Fprintf(w, " # %s\n", oneLine(r.reason))
}
}
return true
}
// reasonText returns a usable skip reason, falling back when the advisor
// gave none, so the config never carries an empty explanation.
func reasonText(reason string) string {
if r := oneLine(reason); r != "" {
return r
}
return "cannot run in a clean container"
}
// oneLine collapses a reason to a single trimmed line for a config comment.
func oneLine(s string) string {
return strings.TrimSpace(strings.Join(strings.Fields(s), " "))
}
// yamlScalar quotes a value for YAML when quoting is needed, so a command
// containing a colon or a quote round-trips.
func yamlScalar(s string) string {
s = oneLine(s)
if s == "" {
return `""`
}
if strings.ContainsAny(s, `:#{}[]&*!|>'"%@`+"`") || strings.HasPrefix(s, " ") {
return `"` + strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(s) + `"`
}
return s
}