| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log/slog" |
| 6 | "net/netip" |
| 7 | "os/exec" |
| 8 | "regexp" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "sync" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/release" |
| 15 | "amadan.net/oficina/equipo/internal/runner" |
| 16 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 17 | "amadan.net/oficina/equipo/internal/runner/render" |
| 18 | "amadan.net/oficina/equipo/internal/runner/webread" |
| 19 | "amadan.net/oficina/equipo/internal/wire" |
| 20 | ) |
| 21 | |
| 22 | // capabilityEvery re-probes what the runner can do. |
| 23 | const capabilityEvery = time.Hour |
| 24 | |
| 25 | // Capabilities is the runner's capability report: computed at start, |
| 26 | // re-probed hourly and when the instance disagrees (a placement this |
| 27 | // runner had to refuse), sent with every heartbeat. |
| 28 | type Capabilities struct { |
| 29 | Probe func(context.Context) wire.Capabilities |
| 30 | // Logger hears each gathering: the first (the one the first hello |
| 31 | // waits on) at info, later ones at debug. |
| 32 | Logger *slog.Logger |
| 33 | |
| 34 | mu sync.Mutex |
| 35 | last wire.Capabilities |
| 36 | at time.Time |
| 37 | logged bool |
| 38 | } |
| 39 | |
| 40 | // Get is the newest report, probing when it is older than an hour. |
| 41 | func (c *Capabilities) Get(ctx context.Context) wire.Capabilities { |
| 42 | c.mu.Lock() |
| 43 | defer c.mu.Unlock() |
| 44 | if c.at.IsZero() || time.Since(c.at) > capabilityEvery { |
| 45 | level := slog.LevelDebug |
| 46 | if !c.logged { |
| 47 | level, c.logged = slog.LevelInfo, true |
| 48 | } |
| 49 | start := time.Now() |
| 50 | if c.Logger != nil { |
| 51 | c.Logger.Log(ctx, level, "capabilities gathering") |
| 52 | } |
| 53 | next := c.Probe(ctx) |
| 54 | if c.Logger != nil { |
| 55 | c.Logger.Log(ctx, level, "capabilities gathered", "took", took(start), "tags", strings.Join(next.Tags, ",")) |
| 56 | } |
| 57 | if wire.ValidCapabilities(next) != nil { |
| 58 | // Out of bounds: say only the tags, which are always valid. |
| 59 | next = wire.Capabilities{Tags: next.Tags} |
| 60 | if wire.ValidCapabilities(next) != nil { |
| 61 | next = wire.Capabilities{} |
| 62 | } |
| 63 | } |
| 64 | c.last, c.at = next, time.Now() |
| 65 | } |
| 66 | return c.last |
| 67 | } |
| 68 | |
| 69 | // Stale makes the next Get probe again. |
| 70 | func (c *Capabilities) Stale() { |
| 71 | c.mu.Lock() |
| 72 | c.at = time.Time{} |
| 73 | c.mu.Unlock() |
| 74 | } |
| 75 | |
| 76 | var goVersion = regexp.MustCompile(`\bgo1\.[0-9]+(?:\.[0-9]+)?\b`) |
| 77 | |
| 78 | // linuxCapabilities is what a Linux runner reports: the linux tag, claude |
| 79 | // when it runs Claude Code, go when checks have the Go toolchain. Never a |
| 80 | // path or raw output. |
| 81 | // addrs lists this machine's addresses (nil: its interfaces). |
| 82 | func linuxCapabilities(c runner.Config, previews func() *wire.PreviewHost, site *SiteOptions, addrs func() ([]netip.Addr, error), renderer *render.Health) func(context.Context) wire.Capabilities { |
| 83 | if addrs == nil { |
| 84 | addrs = webread.LocalAddresses |
| 85 | } |
| 86 | return func(ctx context.Context) wire.Capabilities { |
| 87 | host := previews() |
| 88 | out := wire.Capabilities{Simulators: []wire.Simulator{}, Tags: []string{wire.TagLinux}, Previews: host != nil, PreviewHost: host} |
| 89 | // The Research site inventory's detected addresses (absent when |
| 90 | // they cannot be read). |
| 91 | out.Research = researchCapability(addrs) |
| 92 | // Whether the pinned preview scanner is installed and matches its |
| 93 | // pin (protocol 30, for the coordinator's status). |
| 94 | out.Tools = previewToolHealth(c) |
| 95 | // The page renderer's (Linux amd64; nil and empty elsewhere): ok |
| 96 | // only once its tree verified and rendered the fixture page. |
| 97 | out.Tools.Renderer = renderer.Report(ctx) |
| 98 | // Which carlos, and what it can reach (protocol 31): its version, |
| 99 | // the owner logins and the deploy keys, never a secret. |
| 100 | var cli *carlosCLI |
| 101 | if site != nil { |
| 102 | cli = site.cli() |
| 103 | } |
| 104 | out.Tools.Carlos, out.Tools.CarlosVersion = cli.Health(ctx) |
| 105 | out.Reach = site.Reach(ctx) |
| 106 | if c.ClaudePath != "" { |
| 107 | out.Harness.Claude = provider.ClaudeVersion |
| 108 | out.Tags = append(out.Tags, wire.TagClaude) |
| 109 | } |
| 110 | if c.GoBinary != "" { |
| 111 | pctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 112 | raw, err := exec.CommandContext(pctx, c.GoBinary, "version").Output() |
| 113 | cancel() |
| 114 | if err == nil { |
| 115 | out.Toolchains.Go = goVersion.FindString(string(raw)) |
| 116 | } |
| 117 | out.Tags = append(out.Tags, wire.TagGo) |
| 118 | } |
| 119 | return out |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // hostPlatform is the platform the runner runs on. |
| 124 | func hostPlatform() wire.Platform { return wire.Platform{OS: runtime.GOOS, Arch: runtime.GOARCH} } |
| 125 | |
| 126 | // linkedRendererPin is the renderer pin linked into this build, "" for |
| 127 | // none (the health then says unpinned). |
| 128 | func linkedRendererPin() string { |
| 129 | pin, _ := release.RendererPin() |
| 130 | return pin |
| 131 | } |
| 132 | |
| 133 | // rendererHealth is the page renderer's health on Linux amd64 |
| 134 | // (render.Platform): the tree installed beside this runner for pin |
| 135 | // (release.RendererPin), launched with the configured bubblewrap. Nil |
| 136 | // elsewhere, so the report has no renderer field. |
| 137 | func rendererHealth(c runner.Config, goos, goarch, pin string, logger *slog.Logger) *render.Health { |
| 138 | if !render.Platform(goos, goarch) { |
| 139 | return nil |
| 140 | } |
| 141 | cfg, scratch := render.Installed(c.Bwrap(), c.Executable, pin) |
| 142 | return &render.Health{Config: cfg, Scratch: scratch, Logger: logger} |
| 143 | } |
| 144 | |