// 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 } 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) } 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 }