diagnostics.py

clew/diagnostics.py

A STRUCTURED ZERO IS EVIDENCE — what the build looked for and did NOT find.

accessor_corpus_size

int clew.diagnostics.accessor_corpus_size(sqlite3.Connection conn)

Count the DISTINCT memberdef names the accessor diagnostic searches.

clew/diagnostics.py:175

Show on the map →

Called by collect

Calls none

Source · 18 lines
def accessor_corpus_size(conn: sqlite3.Connection) -> int:
    """THE DENOMINATOR, and it reads `ACCESSOR_CORPUS_KINDS` rather than restating the
    predicate. A zero measured against the wrong corpus is worse than no denominator at
    all: it would report that thousands of names were searched when the search read a
    different set, which is a false reassurance rather than a missing one.

    @brief Size of the corpus the accessor diagnostic examined.
    @return Distinct name count.
    @version 1
    """
    placeholders = ",".join("?" * len(ACCESSOR_CORPUS_KINDS))
    row = conn.execute(
        f"SELECT COUNT(DISTINCT name) FROM memberdef WHERE kind IN ({placeholders})",
        ACCESSOR_CORPUS_KINDS,
    ).fetchone()
    return int(row[0]) if row and row[0] is not None else 0

Activity · 3 steps
lines 185–185placeholders = ",".join("?" * len(ACCESSOR_CORPUS_KINDS))lines 186–189row = conn.execute( f"SELECT COUNT(DISTINCT name) FROM memberdef WHERE kind IN ({placeholders})", ACCESSOR_CORPUS_KINDS, ).fetchone()lines 190–190return int(row[0]) if row and row[0] is not None else 0

Requirements

REQ-DDB-CONFIG-007

as_meta

dict[str, str] clew.diagnostics.BuildDiagnostics.as_meta(self)

Flatten to string values for build_meta persistence.

clew/diagnostics.py:122

Show on the map →

Called by none

Calls none

Source · 48 lines
    def as_meta(self) -> dict[str, str]:
        """Values are STRINGS, and the counts are non-negotiably strings, because
        `write_build_signature` DROPS falsy values: `0` vanishes and `"0"` survives. The
        whole requirement here is that a measured zero is recorded, so passing an int
        would reproduce the silent zero one layer down — in the persistence rather than
        in the detector. `coverage.as_meta` learned the same thing.

        The NAMED lists are allowed to be empty and therefore dropped, because their
        `_count` companion carries the measurement. That asymmetry is deliberate: a name
        list is evidence, a count is the claim, and only the claim must always be present.

        `accessor_families` NAMES ONLY THIS REPO'S (gh#352 half 3), while
        `accessor_families_count` keeps counting ALL of them and
        `accessor_families_external_count` says how many of that total are somebody else's. So
        the three keys still reconcile — first party + external == count — and a reader who
        sees `count: 9` beside an EMPTY name list learns the honest thing: nine were found and
        none are yours to declare. Measured across both public targets, that is exactly the
        state 11 of 11 families were in.

        @brief Diagnostics as build_meta values.
        @return Unprefixed key → string value.
        @version 3
        """
        ours = [f for f in self.accessor_families if not f.external_root]
        return {
            "accessor_families": ", ".join(
                f"{f.prefix}* ({f.keys} keys)" for f in ours[:MAX_NAMED]
            ),
            "accessor_families_count": str(len(self.accessor_families)),
            "accessor_families_first_party_count": str(len(ours)),
            "accessor_families_external_count": str(len(self.accessor_families) - len(ours)),
            "accessor_names_examined": str(self.accessor_names_examined),
            ## gh#385. The ACQUIRE side is what an operator declares, so that is what is
            ## named; the release half is derivable and would double the bytes to say it.
            "undeclared_lock_primitives": ", ".join(
                acquire for acquire, _release in self.undeclared_lock_primitives
            ),
            "undeclared_lock_primitives_count": str(len(self.undeclared_lock_primitives)),
            ## Capped like the alias list: a hint is a starting point, not an inventory.
            "undeclared_spawn_primitives": ", ".join(self.undeclared_spawn_primitives[:MAX_NAMED]),
            "undeclared_spawn_primitives_count": str(len(self.undeclared_spawn_primitives)),
            "unclaimed_event_aliases": ", ".join(self.unclaimed_aliases[:MAX_NAMED]),
            "unclaimed_event_aliases_count": str(len(self.unclaimed_aliases)),
            "event_vocabulary_source": self.event_vocabulary_source,
            "event_vocabulary_size": str(self.event_vocabulary_size),
        }

Activity · 2 steps
lines 145–145ours = [f for f in self.accessor_families if not f.external_root]lines 146–167return { "accessor_families": ", ".join( f"{f.prefix}* ({f.keys} keys)" for f in ours[:MAX_NAMED] ), "accessor_families_count": str(len(self.accessor_families)), "accessor_families_first_party_count": str(len(ours)), "accessor_families_external_count": str(len(self.accessor_families) - len(ours)), "accessor_names_examined": str(self.accessor_names_examined), ## gh#385. The ACQUIRE side is what an operator declares, so that is what is ## named; the release half is derivable and would double the bytes to say it. "undeclared_lock_primitives": ", ".join( acquire for acquire, _release in self.undeclared_lock_primitives ), "undeclared_lock_primitives_count": str(len(self.undeclared_lock_primitives)), ## Capped like the alias list: a hint is a starting point, not an inventory. "undeclared_spawn_primitives": ", ".join(self.undeclared_spawn_primitives[:MAX_NAMED]), "undeclared_spawn_primitives_count": str(len(self.undeclared_spawn_primitives)), "unclaimed_event_aliases": ", ".join(self.unclaimed_aliases[:MAX_NAMED]), "unclaimed_event_aliases_count": str(len(self.unclaimed_aliases)), "event_vocabulary_source": self.event_vocabulary_source, "event_vocabulary_size": str(self.event_vocabulary_size), }

Requirements

REQ-DDB-CONFIG-007 REQ-DDB-QUERY-011

collect

BuildDiagnostics clew.diagnostics.collect(Path|str db_path, Path doxyfile, Path|None patterns_path, Any|None wrappers, dict[str, str]|None event_tags, Path|dict|None lock_patterns=None, Path|dict|None thread_patterns=None)

Measure both discovery diagnostics against a freshly built database.

clew/diagnostics.py:202

Source · 51 lines
def collect(
    db_path: Path | str,
    doxyfile: Path,
    patterns_path: Path | None,
    wrappers: Any | None,
    event_tags: dict[str, str] | None,
    lock_patterns: Path | dict | None = None,
    thread_patterns: Path | dict | None = None,
) -> BuildDiagnostics:
    """CALLED WITH THE SAME INPUTS THE STAGES GOT, which is the whole correctness
    argument for re-deriving rather than carrying. `cli` hoists both patterns arguments
    into locals for exactly this reason — two separately-built expressions can drift into
    resolving different things and one local cannot.

    NO try/except AROUND THE DATABASE. This runs immediately after the same process built
    and wrote that file, so an unreadable one is a real build failure; swallowing it would
    turn "cannot tell" into a healthy-looking zero, which is the failure mode the whole
    module exists to remove. An unreadable DOXYFILE is different and is handled inside
    `classify_aliases`: a synthesised Doxyfile legitimately has no ALIASES, and zero of
    each is the true answer for it.

    @brief Collect this build's discovery diagnostics.
    @return The measured diagnostics.
    @version 1
    """
    writers, readers, _aliases = resolve_shared_key_patterns(patterns_path, wrappers)
    conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)
    try:
        families = detect_undeclared_accessor_families(conn, [*writers, *readers])
        examined = accessor_corpus_size(conn)
        ## gh#385, the LOCK layer's counterpart to the accessor hint. Re-derived from the same
        ## declaration the stage got, for the reason this function's docstring already gives:
        ## two separately-built expressions drift, one local does not.
        lock_pairs = detect_undeclared_lock_primitives(conn, load_lock_patterns(lock_patterns))
        ## Same re-derivation rule as the lock hint above: built from the declaration the
        ## stage got, not from a second expression that could resolve differently.
        spawn_names = detect_undeclared_spawn_primitives(
            conn, load_thread_patterns(thread_patterns)
        )
    finally:
        conn.close()
    _claimed, unclaimed = classify_aliases(doxyfile, event_tags)
    return BuildDiagnostics(
        accessor_families=tuple(families),
        accessor_names_examined=examined,
        undeclared_lock_primitives=tuple(lock_pairs),
        undeclared_spawn_primitives=tuple(spawn_names),
        unclaimed_aliases=unclaimed,
        event_vocabulary_source=SOURCE_BUILT_IN if event_tags is None else SOURCE_DECLARED,
        event_vocabulary_size=len(DEFAULT_EVENT_TAGS if event_tags is None else event_tags),
    )

1 more lines, not shown

Activity · 9 steps
lines 227–227writers, readers, _aliases = resolve_shared_key_patterns(patterns_path, wrappers)lines 228–228conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)lines 229–229trylines 230–230families = detect_undeclared_accessor_families(conn, [*writers, *readers])lines 231–231examined = accessor_corpus_size(conn)lines 235–235lock_pairs = detect_undeclared_lock_primitives(conn, load_lock_patterns(lock_patterns))lines 238–240spawn_names = detect_undeclared_spawn_primitives( conn, load_thread_patterns(thread_patterns) )lines 243–243_claimed, unclaimed = classify_aliases(doxyfile, event_tags)lines 244–252return BuildDiagnostics( accessor_families=tuple(families), accessor_names_examined=examined, undeclared_lock_primitives=tuple(lock_pairs), undeclared_spawn_primitives=tuple(spawn_names), unclaimed_aliases=unclaimed, event_vocabulary_source=SOURCE_BUILT_IN if event_tags is None else SOURCE_DECLARED, event_vocabulary_size=len(DEFAULT_EVENT_TAGS if event_tags is None else event_tags), )

Requirements

REQ-DDB-CONFIG-007