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 exploring the repository structure to understand the codebase.
Assistant
Let me understand the issue. The bug is about two separate clone groups that describe the same duplication relationship. The current
dedupeStrictSubsetGroupMembershandles subsets within a single group, but not the case where two whole groups are near-duplicates of each other.Let me look at the clone_detector to understand the full flow.
Assistant
Let me look at the grouping strategies and how groups are formed.
Assistant
Assistant
Now I understand the issue. Let me look at where
dedupeStrictSubsetGroupMembersis called to understand the full flow.Assistant
Let me look at the helper functions used in tests.
Assistant
Let me build the project and run the existing dedup tests to confirm the baseline.
Assistant
Good, build works now. Let me set these env vars persistently and run tests.
Assistant
Now let me write a temporary test to reproduce the issue described:
Assistant
I've reproduced the issue. Now let me understand how the duplication score/percentage is computed and how clone groups are serialized.
Assistant
Let me look at how the duplication score is computed to understand the relationship with clone groups.
Assistant
So the duplication percentage is computed from
TotalCloneGroups(group count) andLinesAnalyzed. Reducing the group count directly reduces the duplication score. Let me look at how group IDs are assigned and used in serialization.Assistant
I've confirmed the issue. Now let me implement the cross-group dedup. Let me first remove the repro test and then add the implementation.
Assistant
Now let me implement the cross-group dedup. Let me add the new function to
group_dedup.goand wire it intogroupClonesWithStrategy.Assistant
Now let me add the cross-group dedup function. I'll insert it after
dedupeStrictSubsetGroupMembers:Assistant
Now let me wire it into
groupClonesWithStrategy:Assistant
The file wasn't open. Let me open it first then use the replace command, or use sed directly.
Assistant
Build succeeds. Now let me re-add the repro test to verify the fix:
Assistant
The fix works. Let me test more edge cases to ensure correctness:
Assistant
Need to handle a nil suppressed map. Let me fix that:
Assistant
All edge cases pass. Now let me run the full existing test suite to make sure nothing broke:
Assistant
A service test failed. Let me investigate:
Assistant
Let me check if this test was already failing before my change:
Assistant
The test passes on original code. Let me run it with my change to confirm the regression:
Assistant
Let me debug this. Let me add temporary logging:
Assistant
Let me simplify the debug test by using the parser directly. Let me look at how the clone service extracts fragments.
Assistant
Let me rewrite the debug test to use the parser properly.
Assistant
The analyzer-level debug shows the pair survives. The issue must be in the service layer. Let me look at how the service calls detection:
Assistant
Let me look at
convertClonePairsToDomainto see if it filters pairs:Assistant
Let me check how the service builds the detector config from the request:
Assistant
The service config doesn't set
ReduceBoilerplateSimilarity(defaults to false), unlikeDefaultCloneDetectorConfig(). Let me reproduce with the exact service config:Assistant
The analyzer produces 1 pair but service shows 0. Let me check
newDefaultCloneRequestandShouldUseLSHWithPairEstimate:Assistant
The default request has
IgnoreLiterals: true. Let me check the default thresholds and re-run withIgnoreLiterals: true:Assistant
Let me add temporary debug prints to the service to see exactly what's happening:
Assistant
The analyzer produces 1 pair (sim=0.653), but
response.ClonePairsis empty due tofilterClonePairs(MinSimilarity=0.7). The original must produce a pair with sim≥0.7. Let me check the original behavior by reverting only the analyzer files (keeping the service debug):Assistant
Now I understand the root cause! On the original code, there are 2 pairs and 2 groups:
- Group 0: inner if-blocks (a.py:2-5 ↔ b.py:2-5), sim=0.745, Type-3
- Group 1: whole functions (a.py:1-6 ↔ b.py:1-6), sim=0.653, Type-4
The test passes because the inner-block pair (sim 0.745) survives the MinSimilarity=0.7 filter. My change collapses these (group 1 covers group 0), removing the inner-block pair.
The key distinction: in the issue, both groups have all members in the SAME file (intra-file duplication: lint block vs fix block, both in smoke.py). In the test, the groups are cross-file (a.py + b.py) — nested clones at different granularities that are intentionally reported.
So the cross-group dedup should only apply to single-file groups (intra-file duplication), which is exactly the issue's scenario. Let me restore my changes and refine:
Assistant
Now let me update
dedupeOverlappingCloneGroupsto only consider single-file groups. Let me view the current function:Assistant
Let me update the function to restrict to single-file groups. I'll modify the loop guards and add the helper:
Assistant
Now let me remove the temporary service debug prints and re-run the failing test plus my repro:
Assistant
The previously-failing test now passes. Let me re-add my repro test to confirm the issue is still fixed, then run the full suite:
Assistant
All edge cases pass. Now let me remove the repro test and run the full test suite:
Assistant
All tests pass. Now let me do an end-to-end validation by actually reproducing with a realistic Python file mimicking the smoke.py structure. Let me build the CLI and test:
Assistant
Let me try to fetch the actual Fixit smoke.py file to reproduce the exact scenario end-to-end:
Assistant
The file matches the issue exactly. Now let me run the actual pyscn analyze on it to confirm the fix end-to-end. First, let me reproduce on the ORIGINAL code to confirm the bug exists:
Assistant
The output might not be pure JSON. Let me check the analyze command output. Maybe
--jsonwrites to a fileanalyze.jsonrather than stdout (as the issue mentions "In the resultinganalyze.json").Assistant
Hmm, only 1 clone group total and none touching the smoke.py 290-320 range. That means with just smoke.py alone, the duplicate isn't detected (it needs the whole Fixit project with default config perhaps). The issue ran on the whole Fixit project. Let me run on the full Fixit project.
Actually, with only smoke.py, there's only one file. The two similar blocks (lint/fix) within smoke.py would form an intra-file clone. But it shows 1 group total and none in the 290-320 range. Maybe the default config requires more lines/nodes, or the grouping threshold filters it out.
Let me check the actual default config used by
pyscn analyze. Let me dump all clone groups from the single-file run.Assistant
With smoke.py alone, the grouping connects many fragments into one big group — the issue requires the full Fixit project. Let me download the full Fixit repo and run both original and fixed pyscn:
Assistant
With the full Fixit project, ORIG shows 8 clone groups and the smoke.py members (298-315, 317-334) are merged into ONE group (id=6), not two separate groups as in the issue. The exact reproduction depends on the pyscn version/config. Let me check the version and whether there's a config affecting grouping:
Assistant
In this repo's HEAD, the smoke.py windows (298-315, 317-334) are already merged into one group via within-group dedup (#416) because they're transitively connected. The issue's v1.22.9 had them as separate groups. My cross-group dedup handles the separate-groups case (confirmed by the repro test). Let me verify the fixed pyscn on the full Fixit project produces no regression (same or fewer groups):
Assistant
The fixed version produces 7 groups vs 8 in original — exactly one redundant group removed, as the issue describes. Let me compare the two group sets to confirm the removed group was a legitimate redundant-overlapping clone group:
Assistant
The removed group is
id=0: members('api.py', 171, 183), ('api.py', 206, 219)— both in api.py (single-file intra-file group). Let me verify there's a covering group that subsumes it:Assistant
Wait — the removed group id=0 was the ONLY api.py single-file group. Let me check what covered it. Let me look at ALL fixed groups containing api.py members:
Assistant
The removed api.py group has NO surviving covering group in the fixed output — this is a bug in my implementation. Let me instrument the dedup to see what's happening:
Assistant
Now I see: group i = {api.py:153-183, api.py:186-219} covers group j = {api.py:171-183, api.py:206-219}. So j is suppressed and i should survive. But the fixed output had NO api.py groups — so i was ALSO suppressed. Let me find what suppressed i:
Assistant
Let me check whether group id=5 (api.py:153-183, 186-219) is present in the ORIG output, and reconcile the IDs:
Assistant
The IDs differ between runs — Go map iteration is randomized, so group IDs/ordering vary per run. Let me check the actual debug run's output to see if the covering group (api.py:153-183, 186-219) survived:
Assistant
Group i=5 (api.py:153-183, 186-219) should survive but isn't in the output. Let me list ALL debug output groups to find it:
Assistant
Let me add comprehensive debug logging to the cross-group dedup to see the full input and output:
Assistant
Now I see it clearly. The cross-group dedup is correct — it kept the larger api.py group (id=2: 153-183, 186-219) in
cd.cloneGroups(XCROSS OUTPUT = 8 groups). But the final JSON has only 7 groups with NO api.py group. So the service layer removed it. Let me checkfilterCloneGroups:Assistant
The service's
filterCloneGroupsfilters byMinSimilarity. The larger api.py group (153-183, 186-219) likely has lower similarity and gets filtered — independent of my change. But I'm seeing run-to-run variation (8 vs 9 input groups). Let me check if the ORIGINAL binary is itself non-deterministic across runs:Assistant
ORIG consistently produces 8 groups with api.py = {171-183, 206-219} (the smaller). IDs vary but membership is deterministic. My dbg run showed 9 input groups. Let me run the dbg binary multiple times to check if the input count is stable:
Assistant
Now I understand the full picture. The key insight: the cross-group dedup must only collapse near-identical windows (windowing artifacts), NOT genuine nested clones. The api.py case (13-line block inside 31-line function, 42% overlap) is a genuine nested clone, while the issue's case (17-line vs 18-line, 94% overlap) is a windowing artifact.
Let me verify my analysis: ORIG keeps the smaller api.py group (higher sim, passes MinSimilarity filter) while the larger is filtered by the service. So I need a near-identical window criterion (high line-overlap ratio) to avoid collapsing genuine nested clones. Let me rewrite the dedup cleanly:
Assistant
Build succeeds. Let me re-add comprehensive repro tests (including the api.py-style genuine nested clone that should NOT collapse):
Assistant
All edge cases pass. The issue's case collapses to 1 group (keeping the higher-similarity cleaner window), while genuine nested clones are preserved. Let me remove the repro test and run the full suite:
Assistant
All tests pass. Now let me verify the end-to-end Fixit run with the fixed binary produces the same result as ORIG (no regression, and the api.py case preserved):
Assistant
Excellent! The fixed binary produces exactly the same 8 groups as ORIG (just different IDs due to Go map iteration) — the api.py genuine nested clone (
171-183, 206-219) is preserved with NO regression. The duplication % is unchanged.This is expected: on this repo HEAD, the within-group dedup (#416) already merges the smoke.py windows into one group, so the issue's two-separate-groups scenario doesn't occur here. My cross-group dedup correctly handles the case where they ARE separate (validated by the repro test simulating v1.22.9's behavior).
Let me do a final review of the complete diff:
Assistant
The diff is clean. Let me rename
groupCoversAugmentto a cleaner name since it's a generic Kuhn augmenting-path helper, and verify no leftover references to old function names:Assistant
Build and vet pass. Let me run a final verification with the repro test plus the full analyzer and service test suites:
Assistant
All tests pass. Let me check if golangci-lint is available and run it on the changed files (the repo has a
.golangci.yml):Assistant
golangci-lint isn't available. Let me run
go vetandgofmtchecks to ensure code quality:Assistant
All checks pass. Let me confirm no temporary test files are left behind and do a final git status check:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.