* feat: introduce hindsight-api-slim and hindsight-all-slim packages Closes #552 - Move all source code from hindsight-api/ to new hindsight-api-slim/ - hindsight-api-slim has heavy ML deps (torch, sentence-transformers, transformers, einops, flashrank, mlx, mlx-lm, safetensors) and pg0-embedded as optional extras: [local-ml], [embedded-db], [all] - hindsight-api becomes a zero-code meta-package depending on hindsight-api-slim[all] for full backward compatibility - Add hindsight-all-slim meta-package: hindsight-api-slim + client + embed - hindsight-all updated to depend on hindsight-api-slim[all] - pg0.py: lazy-import pg0 with clear ImportError pointing to [embedded-db] - Dockerfile: replace sed hack with proper uv sync --extra flags - Update release.yml, test.yml, lint.sh, release.sh, CLAUDE.md and all path references throughout the repo * refactor: rename hindsight/ directory to hindsight-all/ * docs: document hindsight-api-slim and hindsight-all-slim package variants Add package variants table and extras explanation to installation.md * docs: remove emojis from installation.md, use professional tone * docs: link Docker slim variant to pip package variants section * docs: consolidate Docker image variants into single table * ci: fix working-directory paths after package restructure - Replace all hindsight-api → hindsight-api-slim in test.yml - Replace hindsight → hindsight-all in test.yml - Add --extra embedded-db to test-embed API install step * ci: add local-ml and embedded-db extras to API sync steps These extras were previously implicit in the old hindsight-api package (which bundled everything). Now that hindsight-api-slim uses optional extras, we must explicitly request local-ml and embedded-db in CI. * ci: add API install step with embedded-db to test-embed smoke test The smoke test starts hindsight-api as a daemon, which requires pg0-embedded. Add a dedicated install step for hindsight-api-slim with embedded-db extra so the daemon can start successfully. * ci: remove --no-install-project when using optional extras When --no-install-project is combined with --extra, the optional deps are not installed because extras require the project to be active. Remove --no-install-project from steps that need local-ml or embedded-db. * ci: fix ordering of uv sync steps to preserve optional extras When uv sync runs for a different workspace member, it removes optional extras installed for other members. Fix by always running extra-requiring API sync last, after other workspace member syncs. Also remove --no-install-project from embedded-db sync in test-embed, as --no-install-project prevents optional extras from being active. * ci: add local-ml extra to test-embed API install for smoke test The smoke test starts the full API server which needs sentence-transformers for local embeddings (default provider). Add local-ml extra to the install. * ci: simplify extras with --all-extras and add slim pip smoke test - Replace explicit --extra local-ml --extra embedded-db with --all-extras for cleaner, more maintainable sync steps - Add test-pip-slim job: tests hindsight-api-slim[embedded-db] without local ML models, using Cohere for embeddings/reranking (mirrors Docker slim smoke test approach) * ci: simplify slim smoke test to health check only (mirrors Docker test)
253 lines
8.6 KiB
Python
253 lines
8.6 KiB
Python
"""
|
|
Pytest configuration and shared fixtures.
|
|
"""
|
|
import pytest
|
|
import pytest_asyncio
|
|
import asyncio
|
|
import os
|
|
import filelock
|
|
from pathlib import Path
|
|
from dotenv import load_dotenv
|
|
from hindsight_api import MemoryEngine, LLMConfig, LocalSTEmbeddings, RequestContext
|
|
|
|
from hindsight_api.engine.cross_encoder import LocalSTCrossEncoder
|
|
from hindsight_api.engine.query_analyzer import DateparserQueryAnalyzer
|
|
from hindsight_api.engine.task_backend import SyncTaskBackend
|
|
from hindsight_api.pg0 import EmbeddedPostgres
|
|
|
|
# Default pg0 instance configuration for tests
|
|
DEFAULT_PG0_INSTANCE_NAME = "hindsight-test"
|
|
DEFAULT_PG0_PORT = 5556
|
|
|
|
|
|
# Load environment variables from .env at the start of test session
|
|
def pytest_configure(config):
|
|
"""Load environment variables before running tests."""
|
|
# Look for .env in the workspace root (two levels up from tests dir)
|
|
env_file = Path(__file__).parent.parent.parent / ".env"
|
|
if env_file.exists():
|
|
load_dotenv(env_file)
|
|
else:
|
|
print(f"Warning: {env_file} not found, tests may fail without proper configuration")
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def db_url():
|
|
"""
|
|
Provide a PostgreSQL connection URL for tests.
|
|
|
|
If HINDSIGHT_API_DATABASE_URL is set, use it directly.
|
|
Otherwise, return None to indicate pg0 should be used (managed by pg0_instance fixture).
|
|
"""
|
|
return os.getenv("HINDSIGHT_API_DATABASE_URL")
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def pg0_db_url(db_url, tmp_path_factory, worker_id):
|
|
"""
|
|
Session-scoped fixture that ensures pg0 is running, migrations are applied,
|
|
and returns the database URL.
|
|
|
|
If HINDSIGHT_API_DATABASE_URL is set, uses that directly (no pg0 management).
|
|
Otherwise, starts pg0 once for the entire test session.
|
|
|
|
Uses filelock to ensure only one pytest-xdist worker starts pg0.
|
|
Migrations use PostgreSQL advisory locks internally, so they're safe to call
|
|
from multiple workers - only one will actually run migrations.
|
|
|
|
Note: We don't stop pg0 at the end because pytest-xdist runs workers in separate
|
|
processes that share the same pg0 instance. pg0 will persist for the next test run.
|
|
"""
|
|
if db_url:
|
|
# Use provided database URL directly
|
|
return db_url
|
|
|
|
# Get shared temp dir for coordination between xdist workers
|
|
if worker_id == "master":
|
|
# Running without xdist (-n 0 or no -n flag)
|
|
root_tmp_dir = tmp_path_factory.getbasetemp()
|
|
else:
|
|
# Running with xdist - use parent dir shared by all workers
|
|
root_tmp_dir = tmp_path_factory.getbasetemp().parent
|
|
|
|
# Use a lock file to ensure only one worker starts pg0
|
|
lock_file = root_tmp_dir / "pg0_setup.lock"
|
|
url_file = root_tmp_dir / "pg0_url.txt"
|
|
|
|
with filelock.FileLock(str(lock_file)):
|
|
if url_file.exists():
|
|
# Another worker already started pg0
|
|
url = url_file.read_text().strip()
|
|
else:
|
|
# First worker - start pg0
|
|
pg0 = EmbeddedPostgres(name=DEFAULT_PG0_INSTANCE_NAME, port=DEFAULT_PG0_PORT)
|
|
|
|
# Run ensure_running in a new event loop
|
|
loop = asyncio.new_event_loop()
|
|
try:
|
|
url = loop.run_until_complete(pg0.ensure_running())
|
|
finally:
|
|
loop.close()
|
|
|
|
# Save URL for other workers
|
|
url_file.write_text(url)
|
|
|
|
# Run migrations - uses PostgreSQL advisory lock internally,
|
|
# so safe to call from multiple workers (only one will actually run migrations)
|
|
from hindsight_api.migrations import run_migrations
|
|
run_migrations(url)
|
|
|
|
return url
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def request_context():
|
|
"""Provide a default RequestContext for tests."""
|
|
return RequestContext()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def llm_config():
|
|
"""
|
|
Provide LLM configuration for tests.
|
|
This can be used by tests that need to call LLM directly without memory system.
|
|
"""
|
|
return LLMConfig.for_memory()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def embeddings(tmp_path_factory, worker_id):
|
|
"""
|
|
Session-scoped embeddings fixture with filelock to prevent race conditions.
|
|
|
|
When pytest-xdist runs multiple workers in parallel, they all try to load
|
|
models from the HuggingFace cache simultaneously, which can cause race
|
|
conditions and meta tensor errors. We use a filelock to serialize model
|
|
initialization across workers.
|
|
"""
|
|
# Get shared temp dir for coordination between xdist workers
|
|
if worker_id == "master":
|
|
root_tmp_dir = tmp_path_factory.getbasetemp()
|
|
else:
|
|
root_tmp_dir = tmp_path_factory.getbasetemp().parent
|
|
|
|
lock_file = root_tmp_dir / "embeddings_init.lock"
|
|
|
|
emb = LocalSTEmbeddings()
|
|
|
|
# Serialize model initialization across workers
|
|
with filelock.FileLock(str(lock_file)):
|
|
loop = asyncio.new_event_loop()
|
|
try:
|
|
loop.run_until_complete(emb.initialize())
|
|
finally:
|
|
loop.close()
|
|
|
|
return emb
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def cross_encoder(tmp_path_factory, worker_id):
|
|
"""
|
|
Session-scoped cross-encoder fixture with filelock to prevent race conditions.
|
|
|
|
When pytest-xdist runs multiple workers in parallel, they all try to load
|
|
models from the HuggingFace cache simultaneously, which can cause race
|
|
conditions and meta tensor errors. We use a filelock to serialize model
|
|
initialization across workers.
|
|
"""
|
|
# Get shared temp dir for coordination between xdist workers
|
|
if worker_id == "master":
|
|
root_tmp_dir = tmp_path_factory.getbasetemp()
|
|
else:
|
|
root_tmp_dir = tmp_path_factory.getbasetemp().parent
|
|
|
|
lock_file = root_tmp_dir / "cross_encoder_init.lock"
|
|
|
|
ce = LocalSTCrossEncoder()
|
|
|
|
# Serialize model initialization across workers
|
|
with filelock.FileLock(str(lock_file)):
|
|
loop = asyncio.new_event_loop()
|
|
try:
|
|
loop.run_until_complete(ce.initialize())
|
|
finally:
|
|
loop.close()
|
|
|
|
return ce
|
|
|
|
@pytest.fixture(scope="session")
|
|
def query_analyzer():
|
|
return DateparserQueryAnalyzer()
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="function")
|
|
async def memory(pg0_db_url, embeddings, cross_encoder, query_analyzer):
|
|
"""
|
|
Provide a MemoryEngine instance for each test.
|
|
|
|
Must be function-scoped because:
|
|
1. pytest-xdist runs tests in separate processes with different event loops
|
|
2. asyncpg pools are bound to the event loop that created them
|
|
3. Each test needs its own pool in its own event loop
|
|
|
|
Uses small pool sizes since tests run in parallel.
|
|
Uses pg0_db_url (a postgresql:// URL) directly, so MemoryEngine won't try to
|
|
manage pg0 lifecycle - that's handled by the session-scoped pg0_db_url fixture.
|
|
Migrations are disabled here since they're run once at session scope in pg0_db_url.
|
|
Uses SyncTaskBackend so async tasks execute immediately (no worker needed).
|
|
"""
|
|
mem = MemoryEngine(
|
|
db_url=pg0_db_url, # Direct postgresql:// URL, not pg0://
|
|
memory_llm_provider=os.getenv("HINDSIGHT_API_LLM_PROVIDER", "groq"),
|
|
memory_llm_api_key=os.getenv("HINDSIGHT_API_LLM_API_KEY"),
|
|
memory_llm_model=os.getenv("HINDSIGHT_API_LLM_MODEL", "openai/gpt-oss-120b"),
|
|
memory_llm_base_url=os.getenv("HINDSIGHT_API_LLM_BASE_URL") or None,
|
|
embeddings=embeddings,
|
|
cross_encoder=cross_encoder,
|
|
query_analyzer=query_analyzer,
|
|
pool_min_size=1,
|
|
pool_max_size=5,
|
|
run_migrations=False, # Migrations already run at session scope
|
|
task_backend=SyncTaskBackend(), # Execute tasks immediately in tests
|
|
)
|
|
await mem.initialize()
|
|
yield mem
|
|
try:
|
|
if mem._pool and not mem._pool._closing:
|
|
await mem.close()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="function")
|
|
async def memory_no_llm_verify(pg0_db_url, embeddings, cross_encoder, query_analyzer):
|
|
"""
|
|
Provide a MemoryEngine instance that skips LLM connection verification.
|
|
|
|
This fixture is useful for tests that override the LLM configuration
|
|
after initialization (e.g., to test specific providers).
|
|
"""
|
|
mem = MemoryEngine(
|
|
db_url=pg0_db_url,
|
|
memory_llm_provider="mock", # Use mock provider as placeholder
|
|
memory_llm_api_key="",
|
|
memory_llm_model="mock",
|
|
embeddings=embeddings,
|
|
cross_encoder=cross_encoder,
|
|
query_analyzer=query_analyzer,
|
|
pool_min_size=1,
|
|
pool_max_size=5,
|
|
run_migrations=False,
|
|
task_backend=SyncTaskBackend(),
|
|
skip_llm_verification=True, # Skip verification - will be overridden by test
|
|
)
|
|
await mem.initialize()
|
|
yield mem
|
|
try:
|
|
if mem._pool and not mem._pool._closing:
|
|
await mem.close()
|
|
except Exception:
|
|
pass
|