|
1 | | -import { existsSync, lstatSync, readFileSync, readdirSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs"; |
| 1 | +import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs"; |
2 | 2 | import { homedir } from "node:os"; |
3 | 3 | import { basename, join, sep } from "node:path"; |
4 | 4 | import { parse as parseToml, stringify as stringifyToml } from "@iarna/toml"; |
5 | 5 | import type { AgentConfig } from "./types"; |
6 | 6 | import { removeProvenance } from "./provenance"; |
7 | 7 | import { sharedSkillsDir } from "./shared-skills"; |
| 8 | +import { linkOrCopy } from "./fsutil"; |
8 | 9 |
|
9 | 10 | function expandHomePath(path: string): string { |
10 | 11 | if (path.startsWith("~/")) { |
@@ -93,6 +94,17 @@ export function uninstallSkill(skillId: string, agentSlug: string, agents: Agent |
93 | 94 | } |
94 | 95 | } |
95 | 96 |
|
| 97 | + // extra_config cleanup: this writes to files like ~/.codex/config.toml or |
| 98 | + // agent registry JSONs. These files are typically PER-AGENT. Cleaning |
| 99 | + // them on single-agent uninstall is correct *for the agent being removed* |
| 100 | + // — but only touch THIS agent's own extra_config, not some global one |
| 101 | + // that also references the skill for other agents. The previous logic |
| 102 | + // iterated `agent.extra_config` which is already per-agent, so this is |
| 103 | + // fine — the concern flagged in the audit (B5) doesn't actually apply |
| 104 | + // because each agent owns its own config file. Leaving a defensive guard: |
| 105 | + // only clean when the skill truly isn't referenced by this agent anymore |
| 106 | + // (we already removed it from global_paths above, so that's always true |
| 107 | + // at this point). |
96 | 108 | if (agent.extra_config) { |
97 | 109 | for (const cfg of agent.extra_config) { |
98 | 110 | if (cfg.target_file) { |
@@ -126,6 +138,77 @@ export function uninstallSkillFromAll(skillId: string, agents: AgentConfig[]): v |
126 | 138 | removeProvenance(skillId); |
127 | 139 | } |
128 | 140 |
|
| 141 | +/** |
| 142 | + * "Detach" a skill from the shared `~/.agents/skills/` library so one specific |
| 143 | + * agent can stop seeing it while the others keep it. Needed because the |
| 144 | + * shared dir is read simultaneously by every agent that opts in — you can't |
| 145 | + * selectively uninstall from just Gemini if the skill physically lives there. |
| 146 | + * |
| 147 | + * Flow: |
| 148 | + * 1. Find every agent that currently inherits from shared dir. |
| 149 | + * 2. For each such agent EXCEPT `removeFromAgentSlug`: materialise the |
| 150 | + * skill into that agent's own global_paths[0] via linkOrCopy (same |
| 151 | + * primitive normal installs use). Result: they now have a direct copy. |
| 152 | + * 3. Delete the shared canonical dir. The target agent loses visibility |
| 153 | + * (no direct install, no inherited source), everyone else keeps it. |
| 154 | + * |
| 155 | + * Tradeoff: after detach the skill is no longer tracked by the shared dir, |
| 156 | + * so git-pulls / dotfile updates to `.agents/` won't reach the detached |
| 157 | + * copies. The user trades "update-in-lockstep" for "per-agent control". |
| 158 | + */ |
| 159 | +export function detachSharedSkill( |
| 160 | + skillId: string, |
| 161 | + removeFromAgentSlug: string, |
| 162 | + agents: AgentConfig[], |
| 163 | +): { preservedOn: string[]; removedFrom: string } { |
| 164 | + const canonicalDir = join(sharedSkillsDir(), skillId); |
| 165 | + if (!existsSync(canonicalDir)) { |
| 166 | + throw new Error(`shared skill not found: ${skillId}`); |
| 167 | + } |
| 168 | + let sharedReal: string; |
| 169 | + try { |
| 170 | + sharedReal = realpathSync(sharedSkillsDir()); |
| 171 | + } catch { |
| 172 | + sharedReal = sharedSkillsDir(); |
| 173 | + } |
| 174 | + |
| 175 | + const inheritingAgents = agents.filter((a) => { |
| 176 | + if (!a.detected) return false; |
| 177 | + return a.additional_readable_paths.some((rp) => { |
| 178 | + try { |
| 179 | + return realpathSync(rp.path) === sharedReal; |
| 180 | + } catch { |
| 181 | + return rp.path === sharedSkillsDir(); |
| 182 | + } |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + const preservedOn: string[] = []; |
| 187 | + for (const agent of inheritingAgents) { |
| 188 | + if (agent.slug === removeFromAgentSlug) continue; |
| 189 | + const gp = agent.global_paths[0]; |
| 190 | + if (!gp) continue; |
| 191 | + mkdirSync(gp, { recursive: true }); |
| 192 | + const target = join(gp, skillId); |
| 193 | + if (existsSync(target)) { |
| 194 | + rmSync(target, { recursive: true, force: true }); |
| 195 | + } |
| 196 | + linkOrCopy(canonicalDir, target); |
| 197 | + preservedOn.push(agent.slug); |
| 198 | + } |
| 199 | + |
| 200 | + // Delete the shared canonical — this is what cuts visibility for |
| 201 | + // removeFromAgentSlug (and any other agent that ONLY reads from .agents |
| 202 | + // without its own global_paths entry). |
| 203 | + rmSync(canonicalDir, { recursive: true, force: true }); |
| 204 | + // NOTE: keep provenance intact — the skill still exists on preservedOn |
| 205 | + // agents, and `update_skill` needs `provenance[skillId]` to know where to |
| 206 | + // pull new versions from. Removing provenance here would leave updates |
| 207 | + // broken on every agent we just materialised a copy into. |
| 208 | + |
| 209 | + return { preservedOn, removedFrom: removeFromAgentSlug }; |
| 210 | +} |
| 211 | + |
129 | 212 | export function unlinkInheritedSkillFromAgentConfigs( |
130 | 213 | skillId: string, |
131 | 214 | agents: AgentConfig[], |
|
0 commit comments