Files
pipelines/internal/author/install.go
T
alan 8f3ebf74ad skill: replace the lane model with the work queue
The lane machinery is gone: work is a markdown queue, items run in parallel in
their own worktrees, the committer stage lands its own work, land-check.sh gates
cleanup, branches are never deleted, and pipelines wait (rather than a driver
script) is what wakes a session. references/lanes.md becomes references/queue.md;
the remaining lane references in work/orchestration/debugging/delivery/format and
SKILL.md are rewritten, and the installer now prunes a managed file it no longer
bundles when the copy is unmodified.
2026-10-18 01:12:21 +01:00

146 lines
4.3 KiB
Go

package author
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
)
var skillFiles = []string{"SKILL.md", "references/orchestration.md", "references/work.md", "references/queue.md", "references/debugging.md", "references/delivery.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
}
}
// A file the previous install managed but this version no longer bundles - a
// reference renamed or withdrawn - is removed only when it is unmodified. A copy
// the user edited is left alone, exactly as an edit to a live file is.
for file, hash := range previous {
if _, still := next[file]; still {
continue
}
existing, readErr := os.ReadFile(filepath.Join(dir, file))
if readErr != nil {
continue
}
if digest(existing) == hash {
_ = os.Remove(filepath.Join(dir, file))
}
}
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)
}