-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathinstall.go
More file actions
377 lines (357 loc) · 9.78 KB
/
Copy pathinstall.go
File metadata and controls
377 lines (357 loc) · 9.78 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package agenthook
import (
"errors"
"fmt"
"io"
"path/filepath"
"strings"
"time"
"unicode"
kitagenthook "go.kenn.io/kit/agenthook"
"go.kenn.io/roborev/internal/skills"
)
const (
agentHookRunner = "agent-hook run"
agentHookMarker = "--source=roborev-agent-hook"
)
type InstallOptions struct {
Agent string
Executable string
Command string
ConfigPath string
Timeout time.Duration
DryRun bool
}
type DumpOptions struct {
Agent string
Executable string
Command string
ConfigPath string
Timeout time.Duration
}
func RunInstall(opts InstallOptions, stdout io.Writer) error {
if opts.Timeout < 0 {
return fmt.Errorf("timeout must be >= 0")
}
explicit := strings.TrimSpace(opts.Agent) != "" && !strings.EqualFold(strings.TrimSpace(opts.Agent), "all")
if opts.ConfigPath != "" && !explicit {
return fmt.Errorf("--config requires one explicit agent")
}
if opts.Command != "" && !explicit {
return fmt.Errorf("--command requires one explicit agent")
}
agents, err := SelectProfiles(opts.Agent)
if err != nil {
return err
}
var errs []error
for _, agent := range agents {
result, runErr := runInstall(agent, opts)
if runErr != nil {
errs = append(errs, profileError(agent, opts.ConfigPath, runErr))
continue
}
printInstallResult(stdout, result, opts.DryRun)
}
return errors.Join(errs...)
}
func RunDump(opts DumpOptions, stdout io.Writer) error {
if opts.Timeout < 0 {
return fmt.Errorf("timeout must be >= 0")
}
raw := strings.TrimSpace(opts.Agent)
if raw == "" || strings.EqualFold(raw, "all") {
return fmt.Errorf("dump requires one explicit agent")
}
agent := AgentGrok
if !strings.EqualFold(raw, string(AgentGrok)) {
var err error
agent, err = kitagenthook.ParseAgent(raw)
if err != nil {
return err
}
}
installOpts := InstallOptions{
Agent: raw,
Executable: opts.Executable,
Command: opts.Command,
ConfigPath: opts.ConfigPath,
Timeout: opts.Timeout,
DryRun: true,
}
if agent == AgentGrok {
result, err := planGrokInstall(installOpts)
if err != nil {
return profileError(agent, opts.ConfigPath, err)
}
_, err = stdout.Write(result.Data)
return err
}
kitOpts, err := validatedKitInstallOptions(agent, installOpts)
if err != nil {
return profileError(agent, opts.ConfigPath, err)
}
result, err := kitagenthook.PlanInstall(agent, kitOpts)
if err != nil {
return profileError(agent, opts.ConfigPath, err)
}
result, err = planLegacyHookMigration(agent, result)
if err != nil {
return profileError(agent, opts.ConfigPath, err)
}
_, err = stdout.Write(result.Data)
return err
}
func runInstall(agent kitagenthook.Agent, opts InstallOptions) (kitagenthook.Result, error) {
var planned kitagenthook.Result
var err error
if agent == AgentGrok {
planned, err = planGrokInstall(opts)
} else {
var kitOpts kitagenthook.InstallOptions
kitOpts, err = validatedKitInstallOptions(agent, opts)
if err == nil {
planned, err = kitagenthook.PlanInstall(agent, kitOpts)
}
if err == nil {
planned, err = planLegacyHookMigration(agent, planned)
}
}
if err != nil {
return kitagenthook.Result{}, err
}
if opts.DryRun {
return planned, nil
}
if err := installAgentHookSkills(agent, planned.ConfigPath); err != nil {
return kitagenthook.Result{}, err
}
if !planned.Changed {
return planned, nil
}
if err := commitAgentHookConfig(planned.ConfigPath, planned.Data); err != nil {
return kitagenthook.Result{}, err
}
return planned, nil
}
func installAgentHookSkills(agent kitagenthook.Agent, configPath string) error {
var skillAgent skills.Agent
switch agent {
case kitagenthook.AgentClaude:
skillAgent = skills.AgentClaude
case kitagenthook.AgentCodex:
skillAgent = skills.AgentCodex
case kitagenthook.AgentDroid:
skillAgent = skills.AgentDroid
case AgentGrok:
skillAgent = skills.AgentGrok
default:
return nil
}
configDir := filepath.Dir(configPath)
if agent == AgentGrok && strings.EqualFold(filepath.Base(configDir), "hooks") {
configDir = filepath.Dir(configDir)
}
if _, err := skills.InstallToPath(skillAgent, filepath.Join(configDir, "skills")); err != nil {
return fmt.Errorf("install bundled %s skills: %w", skillAgent, err)
}
return nil
}
func validatedKitInstallOptions(
agent kitagenthook.Agent,
opts InstallOptions,
) (kitagenthook.InstallOptions, error) {
if opts.Command != "" {
selected, err := commandAgent(opts.Command)
if err != nil {
return kitagenthook.InstallOptions{}, err
}
if selected != agent {
return kitagenthook.InstallOptions{}, fmt.Errorf("hook command selects %s, not %s", selected, agent)
}
}
if agent == kitagenthook.AgentDroid {
path := opts.ConfigPath
if path == "" {
var err error
path, err = kitagenthook.ConfigPath(agent)
if err != nil {
return kitagenthook.InstallOptions{}, err
}
}
if err := validateDroidHooksPath(path); err != nil {
return kitagenthook.InstallOptions{}, err
}
}
return kitInstallOptions(agent, opts), nil
}
func kitInstallOptions(agent kitagenthook.Agent, opts InstallOptions) kitagenthook.InstallOptions {
command := strings.TrimSpace(opts.Command)
if command != "" {
command += " " + agentHookMarker
}
kitOpts := kitagenthook.InstallOptions{
ConfigPath: opts.ConfigPath,
Executable: opts.Executable,
Command: command,
Marker: agentHookMarker,
Hooks: []kitagenthook.Hook{
{Event: kitagenthook.EventPreToolUse, Matcher: kitagenthook.ToolBash, Timeout: opts.Timeout},
{Event: kitagenthook.EventPostToolUse, Matcher: kitagenthook.ToolBash, Timeout: opts.Timeout},
{Event: kitagenthook.EventStop, Timeout: opts.Timeout},
},
}
if opts.Command == "" {
kitOpts.Arguments = []string{
"agent-hook", "run", "--agent", string(agent), agentHookMarker,
}
}
return kitOpts
}
func commandAgent(command string) (kitagenthook.Agent, error) {
fields, err := splitHookCommand(command)
if err != nil {
return "", err
}
if len(fields) < 3 || fields[1] != "agent-hook" || fields[2] != "run" {
return "", fmt.Errorf("hook command must invoke %s", agentHookRunner)
}
selected := ""
for i := 3; i < len(fields); i++ {
field := fields[i]
if field == "--" {
return "", fmt.Errorf("hook command must not contain an argument terminator")
}
value := ""
switch {
case field == "--agent":
if i+1 >= len(fields) || strings.HasPrefix(fields[i+1], "--") {
return "", fmt.Errorf("--agent requires a value")
}
i++
value = fields[i]
case strings.HasPrefix(field, "--agent="):
value = strings.TrimPrefix(field, "--agent=")
default:
continue
}
if value == "" {
return "", fmt.Errorf("--agent requires a value")
}
if selected != "" {
return "", fmt.Errorf("hook command must select exactly one agent")
}
selected = value
}
if selected == "" {
return "", fmt.Errorf("hook command must select an agent")
}
if strings.EqualFold(selected, string(AgentGrok)) {
return AgentGrok, nil
}
return kitagenthook.ParseAgent(selected)
}
func splitHookCommand(command string) ([]string, error) {
var fields []string
var field strings.Builder
var quote rune
escaped := false
previousUnescapedDollar := false
started := false
flush := func() {
if !started {
return
}
fields = append(fields, field.String())
field.Reset()
started = false
}
for _, r := range strings.TrimSpace(command) {
if unicode.IsControl(r) {
return nil, fmt.Errorf("hook command must be a single command line")
}
if escaped {
field.WriteRune(r)
started = true
escaped = false
previousUnescapedDollar = false
continue
}
if quote != 0 {
switch {
case r == quote:
quote = 0
previousUnescapedDollar = false
case quote == '"' && r == '\\':
escaped = true
previousUnescapedDollar = false
case quote == '"' && r == '`':
return nil, fmt.Errorf("hook command contains unsupported shell operator %q", r)
case quote == '"' && r == '(' && previousUnescapedDollar:
return nil, fmt.Errorf("hook command contains unsupported shell operator %q", "$(")
default:
field.WriteRune(r)
previousUnescapedDollar = quote == '"' && r == '$'
}
started = true
continue
}
switch {
case unicode.IsSpace(r):
flush()
case r == '\'' || r == '"':
quote = r
started = true
case r == '\\':
escaped = true
started = true
case strings.ContainsRune(";&|<>()`#", r):
return nil, fmt.Errorf("hook command contains unsupported shell operator %q", r)
default:
field.WriteRune(r)
started = true
}
}
if quote != 0 {
return nil, fmt.Errorf("hook command contains an unterminated quote")
}
if escaped {
return nil, fmt.Errorf("hook command contains an incomplete escape")
}
flush()
return fields, nil
}
func profileError(agent kitagenthook.Agent, configuredPath string, err error) error {
profile, _ := kitagenthook.LookupProfile(agent)
if agent == AgentGrok {
profile.DisplayName = "Grok Build"
}
path := configuredPath
if path == "" {
if agent == AgentGrok {
path = DefaultGrokHooksPath()
} else {
path, _ = kitagenthook.ConfigPath(agent)
}
}
if path == "" {
return fmt.Errorf("%s: %w", profile.DisplayName, err)
}
return fmt.Errorf("%s (%s): %w", profile.DisplayName, path, err)
}
func printInstallResult(stdout io.Writer, result kitagenthook.Result, dryRun bool) {
profile, _ := kitagenthook.LookupProfile(result.Agent)
if result.Agent == AgentGrok {
profile.DisplayName = "Grok Build"
}
switch {
case dryRun && result.Changed:
fmt.Fprintf(stdout, "would update %s agent hooks in %s\n", profile.DisplayName, result.ConfigPath)
case dryRun:
fmt.Fprintf(stdout, "%s agent hooks already installed in %s\n", profile.DisplayName, result.ConfigPath)
case result.Changed:
fmt.Fprintf(stdout, "installed %s agent hooks in %s\n", profile.DisplayName, result.ConfigPath)
default:
fmt.Fprintf(stdout, "%s agent hooks already installed in %s\n", profile.DisplayName, result.ConfigPath)
}
}