blob: 7aecb169dd6d5abc389ef84afbaeb44de7b0ee56 [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001// Package dockerimg
2package dockerimg
3
4import (
5 "bytes"
6 "context"
Philip Zeyliger5e227dd2025-04-21 15:55:29 -07007 "crypto/rand"
Earl Lee2e463fb2025-04-17 11:22:22 -07008 "crypto/sha256"
9 "encoding/hex"
10 "encoding/json"
11 "fmt"
12 "io"
13 "log/slog"
14 "net"
15 "net/http"
16 "os"
17 "os/exec"
18 "path/filepath"
19 "runtime"
20 "strings"
Josh Bleecher Snyder99570462025-05-05 10:26:14 -070021 "sync/atomic"
Earl Lee2e463fb2025-04-17 11:22:22 -070022 "time"
23
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +000024 "sketch.dev/browser"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070025 "sketch.dev/llm/ant"
Sean McCulloughbaa2b592025-04-23 10:40:08 -070026 "sketch.dev/loop/server"
Earl Lee2e463fb2025-04-17 11:22:22 -070027 "sketch.dev/skribe"
Philip Zeyliger5d6af872025-04-23 19:48:34 -070028 "sketch.dev/webui"
Earl Lee2e463fb2025-04-17 11:22:22 -070029)
30
31// ContainerConfig holds all configuration for launching a container
32type ContainerConfig struct {
33 // SessionID is the unique identifier for this session
34 SessionID string
35
36 // LocalAddr is the initial address to use (though it may be overwritten later)
37 LocalAddr string
38
39 // SkabandAddr is the address of the skaband service if available
40 SkabandAddr string
41
David Crawshaw5a7b3692025-05-05 16:49:15 -070042 // Model is the name of the LLM model to use.
43 Model string
Earl Lee2e463fb2025-04-17 11:22:22 -070044
David Crawshaw5a7b3692025-05-05 16:49:15 -070045 // ModelURL is the URL of the LLM service.
46 ModelURL string
47
48 // ModelAPIKey is the API key for LLM service.
49 ModelAPIKey string
Earl Lee2e463fb2025-04-17 11:22:22 -070050
51 // Path is the local filesystem path to use
52 Path string
53
54 // GitUsername is the username to use for git operations
55 GitUsername string
56
57 // GitEmail is the email to use for git operations
58 GitEmail string
59
60 // OpenBrowser determines whether to open a browser automatically
61 OpenBrowser bool
62
63 // NoCleanup prevents container cleanup when set to true
64 NoCleanup bool
65
66 // ForceRebuild forces rebuilding of the Docker image even if it exists
67 ForceRebuild bool
68
69 // Host directory to copy container logs into, if not set to ""
70 ContainerLogDest string
71
72 // Path to pre-built linux sketch binary, or build a new one if set to ""
73 SketchBinaryLinux string
74
75 // Sketch client public key.
76 SketchPubKey string
Philip Zeyligerd1402952025-04-23 03:54:37 +000077
Sean McCulloughbaa2b592025-04-23 10:40:08 -070078 // Host port for the container's ssh server
79 SSHPort int
80
Philip Zeyliger18532b22025-04-23 21:11:46 +000081 // Outside information to pass to the container
82 OutsideHostname string
83 OutsideOS string
84 OutsideWorkingDir string
Philip Zeyligerb74c4f62025-04-25 19:18:49 -070085
Pokey Rule0dcebe12025-04-28 14:51:04 +010086 // If true, exit after the first turn
87 OneShot bool
88
89 // Initial prompt
90 Prompt string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +000091
92 // Initial commit to use as starting point
93 InitialCommit string
David Crawshawb5f6a002025-05-05 08:27:16 -070094
95 // Verbose enables verbose output
96 Verbose bool
Philip Zeyliger1dc21372025-05-05 19:54:44 +000097
98 // DockerArgs are additional arguments to pass to the docker create command
99 DockerArgs string
Earl Lee2e463fb2025-04-17 11:22:22 -0700100}
101
102// LaunchContainer creates a docker container for a project, installs sketch and opens a connection to it.
103// It writes status to stdout.
David Crawshawb5f6a002025-05-05 08:27:16 -0700104func LaunchContainer(ctx context.Context, config ContainerConfig) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700105 if _, err := exec.LookPath("docker"); err != nil {
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700106 if runtime.GOOS == "darwin" {
107 return fmt.Errorf("cannot find `docker` binary; run: brew install docker colima && colima start")
108 } else {
109 return fmt.Errorf("cannot find `docker` binary; install docker (e.g., apt-get install docker.io)")
110 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700111 }
112
113 if out, err := combinedOutput(ctx, "docker", "ps"); err != nil {
114 // `docker ps` provides a good error message here that can be
115 // easily chatgpt'ed by users, so send it to the user as-is:
116 // Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
117 return fmt.Errorf("docker ps: %s (%w)", out, err)
118 }
119
120 _, hostPort, err := net.SplitHostPort(config.LocalAddr)
121 if err != nil {
122 return err
123 }
124
125 gitRoot, err := findGitRoot(ctx, config.Path)
126 if err != nil {
127 return err
128 }
129
David Crawshaw5a7b3692025-05-05 16:49:15 -0700130 imgName, err := findOrBuildDockerImage(ctx, config.Path, gitRoot, config.Model, config.ModelURL, config.ModelAPIKey, config.ForceRebuild, config.Verbose)
Earl Lee2e463fb2025-04-17 11:22:22 -0700131 if err != nil {
132 return err
133 }
134
135 linuxSketchBin := config.SketchBinaryLinux
136 if linuxSketchBin == "" {
David Crawshawb5f6a002025-05-05 08:27:16 -0700137 linuxSketchBin, err = buildLinuxSketchBin(ctx)
Earl Lee2e463fb2025-04-17 11:22:22 -0700138 if err != nil {
139 return err
140 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700141 }
142
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000143 cntrName := "sketch-" + config.SessionID
Earl Lee2e463fb2025-04-17 11:22:22 -0700144 defer func() {
145 if config.NoCleanup {
146 return
147 }
148 if out, err := combinedOutput(ctx, "docker", "kill", cntrName); err != nil {
149 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
150 _ = out
151 }
152 if out, err := combinedOutput(ctx, "docker", "rm", cntrName); err != nil {
153 // TODO: print in verbose mode? fmt.Fprintf(os.Stderr, "docker kill: %s: %v\n", out, err)
154 _ = out
155 }
156 }()
157
158 // errCh receives errors from operations that this function calls in separate goroutines.
159 errCh := make(chan error)
160
161 // Start the git server
162 gitSrv, err := newGitServer(gitRoot)
163 if err != nil {
164 return fmt.Errorf("failed to start git server: %w", err)
165 }
166 defer gitSrv.shutdown(ctx)
167
168 go func() {
169 errCh <- gitSrv.serve(ctx)
170 }()
171
172 // Get the current host git commit
173 var commit string
Philip Zeyliger1b47aa22025-04-28 19:25:38 +0000174 if out, err := combinedOutput(ctx, "git", "rev-parse", config.InitialCommit); err != nil {
175 return fmt.Errorf("git rev-parse %s: %w", config.InitialCommit, err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700176 } else {
177 commit = strings.TrimSpace(string(out))
178 }
179 if out, err := combinedOutput(ctx, "git", "config", "http.receivepack", "true"); err != nil {
180 return fmt.Errorf("git config http.receivepack true: %s: %w", out, err)
181 }
182
183 relPath, err := filepath.Rel(gitRoot, config.Path)
184 if err != nil {
185 return err
186 }
187
188 // Create the sketch container
189 if err := createDockerContainer(ctx, cntrName, hostPort, relPath, imgName, config); err != nil {
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000190 return fmt.Errorf("failed to create docker container: %w", err)
Earl Lee2e463fb2025-04-17 11:22:22 -0700191 }
192
193 // Copy the sketch linux binary into the container
194 if out, err := combinedOutput(ctx, "docker", "cp", linuxSketchBin, cntrName+":/bin/sketch"); err != nil {
195 return fmt.Errorf("docker cp: %s, %w", out, err)
196 }
Sean McCulloughf5bb3d32025-04-18 10:47:59 -0700197
198 // Make sure that the webui is built so we can copy the results to the container.
199 _, err = webui.Build()
200 if err != nil {
201 return fmt.Errorf("failed to build webui: %w", err)
202 }
203
David Crawshaw8bff16a2025-04-18 01:16:49 -0700204 webuiZipPath, err := webui.ZipPath()
205 if err != nil {
206 return err
207 }
208 if out, err := combinedOutput(ctx, "docker", "cp", webuiZipPath, cntrName+":/root/.cache/sketch/webui/"+filepath.Base(webuiZipPath)); err != nil {
209 return fmt.Errorf("docker cp: %s, %w", out, err)
210 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700211
David Crawshaw53786ef2025-04-24 12:52:51 -0700212 fmt.Printf("📦 running in container %s\n", cntrName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700213
214 // Start the sketch container
215 if out, err := combinedOutput(ctx, "docker", "start", cntrName); err != nil {
216 return fmt.Errorf("docker start: %s, %w", out, err)
217 }
218
219 // Copies structured logs from the container to the host.
220 copyLogs := func() {
221 if config.ContainerLogDest == "" {
222 return
223 }
224 out, err := combinedOutput(ctx, "docker", "logs", cntrName)
225 if err != nil {
226 fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err)
227 return
228 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700229 prefix := []byte("structured logs:")
230 for line := range bytes.Lines(out) {
231 rest, ok := bytes.CutPrefix(line, prefix)
232 if !ok {
Earl Lee2e463fb2025-04-17 11:22:22 -0700233 continue
234 }
Josh Bleecher Snyder7660e4e2025-04-24 10:34:17 -0700235 logFile := string(bytes.TrimSpace(rest))
Earl Lee2e463fb2025-04-17 11:22:22 -0700236 srcPath := fmt.Sprintf("%s:%s", cntrName, logFile)
237 logFileName := filepath.Base(logFile)
238 dstPath := filepath.Join(config.ContainerLogDest, logFileName)
239 _, err := combinedOutput(ctx, "docker", "cp", srcPath, dstPath)
240 if err != nil {
241 fmt.Fprintf(os.Stderr, "docker cp %s %s failed: %v\n", srcPath, dstPath, err)
242 }
243 fmt.Fprintf(os.Stderr, "\ncopied container log %s to %s\n", srcPath, dstPath)
244 }
245 }
246
247 // NOTE: we want to see what the internal sketch binary prints
248 // regardless of the setting of the verbosity flag on the external
249 // binary, so reading "docker logs", which is the stdout/stderr of
250 // the internal binary is not conditional on the verbose flag.
251 appendInternalErr := func(err error) error {
252 if err == nil {
253 return nil
254 }
255 out, logsErr := combinedOutput(ctx, "docker", "logs", cntrName)
Philip Zeyligerd1402952025-04-23 03:54:37 +0000256 if logsErr != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700257 return fmt.Errorf("%w; and docker logs failed: %s, %v", err, out, logsErr)
258 }
259 out = bytes.TrimSpace(out)
260 if len(out) > 0 {
261 return fmt.Errorf("docker logs: %s;\n%w", out, err)
262 }
263 return err
264 }
265
266 // Get the sketch server port from the container
Sean McCulloughae3480f2025-04-23 15:28:20 -0700267 localAddr, err := getContainerPort(ctx, cntrName, "80")
Earl Lee2e463fb2025-04-17 11:22:22 -0700268 if err != nil {
269 return appendInternalErr(err)
270 }
271
Sean McCulloughae3480f2025-04-23 15:28:20 -0700272 localSSHAddr, err := getContainerPort(ctx, cntrName, "22")
273 if err != nil {
274 return appendInternalErr(err)
275 }
276 sshHost, sshPort, err := net.SplitHostPort(localSSHAddr)
277 if err != nil {
David Crawshawb5f6a002025-05-05 08:27:16 -0700278 return appendInternalErr(fmt.Errorf("failed to split ssh host and port: %w", err))
Sean McCulloughae3480f2025-04-23 15:28:20 -0700279 }
Sean McCullough4854c652025-04-24 18:37:02 -0700280
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700281 var sshServerIdentity, sshUserIdentity []byte
Sean McCullough4854c652025-04-24 18:37:02 -0700282
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700283 if err := CheckForInclude(); err != nil {
284 fmt.Println(err.Error())
285 // continue - ssh config is not required for the rest of sketch to function locally.
286 } else {
Josh Bleecher Snyder50608b12025-05-03 22:55:49 +0000287 cst, err := NewSSHTheater(cntrName, sshHost, sshPort)
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700288 if err != nil {
289 return appendInternalErr(fmt.Errorf("NewContainerSSHTheather: %w", err))
290 }
291
Sean McCulloughea3fc202025-04-28 12:53:37 -0700292 // Note: The vscode: link uses an undocumented request parameter that I really had to dig to find:
293 // https://github.com/microsoft/vscode/blob/2b9486161abaca59b5132ce3c59544f3cc7000f6/src/vs/code/electron-main/app.ts#L878
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700294 fmt.Printf(`Connect to this container via any of these methods:
Sean McCullough4854c652025-04-24 18:37:02 -0700295🖥️ ssh %s
296🖥️ code --remote ssh-remote+root@%s /app -n
Sean McCulloughea3fc202025-04-28 12:53:37 -0700297🔗 vscode://vscode-remote/ssh-remote+root@%s/app?windowId=_blank
Sean McCullough4854c652025-04-24 18:37:02 -0700298`, cntrName, cntrName, cntrName)
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700299 sshUserIdentity = cst.userIdentity
300 sshServerIdentity = cst.serverIdentity
301 defer func() {
302 if err := cst.Cleanup(); err != nil {
303 appendInternalErr(err)
304 }
305 }()
306 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700307
Earl Lee2e463fb2025-04-17 11:22:22 -0700308 // Tell the sketch container which git server port and commit to initialize with.
309 go func() {
310 // TODO: Why is this called in a goroutine? I have found that when I pull this out
311 // of the goroutine and call it inline, then the terminal UI clears itself and all
312 // the scrollback (which is not good, but also not fatal). I can't see why it does this
313 // though, since none of the calls in postContainerInitConfig obviously write to stdout
314 // or stderr.
Sean McCulloughf5e28f62025-04-25 10:48:00 -0700315 if err := postContainerInitConfig(ctx, localAddr, commit, gitSrv.gitPort, gitSrv.pass, sshServerIdentity, sshUserIdentity); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700316 slog.ErrorContext(ctx, "LaunchContainer.postContainerInitConfig", slog.String("err", err.Error()))
317 errCh <- appendInternalErr(err)
318 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700319
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700320 // We open the browser after the init config because the above waits for the web server to be serving.
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700321 ps1URL := "http://" + localAddr
322 if config.SkabandAddr != "" {
323 ps1URL = fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID)
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700324 }
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700325 if config.OpenBrowser {
326 browser.Open(ps1URL)
327 }
328 gitSrv.ps1URL.Store(&ps1URL)
Philip Zeyliger6ed6adb2025-04-23 19:56:38 -0700329 }()
Earl Lee2e463fb2025-04-17 11:22:22 -0700330
331 go func() {
332 cmd := exec.CommandContext(ctx, "docker", "attach", cntrName)
333 cmd.Stdin = os.Stdin
334 cmd.Stdout = os.Stdout
335 cmd.Stderr = os.Stderr
336 errCh <- run(ctx, "docker attach", cmd)
337 }()
338
339 defer copyLogs()
340
341 for {
342 select {
343 case <-ctx.Done():
344 return ctx.Err()
345 case err := <-errCh:
346 if err != nil {
347 return appendInternalErr(fmt.Errorf("container process: %w", err))
348 }
349 return nil
350 }
351 }
352}
353
354func combinedOutput(ctx context.Context, cmdName string, args ...string) ([]byte, error) {
355 cmd := exec.CommandContext(ctx, cmdName, args...)
Earl Lee2e463fb2025-04-17 11:22:22 -0700356 start := time.Now()
357
358 out, err := cmd.CombinedOutput()
359 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700360 slog.ErrorContext(ctx, cmdName, slog.Duration("elapsed", time.Since(start)), slog.String("err", err.Error()), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700361 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700362 slog.DebugContext(ctx, cmdName, slog.Duration("elapsed", time.Since(start)), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700363 }
364 return out, err
365}
366
367func run(ctx context.Context, cmdName string, cmd *exec.Cmd) error {
368 start := time.Now()
369 err := cmd.Run()
370 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700371 slog.ErrorContext(ctx, cmdName, slog.Duration("elapsed", time.Since(start)), slog.String("err", err.Error()), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700372 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700373 slog.DebugContext(ctx, cmdName, slog.Duration("elapsed", time.Since(start)), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700374 }
375 return err
376}
377
378type gitServer struct {
379 gitLn net.Listener
380 gitPort string
381 srv *http.Server
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700382 pass string
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700383 ps1URL atomic.Pointer[string]
Earl Lee2e463fb2025-04-17 11:22:22 -0700384}
385
386func (gs *gitServer) shutdown(ctx context.Context) {
387 gs.srv.Shutdown(ctx)
388 gs.gitLn.Close()
389}
390
391// Serve a git remote from the host for the container to fetch from and push to.
392func (gs *gitServer) serve(ctx context.Context) error {
393 slog.DebugContext(ctx, "starting git server", slog.String("git_remote_addr", "http://host.docker.internal:"+gs.gitPort+"/.git"))
394 return gs.srv.Serve(gs.gitLn)
395}
396
397func newGitServer(gitRoot string) (*gitServer, error) {
Josh Bleecher Snyder9f6a9982025-04-22 17:34:15 -0700398 ret := &gitServer{
399 pass: rand.Text(),
400 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700401
Earl Lee2e463fb2025-04-17 11:22:22 -0700402 gitLn, err := net.Listen("tcp4", ":0")
403 if err != nil {
404 return nil, fmt.Errorf("git listen: %w", err)
405 }
406 ret.gitLn = gitLn
407
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700408 browserC := make(chan bool, 1) // channel of browser open requests
409
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000410 go func() {
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700411 for range browserC {
412 browser.Open(*ret.ps1URL.Load())
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000413 }
414 }()
415
416 srv := http.Server{Handler: &gitHTTP{gitRepoRoot: gitRoot, pass: []byte(ret.pass), browserC: browserC}}
Earl Lee2e463fb2025-04-17 11:22:22 -0700417 ret.srv = &srv
418
419 _, gitPort, err := net.SplitHostPort(gitLn.Addr().String())
420 if err != nil {
421 return nil, fmt.Errorf("git port: %w", err)
422 }
423 ret.gitPort = gitPort
424 return ret, nil
425}
426
427func createDockerContainer(ctx context.Context, cntrName, hostPort, relPath, imgName string, config ContainerConfig) error {
David Crawshaw69c67312025-04-17 13:42:00 -0700428 cmdArgs := []string{
429 "create",
David Crawshaw66cf74e2025-05-05 08:48:39 -0700430 "-i",
Earl Lee2e463fb2025-04-17 11:22:22 -0700431 "--name", cntrName,
432 "-p", hostPort + ":80", // forward container port 80 to a host port
David Crawshaw3659d872025-05-05 17:52:23 -0700433 "-e", "SKETCH_MODEL_API_KEY=" + config.ModelAPIKey,
Earl Lee2e463fb2025-04-17 11:22:22 -0700434 }
David Crawshaw66cf74e2025-05-05 08:48:39 -0700435 if !config.OneShot {
436 cmdArgs = append(cmdArgs, "-t")
437 }
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000438
439 for _, envVar := range getEnvForwardingFromGitConfig(ctx) {
440 cmdArgs = append(cmdArgs, "-e", envVar)
441 }
David Crawshaw5a7b3692025-05-05 16:49:15 -0700442 if config.ModelURL != "" {
David Crawshaw3659d872025-05-05 17:52:23 -0700443 cmdArgs = append(cmdArgs, "-e", "SKETCH_MODEL_URL="+config.ModelURL)
Earl Lee2e463fb2025-04-17 11:22:22 -0700444 }
445 if config.SketchPubKey != "" {
446 cmdArgs = append(cmdArgs, "-e", "SKETCH_PUB_KEY="+config.SketchPubKey)
447 }
Sean McCulloughae3480f2025-04-23 15:28:20 -0700448 if config.SSHPort > 0 {
449 cmdArgs = append(cmdArgs, "-p", fmt.Sprintf("%d:22", config.SSHPort)) // forward container ssh port to host ssh port
450 } else {
451 cmdArgs = append(cmdArgs, "-p", "22") // use an ephemeral host port for ssh.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700452 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700453 if relPath != "." {
454 cmdArgs = append(cmdArgs, "-w", "/app/"+relPath)
455 }
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700456 // colima does this by default, but Linux docker seems to need this set explicitly
457 cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
Earl Lee2e463fb2025-04-17 11:22:22 -0700458 cmdArgs = append(
459 cmdArgs,
460 imgName,
461 "/bin/sketch",
462 "-unsafe",
463 "-addr=:80",
464 "-session-id="+config.SessionID,
Philip Zeyligerd1402952025-04-23 03:54:37 +0000465 "-git-username="+config.GitUsername,
466 "-git-email="+config.GitEmail,
Philip Zeyliger18532b22025-04-23 21:11:46 +0000467 "-outside-hostname="+config.OutsideHostname,
468 "-outside-os="+config.OutsideOS,
469 "-outside-working-dir="+config.OutsideWorkingDir,
Josh Bleecher Snyder3cae7d92025-04-30 09:54:29 -0700470 "-open=false",
Earl Lee2e463fb2025-04-17 11:22:22 -0700471 )
David Crawshaw5a7b3692025-05-05 16:49:15 -0700472 if config.Model != "" {
473 cmdArgs = append(cmdArgs, "-model="+config.Model)
474 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700475 if config.SkabandAddr != "" {
476 cmdArgs = append(cmdArgs, "-skaband-addr="+config.SkabandAddr)
477 }
Pokey Rule0dcebe12025-04-28 14:51:04 +0100478 if config.Prompt != "" {
479 cmdArgs = append(cmdArgs, "-prompt", config.Prompt)
480 }
481 if config.OneShot {
482 cmdArgs = append(cmdArgs, "-one-shot")
Philip Zeyligerb74c4f62025-04-25 19:18:49 -0700483 }
Philip Zeyliger1dc21372025-05-05 19:54:44 +0000484
485 // Add additional docker arguments if provided
486 if config.DockerArgs != "" {
487 // Parse space-separated docker arguments with support for quotes and escaping
488 args := parseDockerArgs(config.DockerArgs)
489 // Insert arguments after "create" but before other arguments
490 for i := len(args) - 1; i >= 0; i-- {
491 cmdArgs = append(cmdArgs[:1], append([]string{args[i]}, cmdArgs[1:]...)...)
492 }
493 }
494
Earl Lee2e463fb2025-04-17 11:22:22 -0700495 if out, err := combinedOutput(ctx, "docker", cmdArgs...); err != nil {
496 return fmt.Errorf("docker create: %s, %w", out, err)
497 }
498 return nil
499}
500
David Crawshawb5f6a002025-05-05 08:27:16 -0700501func buildLinuxSketchBin(ctx context.Context) (string, error) {
David Crawshaw8a617cb2025-04-18 01:28:43 -0700502 homeDir, err := os.UserHomeDir()
David Crawshaw69c67312025-04-17 13:42:00 -0700503 if err != nil {
504 return "", err
505 }
David Crawshaw8a617cb2025-04-18 01:28:43 -0700506 linuxGopath := filepath.Join(homeDir, ".cache", "sketch", "linuxgo")
507 if err := os.MkdirAll(linuxGopath, 0o777); err != nil {
508 return "", err
509 }
510
511 verToInstall := "@latest"
512 if out, err := exec.Command("go", "list", "-m").CombinedOutput(); err != nil {
513 return "", fmt.Errorf("failed to run go list -m: %s: %v", out, err)
514 } else {
515 if strings.TrimSpace(string(out)) == "sketch.dev" {
David Crawshaw094e4d22025-04-24 11:35:14 -0700516 slog.DebugContext(ctx, "built linux agent from currently checked out module")
David Crawshaw8a617cb2025-04-18 01:28:43 -0700517 verToInstall = ""
518 }
519 }
David Crawshaw69c67312025-04-17 13:42:00 -0700520
Earl Lee2e463fb2025-04-17 11:22:22 -0700521 start := time.Now()
David Crawshaw8a617cb2025-04-18 01:28:43 -0700522 cmd := exec.CommandContext(ctx, "go", "install", "sketch.dev/cmd/sketch"+verToInstall)
David Crawshawb9eaef52025-04-17 15:23:18 -0700523 cmd.Env = append(
524 os.Environ(),
525 "GOOS=linux",
526 "CGO_ENABLED=0",
527 "GOTOOLCHAIN=auto",
David Crawshaw8a617cb2025-04-18 01:28:43 -0700528 "GOPATH="+linuxGopath,
Josh Bleecher Snyderfae17572025-04-21 11:48:05 -0700529 "GOBIN=",
David Crawshawb9eaef52025-04-17 15:23:18 -0700530 )
Earl Lee2e463fb2025-04-17 11:22:22 -0700531
Earl Lee2e463fb2025-04-17 11:22:22 -0700532 out, err := cmd.CombinedOutput()
533 if err != nil {
David Crawshawc7e77962025-05-03 13:20:18 -0700534 slog.ErrorContext(ctx, "go", slog.Duration("elapsed", time.Since(start)), slog.String("err", err.Error()), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700535 return "", fmt.Errorf("failed to build linux sketch binary: %s: %w", out, err)
536 } else {
David Crawshawc7e77962025-05-03 13:20:18 -0700537 slog.DebugContext(ctx, "go", slog.Duration("elapsed", time.Since(start)), slog.String("path", cmd.Path), slog.String("args", fmt.Sprintf("%v", skribe.Redact(cmd.Args))))
Earl Lee2e463fb2025-04-17 11:22:22 -0700538 }
539
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700540 if runtime.GOOS != "linux" {
David Crawshawc7e77962025-05-03 13:20:18 -0700541 return filepath.Join(linuxGopath, "bin", "linux_"+runtime.GOARCH, "sketch"), nil
Philip Zeyliger5e227dd2025-04-21 15:55:29 -0700542 }
David Crawshawc7e77962025-05-03 13:20:18 -0700543 // If we are already on Linux, there's no extra platform name in the path
544 return filepath.Join(linuxGopath, "bin", "sketch"), nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700545}
546
Sean McCulloughae3480f2025-04-23 15:28:20 -0700547func getContainerPort(ctx context.Context, cntrName, cntrPort string) (string, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700548 localAddr := ""
Sean McCulloughae3480f2025-04-23 15:28:20 -0700549 if out, err := combinedOutput(ctx, "docker", "port", cntrName, cntrPort); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700550 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
551 } else {
552 v4, _, found := strings.Cut(string(out), "\n")
553 if !found {
554 return "", fmt.Errorf("failed to find container port: %s: %v", out, err)
555 }
556 localAddr = v4
557 if strings.HasPrefix(localAddr, "0.0.0.0") {
558 localAddr = "127.0.0.1" + strings.TrimPrefix(localAddr, "0.0.0.0")
559 }
560 }
561 return localAddr, nil
562}
563
564// Contact the container and configure it.
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700565func postContainerInitConfig(ctx context.Context, localAddr, commit, gitPort, gitPass string, sshServerIdentity, sshAuthorizedKeys []byte) error {
Earl Lee2e463fb2025-04-17 11:22:22 -0700566 localURL := "http://" + localAddr
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700567
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000568 // Check if SSH is available by checking for the Include directive in ~/.ssh/config
569 sshAvailable := true
570 sshError := ""
571 if err := CheckForInclude(); err != nil {
572 sshAvailable = false
573 sshError = err.Error()
574 }
575
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700576 initMsg, err := json.Marshal(
577 server.InitRequest{
578 Commit: commit,
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000579 OutsideHTTP: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s", gitPass, gitPort),
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700580 GitRemoteAddr: fmt.Sprintf("http://sketch:%s@host.docker.internal:%s/.git", gitPass, gitPort),
581 HostAddr: localAddr,
582 SSHAuthorizedKeys: sshAuthorizedKeys,
583 SSHServerIdentity: sshServerIdentity,
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000584 SSHAvailable: sshAvailable,
585 SSHError: sshError,
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700586 })
Earl Lee2e463fb2025-04-17 11:22:22 -0700587 if err != nil {
588 return fmt.Errorf("init msg: %w", err)
589 }
590
Earl Lee2e463fb2025-04-17 11:22:22 -0700591 // Note: this /init POST is handled in loop/server/loophttp.go:
592 initMsgByteReader := bytes.NewReader(initMsg)
593 req, err := http.NewRequest("POST", localURL+"/init", initMsgByteReader)
594 if err != nil {
595 return err
596 }
597
598 var res *http.Response
599 for i := 0; ; i++ {
600 time.Sleep(100 * time.Millisecond)
601 // If you DON'T reset this byteReader, then subsequent retries may end up sending 0 bytes.
602 initMsgByteReader.Reset(initMsg)
603 res, err = http.DefaultClient.Do(req)
604 if err != nil {
David Crawshaw99231ba2025-05-03 10:48:26 -0700605 if i < 100 {
606 if i%10 == 0 {
607 slog.DebugContext(ctx, "postContainerInitConfig retrying", slog.Int("retry", i), slog.String("err", err.Error()))
608 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700609 continue
610 }
611 return fmt.Errorf("failed to %s/init sketch in container, NOT retrying: err: %v", localURL, err)
612 }
613 break
614 }
615 resBytes, _ := io.ReadAll(res.Body)
616 if res.StatusCode != http.StatusOK {
617 return fmt.Errorf("failed to initialize sketch in container, response status code %d: %s", res.StatusCode, resBytes)
618 }
619 return nil
620}
621
David Crawshaw5a7b3692025-05-05 16:49:15 -0700622func findOrBuildDockerImage(ctx context.Context, cwd, gitRoot, model, modelURL, modelAPIKey string, forceRebuild, verbose bool) (imgName string, err error) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700623 h := sha256.Sum256([]byte(gitRoot))
624 imgName = "sketch-" + hex.EncodeToString(h[:6])
625
626 var curImgInitFilesHash string
627 if out, err := combinedOutput(ctx, "docker", "inspect", "--format", "{{json .Config.Labels}}", imgName); err != nil {
628 if strings.Contains(string(out), "No such object") {
629 // Image does not exist, continue and build it.
630 curImgInitFilesHash = ""
631 } else {
632 return "", fmt.Errorf("docker inspect failed: %s, %v", out, err)
633 }
634 } else {
635 m := map[string]string{}
636 if err := json.Unmarshal(bytes.TrimSpace(out), &m); err != nil {
637 return "", fmt.Errorf("docker inspect output unparsable: %s, %v", out, err)
638 }
639 curImgInitFilesHash = m["sketch_context"]
640 }
641
642 candidates, err := findRepoDockerfiles(cwd, gitRoot)
643 if err != nil {
644 return "", fmt.Errorf("find dockerfile: %w", err)
645 }
646
647 var initFiles map[string]string
648 var dockerfilePath string
649
650 // TODO: prefer a "Dockerfile.sketch" so users can tailor any env to this tool.
651 if len(candidates) == 1 && strings.ToLower(filepath.Base(candidates[0])) == "dockerfile" {
652 dockerfilePath = candidates[0]
653 contents, err := os.ReadFile(dockerfilePath)
654 if err != nil {
655 return "", err
656 }
657 fmt.Printf("using %s as dev env\n", candidates[0])
658 if hashInitFiles(map[string]string{dockerfilePath: string(contents)}) == curImgInitFilesHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700659 return imgName, nil
660 }
661 } else {
662 initFiles, err = readInitFiles(os.DirFS(gitRoot))
663 if err != nil {
664 return "", err
665 }
666 subPathWorkingDir, err := filepath.Rel(gitRoot, cwd)
667 if err != nil {
668 return "", err
669 }
670 initFileHash := hashInitFiles(initFiles)
671 if curImgInitFilesHash == initFileHash && !forceRebuild {
Earl Lee2e463fb2025-04-17 11:22:22 -0700672 return imgName, nil
673 }
674
David Crawshaw5a7b3692025-05-05 16:49:15 -0700675 if model == "gemini" {
676 if strings.HasSuffix(modelURL, "/gemmsgs") {
677 // Horrible hack! Switch back to anthropic for container building.
David Crawshaw3659d872025-05-05 17:52:23 -0700678 // We can do this because we are talking to skaband and know the address.
David Crawshaw5a7b3692025-05-05 16:49:15 -0700679 modelURL = strings.Replace(modelURL, "/gemmsgs", "/antmsgs", 1)
680 } else {
681 return "", fmt.Errorf("building docker image with gemini model is not supported yet; start with -model=anthropic first then use gemini")
682 }
683 }
684
Earl Lee2e463fb2025-04-17 11:22:22 -0700685 start := time.Now()
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700686 srv := &ant.Service{
David Crawshaw5a7b3692025-05-05 16:49:15 -0700687 URL: modelURL,
688 APIKey: modelAPIKey,
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700689 HTTPC: http.DefaultClient,
690 }
691 dockerfile, err := createDockerfile(ctx, srv, initFiles, subPathWorkingDir)
Earl Lee2e463fb2025-04-17 11:22:22 -0700692 if err != nil {
693 return "", fmt.Errorf("create dockerfile: %w", err)
694 }
David Crawshaw8fd51042025-05-05 12:52:43 -0700695 dockerfilePath = filepath.Join(cwd, tmpSketchDockerfile)
Earl Lee2e463fb2025-04-17 11:22:22 -0700696 if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0o666); err != nil {
697 return "", err
698 }
699 defer os.Remove(dockerfilePath)
700
David Crawshawb5f6a002025-05-05 08:27:16 -0700701 if verbose {
702 fmt.Fprintf(os.Stderr, "generated Dockerfile in %s:\n\t%s\n\n", time.Since(start).Round(time.Millisecond), strings.Replace(dockerfile, "\n", "\n\t", -1))
703 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700704 }
705
706 var gitUserEmail, gitUserName string
707 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.email"); err != nil {
708 return "", fmt.Errorf("git config: %s: %v", out, err)
709 } else {
710 gitUserEmail = strings.TrimSpace(string(out))
711 }
712 if out, err := combinedOutput(ctx, "git", "config", "--get", "user.name"); err != nil {
713 return "", fmt.Errorf("git config: %s: %v", out, err)
714 } else {
715 gitUserName = strings.TrimSpace(string(out))
716 }
717
718 start := time.Now()
719 cmd := exec.CommandContext(ctx,
720 "docker", "build",
721 "-t", imgName,
722 "-f", dockerfilePath,
723 "--build-arg", "GIT_USER_EMAIL="+gitUserEmail,
724 "--build-arg", "GIT_USER_NAME="+gitUserName,
Earl Lee2e463fb2025-04-17 11:22:22 -0700725 )
David Crawshawb5f6a002025-05-05 08:27:16 -0700726 if !verbose {
727 cmd.Args = append(cmd.Args, "--progress=quiet")
Philip Zeyligere4fa0e32025-04-23 14:15:55 -0700728 }
David Crawshawb5f6a002025-05-05 08:27:16 -0700729 cmd.Args = append(cmd.Args, ".")
730 cmd.Dir = gitRoot
731 cmd.Stdout = os.Stdout
732 cmd.Stderr = os.Stderr
733 fmt.Printf("🏗️ building docker image %s... (use -verbose to see build output)\n", imgName)
Earl Lee2e463fb2025-04-17 11:22:22 -0700734
735 err = run(ctx, "docker build", cmd)
736 if err != nil {
737 return "", fmt.Errorf("docker build failed: %v", err)
738 }
739 fmt.Printf("built docker image %s in %s\n", imgName, time.Since(start).Round(time.Millisecond))
740 return imgName, nil
741}
742
743func findRepoDockerfiles(cwd, gitRoot string) ([]string, error) {
744 files, err := findDirDockerfiles(cwd)
745 if err != nil {
746 return nil, err
747 }
748 if len(files) > 0 {
749 return files, nil
750 }
751
752 path := cwd
753 for path != gitRoot {
754 path = filepath.Dir(path)
755 files, err := findDirDockerfiles(path)
756 if err != nil {
757 return nil, err
758 }
759 if len(files) > 0 {
760 return files, nil
761 }
762 }
763 return files, nil
764}
765
766// findDirDockerfiles finds all "Dockerfile*" files in a directory.
767func findDirDockerfiles(root string) (res []string, err error) {
768 err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
769 if err != nil {
770 return err
771 }
772 if info.IsDir() && root != path {
773 return filepath.SkipDir
774 }
775 name := strings.ToLower(info.Name())
776 if name == "dockerfile" || strings.HasPrefix(name, "dockerfile.") {
777 res = append(res, path)
778 }
779 return nil
780 })
781 if err != nil {
782 return nil, err
783 }
784 return res, nil
785}
786
787func findGitRoot(ctx context.Context, path string) (string, error) {
788 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--git-common-dir")
789 cmd.Dir = path
790 out, err := cmd.CombinedOutput()
791 if err != nil {
792 if strings.Contains(string(out), "not a git repository") {
793 return "", fmt.Errorf(`sketch needs to run from within a git repo, but %s is not part of a git repo.
794Consider one of the following options:
795 - cd to a different dir that is already part of a git repo first, or
796 - to create a new git repo from this directory (%s), run this command:
797
798 git init . && git commit --allow-empty -m "initial commit"
799
800and try running sketch again.
801`, path, path)
802 }
803 return "", fmt.Errorf("git rev-parse --git-common-dir: %s: %w", out, err)
804 }
805 gitDir := strings.TrimSpace(string(out)) // location of .git dir, often as a relative path
806 absGitDir := filepath.Join(path, gitDir)
807 return filepath.Dir(absGitDir), err
808}
809
Josh Bleecher Snyder2772f632025-05-01 21:42:35 +0000810// getEnvForwardingFromGitConfig retrieves environment variables to pass through to Docker
811// from git config using the sketch.envfwd multi-valued key.
812func getEnvForwardingFromGitConfig(ctx context.Context) []string {
813 outb, err := exec.CommandContext(ctx, "git", "config", "--get-all", "sketch.envfwd").CombinedOutput()
814 out := string(outb)
815 if err != nil {
816 if strings.Contains(out, "key does not exist") {
817 return nil
818 }
819 slog.ErrorContext(ctx, "failed to get sketch.envfwd from git config", "err", err, "output", out)
820 return nil
821 }
822
823 var envVars []string
824 for envVar := range strings.Lines(out) {
825 envVar = strings.TrimSpace(envVar)
826 if envVar == "" {
827 continue
828 }
829 envVars = append(envVars, envVar+"="+os.Getenv(envVar))
830 }
831 return envVars
832}
Philip Zeyliger1dc21372025-05-05 19:54:44 +0000833
834// parseDockerArgs parses a string containing space-separated Docker arguments into an array of strings.
835// It handles quoted arguments and escaped characters.
836//
837// Examples:
838//
839// --memory=2g --cpus=2 -> ["--memory=2g", "--cpus=2"]
840// --label="my label" --env=FOO=bar -> ["--label=my label", "--env=FOO=bar"]
841// --env="KEY=\"quoted value\"" -> ["--env=KEY=\"quoted value\""]
842func parseDockerArgs(args string) []string {
843 if args = strings.TrimSpace(args); args == "" {
844 return []string{}
845 }
846
847 var result []string
848 var current strings.Builder
849 inQuotes := false
850 escapeNext := false
851 quoteChar := rune(0)
852
853 for _, char := range args {
854 if escapeNext {
855 current.WriteRune(char)
856 escapeNext = false
857 continue
858 }
859
860 if char == '\\' {
861 escapeNext = true
862 continue
863 }
864
865 if char == '"' || char == '\'' {
866 if !inQuotes {
867 inQuotes = true
868 quoteChar = char
869 continue
870 } else if char == quoteChar {
871 inQuotes = false
872 quoteChar = rune(0)
873 continue
874 }
875 // Non-matching quote character inside quotes
876 current.WriteRune(char)
877 continue
878 }
879
880 // Space outside of quotes is an argument separator
881 if char == ' ' && !inQuotes {
882 if current.Len() > 0 {
883 result = append(result, current.String())
884 current.Reset()
885 }
886 continue
887 }
888
889 current.WriteRune(char)
890 }
891
892 // Add the last argument if there is one
893 if current.Len() > 0 {
894 result = append(result, current.String())
895 }
896
897 return result
898}