From df98f9997668e2f65ece63ad1cda0cd5ba7e9bea Mon Sep 17 00:00:00 2001 From: Aditya Mitra <61635505+uinstinct@users.noreply.github.com> Date: Sat, 21 Mar 2026 05:00:11 +0530 Subject: [PATCH] fix(cli): load rules into system message (#10408) * fix(cli): load rules into system message * show loaded rules in config * Update extensions/cli/src/systemMessage.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * fix config service tests * fix tests --------- Co-authored-by: Nate Sesti <33237525+sestinj@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --- extensions/cli/src/__mocks__/systemMessage.ts | 2 + .../cli/src/services/ConfigService.test.ts | 3 + extensions/cli/src/services/ConfigService.ts | 12 +++ extensions/cli/src/systemMessage.ts | 87 +++++++++++++++++++ extensions/cli/vitest.setup.ts | 1 + 5 files changed, 105 insertions(+) diff --git a/extensions/cli/src/__mocks__/systemMessage.ts b/extensions/cli/src/__mocks__/systemMessage.ts index 0599a90db..3907103b1 100644 --- a/extensions/cli/src/__mocks__/systemMessage.ts +++ b/extensions/cli/src/__mocks__/systemMessage.ts @@ -5,3 +5,5 @@ export const constructSystemMessage = vi .mockResolvedValue( "You are an agent in the Continue CLI. Given the user's prompt, you should use the tools available to you to answer the user's question.", ); + +export const loadMarkdownRulesWithMetadata = vi.fn().mockReturnValue([]); diff --git a/extensions/cli/src/services/ConfigService.test.ts b/extensions/cli/src/services/ConfigService.test.ts index 63cbf5e90..05824b92a 100644 --- a/extensions/cli/src/services/ConfigService.test.ts +++ b/extensions/cli/src/services/ConfigService.test.ts @@ -19,6 +19,9 @@ vi.mock("../configLoader.js", () => ({ vi.mock("../util/logger.js"); vi.mock("./ServiceContainer.js"); vi.mock("@continuedev/config-yaml"); +vi.mock("src/systemMessage.js", () => ({ + loadMarkdownRulesWithMetadata: vi.fn().mockReturnValue([]), +})); const defaultModel = { provider: "anthropic", diff --git a/extensions/cli/src/services/ConfigService.ts b/extensions/cli/src/services/ConfigService.ts index d2fde41ea..ad29220cc 100644 --- a/extensions/cli/src/services/ConfigService.ts +++ b/extensions/cli/src/services/ConfigService.ts @@ -7,6 +7,7 @@ import { import { DefaultApiInterface } from "@continuedev/sdk/dist/api/dist/index.js"; import { isStringRule } from "src/hubLoader.js"; +import { loadMarkdownRulesWithMetadata } from "src/systemMessage.js"; import { getErrorString } from "src/util/error.js"; import { AuthConfig, loadAuthConfig } from "../auth/workos.js"; @@ -275,6 +276,17 @@ export class ConfigService const loadedConfig = result.config; const merged = mergeUnrolledAssistants(loadedConfig, additional); + const markdownRules = loadMarkdownRulesWithMetadata(); + if (markdownRules.length > 0) { + const existingRuleContents = new Set( + (merged.rules ?? []).map((r) => (typeof r === "string" ? r : r?.rule)), + ); + const newRules = markdownRules.filter( + (r) => !existingRuleContents.has(r.rule), + ); + merged.rules = [...(merged.rules ?? []), ...newRules]; + } + const withModel = await this.addDefaultChatModelIfNone( merged, apiClient, diff --git a/extensions/cli/src/systemMessage.ts b/extensions/cli/src/systemMessage.ts index 6e3a74762..bdfbfbba0 100644 --- a/extensions/cli/src/systemMessage.ts +++ b/extensions/cli/src/systemMessage.ts @@ -2,6 +2,9 @@ import { execSync } from "child_process"; import * as fs from "fs"; import * as path from "path"; +import { parseMarkdownRule, RuleObject } from "@continuedev/config-yaml"; + +import { env } from "./env.js"; import { processRule } from "./hubLoader.js"; import { PermissionMode } from "./permissions/types.js"; import { serviceContainer } from "./services/ServiceContainer.js"; @@ -74,6 +77,79 @@ async function getConfigYamlRules(): Promise { return []; } +function getRuleNameFromPath(filePath: string): string { + const segments = filePath.split(/[/\\]/); + const lastTwoParts = segments.slice(-2); + return lastTwoParts.filter(Boolean).join("/").replace(/\.md$/, ""); +} + +/** + * Scan .continue/rules/ directories for markdown rule files and return the rules with metadata that should be always-applied + */ +export function loadMarkdownRulesWithMetadata(): RuleObject[] { + const cwd = process.cwd(); + const rulesDirs = [ + path.join(cwd, ".continue", "rules"), + path.join(env.continueHome, "rules"), + ]; + + const rules: RuleObject[] = []; + + for (const dir of rulesDirs) { + if (!fs.existsSync(dir)) continue; + + let files: string[]; + try { + files = fs.readdirSync(dir, { recursive: true }) as string[]; + } catch { + continue; + } + + for (const file of files) { + if (!String(file).endsWith(".md")) continue; + + const filePath = path.join(dir, String(file)); + try { + const stat = fs.statSync(filePath); + if (!stat.isFile()) continue; + } catch { + continue; + } + + try { + const content = fs.readFileSync(filePath, "utf-8"); + const { frontmatter, markdown } = parseMarkdownRule(content); + + if (frontmatter.invokable) continue; + + const isAlwaysApply = + frontmatter.alwaysApply === true || + (frontmatter.alwaysApply === undefined && + !frontmatter.globs && + !frontmatter.regex); + + if (isAlwaysApply && markdown.trim()) { + const ruleName = + frontmatter.name || getRuleNameFromPath(String(file)); + rules.push({ + name: ruleName, + rule: markdown, + description: frontmatter.description, + globs: frontmatter.globs, + regex: frontmatter.regex, + alwaysApply: true, + sourceFile: filePath, + }); + } + } catch { + // Skip files that can't be read or parsed + } + } + } + + return rules; +} + /** * Load and construct a comprehensive system message with base message and rules section * @param additionalRules - Additional rules from --rule flags @@ -124,6 +200,17 @@ export async function constructSystemMessage( const configYamlRules = await getConfigYamlRules(); processedRules.push(...configYamlRules); + // Load markdown rules from .continue/rules/ directories + const markdownRules = loadMarkdownRulesWithMetadata(); + // Deduplicate against already-loaded rules + const existingRulesSet = new Set(processedRules); + for (const rule of markdownRules) { + if (!existingRulesSet.has(rule.rule)) { + processedRules.push(rule.rule); + existingRulesSet.add(rule.rule); + } + } + // Construct the comprehensive system message let systemMessage = baseSystemMessage; diff --git a/extensions/cli/vitest.setup.ts b/extensions/cli/vitest.setup.ts index ac9193957..7487990f6 100644 --- a/extensions/cli/vitest.setup.ts +++ b/extensions/cli/vitest.setup.ts @@ -55,6 +55,7 @@ vi.mock("./src/systemMessage.js", () => ({ .mockResolvedValue( "You are an agent in the Continue CLI. Given the user's prompt, you should use the tools available to you to answer the user's question.", ), + loadMarkdownRulesWithMetadata: vi.fn().mockReturnValue([]), })); // Mock environment for tests