event_edges.py

clew/event_edges.py

Author-DECLARED event edges, recovered from doxygen xrefitem aliases.

_edges_for

list[tuple[int, int, str]] clew.event_edges._edges_for(dict[str, dict[str, set[int]]] topics)

Expand topic buckets into (emitter_rowid, handler_rowid, topic) triples.

clew/event_edges.py:269

Source · 17 lines
def _edges_for(topics: dict[str, dict[str, set[int]]]) -> list[tuple[int, int, str]]:
    """Every emitter of a topic is paired with every handler of it. This is the
    same honesty level as `shared_key_edges`: sharing a topic proves a POSSIBLE
    causal path, not that a specific emit reached a specific handler — no
    static reading of a decoupled bus can prove the latter. A topic with only
    emitters (or only handlers) yields no edge; those one-sided topics are
    reported by the caller as a diagnostic, since an event emitted but never
    handled is usually a real defect.
    """
    edges: set[tuple[int, int, str]] = set()
    for topic, bucket in topics.items():
        for erow in bucket[PRODUCER]:
            for hrow in bucket[CONSUMER]:
                edges.add((erow, hrow, topic))
    return sorted(edges)

Activity · 6 steps
lines 278–278edges: set[tuple[int, int, str]] = set()lines 279–279◇ for topic, bucket in topics.items()lines 280–280◇ for erow in bucket[PRODUCER]lines 281–281◇ for hrow in bucket[CONSUMER]lines 282–282edges.add((erow, hrow, topic))lines 283–283return sorted(edges)

Requirements

REQ-DDB-SCHEMA-009

_topic_roles

dict[str, dict[str, set[int]]] clew.event_edges._topic_roles(list[tuple[str, str, str, int, bool]] tagged)

Collapse decl/def duplicates into one rowid set per topic and role.

clew/event_edges.py:243

Source · 21 lines
def _topic_roles(
    tagged: list[tuple[str, str, str, int, bool]],
) -> dict[str, dict[str, set[int]]]:
    """Doxygen documents one function twice when it is declared in a header and
    defined in a .cpp, so the SAME logical emitter can carry the tag on two
    rowids. Collapse per (topic, role, name), preferring the definition row —
    but only ever among rows that carried the tag, never widening to untagged
    same-named functions.
    """
    grouped: dict[tuple[str, str, str], list[tuple[int, bool]]] = {}
    for topic, role, name, rowid, is_def in tagged:
        grouped.setdefault((topic, role, name), []).append((rowid, is_def))
    topics: dict[str, dict[str, set[int]]] = {}
    for (topic, role, _name), entries in grouped.items():
        defs = [r for r, is_def in entries if is_def]
        chosen = defs if defs else [r for r, _ in entries]
        bucket = topics.setdefault(topic, {PRODUCER: set(), CONSUMER: set()})
        bucket[role].update(chosen)
    return topics

Activity · 10 steps
lines 252–252grouped: dict[tuple[str, str, str], list[tuple[int, bool]]] = {}lines 253–253◇ for topic, role, name, rowid, is_def in taggedlines 254–254grouped.setdefault((topic, role, name), []).append((rowid, is_def))lines 255–255topics: dict[str, dict[str, set[int]]] = {}lines 256–256◇ for (topic, role, _name), entries in grouped.items()lines 257–257defs = [r for r, is_def in entries if is_def]lines 258–258chosen = defs if defs else [r for r, _ in entries]lines 259–259bucket = topics.setdefault(topic, {PRODUCER: set(), CONSUMER: set()})lines 260–260bucket[role].update(chosen)lines 261–261return topics

Requirements

REQ-DDB-SCHEMA-009

classify_aliases

tuple[dict[str, str], tuple[str, ...]] clew.event_edges.classify_aliases(Path doxyfile, dict[str, str]|None event_tags)

Split a target's xrefitem ALIASES into claimed event keys and unclaimed tags.

clew/event_edges.py:141

Show on the map →

Called by _declared_event_keys, collect

Calls none

Source · 37 lines
def classify_aliases(
    doxyfile: Path, event_tags: dict[str, str] | None
) -> tuple[dict[str, str], tuple[str, ...]]:
    """BOTH HALVES RETURNED, because both are answers. The claimed keys drive the
    import; the unclaimed tags are the only evidence a reader gets that this repo
    documents a bus in words the active vocabulary does not know.

    Split out of `_declared_event_keys` for gh#320 so the diagnostic can be reported
    as DATA and not only logged. It is pure apart from reading one file, which is what
    lets the stamp site re-derive it instead of threading a metadata value back out
    through the importer — the choice `cli` already documents for `key_alias_prefixes`.
    Re-deriving is only safe because nothing in the database can disagree with it: the
    unclaimed tags produce no rows, by definition.

    An unreadable Doxyfile is a correct negative, not an error — a synthesised Doxyfile
    (#33) legitimately has no ALIASES, and reports zero of each.

    @brief Classify a repo's ALIASES against the active event vocabulary.
    @return Claimed keys and unclaimed tag names.
    @version 1
    """
    vocabulary = event_tags if event_tags is not None else DEFAULT_EVENT_TAGS
    try:
        text = doxyfile.read_text(encoding="utf-8", errors="replace")
    except OSError:
        return {}, ()
    keys: dict[str, str] = {}
    unrecognised: set[str] = set()
    for tag, key in _ALIAS_RE.findall(text):
        role = vocabulary.get(tag.lower())
        if role in (PRODUCER, CONSUMER):
            keys[key] = role
        else:
            unrecognised.add(tag)
    return keys, tuple(sorted(unrecognised))

Activity · 14 steps
lines 162–162vocabulary = event_tags if event_tags is not None else DEFAULT_EVENT_TAGSlines 163–163trylines 164–164text = doxyfile.read_text(encoding="utf-8", errors="replace")lines 165–165except OSErrorlines 166–166return {}, ()lines 167–167keys: dict[str, str] = {}lines 168–168unrecognised: set[str] = set()lines 169–169◇ for tag, key in _ALIAS_RE.findall(text)lines 170–170role = vocabulary.get(tag.lower())lines 171–171◇ if role in (PRODUCER, CONSUMER)lines 172–172keys[key] = rolelines 173–173elselines 174–174unrecognised.add(tag)lines 175–175return keys, tuple(sorted(unrecognised))

Requirements

REQ-DDB-CONFIG-007

import_event_edges

None clew.event_edges.import_event_edges(Path db_path, Path doxyfile, dict[str, str]|None event_tags=None)

Import author-declared @emits/@handles event edges into shared_key_edges.

clew/event_edges.py:317

Show on the map →

Called by _build_stages

Tested by

2 teststest_edges_only_attach_to_rowids_that_carried_the_tag, test_repo_without_event_aliases_writes_nothing

Calls _declared_event_keys, _edges_for, _harvest_tagged, _report_one_sided, _topic_roles

Source · 42 lines
def import_event_edges(
    db_path: Path,
    doxyfile: Path,
    event_tags: dict[str, str] | None = None,
) -> None:
    """Recover the event graph the author already declared.

    Rows land as `edge_kind='event'`, `declared=1`,
    `source='shared_key_declared'`, `dispatch_mode='keyed'` (a topic-keyed bus)
    and `edge_triggered=1` (an event is edge-triggered by definition — the
    first time anything populates that column). Runs before
    `annotate_thread_boundaries`, so these edges also acquire
    crosses_thread/to_thread_id.

    @brief Import declared event edges.
    @version 2
    """
    keys = _declared_event_keys(Path(doxyfile), event_tags)
    if not keys:
        logger.info("event edges: no event xrefitem alias declared — skipping (correct negative)")
        return
    conn = sqlite3.connect(db_path)
    try:
        topics = _topic_roles(_harvest_tagged(conn, keys))
        edges = _edges_for(topics)
        conn.executemany(
            "INSERT OR IGNORE INTO shared_key_edges "
            "(writer_rowid, reader_rowid, key_name, edge_kind, declared, source, "
            " confidence, dispatch_mode, edge_triggered) "
            "VALUES (?, ?, ?, 'event', 1, 'shared_key_declared', 'high', 'keyed', 1)",
            edges,
        )
        conn.commit()
        _report_one_sided(topics)
        logger.info(
            "event edges: %d topics from %d declared alias(es) → %d edges",
            len(topics),
            len(keys),
            len(edges),
        )
    finally:
        conn.close()

1 more lines, not shown

Activity · 12 steps
lines 334–334keys = _declared_event_keys(Path(doxyfile), event_tags)lines 335–335◇ if not keyslines 336–336logger.info("event edges: no event xrefitem alias declared — skipping (correct negative)")lines 337–337returnlines 338–338conn = sqlite3.connect(db_path)lines 339–339trylines 340–340topics = _topic_roles(_harvest_tagged(conn, keys))lines 341–341edges = _edges_for(topics)lines 342–348conn.executemany( "INSERT OR IGNORE INTO shared_key_edges " "(writer_rowid, reader_rowid, key_name, edge_kind, declared, source, " " confidence, dispatch_mode, edge_triggered) " "VALUES (?, ?, ?, 'event', 1, 'shared_key_declared', 'high', 'keyed', 1)", edges, )lines 349–349conn.commit()lines 350–350_report_one_sided(topics)lines 351–356logger.info( "event edges: %d topics from %d declared alias(es) → %d edges", len(topics), len(keys), len(edges), )

Requirements

REQ-DDB-SCHEMA-009