package thin import ( "context" "log/slog" "net/netip" "os/exec" "regexp" "runtime" "strings" "sync" "time" "amadan.net/oficina/equipo/internal/release" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runner/render" "amadan.net/oficina/equipo/internal/runner/webread" "amadan.net/oficina/equipo/internal/wire" ) // capabilityEvery re-probes what the runner can do. const capabilityEvery = time.Hour // Capabilities is the runner's capability report: computed at start, // re-probed hourly and when the instance disagrees (a placement this // runner had to refuse), sent with every heartbeat. type Capabilities struct { Probe func(context.Context) wire.Capabilities // Logger hears each gathering: the first (the one the first hello // waits on) at info, later ones at debug. Logger *slog.Logger mu sync.Mutex last wire.Capabilities at time.Time logged bool } // Get is the newest report, probing when it is older than an hour. func (c *Capabilities) Get(ctx context.Context) wire.Capabilities { c.mu.Lock() defer c.mu.Unlock() if c.at.IsZero() || time.Since(c.at) > capabilityEvery { level := slog.LevelDebug if !c.logged { level, c.logged = slog.LevelInfo, true } start := time.Now() if c.Logger != nil { c.Logger.Log(ctx, level, "capabilities gathering") } next := c.Probe(ctx) if c.Logger != nil { c.Logger.Log(ctx, level, "capabilities gathered", "took", took(start), "tags", strings.Join(next.Tags, ",")) } if wire.ValidCapabilities(next) != nil { // Out of bounds: say only the tags, which are always valid. next = wire.Capabilities{Tags: next.Tags} if wire.ValidCapabilities(next) != nil { next = wire.Capabilities{} } } c.last, c.at = next, time.Now() } return c.last } // Stale makes the next Get probe again. func (c *Capabilities) Stale() { c.mu.Lock() c.at = time.Time{} c.mu.Unlock() } var goVersion = regexp.MustCompile(`\bgo1\.[0-9]+(?:\.[0-9]+)?\b`) // linuxCapabilities is what a Linux runner reports: the linux tag, claude // when it runs Claude Code, go when checks have the Go toolchain. Never a // path or raw output. // addrs lists this machine's addresses (nil: its interfaces). func linuxCapabilities(c runner.Config, previews func() *wire.PreviewHost, site *SiteOptions, addrs func() ([]netip.Addr, error), renderer *render.Health) func(context.Context) wire.Capabilities { if addrs == nil { addrs = webread.LocalAddresses } return func(ctx context.Context) wire.Capabilities { host := previews() out := wire.Capabilities{Simulators: []wire.Simulator{}, Tags: []string{wire.TagLinux}, Previews: host != nil, PreviewHost: host} // The Research site inventory's detected addresses (absent when // they cannot be read). out.Research = researchCapability(addrs) // Whether the pinned preview scanner is installed and matches its // pin (protocol 30, for the coordinator's status). out.Tools = previewToolHealth(c) // The page renderer's (Linux amd64; nil and empty elsewhere): ok // only once its tree verified and rendered the fixture page. out.Tools.Renderer = renderer.Report(ctx) // Which carlos, and what it can reach (protocol 31): its version, // the owner logins and the deploy keys, never a secret. var cli *carlosCLI if site != nil { cli = site.cli() } out.Tools.Carlos, out.Tools.CarlosVersion = cli.Health(ctx) out.Reach = site.Reach(ctx) if c.ClaudePath != "" { out.Harness.Claude = provider.ClaudeVersion out.Tags = append(out.Tags, wire.TagClaude) } if c.GoBinary != "" { pctx, cancel := context.WithTimeout(ctx, 5*time.Second) raw, err := exec.CommandContext(pctx, c.GoBinary, "version").Output() cancel() if err == nil { out.Toolchains.Go = goVersion.FindString(string(raw)) } out.Tags = append(out.Tags, wire.TagGo) } return out } } // hostPlatform is the platform the runner runs on. func hostPlatform() wire.Platform { return wire.Platform{OS: runtime.GOOS, Arch: runtime.GOARCH} } // linkedRendererPin is the renderer pin linked into this build, "" for // none (the health then says unpinned). func linkedRendererPin() string { pin, _ := release.RendererPin() return pin } // rendererHealth is the page renderer's health on Linux amd64 // (render.Platform): the tree installed beside this runner for pin // (release.RendererPin), launched with the configured bubblewrap. Nil // elsewhere, so the report has no renderer field. func rendererHealth(c runner.Config, goos, goarch, pin string, logger *slog.Logger) *render.Health { if !render.Platform(goos, goarch) { return nil } cfg, scratch := render.Installed(c.Bwrap(), c.Executable, pin) return &render.Health{Config: cfg, Scratch: scratch, Logger: logger} }