package main
import (
"context"
"fmt"
"os"
"strings"
sandbox "github.com/brimblehq/brimble-sdks/sandbox-go"
)
func runAgent(ctx context.Context, client *sandbox.Client, repoURL, task, anthropicKey string) (string, string, error) {
handle, err := client.Sandboxes.Create(ctx, sandbox.CreateSandboxRequest{
Region: "auto",
Template: "claude-code",
Persistent: true,
PersistentDiskGB: 20,
AutoDestroy: true,
DestroyTimeout: "3h",
})
if err != nil {
return "", "", err
}
defer handle.Destroy(context.Background())
if err := handle.PutFile(ctx, "/work/task.md", strings.NewReader(task), int64(len(task))); err != nil {
return "", "", err
}
cmd := strings.Join([]string{
"set -e",
"mkdir -p /work && cd /work",
fmt.Sprintf("git clone --depth 1 %s repo", repoURL),
"cd repo",
"claude --print --dangerously-skip-permissions < /work/task.md > /work/agent.log 2>&1",
}, " && ")
run, err := handle.Exec(ctx, sandbox.ExecInput{
Cmd: cmd,
Env: map[string]string{"ANTHROPIC_API_KEY": anthropicKey},
TimeoutSeconds: 1800,
})
if err != nil {
return "", "", err
}
if run.ExitCode != 0 {
return "", "", fmt.Errorf("agent failed (%d): %s", run.ExitCode, run.Stderr)
}
diff, err := handle.Exec(ctx, sandbox.ExecInput{
Cmd: "cd /work/repo && git add -A && git diff --staged",
})
if err != nil {
return "", "", err
}
snap, err := handle.Snapshots.Create(ctx, sandbox.CreateSnapshotInput{Name: "post-run"})
if err != nil {
return "", "", err
}
return diff.Stdout, snap.ID, nil
}
func main() {
ctx := context.Background()
client, _ := sandbox.NewClient(sandbox.ClientConfig{})
diff, snapID, _ := runAgent(ctx, client,
"https://github.com/myorg/widgets.git",
"Refactor src/utils to use async/await. Add tests.",
os.Getenv("ANTHROPIC_API_KEY"),
)
fmt.Println("snapshot:", snapID)
fmt.Println(diff)
}