Files
magpie/internal/library/skill_how_test.go
T
yetone ea3babac89 library: TestSkillRelinkKeepsUnreadableCopy holds under load, where the library skill's hash is remembered past its chmod
Red in a full run at 63be47717 under load avg ~280 (8.46s):
  skill_how_test.go:163: problems: [{Agent:claude What:skill:pdf
  Error:open .../src/skills/pdf/blocked.txt: permission denied}]
It passed alone 3 of 3. The cause is the hash memo (treememo.go,
3a80ce7fa, #541): a folder whose newest file is older than settleFor
(3s) has its hash kept against a stamp of place, type, size and time,
not mode, by design (TestPageReadDoesntReadUnchangedSkillsAgain relies on
a chmod not re-reading). On a loaded run more than 3s passed between
writing src/skills/pdf and SetSkillHow, so relink's
hashDir(realDir(skillDir)) under /private/var hit the remembered hash,
while the test's own "unreadable file prevents hashing" check hashed
the /var spelling, never memoised, and read "".

Injected: age(src) and a hash of realDir(skillDir("pdf")) before the
chmod reproduces the exact red on every run. The test now ages and
hashes the library skill first (the loaded case, always), rewrites
blocked.txt's time as it makes it unreadable, and checks hashing fails
at realDir, the path relink reads. Without the time rewrite it goes red
at the precondition ("the unreadable file didn't prevent hashing"); with
it, -count=3 passes. No product code changes.

Run: go test ./internal/library -count=3 -run TestSkillRelinkKeepsUnreadableCopy
ok; go test -timeout 45m ./internal/library ok (1172s, load ~240);
gofmt -l clean; go vet ./internal/library darwin/linux/windows ok.
Full suite: set -o pipefail; go test -timeout 45m ./... at 63be47717
plus the min bump, sandbox HOME, load avg 240-297 from other sessions'
test binaries: 63 packages ok, internal/provider (changed) 1210s; red
were internal/library (TestSkillRelinkKeepsUnreadableCopy, fixed in
the library commit, package ok after in 1172s) and internal/agent and
internal/gateway, which hit the 45m timeout in that run and in two
reruns each, with no "--- FAIL" in any of them; each timeout's stack
shows one test a few seconds in (TestWSLTargets, TestReasonixEffortAndSync,
TestCodexSwitchOnlyKeepsCodexOffTheGateway; TestLoopGuardLeavesOrdinary-
RepliesAlone, TestReplyNamesMember), so slow under that load, not hung.
One gateway rerun didn't build: a shared GOCACHE entry was deleted
under it; the next used its own cache. Neither package imports the
changed test file; gateway imports provider for the string only. The
tag waits for CI's attempt-1 Test run.
2026-10-16 18:38:31 +08:00

189 lines
6.1 KiB
Go

package library
import (
"io/fs"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
"time"
)
// #896: skills are given as links or as copies, for every agent or one;
// a change of way makes each again, a copy behind the library's is said
// and made again by a sync, and the user's own folders are never touched.
func TestSkillHow(t *testing.T) {
h := sandbox(t)
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
// the user's own skill, which the library has nothing to do with
skill(t, filepath.Join(h, ".claude/skills/mine"), "mine", "Mine")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude", "codex"}))
cl, cx := filepath.Join(h, ".claude/skills/pdf"), filepath.Join(h, ".codex/skills/pdf")
if !isLink(t, cl) || !isLink(t, cx) {
t.Fatal("not linked at first")
}
if _, err := SetSkillHow("", "both"); err == nil {
t.Error("an unknown way was taken")
}
if _, err := SetSkillHow("", ""); err == nil {
t.Error("the library was given no way")
}
// copies for every agent
ok(t)(SetSkillHow("", HowCopy))
for _, p := range []string{cl, cx} {
if isLink(t, p) {
t.Fatalf("%s is still a link", p)
}
if read(t, filepath.Join(p, "SKILL.md")) != read(t, filepath.Join(src, "pdf/SKILL.md")) {
t.Errorf("%s: not a copy of the skill", p)
}
if !ours(p, "pdf") {
t.Errorf("%s: not marked magpie's", p)
}
}
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
if !v.CopySkills {
t.Error("the page doesn't say copies")
}
for _, a := range v.Agents {
if (a.ID == "claude" || a.ID == "codex") && (a.How != HowCopy || a.HowOwn) {
t.Errorf("%s: how %q own %v", a.ID, a.How, a.HowOwn)
}
}
if len(v.Skills) != 1 || v.Skills[0].Behind != nil {
t.Fatalf("behind at once: %+v", v.Skills)
}
// the library's skill changes (a folder edited by hand): the copies
// are behind till a sync makes them again
write(t, filepath.Join(src, "pdf/SKILL.md"), "---\nname: pdf\ndescription: Changed\n---\n")
v, _ = Read(nil)
if b := v.Skills[0].Behind; !slices.Equal(b, []string{"claude", "codex"}) {
t.Fatalf("behind: %v", b)
}
ok(t)(Sync())
if !strings.Contains(read(t, filepath.Join(cl, "SKILL.md")), "Changed") {
t.Error("the sync didn't copy it again")
}
if v, _ = Read(nil); v.Skills[0].Behind != nil {
t.Errorf("still behind: %v", v.Skills[0].Behind)
}
// one agent its own way
ok(t)(SetSkillHow("codex", HowLink))
if !isLink(t, cx) || isLink(t, cl) {
t.Fatal("codex isn't a link again, or claude's copy went")
}
v, _ = Read(nil)
for _, a := range v.Agents {
if a.ID == "codex" && (a.How != HowLink || !a.HowOwn) {
t.Errorf("codex: how %q own %v", a.How, a.HowOwn)
}
}
if _, err := SetSkillHow("no such/agent", HowCopy); err == nil {
t.Error("a malformed agent id was given a way")
}
// an edit made in claude's copy isn't lost when it is a link again:
// the library takes it (takeEdits), and the link shows it
time.Sleep(20 * time.Millisecond)
write(t, filepath.Join(cl, "notes.md"), "my notes\n")
ok(t)(SetSkillHow("", HowLink))
if !isLink(t, cl) {
t.Fatal("claude isn't a link again")
}
if read(t, filepath.Join(src, "pdf/notes.md")) != "my notes\n" || read(t, filepath.Join(cl, "notes.md")) != "my notes\n" {
t.Error("the edit in the copy wasn't taken into the library")
}
// codex goes the library's way again
ok(t)(SetSkillHow("codex", HowCopy))
ok(t)(SetSkillHow("codex", ""))
if !isLink(t, cx) {
t.Error("codex didn't go back to the library's way")
}
// a skill of the user's by the library's name stands, whichever way
ok(t)(SkillAgents("pdf", []string{"codex"}))
skill(t, cl, "pdf", "The user's own")
for _, how := range []string{HowCopy, HowLink} {
ok(t)(SetSkillHow("", how))
if isLink(t, cl) || ours(cl, "pdf") || !strings.Contains(read(t, filepath.Join(cl, "SKILL.md")), "The user's own") {
t.Fatalf("%s: the user's own folder was touched", how)
}
}
if !strings.Contains(read(t, filepath.Join(h, ".claude/skills/mine/SKILL.md")), "Mine") {
t.Error("the user's own skill was touched")
}
// a copy is taken away when the skill is
ok(t)(SetSkillHow("", HowCopy))
if isLink(t, cx) {
t.Fatal("codex isn't a copy")
}
ok(t)(RemoveSkill("pdf"))
if _, err := os.Lstat(cx); !os.IsNotExist(err) {
t.Error("codex's copy is still there")
}
if _, err := os.Stat(filepath.Join(cl, "SKILL.md")); err != nil {
t.Error("the user's own pdf went with the library's")
}
}
// Two failed hashes don't prove a copy is unchanged: switching back to a
// link must keep the agent's edits even when neither folder can be read.
func TestSkillRelinkKeepsUnreadableCopy(t *testing.T) {
if runtime.GOOS == "windows" || os.Geteuid() == 0 {
t.Skip("requires Unix read permissions without root")
}
h := sandbox(t)
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
write(t, filepath.Join(src, "pdf/blocked.txt"), "unchanged\n")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude"}))
ok(t)(SetSkillHow("", HowCopy))
p := filepath.Join(h, ".claude/skills/pdf")
write(t, filepath.Join(p, "notes.md"), "my notes\n")
// The library's skill written long ago and hashed, as a run under load
// finds it past the memo's settleFor: a mode alone doesn't change what
// the memo stamps, so blocked.txt is written again as it's made
// unreadable, as an edit that leaves it so would.
age(t, src)
hashDir(realDir(skillDir("pdf")))
for _, dir := range []string{filepath.Join(src, "pdf"), p} {
f := filepath.Join(dir, "blocked.txt")
if err := os.Chmod(f, 0); err != nil {
t.Fatal(err)
}
now := time.Now()
if err := os.Chtimes(f, now, now); err != nil {
t.Fatal(err)
}
if hashDir(realDir(dir)) != "" {
t.Fatal("the unreadable file didn't prevent hashing", dir)
}
}
ok(t)(SetSkillHow("", HowLink))
if !isLink(t, p) {
t.Fatal("claude's copy wasn't replaced by a link")
}
var kept []string
filepath.WalkDir(BackupDir(), func(p string, d fs.DirEntry, err error) error {
if err == nil && d.Name() == "notes.md" {
kept = append(kept, p)
}
return nil
})
if len(kept) != 1 || read(t, kept[0]) != "my notes\n" {
t.Fatalf("the unreadable copy's edits weren't kept: %v", kept)
}
}