codecritique 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,6 +21,7 @@ import {
21
21
  } from './feedback-loader.js';
22
22
  import * as llm from './llm.js';
23
23
  import { findRelevantPRComments } from './pr-history/database.js';
24
+ import { buildCachedSystemPrompt } from './prompt-cache.js';
24
25
  import { inferContextFromCodeContent, inferContextFromDocumentContent } from './utils/context-inference.js';
25
26
  import { isGenericDocument, getGenericDocumentContext } from './utils/document-detection.js';
26
27
  import { isTestFile, shouldProcessFile } from './utils/file-validation.js';
@@ -60,184 +61,11 @@ async function ensureSemanticSimilarityInitialized() {
60
61
  }
61
62
 
62
63
  // ============================================================================
63
- // COMMON PROMPT INSTRUCTIONS
64
+ // PROMPT FORMATTING HELPERS
65
+ // Note: Static prompt content (critical rules, citation requirements, etc.) is
66
+ // centralized in prompt-cache.js for optimal caching across LLM calls.
64
67
  // ============================================================================
65
68
 
66
- /**
67
- * Generate the common critical rules block for all prompts
68
- * @param {Object} options - Options for customization
69
- * @param {string} options.importRuleContext - Context-specific text for import rule ('code', 'test', or 'pr')
70
- * @returns {string} Critical rules block
71
- */
72
- function getCriticalRulesBlock(options = {}) {
73
- const { importRuleContext = 'code' } = options;
74
-
75
- // Customize import rule based on context
76
- let importRuleText;
77
- switch (importRuleContext) {
78
- case 'test':
79
- importRuleText =
80
- 'DO NOT flag missing imports or files referenced in import statements as issues. Focus only on test quality, logic, and patterns within the provided test files.';
81
- break;
82
- case 'pr':
83
- importRuleText =
84
- 'DO NOT flag missing imports or files referenced in import statements as issues. In PR analysis, some files (especially assets like images, fonts, or excluded files) may not be included in the review scope. Focus only on code quality, logic, and patterns within the provided PR files.';
85
- break;
86
- default:
87
- importRuleText =
88
- 'DO NOT flag missing imports or files referenced in import statements as issues. Focus only on code quality, logic, and patterns within the provided files.';
89
- }
90
-
91
- return `**🚨 CRITICAL: LINE NUMBER REPORTING RULE - READ CAREFULLY 🚨**
92
- When reporting issues in the JSON output, NEVER provide exhaustive lists of line numbers. For repeated issues, list only 3-5 representative line numbers maximum. Exhaustive line number lists are considered errors and must be avoided.
93
-
94
- **🚨 CRITICAL: IMPORT STATEMENT RULE - READ CAREFULLY 🚨**
95
- ${importRuleText}
96
-
97
- **🚨 CRITICAL: NO LOW SEVERITY ISSUES - READ CAREFULLY 🚨**
98
- DO NOT report "low" severity issues. Low severity issues typically include:
99
- - Import statement ordering or grouping
100
- - Code formatting and whitespace
101
- - Minor stylistic preferences
102
- - Comment placement or formatting
103
- - Line length or wrapping suggestions
104
- These concerns are handled by project linters (ESLint, Prettier, etc.) and should NOT be included in your review.
105
- Only report issues with severity: "critical", "high", or "medium".
106
-
107
- **🚨 ABSOLUTE RULE: YOUR SUGGESTION MUST FIX BROKEN CODE 🚨**
108
-
109
- Every issue you report MUST identify CODE THAT IS BROKEN OR INCORRECT.
110
-
111
- Your suggestion MUST be a CODE FIX that changes program behavior, not documentation.
112
-
113
- **🚨 COMMENTS ARE NOT CODE FIXES - DO NOT SUGGEST ADDING COMMENTS 🚨**
114
-
115
- Adding comments is DOCUMENTATION, not a code fix. DO NOT suggest:
116
- - "Add a comment explaining..."
117
- - "Add a comment above..."
118
- - "Add a comment to clarify..."
119
- - "Include a comment..."
120
- - Any suggestion that involves writing comments
121
-
122
- Comments do not fix bugs. Comments do not change behavior. Comments are NOT acceptable suggestions.
123
-
124
- **🚨 BANNED WORDS IN SUGGESTIONS - AUTOMATIC DELETION 🚨**
125
-
126
- If your suggestion contains ANY of these words/phrases, DELETE THE ISSUE IMMEDIATELY:
127
- - "Add a comment" / "Add comment" / "Include a comment" / "comment explaining" / "comment to clarify"
128
- - "Consider" / "consider whether" / "consider normalizing"
129
- - "Verify" / "verify that" / "verify the"
130
- - "Ensure" / "ensure that" / "ensure all"
131
- - "Document" / "document why" / "document that"
132
- - "Check" / "check if" / "check whether"
133
- - "Confirm" / "confirm that"
134
- - "Clarify" / "make clearer" / "could be clearer" / "more explicit"
135
- - "Analytics" / "dashboards" / "tracking" / "tracking system"
136
- - "Migration" / "migrated" / "migrate"
137
- - "Downstream" / "consumers" / "external systems"
138
- - "Experiment" / "experiment results" / "experiment analysis"
139
- - "Backward compatibility" / "breaking change" (unless you provide code to fix it)
140
- - "Future maintainers" / "maintainability" / "for clarity"
141
-
142
- **🚨 YOUR SUGGESTION MUST START WITH A VERB THAT CHANGES CODE 🚨**
143
-
144
- GOOD suggestion starters:
145
- - "Change X to Y"
146
- - "Replace X with Y"
147
- - "Add X"
148
- - "Remove X"
149
- - "Rename X to Y"
150
- - "Move X to Y"
151
- - "Update X to Y"
152
-
153
- BAD suggestion starters (DELETE THESE):
154
- - "Consider..." - NO! This is advice, not a code change
155
- - "Verify that..." - NO! This asks someone to check something
156
- - "Ensure that..." - NO! This is a request, not a code change
157
- - "Document..." - NO! Documentation is not a code fix
158
- - "Check..." - NO! This asks for verification
159
-
160
- **EXAMPLES OF ISSUES YOU MUST DELETE:**
161
- ❌ Description: "The default value inconsistency could lead to confusion"
162
- Suggestion: "Consider whether X should also default to Y or document why they differ"
163
- WHY DELETE: "Consider" and "document" are banned. No code change provided.
164
-
165
- ❌ Description: "This could break analytics dashboards"
166
- Suggestion: "Verify that the tracking system can handle the new data structure"
167
- WHY DELETE: "Verify" is banned. "tracking system" is banned. No code change provided.
168
-
169
- ❌ Description: "Users might lose access to features"
170
- Suggestion: "Ensure that all users are properly migrated"
171
- WHY DELETE: "Ensure" is banned. "migrated" is banned. No code change provided.
172
-
173
- ❌ Description: "Tracking data format inconsistency"
174
- Suggestion: "Consider normalizing the value or document that this experiment uses..."
175
- WHY DELETE: "Consider" is banned. "document" is banned. "experiment" is banned.
176
-
177
- **EXAMPLES OF ACCEPTABLE ISSUES:**
178
- ✅ Description: "The function returns null but the return type doesn't allow null"
179
- Suggestion: "Change the return type from \`string\` to \`string | null\` on line 42"
180
- WHY ACCEPT: Identifies a specific bug with a specific code change.
181
-
182
- ✅ Description: "Missing null check will cause runtime error"
183
- Suggestion: "Add optional chaining: change \`user.name\` to \`user?.name\` on line 15"
184
- WHY ACCEPT: Identifies a specific problem with exact code to fix it.
185
-
186
- ✅ Description: "Promise is not awaited"
187
- Suggestion: "Add \`await\` before \`fetchData()\` on line 28"
188
- WHY ACCEPT: Specific bug with specific code fix.
189
-
190
- **THE ONLY ACCEPTABLE ISSUE FORMAT:**
191
- 1. Description: Identifies a SPECIFIC BUG or CODE QUALITY PROBLEM
192
- 2. Suggestion: Provides EXACT CODE CHANGE to fix it (no "consider", "verify", "ensure", "document")
193
-
194
- **FINAL CHECK BEFORE INCLUDING ANY ISSUE:**
195
- □ Does my suggestion contain "consider", "verify", "ensure", "document", "check", or "confirm"? → DELETE
196
- □ Does my suggestion mention "analytics", "tracking", "migration", "downstream", or "experiment"? → DELETE
197
- □ Is my suggestion a request for someone to do something (not a code change)? → DELETE
198
- □ Can I write the exact code change in the suggestion? → If NO, DELETE
199
-
200
- When in doubt, DELETE THE ISSUE. Only report issues with EXACT CODE FIXES.`;
201
- }
202
-
203
- /**
204
- * Generate the common citation requirement block
205
- * @returns {string} Citation requirement block
206
- */
207
- function getCitationRequirementBlock() {
208
- return `**🚨 CRITICAL CITATION REQUIREMENT 🚨**
209
- When you identify issues that violate custom instructions provided at the beginning of this prompt, you MUST:
210
- - Include the source document name in your issue description (e.g., "violates the coding standards specified in '[Document Name]'")
211
- - Reference the source document in your suggestion (e.g., "as required by '[Document Name]'" or "according to '[Document Name]'")
212
- - Do NOT provide generic suggestions - always tie violations back to the specific custom instruction source`;
213
- }
214
-
215
- /**
216
- * Generate the common code suggestions format block
217
- * @returns {string} Code suggestions format block
218
- */
219
- function getCodeSuggestionsFormatBlock() {
220
- return `**🚨 CODE SUGGESTIONS FORMAT 🚨**
221
- When suggesting code changes, you can optionally include a codeSuggestion object with:
222
- - startLine: The starting line number of the code to replace
223
- - endLine: (optional) The ending line number if replacing multiple lines
224
- - oldCode: The exact current code that should be replaced (must match exactly)
225
- - newCode: The proposed replacement code
226
-
227
- Code suggestions enable reviewers to apply fixes directly as GitHub suggestions. Only provide code suggestions when:
228
- 1. The fix is concrete and can be applied automatically
229
- 2. You have the exact current code from the file content
230
- 3. The suggestion is a direct code replacement (not architectural changes)`;
231
- }
232
-
233
- /**
234
- * Generate the final reminder block for custom instructions
235
- * @returns {string} Final reminder block
236
- */
237
- function getFinalReminderBlock() {
238
- return `**FINAL REMINDER: If custom instructions were provided at the start of this prompt, they MUST be followed and take precedence over all other guidelines.**`;
239
- }
240
-
241
69
  /**
242
70
  * Format custom docs section for prompts
243
71
  * @param {Array} customDocs - Array of custom document chunks
@@ -920,18 +748,21 @@ async function callLLMForAnalysis(context, options = {}) {
920
748
  prompt = options.isTestFile ? generateTestFileAnalysisPrompt(context) : generateAnalysisPrompt(context);
921
749
  }
922
750
 
923
- // Call LLM with the prompt
751
+ // Call LLM with the prompt (supports both cached and non-cached structures)
924
752
  const llmResponse = await sendPromptToLLM(prompt, {
925
753
  temperature: 0,
926
754
  maxTokens: maxTokens,
927
755
  model: model,
928
756
  isJsonMode: true, // Standardize on using JSON mode if available
757
+ verbose: options.verbose || false, // Pass verbose flag for cache statistics
758
+ cacheTtl: options.cacheTtl || '5m', // Pass cache TTL option (default: 5m, no extra cost)
929
759
  });
930
760
 
931
761
  console.log(chalk.blue('Received LLM response, attempting to parse...'));
932
762
 
933
763
  console.log(chalk.gray(`Response type: ${typeof llmResponse}`));
934
- console.log(chalk.gray(`Response length: ${llmResponse?.length || 0} characters`));
764
+ console.log(chalk.gray(`Response has json: ${!!llmResponse?.json}`));
765
+ console.log(chalk.gray(`Response content length: ${llmResponse?.content?.length || 0} characters`));
935
766
 
936
767
  // Parse the raw LLM response
937
768
  const analysisResponse = parseAnalysisResponse(llmResponse);
@@ -993,8 +824,8 @@ MARKDOWN FORMATTING IN DESCRIPTIONS AND SUGGESTIONS:
993
824
  Your response must start with { and end with } with no additional text.`;
994
825
  }
995
826
 
996
- // LLM call function
997
- async function sendPromptToLLM(prompt, llmOptions) {
827
+ // LLM call function - sends prompt with cached system content for cost optimization
828
+ async function sendPromptToLLM(promptConfig, llmOptions) {
998
829
  try {
999
830
  if (!llm || typeof llm.sendPromptToClaude !== 'function') {
1000
831
  throw new Error('LLM module does not contain required function: sendPromptToClaude');
@@ -1100,24 +931,46 @@ async function sendPromptToLLM(prompt, llmOptions) {
1100
931
  required: ['summary'],
1101
932
  };
1102
933
 
1103
- const response = await llm.sendPromptToClaude(prompt, {
934
+ // Debug: Log prompt structure
935
+ console.log(chalk.gray(` Prompt config has systemPrompt: ${!!promptConfig.systemPrompt}`));
936
+ console.log(chalk.gray(` Prompt config has userPrompt: ${!!promptConfig.userPrompt}`));
937
+ console.log(chalk.gray(` System prompt length: ${promptConfig.systemPrompt?.length || 0} chars`));
938
+ console.log(chalk.gray(` User prompt length: ${promptConfig.userPrompt?.length || 0} chars`));
939
+
940
+ // Send prompt with system (cached) and user (dynamic) content
941
+ const response = await llm.sendPromptToClaude(promptConfig.userPrompt, {
1104
942
  ...llmOptions,
943
+ system: promptConfig.systemPrompt,
1105
944
  jsonSchema: codeReviewSchema,
1106
945
  });
1107
946
 
1108
- // Return the response object so parseAnalysisResponse can access the json property
1109
947
  return response;
1110
948
  } catch (error) {
1111
949
  console.error(chalk.red(`Error in LLM call: ${error.message}`));
1112
- throw error; // Re-throw to properly handle the error
950
+ throw error;
1113
951
  }
1114
952
  }
1115
953
 
1116
954
  /**
1117
- * Generate analysis prompt for LLM
955
+ * Generate analysis prompt for LLM with support for prompt caching.
956
+ * Returns separate system and user prompts for optimal caching.
957
+ *
958
+ * PROMPT ARCHITECTURE FOR CACHING:
959
+ * - System prompt (cached): Contains all static rules, analysis methodology, and JSON schema
960
+ * - User prompt (dynamic): Contains the actual file content, code examples, and guidelines
961
+ *
962
+ * WHY STATIC INSTRUCTIONS APPEAR AT THE END OF USER PROMPT ("YOUR TASK" section):
963
+ * While the detailed analysis methodology is in the cached system prompt, we include a brief
964
+ * task summary at the END of the user prompt because:
965
+ * 1. LLMs perform better when task instructions are near the content they reference
966
+ * 2. The system prompt's strict filtering rules ("delete the issue when in doubt") can cause
967
+ * over-filtering when the task context is too far away
968
+ * 3. The "CRITICAL RULES" section counterbalances the aggressive filtering by reminding
969
+ * the LLM to report actual issues with concrete fixes
970
+ * 4. This hybrid approach preserves caching benefits while maintaining review quality
1118
971
  *
1119
972
  * @param {Object} context - Context for LLM
1120
- * @returns {string} Analysis prompt
973
+ * @returns {Object} { systemPrompt, userPrompt } for cached prompt structure
1121
974
  */
1122
975
  function generateAnalysisPrompt(context) {
1123
976
  const { file, codeExamples, guidelineSnippets, customDocs, feedbackContext } = context;
@@ -1227,8 +1080,13 @@ ${file.content}
1227
1080
  'code'
1228
1081
  );
1229
1082
 
1230
- // Corrected prompt with full two-stage analysis + combined output stage
1231
- return finalizePrompt(`
1083
+ // Build the cached system prompt (contains all static rules)
1084
+ const systemPrompt = buildCachedSystemPrompt('code');
1085
+
1086
+ // Build the dynamic user prompt (contains all variable content)
1087
+ const userPrompt = finalizePrompt(`
1088
+ ## REVIEW TYPE: CODE FILE ANALYSIS
1089
+
1232
1090
  ${roleDefinition}
1233
1091
 
1234
1092
  ${reviewInstructions}
@@ -1250,114 +1108,29 @@ ${prHistorySection}
1250
1108
 
1251
1109
  ${feedbackContext || ''}
1252
1110
 
1253
- INSTRUCTIONS:
1254
-
1255
- ${getCriticalRulesBlock({ importRuleContext: 'code' })}
1256
-
1257
- **Perform the following analysis stages sequentially:**
1258
-
1259
- **STAGE 1: Custom Instructions & Guideline-Based Review**
1260
- 1. **FIRST AND MOST IMPORTANT**: If custom instructions were provided at the beginning of this prompt, analyze the 'FILE TO REVIEW' against those custom instructions BEFORE all other analysis. Custom instructions always take precedence.
1261
- 2. Analyze the 'FILE TO REVIEW' strictly against the standards, rules, and explanations provided in 'CONTEXT A: EXPLICIT GUIDELINES'.
1262
- 3. Identify any specific deviations where the reviewed code violates custom instructions OR explicit guidelines. **CRITICAL**: When you find violations of custom instructions, you MUST cite the specific custom instruction source document name in your issue description and suggestion.
1263
- 4. Temporarily ignore 'CONTEXT B: SIMILAR CODE EXAMPLES' during this stage.
1264
-
1265
- **STAGE 2: Code Example-Based Review (CRITICAL FOR IMPLICIT PATTERNS)**
1266
- 1. **CRITICAL FIRST STEP**: Scan ALL code examples in Context B and create a mental list of:
1267
- - Common import statements (especially those containing 'helper', 'util', 'shared', 'common', 'test')
1268
- - Frequently used function calls that appear across multiple examples
1269
- - Project-specific wrappers or utilities (e.g., \`renderWithTestHelpers\` instead of direct \`render\`)
1270
- - Consistent patterns in how operations are performed
1271
- 2. **IMPORTANT**: For each common utility or pattern you identify, note:
1272
- - Which files use it (cite specific examples)
1273
- - What the pattern appears to do
1274
- - Whether the reviewed file is using this pattern or not
1275
- 3. Analyze the 'FILE TO REVIEW' against these discovered patterns. Focus on:
1276
- - Missing imports of commonly used utilities
1277
- - Direct library usage where others use project wrappers
1278
- - Deviations from established patterns
1279
- 4. **HIGH PRIORITY**: Flag any instances where:
1280
- - The reviewed code uses a direct library call (e.g., \`render\`) when multiple examples use a project wrapper (e.g., \`renderWithTestHelpers\`)
1281
- - Common utility functions available in the project are not being imported or used
1282
- - The code deviates from patterns that appear in 3+ examples
1283
- 5. Pay special attention to imports - if most similar files import certain utilities, the reviewed file should too.
1284
-
1285
- **STAGE 3: Historical Review Comments Analysis**
1286
- 1. **CRITICAL**: If 'CONTEXT C: HISTORICAL REVIEW COMMENTS' is present, analyze each historical comment:
1287
- - Look for patterns in the types of issues human reviewers have identified in similar code
1288
- - Identify if the SAME DEFINITE issue exists in the current file (not similar - the SAME)
1289
- - Pay special attention to comments with high relevance scores (>70%)
1290
- 2. **Apply Historical Insights**: For each historical comment:
1291
- - Only report if the EXACT same issue type exists with a SPECIFIC code fix
1292
- - Do NOT report speculative issues based on historical patterns
1293
- 3. **Prioritize Historical Issues**: Issues DEFINITELY matching historical patterns get high priority
1294
-
1295
- **STAGE 4: Consolidate, Prioritize, and Generate Output**
1296
- 1. **CRITICAL REMINDER**: If custom instructions were provided at the beginning of this prompt, they take ABSOLUTE PRECEDENCE over all other guidelines and must be followed strictly.
1297
- 2. Combine the potential issues identified in Stage 1 (Guideline-Based), Stage 2 (Example-Based), and Stage 3 (Historical Review Comments).
1298
- 3. **Apply Conflict Resolution AND Citation Rules:**
1299
- * **Guideline Precedence:** If an issue identified in Stage 2 (from code examples) or Stage 3 (from historical comments) **contradicts** an explicit guideline from Stage 1, **discard the conflicting issue**. Guidelines always take precedence.
1300
- * **Citation Priority:** When reporting an issue:
1301
- * **CRITICAL FOR CUSTOM INSTRUCTIONS**: If the issue violates a custom instruction provided at the beginning of this prompt, you MUST include the source document name in both the description and suggestion. For example: "violates the coding standards specified in '[Document Name]'" or "as required by '[Document Name]'".
1302
- * If the relevant convention or standard is defined in 'CONTEXT A: EXPLICIT GUIDELINES', cite the guideline document.
1303
- * For implicit patterns discovered from code examples (like helper utilities, common practices), cite the specific code examples that demonstrate the pattern.
1304
- * For issues identified from historical review comments, report them as standard code review findings without referencing the historical source.
1305
- * **IMPORTANT**: When citing implicit patterns from Context B, be specific about which files demonstrate the pattern and what the pattern is.
1306
- 4. **Special attention to implicit patterns**: Issues related to not using project-specific utilities or helpers should be marked as high priority if the pattern appears consistently across multiple examples in Context B.
1307
- 5. **Special attention to historical patterns**: Issues DEFINITELY matching historical patterns get high priority.
1308
- 6. Assess for DEFINITE logic errors or bugs only - do NOT report speculative issues.
1309
- 7. **CRITICAL OUTPUT FILTER**: Before reporting ANY issue, ask yourself: "Do I have a SPECIFIC code fix?" If not, do NOT report it. Do NOT ask the developer to verify, ensure, or check anything.
1310
- 8. **CRITICAL 'lineNumbers' RULE - MANDATORY COMPLIANCE**:
1311
- - **ALWAYS provide line numbers** - this field is REQUIRED for every issue
1312
- - If you can identify specific lines, provide them (max 3-5 for repeated issues)
1313
- - If the issue affects the entire file or cannot be pinpointed, provide [1] or relevant section line numbers
1314
- - For ANY issue that occurs multiple times in a file, list ONLY the first 3-5 occurrences maximum
1315
- - NEVER provide exhaustive lists of line numbers (e.g., [1,2,3,4,5,6,7,8,9,10...])
1316
- - If an issue affects many lines, use representative examples only
1317
- - Exhaustive line number lists are considered hallucination and must be avoided
1318
- - Example: Instead of listing 20+ line numbers, use [15, 23, 47]
1319
- - **NEVER omit lineNumbers** - empty arrays [] are not allowed
1320
- 9. Format the final, consolidated, and prioritized list of issues, along with a brief overall summary, **strictly** according to the JSON structure below.
1321
- 10. CRITICAL: Respond ONLY with valid JSON - start with { and end with }, no additional text.
1322
-
1323
- ${getFinalReminderBlock()}
1324
-
1325
- ${getCitationRequirementBlock()}
1326
-
1327
- REQUIRED JSON OUTPUT FORMAT:
1328
-
1329
- **REMINDER: lineNumbers is REQUIRED - always provide at least one line number. Use ONLY 3-5 representative line numbers for repeated issues. NEVER provide exhaustive lists or empty arrays.**
1330
-
1331
- ${getCodeSuggestionsFormatBlock()}
1332
-
1333
- You must respond with EXACTLY this JSON structure, with no additional text:
1334
-
1335
- {
1336
- "summary": "Brief summary of the review, highlighting adherence to documented guidelines and consistency with code examples, plus any major issues found.",
1337
- "issues": [
1338
- {
1339
- "type": "bug | improvement | convention | performance | security",
1340
- "severity": "critical | high | medium",
1341
- "description": "Description of the issue, clearly stating the deviation from the prioritized project pattern (guideline or example) OR the nature of the bug/improvement.",
1342
- "lineNumbers": [42, 55, 61],
1343
- "suggestion": "Concrete suggestion for fixing the issue or aligning with the prioritized inferred pattern. Ensure the suggestion is additive if adding missing functionality (like a hook) and doesn't wrongly suggest replacing existing, unrelated code.",
1344
- "codeSuggestion": {
1345
- "startLine": 42,
1346
- "endLine": 44,
1347
- "oldCode": " const result = data.map(item => item.value);",
1348
- "newCode": " const result = data?.map(item => item?.value) ?? [];"
1349
- }
1350
- }
1351
- ]
1352
- }
1111
+ ## YOUR TASK
1112
+
1113
+ Analyze the FILE TO REVIEW above using the 4-stage methodology:
1114
+ 1. Check against CONTEXT A (Guidelines) and any custom instructions
1115
+ 2. Check against CONTEXT B (Code Examples) for implicit patterns - flag deviations from patterns appearing in 3+ examples
1116
+ 3. Check CONTEXT C (Historical Comments) for matching issues
1117
+ 4. Consolidate and output issues in the required JSON format
1118
+
1119
+ **CRITICAL RULES**:
1120
+ - **ONLY report issues that require a code change** - if your analysis concludes the code is correct, DO NOT report it as an issue
1121
+ - Every issue MUST have a concrete code fix in the suggestion - never suggest "no action needed" or "the code is correct"
1122
+ - If you analyzed something and found it's fine, simply don't include it in the output
1353
1123
  `);
1124
+
1125
+ return { systemPrompt, userPrompt };
1354
1126
  }
1355
1127
 
1356
1128
  /**
1357
- * Generate test file analysis prompt for LLM
1129
+ * Generate test file analysis prompt for LLM with support for prompt caching.
1130
+ * Returns separate system and user prompts for optimal caching.
1358
1131
  *
1359
1132
  * @param {Object} context - Context for LLM
1360
- * @returns {string} Test file analysis prompt
1133
+ * @returns {Object} { systemPrompt, userPrompt } for cached prompt structure
1361
1134
  */
1362
1135
  function generateTestFileAnalysisPrompt(context) {
1363
1136
  const { file, codeExamples, guidelineSnippets, customDocs } = context;
@@ -1426,8 +1199,13 @@ ${file.content}
1426
1199
  projectArchitectureSection = formatProjectSummaryForLLM(context.projectSummary);
1427
1200
  }
1428
1201
 
1429
- // Test-specific prompt
1430
- return finalizePrompt(`
1202
+ // Build the cached system prompt (contains all static rules)
1203
+ const systemPrompt = buildCachedSystemPrompt('test');
1204
+
1205
+ // Build the dynamic user prompt (contains all variable content)
1206
+ const userPrompt = finalizePrompt(`
1207
+ ## REVIEW TYPE: TEST FILE ANALYSIS
1208
+
1431
1209
  ${roleDefinition}
1432
1210
 
1433
1211
  ${reviewInstructions}
@@ -1446,90 +1224,30 @@ ${formattedGuidelines}
1446
1224
  CONTEXT B: SIMILAR TEST EXAMPLES FROM PROJECT
1447
1225
  ${formattedCodeExamples}
1448
1226
 
1449
- INSTRUCTIONS:
1450
-
1451
- ${getCriticalRulesBlock({ importRuleContext: 'test' })}
1452
-
1453
- **Perform the following test-specific analysis:**
1454
-
1455
- **STAGE 1: Custom Instructions & Test Coverage Analysis**
1456
- 1. **FIRST AND MOST IMPORTANT**: If custom instructions were provided at the beginning of this prompt, analyze the test file against those custom instructions BEFORE all other analysis. Custom instructions always take precedence.
1457
- 2. Analyze test coverage - identify SPECIFIC missing test cases only if you can name the exact scenario that should be tested.
1458
- 3. Only report coverage gaps where you can provide a concrete test case to add.
1459
-
1460
- **STAGE 2: Test Quality and Best Practices**
1461
- 1. Evaluate test naming conventions - report only DEFINITE violations where you can show the correct naming.
1462
- 2. Analyze test organization - report only if tests are clearly misorganized with a specific fix.
1463
- 3. Assess assertion quality - report only weak assertions where you can provide a stronger alternative.
1464
- 4. Review test isolation - report only if you find a DEFINITE side effect issue with a specific fix.
1465
-
1466
- **STAGE 3: Testing Patterns and Conventions (CRITICAL)**
1467
- 1. **IMPORTANT**: Carefully analyze ALL code examples in Context B to identify:
1468
- - Common helper functions or utilities that appear across multiple test files
1469
- - Consistent patterns in how certain operations are performed (e.g., rendering, mocking, assertions)
1470
- - Any project-specific abstractions or wrappers around standard testing libraries
1471
- 2. **CRITICAL**: Compare the reviewed test file against these discovered patterns. Flag ONLY instances where:
1472
- - The test DEFINITELY uses a direct library call when a project wrapper exists (cite the wrapper)
1473
- - A common utility is DEFINITELY available but not used (cite where it's defined)
1474
- - The test CLEARLY deviates from a pattern shown in 3+ examples (cite the examples)
1475
- 3. Report mocking/stubbing issues only with a specific code fix.
1476
- 4. Report fixture issues only with a specific code fix showing the correct pattern.
1477
- 5. Report async handling issues only with specific code showing the correct approach.
1478
-
1479
- **STAGE 4: Performance and Maintainability**
1480
- 1. Report slow tests only if you can identify the specific cause and fix.
1481
- 2. Report code duplication only with a specific refactoring suggestion.
1482
-
1483
- **STAGE 5: Consolidate and Generate Output**
1484
- 1. **CRITICAL**: Prioritize issues where the test deviates from implicit project patterns shown in Context B (similar test examples), especially regarding test utilities and helper functions.
1485
- 2. Provide concrete suggestions that align with the project's testing patterns, referencing specific examples from Context B when applicable.
1486
- 3. Assess for any potential logic errors or bugs within the reviewed code itself, independent of conventions, and include them as separate issues.
1487
- 4. **CRITICAL 'lineNumbers' RULE - MANDATORY COMPLIANCE**:
1488
- - For ANY issue that occurs multiple times in a test file, list ONLY the first 3-5 occurrences maximum
1489
- - NEVER provide exhaustive lists of line numbers (e.g., [1,2,3,4,5,6,7,8,9,10...])
1490
- - If an issue affects many lines, use representative examples only
1491
- - Exhaustive line number lists are considered hallucination and must be avoided
1492
- - Example: Instead of listing 20+ line numbers, use [15, 23, 47, "...and 12 other occurrences"]
1493
- 5. Format the output according to the JSON structure below.
1494
-
1495
- ${getFinalReminderBlock()}
1496
-
1497
- ${getCitationRequirementBlock()}
1498
-
1499
- REQUIRED JSON OUTPUT FORMAT:
1500
-
1501
- **REMINDER: For lineNumbers array, use ONLY 3-5 representative line numbers for repeated issues. NEVER provide exhaustive lists.**
1502
-
1503
- ${getCodeSuggestionsFormatBlock()}
1504
-
1505
- You must respond with EXACTLY this JSON structure, with no additional text:
1506
-
1507
- {
1508
- "summary": "Brief summary of the test file review, highlighting coverage completeness, adherence to testing best practices, and any critical issues found.",
1509
- "issues": [
1510
- {
1511
- "type": "bug | improvement | convention | performance | coverage",
1512
- "severity": "critical | high | medium",
1513
- "description": "Description of the issue, clearly stating the problem with the test implementation or coverage gap.",
1514
- "lineNumbers": [25, 38],
1515
- "suggestion": "Concrete suggestion for improving the test, adding missing coverage, or following testing best practices.",
1516
- "codeSuggestion": {
1517
- "startLine": 25,
1518
- "endLine": 27,
1519
- "oldCode": " expect(result).toBe(true);",
1520
- "newCode": " expect(result).toBe(true);\n expect(result).not.toBeNull();"
1521
- }
1522
- }
1523
- ]
1524
- }
1227
+ ## YOUR TASK
1228
+
1229
+ Analyze the TEST FILE above using the 5-stage methodology:
1230
+ 1. Check against custom instructions and test coverage
1231
+ 2. Evaluate test quality and best practices
1232
+ 3. Check CONTEXT B for testing patterns - flag deviations from patterns in 3+ examples
1233
+ 4. Check for performance and maintainability issues
1234
+ 5. Consolidate and output issues in the required JSON format
1235
+
1236
+ **CRITICAL RULES**:
1237
+ - **ONLY report issues that require a code change** - if your analysis concludes the code is correct, DO NOT report it as an issue
1238
+ - Every issue MUST have a concrete code fix in the suggestion - never suggest "no action needed" or "the code is correct"
1239
+ - If you analyzed something and found it's fine, simply don't include it in the output
1525
1240
  `);
1241
+
1242
+ return { systemPrompt, userPrompt };
1526
1243
  }
1527
1244
 
1528
1245
  /**
1529
- * Generate holistic PR analysis prompt for LLM
1246
+ * Generate holistic PR analysis prompt for LLM with support for prompt caching.
1247
+ * Returns separate system and user prompts for optimal caching.
1530
1248
  *
1531
1249
  * @param {Object} context - Holistic context for LLM
1532
- * @returns {string} Holistic PR analysis prompt
1250
+ * @returns {Object} { systemPrompt, userPrompt } for cached prompt structure
1533
1251
  */
1534
1252
  function generateHolisticPRAnalysisPrompt(context) {
1535
1253
  const { file, context: contextSections, customDocs } = context;
@@ -1637,7 +1355,13 @@ For each file in this PR, you have access to:
1637
1355
  projectArchitectureSection = formatProjectSummaryForLLM(context.projectSummary);
1638
1356
  }
1639
1357
 
1640
- return finalizePrompt(`
1358
+ // Build the cached system prompt (contains all static rules)
1359
+ const systemPrompt = buildCachedSystemPrompt('pr');
1360
+
1361
+ // Build the dynamic user prompt (contains all variable content)
1362
+ const userPrompt = finalizePrompt(`
1363
+ ## REVIEW TYPE: HOLISTIC PR ANALYSIS
1364
+
1641
1365
  ${roleDefinition}
1642
1366
 
1643
1367
  ## PULL REQUEST OVERVIEW
@@ -1660,151 +1384,27 @@ ${formattedPRComments}
1660
1384
  ## PR FILES WITH CHANGES
1661
1385
  ${formattedPRFiles}
1662
1386
 
1663
- ## ANALYSIS CONTEXT
1387
+ ## CUSTOM INSTRUCTIONS
1664
1388
  ${customDocsSection}
1665
1389
 
1666
- ## ANALYSIS INSTRUCTIONS
1667
-
1668
- ${getCriticalRulesBlock({ importRuleContext: 'pr' })}
1669
-
1670
- **Perform the following holistic analysis stages sequentially for all PR files:**
1671
-
1672
- ### **STAGE 1: Project Pattern Analysis (CRITICAL FOR CONSISTENCY)**
1673
-
1674
- 1. **CRITICAL FIRST STEP**: Scan ALL code examples in PROJECT CODE EXAMPLES and create a comprehensive list of:
1675
- - Common import statements (especially those containing 'helper', 'util', 'shared', 'common', 'test')
1676
- - Frequently used function calls that appear across multiple examples
1677
- - Project-specific wrappers or utilities (e.g., \`renderWithTestHelpers\` instead of direct \`render\`)
1678
- - Consistent patterns in how operations are performed
1679
- - Testing patterns and helper functions
1680
- - Component patterns and architectural approaches
1681
-
1682
- 2. **IMPORTANT**: For each common utility or pattern you identify, note:
1683
- - Which example files demonstrate it (cite specific examples)
1684
- - What the pattern appears to do
1685
- - Whether ALL PR files are using this pattern consistently
1686
-
1687
- 3. **HIGH PRIORITY CROSS-FILE CHECKS**: Flag any instances where:
1688
- - Files use direct library calls when multiple examples use project wrappers
1689
- - Common utility functions available in the project are not being imported/used consistently
1690
- - Files deviate from patterns that appear in 3+ examples
1691
- - Test files don't follow established test helper patterns
1692
- - Import statements are inconsistent across similar files
1693
-
1694
- ### **STAGE 2: Custom Instructions & Guideline Compliance Analysis**
1695
-
1696
- 1. **FIRST AND MOST IMPORTANT**: If custom instructions were provided at the beginning of this prompt, analyze ALL PR files against those custom instructions BEFORE all other analysis. Custom instructions always take precedence.
1697
- 2. Analyze ALL PR files strictly against the standards, rules, and explanations in PROJECT GUIDELINES
1698
- 3. Identify specific deviations where any file violates custom instructions OR explicit guidelines. Note the source for each deviation found.
1699
- 4. Check for consistency of guideline application across all files
1700
- 5. Ensure architectural decisions are consistent across the PR
1701
-
1702
- ### **STAGE 3: Historical Pattern Recognition**
1703
-
1704
- 1. **CRITICAL**: Analyze HISTORICAL REVIEW COMMENTS to identify patterns:
1705
- - Types of issues human reviewers frequently flag in similar code
1706
- - Recurring themes across multiple historical comments
1707
- - High-relevance issues (>70% relevance score) that apply to current PR
1708
-
1709
- 2. **Apply Historical Insights to Each File**:
1710
- - Identify DEFINITE issues that match historical patterns across PR files
1711
- - Apply reviewer suggestions that are relevant to current changes
1712
- - Look for patterns that span multiple files in the PR
1713
-
1714
- ### **STAGE 4: Cross-File Integration Analysis**
1715
-
1716
- 1. **Naming and Import Consistency**:
1717
- - Report naming inconsistencies only with specific examples and fixes
1718
- - Report import/export issues only with specific missing/incorrect imports identified
1719
- - Report duplicated logic only with specific refactoring suggestions
1720
-
1721
- 2. **Test Coverage and Quality**:
1722
- - Report missing tests only if you can specify EXACTLY which test case should be added
1723
- - Report test pattern deviations only with specific code fixes
1724
- - Do NOT suggest "adding tests" without specifying the exact test
1725
-
1726
- 3. **Architectural Integration**:
1727
- - Report breaking changes only if you can identify the SPECIFIC break
1728
- - Report API inconsistencies only with SPECIFIC mismatches identified
1729
- - Report separation of concerns issues only with SPECIFIC refactoring suggestions
1730
-
1731
- ### **STAGE 5: Consolidate and Prioritize Issues**
1732
-
1733
- 1. **Apply Conflict Resolution Rules**:
1734
- - **Guideline Precedence**: If pattern-based or historical insights contradict explicit guidelines, guidelines take precedence
1735
- - **Cross-File Priority**: Issues affecting multiple files get higher priority
1736
- - **Pattern Consistency**: Missing project-specific utilities/helpers are high priority if pattern appears in 3+ examples
1737
-
1738
- 2. **Citation Rules**:
1739
- - For guideline violations: cite the specific guideline document
1740
- - For pattern deviations: cite specific code examples that demonstrate the correct pattern
1741
- - For historical issues: report as standard findings without referencing historical source
1742
- - For cross-file issues: specify all affected files
1743
-
1744
- 3. **CRITICAL OUTPUT FILTER - Apply before reporting ANY issue**:
1745
- - **Only report issues where you have a DEFINITE problem AND a SPECIFIC code fix**
1746
- - **Do NOT report issues that require the developer to "verify", "ensure", or "check" something**
1747
- - **Do NOT report issues where you are uncertain** - if you find yourself writing "may", "might", "could", or "consider", do not report it
1748
- - **Do NOT suggest adding comments or documentation**
1749
-
1750
- 4. Assess for DEFINITE logic errors or bugs only - do not report speculative issues.
1751
- 5. DO NOT check if any file referenced in a import statement, is missing.
1752
- 6. **CRITICAL 'lineNumbers' RULE - MANDATORY COMPLIANCE**:
1753
- - For ANY issue that occurs multiple times in a file, list ONLY the first 3-5 occurrences maximum
1754
- - NEVER provide exhaustive lists of line numbers (e.g., [1,2,3,4,5,6,7,8,9,10...])
1755
- - If an issue affects many lines, use representative examples only
1756
- - Exhaustive line number lists are considered hallucination and must be avoided
1757
- - Example: Instead of listing 20+ line numbers, use [15, 23, 47, "...and 12 other occurrences"]
1758
-
1759
- ${getFinalReminderBlock()}
1760
-
1761
- ${getCitationRequirementBlock()}
1762
-
1763
- REQUIRED JSON OUTPUT FORMAT:
1764
-
1765
- **REMINDER: For lineNumbers array, use ONLY 3-5 representative line numbers for repeated issues. NEVER provide exhaustive lists.**
1766
-
1767
- ${getCodeSuggestionsFormatBlock()}
1768
-
1769
- You must respond with EXACTLY this JSON structure, with no additional text:
1770
-
1771
- {
1772
- "summary": "Brief, high-level summary of the entire PR review...",
1773
- "crossFileIssues": [
1774
- {
1775
- "type": "bug | improvement | convention | architecture",
1776
- "severity": "critical | high | medium",
1777
- "description": "Detailed description of an issue that spans multiple files...",
1778
- "suggestion": "Actionable suggestion to resolve the cross-file issue.",
1779
- "filesInvolved": ["path/to/file1.js", "path/to/file2.ts"]
1780
- }
1781
- ],
1782
- "fileSpecificIssues": {
1783
- "path/to/file1.js": [
1784
- {
1785
- "type": "bug | improvement | convention | performance | security",
1786
- "severity": "critical | high | medium",
1787
- "description": "Description of the issue specific to this file.",
1788
- "lineNumbers": [10, 15],
1789
- "suggestion": "Concrete suggestion for fixing the issue in this file.",
1790
- "codeSuggestion": {
1791
- "startLine": 10,
1792
- "endLine": 15,
1793
- "oldCode": " const result = data.map(item => item.value);",
1794
- "newCode": " const result = data?.map(item => item?.value) ?? [];"
1795
- }
1796
- }
1797
- ]
1798
- },
1799
- "recommendations": [
1800
- {
1801
- "type": "refactoring | testing | documentation",
1802
- "description": "A high-level recommendation for improving the codebase...",
1803
- "filesInvolved": ["path/to/relevant/file.js"]
1804
- }
1805
- ]
1806
- }
1390
+ ## YOUR TASK
1391
+
1392
+ Analyze ALL PR FILES above using the 5-stage methodology:
1393
+ 1. Scan PROJECT CODE EXAMPLES for patterns - note which patterns appear in 3+ files
1394
+ 2. Check all files against custom instructions and PROJECT GUIDELINES
1395
+ 3. Check HISTORICAL REVIEW COMMENTS for matching issues
1396
+ 4. Perform cross-file analysis for consistency issues
1397
+ 5. Consolidate and output using the PR review JSON format (crossFileIssues, fileSpecificIssues, recommendations)
1398
+
1399
+ **CRITICAL RULES**:
1400
+ - Review ONLY the CHANGED lines in the git diffs (lines with + or -)
1401
+ - For cross-file issues, specify all affected files
1402
+ - **ONLY report issues that require a code change** - if your analysis concludes the code is correct, DO NOT report it as an issue
1403
+ - Every issue MUST have a concrete code fix in the suggestion - never suggest "no action needed" or "the code is correct"
1404
+ - If you analyzed something and found it's fine, simply don't include it in the output
1807
1405
  `);
1406
+
1407
+ return { systemPrompt, userPrompt };
1808
1408
  }
1809
1409
 
1810
1410
  /**