05ae530442
Install current verified harness releases inside Docker, preserve persistent accounts and settings, and provide separate interactive and pipeline model defaults with terminal onboarding and fuzzy pickers. Add Docker-aware authoring transport, Pi stage execution, bounded workflow controls, reliable final-result readback and usage settlement, and finite short-lived CLI pipe draining. Keep dated installation evidence and packaged authoring guidance consistent. Verified with a real Docker installation, both harness stages and interactive authors, the built-in plan/implement/review starter, independent host race/vet checks, installer tests, and packaged skill tests. Generated release receipts stay ignored in clean checkouts.
131 lines
3.7 KiB
Go
131 lines
3.7 KiB
Go
package author
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
var skillFiles = []string{"SKILL.md", "references/format.md", "references/notifications.md", "references/operations.md", "references/transfer.md", "references/verification.md", "references/control.md", "references/harnesses.md"}
|
|
|
|
func SkillDirectory() (string, error) {
|
|
dir := os.Getenv("XDG_CONFIG_HOME")
|
|
if dir == "" {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
dir = filepath.Join(home, ".config")
|
|
}
|
|
return filepath.Abs(filepath.Join(dir, "opencode", "skills", "pipelines"))
|
|
}
|
|
|
|
func digest(data []byte) string { sum := sha256.Sum256(data); return hex.EncodeToString(sum[:]) }
|
|
|
|
// InstallSkill only updates files previously installed by Pipelines and unchanged
|
|
// by the user. It never edits OpenCode config, login, or unrelated skills.
|
|
func InstallSkill() (string, error) {
|
|
dir, err := SkillDirectory()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
marker := filepath.Join(dir, ".pipelines-managed.json")
|
|
if info, err := os.Lstat(marker); err == nil && !info.Mode().IsRegular() {
|
|
return "", fmt.Errorf("refusing non-regular skill ownership marker %s", marker)
|
|
} else if err != nil && !os.IsNotExist(err) {
|
|
return "", err
|
|
}
|
|
previous := map[string]string{}
|
|
if data, readErr := os.ReadFile(marker); readErr == nil {
|
|
if err = json.Unmarshal(data, &previous); err != nil {
|
|
return "", fmt.Errorf("invalid skill ownership marker: %w", err)
|
|
}
|
|
} else if !os.IsNotExist(readErr) {
|
|
return "", readErr
|
|
}
|
|
files := map[string][]byte{}
|
|
next := map[string]string{}
|
|
for _, file := range skillFiles {
|
|
data, err := content.ReadFile("skill/" + file)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
files[file] = data
|
|
next[file] = digest(data)
|
|
path := filepath.Join(dir, file)
|
|
if info, statErr := os.Lstat(path); statErr == nil {
|
|
if !info.Mode().IsRegular() {
|
|
return "", fmt.Errorf("refusing to replace non-regular skill file %s", path)
|
|
}
|
|
existing, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if previous[file] == "" || (digest(existing) != previous[file] && digest(existing) != next[file]) {
|
|
return "", fmt.Errorf("skill file %s is user-managed or modified; preserve/rename it before installing the Pipelines skill", path)
|
|
}
|
|
} else if !os.IsNotExist(statErr) {
|
|
return "", statErr
|
|
}
|
|
}
|
|
// Record ownership before first creation so an interrupted installation can
|
|
// safely resume missing files. Updates accept either old or bundled hashes.
|
|
if len(previous) == 0 {
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
return "", err
|
|
}
|
|
data, _ := json.MarshalIndent(next, "", " ")
|
|
if err := writeSkillFile(marker, data); err != nil {
|
|
return "", err
|
|
}
|
|
previous = next
|
|
}
|
|
for _, file := range skillFiles {
|
|
path := filepath.Join(dir, file)
|
|
if previous[file] == next[file] {
|
|
if _, err := os.Stat(path); err == nil {
|
|
continue
|
|
}
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
return "", err
|
|
}
|
|
if err := writeSkillFile(path, files[file]); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
if len(previous) == len(next) {
|
|
equal := true
|
|
for file, hash := range next {
|
|
if previous[file] != hash {
|
|
equal = false
|
|
}
|
|
}
|
|
if equal {
|
|
return dir, nil
|
|
}
|
|
}
|
|
data, _ := json.MarshalIndent(next, "", " ")
|
|
return dir, writeSkillFile(marker, data)
|
|
}
|
|
|
|
func writeSkillFile(path string, data []byte) error {
|
|
f, err := os.CreateTemp(filepath.Dir(path), ".pipelines-skill-*")
|
|
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(), path)
|
|
}
|