codecritique 1.1.0 → 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,127 +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
- **🚨 CRITICAL: ACTIONABLE CODE ISSUES ONLY - NO VERIFICATION REQUESTS 🚨**
108
- Your review must contain ONLY issues where you have identified a DEFINITE problem and can provide a SPECIFIC code fix.
109
-
110
- **AUTOMATIC REJECTION - If your suggestion contains ANY of these phrases, DO NOT include it:**
111
- - "Verify that..." / "Verify the..." / "Verify if..."
112
- - "Ensure that..." / "Ensure the..."
113
- - "Confirm that..." / "Confirm the..."
114
- - "Validate that..." / "Validate the..."
115
- - "Check that..." / "Check if..." / "Check whether..."
116
- - "Add a comment explaining..." / "Add documentation..."
117
- - "Review the documentation..." / "Reference the migration guide..."
118
- - "Consider whether..." / "Consider if..."
119
- - "This could potentially..." / "This might..." / "This may..."
120
- - "If this is intentional..." / "If this change is to fix..."
121
- - "...should be validated" / "...should be verified"
122
- - "...but there's no validation..." / "...but there's no verification..."
123
-
124
- **AUTOMATIC REJECTION - Process/workflow suggestions that are NOT code fixes:**
125
- - "Create a follow-up task..." / "Create a task to..."
126
- - "Document the migration..." / "Document this change..." / "Document the experiment..."
127
- - "Update any analytics..." / "Update any dashboards..." / "Update any reports..."
128
- - "Update any queries..." / "Update downstream..."
129
- - "Notify the team..." / "Communicate this change..." / "Make sure consumers are aware..."
130
- - "Archive the data..." / "Migrate the data..." / "Ensure historical data..."
131
- - "Plan the rollout..." / "Consider a phased rollout..." / "Manage this transition..."
132
- - "Once the migration is complete..." / "Once all consumers have migrated..."
133
- - "...can handle the new..." / "...can handle this change..."
134
- - "...are aware of this change" / "...is properly archived"
135
-
136
- **THE RULE**: If you cannot point to a SPECIFIC BUG or SPECIFIC VIOLATION and provide EXACT CODE to fix it, do not report it.
137
-
138
- **GOOD issue**: "The function returns null on line 42 but the return type doesn't allow null. Fix: Change return type to \`string | null\`"
139
- **BAD issue**: "Verify that the function handles null correctly" (This asks for verification, not a code fix)
140
- **BAD issue**: "The type cast may bypass type safety" (This expresses uncertainty - "may" - without identifying a definite problem)
141
- **BAD issue**: "Add a comment explaining why this type was changed" (This requests documentation, not a code fix)
142
-
143
- When in doubt, leave it out. Only report issues you are CERTAIN about.`;
144
- }
145
-
146
- /**
147
- * Generate the common citation requirement block
148
- * @returns {string} Citation requirement block
149
- */
150
- function getCitationRequirementBlock() {
151
- return `**🚨 CRITICAL CITATION REQUIREMENT 🚨**
152
- When you identify issues that violate custom instructions provided at the beginning of this prompt, you MUST:
153
- - Include the source document name in your issue description (e.g., "violates the coding standards specified in '[Document Name]'")
154
- - Reference the source document in your suggestion (e.g., "as required by '[Document Name]'" or "according to '[Document Name]'")
155
- - Do NOT provide generic suggestions - always tie violations back to the specific custom instruction source`;
156
- }
157
-
158
- /**
159
- * Generate the common code suggestions format block
160
- * @returns {string} Code suggestions format block
161
- */
162
- function getCodeSuggestionsFormatBlock() {
163
- return `**🚨 CODE SUGGESTIONS FORMAT 🚨**
164
- When suggesting code changes, you can optionally include a codeSuggestion object with:
165
- - startLine: The starting line number of the code to replace
166
- - endLine: (optional) The ending line number if replacing multiple lines
167
- - oldCode: The exact current code that should be replaced (must match exactly)
168
- - newCode: The proposed replacement code
169
-
170
- Code suggestions enable reviewers to apply fixes directly as GitHub suggestions. Only provide code suggestions when:
171
- 1. The fix is concrete and can be applied automatically
172
- 2. You have the exact current code from the file content
173
- 3. The suggestion is a direct code replacement (not architectural changes)`;
174
- }
175
-
176
- /**
177
- * Generate the final reminder block for custom instructions
178
- * @returns {string} Final reminder block
179
- */
180
- function getFinalReminderBlock() {
181
- 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.**`;
182
- }
183
-
184
69
  /**
185
70
  * Format custom docs section for prompts
186
71
  * @param {Array} customDocs - Array of custom document chunks
@@ -863,18 +748,21 @@ async function callLLMForAnalysis(context, options = {}) {
863
748
  prompt = options.isTestFile ? generateTestFileAnalysisPrompt(context) : generateAnalysisPrompt(context);
864
749
  }
865
750
 
866
- // Call LLM with the prompt
751
+ // Call LLM with the prompt (supports both cached and non-cached structures)
867
752
  const llmResponse = await sendPromptToLLM(prompt, {
868
753
  temperature: 0,
869
754
  maxTokens: maxTokens,
870
755
  model: model,
871
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)
872
759
  });
873
760
 
874
761
  console.log(chalk.blue('Received LLM response, attempting to parse...'));
875
762
 
876
763
  console.log(chalk.gray(`Response type: ${typeof llmResponse}`));
877
- 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`));
878
766
 
879
767
  // Parse the raw LLM response
880
768
  const analysisResponse = parseAnalysisResponse(llmResponse);
@@ -936,8 +824,8 @@ MARKDOWN FORMATTING IN DESCRIPTIONS AND SUGGESTIONS:
936
824
  Your response must start with { and end with } with no additional text.`;
937
825
  }
938
826
 
939
- // LLM call function
940
- async function sendPromptToLLM(prompt, llmOptions) {
827
+ // LLM call function - sends prompt with cached system content for cost optimization
828
+ async function sendPromptToLLM(promptConfig, llmOptions) {
941
829
  try {
942
830
  if (!llm || typeof llm.sendPromptToClaude !== 'function') {
943
831
  throw new Error('LLM module does not contain required function: sendPromptToClaude');
@@ -1043,24 +931,46 @@ async function sendPromptToLLM(prompt, llmOptions) {
1043
931
  required: ['summary'],
1044
932
  };
1045
933
 
1046
- 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, {
1047
942
  ...llmOptions,
943
+ system: promptConfig.systemPrompt,
1048
944
  jsonSchema: codeReviewSchema,
1049
945
  });
1050
946
 
1051
- // Return the response object so parseAnalysisResponse can access the json property
1052
947
  return response;
1053
948
  } catch (error) {
1054
949
  console.error(chalk.red(`Error in LLM call: ${error.message}`));
1055
- throw error; // Re-throw to properly handle the error
950
+ throw error;
1056
951
  }
1057
952
  }
1058
953
 
1059
954
  /**
1060
- * 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
1061
971
  *
1062
972
  * @param {Object} context - Context for LLM
1063
- * @returns {string} Analysis prompt
973
+ * @returns {Object} { systemPrompt, userPrompt } for cached prompt structure
1064
974
  */
1065
975
  function generateAnalysisPrompt(context) {
1066
976
  const { file, codeExamples, guidelineSnippets, customDocs, feedbackContext } = context;
@@ -1170,8 +1080,13 @@ ${file.content}
1170
1080
  'code'
1171
1081
  );
1172
1082
 
1173
- // Corrected prompt with full two-stage analysis + combined output stage
1174
- 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
+
1175
1090
  ${roleDefinition}
1176
1091
 
1177
1092
  ${reviewInstructions}
@@ -1193,114 +1108,29 @@ ${prHistorySection}
1193
1108
 
1194
1109
  ${feedbackContext || ''}
1195
1110
 
1196
- INSTRUCTIONS:
1197
-
1198
- ${getCriticalRulesBlock({ importRuleContext: 'code' })}
1199
-
1200
- **Perform the following analysis stages sequentially:**
1201
-
1202
- **STAGE 1: Custom Instructions & Guideline-Based Review**
1203
- 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.
1204
- 2. Analyze the 'FILE TO REVIEW' strictly against the standards, rules, and explanations provided in 'CONTEXT A: EXPLICIT GUIDELINES'.
1205
- 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.
1206
- 4. Temporarily ignore 'CONTEXT B: SIMILAR CODE EXAMPLES' during this stage.
1207
-
1208
- **STAGE 2: Code Example-Based Review (CRITICAL FOR IMPLICIT PATTERNS)**
1209
- 1. **CRITICAL FIRST STEP**: Scan ALL code examples in Context B and create a mental list of:
1210
- - Common import statements (especially those containing 'helper', 'util', 'shared', 'common', 'test')
1211
- - Frequently used function calls that appear across multiple examples
1212
- - Project-specific wrappers or utilities (e.g., \`renderWithTestHelpers\` instead of direct \`render\`)
1213
- - Consistent patterns in how operations are performed
1214
- 2. **IMPORTANT**: For each common utility or pattern you identify, note:
1215
- - Which files use it (cite specific examples)
1216
- - What the pattern appears to do
1217
- - Whether the reviewed file is using this pattern or not
1218
- 3. Analyze the 'FILE TO REVIEW' against these discovered patterns. Focus on:
1219
- - Missing imports of commonly used utilities
1220
- - Direct library usage where others use project wrappers
1221
- - Deviations from established patterns
1222
- 4. **HIGH PRIORITY**: Flag any instances where:
1223
- - The reviewed code uses a direct library call (e.g., \`render\`) when multiple examples use a project wrapper (e.g., \`renderWithTestHelpers\`)
1224
- - Common utility functions available in the project are not being imported or used
1225
- - The code deviates from patterns that appear in 3+ examples
1226
- 5. Pay special attention to imports - if most similar files import certain utilities, the reviewed file should too.
1227
-
1228
- **STAGE 3: Historical Review Comments Analysis**
1229
- 1. **CRITICAL**: If 'CONTEXT C: HISTORICAL REVIEW COMMENTS' is present, analyze each historical comment:
1230
- - Look for patterns in the types of issues human reviewers have identified in similar code
1231
- - Identify if the SAME DEFINITE issue exists in the current file (not similar - the SAME)
1232
- - Pay special attention to comments with high relevance scores (>70%)
1233
- 2. **Apply Historical Insights**: For each historical comment:
1234
- - Only report if the EXACT same issue type exists with a SPECIFIC code fix
1235
- - Do NOT report speculative issues based on historical patterns
1236
- 3. **Prioritize Historical Issues**: Issues DEFINITELY matching historical patterns get high priority
1237
-
1238
- **STAGE 4: Consolidate, Prioritize, and Generate Output**
1239
- 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.
1240
- 2. Combine the potential issues identified in Stage 1 (Guideline-Based), Stage 2 (Example-Based), and Stage 3 (Historical Review Comments).
1241
- 3. **Apply Conflict Resolution AND Citation Rules:**
1242
- * **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.
1243
- * **Citation Priority:** When reporting an issue:
1244
- * **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]'".
1245
- * If the relevant convention or standard is defined in 'CONTEXT A: EXPLICIT GUIDELINES', cite the guideline document.
1246
- * For implicit patterns discovered from code examples (like helper utilities, common practices), cite the specific code examples that demonstrate the pattern.
1247
- * For issues identified from historical review comments, report them as standard code review findings without referencing the historical source.
1248
- * **IMPORTANT**: When citing implicit patterns from Context B, be specific about which files demonstrate the pattern and what the pattern is.
1249
- 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.
1250
- 5. **Special attention to historical patterns**: Issues DEFINITELY matching historical patterns get high priority.
1251
- 6. Assess for DEFINITE logic errors or bugs only - do NOT report speculative issues.
1252
- 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.
1253
- 8. **CRITICAL 'lineNumbers' RULE - MANDATORY COMPLIANCE**:
1254
- - **ALWAYS provide line numbers** - this field is REQUIRED for every issue
1255
- - If you can identify specific lines, provide them (max 3-5 for repeated issues)
1256
- - If the issue affects the entire file or cannot be pinpointed, provide [1] or relevant section line numbers
1257
- - For ANY issue that occurs multiple times in a file, list ONLY the first 3-5 occurrences maximum
1258
- - NEVER provide exhaustive lists of line numbers (e.g., [1,2,3,4,5,6,7,8,9,10...])
1259
- - If an issue affects many lines, use representative examples only
1260
- - Exhaustive line number lists are considered hallucination and must be avoided
1261
- - Example: Instead of listing 20+ line numbers, use [15, 23, 47]
1262
- - **NEVER omit lineNumbers** - empty arrays [] are not allowed
1263
- 9. Format the final, consolidated, and prioritized list of issues, along with a brief overall summary, **strictly** according to the JSON structure below.
1264
- 10. CRITICAL: Respond ONLY with valid JSON - start with { and end with }, no additional text.
1265
-
1266
- ${getFinalReminderBlock()}
1267
-
1268
- ${getCitationRequirementBlock()}
1269
-
1270
- REQUIRED JSON OUTPUT FORMAT:
1271
-
1272
- **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.**
1273
-
1274
- ${getCodeSuggestionsFormatBlock()}
1275
-
1276
- You must respond with EXACTLY this JSON structure, with no additional text:
1277
-
1278
- {
1279
- "summary": "Brief summary of the review, highlighting adherence to documented guidelines and consistency with code examples, plus any major issues found.",
1280
- "issues": [
1281
- {
1282
- "type": "bug | improvement | convention | performance | security",
1283
- "severity": "critical | high | medium",
1284
- "description": "Description of the issue, clearly stating the deviation from the prioritized project pattern (guideline or example) OR the nature of the bug/improvement.",
1285
- "lineNumbers": [42, 55, 61],
1286
- "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.",
1287
- "codeSuggestion": {
1288
- "startLine": 42,
1289
- "endLine": 44,
1290
- "oldCode": " const result = data.map(item => item.value);",
1291
- "newCode": " const result = data?.map(item => item?.value) ?? [];"
1292
- }
1293
- }
1294
- ]
1295
- }
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
1296
1123
  `);
1124
+
1125
+ return { systemPrompt, userPrompt };
1297
1126
  }
1298
1127
 
1299
1128
  /**
1300
- * 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.
1301
1131
  *
1302
1132
  * @param {Object} context - Context for LLM
1303
- * @returns {string} Test file analysis prompt
1133
+ * @returns {Object} { systemPrompt, userPrompt } for cached prompt structure
1304
1134
  */
1305
1135
  function generateTestFileAnalysisPrompt(context) {
1306
1136
  const { file, codeExamples, guidelineSnippets, customDocs } = context;
@@ -1369,8 +1199,13 @@ ${file.content}
1369
1199
  projectArchitectureSection = formatProjectSummaryForLLM(context.projectSummary);
1370
1200
  }
1371
1201
 
1372
- // Test-specific prompt
1373
- 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
+
1374
1209
  ${roleDefinition}
1375
1210
 
1376
1211
  ${reviewInstructions}
@@ -1389,90 +1224,30 @@ ${formattedGuidelines}
1389
1224
  CONTEXT B: SIMILAR TEST EXAMPLES FROM PROJECT
1390
1225
  ${formattedCodeExamples}
1391
1226
 
1392
- INSTRUCTIONS:
1393
-
1394
- ${getCriticalRulesBlock({ importRuleContext: 'test' })}
1395
-
1396
- **Perform the following test-specific analysis:**
1397
-
1398
- **STAGE 1: Custom Instructions & Test Coverage Analysis**
1399
- 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.
1400
- 2. Analyze test coverage - identify SPECIFIC missing test cases only if you can name the exact scenario that should be tested.
1401
- 3. Only report coverage gaps where you can provide a concrete test case to add.
1402
-
1403
- **STAGE 2: Test Quality and Best Practices**
1404
- 1. Evaluate test naming conventions - report only DEFINITE violations where you can show the correct naming.
1405
- 2. Analyze test organization - report only if tests are clearly misorganized with a specific fix.
1406
- 3. Assess assertion quality - report only weak assertions where you can provide a stronger alternative.
1407
- 4. Review test isolation - report only if you find a DEFINITE side effect issue with a specific fix.
1408
-
1409
- **STAGE 3: Testing Patterns and Conventions (CRITICAL)**
1410
- 1. **IMPORTANT**: Carefully analyze ALL code examples in Context B to identify:
1411
- - Common helper functions or utilities that appear across multiple test files
1412
- - Consistent patterns in how certain operations are performed (e.g., rendering, mocking, assertions)
1413
- - Any project-specific abstractions or wrappers around standard testing libraries
1414
- 2. **CRITICAL**: Compare the reviewed test file against these discovered patterns. Flag ONLY instances where:
1415
- - The test DEFINITELY uses a direct library call when a project wrapper exists (cite the wrapper)
1416
- - A common utility is DEFINITELY available but not used (cite where it's defined)
1417
- - The test CLEARLY deviates from a pattern shown in 3+ examples (cite the examples)
1418
- 3. Report mocking/stubbing issues only with a specific code fix.
1419
- 4. Report fixture issues only with a specific code fix showing the correct pattern.
1420
- 5. Report async handling issues only with specific code showing the correct approach.
1421
-
1422
- **STAGE 4: Performance and Maintainability**
1423
- 1. Report slow tests only if you can identify the specific cause and fix.
1424
- 2. Report code duplication only with a specific refactoring suggestion.
1425
-
1426
- **STAGE 5: Consolidate and Generate Output**
1427
- 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.
1428
- 2. Provide concrete suggestions that align with the project's testing patterns, referencing specific examples from Context B when applicable.
1429
- 3. Assess for any potential logic errors or bugs within the reviewed code itself, independent of conventions, and include them as separate issues.
1430
- 4. **CRITICAL 'lineNumbers' RULE - MANDATORY COMPLIANCE**:
1431
- - For ANY issue that occurs multiple times in a test file, list ONLY the first 3-5 occurrences maximum
1432
- - NEVER provide exhaustive lists of line numbers (e.g., [1,2,3,4,5,6,7,8,9,10...])
1433
- - If an issue affects many lines, use representative examples only
1434
- - Exhaustive line number lists are considered hallucination and must be avoided
1435
- - Example: Instead of listing 20+ line numbers, use [15, 23, 47, "...and 12 other occurrences"]
1436
- 5. Format the output according to the JSON structure below.
1437
-
1438
- ${getFinalReminderBlock()}
1439
-
1440
- ${getCitationRequirementBlock()}
1441
-
1442
- REQUIRED JSON OUTPUT FORMAT:
1443
-
1444
- **REMINDER: For lineNumbers array, use ONLY 3-5 representative line numbers for repeated issues. NEVER provide exhaustive lists.**
1445
-
1446
- ${getCodeSuggestionsFormatBlock()}
1447
-
1448
- You must respond with EXACTLY this JSON structure, with no additional text:
1449
-
1450
- {
1451
- "summary": "Brief summary of the test file review, highlighting coverage completeness, adherence to testing best practices, and any critical issues found.",
1452
- "issues": [
1453
- {
1454
- "type": "bug | improvement | convention | performance | coverage",
1455
- "severity": "critical | high | medium",
1456
- "description": "Description of the issue, clearly stating the problem with the test implementation or coverage gap.",
1457
- "lineNumbers": [25, 38],
1458
- "suggestion": "Concrete suggestion for improving the test, adding missing coverage, or following testing best practices.",
1459
- "codeSuggestion": {
1460
- "startLine": 25,
1461
- "endLine": 27,
1462
- "oldCode": " expect(result).toBe(true);",
1463
- "newCode": " expect(result).toBe(true);\n expect(result).not.toBeNull();"
1464
- }
1465
- }
1466
- ]
1467
- }
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
1468
1240
  `);
1241
+
1242
+ return { systemPrompt, userPrompt };
1469
1243
  }
1470
1244
 
1471
1245
  /**
1472
- * 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.
1473
1248
  *
1474
1249
  * @param {Object} context - Holistic context for LLM
1475
- * @returns {string} Holistic PR analysis prompt
1250
+ * @returns {Object} { systemPrompt, userPrompt } for cached prompt structure
1476
1251
  */
1477
1252
  function generateHolisticPRAnalysisPrompt(context) {
1478
1253
  const { file, context: contextSections, customDocs } = context;
@@ -1580,7 +1355,13 @@ For each file in this PR, you have access to:
1580
1355
  projectArchitectureSection = formatProjectSummaryForLLM(context.projectSummary);
1581
1356
  }
1582
1357
 
1583
- 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
+
1584
1365
  ${roleDefinition}
1585
1366
 
1586
1367
  ## PULL REQUEST OVERVIEW
@@ -1603,151 +1384,27 @@ ${formattedPRComments}
1603
1384
  ## PR FILES WITH CHANGES
1604
1385
  ${formattedPRFiles}
1605
1386
 
1606
- ## ANALYSIS CONTEXT
1387
+ ## CUSTOM INSTRUCTIONS
1607
1388
  ${customDocsSection}
1608
1389
 
1609
- ## ANALYSIS INSTRUCTIONS
1610
-
1611
- ${getCriticalRulesBlock({ importRuleContext: 'pr' })}
1612
-
1613
- **Perform the following holistic analysis stages sequentially for all PR files:**
1614
-
1615
- ### **STAGE 1: Project Pattern Analysis (CRITICAL FOR CONSISTENCY)**
1616
-
1617
- 1. **CRITICAL FIRST STEP**: Scan ALL code examples in PROJECT CODE EXAMPLES and create a comprehensive list of:
1618
- - Common import statements (especially those containing 'helper', 'util', 'shared', 'common', 'test')
1619
- - Frequently used function calls that appear across multiple examples
1620
- - Project-specific wrappers or utilities (e.g., \`renderWithTestHelpers\` instead of direct \`render\`)
1621
- - Consistent patterns in how operations are performed
1622
- - Testing patterns and helper functions
1623
- - Component patterns and architectural approaches
1624
-
1625
- 2. **IMPORTANT**: For each common utility or pattern you identify, note:
1626
- - Which example files demonstrate it (cite specific examples)
1627
- - What the pattern appears to do
1628
- - Whether ALL PR files are using this pattern consistently
1629
-
1630
- 3. **HIGH PRIORITY CROSS-FILE CHECKS**: Flag any instances where:
1631
- - Files use direct library calls when multiple examples use project wrappers
1632
- - Common utility functions available in the project are not being imported/used consistently
1633
- - Files deviate from patterns that appear in 3+ examples
1634
- - Test files don't follow established test helper patterns
1635
- - Import statements are inconsistent across similar files
1636
-
1637
- ### **STAGE 2: Custom Instructions & Guideline Compliance Analysis**
1638
-
1639
- 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.
1640
- 2. Analyze ALL PR files strictly against the standards, rules, and explanations in PROJECT GUIDELINES
1641
- 3. Identify specific deviations where any file violates custom instructions OR explicit guidelines. Note the source for each deviation found.
1642
- 4. Check for consistency of guideline application across all files
1643
- 5. Ensure architectural decisions are consistent across the PR
1644
-
1645
- ### **STAGE 3: Historical Pattern Recognition**
1646
-
1647
- 1. **CRITICAL**: Analyze HISTORICAL REVIEW COMMENTS to identify patterns:
1648
- - Types of issues human reviewers frequently flag in similar code
1649
- - Recurring themes across multiple historical comments
1650
- - High-relevance issues (>70% relevance score) that apply to current PR
1651
-
1652
- 2. **Apply Historical Insights to Each File**:
1653
- - Identify DEFINITE issues that match historical patterns across PR files
1654
- - Apply reviewer suggestions that are relevant to current changes
1655
- - Look for patterns that span multiple files in the PR
1656
-
1657
- ### **STAGE 4: Cross-File Integration Analysis**
1658
-
1659
- 1. **Naming and Import Consistency**:
1660
- - Report naming inconsistencies only with specific examples and fixes
1661
- - Report import/export issues only with specific missing/incorrect imports identified
1662
- - Report duplicated logic only with specific refactoring suggestions
1663
-
1664
- 2. **Test Coverage and Quality**:
1665
- - Report missing tests only if you can specify EXACTLY which test case should be added
1666
- - Report test pattern deviations only with specific code fixes
1667
- - Do NOT suggest "adding tests" without specifying the exact test
1668
-
1669
- 3. **Architectural Integration**:
1670
- - Report breaking changes only if you can identify the SPECIFIC break
1671
- - Report API inconsistencies only with SPECIFIC mismatches identified
1672
- - Report separation of concerns issues only with SPECIFIC refactoring suggestions
1673
-
1674
- ### **STAGE 5: Consolidate and Prioritize Issues**
1675
-
1676
- 1. **Apply Conflict Resolution Rules**:
1677
- - **Guideline Precedence**: If pattern-based or historical insights contradict explicit guidelines, guidelines take precedence
1678
- - **Cross-File Priority**: Issues affecting multiple files get higher priority
1679
- - **Pattern Consistency**: Missing project-specific utilities/helpers are high priority if pattern appears in 3+ examples
1680
-
1681
- 2. **Citation Rules**:
1682
- - For guideline violations: cite the specific guideline document
1683
- - For pattern deviations: cite specific code examples that demonstrate the correct pattern
1684
- - For historical issues: report as standard findings without referencing historical source
1685
- - For cross-file issues: specify all affected files
1686
-
1687
- 3. **CRITICAL OUTPUT FILTER - Apply before reporting ANY issue**:
1688
- - **Only report issues where you have a DEFINITE problem AND a SPECIFIC code fix**
1689
- - **Do NOT report issues that require the developer to "verify", "ensure", or "check" something**
1690
- - **Do NOT report issues where you are uncertain** - if you find yourself writing "may", "might", "could", or "consider", do not report it
1691
- - **Do NOT suggest adding comments or documentation**
1692
-
1693
- 4. Assess for DEFINITE logic errors or bugs only - do not report speculative issues.
1694
- 5. DO NOT check if any file referenced in a import statement, is missing.
1695
- 6. **CRITICAL 'lineNumbers' RULE - MANDATORY COMPLIANCE**:
1696
- - For ANY issue that occurs multiple times in a file, list ONLY the first 3-5 occurrences maximum
1697
- - NEVER provide exhaustive lists of line numbers (e.g., [1,2,3,4,5,6,7,8,9,10...])
1698
- - If an issue affects many lines, use representative examples only
1699
- - Exhaustive line number lists are considered hallucination and must be avoided
1700
- - Example: Instead of listing 20+ line numbers, use [15, 23, 47, "...and 12 other occurrences"]
1701
-
1702
- ${getFinalReminderBlock()}
1703
-
1704
- ${getCitationRequirementBlock()}
1705
-
1706
- REQUIRED JSON OUTPUT FORMAT:
1707
-
1708
- **REMINDER: For lineNumbers array, use ONLY 3-5 representative line numbers for repeated issues. NEVER provide exhaustive lists.**
1709
-
1710
- ${getCodeSuggestionsFormatBlock()}
1711
-
1712
- You must respond with EXACTLY this JSON structure, with no additional text:
1713
-
1714
- {
1715
- "summary": "Brief, high-level summary of the entire PR review...",
1716
- "crossFileIssues": [
1717
- {
1718
- "type": "bug | improvement | convention | architecture",
1719
- "severity": "critical | high | medium",
1720
- "description": "Detailed description of an issue that spans multiple files...",
1721
- "suggestion": "Actionable suggestion to resolve the cross-file issue.",
1722
- "filesInvolved": ["path/to/file1.js", "path/to/file2.ts"]
1723
- }
1724
- ],
1725
- "fileSpecificIssues": {
1726
- "path/to/file1.js": [
1727
- {
1728
- "type": "bug | improvement | convention | performance | security",
1729
- "severity": "critical | high | medium",
1730
- "description": "Description of the issue specific to this file.",
1731
- "lineNumbers": [10, 15],
1732
- "suggestion": "Concrete suggestion for fixing the issue in this file.",
1733
- "codeSuggestion": {
1734
- "startLine": 10,
1735
- "endLine": 15,
1736
- "oldCode": " const result = data.map(item => item.value);",
1737
- "newCode": " const result = data?.map(item => item?.value) ?? [];"
1738
- }
1739
- }
1740
- ]
1741
- },
1742
- "recommendations": [
1743
- {
1744
- "type": "refactoring | testing | documentation",
1745
- "description": "A high-level recommendation for improving the codebase...",
1746
- "filesInvolved": ["path/to/relevant/file.js"]
1747
- }
1748
- ]
1749
- }
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
1750
1405
  `);
1406
+
1407
+ return { systemPrompt, userPrompt };
1751
1408
  }
1752
1409
 
1753
1410
  /**