Validate and import agent skills into versioned server storage
Adds a SKILL.md bundle validator, immutable per-skill versions with content digests and deduplication, protocol requests to import, list and show skills, and the pipelines skills import/list/show commands. Imports are serialised so concurrent imports never share a version number.
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/skills"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Store) initSkills() error {
|
||||
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS skills (name TEXT PRIMARY KEY, description TEXT NOT NULL, latest_version INTEGER NOT NULL, digest TEXT NOT NULL, imported_at TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS skill_versions (name TEXT NOT NULL REFERENCES skills(name), version INTEGER NOT NULL, digest TEXT NOT NULL, source_path TEXT NOT NULL, imported_at TEXT NOT NULL, PRIMARY KEY(name,version), UNIQUE(name,digest));
|
||||
CREATE TABLE IF NOT EXISTS skill_files (name TEXT NOT NULL, version INTEGER NOT NULL, path TEXT NOT NULL, size INTEGER NOT NULL, PRIMARY KEY(name,version,path), FOREIGN KEY(name,version) REFERENCES skill_versions(name,version));`)
|
||||
return err
|
||||
}
|
||||
|
||||
// skillImports serialises imports in this server process, so the
|
||||
// deduplication check and the next-version allocation below see every
|
||||
// earlier import's committed version and never hand out the same number.
|
||||
var skillImports sync.Mutex
|
||||
|
||||
func (s *Store) ImportSkill(ctx context.Context, name, description, digest, source string, files []protocol.SkillFile) (protocol.SkillVersion, error) {
|
||||
skillImports.Lock()
|
||||
defer skillImports.Unlock()
|
||||
if err := skills.ValidateName(name); err != nil {
|
||||
return protocol.SkillVersion{}, err
|
||||
}
|
||||
if digest == "" || len(files) == 0 {
|
||||
return protocol.SkillVersion{}, fmt.Errorf("invalid skill bundle")
|
||||
}
|
||||
if len(files) > skills.MaxFiles {
|
||||
return protocol.SkillVersion{}, fmt.Errorf("skill has more than %d files", skills.MaxFiles)
|
||||
}
|
||||
ordered := append([]protocol.SkillFile(nil), files...)
|
||||
sort.Slice(ordered, func(i, j int) bool { return ordered[i].Path < ordered[j].Path })
|
||||
h := sha256.New()
|
||||
total := 0
|
||||
for _, f := range ordered {
|
||||
if f.Path == "" || strings.HasPrefix(f.Path, "/") || filepath.Clean(f.Path) != f.Path || strings.HasPrefix(filepath.ToSlash(f.Path), "../") || strings.Contains(f.Path, "\\") {
|
||||
return protocol.SkillVersion{}, fmt.Errorf("invalid skill file path: %s", f.Path)
|
||||
}
|
||||
if len(f.Bytes) > skills.MaxFileBytes {
|
||||
return protocol.SkillVersion{}, fmt.Errorf("file %s exceeds %d bytes", f.Path, skills.MaxFileBytes)
|
||||
}
|
||||
total += len(f.Bytes)
|
||||
fmt.Fprintf(h, "%s\x00%d\x00", f.Path, len(f.Bytes))
|
||||
h.Write(f.Bytes)
|
||||
}
|
||||
if total > skills.MaxTotalBytes || hex.EncodeToString(h.Sum(nil)) != digest {
|
||||
return protocol.SkillVersion{}, fmt.Errorf("skill digest or size limit is invalid")
|
||||
}
|
||||
var existing protocol.SkillVersion
|
||||
err := s.db.QueryRowContext(ctx, `SELECT version, source_path, imported_at FROM skill_versions WHERE name=? AND digest=?`, name, digest).Scan(&existing.Version, &existing.SourcePath, &existing.ImportedAt)
|
||||
if err == nil {
|
||||
existing.SkillSummary = protocol.SkillSummary{Name: name, Description: description, LatestVersion: existing.Version, Digest: digest, ImportedAt: existing.ImportedAt}
|
||||
existing.SkillMD, _ = s.skillMD(name, existing.Version)
|
||||
existing.Manifest, _ = s.skillManifest(ctx, name, existing.Version)
|
||||
return existing, nil
|
||||
}
|
||||
if err != sql.ErrNoRows {
|
||||
return existing, err
|
||||
}
|
||||
var next int
|
||||
_ = s.db.QueryRowContext(ctx, `SELECT COALESCE(MAX(version),0)+1 FROM skill_versions WHERE name=?`, name).Scan(&next)
|
||||
now := time.Now().UTC().Format(time.RFC3339)
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return existing, err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO skills(name,description,latest_version,digest,imported_at) VALUES(?,?,?,?,?) ON CONFLICT(name) DO UPDATE SET description=excluded.description,latest_version=excluded.latest_version,digest=excluded.digest,imported_at=excluded.imported_at`, name, description, next, digest, now); err != nil {
|
||||
tx.Rollback()
|
||||
return existing, err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO skill_versions(name,version,digest,source_path,imported_at) VALUES(?,?,?,?,?)`, name, next, digest, source, now); err != nil {
|
||||
tx.Rollback()
|
||||
return existing, err
|
||||
}
|
||||
dir := filepath.Join(s.dir, "skills", name, fmt.Sprintf("v%d", next))
|
||||
if err = os.MkdirAll(dir, 0700); err != nil {
|
||||
tx.Rollback()
|
||||
return existing, err
|
||||
}
|
||||
for _, f := range files {
|
||||
p := filepath.Join(dir, filepath.FromSlash(f.Path))
|
||||
if err = os.MkdirAll(filepath.Dir(p), 0700); err != nil {
|
||||
tx.Rollback()
|
||||
return existing, err
|
||||
}
|
||||
if err = os.WriteFile(p, f.Bytes, 0600); err != nil {
|
||||
tx.Rollback()
|
||||
return existing, err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO skill_files(name,version,path,size) VALUES(?,?,?,?)`, name, next, f.Path, len(f.Bytes)); err != nil {
|
||||
tx.Rollback()
|
||||
return existing, err
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return existing, err
|
||||
}
|
||||
return s.GetSkill(ctx, name, next)
|
||||
}
|
||||
func (s *Store) ListSkills(ctx context.Context) ([]protocol.SkillSummary, error) {
|
||||
rows, e := s.db.QueryContext(ctx, `SELECT name,description,latest_version,digest,imported_at FROM skills ORDER BY name`)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []protocol.SkillSummary
|
||||
for rows.Next() {
|
||||
var x protocol.SkillSummary
|
||||
if e = rows.Scan(&x.Name, &x.Description, &x.LatestVersion, &x.Digest, &x.ImportedAt); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
out = append(out, x)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (s *Store) GetSkill(ctx context.Context, name string, version int) (protocol.SkillVersion, error) {
|
||||
var x protocol.SkillVersion
|
||||
var desc string
|
||||
if version == 0 {
|
||||
if e := s.db.QueryRowContext(ctx, `SELECT description,latest_version FROM skills WHERE name=?`, name).Scan(&desc, &version); e != nil {
|
||||
return x, e
|
||||
}
|
||||
} else if e := s.db.QueryRowContext(ctx, `SELECT description FROM skills WHERE name=?`, name).Scan(&desc); e != nil {
|
||||
return x, e
|
||||
}
|
||||
if e := s.db.QueryRowContext(ctx, `SELECT digest,source_path,imported_at FROM skill_versions WHERE name=? AND version=?`, name, version).Scan(&x.Digest, &x.SourcePath, &x.ImportedAt); e != nil {
|
||||
return x, e
|
||||
}
|
||||
x.Name = name
|
||||
x.Description = desc
|
||||
x.Version = version
|
||||
x.LatestVersion = version
|
||||
x.SkillMD, _ = s.skillMD(name, version)
|
||||
x.Manifest, _ = s.skillManifest(ctx, name, version)
|
||||
return x, nil
|
||||
}
|
||||
func (s *Store) skillMD(name string, v int) (string, error) {
|
||||
b, e := os.ReadFile(filepath.Join(s.dir, "skills", name, fmt.Sprintf("v%d", v), "SKILL.md"))
|
||||
return string(b), e
|
||||
}
|
||||
func (s *Store) skillManifest(ctx context.Context, name string, v int) ([]protocol.SkillFileMeta, error) {
|
||||
rows, e := s.db.QueryContext(ctx, `SELECT path,size FROM skill_files WHERE name=? AND version=? ORDER BY path`, name, v)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []protocol.SkillFileMeta
|
||||
for rows.Next() {
|
||||
var x protocol.SkillFileMeta
|
||||
if e = rows.Scan(&x.Path, &x.Size); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
out = append(out, x)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func digestSkill(files []protocol.SkillFile) string {
|
||||
h := sha256.New()
|
||||
for _, f := range files {
|
||||
fmt.Fprintf(h, "%s\x00%d\x00", f.Path, len(f.Bytes))
|
||||
h.Write(f.Bytes)
|
||||
}
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
func TestSkillImportDeduplicatesAndVersions(t *testing.T) {
|
||||
s, err := Open(filepath.Join(t.TempDir(), "pipelines.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
files := []protocol.SkillFile{{Path: "SKILL.md", Bytes: []byte("---\nname: demo\ndescription: Demo\n---\n")}}
|
||||
first, err := s.ImportSkill(context.Background(), "demo", "Demo", digestSkill(files), "/one", files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
same, err := s.ImportSkill(context.Background(), "demo", "Demo", digestSkill(files), "/two", files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if same.Version != first.Version {
|
||||
t.Fatalf("dedup version = %d, want %d", same.Version, first.Version)
|
||||
}
|
||||
files[0].Bytes = append(files[0].Bytes, '\n')
|
||||
second, err := s.ImportSkill(context.Background(), "demo", "Demo", digestSkill(files), "/three", files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if second.Version != 2 {
|
||||
t.Fatalf("new version = %d, want 2", second.Version)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkillImportRejectsUnsafeName(t *testing.T) {
|
||||
s, err := Open(filepath.Join(t.TempDir(), "pipelines.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
files := []protocol.SkillFile{{Path: "SKILL.md", Bytes: []byte("skill")}}
|
||||
if _, err := s.ImportSkill(context.Background(), "../../outside", "", digestSkill(files), "/source", files); err == nil {
|
||||
t.Fatal("expected unsafe skill name to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkillImportConcurrentVersionsAreDistinct(t *testing.T) {
|
||||
s, err := Open(filepath.Join(t.TempDir(), "pipelines.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
const n = 8
|
||||
var wg sync.WaitGroup
|
||||
versions := make([]int, n)
|
||||
errs := make([]error, n)
|
||||
for i := range n {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
files := []protocol.SkillFile{{Path: "SKILL.md", Bytes: []byte(fmt.Sprintf("---\nname: demo\ndescription: Demo %d\n---\n", i))}}
|
||||
v, err := s.ImportSkill(context.Background(), "demo", "Demo", digestSkill(files), "/src", files)
|
||||
versions[i], errs[i] = v.Version, err
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
seen := map[int]bool{}
|
||||
for i := range n {
|
||||
if errs[i] != nil {
|
||||
t.Fatalf("import %d: %v", i, errs[i])
|
||||
}
|
||||
if seen[versions[i]] {
|
||||
t.Fatalf("version %d allocated twice: %v", versions[i], versions)
|
||||
}
|
||||
seen[versions[i]] = true
|
||||
}
|
||||
for v := 1; v <= n; v++ {
|
||||
if !seen[v] {
|
||||
t.Fatalf("versions = %v, want 1..%d", versions, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,6 +129,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := s.initSkills(); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user