@contextrail/code-review-agent 0.1.2-alpha.1 → 0.1.2-alpha.3

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.
@@ -1,133 +1,8 @@
1
- import { isDiffInputs } from '../review-inputs/index.js';
2
- /**
3
- * Severity calibration block with definitions and examples for each severity level.
4
- * This block is injected into reviewer system prompts to ensure consistent severity assignment.
5
- */
6
- export const SEVERITY_CALIBRATION_BLOCK = `
7
- ## Severity Calibration
8
-
9
- You must assign severity levels accurately based on the following definitions and examples:
10
-
11
- ### Critical
12
- **Definition**: Blocks deployment, active exploit path, or data corruption risk. Findings that could lead to security breaches, data loss, or system compromise.
13
-
14
- **Examples**:
15
- - SQL injection vulnerability: \`const query = \`SELECT * FROM users WHERE id = \${userInput}\`;\` (user input directly interpolated)
16
- - Authentication bypass: \`if (user.role === 'admin' || user.id === 1) { grantAccess(); }\` (hardcoded admin check)
17
- - Missing input validation on sensitive operations: \`await db.delete(userId);\` (no validation that userId belongs to requester)
18
-
19
- **When to use**: Only when there is a clear exploit path or risk of data corruption/integrity violation.
20
-
21
- ### Major
22
- **Definition**: Significant bug, breaks functionality, or violates established patterns. Findings that cause incorrect behavior or violate architectural standards.
23
-
24
- **Examples**:
25
- - Missing error handling: \`const data = await fetch(url); return data.json();\` (no try-catch, will crash on network error)
26
- - Tight coupling: \`import { DatabaseConnection } from './db'; class UserService { private db = new DatabaseConnection(); }\` (direct instantiation, violates dependency injection)
27
- - Race condition: \`let count = 0; async function increment() { count++; await save(count); }\` (non-atomic increment)
28
-
29
- **When to use**: When functionality is broken or architectural patterns are violated, but no immediate security/data risk.
30
-
31
- ### Minor
32
- **Definition**: Code quality issue, convention violation, or maintainability concern. Findings that don't break functionality but reduce code quality.
33
-
34
- **Examples**:
35
- - Magic numbers: \`if (user.age > 18) { ... }\` (should be \`const MIN_ADULT_AGE = 18\`)
36
- - Inconsistent naming: \`function getUserData() { ... }\` but \`function fetch_user_info() { ... }\` (mixed naming conventions)
37
- - Missing JSDoc: \`function calculateTotal(items) { ... }\` (no documentation for complex logic)
38
-
39
- **When to use**: Code quality, readability, or maintainability issues that don't affect functionality.
40
-
41
- ### Info
42
- **Definition**: Observation, suggestion, or educational note. Findings that provide helpful context or suggestions without indicating a problem.
43
-
44
- **Examples**:
45
- - Performance suggestion: \`// Consider caching this query result if called frequently\`
46
- - Pattern suggestion: \`// This could use the Repository pattern for better testability\`
47
- - Documentation opportunity: \`// This algorithm implements the Fisher-Yates shuffle\`
48
-
49
- **When to use**: Helpful suggestions or observations that don't indicate actual problems.
50
- `;
51
- /**
52
- * Severity validation rules for critic pass (iteration 2+).
53
- * These rules help the critic validate and potentially downgrade severity.
54
- */
55
- export const SEVERITY_VALIDATION_RULES = `
56
- ## Severity Validation Rules
57
-
58
- During the critic pass, validate each finding's severity against these rules:
59
-
60
- 1. **Critical findings MUST have exploit path or data integrity evidence**
61
- - If a critical finding lacks a clear exploit path or data corruption risk described in the rationale, downgrade to major
62
- - Example: "SQL injection" without showing how user input reaches the query → downgrade to major
63
-
64
- 2. **Major findings MUST demonstrate functional impact**
65
- - If a major finding doesn't show how functionality is broken or patterns violated, downgrade to minor
66
- - Example: "Missing error handling" without showing what breaks → downgrade to minor
67
-
68
- 3. **Minor findings MUST indicate code quality impact**
69
- - If a minor finding is just a style preference without maintainability impact, consider downgrade to info
70
- - Example: "Use const instead of let" without explaining why → consider info
71
-
72
- 4. **Downgrade rules**:
73
- - Critical → Major: No exploit path or data integrity risk described
74
- - Major → Minor: No functional impact demonstrated
75
- - Minor → Info: No code quality/maintainability impact shown
76
-
77
- 5. **Never upgrade severity** - Only downgrade if evidence doesn't support the assigned level
78
- `;
79
- /**
80
- * ContextRail tooling workflow requirements.
81
- * Forces reviewers to ground findings in retrieved contexts instead of guessing IDs/titles.
82
- */
83
- export const CONTEXTRAIL_TOOLING_BLOCK = `
84
- ## ContextRail Standards Workflow (Required)
85
-
86
- When you identify potential issues, you MUST ground them in retrieved ContextRail standards:
87
-
88
- 1. Use \`search_contexts\` first to discover relevant standards for this change.
89
- 2. Use \`get_context\` for each context you plan to cite in findings.
90
- 3. Use \`resolve_dependencies\` when cited contexts have required dependencies.
91
- 4. Perform at least one \`search_contexts\` call before finalizing your findings.
92
-
93
- Attribution rules:
94
- - NEVER invent context IDs or titles.
95
- - Only include \`contextIdsUsed\`, \`contextIdsViolated\`, and \`contextTitles\` from contexts you actually retrieved.
96
- - If no relevant ContextRail standard exists after tool lookup, set those fields to \`null\` (not empty arrays) and explain that briefly in \`rationale\`.
97
- `;
98
- /**
99
- * Compact output contract optimized for structured-output reliability.
100
- * Keeps formatting constraints in one place for both standard and critic prompts.
101
- */
102
- export const OUTPUT_CONTRACT_BLOCK = `
103
- ## Output Contract (Strict)
104
-
105
- Return a JSON object with:
106
- - \`findings\`: array
107
- - \`validated\`: boolean
108
- - \`notes\`: string | null
109
-
110
- For each finding:
111
- - Required keys: \`severity\`, \`title\`, \`description\`, \`rationale\`
112
- - Optional-but-required-by-schema keys: \`suggestedFix\`, \`file\`, \`line\`, \`endLine\`, \`contextIdsUsed\`, \`contextIdsViolated\`, \`contextTitles\`
113
-
114
- Schema compatibility rules:
115
- - Include all keys (never omit)
116
- - Use \`null\` when a value is not available
117
- - Use \`null\` (not empty arrays) for context attribution fields when no standards apply
118
- `;
119
- /**
120
- * Final guardrail checklist near generation point.
121
- * Repeats only non-negotiables to reduce mid-prompt loss on long inputs.
122
- */
123
- export const FINAL_CHECKLIST_BLOCK = `
124
- ## Final Checklist (Must Pass)
125
- 1. Every finding is supported by concrete code/diff evidence.
126
- 2. ContextRail workflow was used (\`search_contexts\` -> \`get_context\` -> \`resolve_dependencies\` as needed).
127
- 3. Context attribution fields only reference retrieved contexts (or are \`null\`).
128
- 4. Severity is calibrated to evidence.
129
- 5. Output strictly matches the JSON contract, and \`notes\` summarizes retrieved contexts (or none found).
130
- `;
1
+ import { SEVERITY_CALIBRATION_BLOCK } from '../prompts/blocks.js';
2
+ // Re-export blocks for backward compatibility
3
+ export { SEVERITY_CALIBRATION_BLOCK, SEVERITY_VALIDATION_RULES, CONTEXTRAIL_TOOLING_BLOCK, OUTPUT_CONTRACT_BLOCK, FINAL_CHECKLIST_BLOCK, } from '../prompts/blocks.js';
4
+ // Re-export prompt builders for backward compatibility
5
+ export { buildCriticPrompt, buildStandardReviewPrompt, buildReviewerUserMessage } from '../prompts/reviewer.js';
131
6
  export const buildPromptMessages = (promptResult) => {
132
7
  return promptResult.messages.map((message) => {
133
8
  const role = message.role === 'user' ? 'user' : message.role === 'assistant' ? 'assistant' : 'system';
@@ -139,108 +14,3 @@ export const buildPromptMessages = (promptResult) => {
139
14
  return { role, content };
140
15
  });
141
16
  };
142
- /**
143
- * Build a critic prompt that challenges findings from a previous iteration.
144
- * The critic pass validates findings, removes false positives, and enforces evidence/standards.
145
- */
146
- const buildCriticPrompt = (inputs, understanding, findings, prDescription, reviewDomains) => {
147
- const diffBlock = isDiffInputs(inputs)
148
- ? `\nDiffs:\n${Object.entries(inputs.diffs)
149
- .map(([file, diff]) => `\n## ${file}\n\`\`\`diff\n${diff}\n\`\`\``)
150
- .join('\n')}`
151
- : '';
152
- const contextBlock = inputs.context && Object.keys(inputs.context).length > 0
153
- ? `\n\nSurrounding Code Context:\n${Object.entries(inputs.context)
154
- .map(([file, context]) => `\n## ${file}\n\`\`\`\n${context}\n\`\`\``)
155
- .join('\n')}`
156
- : '';
157
- const prDescriptionBlock = prDescription ? `\n\nPR Description:\n${prDescription}\n` : '';
158
- const reviewDomainsBlock = reviewDomains && reviewDomains.length > 0
159
- ? `\n\nReview Focus Domains:\n${reviewDomains.map((domain) => `- ${domain}`).join('\n')}\n`
160
- : '';
161
- return `You are performing a CRITIC PASS to validate and challenge findings from the previous iteration.
162
-
163
- Your role is to:
164
- 1. Challenge each finding - require concrete evidence from the code or ContextRail standards
165
- 2. Remove false positives - findings that lack evidence or don't violate actual standards
166
- 3. Enforce ContextRail standards - each finding MUST be grounded in retrieved contexts
167
- 4. Validate severity - ensure severity matches the actual risk level using the severity validation rules below
168
- 5. Only keep findings that are:
169
- - Supported by evidence in the code/diffs
170
- - Linked to specific ContextRail standards (contextIdsUsed or contextIdsViolated)
171
- - Accurately categorized by severity
172
-
173
- ${SEVERITY_VALIDATION_RULES}
174
- ${CONTEXTRAIL_TOOLING_BLOCK}
175
-
176
- Context:
177
- ${understanding}
178
- ${prDescriptionBlock}${reviewDomainsBlock}
179
- Files:
180
- ${inputs.files.map((f) => `- ${f}`).join('\n')}
181
- ${diffBlock}${contextBlock}
182
-
183
- Previous findings (iteration 1) - CRITICALLY REVIEW THESE:
184
- ${JSON.stringify(findings.findings, null, 2)}
185
-
186
- For each finding, ask yourself:
187
- - Is there concrete evidence in the code/diffs that supports this finding?
188
- - Does this finding reference specific ContextRail standards (contextIdsUsed or contextIdsViolated)?
189
- - Is the severity appropriate for the actual risk? (Apply severity validation rules above)
190
- - Could this be a false positive that should be removed?
191
- - Should the severity be downgraded based on the validation rules?
192
-
193
- Output requirements:
194
- - Remove any findings that lack evidence
195
- - Remove findings that claim ContextRail impact but do not include retrieved context attribution
196
- - Keep only findings that are well-supported and properly attributed
197
- - Validate severity using the rules above - downgrade if evidence doesn't support the assigned level
198
- - Set validated: true only if all remaining findings meet these criteria
199
- - Always include notes field: use a string when you have notes, or null when none
200
- - If you remove findings or downgrade severity, explain why in notes
201
- - In notes, summarize which ContextRail contexts were retrieved (or state that none were relevant after tool lookup)
202
-
203
- ${OUTPUT_CONTRACT_BLOCK}
204
- ${FINAL_CHECKLIST_BLOCK}`;
205
- };
206
- /**
207
- * Build a standard reviewer prompt for initial review (iteration 1).
208
- */
209
- const buildStandardReviewPrompt = (inputs, understanding, prDescription, reviewDomains) => {
210
- const diffBlock = isDiffInputs(inputs)
211
- ? `\nDiffs:\n${Object.entries(inputs.diffs)
212
- .map(([file, diff]) => `\n## ${file}\n\`\`\`diff\n${diff}\n\`\`\``)
213
- .join('\n')}`
214
- : '';
215
- const contextBlock = inputs.context && Object.keys(inputs.context).length > 0
216
- ? `\n\nSurrounding Code Context:\n${Object.entries(inputs.context)
217
- .map(([file, context]) => `\n## ${file}\n\`\`\`\n${context}\n\`\`\``)
218
- .join('\n')}`
219
- : '';
220
- const prDescriptionBlock = prDescription ? `\n\nPR Description:\n${prDescription}\n` : '';
221
- const reviewDomainsBlock = reviewDomains && reviewDomains.length > 0
222
- ? `\n\nReview Focus Domains:\n${reviewDomains.map((domain) => `- ${domain}`).join('\n')}\n`
223
- : '';
224
- return `Review these changes using the reviewer prompt guidance.
225
-
226
- Context:
227
- ${understanding}
228
- ${prDescriptionBlock}${reviewDomainsBlock}
229
- Files:
230
- ${inputs.files.map((f) => `- ${f}`).join('\n')}
231
- ${diffBlock}${contextBlock}
232
-
233
- Please review these changes and provide findings.
234
-
235
- ${CONTEXTRAIL_TOOLING_BLOCK}
236
- ${OUTPUT_CONTRACT_BLOCK}
237
- ${FINAL_CHECKLIST_BLOCK}`;
238
- };
239
- export const buildReviewerUserMessage = (inputs, understanding, iteration, findings, prDescription, reviewDomains) => {
240
- // Use critic pass for iteration 2+
241
- if (iteration > 1 && findings) {
242
- return buildCriticPrompt(inputs, understanding, findings, prDescription, reviewDomains);
243
- }
244
- // Use standard review for iteration 1
245
- return buildStandardReviewPrompt(inputs, understanding, prDescription, reviewDomains);
246
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contextrail/code-review-agent",
3
- "version": "0.1.2-alpha.1",
3
+ "version": "0.1.2-alpha.3",
4
4
  "description": "CLI tool for orchestrating ContextRail-powered code reviews",
5
5
  "homepage": "https://contextrail.app",
6
6
  "repository": {