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 explore the repository structure first.
Assistant
The issue is about two separate clone groups that describe the same duplication relationship but with shifted windows. The existing dedup logic (
filterMaximalPerFile) only deduplicates members within a single group. The issue here is across two groups: group 6 and group 14 have members that are overlapping windows but in different groups.So we need to add a cross-group dedup step. When two groups have members that overlap (same file, overlapping ranges covering the same underlying fragments), we should merge/suppress the redundant group.
Let me look at the clone_detector.go around the dedup call to understand the flow.
Assistant
Let me look at the CloneGroup struct and related definitions.
Assistant
Let me write a reproduction test to confirm the bug at the unit level.
Assistant
The bug is reproduced. Now let me implement the cross-group dedup. Let me view the relevant part of group_dedup.go to integrate.
Assistant
Now let me implement the cross-group dedup. I'll modify the
dedupeStrictSubsetGroupMembersfunction and add helper functions.Assistant
Now let me modify the function to add the cross-group dedup step:
Assistant
There's a duplicate line. Let me fix it.
Assistant
Now let me add the helper functions at the end of the file.
Assistant
Now it keeps 1 group. Let me run the full existing dedup tests to ensure no regression.
Assistant
Now let me run the full analyzer test suite to check for regressions.
Assistant
Let me run the broader test suite (service, cmd, integration, e2e) to ensure no regressions.
Assistant
Let me check if this test failure pre-exists my change.
Assistant
My change broke a test. Let me examine it.
Assistant
Let me debug the actual scenario with a temporary test.
Assistant
DetectClonesInFilesis a package-level function (not method). Let me check.Assistant
The test uses
service.DetectClonesInFilesas a package-level function. Let me check how the test calls it —service.DetectClonesInFiles(ctx, req.Paths, req). But the method is on*CloneService. Let me check if there's a package-level varserviceof type *CloneService in the test.Look at line 99:
response, err := service.DetectClonesInFiles(ctx, req.Paths, req). Soserviceis a variable. Let me check.Assistant
Let me update my debug test to use the service correctly.
Assistant
Let me see the baseline (without my change) for this debug scenario.
Assistant
Let me re-read my modified function carefully.
Assistant
Let me check how the detector runs end-to-end so I can replicate at the analyzer level.
Assistant
Let me temporarily instrument
groupClonesWithStrategyto print analyzer-level groups.Assistant
Now I understand: there are 2 analyzer-level groups. The whole-function group (G1) covers the if/else body group (G0). My code suppresses G0 and keeps G1, but G1 gets filtered out downstream. Let me investigate the service filtering.
Assistant
Now I understand the conflict. Let me first revert the debug patch, then fix the heuristic: keep the higher-similarity group (the better clone), and treat redundancy symmetrically (either group covers the other).
Assistant
Now let me rewrite the cross-group dedup functions with the symmetric redundancy check and similarity-first preference.
Assistant
Now let me rewrite the
dedupeOverlappingGroupsfunction andgroupBetterto use symmetric redundancy and similarity-first preference.Assistant
Now update
groupBetterto be similarity-first.Assistant
Now the file is open at line 329. Let me run the replace command now.
Assistant
Now let me build and run the tests.
Assistant
Now the repro keeps 1 group (the higher-similarity group 6 windows). Let me run the service test that was failing.
Assistant
Now let me run the full service and analyzer test suites.
Assistant
Now let me run the broader test suite (all packages) to be thorough.
Assistant
All tests pass. Now I must remove my temporary repro test (the guidelines say not to add tests). Let me remove it and verify the build.
Assistant
Let me try to run the actual reproducer from the issue. First, build the pyscn binary.
Assistant
The report shows 8 groups now. Let me inspect the smoke.py clone groups specifically.
Assistant
The output differs from the issue's exact example — likely a different default grouping strategy. Let me check the default grouping mode and reproduce with strategies that produce separate groups.
Assistant
Let me check the CLI flags for grouping mode and try other strategies to see if the duplicate groups appear.
Assistant
Grouping mode is config-driven. Let me create a pyscn.toml with a different grouping mode (complete_linkage) to see if the overlapping groups appear separately, validating the fix end-to-end.
Assistant
Let me try running with a pyscn.toml that uses complete_linkage grouping. Let me create one in the Fixit dir.
Actually, the
--jsonflag... let me check how the analyze command outputs. The report went to /tmp/.pyscn/reports/. Let me set grouping mode via a config file. Let me check how the analyze command reads config — does it look for pyscn.toml in the target dir or cwd?Let me just write a pyscn.toml in /tmp (cwd) and run from there, or in the Fixit dir.
Actually, this is getting complicated and the exact end-to-end repro may depend on version. My unit-level reproduction is faithful to the issue's described mechanism. Let me instead verify the fix doesn't break the default behavior and that the bug mechanism is fixed via the unit test (already done).
But let me try complete_linkage to be thorough. Let me create a config.
Assistant
Hmm, only one report file (the earlier one). The new run maybe didn't generate a new file or used the same. Let me check the latest.
Assistant
The config may not have been picked up. Let me check for a
--configflag and how config is loaded.Assistant
Let me clear reports and re-run cleanly with complete_linkage, then compare baseline (without my fix) vs with fix.
Assistant
Reports go to
Fixit-HEAD/.pyscn/reports/. Let me parse the newest (complete_linkage run with my fix).Assistant
Now let me compare against the baseline (without my fix). Let me stash, rebuild, and run.
Assistant
The end-to-end reproducer on the current HEAD doesn't show the two separate groups (the grouping already merges them into a larger group), so the bug is latent in the current version. My unit-level reproduction directly models the issue's described mechanism and is fixed. Let me run
go vetand the full analyzer/service tests once more to confirm everything is clean.Assistant
Let me add an optimization to avoid O(G²) blowup on large repos: bucket groups by their file-multiset signature (a perfect same-file matching requires identical file multisets), so only same-signature groups are compared. Let me view the current function and refactor.
Assistant
The replace tool wants SEARCH/ REPLACE markers. Let me redo with proper markers.
Assistant
I need to navigate to the line first.
Assistant
Now let me apply the edit replacing the function body (lines 222-253) with the bucketed version plus the signature helper.
Assistant
Now I need to add the
stringsimport.Assistant
Let me clean up the double
groupFileSignaturecall in the bucketing loop.Assistant
Now let me re-add my repro test temporarily to verify the fix still works after refactoring, then remove it.
Assistant
Let me test a 3-group chain scenario and a cross-file 3-member case.
Assistant
All edge cases pass. Now let me remove the temporary repro test and run the full test suite once more.
Assistant
All tests pass. Let me check the CHANGELOG to see if an entry is conventional for bug fixes.
Assistant
I'll add a CHANGELOG entry under a new "Unreleased" section, following the repo's convention.
Assistant
Now let me run the linter to ensure code style compliance.
Assistant
Let me review the complete diff before submitting.
Assistant
Let me verify no stray test files remain in the repo and do a final full test run.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.