Files
magpie/internal/library/skill_router.go
T
yetone 2a64b4bcb7 library: a skill router, one magpie-skills skill listing an agent's skills
Like the MCP router, for skills. With the Skills tab's new "Skill
router" switch on (off by default), each agent's skills folder gets
one skill of magpie's, magpie-skills, in place of a link of each skill
ticked for it. Its SKILL.md lists them, each with its description and
the path of its SKILL.md, and the agent reads that file when a task
fits. An agent then carries one skill's description in its prompt
however many the Library gives it.

- The entry is a link to <settings dir>/skill-router/<hash of the
  folder>/, which holds SKILL.md and a hidden .skills/<name> link of
  each listed skill (hidden, so Codex, OpenCode and Pi don't find a
  skill twice). Its description stays within 1024 bytes (Codex's
  limit), cut on a whole character.
- Every skill is listed unless it is kept direct (a button on the
  skill, or `magpie library skill router <name> direct`; carried by a
  sync), is only for the user to type (disable-model-invocation), is
  in ~/.agents/skills, is blocked, or the agent has its own by that
  name. Those keep their own links.
- An agent whose folder can't take it keeps every link, and the page
  says why: given copies, a live agent picking its own, the shared
  ~/.agents/skills, a Library skill called magpie-skills, or a
  magpie-skills of the user's (never written over).
- Claude Code 2.1.316 asks before reading outside its project, its
  skills folder too, so magpie adds Read rules for the entry and the
  folders behind it to permissions.allow, records them, and takes out
  only its own when they go (as permissions.deny does).
- The switch is this computer's alone; `magpie library skill router
  on|off` sets it from the CLI.
- Sessions count a routed read of .skills/<name>/SKILL.md as <name>
  (Claude Code, Alma, Droid), and calling up magpie-skills itself as no
  skill, so a use counts once.
- docs/subsystems/skill-router.md, with a row in the index.

Run at this tree (rebased on origin/main 8f74442e4), on mote unless
named:
- go test -tags nogui ./internal/library ./internal/sessions -count=1: ok
- go test -tags nogui ./internal/cli ./internal/gui: ok at bca122bf9;
  ./internal/gui again at 8f74442e4 (app.js changed there): ok
- node --test of every library-*, *skill*, i18n* and gui-* GUI file
  (96 files), in the Linux VM (wsl-run): mote's browsers didn't start
  while another session's simulators ran. 1678 pass, 11 fail at
  bca122bf9, every red also red on origin/main and filed: library-all
  WebKit (#4967), skill-where WebKit 400px (#5154), i18n-keys' plugin
  words (below), and library-own-folder under load, fixed here. The
  files this change touches, add-partners-shine and i18n-keys again
  at 8f74442e4: 111 pass, i18n-keys the 1 red.
- The router's row under the skill list moved four GUI tests'
  footing, fixed in the tests: remove-all-skills counted it as a skill
  (now rows of lists other than a hub's, as list-sort does);
  found-skill-pick-remove and library-pick-remove's reach only wheeled
  down from above the target, so a Select pushed under the sticky tabs
  or below the fold was never reached (now toward it, over the list,
  and the scroll at rest before the click, WebKit's settled a pixel
  late); library-own-folder's Playwright click scrolled the now
  taller page under load (now a click where the button is). Each red
  on the branch before, 22/22 or 14/14 after, twice.
- skill-router.test.cjs: 22/22 (chromium and webkit, 1100 and 420px,
  en zh zh-TW ja de)
- go vet -tags nogui, GOOS=linux and GOOS=windows: ok
- win-run (xiaomi-gpu) go test ./internal/library: ok but
  TestGoneAgentListedToTakeOut and TestCheckMixedServerEachSide, red the
  same at origin/main 6b0b7a96e and filed in #4115 (run at that
  base; nothing in internal/library has changed on main since). The
  router tests' expected rules were /C:/…; Claude Code's form is
  //c/… (claudePath), which the code wrote, so the tests now ask
  claudePath too.
- Broken by hand: routable returning true is red in TestSkillRouter (a
  skill for the user to type is listed); with the routerSkill check
  out of the sessions reader, TestClaudeRoutedSkillCounted is red.
- Real Codex 0.165.0, sandbox HOME, through the live gateway with no
  sign-in copied: a binary built from this change at 6b0b7a96e ran `library skill
  hand-over`, `skill agents secret-word codex` and `skill router on`;
  `codex exec "What is the project's secret word?"` read
  magpie-skills/SKILL.md, then .skills/secret-word/SKILL.md at the path
  given, and answered PELICAN-7319. `router off` put the skill's link
  back and removed the router's folder.
- Real Claude Code 2.1.316, sandbox HOME, through the live gateway on
  auto/gpt-5.6-luna: that binary wrote the router and two
  Read rules into settings.json (the user's "model" kept). "Check your
  skills for one that fits, then greet me." ran Skill magpie-skills,
  then Read .skills/greet-user/SKILL.md without asking, and answered
  with the skill's words. Without the rules that Read was refused
  (tried earlier with hand-written settings). Not tried on the Claude
  subscription route with this binary: there the live gateway drops a
  skill's body, which the gateway commit before this one fixes.

Known: Alma reads Claude Code's and Codex's folders too, so it may find
several magpie-skills (#5722, not tried with a real Alma).
i18n-keys is red on 3 keys that aren't this change's, the same
on origin/main (#5327, #5332, #5482, #5426).
2026-11-09 08:38:02 +08:00

430 lines
14 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package library
// The skill router: with the Library's switch on (SkillRouter), an
// agent's skills folder is given one skill of magpie's, magpie-skills, in
// place of a link for each skill ticked for it. Its SKILL.md lists those
// skills, each with its description and the path of its own SKILL.md,
// which the agent reads when a task matches one. An
// agent then carries one skill's description in its prompt, not every
// one's, however many the library gives it.
//
// The entry is a link to a folder magpie writes for the agent's folder
// (routerDir): its SKILL.md, and under .skills a link to each skill it
// lists. The paths the SKILL.md gives lead through the agent's own skills
// folder, and .skills is a hidden folder, which the agents that look for
// skills in every folder under theirs (Codex, OpenCode, Pi) leave alone,
// so none of them finds a listed skill twice. Claude Code asks before it
// reads outside its project, even there, so magpie adds it Read rules
// for the entry and the folders behind it (routerAllow).
//
// What stays a link of its own (routedFor):
// - a skill the user keeps direct (Skill.Router), to be a /command of
// the agent's, or always in its prompt;
// - one whose SKILL.md sets disable-model-invocation: it is for the
// user to type, which a listed skill can't be;
// - one the agent finds in ~/.agents/skills anyway.
//
// And an agent whose folder can't take the router keeps every link
// (routerWhy): one given copies (in WSL, or by the user's choice), Claude
// Desktop (synced by syncDesktopSkills, never here), LiveAgent (which
// uses only the skills picked in its own list), an agent whose folder is
// ~/.agents/skills itself (read by many agents, each of which would find
// a second router beside its own), and one whose folder has a
// magpie-skills of the library's or the user's own.
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"strings"
"github.com/tidwall/jsonc"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/settings"
)
// RouterSkill is the name of the one skill the router gives an agent.
const RouterSkill = "magpie-skills"
// SkillDirect is Skill.Router for a skill the router leaves out: each
// agent given it keeps its own link, as with the router off.
const SkillDirect = "direct"
// Why an agent's folder keeps every skill's own link with the router on.
const (
routerCopies = "copies" // it is given copies, which a link can't list
routerLiveAgent = "liveagent" // it uses only the skills picked in its list
routerShared = "shared" // its folder is ~/.agents/skills, read by many
routerTaken = "taken" // the library has a skill called magpie-skills
routerOwn = "own" // its folder has a magpie-skills of its own
)
// routerDescMax is the most a SKILL.md's description may be: Codex
// refuses a skill whose description is longer.
const routerDescMax = 1024
// SetSkillRouter turns the router on or off: on, each agent that can take
// it is given magpie-skills in place of the links of the skills it lists;
// off, their own links again.
func SetSkillRouter(on bool) (*Result, error) {
return change(func(l *Library) error {
l.SkillRouter = on
return nil
})
}
// SkillRouterHow lists a skill in the router, or keeps it each agent's
// own link (direct); the agents are written again at once.
func SkillRouterHow(name string, direct bool) (*Result, error) {
return change(func(l *Library) error {
s := l.skill(name)
if s == nil {
return fmt.Errorf("no skill called %s", name)
}
s.Router = ""
if direct {
s.Router = SkillDirect
}
return nil
})
}
// routerRoot is where magpie writes the router's folders.
func routerRoot() string { return filepath.Join(settings.Dir(), "skill-router") }
// routerDir is the folder the router entry in the skills folder dir leads
// to: one for each folder, since what it lists is that folder's.
func routerDir(dir string) string {
h := sha256.Sum256([]byte(realDir(dir)))
return filepath.Join(routerRoot(), hex.EncodeToString(h[:6]))
}
// routerOurs is whether the entry at p is magpie's router entry.
func routerOurs(p string) bool {
to, ok := linkTarget(p)
if ok {
return within(filepath.Clean(to), routerRoot())
}
return false
}
// routerWhy is "" for an agent whose skills folder can take the router,
// or why it keeps each skill's own link.
func (l *Library) routerWhy(t *Target, cp bool) string {
switch {
case cp || t.Store != "" || t.Desktop != nil:
return routerCopies
case t.Agent.ID == "liveagent":
return routerLiveAgent
case realDir(t.Skills) == realDir(sharedSkillsDir()):
return routerShared
case l.skill(RouterSkill) != nil:
return routerTaken
}
p := filepath.Join(t.Skills, RouterSkill)
if _, err := os.Lstat(p); err == nil && !routerOurs(p) {
return routerOwn
}
return ""
}
// routable is whether the router may list s: not kept direct, and not one
// for the user alone to type.
func routable(s *Skill) bool {
if s == nil || s.Router == SkillDirect {
return false
}
m, _ := readMeta(skillDir(s.Name))
return !m.Manual
}
// routedFor are the skills the router lists in agent t's folder, which it
// then gives no link of their own, for t and the agents reading the same
// folder (sharers); none with the router off, or for a folder that can't
// take it. inShared is whether t finds a skill in ~/.agents/skills anyway.
func (l *Library) routedFor(t *Target, sharers []string, cp bool, inShared func(*Skill) bool) map[string]bool {
if !l.SkillRouter || t.Gone || l.routerWhy(t, cp) != "" {
return nil
}
out := map[string]bool{}
for _, s := range l.Skills {
if !(s.gives(t.Agent.ID) || slices.ContainsFunc(sharers, s.gives)) || !routable(s) || inShared(s) {
continue
}
// the agent's own skill by that name, or one blocked, stays as it
// is: the user settles it as with the router off
q := filepath.Join(t.Skills, s.Name)
if l.blockedSkills[realDir(q)] {
continue
}
if _, err := os.Lstat(q); err == nil && !ours(q, s.Name) && realDir(q) != realDir(skillDir(s.Name)) {
continue
}
out[s.Name] = true
}
return out
}
// placeRouter writes the router's folder for t's skills folder with the
// skills routed, and its entry in the folder; with none routed, the entry
// magpie made there goes, and its folder.
func (l *Library) placeRouter(t *Target, routed map[string]bool, a *Applied, res *Result) {
id := t.Agent.ID
p := filepath.Join(t.Skills, RouterSkill)
dir := routerDir(t.Skills)
if len(routed) == 0 {
if !a.Router {
l.routerAllow(t, nil, a, res)
return
}
if routerOurs(p) {
if err := unlink(p); err != nil {
res.fail(id, "skill:"+RouterSkill, err)
return
}
res.changed(id)
}
os.RemoveAll(dir)
a.Router = false
l.routerAllow(t, nil, a, res)
return
}
changed, err := writeRouter(dir, t.Skills, routed)
if err != nil {
res.fail(id, "skill:"+RouterSkill, err)
return
}
if !routerOurs(p) || realDir(p) != realDir(dir) {
unlink(p) // magpie's, to an old folder: routerWhy left a user's alone
if err := os.MkdirAll(t.Skills, 0o755); err != nil {
res.fail(id, "skill:"+RouterSkill, err)
return
}
if err := dirLink(dir, p); err != nil {
res.fail(id, "skill:"+RouterSkill, err)
return
}
changed = true
}
a.Router = true
if changed {
res.changed(id)
}
l.routerAllow(t, routerRules(t, routed), a, res)
}
// routerRules are the rules that let Claude Code read a listed skill's
// files without asking. It asks before reading outside the project, its
// own skills folder too, and checks both the path it is given and the
// one a link leads to (Claude Code 2.1.316, tried in a sandbox HOME), so
// one rule is for the router's entry and one for the library's skills
// folder; a skill whose folder is elsewhere gets its own. Other agents
// have no such setting magpie writes: none.
func routerRules(t *Target, routed map[string]bool) []string {
if t.Agent == nil || t.Agent.Plugin != "" || t.Settings == "" {
return nil
}
if id, _, _ := strings.Cut(t.Agent.ID, "@"); id != "claude" {
return nil
}
rule := func(dir string) string { return "Read(/" + claudePath(dir) + "/**)" }
out := []string{rule(filepath.Join(realDir(t.Skills), RouterSkill))}
lib := realDir(skillsDir())
for _, n := range slices.Sorted(maps.Keys(routed)) {
r := rule(lib)
if d := realDir(skillDir(n)); !within(d, lib) {
r = rule(d)
}
if !slices.Contains(out, r) {
out = append(out, r)
}
}
return out
}
// claudePath is an absolute path as Claude Code's permission rules match
// it: with forward slashes, and on Windows C:\x as /c/x
// (code.claude.com/docs/en/permissions, "Read and Edit").
func claudePath(p string) string {
if v := filepath.VolumeName(p); len(v) == 2 && v[1] == ':' {
p = "/" + strings.ToLower(v[:1]) + p[2:]
}
return filepath.ToSlash(p)
}
// routerAllow writes the rules (want) into Claude Code's permissions.allow,
// beside the user's own, and takes away magpie's that are no longer
// wanted; a rule the user had already is theirs and stays when magpie's
// go.
func (l *Library) routerAllow(t *Target, want []string, a *Applied, res *Result) {
if len(want) == 0 && len(a.RouterRules) == 0 {
return
}
id := t.Agent.ID
path := t.Settings
fail := func(err error) {
res.fail(id, "skill:"+RouterSkill, fmt.Errorf("reading skills without asking: %w", err))
}
if path == "" {
a.RouterRules = nil
return
}
raw, err := edit.Read(path)
if err != nil {
fail(err)
return
}
doc := map[string]any{}
if len(strings.TrimSpace(string(raw))) > 0 {
if err := json.Unmarshal(jsonc.ToJSON(raw), &doc); err != nil {
fail(fmt.Errorf("%s: %w", path, err))
return
}
}
perms, _ := doc["permissions"].(map[string]any)
cur, ok := entryTools(doc, []string{"permissions", "allow"})
if !ok {
fail(fmt.Errorf("%s: permissions.allow isn't a list of rules, so magpie leaves it as it is", path))
return
}
list, mine := offList(cur, a.RouterRules, want)
if slices.Equal(cur, list) {
a.RouterRules = mine
return
}
if l.kept == nil {
l.kept = newBackups()
}
if err := l.kept.keep(id, path); err != nil {
fail(err)
return
}
if len(list) > 0 {
err = edit.SetJSON(path, edit.KV{Path: "permissions.allow", Value: list})
} else {
// magpie's were the last: the key goes, and permissions too when
// nothing else is in it
keys := []string{"permissions.allow"}
if len(perms) == 1 {
keys = []string{"permissions"}
}
err = edit.DelJSON(path, keys...)
}
if err != nil {
fail(err)
return
}
a.RouterRules = mine
res.changed(id)
}
// writeRouter writes the router's folder dir for the skills folder at
// skills: a link under .skills to each skill routed, and the SKILL.md
// that lists them. It reports whether anything there changed.
func writeRouter(dir, skills string, routed map[string]bool) (bool, error) {
names := slices.Sorted(func(yield func(string) bool) {
for n := range routed {
if !yield(n) {
return
}
}
})
links := filepath.Join(dir, ".skills")
if err := os.MkdirAll(links, 0o755); err != nil {
return false, err
}
changed := false
es, _ := os.ReadDir(links)
for _, e := range es {
if !routed[e.Name()] {
if err := os.RemoveAll(filepath.Join(links, e.Name())); err != nil {
return false, err
}
changed = true
}
}
type entry struct{ name, desc, at string }
var list []entry
for _, n := range names {
q := filepath.Join(links, n)
at := filepath.Join(realDir(skills), RouterSkill, ".skills", n, "SKILL.md")
if to, ok := linkTarget(q); !ok || filepath.Clean(to) != filepath.Clean(skillDir(n)) {
os.RemoveAll(q)
if err := dirLink(skillDir(n), q); err != nil {
// no link here (Windows without the right to): the library's
// own folder is read instead
at = filepath.Join(skillDir(n), "SKILL.md")
} else {
changed = true
}
}
m, _ := readMeta(skillDir(n))
list = append(list, entry{n, m.Description, at})
}
var b strings.Builder
desc := routerDesc(names, func(i int) string { return list[i].desc })
q, _ := json.Marshal(desc)
fmt.Fprintf(&b, "---\nname: %s\ndescription: %s\n---\n\n", RouterSkill, q)
b.WriteString("# The user's skills\n\n")
b.WriteString("Each skill below is a folder of instructions, and often scripts or templates, for one kind of task. ")
b.WriteString("When a task matches a skill's description, read that skill's SKILL.md at the exact path given before you start (it is there, so don't search for it), and follow it. ")
b.WriteString("A file or script a skill names is in that skill's own folder, beside its SKILL.md.\n\n")
for _, e := range list {
fmt.Fprintf(&b, "## %s\n\n", e.name)
if e.desc != "" {
b.WriteString(e.desc + "\n\n")
}
fmt.Fprintf(&b, "SKILL.md: `%s`\n\n", e.at)
}
md := filepath.Join(dir, "SKILL.md")
if old, err := os.ReadFile(md); err == nil && string(old) == b.String() {
return changed, nil
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
return false, err
}
tmp := md + ".tmp"
if err := os.WriteFile(tmp, []byte(b.String()), 0o644); err != nil {
return false, err
}
return true, os.Rename(tmp, md)
}
// routerDesc is the router's description: what it is, then each skill's
// name with the start of its own description, cut to what Codex takes.
func routerDesc(names []string, desc func(int) string) string {
const head = "The user's skills. Read this before any task one of them may fit, to find the skill and read its SKILL.md: "
short := func(d string, n int) string {
if i := strings.IndexAny(d, ".。;;\n"); i > 0 {
d = d[:i]
}
if r := []rune(d); len(r) > n {
d = strings.TrimSpace(string(r[:n])) + "…"
}
return d
}
for _, n := range []int{80, 40, 20, 0} {
parts := make([]string, len(names))
for i, name := range names {
parts[i] = name
if d := short(desc(i), n); n > 0 && d != "" {
parts[i] += " (" + d + ")"
}
}
if s := head + strings.Join(parts, "; ") + "."; len(s) <= routerDescMax {
return s
}
}
s := head + strings.Join(names, ", ")
if len(s) > routerDescMax-1 {
r := []rune(s[:routerDescMax-len("…")])
s = strings.TrimRight(string(r[:len(r)-1]), ", ") + "…"
}
return s
}