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 repository structure and the issue. This is a Python code similarity/detection tool called "pyscn" (pyastgrep clone detector). The issue is about duplicate overlapping clone groups being emitted - the same duplication relationship between two code fragments is reported as two separate clone groups with windows shifted by one line.
Let me explore the repository structure first, then understand the clone detection logic, reproduce the issue, and fix it.
Assistant
This is a Go project with some Python components. Let me explore the structure more, especially the
internaldirectory which likely contains the clone detection logic.Assistant
Let me look at the analyzer directory since that likely contains the clone detection and grouping logic.
Assistant
The
group_dedup.gofile is very relevant. Let me look at it and also the clone detector and the grouping logicAssistant
Now let me look at
clone_detector.goto see how groups are formed and where dedup is appliedAssistant
Let me see the rest of clone_detector.go and look at the grouping strategies more carefully.
Assistant
Let me see the full clone_detector.go, it was truncated:
Assistant
Let me look at the rest of the clone_detector.go, especially the DetectClonesWithContext function and the grouping part.
Assistant
Let me see the rest of clone_detector.go and the grouping files:
Assistant
Now let me look at the grouping strategies to understand how groups are formed
Assistant
Let me look at the connected grouping and complete linkage grouping implementations.
Assistant
Let me look at the existing tests for group dedup and also the other grouping strategy
Assistant
Now let me also look at the complete_linkage_grouping.go to understand how groups are formed:
Assistant
Let me check the helper functions used across grouping files:
Assistant
Now let me look at helper functions used in grouping:
Assistant
Let me find the helper functions used across the grouping/dedup code:
Assistant
Let me look at the helper functions in star_medoid_grouping.go:
Assistant
Now I have a thorough understanding. Let me look at the test data for the grouping strategies test:
Assistant
Now I have a good understanding of the issue. Let me create a plan:
- The issue is about cross-group deduplication - two separate clone groups describe the same duplication with slightly shifted member windows.
- The existing
dedupeStrictSubsetGroupMembersonly handles within-group dedup (same-file subset members within a group). - I need to add a new cross-group dedup function that merges/suppresses groups whose members are covered by another group's members.
Let me first run the existing tests to make sure I have a baseline:
Assistant
Let me set up Go environment:
Assistant
Go is installed but the module cache isn't set up. Let me set GOPATH and download dependencies
Assistant
Good. Now let me run the existing dedup tests:
Assistant
We need GOCACHE too.
Assistant
Now let me implement the cross-group dedup. First, let me write the code change
Assistant
Let me see the rest of the file:
Assistant
Now let me implement the cross-group dedup. I'll add the new functions to
group_dedup.go:Assistant
Now I need to modify
groupClonesWithStrategyinclone_detector.goto call the new cross-group dedupAssistant
Let me see the current implementation and modify
Assistant
Now I'll modify
groupClonesWithStrategyto add the cross-group dedup call:Assistant
Let me check the state of the files - I'm confused about which file was edited
Assistant
Let me check the current state of clone_detector.go around line 1120 and group_dedup.go around line
Assistant
The edit to clone_detector.go didn't apply. Let me check group_dedup.go and fix clone_detector.go:
Assistant
I see - the edit went to group_dedup.go instead of clone_detector.go. Let me fix this. First, remove the misplaced function from group_dedup.go:
Assistant
Now let me edit clone_detector.go to update
groupClonesWithStrategy:Assistant
Now let me verify the code compiles:
Assistant
There's a syntax error. Let me check the area around line 177-183:
Assistant
I see the issue - the closing
}ofsortCloneGroupsis missing. Let me fix itAssistant
Let me see the exact content:
Assistant
I see - the closing
}forsortCloneGroupsfunction is gone. Let me fix it:Assistant
The edit was applied to the wrong file again. Let me restore both files from git and be more careful:
Assistant
Let me now carefully re-apply the changes, starting with group_dedup.go:
Assistant
Good. Let me scroll to the bottom to add the new functions
Assistant
Now let me add the new functions after line
Assistant
Now let me open clone_detector.go and modify
groupClonesWithStrategyAssistant
Let me navigate to `groupClonesWithStrategy
Assistant
Now let me edit
groupClonesWithStrategyin clone_detector.goAssistant
Now let me verify the code compiles:
Assistant
It compiles. Now let me run the existing tests:
Assistant
All dedup tests pass. Let me run the broader test
Assistant
Let me run all analyzer tests to ensure nothing is broken:
Assistant
All tests pass. Now let me write a test for the new cross-group dedup functionality:
Assistant
I made several typos. Let me rewrite the test:
Assistant
Let me remove the malformed test and re-add it properly
Assistant
Let me create the tests using a script
Assistant
Now let me append these tests to the test
Assistant
Now let me run the new tests
Assistant
All new tests pass. Let me run all tests one more time to ensure nothing is broken
Assistant
Let me also run the full test suite and check if the project builds completely:
Assistant
Let me also run the integration tests:
Assistant
There's a test failure. Let me check if this is pre-existing or caused by my change:
Assistant
The test passes without my changes. Let me restore my changes and investigate
Assistant
Let me look at the failing test
Assistant
Let me find the test
Assistant
Let me search using grep -n with proper escaping.
Assistant
Let me try a simpler
Assistant
Let me look at this
Assistant
Let me run the failing test with verbose output
Assistant
Let me run the full parent
Assistant
Let me look at the service layer to understand how it returns clone pairs:
Assistant
Let me look at how the service uses clone pairs and groups:
Assistant
Let me look at
filterClonePairs:Assistant
Let me write a quick debug script to understand what's happening
Assistant
Let me run this debug
Assistant
Let me rename the
Assistant
Let me instead write an in-package test to debug:
Assistant
Let me check if the service test was already failing before my changes. I already did this and it passed. Let me try a different approach - add some debug logging directly to the group_dedup.go function to trace what's happening
Assistant
Let me look at
DetectClonesWithLSHmore carefully in the context of the service:Assistant
Let me look at
DetectClonesWithContextmoreAssistant
Let me check the default
Assistant
Let me add temporary debug logging to understand what's happening:
Assistant
Let me add temporary debug
Assistant
Let me add temporary debug logging
Assistant
Now let me run the failing test to see the debug
Assistant
Now I see the issue! There are TWO groups (inner
ifblock and outer function def). The cross-group dedup is merging them incorrectly because the outer function def covers the inner if statement in the same file.Let me check what the default request values are
Assistant
Let me check
convertClonePairsToDomain:Assistant
Let me add debug to the service layer to trace what's happening:
Assistant
Let me add debug to the
Assistant
Let me run the test
Assistant
Let me check the domain clone type
Assistant
Let me check
Assistant
Let me check
newDefaultCloneRequestAssistant
Now I understand the issue. The cross-group dedup is too aggressive - it merges groups that describe DIFFERENT logical duplications (function defs vs if-statements). I need a tighter criterion.
The key insight: in the bug scenario, the overlapping fragments share 94% of their lines (17/18). In the test scenario, they share only 67% (4/6). I should add an overlap ratio threshold.
Let me first remove the debug logging and then fix the logic
Assistant
Let me check the current state of both files:
Assistant
Now let me update
group_dedup.goto add an overlap ratio threshold to prevent merging unrelated groups. Let me first update thegroupDominatesfunction to use a tighter criterionAssistant
Let me see the current functions:
Assistant
Now let me update
groupDominatesto use an overlap ratio threshold. This ensures we only merge groups that describe the SAME duplication with slightly shifted windowsAssistant
Let me clean up the
tightCoverfunction - the variable naming is confusingAssistant
Now let me re-apply the change to clone_detector.go:
Assistant
Let me edit
groupClonesWithStrategy:Assistant
Now edit the function to add cross-group dedup:
Assistant
Now let me build and test
Assistant
Let me run the analyzer tests:
Assistant
Now let me run the service test that was failing earlier:
Assistant
All tests pass. Let me run the full test suite:
Assistant
All tests pass. Let me also verify my new dedup tests still pass:
Assistant
All tests pass. Let me do a final review of the changes I've
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.