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
I'll start by exploring the repository to understand its structure and locate relevant files for clone detection. Let me first see what's in the current directory.
- Assistant
Let me check the go.mod file to understand the module and then search for clone-related code.
- Assistant
This is a Go repository. Let me search for clone-related code in the repository.
- Assistant
Let me find the clone-related files specifically.
- Assistant
Let me search for the clone detection logic more specifically.
- Assistant
Let me look at the internal clone detector code to understand how clone groups are created and deduplicated.
- Assistant
Let me examine the key files: group_dedup.go and clone_detector.go.
- Assistant
Let me look at clone_detector.go to understand how clone groups are formed and where deduplication happens.
- Assistant
Let me look at the full clone_detector.go to understand how groups are created, and also at group_dedup_test.go.
- Assistant
Let me look at the full file more carefully.
- Assistant
Let me search for the
Detectmethod that's the main entry point: - Assistant
Let me look at the
DetectClonesWithContextmethod to see the full pipeline: - Assistant
Let me look at the group_dedup_test.go to understand what's already tested:
- Assistant
Now let me look at the grouping strategies to understand how clone groups are formed:
- Assistant
Let me look at the grouping strategy and the connected grouping to understand how groups are created, then figure out where the deduplication of overlapping clone groups should happen.
- Assistant
Let me look at the connected_grouping.go to understand how groups are created:
- Assistant
Now let me look at the
gfhelper used in tests to understand the structure: - Assistant
The
gfhelper might be defined in a test helper file. Let me search for it. - Assistant
Let me look at that helper.
- Assistant
Now let me understand the full picture. Let me look at the
groupClonesWithStrategymethod more carefully, and see the full flow: - Assistant
Now let me look at the full
clone_detector_test.goto understand the test patterns: - Assistant
Now let me look at the fragment extraction code to understand how overlapping windows are created:
- Assistant
Now let me understand the full picture. Let me look at how the
groupClonesWithStrategymethod works and trace through the issue: - Assistant
Now I understand the issue. The
dedupeStrictSubsetGroupMembersfunction only handles within-group deduplication. When two separate clone groups describe the same duplication but with slightly different window boundaries (like (299-315, 318-334) vs (298-315, 317-334)), they are not merged because they are separate groups. Let me check if there are any existing tests for this scenario: - Assistant
No results. Let me search more broadly.
- Assistant
Now I understand the issue. Let me think about the solution:
The problem is that two separate clone groups exist that describe the same duplication relationship, but with slightly different window boundaries (one group includes the
with self.subTest(...)header line, the other doesn't). ThededupeStrictSubsetGroupMembersfunction only handles strict subsets within a single group. It doesn't handle the case where two separate groups have members that are overlapping windows of the same underlying fragments.The fix should add a cross-group deduplication step that detects when two groups describe essentially the same duplication relationship by checking if their members overlap pairwise in the same files.
Let me examine the existing tests to understand the test patterns better:
- Assistant
Now I have a clear picture. The issue is about two separate clone groups that describe the same duplication with slightly different window boundaries. The
dedupeStrictSubsetGroupMembersonly handles within-group deduplication, but we need cross-group deduplication. Let me implement the fix.First, let me look at the
groupClonesWithStrategymethod and thegroup_dedup.goto plan the fix: - Assistant
Now let me understand the issue thoroughly. The problem is that clone groups are formed independently and two groups can describe the same duplication with slightly different windows. Let me trace through the logic to understand how this happens:
- Assistant
Let me look at the utility functions:
- Assistant
Now I understand the issue clearly. The problem is that:
- Fragment A (299-315) and Fragment B (318-334) form one clone pair → Group 6
- Fragment C (298-315) and Fragment D (317-334) form another clone pair → Group 14
- A and C overlap (same file, C covers A), B and D overlap (same file, D covers B)
- But
dedupeStrictSubsetGroupMembersonly deduplicates within a single group, not across groups
The fix needs to add cross-group deduplication. Let me first write a test to reproduce the exact issue, then implement the fix.
- Assistant
Let me run this test to confirm it fails with the current code:
- Assistant
Need to set up the Go environment.
- Assistant
Let me try setting GOMODCACHE or GOPATH:
- Assistant
Let me set both GOPATH and GOCACHE.
- Assistant
Good, the test fails as expected. Now let me implement the fix. The issue is that
dedupeStrictSubsetGroupMembersonly handles within-group deduplication. We need to add cross-group deduplication for groups whose members are pairwise overlapping windows of the same fragments.Let me now implement the fix:
- Assistant
Now let me understand the exact algorithm needed. The problem is that two separate clone groups describe the same duplication. The key insight is that when two groups have members that pairwise overlap in the same files, they describe the same clone relationship and should be merged. Let me implement the fix:
- Assistant
Let me see the full file:
- Assistant
Now I understand the full picture. The problem is that
dedupeStrictSubsetGroupMembersonly handles within-group dedup. Two separate groups (Group 6 with {A, B} and Group 14 with {C, D}) describe the same duplication but with different window boundaries. The fix is to add cross-group deduplication for overlapping clone groups.Let me implement the solution:
- Assistant
Now I understand the full picture. Let me implement the fix by adding a
dedupeOverlappingGroupsfunction that handles cross-group deduplication. The key insight is: when two groups have members that are pairwise overlapping (each member in group A has a corresponding member in group B in the same file that covers it or is covered by it), they describe the same duplication and should be merged. - Assistant
Let me check the file to see what happened:
- Assistant
I see the edit caused an issue - the
coversfunction lost its closing brace. Let me fix this properly: - Assistant
I see the issue - the
coversfunction lost its closing brace. Let me fix this properly: - Assistant
I need to add a blank line between the closing brace of
coversand the comment fordedupeOverlappingGroups. Let me also verify the rest of the file: - Assistant
Good, now let me check the rest of the file to make sure everything is correct:
- Assistant
Good, the file looks correct. Now I need to integrate the
dedupeOverlappingGroupsfunction into the pipeline. Let me look at wheregroupClonesWithStrategyis called: - Assistant
Now I need to integrate
dedupeOverlappingGroupsinto thegroupClonesWithStrategymethod. The fix is to calldedupeOverlappingGroupsafterdedupeStrictSubsetGroupMembers: - Assistant
I need to add a blank line between the closing brace of
groupClonesWithStrategyand the comment forisSameLocation. Let me fix that: - Assistant
Good, the file looks correct now. Let me run the test to see if it passes:
- Assistant
The test still fails because the test is calling
dedupeGroupswhich only callsdedupeStrictSubsetGroupMembers. Let me update the test to calldedupeOverlappingGroupsas well: - Assistant
I need to update the test to also call
dedupeOverlappingGroups. Let me also update thededupeGroupshelper to include the new function, or have the test call it directly. Let me update the test: - Assistant
Now let me run the test again:
- Assistant
The test passes. Now I need to:
- Remove the temporary test file
- Add the test to the proper test file (
group_dedup_test.go) - Run all existing tests to make sure nothing is broken
Let me do this step by step:
- Assistant
Now let me add the test to the proper test file
group_dedup_test.go. I need to add the test and a helper function: - Assistant
Let me see the end of the test file:
- Assistant
Now let me add the test to the end of the test file and also update the
dedupeGroupshelper to include the cross-group dedup step (matching the real pipeline): - Assistant
Now let me add the new test case at the end of the file:
- Assistant
I see there's a duplicate block in the test file. Let me fix it:
- Assistant
I see there's a duplicate block in the test file. Let me fix it:
- Assistant
Good, the file looks correct now. Let me run all the tests in the test file to make sure everything passes:
- Assistant
All dedup tests pass. Now let me run the full test suite for the analyzer package to make sure nothing is broken:
- Assistant
All tests pass. Now let me run the full test suite to make sure nothing is broken across the whole project:
- Assistant
All tests pass across the entire codebase. Let me now verify the changes are complete and correct by looking at what I've changed:
- Assistant
Let me verify that
groupClonesWithStrategyis the only place where grouping happens, and check if there are other code paths that could produce clone groups: - Assistant
Good. The
groupClonesWithStrategyis the single bottleneck through which all grouping flows. Let me also verify the LSH path: - Assistant
Both paths use
groupClonesWithStrategy. Let me now review all my changes one final time: - Assistant
The diff looks good. Let me also check the clone_detector.go diff and the test diff.
- Assistant
Let me also check the test file diff:
- Assistant
All changes look correct. Let me now run the full test suite one more time to confirm everything is good:
- Assistant
All tests pass. Let me also think about whether there's any issue with the "mixed directions" case in
compareOverlappingGroups. Let me add one more edge case test to make sure: - Assistant
I see there's a duplicate line on line 352-353. Let me fix that:
- Assistant
Good, the file looks correct now. Let me run the tests again to make sure everything passes:
- Assistant
All tests pass. Let me also run the full test suite one more time to make sure nothing is broken:
- Assistant
All tests pass. Let me review the final diff one more time to make sure everything is correct:
- Assistant
Let me also verify the full group_dedup.go to make sure it compiles cleanly:
- Assistant
Everything looks good. Let me now review the complete diff one final time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.