package runner import ( "net/url" "os" "path/filepath" "runtime" "strings" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" "golang.org/x/sys/unix" ) type Config struct { MachineLabel string ClaudeModel, CodexModel string ToolProxyPath, BwrapPath, ModuleCache, PreviewCredentialPath, CarlosPath, PreviewHost string ClaudePath, CodexCodeModeHostPath string Report func(string) Origin, StateDir, AmadanCredentialPath, AccountBindingsPath, CodexPath string GoRoot, GoBinary, WorkspaceRoot, ScratchRoot, GitPath, AmadanPath, Executable string InstanceID string // PiRoot is where the pinned Pi is installed (EQUIPO_PI_ROOT, laid // out as provider/pi.Paths says); empty, this runner has no Pi and // runs no step on a metered connection. PiRoot string // OpenCodeRoot is where the pinned OpenCode is installed // (EQUIPO_OPENCODE_ROOT, laid out as opencode.Paths says; equipo-runner // install-opencode makes it). Empty: this runner runs no OpenCode Go // step. OpenCodeRoot string Copy ToolCopy // WorkerOnly: a runner that can only be a worker (a Mac, or the test // build's fake Mac). It refuses an instance without workers. WorkerOnly bool // EnrolNotify hears how enrolment is going (EnrolNotice); nil is // silent. EnrolNotify func(EnrolNotice) } func LoadConfig(getenv func(string) string) (Config, error) { c := Config{WorkerOnly: runtime.GOOS == "darwin" || getenv("EQUIPO_FAKE_PLATFORM") != "", ClaudeModel: getenv("EQUIPO_CLAUDE_MODEL"), CodexModel: getenv("EQUIPO_CODEX_MODEL"), MachineLabel: getenv("EQUIPO_MACHINE_LABEL"), ToolProxyPath: getenv("EQUIPO_TOOL_PROXY_PATH"), CodexCodeModeHostPath: getenv("EQUIPO_CODEX_CODE_MODE_HOST_PATH"), BwrapPath: getenv("EQUIPO_BWRAP_PATH"), ModuleCache: getenv("EQUIPO_GO_MODULE_CACHE"), PreviewCredentialPath: getenv("EQUIPO_PREVIEW_CREDENTIAL_PATH"), CarlosPath: getenv("EQUIPO_CARLOS_PATH"), PreviewHost: getenv("EQUIPO_PREVIEW_HOST"), ClaudePath: getenv("EQUIPO_CLAUDE_PATH"), Origin: getenv("EQUIPO_ORIGIN"), StateDir: getenv("EQUIPO_RUNNER_STATE_DIR"), AmadanCredentialPath: getenv("EQUIPO_AMADAN_CREDENTIAL_PATH"), AccountBindingsPath: getenv("EQUIPO_ACCOUNT_BINDINGS_PATH"), CodexPath: getenv("EQUIPO_CODEX_PATH"), GoRoot: getenv("EQUIPO_GO_ROOT"), GoBinary: getenv("EQUIPO_GO_BINARY"), WorkspaceRoot: getenv("EQUIPO_WORKSPACE_ROOT"), ScratchRoot: getenv("EQUIPO_SCRATCH_ROOT"), GitPath: getenv("EQUIPO_GIT_PATH"), AmadanPath: getenv("EQUIPO_AMADAN_PATH"), InstanceID: getenv("EQUIPO_INSTANCE_ID"), PiRoot: getenv("EQUIPO_PI_ROOT"), OpenCodeRoot: getenv("EQUIPO_OPENCODE_ROOT")} if c.PreviewHost == "" { c.PreviewHost = "devbox" } var err error c.Executable, err = os.Executable() if err != nil { return c, err } return c, c.validate() } func validOrigin(origin string) bool { u, err := url.Parse(origin) return err == nil && u.Host != "" && u.User == nil && u.Path == "" && u.RawQuery == "" && !u.ForceQuery && u.Fragment == "" && !strings.Contains(origin, "#") && (u.Scheme == "https" || (u.Scheme == "http" && u.Hostname() == "127.0.0.1")) } func (c Config) validate() error { return c.validateFor(runtime.GOOS) } // validateFor checks the configuration a runner on goos needs. A Mac // runs checks only (a worker): no bubblewrap, model harness, forge // credential or Go toolchain, so it needs its roots, git and itself. func (c Config) validateFor(goos string) error { if goos == "darwin" { return c.validateWorker() } if !validOrigin(c.Origin) { return configIssue("runner.config.origin", "EQUIPO_ORIGIN", provider.ErrContract) } for _, v := range []struct{ name, value string }{{"EQUIPO_INSTANCE_ID", c.InstanceID}} { if v.value == "" { return configIssue("runner.config.missing", v.name, provider.ErrContract) } } for _, v := range []struct{ name, path string }{ {"EQUIPO_BWRAP_PATH", c.BwrapPath}, {"EQUIPO_CLAUDE_PATH", c.ClaudePath}, {"EQUIPO_CODEX_CODE_MODE_HOST_PATH", c.CodexCodeModeHostPath}, {"EQUIPO_RUNNER_STATE_DIR", c.StateDir}, {"EQUIPO_AMADAN_CREDENTIAL_PATH", c.AmadanCredentialPath}, {"EQUIPO_ACCOUNT_BINDINGS_PATH", c.AccountBindingsPath}, {"EQUIPO_CODEX_PATH", c.CodexPath}, {"EQUIPO_GO_ROOT", c.GoRoot}, {"EQUIPO_GO_BINARY", c.GoBinary}, {"EQUIPO_WORKSPACE_ROOT", c.WorkspaceRoot}, {"EQUIPO_SCRATCH_ROOT", c.ScratchRoot}, {"EQUIPO_GIT_PATH", c.GitPath}, {"EQUIPO_AMADAN_PATH", c.AmadanPath}, {"EQUIPO_RUNNER_EXECUTABLE", c.Executable}, } { if !filepath.IsAbs(v.path) || filepath.Clean(v.path) != v.path || v.path == "/" { return configIssue("runner.config.path", v.name, provider.ErrContract) } } if c.PiRoot != "" && (!filepath.IsAbs(c.PiRoot) || filepath.Clean(c.PiRoot) != c.PiRoot || c.PiRoot == "/") { return configIssue("runner.config.path", "EQUIPO_PI_ROOT", provider.ErrContract) } if c.OpenCodeRoot != "" && (!filepath.IsAbs(c.OpenCodeRoot) || filepath.Clean(c.OpenCodeRoot) != c.OpenCodeRoot || c.OpenCodeRoot == "/") { return configIssue("runner.config.path", "EQUIPO_OPENCODE_ROOT", provider.ErrContract) } info, err := os.Stat(c.AmadanPath) if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 { return configIssue("runner.config.path", "EQUIPO_AMADAN_PATH", provider.ErrContract) } for _, v := range []struct{ name, path string }{{"EQUIPO_WORKSPACE_ROOT", c.WorkspaceRoot}, {"EQUIPO_SCRATCH_ROOT", c.ScratchRoot}} { if containsPath(c.StateDir, v.path) || containsPath(v.path, c.StateDir) { return configIssue("runner.config.overlap", v.name, provider.ErrPolicy) } } return nil } func (c Config) validateWorker() error { if !validOrigin(c.Origin) { return configIssue("runner.config.origin", "EQUIPO_ORIGIN", provider.ErrContract) } if c.InstanceID == "" { return configIssue("runner.config.missing", "EQUIPO_INSTANCE_ID", provider.ErrContract) } for _, v := range []struct{ name, path string }{{"EQUIPO_RUNNER_STATE_DIR", c.StateDir}, {"EQUIPO_WORKSPACE_ROOT", c.WorkspaceRoot}, {"EQUIPO_SCRATCH_ROOT", c.ScratchRoot}, {"EQUIPO_GIT_PATH", c.GitPath}, {"EQUIPO_RUNNER_EXECUTABLE", c.Executable}} { if !filepath.IsAbs(v.path) || filepath.Clean(v.path) != v.path || v.path == "/" { return configIssue("runner.config.path", v.name, provider.ErrContract) } } for _, v := range []struct{ name, path string }{{"EQUIPO_WORKSPACE_ROOT", c.WorkspaceRoot}, {"EQUIPO_SCRATCH_ROOT", c.ScratchRoot}} { if containsPath(c.StateDir, v.path) || containsPath(v.path, c.StateDir) { return configIssue("runner.config.overlap", v.name, provider.ErrPolicy) } } return nil } func containsPath(root, path string) bool { r, err := filepath.Rel(root, path) return err == nil && (r == "." || r != ".." && !strings.HasPrefix(r, "../")) } func lockState(dir string) (*os.File, error) { if !filepath.IsAbs(dir) || filepath.Clean(dir) != dir { return nil, provider.ErrPolicy } if err := os.MkdirAll(dir, 0700); err != nil { return nil, err } real, err := filepath.EvalSymlinks(dir) if err != nil || real != dir { return nil, provider.ErrPolicy } info, err := os.Stat(dir) if err != nil || info.Mode().Perm() != 0700 { return nil, provider.ErrPolicy } fd, err := unix.Open(filepath.Join(dir, "runner.lock"), unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0600) if err != nil { return nil, err } f := os.NewFile(uintptr(fd), "runner.lock") if err = unix.Flock(fd, unix.LOCK_EX|unix.LOCK_NB); err != nil { f.Close() return nil, provider.ErrUncertain } return f, nil } func privateJSON(path string, out any) error { info, err := os.Lstat(path) if err != nil { return err } if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() > 1<<20 { return provider.ErrPolicy } f, err := os.Open(path) if err != nil { return err } defer f.Close() return runnerproto.DecodeLimit(f, out, 1<<20) } func readCredentials(path string) (runnerproto.CredentialRotation, error) { var value runnerproto.CredentialRotation err := privateJSON(path, &value) if err == nil && value.Bearer.Token == "" { err = provider.ErrIdentity } return value, err } // DirectBinding supplies configured provenance inputs. Only OpenAccount can // validate them and grant DedicatedHome through the returned lease Binding. func (c Config) DirectBinding(b AccountBinding) (provider.Binding, error) { if !filepath.IsAbs(c.ScratchRoot) || filepath.Clean(c.ScratchRoot) != c.ScratchRoot || c.ScratchRoot == "/" || containsPath(b.ProviderHome, c.ScratchRoot) || containsPath(c.ScratchRoot, b.ProviderHome) { return provider.Binding{}, provider.ErrPolicy } return provider.Binding{ID: b.ID, Home: b.ProviderHome, AccountID: b.AccountID, Provider: b.Provider, Token: b.Kind == "token", Expected: b.ExpectedIdentity, RuntimeRoot: filepath.Join(c.ScratchRoot, "homes")}, nil } func (c Config) bwrap() string { if c.BwrapPath != "" { return c.BwrapPath } return "/usr/bin/bwrap" } func (c Config) moduleCache() string { if c.ModuleCache != "" { return c.ModuleCache } home, _ := os.UserHomeDir() return filepath.Join(home, "go/pkg/mod") } func (c Config) toolProxy() string { if c.ToolProxyPath != "" { return c.ToolProxyPath } return filepath.Join(filepath.Dir(c.Executable), "equipo-tool-proxy") }