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.9curl -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:{ "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:with self.subTest("lint"): # line 298 (grp14 member start) with TemporaryDirectory() as td: # line 299 (grp6 member start) ... with self.subTest("fix"): # line 317 (grp14 member start) with TemporaryDirectory() as td: # line 318 (grp6 member start)Both groups end on the same lines (315 / 334). The two groups are the same logical clone reported twice at slightly different window boundaries.
Expected behavior
The clone-grouping/dedup stage should merge (or suppress) clone groups whose members are overlapping windows of the same underlying fragments, so each duplication relationship is reported once. As-is, the count (10 here) and the duplication score over-count by one redundant group.
Assistant
Let me start by exploring the repository structure to understand how the clone detector works and where the issue might be located.
Assistant
This appears to be a Go-based clone detector (pyscn). Let me explore the directory structure to understand the code better.
<tool_call> <function=bash_terminal> <parameter=command> find . -name "*.go" | head -50 </tool_call>
Assistant
Let me search for Go files to understand the codebase structure.
Assistant
Let me explore the clone-related files since the issue is about clone detection. The bug is about duplicate clone groups being emitted.
<tool_call> <function=bash_terminal> <parameter=command> find . -name "*.go" | grep -i clone </tool_call>
Assistant
I need to use proper formatting for tool calls - let me search for clone-related files.
Assistant
Let me first reproduce the issue by downloading the Fixit repo and running pyscn on it.
Assistant
It seems the tar might not have extracted properly or it's extracting to a parent directory. Let me check.
Assistant
Good, the Fixit repo is extracted. Now let me build pyscn and run it to reproduce the issue.
Assistant
I need to set up Go module cache first.
Assistant
I need to set more environment variables for Go to work. Let me try a different approach.
Assistant
Good, the build succeeded. Now let me run pyscn on the Fixit repo to reproduce the issue.
Assistant
Let me check the JSON output to find the duplicate clone groups described in the issue.
Assistant
I need to extract the clone_groups with smoke.py from the JSON file to see if groups 6 and 14 exist.
<tool_call> <function=bash_terminal> <parameter=command> python3 -c " import json with open('.pyscn/reports/analyze_20260721_164208.json', 'r') as f: data = json.load(f)
Find clone_groups containing smoke.py
clone_groups = data.get('clone', {}).get('clone_groups', []) for grp in clone_groups: clones = grp.get('clones', []) if any('smoke.py' in c.get('location', {}).get('file_path', '') for c in clones): print(json.dumps(grp, indent=2)) print('---') " </tool_call>
Assistant
Let me run this Python command properly.
Assistant
I can see the duplicate groups! In this version, the duplicate groups are:
- Group 17: clones start at lines 299 and 318 (without the
with self.subTest(...)header) - Group 8: clones start at lines 298 and 317 (with the
with self.subTest(...)header)
Both groups cover the same duplication between the
subTest("lint")block and thesubTest("fix")block, differing only by whether thewith self.subTest(...)header line is included.Now let me understand the code structure to figure out where the deduplication should happen. Let me look at the clone detector and classifier files.
<tool_call> <function=bash_terminal> <parameter=command> cat ./internal/analyzer/clone_detector.go | head -100 </tool_call>
- Group 17: clones start at lines 299 and 318 (without the
Assistant
I need to run the bash command properly to look at the clone_detector.go file.
Assistant
Let me continue reading the clone_detector.go file to see more of the code.
Assistant
The clone_detector.go file has 1223 lines. Let me look at more sections to understand the clone grouping logic.
Assistant
Let me look at the clone classifier which seems responsible for determining clone type.
<tool_call> <function=bash_terminal> <parameter=command> cat ./internal/analyzer/clone_classifier.go | head -200 </tool_call>
Assistant
I need to use proper bash_terminal syntax to run commands.
Assistant
Let me list the files in the analyzer directory.
Assistant
I found a
group_dedup.gofile which might be related to the issue. Let me examine it.Assistant
The
group_dedup.gofile handles deduplication of clone group members within a single group, but it doesn't handle the case where two separate groups are near-duplicates of each other. The issue is that groups 6 and 14 (or 8 and 17 in this case) represent the same duplication relationship but are reported as separate groups.Let me look at the clone_detector.go file to see where the clone groups are being created and if there's any deduplication of groups.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "CloneGroup" ./internal/analyzer/clone_detector.go | head -40 </tool_call>
Assistant
Let me search for CloneGroup in the clone_detector.go file to understand where clone groups are created and where deduplication happens.
Assistant
Let me look at the DetectClonesWithLSH function to understand how clone groups are created, particularly in the LSH path since the issue mentions that both groups are detected.
Assistant
Let me continue reading more of the clone_detector.go file to find where clone groups are grouped and where the issue might be.
Assistant
Let me continue reading more of the clone_detector.go file to understand the clone grouping logic.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '950,1150p' ./internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me read more of the clone_detector.go file to understand the clone grouping logic.
Assistant
I found a key function
groupClonesWithStrategywhich callsdedupeStrictSubsetGroupMembers. This function deduplicates members within a group but doesn't deduplicate across groups.Now let me look at the connected_grouping.go file to understand how groups are formed, which might be where the issue is.
<tool_call> <function=bash_terminal> <parameter=command> cat ./internal/analyzer/connected_grouping.go </tool_call>
Assistant
I need to use proper bash_terminal syntax to run the cat command to look at connected_grouping.go.
Assistant
I see the connected grouping logic. The groups are created from fragments that are connected via clone pairs with similarity >= threshold. The issue is that there are two groups that cover the same logical duplication but with slightly different window sizes.
Let me look at the issue more closely. The problem is that:
- Group 8 covers fragments starting at lines 298/317 (with the
withheader) - Group 17 covers fragments starting at lines 299/318 (without the
withheader)
Both groups cover the same duplication relationship between the
subTest("lint")block and thesubTest("fix")block. The issue is that both windows are valid and similar, but they represent the same logical clone relationship.Let me look at how the clone pairs are created to understand why both are being created as separate pairs. The key is probably in the
tryCreateClonePairfunction which might be creating overlapping pairs for fragments that are essentially the same.Let me search for where overlapping fragments might be created.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "tryCreateClonePair" ./internal/analyzer/clone_detector.go </tool_call>
- Group 8 covers fragments starting at lines 298/317 (with the
Assistant
I need to look at the tryCreateClonePair function to understand how clone pairs are created.
Assistant
Let me look at the tryCreateClonePair function to understand how pairs are created.
Assistant
Now let me understand the issue better. The problem is that there are two different clone groups:
- Group 8: covers lines 298-315 and 317-334 (includes the
withheader) - Group 17: covers lines 299-315 and 318-334 (excludes the
withheader)
Both groups cover the same logical duplication. The issue is that these two groups are near-duplicates of each other, meaning they represent the same duplication relationship but with slightly different window boundaries.
The
group_dedup.gofile hasdedupeStrictSubsetGroupMemberswhich handles the case where one fragment is a strict subset of another within the same group. However, the issue is about two separate groups that are near-duplicates of each other across groups.Let me look at the smoke.py file in the Fixit repository to understand the actual code structure.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '295,340p' Fixit-HEAD/src/fixit/tests/smoke.py </tool_call>
- Group 8: covers lines 298-315 and 317-334 (includes the
Assistant
Let me read the smoke.py file to understand the code structure that's causing the duplicate clone groups.
Assistant
Now I can see the code structure. The two blocks are:
Group 8 (lines 298-315 and 317-334):
with self.subTest("lint"): # line 298 with TemporaryDirectory() as td: # line 299 ... with self.subTest("fix"): # line 317 with TemporaryDirectory() as td: # line 318 ...Group 17 (lines 299-315 and 318-334):
with TemporaryDirectory() as td: # line 299 ... with TemporaryDirectory() as td: # line 318 ...The issue is that both groups represent the same duplication, but one includes the
with self.subTest(...)header line and the other doesn't. The two groups are near-duplicates of each other, and the problem is that the deduplication logic ingroup_dedup.goonly handles cases where one fragment is a strict subset of another within the same group, but doesn't handle the case where two separate groups are near-duplicates.Let me now think about how to fix this issue. The fix should be to add a deduplication step for groups that are near-duplicates of each other. This should happen after the grouping step but before the final results are returned.
Assistant
I need to use proper bash_terminal syntax to read the test file.
Assistant
Now I understand the issue. The current
dedupeStrictSubsetGroupMembershandles cases where one fragment is a strict subset of another within the same group. However, the issue is about two separate groups that are near-duplicates of each other.Let me check if there's an issue with how clone pairs are being created - perhaps both the "with header" and "without header" versions are being detected as valid clone pairs.
Assistant
Now I understand the issue. We have two separate clone groups that represent the same duplication relationship but with different window boundaries:
- Group 17: lines 299-315 and 318-334 (without
withheader) - Group 8: lines 298-315 and 317-334 (with
withheader)
Group 17's fragments are strict subsets of Group 8's fragments. The current deduplication only handles subsets within a single group, not across groups. I need to add a post-processing step to remove duplicate groups.
Let me implement this fix in the
group_dedup.gofile.- Group 17: lines 299-315 and 318-334 (without
Assistant
Now I need to add a new function to deduplicate clone groups that are near-duplicates of each other. This should check if two groups are essentially the same (i.e., they cover the same logical duplication relationship but with slightly different window boundaries).
The key insight is that when two groups have fragments that are nearly identical (with one being a strict subset of the other in the same file), they should be deduplicated. The group with the maximal fragments should be kept.
Let me add a new function
dedupeOverlappingGroupsthat handles this case. This function will:- Check if two groups have overlapping members in the same file
- If so, keep only the group with the maximal members (not subsets of the other)
- Remove groups that are subsets of other groups
Let me first look at the test file to understand the helper functions better.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "gf|gp" ./internal/analyzer/group_dedup_test.go | head -20 </tool_call>
Assistant
Let me search for the helper functions used in the test file.
Assistant
Let me look at the beginning of the test file for helper functions.
Assistant
Let me look for the gf helper function at the end of the test file.
Assistant
Let me search for the gf and gp helper functions which are used in the test.
Assistant
Let me check the clone_detector_test.go file for helper functions.
Assistant
The helper functions
gfandgpare defined somewhere. Let me search for them more broadly.<tool_call> <function=bash_terminal> <parameter=command> grep -rn "func gf" ./internal/analyzer/*.go </tool_call>
Assistant
Let me run a broader search for the helper functions.
Assistant
Now I found it. Let me also find the
gphelper function.Assistant
Now I understand the helper functions. Let me also look at the
gpfunction in grouping_strategies_test.go.<tool_call> <function=bash_terminal> <parameter=command> cat ./internal/analyzer/grouping_strategies_test.go </tool_call>
Assistant
I need to use proper bash_terminal syntax to run the cat command.
Assistant
Now I understand the issue. The problem is that there are two separate clone groups that represent the same duplication relationship:
- Group 8: lines 298-315 and 317-334 (with
withheader) - Group 17: lines 299-315 and 318-334 (without
withheader)
Group 17's fragments are strict subsets of Group 8's fragments in the same file. The current deduplication (
dedupeStrictSubsetGroupMembers) handles subsets within a single group, but not across groups.I need to add a new deduplication function that removes groups where all fragments in the group are strict subsets of another group's fragments in the same files. Let me implement this fix:
- Group 8: lines 298-315 and 317-334 (with
Assistant
Now I need to update the
groupClonesWithStrategyfunction inclone_detector.goto call the newdedupeSubsetGroupsfunction afterdedupeStrictSubsetGroupMembers.Assistant
Let me look at the
groupClonesWithStrategyfunction and update it to calldedupeSubsetGroups.<tool_call> <function=bash_terminal> <parameter=command> sed -n '1116,1135p' ./internal/analyzer/clone_detector.go </tool_call>
Assistant
I need to use proper bash_terminal syntax to get the content of the file around line 1116.
Assistant
Now I need to update the
groupClonesWithStrategyfunction to also calldedupeSubsetGroupsafterdedupeStrictSubsetGroupMembers.Assistant
Let me verify the change was applied correctly.
Assistant
Now let me rebuild the project and test the fix.
Assistant
Good, the build succeeded. Now let me run pyscn on the Fixit repo again to see if the fix works.
Assistant
The output now shows 8 groups instead of 9 groups. Let me verify that the duplicate clone groups were properly removed by checking the smoke.py groups.
Assistant
The fix is working. We now have only one clone group (ID: 12) instead of two duplicate groups. The group that was kept is the one with the larger window (298-315 and 317-334) that includes the
with self.subTest(...)header, which is the correct choice.Now let me run the existing tests to make sure I haven't broken anything:
Assistant
All existing dedupe tests pass. Now let me add a test for the new
dedupeSubsetGroupsfunction to ensure the fix is properly tested.Assistant
Now let me run the new tests to make sure they pass.
Assistant
All tests pass. Let me run the full test suite for the analyzer package to make sure I haven't broken anything:
Assistant
Let me run the full test suite to make sure nothing is broken:
Assistant
All tests pass. Now let me also check if there are any integration tests related to clone detection:
<tool_call> <function=bash_terminal> <parameter=command> export HOME=/root && export GOPATH=/root/go && export GOMODCACHE=GOPATH/pkg/mod && export GOCACHE=HOME/.cache/go-build && go test ./integration/... -v -run ".clone." 2>&1 </tool_call>
Assistant
Let me run the integration tests for clone detection.
Assistant
Let me look at the integration tests to understand what tests exist:
Assistant
The integration tests pass, including clone detection tests. Let me also look at the clone_integration_test.go to understand what it tests and verify our fix is compatible:
<tool_call> <function=bash_terminal> <parameter=command> export HOME=/root && export GOPATH=/root/go && export GOMODCACHE=GOPATH/pkg/mod && export GOCACHE=HOME/.cache/go-build && go test ./integration/... -v 2>&1 | tail -20 </tool_call>
Assistant
Let me run the integration tests and see the end of the output.
Assistant
All integration tests pass. Now let me verify the fix one more time by checking if the duplicate groups were properly removed.
<tool_call> <function=bash_terminal> <parameter=command> python3 -c " import json with open('.pyscn/reports/analyze_20260721_164838.json', 'r') as f: data = json.load(f)
clone_groups = data.get('clone', {}).get('clone_groups', []) print(f'Total clone groups: {len(clone_groups)}') print()
Print all smoke.py groups
smoke_groups = [] for grp in clone_groups: clones = grp.get('clones', []) if any('smoke.py' in c.get('location', {}).get('file_path', '') for c in clones): smoke_groups.append(grp)
print('Clone groups involving smoke.py:') for g in smoke_groups: print(f'ID: {g["id"]}') for c in g['clones']: loc = c.get('location', {}) print(f' {loc["file_path"]}:{loc["start_line"]}-{loc["end_line"]}') print(f' Similarity: {g["similarity"]}') print()
print(f'Total smoke.py groups: {len(smoke_groups)}') print() print('Expected: 2 groups (one for lines 268-275/277-286, one for lines 298-315/317-334)') " </tool_call>
Assistant
Let me run this Python command properly.
Assistant
The fix is working correctly. We now have:
- Total clone groups: 8 (down from 9)
- Smoke.py groups: 2 (down from 3)
The duplicate clone groups (ID 8 and 17, or ID 12 in this case) have been properly deduplicated. The group with the larger window (lines 298-315 and 317-334, which includes the
with self.subTest(...)header) was correctly retained.Let me also verify that the fix handles edge cases correctly by checking a few more scenarios:
Assistant
The fix is working correctly. All the clone groups are now properly deduplicated:
- Each group represents a unique duplication relationship
- No duplicate groups with overlapping members are present
- The groups are sorted by similarity (descending)
Let me also check if there are any similar patterns in other test files that might have been affected by the fix:
Assistant
Let me verify that the fix handles all edge cases correctly by looking at the actual group deduplication logic:
Assistant
The fix is complete and working correctly. Let me summarize what I've done:
-
Identified the issue: Two clone groups were being emitted that represent the same duplication relationship but with different window boundaries (one includes the
with self.subTest(...)header line and the other doesn't). -
Added a new deduplication function:
dedupeSubsetGroupsthat removes clone groups whose fragments are all strict subsets of another group's fragments in the same files. -
Updated the
groupClonesWithStrategyfunction: This function now callsdedupeSubsetGroupsafterdedupeStrictSubsetGroupMembersto ensure that both within-group and across-group deduplication happen.
-
Assistant
Let me run the full test suite again to make sure everything is working correctly.
Assistant
All tests pass. Let me verify the fix one more time by running pyscn on the Fixit repository:
Assistant
Let me fix the Python command:
Assistant
The fix is working correctly. Let me provide a summary of what was done:
Summary of Changes
Issue
pyscn's clone detector was emitting two separate clone groups that describe the same duplication relationship between the same two code fragments. The two groups differed only by whether an enclosing
with self.subTest(...)header line was included. This resulted in inflated clone group counts (9 instead of 8) and duplication percentages.Solution
Added a new deduplication function
dedupeSubsetGroupsthat removes clone groups whose fragments are all strict subsets of another group's fragments in the same files.Files Modified
./internal/analyzer/group_dedup.go: AddeddedupeSubsetGroupsandgroupsAreSubsetOffunctions./internal/analyzer/clone_detector.go: UpdatedgroupClonesWithStrategyto calldedupeSubsetGroups./internal/analyzer/group_dedup_test.go: Added new tests for the deduplication logic
Verification
- Total clone groups reduced from 9 to 8
Assistant
I need to use the submit command without arguments. Let me try again.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.