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 start by understanding the issue and exploring the repository structure to understand how the clone detection and grouping works.
The bug is about duplicate-overlapping-clone-groups: the 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.
Let me first explore the repository structure.
Assistant
Let me explore the source code structure more, specifically looking at the clone detection code.
Assistant
It seems like the main code might be in Rust. Let me check.
Assistant
Let me look more carefully at the project structure.
Assistant
This is a Go project. Let me explore the internal directory where the clone detection logic likely resides.
Assistant
Let me explore the key files related to clone detection and deduplication.
Assistant
Now let me look at the clone detector code:
Assistant
Let me look at the connected grouping and other grouping strategy files:
Assistant
Now let me look at the group_dedup_test.go to understand the existing tests for deduplication:
Assistant
Now I understand the existing code. The issue is about two separate clone groups that describe the same duplication, but with member windows shifted by one line. The existing
dedupeStrictSubsetGroupMembersonly handles strict subsets within a group.The problem is about cross-group deduplication: two groups that are near-duplicates of each other, where:
- Group 6: members at lines 299-315 and 318-334
- Group 14: members at lines 298-315 and 317-334
Each member in Group 14 covers exactly one more line at the start than the corresponding member in Group 6 (the
with self.subTest(...)header). So Group 14 is "larger" than Group 6 in terms of line coverage.The fix should detect when two groups describe the same duplication relationship and keep only the one with the maximal coverage (or the more similar one).
Let me look at what
gffunction does and understand the test infrastructure better:<tool_call> <function=bash_terminal> <parameter=command> grep -r "func gf" /pyscn/internal/analyzer/ | head -5 </tool_call>
Assistant
Let me search for the gf helper function properly.
Assistant
Let me see that file to understand the test helper.
Assistant
Now let me understand how the grouping and deduplication is orchestrated:
Assistant
Now let me look at how
groupClonesWithStrategyis called and what the full flow looks like:Assistant
Now I understand the issue. Let me look at the
pairKeyfunction and understand how fragments are compared:Assistant
Now I need to understand the
pairKeyfunction to see how it handles pair identity.Let me look at the
pairKeyfunction.Assistant
Now I have a good understanding of the codebase. Let me examine how the clone groups are generated and where the deduplication happens:
Assistant
Now let me look at the full
group_dedup.gofile and understand the structure better:Assistant
Now I have a good understanding of the codebase. The issue is that
dedupeStrictSubsetGroupMembersonly handles deduplication within a single group (removing overlapping members within the same group), but it doesn't handle the case where two separate clone groups describe the same duplication with slightly different window boundaries.Let me first write a test to reproduce the issue, then implement the fix.
Assistant
I need to set up Go environment first.
Assistant
Now I need to reproduce the issue. Let me first try to find test data or create a test case that reproduces the problem described in the bug report. The issue is about two groups that have overlapping members, shifted by exactly one line.
Let me think about the fix:
-
The problem: Two separate clone groups describe the same duplication, with Group 14's members each starting one line earlier than Group 6's members.
-
The existing
dedupeStrictSubsetGroupMembershandles overlapping fragments within a single group, but doesn't handle overlapping groups. -
My fix: After deduping within groups, I need to add a cross-group deduplication step that:
- For each pair of groups, check if they describe the same duplication relationship
- A group A "covers" group B if, for every member of B, there's a corresponding member of A in the same file that covers it (starts at the same or earlier line and ends at the same or later line)
-
Assistant
I need to set HOME variable for Go to work properly.
Assistant
All existing tests pass. Now let me write a test that reproduces the specific issue reported, then implement the fix.
Assistant
Now let me run the new tests to confirm they fail with the current code:
Assistant
The tests confirm the bug. Now let me implement the cross-group deduplication fix in
group_dedup.go:Assistant
Now I need to implement the cross-group deduplication logic. Let me understand the full structure of the fix:
- Add a
fragCoversfunction to check if one fragment's location covers another's - Add a
groupCoversStrictlyfunction to check if one group covers another via a bijection - Add a
dedupeCrossGroupDuplicatesfunction that suppresses groups covered by another - Integrate this into the existing
dedupeStrictSubsetGroupMembersfunction
- Add a
Assistant
Let me use the
replacecommand instead:Assistant
Now let me run the tests to see if they pass:
Assistant
I need to remove the unused "fmt" import. Let me check the file:
Assistant
I need to fix the unused import and check if the
groupDedupeResulttype is defined elsewhere:Assistant
The
groupDedupeResulttype was removed. Let me fix the file properly:Assistant
Let me run the tests to see if they pass now.
Assistant
The build succeeded. Now let me run the tests.
Assistant
All 14 tests pass, including the 4 new cross-group deduplication tests. Let me now run the full test suite to make sure I haven't broken anything:
Assistant
All tests pass. Let me also run the full test suite for the entire project to make sure nothing is broken:
Assistant
All tests pass. Let me now review the complete changes I made to ensure everything is correct:
Assistant
All tests pass. Let me now verify the fix works end-to-end by also running the grouping strategy tests:
Assistant
Let me run the complete test suite once more to ensure everything is working:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.