-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.go
More file actions
479 lines (442 loc) · 13.3 KB
/
tools.go
File metadata and controls
479 lines (442 loc) · 13.3 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package iteragent
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
var (
protectedPaths []string
protectedPathsMu sync.RWMutex
dangerousPatterns []*regexp.Regexp
dangerousPatternsMu sync.RWMutex
)
func init() {
patterns := []string{
// Recursive force remove
`\brm\s+(-[a-zA-Z]*r[a-zA-Z]*f[a-zA-Z]*|-[a-zA-Z]*f[a-zA-Z]*r[a-zA-Z]*)\b`,
// Dangerous chmod
`\bchmod\s+-R\s+777\b`,
// Write to system paths
`>\s*/etc/`,
// Pipe to shell from network
`\bcurl\b.*\|\s*(ba)?sh\b`,
`\bwget\b.*\|\s*(ba)?sh\b`,
// Fork bombs and process abuse
`:(){ :\|:& };:`,
`\bmkfs\b`,
`\bdd\b.*of=/dev/`,
// Destructive redirects
`>\s*/dev/sd`,
`>\s*/dev/nvme`,
// Privilege escalation
`\bsudo\s+rm\b`,
`\bsudo\s+chmod\b.*777`,
// Network exfiltration
`\bnc\s+-[el]`,
`\bncat\b.*-e`,
`\bsocat\b.*exec`,
// Environment variable leakage to network
`\benv\b.*\|\s*(nc|curl|wget)`,
`\bprintenv\b.*\|\s*(nc|curl|wget)`,
// Dangerous git operations
`\bgit\s+push\s+.*--force\b.*\bmain\b`,
`\bgit\s+push\s+.*--force\b.*\bmaster\b`,
`\bgit\s+clean\s+-[a-zA-Z]*f[a-zA-Z]*d\b`,
// Shellshock-style
`\(\)\s*\{.*\bexport\b`,
}
for _, p := range patterns {
re, err := regexp.Compile(p)
if err == nil {
dangerousPatterns = append(dangerousPatterns, re)
}
}
}
func SetProtectedPaths(paths []string) {
protectedPathsMu.Lock()
defer protectedPathsMu.Unlock()
protectedPaths = paths
}
func GetProtectedPaths() []string {
protectedPathsMu.RLock()
defer protectedPathsMu.RUnlock()
return protectedPaths
}
func isPathProtected(path string) bool {
protectedPathsMu.RLock()
defer protectedPathsMu.RUnlock()
for _, p := range protectedPaths {
if strings.HasPrefix(path, p) {
return true
}
}
return false
}
func isCommandDangerous(cmd string) bool {
dangerousPatternsMu.RLock()
defer dangerousPatternsMu.RUnlock()
for _, re := range dangerousPatterns {
if re.MatchString(cmd) {
return true
}
}
return false
}
// safeJoin joins repoPath with the user-supplied relative path and ensures the
// result stays within repoPath, preventing path traversal attacks.
func safeJoin(repoPath, rel string) (string, error) {
absRepo, err := filepath.Abs(repoPath)
if err != nil {
return "", fmt.Errorf("invalid repo path: %w", err)
}
absRepo = filepath.Clean(absRepo) + string(filepath.Separator)
joined := filepath.Join(repoPath, rel)
absJoined, err := filepath.Abs(joined)
if err != nil {
return "", fmt.Errorf("invalid path: %w", err)
}
absJoined = filepath.Clean(absJoined)
if !strings.HasPrefix(absJoined, absRepo) {
return "", fmt.Errorf("path %q is outside the repository", rel)
}
// Resolve symlinks to prevent symlink-based path traversal.
resolved, err := filepath.EvalSymlinks(absJoined)
if err != nil {
// File may not exist yet (for write operations) — that's OK.
if os.IsNotExist(err) {
return absJoined, nil
}
return "", fmt.Errorf("resolve symlink: %w", err)
}
if !strings.HasPrefix(resolved, absRepo) {
return "", fmt.Errorf("path %q resolves outside the repository via symlink", rel)
}
return resolved, nil
}
// DefaultTools returns all built-in tools available to the agent.
func DefaultTools(repoPath string) []Tool {
return []Tool{
BashTool(repoPath),
ReadFileTool(repoPath),
WriteFileTool(repoPath),
EditFileTool(repoPath),
ListFilesTool(repoPath),
SearchTool(repoPath),
GitDiffTool(repoPath),
GitCommitTool(repoPath),
GitRevertTool(repoPath),
RunTestsTool(repoPath),
}
}
func BashTool(repoPath string) Tool {
return Tool{
Name: "bash",
Description: "Run a shell command in the repo directory.\nArgs: {\"cmd\": \"go build ./...\"}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
cmd := args["cmd"]
if cmd == "" {
return "", fmt.Errorf("cmd is required")
}
if isCommandDangerous(cmd) {
return "", fmt.Errorf("command contains dangerous pattern: %s", cmd)
}
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
c := exec.CommandContext(ctx, "bash", "-c", cmd)
c.Dir = repoPath
var out bytes.Buffer
c.Stdout = &out
c.Stderr = &out
err := c.Run()
return out.String(), err
},
}
}
func ReadFileTool(repoPath string) Tool {
return Tool{
Name: "read_file",
Description: "Read a file from the repo.\nArgs: {\"path\": \"internal/agent/agent.go\"}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
path, err := safeJoin(repoPath, args["path"])
if err != nil {
return "", err
}
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("read %s: %w", args["path"], err)
}
return string(data), nil
},
}
}
func WriteFileTool(repoPath string) Tool {
return Tool{
Name: "write_file",
Description: "Write or overwrite a file in the repo.\nArgs: {\"path\": \"internal/agent/agent.go\", \"content\": \"...\"}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
path, err := safeJoin(repoPath, args["path"])
if err != nil {
return "", err
}
if isPathProtected(path) {
return "", fmt.Errorf("write to %s is protected", args["path"])
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return "", err
}
if err := os.WriteFile(path, []byte(args["content"]), 0o644); err != nil {
return "", fmt.Errorf("write %s: %w", args["path"], err)
}
return fmt.Sprintf("wrote %s (%d bytes)", args["path"], len(args["content"])), nil
},
}
}
func EditFileTool(repoPath string) Tool {
return Tool{
Name: "edit_file",
Description: "Edit a file by replacing oldString with newString.\nArgs: {\"path\": \"file.go\", \"oldString\": \"old\", \"newString\": \"new\"}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
path, err := safeJoin(repoPath, args["path"])
if err != nil {
return "", err
}
if isPathProtected(path) {
return "", fmt.Errorf("edit %s is protected", args["path"])
}
oldStr := args["oldString"]
newStr := args["newString"]
if oldStr == "" {
return "", fmt.Errorf("oldString is required")
}
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("read %s: %w", args["path"], err)
}
content := string(data)
if !strings.Contains(content, oldStr) {
return "", fmt.Errorf("oldString not found in file")
}
newContent := strings.Replace(content, oldStr, newStr, 1)
if err := os.WriteFile(path, []byte(newContent), 0o644); err != nil {
return "", fmt.Errorf("write %s: %w", args["path"], err)
}
return fmt.Sprintf("edited %s", args["path"]), nil
},
}
}
func ListFilesTool(repoPath string) Tool {
return Tool{
Name: "list_files",
Description: "List all files in the repo.\nArgs: {}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
const maxFiles = 1000
var files []string
var walkErr error
err := filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
walkErr = err
return nil
}
if info.IsDir() && (info.Name() == ".git" || info.Name() == "vendor" || info.Name() == "node_modules") {
return filepath.SkipDir
}
rel, _ := filepath.Rel(repoPath, path)
files = append(files, rel)
if len(files) >= maxFiles {
return filepath.SkipAll
}
return nil
})
if err != nil && err != filepath.SkipAll {
return strings.Join(files, "\n"), err
}
result := strings.Join(files, "\n")
if len(files) >= maxFiles {
result += fmt.Sprintf("\n\n[truncated: showing first %d files]", maxFiles)
}
return result, walkErr
},
}
}
func SearchTool(repoPath string) Tool {
return Tool{
Name: "search",
Description: "Search for text in files.\nArgs: {\"pattern\": \"TODO\", \"path\": \".\"}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
pattern := args["pattern"]
if pattern == "" {
return "", fmt.Errorf("pattern is required")
}
searchPath := repoPath
if args["path"] != "" {
var err error
searchPath, err = safeJoin(repoPath, args["path"])
if err != nil {
return "", err
}
}
const maxResults = 200
var results []string
err := filepath.Walk(searchPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if ctx.Err() != nil {
return ctx.Err()
}
if info.IsDir() {
if info.Name() == ".git" || info.Name() == "vendor" || info.Name() == "node_modules" {
return filepath.SkipDir
}
return nil
}
if info.Size() > 1024*1024 {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil
}
rel, _ := filepath.Rel(repoPath, path)
lines := strings.Split(string(data), "\n")
for i, line := range lines {
if strings.Contains(line, pattern) {
results = append(results, fmt.Sprintf("%s:%d:%s", rel, i+1, line))
if len(results) >= maxResults {
return filepath.SkipAll
}
}
}
return nil
})
if err != nil && err != filepath.SkipAll {
return strings.Join(results, "\n"), err
}
if len(results) == 0 {
return "no matches found", nil
}
out := strings.Join(results, "\n")
if len(results) >= maxResults {
out += fmt.Sprintf("\n\n[truncated: showing first %d matches]", maxResults)
}
return out, nil
},
}
}
func GitDiffTool(repoPath string) Tool {
return Tool{
Name: "git_diff",
Description: "Show current unstaged changes.\nArgs: {}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
c := exec.CommandContext(ctx, "git", "diff")
c.Dir = repoPath
out, err := c.Output()
return string(out), err
},
}
}
func GitCommitTool(repoPath string) Tool {
return Tool{
Name: "git_commit",
Description: "Stage all changes and commit.\nArgs: {\"message\": \"feat: improve error handling\"}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
// Check if there are any changes to commit.
status := exec.CommandContext(ctx, "git", "status", "--porcelain")
status.Dir = repoPath
statusOut, err := status.CombinedOutput()
if err != nil {
return string(statusOut), fmt.Errorf("git status: %w", err)
}
if len(bytes.TrimSpace(statusOut)) == 0 {
return "nothing to commit", nil
}
msg := args["message"]
if msg == "" {
msg = fmt.Sprintf("iterate: auto-improvement session %s", time.Now().Format("2006-01-02"))
}
// Stage only tracked/changed files, not everything.
diffFiles := exec.CommandContext(ctx, "git", "diff", "--name-only", "--diff-filter=ACMR")
diffFiles.Dir = repoPath
diffOut, _ := diffFiles.CombinedOutput()
// Also include already-staged files.
cachedFiles := exec.CommandContext(ctx, "git", "diff", "--cached", "--name-only")
cachedFiles.Dir = repoPath
cachedOut, _ := cachedFiles.CombinedOutput()
untracked := exec.CommandContext(ctx, "git", "ls-files", "--others", "--exclude-standard")
untracked.Dir = repoPath
untrackedOut, _ := untracked.CombinedOutput()
allFiles := strings.TrimSpace(string(diffOut) + "\n" + string(cachedOut) + "\n" + string(untrackedOut))
if allFiles == "" {
return "nothing to commit", nil
}
fileList := strings.Split(allFiles, "\n")
addArgs := append([]string{"add", "--"}, fileList...)
add := exec.CommandContext(ctx, "git", addArgs...)
add.Dir = repoPath
if out, err := add.CombinedOutput(); err != nil {
return string(out), fmt.Errorf("git add: %w", err)
}
commit := exec.CommandContext(ctx, "git", "commit", "-m", msg)
commit.Dir = repoPath
commit.Env = append(os.Environ(),
"GIT_AUTHOR_NAME=iterate[bot]",
"GIT_AUTHOR_EMAIL=iterate@users.noreply.github.com",
"GIT_COMMITTER_NAME=iterate[bot]",
"GIT_COMMITTER_EMAIL=iterate@users.noreply.github.com",
)
out, err := commit.CombinedOutput()
return string(out), err
},
}
}
func GitRevertTool(repoPath string) Tool {
return Tool{
Name: "git_revert",
Description: "Discard all unstaged changes.\nArgs: {}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
c := exec.CommandContext(ctx, "git", "checkout", "--", ".")
c.Dir = repoPath
out, err := c.CombinedOutput()
return string(out), err
},
}
}
func RunTestsTool(repoPath string) Tool {
return Tool{
Name: "run_tests",
Description: "Run go build and go test.\nArgs: {}",
Execute: func(ctx context.Context, args map[string]string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 120*time.Second)
defer cancel()
var results strings.Builder
build := exec.CommandContext(ctx, "go", "build", "./...")
build.Dir = repoPath
out, err := build.CombinedOutput()
results.WriteString("=== go build ===\n")
results.Write(out)
if err != nil {
results.WriteString("\nBUILD FAILED\n")
return results.String(), fmt.Errorf("build failed")
}
results.WriteString("BUILD OK\n\n")
test := exec.CommandContext(ctx, "go", "test", "./...")
test.Dir = repoPath
out, err = test.CombinedOutput()
results.WriteString("=== go test ===\n")
results.Write(out)
if err != nil {
results.WriteString("\nTESTS FAILED\n")
return results.String(), fmt.Errorf("tests failed")
}
results.WriteString("ALL TESTS PASSED\n")
return results.String(), nil
},
}
}