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 exploring the repository structure to understand the
Assistant
Let me explore the structure further to understand the codebase
Assistant
let me look at the clone detector and where this is applied:
Assistant
me look at the existing group dedup tests:
Assistant
me look at the CloneGroup definition in the analyzer:
Assistant
Now let me understand the issue better. The bug is:
- Two clone groups are emitted, each describing the same pair of fragments in smoke.py
- Group 6 has windows: line 299-315 and 318-334
- Group 14 has windows: line 298-315 and 317-334
- Each fragment in group 14 starts exactly one line earlier than in group 6 (the
with self.subTest(...)header line)
So we have two clone groups, each with 2 fragments, where:
- grp6.frags[0].location = smoke.py:299-315
- grp6.frags[1].location = smoke.py:318-334
- grp14.frags[0].location = smoke.py:298-315
- grp14.frags[1].location = smoke.py:317-334
These two groups describe the same logical cloning relationship between
subTest("lint")andsubTest("fix")blocks but with slightly shifted windows.In other words, between the two groups:
- grp6.frag1 (299-315) ⊂ grp14.frag1 (298-315): grp6.frag1 is a strict subset (in the same file)
- grp6.frag2 (318-334) ⊂ grp14.frag2 (317-334): grp6.frag2 is a strict subset (in the same file)
Assistant
Let me continue exploring the connected grouping
Assistant
Assistant
Let me look at the gf and gp helper functions
Assistant
Let me now look at the existing test to understand
TestDedupe_E2EThroughConnectedGrouping:Let me look at the integration test to understand the existing scenarios:Let me now reproduce the bug. First, let me clone the Fixit repo to reproduce:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[ls testdata/]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
Now let me see if pyscn is built and ready to use:
Assistant
Now let me build pyscn and run it on the Fixit repo.
Assistant
Let me set up Go paths
Assistant
Let me set HOME and GOPATH
Assistant
Now let me try building
Assistant
The build succeeded. Now let me check that the binary was created.
Assistant
Let me check the analyze options and run it on the Fixit repo to reproduce the bug.
Assistant
Let me run the analyze command on the Fixit repo.
Assistant
bug is reproduced - 10 clone groups. Let me look at the JSON output to see groups 6 and 14.
Assistant
Let me run without --select to reproduce the issue more accurately:
Assistant
With both the current binary and the original, the count is 8 groups. But the issue says there are 10. Let me check what version pyscn is:
Assistant
Let me check the issue more carefully. The issue says pyscn v1.22.9 was used and 10 clone groups were reported. The current built version shows 8 clone groups. There may be additional deduplication or filtering in the newer version. Let me look at the git log:
Assistant
Let me check the JSON output for the smoke.py file specifically:
Assistant
Let me look at all smoke.py groups:
Assistant
Let me check if there are groups with the larger windows in the current output:
Assistant
look at git history for related fixes:
Assistant
Let me check if there's a tag for v1.22.9:
Assistant
Let me write a reproducer that builds the scenario described in the issue:
Assistant
Let me write a test that simulates the exact scenario:
Assistant
I've successfully reproduced the bug! Now let me look at the actual Fixit smoke.py to understand the connected scenario:
Assistant
Let me look at where the IDs are assigned initially:
Assistant
look at how IDs are used downstream:
Assistant
Let me look at the star_medoid_grouping to see how IDs are sorted:
Assistant
me search for where IDs get reassigned:
Assistant
Let me look at the output formatter to see how IDs are computed:
Assistant
Let me look at clone_service.go to see how groups are converted:
Assistant
Now I have a clear understanding. Let me implement the fix. First let me see what the existing dedup function structure looks like:
Assistant
Now let me write the new function. Let me first clean up my test file:
Assistant
Now let me design and add the new function. Let me first plan it:
Assistant
Now I'll add the new function to group_dedup.go:
Assistant
Let me add the new function at the end of group_dedup.go:
Assistant
Now let me wire it into clone_detector.go:
Assistant
Let me check where the existing dedup is called:
Assistant
me look at the other place:
Assistant
Now let me update groupClonesWithStrategy to call the new dedup:
Assistant
me use replace instead:
Assistant
The sed command didn't work because of
\nhandling. Let me use Python instead:Assistant
Let me verify the change:
Assistant
Now let me build and run the tests:
Assistant
succeeded. Now let me write a test to reproduce the bug and verify the fix:
Assistant
The fix works! Let me also test some edge cases to make sure the implementation is correct:
Assistant
Let me fix the function to also drop nil groups:
Assistant
Let me re-run the edge case tests:
Assistant
Let me debug what's happening:
Assistant
I see - the function should always filter nil groups. Let me fix this:
Assistant
Looking at the test for the existing function, nil groups are dropped. Let me update my edge case test to match:
Assistant
check the test file:
Assistant
Let me debug what's happening:
Assistant
I need to also handle the n<=1 case to filter out nil groups:
Assistant
All tests pass. Let me run the existing tests to make sure I didn't break anything:
Assistant
All existing tests pass. Let me run a wider range of tests:
Assistant
analyzer tests pass. Let me also run the broader test suite:
Assistant
All tests pass! Let me rebuild pyscn and verify the fix on the actual Fixit repo:
Assistant
The number of groups went from 8 to 7, and duplication percentage stayed at 10.0%. Let me also check the JSON output to see what changed:Let me also check what changed in the JSON output:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /tmp/fixit-repro && ls -la .pyscn/reports/ | tail -5]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me run the command directly
Assistant
compare the groups in the new output vs the old output:
Assistant
Hmm, the api.py group disappeared in the new output. Let me investigate:
Assistant
Let me investigate further - the api.py group was lost. Let me check more carefully:
Assistant
Let me check the new output more carefully:
Assistant
api.py is completely missing from the new output! Let me debug what's happening:
Assistant
I need to run the test from the /pyscn directory:
Assistant
Let me trace through to see if there's something else happening:
Assistant
me add temporary logging to debug:
Assistant
me look at the api.py content to understand the duplication:
Assistant
Let me remove the debug output and think more carefully:
Assistant
I'm in /tmp/fixit-repro. Let me cd to /pyscn:
Assistant
verify the changes:
Assistant
No fmt/debug left. Now let me dig into why the api.py small group was being suppressed but the larger group wasn't showing up. Let me add debug print only to that specific scenario to investigate:Let me dig deeper - I need to understand why the larger api.py group isn't showing up. Let me add debug print just for the api.py groups:Let me add a temporary debug to trace what happens with api.py groups:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /pyscn && python3 << 'PYEOF' with open('internal/analyzer/group_dedup.go', 'r') as f: content = f.read()
Add temporary debug prints
old = '''\tsuppressed := make(map[*CloneGroup]struct{}) \tanyChanged := false \tfor i := 0; i < n; i++ { \t\tgi := groups[i] \t\tif gi == nil { \t\t\tcontinue \t\t} \t\tif _, ok := suppressed[gi]; ok { \t\t\tcontinue \t\t}'''
new = '''\tfmt.Printf("DEBUG dedupe: input has %d groups\n", n) \tfor k, g := range groups { \t\tif g == nil { \t\t\tfmt.Printf("DEBUG k=%d: nil\n", k) \t\t\tcontinue \t\t} \t\tfor _, f := range g.Fragments { \t\t\tfmt.Printf("DEBUG k=%d: %s:%d-%d\n", k, f.Location.FilePath, f.Location.StartLine, f.Location.EndLine) \t\t} \t} \tsuppressed := make(map[*CloneGroup]struct{}) \tanyChanged := false \tfor i := 0; i < n; i++ { \t\tgi := groups[i] \t\tif gi == nil { \t\t\tcontinue \t\t} \t\tif _, ok := suppressed[gi]; ok { \t\t\tcontinue \t\t}'''
Assistant
me run the python script directly
Assistant
Now I can see clearly:
- Input has 9 groups
- Group k=2: api.py:171-183, api.py:206-219 (the SMALLER api.py group)
- Group k=5: api.py:153-183, api.py:186-219 (the LARGER api.py group)
Both groups ARE present in the input! And my function correctly suppresses k=2 (smaller) because k=5 (larger) covers it.
But the output has 7 groups, and the api.py group is missing! Let me check the output...
Assistant
Let me add more debug to see what's happening:
Assistant
Let me check the most recent output:
Assistant
Let me check the previous dedup:
Assistant
Let me check the output more carefully:
Assistant
Let me check if api.py is in any of the output groups:
Assistant
Let me add more debug to see the final output of my function:
Assistant
My function returns 8 groups, but the JSON output has 7. The api.py group is being lost somewhere downstream. Let me check the service layer:
Assistant
Let me check the detectClonesWithLSH function:
Assistant
Let me check the LSH path:
Assistant
Let me check the convertCloneGroupsToDomain function:
Assistant
Let me check what comes after:
Assistant
Let me check the larger api.py group's similarity:
Assistant
Let me add debug to the conversion function:
Assistant
Let me check the filterCloneGroups function:
Assistant
Let me look at the latest output:
Assistant
Let me check the request:
Assistant
OK so the group threshold is req.GroupThreshold which defaults to DefaultType4CloneThreshold. Let me check that:
Assistant
Let me look at the actual filter being applied:
Assistant
Let me check the type thresholds:
Assistant
Let me check the clone type of the api.py larger group:
Assistant
I see - the api.py larger group is Type-3 which is filtered out by default. My function is suppressing the smaller group but the larger group is then filtered out, leaving nothing. Let me change the approach to keep the group with higher similarity instead of always keeping the larger one:
Assistant
Now let me remove the debug code from clone_detector.go and clone_service.go:
Assistant
Now I need to add the
groupBetterhelper function. Let me view and clean up the function:Assistant
Let me view the function and add the helper:
Assistant
Let me update the documentation and add the groupBetter helper:
Assistant
Now let me add the
groupBetterhelper at the end:Assistant
Let me check the syntax error:
Assistant
an extra closing brace. Let me fix this:
Assistant
me check clone_detector.go for fmt usage:
Assistant
fmt was already in use. My removal accidentally removed it. Let me add it back:
Assistant
Same issue with clone_service.go. Let me fix:Same issue with clone_service.go. Let me fix:
Assistant
succeeded. Let me run the test and check the api.py case:
Assistant
Now 8 groups (back to the original). Let me check the api.py group:
Assistant
The api.py group is back at sim=0.8866 (the smaller group, which has higher similarity). Let me run the full test suite:
Assistant
All tests pass. Now let me also test the original bug case (smoke.py:298-315 vs 299-315):All tests pass. Now let me also test the original bug case (smoke.py:298-315 vs 299-315):
Assistant
pass. Let me verify all existing dedup tests still pass:
Assistant
All existing tests pass. Let me also test the original bug scenario and other edge cases. Let me also create a comprehensive test file:
Assistant
Now let me think about edge cases and write a few more tests:
Assistant
All comprehensive tests pass. Now let me clean up the test files (since I shouldn't add new tests to the repository per the instructions):
Assistant
Wait, re-reading the instructions:
Assistant
Good, all tests pass. Let me also run a broader test:
Assistant
All tests pass. Let me view the final state of my changes:
Assistant
There's still a dbg4_test.go I forgot to remove. Let me remove it:There's still a dbg4_test.go I forgot to remove. Let me remove it:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[rm /pyscn/internal/analyzer/dbg4_test.go && cd /pyscn && git status]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
me run the command properly
Assistant
Now let me view the changes in detail.
Assistant
Let me also check the changes to group_dedup.go:
Assistant
changes look good. Let me also do a final test to verify everything is working:
Assistant
Let me check the output to see the smoke.py case:Let me check the output for the smoke.py case:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /tmp/fixit-repro && ls -t .pyscn/reports/ | head -2]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me run the command directly:Let me run the command directly]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /tmp/fixit-repro && ls -t .pyscn/reports/ | head -2]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
There seems to be an issue with the tool call. Let me try the command:There seems to be an issue with the tool call. Let me try the command:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[ls -t /tmp/fixit-repro/.pyscn/reports/ | head -2]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Now let me look specifically for the smoke.py case to verify the bug is fixed:
Assistant
smoke.py case is now correctly showing ONE group with the larger windows. The within-group dedup already suppressed the smaller fragments within that single group. The cross-group dedup I added handles the case when they're in separate groups.
Let me also verify the issue's exact reproducer works:
Assistant
The exact scenario from the issue is reproduced and fixed. Now let me clean up the temporary test file:Excellent! The exact scenario from the issue is now fixed. Let me clean up the temporary test:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[rm /pyscn/internal/analyzer/repro_issue_test.go && cd /pyscn && git status]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me try the command again:Let me try the command again]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[rm -f /pyscn/internal/analyzer/repro_issue_test.go && cd /pyscn && git status]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Something is wrong with the tool call. Let me try a simple command first:Let me try a simple command first]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[pwd]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
The temporary test file is removed. Let me run the full test suite one more time to confirm everything works:Now let me run the full test suite to confirm everything works:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[export HOME=/root && export GOPATH=/root/go && go test ./... 2>&1 | tail -20]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Tool is having issues. Let me try simpler:
Assistant
All tests pass. Now let me also verify the final state of my changes:All tests pass. Let me verify the final state of my changes:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[git status && git diff --stat]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
The diff is clean. Let me also view the final group_dedup.go to confirm everything is in order:Let me also view the final group_dedup.go to confirm everything is in order:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cat internal/analyzer/group_dedup.go | wc -l]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Looks good. Let me also check the rest:Let me also check the rest:]<]minimax[>[<tool_call> ]<]minimax[>[<invoke name "function_calls"> ]<]minimax[>[sed -n '280,395p' internal/analyzer/group_dedup.go]<]minimax[>[]<]minimax[>[ ]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
The implementation is complete. Let me also check the gofmt output to make sure the code is properly formatted:Now let me also verify gofmt is happy with the code:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[gofmt -l internal/analyzer/group_dedup.go internal/analyzer/clone_detector.go service/clone_service.go]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
All three files are properly formatted (gofmt -l returns 0 means no changes needed).
Now let me also run go vet to check for any issues:All files are properly formatted. Let me also run go vet to check for any issues:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[go vet ./internal/analyzer/ ./service/]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
go vet returns clean. Let me also run a final test to verify everything works:go vet is clean. Let me do a final comprehensive test:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[go test ./...]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
All tests pass. Let me also verify the Fixit smoke.py case is handled correctly:All tests pass. Let me also verify the Fixit smoke.py case is handled correctly:]<]minimax[>[<tool_call> ]<]minimax[>[<invoke name "function_calls"> ]<]minimax[>[]<]minimax[>[go build -o /tmp/pyscn ./cmd/pyscn && cd /tmp/fixit-repro && /tmp/pyscn analyze --json --no-open Fixit-bf8e1f55f8a606eae0f0c64bab575e84a2a608b3/ --select=clones 2>&1 | tail -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
The Fixit analysis shows 8 groups, which is the correct count. The original bug from the issue described groups 6 and 14 in pyscn v1.22.9 - those have been merged into single groups.
Let me now verify that the issue's exact case (the smoke.py fragments) is now correctly handled:Let me verify that the issue's exact case (the smoke.py fragments) is now correctly handled:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[ls -t /tmp/fixit-repro/.pyscn/reports/ | head -1]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Excellent! The fix correctly handles the case. The output now has only ONE group (id=5) with smoke.py fragments at 298-315 and 317-334. The smaller fragments (299-315 and 318-334) are not present because:
- The within-group dedup (dedupeStrictSubsetGroupMembers) suppressed them as strict subsets within the group.
Assistant
Assistant
Imports look clean. Let me also verify the full test once more and check the implementation:Imports look clean. Let me also verify the full test once more and check the implementation:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[go test ./internal/analyzer/ -run TestDedupe -v 2>&1 | tail -30]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Assistant
The bug is correctly fixed. Now let me clean up the temporary test file:Now let me clean up the temporary test file:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[rm -f /pyscn/internal/analyzer/repro_final_test.go && git status]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Now let me also make sure my fix doesn't break any other tests. Let me run the complete test suite one more time:Let me run the complete test suite one more time to be sure:
Assistant
All tests pass. Let me also verify the final state of my changes one more time:
Assistant
The fix is complete and all tests pass. Let me also verify my fix one more time:
Assistant
Assistant
Only the two expected files are modified. Let me also do a final summary of the fix.
Summary
The fix addresses the bug described in the issue:
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 /header line).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.