@rbbtsn0w/adg 0.9.0-dev.pr113.93c1cf1 → 0.9.0-dev.pr114.11f047b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join, resolve, sep } from "node:path";
|
|
4
|
+
import { ANTIGRAVITY_MCP_CONFIG } from "../adapters/antigravity.js";
|
|
5
|
+
import { extractMcpServers, mcpConfigPath } from "../mcp.js";
|
|
6
|
+
import { findManifestFile, readManifest } from "../manifest.js";
|
|
7
|
+
/**
|
|
8
|
+
* @module antigravity-mcp-compat
|
|
9
|
+
*
|
|
10
|
+
* [COMPATIBILITY SHIM / ANTI-CORRUPTION LAYER FOR ANTIGRAVITY RUNTIMES]
|
|
11
|
+
*
|
|
12
|
+
* 存在原因 (Why this exists):
|
|
13
|
+
* Antigravity 采用被动的物理目录扫描机制(Physical-directory discovery model)。
|
|
14
|
+
* 激活插件时,Antigravity 的 Language Server 会在启动阶段自发连接 MCP 并将生成的
|
|
15
|
+
* 工具 Schema 写入各个宿主环境的私有缓存目录(~/.gemini/<marker>/mcp/),并在
|
|
16
|
+
* ~/.gemini/config/config.json 中记录启用状态。
|
|
17
|
+
*
|
|
18
|
+
* 当 ADG 停用、取消链接或卸载插件时,Antigravity 自身缺乏对“外部软链接被删除”进行
|
|
19
|
+
* 自动垃圾回收(Deep GC)的能力,导致残存的工具 Schema 依然被 Antigravity 加载为
|
|
20
|
+
* 有效工具(幽灵工具),引发上下文污染与调用失败。
|
|
21
|
+
*
|
|
22
|
+
* 核心职责 (Scope):
|
|
23
|
+
* 1. 垃圾回收多宿主环境(antigravity, antigravity-cli, antigravity-ide)下残留的 MCP 目录。
|
|
24
|
+
* 2. 严格遵循“全局配置白名单”与“多插件活跃共享白名单”,防止误删用户全局配置与其他插件依赖。
|
|
25
|
+
* 3. 状态对齐 ~/.gemini/config/config.json 中的 plugins.<name>.enabled。
|
|
26
|
+
*
|
|
27
|
+
* 退役 / 演进路线 (Retirement / Evolution Path):
|
|
28
|
+
* 未来本兼容模块可按以下路径逐步抛弃或解耦:
|
|
29
|
+
* 1. 【原生支持退役】:当 Google Antigravity 原生支持了外部目录删除自动清理或官方 CLI 反注册时,
|
|
30
|
+
* 直接删除本文件并移除 src/agents/antigravity.ts 中的两处调用即可。
|
|
31
|
+
* 2. 【收敛至诊断层】:亦可轻松迁移至 `adg doctor` 或 `adg plugins cache prune` 作为独立的孤儿工具清理逻辑。
|
|
32
|
+
*/
|
|
33
|
+
/** Antigravity runtime markers under the Gemini home. */
|
|
34
|
+
const COMPAT_ANTIGRAVITY_MARKERS = ["antigravity", "antigravity-cli", "antigravity-ide"];
|
|
35
|
+
/** Conventional manifest name scanned by Antigravity at plugin roots. */
|
|
36
|
+
const ANTIGRAVITY_MANIFEST = "plugin.json";
|
|
37
|
+
function compatGeminiHome(env = process.env) {
|
|
38
|
+
return env.GEMINI_HOME?.trim() || join(homedir(), ".gemini");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Safely extract all MCP server names declared by a plugin directory, supporting both
|
|
42
|
+
* ADG manifests (.agents/.plugin.json) and native Antigravity configs (mcp_config.json, .mcp.json).
|
|
43
|
+
*/
|
|
44
|
+
function collectPluginServers(pluginDir, exists = existsSync, readFile = readFileSync) {
|
|
45
|
+
const servers = new Set();
|
|
46
|
+
if (!exists(pluginDir))
|
|
47
|
+
return servers;
|
|
48
|
+
const candidates = [];
|
|
49
|
+
try {
|
|
50
|
+
if (findManifestFile(pluginDir)) {
|
|
51
|
+
const manifest = readManifest(pluginDir);
|
|
52
|
+
const mcp = mcpConfigPath(manifest);
|
|
53
|
+
if (mcp)
|
|
54
|
+
candidates.push(resolve(pluginDir, mcp));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// Ignore invalid ADG manifest; continue to probe native files below
|
|
59
|
+
}
|
|
60
|
+
candidates.push(join(pluginDir, ANTIGRAVITY_MCP_CONFIG));
|
|
61
|
+
candidates.push(join(pluginDir, ".mcp.json"));
|
|
62
|
+
for (const candidate of candidates) {
|
|
63
|
+
if (exists(candidate)) {
|
|
64
|
+
try {
|
|
65
|
+
const raw = readFile(candidate, "utf8");
|
|
66
|
+
const parsed = JSON.parse(raw);
|
|
67
|
+
const extracted = extractMcpServers(parsed);
|
|
68
|
+
if (extracted) {
|
|
69
|
+
for (const s of Object.keys(extracted))
|
|
70
|
+
servers.add(s);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// Ignore unparseable candidate
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return servers;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Update the plugin's enablement state in Antigravity's ~/.gemini/config/config.json
|
|
82
|
+
* when that file is present.
|
|
83
|
+
*/
|
|
84
|
+
export function syncAntigravityPluginConfig(name, enabled, env = process.env) {
|
|
85
|
+
const configPath = join(compatGeminiHome(env), "config", "config.json");
|
|
86
|
+
if (!existsSync(configPath))
|
|
87
|
+
return;
|
|
88
|
+
try {
|
|
89
|
+
const raw = readFileSync(configPath, "utf8");
|
|
90
|
+
const parsed = JSON.parse(raw);
|
|
91
|
+
if (parsed &&
|
|
92
|
+
typeof parsed === "object" &&
|
|
93
|
+
!Array.isArray(parsed) &&
|
|
94
|
+
parsed.plugins &&
|
|
95
|
+
typeof parsed.plugins === "object" &&
|
|
96
|
+
!Array.isArray(parsed.plugins)) {
|
|
97
|
+
const plugins = parsed.plugins;
|
|
98
|
+
if (plugins[name] || !enabled) {
|
|
99
|
+
plugins[name] = { ...plugins[name], enabled };
|
|
100
|
+
writeFileSync(configPath, `${JSON.stringify(parsed, null, 2)}\n`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// Best-effort; avoid throwing if config.json is malformed
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Remove Antigravity runtime MCP tool schema directories (<marker>/mcp/) created for
|
|
110
|
+
* a plugin when that plugin is deactivated. Never removes servers declared in the
|
|
111
|
+
* global ~/.gemini/config/mcp_config.json or by other active plugins.
|
|
112
|
+
*/
|
|
113
|
+
export function cleanupAntigravityMcp(name, scanDir, realDir, env = process.env) {
|
|
114
|
+
const home = compatGeminiHome(env);
|
|
115
|
+
// 1. Collect MCP server names declared by this plugin (if realDir is accessible).
|
|
116
|
+
const declaredServers = realDir ? collectPluginServers(realDir) : new Set();
|
|
117
|
+
// 2. Collect servers declared in global mcp_config.json (must NEVER be deleted).
|
|
118
|
+
const globalServers = new Set();
|
|
119
|
+
const globalMcpFile = join(home, "config", "mcp_config.json");
|
|
120
|
+
if (existsSync(globalMcpFile)) {
|
|
121
|
+
try {
|
|
122
|
+
const parsed = JSON.parse(readFileSync(globalMcpFile, "utf8"));
|
|
123
|
+
const servers = extractMcpServers(parsed);
|
|
124
|
+
if (servers) {
|
|
125
|
+
for (const s of Object.keys(servers))
|
|
126
|
+
globalServers.add(s);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
// ignore
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// 3. Collect servers declared by any other active plugin in scanDir.
|
|
134
|
+
const otherPluginServers = new Set();
|
|
135
|
+
if (existsSync(scanDir)) {
|
|
136
|
+
try {
|
|
137
|
+
for (const entry of readdirSync(scanDir)) {
|
|
138
|
+
if (entry === name)
|
|
139
|
+
continue;
|
|
140
|
+
const entryDir = join(scanDir, entry);
|
|
141
|
+
if (existsSync(join(entryDir, ANTIGRAVITY_MANIFEST))) {
|
|
142
|
+
for (const s of collectPluginServers(entryDir)) {
|
|
143
|
+
otherPluginServers.add(s);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
// ignore
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// 4. Sweep each runtime directory's mcp/ folder.
|
|
153
|
+
const prefix = `${name}_`;
|
|
154
|
+
for (const marker of COMPAT_ANTIGRAVITY_MARKERS) {
|
|
155
|
+
const mcpDir = join(home, marker, "mcp");
|
|
156
|
+
if (!existsSync(mcpDir))
|
|
157
|
+
continue;
|
|
158
|
+
try {
|
|
159
|
+
for (const entry of readdirSync(mcpDir)) {
|
|
160
|
+
if (entry.startsWith("."))
|
|
161
|
+
continue;
|
|
162
|
+
const fullPath = join(mcpDir, entry);
|
|
163
|
+
if (!resolve(fullPath).startsWith(resolve(mcpDir) + sep))
|
|
164
|
+
continue;
|
|
165
|
+
// Plugin-prefixed server (e.g. appscope_appscope, xcode_xcrun-mcp): uniquely owned
|
|
166
|
+
if (entry.startsWith(prefix)) {
|
|
167
|
+
rmSync(fullPath, { recursive: true, force: true });
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
// Bare plugin name (e.g. appscope)
|
|
171
|
+
if (entry === name) {
|
|
172
|
+
if (!globalServers.has(entry) && !otherPluginServers.has(entry)) {
|
|
173
|
+
rmSync(fullPath, { recursive: true, force: true });
|
|
174
|
+
}
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
// Bare server name declared by this plugin (e.g. xcrun-mcp)
|
|
178
|
+
if (declaredServers.has(entry)) {
|
|
179
|
+
if (!globalServers.has(entry) && !otherPluginServers.has(entry)) {
|
|
180
|
+
rmSync(fullPath, { recursive: true, force: true });
|
|
181
|
+
}
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
// ignore
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Public facade: Reconcile Antigravity runtime state when a plugin is activated.
|
|
193
|
+
*/
|
|
194
|
+
export function reconcileAntigravityActivation(name, env = process.env) {
|
|
195
|
+
syncAntigravityPluginConfig(name, true, env);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Public facade: Reconcile Antigravity runtime state when a plugin is deactivated.
|
|
199
|
+
*/
|
|
200
|
+
export function reconcileAntigravityDeactivation(name, scanDir, realDir, env = process.env) {
|
|
201
|
+
cleanupAntigravityMcp(name, scanDir, realDir, env);
|
|
202
|
+
syncAntigravityPluginConfig(name, false, env);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Sweep Antigravity runtime directories (<marker>/mcp/) for orphaned tool schema directories
|
|
206
|
+
* left behind by uninstalled or renamed plugins, and align ~/.gemini/config/config.json.
|
|
207
|
+
* Safe: strictly preserves servers in ~/.gemini/config/mcp_config.json and all active plugins.
|
|
208
|
+
*/
|
|
209
|
+
export function pruneStaleAntigravity(opts = {}) {
|
|
210
|
+
const env = opts.env ?? process.env;
|
|
211
|
+
const exists = opts.exists ?? existsSync;
|
|
212
|
+
const readDir = opts.readDir ?? ((p) => (exists(p) ? readdirSync(p) : []));
|
|
213
|
+
const removeDir = opts.removeDir ?? ((p) => rmSync(p, { recursive: true, force: true }));
|
|
214
|
+
const readFile = opts.readFile ?? ((p, enc) => readFileSync(p, enc));
|
|
215
|
+
const writeFile = opts.writeFile ?? ((p, d) => writeFileSync(p, d));
|
|
216
|
+
const home = compatGeminiHome(env);
|
|
217
|
+
const isPresent = COMPAT_ANTIGRAVITY_MARKERS.some((marker) => exists(join(home, marker)));
|
|
218
|
+
if (!isPresent) {
|
|
219
|
+
return { agent: "antigravity", skipped: true, removed: [], errors: [] };
|
|
220
|
+
}
|
|
221
|
+
const removed = [];
|
|
222
|
+
const errors = [];
|
|
223
|
+
// 1. Whitelist: Globally declared servers that must NEVER be deleted.
|
|
224
|
+
const retainedServers = new Set();
|
|
225
|
+
const globalMcpFile = join(home, "config", "mcp_config.json");
|
|
226
|
+
if (exists(globalMcpFile)) {
|
|
227
|
+
try {
|
|
228
|
+
const parsed = JSON.parse(readFile(globalMcpFile, "utf8"));
|
|
229
|
+
const servers = extractMcpServers(parsed);
|
|
230
|
+
if (servers) {
|
|
231
|
+
for (const s of Object.keys(servers))
|
|
232
|
+
retainedServers.add(s);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
// ignore
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
// 2. Whitelist: Servers and plugin names from all active plugins in global scanDir and optional projectPluginsDir.
|
|
240
|
+
const retainedPluginNames = new Set();
|
|
241
|
+
const scanDirs = [join(home, "config", "plugins")];
|
|
242
|
+
if (opts.projectPluginsDir) {
|
|
243
|
+
scanDirs.push(opts.projectPluginsDir);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
const cwdProjectPlugins = resolve(process.cwd(), ".agents", "plugins");
|
|
247
|
+
if (exists(cwdProjectPlugins)) {
|
|
248
|
+
scanDirs.push(cwdProjectPlugins);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
for (const scanDir of scanDirs) {
|
|
252
|
+
if (!exists(scanDir))
|
|
253
|
+
continue;
|
|
254
|
+
try {
|
|
255
|
+
for (const entry of readDir(scanDir)) {
|
|
256
|
+
const entryDir = join(scanDir, entry);
|
|
257
|
+
if (!exists(join(entryDir, ANTIGRAVITY_MANIFEST)))
|
|
258
|
+
continue;
|
|
259
|
+
retainedPluginNames.add(entry);
|
|
260
|
+
for (const s of collectPluginServers(entryDir, exists, readFile)) {
|
|
261
|
+
retainedServers.add(s);
|
|
262
|
+
retainedServers.add(`${entry}_${s}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
// ignore
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// 3. Sweep all runtime marker mcp directories and remove orphans.
|
|
271
|
+
for (const marker of COMPAT_ANTIGRAVITY_MARKERS) {
|
|
272
|
+
const mcpDir = join(home, marker, "mcp");
|
|
273
|
+
if (!exists(mcpDir))
|
|
274
|
+
continue;
|
|
275
|
+
try {
|
|
276
|
+
for (const entry of readDir(mcpDir)) {
|
|
277
|
+
if (entry.startsWith("."))
|
|
278
|
+
continue;
|
|
279
|
+
const fullPath = join(mcpDir, entry);
|
|
280
|
+
if (!resolve(fullPath).startsWith(resolve(mcpDir) + sep))
|
|
281
|
+
continue;
|
|
282
|
+
if (retainedServers.has(entry))
|
|
283
|
+
continue;
|
|
284
|
+
if (retainedPluginNames.has(entry))
|
|
285
|
+
continue;
|
|
286
|
+
try {
|
|
287
|
+
removeDir(fullPath);
|
|
288
|
+
removed.push({ name: entry, path: fullPath });
|
|
289
|
+
}
|
|
290
|
+
catch (err) {
|
|
291
|
+
errors.push(`failed to remove stale Antigravity MCP directory "${fullPath}": ${err instanceof Error ? err.message : String(err)}`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
catch {
|
|
296
|
+
// ignore
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
// 4. Align ~/.gemini/config/config.json plugin enablement states.
|
|
300
|
+
const configPath = join(home, "config", "config.json");
|
|
301
|
+
if (exists(configPath)) {
|
|
302
|
+
try {
|
|
303
|
+
const raw = readFile(configPath, "utf8");
|
|
304
|
+
const parsed = JSON.parse(raw);
|
|
305
|
+
if (parsed &&
|
|
306
|
+
typeof parsed === "object" &&
|
|
307
|
+
!Array.isArray(parsed) &&
|
|
308
|
+
parsed.plugins &&
|
|
309
|
+
typeof parsed.plugins === "object" &&
|
|
310
|
+
!Array.isArray(parsed.plugins)) {
|
|
311
|
+
const plugins = parsed.plugins;
|
|
312
|
+
let modified = false;
|
|
313
|
+
for (const [pName, pConf] of Object.entries(plugins)) {
|
|
314
|
+
if (pConf && pConf.enabled && !retainedPluginNames.has(pName)) {
|
|
315
|
+
pConf.enabled = false;
|
|
316
|
+
modified = true;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (modified) {
|
|
320
|
+
writeFile(configPath, `${JSON.stringify(parsed, null, 2)}\n`);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
// ignore
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return { agent: "antigravity", skipped: false, removed, errors };
|
|
329
|
+
}
|
|
@@ -10,7 +10,9 @@ import { toAntigravityManifest, writeAntigravityMcpConfig } from "../adapters/an
|
|
|
10
10
|
import { writeAntigravityHooks } from "../adapters/antigravity-hooks.js";
|
|
11
11
|
import { applySlotAction, markOwned, observeSlot, reconcileSlot } from "../projection-slot.js";
|
|
12
12
|
import { skippedResult } from "./base.js";
|
|
13
|
+
import { cleanupAntigravityMcp, pruneStaleAntigravity, reconcileAntigravityActivation, reconcileAntigravityDeactivation, syncAntigravityPluginConfig, } from "./antigravity-mcp-compat.js";
|
|
13
14
|
import { resolveSelectionDependencies } from "../types.js";
|
|
15
|
+
export { cleanupAntigravityMcp, pruneStaleAntigravity, reconcileAntigravityActivation, reconcileAntigravityDeactivation, syncAntigravityPluginConfig, };
|
|
14
16
|
/**
|
|
15
17
|
* Antigravity (`agy`) agent — physical-directory model, no CLI.
|
|
16
18
|
*
|
|
@@ -257,8 +259,10 @@ export const antigravityAgent = {
|
|
|
257
259
|
if (!real)
|
|
258
260
|
continue;
|
|
259
261
|
ensureAntigravityRoot(real.dir, real.selection);
|
|
260
|
-
if (exposeAt(scanDir, p, real.dir))
|
|
262
|
+
if (exposeAt(scanDir, p, real.dir)) {
|
|
263
|
+
reconcileAntigravityActivation(p);
|
|
261
264
|
affected.push(p);
|
|
265
|
+
}
|
|
262
266
|
}
|
|
263
267
|
catch (err) {
|
|
264
268
|
console.error(`failed to enable "${p}" in Antigravity:`, err);
|
|
@@ -275,6 +279,12 @@ export const antigravityAgent = {
|
|
|
275
279
|
for (const p of ctx.plugins) {
|
|
276
280
|
const entry = lock[p];
|
|
277
281
|
const realDir = entry ? installedPluginDir(ctx.pluginsDir, p, entry.origin) : undefined;
|
|
282
|
+
const target = join(scanDir, p);
|
|
283
|
+
adoptLegacyExposure(target, p);
|
|
284
|
+
if ((!realDir || resolve(target) !== resolve(realDir)) && observeSlot(target).kind === "foreign") {
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
reconcileAntigravityDeactivation(p, scanDir, realDir);
|
|
278
288
|
removeProjection(scanDir, p, realDir);
|
|
279
289
|
affected.push(p);
|
|
280
290
|
}
|
|
@@ -297,4 +307,10 @@ export const antigravityAgent = {
|
|
|
297
307
|
const names = Object.keys(readLock(lockPath(ctx.pluginsDir)).plugins);
|
|
298
308
|
return names.filter((name) => existsSync(join(scanDir, name, ANTIGRAVITY_MANIFEST)));
|
|
299
309
|
},
|
|
310
|
+
pruneStale() {
|
|
311
|
+
if (!antigravityAgent.available()) {
|
|
312
|
+
return { agent: ID, skipped: true, removed: [], errors: [] };
|
|
313
|
+
}
|
|
314
|
+
return pruneStaleAntigravity();
|
|
315
|
+
},
|
|
300
316
|
};
|
|
@@ -3,8 +3,7 @@ import { allAgents, resolveAgents } from "../agents/index.js";
|
|
|
3
3
|
* `adg plugins prune` — ask every resolved agent to remove its own ADG-owned
|
|
4
4
|
* registrations (marketplaces, cached snapshots, …) that still point at a
|
|
5
5
|
* plugin directory ADG no longer has on disk. Read-only for agents that don't
|
|
6
|
-
* implement `pruneStale` (
|
|
7
|
-
* analogous to prune), which report back `skipped: true`.
|
|
6
|
+
* implement `pruneStale` (which report back `skipped: true`).
|
|
8
7
|
*/
|
|
9
8
|
export function pruneAgents(opts = {}) {
|
|
10
9
|
const agents = opts.agents ?? (opts.targets ? resolveAgents(opts.targets) : allAgents());
|
package/package.json
CHANGED