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.
217 lines
5.7 KiB
Go
217 lines
5.7 KiB
Go
// Package draft owns retained, private Markdown drafts shared by both interfaces.
|
||
package draft
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"os"
|
||
"path/filepath"
|
||
"sort"
|
||
"strings"
|
||
)
|
||
|
||
type Draft struct {
|
||
Path string `json:"path"`
|
||
Name string `json:"name"`
|
||
Server string `json:"server,omitempty"`
|
||
CreateOnly bool `json:"create_only"`
|
||
BaseVersion int `json:"base_version,omitempty"`
|
||
}
|
||
|
||
// ForServer projects publication scope onto a destination without changing the
|
||
// retained draft. Version numbers belong to the origin, not another server.
|
||
func (d Draft) ForServer(server string) Draft {
|
||
if d.Server != "" && d.Server != server {
|
||
d.CreateOnly = true
|
||
d.BaseVersion = 0
|
||
}
|
||
d.Server = server
|
||
return d
|
||
}
|
||
|
||
// Resolve projects this retained draft onto the configured server and reports a
|
||
// user-visible notice when the draft was created against a different server.
|
||
// The projection through ForServer keeps cross-server publication create-only,
|
||
// so the original version never leaks; the notice names both URLs so neither the
|
||
// CLI nor the TUI silently retargets a foreign draft. The returned notice is
|
||
// empty when the draft already matches (or records no server).
|
||
func (d Draft) Resolve(configured string) (Draft, string) {
|
||
resolved := d.ForServer(configured)
|
||
if d.Server == "" || d.Server == configured {
|
||
return resolved, ""
|
||
}
|
||
return resolved, fmt.Sprintf(
|
||
"draft was created against %s but this client is configured for %s; publishing to %s as a new pipeline (version numbers do not carry over)",
|
||
d.Server, configured, configured)
|
||
}
|
||
|
||
func Directory() (string, error) {
|
||
if dir := os.Getenv("PIPELINES_DRAFT_DIR"); dir != "" {
|
||
return filepath.Abs(dir)
|
||
}
|
||
cache, err := os.UserCacheDir()
|
||
return filepath.Join(cache, "pipelines", "drafts"), err
|
||
}
|
||
|
||
func Create(name, source, server string, fresh bool, base int) (Draft, error) {
|
||
dir, err := Directory()
|
||
if err != nil {
|
||
return Draft{}, err
|
||
}
|
||
if err = os.MkdirAll(dir, 0700); err != nil {
|
||
return Draft{}, err
|
||
}
|
||
f, err := os.CreateTemp(dir, "pipeline-*.md")
|
||
if err != nil {
|
||
return Draft{}, err
|
||
}
|
||
path := f.Name()
|
||
_, writeErr := f.WriteString(source)
|
||
closeErr := f.Close()
|
||
if writeErr != nil {
|
||
return Draft{}, writeErr
|
||
}
|
||
if closeErr != nil {
|
||
return Draft{}, closeErr
|
||
}
|
||
d := Draft{Path: path, Name: name, Server: server, CreateOnly: fresh, BaseVersion: base}
|
||
data, _ := json.MarshalIndent(d, "", " ")
|
||
err = os.WriteFile(path+".json", data, 0600)
|
||
return d, err
|
||
}
|
||
|
||
func Read(path string) (string, error) {
|
||
f, err := os.Open(path)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
defer f.Close()
|
||
info, err := f.Stat()
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
if !info.Mode().IsRegular() || info.Size() > 1<<20 {
|
||
return "", fmt.Errorf("draft must be a regular file no larger than 1 MiB")
|
||
}
|
||
// Bound the read even if an editor grows the file after Stat.
|
||
data, err := io.ReadAll(io.LimitReader(f, (1<<20)+1))
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
if len(data) > 1<<20 {
|
||
return "", fmt.Errorf("draft exceeds 1 MiB")
|
||
}
|
||
return string(data), nil
|
||
}
|
||
|
||
func Load(path string) (Draft, error) {
|
||
absolute, err := filepath.Abs(path)
|
||
if err != nil {
|
||
return Draft{}, err
|
||
}
|
||
data, err := os.ReadFile(absolute + ".json")
|
||
if err != nil {
|
||
return Draft{}, err
|
||
}
|
||
var d Draft
|
||
if err = json.Unmarshal(data, &d); err != nil {
|
||
return d, err
|
||
}
|
||
d.Path = absolute // Never trust a metadata file to redirect submission to another source.
|
||
return d, nil
|
||
}
|
||
|
||
func List() ([]Draft, error) {
|
||
dir, err := Directory()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
paths, err := filepath.Glob(filepath.Join(dir, "pipeline-*.md.json"))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
sort.Strings(paths)
|
||
items := []Draft{}
|
||
for _, path := range paths {
|
||
d, err := Load(path[:len(path)-5])
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
items = append(items, d)
|
||
}
|
||
return items, nil
|
||
}
|
||
|
||
// SetName updates only retained metadata, preserving source and publication scope.
|
||
func SetName(path, name string) error {
|
||
name = strings.TrimSpace(name)
|
||
if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") {
|
||
return fmt.Errorf("pipeline name must be 1–200 bytes without line breaks or NUL")
|
||
}
|
||
d, err := Load(path)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
metadata := d.Path + ".json"
|
||
info, err := os.Lstat(metadata)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !info.Mode().IsRegular() {
|
||
return fmt.Errorf("draft metadata must be a regular file, not a symlink")
|
||
}
|
||
d.Name = name
|
||
data, err := json.MarshalIndent(d, "", " ")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
f, err := os.CreateTemp(filepath.Dir(metadata), ".draft-name-*")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer os.Remove(f.Name())
|
||
_, writeErr := f.Write(data)
|
||
closeErr := f.Close()
|
||
if writeErr != nil {
|
||
return writeErr
|
||
}
|
||
if closeErr != nil {
|
||
return closeErr
|
||
}
|
||
return os.Rename(f.Name(), metadata)
|
||
}
|
||
|
||
// Delete only a selected, managed cache draft and its sidecar. Never recurse,
|
||
// follow a file symlink, or delete an arbitrary external submission source.
|
||
func Delete(path string) error {
|
||
dir, err := Directory()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
absolute, err := filepath.Abs(path)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
name := filepath.Base(absolute)
|
||
if filepath.Dir(absolute) != filepath.Clean(dir) || !strings.HasPrefix(name, "pipeline-") || !strings.HasSuffix(name, ".md") {
|
||
return fmt.Errorf("only managed drafts inside %s can be deleted", dir)
|
||
}
|
||
for _, file := range []string{absolute + ".json", absolute} {
|
||
info, err := os.Lstat(file)
|
||
if os.IsNotExist(err) && file == absolute {
|
||
continue
|
||
}
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !info.Mode().IsRegular() {
|
||
return fmt.Errorf("draft and metadata must be regular files, not symlinks or directories")
|
||
}
|
||
}
|
||
if err := os.Remove(absolute); err != nil && !os.IsNotExist(err) {
|
||
return err
|
||
}
|
||
return os.Remove(absolute + ".json")
|
||
}
|