Skip to content

Commit ff82dce

Browse files
committed
refactor: streamline rule loading and formatting in custom instructions
1 parent edde83a commit ff82dce

File tree

2 files changed

+23
-34
lines changed

2 files changed

+23
-34
lines changed

src/core/prompts/sections/__tests__/custom-instructions-global.spec.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import * as path from "path"
22
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"
33

4-
// Access the original console.log that was saved in vitest.setup.ts
5-
const originalConsoleLog = (global as any).originalConsoleLog || console.error
6-
74
// Use vi.hoisted to ensure mocks are available during hoisting
85
const { mockHomedir, mockStat, mockReadFile, mockReaddir, mockGetRooDirectoriesForCwd, mockGetGlobalRooDirectory } =
96
vi.hoisted(() => ({
@@ -77,14 +74,10 @@ describe("custom-instructions global .roo support", () => {
7774

7875
const result = await loadRuleFiles(mockCwd)
7976

80-
originalConsoleLog("Result:", result)
81-
originalConsoleLog("mockStat calls:", mockStat.mock.calls)
82-
originalConsoleLog("mockReaddir calls:", mockReaddir.mock.calls)
83-
originalConsoleLog("mockReadFile calls:", mockReadFile.mock.calls)
84-
85-
expect(result).toContain("# Global rules:")
77+
expect(result).toContain("# Rules from")
78+
expect(result).toContain("rules.md:")
8679
expect(result).toContain("global rule content")
87-
expect(result).not.toContain("# Project-specific rules:")
80+
expect(result).not.toContain("project rule content")
8881
})
8982

9083
it("should load project rules only when global rules do not exist", async () => {
@@ -104,9 +97,10 @@ describe("custom-instructions global .roo support", () => {
10497

10598
const result = await loadRuleFiles(mockCwd)
10699

107-
expect(result).toContain("# Project-specific rules:")
100+
expect(result).toContain("# Rules from")
101+
expect(result).toContain("rules.md:")
108102
expect(result).toContain("project rule content")
109-
expect(result).not.toContain("# Global rules:")
103+
expect(result).not.toContain("global rule content")
110104
})
111105

112106
it("should merge global and project rules with project rules after global", async () => {
@@ -127,14 +121,15 @@ describe("custom-instructions global .roo support", () => {
127121

128122
const result = await loadRuleFiles(mockCwd)
129123

130-
expect(result).toContain("# Global rules:")
124+
expect(result).toContain("# Rules from")
125+
expect(result).toContain("global.md:")
131126
expect(result).toContain("global rule content")
132-
expect(result).toContain("# Project-specific rules:")
127+
expect(result).toContain("project.md:")
133128
expect(result).toContain("project rule content")
134129

135130
// Ensure project rules come after global rules
136-
const globalIndex = result.indexOf("# Global rules:")
137-
const projectIndex = result.indexOf("# Project-specific rules:")
131+
const globalIndex = result.indexOf("global rule content")
132+
const projectIndex = result.indexOf("project rule content")
138133
expect(globalIndex).toBeLessThan(projectIndex)
139134
})
140135

@@ -203,9 +198,10 @@ describe("custom-instructions global .roo support", () => {
203198

204199
const result = await addCustomInstructions("", "", mockCwd, mode)
205200

206-
expect(result).toContain("# Global mode-specific rules:")
201+
expect(result).toContain("# Rules from")
202+
expect(result).toContain("global-mode.md:")
207203
expect(result).toContain("global mode rule content")
208-
expect(result).toContain("# Project-specific mode-specific rules:")
204+
expect(result).toContain("project-mode.md:")
209205
expect(result).toContain("project mode rule content")
210206
})
211207

src/core/prompts/sections/custom-instructions.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,11 @@ async function readTextFilesFromDirectory(dirPath: string): Promise<Array<{ file
146146
function formatDirectoryContent(dirPath: string, files: Array<{ filename: string; content: string }>): string {
147147
if (files.length === 0) return ""
148148

149-
return (
150-
"\n\n" +
151-
files
152-
.map((file) => {
153-
return `# Rules from ${file.filename}:\n${file.content}`
154-
})
155-
.join("\n\n")
156-
)
149+
return files
150+
.map((file) => {
151+
return `# Rules from ${file.filename}:\n${file.content}`
152+
})
153+
.join("\n\n")
157154
}
158155

159156
/**
@@ -170,17 +167,15 @@ export async function loadRuleFiles(cwd: string): Promise<string> {
170167
if (await directoryExists(rulesDir)) {
171168
const files = await readTextFilesFromDirectory(rulesDir)
172169
if (files.length > 0) {
173-
const isGlobal = path.resolve(rooDir) === path.resolve(getGlobalRooDirectory())
174-
const prefix = isGlobal ? "# Global rules" : "# Project-specific rules"
175170
const content = formatDirectoryContent(rulesDir, files)
176-
rules.push(`${prefix}:\n${content}`)
171+
rules.push(content)
177172
}
178173
}
179174
}
180175

181176
// If we found rules in .roo/rules/ directories, return them
182177
if (rules.length > 0) {
183-
return "\n\n" + rules.join("\n\n")
178+
return "\n" + rules.join("\n\n")
184179
}
185180

186181
// Fall back to existing behavior for legacy .roorules/.clinerules files
@@ -219,17 +214,15 @@ export async function addCustomInstructions(
219214
if (await directoryExists(modeRulesDir)) {
220215
const files = await readTextFilesFromDirectory(modeRulesDir)
221216
if (files.length > 0) {
222-
const isGlobal = path.resolve(rooDir) === path.resolve(getGlobalRooDirectory())
223-
const prefix = isGlobal ? "# Global mode-specific rules" : "# Project-specific mode-specific rules"
224217
const content = formatDirectoryContent(modeRulesDir, files)
225-
modeRules.push(`${prefix}:\n${content}`)
218+
modeRules.push(content)
226219
}
227220
}
228221
}
229222

230223
// If we found mode-specific rules in .roo/rules-${mode}/ directories, use them
231224
if (modeRules.length > 0) {
232-
modeRuleContent = "\n\n" + modeRules.join("\n\n")
225+
modeRuleContent = "\n" + modeRules.join("\n\n")
233226
usedRuleFile = `rules-${mode} directories`
234227
} else {
235228
// Fall back to existing behavior for legacy files

0 commit comments

Comments
 (0)