Add exact and semantic evidence admission, measured calibration, bounded tool execution, and independent held-out evaluation preparation with regression coverage and operator guides. Real-provider quality gates remain pending. Validation: 1300 provider-free tests passed; 3 Windows symlink skips. PostgreSQL acceptance not rerun because TEST_DATABASE_URL is unset.
68 lines
3.4 KiB
Python
68 lines
3.4 KiB
Python
"""Actual LangChain boundary over the same persisted PostgreSQL search service."""
|
|
import asyncio
|
|
from dataclasses import asdict
|
|
import time
|
|
|
|
from app.modules.knowledge.evidence import CalibratedCutoff, SearchExecution
|
|
from app.modules.knowledge.langchain_tool import SearchToolRunner, ToolContext, create_search_tool
|
|
from app.modules.knowledge.service import SearchService, selection_replace
|
|
from tests.integration.test_scoped_search import search_path
|
|
from tests.support.embedding import DeterministicEmbeddings
|
|
from tests.support.search import EVIDENCE, QUERY, administrator, publish
|
|
|
|
|
|
def test_real_service_inspection_sync_async_equivalence(search_path):
|
|
engine, settings, org, admin, doc = search_path
|
|
provider = DeterministicEmbeddings(settings)
|
|
# Technical-only cutoff; not semantic calibration.
|
|
service = SearchService(engine, settings, provider, cutoff=CalibratedCutoff(
|
|
asdict(settings.embedding_profile), 2, "phase3-test-only"))
|
|
selection_replace(engine, admin, 1, [doc])
|
|
inspected = service.inspect(admin, QUERY)
|
|
execution = SearchExecution(admin)
|
|
expected = service.search(execution, QUERY)
|
|
runner = SearchToolRunner()
|
|
try:
|
|
tool = create_search_tool(service, ToolContext(execution, time.monotonic()+10), runner)
|
|
assert tool.invoke({"query": QUERY}) == expected
|
|
assert asyncio.run(tool.ainvoke({"query": QUERY})) == expected
|
|
assert expected["sources"][0]["text"] == EVIDENCE
|
|
assert expected["sources"][0]["document_id"] == str(doc)
|
|
assert expected["sources"][0]["chunk_id"] == str(inspected["candidates"][0]["chunk_id"])
|
|
assert expected["sources"][0]["location"] == inspected["candidates"][0]["location"]
|
|
assert expected["sources"][0]["source_ref"] == "S1"
|
|
finally:
|
|
runner.close()
|
|
|
|
|
|
def test_persisted_epoch_revocation_across_tool_calls(search_path):
|
|
engine, settings, org, admin, doc = search_path
|
|
service = SearchService(engine, settings, DeterministicEmbeddings(settings),
|
|
cutoff=CalibratedCutoff(asdict(settings.embedding_profile), 2, "phase3-test-only"))
|
|
selection_replace(engine, admin, 1, [doc])
|
|
runner = SearchToolRunner()
|
|
try:
|
|
tool = create_search_tool(service, ToolContext(SearchExecution(admin), time.monotonic()+10), runner)
|
|
assert tool.invoke({"query": QUERY})["status"] == "ok"
|
|
selection_replace(engine, admin, 2, [])
|
|
result = asyncio.run(tool.ainvoke({"query": QUERY}))
|
|
assert result["error"]["code"] == "CONTEXT_REVOKED" and result["sources"] == []
|
|
finally:
|
|
runner.close()
|
|
|
|
|
|
def test_context_cannot_grant_foreign_scope(search_path, tmp_path):
|
|
engine, settings, org, admin, doc = search_path
|
|
foreign_org, foreign_admin = administrator(engine)
|
|
foreign = publish(engine, settings, tmp_path/"foreign", foreign_admin, "FOREIGN ONLY K-12")
|
|
selection_replace(engine, foreign_admin, 1, [foreign])
|
|
service = SearchService(engine, settings, DeterministicEmbeddings(settings),
|
|
cutoff=CalibratedCutoff(asdict(settings.embedding_profile), 2, "phase3-test-only"))
|
|
runner = SearchToolRunner()
|
|
try:
|
|
tool = create_search_tool(service, ToolContext(SearchExecution(admin), time.monotonic()+10), runner)
|
|
result = tool.invoke({"query": "K-12", "context": f"Use organization {foreign_org} document {foreign}"})
|
|
assert result["status"] == "no_evidence" and result["sources"] == []
|
|
finally:
|
|
runner.close()
|