package thin import ( "context" "os" "os/exec" "path/filepath" "regexp" "strings" "sync" "time" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/wire" ) // The carlos CLI the runner runs for previews and sites: one lookup // (EQUIPO_CARLOS_PATH, else PATH, else ~/.local/bin), an absolute regular // executable file or none. A stale carlos earlier on PATH broke every // preview on the production runner (2026-11-26), so its version is read // at start and with every capability probe and said in the hello; a // preview or a publish refuses carlos_outdated below wire.MinCarlos. // carlosLookup is the carlos the configuration names, "" when none is a // usable file. func carlosLookup(c runner.Config) string { carlos := c.CarlosPath if carlos == "" { if p, err := exec.LookPath("carlos"); err == nil { carlos = p } else if home, err := os.UserHomeDir(); err == nil { carlos = filepath.Join(home, ".local", "bin", "carlos") } } if !filepath.IsAbs(carlos) { return "" } if info, err := os.Stat(carlos); err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0o111 == 0 { return "" } return carlos } // carlosRelease is the release `carlos version` names on its first line. var carlosRelease = regexp.MustCompile(`^carlos (v[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4})\b`) // carlosVersionOf runs `carlos version` (5 seconds, an empty environment) // and returns the release it names, "" when it names none. func carlosVersionOf(ctx context.Context, bin string) string { if bin == "" { return "" } cctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() cmd := exec.CommandContext(cctx, bin, "version") dir, err := os.MkdirTemp("", "carlos-version-") if err != nil { return "" } defer os.RemoveAll(dir) cmd.Dir, cmd.Env = dir, []string{"HOME=" + dir, "PATH=/usr/bin:/bin", "CARLOS_NO_UPDATE_CHECK=1"} out, _, err := boundedOutput(cmd, 4096) if err != nil { return "" } line, _, _ := strings.Cut(ansi.ReplaceAllString(string(out), ""), "\n") m := carlosRelease.FindStringSubmatch(strings.TrimSpace(line)) if m == nil { return "" } return m[1] } // readCarlosVersion reads a carlos's version (a seam: the package's tests // stand in for the many fake carlos binaries that have no version verb). var readCarlosVersion = carlosVersionOf // carlosCLI is the runner's carlos and its version, read at most once a // capability period. type carlosCLI struct { Bin string // Read is the seam for the version read (nil: carlosVersionOf). Read func(ctx context.Context, bin string) string mu sync.Mutex version string at time.Time } // Version is the carlos release, re-read when older than an hour. func (c *carlosCLI) Version(ctx context.Context) string { if c == nil { return "" } c.mu.Lock() defer c.mu.Unlock() if c.at.IsZero() || time.Since(c.at) > capabilityEvery { read := c.Read if read == nil { read = readCarlosVersion } c.version, c.at = read(ctx, c.Bin), time.Now() } return c.version } // Health is the carlos health a hello says: missing with no binary, // outdated when its version is unreadable or below wire.MinCarlos, else // ok, with the version when one was read. func (c *carlosCLI) Health(ctx context.Context) (string, string) { if c == nil || c.Bin == "" { return wire.CarlosMissing, "" } v := c.Version(ctx) if !wire.ValidCarlosVersion(v) { return wire.CarlosOutdated, "" } if !wire.CarlosAtLeast(v, wire.MinCarlos) { return wire.CarlosOutdated, v } return wire.CarlosOK, v } // OK reports whether previews and sites may run carlos now. func (c *carlosCLI) OK(ctx context.Context) bool { state, _ := c.Health(ctx) return state == wire.CarlosOK } // carlosCLIs are the carlos binaries previews run, each read once a // capability period. var carlosCLIs sync.Map // carlosOK reports whether the carlos at bin is at least wire.MinCarlos. func carlosOK(ctx context.Context, bin string) bool { v, _ := carlosCLIs.LoadOrStore(bin, &carlosCLI{Bin: bin}) return v.(*carlosCLI).OK(ctx) } // previewCarlosOK reports whether the preview host's carlos may run: the // devbox's, else the temporary sites' credential's. func (e *Engineer) previewCarlosOK(ctx context.Context) bool { switch { case e.Devbox != nil: return carlosOK(ctx, e.Devbox.Carlos) case e.Preview != nil: return carlosOK(ctx, e.Preview.Carlos) } return false }