From 660528c55edca1a0a07d7adcd905113b9cb5fa03 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 13 Jul 2026 03:23:30 +0100 Subject: [PATCH] Fix Python vector search authentication --- python/qihse/core.py | 14 ++++++++++++++ python/tests/test_vector_search_auth.py | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 python/tests/test_vector_search_auth.py diff --git a/python/qihse/core.py b/python/qihse/core.py index f2f0156..31f2fbc 100644 --- a/python/qihse/core.py +++ b/python/qihse/core.py @@ -69,6 +69,18 @@ class DistanceMetric(IntEnum): ] _lib.qihse_vector_db_add_vectors.restype = ctypes.c_bool +_lib.qihse_auth_init.argtypes = [] +_lib.qihse_auth_init.restype = None +_lib.qihse_auth_get_user.argtypes = [ctypes.c_uint32] +_lib.qihse_auth_get_user.restype = ctypes.c_void_p + +# Vector search is authorization-aware. Initialize the process-local auth +# context once and retain the pre-seeded operator for local SDK operations. +_lib.qihse_auth_init() +_local_sdk_user = _lib.qihse_auth_get_user(0) +if not _local_sdk_user: + raise RuntimeError("QIHSE failed to initialize the local SDK auth context") + class CVectorQuery(ctypes.Structure): _fields_ = [ ("query_vector", ctypes.POINTER(ctypes.c_float)), @@ -84,6 +96,7 @@ class CVectorQuery(ctypes.Structure): ("distance_metric", ctypes.c_int), ("metadata_filter", ctypes.c_void_p), ("metadata_filter_opaque", ctypes.c_void_p), + ("user", ctypes.c_void_p), ] class CVectorResult(ctypes.Structure): @@ -253,6 +266,7 @@ def search( c_query.distance_metric = metric.value c_query.metadata_filter = None c_query.metadata_filter_opaque = None + c_query.user = _local_sdk_user out_results = (CVectorResult * top_k)() diff --git a/python/tests/test_vector_search_auth.py b/python/tests/test_vector_search_auth.py new file mode 100644 index 0000000..02bb655 --- /dev/null +++ b/python/tests/test_vector_search_auth.py @@ -0,0 +1,24 @@ +import os +import tempfile + +import numpy as np + +from qihse.core import DistanceMetric, VectorDB + + +def test_vector_search_supplies_authenticated_user(): + with tempfile.TemporaryDirectory() as temp_dir: + path = os.path.join(temp_dir, "auth-search.qdb") + vectors = np.eye(4, dtype=np.float32) + + with VectorDB.create(path, dims=4) as database: + database.add_vectors(vectors, ids=[10, 11, 12, 13]) + database.build_graph() + results = database.search( + vectors[2], + k=2, + metric=DistanceMetric.COSINE, + ) + + assert results + assert results[0].id == 12