diff --git a/core/llm/rules/alwaysApplyRules.vitest.ts b/core/llm/rules/alwaysApplyRules.vitest.ts new file mode 100644 index 000000000..a20346504 --- /dev/null +++ b/core/llm/rules/alwaysApplyRules.vitest.ts @@ -0,0 +1,137 @@ +import { describe, expect, it } from 'vitest'; +import { ContextItemWithId, RuleWithSource, UserChatMessage } from '../..'; +import { getApplicableRules } from './getSystemMessageWithRules'; + +describe('Rule application with alwaysApply', () => { + // Create an always-apply rule + const alwaysApplyRule: RuleWithSource = { + name: 'Always Apply Rule', + rule: 'This rule should always be applied', + alwaysApply: true, + source: 'rules-block', + ruleFile: '.continue/always-apply.md', + }; + + // Create a colocated rule in a nested directory + const nestedDirRule: RuleWithSource = { + name: 'Nested Directory Rule', + rule: 'This rule applies to files in the nested directory', + source: 'rules-block', + ruleFile: 'nested-folder/rules.md', + }; + + it('should apply alwaysApply rules even with no file references', () => { + // Message with no code blocks or file references + const simpleMessage: UserChatMessage = { + id: '1', + role: 'user', + content: 'Can you help me understand how this works?', + createdAt: new Date().toISOString(), + }; + + // Apply rules with no context items + const applicableRules = getApplicableRules( + simpleMessage, + [alwaysApplyRule, nestedDirRule], + [] + ); + + // The always apply rule should be included regardless of context + expect(applicableRules).toHaveLength(1); + expect(applicableRules.map(r => r.name)).toContain('Always Apply Rule'); + expect(applicableRules.map(r => r.name)).not.toContain('Nested Directory Rule'); + }); + + it('should apply nested directory rules to files in that directory', () => { + // Context with a file in the nested directory + const nestedFileContext: ContextItemWithId = { + id: 'context1', + uri: { type: 'file', value: 'nested-folder/example.ts' }, + content: 'export const example = () => {...}', + retrievedAt: new Date().toISOString(), + }; + + // Apply rules with file context in nested directory + const applicableRules = getApplicableRules( + undefined, // No message needed + [alwaysApplyRule, nestedDirRule], + [nestedFileContext] + ); + + // Both rules should apply + expect(applicableRules).toHaveLength(2); + expect(applicableRules.map(r => r.name)).toContain('Always Apply Rule'); + expect(applicableRules.map(r => r.name)).toContain('Nested Directory Rule'); + }); + + it('should NOT apply nested directory rules to files outside that directory', () => { + // Context with a file outside the nested directory + const outsideFileContext: ContextItemWithId = { + id: 'context2', + uri: { type: 'file', value: 'src/utils/helper.ts' }, + content: 'export const helper = () => {...}', + retrievedAt: new Date().toISOString(), + }; + + // Apply rules with file context outside nested directory + const applicableRules = getApplicableRules( + undefined, // No message needed + [alwaysApplyRule, nestedDirRule], + [outsideFileContext] + ); + + // Only the always apply rule should be included + expect(applicableRules).toHaveLength(1); + expect(applicableRules.map(r => r.name)).toContain('Always Apply Rule'); + expect(applicableRules.map(r => r.name)).not.toContain('Nested Directory Rule'); + }); + + it('should correctly apply rules when a file is mentioned in a message', () => { + // Message with a file reference + const messageWithFile: UserChatMessage = { + id: '3', + role: 'user', + content: 'Can you help with this file?\n```ts nested-folder/example.ts\nexport const example = () => {...}\n```', + createdAt: new Date().toISOString(), + }; + + // Apply rules with a message containing a file reference + const applicableRules = getApplicableRules( + messageWithFile, + [alwaysApplyRule, nestedDirRule], + [] + ); + + // Both rules should apply + expect(applicableRules).toHaveLength(2); + expect(applicableRules.map(r => r.name)).toContain('Always Apply Rule'); + expect(applicableRules.map(r => r.name)).toContain('Nested Directory Rule'); + }); + + it('should always apply rules with alwaysApply regardless of message or context', () => { + // Test with no message or context + let applicableRules = getApplicableRules( + undefined, + [alwaysApplyRule], + [] + ); + expect(applicableRules).toHaveLength(1); + expect(applicableRules.map(r => r.name)).toContain('Always Apply Rule'); + + // Test with message but no file references and no context + const simpleMessage: UserChatMessage = { + id: '4', + role: 'user', + content: 'Hello, can you help me?', + createdAt: new Date().toISOString(), + }; + + applicableRules = getApplicableRules( + simpleMessage, + [alwaysApplyRule], + [] + ); + expect(applicableRules).toHaveLength(1); + expect(applicableRules.map(r => r.name)).toContain('Always Apply Rule'); + }); +}); \ No newline at end of file diff --git a/core/llm/rules/getSystemMessageWithRules.ts b/core/llm/rules/getSystemMessageWithRules.ts index 3642b880b..7d3ce7dee 100644 --- a/core/llm/rules/getSystemMessageWithRules.ts +++ b/core/llm/rules/getSystemMessageWithRules.ts @@ -114,11 +114,6 @@ export const shouldApplyRule = ( rule: RuleWithSource, filePaths: string[], ): boolean => { - // If there are no file paths to check, we can't apply directory-specific rules - if (filePaths.length === 0) { - return rule.alwaysApply === true; - } - // If alwaysApply is explicitly true, always apply the rule regardless of file paths if (rule.alwaysApply === true) { return true; @@ -135,7 +130,6 @@ export const shouldApplyRule = ( const filesInRuleDirectory = filePaths.filter((filePath) => isFileInDirectory(filePath, ruleDirectory), ); - // If no files are in this directory, don't apply the rule if (filesInRuleDirectory.length === 0) { return false; @@ -185,6 +179,10 @@ export const getApplicableRules = ( rules: RuleWithSource[], contextItems: ContextItemWithId[], ): RuleWithSource[] => { + // First, extract any rules that should always apply regardless of context + const alwaysApplyRules = rules.filter((rule) => rule.alwaysApply === true); + + // Get file paths from message and context for regular rule matching const filePathsFromMessage = userMessage ? extractPathsFromCodeBlocks(renderChatMessage(userMessage)) : []; @@ -193,10 +191,22 @@ export const getApplicableRules = ( const filePathsFromContextItems = contextItems .filter((item) => item.uri?.type === "file" && item.uri?.value) .map((item) => item.uri!.value); + // Combine file paths from both sources const allFilePaths = [...filePathsFromMessage, ...filePathsFromContextItems]; - return rules.filter((rule) => shouldApplyRule(rule, allFilePaths)); + // If we have no file paths and no always-apply rules, return an empty array + if (allFilePaths.length === 0) { + return alwaysApplyRules; + } + + // Get rules that match file paths + const matchingRules = rules + .filter((rule) => rule.alwaysApply !== true) // Skip always-apply rules as we've already handled them + .filter((rule) => shouldApplyRule(rule, allFilePaths)); + + // Combine always-apply rules with matching rules, ensuring no duplicates + return [...alwaysApplyRules, ...matchingRules]; }; /**