refactor: enhance context file handling in AgentService and AutoModeService

- Updated both services to conditionally load context files while excluding CLAUDE.md when autoLoadClaudeMd is enabled, preventing duplication.
- Improved the structure and clarity of the context files prompt, emphasizing the importance of following project-specific rules and conventions.
- Ensured consistent handling of context file loading across different methods in both services.
This commit is contained in:
Kacper
2025-12-24 23:07:00 +01:00
parent 077dd31b4f
commit 387bb15a3d
2 changed files with 135 additions and 21 deletions

View File

@@ -198,15 +198,53 @@ export class AgentService {
);
// Load project context files (CLAUDE.md, CODE_QUALITY.md, etc.)
// Note: When autoLoadClaudeMd is enabled, skip loading CLAUDE.md here since SDK handles it
// to avoid duplication. The SDK's settingSources will load CLAUDE.md from standard locations.
const { formattedPrompt: contextFilesPrompt } = autoLoadClaudeMd
? { formattedPrompt: '' }
: await loadContextFiles({
projectPath: effectiveWorkDir,
fsModule: secureFs as Parameters<typeof loadContextFiles>[0]['fsModule'],
const contextResult = await loadContextFiles({
projectPath: effectiveWorkDir,
fsModule: secureFs as Parameters<typeof loadContextFiles>[0]['fsModule'],
});
// When autoLoadClaudeMd is enabled, filter out CLAUDE.md to avoid duplication
// (SDK handles CLAUDE.md via settingSources), but keep other context files like CODE_QUALITY.md
let contextFilesPrompt = contextResult.formattedPrompt;
if (autoLoadClaudeMd && contextResult.files.length > 0) {
const nonClaudeFiles = contextResult.files.filter(
(f) => f.name.toLowerCase() !== 'claude.md'
);
// Rebuild prompt without CLAUDE.md
if (nonClaudeFiles.length > 0) {
const formattedFiles = nonClaudeFiles.map((file) => {
const header = `## ${file.name}`;
const pathInfo = `**Path:** \`${file.path}\``;
const descriptionInfo = file.description ? `\n**Purpose:** ${file.description}` : '';
return `${header}\n${pathInfo}${descriptionInfo}\n\n${file.content}`;
});
contextFilesPrompt = `# Project Context Files
The following context files provide project-specific rules, conventions, and guidelines.
Each file serves a specific purpose - use the description to understand when to reference it.
If you need more details about a context file, you can read the full file at the path provided.
**IMPORTANT**: You MUST follow the rules and conventions specified in these files.
- Follow ALL commands exactly as shown (e.g., if the project uses \`pnpm\`, NEVER use \`npm\` or \`npx\`)
- Follow ALL coding conventions, commit message formats, and architectural patterns specified
- Reference these rules before running ANY shell commands or making commits
---
${formattedFiles.join('\n\n---\n\n')}
---
**REMINDER**: Before taking any action, verify you are following the conventions specified above.
`;
} else {
// All files were CLAUDE.md, so no context to add
contextFilesPrompt = '';
}
}
// Build combined system prompt with base prompt and context files
const baseSystemPrompt = this.getSystemPrompt();
const combinedSystemPrompt = contextFilesPrompt

View File

@@ -569,15 +569,53 @@ export class AutoModeService {
// Build the prompt - use continuation prompt if provided (for recovery after plan approval)
let prompt: string;
// Load project context files (CLAUDE.md, CODE_QUALITY.md, etc.) - passed as system prompt
// Note: When autoLoadClaudeMd is enabled, skip loading CLAUDE.md here since SDK handles it
// to avoid duplication. The SDK's settingSources will load CLAUDE.md from standard locations.
const { formattedPrompt: contextFilesPrompt } = autoLoadClaudeMd
? { formattedPrompt: '' }
: await loadContextFiles({
projectPath,
fsModule: secureFs as Parameters<typeof loadContextFiles>[0]['fsModule'],
const contextResult = await loadContextFiles({
projectPath,
fsModule: secureFs as Parameters<typeof loadContextFiles>[0]['fsModule'],
});
// When autoLoadClaudeMd is enabled, filter out CLAUDE.md to avoid duplication
// (SDK handles CLAUDE.md via settingSources), but keep other context files like CODE_QUALITY.md
let contextFilesPrompt = contextResult.formattedPrompt;
if (autoLoadClaudeMd && contextResult.files.length > 0) {
const nonClaudeFiles = contextResult.files.filter(
(f) => f.name.toLowerCase() !== 'claude.md'
);
// Rebuild prompt without CLAUDE.md
if (nonClaudeFiles.length > 0) {
const formattedFiles = nonClaudeFiles.map((file) => {
const header = `## ${file.name}`;
const pathInfo = `**Path:** \`${file.path}\``;
const descriptionInfo = file.description ? `\n**Purpose:** ${file.description}` : '';
return `${header}\n${pathInfo}${descriptionInfo}\n\n${file.content}`;
});
contextFilesPrompt = `# Project Context Files
The following context files provide project-specific rules, conventions, and guidelines.
Each file serves a specific purpose - use the description to understand when to reference it.
If you need more details about a context file, you can read the full file at the path provided.
**IMPORTANT**: You MUST follow the rules and conventions specified in these files.
- Follow ALL commands exactly as shown (e.g., if the project uses \`pnpm\`, NEVER use \`npm\` or \`npx\`)
- Follow ALL coding conventions, commit message formats, and architectural patterns specified
- Reference these rules before running ANY shell commands or making commits
---
${formattedFiles.join('\n\n---\n\n')}
---
**REMINDER**: Before taking any action, verify you are following the conventions specified above.
`;
} else {
// All files were CLAUDE.md, so no context to add
contextFilesPrompt = '';
}
}
if (options?.continuationPrompt) {
// Continuation prompt is used when recovering from a plan approval
// The plan was already approved, so skip the planning phase
@@ -774,15 +812,53 @@ export class AutoModeService {
);
// Load project context files (CLAUDE.md, CODE_QUALITY.md, etc.) - passed as system prompt
// Note: When autoLoadClaudeMd is enabled, skip loading CLAUDE.md here since SDK handles it
// to avoid duplication. The SDK's settingSources will load CLAUDE.md from standard locations.
const { formattedPrompt: contextFilesPrompt } = autoLoadClaudeMd
? { formattedPrompt: '' }
: await loadContextFiles({
projectPath,
fsModule: secureFs as Parameters<typeof loadContextFiles>[0]['fsModule'],
const contextResult = await loadContextFiles({
projectPath,
fsModule: secureFs as Parameters<typeof loadContextFiles>[0]['fsModule'],
});
// When autoLoadClaudeMd is enabled, filter out CLAUDE.md to avoid duplication
// (SDK handles CLAUDE.md via settingSources), but keep other context files like CODE_QUALITY.md
let contextFilesPrompt = contextResult.formattedPrompt;
if (autoLoadClaudeMd && contextResult.files.length > 0) {
const nonClaudeFiles = contextResult.files.filter(
(f) => f.name.toLowerCase() !== 'claude.md'
);
// Rebuild prompt without CLAUDE.md
if (nonClaudeFiles.length > 0) {
const formattedFiles = nonClaudeFiles.map((file) => {
const header = `## ${file.name}`;
const pathInfo = `**Path:** \`${file.path}\``;
const descriptionInfo = file.description ? `\n**Purpose:** ${file.description}` : '';
return `${header}\n${pathInfo}${descriptionInfo}\n\n${file.content}`;
});
contextFilesPrompt = `# Project Context Files
The following context files provide project-specific rules, conventions, and guidelines.
Each file serves a specific purpose - use the description to understand when to reference it.
If you need more details about a context file, you can read the full file at the path provided.
**IMPORTANT**: You MUST follow the rules and conventions specified in these files.
- Follow ALL commands exactly as shown (e.g., if the project uses \`pnpm\`, NEVER use \`npm\` or \`npx\`)
- Follow ALL coding conventions, commit message formats, and architectural patterns specified
- Reference these rules before running ANY shell commands or making commits
---
${formattedFiles.join('\n\n---\n\n')}
---
**REMINDER**: Before taking any action, verify you are following the conventions specified above.
`;
} else {
// All files were CLAUDE.md, so no context to add
contextFilesPrompt = '';
}
}
// Build complete prompt with feature info, previous context, and follow-up instructions
let fullPrompt = `## Follow-up on Feature Implementation