Skip to content

duplicate_logic heuristic is scope-blind: same-named shadowed local helpers across different where-blocks flagged as duplicates #1498

Closed

Description

@squid-protocol

Summary

Found while verifying #1442 (haskell equation-form func_start, PR pending). detector.py's
intra-file duplicate-logic detector (coding_analysis, around the "INTRA-FILE ORPHAN & DUPLICATE
DETECTOR" comment) flags a function as duplicate logic purely by name collision across the whole
file:

func_names = [f.get("name", "") for f in functions]
func_name_counts = collections.Counter(func_names)
...
if func_name and func_name_counts[func_name] > 1:
    usage_status = 2  # 2 = Duplicate
    duplicate_count += 1

This is flat and scope-blind -- it has no awareness of lexical nesting/shadowing. Any language
where a generic local-helper name (go, loop, helper, etc.) is conventionally reused across
many different, unrelated where/let/nested-function scopes in the same file will have every
one of those legitimately-distinct functions misclassified as "duplicate logic", inflating
duplicate_logic/Tech Debt Exposure in the risk-scoring output.

Where this became visible

This bug is architecture-wide (not haskell-specific) but was invisible for haskell until now: PR
#1442 makes haskell's func_start detect where-clause local helpers for the first time (they
were previously undetectable at all -- see #1442's own issue body). Haskell idiomatically reuses
go as a local recursion helper name across many unrelated top-level functions in the same file;
once #1442 started detecting these, pandoc/Shared.hs alone went from 4 to 13 "duplicate" flags
-- 7 of which are distinct go helpers in 7 different where scopes, not real duplication.

Confirmed via the golden-master differential scan while preparing #1442's PR: Shared.hs's
"Duplicate Logic" signal jumped 4 -> 13 and its "Tech Debt Exposure" jumped ~6% -> ~25% purely
from this scope-blindness, not from any actual copy-pasted logic. The golden masters were
re-blessed to accept this as expected/explained drift from #1442's own intentional recall
improvement, but the underlying heuristic gap is real and worth fixing on its own.

Suggested fix direction

Scope the duplicate check to something narrower than "same name anywhere in the file" --
options include using each function's start/end line range plus enclosing indentation depth as a
secondary key (two same-named functions at genuinely different, non-overlapping indentation
scopes are very unlikely to be real duplicates), or comparing actual body content/hash rather than
name alone. Needs its own design pass -- this issue is scoped to describing the gap, not
prescribing the exact fix.

Scope note

This is a risk-scoring/metrics concern (duplicate_logic/orphaned_logic signal), not a
func_start extraction-accuracy concern -- unrelated to the extraction gauntlets (epic #813) or
tree-sitter accuracy audits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-engine

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions