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
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
Requirements
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
Requirements
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
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
Requirements
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
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
Requirements
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
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
Requirements
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
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
Requirements
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
Requirements
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
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