Skip to content

Conversation

@elisafalk
Copy link
Collaborator

@elisafalk elisafalk commented Nov 20, 2025

Overview: This PR introduces a new function to generate comprehensive code audit summaries using LLM prompts.

Changes

  • Added generate_code_audit_text(code_data, audit_data) to create audit summaries.
  • The function incorporates clarity points, risk highlights, code activity, and repository quality indicators.
  • Implemented graceful handling for scenarios where audit information might be missing.
  • This enhancement improves the code_audit_agent's ability to provide detailed insights.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added code audit summary generation capability that combines code metrics and audit data to produce detailed, comprehensive audit reports.
  • Tests

    • Added comprehensive test coverage for the new code audit generation functionality, including success, error, and edge case scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Nov 20, 2025

Walkthrough

This pull request adds a new asynchronous method to the NLGEngine class that generates code audit summaries by combining code and audit data, constructing a prompt from a new template, and invoking the LLM client with comprehensive error handling and response validation.

Changes

Cohort / File(s) Change Summary
NLG Engine Enhancement
backend/app/services/nlg/nlg_engine.py
Adds async method generate_code_audit_text() that accepts code_data and audit_data dictionaries, converts them to indented JSON strings, constructs a prompt using the "code_audit_summary" template, invokes the LLM client, and returns generated text with fallback error messages for empty responses or exceptions.
Prompt Template Addition
backend/app/services/nlg/prompt_templates.py
Adds new "code_audit_summary" template to the templates dictionary with structured placeholders for code_data and audit_data, designed to generate comprehensive audit summaries covering clarity, risks, activity, and quality metrics.
Test Coverage
backend/app/services/nlg/tests/test_nlg_engine.py
Adds multiple test cases for code audit text generation covering success, empty data, error, and empty content scenarios; modifies existing tests to include duplicated assertions in error cases and restructures sentiment tests.

Sequence Diagram

sequenceDiagram
    participant Client
    participant NLGEngine
    participant PromptTemplates
    participant LLMClient
    participant Logger

    Client->>NLGEngine: generate_code_audit_text(code_data, audit_data)
    
    alt Both data inputs present
        NLGEngine->>NLGEngine: Convert to JSON strings with indentation
        NLGEngine->>PromptTemplates: get_template("code_audit_summary")
        PromptTemplates-->>NLGEngine: Return template
        NLGEngine->>NLGEngine: Fill template with combined data
        NLGEngine->>LLMClient: invoke(prompt)
        
        alt LLM Success
            LLMClient-->>NLGEngine: Generated text
            NLGEngine-->>Client: Return audit summary
        else LLM Error or Empty
            LLMClient-->>NLGEngine: Exception or empty response
            NLGEngine->>Logger: Log error
            NLGEngine-->>Client: Return failure message
        end
    else Data absent
        NLGEngine->>Logger: Log warning
        NLGEngine-->>Client: Return "Data not yet available" message
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Areas requiring attention:

  • Error handling paths in generate_code_audit_text() and the specific fallback messages returned
  • JSON serialization logic and handling of edge cases with indentation formatting
  • Prompt template structure and placeholder field names to ensure correct data injection
  • Test assertions in modified test cases, particularly the duplicated checks in error test scenarios
  • LLM client invocation and response validation logic

Possibly related PRs

Suggested reviewers

  • felixjordandev

Poem

🐰 A rabbit hops through code audit dreams,
With JSON strings and LLM schemes,
Templates crafted, errors are caught,
New methods tested, just as they ought! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: implementing LLM-driven code audit summary generation, which aligns with the addition of the generate_code_audit_text method and related infrastructure across all modified files.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/add-code-audit-text-generation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
backend/app/services/nlg/nlg_engine.py (1)

130-163: Code audit summary generation is sound; consider deduping LLM call logic

The control flow looks correct: you short‑circuit when both inputs are empty, fill the template with "N/A" for missing parts, treat empty LLM content as an error, and return a consistent JSON wrapper in all paths. This matches the existing section generators.

You do, however, duplicate the LLM invocation / response‑parsing pattern from _generate_section_with_llm. If you want to keep Ruff happy (TRY301/TRY003 on the explicit ValueError) and reduce repetition, consider extracting a small helper like _generate_with_llm(prompt: str, section_id: str, error_msg: str) that encapsulates the generate_text call, choices extraction, empty‑content check, and exception handling, then use it both here and in _generate_section_with_llm. That would centralize error behavior and message text while keeping this method focused on combining code_data and audit_data.

backend/app/services/nlg/tests/test_nlg_engine.py (1)

176-179: Remove duplicated assertions in sentiment LLM‑error test

These three assertions repeat the ones immediately above in the same test function and add no extra coverage. Dropping the duplicated block will keep the test concise without changing behavior.

-        assert parsed_response["section_id"] == "social_sentiment"
-        assert "Failed to generate social sentiment summary due to an internal error." in parsed_response["text"]
-        assert respx_mock.calls.call_count == 1
-
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f006f60 and be31a5b.

⛔ Files ignored due to path filters (3)
  • backend/app/services/nlg/__pycache__/nlg_engine.cpython-313.pyc is excluded by !**/*.pyc
  • backend/app/services/nlg/__pycache__/prompt_templates.cpython-313.pyc is excluded by !**/*.pyc
  • backend/app/services/nlg/tests/__pycache__/test_nlg_engine.cpython-313-pytest-8.4.2.pyc is excluded by !**/*.pyc
📒 Files selected for processing (3)
  • backend/app/services/nlg/nlg_engine.py (1 hunks)
  • backend/app/services/nlg/prompt_templates.py (1 hunks)
  • backend/app/services/nlg/tests/test_nlg_engine.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
backend/app/services/nlg/nlg_engine.py (2)
backend/app/services/nlg/prompt_templates.py (2)
  • get_template (6-125)
  • fill_template (127-132)
backend/app/services/nlg/llm_client.py (2)
  • LLMClient (9-55)
  • generate_text (30-55)
backend/app/services/nlg/tests/test_nlg_engine.py (1)
backend/app/services/nlg/nlg_engine.py (1)
  • generate_code_audit_text (130-163)
🪛 Ruff (0.14.5)
backend/app/services/nlg/nlg_engine.py

156-156: Abstract raise to an inner function

(TRY301)


156-156: Avoid specifying long messages outside the exception class

(TRY003)

🔇 Additional comments (2)
backend/app/services/nlg/prompt_templates.py (1)

70-90: Template wiring and content look correct

The "code_audit_summary" template is consistent with the existing templates, uses {code_data} / {audit_data} placeholders that match generate_code_audit_text, and clearly guides the model toward the desired structure (clarity, risks, activity, quality, missing info). No issues from a formatting or API‑usage standpoint.

backend/app/services/nlg/tests/test_nlg_engine.py (1)

181-244: New code‑audit tests give good coverage of success and failure paths

The new tests for generate_code_audit_text exercise the main behaviors: successful generation, both inputs empty, upstream error response, and empty content from the model. They assert on section_id, key text fragments, and call counts, mirroring the existing patterns for other sections. This should be sufficient to guard regressions in the new feature.

@felixjordandev felixjordandev merged commit de00601 into main Nov 20, 2025
1 check passed
@felixjordandev felixjordandev deleted the feat/add-code-audit-text-generation branch November 20, 2025 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants