diff --git a/docs/subsystems/library.md b/docs/subsystems/library.md index 2ddfb4b92..b32c50221 100644 --- a/docs/subsystems/library.md +++ b/docs/subsystems/library.md @@ -19,7 +19,7 @@ Pi's extensions and packages. | library.json | `Library` holds the instruction sets, the MCP servers, the skills, the projects, skill groups and copy settings. `Applied` records what magpie last wrote into each agent, so it only ever takes away its own. The instruction texts and skill folders live under `Dir` (`library/` in magpie's config folder). | [`library.go`](../../internal/library/library.go) | | A Remote magpie's skills and prompts | A Remote magpie provider's editor has a switch, Use its skills and prompts (`borrowOn` in library.json, `SetBorrow`; KoiLx0 on X: manage them once on the remote machine). The remote serves its Library at `GET /v1/magpie/library` (`gateway.SharedLibrary` = `library.Shared`, to `local` or `sharedWith` callers; a held key is refused; no MCP servers, which the MCP switch covers; borrowed items are never passed on). `keepBorrowed` (10 minutes, and once on switching on) fetches it with the provider's key (`provider.RemoteLibrary`) and `Borrow` adds each skill and each instruction set as `remote--`, recording a hash of what it wrote (`Borrowed`). It only ever changes what still matches that hash: a skill or set of the user's by the same name, one the user edited, and one the user removed after it was taken stay as they are (`Kept`, shown under the switch); an update or a drop moves the old copy to the backups; switching off takes out what is unchanged. The first set becomes the active one only when nothing is active. `Collect`/`Put` (sync backups) still carry borrowed items as ordinary ones. | [`borrow.go`](../../internal/library/borrow.go), [`remote_library.go`](../../internal/provider/remote_library.go), `TestBorrowRemoteLibrary` | | One change | `change` locks the library, loads it, applies one edit, saves, syncs every agent and saves again. Every exported mutation (`SaveServer`, `InstallSkills`, `SetSkillHow` and the rest) goes through it. One change runs at a time, whether from the page or the CLI. | [`library.go`](../../internal/library/library.go) | -| Targets | `Targets` lists the detected agents (`agent.Detected`), a folder an agent shares with its vendor's other app when only that app is here (`agent.SharedHere`: the Cursor editor's `~/.cursor`, whose `mcp.json` and skills it reads, listed as Cursor; `TestCursorEditorAloneKeepsItsLibraryRow`), plus apps like Claude Desktop, with where each keeps its instructions file (and an `Override` it reads instead), its MCP file and its skills folder. An agent whose CLI is gone (`CLIMissing`: an uninstall that left the folder behind, #2177, #2254) is no target: it is given nothing it can't read. One that isn't a target but still has skills, servers or hooks magpie gave it (in `Applied`, or listed in an item's `Agents`) is held (`held`, a `Target` with `Gone`): a sync takes edits from its copies and then only takes out of it (`syncSkills`, `syncMCP` and `syncHooks` give a gone target nothing new), and `Read` lists it with `gone: true` while it has any. On a row, an agent's profiles (`hermes#name`, `omp#name`, `claude#name`, `qwenpaw#id`, WSL ones included) are one chip with a count, when an agent has more than one (`profileChip` in plugins/library/client.js, Yanzhongixuy on X: about twenty identical Hermes glyphs on every skill row); a click opens the app menu to pick profiles singly, and All still means every profile (`library-profile-chip.test.cjs`). A repository's, part's or pick's heading folds them the same way (`groupChips` sets `change.rows`; its chip is half lit when a profile has some of the skills), and the profiles chip takes part in the row's fan like any other (`[data-members]` in library.css), so the hand stays in line (Yanzhongixuy on X, 0.1.3926; `library-profile-group.test.cjs`). The page shows a gone agent's chip only on the rows of what it has, lit, so a click takes it out (`withGone` in plugins/library/client.js), leaves it out of All, the per-agent How sheet and every picker, and lists it in By agent as Not installed here with Turn all on off (@jiahao_jayden on X: an uninstalled WorkBuddy was on every skill's row; `TestGoneAgentListedToTakeOut`, `library-gone-agents.test.cjs`). On a Mac WorkBuddy and WorkBuddy AI are detected by their apps' bundle ids (`com.tencent.workbuddy.mac`, `com.workbuddy.workbuddy-ai`, `macBundle`), not by `~/.workbuddy`, which an uninstall leaves and magpie itself writes into (`TestWorkBuddyOnMacIsItsApp`); off a Mac the folder still counts. `everyTarget` keeps it for the guards that refuse an agent's own places: a project folder whose skills folder or MCP file is the agent's, and an instruction set kept in its instructions file. An agent in a running WSL distro has its places under the distro's home (`wslTargetOf`): Claude Code, Codex, Pi, OmO, Gemini CLI, Antigravity CLI, Crush, Cline, CodeBuddy Code, Kimi Code and Goose each by its own rule, never reading this machine's variables (Kimi Code's and Goose's shared `~/.agents/skills` there isn't given), Pi's MCP file chosen by `piMCP` as on this machine from the version the distro's `pi --version` gave its probe (`InstalledVersion`) and the distro's npm folders (`wslPiRoots`; leokon on Discord: only the Windows Pi was offered MCP servers, `TestWSLPiMCP`, `TestWSLMCPSiblings`), and every agent that keeps all of them in its own folder (Hermes Agent, omp, OpenCode, Droid and the rest of `ownFolder`, #1323) by the same rule as on this machine, its MCP file marked as WSL's so a Windows program isn't given to it. A place below a folder the distro has as a link is opened where the link leads (`wslOpen`, agent-wiring's WSL folders that are links, #4502); a link the distro couldn't resolve gives that agent one `wsl` problem and nothing is written to it (`TestWSLFolderIsALinkToC`, `TestWSLFolderLinkUnresolved`). An agent a plugin adds (`a.Plugin`) has the places its plugin's `agent.library` names (`pluginTarget`): an instructions file, an MCP file in one of `agentplug.MCPFormats` (each the `mcpFormat` of the agent it is named for, `pluginMCP`) and a skills folder, `~` and `$VAR` expanded as its config is; one that names none is no target. Before, a plugin's agent got models and keys only. Every built-in agent is a target or is in `noLibrary` with why: OpenBot was added with neither and was in no picker (SSNQT on X), so `TestEveryAgentInLibrary` fails a new agent until it gets one (34 were in no picker when it landed, listed there under #5897). | [`targets.go`](../../internal/library/targets.go) | +| Targets | `Targets` lists the detected agents (`agent.Detected`), a folder an agent shares with its vendor's other app when only that app is here (`agent.SharedHere`: the Cursor editor's `~/.cursor`, whose `mcp.json` and skills it reads, listed as Cursor; `TestCursorEditorAloneKeepsItsLibraryRow`; and `~/.copilot` or `$COPILOT_HOME`, listed as GitHub Copilot, whose `copilot-instructions.md` Copilot CLI, VS Code and GitHub Copilot's JetBrains plugin all read as the user's own instructions, kept with VS Code, VS Code Insiders or the plugin alone here even before the folder is (`agent.SharedRead`, `sharedApp`; @codealc on X, `TestCopilotVSCodeAloneGetsInstructions`), plus apps like Claude Desktop, with where each keeps its instructions file (and an `Override` it reads instead), its MCP file and its skills folder. An agent whose CLI is gone (`CLIMissing`: an uninstall that left the folder behind, #2177, #2254) is no target: it is given nothing it can't read. One whose vendor's other app still reads the folder (`SharedRead`: VS Code for `~/.copilot`) stays one (`TestCopilotCLIGoneVSCodeKeepsTheRow`). One that isn't a target but still has skills, servers or hooks magpie gave it (in `Applied`, or listed in an item's `Agents`) is held (`held`, a `Target` with `Gone`): a sync takes edits from its copies and then only takes out of it (`syncSkills`, `syncMCP` and `syncHooks` give a gone target nothing new), and `Read` lists it with `gone: true` while it has any. On a row, an agent's profiles (`hermes#name`, `omp#name`, `claude#name`, `qwenpaw#id`, WSL ones included) are one chip with a count, when an agent has more than one (`profileChip` in plugins/library/client.js, Yanzhongixuy on X: about twenty identical Hermes glyphs on every skill row); a click opens the app menu to pick profiles singly, and All still means every profile (`library-profile-chip.test.cjs`). A repository's, part's or pick's heading folds them the same way (`groupChips` sets `change.rows`; its chip is half lit when a profile has some of the skills), and the profiles chip takes part in the row's fan like any other (`[data-members]` in library.css), so the hand stays in line (Yanzhongixuy on X, 0.1.3926; `library-profile-group.test.cjs`). The page shows a gone agent's chip only on the rows of what it has, lit, so a click takes it out (`withGone` in plugins/library/client.js), leaves it out of All, the per-agent How sheet and every picker, and lists it in By agent as Not installed here with Turn all on off (@jiahao_jayden on X: an uninstalled WorkBuddy was on every skill's row; `TestGoneAgentListedToTakeOut`, `library-gone-agents.test.cjs`). On a Mac WorkBuddy and WorkBuddy AI are detected by their apps' bundle ids (`com.tencent.workbuddy.mac`, `com.workbuddy.workbuddy-ai`, `macBundle`), not by `~/.workbuddy`, which an uninstall leaves and magpie itself writes into (`TestWorkBuddyOnMacIsItsApp`); off a Mac the folder still counts. `everyTarget` keeps it for the guards that refuse an agent's own places: a project folder whose skills folder or MCP file is the agent's, and an instruction set kept in its instructions file. An agent in a running WSL distro has its places under the distro's home (`wslTargetOf`): Claude Code, Codex, Pi, OmO, Gemini CLI, Antigravity CLI, Crush, Cline, CodeBuddy Code, Kimi Code and Goose each by its own rule, never reading this machine's variables (Kimi Code's and Goose's shared `~/.agents/skills` there isn't given), Pi's MCP file chosen by `piMCP` as on this machine from the version the distro's `pi --version` gave its probe (`InstalledVersion`) and the distro's npm folders (`wslPiRoots`; leokon on Discord: only the Windows Pi was offered MCP servers, `TestWSLPiMCP`, `TestWSLMCPSiblings`), and every agent that keeps all of them in its own folder (Hermes Agent, omp, OpenCode, Droid and the rest of `ownFolder`, #1323) by the same rule as on this machine, its MCP file marked as WSL's so a Windows program isn't given to it. A place below a folder the distro has as a link is opened where the link leads (`wslOpen`, agent-wiring's WSL folders that are links, #4502); a link the distro couldn't resolve gives that agent one `wsl` problem and nothing is written to it (`TestWSLFolderIsALinkToC`, `TestWSLFolderLinkUnresolved`). An agent a plugin adds (`a.Plugin`) has the places its plugin's `agent.library` names (`pluginTarget`): an instructions file, an MCP file in one of `agentplug.MCPFormats` (each the `mcpFormat` of the agent it is named for, `pluginMCP`) and a skills folder, `~` and `$VAR` expanded as its config is; one that names none is no target. Before, a plugin's agent got models and keys only. Every built-in agent is a target or is in `noLibrary` with why: OpenBot was added with neither and was in no picker (SSNQT on X), so `TestEveryAgentInLibrary` fails a new agent until it gets one (34 were in no picker when it landed, listed there under #5897). | [`targets.go`](../../internal/library/targets.go) | | Stopped WSL distro | A stopped distro's agents are no targets: opening their files would start the distro, or the read fails (#3896). While a distro runs, `keepShape` keeps each agent's places and what its MCP file takes in `wsl.json` (`agent.WSLKeep`, key `library:`). While it is stopped, `Read` lists its agents from that (`asleepView`, `asleep: `), ticks as library.json has them; one magpie never saw there is listed only on the rows that name it, as `unseen`, its ticks not shown (cybercaius on Discord: the MCP page knew a distro's agents only once WSL had started). A tick on a stopped agent is kept in library.json as any is (`Takes` accepts one with a known shape; `serversAgents` judges it by `shape.reaches`), nothing is written and nothing in the distro runs; `Woken`, from `KeepLinked`'s loop (and the app's start-up sync), syncs when a distro the library names is seen go from stopped to running (`wsl.exe -l --running`, which starts nothing). The page greys a stopped agent's chip, says so in its tooltip and in one line per distro (`.lib-asleep`); an unseen chip is `aria-disabled` and kept by every click on its row (`keptOf`). The CLI says the same on stderr (`AsleepIn`). Queued, not a start button: a start costs the memory #3896 is about, and All or a bulk tick would start every distro. The Instructions page lists them too (`asleepInstructions`, #4182), at the instructions file the shape keeps, switched on as library.json has them, with what each gets besides (magpie's own folder) and nothing read from the file: its own lines and edits aren't counted then, and its path is plain text, not a reveal, which would open the distro. One switched on whose file magpie never saw is listed as `unseen`, its switch greyed out and kept by every other switch; before, a switch on the page sent the agents without the stopped distro's and took them off. A switch on one is written by `Woken`, which watches a distro named in `Instructions.Agents`, or whose agent still has magpie's part (`Applied.Instructions`), so one switched off has it taken out. The Hooks page lists the stopped agents with a shape as it does while the distro runs (`hooksView`, #4237): greyed, since magpie gives no agent in WSL hooks yet (`wslNoHooks`), and a hook given one says so; there is nothing for `Woken` to write. So does the Subagents page for the kinds it lists (`subagentsView`, `wslNoSubagents`, #4238). | [`wsl_asleep.go`](../../internal/library/wsl_asleep.go), [`targets.go`](../../internal/library/targets.go), [`plugins/library/client.js`](../../internal/gui/assets/plugins/library/client.js) | | Sync | `sync` writes each target's instructions and MCP servers. It then takes edits made in agents' skill copies into the library (`takeEdits`) and links or copies the skills. `~/.agents/skills` goes first (`sweepShared`, then `syncShared` for the Put skills in setting), so an agent that reads it gets no second link. Then it syncs projects' skills. | [`sync.go`](../../internal/library/sync.go) | | Projects | A project (`Project`) is a folder given some of the library's skills and MCP servers, each for some agents. A skill is linked (or copied) into the folder the agent reads a project's skills from (`projectSkillsDirs`): `.claude/skills` for Claude Code, `.qoder/skills` for Qoder and Qoder CN, `.codebuddy/skills` for CodeBuddy Code, `.commandcode/skills` for Command Code, and `.agents/skills` for Codex, Gemini CLI, OpenCode, Pi, Cursor, Copilot CLI, Antigravity, Kimi, Goose, Grok, Droid, Cline and DeepSeek Harness (which reads it, and `.dsh/skills`, at the nearest folder up with a `.git`, else where it runs: a project folder inside a repository but not at its root doesn't reach it, #5172). A server is written into the file the agent reads a project's servers from (`projectMCPFiles`), in the shape its user-wide file has: `.mcp.json` for Claude Code and CodeBuddy Code (CodeBuddy's `mcp.json` when the project has that and no `.mcp.json`, since it reads only the first of the two), `.codex/config.toml`, `.gemini/settings.json`, `.cursor/mcp.json`, `opencode.json`, `.zcode/config.json`, `.pi/mcp.json`, `.qoder/settings.json`'s `mcpServers` for Qoder and Qoder CN, and `.factory/mcp.json` for Droid. Claude Code, Qoder and CodeBuddy ask the user to approve a project's server before using it. Only targets whose vendor source or docs name them are listed (each map's comment cites them); an agent with none (Copilot CLI's MCP, Zed, WorkBuddy, Command Code's MCP, whose `.mcp.json` has its own `transport` shape, DeepSeek Harness's MCP, whose servers are rows in its profiles' patch layers with no project layer) can't be picked for a project (eirc on Discord). `Placed`, `Wrote`, `Made` and `MadeFiles` record what magpie put there, the only things it takes away. Nothing of magpie's goes into the project's git: what it placed is listed in the repository's own `info/exclude` (`writeExclude`, one block per project between `excludeBegin(dir)` and `ignoreEnd`, read by every worktree, never committed), and the project's `.gitignore` is left as it is (a block an older magpie wrote there is taken out, `TestProjectKeptOutOfGitByExclude`). A folder outside git gets no ignore file; once it is `git init`ed, `treesChanged` sees no block and the next `KeepLinked` writes it (`TestProjectOutsideGit`). A project is the global set ± its own (WhyULose0 on X): `ServersOff` and `SkillsOff` turn a global server or skill off in the project for some agents (`ProjectServerOff`, `ProjectSkillsOff`; `POST /api/library/projects/server-off` and `skill-off` with `dir`, `name`/`names`, `agents`; none gives it back). Each is done the way the agent reads it for a folder (`project_scope.go`): Claude Code's record of the project in `~/.claude.json` (`projects[
]`, `claudeRoot`) gets the name in `disabledMcpServers`, where magpie takes out only names it added (`Claude.Off`); Codex gets `[mcp_servers.] enabled = false` in the project's `.codex/config.toml`, only for a server in its own config (`CodexOff`); a skill is `skillOverrides. = "off"` in the project's `.claude/settings.local.json` (`Overrides`), a value the user set there is theirs (`TestProjectServerOff`, `TestProjectSkillOff`). Only those (`ProjectOffAgents`, `ProjectSkillOffAgents`) can take an off, and a skill an agent reaches through the [skill router](skill-router.md) can't; the card shows a global agent that can't as always on, with why. With the [MCP router](mcp-router.md) on, a project that adds or turns off a server the router serves gives Claude Code and Codex their own hub entry, `~` (`projectHubs`): Claude Code's in its record's `mcpServers` (local scope), Codex's in the project's `.codex/config.toml`; the server added isn't written into `.mcp.json` then (`TestProjectSetThroughRouter`). Claude Code keys its record by the repository's main top folder, so a project in a subfolder turns its offs on for the whole repository. A git worktree of the project's repository follows the project (`treeDirs`, `Trees`): the project's folder in each worktree gets the same skills, servers and offs, and is let go when the worktree is removed; `KeepLinked` checks for new ones (`keepProjectTrees`, `TestProjectWorktreeFollows`). A project also takes the library's subagents (jinpwng on X; `Subagents`, `AgentFiles`, [`project_subagents.go`](../../internal/library/project_subagents.go)): one file per subagent in the folder the agent reads a project's subagents from (`projectSubagentDirs`, cited per agent in its comment): `.claude/agents` for Claude Code, `.codex/agents`, `.opencode/agents`, `.gemini/agents`, `.qwen/agents`, `.factory/droids` for Droid, `.cursor/agents` and `.github/agents` for Copilot, in that agent's own format. A file is recorded with its hash (`SubagentFile`) and rewritten or removed only while it is exactly as magpie wrote it, so a same-name file of the user's is never touched (`TestProjectSubagentsLeaveTheUsersFiles`); a subagent renamed in the library is renamed in each project (`TestProjectSubagentsFollowTheLibrary`); the files are kept out of git by the same `info/exclude` block and follow the project's worktrees. `POST /api/library/projects/subagents` with `dir`, `name`/`names`, `agents`; the Subagents tab lists the projects as Skills and MCP do (`project-subagents.test.cjs`). It has no CLI yet, like project servers. Hooks, instructions and Claude plugins have no project scope yet. `magpie library project …` (#5193, [`library_project_cli.go`](../../internal/cli/library_project_cli.go)) adds and removes a project and its skills through the same calls as the page (`AddProject`, `RemoveProject`, `ProjectSkills`, `ProjectSkill`, `ProjectCopy`); a skill added with no `agents=` goes to the agents the page's Skills menu picks (those the project's skills are for already, else every agent here with a project skills folder), and one not placed exits 1 with why (`TestLibraryProjectCmd`). A project card's Skills menu lists the skills by source as the Library does (`skillGroups(true)`, whatever sort the list has; one group is the flat list), a heading over each that ticks or unticks the whole group (`group` in `openProtoMenu`, half-ticked while some are in; Elorynth on X, `project-skills-groups.test.cjs`). | [`projects.go`](../../internal/library/projects.go), [`project_mcp.go`](../../internal/library/project_mcp.go), [`project_scope.go`](../../internal/library/project_scope.go), [`project_git.go`](../../internal/library/project_git.go) | diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 93cfafa4b..673228746 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -379,6 +379,11 @@ type Agent struct { // ~/.cursor (MCP servers, skills, hooks, chats) is the Cursor editor's // too, so the Library and Sessions call it Cursor (SharedName). shared string + // sharedApp, when set, reports whether an app of the vendor's that + // reads the shared folder is here, the folder or not: VS Code and + // GitHub Copilot's JetBrains plugin read ~/.copilot's + // copilot-instructions.md before anything has made the folder. + sharedApp func() bool // pathOwn, when set, says whether the file at Path is this agent's, // not one another tool wrote there first: a JetBrains plugin's // settings.json in Qoder CN's folder (#1495). @@ -446,7 +451,15 @@ func (a *Agent) SharedName() string { // but what the other app reads there too, the Library's MCP servers and // skills, is still given. func (a *Agent) SharedHere() bool { - return a.shared != "" && a.WSL == "" && a.Dir != "" && !Taken(a.Dir) && agentDir(a.Dir) + return a.shared != "" && a.WSL == "" && a.Dir != "" && !Taken(a.Dir) && (agentDir(a.Dir) || a.SharedRead()) +} + +// SharedRead reports whether an app of the agent's vendor that reads its +// shared folder is here (sharedApp): VS Code, or GitHub Copilot's JetBrains +// plugin, for Copilot CLI's ~/.copilot. A folder left by the CLI's +// uninstall is then no leftover: that app still reads it. +func (a *Agent) SharedRead() bool { + return a.shared != "" && a.WSL == "" && a.Dir != "" && a.sharedApp != nil && !Taken(a.Dir) && a.sharedApp() } // Detected reports whether the agent seems to be installed or configured. diff --git a/internal/agent/agents.go b/internal/agent/agents.go index cb247776d..7add42132 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -325,7 +325,7 @@ func onMachineAll(home, cfg string) []*Agent { copilotJetBrains(home), air(home, cfg), airIDE(home, cfg), - copilot(home), + copilot(home, cfg), crush(home, cfg), dsh(home), dsbot(home), @@ -1864,7 +1864,7 @@ func cursor(home string) *Agent { } } -func copilot(home string) *Agent { +func copilot(home, cfg string) *Agent { dir := filepath.Join(home, ".copilot") // COPILOT_HOME moves the whole folder, settings.json and providers.json // with it (Copilot's `copilot help environment`) @@ -1898,6 +1898,14 @@ func copilot(home string) *Agent { // folder says nothing of the CLI: its settings.json or the copilot // command does (#1495) dirShared: true, + // and they read its copilot-instructions.md as the user's own + // instructions (VS Code's promptsServiceImpl, the JetBrains + // plugin's getCopilotCliHomePath): the Library's row is GitHub + // Copilot's, kept with VS Code or the plugin alone here + shared: "GitHub Copilot", + sharedApp: func() bool { + return copilotJBInstalled(home) || vscode(home, cfg).Detected() || vscodeInsidersAgent(home, cfg).Detected() + }, // VS Code 1.143 ships a copilot launcher of its own in // bin\copilot-shim in its install folder, which is on the user's // PATH, and running it only asks for the CLI to be installed diff --git a/internal/agent/copilot_byok_test.go b/internal/agent/copilot_byok_test.go index 13203ec5c..33b5267e3 100644 --- a/internal/agent/copilot_byok_test.go +++ b/internal/agent/copilot_byok_test.go @@ -34,7 +34,7 @@ func copilotSandbox(t *testing.T) (*Agent, string) { home := effortHome(t) t.Setenv("COPILOT_HOME", "") t.Setenv("COPILOT_PROVIDERS_CONFIG", "") - return copilot(home), filepath.Join(home, ".copilot", "providers.json") + return copilot(home, filepath.Join(home, ".config")), filepath.Join(home, ".copilot", "providers.json") } // Connecting Copilot CLI to magpie's model writes the provider Copilot reads @@ -151,7 +151,7 @@ func TestCopilotSyncCheckAndEnv(t *testing.T) { other := filepath.Join(t.TempDir(), "mine.json") t.Setenv("COPILOT_PROVIDERS_CONFIG", other) home := t.TempDir() - cp2 := copilot(home) + cp2 := copilot(home, filepath.Join(home, ".config")) if err := cp2.Field("model").Set("magpie/deepseek/flash"); err != nil { t.Fatal(err) } @@ -160,7 +160,7 @@ func TestCopilotSyncCheckAndEnv(t *testing.T) { } t.Setenv("COPILOT_PROVIDERS_CONFIG", "") t.Setenv("COPILOT_HOME", filepath.Join(home, "ch")) - if got := copilot(home).Path; got != filepath.Join(home, "ch", "settings.json") { + if got := copilot(home, filepath.Join(home, ".config")).Path; got != filepath.Join(home, "ch", "settings.json") { t.Fatalf("COPILOT_HOME ignored: %s", got) } } diff --git a/internal/agent/copilot_shim_test.go b/internal/agent/copilot_shim_test.go index cead23365..657a47e70 100644 --- a/internal/agent/copilot_shim_test.go +++ b/internal/agent/copilot_shim_test.go @@ -31,13 +31,13 @@ func TestCopilotLauncherFromAnEditorIsNotTheCLI(t *testing.T) { install := filepath.Join(t.TempDir(), "Microsoft VS Code") writeAt(t, filepath.Join(install, "resources", "app", "product.json"), `{"nameShort":"Visual Studio Code"}`) put(filepath.Join(install, "bin", "copilot-shim")) - if copilot(home).Detected() { + if copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("VS Code's copilot launcher is taken for the Copilot CLI") } - if bin, _ := copilot(home).cliBin(); bin != "" { + if bin, _ := copilot(home, filepath.Join(home, ".config")).cliBin(); bin != "" { t.Errorf("the CLI looked for is %s, the launcher", bin) } - if bin := copilot(home).BinPath(); bin != "" { + if bin := copilot(home, filepath.Join(home, ".config")).BinPath(); bin != "" { t.Errorf("the agent's program is %s, the launcher", bin) } // a Mac's editor keeps its launcher inside the app proper, whose @@ -45,12 +45,12 @@ func TestCopilotLauncherFromAnEditorIsNotTheCLI(t *testing.T) { app := filepath.Join(t.TempDir(), "Visual Studio Code.app", "Contents", "Resources", "app") writeAt(t, filepath.Join(app, "product.json"), `{"applicationName":"code"}`) put(filepath.Join(app, "bin", "copilot-shim")) - if copilot(home).Detected() { + if copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("the editor app's copilot launcher is taken for the Copilot CLI") } // a launcher in the editor's bin, no shim folder of its own put(filepath.Join(install, "bin")) - if copilot(home).Detected() { + if copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("a copilot launcher in the editor's bin is taken for the Copilot CLI") } // the CLI itself, where a user's installs put it, still is one @@ -60,10 +60,10 @@ func TestCopilotLauncherFromAnEditorIsNotTheCLI(t *testing.T) { t.Fatal(err) } t.Setenv("PATH", cli) - if !copilot(home).Detected() { + if !copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("the copilot command on PATH no longer finds the Copilot CLI") } - if bin, _ := copilot(home).cliBin(); bin == "" { + if bin, _ := copilot(home, filepath.Join(home, ".config")).cliBin(); bin == "" { t.Error("the copilot command on PATH no longer stands for the CLI") } // a product.json further up than an editor keeps one, as a folder of @@ -77,7 +77,7 @@ func TestCopilotLauncherFromAnEditorIsNotTheCLI(t *testing.T) { t.Fatal(err) } t.Setenv("PATH", npm) - if !copilot(home).Detected() { + if !copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("a product.json far above the CLI's folder makes the CLI an editor's launcher") } if runtime.GOOS == "windows" { @@ -90,7 +90,7 @@ func TestCopilotLauncherFromAnEditorIsNotTheCLI(t *testing.T) { t.Fatal(err) } t.Setenv("PATH", links) - if copilot(home).Detected() { + if copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("a link to the editor's launcher is taken for the Copilot CLI") } } @@ -119,10 +119,10 @@ func TestCopilotLauncherBesideTheBuildsHashFolderIsNotTheCLI(t *testing.T) { t.Fatal(err) } t.Setenv("PATH", shim) - if copilot(home).Detected() { + if copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("VS Code 1.144's copilot launcher is taken for the Copilot CLI") } - if bin, _ := copilot(home).cliBin(); bin != "" { + if bin, _ := copilot(home, filepath.Join(home, ".config")).cliBin(); bin != "" { t.Errorf("the CLI looked for is %s, the launcher", bin) } // a folder beside bin that no commit's short hash names, holding a @@ -131,7 +131,7 @@ func TestCopilotLauncherBesideTheBuildsHashFolderIsNotTheCLI(t *testing.T) { // launcher reads as the CLI again, as it did before writeAt(t, filepath.Join(install, "workshop", "resources", "app", "product.json"), `{"nameShort":"Visual Studio Code"}`) os.RemoveAll(filepath.Join(install, "2c33b50b16")) - if !copilot(home).Detected() { + if !copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("a folder beside bin that is no build's hash counts as an editor's install") } } diff --git a/internal/agent/cursor_editor_dir_test.go b/internal/agent/cursor_editor_dir_test.go index 9349d8612..7f181d197 100644 --- a/internal/agent/cursor_editor_dir_test.go +++ b/internal/agent/cursor_editor_dir_test.go @@ -43,9 +43,10 @@ func TestCursorFolderFromTheEditorIsNotTheCLI(t *testing.T) { if cursor(t.TempDir()).SharedHere() { t.Error("SharedHere with no ~/.cursor") } - // only Cursor CLI shares its folder + // only Cursor CLI and Copilot CLI (~/.copilot, which VS Code's and the + // JetBrains plugin's Copilot read) share their folders for _, b := range All() { - if b.ID != "cursor" && b.shared != "" { + if b.ID != "cursor" && b.ID != "copilot" && b.shared != "" { t.Errorf("%s shares its folder too: %q", b.ID, b.shared) } } diff --git a/internal/agent/cursor_name_test.go b/internal/agent/cursor_name_test.go index 9e501c50d..1b9c47375 100644 --- a/internal/agent/cursor_name_test.go +++ b/internal/agent/cursor_name_test.go @@ -17,8 +17,15 @@ func TestCursorIsCursorCLI(t *testing.T) { if got := a.SharedName(); got != "Cursor" { t.Errorf("SharedName = %q, want Cursor", got) } - // every other agent keeps one name + // Copilot CLI's ~/.copilot is GitHub Copilot's, read by VS Code and + // the JetBrains plugin too; every other agent keeps one name for _, b := range All() { + if b.ID == "copilot" { + if b.Name != "Copilot CLI" || b.SharedName() != "GitHub Copilot" { + t.Errorf("copilot: Name %q, SharedName %q", b.Name, b.SharedName()) + } + continue + } if b.ID != "cursor" && b.SharedName() != b.Name { t.Errorf("%s: SharedName %q, Name %q", b.ID, b.SharedName(), b.Name) } diff --git a/internal/agent/effort_test.go b/internal/agent/effort_test.go index 217862bc5..91b24b8f0 100644 --- a/internal/agent/effort_test.go +++ b/internal/agent/effort_test.go @@ -100,7 +100,7 @@ func TestEffortFields(t *testing.T) { } writeFile(t, filepath.Join(home, ".copilot", "settings.json"), `{"model":"gpt-5.5","contextTier":"long"}`) - setEffort(t, copilot(home), "xhigh", `"contextTier"`) + setEffort(t, copilot(home, filepath.Join(home, ".config")), "xhigh", `"contextTier"`) cr := crush(home, cfg) os.MkdirAll(filepath.Dir(cr.Path), 0o755) diff --git a/internal/agent/ide_plugin_dir_test.go b/internal/agent/ide_plugin_dir_test.go index bac9b9949..71b4145cd 100644 --- a/internal/agent/ide_plugin_dir_test.go +++ b/internal/agent/ide_plugin_dir_test.go @@ -23,21 +23,21 @@ func TestCopilotFolderFromAnIDEPluginIsNotTheCLI(t *testing.T) { bin := t.TempDir() t.Setenv("PATH", bin) home := t.TempDir() - a := copilot(home) + a := copilot(home, filepath.Join(home, ".config")) for _, f := range []string{"ide/.registry/1.jsonl", "instructions/a.md", "prompts/b.md", "agents/c.agent.md", "hooks/h.json"} { writeAt(t, filepath.Join(a.Dir, f), "{}") } - if copilot(home).Detected() { + if copilot(home, filepath.Join(home, ".config")).Detected() { t.Errorf("a ~/.copilot holding only the JetBrains plugin's folders is taken for the Copilot CLI") } writeAt(t, filepath.Join(bin, "copilot"), "#!/bin/sh\n") os.Chmod(filepath.Join(bin, "copilot"), 0o755) - if !copilot(home).Detected() { + if !copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("the copilot command on PATH no longer finds the Copilot CLI") } os.Remove(filepath.Join(bin, "copilot")) writeAt(t, a.Path, `{"model":"auto"}`) - if !copilot(home).Detected() { + if !copilot(home, filepath.Join(home, ".config")).Detected() { t.Error("~/.copilot/settings.json no longer finds the Copilot CLI") } } diff --git a/internal/library/copilot_shared_test.go b/internal/library/copilot_shared_test.go new file mode 100644 index 000000000..3ec36c413 --- /dev/null +++ b/internal/library/copilot_shared_test.go @@ -0,0 +1,179 @@ +package library + +import ( + "os" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/yetone/magpie/internal/agent" +) + +// GitHub Copilot reads ~/.copilot/copilot-instructions.md as the user's own +// instructions in all three of its surfaces: Copilot CLI, VS Code (its +// promptsServiceImpl reads the Copilot harness's user folder) and the +// JetBrains plugin (getCopilotCliHomePath()). The Library's Instructions +// had a row for it only with Copilot CLI installed, named Copilot CLI +// (@codealc on X: 资源库 › 指令 has no GitHub Copilot). + +// noCopilotCLI takes Copilot CLI away: no settings.json, no command. +func noCopilotCLI(t *testing.T, h string) { + t.Helper() + os.RemoveAll(filepath.Join(h, ".copilot")) + os.Remove(filepath.Join(h, "bin", "copilot")) + os.Remove(filepath.Join(h, "bin", "copilot.exe")) +} + +// vscodeUser makes VS Code's User folder, as VS Code does on its first start. +func vscodeUser(t *testing.T, h string) { + t.Helper() + var d string + switch runtime.GOOS { + case "darwin": + d = filepath.Join(h, "Library", "Application Support", "Code", "User") + case "windows": + d = filepath.Join(h, "AppData", "Roaming", "Code", "User") + default: + d = filepath.Join(h, ".config", "Code", "User") + } + if err := os.MkdirAll(d, 0o755); err != nil { + t.Fatal(err) + } +} + +// jetBrainsCopilot installs GitHub Copilot's JetBrains plugin in an IDE. +func jetBrainsCopilot(t *testing.T, h string) { + t.Helper() + t.Setenv("XDG_DATA_HOME", "") + var d string + switch runtime.GOOS { + case "darwin": + d = filepath.Join(h, "Library", "Application Support", "JetBrains", "IntelliJIdea2026.2", "plugins") + case "windows": + d = filepath.Join(h, "AppData", "Roaming", "JetBrains", "IntelliJIdea2026.2", "plugins") + default: + d = filepath.Join(h, ".local", "share", "JetBrains", "IntelliJIdea2026.2") + } + if err := os.MkdirAll(filepath.Join(d, "github-copilot-intellij"), 0o755); err != nil { + t.Fatal(err) + } +} + +func copilotRow(t *testing.T, h string) { + t.Helper() + c := targetByID("copilot") + if c == nil { + t.Fatalf("no copilot target: %v", ids(Targets())) + } + if c.Agent.Name != "GitHub Copilot" { + t.Errorf("the Library's row is %q, want GitHub Copilot", c.Agent.Name) + } + if want := filepath.Join(h, ".copilot", "copilot-instructions.md"); c.Instructions != want { + t.Errorf("instructions go to %q, want %q", c.Instructions, want) + } +} + +func TestCopilotLibraryRowIsGitHubCopilot(t *testing.T) { + h := sandbox(t) + copilotRow(t, h) + a, err := agent.Find("copilot") + if err != nil { + t.Fatal(err) + } + if a.Name != "Copilot CLI" { + t.Errorf("the agent is %q after the Library named its row, want Copilot CLI", a.Name) + } + v, err := Read(nil) + if err != nil { + t.Fatal(err) + } + for _, av := range v.Agents { + if av.ID == "copilot" && av.Name != "GitHub Copilot" { + t.Errorf("the Library's view names it %q", av.Name) + } + } +} + +// VS Code alone, with no ~/.copilot yet: the row is there, and saving +// instructions writes the file VS Code reads. +func TestCopilotVSCodeAloneGetsInstructions(t *testing.T) { + h := sandbox(t) + noCopilotCLI(t, h) + if c := targetByID("copilot"); c != nil { + t.Fatalf("a copilot target with no Copilot here: %+v", c) + } + vscodeUser(t, h) + copilotRow(t, h) + shared := "Answer in Chinese." + ok(t)(SaveInstructions(InstructionsChange{Shared: &shared, Agents: []string{"copilot"}})) + p := filepath.Join(h, ".copilot", "copilot-instructions.md") + if s := read(t, p); s != blockBegin+"\nAnswer in Chinese.\n"+blockEnd+"\n" { + t.Errorf("%s:\n%q", p, s) + } + a, err := agent.Find("copilot") + if err != nil { + t.Fatal(err) + } + if a.Detected() { + t.Error("the instructions magpie wrote in ~/.copilot made Copilot CLI detected") + } +} + +func TestCopilotJetBrainsAloneGetsInstructions(t *testing.T) { + h := sandbox(t) + noCopilotCLI(t, h) + jetBrainsCopilot(t, h) + copilotRow(t, h) +} + +// The user's own copilot-instructions.md is kept, with magpie's block +// beside it, and comes back whole when magpie's instructions are off. +func TestCopilotInstructionsKeepTheUsersOwn(t *testing.T) { + h := sandbox(t) + noCopilotCLI(t, h) + vscodeUser(t, h) + p := filepath.Join(h, ".copilot", "copilot-instructions.md") + mine := "# Mine\n\nUse tabs.\n" + write(t, p, mine) + shared := "Answer in Chinese." + ok(t)(SaveInstructions(InstructionsChange{Shared: &shared, Agents: []string{"copilot"}})) + s := read(t, p) + if !strings.Contains(s, "Use tabs.") || !strings.Contains(s, "Answer in Chinese.") { + t.Errorf("%s:\n%s", p, s) + } + empty := "" + ok(t)(SaveInstructions(InstructionsChange{Shared: &empty, Agents: []string{"copilot"}})) + if s := read(t, p); strings.TrimSpace(s) != strings.TrimSpace(mine) { + t.Errorf("after magpie's instructions are gone:\n%q", s) + } +} + +// Copilot CLI uninstalled, its settings.json left behind: the Agents page +// calls it gone, but VS Code still reads ~/.copilot, so the row stays; with +// no app reading it, it goes. +func TestCopilotCLIGoneVSCodeKeepsTheRow(t *testing.T) { + h := sandbox(t) + os.Remove(filepath.Join(h, "bin", "copilot")) + os.Remove(filepath.Join(h, "bin", "copilot.exe")) + if c := targetByID("copilot"); c != nil { + t.Fatalf("a leftover ~/.copilot with nothing reading it is a target: %+v", c) + } + vscodeUser(t, h) + copilotRow(t, h) +} + +func TestCopilotInstructionsFollowCopilotHome(t *testing.T) { + h := sandbox(t) + noCopilotCLI(t, h) + vscodeUser(t, h) + d := filepath.Join(h, "ch") + t.Setenv("COPILOT_HOME", d) + c := targetByID("copilot") + if c == nil { + t.Fatalf("no copilot target: %v", ids(Targets())) + } + if want := filepath.Join(d, "copilot-instructions.md"); c.Instructions != want { + t.Errorf("instructions go to %q, want %q", c.Instructions, want) + } +} diff --git a/internal/library/targets.go b/internal/library/targets.go index 06ec6b117..5e7d7f8bc 100644 --- a/internal/library/targets.go +++ b/internal/library/targets.go @@ -997,8 +997,11 @@ func targetsAsleep(gone bool) ([]*Target, []*agent.Agent) { } // an uninstall that left the folder behind (#2177, #2173): the // Agents page calls the agent gone and offers none of its settings, - // since nothing reads them; the library gives nothing to it either - if !gone && a.CLIMissing() { + // since nothing reads them; the library gives nothing to it either, + // unless an app of its vendor's reads the folder too: VS Code's + // Copilot reads ~/.copilot's copilot-instructions.md with no + // Copilot CLI here + if !gone && a.CLIMissing() && !a.SharedRead() { continue } if isAsleep(a) {