Files
magpie/internal/library/skill_changed.go
T
yetone 9fd80b2128 library: skills install from any git repository, GitLab and a company's own among them (sunset on Discord)
ProbeSkills took only a GitHub source or a folder. It now also takes any
git repository's address: https/http on gitlab.com or a self-hosted
server (any depth of groups), a GitLab or Gitea page of a folder
(/-/tree/<ref>/<path>, /-/blob/..., /src/branch/...), ssh://,
git@host:group/repo.git, a bare host.name/group/repo, and file://.
Such a source is kind "git" (gitSource, skillgit.go).

It is fetched with the user's own git (proc.FindTool), so their
credential helpers and SSH keys reach an intranet host: git init, then
fetch --depth 1 --no-tags -- <url> <ref|HEAD>, checkout FETCH_HEAD into
a magpie-skill-* folder, .git removed; the same keepProbe/maxFetch path
GitHub's tarball takes.

What the user typed is data: an address starting with - and any
transport:: address (ext::, fd::, file::) are refused before git runs;
the address follows --; git runs with protocol.ext.allow=never and
core.symlinks=false, no shell, GIT_TERMINAL_PROMPT=0, and ssh -o
BatchMode=yes unless the user set their own SSH command (GIT_SSH,
GIT_SSH_COMMAND, core.sshCommand), so a sign-in it can't make fails
instead of waiting. An address with a password or token in it is
refused: library.json is shown and backed up. Git missing, a sign-in
refused, an unknown host key, a missing branch and an unreachable host
each say so plainly (gitError).

Updates: Commit keeps the repository head the skill was fetched at. A
check runs git ls-remote; another head is fetched (once per repository
and ref) and the skill's folder hashed, so a commit elsewhere in the
repository leaves it current and moves Commit on. UpdateSkill and the
edited-skill refusal work as for GitHub; EditedError and the page's
Changed here / Update / replace question name the repository instead of
GitHub. findNew, Group again, icons and the market stay GitHub's only
(documented).

Page, TUI and docs: the add box's placeholder in en/zh/zh-TW/ja/de
(zh: owner/repo、GitHub 或 GitLab 链接、任意 git 仓库地址,或本机上的文件夹),
a git skill's source shown as a link (https) or text (SSH), its own
Update and Check for updates; the TUI's prompt; library.md's Skills and
Skill updates rows. GIT_SSH/GIT_SSH_COMMAND are listed in agentenv's
notAnAgent (TestFolderVarsAreListed).

Tests (fail on the old code: gitSource/gitPath don't exist there, and
skill-git-source.test.cjs fails on the old assets' placeholder, checked
by stashing client.js/i18n.js): TestGitSourceReads,
TestInstallFromGitRepo (a local bare repo over file://: probe, install,
check finds an update, update, a commit elsewhere stays current, an
edit here is refused naming the repo), TestGitTreeURLInstallsFolder,
TestGitMissingSaysSo, TestGitSignInRefusedSaysSo (a local httptest 401,
GIT_CONFIG_NOSYSTEM so no Keychain), TestGitRefusesOptionsAndTransports,
TestGitErrorsSaid. No test reaches the network.

Run (worktree at origin/main 224ea7d5b, machine load average ~120-145
from other sessions):
- gofmt -l: clean. go vet darwin, GOOS=linux -tags nogui, GOOS=windows:
  clean.
- set -o pipefail; go test -timeout 45m ./...: 63 packages ok; one red,
  internal/agent TestFolderVarsAreListed (GIT_SSH vars), fixed above
  and rerun alone: ok.
- After later edits, rerun ./internal/library ./internal/cli
  ./internal/tui ok; internal/gui TestMainWindowCorners red once at
  load 144 (76.5s, AppKit window test this change doesn't touch),
  passed alone (28.6s); internal/agent timed out at 45m in that
  combined rerun at load ~140 (it ran 962s in the full run) and passed
  alone (1808s, load ~100).
- GUI: the 88 test files that load the library page, chromium and
  webkit: 1420/1420 pass; skill-git-source + skill-update-edited rerun
  after the last client.js edit: 20/20.
- Real use in a sandbox HOME (GIT_CONFIG_NOSYSTEM=1): a local bare repo
  over file:// probed, installed, checked current, a new commit found as
  update, updated to v2; ext:: and --upload-pack refused with nothing
  run; https://gitlab.com/gitlab-org/ai/skills probed (52 skills), its
  /-/tree/main/skills/glab installed and checked current; a private
  gitlab.com address said "git couldn't sign in to gitlab.com ...".
- Not tried: a real self-hosted GitLab with credentials, ssh:// against
  a real host, Windows.
2026-10-19 18:16:57 +08:00

141 lines
4.0 KiB
Go

package library
// A skill from GitHub can be changed on this machine after it was
// installed: the user edits it in the library's folder, an agent it is
// linked into edits it there, or an agent's copy is edited and taken in
// (skill_edits.go). An update from GitHub takes its files in place of the
// library's, so it would undo such a change without a word (#1449). So
// the library tells a changed skill by the hash of its files as fetched
// (Skill.Hash): the page says it was changed here, an update of it alone
// asks first, an update of many leaves it as it is, and the version an
// update replaces is kept with the backups unless it is known to be
// GitHub's own.
import (
"fmt"
"os"
"path/filepath"
"slices"
"sync"
"time"
)
// EditedError is an update refused because the skill was changed here
// since it was fetched from GitHub, or From, the git repository it came
// from.
type EditedError struct{ Name, From string }
func (e *EditedError) Error() string {
from := e.From
if from == "" {
from = "GitHub"
}
return fmt.Sprintf("%s was changed here since it was fetched from %s; updating it would replace those changes", e.Name, from)
}
// editedError is the refusal of an update of s, naming where it came from.
func editedError(s *Skill) *EditedError {
e := &EditedError{Name: s.Name}
if s.Source != nil && s.Source.Kind == "git" {
e.From = s.Source.Repo
}
return e
}
// edits remembers, for each skill's library folder, its hash as of when
// anything in it last changed, so a page read doesn't read every skill's
// files again.
var edits = struct {
sync.Mutex
m map[string]editMemo
}{m: map[string]editMemo{}}
type editMemo struct {
at time.Time
hash string
}
// libraryHash is the hash of the library's folder of a skill, leaving
// aside what Finder keeps in it: "" when it can't be read.
func libraryHash(name string) string {
dir := skillDir(name)
// a folder not wholly read has no time to remember its hash by
at, err := changedAt(dir)
if err != nil {
at = time.Time{}
}
edits.Lock()
m, ok := edits.m[dir]
edits.Unlock()
if ok && !at.IsZero() && m.at.Equal(at) {
return m.hash
}
h := hashFiles(dir, true)
if h != "" && !at.IsZero() {
edits.Lock()
edits.m[dir] = editMemo{at: at, hash: h}
edits.Unlock()
}
return h
}
// editState tells whether a skill from GitHub was changed here since it
// was fetched: its library folder no longer holds the files fetched, or
// an agent's copy of it was edited since magpie made it and isn't taken
// in yet. known is false when that can't be told: magpie didn't keep the
// hash of what it fetched (installed before it did), or the folder can't
// be read.
func (l *Library) editState(s *Skill, targets []*Target) (edited, known bool) {
if !s.Source.remote() || s.Hash == "" {
return false, false
}
lib := skillDir(s.Name)
if linked(lib) {
return false, false
}
h := libraryHash(s.Name)
if h == "" {
return false, false
}
// the fetched files may hold a .DS_Store of their own
if h != s.Hash && hashDir(lib) != s.Hash {
return true, true
}
for _, t := range targets {
if t.Skills == "" || t.Desktop != nil || !slices.Contains(s.Agents, t.Agent.ID) {
continue
}
p := filepath.Join(t.Skills, s.Name)
if !ours(p, s.Name) || linked(p) {
continue
}
made, ok := copiedAt(p)
if !ok {
continue
}
// a copy not wholly read can't be told unchanged; the sync keeps
// it in place, and an update keeps the library's version aside
at, err := changedAt(p)
if err != nil {
return false, false
}
if !at.After(made) {
continue
}
if !sameTree(p, lib) {
return true, true
}
}
return false, true
}
// keepReplaced moves the library's version of a skill an update replaces
// to the backups, where it is found as an edit taken over is.
func keepReplaced(name, old string) (string, error) {
dst := filepath.Join(BackupDir(), time.Now().Format("2006-01-02_15-04-05.000"), "library", "skills", name)
if err := os.MkdirAll(filepath.Dir(dst), 0o700); err != nil {
return "", err
}
return dst, move(old, dst)
}