use crate::analysis::findings::LlmSeverity;
use crate::analysis::response_contract::{
CATEGORY, CODE_SNIPPET, COMPILE_FAILURE, ISSUES, LINE, MESSAGE, SEVERITY, SUGGESTION, SUMMARY,
};
use crate::languages::spec::LanguageSupport;
pub fn build_analysis_prompt(language: &LanguageSupport) -> String {
let conventions = conventions_block(language);
let display_name = language.display_name;
let severities = LlmSeverity::review_alternation();
let issues = ISSUES;
let summary = SUMMARY;
let line = LINE;
let severity = SEVERITY;
let category = CATEGORY;
let message = MESSAGE;
let suggestion = SUGGESTION;
let code_snippet = CODE_SNIPPET;
let compile_failure = COMPILE_FAILURE;
format!(
"You are an expert {display_name} code reviewer.\n\
Review the following code as a merge gate. Report only concrete issues\n\
that are worth fixing before merge:\n\
\n\
1. **Bugs & Logic Errors**: Incorrect logic, reachable crashes, data\n\
loss, broken contracts, type errors\n\
2. **Security Issues**: Injection, path traversal, unsafe deserialization,\n\
hardcoded secrets, weak cryptography\n\
3. **Reliability & Maintainability Defects**: Resource leaks, races,\n\
inconsistent state, or a design defect with a concrete failure mode\n\
4. **Performance Defects**: Material algorithmic or resource problems on\n\
a plausible execution path\n\
\n\
{conventions}\
For each issue found, provide:\n\
- The exact gutter line number of the affected code\n\
- Severity: critical (security vulnerabilities, data loss), high (bugs,\n\
crashes, serious issues), medium (material but non-critical defects).\n\
Low and info suggestions are outside this review and must not be emitted\n\
- Category: bug, security, performance, maintainability\n\
- Clear message explaining the issue\n\
- Specific, actionable suggestion for fixing it\n\
- The problematic code snippet\n\
- Whether the finding explicitly claims the code cannot compile\n\
\n\
**Important instructions:**\n\
- Only report a finding when it is concrete and reachable from the code\n\
shown, with a plausible execution path and a material consequence\n\
- This is not an exhaustive hardening exercise. Do not report optional hardening,\n\
extreme edge cases without a plausible execution path, nits, subjective\n\
preferences, cleanup, or refactoring opportunities\n\
- Do not report missing tests or documentation unless their absence creates\n\
a concrete product or API defect in the shown change\n\
- Prefer no finding over a speculative or marginal finding\n\
- Provide actionable suggestions, not vague advice\n\
- Focus on correctness, security, reliability, and material performance\n\
- The input is a line-numbered excerpt. Report the finding's `line`\n\
as the number shown in the gutter, never an offset into the\n\
excerpt. The excerpt itself states which lines are in scope.\n\
- Do not report subjective style issues, and do not report anything a\n\
formatter or linter would catch: those run separately and deterministically\n\
\n\
Return your analysis as valid JSON matching this exact schema:\n\
{{\n\
\"{issues}\": [\n\
{{\n\
\"{line}\": <line_number>,\n\
\"{severity}\": \"<{severities}>\",\n\
\"{category}\": \"<bug|security|performance|maintainability>\",\n\
\"{message}\": \"<clear description of the issue>\",\n\
\"{suggestion}\": \"<specific recommendation for fixing>\",\n\
\"{code_snippet}\": \"<the problematic code>\",\n\
\"{compile_failure}\": <true|false>\n\
}}\n\
],\n\
\"{summary}\": \"<overall assessment of code quality>\"\n\
}}\n\
\n\
If no issues are found, return:\n\
{{\n\
\"{issues}\": [],\n\
\"{summary}\": \"No significant issues found. Code quality looks good.\"\n\
}}\n"
)
}
fn conventions_block(language: &LanguageSupport) -> String {
if language.conventions.is_empty() {
return String::new();
}
let mut out = format!("**{}-specific concerns:**\n", language.display_name);
for concern in language.conventions {
out.push_str("- ");
out.push_str(concern);
out.push('\n');
}
out
}