ISSUE DESCRIPTION [BUG][auto] duplicate-overlapping-clone-groups: same duplication…
- User
ISSUE DESCRIPTION
[BUG][auto] duplicate-overlapping-clone-groups: same duplication emitted as two separate near-identical clone groups
Bug
pyscn's clone detector emits two separate clone groups that describe the same duplication relationship between the same two code fragments, with member windows shifted by exactly one line (the enclosing
with/header line). The result is a redundant clone group that inflatesclone_groupscount and the duplication percentage.This is related to but distinct from the (closed) #416
clone-overlapping-window-self-clone: #416 was about strict-subset members within a single group; here the redundancy is across two whole groups that are near-duplicates of each other.Reproducer
Repo:
Instagram/Fixit@bf8e1f55f8a606eae0f0c64bab575e84a2a608b3pyscn:v1.22.9bashcurl -fsSL "https://codeload.github.com/Instagram/Fixit/tar.gz/HEAD" | tar -xz pyscn analyze --json --no-open Fixit-*/In the resulting
analyze.json,clone.clone_groupscontains two groups that cover the same pair of fragments insrc/fixit/tests/smoke.py:json{ "id": 6, "type": 2, "similarity": 0.9692, "clones": [ {"location": {"file_path": ".../smoke.py", "start_line": 299, "end_line": 315}}, {"location": {"file_path": ".../smoke.py", "start_line": 318, "end_line": 334}} ] }, { "id": 14, "type": 2, "similarity": 0.9613, "clones": [ {"location": {"file_path": ".../smoke.py", "start_line": 298, "end_line": 315}}, {"location": {"file_path": ".../smoke.py", "start_line": 317, "end_line": 334}} ] }Both groups describe the duplication between the
subTest("lint")block and thesubTest("fix")block. Group 14's members each start exactly one line earlier than group 6's — that line is thewith self.subTest("lint"):/with self.subTest("fix"):header: Assistant
Let me analyze this issue. The bug is about pyscn's clone detector emitting two separate clone groups that describe the same duplication relationship between the same two code fragments, with member windows shifted by exactly one line. This results in redundant clone groups that inflate the clone_groups count and duplication percentage.
Let me start by exploring the repository structure to understand the codebase.
Assistant
Good, I can see the repository structure. Let me explore more to understand the clone detection logic.
Assistant
This appears to be a Go-based project with some Python bindings. Let me look at the domain and internal directories for clone-related code.
Assistant
Let me look at the clone domain file and the internal analyzer files for grouping logic:
Assistant
Now let me look at the internal analyzer directory for the grouping logic:
Assistant
Let me look at the key files related to clone grouping and deduplication:
Assistant
This is important. The
dedupeStrictSubsetGroupMembersfunction only deduplicates within a single group, but doesn't handle the case where two entire groups are near-duplicates of each other. Let me look at the clone detector to understand how groups are formed:Assistant
Now let me see more of the clone_detector.go file, especially the grouping logic:
Assistant
Let me look at the grouping strategy files and find where groups are created:
Assistant
Now let me look at the group dedup test file to understand the expected behavior better:
Assistant
Now I understand the issue better. The existing
dedupeStrictSubsetGroupMembersfunction deduplicates members within a single group, but the bug is about two separate groups that are near-duplicates of each other. Let me understand this more:- Group 6:
{smoke.py:299-315, smoke.py:318-334} - Group 14:
{smoke.py:298-315, smoke.py:317-334}
Each group has 2 members. The members of group 14 each start exactly one line earlier than group 6's corresponding members. Both describe the same duplication between
subTest("lint")andsubTest("fix")blocks.The issue is that the grouping algorithm produces two separate groups because there are two different pairs at different window boundaries. The dedup logic only removes strict-subset members within a group, but doesn't merge or suppress overlapping groups.
Let me look for where this dedup happens and see if there's cross-group deduplication:
- Group 6:
Assistant
I see. The dedup logic only works within a single group but doesn't handle overlapping groups. Let me look at the complete picture - how groups can end up being near-duplicates of each other:
Assistant
Let me now reproduce the issue by downloading the Fixit repository and running the analysis:
Assistant
I need to set up the Go environment properly. Let me set all the required environment variables.
Assistant
I can see the issue - there are 9 clone groups and 10% duplication. Let me check the JSON output to see the duplicate groups:
Assistant
I can clearly see the bug. Groups 6 and 7 are near-duplicates:
- Group 6:
smoke.py:299-315, smoke.py:318-334 - Group 7:
smoke.py:298-315, smoke.py:317-334
Each member in group 7 starts exactly one line earlier than the corresponding member in group 6. They describe the same duplication. Now I need to understand why this happens and fix it.
Let me look at how fragments are extracted and why we get overlapping windows:
- Group 6:
Assistant
I can see the code pattern - two
with self.subTest(...)blocks that are very similar. The clone detector identifies both:- The inner
with TemporaryDirectory()blocks (lines 299-315 and 318-334) - The outer
with self.subTest()blocks (lines 298-315 and 317-334)
Both are reported as separate groups. The fix should deduplicate groups where every member in one group overlaps with (or is a strict subset of) a member in another group.
Let me look more at the code to understand how groups are formed and where to add the cross-group deduplication:
- The inner
Assistant
Now I understand the flow:
- Clone pairs are detected
strategy.GroupClones()creates groups from pairs using union-finddedupeStrictSubsetGroupMembers()removes strict-subset members within each group
The problem is that the same pair of code fragments can be detected as clone pairs at different window boundaries (e.g., one line offset). These become separate groups because the fragments are distinct objects.
The fix should add cross-group deduplication after
dedupeStrictSubsetGroupMembers. This should identify groups where every member in one group is a strict subset of (or overlaps significantly with) a member in another group, and then keep only the "larger" group.Let me think about the algorithm:
- After intra-group dedup, compare all pairs of groups
- Two groups are "redundant" if for every member in one group, there's a member in the other group whose location covers it (same file, superset or equal range)
- When two groups are redundant, keep the one with larger members (more inclusive) or higher similarity
Let me implement this:
Assistant
Now I understand the full picture. The
dedupeStrictSubsetGroupMembersfunction only deduplicates members within each group. The fix needs to add cross-group deduplication that removes groups whose members are all covered by (overlapping/contained in) the members of another group.Let me look at what the issue describes more carefully:
- Group 6:
{smoke.py:299-315, smoke.py:318-334}(type 2, similarity 0.9692) - Group 7:
{smoke.py:298-315, smoke.py:317-334}(type 2, similarity 0.9613)
Each member of group 6 is a strict subset of the corresponding member in group 7. Group 7's members each start one line earlier but end at the same line.
The fix should:
- After intra-group dedup, compare groups pairwise
- If all members of group A are covered by members of group B (and vice versa in the same-file sense), suppress the smaller group
- "Covered" means same file, and the range is a subset or equal
Let me implement the cross-group deduplication:
- Group 6:
Assistant
Wait, I made a mistake in the
clonePairMetadatafunction - I changed the original logic. Let me fix that and also reconsider my approach. ThelocationsOverlapfunction needs to be more precise - I want to check if fragments from different groups are describing the same duplication, not just any overlap.Let me reconsider the algorithm:
For groups to be "duplicate" reports of the same duplication:
- They must have the same number of members
- There must be a bijection between members such that each pair of corresponding members (one from each group) overlaps in the same file
But more importantly, looking at the issue more carefully:
- Group 6:
{smoke.py:299-315, smoke.py:318-334} - Group 7:
{smoke.py:298-315, smoke.py:317-334}
The key insight is that group 7's members cover group 6's members (same file, and group 7's range is a superset). So the cross-group dedup should check if one group's members are all covered by the other group's members.
Let me also revert the
clonePairMetadatachange:Assistant
Now let me build the project and test it:
Assistant
The clone groups count went from 9 to 8. Let me verify the JSON output to confirm the duplicate group was removed:
Assistant
The duplicate group has been removed. Now we have 8 groups instead of 9. The group
smoke.py:298-315, smoke.py:317-334(the larger one with thewith self.subTest(...)header) is kept, and the redundantsmoke.py:299-315, smoke.py:318-334group is suppressed.Now let me run the existing tests to make sure I didn't break anything:
Assistant
All existing dedup tests pass. Let me run the full test suite for the analyzer package:
Assistant
All tests pass. Let me also run the tests in the service package to make sure the integration tests pass:
Assistant
All tests pass. Let me also run the integration tests for clones:
Assistant
Let me now also add a test case for the cross-group deduplication to make sure my fix is properly tested:
Assistant
Now let me add test cases specifically for the cross-group deduplication (the new functionality). I'll add them to the existing test file:
Assistant
I see. The test
TestDedupe_CrossGroupEqualLinesKeepsFirstis failing because two groups with exactly the same ranges aren't being deduped. This is actually expected behavior - if two groups have exactly the same ranges but are different fragment objects (created independently), they may be separate duplication relationships. Let me think about this...Actually, looking at the test more carefully, it's testing a case where two groups have the same file and line ranges but are different fragment objects. In the real scenario from the bug report, the groups have overlapping but different line ranges (one starts one line earlier). So identical ranges should also be deduped.
The issue is that my
locationsOverlapfunction correctly identifies them as overlapping, but thegroupsOverlapfunction requires that every member overlaps. Since they have identical ranges, they should be considered overlapping. Let me debug this:Assistant
The overlap check works. Let me check the
groupsOverlapfunction more carefully. The issue might be that identical groups are not being suppressed. Let me check what's happening:Assistant
The logic seems correct. So the test should be passing. Let me look at what happens with my test - maybe there's an issue with how the groups are created in the test:
Assistant
I see - the
gffunction must be defined somewhere. Let me look for it in the test file or other test files.Assistant
I see the issue now. The test
TestDedupe_CrossGroupEqualLinesKeepsFirstcreates two groups with identical line ranges but different fragment pointers. Since they have the same total lines, the code should suppress the second group. But the test is failing, meaning both groups are kept.Let me trace through the code logic more carefully. The issue is that when
linesI == linesJ, neither group is marked for suppression. In my code, I wrote:goif linesI < linesJ { marked[i] = true break } else if linesJ < linesI { marked[j] = true } // If equal, keep the first one (i) — deterministicWait, when they're equal, I don't mark
jas suppressed! Let me fix this:Assistant
I see the bug. When lines are equal, I need to mark
j(the later group) as suppressed. Let me fix this:Assistant
Now let me run the tests again:
Assistant
All dedup tests pass. Now let me run all analyzer tests to make sure I didn't break anything:
Assistant
Let me rebuild and re-test with the Fixit repository to confirm the fix still works:
Assistant
The fix works - we now have 8 clone groups instead of 9 (the duplicate group has been removed). Let me verify the JSON output one more time:
Assistant
The fix is working correctly. The duplicate group has been removed:
- Before: 9 clone groups (including both
smoke.py:299-315, smoke.py:318-334andsmoke.py:298-315, smoke.py:317-334) - After: 8 clone groups (only keeping the larger group
smoke.py:298-315, smoke.py:317-334)
Now let me run the full test suite to make sure nothing is broken:
- Before: 9 clone groups (including both
Assistant
All tests pass. Let me also think about edge cases and make sure my fix handles them:
- Empty groups / nil groups: Handled by existing checks (
if g == nil,len(g.Fragments) < 2) - Groups with different number of members: Handled by
if len(gi.Fragments) != len(gj.Fragments) - Groups in completely different files: Handled by
locationsOverlapwhich checksFilePath - Partial overlap (only some members overlap): Handled by
groupsOverlaprequiring all members to be covered - Equal total lines: Handled by suppressing the later group (
marked[j] = true) - Multiple overlapping groups: The outer loop iterates through all groups and marks duplicates
Let me also check one more edge case - what happens when there are three overlapping groups?
- Empty groups / nil groups: Handled by existing checks (
Assistant
The three-overlapping-groups case works correctly. Let me run all the tests one more time to make sure everything is working:
Assistant
Let me continue running the full test suite to ensure everything passes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.