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.

clew/dominated_edges.py:210

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
lines 228–228anchors = _anchors(rows, identity)lines 229–229doomed: list[tuple[int, int, str]] = []lines 230–230tally: Counter = Counter()lines 231–231◇ for caller, callee, source, confidence in rowslines 232–232◇ if confidence != CALL_MATCH_FUZZY or source not in FANOUT_SOURCESlines 233–233continuelines 234–234◇ if callee not in identitylines 235–235continuelines 236–236name, qualified = identity[callee]lines 237–237anchored = anchors.get((caller, name))lines 238–238◇ if not anchoredlines 239–239tally["unanchored_kept"] += 1lines 240–240◇ elif qualified in anchoredlines 241–241tally["same_identity_kept"] += 1lines 242–242◇ elif any(frozenset((qualified, a)) in overrides for a in anchored)lines 243–243tally["override_kept"] += 1lines 244–244elselines 245–245tally["dominated_deleted"] += 1lines 246–246doomed.append((caller, callee, source))lines 247–247return doomed, tally

Requirements

REQ-DDB-PIPE-009

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.

clew/dominated_edges.py:341

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 deleted

1 more lines, not shown

Activity · 5 steps
lines 350–350conn = sqlite3.connect(str(db_path))lines 351–351trylines 352–352deleted = prune_dominated_fuzzy_edges(conn)lines 353–353conn.commit()lines 356–356return deleted

Requirements

REQ-DDB-PIPE-009

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).

clew/dominated_edges.py:305

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

Activity · 10 steps
lines 322–322trylines 323–325rows = conn.execute( "SELECT caller_rowid, callee_rowid, source, confidence FROM call_edges" ).fetchall()lines 326–326except sqlite3.OperationalErrorlines 327–327logger.info("call_edges: dominated-fuzzy suppression — no call_edges table, skipping")lines 328–328return 0lines 329–329identity = _identity_index(conn)lines 330–330doomed, tally = classify_dominated(rows, identity, _override_pairs(conn, identity))lines 331–331deleted = _delete(conn, doomed)lines 332–332_log_verdict(tally, deleted)lines 333–333return deleted

Requirements

REQ-DDB-PIPE-009