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. The bug is about duplicate clone groups being emitted by pyscn's clone detector. Two separate clone groups 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 to understand how the clone detection works.
Assistant
Let me explore the repository structure more to find the clone detection code.
Assistant
This looks like a Go project. Let me look for the clone detection code.
Assistant
Let me look at the internal directory and clone-related code.
Assistant
Let me look at the clone detector and group dedup code:
Assistant
Let me examine the group deduplication code and the clone detector:
Assistant
Now I understand the issue. The
dedupeStrictSubsetGroupMembersfunction handles deduplication of members within a single group, but the bug is about two separate groups that are near-duplicates of each other. Let me look at the clone detector and understand the full pipeline.Let me look at the clone detector to understand how groups are created and where deduplication happens.
Assistant
Let me look at the grouping strategy to understand how clone groups are formed:
Assistant
Let me look at the connected grouping implementation and how groups are formed.
Assistant
Now let me look at the group_dedup_test.go to understand how the deduplication works and what tests exist:
Assistant
Now I understand the issue. Let me look at how fragments are created and how the clone detection pipeline works more closely:
Assistant
Now let me understand the problem better. The issue is that two separate clone groups are emitted that describe the same duplication between the same two logical code regions, but with slightly different windows (differing by 1 line). Let me look at the complete flow:
Assistant
Now let me look at the clone_detector.go more closely to see the Analyze method and how groups are assembled:
Assistant
Let me see the full context around line 641 and 810 to understand how clone groups are formed and then deduplicated.
Assistant
Let me understand the fragment creation and how two groups could end up with overlapping fragments:
Assistant
Let me look at more of the clone detector to understand the flow:
Assistant
Let me look at the
fragmentLessfunction and understand how fragments are compared:Assistant
Let me look at how fragments are created and understand the full pipeline:
Assistant
Now let me look at the
analyzemethod and the full clone detector to understand the flow:Assistant
Now I have a good understanding of the issue. The problem is:
- Two different AST fragments can be created from overlapping code regions (e.g., a
withstatement vs. a nestedwithstatement inside it) - Both fragments produce clone pairs with the same partner fragments
- This creates two separate clone groups that are near-duplicates of each other
Let me look at how fragments are extracted and how pairs are detected:
- Two different AST fragments can be created from overlapping code regions (e.g., a
Assistant
Let me look at the
pairKeyfunction and understand how pairs are identified:Assistant
Let me understand how fragments are created to get a better picture. Let me look at the file analyzer:
Assistant
Now I need to understand how fragments are created. Let me look at the module analyzer:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "CreateFragment|createFragment|NewCodeFragment|CodeFragment{" /pyscn/internal/analyzer/module_analyzer.go | head -30 </tool_call>
Assistant
Let me try again with the command.
Assistant
Let me look at the module analyzer to understand how fragments are created.
Assistant
The output seems to be empty or the command is not returning anything. Let me try a different approach.
Assistant
Let me focus on finding where fragments are created in the clone detection pipeline. Let me look at the analyzer more carefully.
Assistant
Now let me see how fragments are created around lines 456 and 528 in clone_detector.go:
Assistant
Now I understand the issue. Let me look at the complete flow more carefully. The problem is that nested
withstatements create fragments at different nesting levels, and both levels form separate clone groups for the same underlying duplication relationship.Assistant
Now I have a good understanding of the issue. Let me summarize my reasoning:
-
The Problem: Two separate clone groups are emitted that describe the same duplication relationship, with member windows shifted by exactly one line (the enclosing
with/header line). -
Current deduplication:
dedupeStrictSubsetGroupMembershandles deduplication of members within a single group, but NOT deduplication of entire groups that are near-duplicates of each other. -
The Scenario in the bug:
- Group 6: members (299-315, 318-334)
- Group 14: members (298-315, 317-334)
Group 6's members are strict subsets of Group 14's members. Both represent the same logical duplication, just with different window boundaries.
-
The Fix: Add a new deduplication pass that removes clone groups whose members are all subsets/supersets of another group's members.
Let me first create a test case to reproduce the issue, then implement the fix.
-
Assistant
Now I understand the issue. The
dedupeStrictSubsetGroupMembersfunction only deduplicates members within a single group, but doesn't deduplicate entire groups that are near-duplicates of each other. I need to add a new deduplication pass.Let me first create a test to reproduce the issue, then implement the fix.
Assistant
Let me now understand the issue more deeply by looking at how the grouping works:
Assistant
Let me check the Go environment and try to run the tests properly.
Assistant
The environment doesn't have a proper Go setup. Let me try setting the necessary environment variables.
Assistant
The existing tests pass. Now let me understand the issue more clearly by analyzing the code flow and then implementing a fix.
The issue is:
- Two separate clone groups exist where each group's members are subsets/supersets of the other group's members
- The current deduplication only handles members within a single group, not across groups
- I need to add a cross-group deduplication pass
Let me implement the fix in
group_dedup.go:Assistant
Now I understand the issue fully. The problem is that two separate clone groups are emitted that describe the same duplication relationship but with slightly different window boundaries. The current deduplication only handles members within a single group, not entire groups that are near-duplicates of each other.
Let me implement a fix by adding a new deduplication pass that detects and removes duplicate clone groups:
Assistant
Let me reconsider my approach. I need to add the new function to the end of the file, not replace the entire file. Let me append to the file instead.
Assistant
Let me verify the file was updated correctly.
Assistant
Now I need to add the cross-group deduplication logic. Let me add a function that removes groups whose members are all covered by another group's members.
Assistant
Let me verify the current state of my changes and then check if the code compiles.
Assistant
Good, the code looks correct. Now let me verify it compiles:
Assistant
I need to fix the compilation error - the
keepIdxvariable is declared but not used. Let me fix it:Assistant
Let me fix the unused variable issue by simplifying the logic:
Assistant
The user wants me to continue fixing the issue. Let me check if the compilation error is resolved.
Assistant
The user wants me to continue fixing the compilation error. Let me check if the compilation error is resolved. I need to see the current state of the file and fix the remaining issues.
Assistant
The file has been corrupted by the sed commands. Let me rewrite the entire file properly.
Assistant
Now let me verify the code compiles correctly:
Assistant
The code compiles. Now I need to integrate the new
dedupeDuplicateGroupsfunction into the deduplication pipeline. Let me modify thededupeStrictSubsetGroupMembersfunction to also calldedupeDuplicateGroupsafter processing.Assistant
I need to modify
dedupeStrictSubsetGroupMembersto also calldedupeDuplicateGroupsafter the in-group deduplication. Let me update the function:Assistant
Now I need to replace the old function with the new one. Let me use the edit command:
Assistant
Let me rewrite the entire file using bash:
Assistant
Now let me verify the code compiles and run the existing tests:
Assistant
Good, the code compiles. Now let me run the existing tests to make sure they still pass:
Assistant
All existing tests pass. Now let me create a test case that reproduces the exact bug scenario described in the issue - two clone groups that differ by exactly one line (the enclosing
withheader):Assistant
Now let me run the tests to see if my fix works correctly:
Assistant
The new tests pass. Let me run all the existing tests to make sure nothing is broken:
Assistant
All tests pass. Now let me run the full test suite for the analyzer package to make sure nothing else is broken:
Assistant
All tests pass. Let me also run the full test suite to ensure nothing else is broken:
Assistant
All tests pass across the entire codebase. Let me now review the final state of my changes and make sure they handle all edge cases properly.
Assistant
The changes look good. Let me verify the complete test suite passes one more time:
Assistant
All tests pass. Let me review my changes one more time and then submit:
Assistant
Let me fix the test code which has awkward assertions that don't properly validate the outcome:
Assistant
The tests pass. However, I realize there's an issue: when
dedupeDuplicateGroupssuppresses groups, the fragments in those groups should also be added to thesuppressedmap so thatfilterClonePairsWithSuppressedMemberscan filter them out. Let me fix this:Assistant
Let me verify the code compiles and the tests pass.
Assistant
Now I need to run the tests to verify my changes work correctly.
Assistant
All tests pass. Let me run the full test suite to confirm everything works.
Assistant
All tests pass. Let me review my changes:
Assistant
All tests pass. Let me summarize my changes and submit:
Summary of Changes
The bug was that pyscn's clone detector emitted two separate clone groups describing the same duplication relationship between the same two code fragments, with member windows shifted by exactly one line (the enclosing
with/header line).Root Cause
The existing deduplication only handled members within a single group, but didn't handle deduplication of entire groups that are near-duplicates of each other.
Fix
Added a new cross-group deduplication pass in
group_dedup.go:-
isGroupCoveredBy(): Checks if every member of one group is covered by the corresponding member of another group (after sorting both groups by fragment location). -
dedupeDuplicateGroups(): Removes clone groups that are near-duplicates of each other. When a duplicate is found, the group with higher similarity is kept. When similarities are equal, the group with fewer total lines (smaller window) is kept.
-
Assistant
The user is asking me to submit my changes. Let me call the submit command properly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.