add confirmation if removing ALL rules profiles, and add --force flag on rules remove

This commit is contained in:
Joe Danziger
2025-05-26 22:09:45 -04:00
parent bd81d00169
commit 36e8257d08
7 changed files with 318 additions and 5 deletions

View File

@@ -15,6 +15,10 @@ import {
} from '../../../../src/utils/rule-transformer.js';
import { RULES_PROFILES } from '../../../../src/constants/profiles.js';
import { RULES_ACTIONS } from '../../../../src/constants/rules-actions.js';
import {
wouldRemovalLeaveNoProfiles,
getInstalledRulesProfiles
} from '../../../../src/utils/rules-detection.js';
import path from 'path';
import fs from 'fs';
@@ -32,7 +36,7 @@ import fs from 'fs';
export async function rulesDirect(args, log, context = {}) {
enableSilentMode();
try {
const { action, profiles, projectRoot, yes } = args;
const { action, profiles, projectRoot, yes, force } = args;
if (
!action ||
!Array.isArray(profiles) ||
@@ -52,6 +56,21 @@ export async function rulesDirect(args, log, context = {}) {
const addResults = [];
if (action === RULES_ACTIONS.REMOVE) {
// Safety check: Ensure this won't remove all rules profiles (unless forced)
if (!force && wouldRemovalLeaveNoProfiles(projectRoot, profiles)) {
const installedProfiles = getInstalledRulesProfiles(projectRoot);
const remainingProfiles = installedProfiles.filter(
(profile) => !profiles.includes(profile)
);
return {
success: false,
error: {
code: 'CRITICAL_REMOVAL_BLOCKED',
message: `CRITICAL: This operation would remove ALL remaining rules profiles (${profiles.join(', ')}), leaving your project with no rules configurations. This could significantly impact functionality. Currently installed profiles: ${installedProfiles.join(', ')}. If you're certain you want to proceed, set force: true or use the CLI with --force flag.`
}
};
}
for (const profile of profiles) {
if (!isValidProfile(profile)) {
removalResults.push({

View File

@@ -34,6 +34,13 @@ export function registerRulesTool(server) {
.string()
.describe(
'The root directory of the project. Must be an absolute path.'
),
force: z
.boolean()
.optional()
.default(false)
.describe(
'DANGEROUS: Force removal even if it would leave no rules profiles. Only use if you are absolutely certain.'
)
}),
execute: withNormalizedProjectRoot(async (args, { log, session }) => {