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 understanding the issue. The bug report is about duplicate clone groups being emitted by pyscn's clone detector. Specifically, 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 it's organized.
Assistant
Let me understand the issue better. The bug report mentions pyscn's clone detector. This appears to be a code clone detection tool. Let me explore the python directory since this is likely a Python-based clone detector.
<tool_call> <function=bash_terminal> <parameter=command> ls -la python/ </tool_call>
Assistant
Let me explore the python directory to understand the clone detection implementation.
Assistant
Let me continue exploring the Python source directory.
<tool_call> <function=bash_terminal> <parameter=command> ls -la python/src/ </tool_call>
Assistant
Let me try a different approach - just list the directory contents.
Assistant
So the clone detection is in
python/src/pyscn/. Let me explore that directory.<tool_call> <function=bash_terminal> <parameter=command> ls -la python/src/pyscn/ </tool_call>
Assistant
I need to use bash_terminal with the command properly. Let me continue exploring the pyscn directory.
Assistant
It seems like the Python code is mainly the entry points. Let me check the Go code since the repository structure shows Go files (go.mod, go.sum). The actual clone detection might be implemented in Go. Let me explore the internal and service directories.
<tool_call> <function=bash_terminal> <parameter=command> ls -la internal/ </tool_call>
Assistant
Let me explore the Go source code directory to understand the clone detection implementation.
Assistant
Let me explore the analyzer directory which likely contains the clone detection logic.
<tool_call> <function=bash_terminal> <parameter=command> ls -la internal/analyzer/ </tool_call>
Assistant
Let me explore the analyzer directory to find the clone detection code.
Assistant
The
group_dedup.gofile looks directly relevant to the issue. Let me examine it first.Assistant
Now let me look at the clone_detector.go to understand how clone groups are formed:
<tool_call> <function=bash_terminal> <parameter=command> cat internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me read the clone_detector.go file to understand how clone groups are formed.
Assistant
The file was truncated. Let me see the full clone_detector.go file to understand the full logic.
<tool_call> <function=bash_terminal> <parameter=command> wc -l internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me view the clone_detector.go file in parts to understand the grouping logic.
Assistant
I see the clone_detector.go file is long. Let me look at how clone groups are formed and see the groupDedup logic more closely. The issue is about duplicate overlapping clone groups. Let me look at the group_dedup_test.go to understand the existing deduplication tests.
<tool_call> <function=bash_terminal> <parameter=command> cat internal/analyzer/group_dedup_test.go </tool_call>
Assistant
Let me examine the group_dedup_test.go file to understand the existing deduplication tests and how the deduplication works.
Assistant
Now let me understand the issue better. The bug report says that two separate clone groups are emitted for the same duplication, with member windows shifted by exactly one line. The issue is that group 6 and group 14 both describe the same duplication but with different window boundaries.
Let me look at how clone groups are formed and see if there's logic to detect and deduplicate overlapping clone groups.
Let me first search for how clone groups are deduplicated or if there's any logic to handle this case.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "dedupe|deduplicate|duplicate" internal/analyzer/*.go </tool_call>
Assistant
Let me run the grep command to search for deduplication logic.
Assistant
Now I understand the issue better. The current deduplication logic (
dedupeStrictSubsetGroupMembers) only removes strict subset members within a single clone group. However, the bug is about two separate clone groups that describe the same duplication relationship but with slightly different window boundaries.The issue is that there are two clone groups (id: 6 and id: 14) where:
- Group 6: fragments at lines 299-315 and 318-334
- Group 14: fragments at lines 298-315 and 317-334
These groups describe the same duplication (between the
subTest("lint")block andsubTest("fix")block) but with different boundaries - group 14's members each start exactly one line earlier than group 6's.This is a case of duplicate clone groups that need to be deduplicated at the group level, not just at the fragment level within a group.
Let me first reproduce the issue to understand it better. The bug report mentions the Fixit repo as a reproducer. Let me try to run the reproduction steps.
<tool_call> <function=bash_terminal> <parameter=command> cd /pyscn && make help 2>&1 | head -50 </tool_call>
Assistant
Let me run the command to understand how to build and use the pyscn tool.
Assistant
Let me check how to build the pyscn binary.
Assistant
Now let me build the pyscn tool and test the issue.
<tool_call> <function=bash_terminal> <parameter=command> cd /pyscn && make build 2>&1 </tool_call>
Assistant
Let me build the pyscn tool properly.
Assistant
I need to set up the Go module cache first. Let me configure Go properly.
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && go mod download && go build -o pyscn ./cmd/pyscn </tool_call>
Assistant
Let me try setting up Go environment properly and building.
Assistant
Now let me build the pyscn binary.
Assistant
The build requires cache directories. Let me set up proper Go environment.
Assistant
Now let me verify the binary works and then try to reproduce the issue.
Assistant
Let me download the Fixit repository to reproduce the issue.
Assistant
Now let me run pyscn on the Fixit repository to reproduce the issue.
Assistant
Now let me examine the clone groups in the JSON output to verify the duplicate issue.
Assistant
Let me use Python to examine the JSON output.
Assistant
Let me check all clone groups to see the full picture.
Assistant
I've reproduced the issue! Looking at the clone groups:
- Group 16: fragments at lines 299-315 and 318-334 (without
withheader line) - Group 17: fragments at lines 298-315 and 317-334 (with
withheader line included)
Both groups describe the same duplication in
smoke.py, just with slightly different window boundaries. Let me examine the source file to confirm.- Group 16: fragments at lines 299-315 and 318-334 (without
Assistant
Let me check the exact line numbers in the file:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '298p;317p;318p Fixit-HEAD/src/fixit/tests/smoke.py </tool_call>
Assistant
Let me verify the exact line numbers:
Assistant
Assistant
Let me continue reading the clone_detector.go file to understand the grouping logic.
Assistant
Now I have a better understanding of the code structure. The key function that needs to be modified is
groupClonesWithStrategywhich callsdedupeStrictSubsetGroupMembers. I need to add additional deduplication logic to handle the case of duplicate overlapping clone groups.The issue is that we have two separate clone groups that describe the same duplication:
- Group 16: fragments at 299-315 and 318-334
- Group 17: fragments at 298-315 and 317-334
These groups are "near-duplicates" because:
- They have the same number of fragments (2 each)
- The fragments are from the same files
Assistant
Let me view the group_dedup.go file to understand the current deduplication logic:
Assistant
Now I understand the issue. Let me analyze the situation:
- The current
dedupeStrictSubsetGroupMembersonly removes strict subset members within a single clone group. - The bug is about two separate clone groups that describe the same duplication but with slightly different window boundaries:
- Group 16: fragments at 299-315 and 318-334 (without
withheader) - Group 17: fragments at 298-315 and 317-334 (with
withheader)
- Group 16: fragments at 299-315 and 318-334 (without
These groups are "near-duplicates" - they describe the same logical duplication, just with 1-line window shifts.
I need to add a new deduplication function that detects and removes such duplicate groups. Let me implement the fix:
- The current
Assistant
Now I need to integrate the new
dedupeOverlappingCloneGroupsfunction into the clone detection pipeline. Let me update the clone_detector.go to call this function after the existing deduplication.Let me first check the clone_detector.go to see where the deduplication is called:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "dedupeStrictSubsetGroupMembers|groupClonesWithStrategy" internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me run the grep command properly to see where the deduplication is called.
Assistant
Now let me update the
groupClonesWithStrategyfunction to also calldedupeOverlappingCloneGroups:<tool_call> <function=bash_terminal> <parameter=command> sed -n '1116,1135p' internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me view the groupClonesWithStrategy function and modify it to also call dedupeOverlappingCloneGroups.
Assistant
Now I need to modify the
groupClonesWithStrategyfunction to also calldedupeOverlappingCloneGroups. Let me update this function:Assistant
The sed command ran, but it might not have worked correctly. Let me verify and fix if needed by viewing the updated code.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '1116,1140p' internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me check the update to the groupClonesWithStrategy function.
Assistant
The sed command inserted the new line but also kept the old line that assigns
dedupeResult.groupstocd.cloneGroups. I need to fix this by removing the old line. Let me update the function properly.<tool_call> <function=bash_terminal> <parameter=command> sed -i '1129d' internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me check the updated function to see if it's correct now.
Assistant
The old line is still there. Let me remove it with the correct line number.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '1119,1135p' internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me try using the edit command to fix this function properly.
Assistant
Let me verify the function is now correct.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '1116,1135p' internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me try the sed command again to delete line 1129 and verify the function.
Assistant
The sed command deleted a line but there's still a duplicate assignment. Let me fix this properly by using the edit command.
Assistant
Now I can see the issue - line 1128 has the old assignment
cd.cloneGroups = dedupeResult.groupsthat needs to be removed. Let me delete line 1128.Assistant
Let me verify the function is now correct and also check that the call to
filterClonePairsWithSuppressedMembersis still there.Assistant
The function is now missing the
filterClonePairsWithSuppressedMemberscall. I need to add it back. Also, sincededupeOverlappingCloneGroupsdoesn't return a result with suppressed fragments, I should not filter pairs based on suppression from the overlapping deduplication. But I still need to filter pairs based on the strict subset deduplication.Let me fix the function properly:
Assistant
Let me verify the function is now correct.
Assistant
Now let me check if there are any other places where
groupClonesWithStrategyis called and if the code compiles correctly.Assistant
Let me check the other call to
groupClonesWithStrategy:Assistant
Alright, the code looks good. Now let me try to build the code to see if there are any compilation errors.
Assistant
There are two issues:
fileSigcan't be used as a map key because it contains a slice (which is not hashable)absIntis already defined inapted.go
Let me fix these issues. I need to:
- Change
fileSigto use a string key instead of a slice - Remove the
absIntfunction since it's already defined elsewhere
Let me first check if
absIntexists in apted.go:<tool_call> <function=bash_terminal> <parameter=command> grep -n "func absInt" internal/analyzer/apted.go </tool_call>
Assistant
Let me check the apted.go file for the absInt function and also view the current group_dedup.go file to fix the issues.
Assistant
Now let me fix the group_dedup.go file by removing the duplicate
absIntfunction and fixing the map key issue by using a sorted string instead of a slice.Assistant
Assistant
I need to fix the group_dedup.go file by replacing the dedupeOverlappingCloneGroups function and removing the duplicate absInt function. I'll need to add the "strings" import since I'm using strings.Join.
Let me rewrite the entire file with the fixes.
Assistant
Now let me try to build the code again to see if the compilation errors are fixed:
Assistant
The build succeeded. Now let me test the fix by running pyscn on the Fixit repository again to verify that the duplicate clone groups are now deduplicated:
Assistant
The fix is working - we now have 8 groups instead of 9 groups before. Let me verify that the duplicate clone groups (the ones with lines 298-315/317-334 and 299-315/318-334) have been properly deduplicated:
Assistant
The duplicate clone groups have been deduplicated! Now we only have one group for the
subTestduplication (group id: 7 with lines 299-315 and 318-334), whereas before we had two groups (id: 16 and id: 17) with the same fragments but slightly different window boundaries.Let me verify the fix more thoroughly by checking that all groups are correct and no legitimate duplicates were removed:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-cache && python3 -c "import json; data = json.load(open('.pyscn/reports/analyze_20260721_165230.json')); print('Total groups:', len(data['clone']['clone_groups'])); print('Group sizes:', [g['size'] for g in data['clone']['clone_groups']])" </tool_call>
Assistant
Let me verify the fix and run the existing tests to make sure I haven't broken anything:
Assistant
The existing deduplication tests pass. Now let me add a test for the new
dedupeOverlappingCloneGroupsfunction to verify it works correctly:<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-cache && go test ./... -count=1 2>&1 | head -100 </tool_call>
Assistant
Let me run all the tests to make sure the fix doesn't break existing functionality:
Assistant
All the analyzer tests pass. Now let me also run the complete test suite to ensure nothing else is broken:
Assistant
All tests pass. Now let me add a test case for the
dedupeOverlappingCloneGroupsfunction to ensure the fix is properly tested:Assistant
Now let me run the new tests to make sure they pass:
Assistant
All the new tests pass. Now let me run all the tests again to make sure everything still works:
Assistant
The fix is working correctly. Let me also verify the fix one more time by checking the Fixit output:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-cache && python3 -c "import json; data = json.load(open('.pyscn/reports/analyze_20260721_165230.json')); print('Total groups:', len(data['clone']['clone_groups'])); print('Clone groups:'); [print(f" Group {g['id']}: {g['size']} fragments, sim={g['similarity']:.4f}") for g in data['clone']['clone_groups']]" </tool_call>
Assistant
Let me verify the fix by checking the Fixit output one more time.
Assistant
Excellent! The fix is working correctly. Now we have 8 groups instead of 9, and the duplicate clone groups (which were describing the same duplication between the
subTest("lint")andsubTest("fix")blocks) have been properly deduplicated.Let me clean up and run a final verification to make sure everything is in order:
Assistant
Let me verify the final state of the changes by checking the git diff:
Assistant
Let me also check the changes to the clone_detector.go file:
Assistant
And let me check the changes to the test file:
Assistant
The changes look good. Let me run the full test suite one more time to make sure everything is working correctly:
Assistant
All tests pass. Let me now do a final verification by rebuilding the tool and running it against the Fixit repository to confirm the fix works as expected:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-cache && go build -o pyscn ./cmd/pyscn && curl -fsSL "https://codeload.github.com/Instagram/Fixit/tar.gz/HEAD" | tar -xz && ./pyscn analyze --json --no-open Fixit-*/ 2>&1 | head -30 </tool_call>
Assistant
Let me run the command to rebuild and test the fix.
Assistant
I've successfully fixed the issue. Let me verify that the duplicate clone groups have been removed:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-cache && python3 -c "import json; data = json.load(open('.pyscn/reports/analyze_20260721_165624.json')); groups = [g for g in data['clone']['clone_groups'] if any('smoke.py' in f['location']['file_path'] for f in g['clones'])]; print('Clone groups in smoke.py:'); print(json.dumps(groups, indent=2))" </tool_call>
Assistant
The fix is working correctly. Let me clean up and provide a summary.
Assistant
Summary
I have successfully fixed the issue where pyscn's clone detector was emitting two separate clone groups that describe the same duplication relationship with slightly different window boundaries.
Problem
The clone detector was reporting two duplicate clone groups:
- Group 16: fragments at lines 299-315 and 318-334 (without
withheader line) - Group 17: fragments at lines 298-315 and 317-334 (with
withheader line)
Both groups described the same duplication between the
subTest("lint")block andsubTest("fix")block insmoke.py, differing only by whether thewith self.subTest(...)header line was included.Solution
I added a new deduplication function that detects and removes near-identical clone groups where:
- Group 16: fragments at lines 299-315 and 318-334 (without
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.