// Package harness owns configured interactive/stage harness preferences, not credentials. package harness import ( "encoding/json" "fmt" "os" "path/filepath" "sort" "strings" ) type Profile struct { DockerDirectory string `json:"docker_directory,omitempty"` Executable string `json:"executable"` Model string `json:"model"` PipelineModel string `json:"pipeline_model"` } type Config struct { Default string `json:"default"` Profiles map[string]Profile `json:"profiles"` } func Path() (string, error) { if path := os.Getenv("PIPELINES_HARNESS_CONFIG"); path != "" { return filepath.Abs(path) } root := os.Getenv("XDG_CONFIG_HOME") if root == "" { home, err := os.UserHomeDir() if err != nil { return "", err } root = filepath.Join(home, ".config") } return filepath.Join(root, "pipelines", "harnesses.json"), nil } // Mode returns the profile's launch mode. A profile with a recorded Docker // directory launches through that Compose installation; every other profile // launches the host executable. It is the single mode label shared by setup's // summary and any caller that reports where a harness runs. func (p Profile) Mode() string { if p.DockerDirectory == "" { return "host" } return "Docker-linked (" + p.DockerDirectory + ")" } // VerifyDockerLink re-checks that a recorded Docker installation still owns a // compose.yaml, the same requirement `harness link` enforces when first // linking. It is the single re-verification used before a Docker-linked profile // is reconfigured or reported as usable, so a stale link is never silently kept. func VerifyDockerLink(directory string) error { if directory == "" { return fmt.Errorf("no Docker installation directory is recorded; run pipelines harness setup --host for a native harness") } if _, err := os.Stat(filepath.Join(directory, "compose.yaml")); err != nil { return fmt.Errorf("recorded Docker installation %s is no longer usable: %w; relink with pipelines harness link --docker-directory , or switch to a host harness with pipelines harness setup --host", directory, err) } return nil } func ValidModel(value string) bool { provider, model, ok := strings.Cut(value, "/") if !ok || provider == "" || model == "" || len(value) > 501 { return false } for _, r := range value { if !strings.ContainsRune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_./:-", r) { return false } } return true } func Load() (Config, error) { c := Config{Profiles: map[string]Profile{}} path, err := Path() if err != nil { return c, err } info, err := os.Lstat(path) if os.IsNotExist(err) { return c, nil } if err != nil { return c, err } if !info.Mode().IsRegular() { return c, fmt.Errorf("refusing non-regular harness settings %s", path) } data, err := os.ReadFile(path) if err != nil { return c, err } if err = json.Unmarshal(data, &c); err != nil { return c, fmt.Errorf("invalid harness settings: %w", err) } if c.Profiles == nil { c.Profiles = map[string]Profile{} } for name, p := range c.Profiles { if (name != "opencode" && name != "pi") || p.Executable == "" || !ValidModel(p.Model) || !ValidModel(p.PipelineModel) { return c, fmt.Errorf("invalid harness profile %q", name) } } if c.Default != "" { if _, ok := c.Profiles[c.Default]; !ok { return c, fmt.Errorf("default harness %q is not configured", c.Default) } } return c, nil } func Save(c Config) error { path, err := Path() if err != nil { return err } // Preserve unrelated/future settings in this file as well as other harness profiles. fields := map[string]json.RawMessage{} if info, err := os.Lstat(path); err == nil { if !info.Mode().IsRegular() { return fmt.Errorf("refusing non-regular harness settings") } data, err := os.ReadFile(path) if err != nil { return err } if err = json.Unmarshal(data, &fields); err != nil { return err } } else if !os.IsNotExist(err) { return err } fields["default"], _ = json.Marshal(c.Default) fields["profiles"], _ = json.Marshal(c.Profiles) data, err := json.MarshalIndent(fields, "", " ") if err != nil { return err } if err = os.MkdirAll(filepath.Dir(path), 0700); err != nil { return err } file, err := os.CreateTemp(filepath.Dir(path), ".harnesses-*") if err != nil { return err } defer os.Remove(file.Name()) if _, err = file.Write(data); err != nil { file.Close() return err } if err = file.Close(); err != nil { return err } return os.Rename(file.Name(), path) } // ListView is the JSON shape `harness list` emits. It mirrors Config and adds a // computed per-profile Mode so callers and scripts can read the launch mode // without inferring it from docker_directory. The embedded Profile keeps every // stored field (executable/model/pipeline_model/docker_directory) at the same // level, so existing consumers such as setup-docker.py's link_host are unaffected. type ListView struct { Default string `json:"default"` Profiles map[string]ProfileView `json:"profiles"` } type ProfileView struct { Profile Mode string `json:"mode"` } // List returns the configured profiles with an explicit launch mode. func List() (ListView, error) { c, err := Load() if err != nil { return ListView{}, err } view := ListView{Default: c.Default, Profiles: map[string]ProfileView{}} for name, p := range c.Profiles { view.Profiles[name] = ProfileView{Profile: p, Mode: p.Mode()} } return view, nil } func Choices() ([]string, string, error) { c, err := Load() if err != nil { return nil, "", err } var choices []string for name := range c.Profiles { choices = append(choices, name) } sort.Strings(choices) if c.Default != "" { for i, name := range choices { if name == c.Default { choices = append([]string{name}, append(choices[:i], choices[i+1:]...)...) break } } } return choices, c.Default, nil }