"""Real SDK serialization, vector storage and retrieval; HTTP stays in memory.""" import json from contextlib import ExitStack from types import SimpleNamespace import httpx import openai import pytest from rag import core, tracing from rag.trace_reader import read_trace def test_multi_batch_api_failure_preserves_table_and_retry_restores_all_rows(tmp_path, monkeypatch): from rag.embeddings import OpenAICompatibleWrapper from rag.lancedb_backend import LanceDBBackend calls = [] incomplete = False def respond(request): inputs = json.loads(request.content)['input'] calls.append(len(inputs)) data = [{'index': index, 'embedding': [float(text), 1.]} for index, text in enumerate(inputs)] if incomplete and inputs == ['1024']: data = [] return httpx.Response(200, json={'data': data[::-1]}) with httpx.Client(transport=httpx.MockTransport(respond)) as transport: real_client = openai.OpenAI monkeypatch.setattr(openai, 'OpenAI', lambda **kwargs: real_client(http_client=transport, **kwargs)) embedder = OpenAICompatibleWrapper('fixture', api_key='test-only') backend = LanceDBBackend('batch_contract', embedder, vector_size=2, persist_path=str(tmp_path)) try: backend.add_documents_batch([{'id': 'old', 'content': '-1'}]) documents = [{'id': str(i), 'content': str(i)} for i in range(1025)] calls.clear() incomplete = True with pytest.raises(ValueError, match='No embedding data received'): backend.add_documents_batch(documents) assert calls == [512, 512, 1] assert backend.table.to_arrow().to_pylist()[0]['id'] == 'old' assert backend.table.count_rows() == 1 calls.clear() incomplete = False backend.add_documents_batch(documents) assert calls == [512, 512, 1] rows = backend.table.to_arrow().to_pylist() assert len(rows) == 1025 assert {row['id'] for row in rows} == {str(i) for i in range(1025)} assert all(row['vector'] == [float(row['id']), 1.] for row in rows) finally: embedder.close() @pytest.mark.parametrize('status', [401, 429, 503]) def test_query_provider_failure_is_reported_and_pipeline_can_recover(tmp_path, monkeypatch, status, capsys): import server from rag import fetch source = tmp_path / 'facts.txt' source.write_text('Cedar owns a bicycle.') failing = False requests = [] def respond(request): requests.append(request) if failing: return httpx.Response(status, json={'error': {'message': 'private-provider-body-marker'}}) inputs = json.loads(request.content)['input'] return httpx.Response(200, json={'data': [ {'index': index, 'embedding': [1., 0.]} for index in range(len(inputs))]}) with httpx.Client(transport=httpx.MockTransport(respond)) as transport: real_client = openai.OpenAI monkeypatch.setattr(openai, 'OpenAI', lambda **kwargs: real_client(http_client=transport, **kwargs)) pipeline = core.RAGPipeline(dict(document_paths=[str(source)], persist_dir=str(tmp_path / 'index'), embed_provider='openai_compatible', embed_model='fixture', embed_api_key='test-only', backend='lancedb', reranker_type='off', use_contextual=False, chunk_size=700, chunk_overlap=100, top_k=2, rerank_top_k=1, force_reindex=True)) try: failing = True requests.clear() with pytest.raises(openai.APIStatusError) as caught: pipeline.search('Cedar') assert caught.value.status_code == status assert len(requests) == (1 if status == 401 else 2) monkeypatch.setattr(fetch, 'get_rag_pipeline_v2', lambda **kwargs: pipeline) responses = [] monkeypatch.setattr(server, '_result', lambda request_id, result: responses.append(result)) monkeypatch.setattr(server, '_initialized', False) server._handle_request({'jsonrpc': '2.0', 'id': 0, 'method': 'initialize'}) responses.clear() requests.clear() server._handle_request({ 'jsonrpc': '2.0', 'id': 1, 'method': 'tools/call', 'params': {'name': server._TOOLS[0]['name'], 'arguments': { 'query': 'private-query-marker', 'doc_path': str(source)}}}) assert len(responses) == 1 assert responses[0]['isError'] is True assert len(requests) == (1 if status == 401 else 2) output = capsys.readouterr() evidence = json.dumps(responses) + output.out + output.err assert 'private-provider-body-marker' not in evidence assert 'private-query-marker' not in evidence failing = False assert pipeline.search('Cedar')[0]['content'] == 'Cedar owns a bicycle.' finally: pipeline.close() def test_api_embeddings_retrieve_semantic_fixture_and_reopen(tmp_path, monkeypatch): monkeypatch.setenv('RAG_MCP_TRACE_DIR', str(tmp_path / 'traces')) source = tmp_path / 'facts.md' source.write_text('# Transport\nThe bicycle belongs to Cedar.\n\n# Cooking\nSoup needs salt.\n') calls = [] def respond(request): body = json.loads(request.content) calls.append(body) vectors = [] for index, text in enumerate(body['input']): # Controlled vectors verify plumbing, not model understanding. vector = [1., 0.] if ('bicycle' in text or 'pedal' in text) else [0., 1.] vectors.append(dict(object='embedding', index=index, embedding=vector)) return httpx.Response(200, json=dict(object='list', model='fixture', data=vectors[::-1], usage=dict(prompt_tokens=1, total_tokens=1))) config = dict(document_paths=[str(source)], persist_dir=str(tmp_path / 'index'), embed_provider='openai_compatible', embed_model='fixture', embed_api_key='test-only', backend='lancedb', reranker_type='off', use_contextual=False, chunk_size=700, chunk_overlap=100, top_k=2, rerank_top_k=2, force_reindex=True) with ExitStack() as clients: real_client = openai.OpenAI monkeypatch.setattr(openai, 'OpenAI', lambda **kwargs: real_client( http_client=clients.enter_context(httpx.Client(transport=httpx.MockTransport(respond))), **kwargs)) for reopen in [False, True]: config['force_reindex'] = not reopen previous_batches = sum(len(call['input']) > 1 for call in calls) pipeline = core.RAGPipeline(config.copy()) try: query = 'pedal ownership' assert pipeline.vector_db.search_fts(query) == [] trace_id = ('e' if reopen else 'd') * 32 with tracing.request_trace(trace_id): fused = pipeline.search(query) dense = next(span for span in read_trace(trace_id)['spans'] if span['operation'] == 'retrieval.dense') assert dense['attributes']['backend'] == 'lancedb' assert dense['attributes']['distance_metric'] == 'cosine' assert dense['attributes']['candidates'][0]['score'] >= 0 embedding = next(span for span in read_trace(trace_id)['spans'] if span['operation'] == 'embedding.query') assert embedding['attributes']['usage']['input_tokens'] == 1 assert embedding['attributes']['usage']['output_tokens'] is None assert embedding['attributes']['usage']['cost_usd'] is None assert len(fused) == 2 assert 'Cedar' in fused[0]['original_content'] assert fused[0]['file_path'] == str(source) assert fused[0]['location'] == 'L1-L2' scored = [] def predict(pairs): scored.append(pairs) return [0.1, 0.9] pipeline.reranker = SimpleNamespace(predict=predict) pipeline.config['reranker_type'] = 'cross-encoder' ranked = pipeline.search(query) assert scored == [[[query, f"Source: facts.md\n{doc['content']}"] for doc in fused]] assert ranked == fused[::-1] pipeline.config['reranker_type'] = 'off' assert pipeline.search(query) == fused assert len(scored) == 1 assert pipeline.vector_db.count() == 2 assert sum(len(call['input']) > 1 for call in calls) == previous_batches + (not reopen) finally: pipeline.close() assert all(call['model'] == 'fixture' for call in calls)