Files
magpie/internal/library/skill_router.go
T
yetone afabb7f90f library: a skill the router lists stays a /name of Claude Code's
RoyShen on X (yuushabu_buyinn): with the skill router on, / in the agent
no longer picks a skill. A listed skill has no link of its own in the
skills folder, so Claude Code had no /name for it.

For each skill the router lists for Claude Code (any profile; not a
plugin agent or WSL), routerCommands writes <claude dir>/commands/
<name>.md: the skill's description, disable-model-invocation: true, and
a body that has the agent read the skill's SKILL.md at the router's path
and follow it, with $ARGUMENTS. The flag keeps the description out of
the model's prompt, so the router still saves what it saved. The stubs
go in the commands folder, not the skills folder, because Crush, DevEco
Code and Kimi Code read ~/.claude/skills too and ignore the flag.

Applied.RouterCommands records the files with their hash (syncOwnFiles):
one the user changed is theirs. A user's own command file by that name,
or a Library command of that name given to Claude Code, means no stub;
syncCommands takes a stub's file over when such a command is added. The
command import leaves the stubs out. Router off, or the skill kept
direct: the stub goes.

Run:
- mica-run go test -tags nogui ./internal/library -count=1 -timeout 45m: ok
  (277s); GOOS=linux and GOOS=windows go vet ./internal/library: ok;
  gofmt -l clean. Base 18fe1e2dc (fetch failed at commit time).
- Broken by hand: routerCommandsDir returning "" (the old behaviour)
  turns TestSkillRouterKeepsSlashCommands red.
- Real Claude Code 2.1.316 in a sandbox HOME, magpie built from this
  tree placing two skills (okapi, zebu) with the router on, through a
  recording proxy to the gateway (auto/claude-haiku-5-5): the / menu
  lists /okapi and /zebu with their descriptions; "/okapi" answered the
  codeword from okapi's SKILL.md; "Use my zebu skill." went through
  magpie-skills and answered zebu's; every run's first request has each
  description once, in the router's own line, none from a stub. Router
  off: both stubs gone, links back, settings.json's rules gone.

Not tried: Windows and Linux Claude Code (the path is the same
filepath.Join; Windows rules use claudePath already). The Library's
"keep direct" tooltip still says "to type it as /name", which stays
true for the other agents.
2026-11-18 13:43:49 +08:00

526 lines
18 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
// an agent other than Claude Code, whose routed skills each have a
// slash command (routerCommands), 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
routerNone = "none" // its folder takes it, but no skill is listed there
)
// 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)
l.routerCommands(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)
l.routerCommands(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)
l.routerCommands(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)
if to, ok := linkTarget(q); !ok || filepath.Clean(to) != filepath.Clean(skillDir(n)) {
os.RemoveAll(q)
// no link here (Windows without the right to): the library's
// own folder is read instead (routedAt)
if dirLink(skillDir(n), q) == nil {
changed = true
}
}
at := filepath.Join(routedAt(dir, skills, n), "SKILL.md")
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)
}
// routedAt is the folder of the routed skill n the router's SKILL.md
// names: through the agent's own skills folder, where the router's folder
// dir links to it, else the library's own.
func routedAt(dir, skills, n string) string {
if to, ok := linkTarget(filepath.Join(dir, ".skills", n)); ok && filepath.Clean(to) == filepath.Clean(skillDir(n)) {
return filepath.Join(realDir(skills), RouterSkill, ".skills", n)
}
return skillDir(n)
}
// routerCommands keeps each routed skill a /name of Claude Code's (RoyShen
// on X: with the router on, / no longer picked a skill). For each one it
// writes a slash command, <claude dir>/commands/<name>.md, that has the
// agent read the skill's SKILL.md and follow it. The command says
// disable-model-invocation, so Claude Code lists it under / with the
// skill's description but leaves it out of the model's prompt (Claude
// Code 2.1.316, tried in a sandbox HOME: the description is in no request,
// and /name reads and follows the skill). They go in Claude Code's own
// commands folder, not its skills folder, which Crush, DevEco Code and
// Kimi Code read too, and which would give them every description again.
// What magpie wrote is recorded as a command's is (Applied.RouterCommands,
// syncOwnFiles); a library command or a file of the user's by that name
// stays, and that skill gets none. Other agents: none.
func (l *Library) routerCommands(t *Target, routed map[string]bool, a *Applied, res *Result) {
if len(routed) == 0 && len(a.RouterCommands) == 0 {
return
}
var want []ownFile
if dir := routerCommandsDir(t); dir != "" {
had := map[string]bool{}
for _, f := range a.RouterCommands {
had[f.Path] = true
}
taken := map[string]bool{}
for _, f := range a.Commands {
taken[f.Path] = true
}
rdir := routerDir(t.Skills)
for _, n := range slices.Sorted(maps.Keys(routed)) {
if strings.ContainsAny(n, `:/\`) {
continue
}
if c := l.command(n); c != nil && slices.Contains(c.Agents, t.Agent.ID) {
continue
}
path := filepath.Join(dir, n+".md")
if taken[path] {
continue
}
at := routedAt(rdir, t.Skills, n)
body := routerCommand(n, at)
if _, err := os.Lstat(path); err == nil && !had[path] {
// the user's own command by that name, unless it is what
// magpie would write
if cur, err := os.ReadFile(path); err != nil || !slices.Equal(cur, body) {
continue
}
}
want = append(want, ownFile{name: n, path: path, body: func() []byte { return body }})
}
}
if l.kept == nil {
l.kept = newBackups()
}
a.RouterCommands = syncOwnFiles(t, "command", a.RouterCommands, want, l.kept, res)
}
// routerCommandsDir is Claude Code's commands folder, beside the settings
// routerRules writes; "" for any other agent.
func routerCommandsDir(t *Target) string {
if t.Agent == nil || t.Agent.Plugin != "" || t.Settings == "" || t.Agent.WSL != "" {
return ""
}
if id, _, _ := strings.Cut(t.Agent.ID, "@"); id != "claude" {
return ""
}
return filepath.Join(filepath.Dir(t.Settings), "commands")
}
// routerCommand is the slash command for the routed skill n, whose folder
// is at.
func routerCommand(n, at string) []byte {
m, _ := readMeta(skillDir(n))
kv := [][2]string{}
if m.Description != "" {
kv = append(kv, [2]string{"description", yamlStr(m.Description)})
}
kv = append(kv, [2]string{"disable-model-invocation", "true"})
body := fmt.Sprintf("Use the user's skill %s: read `%s` and follow it. A file or script it names is in `%s`, beside it.\n\n$ARGUMENTS\n",
n, filepath.Join(at, "SKILL.md"), at)
return frontmatter(kv, body)
}
// 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
}