From a217d3cac3839e5a24ff3f30c2117d57798978b3 Mon Sep 17 00:00:00 2001 From: Kenneth Bruskiewicz Date: Mon, 16 Nov 2026 11:33:48 -0700 Subject: [PATCH 1/2] Make provider merging repeatable and bound provider requests An empty answer from a provider is no longer handled by catching a TypeError. query_KP returns None for it, and the merge loop now checks for that. Responses are merged in provider-name order, so the edge kept for a shared edge ID no longer depends on which response arrived first. Provider queries set a (10, 300) second connect/read timeout. A host that never accepts the connection previously held the query until the OS gave up. Co-Authored-By: Claude Fable 5.2 --- TCT/translator_query.py | 20 ++++++++++-- tests/test_translator_query.py | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 tests/test_translator_query.py diff --git a/TCT/translator_query.py b/TCT/translator_query.py index 0e10d64..6461e29 100644 --- a/TCT/translator_query.py +++ b/TCT/translator_query.py @@ -4,6 +4,10 @@ from TCT import translator_metakg from TCT import translator_kpinfo +# (connect, read) seconds for one provider query. Without it a provider that +# never accepts the connection holds the query for as long as the OS allows. +PROVIDER_TIMEOUT = (10, 300) + def get_translator_API_predicates() -> tuple[dict, pandas.DataFrame, dict]: ''' Get the predicates supported by each API. @@ -178,7 +182,7 @@ def query_KP(API_name_cur, query_json, APInames, API_predicates): query_copy = deepcopy(query_json) # optimize on our private copy query_json_cur = optimize_query_json(query_copy, API_name_cur, API_predicates) - response = requests.post(API_url_cur, json=query_json_cur) + response = requests.post(API_url_cur, json=query_json_cur, timeout=PROVIDER_TIMEOUT) if response.status_code == 200: result = response.json().get("message", {}) kg = result.get("knowledge_graph", {}) @@ -225,14 +229,24 @@ def parallel_api_query(query_json, select_APIs, APInames, API_predicates,max_wor query_json_cur = deepcopy(query_json) future_to_url = {executor.submit(query_KP, API_name_cur, query_json_cur, APInames, API_predicates): API_name_cur for API_name_cur in select_APIs} + responses = {} for future in as_completed(future_to_url): url = future_to_url[future] try: data = future.result() - if 'knowledge_graph' in data: - result.append(data) except Exception: no_results_returned.append(url) + continue + if data is None: + # query_KP returns None for an answer with no edges and for a + # non-200 response. Neither is an exception. + no_results_returned.append(url) + elif 'knowledge_graph' in data: + responses[url] = data + + # Merge in provider-name order. Where two providers return the same edge + # ID the later name wins, whichever response arrived first. + result = [responses[name] for name in sorted(responses)] included_KP_ID = [] for i in range(0,len(result)): diff --git a/tests/test_translator_query.py b/tests/test_translator_query.py new file mode 100644 index 0000000..152a13d --- /dev/null +++ b/tests/test_translator_query.py @@ -0,0 +1,57 @@ +"""Offline tests for provider querying and merging.""" + +import time + +from TCT import translator_query + + +def _message(edge_source): + return {"knowledge_graph": {"edges": {"e1": {"source": edge_source}}}} + + +def test_merge_ignores_arrival_order_and_empty_answers(monkeypatch): + delays = {} + + def fake_query_kp(name, query_json, api_names, api_predicates): + time.sleep(delays[name]) + if name == "empty": + return None + if name == "broken": + raise ConnectionError("refused") + return _message(name) + + monkeypatch.setattr(translator_query, "query_KP", fake_query_kp) + apis = ["zeta", "alpha", "empty", "broken"] + merged = [] + for slow in ("alpha", "zeta"): + delays.update({name: 0.0 for name in apis}) + delays[slow] = 0.2 + merged.append( + translator_query.parallel_api_query({}, apis, {}, {}, max_workers=4) + ) + + assert merged[0] == merged[1] == {"e1": {"source": "zeta"}} + + +def test_provider_query_sets_a_timeout(monkeypatch): + seen = {} + + class Response: + status_code = 500 + + def fake_post(url, **kwargs): + seen.update(kwargs) + return Response() + + monkeypatch.setattr(translator_query.requests, "post", fake_post) + query = { + "message": { + "query_graph": {"edges": {"e00": {"predicates": ["biolink:treats"]}}} + } + } + result = translator_query.query_KP( + "kp", query, {"kp": "https://example.org/query"}, {"kp": []} + ) + + assert result is None + assert seen["timeout"] == translator_query.PROVIDER_TIMEOUT From 5571df514bc9c435829bc7125db325ee24d3b887 Mon Sep 17 00:00:00 2001 From: Kenneth Bruskiewicz Date: Mon, 16 Nov 2026 13:25:32 -0700 Subject: [PATCH 2/2] test: update live-service expectations for NameRes and Node Annotator These three tests fail on main because the services changed, not the toolkit. The changes are identical to the ones on #68. NameRes on Babel 2026jul22 reports Solr stats at the top level and indexes about 331M names. The status test accepts both shapes and uses a 300M floor. Node Annotator no longer returns clinical_approval for CHEBI:15377. The expectation drops the volatile warning and clinical fields. Co-Authored-By: Claude Fable 5.2 --- tests/test_nameres.py | 7 ++++++- tests/test_node_annotator.py | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/test_nameres.py b/tests/test_nameres.py index 2f3e726..eddb7f7 100644 --- a/tests/test_nameres.py +++ b/tests/test_nameres.py @@ -61,7 +61,12 @@ def test_nameres_status(): assert status["status"] == "ok" assert status["babel_version"] != "" assert status["babel_version_url"] != "" - assert status["solr"]["numDocs"] > 425_000_000 + # Older NameRes releases nest Solr index stats under "solr"; newer ones + # report them at the top level. + # Babel 2025sep1 indexed ~425M names and Babel 2026jul22 ~331M; the floor + # is a sanity check that the index is loaded, not a fixed release size. + num_docs = status.get("solr", status)["numDocs"] + assert num_docs > 300_000_000 def test_nameres_incorrect(): diff --git a/tests/test_node_annotator.py b/tests/test_node_annotator.py index daa4135..79576ff 100644 --- a/tests/test_node_annotator.py +++ b/tests/test_node_annotator.py @@ -31,8 +31,18 @@ def use_node_annotator_prod(): 'curie': 'CHEBI:15377', 'expected': { 'query': 'CHEBI:15377', - 'boxed_warning': True, - 'sections': ['aeolus', 'chebi', 'chembl', 'clinical_approval', 'clinical_trials', 'drugbank', 'ndc', 'pubchem', 'unichem', 'unii'], + # Warning and clinical approval/trial fields depend on volatile + # upstream providers and are not part of the stable contract. + 'sections': [ + 'aeolus', + 'chebi', + 'chembl', + 'drugbank', + 'ndc', + 'pubchem', + 'unichem', + 'unii', + ], 'chembl.availability_type': 2, }, },