[Skills] Multi-scope skill enablement and shadowing fix (#15953)

This commit is contained in:
N. Taylor Mullen
2026-01-06 20:24:29 -08:00
committed by GitHub
parent 7956eb239e
commit 2d683bb6f8
5 changed files with 154 additions and 64 deletions
+53 -10
View File
@@ -11,7 +11,6 @@ import {
loadSettings, loadSettings,
SettingScope, SettingScope,
type LoadedSettings, type LoadedSettings,
type LoadableSettingScope,
} from '../../config/settings.js'; } from '../../config/settings.js';
const emitConsoleLog = vi.hoisted(() => vi.fn()); const emitConsoleLog = vi.hoisted(() => vi.fn());
@@ -58,8 +57,14 @@ describe('skills enable command', () => {
describe('handleEnable', () => { describe('handleEnable', () => {
it('should enable a disabled skill in user scope', async () => { it('should enable a disabled skill in user scope', async () => {
const mockSettings = { const mockSettings = {
forScope: vi.fn().mockReturnValue({ forScope: vi.fn().mockImplementation((scope) => {
settings: { skills: { disabled: ['skill1'] } }, if (scope === SettingScope.User) {
return {
settings: { skills: { disabled: ['skill1'] } },
path: '/user/settings.json',
};
}
return { settings: {}, path: '/project/settings.json' };
}), }),
setValue: vi.fn(), setValue: vi.fn(),
}; };
@@ -67,10 +72,7 @@ describe('skills enable command', () => {
mockSettings as unknown as LoadedSettings, mockSettings as unknown as LoadedSettings,
); );
await handleEnable({ await handleEnable({ name: 'skill1' });
name: 'skill1',
scope: SettingScope.User as LoadableSettingScope,
});
expect(mockSettings.setValue).toHaveBeenCalledWith( expect(mockSettings.setValue).toHaveBeenCalledWith(
SettingScope.User, SettingScope.User,
@@ -79,7 +81,48 @@ describe('skills enable command', () => {
); );
expect(emitConsoleLog).toHaveBeenCalledWith( expect(emitConsoleLog).toHaveBeenCalledWith(
'log', 'log',
'Skill "skill1" enabled by removing it from the disabled list in user settings.', 'Skill "skill1" enabled by removing it from the disabled list in user and project settings.',
);
});
it('should enable a skill across multiple scopes', async () => {
const mockSettings = {
forScope: vi.fn().mockImplementation((scope) => {
if (scope === SettingScope.User) {
return {
settings: { skills: { disabled: ['skill1'] } },
path: '/user/settings.json',
};
}
if (scope === SettingScope.Workspace) {
return {
settings: { skills: { disabled: ['skill1'] } },
path: '/workspace/settings.json',
};
}
return { settings: {}, path: '' };
}),
setValue: vi.fn(),
};
mockLoadSettings.mockReturnValue(
mockSettings as unknown as LoadedSettings,
);
await handleEnable({ name: 'skill1' });
expect(mockSettings.setValue).toHaveBeenCalledWith(
SettingScope.User,
'skills.disabled',
[],
);
expect(mockSettings.setValue).toHaveBeenCalledWith(
SettingScope.Workspace,
'skills.disabled',
[],
);
expect(emitConsoleLog).toHaveBeenCalledWith(
'log',
'Skill "skill1" enabled by removing it from the disabled list in project and user settings.',
); );
}); });
@@ -91,11 +134,11 @@ describe('skills enable command', () => {
}), }),
setValue: vi.fn(), setValue: vi.fn(),
}; };
vi.mocked(loadSettings).mockReturnValue( mockLoadSettings.mockReturnValue(
mockSettings as unknown as LoadedSettings, mockSettings as unknown as LoadedSettings,
); );
await handleEnable({ name: 'skill1', scope: SettingScope.User }); await handleEnable({ name: 'skill1' });
expect(mockSettings.setValue).not.toHaveBeenCalled(); expect(mockSettings.setValue).not.toHaveBeenCalled();
expect(emitConsoleLog).toHaveBeenCalledWith( expect(emitConsoleLog).toHaveBeenCalledWith(
+8 -24
View File
@@ -5,11 +5,7 @@
*/ */
import type { CommandModule } from 'yargs'; import type { CommandModule } from 'yargs';
import { import { loadSettings } from '../../config/settings.js';
loadSettings,
SettingScope,
type LoadableSettingScope,
} from '../../config/settings.js';
import { debugLogger } from '@google/gemini-cli-core'; import { debugLogger } from '@google/gemini-cli-core';
import { exitCli } from '../utils.js'; import { exitCli } from '../utils.js';
import { enableSkill } from '../../utils/skillSettings.js'; import { enableSkill } from '../../utils/skillSettings.js';
@@ -17,15 +13,14 @@ import { renderSkillActionFeedback } from '../../utils/skillUtils.js';
interface EnableArgs { interface EnableArgs {
name: string; name: string;
scope: LoadableSettingScope;
} }
export async function handleEnable(args: EnableArgs) { export async function handleEnable(args: EnableArgs) {
const { name, scope } = args; const { name } = args;
const workspaceDir = process.cwd(); const workspaceDir = process.cwd();
const settings = loadSettings(workspaceDir); const settings = loadSettings(workspaceDir);
const result = enableSkill(settings, name, scope); const result = enableSkill(settings, name);
debugLogger.log(renderSkillActionFeedback(result, (label, _path) => label)); debugLogger.log(renderSkillActionFeedback(result, (label, _path) => label));
} }
@@ -33,25 +28,14 @@ export const enableCommand: CommandModule = {
command: 'enable <name>', command: 'enable <name>',
describe: 'Enables an agent skill.', describe: 'Enables an agent skill.',
builder: (yargs) => builder: (yargs) =>
yargs yargs.positional('name', {
.positional('name', { describe: 'The name of the skill to enable.',
describe: 'The name of the skill to enable.', type: 'string',
type: 'string', demandOption: true,
demandOption: true, }),
})
.option('scope', {
alias: 's',
describe: 'The scope to enable the skill in (user or project).',
type: 'string',
default: 'user',
choices: ['user', 'project'],
}),
handler: async (argv) => { handler: async (argv) => {
const scope: LoadableSettingScope =
argv['scope'] === 'project' ? SettingScope.Workspace : SettingScope.User;
await handleEnable({ await handleEnable({
name: argv['name'] as string, name: argv['name'] as string,
scope,
}); });
await exitCli(); await exitCli();
}, },
@@ -193,7 +193,6 @@ describe('skillsCommand', () => {
(s) => s.name === 'enable', (s) => s.name === 'enable',
)!; )!;
context.services.settings.merged.skills = { disabled: ['skill1'] }; context.services.settings.merged.skills = { disabled: ['skill1'] };
// Also need to mock the scope-specific disabled list
( (
context.services.settings as unknown as { context.services.settings as unknown as {
workspace: { settings: { skills: { disabled: string[] } } }; workspace: { settings: { skills: { disabled: string[] } } };
@@ -212,7 +211,47 @@ describe('skillsCommand', () => {
expect(context.ui.addItem).toHaveBeenCalledWith( expect(context.ui.addItem).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
type: MessageType.INFO, type: MessageType.INFO,
text: 'Skill "skill1" enabled by removing it from the disabled list in project settings. Use "/skills reload" for it to take effect.', text: 'Skill "skill1" enabled by removing it from the disabled list in project and user settings. Use "/skills reload" for it to take effect.',
}),
expect.any(Number),
);
});
it('should enable a skill across multiple scopes', async () => {
const enableCmd = skillsCommand.subCommands!.find(
(s) => s.name === 'enable',
)!;
(
context.services.settings as unknown as {
user: { settings: { skills: { disabled: string[] } } };
}
).user.settings = {
skills: { disabled: ['skill1'] },
};
(
context.services.settings as unknown as {
workspace: { settings: { skills: { disabled: string[] } } };
}
).workspace.settings = {
skills: { disabled: ['skill1'] },
};
await enableCmd.action!(context, 'skill1');
expect(context.services.settings.setValue).toHaveBeenCalledWith(
SettingScope.User,
'skills.disabled',
[],
);
expect(context.services.settings.setValue).toHaveBeenCalledWith(
SettingScope.Workspace,
'skills.disabled',
[],
);
expect(context.ui.addItem).toHaveBeenCalledWith(
expect.objectContaining({
type: MessageType.INFO,
text: 'Skill "skill1" enabled by removing it from the disabled list in project and user settings. Use "/skills reload" for it to take effect.',
}), }),
expect.any(Number), expect.any(Number),
); );
@@ -127,11 +127,7 @@ async function enableAction(
return; return;
} }
const scope = context.services.settings.workspace.path const result = enableSkill(context.services.settings, skillName);
? SettingScope.Workspace
: SettingScope.User;
const result = enableSkill(context.services.settings, skillName, scope);
let feedback = renderSkillActionFeedback( let feedback = renderSkillActionFeedback(
result, result,
+51 -23
View File
@@ -5,8 +5,8 @@
*/ */
import { import {
SettingScope,
isLoadableSettingScope, isLoadableSettingScope,
type SettingScope,
type LoadedSettings, type LoadedSettings,
} from '../config/settings.js'; } from '../config/settings.js';
@@ -33,47 +33,57 @@ export interface SkillActionResult {
} }
/** /**
* Enables a skill by removing it from the specified disabled list. * Enables a skill by removing it from all writable disabled lists (User and Workspace).
*/ */
export function enableSkill( export function enableSkill(
settings: LoadedSettings, settings: LoadedSettings,
skillName: string, skillName: string,
scope: SettingScope,
): SkillActionResult { ): SkillActionResult {
if (!isLoadableSettingScope(scope)) { const writableScopes = [SettingScope.Workspace, SettingScope.User];
return { const foundInDisabledScopes: ModifiedScope[] = [];
status: 'error', const alreadyEnabledScopes: ModifiedScope[] = [];
skillName,
action: 'enable', for (const scope of writableScopes) {
modifiedScopes: [], if (isLoadableSettingScope(scope)) {
alreadyInStateScopes: [], const scopePath = settings.forScope(scope).path;
error: `Invalid settings scope: ${scope}`, const scopeDisabled = settings.forScope(scope).settings.skills?.disabled;
}; if (scopeDisabled?.includes(skillName)) {
foundInDisabledScopes.push({ scope, path: scopePath });
} else {
alreadyEnabledScopes.push({ scope, path: scopePath });
}
}
} }
const scopePath = settings.forScope(scope).path; if (foundInDisabledScopes.length === 0) {
const currentScopeDisabled =
settings.forScope(scope).settings.skills?.disabled ?? [];
if (!currentScopeDisabled.includes(skillName)) {
return { return {
status: 'no-op', status: 'no-op',
skillName, skillName,
action: 'enable', action: 'enable',
modifiedScopes: [], modifiedScopes: [],
alreadyInStateScopes: [{ scope, path: scopePath }], alreadyInStateScopes: alreadyEnabledScopes,
}; };
} }
const newDisabled = currentScopeDisabled.filter((name) => name !== skillName); const modifiedScopes: ModifiedScope[] = [];
settings.setValue(scope, 'skills.disabled', newDisabled); for (const { scope, path } of foundInDisabledScopes) {
if (isLoadableSettingScope(scope)) {
const currentScopeDisabled =
settings.forScope(scope).settings.skills?.disabled ?? [];
const newDisabled = currentScopeDisabled.filter(
(name) => name !== skillName,
);
settings.setValue(scope, 'skills.disabled', newDisabled);
modifiedScopes.push({ scope, path });
}
}
return { return {
status: 'success', status: 'success',
skillName, skillName,
action: 'enable', action: 'enable',
modifiedScopes: [{ scope, path: scopePath }], modifiedScopes,
alreadyInStateScopes: [], alreadyInStateScopes: alreadyEnabledScopes,
}; };
} }
@@ -110,6 +120,24 @@ export function disableSkill(
}; };
} }
// Check if it's already disabled in the other writable scope
const otherScope =
scope === SettingScope.Workspace
? SettingScope.User
: SettingScope.Workspace;
const alreadyDisabledInOther: ModifiedScope[] = [];
if (isLoadableSettingScope(otherScope)) {
const otherScopeDisabled =
settings.forScope(otherScope).settings.skills?.disabled;
if (otherScopeDisabled?.includes(skillName)) {
alreadyDisabledInOther.push({
scope: otherScope,
path: settings.forScope(otherScope).path,
});
}
}
const newDisabled = [...currentScopeDisabled, skillName]; const newDisabled = [...currentScopeDisabled, skillName];
settings.setValue(scope, 'skills.disabled', newDisabled); settings.setValue(scope, 'skills.disabled', newDisabled);
@@ -118,6 +146,6 @@ export function disableSkill(
skillName, skillName,
action: 'disable', action: 'disable',
modifiedScopes: [{ scope, path: scopePath }], modifiedScopes: [{ scope, path: scopePath }],
alreadyInStateScopes: [], alreadyInStateScopes: alreadyDisabledInOther,
}; };
} }