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 codebase.
Assistant
Let me explore the structure more. The clone detection logic is likely in Go (go.mod present). Let me look at the relevant directories.
Assistant
There's a
group_dedup.goandgroup_dedup_test.go. Let me examine these and the clone detector.Assistant
Let me look at the clone detector and how dedup is called in the pipeline.
Assistant
Let me look at how clone pairs are formed and how groups are formed. The issue is that two separate groups are formed for the same duplication. Let me understand the grouping pipeline.
The issue: Two clone groups describe the same pair of fragments, with members shifted by one line (the header). Group 14: members (298-315) and (317-334). Group 6: members (299-315) and (318-334).
Assistant
Assistant
So fragments include nested
withstatements. The outerwith self.subTest("lint"):is aNodeWith(298-315), and the innerwith TemporaryDirectory()is aNodeWith(299-315). Both are fragment candidates, both pass size requirements. So we have two overlapping fragments in the same file. That's the root.Now there's
isOverlappingLocationthat prevents direct pairs between overlapping same-file fragments. So (298-315) won't pair with (299-315). But each can pair with a non-overlapping target.The example has both blocks in the SAME file (smoke.py). Group 14: (298-315) and (317-334) — same file, non-overlapping. Group 6: (299-315) and (318-334) — same file, non-overlapping.
So we get two pairs and two groups. The cross-group dedup is needed.
Now, where to add this? The cleanest place is in
dedupeStrictSubsetGroupMembers(or a new function called after it). Let me add a cross-group dedup pass.Let me look at the CloneGroup structure and the existing dedup tests to understand conventions.
Assistant
I notice that the domain
CloneGrouphasClones []*Clone, but the internal analyzer uses aCloneGroupwithFragments []*CodeFragment. Let me check the internal analyzer's CloneGroup definition.Assistant
Now let me look at the existing group_dedup_test.go to understand test conventions and how groups/clones are constructed.
Assistant
Let me look at the helper functions
gfandgpused in tests.Assistant
Let me check how groups get IDs assigned and where dedup fits in the pipeline.
Assistant
Let me look at the helper functions
pairKey,averageGroupSimilarity,fragmentLess,almostEqual.Assistant
Let me look at
shouldCompareFragmentsand how the analyze path invokes grouping.Assistant
Let me find how the analyze path wires up grouping and how final group IDs / output are produced.
Assistant
Now let me check
convertCloneGroupsToDomainandfilterCloneGroupsandsortResultsto see how IDs are assigned.Assistant
Let me get the actual smoke.py from Fixit to understand the exact structure, then build pyscn and reproduce.
Assistant
Good. The structure matches. Let me check the exact line numbers.
Assistant
Exact match. Now let me build pyscn and reproduce.
Assistant
Now let me reproduce on the smoke.py file. Let me create a minimal reproducer directory.
Assistant
Where does pyscn write the JSON? Let me check.
Assistant
With just smoke.py, only 1 group. The issue requires the full repo context. Let me run on the full Fixit repo.
Assistant
8 groups here, not 10. The issue says 10 groups (pyscn v1.22.9). Version difference. Let me check the clone groups in smoke.py specifically.
Assistant
In this version, the smoke.py lint/fix blocks merged into one big connected group (group 3) rather than two separate groups. The exact reproducer from v1.22.9 doesn't reproduce in current HEAD because transitive grouping now pulls them together via other fragments. But the underlying bug—cross-group redundancy where one group's members are overlapping windows contained by another group's members—is still a real gap in the dedup logic. Let me verify with a unit-level reproducer that the current dedup does NOT collapse two near-identical groups.
Let me check the existing tests and the grouping mode, then write a quick reproducer test.
Assistant
Let me write a temporary reproducer test to confirm the current behavior.
Assistant
Confirmed: the current dedup keeps both groups. The within-group
filterMaximalPerFiledoesn't help because each group's two members are disjoint. Now let me check how the duplication percentage is computed to know whether I also need to filter pairs.Assistant
Let me find where the analyze summary's
CloneGroupsandCodeDuplicationare computed from the clone response.Assistant
Now I understand the full pipeline. The duplication percentage is driven by
TotalCloneGroups(group count). The fix: add a cross-group dedup pass that suppresses redundant groups whose members are contained (file-by-file) by another group's members, keeping the maximal representative. Let me implement it ingroup_dedup.go.Assistant
Now let me implement the cross-group dedup. First, let me update the imports and the main function to call the new pass.
Assistant
Now let me add the cross-group dedup pass into the main function and append the new helper functions. Let me edit the import line first.
Assistant
Now let me build and run my reproducer test plus the existing dedup tests.
Assistant
The reproducer now collapses to 1 group, keeping the maximal (group 14, the outer windows). Now let me remove the temp test and run the full existing test suite for the analyzer package.
Assistant
All analyzer tests pass. Now let me add proper test cases to the existing
group_dedup_test.go(the issue says I shouldn't add tests... wait, the instructions say not to modify existing tests or add new tests to the repository). Let me re-read.The instructions say: "UNDER NO CIRCUMSTANCES should you modify existing tests or add new tests to the repository." So I should NOT add tests. Let me remove my temp test (already done) and not add new ones. Let me verify the git status to confirm only source changes.
Assistant
Only
group_dedup.gois modified. Now let me run the broader test suites that exercise clone detection (service, app) to ensure nothing breaks.Assistant
A service test failed. Let me check whether this test was already failing before my change (by stashing my changes).
Assistant
My change broke that test. Let me examine the test to understand what's happening.
Assistant
The test failure is unexpected for a single-group scenario. Let me debug by replicating the scenario and inspecting groups/pairs.
Assistant
No fragments extracted via that API. Let me use the real service path like the test does.
Assistant
Let me run with the actual test's
MinSimilarity(0.7) both before and after my change.Assistant
This is confusing — the behavior differs in surprising ways. Let me debug the internal detector state directly using the real fragment-extraction path.
Assistant
Let me write a debug test in the analyzer package replicating the real flow and inspect the groups/pairs at each stage.
Assistant
The behavior depends on config (classifier, IgnoreLiterals). Let me check
NewCloneDetectordefaults and how the classifier is wired, to understand the service path.Assistant
Let me replicate the exact service config in the analyzer debug (IgnoreLiterals=true etc.) and compare BEFORE/AFTER at the detector output level.
Assistant
Now I understand the root cause fully. BEFORE my change, the connected grouping produces two separate groups
{if_a, if_b}(sim 0.7455) and{def_a, def_b}(sim 0.6533), because the cross-file pairs (def↔if) fall below threshold. Each group's members are in different files so within-group dedup keeps both. The existing test relies on the higher-similarity inner (if) group surviving the MinSimilarity filter.My fix kept the larger-span (outer
def) group, which has lower similarity and gets filtered downstream — breaking the test. The correct choice is to keep the higher-similarity representative and suppress the redundant lower-similarity one. Let me rewrite the cross-group dedup accordingly.Assistant
Now let me rewrite
dedupeOverlappingGroupsto keep the higher-similarity representative and use bidirectional redundancy.Assistant
Now the higher-similarity inner (
if) group survives, and the lower-similarity outer (def) group is suppressed — matching the original behavior. Let me re-run the original reproducer (issue scenario) and the service tests.Assistant
The issue scenario collapses to 1 group (keeping the higher-similarity inner group 6). Disjoint and partial-overlap groups are both kept (no false suppression). Now let me remove the temp test files and run the full test suites.
Assistant
All pass. Let me run the full test suite (all packages) to be thorough.
Assistant
All tests pass. Now let me rebuild pyscn and verify end-to-end on the Fixit repo that nothing regresses and the smoke.py clone groups are reasonable.
Assistant
The full-repo output is unchanged (8 groups, same smoke.py groups) — no regression. The fix is in place for the general cross-group redundancy case. Let me check the CHANGELOG convention to add an entry.
Assistant
Let me add a CHANGELOG entry following the existing convention.
Assistant
Now let me review the final diff and run the analyzer dedup tests specifically to confirm.
Assistant
Let me ensure gofmt formatting is clean.
Assistant
Let me run the analyzer and service tests fresh (count=1) to be sure after formatting.
Assistant
All tests pass. Let me do a final review of the checklist and verify the git status is clean of temp files.
Assistant
All changes are clean. Let me do a final full-suite run to confirm everything is green before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.