From 4f2445bf7163f64e63c269ba858f0e97722a89dc Mon Sep 17 00:00:00 2001 From: "Baima (Hermes Agent)" Date: Fri, 8 May 2026 07:14:10 +0800 Subject: [PATCH 1/2] fix: guard against IndexError on empty query string in SPARQL generator Issue #870: query.split() on an empty or whitespace-only string returns an empty list, and accessing [0] raises IndexError. Although the existing startswith guard on line 205 already prevents this in practice (empty strings won't match SPARQL keywords), the direct indexing is fragile. Refactor to split first and check the result before indexing, making the code resilient to future refactoring of the upstream guard. --- trustgraph-flow/trustgraph/query/ontology/sparql_generator.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/trustgraph-flow/trustgraph/query/ontology/sparql_generator.py b/trustgraph-flow/trustgraph/query/ontology/sparql_generator.py index 44c7e0a1..7d25e3ca 100644 --- a/trustgraph-flow/trustgraph/query/ontology/sparql_generator.py +++ b/trustgraph-flow/trustgraph/query/ontology/sparql_generator.py @@ -203,10 +203,12 @@ ASK {{ if response and isinstance(response, dict): query = response.get('query', '').strip() if query.upper().startswith(('SELECT', 'ASK', 'CONSTRUCT', 'DESCRIBE')): + parts = query.split() + query_type = parts[0].upper() if parts else 'UNKNOWN' return SPARQLQuery( query=query, variables=self._extract_variables(query), - query_type=query.split()[0].upper(), + query_type=query_type, explanation=response.get('explanation', 'Generated by LLM'), complexity_score=self._calculate_complexity(query) ) From a943c4d4b6b48575280982f293b2f05b4a767745 Mon Sep 17 00:00:00 2001 From: "Baima (Hermes Agent)" Date: Fri, 8 May 2026 07:15:34 +0800 Subject: [PATCH 2/2] fix: safe parsing of Prometheus metric labels in ontology monitoring Issue #873: The cache_type label extraction via chained split() calls is fragile and would crash with IndexError if the guard on line 477 is ever moved or removed. Add a length check on the split result before indexing, making the code resilient to future refactoring. --- trustgraph-flow/trustgraph/query/ontology/monitoring.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/trustgraph-flow/trustgraph/query/ontology/monitoring.py b/trustgraph-flow/trustgraph/query/ontology/monitoring.py index 703c6e95..4b0106fc 100644 --- a/trustgraph-flow/trustgraph/query/ontology/monitoring.py +++ b/trustgraph-flow/trustgraph/query/ontology/monitoring.py @@ -475,8 +475,11 @@ class PerformanceMonitor: cache_types = set() for metric_name in self.metrics_collector.counters.keys(): if 'cache_type=' in metric_name: - cache_type = metric_name.split('cache_type=')[1].split(',')[0].split('}')[0] - cache_types.add(cache_type) + # Safely extract cache_type label value + parts = metric_name.split('cache_type=') + if len(parts) >= 2: + cache_type = parts[1].split(',')[0].split('}')[0] + cache_types.add(cache_type) for cache_type in cache_types: labels = {'cache_type': cache_type}