Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions redisvl/mcp/tools/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def validate_profile_against_schema(
f"vector field '{field_name}'"
)

_validate_locked_exists_fields(profile, schema, vector_field_names)
_validate_locked_exists_fields(profile, schema)

# Raises RedisVLMCPError(INVALID_FILTER) naming the offending field.
resolve_locked_filter(profile, schema)
Expand All @@ -150,16 +150,15 @@ def validate_profile_against_schema(
def _validate_locked_exists_fields(
profile: MCPCustomToolConfig,
schema: IndexSchema,
vector_field_names: set[str],
) -> None:
"""Reject a locked ``exists`` clause the index cannot actually answer.

``exists`` compiles to ``ismissing``, which Redis refuses unless the field was
declared with ``INDEXMISSING``. The DSL also lets ``exists`` through for vector
fields, which no other operator allows. Either way the profile parses fine and
then fails every single call as a *retryable* backend error, so a model retries
a permanently broken tool forever. Catching it here turns that into a startup
failure the operator sees once.
declared with ``INDEXMISSING``. That holds for vector fields too, so a vector
field passes only when it declares ``index_missing``. Otherwise the profile
parses fine and then fails every single call as a *retryable* backend error, so
a model retries a permanently broken tool forever. Catching it here turns that
into a startup failure the operator sees once.
"""
if profile.lock.filter is None:
return
Expand All @@ -172,11 +171,6 @@ def _validate_locked_exists_fields(
if field is None:
continue # resolve_locked_filter reports unknown fields.

if field_name in vector_field_names:
raise ValueError(
f"custom_tools '{profile.name}' lock.filter cannot use 'exists' on "
f"vector field '{field_name}'"
)
if not getattr(field.attrs, "index_missing", False):
raise ValueError(
f"custom_tools '{profile.name}' lock.filter uses 'exists' on field "
Expand Down
118 changes: 118 additions & 0 deletions tests/integration/test_mcp/test_locked_exists_vector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""A locked ``exists`` filter on a vector field, checked against live Redis.

``validate_profile_against_schema`` accepts ``exists`` on a vector field only
when the field declares ``index_missing``, on the grounds that Redis then answers
``ismissing()`` for it. These tests pin that assumption: if ``FT.CREATE`` ever
stopped carrying ``INDEXMISSING`` for a vector field, the validator would still
pass the profile and every call would fail as a retryable backend error.
"""

import numpy as np
import pytest

from redisvl.index import SearchIndex
from redisvl.mcp.filters import parse_filter
from redisvl.query import FilterQuery, VectorQuery
from redisvl.redis.connection import supports_svs
from redisvl.redis.utils import array_to_buffer
from redisvl.schema import IndexSchema

DIMS = 4
EMBEDDING = [0.1, 0.2, 0.3, 0.4]


@pytest.fixture(
params=[
pytest.param({"algorithm": "flat"}, id="flat"),
pytest.param({"algorithm": "hnsw"}, id="hnsw"),
pytest.param({"algorithm": "svs-vamana"}, id="svs-vamana"),
]
)
def vector_attrs(request, client):
if request.param["algorithm"] == "svs-vamana" and not supports_svs(client):
pytest.skip("SVS-VAMANA requires Redis 8.2+ with SVS support")
return request.param


@pytest.fixture(params=["hash", "json"])
def storage_type(request):
return request.param


@pytest.fixture
def index_with_missing_embedding(
redis_url, redis_test_name, vector_attrs, storage_type
):
name = redis_test_name("locked_exists_vector")
schema = IndexSchema.from_dict(
{
"index": {
"name": name,
"prefix": name,
"storage_type": storage_type,
},
"fields": [
{"name": "doc_id", "type": "tag"},
{
"name": "embedding",
"type": "vector",
"attrs": {
**vector_attrs,
"dims": DIMS,
"distance_metric": "cosine",
"datatype": "float32",
"index_missing": True,
},
},
],
}
)
index = SearchIndex(schema=schema, redis_url=redis_url)
index.create(overwrite=True, drop=True)

embedding = (
array_to_buffer(EMBEDDING, "float32") if storage_type == "hash" else EMBEDDING
)
index.load(
[
{"doc_id": "with-embedding", "embedding": embedding},
{"doc_id": "without-embedding"},
],
id_field="doc_id",
)

yield index

index.delete(drop=True)


def _locked_exists_filter(index):
# The same parser a profile's lock.filter goes through at startup.
return parse_filter({"field": "embedding", "op": "exists"}, index.schema)


def test_locked_exists_on_vector_field_as_filter(index_with_missing_embedding):
index = index_with_missing_embedding
locked = _locked_exists_filter(index)
assert str(locked) == "(-ismissing(@embedding))"

results = index.query(
FilterQuery(filter_expression=locked, return_fields=["doc_id"])
)

assert [doc["doc_id"] for doc in results] == ["with-embedding"]


def test_locked_exists_on_vector_field_as_knn_prefilter(index_with_missing_embedding):
index = index_with_missing_embedding
query = VectorQuery(
vector=np.array(EMBEDDING, dtype=np.float32).tolist(),
vector_field_name="embedding",
return_fields=["doc_id"],
filter_expression=_locked_exists_filter(index),
num_results=10,
)

results = index.query(query)

assert [doc["doc_id"] for doc in results] == ["with-embedding"]
30 changes: 30 additions & 0 deletions tests/unit/test_mcp/test_profiles_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
validate_profile_against_schema,
)
from redisvl.mcp.tools.search import merge_locked_filter
from redisvl.schema import IndexSchema

LOCKED_CATEGORY_FILTER = {"field": "category", "op": "eq", "value": "resolved"}
FILTER_HINT = "Object filter fields: content(text), category(tag), rating(numeric)."
Expand Down Expand Up @@ -618,6 +619,35 @@ def test_validate_profile_against_schema_rejects_locked_filter_on_unknown_field(
assert exc_info.value.code == MCPErrorCode.INVALID_FILTER


def _schema_with_index_missing(*field_names: str) -> IndexSchema:
schema_dict = _schema().to_dict()
for field in schema_dict["fields"]:
if field["name"] in field_names:
field.setdefault("attrs", {})["index_missing"] = True
return IndexSchema.from_dict(schema_dict)


@pytest.mark.parametrize("field_name", ["category", "embedding"])
def test_validate_profile_against_schema_rejects_locked_exists_without_index_missing(
field_name,
):
profile = _profile(lock={"filter": {"field": field_name, "op": "exists"}})

with pytest.raises(ValueError, match="not indexed with INDEXMISSING"):
validate_profile_against_schema(profile, _schema())


@pytest.mark.parametrize("field_name", ["category", "embedding"])
def test_validate_profile_against_schema_accepts_locked_exists_with_index_missing(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test lets the profile through on the grounds that Redis answers ismissing() on the vector field. Nothing in tests/integration checks that, though: ismissing doesn't appear there at all. If FT.CREATE ever stopped carrying INDEXMISSING for a vector field, this validator would still pass the profile, and every call would fail as a retryable backend error. That's the failure mode the validator exists to prevent.

Could you add one integration test? It would create a vector field with index_missing: True, load one document with an embedding and one without, and assert that (-ismissing(@embedding)) returns only the first, both as a filter and as a KNN prefilter. I checked that behaviour by hand on 8.2, 8.4 and 8.10, for FLAT, HNSW and SVS-VAMANA, on HASH and JSON, so the test should pass on every CI version.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Purv-Kabaria once this change is in, I can merge the PR 👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9620555. Added integration coverage using RedisVL's own SearchIndex.create: one document with an embedding and one without, asserting that (-ismissing(@embedding)) selects only the first, both as a filter and as a KNN prefilter. The matrix covers HASH/JSON with FLAT, HNSW and SVS-VAMANA. All 12 cases and the 598 MCP unit tests pass against redis:8.10.3; formatting and mypy pass. The Reviewer Brief is updated. Thanks for spelling out the backend failure this needs to catch.

field_name,
):
# Redis answers ismissing() on vector fields declared with INDEXMISSING too,
# so a vector field is held to the same rule as every other field type.
profile = _profile(lock={"filter": {"field": field_name, "op": "exists"}})

validate_profile_against_schema(profile, _schema_with_index_missing(field_name))


def test_resolve_locked_filter_returns_none_when_no_filter_is_locked():
assert resolve_locked_filter(_profile(), _schema()) is None

Expand Down