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 structure to understand the codebase and locate the clone detection logic. Let's first see what's in the current directory.
- Assistant
Let me get a better understanding of the repository structure and find the clone detection code.
- Assistant
Let me explore the internal directory structure to find clone-related code.
- Assistant
Let me look deeper into the internal directory for clone detection logic.
- Assistant
Let me look at the key files for clone detection and deduplication:
- Assistant
Let me see the rest of the file and also look at the clone_detector.go to understand how clone groups are created.
- Assistant
Let me look at the clone_detector.go to understand the full flow:
- Assistant
Let me continue reading the clone detector to see how groups are formed:
- Assistant
Let me look at more of the clone detector, specifically the grouping and dedup logic:
- Assistant
The issue is about duplicate clone groups where two groups describe the same duplication relationship between the same code fragments, but with slightly different window boundaries. Let me understand the structure better.
Looking at the issue:
- Group 6: smoke.py lines 299-315 and 318-334
- Group 14: smoke.py lines 298-315 and 317-334
Group 14's members each start exactly one line earlier than Group 6's members. They end on the same lines. Both describe the same duplication between
subTest("lint")andsubTest("fix")blocks.The fix should be in the dedup stage - we need to detect when two clone groups are "near duplicates" of each other (their members are overlapping windows of the same fragments) and merge or suppress one of them.
Let me explore the codebase more to understand how groups are formed and deduplicated.
- Assistant
Now I need to understand the full picture. Let me look at the grouping strategy and the existing dedup test:
- Assistant
Let me look at the grouping strategies and how clone pairs are created:
- Assistant
Let me see how groups are produced and the full clone detection flow:
- Assistant
Let me look at the connected grouping to understand how groups are formed:
- Assistant
Let me understand the structure better. Let me look at the test file for helper functions:
- Assistant
Let me look for the
gfandgphelper functions: - Assistant
Let me search for gf and gp in the test files
- Assistant
Let me look at the helper functions in grouping_strategies_test.go:
- Assistant
Now I understand the problem. Let me look at how
groupClonesWithStrategyis called: - Assistant
Now I understand the problem clearly. Let me analyze:
-
The problem is that two clone groups are emitted that describe the same duplication between the same two code fragments, but with slightly different window boundaries (one includes the header line, the other doesn't).
-
- Assistant
Now I have a clear understanding of the problem. The issue is:
dedupeStrictSubsetGroupMembersonly handles strict subset removal within a single group (members from the same file where one covers another).- But the bug is about two separate groups that describe the same duplication, with members that are overlapping windows of the same code regions. Group 6 has fragments at (299-315, 318-334) while Group 14 has fragments at (298-315, 317-334) — each member of Group 14 starts exactly one line earlier.
I need to add a second deduplication pass that detects when two groups are near-duplicates of each other (same duplication relationship, different window boundaries) and suppresses the one with smaller members.
Let me first understand the helper functions:
- Assistant
Let me search more broadly for these functions.
- Assistant
Now let me look at the existing test file to understand the test patterns:
- Assistant
Let me also look at how existing tests run:
- Assistant
Let me check the Go environment:
- Assistant
Let me try to set up GOPATH properly.
- Assistant
I need to set HOME or GOCACHE.
- Assistant
Now I have a thorough understanding of the issue and the codebase. The problem is:
-
dedupeStrictSubsetGroupMembersonly handles strict subset removal within a single group (same-file members that are subsets of each other get collapsed). -
But two separate groups (Group 6 and Group 14) describe the same duplication relationship with members at slightly different window boundaries. Group 14's members each start exactly one line earlier (including the
with self.subTest(...)header). These groups are never compared against each other. -
I need to add a cross-group deduplication pass that detects when two groups describe the same duplication (their members overlap pairwise) and suppresses the one with the smaller members.
Let me plan my implementation:
- Add a new function
dedupeOverlappingGroupsingroup_dedup.go - This function compares each pair of groups to see if they describe the same duplication
- Two groups describe the same duplication if:
- Same number of members
- For each file, members can be paired such that one group's members are covered by the other's
-
- Assistant
Now I have a complete understanding. Let me implement the fix. I need to add a function that detects and suppresses duplicate overlapping clone groups — groups that describe the same duplication but with slightly different window boundaries. The algorithm:
- Compare each pair of groups with the same number of members
- For each pair, try to match members: for each member in group A, find a corresponding member in group B from the same file with an overlapping range
- If a complete 1:1 matching exists and one group's members are all covered by the other's, suppress the smaller group
- Assistant
Now I understand the problem completely. The issue is:
dedupeStrictSubsetGroupMembersonly handles within-group strict subsets- Two separate groups (Group 6 and Group 14) describe the same duplication with slightly different window boundaries
- Group 14's members are each a strict superset of Group 6's members (one extra line at the start)
- I need to add cross-group deduplication
Let me implement the fix. I'll add a new function
dedupeOverlappingGroupsthat detects when two groups describe the same duplication relationship and suppresses the one with smaller members: - Assistant
Let me add the new function at the end of the file:
- Assistant
Let me use bash to add the new function to the end of the file:
- Assistant
Now I need to wire
dedupeOverlappingGroupsintogroupClonesWithStrategyso it runs after the existing dedup pass. Let me look at that function: - Assistant
Now I need to modify
groupClonesWithStrategyto also calldedupeOverlappingGroupsafterdedupeStrictSubsetGroupMembers: - Assistant
Let me try with replace:
- Assistant
Let me try the edit command with the proper syntax:
- Assistant
Let me try with sed command:
- Assistant
Let me use sed to replace the function:
- Assistant
Let me verify the replacement was correct:
- Assistant
Good. Now let me verify the new group_dedup.go file looks correct:
- Assistant
Good. Now let me compile and run the existing tests:
- Assistant
Build succeeded. Now let me run the existing tests:
- Assistant
All existing tests pass. Now let me write a test that reproduces the exact issue described in the bug report. The test should verify that overlapping clone groups are properly deduplicated:
- Assistant
Now let me run the new tests to see if they pass:
- Assistant
All tests pass. Now let me run the full analyzer test suite to make sure I haven't broken anything:
- Assistant
All tests pass. Let me also run the service and integration tests:
- Assistant
There's a test failure. Let me see which test failed:
- Assistant
Let me look at this failing test to understand if my change caused it:
- Assistant
Let me search for this test name in the test file:
- Assistant
Let me look at the test more carefully:
- Assistant
Let me search for the test more broadly:
- Assistant
Let me search for the test name from the error:
- Assistant
Let me look at that test:
- Assistant
Let me check if this test was already failing before my change by reverting temporarily:
- Assistant
The test name has a space issue. Let me try the exact test name pattern.
- Assistant
The test passes without my changes. Let me restore my changes and investigate:
- Assistant
Now let me run the specific failing test with my changes to see the error:
- Assistant
The test is failing because my
dedupeOverlappingGroupsis being too aggressive. Let me add debug logging to understand what's happening: - Assistant
Let me write a more direct test to understand what's happening:
- Assistant
OK so my dedup logic works correctly - the inner-block pair gets suppressed and the full-function pair survives. But the service test is still failing. Let me investigate further by adding temporary debug logging to the
groupClonesWithStrategy: - Assistant
I need to make sure the
fmtimport is there. Let me check: - Assistant
Good,
fmtis already imported. Let me run the failing test now: - Assistant
I see! After
dedupeOverlappingGroups, we have 1 group and 1 pair, but the test saysresponse.ClonePairsis empty. The issue might be in how the service converts detector output to the response. Let me check the service layer: - Assistant
Let me check how the service filters clone pairs:
- Assistant
Let me look at the
filterCloneGroupsfunction too: - Assistant
Let me look at how the test creates the request:
- Assistant
Let me look at the
newDefaultCloneRequestfunction and thefilterClonePairs: - Assistant
The debug output shows 1 group and 1 pair after dedup, but the service returns empty pairs. Let me check if there's something wrong with the remaining pair. Let me look at how the service converts pairs:
- Assistant
Wait, looking at the debug output: after my dedup there's 1 group and 1 pair. But the test says
response.ClonePairsis empty. Let me check if the service applies additional filtering. Let me add more detailed debugging: - Assistant
Let me run the test again to see the pair details:
- Assistant
Now I see the problem clearly. Let me analyze:
- Group 0: a.py:2-5 ↔ b.py:2-5 (sim=0.745, type=3)
- Group 1: a.py:1-6 ↔ b.py:1-6 (sim=0.653, type=4)
My dedup correctly identifies Group 0 as a subset of Group 1 and suppresses it. But the pair from Group 0 (sim=0.745) was the one passing the service's
MinSimilarity=0.7filter. The surviving pair from Group 1 (sim=0.653) fails that filter.The root issue is: when suppressing an overlapping group, I should NOT suppress the clone pairs — only the group. The issue is specifically about duplicate groups, not duplicate pairs. The
dedupeStrictSubsetGroupMemberscorrectly suppresses pairs (because the suppressed fragments are genuinely redundant within-group subsets), butdedupeOverlappingGroupsshould only suppress groups, not their pairs.Let me fix the
groupClonesWithStrategyfunction: - Assistant
Let me verify the change:
- Assistant
Good. Now let me build and run the tests:
- Assistant
The test passes now. Let me run all the tests to make sure everything is good:
- Assistant
Let me also run the full service tests:
- Assistant
All tests pass. Let me also run the overlapping group tests to make sure they still pass:
- Assistant
All tests pass. Now let me clean up the temporary debug test files:
- Assistant
Let me also check that the
TestGroupClonesWithStrategy_FiltersSuppressedClonePairstest still passes since I changed the behavior: - Assistant
Let me also run the integration tests:
- Assistant
All integration tests pass. Let me now run the full test suite to make sure everything is good:
- Assistant
All tests pass across the entire codebase. Let me now review the final state of my changes:
- Assistant
Let me look at the full diff to review the changes:
- Assistant
Let me also check that the test file is clean:
- Assistant
Let me verify the final state of the repo:
- Assistant
Let me run all the tests one final time to make sure 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.