Skip to content

api_declared_orphans tokenizes api lines with \b\w+\b, so a hyphenated or qualified name is never recognised as already-declared (#2731's double count returns for cobol and scheme) #2827

Closed

Description

@squid-protocol

detector.py's api_declared_orphans (#2731, the input to galaxyscope.py's orphan → api
conversion) decides whether the api rule already counted an unreferenced function by tokenizing
each api-matched line with \b\w+\b and testing the orphan's name for membership:

api_line_tokens.update(re.findall(r"\b\w+\b", code_lines[line_no - 1]))
api_declared_orphans = sum(1 for name in orphan_names if name in api_line_tokens)

\b\w+\b cannot produce a token containing -, :, ., ! or ?. Every function whose name
holds one is therefore invisible to the test, the overlap reads 0, and the conversion credits the
orphan a second time — which is exactly the double count #2731/#2734 was built to remove.

This is the same tokenizer family as #2754, where the census itself counted with \b\w+\b
and every hyphenated Scheme define, Verb-Noun PowerShell function, ::ns::proc and
Class::method read as an orphan regardless of use. #2754 fixed the census and left this
consumer on the old tokenizer.

Measured

Per-file over keyword-rosetta/data, engine fix/2823-export-list-declarations, comparing the
current \b\w+\b line tokenizer against _name_boundary_pattern(name) searched over the same
api-line text:

language api_declared_orphans today with a name-aware test why
cobol 0 9 PROBE-GLOBALS, PROBE-TEST, … — hyphenated paragraph names, 3 per file in a/b/c. Present on main today
scheme 0 9 (export probe-globals) — 3 per file in a/b/c
every other language (42) 292 292 unchanged; all-\w names

haskell is the control that shows the mechanism working correctly when the names cooperate: its
export list is module A (probeGlobals, probeTest, probeSafety) where, all three names are
plain \w, api_declared_orphans reads 3, and api_orphan_credit correctly stays 0.

Consequence

api_orphan_credit (= arch_api − raw_arch_api) over-reads for these languages, and it feeds
risk_api_exposure and risk_documentation. cobol's a.cpy/b.cpy/c.cpy carry
api_orphan_credit: 3 in keyword-rosetta's manifest today; under a name-aware test they are 0,
because PROBE-GLOBALS really is declared by the api rule that matched its own paragraph line.

Fix shape

Test the orphan's real name against the api-line text instead of tokenizing the line:

api_blob = "\n".join(api_lines)
api_declared_orphans = sum(1 for name in orphan_names if re.search(_name_boundary_pattern(name), api_blob))

_name_boundary_pattern is the same lookaround pair _is_orphan already uses, so the two halves
of the census agree about what an occurrence of a name is — which is the property #2754's fix
established and this line never inherited. The "no false positives are possible" argument in the
existing comment still holds verbatim: an orphan's name occurs exactly once in the whole file, so
a match on an api-matched line can only be its own declaration.

It needs a golden-master bless (cobol moves) and a keyword-rosetta re-bless of cobol's three
api_orphan_credit cells, which is why it is filed rather than folded into #2823.

Provenance

Found while doing #2823. Giving haskell and scheme a _visibility_export_list made scheme
produce orphans for the first time, and rosetta_audit.py --baseline-bin reported scheme's
api_orphan_credit 0 → 3 as the only cell moved outside the census. cobol's identical reading is
pre-existing and unrelated to that change. #2823 re-blesses scheme's cell against a ledger entry
naming this issue; both cells come back to 0 when this lands.

Part of #2812. Same tokenizer family as #2754; same double count as #2731/#2734.

Activity

  1. added
    bug
    metrics
    core-engine
    on Sep 6, 2026
  2. squid-protocol commented on Sep 6, 2026

    @squid-protocol
    OwnerAuthor

    The real corpus makes this much bigger than the control corpus does

    Same probe over language-crucible (all 24 languages that produce both orphans and api hits):
    4,983 → 5,122 api_declared_orphans, +139 across three languages. Every other language is
    unchanged, and every mover is a name shape \b\w+\b cannot tokenize:

    language today name-aware the names it cannot see
    zig 924 1001 @"?TryGetCurrent@Isolate@v8@@SAPEAV12@XZ", @"resume" — quoted identifiers (bun/napi.zig alone: 402 → 478)
    lua 67 125 bit.bor, bit.lshift, M.start, mt.__gc, cli.autoboot — dotted module members
    powershell 0 4 New-ScriptShim, Get-MachineArchitecture — Verb-Noun, i.e. every idiomatic PowerShell function name

    powershell is the clearest reading of the defect: api_declared_orphans is 0 for the whole
    language
    across the crucible, because a PowerShell function name essentially always contains a
    hyphen. The mechanism is not degraded there, it is entirely off.

    This is why the fix is filed on its own rather than folded into #2823 — it moves five languages
    (these three plus cobol and scheme on the control corpus), needs its own golden-master bless, and
    its own keyword-rosetta re-bless of cobol's three api_orphan_credit cells.

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