Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /als-apg_osprey /tests /e2e /test_ariel_search.py
| """ARIEL Search E2E Tests. | |
| Tests the full ARIEL pipeline: ingest -> enhance -> search. | |
| Requires Ollama with nomic-embed-text (skips if unavailable). | |
| Run with: | |
| pytest tests/e2e/test_ariel_search.py -v | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| import os | |
| from pathlib import Path | |
| from typing import TYPE_CHECKING | |
| import pytest | |
| from tests._container_support import is_docker_available, start_or_skip, stop_quietly | |
| if TYPE_CHECKING: | |
| from osprey.services.ariel_search.config import ARIELConfig | |
| pytestmark = [pytest.mark.e2e, pytest.mark.slow, pytest.mark.asyncio] | |
| logger = logging.getLogger(__name__) | |
| # Path to synthetic test data | |
| TEST_DATA_PATH = Path(__file__).parent.parent / "fixtures" / "ariel" / "test_logbook_entries.jsonl" | |
| # Path to config file for LLM access (RAG tests need this) | |
| # Uses minimal test config with ALS-APG API access (AWS Bedrock proxy) | |
| CONFIG_FILE_PATH = Path(__file__).parent.parent / "fixtures" / "ariel" / "test_config.yml" | |
| # Dev database URL - uses port 5432 (ariel-postgres container) | |
| # Also try port 5433 (ariel-dev-db from docker/ariel-dev.yml) | |
| DEV_DATABASE_URL_5432 = "postgresql://ariel:ariel@localhost:5432/ariel_e2e_test" | |
| DEV_DATABASE_URL_5433 = "postgresql://ariel:ariel@localhost:5433/ariel_e2e_test" | |
| def is_ollama_available() -> bool: | |
| """Check if Ollama is available for tests.""" | |
| try: | |
| import requests | |
| response = requests.get("http://localhost:11434/api/tags", timeout=2) | |
| return response.status_code == 200 | |
| except Exception: | |
| return False | |
| def is_dev_database_available() -> tuple[bool, str]: | |
| """Check if a dev database is running (tries 5432 then 5433). | |
| Returns: | |
| Tuple of (available, url) - url is the working database URL | |
| """ | |
| import psycopg | |
| for dev_url in [DEV_DATABASE_URL_5432, DEV_DATABASE_URL_5433]: | |
| try: | |
| with psycopg.connect( | |
| dev_url.replace("/ariel_e2e_test", "/ariel"), autocommit=True | |
| ) as conn: | |
| conn.execute("SELECT 1") | |
| try: | |
| conn.execute("CREATE DATABASE ariel_e2e_test") | |
| logger.info("Created ariel_e2e_test database") | |
| except psycopg.errors.DuplicateDatabase: | |
| pass | |
| return True, dev_url | |
| except Exception as e: | |
| logger.debug(f"Dev database at {dev_url} not available: {e}") | |
| continue | |
| return False, "" | |
| # ============================================================================= | |
| # Module-scoped fixtures for E2E tests | |
| # ============================================================================= | |
| def e2e_database_url(request: pytest.FixtureRequest) -> str: | |
| """Get database connection URL for e2e tests. | |
| Tries: | |
| 1. ARIEL_TEST_DATABASE_URL environment variable | |
| 2. Existing docker-compose dev database | |
| 3. Testcontainers | |
| Only the testcontainers branch owns a container, so teardown is registered | |
| with ``request.addfinalizer`` on that branch alone. A ``yield`` would turn | |
| all three exits into generator paths and make the two that return an | |
| externally managed URL fail with "did not yield a value". | |
| """ | |
| # Check env var | |
| env_url = os.environ.get("ARIEL_TEST_DATABASE_URL") | |
| if env_url: | |
| logger.info("Using database from ARIEL_TEST_DATABASE_URL") | |
| return env_url | |
| # Try dev database | |
| available, dev_url = is_dev_database_available() | |
| if available: | |
| logger.info(f"Using docker dev database for e2e tests: {dev_url.split('@')[-1]}") | |
| return dev_url | |
| # Fall back to testcontainers | |
| if not is_docker_available(): | |
| pytest.skip("No database available - start docker-compose or install Docker") | |
| logger.info("Starting testcontainers PostgreSQL for e2e tests") | |
| from testcontainers.postgres import PostgresContainer | |
| container = start_or_skip( | |
| lambda: PostgresContainer( | |
| image="ankane/pgvector:latest", | |
| username="ariel", | |
| password="ariel", | |
| dbname="ariel_e2e_test", | |
| ), | |
| label="ariel-e2e-postgres", | |
| ) | |
| request.addfinalizer(lambda: stop_quietly(container)) | |
| url = container.get_connection_url() | |
| if url.startswith("postgresql+psycopg2://"): | |
| url = url.replace("postgresql+psycopg2://", "postgresql://") | |
| logger.info(f"Testcontainer started: {url.split('@')[-1]}") | |
| return url | |
| def e2e_ariel_config(e2e_database_url: str) -> ARIELConfig: | |
| """Create ARIELConfig for e2e tests with all modules enabled.""" | |
| from osprey.services.ariel_search.config import ARIELConfig | |
| return ARIELConfig.from_dict( | |
| { | |
| "database": {"uri": e2e_database_url}, | |
| "search_modules": { | |
| "keyword": {"enabled": True}, | |
| "semantic": {"enabled": True, "provider": "ollama", "model": "nomic-embed-text"}, | |
| "rag": {"enabled": True, "provider": "ollama", "model": "nomic-embed-text"}, | |
| }, | |
| "enhancement_modules": { | |
| "text_embedding": { | |
| "enabled": True, | |
| "provider": "ollama", | |
| "models": [{"name": "nomic-embed-text", "dimension": 768}], | |
| }, | |
| }, | |
| "embedding": {"provider": "ollama"}, | |
| "ingestion": { | |
| "adapter": "als_logbook", | |
| "source_url": str(TEST_DATA_PATH), | |
| }, | |
| } | |
| ) | |
| async def e2e_connection_pool(e2e_database_url: str): | |
| """Module-scoped async connection pool.""" | |
| from osprey.services.ariel_search.config import DatabaseConfig | |
| from osprey.services.ariel_search.database import create_connection_pool | |
| config = DatabaseConfig(uri=e2e_database_url) | |
| pool = await create_connection_pool(config) | |
| yield pool | |
| await pool.close() | |
| async def e2e_migrated_pool(e2e_connection_pool, e2e_ariel_config: ARIELConfig): | |
| """Connection pool with migrations applied (module-scoped).""" | |
| from osprey.services.ariel_search.database import run_migrations | |
| await run_migrations(e2e_connection_pool, e2e_ariel_config) | |
| logger.info("E2E migrations applied") | |
| return e2e_connection_pool | |
| def require_ollama(): | |
| """Skip all tests in module if Ollama not available.""" | |
| if not is_ollama_available(): | |
| pytest.skip("Ollama not available - install and run 'ollama pull nomic-embed-text'") | |
| def e2e_config_file(): | |
| """Module-scoped config file setup (used during data seeding). | |
| Required for RAG tests which call get_chat_completion. | |
| Uses my-control-assistant config which has ALS-APG configured. | |
| """ | |
| if not CONFIG_FILE_PATH.exists(): | |
| pytest.skip(f"Config file not found: {CONFIG_FILE_PATH}") | |
| return str(CONFIG_FILE_PATH) | |
| def rag_config_env(e2e_config_file): | |
| """Function-scoped fixture to set CONFIG_FILE for RAG tests. | |
| This runs AFTER the autouse reset_registry_between_tests fixture | |
| in tests/e2e/conftest.py which clears CONFIG_FILE. | |
| Must be used by RAG tests that need LLM access. | |
| """ | |
| os.environ["CONFIG_FILE"] = e2e_config_file | |
| # Clear config cache to force reload with new CONFIG_FILE | |
| from osprey.utils import config as config_module | |
| config_module._default_config = None | |
| config_module._default_configurable = None | |
| config_module._config_cache.clear() | |
| # Reset and initialize the registry with new config | |
| from osprey.registry import initialize_registry, reset_registry | |
| reset_registry() | |
| initialize_registry() | |
| yield e2e_config_file | |
| async def seeded_ariel_db( | |
| require_ollama, e2e_database_url, e2e_migrated_pool, e2e_ariel_config, e2e_config_file | |
| ): | |
| """Database with test entries and embeddings. | |
| Ingests test_logbook_entries.jsonl and generates embeddings. | |
| Module-scoped for efficiency (~10-15s setup). | |
| ``require_ollama`` is deliberately the FIRST parameter: pytest resolves | |
| fixture params in order, and with it listed after the DB fixtures a | |
| runner with docker but no Ollama booted a real pgvector testcontainer | |
| (image pull + migrations) only to skip the whole module immediately | |
| afterwards. | |
| Yields: | |
| Dict with repository, config, pool, and entry_count | |
| """ | |
| from osprey.models.embeddings.ollama import OllamaEmbeddingProvider | |
| from osprey.services.ariel_search.database import ARIELRepository | |
| from osprey.services.ariel_search.ingestion.adapters.als import ALSLogbookAdapter | |
| repo = ARIELRepository(e2e_migrated_pool, e2e_ariel_config) | |
| # Initialize adapter | |
| adapter = ALSLogbookAdapter(e2e_ariel_config) | |
| # Initialize embedder | |
| embedder = OllamaEmbeddingProvider() | |
| # Ingest entries | |
| entry_count = 0 | |
| logger.info(f"Ingesting test entries from {TEST_DATA_PATH}") | |
| async for entry in adapter.fetch_entries(): | |
| await repo.upsert_entry(entry) | |
| # Generate embedding | |
| try: | |
| embeddings = embedder.execute_embedding( | |
| texts=[entry["raw_text"]], | |
| model_id="nomic-embed-text", | |
| ) | |
| if embeddings and embeddings[0]: | |
| await repo.store_text_embedding( | |
| entry_id=entry["entry_id"], | |
| embedding=embeddings[0], | |
| model_name="nomic-embed-text", | |
| ) | |
| except Exception as e: | |
| logger.warning(f"Failed to embed entry {entry['entry_id']}: {e}") | |
| entry_count += 1 | |
| logger.info(f"Ingested {entry_count} entries with embeddings") | |
| yield { | |
| "repository": repo, | |
| "config": e2e_ariel_config, | |
| "pool": e2e_migrated_pool, | |
| "entry_count": entry_count, | |
| } | |
| # Cleanup test data | |
| logger.info("Cleaning up test entries") | |
| async with e2e_migrated_pool.connection() as conn: | |
| # Clean up embeddings first (foreign key) | |
| try: | |
| await conn.execute(""" | |
| DELETE FROM text_embeddings_nomic_embed_text | |
| WHERE entry_id LIKE 'E%' | |
| """) | |
| except Exception: | |
| pass # Table may not exist | |
| # Clean up entries | |
| await conn.execute(""" | |
| DELETE FROM enhanced_entries | |
| WHERE entry_id LIKE 'E%' | |
| """) | |
| # ============================================================================= | |
| # Test Classes | |
| # ============================================================================= | |
| class TestKeywordSearchE2E: | |
| """Keyword search tests with deterministic assertions.""" | |
| async def test_exact_term_match(self, seeded_ariel_db): | |
| """Search for 'klystron' finds RF cavity trip entry (E001).""" | |
| from osprey.services.ariel_search.search.keyword import keyword_search | |
| results = await keyword_search( | |
| query="klystron", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| max_results=5, | |
| ) | |
| # Results are (entry, score, highlights) tuples | |
| entry_ids = [entry["entry_id"] for entry, score, highlights in results] | |
| assert "E001" in entry_ids, f"Expected E001 in results for 'klystron', got: {entry_ids}" | |
| async def test_multi_term_search(self, seeded_ariel_db): | |
| """Search 'vacuum leak' finds vacuum entry (E002).""" | |
| from osprey.services.ariel_search.search.keyword import keyword_search | |
| results = await keyword_search( | |
| query="vacuum leak", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| max_results=5, | |
| ) | |
| assert len(results) > 0, "Expected results for 'vacuum leak'" | |
| entry_ids = [entry["entry_id"] for entry, score, highlights in results] | |
| assert "E002" in entry_ids, f"Expected E002 in results for 'vacuum leak', got: {entry_ids}" | |
| async def test_boolean_and_search(self, seeded_ariel_db): | |
| """Search 'beam AND loss' finds beam loss entry (E003).""" | |
| from osprey.services.ariel_search.search.keyword import keyword_search | |
| results = await keyword_search( | |
| query="beam AND loss", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| max_results=5, | |
| ) | |
| assert len(results) > 0, "Expected results for 'beam AND loss'" | |
| entry_ids = [entry["entry_id"] for entry, score, highlights in results] | |
| assert "E003" in entry_ids, ( | |
| f"Expected E003 in results for 'beam AND loss', got: {entry_ids}" | |
| ) | |
| async def test_author_filter(self, seeded_ariel_db): | |
| """Search 'author:oper_smith' finds entries by that author.""" | |
| from osprey.services.ariel_search.search.keyword import keyword_search | |
| # The keyword search parses author: prefix but needs a search term too | |
| # Search for something oper_smith wrote about | |
| results = await keyword_search( | |
| query="author:oper_smith shift", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| max_results=10, | |
| ) | |
| # Should find E006 (Morning Shift Summary by oper_smith) | |
| assert results, "Expected at least one result for 'author:oper_smith shift'" | |
| authors = {entry["author"] for entry, score, highlights in results} | |
| assert "oper_smith" in authors, f"Expected oper_smith in authors, got: {authors}" | |
| async def test_no_results_for_nonexistent_term(self, seeded_ariel_db): | |
| """Search for nonexistent term returns empty results.""" | |
| from osprey.services.ariel_search.search.keyword import keyword_search | |
| results = await keyword_search( | |
| query="xyznonexistent123abc", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| max_results=5, | |
| fuzzy_fallback=False, # Disable fuzzy to ensure empty result | |
| ) | |
| assert len(results) == 0, f"Expected no results for nonexistent term, got: {len(results)}" | |
| class TestSemanticSearchE2E: | |
| """Semantic search with deterministic assertions. | |
| Note: LLM judge evaluation is optional - tests skip judge if config unavailable. | |
| """ | |
| async def test_conceptual_search(self, seeded_ariel_db): | |
| """Search 'electrical failure' finds power-related entries.""" | |
| from osprey.models.embeddings.ollama import OllamaEmbeddingProvider | |
| from osprey.services.ariel_search.search.semantic import semantic_search | |
| embedder = OllamaEmbeddingProvider() | |
| results = await semantic_search( | |
| query="electrical failure", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| embedder=embedder, | |
| max_results=5, | |
| similarity_threshold=0.3, # Lower threshold for conceptual match | |
| ) | |
| # Deterministic: we got results | |
| assert len(results) > 0, "Expected results for 'electrical failure'" | |
| # Check that power-related entries appear in results | |
| entry_ids = [entry["entry_id"] for entry, sim in results] | |
| # E005 (Power Outage) or E001 (RF trip) should be in top results | |
| power_related = {"E001", "E005", "E012"} # RF trip, Power outage, Network outage | |
| found = power_related & set(entry_ids) | |
| assert len(found) > 0, f"Expected power-related entries in results, got: {entry_ids}" | |
| async def test_rephrased_query(self, seeded_ariel_db): | |
| """Search 'accelerator shutdown' finds trip/failure entries.""" | |
| from osprey.models.embeddings.ollama import OllamaEmbeddingProvider | |
| from osprey.services.ariel_search.search.semantic import semantic_search | |
| embedder = OllamaEmbeddingProvider() | |
| results = await semantic_search( | |
| query="accelerator shutdown incident", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| embedder=embedder, | |
| max_results=5, | |
| similarity_threshold=0.3, | |
| ) | |
| # Deterministic: we got results | |
| assert len(results) > 0, "Expected results for 'accelerator shutdown incident'" | |
| # Should find incident entries | |
| entry_ids = [entry["entry_id"] for entry, sim in results] | |
| # Incident entries: E001 (RF trip), E002 (vacuum leak), E003 (beam loss), E004 (quench), E005 (power) | |
| incident_entries = {"E001", "E002", "E003", "E004", "E005", "E009"} | |
| found = incident_entries & set(entry_ids) | |
| assert len(found) > 0, f"Expected incident entries in results, got: {entry_ids}" | |
| async def test_domain_concept_search(self, seeded_ariel_db): | |
| """Search 'cold equipment problems' finds cryogenic issues.""" | |
| from osprey.models.embeddings.ollama import OllamaEmbeddingProvider | |
| from osprey.services.ariel_search.search.semantic import semantic_search | |
| embedder = OllamaEmbeddingProvider() | |
| results = await semantic_search( | |
| query="cold equipment temperature problems", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| embedder=embedder, | |
| max_results=5, | |
| similarity_threshold=0.3, | |
| ) | |
| # Deterministic: we got results | |
| assert len(results) > 0, "Expected results for 'cold equipment temperature problems'" | |
| # Should find cryogenic-related entries | |
| entry_ids = [entry["entry_id"] for entry, sim in results] | |
| # Cryogenic entries: E004 (magnet quench), E009 (LN2 spill), E011 (cryostat) | |
| cryo_entries = {"E004", "E009", "E011"} | |
| found = cryo_entries & set(entry_ids) | |
| assert len(found) > 0, f"Expected cryogenic entries in results, got: {entry_ids}" | |
| class TestSearchIntegration: | |
| """Tests for search module integration and edge cases.""" | |
| async def test_entry_count(self, seeded_ariel_db): | |
| """Verify all 15 test entries were ingested.""" | |
| assert seeded_ariel_db["entry_count"] == 15, ( | |
| f"Expected 15 entries, got {seeded_ariel_db['entry_count']}" | |
| ) | |
| async def test_embedding_dimension(self, seeded_ariel_db): | |
| """Verify embeddings have correct dimension (768 for nomic-embed-text).""" | |
| pool = seeded_ariel_db["pool"] | |
| async with pool.connection() as conn: | |
| result = await conn.execute(""" | |
| SELECT embedding FROM text_embeddings_nomic_embed_text | |
| WHERE entry_id = 'E001' | |
| LIMIT 1 | |
| """) | |
| row = await result.fetchone() | |
| assert row is not None, "Expected embedding for E001" | |
| embedding = row[0] | |
| # pgvector stores as string or list | |
| if isinstance(embedding, str): | |
| dim = embedding.count(",") + 1 | |
| else: | |
| dim = len(embedding) | |
| assert dim == 768, f"Expected 768 dimensions, got {dim}" | |
| async def test_keyword_and_semantic_consistency(self, seeded_ariel_db): | |
| """Keyword and semantic search both find the same entry for exact terms.""" | |
| from osprey.models.embeddings.ollama import OllamaEmbeddingProvider | |
| from osprey.services.ariel_search.search.keyword import keyword_search | |
| from osprey.services.ariel_search.search.semantic import semantic_search | |
| embedder = OllamaEmbeddingProvider() | |
| # Both should find E002 for "vacuum leak sector 5" | |
| keyword_results = await keyword_search( | |
| query="vacuum leak sector 5", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| max_results=5, | |
| ) | |
| semantic_results = await semantic_search( | |
| query="vacuum leak in sector 5", | |
| repository=seeded_ariel_db["repository"], | |
| config=seeded_ariel_db["config"], | |
| embedder=embedder, | |
| max_results=5, | |
| similarity_threshold=0.3, | |
| ) | |
| keyword_ids = {entry["entry_id"] for entry, score, highlights in keyword_results} | |
| semantic_ids = {entry["entry_id"] for entry, score in semantic_results} | |
| # E002 should appear in both | |
| assert "E002" in keyword_ids, f"E002 not in keyword results: {keyword_ids}" | |
| assert "E002" in semantic_ids, f"E002 not in semantic results: {semantic_ids}" | |
| # ============================================================================= | |
| # Vocabulary expansion through the MCP surface | |
| # ============================================================================= | |
| # | |
| # Harness-integrity lane (`tests/e2e/README.md`): an operator-facing surface is | |
| # driven end to end, but every assertion is model-independent OSPREY behavior -- | |
| # the vocabulary file is loaded from the config directory, the expansion is | |
| # applied to a real PostgreSQL full-text search, and the MCP server refuses to | |
| # start when the configured vocabulary file is missing. | |
| #: The entry seeded for the expansion scenario. Its prose contains ONLY the | |
| #: canonical forms ("troubleshoot", "beam position monitor"), never the | |
| #: shorthand, so a hit for ``ts bpm`` can only come from the vocabulary. | |
| VOCAB_ENTRY_ID = "VOCAB-E2E-1" | |
| VOCAB_ENTRY_TEXT = ( | |
| "Had to troubleshoot the beam position monitor offset after the orbit drift this morning." | |
| ) | |
| #: The vocabulary the ``control_assistant`` app template ships, used verbatim. | |
| SHIPPED_VOCABULARY_PATH = ( | |
| Path(__file__).parent.parent.parent | |
| / "src" | |
| / "osprey" | |
| / "templates" | |
| / "apps" | |
| / "control_assistant" | |
| / "data" | |
| / "ariel" | |
| / "vocabulary.yml" | |
| ) | |
| def _write_vocabulary_project(project_dir: Path, database_uri: str, vocabulary_path: str) -> Path: | |
| """Render a minimal project whose config.yml configures a vocabulary. | |
| Args: | |
| project_dir: Directory to write ``config.yml`` into. | |
| database_uri: The ARIEL database the project points at. | |
| vocabulary_path: Value of ``ariel.vocabulary.path``, relative to | |
| ``project_dir`` -- the interesting case, since a relative path must | |
| resolve against the config file's directory and not the CWD. | |
| Returns: | |
| The path of the written ``config.yml``. | |
| """ | |
| config_path = project_dir / "config.yml" | |
| config_path.write_text( | |
| "project_name: ariel-vocabulary-e2e\n" | |
| "registry_path: null\n" | |
| "ariel:\n" | |
| " database:\n" | |
| f" uri: {database_uri}\n" | |
| " search_modules:\n" | |
| " keyword:\n" | |
| " enabled: true\n" | |
| " vocabulary:\n" | |
| " enabled: true\n" | |
| f" path: {vocabulary_path}\n" | |
| ) | |
| return config_path | |
| def vocabulary_project_dir(tmp_path_factory, e2e_database_url: str) -> Path: | |
| """A project directory carrying the shipped vocabulary at a RELATIVE path. | |
| The vocabulary is copied to ``data/ariel/vocabulary.yml`` and referenced as | |
| that relative path, so the test exercises resolution against the directory | |
| of the config file the process actually loaded. | |
| """ | |
| import shutil | |
| # A moved or deleted template is a repo regression, never a reason to skip. | |
| assert SHIPPED_VOCABULARY_PATH.exists(), ( | |
| f"Shipped vocabulary missing: {SHIPPED_VOCABULARY_PATH}" | |
| ) | |
| project_dir = tmp_path_factory.mktemp("ariel-vocabulary-project") | |
| vocabulary_dir = project_dir / "data" / "ariel" | |
| vocabulary_dir.mkdir(parents=True) | |
| shutil.copy(SHIPPED_VOCABULARY_PATH, vocabulary_dir / "vocabulary.yml") | |
| _write_vocabulary_project(project_dir, e2e_database_url, "data/ariel/vocabulary.yml") | |
| return project_dir | |
| async def vocabulary_seeded_entry(seeded_ariel_db): | |
| """Seed one entry whose prose contains only the canonical forms. | |
| Added on top of ``seeded_ariel_db`` through the same repository and embedder | |
| it uses, and deleted again at teardown. Its ID deliberately does not start | |
| with ``E``, so the seeding fixture's own ``LIKE 'E%'`` cleanup never touches | |
| it and the existing exact-count assertions stay true. | |
| """ | |
| from datetime import UTC, datetime | |
| from osprey.models.embeddings.ollama import OllamaEmbeddingProvider | |
| repository = seeded_ariel_db["repository"] | |
| entry = { | |
| "entry_id": VOCAB_ENTRY_ID, | |
| "source_system": "ALS eLog", | |
| "timestamp": datetime(2024, 1, 20, 8, 15, tzinfo=UTC), | |
| "author": "oper_vocab", | |
| "raw_text": VOCAB_ENTRY_TEXT, | |
| "attachments": [], | |
| "metadata": {}, | |
| "enhancement_status": {}, | |
| } | |
| await repository.upsert_entry(entry) | |
| try: | |
| embeddings = OllamaEmbeddingProvider().execute_embedding( | |
| texts=[VOCAB_ENTRY_TEXT], | |
| model_id="nomic-embed-text", | |
| ) | |
| if embeddings and embeddings[0]: | |
| await repository.store_text_embedding( | |
| entry_id=VOCAB_ENTRY_ID, | |
| embedding=embeddings[0], | |
| model_name="nomic-embed-text", | |
| ) | |
| except Exception as e: # pragma: no cover - embedding is not load-bearing here | |
| logger.warning(f"Failed to embed {VOCAB_ENTRY_ID}: {e}") | |
| yield entry | |
| async with seeded_ariel_db["pool"].connection() as conn: | |
| try: | |
| await conn.execute( | |
| "DELETE FROM text_embeddings_nomic_embed_text WHERE entry_id = %s", | |
| [VOCAB_ENTRY_ID], | |
| ) | |
| except Exception: | |
| pass # Table may not exist | |
| await conn.execute( | |
| "DELETE FROM enhanced_entries WHERE entry_id = %s", | |
| [VOCAB_ENTRY_ID], | |
| ) | |
| def vocabulary_mcp_context(vocabulary_project_dir: Path, monkeypatch): | |
| """Boot the ARIEL MCP server context against the vocabulary project. | |
| Chdir'ing into the project makes it the config the server resolves, exactly | |
| as a deployed container does (its WORKDIR is the project root). The registry | |
| is initialized because the search service dispatches modes through it. | |
| """ | |
| from osprey.mcp_server.ariel.server_context import ( | |
| initialize_ariel_context, | |
| reset_ariel_context, | |
| ) | |
| from osprey.registry import initialize_registry, reset_registry | |
| from osprey.utils.workspace import reset_config_cache | |
| monkeypatch.delenv("OSPREY_CONFIG", raising=False) | |
| monkeypatch.chdir(vocabulary_project_dir) | |
| reset_config_cache() | |
| reset_registry() | |
| initialize_registry(auto_export=False) | |
| context = initialize_ariel_context() | |
| yield context | |
| reset_ariel_context() | |
| reset_config_cache() | |
| async def _keyword_search_envelope(expand_query): | |
| """Call the ARIEL ``keyword_search`` MCP tool and parse its JSON envelope.""" | |
| import json | |
| from osprey.mcp_server.ariel.tools.keyword_search import keyword_search | |
| from tests.mcp_server.conftest import get_tool_fn | |
| raw = await get_tool_fn(keyword_search)( | |
| query="ts bpm", | |
| max_results=10, | |
| expand_query=expand_query, | |
| ) | |
| return json.loads(raw) | |
| async def test_vocabulary_expansion_through_mcp_keyword_search( | |
| vocabulary_seeded_entry, vocabulary_mcp_context | |
| ): | |
| """``ts bpm`` finds a canonical-only entry, and only with expansion on.""" | |
| context = vocabulary_mcp_context | |
| assert context.config.vocabulary_active, ( | |
| f"Vocabulary not active: {context.config.vocabulary_errors}" | |
| ) | |
| try: | |
| expanded = await _keyword_search_envelope(True) | |
| assert not expanded.get("error"), expanded | |
| assert expanded["results_found"] >= 1, expanded | |
| hits = {entry["entry_id"]: entry for entry in expanded["entries"]} | |
| assert VOCAB_ENTRY_ID in hits, f"Expected {VOCAB_ENTRY_ID} in {sorted(hits)}" | |
| assert hits[VOCAB_ENTRY_ID]["score"] > 0, hits[VOCAB_ENTRY_ID] | |
| assert expanded["expanded_terms"] == [ | |
| {"original": "ts", "alternatives": ["troubleshoot"]}, | |
| {"original": "bpm", "alternatives": ["beam position monitor"]}, | |
| ] | |
| # Same query, expansion explicitly refused: nothing bridges the | |
| # shorthand to the prose, so the entry is not reachable at all. | |
| unexpanded = await _keyword_search_envelope(False) | |
| assert not unexpanded.get("error"), unexpanded | |
| assert VOCAB_ENTRY_ID not in {entry["entry_id"] for entry in unexpanded["entries"]} | |
| assert unexpanded["expanded_terms"] == [] | |
| # No preference stated: expand_by_default decides, and its default is on. | |
| defaulted = await _keyword_search_envelope(None) | |
| assert not defaulted.get("error"), defaulted | |
| assert VOCAB_ENTRY_ID in {entry["entry_id"] for entry in defaulted["entries"]} | |
| assert defaulted["expanded_terms"] == expanded["expanded_terms"] | |
| finally: | |
| await context.shutdown() | |
| async def test_vocabulary_mcp_server_refuses_to_start_without_vocabulary_file( | |
| tmp_path, monkeypatch | |
| ): | |
| """A configured-but-missing vocabulary refuses MCP startup, naming the key. | |
| No database is contacted before the refusal, so this holds on a host with | |
| no ARIEL Postgres at all. | |
| """ | |
| from osprey.mcp_server.ariel.server_context import ( | |
| initialize_ariel_context, | |
| reset_ariel_context, | |
| ) | |
| from osprey.utils.workspace import reset_config_cache | |
| _write_vocabulary_project( | |
| tmp_path, | |
| "postgresql://ariel:ariel@localhost:5999/does_not_exist", | |
| "data/missing.yml", | |
| ) | |
| monkeypatch.delenv("OSPREY_CONFIG", raising=False) | |
| monkeypatch.chdir(tmp_path) | |
| reset_config_cache() | |
| from osprey.services.ariel_search.exceptions import ConfigurationError | |
| try: | |
| with pytest.raises(ConfigurationError) as excinfo: | |
| initialize_ariel_context() | |
| assert "ariel.vocabulary.path" in str(excinfo.value), str(excinfo.value) | |
| finally: | |
| reset_ariel_context() | |
| reset_config_cache() | |
Xet Storage Details
- Size:
- 30.2 kB
- Xet hash:
- 69b1e8170d81c521a8dfe9a3003645b4617a9a28db0db6cc30389339784acbba
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.