fix(cli): add scope context and better git commands to review prompt

This commit is contained in:
Alex Alecu
2026-04-07 14:19:33 +03:00
parent d10d25a4d7
commit 41964a6db4
@@ -13,6 +13,11 @@ You are reviewing: \${SCOPE_DESCRIPTION}
\${FILE_LIST}
## Scope
\${SCOPE}
**IMPORTANT**: ONLY review code changes from the files listed above. Do NOT review or flag issues in code that is not part of this diff. If you use git commands to gather context, use them only to understand the surrounding code — not to expand the scope of your review.
## How to Review
1. **Gather context**: Read full file context when needed; diffs alone can be misleading, as code that looks wrong in isolation may be correct given surrounding logic.
@@ -150,13 +155,13 @@ function buildToolsSection(scope: "uncommitted" | "branch", baseBranch?: string,
return `Use these git commands to explore the changes:
- View all changes: \`git diff && git diff --cached\`
- View specific file change: \`git diff -- <file> && git diff --cached -- <file>\`
- View commit history: \`git log\`
- View recent commit history: \`git log --oneline -20\`
- View file history: \`git blame <file>\``
}
return `Use these git commands to explore the changes:
- View branch diff: \`git diff ${baseBranch}...${currentBranch}\`
- View specific file diff: \`git diff ${baseBranch}...${currentBranch} -- <file>\`
- View commit history: \`git log\`
- View branch commit history: \`git log ${baseBranch}..${currentBranch} --oneline\`
- View file history: \`git blame <file>\``
}
@@ -262,8 +267,11 @@ export namespace Review {
log.info("building uncommitted review prompt", { fileCount: diff.files.length })
const scopeDescription = "**uncommitted changes**"
const fileList = formatFileList(diff.files)
const scope =
"Reviewing uncommitted changes (staged + unstaged) in the working tree. Only review the changes shown in the diff — do not review committed code."
return REVIEW_PROMPT.replaceAll("${SCOPE_DESCRIPTION}", scopeDescription)
.replace("${FILE_LIST}", fileList)
.replace("${SCOPE}", scope)
.replace("${TOOLS}", buildToolsSection("uncommitted"))
}
@@ -286,8 +294,13 @@ export namespace Review {
log.info("building branch review prompt", { fileCount: diff.files.length, baseBranch: base })
const scopeDescription = `**branch diff**: \`${currentBranch}\` -> \`${base}\``
const fileList = formatFileList(diff.files)
const commits = await getBranchCommits(base, currentBranch)
const scope = commits
? `These are the commits on \`${currentBranch}\` since diverging from \`${base}\`:\n\n${commits}\n\nOnly review changes introduced by these commits.`
: `Reviewing all changes on \`${currentBranch}\` since diverging from \`${base}\`.`
return REVIEW_PROMPT.replaceAll("${SCOPE_DESCRIPTION}", scopeDescription)
.replace("${FILE_LIST}", fileList)
.replace("${SCOPE}", scope)
.replace("${TOOLS}", buildToolsSection("branch", base, currentBranch))
}
@@ -408,4 +421,24 @@ export namespace Review {
return parsed
}
/**
* Get the list of commits on the current branch since diverging from base.
* Uses two-dot range (base..current) to only include branch-specific commits.
*
* @returns Commit list as a string, or empty string if none found
*/
async function getBranchCommits(base: string, current: string): Promise<string> {
const result = await $`git log ${base}..${current} --oneline`.cwd(Instance.directory).quiet().nothrow()
if (result.exitCode !== 0) {
log.warn("git log for branch commits failed", {
exitCode: result.exitCode,
stderr: result.stderr.toString(),
})
return ""
}
return result.stdout.toString().trim()
}
}