diff --git a/tests/integration/test_pg_authority.py b/tests/integration/test_pg_authority.py index fdae7ce954..07728b24dd 100644 --- a/tests/integration/test_pg_authority.py +++ b/tests/integration/test_pg_authority.py @@ -2421,6 +2421,18 @@ async def test_a_reject_is_the_authoritys_head_and_clickhouses_row( ns = f"pg-{uuid.uuid4().hex[:8]}" assert (await call("memory.set", _set(ns, "k", "a fact to reject"))).status_code == 200 before = int((await _head_row(pg_conn, org, ns, "k"))["version"]) + + # A new set answers at commit; reject copies the vectors already published. + async def document_published() -> bool: + rows = ch_client.query( + "SELECT status, version FROM loom.memory_embeddings FINAL " + "WHERE org = {org:String} AND namespace = {ns:String} AND memory_key = 'k'", + settings=_SEQUENTIAL, + parameters={"org": org, "ns": ns}, + ).result_rows + return rows == [("active", before)] + + await eventually(document_published, description="the initial vector is published") body = {"namespace": ns, "agent": "a", "memory_key": "k", "rejection_reason": "wrong"} rejected = await call("memory.reject", body) assert rejected.status_code == 200 and rejected.json()["rejected"] is True, rejected.text diff --git a/tests/unit/test_pg_reject_check_publication.py b/tests/unit/test_pg_reject_check_publication.py new file mode 100644 index 0000000000..8a3e0ebcc5 --- /dev/null +++ b/tests/unit/test_pg_reject_check_publication.py @@ -0,0 +1,92 @@ +"""The reject integration check observes the vector it is about to copy.""" + +from __future__ import annotations + +import asyncio +from types import SimpleNamespace +from typing import Any + +import httpx +import pytest + +from tests.integration import test_pg_authority as integration + + +class Publication: + def __init__(self, delayed: bool) -> None: + self.delayed = delayed + self.head = {"version": 11, "status": "active", "rejection_reason": ""} + self.document = None if delayed else ("active", 11) + self.read_pending = asyncio.Event() + self.published = asyncio.Event() + if not delayed: + self.published.set() + + async def publish(self) -> None: + await self.read_pending.wait() + self.document = ("active", 11) + self.published.set() + + async def call(self, op: str, body: dict[str, Any]) -> httpx.Response: + assert body["agent"] == "a" and body["memory_key"] == "k" + if op == "memory.set": + assert body["memory_type"] == "semantic" and body["value"] == "a fact to reject" + return httpx.Response(200, json={}) + assert op == "memory.reject" and body["rejection_reason"] == "wrong" + self.head = {"version": 12, "status": "rejected", "rejection_reason": "wrong"} + if self.document is not None: + self.document = ("rejected", 12) + return httpx.Response(200, json={"rejected": True}) + + async def fetchval(self, sql: str, org: str, ns: str, key: str) -> dict[str, Any]: + assert "FROM loom.memory_current" in sql and org == "fixture-org" and key == "k" + assert ns.startswith("pg-") + return dict(self.head) + + def query( + self, sql: str, *, parameters: dict[str, Any], settings: dict[str, Any] | None = None + ) -> Any: + assert parameters["org"] == "fixture-org" and parameters["ns"].startswith("pg-") + if "FROM loom.memory_embeddings" in sql: + assert settings == integration._SEQUENTIAL + rows = [] if self.document is None else [self.document] + if self.document is None: + self.read_pending.set() + return SimpleNamespace(result_rows=rows) + assert "FROM loom.memory FINAL" in sql + return SimpleNamespace(result_rows=[(self.head["status"], self.head["rejection_reason"])]) + + async def eventually( + self, + predicate: Any, + *, + description: str, + max_wait: float = 15.0, + interval: float = 0.1, + ) -> None: + assert (max_wait, interval) == (15.0, 0.1) + if await predicate(): + return + # The first empty read releases publication, without a clock or database. + assert self.read_pending.is_set() + await self.published.wait() + assert await predicate(), description + + +@pytest.mark.asyncio +@pytest.mark.parametrize("delayed", [False, True], ids=["published", "publication-pending"]) +async def test_reject_check_waits_for_the_initial_vector( + monkeypatch: pytest.MonkeyPatch, delayed: bool +) -> None: + publication = Publication(delayed) + monkeypatch.setattr(integration, "eventually", publication.eventually) + publisher = asyncio.create_task(publication.publish()) if delayed else None + try: + await integration.test_a_reject_is_the_authoritys_head_and_clickhouses_row( + publication.call, publication, "fixture-org", publication + ) + finally: + if publisher is not None: + publisher.cancel() + await asyncio.gather(publisher, return_exceptions=True) + assert publication.document == ("rejected", 12) diff --git a/tests/unit/test_source_repo_search.py b/tests/unit/test_source_repo_search.py index c270191bdb..51999084f9 100644 --- a/tests/unit/test_source_repo_search.py +++ b/tests/unit/test_source_repo_search.py @@ -399,12 +399,17 @@ async def expire_before_renewal(request, plan): assert not children["home"].lost and "check:home" in events -async def test_timed_out_extra_fallback_keeps_loss_sticky_and_preserves_base_rebuild(monkeypatch): +@pytest.mark.asyncio(loop_scope="function") +async def test_timed_out_extra_fallback_keeps_loss_sticky_and_preserves_base_rebuild( + monkeypatch, renewal_clock +): from unittest.mock import AsyncMock events, children = install(monkeypatch) + clock, active = renewal_clock original = search.run_checked_search canceled = [] + at_expiry = [] async def run(request, plan): result = await original(request, plan) @@ -412,6 +417,16 @@ async def run(request, plan): async def block(*_args): try: + deadline, scope = active[-1] + at_expiry.append( + ( + children["repo"].lost, + children["repo"].proofs, + deadline - clock.monotonic(), + ) + ) + clock.advance(0.01) + scope.reschedule(asyncio.get_running_loop().time()) await asyncio.Event().wait() finally: canceled.append("drained") @@ -425,6 +440,7 @@ async def block(*_args): request = await repo_docs.resolve_repo_search(INPUT, repo_namespace="repo") result = await repo_docs.run_repo_search(request, source_access.ReadPlan("acme", marked=True)) out = await asyncio.wait_for(result.current(), timeout=0.2) + assert at_expiry == [(True, None, 0.01)] assert [h.source_uri for h in out.results] == ["ordinary:home"] assert result.lost and result.extra is None and canceled == ["drained"] assert children["repo"]._failed