Skip to content

[code_review] Add explanation to the generated comments #5005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

suhaibmujahid
Copy link
Member

Resolves #4785

@suhaibmujahid suhaibmujahid requested review from marco-c and Copilot May 8, 2025 17:37
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds an explanation property to generated comments and updates related documentation and examples to include justification information in the JSON objects.

  • Added an explanation attribute to the InlineComment class.
  • Adjusted documentation and JSON examples to require an explanation key in each comment.
  • Updated the generate_processed_output and _get_comment_examples functions to propagate the explanation field.

@@ -51,6 +51,7 @@ class InlineComment:
hunk_start_line: int | None = None
hunk_end_line: int | None = None
is_generated: bool | None = None
explanation: str | None = None


Copy link
Preview

Copilot AI May 8, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding a docstring or inline comment to clarify the purpose of the 'explanation' attribute in InlineComment.

Suggested change

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

That will not be consistent with the rest of the attributes.

@@ -1401,6 +1407,9 @@ def _get_comment_examples(self, patch, created_before: datetime | None = None):

if not comment_examples:
comment_examples = STATIC_COMMENT_EXAMPLES
else:
for example in comment_examples:
example["comment"]["explanation"] = "THE JUSTIFICATION GOES HERE"
Copy link
Preview

Copilot AI May 8, 2025

Choose a reason for hiding this comment

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

In _get_comment_examples, the 'explanation' field is being nested under the 'comment' key; for consistency with the rest of the schema, consider adding 'explanation' as a top-level key in the JSON objects.

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Invalid suggestion. The explanation needs to be part of the comment dict.

@suhaibmujahid
Copy link
Member Author

The evaluation results:

--------------------
Variant Name: with-explanation
--------------------
New Comments: 331
New Valid Comments: 46
New Invalid Comments: 97
New Unevaluated Comments: 188
--------------------
Old Comments: 273
Old Valid Comments: 84
Old Invalid Comments: 189
--------------------
Recalled comments: 55.67765567765568
Recalled valid comments: 54.761904761904766
Recalled invalid comments: 56.08465608465608
--------------------
Missed valid comments: 45.23809523809524
Missed invalid comments: 43.91534391534391

This can be compared to #5002 (comment). The results look similar, with a minor improvement.

@pr-test-bot

This comment was marked as off-topic.

@@ -152,7 +153,7 @@ class LargeDiffError(Exception):
- include praising;
- ask if changes are intentional or ask to ensure things exist.

Do not report any explanation about your choice. Only return a valid JSON list.
Only return a valid JSON list. Do not drop any key from the JSON objects.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the second part needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

At the beginning, it was without the sound part. However, the filtering step was omitting the explanation key from he final result.

@@ -212,6 +214,7 @@ class LargeDiffError(Exception):
"filename": "netwerk/streamconv/converters/mozTXTToHTMLConv.cpp",
"start_line": 1211,
"content": "You are using `nsAutoStringN<256>` instead of `nsString`. This is a good change as `nsAutoStringN<256>` is more efficient for small strings. However, you should ensure that the size of `tempString` does not exceed 256 characters, as `nsAutoStringN<256>` has a fixed size.",
"explanation": "THE JUSTIFICATION GOES HERE",
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should maybe add one

Copy link
Member Author

Choose a reason for hiding this comment

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

The generated explanation seems reasonable, even without it.

@@ -1401,6 +1407,9 @@ def _get_comment_examples(self, patch, created_before: datetime | None = None):

if not comment_examples:
comment_examples = STATIC_COMMENT_EXAMPLES
else:
for example in comment_examples:
example["comment"]["explanation"] = "THE JUSTIFICATION GOES HERE"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here we should reuse the old generated explanations when there are some

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure that would add value, especially since it will not be consistent when some comments have justification value and others do not. But it will definitely consume more tokens.

I added the placeholder here to avoid situations where the explanation is omitted.

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.

[code_review] Add explanations to generated comments
3 participants