| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package claudetool |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "math" |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 9 | "os" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 10 | "os/exec" |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 11 | "path/filepath" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 12 | "strings" |
| 13 | "syscall" |
| 14 | "time" |
| 15 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 16 | "sketch.dev/claudetool/bashkit" |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 17 | "sketch.dev/llm" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 18 | ) |
| 19 | |
| Josh Bleecher Snyder | d499fd6 | 2025-04-30 01:31:29 +0000 | [diff] [blame] | 20 | // PermissionCallback is a function type for checking if a command is allowed to run |
| 21 | type PermissionCallback func(command string) error |
| 22 | |
| 23 | // BashTool is a struct for executing shell commands with bash -c and optional timeout |
| 24 | type BashTool struct { |
| 25 | // CheckPermission is called before running any command, if set |
| 26 | CheckPermission PermissionCallback |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| Josh Bleecher Snyder | d499fd6 | 2025-04-30 01:31:29 +0000 | [diff] [blame] | 29 | // NewBashTool creates a new Bash tool with optional permission callback |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 30 | func NewBashTool(checkPermission PermissionCallback) *llm.Tool { |
| Josh Bleecher Snyder | d499fd6 | 2025-04-30 01:31:29 +0000 | [diff] [blame] | 31 | tool := &BashTool{ |
| 32 | CheckPermission: checkPermission, |
| 33 | } |
| 34 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 35 | return &llm.Tool{ |
| Josh Bleecher Snyder | d499fd6 | 2025-04-30 01:31:29 +0000 | [diff] [blame] | 36 | Name: bashName, |
| 37 | Description: strings.TrimSpace(bashDescription), |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 38 | InputSchema: llm.MustSchema(bashInputSchema), |
| Josh Bleecher Snyder | d499fd6 | 2025-04-30 01:31:29 +0000 | [diff] [blame] | 39 | Run: tool.Run, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // The Bash tool executes shell commands with bash -c and optional timeout |
| 44 | var Bash = NewBashTool(nil) |
| 45 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 46 | const ( |
| 47 | bashName = "bash" |
| 48 | bashDescription = ` |
| 49 | Executes a shell command using bash -c with an optional timeout, returning combined stdout and stderr. |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 50 | When run with background flag, the process may keep running after the tool call returns, and |
| 51 | the agent can inspect the output by reading the output files. Use the background task when, for example, |
| 52 | starting a server to test something. Be sure to kill the process group when done. |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 53 | |
| 54 | Executables pre-installed in this environment include: |
| 55 | - standard unix tools |
| 56 | - go |
| 57 | - git |
| 58 | - rg |
| 59 | - jq |
| 60 | - gopls |
| 61 | - sqlite |
| 62 | - fzf |
| 63 | - gh |
| 64 | - python3 |
| 65 | ` |
| 66 | // If you modify this, update the termui template for prettier rendering. |
| 67 | bashInputSchema = ` |
| 68 | { |
| 69 | "type": "object", |
| 70 | "required": ["command"], |
| 71 | "properties": { |
| 72 | "command": { |
| 73 | "type": "string", |
| 74 | "description": "Shell script to execute" |
| 75 | }, |
| 76 | "timeout": { |
| 77 | "type": "string", |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 78 | "description": "Timeout as a Go duration string, defaults to 1m if background is false; 10m if background is true" |
| 79 | }, |
| 80 | "background": { |
| 81 | "type": "boolean", |
| 82 | "description": "If true, executes the command in the background without waiting for completion" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | ` |
| 87 | ) |
| 88 | |
| 89 | type bashInput struct { |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 90 | Command string `json:"command"` |
| 91 | Timeout string `json:"timeout,omitempty"` |
| 92 | Background bool `json:"background,omitempty"` |
| 93 | } |
| 94 | |
| 95 | type BackgroundResult struct { |
| 96 | PID int `json:"pid"` |
| 97 | StdoutFile string `json:"stdout_file"` |
| 98 | StderrFile string `json:"stderr_file"` |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func (i *bashInput) timeout() time.Duration { |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 102 | if i.Timeout != "" { |
| 103 | dur, err := time.ParseDuration(i.Timeout) |
| 104 | if err == nil { |
| 105 | return dur |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Otherwise, use different defaults based on background mode |
| 110 | if i.Background { |
| 111 | return 10 * time.Minute |
| 112 | } else { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 113 | return 1 * time.Minute |
| 114 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| Josh Bleecher Snyder | d499fd6 | 2025-04-30 01:31:29 +0000 | [diff] [blame] | 117 | func (b *BashTool) Run(ctx context.Context, m json.RawMessage) (string, error) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 118 | var req bashInput |
| 119 | if err := json.Unmarshal(m, &req); err != nil { |
| 120 | return "", fmt.Errorf("failed to unmarshal bash command input: %w", err) |
| 121 | } |
| Josh Bleecher Snyder | d499fd6 | 2025-04-30 01:31:29 +0000 | [diff] [blame] | 122 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 123 | // do a quick permissions check (NOT a security barrier) |
| 124 | err := bashkit.Check(req.Command) |
| 125 | if err != nil { |
| 126 | return "", err |
| 127 | } |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 128 | |
| Josh Bleecher Snyder | d499fd6 | 2025-04-30 01:31:29 +0000 | [diff] [blame] | 129 | // Custom permission callback if set |
| 130 | if b.CheckPermission != nil { |
| 131 | if err := b.CheckPermission(req.Command); err != nil { |
| 132 | return "", err |
| 133 | } |
| 134 | } |
| 135 | |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 136 | // If Background is set to true, use executeBackgroundBash |
| 137 | if req.Background { |
| 138 | result, err := executeBackgroundBash(ctx, req) |
| 139 | if err != nil { |
| 140 | return "", err |
| 141 | } |
| 142 | // Marshal the result to JSON |
| Josh Bleecher Snyder | 56ac605 | 2025-04-24 10:40:55 -0700 | [diff] [blame] | 143 | // TODO: emit XML(-ish) instead? |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 144 | output, err := json.Marshal(result) |
| 145 | if err != nil { |
| 146 | return "", fmt.Errorf("failed to marshal background result: %w", err) |
| 147 | } |
| 148 | return string(output), nil |
| 149 | } |
| 150 | |
| 151 | // For foreground commands, use executeBash |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 152 | out, execErr := executeBash(ctx, req) |
| 153 | if execErr == nil { |
| 154 | return out, nil |
| 155 | } |
| 156 | return "", execErr |
| 157 | } |
| 158 | |
| 159 | const maxBashOutputLength = 131072 |
| 160 | |
| 161 | func executeBash(ctx context.Context, req bashInput) (string, error) { |
| 162 | execCtx, cancel := context.WithTimeout(ctx, req.timeout()) |
| 163 | defer cancel() |
| 164 | |
| 165 | // Can't do the simple thing and call CombinedOutput because of the need to kill the process group. |
| 166 | cmd := exec.CommandContext(execCtx, "bash", "-c", req.Command) |
| 167 | cmd.Dir = WorkingDir(ctx) |
| 168 | cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 169 | |
| 170 | var output bytes.Buffer |
| 171 | cmd.Stdin = nil |
| 172 | cmd.Stdout = &output |
| 173 | cmd.Stderr = &output |
| 174 | if err := cmd.Start(); err != nil { |
| 175 | return "", fmt.Errorf("command failed: %w", err) |
| 176 | } |
| 177 | proc := cmd.Process |
| 178 | done := make(chan struct{}) |
| 179 | go func() { |
| 180 | select { |
| 181 | case <-execCtx.Done(): |
| 182 | if execCtx.Err() == context.DeadlineExceeded && proc != nil { |
| 183 | // Kill the entire process group. |
| 184 | syscall.Kill(-proc.Pid, syscall.SIGKILL) |
| 185 | } |
| 186 | case <-done: |
| 187 | } |
| 188 | }() |
| 189 | |
| 190 | err := cmd.Wait() |
| 191 | close(done) |
| 192 | |
| 193 | if execCtx.Err() == context.DeadlineExceeded { |
| 194 | return "", fmt.Errorf("command timed out after %s", req.timeout()) |
| 195 | } |
| 196 | longOutput := output.Len() > maxBashOutputLength |
| 197 | var outstr string |
| 198 | if longOutput { |
| 199 | outstr = fmt.Sprintf("output too long: got %v, max is %v\ninitial bytes of output:\n%s", |
| 200 | humanizeBytes(output.Len()), humanizeBytes(maxBashOutputLength), |
| 201 | output.Bytes()[:1024], |
| 202 | ) |
| 203 | } else { |
| 204 | outstr = output.String() |
| 205 | } |
| 206 | |
| 207 | if err != nil { |
| 208 | return "", fmt.Errorf("command failed: %w\n%s", err, outstr) |
| 209 | } |
| 210 | |
| 211 | if longOutput { |
| 212 | return "", fmt.Errorf("%s", outstr) |
| 213 | } |
| 214 | |
| 215 | return output.String(), nil |
| 216 | } |
| 217 | |
| 218 | func humanizeBytes(bytes int) string { |
| 219 | switch { |
| 220 | case bytes < 4*1024: |
| 221 | return fmt.Sprintf("%dB", bytes) |
| 222 | case bytes < 1024*1024: |
| 223 | kb := int(math.Round(float64(bytes) / 1024.0)) |
| 224 | return fmt.Sprintf("%dkB", kb) |
| 225 | case bytes < 1024*1024*1024: |
| 226 | mb := int(math.Round(float64(bytes) / (1024.0 * 1024.0))) |
| 227 | return fmt.Sprintf("%dMB", mb) |
| 228 | } |
| 229 | return "more than 1GB" |
| 230 | } |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 231 | |
| 232 | // executeBackgroundBash executes a command in the background and returns the pid and output file locations |
| 233 | func executeBackgroundBash(ctx context.Context, req bashInput) (*BackgroundResult, error) { |
| 234 | // Create temporary directory for output files |
| 235 | tmpDir, err := os.MkdirTemp("", "sketch-bg-") |
| 236 | if err != nil { |
| 237 | return nil, fmt.Errorf("failed to create temp directory: %w", err) |
| 238 | } |
| 239 | |
| 240 | // Create temp files for stdout and stderr |
| 241 | stdoutFile := filepath.Join(tmpDir, "stdout") |
| 242 | stderrFile := filepath.Join(tmpDir, "stderr") |
| 243 | |
| 244 | // Prepare the command |
| 245 | cmd := exec.Command("bash", "-c", req.Command) |
| 246 | cmd.Dir = WorkingDir(ctx) |
| 247 | cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 248 | |
| 249 | // Open output files |
| 250 | stdout, err := os.Create(stdoutFile) |
| 251 | if err != nil { |
| 252 | return nil, fmt.Errorf("failed to create stdout file: %w", err) |
| 253 | } |
| 254 | defer stdout.Close() |
| 255 | |
| 256 | stderr, err := os.Create(stderrFile) |
| 257 | if err != nil { |
| 258 | return nil, fmt.Errorf("failed to create stderr file: %w", err) |
| 259 | } |
| 260 | defer stderr.Close() |
| 261 | |
| 262 | // Configure command to use the files |
| 263 | cmd.Stdin = nil |
| 264 | cmd.Stdout = stdout |
| 265 | cmd.Stderr = stderr |
| 266 | |
| 267 | // Start the command |
| 268 | if err := cmd.Start(); err != nil { |
| 269 | return nil, fmt.Errorf("failed to start background command: %w", err) |
| 270 | } |
| 271 | |
| 272 | // Start a goroutine to reap the process when it finishes |
| 273 | go func() { |
| 274 | cmd.Wait() |
| 275 | // Process has been reaped |
| 276 | }() |
| 277 | |
| 278 | // Set up timeout handling if a timeout was specified |
| 279 | pid := cmd.Process.Pid |
| 280 | timeout := req.timeout() |
| 281 | if timeout > 0 { |
| 282 | // Launch a goroutine that will kill the process after the timeout |
| 283 | go func() { |
| Josh Bleecher Snyder | 56ac605 | 2025-04-24 10:40:55 -0700 | [diff] [blame] | 284 | // TODO(josh): this should use a context instead of a sleep, like executeBash above, |
| 285 | // to avoid goroutine leaks. Possibly should be partially unified with executeBash. |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 286 | // Sleep for the timeout duration |
| 287 | time.Sleep(timeout) |
| 288 | |
| 289 | // TODO(philip): Should we do SIGQUIT and then SIGKILL in 5s? |
| 290 | |
| 291 | // Try to kill the process group |
| 292 | killErr := syscall.Kill(-pid, syscall.SIGKILL) |
| 293 | if killErr != nil { |
| 294 | // If killing the process group fails, try to kill just the process |
| 295 | syscall.Kill(pid, syscall.SIGKILL) |
| 296 | } |
| 297 | }() |
| 298 | } |
| 299 | |
| 300 | // Return the process ID and file paths |
| 301 | return &BackgroundResult{ |
| 302 | PID: cmd.Process.Pid, |
| 303 | StdoutFile: stdoutFile, |
| 304 | StderrFile: stderrFile, |
| 305 | }, nil |
| 306 | } |
| Josh Bleecher Snyder | d499fd6 | 2025-04-30 01:31:29 +0000 | [diff] [blame] | 307 | |
| 308 | // BashRun is the legacy function for testing compatibility |
| 309 | func BashRun(ctx context.Context, m json.RawMessage) (string, error) { |
| 310 | // Use the default Bash tool which has no permission callback |
| 311 | return Bash.Run(ctx, m) |
| 312 | } |