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 understand the issue first. 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
This is a Go project with some Python components. Let me explore the structure more, especially the internal and service directories that likely contain the clone detection logic.
Assistant
Let me look at the internal directory structure and find clone-related files:
Assistant
I can see there's a
group_dedup.gofile that seems relevant. Let me also look at the clone detector and grouping strategy files:Assistant
Now let me look at the test file for group dedup to understand what cases are covered:
Assistant
Now I understand the existing dedup logic. It handles members within a single group but doesn't handle the case described in the bug report: two separate groups that are near-duplicates of each other. Let me look at the clone detector to understand how groups are produced and processed:
Assistant
Let me see more of the clone detector file, particularly around where groups are created:
Assistant
Now I understand the flow. The
dedupeStrictSubsetGroupMembersfunction handles dedup within a single group, but there's no dedup across multiple groups. Let me look at the grouping strategy to understand how groups are formed:Assistant
Now I understand the issue better. The problem is that when grouping happens, two separate clone groups can be formed for the same duplication relationship if fragments have slightly different boundaries. The existing
dedupeStrictSubsetGroupMembersonly handles dedup within a single group, not across groups.Let me look at the issue more closely. The bug describes two groups:
- Group 6:
[(smoke.py:299-315), (smoke.py:318-334)] - Group 14:
[(smoke.py:298-315), (smoke.py:317-334)]
Each member of group 14 starts one line earlier than the corresponding member in group 6, but they end at the same lines. This means the fragments in group 6 are strict subsets of the fragments in group 14.
The fix should add a cross-group deduplication step that removes one of two groups when one group's members are all strict subsets of (or equal to) the corresponding members of another group.
Let me first reproduce the issue to confirm:
- Group 6:
Assistant
I can confirm the bug. Looking at groups with id 6 and id 16:
- Group 6:
[(smoke.py:299-315), (smoke.py:318-334)] - Group 16:
[(smoke.py:298-315), (smoke.py:317-334)]
Each member in group 16 starts one line earlier and ends at the same line as the corresponding member in group 6. Group 6's members are strict subsets of group 16's members. This is the exact bug described in the issue.
Now let me design a fix. The solution should add a cross-group deduplication step that removes a group when all its members are strict subsets of (or equal to) members of another group.
Let me look at the existing dedup code more carefully and add a new function for cross-group dedup:
- Group 6:
Assistant
Now let me understand the full picture. The issue is that two groups are near-duplicates of each other because the grouping strategy found pairs at slightly different window boundaries. I need to add cross-group dedup that:
- Detects when two groups have the same "logical duplication relationship" — i.e., their members are overlapping windows of the same underlying fragments
- Keeps the group with larger/more inclusive windows (the superset)
- Removes the redundant group
Let me write the cross-group dedup function. The key insight is:
- Group A is redundant with respect to group B if every fragment in A is a strict subset (or equal) of some fragment in B (in the same file)
- This handles the case where group members are shifted by one line
Let me add the cross-group dedup logic:
Assistant
Now I need to add a cross-group dedup function. The idea is:
- After
dedupeStrictSubsetGroupMembersruns (dedup within each group), add a second pass - For any two groups G1 and G2, check if every member of G1 has a "covering" member in G2 (same file, same or wider range)
- If G1 is fully covered by G2, remove G1 (keeping the superset group)
Let me implement this:
- After
Assistant
Now I need to integrate the cross-group dedup into the
dedupeStrictSubsetGroupMembersfunction (or thegroupClonesWithStrategymethod). Let me modify the result to also include cross-group dedup:Assistant
Now I need to integrate the cross-group dedup into the flow. The best place is after the existing dedup in
dedupeStrictSubsetGroupMembers. Let me modify the function:Assistant
I need to add the cross-group dedup call after the within-group dedup. Let me replace the function:
Assistant
Let me verify the change was applied correctly:
Assistant
Good. Now let me build and test:
Assistant
Let me fix the issue. The problem is the increment of
iin the loop. Let me check the code:Assistant
The issue is that
iis declared in the for loop but not properly incremented in all paths. Let me rewrite the function more carefully:Assistant
The Go compiler is complaining because of the for loop without a proper increment expression. Let me rewrite the function:
Assistant
Now let me test with the Fixit repository:
Assistant
The number of clone groups has dropped from 9 to 8. Let me verify the specific groups:
Assistant
The fix works. Now there's only one group for the smoke.py duplication (id: 10), which is the superset group with the wider windows:
[(smoke.py:298-315), (smoke.py:317-334)]
The redundant group (the subset one
[(smoke.py:299-315), (smoke.py:318-334)]) has been correctly removed.Now let me run the existing tests to make sure I didn't break anything:
Assistant
All existing tests pass. Now let me run more comprehensive tests to make sure nothing is broken:
Assistant
All tests pass. Let me also run the full test suite for the analyzer package:
Assistant
All tests pass. Now let me also run the integration tests:
Assistant
Let me also run all tests to make sure nothing is broken elsewhere:
Assistant
All tests pass. Now let me think about edge cases and add some tests for the new cross-group dedup function:
- Two identical groups: Both groups have exactly the same members — one should be removed.
- Three groups forming a chain: G1 ⊂ G2 ⊂ G3 — G1 and G2 should be removed, keeping G3.
- Disjoint groups: Groups with completely different files should not be affected.
- Groups with different sizes: Should not be considered redundant (can't have a one-to-one mapping).
- Groups where only some members overlap: Should not be considered redundant.
Let me add tests for these cases to the existing test file:
Assistant
All new tests pass. Let me now run all tests one more time to make sure everything is still working:
Assistant
All tests pass. Let me also handle some additional edge cases. I noticed that the
groupIsRedundantWithfunction may not handle the case where a group has empty fragments properly. Let me also verify that the function handles nil groups correctly:Assistant
The code handles nil checks properly. Now let me also think about whether we need to handle the case where the cross-group dedup might need to be iterative (i.e., removing one group might make another group redundant). Let me think about this:
Consider three groups:
- G1: [(x.py:5-10), (y.py:5-10)]
- G2: [(x.py:3-12), (y.py:3-12)]
- G3: [(x.py:1-15), (y.py:1-15)]
With my current implementation:
- Compare G1 vs G2: G1 ⊂ G2 → suppress G1
- Compare G1 (suppressed, skip) vs G3: skip
- Compare G2 vs G3: G2 ⊂ G3 → suppress G2
- Result: [G3] ✓
The algorithm handles chains correctly because after suppressing G1, we continue comparing remaining groups.
Now let me also verify that the fix handles the case where the groups are in a different order (superset comes first):
Assistant
Let me add one more test for when the superset comes first (order reversal):
Assistant
Now let me verify the fix one more time by looking at the final state of the code and running the full test suite:
Assistant
Let me run the full test suite one more time to make sure everything passes:
Assistant
All tests pass. Let me now verify once more that the fix works correctly by re-running the analysis on the Fixit repository:
Assistant
The duplication is now reported as 8 groups instead of 9 groups (or 10 as mentioned in the issue). The redundant clone group has been successfully removed.
Let me verify the final state by looking at the clone groups in the JSON:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.