c42a19f7d3
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
187 lines
5.0 KiB
Go
187 lines
5.0 KiB
Go
// Package transfer handles portable pipeline definitions, never run history or credentials.
|
||
package transfer
|
||
|
||
import (
|
||
"archive/zip"
|
||
"bytes"
|
||
"crypto/sha256"
|
||
"encoding/hex"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
const maxSource = 1 << 20
|
||
const maxBundle = 32 << 20
|
||
const maxPipelines = 1000
|
||
|
||
type Entry struct {
|
||
Name string `json:"name"`
|
||
Version int `json:"version"`
|
||
File string `json:"file"`
|
||
SHA256 string `json:"sha256"`
|
||
Source string `json:"-"`
|
||
}
|
||
type Manifest struct {
|
||
Format string `json:"format"`
|
||
Version int `json:"version"`
|
||
CreatedAt string `json:"created_at"`
|
||
Pipelines []Entry `json:"pipelines"`
|
||
}
|
||
|
||
func digest(source string) string {
|
||
sum := sha256.Sum256([]byte(source))
|
||
return hex.EncodeToString(sum[:])
|
||
}
|
||
func validName(name string) bool {
|
||
return strings.TrimSpace(name) != "" && name == strings.TrimSpace(name) && len(name) <= 200 && !strings.ContainsAny(name, "\r\n\x00")
|
||
}
|
||
|
||
func Write(filename string, entries []Entry) error {
|
||
if len(entries) == 0 || len(entries) > maxPipelines {
|
||
return fmt.Errorf("select 1–%d pipelines", maxPipelines)
|
||
}
|
||
manifest := Manifest{Format: "pipelines-definitions", Version: 1, CreatedAt: time.Now().UTC().Format(time.RFC3339), Pipelines: append([]Entry(nil), entries...)}
|
||
var data bytes.Buffer
|
||
writer := zip.NewWriter(&data)
|
||
total := 0
|
||
names := map[string]bool{}
|
||
for i := range manifest.Pipelines {
|
||
e := &manifest.Pipelines[i]
|
||
if !validName(e.Name) || names[e.Name] || e.Version < 1 {
|
||
return fmt.Errorf("invalid or duplicate pipeline name/version")
|
||
}
|
||
names[e.Name] = true
|
||
total += len(e.Source)
|
||
if len(e.Source) == 0 || len(e.Source) > maxSource || total > maxBundle {
|
||
return fmt.Errorf("pipeline export exceeds size limits")
|
||
}
|
||
e.File = fmt.Sprintf("pipelines/%04d.md", i+1)
|
||
e.SHA256 = digest(e.Source)
|
||
file, err := writer.Create(e.File)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if _, err = io.WriteString(file, e.Source); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
meta, err := json.MarshalIndent(manifest, "", " ")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
file, err := writer.Create("manifest.json")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if _, err = file.Write(meta); err != nil {
|
||
return err
|
||
}
|
||
if err = writer.Close(); err != nil {
|
||
return err
|
||
}
|
||
// Publish only a finished archive, without replacing any existing destination.
|
||
temp, err := os.CreateTemp(filepath.Dir(filename), ".pipelines-export-*.zip")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
name := temp.Name()
|
||
defer os.Remove(name)
|
||
if _, err = temp.Write(data.Bytes()); err != nil {
|
||
temp.Close()
|
||
return err
|
||
}
|
||
if err = temp.Sync(); err != nil {
|
||
temp.Close()
|
||
return err
|
||
}
|
||
if err = temp.Close(); err != nil {
|
||
return err
|
||
}
|
||
if err = os.Link(name, filename); err != nil {
|
||
return fmt.Errorf("cannot save export (existing files are never overwritten): %w", err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func Read(filename string) ([]Entry, error) {
|
||
info, err := os.Stat(filename)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if !info.Mode().IsRegular() || info.Size() > 2*maxBundle {
|
||
return nil, fmt.Errorf("bundle is not a regular file or exceeds size limit")
|
||
}
|
||
reader, err := zip.OpenReader(filename)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer reader.Close()
|
||
if len(reader.File) < 2 || len(reader.File) > maxPipelines+1 {
|
||
return nil, fmt.Errorf("invalid bundle entry count")
|
||
}
|
||
files := map[string]*zip.File{}
|
||
for _, f := range reader.File {
|
||
if files[f.Name] != nil {
|
||
return nil, fmt.Errorf("duplicate archive entry")
|
||
}
|
||
files[f.Name] = f
|
||
}
|
||
read := func(name string, limit int) ([]byte, error) {
|
||
f := files[name]
|
||
if f == nil || f.UncompressedSize64 > uint64(limit) {
|
||
return nil, fmt.Errorf("missing or oversized bundle entry %q", name)
|
||
}
|
||
r, err := f.Open()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer r.Close()
|
||
data, err := io.ReadAll(io.LimitReader(r, int64(limit+1)))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(data) > limit {
|
||
return nil, fmt.Errorf("bundle entry exceeds limit")
|
||
}
|
||
return data, nil
|
||
}
|
||
data, err := read("manifest.json", maxSource)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var m Manifest
|
||
if err = json.Unmarshal(data, &m); err != nil {
|
||
return nil, err
|
||
}
|
||
if m.Format != "pipelines-definitions" || m.Version != 1 || len(m.Pipelines) == 0 || len(m.Pipelines) > maxPipelines || len(files) != len(m.Pipelines)+1 {
|
||
return nil, fmt.Errorf("unsupported or malformed pipeline bundle")
|
||
}
|
||
total := 0
|
||
names := map[string]bool{}
|
||
for i := range m.Pipelines {
|
||
e := &m.Pipelines[i]
|
||
if !validName(e.Name) || names[e.Name] || e.Version < 1 || e.File != fmt.Sprintf("pipelines/%04d.md", i+1) {
|
||
return nil, fmt.Errorf("invalid pipeline manifest entry")
|
||
}
|
||
names[e.Name] = true
|
||
source, err := read(e.File, maxSource)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
total += len(source)
|
||
if total > maxBundle {
|
||
return nil, fmt.Errorf("bundle exceeds size limit")
|
||
}
|
||
e.Source = string(source)
|
||
if len(source) == 0 || digest(e.Source) != e.SHA256 {
|
||
return nil, fmt.Errorf("checksum mismatch for %q", e.Name)
|
||
}
|
||
}
|
||
return m.Pipelines, nil
|
||
}
|