diff --git a/apps/server/src/provider/Drivers/AntigravityDriver.ts b/apps/server/src/provider/Drivers/AntigravityDriver.ts index 2491b5ef46442..6d9361d3e6a03 100644 --- a/apps/server/src/provider/Drivers/AntigravityDriver.ts +++ b/apps/server/src/provider/Drivers/AntigravityDriver.ts @@ -566,7 +566,7 @@ export const AntigravityDriver: ProviderDriver< snapshotForCwd: (cwd) => !enabled ? provider.snapshot.getSnapshot - : discoverAntigravitySkills({ cwd, userHome }).pipe( + : discoverAntigravitySkills({ cwd, userHome, profileDirectory }).pipe( Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(Path.Path, path), Effect.flatMap((skills) => provider.snapshotForCwd(cwd, skills)), diff --git a/apps/server/src/provider/Drivers/AntigravitySkills.test.ts b/apps/server/src/provider/Drivers/AntigravitySkills.test.ts index c81b38a00dbcb..73c85146a460e 100644 --- a/apps/server/src/provider/Drivers/AntigravitySkills.test.ts +++ b/apps/server/src/provider/Drivers/AntigravitySkills.test.ts @@ -25,6 +25,7 @@ const makeWorkspace = Effect.fn("makeWorkspace")(function* () { return { cwd: path.join(temporaryDirectory, "workspace"), userHome: path.join(temporaryDirectory, "home"), + profileDirectory: path.join(temporaryDirectory, "profile"), }; }); @@ -93,6 +94,85 @@ it.layer(NodeServices.layer)("discoverAntigravitySkills", (it) => { }), ); + it.effect("reads skills from enabled plugins under their own names", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const input = yield* makeWorkspace(); + const config = path.join(input.profileDirectory, "config"); + const expected = []; + for (const name of ["alpha", "beta", "broken", "off", "disabled", "unregistered"]) { + const plugin = path.join(config, "plugins", name); + const skillPath = yield* writeSkill( + path.join(plugin, "skills", "review"), + `---\nname: ${name}-review\ndescription: Review changes.\n---\n`, + ); + if (name !== "unregistered") { + yield* fileSystem.writeFileString( + path.join(plugin, "plugin.json"), + name === "off" || name === "broken" + ? "{ invalid" + : JSON.stringify({ + name: `--- Display ${name} ---`, + disabled: name === "beta" || name === "disabled", + }), + ); + } + if (name === "alpha" || name === "beta") { + expected.push({ + name: `${name}-review`, + description: "Review changes.", + path: skillPath, + scope: "user", + enabled: true, + }); + } + } + yield* fileSystem.writeFileString( + path.join(config, "config.json"), + JSON.stringify({ plugins: { beta: { enabled: true }, off: { enabled: false } } }), + ); + // agy in a T3 thread cannot load plugins outside its profile, so the picker must not offer them. + const homePlugin = path.join(input.userHome, ".gemini", "config", "plugins", "home"); + yield* writeSkill(path.join(homePlugin, "skills", "review"), "---\nname: home-review\n---\n"); + yield* fileSystem.writeFileString(path.join(homePlugin, "plugin.json"), "{}"); + for (const [directory, scope] of [ + [path.join(input.cwd, ".agents", "skills"), "project"], + [path.join(input.userHome, ".gemini", "config", "skills"), "user"], + ] as const) { + const name = `standalone-${scope}`; + const skillPath = yield* writeSkill( + path.join(directory, name), + `---\nname: ${name}\n---\n`, + ); + expected.push({ name, path: skillPath, scope, enabled: true }); + } + assert.deepEqual(yield* discoverAntigravitySkills(input), expected); + for (const contents of ["{ invalid", '{"plugins":{"alpha":{"enabled":"yes"}}}']) { + yield* fileSystem.writeFileString(path.join(config, "config.json"), contents); + assert.deepEqual(yield* discoverAntigravitySkills(input), expected.slice(2)); + } + }), + ); + + it.effect("keeps standalone skills when plugin discovery fails", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const input = yield* makeWorkspace(); + const skillPath = yield* writeSkill( + path.join(input.userHome, ".gemini", "config", "skills", "standalone"), + "---\nname: standalone\n---\n", + ); + const plugin = path.join(input.profileDirectory, "config", "plugins", "broken"); + yield* writeSkill(path.join(plugin, "skills", "review"), "---\nname: review\n---\n"); + yield* fileSystem.writeFileString(path.join(plugin, "plugin.json"), "x".repeat(1_000_001)); + assert.deepEqual(yield* discoverAntigravitySkills(input), [ + { name: "standalone", path: skillPath, scope: "user", enabled: true }, + ]); + }), + ); + it.effect("uses native root order for duplicate names", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/apps/server/src/provider/Drivers/AntigravitySkills.ts b/apps/server/src/provider/Drivers/AntigravitySkills.ts index 96e1fe61109f4..45a34cd394f05 100644 --- a/apps/server/src/provider/Drivers/AntigravitySkills.ts +++ b/apps/server/src/provider/Drivers/AntigravitySkills.ts @@ -162,32 +162,17 @@ const readSkillFrontmatter = Effect.fn("readAntigravitySkill")(function* ( return bytes.toString("utf8", 0, frontmatterEnd); }); -/** Keep read errors typed so workspace snapshots cannot cache partial results. */ -export const discoverAntigravitySkills = Effect.fn("discoverAntigravitySkills")(function* (input: { - readonly cwd: string; - readonly userHome: string; -}): Effect.fn.Return< - ReadonlyArray, - AntigravitySkillsProbeError, - FileSystem.FileSystem | Path.Path -> { +interface SkillRoot { + readonly directory: string; + readonly scope: string; +} + +const scanSkillDirectories = Effect.fn("scanAntigravitySkillDirectories")(function* ( + roots: ReadonlyArray, + budget: ScanBudget, +) { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; - const [configSkills, cliSkills] = antigravityUserSkillDirectories( - path, - path.join(input.userHome, ".gemini"), - ); - const roots = [ - { directory: configSkills, scope: "user" }, - { directory: path.resolve(input.cwd, ".gemini", "skills"), scope: "project" }, - { directory: cliSkills, scope: "user" }, - { directory: path.resolve(input.cwd, ".agents", "skills"), scope: "project" }, - { directory: path.resolve(input.cwd, ".agent", "skills"), scope: "project" }, - ]; - const budget: ScanBudget = { - remainingBytes: MAX_SCAN_BYTES, - remainingEntries: MAX_SCAN_ENTRIES, - }; const skillsByName = new Map(); const scanDirectory = Effect.fn("scanAntigravitySkillDirectory")(function* ( @@ -241,3 +226,139 @@ export const discoverAntigravitySkills = Effect.fn("discoverAntigravitySkills")( } return [...skillsByName.values()].sort((left, right) => left.name.localeCompare(right.name)); }); + +const readJsonFile = Effect.fn("readAntigravityPluginConfig")(function* ( + filePath: string, + budget: ScanBudget, +) { + const fileSystem = yield* FileSystem.FileSystem; + const info = yield* readIfPresent(fileSystem.stat(filePath), filePath); + if (info?.type !== "File") return undefined; + if (info.size > BigInt(Math.min(MAX_SKILL_BYTES, budget.remainingBytes))) { + return yield* new AntigravitySkillsProbeError({ + reason: "scan-budget-exhausted", + path: filePath, + }); + } + const contents = yield* readIfPresent(fileSystem.readFileString(filePath), filePath); + if (contents === undefined) return undefined; + budget.remainingBytes -= Buffer.byteLength(contents); + return contents; +}); + +const PluginManifest = Schema.fromJsonString( + Schema.Struct({ disabled: Schema.optional(Schema.Boolean) }), +); +const PluginPreferences = Schema.fromJsonString( + Schema.Struct({ + plugins: Schema.optional( + Schema.Record(Schema.String, Schema.Struct({ enabled: Schema.optional(Schema.Boolean) })), + ), + }), +); + +const decodePluginManifest = Schema.decodeEffect(PluginManifest); +const decodePluginPreferences = Schema.decodeEffect(PluginPreferences); + +/** agy runs with GEMINI_HOME set to the instance profile, so it loads plugins from there only. */ +const discoverAntigravityPlugins = Effect.fn("discoverAntigravityPlugins")(function* ( + profileDirectory: string, +) { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const config = path.join(profileDirectory, "config"); + const directory = path.join(config, "plugins"); + const entries = yield* readIfPresent(fileSystem.readDirectory(directory), directory); + if (entries === undefined) return []; + const budget: ScanBudget = { + remainingBytes: MAX_SCAN_BYTES, + remainingEntries: MAX_SCAN_ENTRIES, + }; + if (entries.length > budget.remainingEntries) { + return yield* new AntigravitySkillsProbeError({ + reason: "scan-budget-exhausted", + path: directory, + }); + } + budget.remainingEntries -= entries.length; + const preferencesPath = path.join(config, "config.json"); + const preferencesContents = yield* readJsonFile(preferencesPath, budget); + const preferences = + preferencesContents === undefined + ? { plugins: {} } + : yield* decodePluginPreferences(preferencesContents).pipe( + Effect.catchTags({ + SchemaError: () => + Effect.logWarning("Antigravity plugin preferences are invalid.", { + errorTag: "invalid-config", + }).pipe(Effect.as(undefined)), + }), + ); + if (preferences === undefined) return []; + const roots: Array = []; + for (const entry of entries.toSorted()) { + const pluginDirectory = path.join(directory, entry); + if ( + (yield* readIfPresent(fileSystem.stat(pluginDirectory), pluginDirectory))?.type !== + "Directory" + ) + continue; + const enabled = preferences.plugins?.[entry]?.enabled; + if (enabled === false) continue; + const manifestPath = path.join(pluginDirectory, "plugin.json"); + const contents = yield* readJsonFile(manifestPath, budget); + if (contents === undefined) continue; + const manifest = yield* decodePluginManifest(contents).pipe( + Effect.catchTags({ + SchemaError: () => + Effect.logWarning("Skipped an Antigravity plugin with an invalid manifest.", { + errorTag: "invalid-config", + }).pipe(Effect.as(undefined)), + }), + ); + if (manifest === undefined) continue; + if (manifest.disabled && enabled !== true) continue; + roots.push({ directory: path.join(pluginDirectory, "skills"), scope: "user" }); + } + return yield* scanSkillDirectories(roots, budget); +}); + +/** Keep read errors typed so workspace snapshots cannot cache partial results. */ +export const discoverAntigravitySkills = Effect.fn("discoverAntigravitySkills")(function* (input: { + readonly cwd: string; + readonly userHome: string; + readonly profileDirectory: string; +}): Effect.fn.Return< + ReadonlyArray, + AntigravitySkillsProbeError, + FileSystem.FileSystem | Path.Path +> { + const path = yield* Path.Path; + const [configSkills, cliSkills] = antigravityUserSkillDirectories( + path, + path.join(input.userHome, ".gemini"), + ); + const skills = yield* scanSkillDirectories( + [ + { directory: configSkills, scope: "user" }, + { directory: path.resolve(input.cwd, ".gemini", "skills"), scope: "project" }, + { directory: cliSkills, scope: "user" }, + { directory: path.resolve(input.cwd, ".agents", "skills"), scope: "project" }, + { directory: path.resolve(input.cwd, ".agent", "skills"), scope: "project" }, + ], + { remainingBytes: MAX_SCAN_BYTES, remainingEntries: MAX_SCAN_ENTRIES }, + ); + const pluginSkills = yield* discoverAntigravityPlugins(input.profileDirectory).pipe( + Effect.catchTags({ + AntigravitySkillsProbeError: (cause) => + Effect.logWarning("Antigravity plugin skills are unavailable.", { + reason: cause.reason, + }).pipe(Effect.as([])), + }), + ); + // agy lists plugin skills under their own names, so the picker keeps one entry per name. + const names = new Set(skills.map((skill) => skill.name)); + return [...skills, ...pluginSkills.filter((skill) => !names.has(skill.name))].sort( + (left, right) => left.name.localeCompare(right.name), + ); +});