Files
magpie/internal/library/project_targets_test.go
T
yetone 87b3af7653 library: a project turns a global MCP server or skill off, out of git, and its worktrees follow (WhyULose0 on X)
WhyULose0 asked for MCP servers and skills per project: a global set every
project inherits, each project that set ± items (a global one turned off
for one project too), one place to tick for every agent, nothing written
into the project's git, the router running a shared server once, and a
new git worktree following its main project.

What was there: projects could add skills and servers (Library → a
project's card), the MCP router ran each server once, but a project could
not turn a global item off, magpie wrote its entries into the project's
.gitignore (a tracked file), and a worktree got nothing.

What changed:
- Turning a global item off in one project (ProjectServerOff,
  ProjectSkillsOff; POST /api/library/projects/server-off, skill-off),
  done the way each agent reads a folder: Claude Code's record of the
  project in ~/.claude.json (disabledMcpServers; magpie takes out only
  names it put there), Codex's project .codex/config.toml
  ([mcp_servers.X] enabled = false, only for a server in Codex's own
  config), Claude Code's skillOverrides "off" in the project's
  .claude/settings.local.json.
- With the router on, a project that adds or turns off a routed server
  gives Claude Code (local scope in its project record) and Codex (project
  config) the hub at /mcp/magpie/hub/<agent>~<projectKey>; hubServers
  serves that address the project's set, each server still started once.
  gateway.HubAgentOf cuts the key off for the log, errors and on-demand.
- Out of git: entries go to the repository's info/exclude (one block per
  project, shared by every worktree); the .gitignore is left as the user
  wrote it, and a block an older magpie wrote there is taken out. A folder
  git-inited later gets its block on the next KeepLinked.
- Worktrees: the project's folder in each worktree of its repository gets
  the same skills, servers and offs, found on KeepLinked, let go when
  removed.
- GUI: a project's card lights each agent that has an item from the
  Library; a click turns it off (or back on) in that project. An agent that
  can't take an off (Gemini CLI etc., or a skill that goes through the
  skill router) stays lit and says why. The Skills menu ticks inherited
  skills; unticking turns one off. zh, zh-TW, ja, de.

Limits, per agent:
- Offs only for Claude Code (servers and skills) and Codex (servers).
  Codex reads the project config only in a trusted project.
- A skill reached through the skill router can't be turned off in one
  project.
- Claude Code keys its record by the repository's main top folder, so a
  project in a subfolder turns its server offs on repo-wide.

Run (mote's SSH refused every try today, kex reset; every run went to
the WSL box through wsl-run, linux, at load 60 to 80 from other runs):
- go test -tags nogui ./internal/library: ok at this head. At the base
  before (4a9142ccd) TestEveryAgentInLibrary was red on main's own
  avibe/obsidian, fixed upstream since.
- go test -tags nogui ./internal/gateway ./internal/gui, one rebase back:
  gateway ok; gui ok but for TestSetAnswersWhileDevinAndProvidersHang
  (a set waited 37s; #1375, numbers added there). An earlier run had one
  TestMiddlewareThroughGateway red (the reply's first delta skipped a
  middleware's onEvent; nothing here touches middleware), 5 of 5 alone,
  filed as #5955.
- go vet: linux (-tags nogui) and windows clean; darwin (-tags nogui,
  cross from linux, no cgo) clean but for internal/gui's
  traywindows_darwin_test.go, which needs trayImagePNG from a
  darwin && cgo file, untouched here.
- gofmt -l: clean
- GUI, chromium + webkit: project-*, i18n-* and library-* 4 wide, then
  each red file alone at this head. Alone, all pass, project-scope
  included, but for library-mcp-copy-config ja chromium (#5941) and
  library-browser-setup (#5974); both are red the same way at origin/main
  without this change (40ca0c88d and 6bac8f8a5). library-all webkit
  (#4967) was red alone before the rebase, also at 40ca0c88d.

At 82a8acb09 (this change on 065ca02c2): go vet -tags nogui ./...
clean, ./internal/library ok, project-* and i18n-* GUI 32 of 32. Then rebased over b2b83d4f9's four commits (lessons,
gateway plugin models through a remote, a plugin hook test, agent auto
mode), none of them in a file this change touches.

Broken by hand, each red:
- Claude offs not written: TestProjectServerOff (no record)
- Codex offs not written: TestProjectServerOff (codex project config)
- hubServers ignoring a project's offs: TestProjectSetThroughRouter
- skill offs not written: TestProjectSkillOff
- worktrees not listed: TestProjectWorktreeFollows
- writeExclude a no-op: TestProjectServerOff, TestProjectKeptOutOfGitByExclude,
  TestProjectOutsideGit
- every chip click sent to the add path: project-scope.test.cjs

Not tried: a real Claude Code or Codex run in a project with an off (the
record and table shapes are read from Claude Code 2.1.316's and Codex's
config code, not watched working); darwin (mote down), Windows runs;
a real `git worktree add` (the test writes git's worktree files by hand).
2026-11-10 22:39:29 +08:00

166 lines
6.6 KiB
Go

package library
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// eirc on Discord: Qoder and other agents could be given skills and MCP
// servers only user-wide. Each now gets a project's in the folder or file
// it reads them from there.
func TestProjectSkillsMoreAgents(t *testing.T) {
_, proj := projectLib(t)
ok(t)(ProjectSkill(proj, "pdf", []string{"qoder", "qoder-cn", "codebuddy", "commandcode", "droid", "cline"}))
for _, e := range []string{".qoder/skills/pdf", ".codebuddy/skills/pdf", ".commandcode/skills/pdf", ".agents/skills/pdf"} {
p := filepath.Join(proj, e)
if !isLink(t, p) || !ours(p, "pdf") {
t.Errorf("%s isn't a link to the library's", e)
}
if _, err := os.Stat(filepath.Join(p, "SKILL.md")); err != nil {
t.Errorf("%s: %v", e, err)
}
}
g := excluded(t, proj)
for _, e := range []string{"/.qoder/skills/pdf", "/.codebuddy/skills/pdf", "/.commandcode/skills/pdf", "/.agents/skills/pdf"} {
if !strings.Contains(g, e+"\n") {
t.Errorf("info/exclude lacks %s:\n%s", e, g)
}
}
// Qoder CN shares Qoder's project folder: one of the two taken away
// leaves it for the other
ok(t)(ProjectSkill(proj, "pdf", []string{"qoder-cn"}))
if !isLink(t, filepath.Join(proj, ".qoder/skills/pdf")) {
t.Error(".qoder/skills/pdf went while Qoder CN still has it")
}
gone(t, filepath.Join(proj, ".codebuddy"))
ok(t)(RemoveProject(proj, false))
gone(t, filepath.Join(proj, ".qoder"))
if g := excluded(t, proj); g != "" {
t.Errorf("info/exclude after: %q", g)
}
}
// Qoder's project servers are .qoder/settings.json's mcpServers, in the
// shape its user-wide settings.json has them (type beside a url, none on
// a command); CodeBuddy's the project's .mcp.json, as Claude Code's;
// Droid's .factory/mcp.json. What the user has in each stays.
func TestProjectServersQoderCodeBuddyDroid(t *testing.T) {
h, proj := mcpProject(t)
qs := filepath.Join(proj, ".qoder", "settings.json")
write(t, qs, `{
"permissions": {"allow": ["Bash(npm test)"]},
"mcpServers": {"linear": {"type": "http", "url": "https://mcp.linear.app/mcp"}}
}
`)
ok(t)(SaveServer("", Server{Name: "events", Transport: "sse", URL: "https://x.example/sse"}))
for _, name := range []string{"fs", "docs", "events"} {
ok(t)(ProjectServer(proj, name, []string{"qoder", "codebuddy", "droid"}))
}
doc := jsonOf(t, qs)
if p, _ := doc["permissions"].(map[string]any); p == nil {
t.Errorf("Qoder's permissions lost:\n%s", read(t, qs))
}
s, _ := doc["mcpServers"].(map[string]any)
if l, _ := s["linear"].(map[string]any); l["url"] != "https://mcp.linear.app/mcp" {
t.Errorf("the user's server lost: %v", s)
}
if fs, _ := s["fs"].(map[string]any); fs["command"] != "npx" || fs["type"] != nil {
t.Errorf("qoder fs: %v", fs)
}
if d, _ := s["docs"].(map[string]any); d["type"] != "http" || d["url"] != "https://docs.example/mcp" {
t.Errorf("qoder docs: %v", d)
}
if e, _ := s["events"].(map[string]any); e["type"] != "sse" {
t.Errorf("qoder events: %v", e)
}
m := servers(t, filepath.Join(proj, ".mcp.json"), "mcpServers")
if m["team"] == nil || m["fs"] == nil || m["docs"] == nil || m["events"] == nil {
t.Errorf("codebuddy .mcp.json: %v", m)
}
f := filepath.Join(proj, ".factory", "mcp.json")
if d := servers(t, f, "mcpServers"); d["fs"].(map[string]any)["type"] != "stdio" || d["docs"].(map[string]any)["type"] != "http" || d["events"] == nil {
t.Errorf("droid: %v", d)
}
g := excluded(t, proj)
if !strings.Contains(g, "/.factory/mcp.json\n") || strings.Contains(g, ".qoder") || strings.Contains(g, "/.mcp.json") {
t.Errorf("info/exclude:\n%s", g)
}
if _, err := os.Stat(filepath.Join(h, ".qoder", "settings.json")); err == nil {
t.Error("Qoder's user-wide settings were written")
}
ok(t)(RemoveProject(proj, false))
if s, _ := jsonOf(t, qs)["mcpServers"].(map[string]any); len(s) != 1 || s["linear"] == nil {
t.Errorf("qoder after: %s", read(t, qs))
}
gone(t, filepath.Join(proj, ".factory"))
if ProjectMCPFile("qoder") != ".qoder/settings.json" || ProjectMCPFile("qoder-cn") != ".qoder/settings.json" || ProjectMCPFile("codebuddy") != ".mcp.json" || ProjectMCPFile("droid") != ".factory/mcp.json" {
t.Error("ProjectMCPFile")
}
if ProjectSkillsDir("qoder") != ".qoder/skills" || ProjectSkillsDir("cline") != ".agents/skills" || ProjectSkillsDir("droid") != ".agents/skills" {
t.Error("ProjectSkillsDir")
}
}
// CodeBuddy reads only the first there is of .mcp.json and mcp.json: a
// project with mcp.json alone has its servers put there, so a new
// .mcp.json doesn't hide the user's from it.
func TestProjectServersCodeBuddyMcpJSON(t *testing.T) {
_, proj := mcpProject(t)
os.Remove(filepath.Join(proj, ".mcp.json"))
write(t, filepath.Join(proj, "mcp.json"), `{"mcpServers": {"team": {"command": "team-mcp"}}}`)
ok(t)(ProjectServer(proj, "fs", []string{"codebuddy"}))
if _, err := os.Stat(filepath.Join(proj, ".mcp.json")); err == nil {
t.Errorf(".mcp.json made:\n%s", read(t, filepath.Join(proj, ".mcp.json")))
}
if s := servers(t, filepath.Join(proj, "mcp.json"), "mcpServers"); s["team"] == nil || s["fs"] == nil {
t.Errorf("mcp.json: %v", s)
}
if v, _ := Read(nil); !slices.Equal(v.Projects[0].Wrote["mcp.json"], []string{"fs"}) {
t.Errorf("wrote: %v", v.Projects[0].Wrote)
}
ok(t)(ProjectServer(proj, "fs", nil))
if s := servers(t, filepath.Join(proj, "mcp.json"), "mcpServers"); len(s) != 1 || s["team"] == nil {
t.Errorf("after: %v", s)
}
}
// xingchen_c on Discord: DeepSeek Harness couldn't be given a project's
// skills. It reads <projectRoot>/.agents/skills (and .dsh/skills), the
// project root being the nearest folder with a .git, so the project's
// shared folder is where it gets them, listed for it on the page too.
func TestProjectSkillsDsh(t *testing.T) {
h, proj := projectLib(t)
write(t, filepath.Join(h, ".dsh/profiles/web/cordis.patch.yml"), "[]\n")
os.MkdirAll(filepath.Join(proj, ".git"), 0o755)
ok(t)(ProjectSkill(proj, "pdf", []string{"dsh"}))
p := filepath.Join(proj, ".agents/skills/pdf")
if !isLink(t, p) || !ours(p, "pdf") {
t.Errorf("%s isn't a link to the library's", p)
}
if g := excluded(t, proj); !strings.Contains(g, "/.agents/skills/pdf\n") {
t.Errorf("info/exclude lacks /.agents/skills/pdf:\n%s", g)
}
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
i := slices.IndexFunc(v.Agents, func(a AgentView) bool { return a.ID == "dsh" })
if i < 0 {
t.Fatal("no dsh on the page")
}
if v.Agents[i].ProjectSkills != ".agents/skills" {
t.Errorf("dsh's project skills: %q", v.Agents[i].ProjectSkills)
}
// it reads no project MCP file: its servers are rows in its profiles'
// patch lists, which have no project layer
if v.Agents[i].ProjectMCP != "" {
t.Errorf("dsh's project MCP: %q", v.Agents[i].ProjectMCP)
}
}