Elffuss BraidHub

kikocis/continue public

Codebase/Colocated Rules Cache (#6603)

* codebase rules cache

* reload config on codebase rule update

* void codebase rule promise

* void rules cache promises part 2

* better colocated rule source label
Changesetb7ca0fa4fd0c031734694d4e5fc1575e139eca1633c05a2333002260d5a8a002
AuthorDallin Romney <dallinromney@gmail.com> · 2025-07-17 20:34 +02:00
CommitterGitHub <noreply@github.com> · 2025-07-17 11:34 -07:00
Parentsa0535c60dff9

Browse the files at this changeset

9 files changed

modified core/config/markdown/loadCodebaseRules.ts +42 −1
@@ -7,6 +7,43 @@
77 import { RULES_MARKDOWN_FILENAME } from "../../llm/rules/constants";
88 import { getUriPathBasename } from "../../util/uri";
99
10+export class CodebaseRulesCache {
11+ private static instance: CodebaseRulesCache | null = null;
12+ private constructor() {}
13+
14+ public static getInstance(): CodebaseRulesCache {
15+ if (CodebaseRulesCache.instance === null) {
16+ CodebaseRulesCache.instance = new CodebaseRulesCache();
17+ }
18+ return CodebaseRulesCache.instance;
19+ }
20+ rules: RuleWithSource[] = [];
21+ errors: ConfigValidationError[] = [];
22+ async refresh(ide: IDE) {
23+ const { rules, errors } = await loadCodebaseRules(ide);
24+ this.rules = rules;
25+ this.errors = errors;
26+ }
27+ async update(ide: IDE, uri: string) {
28+ const content = await ide.readFile(uri);
29+ const rule = markdownToRule(content, { uriType: "file", filePath: uri });
30+ const ruleWithSource: RuleWithSource = {
31+ ...rule,
32+ source: "colocated-markdown",
33+ ruleFile: uri,
34+ };
35+ const matchIdx = this.rules.findIndex((r) => r.ruleFile === uri);
36+ if (matchIdx === -1) {
37+ this.rules.push(ruleWithSource);
38+ } else {
39+ this.rules[matchIdx] = ruleWithSource;
40+ }
41+ }
42+ remove(uri: string) {
43+ this.rules = this.rules.filter((r) => r.ruleFile !== uri);
44+ }
45+}
46+
1047 /**
1148 * Loads rules from rules.md files colocated in the codebase
1249 */
@@ -33,7 +70,11 @@
3370 const content = await ide.readFile(filePath);
3471 const rule = markdownToRule(content, { uriType: "file", filePath });
3572
36- rules.push({ ...rule, source: "rules-block", ruleFile: filePath });
73+ rules.push({
74+ ...rule,
75+ source: "colocated-markdown",
76+ ruleFile: filePath,
77+ });
3778 } catch (e) {
3879 errors.push({
3980 fatal: false,
modified core/config/markdown/loadCodebaseRules.vitest.ts +4 −4
@@ -45,27 +45,27 @@
4545 "src/rules.md": {
4646 name: "General Rules",
4747 rule: "Follow coding standards",
48- source: "rules-block",
48+ source: "colocated-markdown",
4949 ruleFile: "src/rules.md",
5050 },
5151 "src/redux/rules.md": {
5252 name: "Redux Rules",
5353 rule: "Use Redux Toolkit",
5454 globs: "**/*.{ts,tsx}",
55- source: "rules-block",
55+ source: "colocated-markdown",
5656 ruleFile: "src/redux/rules.md",
5757 },
5858 "src/components/rules.md": {
5959 name: "Component Rules",
6060 rule: "Use functional components",
6161 globs: ["**/*.tsx", "**/*.jsx"],
62- source: "rules-block",
62+ source: "colocated-markdown",
6363 ruleFile: "src/components/rules.md",
6464 },
6565 ".continue/rules.md": {
6666 name: "Global Rules",
6767 rule: "Follow project guidelines",
68- source: "rules-block",
68+ source: "colocated-markdown",
6969 ruleFile: ".continue/rules.md",
7070 },
7171 };
modified core/config/markdown/ruleCollocationApplication.vitest.ts +9 −9
@@ -16,7 +16,7 @@
1616 {
1717 name: "Root Rule",
1818 rule: "Follow project standards",
19- source: "rules-block",
19+ source: "colocated-markdown",
2020 ruleFile: ".continue/rules.md",
2121 },
2222
@@ -24,7 +24,7 @@
2424 {
2525 name: "React Components Rule",
2626 rule: "Use functional components with hooks",
27- source: "rules-block",
27+ source: "colocated-markdown",
2828 ruleFile: "src/components/rules.md",
2929 // No explicit globs - should implicitly only apply to files in that directory
3030 },
@@ -34,7 +34,7 @@
3434 name: "Redux Rule",
3535 rule: "Use Redux Toolkit for state management",
3636 globs: "src/redux/**/*.{ts,tsx}",
37- source: "rules-block",
37+ source: "colocated-markdown",
3838 ruleFile: "src/redux/rules.md",
3939 },
4040
@@ -43,7 +43,7 @@
4343 name: "TypeScript Components Rule",
4444 rule: "Use TypeScript with React components",
4545 globs: "**/*.tsx", // Only apply to .tsx files
46- source: "rules-block",
46+ source: "colocated-markdown",
4747 ruleFile: "src/components/rules.md",
4848 },
4949
@@ -52,7 +52,7 @@
5252 name: "API Utils Rule",
5353 rule: "Follow API utility conventions",
5454 globs: "**/*.ts", // Only TypeScript files in this directory
55- source: "rules-block",
55+ source: "colocated-markdown",
5656 ruleFile: "src/utils/api/rules.md",
5757 },
5858 ];
@@ -183,7 +183,7 @@
183183 const impliedComponentRule: RuleWithSource = {
184184 name: "Implied Components Rule",
185185 rule: "Use React component best practices",
186- source: "rules-block",
186+ source: "colocated-markdown",
187187 ruleFile: "src/components/rules.md",
188188 // No explicit globs - should infer from directory
189189 };
@@ -233,7 +233,7 @@
233233 name: "TypeScript Component Rule",
234234 rule: "Use TypeScript with React components",
235235 globs: "**/*.tsx", // Only apply to .tsx files
236- source: "rules-block",
236+ source: "colocated-markdown",
237237 ruleFile: "src/components/rules.md",
238238 };
239239
@@ -280,7 +280,7 @@
280280 name: "API Utils Rule",
281281 rule: "Follow API utility conventions",
282282 globs: "**/*.ts", // Only TypeScript files in this directory
283- source: "rules-block",
283+ source: "colocated-markdown",
284284 ruleFile: "src/utils/api/rules.md",
285285 };
286286
@@ -323,7 +323,7 @@
323323 return {
324324 name: `Inferred Rule for ${directory}`,
325325 rule: `Follow standards for ${directory}`,
326- source: "rules-block",
326+ source: "colocated-markdown",
327327 ruleFile: ruleFilePath,
328328 // In a fixed implementation, these globs would be automatically inferred
329329 // globs: expectedGlob,
modified core/config/profile/doLoadConfig.ts +4 −5
@@ -37,7 +37,7 @@
3737 import { TTS } from "../../util/tts";
3838 import { getWorkspaceContinueRuleDotFiles } from "../getWorkspaceContinueRuleDotFiles";
3939 import { loadContinueConfigFromJson } from "../load";
40-import { loadCodebaseRules } from "../markdown/loadCodebaseRules";
40+import { CodebaseRulesCache } from "../markdown/loadCodebaseRules";
4141 import { loadMarkdownRules } from "../markdown/loadMarkdownRules";
4242 import { migrateJsonSharedConfig } from "../migrateSharedConfig";
4343 import { rectifySelectedModelsFromGlobalContext } from "../selectedModels";
@@ -165,10 +165,9 @@
165165 }
166166
167167 // Add rules from colocated rules.md files in the codebase
168- const { rules: codebaseRules, errors: codebaseRulesErrors } =
169- await loadCodebaseRules(ide);
170- newConfig.rules.unshift(...codebaseRules);
171- errors.push(...codebaseRulesErrors);
168+ const codebaseRulesCache = CodebaseRulesCache.getInstance();
169+ newConfig.rules.unshift(...codebaseRulesCache.rules);
170+ errors.push(...codebaseRulesCache.errors);
172171
173172 // Rectify model selections for each role
174173 newConfig = rectifySelectedModelsFromGlobalContext(newConfig, profileId);
modified core/core.ts +34 −10
@@ -52,6 +52,7 @@
5252 import { getDiffFn, GitDiffCache } from "./autocomplete/snippets/gitDiffCache";
5353 import { stringifyMcpPrompt } from "./commands/slash/mcpSlashCommand";
5454 import { isLocalDefinitionFile } from "./config/loadLocalAssistants";
55+import { CodebaseRulesCache } from "./config/markdown/loadCodebaseRules";
5556 import {
5657 setupLocalConfig,
5758 setupProviderConfig,
@@ -69,11 +70,11 @@
6970 import { llmStreamChat } from "./llm/streamChat";
7071 import { BeforeAfterDiff } from "./nextEdit/context/diffFormatting";
7172 import { processSmallEdit } from "./nextEdit/context/processSmallEdit";
73+import { NextEditProvider } from "./nextEdit/NextEditProvider";
7274 import type { FromCoreProtocol, ToCoreProtocol } from "./protocol";
7375 import { OnboardingModes } from "./protocol/core";
7476 import type { IMessenger, Message } from "./protocol/messenger";
7577 import { getUriPathBasename } from "./util/uri";
76-import { NextEditProvider } from "./nextEdit/NextEditProvider";
7778
7879 const hasRulesFiles = (uris: string[]): boolean => {
7980 for (const uri of uris) {
@@ -235,6 +236,15 @@
235236 (..._) => Promise.resolve([]),
236237 );
237238
239+ const codebaseRulesCache = CodebaseRulesCache.getInstance();
240+ void codebaseRulesCache
241+ .refresh(ide)
242+ .catch((e) => console.error("Failed to initialize colocated rules cache"))
243+ .then(() => {
244+ void this.configHandler.reloadConfig(
245+ "Initial codebase rules post-walkdir/load reload",
246+ );
247+ });
238248 this.nextEditProvider = NextEditProvider.initialize(
239249 this.configHandler,
240250 ide,
@@ -342,10 +352,12 @@
342352 });
343353
344354 on("config/reload", async (msg) => {
355+ // User force reloading will retrigger colocated rules
356+ const codebaseRulesCache = CodebaseRulesCache.getInstance();
357+ await codebaseRulesCache.refresh(this.ide);
345358 void this.configHandler.reloadConfig(
346359 "Force reloaded (config/reload message)",
347360 );
348- return await this.configHandler.getSerializedConfig();
349361 });
350362
351363 on("config/ideSettingsUpdate", async (msg) => {
@@ -682,9 +694,12 @@
682694 void refreshIfNotIgnored(data.uris);
683695
684696 if (hasRulesFiles(data.uris)) {
697+ const rulesCache = CodebaseRulesCache.getInstance();
698+ await Promise.all(
699+ data.uris.map((uri) => rulesCache.update(this.ide, uri)),
700+ );
685701 await this.configHandler.reloadConfig("Rules file created");
686702 }
687-
688703 // If it's a local assistant being created, we want to reload all assistants so it shows up in the list
689704 let localAssistantCreated = false;
690705 for (const uri of data.uris) {
@@ -704,7 +719,9 @@
704719 void refreshIfNotIgnored(data.uris);
705720
706721 if (hasRulesFiles(data.uris)) {
707- await this.configHandler.reloadConfig("Rules file deleted");
722+ const rulesCache = CodebaseRulesCache.getInstance();
723+ data.uris.forEach((uri) => rulesCache.remove(uri));
724+ await this.configHandler.reloadConfig("Codebase rule file deleted");
708725 }
709726 }
710727 });
@@ -1020,17 +1037,24 @@
10201037 uri.endsWith(".continuerc.json") ||
10211038 uri.endsWith(".prompt") ||
10221039 uri.endsWith(SYSTEM_PROMPT_DOT_FILE) ||
1023- (uri.includes(".continue") && uri.endsWith(".yaml")) ||
1024- uri.endsWith(RULES_MARKDOWN_FILENAME) ||
1025- BLOCK_TYPES.some(
1026- (blockType) =>
1027- uri.includes(`.continue/${blockType}`) ||
1028- uri.includes(`.continue\\${blockType}`),
1040+ (uri.includes(".continue") &&
1041+ (uri.endsWith(".yaml") || uri.endsWith("yml"))) ||
1042+ BLOCK_TYPES.some((blockType) =>
1043+ uri.includes(`.continue/${blockType}`),
10291044 )
10301045 ) {
10311046 await this.configHandler.reloadConfig(
10321047 "Config-related file updated: continuerc, prompt, local block, etc",
10331048 );
1049+ } else if (uri.endsWith(RULES_MARKDOWN_FILENAME)) {
1050+ try {
1051+ const codebaseRulesCache = CodebaseRulesCache.getInstance();
1052+ void codebaseRulesCache.update(this.ide, uri).then(() => {
1053+ void this.configHandler.reloadConfig("Codebase rule update");
1054+ });
1055+ } catch (e) {
1056+ console.error("Failed to update codebase rule", e);
1057+ }
10341058 } else if (
10351059 uri.endsWith(".continueignore") ||
10361060 uri.endsWith(".gitignore")
modified core/index.d.ts +1 −0
@@ -1664,6 +1664,7 @@
16641664 | "model-options-plan"
16651665 | "model-options-agent"
16661666 | "rules-block"
1667+ | "colocated-markdown"
16671668 | "json-systemMessage"
16681669 | ".continuerules";
16691670
modified core/llm/rules/ruleColocation.vitest.ts +9 −9
@@ -9,7 +9,7 @@
99 generalRule: {
1010 name: "General Rule",
1111 rule: "Follow coding standards",
12- source: "rules-block",
12+ source: "colocated-markdown",
1313 ruleFile: "src/rules.md",
1414 },
1515
@@ -18,7 +18,7 @@
1818 name: "Redux Rule",
1919 rule: "Use Redux Toolkit",
2020 globs: "src/redux/**/*.{ts,tsx}",
21- source: "rules-block",
21+ source: "colocated-markdown",
2222 ruleFile: "src/redux/rules.md",
2323 },
2424
@@ -27,7 +27,7 @@
2727 name: "Component Rule",
2828 rule: "Use functional components",
2929 globs: ["src/components/**/*.tsx", "src/components/**/*.jsx"],
30- source: "rules-block",
30+ source: "colocated-markdown",
3131 ruleFile: "src/components/rules.md",
3232 },
3333
@@ -37,7 +37,7 @@
3737 rule: "Follow these guidelines always",
3838 alwaysApply: true,
3939 globs: "src/specific/**/*.ts", // Should be ignored since alwaysApply is true
40- source: "rules-block",
40+ source: "colocated-markdown",
4141 ruleFile: ".continue/rules.md",
4242 },
4343
@@ -47,7 +47,7 @@
4747 rule: "This rule should only apply to matching files",
4848 alwaysApply: false,
4949 // No globs, so should never apply
50- source: "rules-block",
50+ source: "colocated-markdown",
5151 ruleFile: ".continue/rules.md",
5252 },
5353
@@ -57,7 +57,7 @@
5757 rule: "Apply only to matching files",
5858 alwaysApply: false,
5959 globs: "src/utils/**/*.ts",
60- source: "rules-block",
60+ source: "colocated-markdown",
6161 ruleFile: "src/utils/rules.md",
6262 },
6363 };
@@ -127,7 +127,7 @@
127127 rule: "Follow nested module standards",
128128 // Fix: Specify the exact path prefix to restrict to this directory structure
129129 globs: "src/features/auth/utils/**/*.ts",
130- source: "rules-block",
130+ source: "colocated-markdown",
131131 ruleFile: "src/features/auth/utils/rules.md",
132132 };
133133
@@ -157,7 +157,7 @@
157157 // Note: Negative globs may not be supported by the current implementation
158158 // Testing with standard pattern instead
159159 globs: "src/**/[!.]*.ts",
160- source: "rules-block",
160+ source: "colocated-markdown",
161161 ruleFile: "src/rules.md",
162162 };
163163
@@ -174,7 +174,7 @@
174174 // Use a pattern that doesn't match test files
175175 globs: ["src/**/*.ts", "!src/**/*.test.ts", "!src/**/*.spec.ts"],
176176 alwaysApply: false,
177- source: "rules-block",
177+ source: "colocated-markdown",
178178 ruleFile: "src/rules.md",
179179 };
180180
modified core/protocol/core.ts +1 −1
@@ -91,7 +91,7 @@
9191 },
9292 ];
9393 "config/deleteModel": [{ title: string }, void];
94- "config/reload": [undefined, ConfigResult<BrowserSerializedContinueConfig>];
94+ "config/reload": [undefined, void];
9595 "config/refreshProfiles": [
9696 (
9797 | undefined
modified gui/src/components/mainInput/belowMainInput/RulesPeek.tsx +12 −5
@@ -1,5 +1,6 @@
11 import { DocumentTextIcon, GlobeAltIcon } from "@heroicons/react/24/outline";
2-import { RuleSource, RuleWithSource } from "core";
2+import { RuleWithSource } from "core";
3+import { getLastNPathParts } from "core/util/uri";
34 import { ComponentType, useMemo, useState } from "react";
45 import ToggleDiv from "../../ToggleDiv";
56
@@ -13,8 +14,8 @@
1314 }
1415
1516 // Convert technical source to user-friendly text
16-const getSourceLabel = (source: RuleSource): string => {
17- switch (source) {
17+const getSourceLabel = (rule: RuleWithSource): string => {
18+ switch (rule.source) {
1819 case "default-chat":
1920 return "Default Chat";
2021 case "default-agent":
@@ -27,12 +28,18 @@
2728 return "Model Agent Options";
2829 case "rules-block":
2930 return "Rules Block";
31+ case "colocated-markdown":
32+ if (rule.ruleFile) {
33+ return getLastNPathParts(rule.ruleFile, 2);
34+ } else {
35+ return "rules.md";
36+ }
3037 case "json-systemMessage":
3138 return "System Message";
3239 case ".continuerules":
3340 return "Project Rules";
3441 default:
35- return source;
42+ return rule.source;
3643 }
3744 };
3845
@@ -95,7 +102,7 @@
95102 )}
96103 </div>
97104 <div className="mt-1 pl-6 pr-2 text-xs text-gray-500">
98- Source: {getSourceLabel(rule.source)}
105+ Source: {getSourceLabel(rule)}
99106 </div>
100107 </div>
101108 );