feat: Add @codebot explain-diff command for plain-language PR explanations #26
Reference in New Issue
Block a user
Delete Branch "feature/code-diff-explainer"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Implements code diff explainer that translates technical changes into
plain language for non-technical stakeholders (PMs, designers, new team members).
Features:
Implementation:
Workflow Safety (prevents duplicate runs):
Testing:
Documentation:
Related: Milestone 2 high-priority feature - code diff explainer
📋 Pull Request Summary
This PR introduces a new
@codebot explain-diffcommand that provides plain-language explanations of code changes in pull requests, aimed at non-technical stakeholders. It includes the implementation of the command handler, prompt template, workflow integration, documentation updates, and comprehensive tests to ensure functionality and usability.Type: ✨ Feature
Changes
✅ Added:
@codebot explain-diffcommand for plain-language diff explanationsexplain_diff.mdfor generating structured explanations_handle_explain_diff_commandinpr_agent.py_format_diff_explanationfor outputai-comment-reply.ymland exclusion inai-chat.ymlREADME.mdandCLAUDE.mddescribing the new commandtest_ai_review.pycovering prompt, command handling, formatting, and config📝 Modified:
explain-diffcommand to config.yml interaction commandsexplain-diffcommands correctlyPRAgent.can_handleto recognizeexplain-diffcommandexplain-diffcommandFiles Affected
tools/ai-review/agents/pr_agent.py- Added handler and formatter for the new@codebot explain-diffcommand to generate plain-language explanations of PR diffstools/ai-review/prompts/explain_diff.md- New prompt template defining the structure and rules for generating plain-language diff explanationstools/ai-review/config.yml- Addedexplain-diffto the list of recognized interaction commands.gitea/workflows/ai-comment-reply.yml- Updated workflow to trigger on@codebot explain-diffcommand in PR comments.gitea/workflows/ai-chat.yml- Excluded@codebot explain-difffrom ai-chat workflow to prevent duplicate runsREADME.md- Added documentation and usage instructions for the new@codebot explain-diffcommandCLAUDE.md- Extensive documentation on the purpose, implementation, usage, and examples of the@codebot explain-diffcommandtests/test_ai_review.py- Added comprehensive tests for the new diff explanation feature, including prompt existence, formatting, command handling, and config validationImpact
🟡 Scope: Medium
This feature enhances the AI review system by enabling non-technical stakeholders to understand code changes easily, improving cross-team communication and onboarding. It introduces a new command with minimal risk to existing functionality, integrated cleanly into workflows and well-covered by tests.
AI Code Review - Inline Comments
[LOW] Testing
The new TestDiffExplanation class provides good coverage for the new explain-diff command, including prompt existence, formatting, command handling, and output formatting.
Recommendation: Consider adding tests for error scenarios such as empty diffs, malformed LLM responses, and large PRs to ensure robust error handling.
[LOW] Maintainability
The _handle_explain_diff_command method is quite large and handles multiple responsibilities including fetching PR data, loading prompts, calling the LLM, formatting output, and posting comments.
Recommendation: Consider refactoring _handle_explain_diff_command into smaller helper methods to improve readability and maintainability, e.g., separate methods for fetching PR data, generating prompt, calling LLM, and posting results.
[LOW] Readability
The _format_diff_explanation method uses multiple nested if checks and appends lines conditionally, which could be simplified for clarity.
Recommendation: Add inline comments explaining each section and consider extracting repeated patterns (e.g., listing items) into helper functions to improve readability.
[LOW] Correctness
The method _handle_explain_diff_command assumes the LLM returns valid JSON matching the expected schema but does not validate or handle malformed JSON or missing keys explicitly.
Recommendation: Add validation of the LLM JSON response to handle unexpected or malformed data gracefully, with fallback messages or error logging.
[LOW] Readability
The prompt template is comprehensive and well-written but could benefit from explicit instructions on maximum length or complexity to avoid overly verbose LLM responses.
Recommendation: Add guidance in the prompt to keep explanations concise and focused, e.g., limit overview to 2 sentences and key changes to brief bullet points.
AI Code Review
This PR adds a new '@codebot explain-diff' command to the AI review system, enabling plain-language explanations of PR diffs for non-technical stakeholders. The implementation includes workflow updates, prompt templates, command handling in the PR agent, markdown formatting of explanations, and comprehensive tests. The changes are well-integrated, maintainable, and include appropriate error handling and documentation.
Summary
Review Findings
tools/ai-review/agents/pr_agent.py:1211- The _handle_explain_diff_command method is quite large and handles multiple responsibilities including fetching PR data, loading prompts, calling the LLM, formatting output, and posting comments.tools/ai-review/agents/pr_agent.py:1247- The _format_diff_explanation method uses multiple nested if checks and appends lines conditionally, which could be simplified for clarity.tools/ai-review/agents/pr_agent.py:1211- The method _handle_explain_diff_command assumes the LLM returns valid JSON matching the expected schema but does not validate or handle malformed JSON or missing keys explicitly.tools/ai-review/prompts/explain_diff.md:1- The prompt template is comprehensive and well-written but could benefit from explicit instructions on maximum length or complexity to avoid overly verbose LLM responses.tests/test_ai_review.py:1052- The new TestDiffExplanation class provides good coverage for the new explain-diff command, including prompt existence, formatting, command handling, and output formatting.Overall Severity:
LOWAI Recommendation: Approve
@None
Explanation:
There is no content provided in the title or body of the issue. Without any information, it is not possible to determine what the user is asking for or reporting. Please provide the issue details to enable analysis.
@None
Explanation:
There is no content provided in the title or body to analyze. Please provide the issue details so I can assist you effectively.