library: an unreadable skill copy stays in place during sync (#1102)
An unreadable subfolder in an agent's copy of a skill hid edits from the timestamp scan, which ignored read and stat errors. So switching copy/link mode, refreshing the skill, or removing a duplicate copy could replace the copy and lose those edits. Shared-folder deduplication could remove SKILL.md before failing on the unreadable subtree. changedAt and copyIsFresh now return the scan error. The copy is kept in place for that sync (Library.blockedSkills) and reported as a Problem naming the path. This covers agent copies, shared-folder deduplication, project copies and Claude Desktop copies. Other agents still sync, and a later sync tries again once the copy can be read. Merged onto main. #1449 added two callers of changedAt in skill_changed.go: - libraryHash doesn't remember a hash for a folder it couldn't wholly read. - editState says unknown for an agent's copy it couldn't wholly read, so an update from GitHub keeps the library's version aside. TestEditStateUnknownForAnUnreadableCopy covers editState. docs/subsystems/library.md keeps both new rules. Run: - go test -tags nogui ./internal/library/ and ./... (see the PR), go vet for darwin, linux with nogui, and windows, and gofmt. - With changedAt ignoring walk errors again, the four ScanError and EditState tests go red. - With editState ignoring the error, the new test goes red (edited true, known true). - Checked with Unix permissions on macOS only, not as root and not on Windows.
This commit is contained in:
@@ -39,6 +39,7 @@ agents RTK, a CLI that cuts shell output down before the model reads it.
|
||||
- magpie takes away only what `Applied` or its mark says it wrote. A folder of the user's with a skill's name is never touched. The user's own fields in an MCP entry are kept.
|
||||
- An edit never silently loses. When an agent's copy of a skill and the library both changed, the newest edit wins, and the losing one is kept with the backups.
|
||||
- An update from GitHub never silently replaces a skill changed here. It is refused, or skipped in an update of several, until the user says to replace it, and the version replaced goes to the backups.
|
||||
- A copy's mark establishes that it is unchanged only after its whole folder was scanned. If a subtree or its metadata can't be read, the copy stays in place and the sync reports a `Problem`; it isn't replaced, relinked or removed. A later sync tries again once it can be read. The same scan failure stops replacing a project's or Claude Desktop's copy.
|
||||
- A failure for one agent is a `Problem` in the result. It doesn't stop the others.
|
||||
- A server an agent can't reach is not written there, and `mcpFile.supports` says why. For example, Claude Desktop takes no remote server, and Codex, Goose, dsh, Grok, Command Code and Zed take no SSE.
|
||||
- A server's headers (remote) or environment (command) can name a variable as `${NAME}` (#1250). `library.json` keeps the placeholder, and `refsOf` gives each MCP format the agent's own syntax, with the agent's source cited beside it: `${NAME}` for most, `{env:NAME}` for OpenCode and MiMo, `${env:NAME}` for Cursor, and Codex's own keys (`bearer_token_env_var`, `env_http_headers`, `env_vars`). Goose gets `${NAME}` plus `env_keys`. dsh (DeepSeek Harness) gets a `!!js` expression it evaluates as it loads the server (#1435): a whole `${NAME}` is `!!js process.env.NAME`, and a value mixing text and variables is the text as JSON string literals joined with `(process.env.NAME ?? "")`, so the user's text is never run as code and an unset variable inside it is empty. A whole variable that isn't set makes dsh leave the server out as invalid. magpie reads only those forms back as `${NAME}`; any other `!!js` keeps the entry the user's. An agent with no syntax (Claude Desktop, Antigravity, Kimi, Cline, ZCode, Zed, Devin, Alma, Pi with pi-mcp-extension) is never written the value: `refsProblem` makes `supports` refuse the server, a magpie entry already there is taken out, and `AgentView.NoEnvRefs` greys its chip with the reason. Codex and Goose's extra keys are magpie's only while the server names a variable there (`refKeys`), so the user's own `env_vars` or `env_keys` are kept. `CheckServers` expands the variable from magpie's own environment, and one that isn't set is `novar`, naming it. It starts a command server in home, with `PWD` set to it (not on Windows), as an agent started from home would, never in magpie's own working folder: a magpie started from the Windows Start menu has `C:\Windows\system32`, where a server that writes into `.` exits before it answers (#1727, `TestCheckStdio`).
|
||||
|
||||
@@ -276,15 +276,24 @@ func (l *Library) syncDesktopSkills(t *Target, res *Result) {
|
||||
p := filepath.Join(dir, s.Name)
|
||||
if ours(p, s.Name) {
|
||||
keep(s.Name)
|
||||
current := false
|
||||
if !linked(p) {
|
||||
var err error
|
||||
current, err = copyIsFresh(p, l.libHash(s.Name), false)
|
||||
if err != nil {
|
||||
res.fail(id, "skill:"+s.Name, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case linked(p) || !fresh(p, s.Name) && !desktopEdited(p):
|
||||
case linked(p) || !current && !desktopEdited(p):
|
||||
if err := desktopCopy(p, s.Name); err != nil {
|
||||
res.fail(id, "skill:"+s.Name, err)
|
||||
continue
|
||||
}
|
||||
put[s.Name] = true
|
||||
res.changed(id)
|
||||
case !fresh(p, s.Name):
|
||||
case !current:
|
||||
res.Problems = append(res.Problems, Problem{Agent: id, What: "skill:" + s.Name,
|
||||
Error: fmt.Sprintf("%s was changed in Claude Desktop, so the library's copy isn't put over it: delete it in Desktop to have the library's again", s.Name)})
|
||||
}
|
||||
|
||||
@@ -45,6 +45,37 @@ func desktopEntries(t *testing.T, root string) map[string]desktopEntry {
|
||||
return out
|
||||
}
|
||||
|
||||
func TestDesktopSkillScanErrorPreservesCopy(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
p, _ := desktopFiles(t, h)
|
||||
write(t, p, `{}`)
|
||||
root := desktopAccount(t, filepath.Dir(p), "org", "acct", `{"skills":[]}`)
|
||||
src := filepath.Join(h, "src/skills")
|
||||
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
|
||||
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude-desktop"}))
|
||||
cp := filepath.Join(root, "skills/pdf")
|
||||
write(t, filepath.Join(cp, "scripts/run.sh"), "echo mine\n")
|
||||
manifest := read(t, filepath.Join(root, desktopManifest))
|
||||
blocked := filepath.Join(cp, "scripts")
|
||||
blockSkillSubtree(t, h, blocked)
|
||||
r, err := Sync()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(r.Problems) != 1 || r.Problems[0].Agent != "claude-desktop" || !strings.Contains(r.Problems[0].Error, blocked) {
|
||||
t.Fatalf("unreadable Desktop copy wasn't reported: %+v", r.Problems)
|
||||
}
|
||||
if got := read(t, filepath.Join(root, desktopManifest)); got != manifest {
|
||||
t.Fatalf("unreadable Desktop copy's manifest changed: %s", got)
|
||||
}
|
||||
if err := os.Chmod(blocked, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := read(t, filepath.Join(cp, "scripts/run.sh")); got != "echo mine\n" {
|
||||
t.Fatalf("unreadable Desktop copy's edit was replaced: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// #638: the library gives Claude Desktop's Cowork skills: a copy in every
|
||||
// account's skills-plugin (no link: Cowork won't read a linked skill's
|
||||
// other files) and an entry in its manifest, which Desktop lists from.
|
||||
|
||||
@@ -57,6 +57,9 @@ type Library struct {
|
||||
// untouched are the agents' copies takeEdits found unchanged since
|
||||
// magpie made them, for the sync not to look through them again
|
||||
untouched map[string]bool
|
||||
// blockedSkills are copies takeEdits couldn't scan, kept in place for
|
||||
// this sync rather than replaced or removed. Keys are physical paths.
|
||||
blockedSkills map[string]bool
|
||||
}
|
||||
|
||||
// Instructions are one shared text, and for each agent whether it gets it
|
||||
|
||||
@@ -167,9 +167,18 @@ func (p *Project) place(e, name string) error {
|
||||
}
|
||||
link := linkEntry(fi)
|
||||
// a copy stands for a link on Windows without the right to make one
|
||||
if !p.Copy && link || !link && (p.Copy || runtime.GOOS == "windows") && fresh(abs, name) {
|
||||
if !p.Copy && link {
|
||||
return nil
|
||||
}
|
||||
if !link {
|
||||
current, err := copyIsFresh(abs, hashDir(realDir(skillDir(name))), false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if (p.Copy || runtime.GOOS == "windows") && current {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := p.mkdirs(filepath.Dir(abs)); err != nil {
|
||||
return err
|
||||
|
||||
@@ -119,6 +119,32 @@ func TestProjectSkillsCopied(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectSkillScanErrorPreservesCopy(t *testing.T) {
|
||||
h, proj := projectLib(t)
|
||||
ok(t)(ProjectCopy(proj, true))
|
||||
ok(t)(ProjectSkill(proj, "pdf", []string{"claude"}))
|
||||
p := filepath.Join(proj, ".claude/skills/pdf")
|
||||
// Copies from before marks kept hashes must reject incomplete scans too.
|
||||
write(t, filepath.Join(p, marker), "copied from "+skillDir("pdf")+"\n")
|
||||
write(t, filepath.Join(p, "scripts/run.sh"), "echo mine\n")
|
||||
write(t, filepath.Join(h, "src/pdf/scripts/run.sh"), "echo library update\n")
|
||||
blocked := filepath.Join(p, "scripts")
|
||||
blockSkillSubtree(t, proj, blocked)
|
||||
r, err := Sync()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(r.Problems) != 1 || r.Problems[0].Agent != proj || !strings.Contains(r.Problems[0].Error, blocked) {
|
||||
t.Fatalf("unreadable project copy wasn't reported: %+v", r.Problems)
|
||||
}
|
||||
if err := os.Chmod(blocked, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := read(t, filepath.Join(p, "scripts/run.sh")); got != "echo mine\n" {
|
||||
t.Fatalf("unreadable project copy's edit was replaced: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectRefusesForeignFolder(t *testing.T) {
|
||||
_, proj := projectLib(t)
|
||||
skill(t, filepath.Join(proj, ".claude/skills/pdf"), "pdf", "The project's own")
|
||||
|
||||
@@ -45,7 +45,11 @@ type editMemo struct {
|
||||
// aside what Finder keeps in it: "" when it can't be read.
|
||||
func libraryHash(name string) string {
|
||||
dir := skillDir(name)
|
||||
at := changedAt(dir)
|
||||
// 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()
|
||||
@@ -92,7 +96,16 @@ func (l *Library) editState(s *Skill, targets []*Target) (edited, known bool) {
|
||||
continue
|
||||
}
|
||||
made, ok := copiedAt(p)
|
||||
if !ok || !changedAt(p).After(made) {
|
||||
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) {
|
||||
|
||||
@@ -224,3 +224,34 @@ func TestCheckSkillsMarksItDone(t *testing.T) {
|
||||
t.Fatal("due right after a check")
|
||||
}
|
||||
}
|
||||
|
||||
// A copy with a folder that can't be read can't be told unchanged (#1102):
|
||||
// an edit may be in it. The skill's edit state is then unknown, so an
|
||||
// update keeps the library's version aside, and the sync keeps the copy.
|
||||
func TestEditStateUnknownForAnUnreadableCopy(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
version := "one"
|
||||
fakeSkillTarball(t, &version)
|
||||
ok(t)(InstallSkills("owner/repo", []string{"skills/pdf"}, []string{"claude"}))
|
||||
ok(t)(SetSkillHow("", HowCopy))
|
||||
p := filepath.Join(h, ".claude/skills/pdf")
|
||||
if linked(p) {
|
||||
t.Fatal("claude's pdf is still a link")
|
||||
}
|
||||
edit := filepath.Join(p, "scripts/run.sh")
|
||||
write(t, edit, "echo mine\n")
|
||||
later := time.Now().Add(2 * time.Second)
|
||||
os.Chtimes(edit, later, later)
|
||||
blockSkillSubtree(t, h, filepath.Join(p, "scripts"))
|
||||
l, err := load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := l.skill("pdf")
|
||||
if s == nil {
|
||||
t.Fatal("no pdf in the library")
|
||||
}
|
||||
if edited, known := l.editState(s, Targets()); edited || known {
|
||||
t.Fatalf("an unreadable copy's state: edited %v, known %v; want unknown", edited, known)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,116 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// blockSkillSubtree makes dir unreadable, and restores it wherever a broken
|
||||
// implementation moved it within root so TempDir can clean it up.
|
||||
func blockSkillSubtree(t *testing.T, root, dir string) {
|
||||
t.Helper()
|
||||
if runtime.GOOS == "windows" || os.Geteuid() == 0 {
|
||||
t.Skip("requires Unix read permissions without root")
|
||||
}
|
||||
if err := os.Chmod(dir, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
||||
if d != nil && d.IsDir() && d.Name() == filepath.Base(dir) {
|
||||
return os.Chmod(p, 0o755)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
})
|
||||
if _, err := os.ReadDir(dir); !os.IsPermission(err) {
|
||||
t.Fatalf("subtree is not unreadable: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// An unreadable subtree hides edits from the mark's timestamp check. Neither
|
||||
// relinking, refreshing nor removing a duplicate copy may take those edits away.
|
||||
func TestSkillCopyScanErrorPreservesEdits(t *testing.T) {
|
||||
for _, how := range []string{HowLink, HowCopy, "shared"} {
|
||||
t.Run(how, func(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
id := "claude"
|
||||
other := "codex"
|
||||
if how == "shared" {
|
||||
id = "codex"
|
||||
other = "claude"
|
||||
write(t, filepath.Join(h, ".kimi/config.toml"), "")
|
||||
}
|
||||
src := filepath.Join(h, "src/skills")
|
||||
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
|
||||
ok(t)(SetSkillHow("", HowCopy))
|
||||
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude", "codex"}))
|
||||
p := filepath.Join(h, "."+id, "skills/pdf")
|
||||
original := read(t, filepath.Join(p, "SKILL.md"))
|
||||
edited := filepath.Join(p, "scripts/run.sh")
|
||||
write(t, edited, "echo mine\n")
|
||||
made, marked := copiedAt(p)
|
||||
if !marked {
|
||||
t.Fatal("copy has no mark")
|
||||
}
|
||||
later := made.Add(time.Minute)
|
||||
if err := os.Chtimes(edited, later, later); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if how == HowCopy {
|
||||
write(t, filepath.Join(src, "pdf/scripts/run.sh"), "echo library update\n")
|
||||
}
|
||||
blocked := filepath.Join(p, "scripts")
|
||||
blockSkillSubtree(t, h, blocked)
|
||||
var res *Result
|
||||
var err error
|
||||
if how == "shared" {
|
||||
// Kimi gives it to ~/.agents/skills, so Codex no longer needs
|
||||
// its own copy. That copy still has edits to take first.
|
||||
res, err = SkillAgents("pdf", []string{"claude", "codex", "kimi"})
|
||||
} else {
|
||||
res, err = SetSkillHow(id, how)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if linked(p) {
|
||||
t.Fatal("unreadable copy was replaced by a link")
|
||||
}
|
||||
if got := read(t, filepath.Join(p, "SKILL.md")); got != original {
|
||||
t.Fatalf("unreadable copy was partly removed: %q", got)
|
||||
}
|
||||
if len(res.Problems) != 1 || res.Problems[0].Agent != id || res.Problems[0].What != "skill:pdf" || !strings.Contains(res.Problems[0].Error, blocked) {
|
||||
t.Fatalf("unreadable copy wasn't reported: %+v", res.Problems)
|
||||
}
|
||||
if how == HowCopy && read(t, filepath.Join(h, ".codex/skills/pdf/scripts/run.sh")) != "echo library update\n" {
|
||||
t.Error("one unreadable copy stopped another agent's update")
|
||||
}
|
||||
if err := os.Chmod(blocked, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := read(t, edited); got != "echo mine\n" {
|
||||
t.Fatalf("unreadable copy's edit was replaced: %q", got)
|
||||
}
|
||||
ok(t)(Sync())
|
||||
if how == "shared" {
|
||||
edited = filepath.Join(sharedSkillsDir(), "pdf/scripts/run.sh")
|
||||
}
|
||||
for _, f := range []string{edited, filepath.Join(src, "pdf/scripts/run.sh"), filepath.Join(h, "."+other, "skills/pdf/scripts/run.sh")} {
|
||||
if got := read(t, f); got != "echo mine\n" {
|
||||
t.Errorf("edit wasn't kept after permissions returned at %s: %q", f, got)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// #1031: switching a group of skills on for one agent made the sync read
|
||||
// every file of every copy every agent has, which over a WSL distro's
|
||||
// share took 10 seconds and more. A copy unchanged since magpie made it is
|
||||
@@ -59,6 +161,14 @@ func TestSyncDoesNotReadUnchangedCopies(t *testing.T) {
|
||||
// copied or is one from before marks did (no hash in it).
|
||||
func TestCopyFreshByMark(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
fresh := func(p string) bool {
|
||||
t.Helper()
|
||||
current, err := copyIsFresh(p, hashDir(realDir(skillDir("pdf"))), false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return current
|
||||
}
|
||||
src := filepath.Join(h, "src/skills")
|
||||
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
|
||||
ok(t)(SetSkillHow("", HowCopy))
|
||||
@@ -67,12 +177,12 @@ func TestCopyFreshByMark(t *testing.T) {
|
||||
if _, ok := markedHash(cl); !ok {
|
||||
t.Fatal("the copy's mark says no hash")
|
||||
}
|
||||
if !fresh(cl, "pdf") || !fresh(cx, "pdf") {
|
||||
if !fresh(cl) || !fresh(cx) {
|
||||
t.Fatal("a copy just made isn't fresh")
|
||||
}
|
||||
// a mark from before: no hash
|
||||
write(t, filepath.Join(cx, marker), "copied from "+skillDir("pdf")+" by magpie, and copied again when it changes\n")
|
||||
if !fresh(cx, "pdf") {
|
||||
if !fresh(cx) {
|
||||
t.Error("an old mark's copy, the same as the library's, isn't fresh")
|
||||
}
|
||||
|
||||
@@ -81,7 +191,7 @@ func TestCopyFreshByMark(t *testing.T) {
|
||||
write(t, lib, "echo new\n")
|
||||
later := time.Now().Add(time.Minute)
|
||||
os.Chtimes(lib, later, later)
|
||||
if fresh(cl, "pdf") || fresh(cx, "pdf") {
|
||||
if fresh(cl) || fresh(cx) {
|
||||
t.Fatal("copies of an older skill are fresh")
|
||||
}
|
||||
ok(t)(Sync())
|
||||
@@ -96,7 +206,7 @@ func TestCopyFreshByMark(t *testing.T) {
|
||||
write(t, edited, "echo mine\n")
|
||||
later = time.Now().Add(2 * time.Minute)
|
||||
os.Chtimes(edited, later, later)
|
||||
if fresh(cl, "pdf") {
|
||||
if fresh(cl) {
|
||||
t.Error("an edited copy is fresh")
|
||||
}
|
||||
ok(t)(Sync())
|
||||
|
||||
@@ -25,6 +25,13 @@ import (
|
||||
// takeEdits takes into the library the newest edit made in an agent's copy
|
||||
// of each of its skills, before the sync gives the library's to the agents.
|
||||
func (l *Library) takeEdits(targets []*Target, res *Result) {
|
||||
failed := func(agent, name, p string, err error) {
|
||||
res.fail(agent, "skill:"+name, err)
|
||||
if l.blockedSkills == nil {
|
||||
l.blockedSkills = map[string]bool{}
|
||||
}
|
||||
l.blockedSkills[realDir(p)] = true
|
||||
}
|
||||
for _, s := range l.Skills {
|
||||
lib := realDir(skillDir(s.Name))
|
||||
if l.libHash(s.Name) == "" {
|
||||
@@ -49,7 +56,11 @@ func (l *Library) takeEdits(targets []*Target, res *Result) {
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
at := changedAt(p)
|
||||
at, err := changedAt(p)
|
||||
if err != nil {
|
||||
failed(t.Agent.ID, s.Name, p, err)
|
||||
continue
|
||||
}
|
||||
if !at.After(made) {
|
||||
if l.untouched == nil {
|
||||
l.untouched = map[string]bool{}
|
||||
@@ -67,8 +78,15 @@ func (l *Library) takeEdits(targets []*Target, res *Result) {
|
||||
}
|
||||
// the newest edit, unless the library changed after it
|
||||
slices.SortStableFunc(edits, func(a, b edit) int { return b.at.Compare(a.at) })
|
||||
at, err := changedAt(lib)
|
||||
if err != nil {
|
||||
for _, e := range edits {
|
||||
failed(e.agent, s.Name, e.path, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
won := -1
|
||||
if edits[0].at.After(changedAt(lib)) {
|
||||
if edits[0].at.After(at) {
|
||||
won = 0
|
||||
}
|
||||
if won == 0 {
|
||||
@@ -120,12 +138,12 @@ func copiedAt(p string) (time.Time, bool) {
|
||||
// changedAt is when anything in the folder last changed: the newest time
|
||||
// of what is in it, a file or a folder (one taken away changes its
|
||||
// folder's), leaving aside magpie's mark and what Finder or version
|
||||
// control keep in it.
|
||||
func changedAt(dir string) time.Time {
|
||||
// control keep in it. An incomplete walk can't establish when it changed.
|
||||
func changedAt(dir string) (time.Time, error) {
|
||||
var at time.Time
|
||||
filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
|
||||
err := filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
if p != dir && skipInSkill(d.Name()) {
|
||||
if d.IsDir() {
|
||||
@@ -133,12 +151,16 @@ func changedAt(dir string) time.Time {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if fi, err := d.Info(); err == nil && fi.ModTime().After(at) {
|
||||
fi, err := d.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if fi.ModTime().After(at) {
|
||||
at = fi.ModTime()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return at
|
||||
return at, err
|
||||
}
|
||||
|
||||
func skipInSkill(name string) bool {
|
||||
|
||||
@@ -87,13 +87,16 @@ func SetSkillHow(agent, how string) (*Result, error) {
|
||||
// which leaves the copy as it is. A copy that differs from the library's
|
||||
// skill (edited in the agent) is kept aside rather than lost.
|
||||
func relink(agent, p, name string) (bool, error) {
|
||||
current, err := copyIsFresh(p, hashDir(realDir(skillDir(name))), false)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
next := filepath.Join(filepath.Dir(p), "."+name+".magpie-link")
|
||||
os.Remove(next)
|
||||
if err := dirLink(skillDir(name), next); err != nil {
|
||||
return false, nil
|
||||
}
|
||||
var err error
|
||||
if fresh(p, name) {
|
||||
if current {
|
||||
old := oldPath(p, name)
|
||||
if err = os.Rename(p, old); err == nil {
|
||||
// best effort: what's left is tried again on the next sync
|
||||
@@ -129,7 +132,12 @@ func (l *Library) behind(targets []*Target) map[string][]string {
|
||||
if !ours(p, s.Name) || linked(p) {
|
||||
continue
|
||||
}
|
||||
if h := l.libHash(s.Name); h != "" && !copyIsFresh(p, h) && !slices.Contains(out[s.Name], t.Agent.ID) {
|
||||
h := l.libHash(s.Name)
|
||||
if h == "" {
|
||||
continue
|
||||
}
|
||||
current, _ := copyIsFresh(p, h, false)
|
||||
if !current && !slices.Contains(out[s.Name], t.Agent.ID) {
|
||||
out[s.Name] = append(out[s.Name], t.Agent.ID)
|
||||
}
|
||||
}
|
||||
|
||||
+40
-25
@@ -283,37 +283,32 @@ func isDigits(s string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// fresh is whether both folders could be read and the copy at p holds
|
||||
// what the library's skill does. Failed hashes don't prove it can be discarded.
|
||||
func fresh(p, name string) bool {
|
||||
return copyIsFresh(p, hashDir(realDir(skillDir(name))))
|
||||
}
|
||||
|
||||
// copyIsFresh is fresh against the library skill's hash, lib. A copy whose
|
||||
// mark says it was made of lib, with nothing in it changed since the mark,
|
||||
// is that without its files being read: a sync used to read every file of
|
||||
// every copy in every agent, which over a WSL distro's share took a group's
|
||||
// switch 10 seconds and more (#1031). Any other copy is read and hashed.
|
||||
func copyIsFresh(p, lib string) bool {
|
||||
return copyIsFreshAs(p, lib, false)
|
||||
}
|
||||
|
||||
// copyIsFreshAs is copyIsFresh for a copy known to be unchanged since its
|
||||
// mark (untouched), which then isn't walked again.
|
||||
func copyIsFreshAs(p, lib string, untouched bool) bool {
|
||||
// copyIsFresh is whether the copy at p holds the skill hashed as lib.
|
||||
// A mark matching lib, with nothing changed since it was made, establishes
|
||||
// that without reading the files (#1031). A copy takeEdits found unchanged
|
||||
// (untouched) isn't walked again. An incomplete scan returns an error.
|
||||
func copyIsFresh(p, lib string, untouched bool) (bool, error) {
|
||||
var at time.Time
|
||||
if !untouched {
|
||||
var err error
|
||||
at, err = changedAt(p)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
if lib == "" {
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
if h, ok := markedHash(p); ok {
|
||||
if untouched {
|
||||
return h == lib
|
||||
return h == lib, nil
|
||||
}
|
||||
if made, ok := copiedAt(p); ok && !changedAt(p).After(made) {
|
||||
return h == lib
|
||||
if made, ok := copiedAt(p); ok && !at.After(made) {
|
||||
return h == lib, nil
|
||||
}
|
||||
}
|
||||
h := hashDir(p)
|
||||
return h != "" && h == lib
|
||||
return h != "" && h == lib, nil
|
||||
}
|
||||
|
||||
// libHash is hashDir of the library's skill by that name, read once for
|
||||
@@ -502,6 +497,10 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) {
|
||||
for _, name := range a.Skills {
|
||||
s := l.skill(name)
|
||||
p := filepath.Join(t.Skills, name)
|
||||
if l.blockedSkills[realDir(p)] {
|
||||
mine = append(mine, name)
|
||||
continue
|
||||
}
|
||||
if wanted(s) && !inShared(s) || !ours(p, name) {
|
||||
continue
|
||||
}
|
||||
@@ -518,6 +517,12 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) {
|
||||
continue
|
||||
}
|
||||
p := filepath.Join(t.Skills, s.Name)
|
||||
if l.blockedSkills[realDir(p)] {
|
||||
if !slices.Contains(mine, s.Name) {
|
||||
mine = append(mine, s.Name)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if inShared(s) {
|
||||
// a copy of its own of the very same files would be the skill
|
||||
// twice as much as a link: kept aside, as it would be for one
|
||||
@@ -533,6 +538,7 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) {
|
||||
// the folder the library's skill links to is there already: one
|
||||
// brought in from ~/.agents/skills, which stays where it is
|
||||
if ours(p, s.Name) || realDir(p) == realDir(skillDir(s.Name)) {
|
||||
mine = append(mine, s.Name)
|
||||
// an old copy a previous swap couldn't remove is tried again
|
||||
if ours(p, s.Name) {
|
||||
dropOld(p, s.Name)
|
||||
@@ -543,6 +549,7 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) {
|
||||
if ours(p, s.Name) && !cp && !linked(p) {
|
||||
if ok, err := relink(id, p, s.Name); err != nil {
|
||||
res.fail(id, "skill:"+s.Name, err)
|
||||
continue
|
||||
} else if ok {
|
||||
res.changed(id)
|
||||
}
|
||||
@@ -550,14 +557,22 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) {
|
||||
// a copy is made again once the library's skill has changed: in
|
||||
// an agent that takes copies, and where magpie couldn't link
|
||||
// (Windows without the right to) and left a copy instead
|
||||
if ours(p, s.Name) && ((cp && linked(p)) || (!linked(p) && l.libHash(s.Name) != "" && !copyIsFreshAs(p, l.libHash(s.Name), l.untouched[p]))) {
|
||||
again := cp && linked(p)
|
||||
if ours(p, s.Name) && !linked(p) && l.libHash(s.Name) != "" {
|
||||
current, err := copyIsFresh(p, l.libHash(s.Name), l.untouched[p])
|
||||
if err != nil {
|
||||
res.fail(id, "skill:"+s.Name, err)
|
||||
continue
|
||||
}
|
||||
again = !current
|
||||
}
|
||||
if ours(p, s.Name) && again {
|
||||
if err := copyIn(p, s.Name); err != nil {
|
||||
res.fail(id, "skill:"+s.Name, err)
|
||||
} else {
|
||||
res.changed(id)
|
||||
}
|
||||
}
|
||||
mine = append(mine, s.Name)
|
||||
continue
|
||||
}
|
||||
if _, err := os.Lstat(p); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user