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
I’ll first map the repository and locate the clone grouping/deduplication implementation and its tests. Then I’ll reproduce the overlapping-group behavior with the smallest practical fixture, implement a general group-level deduplication rule, and run the relevant test suite plus a focused reproduction.
- Assistant
The project is primarily Go, and the initial path assumptions missed its actual layout. I’ll now inspect clone-specific domain/internal/service files and existing integration expectations to identify where groups are formed versus merely formatted.
- Assistant
There is already a dedicated
group_dedup.goand tests for the earlier strict-subset-within-group issue, making this the correct layer for the new behavior. I’ll read that implementation, its full test coverage, and the detector call site so the fix preserves metadata, deterministic ordering, and pair consistency. - Assistant
The current pass only compares members inside each group; it cannot recognize two groups whose members correspond by containment. The required rule should compare whole groups and suppress a group only when there is a one-to-one, same-file containment mapping for every member (thus preserving genuinely distinct groups that merely share one fragment). Before editing, I’ll inspect all grouping call paths and helper semantics, especially pair keys and metadata calculations.
- Assistant
Both exhaustive and LSH flows converge on this pass. The reproducer is a uniform group-containment case: every member of group 6 is strictly contained by its counterpart in group 14. I’ll implement deterministic whole-group containment deduplication with bipartite matching (important when multiple members are in one file), retain the maximal group, collapse exact duplicate groups, and only filter pair fragments that do not remain in any retained group. This avoids conflating groups that overlap only partially or share only some members.
- Assistant
A key false-positive edge case is an outer cloned function containing a separately cloned inner loop: whole-group containment alone would wrongly erase a legitimate finding. I’ll therefore define “near-identical” using high line-window overlap (at least 90% of the enclosing window) for every one-to-one mapped member, while still requiring containment and identical member counts. The reported 17/18-line windows satisfy this; substantially nested clone relationships do not.
- Assistant
The implementation is now in place. I’ll run the analyzer package tests first; these heavily exercise grouping invariants and the existing #416 deduplication behavior, so they should quickly reveal ordering, metadata, or pair-filter regressions.
- Assistant
The test command is blocked only because this sandbox has neither
GOMODCACHEnorGOPATHconfigured. I’ll point both at writable temporary directories and rerun without changing repository configuration. - Assistant
Dependencies downloaded successfully; Go additionally requires an explicit build cache in this minimal environment. I’ll provide
GOCACHEand rerun. - Assistant
The analyzer suite passes. To satisfy the reported real-world reproduction without adding repository tests, I’ll build the CLI, download the pinned Fixit revision into
/tmp, run the same JSON analysis there, and inspect the resulting smoke.py groups before/after behavior. First I’ll confirm CLI output flags and build the binary. - Assistant
The CLI is ready. I’ll fetch exactly
bf8e1f55f8a606eae0f0c64bab575e84a2a608b3rather than HEAD, run clone analysis only for speed, and use Python to list all groups touching the two reported line windows. - Assistant
The fixed build reports 8 groups (the issue’s older release reported 10, so unrelated detector evolution changed the absolute count). I’ll inspect the exact smoke.py group ranges to verify that only the maximal 298–315 / 317–334 relationship remains and the shifted inner duplicate is absent.
- Assistant
The maximal windows remain, while 299/318 are absent, but current repository behavior may already differ from v1.22.9 enough that this alone does not prove the patch caused it. I’ll build pristine repository
HEADin a temporary worktree and analyze the same checkout, allowing a direct baseline-versus-fixed comparison without disturbing the working tree. - Assistant
The shell’s working directory persisted in
/tmp/fixit-run, so Git correctly could not find the pyscn repository. I’ll rerun from/pyscn. - Assistant
The pristine current HEAD also reports 8 groups, confirming the historical v1.22.9 reproduction has evolved and cannot be observed end-to-end at this revision; however, both runs establish that the patch preserves current Fixit output and the fixed output contains no redundant inner group. I’ll now run the full repository Go test suite to catch service/statistics and integration regressions.
- Assistant
All repository tests pass. One edge case remains: “one header line” should deduplicate even for short clones where 5/6 lines is below 90%. I’ll make the near-identical predicate accept either a one-line-or-less boundary expansion or ≥90% containment. This directly covers the bug shape at any valid clone size while the high-overlap fallback handles slightly broader windows. I’ll also clarify the top-level pass documentation.
- Assistant
To directly exercise the historical shape without adding or modifying repository tests, I’ll use Go’s
-overlayfeature: a temporary test file in/tmpis virtually presented to the analyzer package. It will verify the exact 298/299 and 317/318 ranges collapse to the maximal group, while deeply nested and merely partial-overlap groups remain distinct. - Assistant
The focused virtual tests pass: the exact reported duplicate collapses to group 14, and both deep nesting and partial overlap remain separate. I’ll now rerun the normal analyzer suite after the predicate adjustment, run
go veton the affected package, and inspect the final diff for unintended changes. - Assistant
Final verification is clean: standard analyzer tests pass, the focused reproduction passes,
go vetreports no issues, andgit diff --checkis clean. I’ll remove the temporary detached worktree reference and confirm that the submission contains only the intended source change. - Assistant
Only
internal/analyzer/group_dedup.gois modified. The fix performs one-to-one whole-group containment matching, keeps maximal windows, handles exact duplicates deterministically, avoids suppressing nested/partial findings, and keeps pair filtering consistent with fragments retained elsewhere. I’m submitting the verified change now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.