tawada / grass-grower

0 stars 0 forks source link

Add comprehensive error handling and validation for generated commit messages in the `generate_commit_message` function. #86

Open tawada opened 4 months ago

tawada commented 4 months ago

After reviewing the code of the existing program, one notable issue is that the generate_commit_message function in logic/code_modification.py does not include proper error handling or validation for the commit message generated by the LLM. If the message is not correctly formatted, too lengthy, or invalid for some other reason, the commit process might fail silently without any informative errors.

Specific Recommendation: Add comprehensive checks and validations on the generated commit message. This should include length validation, content checks to ensure it's meaningful and non-empty, and additional logging to capture the message before attempting the commit.

Contextual Example of where additional error handling should be introduced:

def generate_commit_message(repo, issue, modification: CodeModification):
    """Generate a commit message from an issue and a modification."""
    messages = logic_utils.generate_messages_from_issue(issue)
    messages.append({
        "role":
        "assistant",
        "content":
        f"Before:\n{modification.before_code}\nAfter:\n{modification.after_code}",
    })
    messages.append({
        "role":
        "system",
        "content":
        "Output commit message from the issue and the modification.",
    })
    openai_client = services.llm.get_openai_client()
    commit_message: str = services.llm.generate_text(messages, openai_client)

    # Add additional validation and logging here
    if not commit_message or len(commit_message) == 0:
        log("Generated commit message is empty.", level="error")
        raise ValueError("Generated commit message cannot be empty.")

    if "\n" in commit_message:
        commit_message = commit_message.split("\n")[0].strip('"')
    elif ". " in commit_message:
        commit_message = commit_message.split(". ")[0].strip('"')
    elif len(commit_message) > 72:
        commit_message = commit_message[:72]

    log(f"Generated Commit Message: {commit_message}", level="info")

    commit_message += f" (#{issue.id})"
    return commit_message

By introducing these additional validations and error logs, this will help in identifying issues early during the commit message generation process.