Skip to content

orphan census: \b anchors never match a name ending in a non-word char — every ruby foo?/foo! and C++ operator== is an unconditional false orphan #2777

Closed

Description

@squid-protocol

Found while implementing #2768/#2774 (2026-09-05, engine afc2ff40), and confirmed by an independent Gemini review of _is_orphan commissioned for that PR. This is not in the #2763–#2774 filing wave — it is a fourth, larger cause behind raw_state_slop_orphans, the corpus's third-weakest metric.

The anchors

_is_orphan builds its reference test as (detector.py:7025):

word = re.compile(r"\b" + re.escape(func_name) + r"\b")

func_name is correctly escaped. The anchors are not correct. Python's \b asserts a \w↔\W transition, so when func_name itself ends in a non-word character, the trailing \b demands that the next character be a word character — which it never is, because the name is followed by (, ;, whitespace or EOL.

>>> re.compile(r"\b" + re.escape("empty?") + r"\b").findall("x.empty?()\ndef empty?\n")
[]
>>> re.compile(r"(?<!\w)" + re.escape("empty?") + r"(?!\w)").findall("x.empty?()\ndef empty?\n")
['empty?', 'empty?']

Zero matches — not "misses the call", misses everything, including the declaration. So inside == 0 and outside == 0, the declaration discount fires (outside -= 1), and the function returns True:

inside  = len(word.findall(code_stream[start_idx:end_idx]))    # 0
outside = len(word.findall(...)) + len(word.findall(...))      # 0
if inside == 0:
    outside -= 1                                                # -1
return outside <= 0                                             # True, always

A function whose name ends in a non-word character is reported as an orphan unconditionally, no matter how heavily it is used.

Incidence

galaxyscope over language-crucible/data/<lang>, counting extracted functions whose name ends in \W, and how many of those were flagged usage_status = 1:

language functions name ends non-word flagged orphan examples
ruby 124 32 (25.8%) 32 / 32 (100%) image?, audio?, video?, create_and_upload!, find_signed!, custom_metadata=
cpp 1134 12 (1.1%) 12 / 12 (100%) operator==, operator<, operator(), Variant::operator=
scheme 63 0 0 —
lua 623 0 0 —

100% in both languages that have the shape, which is the signature of an unconditional failure rather than a measurement.

The affected population is a language's whole idiom, not a fringe: Ruby predicate (?), bang (!) and setter (=) methods, and every C++ operator overload. scheme's set!-style names score 0 here only because the crucible's scheme corpus extracts none — the rule shape is identical and would fail the same way.

Interaction with #2768

#2768 proposes deleting the len(func_name) > 3 guard at detector.py:1496. That guard is currently the only thing suppressing this bug for short non-word names (p?, x!, <<, []). Deleting it without fixing the anchors widens this bug's blast radius rather than narrowing it. The two must land together, anchors first.

Fix

word = re.compile(r"(?<!\w)" + re.escape(func_name) + r"(?!\w)")

Lookarounds assert the same thing \b was meant to assert — no adjacent word character — without depending on the character class of the name's own first and last character. For an all-\w name the two are exactly equivalent, so no currently-correct language moves.

Negative tests belong in the orphan-census suite: a ruby empty? called once (not an orphan), a ruby empty? never called (an orphan), a C++ operator== used in an expression, and a name-prefix guard (probe must not match probe_globals) to prove the lookarounds did not widen the test.

Blast radius

Golden-master movement confined to orphaned_logic / raw_state_slop_orphans / usage_status / risk_tech_debt in directory groups containing .rb and C++ files, all downward (false orphans disappear).

Part of #2669. Found under #2768/#2774; pairs with #2727 and #2754, which built this test and did not touch its anchors.

Activity

  1. added
    bug
    metrics
    core-engine
    on Sep 5, 2026
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-enginemetrics

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions