dominated_edges.py¶
clew/dominated_edges.py
DOMINATED-FUZZY SUPPRESSION (gh#26) — delete the name fan-out the AST layers publish when a stronger layer already named the real callee.
classify_dominated
tuple[list[tuple[int, int, str]], Counter] clew.dominated_edges.classify_dominated(list[tuple[int, int, str, str]] rows, dict[int, tuple[str, str]] identity, set[frozenset[str]] overrides)
The fan-out rows a non-fuzzy anchor dominates, and why each was spared or doomed.
Source · 40 lines
def classify_dominated(
rows: list[tuple[int, int, str, str]],
identity: dict[int, tuple[str, str]],
overrides: set[frozenset[str]],
) -> tuple[list[tuple[int, int, str]], Counter]:
"""Pure over its arguments so the rule can be tested without a built index —
the measurement scripts and the tests exercise this function, and the SQL
around it only supplies rows and deletes what comes back.
The tally counts every fan-out row in an anchored group under one of four
verdicts, so the log can state what was spared alongside what was deleted. A
pass that reports only its deletions gives a reader no way to see that the
same-identity and override exemptions actually fired.
@brief Decide which fuzzy fan-out rows a non-fuzzy anchor dominates.
@return (doomed edge triples, verdict counter).
@version 2
"""
anchors = _anchors(rows, identity)
doomed: list[tuple[int, int, str]] = []
tally: Counter = Counter()
for caller, callee, source, confidence in rows:
if confidence != CALL_MATCH_FUZZY or source not in FANOUT_SOURCES:
continue
if callee not in identity:
continue
name, qualified = identity[callee]
anchored = anchors.get((caller, name))
if not anchored:
tally["unanchored_kept"] += 1
elif qualified in anchored:
tally["same_identity_kept"] += 1
elif any(frozenset((qualified, a)) in overrides for a in anchored):
tally["override_kept"] += 1
else:
tally["dominated_deleted"] += 1
doomed.append((caller, callee, source))
return doomed, tally
Activity · 20 steps
Requirements
prune_dominated_fuzzy_call_edges
int clew.dominated_edges.prune_dominated_fuzzy_call_edges(Path db_path)
Pipeline stage: open the built database and run the suppression pass.
Source · 16 lines
def prune_dominated_fuzzy_call_edges(db_path: Path) -> int:
"""The Path-taking form every other stage in `_build_stages` has, kept separate
from the connection-taking rule above so a test can exercise the rule against a
hand-built `memberdef` without a real build.
@brief Run dominated-fuzzy suppression against a database path.
@return Rows deleted.
@version 1
"""
conn = sqlite3.connect(str(db_path))
try:
deleted = prune_dominated_fuzzy_edges(conn)
conn.commit()
finally:
conn.close()
return deleted1 more lines, not shown
Activity · 5 steps
Requirements
prune_dominated_fuzzy_edges
int clew.dominated_edges.prune_dominated_fuzzy_edges(sqlite3.Connection conn)
Delete AST name-fan-out call edges that a non-fuzzy anchor dominates (gh#26).
Called by prune_dominated_fuzzy_call_edges
Tested by 10 tests
test_a_resolved_anchor_from_any_layer_counts, test_caller_of_two_same_named_functions_keeps_both, test_declared_dispatch_fuzzy_row_is_never_deleted, test_fabricated_fanout_row_is_deleted, test_missing_call_edges_table_is_not_fatal, test_same_identity_row_is_kept, test_schema_without_definition_column_deletes_nothing, test_unanchored_fanout_is_left_alone, test_verdict_log_names_every_exemption, test_virtual_override_of_the_anchor_is_kept
Calls _delete, _identity_index, _log_verdict, _override_pairs, classify_dominated
Source · 31 lines
def prune_dominated_fuzzy_edges(conn: sqlite3.Connection) -> int:
"""Run as its own pipeline stage AFTER every call-edge layer, unlike the
self-edge guard which is embedded in Layer 3. The placement is the whole
reason this is a separate stage: an anchor may come from `macro_hop`,
`fnptr` or `declared_dispatch`, so judging the fan-out before those layers
have run would spare rows a later layer disproves. It still sits ABOVE
`extract_threads` and `mark_reachability`, which is free — deleting only
fuzzy rows cannot change a traversal that skips fuzzy rows.
Non-raising on a thin index: a database with no `call_edges` table at all
returns 0 rather than failing a build, matching how every other augmentation
stage treats a missing predecessor.
@brief Delete the dominated fuzzy fan-out rows from call_edges.
@return Rows deleted.
@version 1
"""
try:
rows = conn.execute(
"SELECT caller_rowid, callee_rowid, source, confidence FROM call_edges"
).fetchall()
except sqlite3.OperationalError:
logger.info("call_edges: dominated-fuzzy suppression — no call_edges table, skipping")
return 0
identity = _identity_index(conn)
doomed, tally = classify_dominated(rows, identity, _override_pairs(conn, identity))
deleted = _delete(conn, doomed)
_log_verdict(tally, deleted)
return deleted