Skip to content

orphan census: an export -f name line counts as a use, so a declared-public uncalled function can never be reported dead #2774

Closed

Description

@squid-protocol

Same pass as #2769–#2773 (2026-09-05, engine 3b3568e0, corpus main 6183c4b). raw_state_slop_orphans is the corpus's third-worst metric at 61%. #2754 fixed the census's reference test; #2768 covers the length guard in front of it. This is the third piece, and it is the one with real-world consequences: naming a function in an export statement clears its orphan flag.

Repro

keyword-rosetta/data/shell, whose a.sh defines three probes and calls none of them. Two scans, identical but for the three export -f lines:

                      raw_state_slop_orphans
with    export -f            a.sh 0   b.sh 0   c.sh 0
without export -f            a.sh 3   b.sh 3   c.sh 3

export -f probe_globals is a.sh's api plant — the only way a shell script publishes a function rather than a variable (#2730 widened the rule to reach it). It also names probe_globals on a line outside the function's span, which is exactly what _is_orphan counts:

outside = len(word.findall(code_stream[:start_idx])) + len(word.findall(code_stream[end_idx:]))
if inside == 0:
    outside -= 1          # the declaration itself
return outside <= 0       # detector.py:6995

One export line, one outside occurrence, flag cleared. The function is still never called.

Scope

Five languages in the corpus reach this shape because their api idiom names the function: assembly, powershell, ruby, shell, tcl — all sitting at raw_state_slop_orphans 0.25 against a 2.50 median (−90%). The ledger entry api-export-names-suppress-orphan-credit records the trade and its own text calls the outcome out: "a mention in an export statement is outside the span, so it will still clear the flag — the honest fix is a real call graph."

Outside the corpus it is worse, because the affected shape is the common one in library code:

  • export -f name (bash), Export-ModuleMember -Function name (PowerShell), module_function :name / public :name (Ruby), namespace export name (Tcl), __all__ = ["name"] (Python), export { name } (JS/TS), pub use module::name (Rust), a C header's prototype.
  • In every one of those, the export list is the file's public API — and the functions on it are precisely the ones with no in-repo caller, which is the definition of what the orphan census is looking for.

So orphaned_logic — and risk_tech_debt, which reads it — is systematically blind to exactly the population it is asked about. A library whose every function is exported and none called internally reports zero dead code.

The engine already computes the answer

detector.py computes api_declared_orphans for #2731's purpose (don't double-credit an orphan the api rule already counted), and it does so by intersecting orphan names with the tokens on lines the api rule matched:

for line_no in set(threat_locations["api"]):
    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)

threat_locations["api"] is the exact set of lines that should not count as uses. The fix is to consult it one step earlier:

# an occurrence on a line the api rule matched is a visibility declaration,
# not a use -- discount it the way the definition site is already discounted
api_lines = set(threat_locations.get("api", []))

and have _is_orphan skip occurrences whose line number is in that set. It needs the line index for each match, which code_stream[:start_idx].count("\n") gives cheaply, or a pre-computed line-offset table if the cost matters on large files.

Note the ordering consequence, which is a simplification rather than a complication: with this in place, api_declared_orphans stops being a correction applied downstream in galaxyscope.py's Contextual Baseline Fix and starts being the natural result — an exported, uncalled function reads as an orphan, and the conversion credits it once. #2731's already_public subtraction was compensating for a census that had already been fooled.

The design question this does not settle

_is_orphan is a textual name-recurrence test, not a call graph — #2727 said so and #2754 kept it deliberately. Export lines are the largest and most systematic category of non-call mention, but not the only one: a name in a dispatch table, a docstring, a test-only reference, or a string literal all clear the flag too. This issue proposes fixing the category the engine can already identify exactly, and leaving the general case where #2727 left it.

Corpus pairing

No authoring change. The re-bless moves raw_state_slop_orphans 0.25 → 3.25 in the five languages (every probe orphaned, then converted to api for the three imported files by the Contextual Baseline Fix, which is the correct reading of an imported library file) and risk_tech_debt follows. Ledger: api-export-names-suppress-orphan-credit retires — and with it the reason makefile .PHONY: probe_x and m4 m4_provide([probe_x]) were rejected as plants during the #2729 wave, so those two languages' api idioms become available again.

Part of #2669. Follow-up to #2727/#2754; pairs with #2731 and #2768.

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