Files
magpie/internal/davsync/skill_off_here_test.go
T
yetone fe05f0a721 library: a skill can be turned off on this computer alone, and a sync neither carries that nor turns it on
hanslin1666 on Discord (#general 1565016936344191096) wanted a library
skill off on one computer only ("只在这台电脑上关掉"). The only way to do
that was to untick its agents, and the ticks are what a sync carries, so
every other computer lost it too. A sync from a computer that had it on
also gave it back here.

Skill.OffHere (library.json "offHere", omitempty) turns a skill off on
this computer. No agent here gets it, whatever Agents says. Every reader
that gives a skill to an agent now asks gives():
- syncSkills, including the shared ~/.agents/skills check;
- desktop_skills.go;
- skill_changed.go;
- skill_edits.go;
- behind;
- Targets.
Agents stays what a sync carries, and the page shows those chips faded.

Collect never carries OffHere, so turning a skill off changes no part a
sync uploads. Put keeps this computer's OffHere for a skill it puts, so
a sync from a computer that has it on (or that gives it to more agents)
leaves it off here.

Where to turn it off:
- GUI: a power button on the skill row (aria-pressed). When off, an
  "Off on this computer" tag sits on its own line under the name; it
  says why and turns the skill on when clicked.
- CLI: `magpie library skill off-here|on-here <name>`.
- TUI: the row says the skill is off.
The strings are in en, zh, zh-TW, ja and de.

Existing users: nothing changes. An older library.json has no offHere,
so every skill stays on, and a skill that was never turned off writes no
key (TestSkillOffHereOldLibraryIsOn).

Not covered:
- a project's skills;
- a library skill whose own folder is inside ~/.agents/skills, which
  agents read in place;
- a skill another computer removes (the flag goes with it).
The skill's files in the library folder still update while it is off.

Docs: docs/subsystems/library.md and sync-backup.md.

Run at the parent aeacc6358 (rebased on origin/main), on mote:
- gofmt -l: clean.
- go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go
  vet ./...; go build ./...: ok.
- go test -tags nogui -timeout 45m ./internal/library ./internal/davsync
  ./internal/cli ./internal/tui ./internal/gui ./internal/backup: all ok.
- GUI, Chromium and WebKit:
  - skill-off-here: 22/22 (en, zh, zh-TW, ja, de at 860 and 400px).
  - i18n-keys, gui-ja, gui-de, gui-zh-tw, library-items, library-mcp-hub,
    library-all and library-toggle-status: green. The first try timed out
    in browserType.launch (mote at load ~60); run again one file at a
    time, they passed.
  - library-row-remove: Chromium red in the hooks part, 2 or 3 of 11
    each run. It is red the same way on origin/main aeacc6358, 3 of 3
    runs there too (#3099); this change doesn't touch hook rows.
- Before the rebase, at e5e2aa58e plus this change: the 98 GUI files
  that reach the library page (1350 tests). 68 reds, nearly all
  browserType.launch timeouts under load; run again one file at a time,
  all passed except mobile-web's "desktop matches origin/main"
  subtests. Those need .git, which mote-run doesn't copy.

Then rebased onto ec3dad75a, which changes no library file. At this
pushed head: go build ./..., go vet of library and gui, the gui Budget
tests, and i18n-keys plus skill-off-here in both engines (26/26), all
ok.

Broken by hand (restored afterwards):
- Put dropping OffHere:
  - TestSkillOffHereStaysHere:96 failed: "a sync from a computer with it
    on gave pdf to an agent here again".
  - TestSkillOffHereThroughSync:108 failed: "a sync from A, with pdf on,
    gave it to an agent on B again".
- gives() ignoring OffHere: TestSkillOffHereStaysHere:74 failed: "off
  here, claude still has pdf".
- The GUI test fails on the old code, which has no button.

Not tried:
- no real WebDAV or GitHub backend (the sync test uses the fake S3);
- no real app run with real agents;
- no Windows or Linux test runs (only GOOS=windows and GOOS=linux vet);
- not the whole GUI suite, only the 98 files that reach the library page,
  the i18n checks and gui-ja, gui-de and gui-zh-tw.
2026-10-29 04:23:33 +08:00

118 lines
3.0 KiB
Go

package davsync
import (
"context"
"os"
"path/filepath"
"slices"
"testing"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/library"
"github.com/yetone/magpie/internal/provider"
)
// A skill turned off on one computer (hanslin1666 on Discord) stays off
// there through a real sync, and on on the other: a sync carries neither
// the off nor turns it on again, while what each agent is ticked for
// still goes both ways.
func TestSkillOffHereThroughSync(t *testing.T) {
f, srv := newFakeS3(t)
cfg := f.config(srv)
cfg.Keys, cfg.Agents = false, false
a, b := newComputer(t), newComputer(t)
use := func(c computer) {
t.Helper()
c.use(t)
appdir.UseExecutable("")
provider.ForgetAccounts()
for _, p := range []string{".claude/settings.json", ".codex/config.toml"} {
p = filepath.Join(string(c), p)
os.MkdirAll(filepath.Dir(p), 0o755)
if _, err := os.Stat(p); err != nil {
os.WriteFile(p, nil, 0o644)
}
}
}
t.Cleanup(provider.ForgetAccounts)
now := func() {
t.Helper()
if err := Now(context.Background()); err != nil {
t.Fatal(err)
}
}
has := func(c computer, agent string) bool {
_, err := os.Lstat(filepath.Join(string(c), agent, "skills", "pdf"))
return err == nil
}
view := func() library.SkillView {
t.Helper()
v, err := library.Read(nil)
if err != nil {
t.Fatal(err)
}
for _, s := range v.Skills {
if s.Name == "pdf" {
return s
}
}
t.Fatalf("no pdf: %+v", v.Skills)
return library.SkillView{}
}
ok := func(_ *library.Result, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
// A has pdf on Claude Code, and syncs first
use(a)
src := filepath.Join(string(a), "src", "pdf")
os.MkdirAll(src, 0o755)
os.WriteFile(filepath.Join(src, "SKILL.md"), []byte("---\nname: pdf\ndescription: Read PDFs\n---\nRead them.\n"), 0o644)
ok(library.InstallSkills(filepath.Join(string(a), "src"), []string{"pdf"}, []string{"claude"}))
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
now()
// B takes it, then turns it off on B alone and syncs
use(b)
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
now()
if !has(b, ".claude") {
t.Fatal("B's first sync didn't give pdf to claude")
}
ok(library.SkillHere("pdf", false))
now()
if has(b, ".claude") || !view().OffHere {
t.Fatal("off on B, claude still has pdf")
}
// A still has it on, gives it to Codex too, and syncs
use(a)
now()
if v := view(); v.OffHere || !has(a, ".claude") {
t.Fatalf("B's off reached A: %+v", v)
}
ok(library.SkillAgents("pdf", []string{"claude", "codex"}))
now()
// B syncs: still off here, A's ticks taken in for when it is on again
use(b)
now()
if has(b, ".claude") || has(b, ".codex") {
t.Fatal("a sync from A, with pdf on, gave it to an agent on B again")
}
if v := view(); !v.OffHere || !slices.Equal(v.Agents, []string{"claude", "codex"}) {
t.Fatalf("B after the sync: %+v", v)
}
ok(library.SkillHere("pdf", true))
if !has(b, ".claude") || !has(b, ".codex") {
t.Fatal("on again on B, its agents didn't get pdf back")
}
}