sharedkey_detect.py

clew/propose/sharedkey_detect.py

Detect key-in-the-NAME accessor families from CALL SITES.

above_threshold

bool clew.propose.sharedkey_detect.above_threshold(Pair pair)

Whether a candidate pair carries enough evidence to be worth measuring.

clew/propose/sharedkey_detect.py:288

Show on the map →

Called by _threshold_rejections, pair_families

Calls none

Source · 6 lines
def above_threshold(pair: Pair) -> bool:
    """@brief Apply the shared-key and call-site evidence floors to one pair."""
    sites = len(pair.writer.sites) + len(pair.reader.sites)
    return len(pair.shared_keys) >= MIN_SHARED_KEYS and sites >= MIN_PAIR_SITES

Activity · 2 steps
lines 290–290sites = len(pair.writer.sites) + len(pair.reader.sites)lines 291–291return len(pair.shared_keys) >= MIN_SHARED_KEYS and sites >= MIN_PAIR_SITES

Requirements

REQ-DDB-CONFIG-001

accessor_families

dict[str, Family] clew.propose.sharedkey_detect.accessor_families(Sequence[AccessorSite] sites)

Group accessor-shaped call sites into families by prefix.

clew/propose/sharedkey_detect.py:201

Source · 27 lines
def accessor_families(sites: Sequence[AccessorSite]) -> dict[str, Family]:
    """@brief Fold accessor call sites into per-prefix families."""
    acc: dict[str, dict] = {}
    for site in sites:
        split = split_accessor(site.callee)
        if split is None:
            continue
        bucket = acc.setdefault(
            split.prefix,
            {"verb": split.verb, "stem": split.stem, "keys": set(), "in": [], "out": []},
        )
        bucket["keys"].add(split.key)
        bucket["in" if site.in_scope else "out"].append(site)
    return {
        prefix: Family(
            prefix=prefix,
            verb=data["verb"],
            stem=data["stem"],
            keys=frozenset(data["keys"]),
            sites=tuple(data["in"]),
            out_of_scope=tuple(data["out"]),
        )
        for prefix, data in acc.items()
        if data["in"]
    }

Activity · 9 steps
lines 203–203acc: dict[str, dict] = {}lines 204–204◇ for site in siteslines 205–205split = split_accessor(site.callee)lines 206–206◇ if split is Nonelines 207–207continuelines 208–211bucket = acc.setdefault( split.prefix, {"verb": split.verb, "stem": split.stem, "keys": set(), "in": [], "out": []}, )lines 212–212bucket["keys"].add(split.key)lines 213–213bucket["in" if site.in_scope else "out"].append(site)lines 214–225return { prefix: Family( prefix=prefix, verb=data["verb"], stem=data["stem"], keys=frozenset(data["keys"]), sites=tuple(data["in"]), out_of_scope=tuple(data["out"]), ) for prefix, data in acc.items() if data["in"] }

Requirements

REQ-DDB-CONFIG-001

arities

frozenset[int] clew.propose.sharedkey_detect.Family.arities(self)

The distinct argument counts observed at this family's call sites.

clew/propose/sharedkey_detect.py:191

Show on the map →

Called by none

Calls none

Source · 5 lines
    def arities(self) -> frozenset[int]:
        """@brief Observed arities across the family's in-scope call sites."""
        return frozenset(site.argc for site in self.sites)

Activity · 1 steps
lines 193–193return frozenset(site.argc for site in self.sites)

Requirements

REQ-DDB-CONFIG-001

arity_consistent

bool clew.propose.sharedkey_detect.arity_consistent(Family family, frozenset[int] expect)

Whether every in-scope call site of a family has the expected arity.

clew/propose/sharedkey_detect.py:234

Show on the map →

Called by _family_rejections, candidate_pairs

Calls none

Source · 5 lines
def arity_consistent(family: Family, expect: frozenset[int]) -> bool:
    """@brief Apply the arity gate to one family."""
    return family.arities() == expect

Activity · 1 steps
lines 236–236return family.arities() == expect

Requirements

REQ-DDB-CONFIG-001

candidate_pairs

list[Pair] clew.propose.sharedkey_detect.candidate_pairs(dict[str, Family] families)

Every arity-consistent writer/reader pairing, thresholds not yet applied.

clew/propose/sharedkey_detect.py:258

Show on the map →

Called by _threshold_rejections, pair_families

Calls arity_consistent

Source · 25 lines
def candidate_pairs(families: dict[str, Family]) -> list[Pair]:
    """Split out from `pair_families` so the evidence thresholds can be reported
    on rather than applied invisibly: a family that passes arity and then falls
    below the shared-key floor used to be dropped here with no trace, which is the
    one thing a reader cannot recover from — on the demobot fixture that silently
    swallowed its ENTIRE data model, whose punchline is that exactly one key is
    both written and read.

    @brief Pair writer families with reader families sharing a stem.
    @version 1
    """
    readers = {
        fam.stem: fam
        for fam in families.values()
        if fam.verb == "get" and arity_consistent(fam, READER_ARITY)
    }
    pairs: list[Pair] = []
    for family in sorted(families.values(), key=lambda f: -len(f.keys)):
        reader = readers.get(family.stem)
        if family.verb != "set" or reader is None or not arity_consistent(family, WRITER_ARITY):
            continue
        pairs.append(Pair(family, reader, family.keys & reader.keys))
    return pairs

Activity · 8 steps
lines 269–273readers = { fam.stem: fam for fam in families.values() if fam.verb == "get" and arity_consistent(fam, READER_ARITY) }lines 274–274pairs: list[Pair] = []lines 275–275◇ for family in sorted(families.values(), key=lambda f: -len(f.keys))lines 276–276reader = readers.get(family.stem)lines 277–277◇ if family.verb != "set" or reader is None or not arity_consistent(family, WRITER_ARITY)lines 278–278continuelines 279–279pairs.append(Pair(family, reader, family.keys & reader.keys))lines 280–280return pairs

Requirements

REQ-DDB-CONFIG-001

covering_prefix

str clew.propose.sharedkey_detect.covering_prefix(str prefix, Sequence[str] active)

The active NamePrefixPattern whose prefix already covers a family.

clew/propose/sharedkey_detect.py:310

Show on the map →

Called by _judge

Calls none

Source · 10 lines
def covering_prefix(prefix: str, active: Sequence[str]) -> str:
    """CASE-SENSITIVE, matching `shared_key_edges._match_accessor`'s runtime
    `name.startswith(...)`. The build-time diagnostic lowercases both sides,
    which would wrongly report `STORE_SET_` as already covered by the
    built-in `DataModel_Set_` default.

    @brief Find the active pattern covering a family, if any.
    @version 1
    """
    return next((cover for cover in active if cover and prefix.startswith(cover)), "")

1 more lines, not shown

Activity · 1 steps
lines 319–319return next((cover for cover in active if cover and prefix.startswith(cover)), "")

Requirements

REQ-DDB-CONFIG-001

pair_families

list[Pair] clew.propose.sharedkey_detect.pair_families(dict[str, Family] families)

Pair arity-consistent writer families with their reader counterparts.

clew/propose/sharedkey_detect.py:299

Source · 5 lines
def pair_families(families: dict[str, Family]) -> list[Pair]:
    """@brief Match writer families to readers, keeping only well-evidenced pairs."""
    return [pair for pair in candidate_pairs(families) if above_threshold(pair)]

Activity · 1 steps
lines 301–301return [pair for pair in candidate_pairs(families) if above_threshold(pair)]

Requirements

REQ-DDB-CONFIG-001

split_accessor

Split | None clew.propose.sharedkey_detect.split_accessor(str name)

Split an accessor-shaped name into its family prefix and key.

clew/propose/sharedkey_detect.py:81

Show on the map →

Called by accessor_families

Calls _next_verb, _split_at

Source · 19 lines
def split_accessor(name: str) -> Split | None:
    """Takes the FIRST qualifying verb occurrence, not the last: in a
    hypothetical `A_SET_B_GET_C` the family is `A_SET_` writing the key
    `B_GET_C`, and a greedy match would invert writer and reader.

    @brief Decompose one callee name into an accessor family + key.
    @version 1
    """
    lowered = name.lower()
    found: Split | None = None
    index = 0
    while found is None and index >= 0:
        index = _next_verb(lowered, index)
        if index >= 0:
            found = _split_at(name, index)
            index += 1
    return found

Activity · 9 steps
lines 89–89lowered = name.lower()lines 90–90found: Split | None = Nonelines 91–91index = 0lines 92–92◇ while found is None and index >= 0lines 93–93index = _next_verb(lowered, index)lines 94–94◇ if index >= 0lines 95–95found = _split_at(name, index)lines 96–96index += 1lines 97–97return found

Requirements

REQ-DDB-CONFIG-001