From 9ca93c65a6c224679e035964efc669c74b0c84a7 Mon Sep 17 00:00:00 2001 From: kert Date: Fri, 13 Nov 2026 03:07:31 -0500 Subject: [PATCH] test(llm): live CPT-run guard checks against a plain frozenset (3.8 min to 2 s) (S1) --- tests/llm/test_policy_live.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/llm/test_policy_live.py b/tests/llm/test_policy_live.py index 717d408..a3eb4cb 100644 --- a/tests/llm/test_policy_live.py +++ b/tests/llm/test_policy_live.py @@ -105,23 +105,28 @@ def ama_index(): tail and elements, plus section guidelines. Built once per session.""" from market.edgar import AmaIndex + # SELECT DISTINCT: the runs are the same, far fewer rows to tokenize con = duckdb.connect(str(REPLICA), read_only=True) try: texts = [ t for (t,) in con.execute( - "SELECT descriptor FROM pfs.cpt_code UNION ALL " + "SELECT DISTINCT t FROM (" + "SELECT descriptor AS t FROM pfs.cpt_code UNION ALL " "SELECT stem FROM pfs.cpt_code UNION ALL " "SELECT tail FROM pfs.cpt_code UNION ALL " "SELECT unnest(elements) FROM pfs.cpt_code UNION ALL " - "SELECT guideline FROM pfs.cpt_section" + "SELECT guideline FROM pfs.cpt_section)" ).fetchall() if t ] finally: con.close() assert texts, "pfs.cpt_code is empty; nothing to guard against" - return AmaIndex.of(texts) + # AmaIndex.of builds the runs; a plain frozenset copy is what the checks + # intersect with, because set.isdisjoint on the frozenset *subclass* takes + # a slow generic path (about 60 ms per call against 181k runs). + return frozenset(AmaIndex.of(texts)) def _strings(x): @@ -149,13 +154,19 @@ def _prose_segments(text): def test_live_no_eight_word_cpt_runs(cfg, slices, ama_index): """No served string shares an 8-word run with licensed CPT text.""" from llm import policy - from market.edgar import shares_ama_run + from market.edgar import _WORD + + def shares_run(text, n=8): # same tokenization as market.edgar.shares_ama_run + w = _WORD.findall(text.lower()) + return not {tuple(w[i : i + n]) for i in range(len(w) - n + 1)}.isdisjoint( + ama_index + ) served = {**slices, "contents": policy.load_contents(cfg)} hits = [] for name, body in served.items(): for text in _strings(body): - if any(shares_ama_run(seg, ama_index) for seg in _prose_segments(text)): + if any(shares_run(seg) for seg in _prose_segments(text)): hits.append(name) break assert not hits, f"8-word CPT run shared in slices {hits}"