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 understand the issue:
- The clone detector is emitting two separate clone groups that describe the same duplication relationship between the same two code fragments.
- The difference is that one group has windows shifted by exactly one line (the enclosing
with/header line). - This results in redundant clone groups that inflate the
clone_groupscount and duplication percentage.
Let me first explore the repository structure and understand the codebase.
Assistant
Let me explore the codebase to understand how clone detection and grouping works. The issue is about duplicate clone groups being generated. Let me first understand the structure and find where clone groups are generated and deduplicated.
<tool_call> <function=bash_terminal> <parameter=command> find /pyscn -name "*.go" | head -50 </tool_call>
Assistant
The previous bash command was not included in the proper format. Let me run it again properly.
Assistant
Let me explore the clone-related files to understand how clone detection works.
Assistant
Let me look at the clone grouping related files. The issue mentions that the same clone group is being emitted twice with windows shifted by one line. Let me look at the group_dedup_test.go first to understand how dedup is implemented.
Assistant
Now let me look at the actual deduplication implementation:
<tool_call> <function=bash_terminal> <parameter=command> cat /pyscn/internal/analyzer/connected_grouping.go </tool_call>
Assistant
I need to properly format the command with the function name and arguments.
Assistant
Now let me look at the group_dedup.go to understand the deduplication logic:
<tool_call> <function=bash_terminal> <parameter=command> find /pyscn -name "dedup.go" | xargs cat </tool_call>
Assistant
Let me run the command again properly with the function call format.
Assistant
Now I understand the code. The issue is about duplicate clone groups - two separate groups that describe the same duplication relationship but with shifted window boundaries. Let me understand the problem better:
- Group 6 has clones at lines 299-315 and 318-334
- Group 14 has clones at lines 298-315 and 317-334
The difference is that Group 14 includes the
with self.subTest(...)header line in each fragment, while Group 6 starts one line later (without the header).This means both groups are essentially describing the same clone relationship, just with different window boundaries. The issue is that these two groups are being treated as separate groups instead of being recognized as duplicates of each other.
Let me look at the code to understand how clone groups are being created and where the deduplication might be missing. The key is that the current deduplication logic only handles:
- Strict subset members within a single group (which was issue #416)
- But it doesn't handle the case where two separate groups describe the same underlying fragments
Let me check how groups are created and whether there's any deduplication across groups:
<tool_call> <function=bash_terminal> <parameter=command> cat /pyscn/internal/analyzer/grouping_strategy.go </tool_call>
Assistant
I need to use the correct format for the bash_terminal tool.
Assistant
Let me look at the clone_detector.go to understand the clone detection pipeline:
Assistant
Now let me look at how clone groups are generated. The issue is that there are two separate clone groups with slightly different window boundaries that describe the same duplication. Let me look at the grouping logic more closely.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "groupClones|CloneGroup|cloneGroups" /pyscn/internal/analyzer/clone_detector.go | head -80 </tool_call>
Assistant
Let me run the grep command properly.
Assistant
Let me look at how the clone groups are actually being created and if there's any deduplication happening between groups. The issue is that two groups with shifted windows are being treated as separate groups.
Let me look at the k_core_grouping.go to see if that's where groups might be generated:
Assistant
Now I need to understand the issue better. Let me look at the test file for clone integration to understand how clone groups are being tested:
<tool_call> <function=bash_terminal> <parameter=command> cat /pyscn/integration/clone_integration_test.go </tool_call>
Assistant
Let me run the command properly.
Assistant
Now I need to understand the issue better. Let me look at the test data to understand the test structure, and then try to reproduce the issue or understand the exact scenario.
The issue mentions that two separate clone groups are generated that describe the same duplication relationship, with member windows shifted by exactly one line. The two groups are:
Group 6:
- Clone 1: lines 299-315
- Clone 2: lines 318-334
Group 14:
- Clone 1: lines 298-315
- Clone 2: lines 317-334
The difference is that Group 14 includes the
with self.subTest(...)header line.The key insight is that the clone detector might be generating two separate groups because:
- The fragments at (299, 318) and (317, 318) are both considered valid clones
- But the grouping logic treats them as separate groups
Looking at the code, the current deduplication only happens within a single group - it suppresses strict subset members. But what if two separate groups are being created that describe the same duplication relationship?
Assistant
Let me run the command properly using the function call format.
Assistant
Now let me understand how clone groups are deduplicated. Looking at the issue, it seems like two separate clone groups are being generated that describe the same underlying duplication but with different window sizes.
The current deduplication logic (
dedupeStrictSubsetGroupMembers) only handles the case where fragments within the same group have a strict subset relationship. But the issue is about two separate groups that are "near-duplicates" of each other.Let me look at the complete link grouping and how groups are created:
<tool_call> <function=bash_terminal> <parameter=command> cat /pyscn/internal/analyzer/complete_linkage_grouping_test.go </tool_call>
Assistant
Let me run the command properly using the function call format.
Assistant
Assistant
Let me run the grep command properly using the function call format.
Assistant
Now let me understand the issue better. The problem is that two separate clone groups are generated that describe the same duplication, with slightly different window sizes. The fix should be to detect such "near-identical" groups and merge/suppress one of them.
Let me look at the group_dedup.go file more closely and understand what deduplication is happening:
<tool_call> <function=bash_terminal> <parameter=command> cat /pyscn/internal/analyzer/group_dedup.go </tool_call>
Assistant
I need to run the command properly using the function call format.
Assistant
Now I understand the issue. The problem is that two separate clone groups are being generated that describe the same duplication relationship, with member windows shifted by exactly one line (the enclosing
with/header line). The current deduplication only handles strict subset relationships within a single group, but not near-identical groups.Let me first check the existing test files and understand how to reproduce this issue:
Assistant
Let me create a test case that reproduces the issue. Based on the description, I need to create two clone groups where:
- Group 6: clones at lines 299-315 and 318-334
- Group 14: clones at lines 298-315 and 317-334
These groups describe the same duplication, just with different window boundaries.
First, let me understand the fragment structure better:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "gf|CodeFragment|clonePair" /pyscn/internal/analyzer/group_dedup_test.go | head -30 </tool_call>
Assistant
Let me run the grep command properly.
Assistant
Let me look at how
gfis defined - it's a helper function in the test file:<tool_call> <function=bash_terminal> <parameter=command> grep -n "func gf" /pyscn/internal/analyzer/clone_detector_test.go </tool_call>
Assistant
Let me search for the gf function definition.
Assistant
Let me look at this test file to understand the gf helper.
<tool_call> <function=bash_terminal> <parameter=command> cat /pyscn/internal/analyzer/grouping_strategies_test.go </tool_call>
Assistant
Let me run the command properly with the function call format.
Assistant
Now I understand the issue. Two separate clone groups are being generated that describe the same duplication relationship, with member windows shifted by exactly one line. The fix should detect such "near-identical" groups and merge/suppress them.
Let me first create a test case to reproduce the issue:
Assistant
Now I understand the issue. Looking at the bug report:
- Group 6: clones at lines 299-315 and 318-334
- Group 14: clones at lines 298-315 and 317-334
These are two separate groups that describe the same duplication relationship, just with shifted boundaries by one line (the
with self.subTest(...)header line).The current deduplication only handles strict subset relationships within a single group, not near-identical groups across multiple groups.
Let me first write a test case that reproduces this issue, then implement the fix.
Assistant
Now let me run this test to verify it fails (since the deduplication for near-identical groups doesn't exist yet):
Assistant
Let me set the go module cache and try again.
Assistant
Let me set all the necessary environment variables and try again.
Assistant
Now I've confirmed the issue - the test shows that two groups are being returned when only one should be. Now I need to implement the fix to deduplicate near-identical groups.
The fix should:
- After the existing strict subset deduplication, check if any two groups are "near-identical"
- If so, merge them or keep the one with higher similarity
Let me think about how to define "near-identical":
- Two groups are near-identical if they describe the same duplication relationship
- This can be detected when each member in one group is a strict subset of (or very close to) a member in the other group
For the specific case in the issue:
- Group A: (299-315, 318-334)
- Group B: (298-315, 317-334)
Group B's members are strict subsets of Group A's members:
- (298-315) covers (299-315)
- (317-334) covers (318-334)
Wait, actually looking at it again:
- Group A's fragments are SMALLER (start later, end same)
- Group B's fragments are LARGER (start earlier, end same)
Assistant
I need to use the function call format to run the command.
Assistant
Assistant
I need to view the file first before modifying it.
Assistant
Assistant
I need to use the proper format for the bash_terminal command with the function name and arguments.
Assistant
Now I need to call
dedupeNearIdenticalGroupsfrom thegroupClonesWithStrategyfunction in clone_detector.go. Let me update that file:<tool_call> <function=bash_terminal> <parameter=command> grep -n "groupClonesWithStrategy" /pyscn/internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me run the grep command properly using the function call format.
Assistant
Let me view the
groupClonesWithStrategyfunction and update it to calldedupeNearIdenticalGroups:<tool_call> <function=bash_terminal> <parameter=command> sed -n '1116,1135p' /pyscn/internal/analyzer/clone_detector.go </tool_call>
Assistant
Let me try running the sed command properly using the function call format.
Assistant
Now let me update the
groupClonesWithStrategyfunction to also calldedupeNearIdenticalGroups:Assistant
Now let me use the edit command to update the
groupClonesWithStrategyfunction:Assistant
Let me verify the changes and fix the implementation. First, let me check the current state:
Assistant
The sed command created duplicate code. Let me fix this by rewriting the function properly:
Assistant
I need to add the correct implementation. Let me check the file state and insert the correct code.
Assistant
Let me add back the correct
groupClonesWithStrategyfunction:Assistant
Let me verify the changes:
Assistant
Let me fix the malformed code by removing the extra lines and the misplaced comment:
Assistant
Let me verify the file is correct now.
Assistant
Now let me run the test to see if my fix works:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-build && cd /pyscn && go test -v -run TestDedupe_NearIdenticalGroups ./internal/analyzer/ </tool_call>
Assistant
I need to use the function call format to run the command.
Assistant
The test still fails. Let me debug my implementation. I need to check if
dedupeNearIdenticalGroupsis being called in the test flow. The test callsdedupeGroupswhich only callsdedupeStrictSubsetGroupMembers. I need to add the call todedupeNearIdenticalGroupsin the test helper.Let me check the test helper function and update it:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "func dedupeGroups" /pyscn/internal/analyzer/group_dedup_test.go </tool_call>
Assistant
Let me run the grep command properly using the function call format.
Assistant
Let me see the dedupeGroups function and update it to also call dedupeNearIdenticalGroups:
Assistant
Now I need to update the test helper to also call dedupeNearIdenticalGroups:
Assistant
Let me verify the change:
Assistant
Now let me run the test again to see if it passes:
Assistant
The tests pass. Now let me run all the existing tests to make sure I didn't break anything:
Assistant
Let me continue running all the tests to see the full output:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-build && cd /pyscn && go test ./internal/analyzer/ 2>&1 | tail -50 </tool_call>
Assistant
Let me run the command properly.
Assistant
All tests pass. Let me also run the integration tests to make sure the fix works end-to-end:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-build && cd /pyscn && go test ./integration/ 2>&1 | tail -50 </tool_call>
Assistant
I need to run the integration tests using the correct bash command format.
Assistant
The integration tests pass as well. Now let me run all the tests in the repository to make sure I haven't broken anything:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-build && cd /pyscn && go test ./... 2>&1 | tail -80 </tool_call>
Assistant
I need to run all the tests using the correct bash command format.
Assistant
All tests pass. Now let me verify my fix by checking the edge cases and ensuring the implementation is robust. Let me also look at my test cases to ensure they cover the issue properly:
Assistant
I see that my test case for
TestDedupe_NearIdenticalGroups_Symmetricmay not be correct. Let me check the logic again. In that test:- Group A: (100-150, 200-250)
- Group B: (99-151, 199-251)
Group B's fragments cover Group A's fragments:
- 99 <= 100 and 151 >= 150 (B1 covers A1)
- 199 <= 200 and 251 >= 250 (B2 covers A2)
So this is actually the same as the first test, where one group's fragments are strict subsets of the other. Let me update the test to be more accurate and also add more edge case tests.
Assistant
Let me check the exact line number and then update the test if needed.
Assistant
Looking at the test more carefully, I realize the issue. In my test:
- Group A: (100-150, 200-250)
- Group B: (99-151, 199-251)
Group B covers Group A because:
- B1 (99-151) starts before A1 (100) and ends after A1 (151 > 150)
- B2 (199-251) starts before A2 (200) and ends after A2 (251 > 250)
So actually this test case IS a strict subset case, not a symmetric overlapping case. Let me create a better test that handles a more complex scenario where the groups have overlapping but not strict subset relationships.
Actually, thinking about this more, the bug report describes a case where the two groups have fragments that are strict subsets of each other. The issue is that both groups are reported separately even though they describe the same duplication.
Let me reconsider the fix. The current implementation of
dedupeNearIdenticalGroupsworks by checking if all members of one group are strict subsets of corresponding members in another group. This should handle the reported issue.However, I realize that my test case
TestDedupe_NearIdenticalGroups_Symmetricmight not be testing what I intended. Let me update it to remove the incorrect comment and verify the test still passes:Assistant
Let me fix the comment in the test - it was misleading:
Assistant
Let me verify the changes:
Assistant
Let me fix the function name as well since the sed command didn't change it properly:
<tool_call> <function=bash_terminal> <parameter=command> sed -i 's/func TestDedupe_NearIdenticalGroups_Symmetric/func TestDedupe_NearIdenticalGroups_ShiftedBounds/' /pyscn/internal/analyzer/group_dedup_test.go </tool_call>
Assistant
I need to use the proper bash_terminal function call format.
Assistant
Let me also fix the comments to be more accurate:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '324,335p' /pyscn/internal/analyzer/group_dedup_test.go </tool_call>
Assistant
Let me use the correct bash_terminal format.
Assistant
Let me fix the comment to be more accurate:
Assistant
Let me verify the changes:
Assistant
The sed command duplicated the lines. Let me fix this by rewriting the test file properly:
Assistant
Now let me add back the correct test comment:
Assistant
Let me verify the changes and run the tests:
Assistant
Let me clean up the test file by rewriting the relevant section:
<tool_call> <function=bash_terminal> <parameter=command> wc -l /pyscn/internal/analyzer/group_dedup_test.go </tool_call>
Assistant
Let me check how many lines the test file has and clean it up properly.
Assistant
Let me look at the problematic area and fix it:
Assistant
Let me delete the duplicated comment lines:
Assistant
Now let me verify the test file is clean and run the tests:
Assistant
There's still a duplicate line. Let me fix this:
<tool_call> <function=bash_terminal> <parameter=command> sed -i '326d' /pyscn/internal/analyzer/group_dedup_test.go </tool_call>
Assistant
Let me run the command properly.
Assistant
Now let me verify the test is clean:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '320,355p' /pyscn/internal/analyzer/group_dedup_test.go </tool_call>
Assistant
I need to run the command properly using the bash_terminal function.
Assistant
I need to fix the test file - it's missing the function declaration line. Let me fix this properly.
<tool_call> <function=bash_terminal> <parameter=command> sed -i '327a func TestDedupe_NearIdenticalGroups_ShiftedBounds(t *testing.T) {' /pyscn/internal/analyzer/group_dedup_test.go </tool_call>
Assistant
Let me run the command properly using the bash_terminal function.
Assistant
Now let me verify the test is correct:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '320,360p' /pyscn/internal/analyzer/group_dedup_test.go </tool_call>
Assistant
I need to run the command properly. Let me try again.
Assistant
The test looks correct now. Let me run the tests to verify everything is working:
Assistant
Both tests pass. Now let me run all the tests to make sure nothing is broken:
Assistant
All tests pass. Now let me also add a test for the edge case where the groups have different files but still describe the same duplication relationship, to ensure the fix handles all cases properly.
Actually, let me think about this more carefully. Looking at the issue description again, I see that the problem is:
- Group 6 and Group 14 both describe the same duplication between two code fragments
- Group 14 includes the
with self.subTest(...)header line in each fragment - Group 6 starts one line later (without the header)
This means that for each corresponding pair of fragments:
- Group 14's fragment (298-315) covers Group 6's fragment (299-315)
- Group 14's fragment (317-334) covers Group 6's fragment (318-334)
So Group 6's fragments are strict subsets of Group 14's fragments. The current fix should handle this correctly.
Let me also verify that the fix works correctly in the full cloning flow. I need to check if there's a way to test this end-to-end. Let me look at the clone_integration_test.go to see how integration tests are structured:
<tool_call> <function=bash_terminal> <parameter=command> grep -A 30 "TestCloneDetectionIntegration" /pyscn/integration/clone_integration_test.go </tool_call>
Assistant
Let me run the grep command properly to see the test structure.
Assistant
Now let me verify the fix works by creating a simple end-to-end test case. Let me also review my implementation to make sure it handles all edge cases correctly.
Let me look at the current implementation of
dedupeNearIdenticalGroups:<tool_call> <function=bash_terminal> <parameter=command> grep -A 50 "func dedupeNearIdenticalGroups" /pyscn/internal/analyzer/group_dedup.go </tool_call>
Assistant
Let me run the grep command properly using the bash_terminal function.
Assistant
The fix looks correct. Let me now verify the complete implementation and make sure the fix is properly integrated.
Let me also review the
isNearIdenticalGroupfunction to ensure it's working correctly:<tool_call> <function=bash_terminal> <parameter=command> grep -A 40 "func isNearIdenticalGroup" /pyscn/internal/analyzer/group_dedup.go </tool_call>
Assistant
Let me run the grep command properly to see the
isNearIdenticalGroupfunction.Assistant
The implementation looks correct. The function checks if all members of one group are strict subsets of corresponding members in the other group, or vice versa.
Now let me verify that the fix is complete by reviewing the changes I made:
- Added
dedupeNearIdenticalGroupsfunction ingroup_dedup.go - Added
isNearIdenticalGroupfunction ingroup_dedup.go - Added
isStrictSubsetfunction ingroup_dedup.go - Updated
groupClonesWithStrategyinclone_detector.goto calldedupeNearIdenticalGroups - Updated
dedupeGroupstest helper ingroup_dedup_test.goto calldedupeNearIdenticalGroups - Added test cases
TestDedupe_NearIdenticalGroupsandTestDedupe_NearIdenticalGroups_ShiftedBounds
Let me now verify the changes are correct by reviewing the git diff:
<tool_call> <function=bash_terminal> <parameter=command> cd /pyscn && git diff --stat </tool_call>
- Added
Assistant
I need to use the bash_terminal function to see the git diff.
Assistant
Good, the changes look reasonable. Let me verify the actual diff content to make sure everything is correct:
<tool_call> <function=bash_terminal> <parameter=command> cd /pyscn && git diff internal/analyzer/clone_detector.go </tool_call>
Assistant
I need to run the git diff command using the bash_terminal function to see the changes.
Assistant
Good, the change in
clone_detector.golooks correct. It callsdedupeNearIdenticalGroupsafterdedupeStrictSubsetGroupMembersto deduplicate near-identical groups.Let me also verify the changes in
group_dedup.go:Assistant
The changes look correct. Let me also verify the test file changes:
Assistant
The changes look correct. Let me run all the tests one more time to make sure everything is working:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-build && cd /pyscn && go test ./... 2>&1 </tool_call>
Assistant
I need to run the test command properly.
Assistant
All tests pass! Let me also run the tests with the -count=1 flag to ensure they're not just using cached results:
<tool_call> <function=bash_terminal> <parameter=command> export GOPATH=/root/go && export GOMODCACHE=/root/go/pkg/mod && export GOCACHE=/tmp/go-build && cd /pyscn && go test -count=1 ./internal/analyzer/ 2>&1 | tail -20 </tool_call>
Assistant
I need to run the command properly using the bash_terminal function.
Assistant
Excellent! All tests pass with fresh cache. The fix is working correctly.
Let me also create a summary of the changes I made:
Summary of Changes
Problem
The clone detector was emitting 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). This resulted in redundant clone groups that inflated theclone_groupscount and duplication percentage.Solution
Added a new deduplication pass
dedupeNearIdenticalGroupsthat detects and removes clone groups that are near-duplicates of each other. Two groups are considered near-identical if:- They have the same number of members
Assistant
I need to submit the fix. Let me do that now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.