mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
The Claude manifest fallback from #806 offered Codex more than it can install: - A Claude package whose only parts are ones Codex does not read, such as agents, was offered to Codex with no components, so Codex would install an empty plugin. - When Codex's own catalog pointed at a folder it rejects, the whole candidate could be replaced by the Claude catalog's folder, which now carries a Codex fallback. Codex reads only its own catalog, so it would never install from that folder. #806 also stopped every Agent from using a later catalog's folder when the first one was rejected. Only Codex needs that, since only Codex reads one catalog exclusively, so other Agents get the old behaviour back.
436 lines
15 KiB
Go
436 lines
15 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"maps"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
var namePattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]*$`)
|
|
var githubPattern = regexp.MustCompile(`^[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+$`)
|
|
|
|
// Codex accepts only ASCII letters, digits, `_`, and `-` in marketplace names.
|
|
var marketUnsafe = regexp.MustCompile(`[^a-zA-Z0-9_-]+`)
|
|
|
|
// acquire checks out remote sources in an ephemeral directory, without running
|
|
// repository code or registering anything with an Agent. Callers must clean up.
|
|
func acquire(ctx context.Context, source string) (string, string, func(), error) {
|
|
return acquireRef(ctx, source, "")
|
|
}
|
|
|
|
func acquireRef(ctx context.Context, source, ref string) (string, string, func(), error) {
|
|
cleanup := func() {}
|
|
if strings.HasPrefix(ref, "-") || strings.ContainsAny(ref, "\x00\r\n ") {
|
|
return "", "", cleanup, fmt.Errorf("invalid source ref")
|
|
}
|
|
if strings.HasPrefix(source, "~/") {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", "", cleanup, err
|
|
}
|
|
source = filepath.Join(home, source[2:])
|
|
}
|
|
if info, err := os.Stat(source); err == nil && info.IsDir() {
|
|
if ref != "" {
|
|
return "", "", cleanup, fmt.Errorf("source ref requires a remote Git source")
|
|
}
|
|
p, err := filepath.Abs(source)
|
|
if err != nil {
|
|
return "", "", cleanup, err
|
|
}
|
|
// Read from the real location: a linked parent (macOS /var -> /private/var) is not plugin content.
|
|
root, err := filepath.EvalSymlinks(p)
|
|
return root, p, cleanup, err
|
|
}
|
|
if isNpmSource(source) {
|
|
return "", "", cleanup, fmt.Errorf("npm packages can't be previewed: Pi downloads them when it installs them. Add one to a Pi target with: skillshare plugin add %s --target pi", source)
|
|
}
|
|
if githubPattern.MatchString(source) {
|
|
source = "https://github.com/" + source + ".git"
|
|
}
|
|
u, err := url.Parse(source)
|
|
if err != nil || u.Scheme != "https" || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" {
|
|
return "", "", cleanup, fmt.Errorf("use an existing directory, owner/repo, or HTTPS Git URL without credentials")
|
|
}
|
|
root, err := os.MkdirTemp("", "skillshare-plugin-")
|
|
if err != nil {
|
|
return "", "", cleanup, err
|
|
}
|
|
cleanup = func() { _ = os.RemoveAll(root) }
|
|
clone, err := filepath.EvalSymlinks(root)
|
|
if err != nil {
|
|
cleanup()
|
|
return "", "", func() {}, err
|
|
}
|
|
_, err = runCommand(ctx, "", nil, "git", "-c", "core.hooksPath=/dev/null", "clone", "--depth", "1", "--", source, root)
|
|
if err != nil {
|
|
cleanup()
|
|
return "", "", func() {}, fmt.Errorf("download plugin source: %w", err)
|
|
}
|
|
if ref != "" {
|
|
if _, err = runCommand(ctx, root, nil, "git", "fetch", "--depth", "1", "origin", ref); err == nil {
|
|
_, err = runCommand(ctx, root, nil, "git", "-c", "core.hooksPath=/dev/null", "checkout", "--detach", "FETCH_HEAD")
|
|
}
|
|
if err != nil {
|
|
cleanup()
|
|
return "", "", func() {}, fmt.Errorf("resolve source ref: %w", err)
|
|
}
|
|
}
|
|
return clone, source, cleanup, nil
|
|
}
|
|
|
|
// treeDigest includes safe relative links, leaves out unsafe ones and rejects special files.
|
|
// The entire package tree is retained, including scripts and referenced assets.
|
|
func treeDigest(root string) (string, error) {
|
|
digest, _, err := hashTree(root)
|
|
return digest, err
|
|
}
|
|
|
|
// hashTree is treeDigest plus the links it left out, relative to root.
|
|
func hashTree(root string) (string, map[string]bool, error) {
|
|
skipped, err := validateSourceLinks(root)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
h := sha256.New()
|
|
var size int64
|
|
count := 0
|
|
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rel, _ := filepath.Rel(root, path)
|
|
if d.IsDir() {
|
|
if d.Name() == ".git" {
|
|
return filepath.SkipDir
|
|
}
|
|
return nil
|
|
}
|
|
if skipped[rel] {
|
|
return nil
|
|
}
|
|
info, err := d.Info()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if utils.IsLinkMode(path, info.Mode()) {
|
|
link, err := os.Readlink(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(h, "%s\x00link\x00%s\x00", filepath.ToSlash(rel), link)
|
|
return nil
|
|
}
|
|
if !info.Mode().IsRegular() {
|
|
return fmt.Errorf("unsupported special file: %s", rel)
|
|
}
|
|
count++
|
|
size += info.Size()
|
|
if count > 20000 || size > 100*1024*1024 {
|
|
return fmt.Errorf("plugin source exceeds 20,000 files or 100 MiB")
|
|
}
|
|
fmt.Fprintf(h, "%s\x00%d\x00%d\x00", filepath.ToSlash(rel), info.Mode().Perm(), info.Size())
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = io.Copy(h, f)
|
|
_ = f.Close()
|
|
return err
|
|
})
|
|
return hex.EncodeToString(h.Sum(nil)), skipped, err
|
|
}
|
|
|
|
func Discover(ctx context.Context, source string) (*Discovery, error) {
|
|
return DiscoverRef(ctx, source, "")
|
|
}
|
|
|
|
func DiscoverRef(ctx context.Context, source, ref string) (*Discovery, error) {
|
|
return DiscoverOptions(ctx, source, ref, "")
|
|
}
|
|
|
|
func DiscoverOptions(ctx context.Context, source, ref, entry string) (*Discovery, error) {
|
|
root, normalized, cleanup, err := acquireRef(ctx, source, ref)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cleanup()
|
|
d, err := discoverRoot(root, normalized, entry)
|
|
if err == nil {
|
|
d.SourceRef = ref
|
|
if strings.HasPrefix(normalized, "https://") {
|
|
if commit, e := runCommand(ctx, root, nil, "git", "rev-parse", "HEAD"); e == nil {
|
|
d.Commit = strings.TrimSpace(string(commit))
|
|
} else {
|
|
return nil, e
|
|
}
|
|
}
|
|
}
|
|
return d, err
|
|
}
|
|
|
|
func discoverRoot(root, source string, explicit ...string) (*Discovery, error) {
|
|
digest, skipped, err := hashTree(root)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := &Discovery{TargetDefinitions: TargetDefinitions(), Source: source, Digest: digest, Candidates: []Candidate{}}
|
|
if len(skipped) > 0 {
|
|
links := slices.Sorted(maps.Keys(skipped))
|
|
if len(links) > 3 {
|
|
links = append(links[:3], "...")
|
|
}
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("Skipped %d broken or unsafe links; they are left out of the install: %s", len(skipped), strings.Join(links, ", ")))
|
|
}
|
|
seenAt := map[string]int{}
|
|
codexListed := map[string]bool{}
|
|
var ompCatalog map[string]Candidate
|
|
// Read all native catalogs; the same package may be advertised more than once.
|
|
for _, path := range []string{".agents/plugins/marketplace.json", ".omp-plugin/marketplace.json", ".claude-plugin/marketplace.json", ".cursor-plugin/marketplace.json", ".github/plugin/marketplace.json", ".plugin/marketplace.json", "marketplace.json"} {
|
|
data, err := os.ReadFile(filepath.Join(root, path))
|
|
if os.IsNotExist(err) {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if path == ".omp-plugin/marketplace.json" {
|
|
ompCatalog = map[string]Candidate{}
|
|
}
|
|
var catalog struct {
|
|
Name string `json:"name"`
|
|
Plugins []map[string]json.RawMessage `json:"plugins"`
|
|
}
|
|
if err := json.Unmarshal(data, &catalog); err != nil {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("%s: invalid marketplace: %v", path, err))
|
|
continue
|
|
}
|
|
if !namePattern.MatchString(catalog.Name) {
|
|
result.Warnings = append(result.Warnings, path+": marketplace requires a valid name")
|
|
continue
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, fields := range catalog.Plugins {
|
|
var name string
|
|
_ = json.Unmarshal(fields["name"], &name)
|
|
if !namePattern.MatchString(name) || seen[name] {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("%s: invalid or duplicate plugin name: %s", path, name))
|
|
continue
|
|
}
|
|
seen[name] = true
|
|
var rel string
|
|
if json.Unmarshal(fields["source"], &rel) != nil {
|
|
var src struct {
|
|
Source string `json:"source"`
|
|
Path string `json:"path"`
|
|
URL string `json:"url"`
|
|
}
|
|
_ = json.Unmarshal(fields["source"], &src)
|
|
if src.Source == "local" {
|
|
rel = src.Path
|
|
} else if src.Source == "url" && (src.URL == "." || strings.HasPrefix(src.URL, "./")) {
|
|
rel = src.URL
|
|
}
|
|
}
|
|
// One broken entry marks its own row; the rest of the marketplace stays installable.
|
|
c := Candidate{Name: name, Marketplace: catalog.Name, Targets: []string{}, Components: []string{}, TargetInfo: map[string]TargetPackage{}}
|
|
clean := filepath.Clean(rel)
|
|
switch {
|
|
case rel == "":
|
|
c.block("plugins.problem.externalSource", "This entry uses an external source. Add its Git repository directly, or install with the native client and import it.", nil)
|
|
case filepath.IsAbs(clean) || clean == ".." || strings.HasPrefix(clean, ".."+string(filepath.Separator)):
|
|
c.block("plugins.problem.pathEscapes", "Plugin path escapes the marketplace: "+rel, map[string]string{"path": rel})
|
|
default:
|
|
dir := filepath.Join(root, clean)
|
|
found, err := inspect(dir, explicit...)
|
|
ompPackage := false
|
|
if info, ok := found.TargetInfo["omp"]; path == ".omp-plugin/marketplace.json" && ok && info.Manifest == "package.json" && (info.Problem == "" || info.ProblemKey == "plugins.problem.ompName") {
|
|
// A marketplace names an OMP package independently of its optional npm name.
|
|
ompPackage, err = true, nil
|
|
info.Problem, info.ProblemKey, info.ProblemArgs = "", "", nil
|
|
if !validOMPName(name) {
|
|
info.block("plugins.problem.ompName", "OMP plugin names use only ASCII letters, digits, hyphens and dots, up to 64 characters", nil)
|
|
}
|
|
found.TargetInfo["omp"] = info
|
|
found.collectTargets()
|
|
}
|
|
if _, claudeManifest := found.TargetInfo["claude"]; path == ".claude-plugin/marketplace.json" && !claudeManifest {
|
|
// Claude makes plugin.json optional, even alongside another Agent's package manifest.
|
|
claude := claudeCatalogCandidate(dir, name, fields)
|
|
if err != nil {
|
|
maps.Copy(claude.TargetInfo, found.TargetInfo)
|
|
claude.collectTargets()
|
|
found, err = claude, nil
|
|
} else {
|
|
maps.Copy(found.TargetInfo, claude.TargetInfo)
|
|
found.collectTargets()
|
|
}
|
|
}
|
|
switch {
|
|
case err != nil:
|
|
found.block("plugins.problem.noManifest", name+": "+err.Error(), nil)
|
|
case found.Name != name:
|
|
// Claude installs under the catalog's name; Codex refuses the mismatch.
|
|
// ponytail: other Agents are unverified, so they are blocked too; unblock one once its CLI is checked.
|
|
args := map[string]string{"name": name, "manifest": found.Name}
|
|
for target, info := range found.TargetInfo {
|
|
if target != "claude" && !(target == "omp" && ompPackage) && info.Problem == "" {
|
|
info.block("plugins.problem.catalogNameDiffers", "Catalog and manifest names differ for "+name, args)
|
|
found.TargetInfo[target] = info
|
|
}
|
|
}
|
|
found.collectTargets()
|
|
if len(found.Targets) == 0 {
|
|
found.block("plugins.problem.catalogNameDiffers", "Catalog and manifest names differ for "+name, args)
|
|
}
|
|
}
|
|
c = found
|
|
c.Name, c.Path, c.Marketplace = name, filepath.ToSlash(clean), catalog.Name
|
|
for target, info := range c.TargetInfo {
|
|
info.Marketplace = catalog.Name
|
|
if target == "omp" {
|
|
var version string
|
|
if json.Unmarshal(fields["version"], &version) == nil && version != "" {
|
|
info.Version = version
|
|
}
|
|
}
|
|
c.TargetInfo[target] = info
|
|
}
|
|
if path == ".claude-plugin/marketplace.json" {
|
|
c.catalogEntry = claudeEntryFields(fields)
|
|
}
|
|
}
|
|
if path == ".omp-plugin/marketplace.json" {
|
|
native := c
|
|
native.TargetInfo = maps.Clone(c.TargetInfo)
|
|
ompCatalog[c.Name] = native
|
|
}
|
|
if i, ok := seenAt[c.Name]; ok {
|
|
// An external entry has no path; a catalog that points inside the source wins over it.
|
|
// The same folder may be readable by one catalog only (Claude allows no plugin.json).
|
|
switch previous := result.Candidates[i].Path; {
|
|
case c.Path == "" || (c.Path == previous && c.Problem != ""):
|
|
case previous == "":
|
|
result.Candidates[i] = c
|
|
case result.Candidates[i].Problem != "":
|
|
// Codex reads only its own catalog, so a folder it rejected there is not replaced.
|
|
if codexListed[c.Name] {
|
|
delete(c.TargetInfo, "codex")
|
|
c.collectTargets()
|
|
}
|
|
result.Candidates[i] = c
|
|
case c.Problem == "":
|
|
mergeCatalogPath(&result.Candidates[i], c, catalogOwner[path])
|
|
}
|
|
continue
|
|
}
|
|
seenAt[c.Name] = len(result.Candidates)
|
|
codexListed[c.Name] = path == ".agents/plugins/marketplace.json"
|
|
result.Candidates = append(result.Candidates, c)
|
|
}
|
|
}
|
|
if len(result.Candidates) == 0 {
|
|
c, err := inspect(root, explicit...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.Path = "."
|
|
result.Candidates = append(result.Candidates, c)
|
|
}
|
|
for i := range result.Candidates {
|
|
c := &result.Candidates[i]
|
|
// OMP reads its own marketplace exclusively when present, even if an entry is broken.
|
|
if ompCatalog != nil {
|
|
native, ok := ompCatalog[c.Name]
|
|
info, supported := native.TargetInfo["omp"]
|
|
delete(c.TargetInfo, "omp")
|
|
if ok {
|
|
if native.Problem != "" {
|
|
info.block(native.ProblemKey, native.Problem, native.ProblemArgs)
|
|
} else if !supported {
|
|
info.block("plugins.problem.noManifest", native.Name+": no OMP-compatible manifest found", nil)
|
|
}
|
|
info.Path = native.Path
|
|
c.TargetInfo["omp"] = info
|
|
}
|
|
}
|
|
c.collectTargets()
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// catalogOwner is the Agent each native catalog is written for.
|
|
var catalogOwner = map[string]string{".agents/plugins/marketplace.json": "codex", ".omp-plugin/marketplace.json": "omp", ".claude-plugin/marketplace.json": "claude", ".cursor-plugin/marketplace.json": "cursor", ".github/plugin/marketplace.json": "copilot", ".plugin/marketplace.json": "copilot"}
|
|
|
|
// mergeCatalogPath folds in the same plugin advertised by another catalog, possibly in another
|
|
// folder. An Agent keeps the folder found first, unless its own catalog says otherwise or that
|
|
// folder was rejected. Codex reads only its own catalog, so a folder rejected there stays rejected.
|
|
func mergeCatalogPath(kept *Candidate, c Candidate, owner string) {
|
|
for target, info := range c.TargetInfo {
|
|
if existing, ok := kept.TargetInfo[target]; info.Problem != "" || (ok && target != owner && (existing.Problem == "" || target == "codex")) {
|
|
continue
|
|
}
|
|
info.Path = c.Path
|
|
kept.TargetInfo[target] = info
|
|
}
|
|
if kept.catalogEntry == nil {
|
|
kept.catalogEntry = c.catalogEntry
|
|
}
|
|
kept.collectTargets()
|
|
}
|
|
|
|
func copyTree(root, dest string) error {
|
|
_, skipped, err := hashTree(root)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rel, _ := filepath.Rel(root, path)
|
|
to := filepath.Join(dest, rel)
|
|
if d.IsDir() {
|
|
if d.Name() == ".git" {
|
|
return filepath.SkipDir
|
|
}
|
|
return os.MkdirAll(to, 0755)
|
|
}
|
|
if skipped[rel] {
|
|
return nil
|
|
}
|
|
info, err := d.Info()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if utils.IsLinkMode(path, info.Mode()) {
|
|
link, err := os.Readlink(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.Symlink(link, to)
|
|
}
|
|
if !info.Mode().IsRegular() {
|
|
return fmt.Errorf("unsupported file: %s", rel)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(to, data, info.Mode().Perm())
|
|
})
|
|
}
|