914345cb31
Native installs left Docker-era harness profiles in harnesses.json, so every authoring launch failed trying to reach a Docker service, and a retained draft bound to the old Docker server was silently projected onto the configured one. - harness: add Profile.Mode and VerifyDockerLink as the single mode/verification helpers. `harness setup --host` configures or converts a host profile and clears docker_directory; reconfiguring a Docker-linked profile re-verifies the recorded directory still owns compose.yaml and fails naming it instead of saving a link that cannot launch. Setup's summary and `harness list` (via the new List/ListView shape) report the resulting mode and, for Docker, the directory, without changing the stored Config schema. - setup-native.py: after build/install-skill it writes host (no docker_directory) profiles for the detected harnesses, defaulting to the detected OpenCode profile rather than a stale pi default, taking models from --model or the existing valid profile and never inventing one; it clears a stale Docker link and preserves unrelated keys/profiles. Detection-only: no harness install, upgrade or download. - draft: add Draft.Resolve, the shared mismatch helper, which projects a draft onto the configured server and returns a dual-URL notice. CLI validate/submit and the TUI draft browser use it so a foreign draft is never silently retargeted; the TUI pre-wraps the notice to the panel width. - Tests cover the Docker->host and host->host transitions, the missing-directory failure, installer write/migration (Python), and the CLI/TUI notice, using the real config file path in a temp dir. - Update help text, README, docs and the packaged authoring skill reference to describe host as the default and the Docker-linked case.
210 lines
5.8 KiB
Go
210 lines
5.8 KiB
Go
// 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 <opencode|pi> --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 <opencode|pi> --docker-directory <checkout>, or switch to a host harness with pipelines harness setup <opencode|pi> --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
|
|
}
|