feat: pin agent definitions next to skills
A source can install agents (agents/<name>.md files with a frontmatter name) alongside or instead of skills. - manifest: optional "agents" selection; lock: "agents" map - add --agent / --all-agents, remove --agent, list shows the kind - check/update diff agents like skills (files, not directories) - dashboard lists and removes agents - home-manager module: agentTargets (default .claude/agents) links <name>.md Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0aedef8c8c
commit
df8d11d8cb
15 files changed
+648
-131
No files matched your search
@@ -0,0 +1,227 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"skill-manager/internal/lock"
|
||||
"skill-manager/internal/manifest"
|
||||
)
|
||||
|
||||
// withAgents adds pstack/agents/<name>.md files ("name" or "name:body") to a fixture root.
|
||||
func withAgents(t *testing.T, root string, agents ...string) string {
|
||||
t.Helper()
|
||||
dir := filepath.Join(root, "pstack", "agents")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, a := range agents {
|
||||
name, body, _ := strings.Cut(a, ":")
|
||||
text := "---\nname: " + name + "\ndescription: x\n---\n" + body + "\n"
|
||||
if err := os.WriteFile(filepath.Join(dir, name+".md"), []byte(text), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
func agentStore(t *testing.T) (Store, *fakeFetcher) {
|
||||
f := &fakeFetcher{repos: map[string]*fakeRepo{
|
||||
"github:cursor/plugins": {head: "r1", revs: map[string]string{
|
||||
"r1": withAgents(t, fixture(t, "tdd"), "comment-sicko:v1", "poteto-agent"),
|
||||
}},
|
||||
}}
|
||||
return Store{Dir: t.TempDir(), Fetcher: f}, f
|
||||
}
|
||||
|
||||
// agents builds manifest entries; "name=alias" renames.
|
||||
func agents(items ...string) []manifest.Skill {
|
||||
var out []manifest.Skill
|
||||
for _, n := range items {
|
||||
name, as, _ := strings.Cut(n, "=")
|
||||
out = append(out, manifest.Skill{Name: name, As: as})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func lockedAgents(t *testing.T, s Store, key string) map[string]string {
|
||||
t.Helper()
|
||||
l, err := lock.Load(filepath.Join(s.Dir, LockFile))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return l.Sources[key].Agents
|
||||
}
|
||||
|
||||
func TestAddAgentsOnly(t *testing.T) {
|
||||
s, _ := agentStore(t)
|
||||
key, err := s.Add(context.Background(), AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]string{"comment-sicko": "pstack/agents/comment-sicko.md"}
|
||||
if got := lockedAgents(t, s, key); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("agents = %v, want %v", got, want)
|
||||
}
|
||||
list, err := s.List()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(list) != 1 || !list[0].Agent || list[0].Name != "comment-sicko" {
|
||||
t.Errorf("list = %+v, want just the agent", list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddAgentAliasThenAll(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, _ := agentStore(t)
|
||||
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko=sicko"), Skills: pick("tdd")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := lockedAgents(t, s, "cursor/plugins"); got["sicko"] != "pstack/agents/comment-sicko.md" {
|
||||
t.Errorf("agents = %v", got)
|
||||
}
|
||||
// Installing every agent replaces the list with "*".
|
||||
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", AllAgents: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := lockedAgents(t, s, "cursor/plugins"); len(got) != 2 {
|
||||
t.Errorf("agents = %v, want both", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddNothingListsAgents(t *testing.T) {
|
||||
s, _ := agentStore(t)
|
||||
_, err := s.Add(context.Background(), AddRequest{URL: "github:cursor/plugins"})
|
||||
var sel *NoSelectionError
|
||||
if !errors.As(err, &sel) || len(sel.AvailableAgents) != 2 {
|
||||
t.Fatalf("err = %v, want a NoSelectionError listing both agents", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddUnknownAgent(t *testing.T) {
|
||||
s, _ := agentStore(t)
|
||||
_, err := s.Add(context.Background(), AddRequest{URL: "github:cursor/plugins", Agents: agents("nope")})
|
||||
if err == nil || !strings.Contains(err.Error(), `no agent "nope"`) {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentModifiedUpstream(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, f := agentStore(t)
|
||||
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko", "poteto-agent")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
f.repos["github:cursor/plugins"].push("r2", withAgents(t, fixture(t, "tdd"), "comment-sicko:v2", "poteto-agent"))
|
||||
sts := check(t, s)
|
||||
if got, want := changes(sts[0]), []string{"↑comment-sicko"}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("changes = %v, want %v", got, want)
|
||||
}
|
||||
ch := sts[0].Changes[0]
|
||||
if !ch.Agent || ch.Label() != "agent comment-sicko" {
|
||||
t.Errorf("change = %+v", ch)
|
||||
}
|
||||
d, err := Diff(ctx, ch)
|
||||
if err != nil || !strings.Contains(d, "-v1") || !strings.Contains(d, "+v2") {
|
||||
t.Errorf("diff = %q, err %v", d, err)
|
||||
}
|
||||
if err := s.Apply(sts); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := check(t, s); got[0].Outdated() {
|
||||
t.Errorf("still outdated after apply: %v", changes(got[0]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentAddedUpstreamWithStar(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, f := agentStore(t)
|
||||
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", AllAgents: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.repos["github:cursor/plugins"].push("r2", withAgents(t, fixture(t, "tdd"), "comment-sicko:v1", "poteto-agent", "new-one"))
|
||||
sts := check(t, s)
|
||||
if got, want := changes(sts[0]), []string{"+new-one"}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("changes = %v, want %v", got, want)
|
||||
}
|
||||
// An added agent is a file, so the diff compares against an empty file.
|
||||
d, err := Diff(ctx, sts[0].Changes[0])
|
||||
if err != nil || !strings.Contains(d, "+name: new-one") {
|
||||
t.Errorf("diff = %q, err %v", d, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentVanishesUpstream(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, f := agentStore(t)
|
||||
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("poteto-agent")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.repos["github:cursor/plugins"].push("r2", withAgents(t, fixture(t, "tdd"), "comment-sicko"))
|
||||
sts, err := s.Check(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sts[0].Err == nil || !strings.Contains(sts[0].Err.Error(), `no agent "poteto-agent"`) {
|
||||
t.Fatalf("status err = %v, want the missing agent reported", sts[0].Err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveAgent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, _ := agentStore(t)
|
||||
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko", "poteto-agent")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Remove("comment-sicko"); err == nil {
|
||||
t.Error("Remove should only find skills")
|
||||
}
|
||||
if _, err := s.RemoveAgent("poteto-agent"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := lockedAgents(t, s, "cursor/plugins"); len(got) != 1 {
|
||||
t.Errorf("agents = %v, want one left", got)
|
||||
}
|
||||
// The last entry takes the source with it.
|
||||
if _, err := s.RemoveAgent("comment-sicko"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m, err := manifest.Load(filepath.Join(s.Dir, ManifestFile))
|
||||
if err != nil || len(m.Sources) != 0 {
|
||||
t.Errorf("manifest = %+v, err %v; want the empty source dropped", m, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveSkillKeepsSourceWithAgents(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, _ := agentStore(t)
|
||||
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Skills: pick("tdd"), Agents: agents("comment-sicko")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Remove("tdd"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := lockedAgents(t, s, "cursor/plugins"); len(got) != 1 {
|
||||
t.Errorf("agents = %v, want the agent kept", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentNameCollisionAcrossSources(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, f := agentStore(t)
|
||||
f.repos["github:other/plugins"] = &fakeRepo{head: "r1", revs: map[string]string{"r1": withAgents(t, fixture(t, "x"), "comment-sicko")}}
|
||||
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := s.Add(ctx, AddRequest{URL: "github:other/plugins", Agents: agents("comment-sicko")})
|
||||
if err == nil || !strings.Contains(err.Error(), `agent "comment-sicko" comes from both`) {
|
||||
t.Fatalf("err = %v, want an agent collision", err)
|
||||
}
|
||||
}
|
||||
+139
-67
@@ -3,6 +3,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
@@ -88,16 +89,24 @@ type AddRequest struct {
|
||||
// Skills to add. Ignored when All is set.
|
||||
Skills []manifest.Skill
|
||||
All bool
|
||||
// Agents to add, by name or path; ignored when AllAgents is set.
|
||||
Agents []manifest.Skill
|
||||
AllAgents bool
|
||||
}
|
||||
|
||||
// NoSelectionError is returned by Add when neither skills nor All were given.
|
||||
type NoSelectionError struct {
|
||||
Source string
|
||||
Available []skills.Skill
|
||||
Source string
|
||||
Available []skills.Skill
|
||||
AvailableAgents []skills.Skill
|
||||
}
|
||||
|
||||
func (e *NoSelectionError) Error() string {
|
||||
return fmt.Sprintf("no skills selected from %s; available: %s", e.Source, available(e.Available))
|
||||
msg := fmt.Sprintf("no skills selected from %s; available: %s", e.Source, available(e.Available))
|
||||
if len(e.AvailableAgents) > 0 {
|
||||
msg += "; agents: " + available(e.AvailableAgents)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func available(found []skills.Skill) string {
|
||||
@@ -120,43 +129,60 @@ func (s Store) Add(ctx context.Context, req AddRequest) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
res, found, err := s.fetchSkills(ctx, ref)
|
||||
res, found, agents, err := s.fetchSource(ctx, ref)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case req.All:
|
||||
src.Skills = manifest.Selection{All: true}
|
||||
case len(req.Skills) == 0:
|
||||
return "", &NoSelectionError{Source: key, Available: found}
|
||||
case src.Skills.All:
|
||||
return "", fmt.Errorf("source %q already installs every skill", key)
|
||||
default:
|
||||
for _, k := range req.Skills {
|
||||
if k.Name == "" {
|
||||
i := slices.IndexFunc(found, func(f skills.Skill) bool { return f.Path == k.Path })
|
||||
if i < 0 {
|
||||
return "", fmt.Errorf("%s: no skill at %s", key, k.Path)
|
||||
}
|
||||
k.Name = found[i].Name
|
||||
}
|
||||
if slices.ContainsFunc(src.Skills.Items, func(e manifest.Skill) bool { return e.Installed() == k.Installed() }) {
|
||||
return "", fmt.Errorf("%s is already installed from %s", k.Installed(), key)
|
||||
}
|
||||
src.Skills.Items = append(src.Skills.Items, k)
|
||||
}
|
||||
if !req.All && len(req.Skills) == 0 && !req.AllAgents && len(req.Agents) == 0 {
|
||||
return "", &NoSelectionError{Source: key, Available: found, AvailableAgents: agents}
|
||||
}
|
||||
if err := selectInto(&src.Skills, req.Skills, req.All, found, key, "skill"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := selectInto(&src.Agents, req.Agents, req.AllAgents, agents, key, "agent"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resolved, err := resolve(src.Skills, found)
|
||||
resolved, err := resolve(src.Skills, found, "skill")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
resolvedAgents, err := resolve(src.Agents, agents, "agent")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
m.Sources[key] = src
|
||||
l.Sources[key] = &lock.Source{Locked: res.Locked, Skills: resolved}
|
||||
l.Sources[key] = &lock.Source{Locked: res.Locked, Skills: resolved, Agents: resolvedAgents}
|
||||
return key, s.save(m, l)
|
||||
}
|
||||
|
||||
// selectInto adds the requested entries of one kind to sel, or switches it to "*".
|
||||
func selectInto(sel *manifest.Selection, req []manifest.Skill, all bool, found []skills.Skill, key, kind string) error {
|
||||
switch {
|
||||
case all:
|
||||
*sel = manifest.Selection{All: true}
|
||||
case len(req) == 0:
|
||||
case sel.All:
|
||||
return fmt.Errorf("source %q already installs every %s", key, kind)
|
||||
default:
|
||||
for _, k := range req {
|
||||
if k.Name == "" {
|
||||
i := slices.IndexFunc(found, func(f skills.Skill) bool { return f.Path == k.Path })
|
||||
if i < 0 {
|
||||
return fmt.Errorf("%s: no %s at %s", key, kind, k.Path)
|
||||
}
|
||||
k.Name = found[i].Name
|
||||
}
|
||||
if slices.ContainsFunc(sel.Items, func(e manifest.Skill) bool { return e.Installed() == k.Installed() }) {
|
||||
return fmt.Errorf("%s is already installed from %s", k.Installed(), key)
|
||||
}
|
||||
sel.Items = append(sel.Items, k)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// target resolves the source an add goes to and the flake ref to fetch: the pinned
|
||||
// rev for a tracked source, the head of its ref for a new one. src is a copy that
|
||||
// is safe to modify.
|
||||
@@ -180,10 +206,10 @@ func target(m *manifest.Manifest, l *lock.Lock, req AddRequest) (key string, src
|
||||
if locked == nil {
|
||||
return "", nil, "", fmt.Errorf("source %q is in %s but not locked; run skm update %s first", key, ManifestFile, key)
|
||||
}
|
||||
src = &manifest.Source{URL: existing.URL, Ref: existing.Ref, Skills: manifest.Selection{
|
||||
All: existing.Skills.All,
|
||||
Items: slices.Clone(existing.Skills.Items),
|
||||
}}
|
||||
src = &manifest.Source{URL: existing.URL, Ref: existing.Ref,
|
||||
Skills: manifest.Selection{All: existing.Skills.All, Items: slices.Clone(existing.Skills.Items)},
|
||||
Agents: manifest.Selection{All: existing.Agents.All, Items: slices.Clone(existing.Agents.Items)},
|
||||
}
|
||||
return key, src, fetch.AtRev(flakeRef(src), locked.Rev()), nil
|
||||
}
|
||||
|
||||
@@ -216,7 +242,7 @@ func (s Store) Available(ctx context.Context, req AddRequest) ([]skills.Skill, m
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
_, found, err := s.fetchSkills(ctx, ref)
|
||||
_, found, _, err := s.fetchSource(ctx, ref)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
@@ -229,27 +255,41 @@ func (s Store) Available(ctx context.Context, req AddRequest) ([]skills.Skill, m
|
||||
return found, installed, nil
|
||||
}
|
||||
|
||||
func (s Store) fetchSkills(ctx context.Context, ref string) (fetch.Result, []skills.Skill, error) {
|
||||
// fetchSource prefetches ref and finds its skills and agent definitions.
|
||||
func (s Store) fetchSource(ctx context.Context, ref string) (fetch.Result, []skills.Skill, []skills.Skill, error) {
|
||||
res, err := s.Fetcher.Prefetch(ctx, ref)
|
||||
if err != nil {
|
||||
return fetch.Result{}, nil, err
|
||||
return fetch.Result{}, nil, nil, err
|
||||
}
|
||||
found, err := skills.Discover(res.StorePath)
|
||||
if err != nil {
|
||||
return fetch.Result{}, nil, err
|
||||
return fetch.Result{}, nil, nil, err
|
||||
}
|
||||
if len(found) == 0 {
|
||||
return fetch.Result{}, nil, fmt.Errorf("%s contains no SKILL.md", ref)
|
||||
agents, err := skills.DiscoverAgents(res.StorePath)
|
||||
if err != nil {
|
||||
return fetch.Result{}, nil, nil, err
|
||||
}
|
||||
return res, found, nil
|
||||
if len(found) == 0 && len(agents) == 0 {
|
||||
return fetch.Result{}, nil, nil, fmt.Errorf("%s contains no SKILL.md or agent definition", ref)
|
||||
}
|
||||
return res, found, agents, nil
|
||||
}
|
||||
|
||||
// fetchSkills is fetchSource for callers that only install skills.
|
||||
func (s Store) fetchSkills(ctx context.Context, ref string) (fetch.Result, []skills.Skill, error) {
|
||||
res, found, _, err := s.fetchSource(ctx, ref)
|
||||
if err == nil && len(found) == 0 {
|
||||
err = fmt.Errorf("%s contains no SKILL.md", ref)
|
||||
}
|
||||
return res, found, err
|
||||
}
|
||||
|
||||
// resolve maps installed names to paths within the source.
|
||||
func resolve(sel manifest.Selection, found []skills.Skill) (map[string]string, error) {
|
||||
func resolve(sel manifest.Selection, found []skills.Skill, kind string) (map[string]string, error) {
|
||||
out := map[string]string{}
|
||||
add := func(name, path string) error {
|
||||
if prev, dup := out[name]; dup {
|
||||
return fmt.Errorf("two skills installed as %q: %s and %s", name, prev, path)
|
||||
return fmt.Errorf("two %ss installed as %q: %s and %s", kind, name, prev, path)
|
||||
}
|
||||
out[name] = path
|
||||
return nil
|
||||
@@ -258,7 +298,7 @@ func resolve(sel manifest.Selection, found []skills.Skill) (map[string]string, e
|
||||
if sel.All {
|
||||
for _, f := range found {
|
||||
if err := add(f.Name, f.Path); err != nil {
|
||||
return nil, fmt.Errorf(`%w; list skills explicitly, picking one with "path"`, err)
|
||||
return nil, fmt.Errorf(`%w; list %ss explicitly, picking one with "path"`, err, kind)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
@@ -272,15 +312,15 @@ func resolve(sel manifest.Selection, found []skills.Skill) (map[string]string, e
|
||||
}
|
||||
switch {
|
||||
case len(matches) == 0 && k.Path != "":
|
||||
return nil, fmt.Errorf("no skill %q at %s", k.Name, k.Path)
|
||||
return nil, fmt.Errorf("no %s %q at %s", kind, k.Name, k.Path)
|
||||
case len(matches) == 0:
|
||||
return nil, fmt.Errorf("no skill %q; available: %s", k.Name, available(found))
|
||||
return nil, fmt.Errorf("no %s %q; available: %s", kind, k.Name, available(found))
|
||||
case len(matches) > 1:
|
||||
var paths []string
|
||||
for _, f := range matches {
|
||||
paths = append(paths, f.Path)
|
||||
}
|
||||
return nil, fmt.Errorf(`several skills named %q (%s); pick one with "path"`, k.Name, strings.Join(paths, ", "))
|
||||
return nil, fmt.Errorf(`several %ss named %q (%s); pick one with "path"`, kind, k.Name, strings.Join(paths, ", "))
|
||||
}
|
||||
if err := add(k.Installed(), matches[0].Path); err != nil {
|
||||
return nil, err
|
||||
@@ -290,13 +330,19 @@ func resolve(sel manifest.Selection, found []skills.Skill) (map[string]string, e
|
||||
}
|
||||
|
||||
func checkCollisions(l *lock.Lock) error {
|
||||
owner := map[string]string{}
|
||||
for _, key := range slices.Sorted(maps.Keys(l.Sources)) {
|
||||
for _, name := range slices.Sorted(maps.Keys(l.Sources[key].Skills)) {
|
||||
if prev, ok := owner[name]; ok {
|
||||
return fmt.Errorf(`skill %q comes from both %s and %s; rename one with "as"`, name, prev, key)
|
||||
for _, kind := range []string{"skill", "agent"} {
|
||||
owner := map[string]string{}
|
||||
for _, key := range slices.Sorted(maps.Keys(l.Sources)) {
|
||||
installed := l.Sources[key].Skills
|
||||
if kind == "agent" {
|
||||
installed = l.Sources[key].Agents
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(installed)) {
|
||||
if prev, ok := owner[name]; ok {
|
||||
return fmt.Errorf(`%s %q comes from both %s and %s; rename one with "as"`, kind, name, prev, key)
|
||||
}
|
||||
owner[name] = key
|
||||
}
|
||||
owner[name] = key
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -304,42 +350,51 @@ func checkCollisions(l *lock.Lock) error {
|
||||
|
||||
// Remove drops one installed skill, and its source once that is empty.
|
||||
// Returns the source it came from.
|
||||
func (s Store) Remove(name string) (string, error) {
|
||||
func (s Store) Remove(name string) (string, error) { return s.removeOne(name, false) }
|
||||
|
||||
// RemoveAgent drops one installed agent definition, like Remove.
|
||||
func (s Store) RemoveAgent(name string) (string, error) { return s.removeOne(name, true) }
|
||||
|
||||
func (s Store) removeOne(name string, agent bool) (string, error) {
|
||||
kind := "skill"
|
||||
if agent {
|
||||
kind = "agent"
|
||||
}
|
||||
m, l, err := s.load()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, key := range slices.Sorted(maps.Keys(m.Sources)) {
|
||||
src := m.Sources[key]
|
||||
i := slices.IndexFunc(src.Skills.Items, func(k manifest.Skill) bool { return k.Installed() == name })
|
||||
locked := isLocked(l, key, name)
|
||||
sel, locks := &src.Skills, func(ls *lock.Source) map[string]string { return ls.Skills }
|
||||
if agent {
|
||||
sel, locks = &src.Agents, func(ls *lock.Source) map[string]string { return ls.Agents }
|
||||
}
|
||||
i := slices.IndexFunc(sel.Items, func(k manifest.Skill) bool { return k.Installed() == name })
|
||||
locked := l.Sources[key] != nil && has(locks(l.Sources[key]), name)
|
||||
if i < 0 && !locked {
|
||||
continue
|
||||
}
|
||||
if src.Skills.All {
|
||||
return "", fmt.Errorf(`%s comes from %s, which installs every skill ("*"); remove the whole source with --source %s`, name, key, key)
|
||||
if sel.All {
|
||||
return "", fmt.Errorf(`%s comes from %s, which installs every %s ("*"); remove the whole source with --source %s`, name, key, kind, key)
|
||||
}
|
||||
if i >= 0 {
|
||||
src.Skills.Items = slices.Delete(src.Skills.Items, i, i+1)
|
||||
sel.Items = slices.Delete(sel.Items, i, i+1)
|
||||
}
|
||||
if locked {
|
||||
delete(l.Sources[key].Skills, name)
|
||||
delete(locks(l.Sources[key]), name)
|
||||
}
|
||||
if len(src.Skills.Items) == 0 {
|
||||
if src.Empty() {
|
||||
delete(m.Sources, key)
|
||||
delete(l.Sources, key)
|
||||
}
|
||||
return key, s.save(m, l)
|
||||
}
|
||||
return "", fmt.Errorf("no installed skill %q", name)
|
||||
return "", fmt.Errorf("no installed %s %q", kind, name)
|
||||
}
|
||||
|
||||
func isLocked(l *lock.Lock, key, name string) bool {
|
||||
src := l.Sources[key]
|
||||
if src == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := src.Skills[name]
|
||||
func has(m map[string]string, name string) bool {
|
||||
_, ok := m[name]
|
||||
return ok
|
||||
}
|
||||
|
||||
@@ -362,9 +417,11 @@ type Installed struct {
|
||||
Source string
|
||||
Path string
|
||||
Rev string
|
||||
// Agent is set for an agent definition, which Path names as a file.
|
||||
Agent bool
|
||||
}
|
||||
|
||||
// List returns the locked skills, sorted by name.
|
||||
// List returns the locked skills and agents, sorted by name.
|
||||
func (s Store) List() ([]Installed, error) {
|
||||
_, l, err := s.load()
|
||||
if err != nil {
|
||||
@@ -375,7 +432,22 @@ func (s Store) List() ([]Installed, error) {
|
||||
for name, path := range src.Skills {
|
||||
out = append(out, Installed{Name: name, Source: key, Path: path, Rev: src.Rev()})
|
||||
}
|
||||
for name, path := range src.Agents {
|
||||
out = append(out, Installed{Name: name, Source: key, Path: path, Rev: src.Rev(), Agent: true})
|
||||
}
|
||||
}
|
||||
slices.SortFunc(out, func(a, b Installed) int { return strings.Compare(a.Name, b.Name) })
|
||||
slices.SortFunc(out, func(a, b Installed) int {
|
||||
return cmp.Or(strings.Compare(a.Name, b.Name), compareBool(a.Agent, b.Agent))
|
||||
})
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func compareBool(a, b bool) int {
|
||||
switch {
|
||||
case a == b:
|
||||
return 0
|
||||
case b:
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func (s Store) Import(ctx context.Context, vercelPath, localDir string) (ImportR
|
||||
if !f.src.Skills.All && len(f.src.Skills.Items) == 0 {
|
||||
continue
|
||||
}
|
||||
resolved, err := resolve(f.src.Skills, f.found)
|
||||
resolved, err := resolve(f.src.Skills, f.found, "skill")
|
||||
if err != nil {
|
||||
return rep, fmt.Errorf("%s: %w", f.key, err)
|
||||
}
|
||||
|
||||
+77
-21
@@ -31,15 +31,25 @@ func (k ChangeKind) Symbol() string {
|
||||
return [...]string{"↑", "+", "-"}[k]
|
||||
}
|
||||
|
||||
// Change is one installed skill that differs between the lock and upstream.
|
||||
// Change is one installed skill or agent that differs between the lock and upstream.
|
||||
type Change struct {
|
||||
Name string
|
||||
Kind ChangeKind
|
||||
// Skill directories in the store; empty when added or removed, and Old is
|
||||
// also empty for a source dropped from the manifest.
|
||||
// Agent is set for an agent definition, whose Old and New are files, not directories.
|
||||
Agent bool
|
||||
// Skill directories (or agent files) in the store; empty when added or removed,
|
||||
// and Old is also empty for a source dropped from the manifest.
|
||||
Old, New string
|
||||
}
|
||||
|
||||
// Label is Name, marked when the change is an agent.
|
||||
func (c Change) Label() string {
|
||||
if c.Agent {
|
||||
return "agent " + c.Name
|
||||
}
|
||||
return c.Name
|
||||
}
|
||||
|
||||
// SourceStatus compares a source's lock entry with the head of its ref.
|
||||
type SourceStatus struct {
|
||||
Source string
|
||||
@@ -91,7 +101,7 @@ func selectSources(m *manifest.Manifest, l *lock.Lock, names []string) ([]string
|
||||
for _, name := range names {
|
||||
key, ok := owner(m, l, name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no source or installed skill %q", name)
|
||||
return nil, fmt.Errorf("no source or installed skill or agent %q", name)
|
||||
}
|
||||
picked[key] = true
|
||||
}
|
||||
@@ -110,9 +120,13 @@ func owner(m *manifest.Manifest, l *lock.Lock, name string) (string, bool) {
|
||||
if _, ok := src.Skills[name]; ok {
|
||||
return key, true
|
||||
}
|
||||
if _, ok := src.Agents[name]; ok {
|
||||
return key, true
|
||||
}
|
||||
}
|
||||
for key, src := range m.Sources {
|
||||
if slices.ContainsFunc(src.Skills.Items, func(k manifest.Skill) bool { return k.Installed() == name }) {
|
||||
named := func(k manifest.Skill) bool { return k.Installed() == name }
|
||||
if slices.ContainsFunc(src.Skills.Items, named) || slices.ContainsFunc(src.Agents.Items, named) {
|
||||
return key, true
|
||||
}
|
||||
}
|
||||
@@ -131,6 +145,9 @@ func (s Store) check(ctx context.Context, key string, src *manifest.Source, lock
|
||||
for _, name := range slices.Sorted(maps.Keys(locked.Skills)) {
|
||||
st.Changes = append(st.Changes, Change{Name: name, Kind: Removed})
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(locked.Agents)) {
|
||||
st.Changes = append(st.Changes, Change{Name: name, Kind: Removed, Agent: true})
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
@@ -150,31 +167,60 @@ func (s Store) check(ctx context.Context, key string, src *manifest.Source, lock
|
||||
}
|
||||
st.NewRev = res.Rev()
|
||||
found, err := skills.Discover(res.StorePath)
|
||||
var agents []skills.Skill
|
||||
if err == nil {
|
||||
agents, err = skills.DiscoverAgents(res.StorePath)
|
||||
}
|
||||
if err == nil {
|
||||
st.next = &lock.Source{Locked: res.Locked}
|
||||
st.next.Skills, err = resolve(src.Skills, found)
|
||||
st.next.Skills, err = resolve(src.Skills, found, "skill")
|
||||
}
|
||||
if err == nil {
|
||||
st.next.Agents, err = resolve(src.Agents, agents, "agent")
|
||||
}
|
||||
if err != nil {
|
||||
st.Err = err
|
||||
return st
|
||||
}
|
||||
|
||||
names := slices.Sorted(maps.Keys(locked.Skills))
|
||||
for name := range st.next.Skills {
|
||||
if _, ok := locked.Skills[name]; !ok {
|
||||
for _, kind := range []struct {
|
||||
old, new map[string]string
|
||||
agent bool
|
||||
}{
|
||||
{locked.Skills, st.next.Skills, false},
|
||||
{locked.Agents, st.next.Agents, true},
|
||||
} {
|
||||
changes, err := compare(ctx, kind.old, kind.new, oldRoot, res.StorePath, kind.agent)
|
||||
if err != nil {
|
||||
st.Err = err
|
||||
return st
|
||||
}
|
||||
st.Changes = append(st.Changes, changes...)
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
// compare lists the entries of one kind that differ between the locked paths (in
|
||||
// oldRoot) and the resolved ones (in newRoot), sorted by name.
|
||||
func compare(ctx context.Context, locked, next map[string]string, oldRoot, newRoot string, agent bool) ([]Change, error) {
|
||||
names := slices.Sorted(maps.Keys(locked))
|
||||
for name := range next {
|
||||
if _, ok := locked[name]; !ok {
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
slices.Sort(names)
|
||||
|
||||
var out []Change
|
||||
for _, name := range names {
|
||||
oldPath, wasLocked := locked.Skills[name]
|
||||
newPath, stays := st.next.Skills[name]
|
||||
ch := Change{Name: name}
|
||||
oldPath, wasLocked := locked[name]
|
||||
newPath, stays := next[name]
|
||||
ch := Change{Name: name, Agent: agent}
|
||||
if wasLocked {
|
||||
ch.Old = filepath.Join(oldRoot, oldPath)
|
||||
}
|
||||
if stays {
|
||||
ch.New = filepath.Join(res.StorePath, newPath)
|
||||
ch.New = filepath.Join(newRoot, newPath)
|
||||
}
|
||||
switch {
|
||||
case !stays:
|
||||
@@ -184,17 +230,16 @@ func (s Store) check(ctx context.Context, key string, src *manifest.Source, lock
|
||||
default:
|
||||
same, err := sameDir(ctx, ch.Old, ch.New)
|
||||
if err != nil {
|
||||
st.Err = err
|
||||
return st
|
||||
return nil, err
|
||||
}
|
||||
if same {
|
||||
continue
|
||||
}
|
||||
ch.Kind = Modified
|
||||
}
|
||||
st.Changes = append(st.Changes, ch)
|
||||
out = append(out, ch)
|
||||
}
|
||||
return st
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Apply writes the lock entries of the outdated statuses.
|
||||
@@ -232,7 +277,7 @@ func CommitMessage(applied []SourceStatus) string {
|
||||
heads = append(heads, fmt.Sprintf("update %s %.7s→%.7s", st.Source, st.OldRev, st.NewRev))
|
||||
}
|
||||
for _, ch := range st.Changes {
|
||||
body = append(body, ch.Kind.Symbol()+" "+ch.Name)
|
||||
body = append(body, ch.Kind.Symbol()+" "+ch.Label())
|
||||
}
|
||||
}
|
||||
return "skills: " + strings.Join(heads, ", ") + "\n\n" + strings.Join(body, "\n")
|
||||
@@ -240,9 +285,20 @@ func CommitMessage(applied []SourceStatus) string {
|
||||
|
||||
// Diff renders a change as a unified diff with a/<name> and b/<name> paths.
|
||||
func Diff(ctx context.Context, ch Change) (string, error) {
|
||||
empty, err := os.MkdirTemp("", "skm-empty-")
|
||||
if err != nil {
|
||||
return "", err
|
||||
// An added or removed agent is a file, so it needs an empty file to compare with.
|
||||
var empty string
|
||||
if ch.Agent {
|
||||
f, err := os.CreateTemp("", "skm-empty-")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
f.Close()
|
||||
empty = f.Name()
|
||||
} else {
|
||||
var err error
|
||||
if empty, err = os.MkdirTemp("", "skm-empty-"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
defer os.Remove(empty)
|
||||
oldDir, newDir := cmp.Or(ch.Old, empty), cmp.Or(ch.New, empty)
|
||||
|
||||
Reference in new issue
Block a user