Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

dx(backend/copilot): add dream e2e integration test for envelope → tentative edge → ratification (#13777)

Krzysztof Czerwinski2ad2b3847ccd
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/client.py b/autogpt_platform/backend/backend/copilot/graphiti/client.py
index 54d231d849..1181fe9437 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/client.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/client.py
@@ -133,12 +133,27 @@ def _build_llm_config():
)
-def _build_graphiti(group_id: str, llm_client):
+def _build_graphiti(
+ group_id: str,
+ llm_client,
+ *,
+ embedder=None,
+ cross_encoder=None,
+ graph_driver=None,
+):
"""Construct a ``Graphiti`` instance bound to a per-group FalkorDB.
Pure factory: no caching. Callers decide whether to memoize.
``llm_client`` lets the caller pick the LLM-tier behavior (sync vs
flex) without disturbing the embedder + cross-encoder defaults.
+
+ The keyword overrides exist so integration tests can substitute
+ individual boundaries (a stub embedder / cross-encoder, a driver bound
+ to a scratch database) while still building the client through THIS
+ function. One construction site means a kwarg added here reaches the
+ tests too, instead of silently drifting from a hand-mirrored copy.
+ Production passes none of them; each defaults to the real component.
+ ``group_id`` is only consulted when ``graph_driver`` is not supplied.
"""
from graphiti_core import Graphiti
from graphiti_core.embedder import OpenAIEmbedder, OpenAIEmbedderConfig
@@ -147,12 +162,13 @@ def _build_graphiti(group_id: str, llm_client):
from .falkordb_driver import AutoGPTFalkorDriver
from .reranker import CompatOpenAIRerankerClient
- embedder_config = OpenAIEmbedderConfig(
- api_key=graphiti_config.resolve_embedder_api_key(),
- embedding_model=graphiti_config.embedder_model,
- base_url=graphiti_config.resolve_embedder_base_url(),
- )
- embedder = OpenAIEmbedder(config=embedder_config)
+ if embedder is None:
+ embedder_config = OpenAIEmbedderConfig(
+ api_key=graphiti_config.resolve_embedder_api_key(),
+ embedding_model=graphiti_config.embedder_model,
+ base_url=graphiti_config.resolve_embedder_base_url(),
+ )
+ embedder = OpenAIEmbedder(config=embedder_config)
# P-1.4: cross-encoder reranker for warm-context retrieval.
# Runs concurrent boolean-classifier prompts (one per candidate
@@ -161,19 +177,21 @@ def _build_graphiti(group_id: str, llm_client):
# of small calls per session-start search. The Compat subclass
# fixes the stock client's max_tokens=1, which OpenAI-compatible
# upstreams now reject with a 400 (minimum is 16).
- reranker_config = LLMConfig(
- api_key=graphiti_config.resolve_llm_api_key(),
- model=graphiti_config.reranker_model,
- base_url=graphiti_config.resolve_llm_base_url(),
- )
- cross_encoder = CompatOpenAIRerankerClient(config=reranker_config)
-
- graph_driver = AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=group_id,
- )
+ if cross_encoder is None:
+ reranker_config = LLMConfig(
+ api_key=graphiti_config.resolve_llm_api_key(),
+ model=graphiti_config.reranker_model,
+ base_url=graphiti_config.resolve_llm_base_url(),
+ )
+ cross_encoder = CompatOpenAIRerankerClient(config=reranker_config)
+
+ if graph_driver is None:
+ graph_driver = AutoGPTFalkorDriver(
+ host=graphiti_config.falkordb_host,
+ port=graphiti_config.falkordb_port,
+ password=graphiti_config.falkordb_password or None,
+ database=group_id,
+ )
return Graphiti(
llm_client=llm_client,
embedder=embedder,
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/conftest.py b/autogpt_platform/backend/backend/copilot/graphiti/conftest.py
index 586abcb1d1..ba04462062 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/conftest.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/conftest.py
@@ -13,6 +13,11 @@ Two responsibilities:
gracefully when FalkorDB is not reachable, so local unit-test runs
stay fast.
+3. **Stub the LLM boundary.** ``stub_graphiti_client`` builds a real
+ ``Graphiti`` over a live FalkorDB driver with canned LLM responses,
+ so a test can drive the genuine ``add_episode`` pipeline — entity /
+ edge extraction, resolution, persistence — without an API key.
+
Run integration tests with the platform docker-compose stack up::
cd autogpt_platform
@@ -27,16 +32,93 @@ The fixtures connect using ``GraphitiConfig`` defaults
import socket
import uuid
+from collections.abc import Iterable
from datetime import datetime, timezone
from typing import AsyncIterator
import pytest
import pytest_asyncio
+from graphiti_core import Graphiti
+from graphiti_core.cross_encoder.client import CrossEncoderClient
+from graphiti_core.embedder.client import EMBEDDING_DIM, EmbedderClient
+from graphiti_core.llm_client.client import LLMClient
+from graphiti_core.llm_client.config import ModelSize
+from graphiti_core.prompts.models import Message
+from pydantic import BaseModel
+from .client import _build_graphiti
from .config import graphiti_config
from .falkordb_driver import AutoGPTFalkorDriver
+class ScriptedLLMClient(LLMClient):
+ """Real ``LLMClient`` whose responses are canned per response model.
+
+ Keyed by ``response_model.__name__`` so one instance can serve every
+ prompt ``add_episode`` issues. Unlisted models get ``{}`` — graphiti's
+ per-entity-type attribute models are all-optional, so an empty payload
+ validates into defaults.
+ """
+
+ def __init__(self, responses: dict[str, dict] | None = None) -> None:
+ super().__init__(config=None, cache=False)
+ self.responses = responses or {}
+
+ async def _generate_response(
+ self,
+ messages: list[Message],
+ response_model: type[BaseModel] | None = None,
+ max_tokens: int = 16384,
+ model_size: ModelSize = ModelSize.medium,
+ ) -> dict:
+ name = response_model.__name__ if response_model else "raw"
+ return self.responses.get(name, {})
+
+
+class StubEmbedder(EmbedderClient):
+ """Constant vectors — no integration test ranks by similarity."""
+
+ async def create(
+ self, input_data: str | list[str] | Iterable[int] | Iterable[Iterable[int]]
+ ) -> list[float]:
+ return [0.1] * EMBEDDING_DIM
+
+ async def create_batch(self, input_data_list: list[str]) -> list[list[float]]:
+ return [[0.1] * EMBEDDING_DIM for _ in input_data_list]
+
+
+class StubCrossEncoder(CrossEncoderClient):
+ """Reranking is a search-path concern; ingestion never calls it."""
+
+ async def rank(self, query: str, passages: list[str]) -> list[tuple[str, float]]:
+ return [(passage, 1.0) for passage in passages]
+
+
+def stub_graphiti_client():
+ """Factory building a ``Graphiti`` with only the LLM boundary stubbed.
+
+ Call as ``stub_graphiti_client(driver, responses)``. Builds through the
+ production factory ``client._build_graphiti``, overriding only the three
+ boundaries that would otherwise make network calls, so the ingestion
+ pipeline under test behaves as it does in production minus the API
+ calls — and so a kwarg added to production wiring reaches these tests
+ instead of drifting from a hand-mirrored copy.
+ """
+
+ def _build(driver: AutoGPTFalkorDriver, responses: dict[str, dict]) -> Graphiti:
+ return _build_graphiti(
+ # Unused: the driver below already carries the test database.
+ group_id="",
+ llm_client=ScriptedLLMClient(responses),
+ embedder=StubEmbedder(),
+ cross_encoder=StubCrossEncoder(),
+ graph_driver=driver,
+ )
+
+ return _build
+
+
@pytest_asyncio.fixture(scope="session", loop_scope="session")
async def server(): # type: ignore[override]
"""No-op server stub — graphiti tests don't need the full backend."""
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/dream_ratification_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/dream_ratification_integration_test.py
new file mode 100644
index 0000000000..e61b431ceb
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/graphiti/dream_ratification_integration_test.py
@@ -0,0 +1,510 @@
+"""End-to-end dream test: envelope → tentative edge → ratification (SECRT-2482).
+
+Every existing ratification suite pre-fabricates its ``status='tentative'``
+rows — ``dream/ratification_test.py`` mocks the FalkorDB driver outright, and
+the edge-stamping tests hand-build edges. Nothing asserted that a real
+``MemoryEnvelope`` pushed through the real ingestion path actually LANDS a
+tentative edge carrying the envelope's ``source_kind``/``provenance``, which
+is how the edge-attribute no-op bug fixed in #13390 went undetected.
+
+This file closes that gap. It drives the production write path
+(``dream/apply._write_proposed_finding`` → ``ingest.enqueue_episode`` →
+``Graphiti.add_episode`` → ``ingest._stamp_edge_metadata``) against a live
+FalkorDB, then runs both ratification legs on the edge that path produced:
+``try_ratify_on_hit`` (promote on a warm-context hit) and
+``run_ratification_pass`` (the nightly sweep that supersedes a tentative edge
+which never earned a hit before its grace period elapsed).
+
+Only the LLM boundary is stubbed — no API key, no network.
+
+Lives in ``graphiti/`` rather than ``dream/`` for the local conftest, which
+supplies the ``falkordb_available`` skip guard, the stubbed Graphiti client,
+and the no-op ``server`` / ``graph_cleanup`` overrides that keep this suite
+off SpinTestServer (postgres + rabbitmq). ``dream/`` has no conftest, and
+adding one would change how the existing dream unit tests run.
+
+Run with the platform stack up::
+
+ cd autogpt_platform && docker compose up -d falkordb && cd backend
+ poetry run pytest backend/copilot/graphiti/dream_ratification_integration_test.py -xvs
+"""
+
+import asyncio
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import Any, AsyncIterator
+
+import pytest
+import pytest_asyncio
+
+from backend.copilot.dream import apply as apply_mod
+from backend.copilot.dream.ratification import run_ratification_pass, try_ratify_on_hit
+from backend.copilot.dream.ratification_hits import (
+ RATIFICATION_GRACE_PERIOD,
+ get_hit_count,
+ parse_created_at,
+ record_memory_hit,
+)
+from backend.copilot.dream.schemas import ProposedFinding
+
+from . import ingest as ingest_mod
+from .client import derive_group_id
+from .config import graphiti_config
+from .falkordb_driver import AutoGPTFalkorDriver
+
+PASS_ID = "e2e-pass"
+SESSION_ID = "e2e-session"
+FACT_TEXT = "Atlas deploys are gated on the smoke suite"
+# Everything slow is stubbed (LLM, embedder, cross-encoder), so ingestion is
+# local Cypher only. Kept well above the ~1s real cost, but low enough that a
+# pipeline that never signals completion fails fast instead of burning a
+# minute per test.
+INGEST_TIMEOUT_SECONDS = 25.0
+
+PROPOSAL = ProposedFinding(
+ content=FACT_TEXT,
+ scope="project:atlas",
+ confidence=0.62,
+ rationale="Two weak-linked episodes both mention the smoke suite blocking a deploy.",
+)
+
+
+# --- Scripted LLM output ----------------------------------------------------
+
+# ``entity_type_id: 0`` is graphiti's built-in 'Entity' type, so both nodes
+# carry only the base label — the case the ('Entity','Entity') wildcard in
+# ``EDGE_TYPE_MAP`` exists to cover (the #13389 bug). ``relation_type`` must
+# name a key of ``EDGE_TYPES`` or graphiti skips edge-attribute extraction
+# altogether and the stamp would have nothing to correct.
+_SCRIPTED_RESPONSES: dict[str, dict] = {
+ "ExtractedEntities": {
+ "extracted_entities": [
+ {"name": "Atlas", "entity_type_id": 0},
+ {"name": "smoke suite", "entity_type_id": 0},
+ ]
+ },
+ "NodeResolutions": {
+ "entity_resolutions": [
+ {"id": 0, "name": "Atlas", "duplicate_name": ""},
+ {"id": 1, "name": "smoke suite", "duplicate_name": ""},
+ ]
+ },
+ "ExtractedEdges": {
+ "edges": [
+ {
+ "source_entity_name": "Atlas",
+ "target_entity_name": "smoke suite",
+ "relation_type": "MemoryFact",
+ "fact": FACT_TEXT,
+ "valid_at": None,
+ "invalid_at": None,
+ }
+ ]
+ },
+ # The MemoryFact attribute extractor only ever sees the fact TEXT, so in
+ # production it returns the model defaults — which contradict the dream
+ # envelope on every field that matters. Reproducing that here is what
+ # makes the stamp assertions below meaningful: every value the tests
+ # assert is one this payload got wrong.
+ "MemoryFact": {
+ "status": "active",
+ "source_kind": "user_asserted",
+ "scope": "real:global",
+ "confidence": None,
+ "provenance": None,
+ },
+}
+
+
+class _FakeRedis:
+ """In-memory stand-in for the ``mem:hits:*`` counters.
+
+ The hit counter is best-effort by design (``record_memory_hit`` swallows
+ every failure), so pointing it at the dev Redis cluster would only add
+ flakiness. What these tests pin is the Cypher status transition each hit
+ count drives.
+ """
+
+ def __init__(self) -> None:
+ self.hits: dict[str, int] = {}
+
+ async def get(self, key: str) -> bytes | None:
+ # Key-presence, not truthiness: real Redis returns b"0" for a key
+ # explicitly set to 0, and ``record_memory_hit`` seeds exactly that
+ # before its INCR. Keying off the value would report a seeded
+ # counter as absent.
+ return str(self.hits[key]).encode() if key in self.hits else None
+
+ async def set(self, key: str, value: int, **kwargs: object) -> bool:
+ # Honour NX like real Redis: ``record_memory_hit`` seeds with
+ # ``set(key, 0, nx=True, ex=…)`` before INCR, so an unconditional
+ # overwrite would reset the counter on every hit and cap it at 1.
+ # Without NX, SET overwrites — setdefault everywhere would instead
+ # mask a genuine counter reset.
+ if kwargs.get("nx") and key in self.hits:
+ return False
+ self.hits[key] = int(value)
+ return True
+
+ async def incr(self, key: str) -> int:
+ self.hits[key] = self.hits.get(key, 0) + 1
+ return self.hits[key]
+
+ async def expire(self, key: str, ttl_seconds: int) -> bool:
+ return True
+
+
+# --- Fixtures ---------------------------------------------------------------
+
+
+@pytest_asyncio.fixture(loop_scope="function")
+async def dream_graph(
+ falkordb_available: bool,
+) -> AsyncIterator[tuple[AutoGPTFalkorDriver, str]]:
+ """A per-test FalkorDB database named ``derive_group_id(user_id)``.
+
+ Deliberately not the shared ``clean_graph`` fixture: that one mints a
+ standalone ``test_*`` database, while ratification opens its OWN driver
+ from the user_id and would read an empty graph. Yields ``(driver,
+ user_id)``; the graph key is dropped afterwards.
+ """
+ user_id = f"test-{uuid.uuid4().hex[:16]}"
+ driver = AutoGPTFalkorDriver(
+ host=graphiti_config.falkordb_host,
+ port=graphiti_config.falkordb_port,
+ password=graphiti_config.falkordb_password or None,
+ database=derive_group_id(user_id),
+ build_indices=False,
+ )
+ try:
+ yield driver, user_id
+ finally:
+ # A true GRAPH.DELETE: DETACH DELETE only empties the graph and
+ # leaks the ``user_test-*`` graphdata key on the shared instance —
+ # indistinguishable from a real user graph to ops tooling. A
+ # failed delete surfaces as a test error; the driver still closes.
+ try:
+ try:
+ await driver._get_graph(None).delete()
+ except Exception as exc:
+ if "empty key" not in str(exc).lower():
+ raise
+ finally:
+ await driver.close()
+
+
[email protected](autouse=True)
+def stub_boundaries(
+ mocker, dream_graph: tuple[AutoGPTFalkorDriver, str], stub_graphiti_client
+):
+ """Replace the three external boundaries ingestion would otherwise reach.
+
+ Intentionally a SYNC fixture depending on the async ``dream_graph``:
+ pytest-asyncio resolves the async dependency before invoking this one, so
+ the tuple is already awaited here. Collection and the live run both pass —
+ the sync/async mix is deliberate, not an oversight.
+
+ * ``get_graphiti_client`` — patched on ``ingest`` (where it is used) to
+ return a Graphiti wired to the test's real driver with the LLM stubbed.
+ * ``ensure_dream_system_scheduled`` — ``_ensure_worker`` imports it lazily
+ from ``dream.scheduling``, so that module IS the use site. Left live it
+ would fire LaunchDarkly + scheduler RPCs from a background task.
+ * ``get_redis_async`` — likewise imported lazily by the hit tracker.
+ """
+ driver, _user_id = dream_graph
+ client = stub_graphiti_client(driver, _SCRIPTED_RESPONSES)
+ mocker.patch.object(
+ ingest_mod, "get_graphiti_client", mocker.AsyncMock(return_value=client)
+ )
+ mocker.patch(
+ "backend.copilot.dream.scheduling.ensure_dream_system_scheduled",
+ mocker.AsyncMock(return_value={}),
+ )
+ mocker.patch(
+ "backend.data.redis_client.get_redis_async",
+ mocker.AsyncMock(return_value=_FakeRedis()),
+ )
+ return client
+
+
+@pytest_asyncio.fixture(loop_scope="function", autouse=True)
+async def cancel_ingestion_workers() -> AsyncIterator[None]:
+ """Stop the per-user ingestion worker before the test's loop closes.
+
+ ``_ensure_worker`` spawns a task that idles for 60s waiting for more
+ episodes. Left alone it outlives the test and pytest reports "Task was
+ destroyed but it is pending". Reaching into ``_get_loop_state`` is the
+ only handle on it — the registry is loop-local by design.
+ """
+ yield
+ state = ingest_mod._get_loop_state()
+ workers = list(state.user_workers.values())
+ for worker in workers:
+ worker.cancel()
+ if workers:
+ await asyncio.gather(*workers, return_exceptions=True)
+
+
+# --- Helpers ----------------------------------------------------------------
+
+
+async def _ingest_dream_proposal(user_id: str) -> None:
+ """Write one dream proposal through the production path and await it."""
+ completion = ingest_mod.IngestionCompletion()
+ queued = await apply_mod._write_proposed_finding(
+ user_id,
+ PASS_ID,
+ 0,
+ PROPOSAL,
+ session_id=SESSION_ID,
+ completion=completion,
+ )
+ assert queued, "proposal was dropped before reaching the ingestion queue"
+ completion.register()
+ drained = await ingest_mod.wait_for_ingestion(
+ completion, timeout_seconds=INGEST_TIMEOUT_SECONDS
+ )
+ assert drained, f"ingestion did not finish within {INGEST_TIMEOUT_SECONDS}s"
+
+
+async def _read_edges(driver: AutoGPTFalkorDriver) -> list[dict[str, Any]]:
+ records, _, _ = await driver.execute_query(
+ """
+ MATCH ()-[e:RELATES_TO]->()
+ RETURN e.uuid AS uuid,
+ e.status AS status,
+ e.source_kind AS source_kind,
+ e.scope AS scope,
+ e.confidence AS confidence,
+ e.provenance AS provenance,
+ e.created_at AS created_at,
+ e.ratified_at AS ratified_at,
+ e.expired_at AS expired_at,
+ e.expiration_reason AS expiration_reason
+ """
+ )
+ return records
+
+
+async def _sole_edge(driver: AutoGPTFalkorDriver) -> dict[str, Any]:
+ edges = await _read_edges(driver)
+ assert len(edges) == 1, (
+ f"expected exactly one :RELATES_TO edge, got {len(edges)}. Zero edges "
+ "usually means add_episode raised — the ingestion worker swallows and "
+ "logs it, so re-run with --log-cli-level=WARNING to see the cause."
+ )
+ return edges[0]
+
+
+async def _backdate_edge(
+ driver: AutoGPTFalkorDriver, edge_uuid: str, age: timedelta
+) -> None:
+ """Age an edge so the sweep's grace-period branch becomes reachable.
+
+ Written as an ISO-8601 string, which is how graphiti's FalkorDB driver
+ stores ``created_at`` — ``parse_created_at`` therefore sees the same shape
+ it sees in production.
+ """
+ await driver.execute_query(
+ "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->() SET e.created_at = $created_at",
+ uuid=edge_uuid,
+ created_at=(datetime.now(timezone.utc) - age).isoformat(),
+ )
+
+
+# --- Tests ------------------------------------------------------------------
+
+
+async def test_dream_envelope_lands_tentative_edge_with_envelope_metadata(
+ dream_graph,
+) -> None:
+ """The core regression: a dream proposal's envelope metadata reaches the edge.
+
+ Every asserted value contradicts what the LLM attribute extractor returned
+ (see ``_SCRIPTED_RESPONSES['MemoryFact']``), so this fails loudly if the
+ deterministic stamp ever goes back to being a no-op.
+ """
+ driver, user_id = dream_graph
+ await _ingest_dream_proposal(user_id)
+
+ edge = await _sole_edge(driver)
+ assert edge["status"] == "tentative", (
+ "dream proposals must land on probation; 'active' here means the "
+ "envelope's status was lost and ratification will never examine it"
+ )
+ assert edge["source_kind"] == "assistant_derived", (
+ "'user_asserted' would grant a dream-derived fact the user-level trust "
+ "ratification and retrieval key on"
+ )
+ assert edge["scope"] == PROPOSAL.scope
+ assert edge["confidence"] == pytest.approx(PROPOSAL.confidence)
+ assert edge["provenance"].startswith(f"dream:{PASS_ID}:recombine:")
+ assert parse_created_at(edge["created_at"]) is not None, (
+ "the sweep dates every tentative edge off created_at; a value it cannot "
+ "parse leaves the edge tentative forever"
+ )
+
+
+async def test_warm_context_hit_promotes_the_tentative_edge(dream_graph) -> None:
+ """Promote-on-hit leg: retrieval flips the freshly ingested edge to active."""
+ driver, user_id = dream_graph
+ await _ingest_dream_proposal(user_id)
+ edge_uuid = (await _sole_edge(driver))["uuid"]
+
+ assert await try_ratify_on_hit(user_id, [edge_uuid]) == 1
+
+ edge = await _sole_edge(driver)
+ assert edge["status"] == "active"
+ assert edge["ratified_at"] is not None
+ assert edge["source_kind"] == "assistant_derived", (
+ "promotion must not disturb provenance — an active edge still has to "
+ "be attributable to the dream pass that proposed it"
+ )
+ assert edge["provenance"].startswith(f"dream:{PASS_ID}:recombine:"), (
+ "promotion writes status/ratified_at and must leave the rest of the "
+ "envelope alone; a rewritten provenance would orphan the edge from "
+ "the pass that proposed it"
+ )
+ assert edge["scope"] == PROPOSAL.scope
+ assert edge["confidence"] == pytest.approx(PROPOSAL.confidence)
+
+ assert (
+ await try_ratify_on_hit(user_id, [edge_uuid]) == 0
+ ), "the status='tentative' guard makes repeat hits no-ops"
+
+
+async def test_sweep_promotes_a_tentative_edge_that_earned_a_hit(dream_graph) -> None:
+ """Promote leg of the SWEEP: distinct from the warm-context hook.
+
+ ``try_ratify_on_hit`` promotes via ``_promote_if_tentative``; the sweep
+ reaches ``_promote_edge`` only through ``_process_edge``'s ``hits >= 1``
+ branch, keyed off the Redis counter — this is the test that makes the
+ ``_FakeRedis`` counter load-bearing.
+ """
+ driver, user_id = dream_graph
+ await _ingest_dream_proposal(user_id)
+ edge_uuid = (await _sole_edge(driver))["uuid"]
+ await record_memory_hit(user_id, edge_uuid)
+
+ result = await run_ratification_pass(user_id)
+
+ assert result.error is None
+ assert result.examined_count == 1
+ assert result.ratified_count == 1
+ assert result.superseded_count == 0
+
+ edge = await _sole_edge(driver)
+ assert edge["status"] == "active"
+ assert edge["ratified_at"] is not None
+
+
+async def test_repeated_hits_accumulate_in_the_counter(dream_graph) -> None:
+ """``record_memory_hit`` seeds with SET NX then INCRs, so a fake that
+ overwrites unconditionally would pin every edge at one hit and hide
+ whatever the sweep's hit thresholds actually do."""
+ driver, user_id = dream_graph
+ await _ingest_dream_proposal(user_id)
+ edge_uuid = (await _sole_edge(driver))["uuid"]
+
+ await record_memory_hit(user_id, edge_uuid)
+ await record_memory_hit(user_id, edge_uuid)
+
+ assert await get_hit_count(user_id, edge_uuid) == 2
+
+
+async def test_sweep_supersedes_unhit_edge_past_its_grace_period(dream_graph) -> None:
+ """Supersede leg: the nightly sweep retires a tentative edge nobody used."""
+ driver, user_id = dream_graph
+ await _ingest_dream_proposal(user_id)
+ edge_uuid = (await _sole_edge(driver))["uuid"]
+ await _backdate_edge(
+ driver, edge_uuid, RATIFICATION_GRACE_PERIOD + timedelta(days=1)
+ )
+
+ result = await run_ratification_pass(user_id)
+
+ assert result.error is None
+ assert result.per_edge_errors == []
+ assert result.examined_count == 1, (
+ "the sweep lists tentatives with Cypher; examining zero means the edge "
+ "never carried status='tentative' in the first place"
+ )
+ assert result.superseded_count == 1
+ assert result.ratified_count == 0
+
+ edge = await _sole_edge(driver)
+ assert edge["status"] == "superseded"
+ assert edge["expiration_reason"] == "unratified"
+ assert edge["expired_at"] is not None
+
+
+async def test_sweep_never_touches_an_already_active_edge(dream_graph) -> None:
+ """The sweep must be inert on ratified memory, however old it gets.
+
+ Every other sweep test starts from ``tentative``, so nothing pins the
+ status filter in the listing Cypher. Drop it and an ``active`` edge past
+ the grace period is superseded on the next nightly run — silent deletion
+ of a memory the user already confirmed by using it. This ages a promoted
+ edge well past that boundary and asserts the sweep ignores it entirely.
+ """
+ driver, user_id = dream_graph
+ await _ingest_dream_proposal(user_id)
+ edge_uuid = (await _sole_edge(driver))["uuid"]
+
+ # Promote it the production way: a real hit, then the sweep's promote leg.
+ await record_memory_hit(user_id, edge_uuid)
+ assert (await run_ratification_pass(user_id)).ratified_count == 1
+ promoted = await _sole_edge(driver)
+ assert promoted["status"] == "active"
+ ratified_at = promoted["ratified_at"]
+ assert ratified_at is not None
+
+ # Now age it far past the grace period and sweep again.
+ await _backdate_edge(
+ driver, edge_uuid, RATIFICATION_GRACE_PERIOD + timedelta(days=30)
+ )
+ result = await run_ratification_pass(user_id)
+
+ assert result.error is None
+ assert result.examined_count == 0, (
+ "the sweep lists only status='tentative' edges — examining an active "
+ "one means the filter is gone and ratified memory is in scope"
+ )
+ assert result.superseded_count == 0
+ assert result.ratified_count == 0
+
+ edge = await _sole_edge(driver)
+ assert edge["status"] == "active"
+ assert edge["expired_at"] is None
+ assert edge["ratified_at"] == ratified_at, (
+ "a re-run must not re-stamp ratified_at — that timestamp is the "
+ "audit record of when the memory was actually confirmed"
+ )
+
+
+async def test_sweep_leaves_a_fresh_unhit_edge_tentative(dream_graph) -> None:
+ """Within the grace period an unhit edge is still earning its keep."""
+ driver, user_id = dream_graph
+ await _ingest_dream_proposal(user_id)
+
+ result = await run_ratification_pass(user_id)
+
+ assert result.error is None
+ assert result.examined_count == 1
+ assert result.superseded_count == 0
+ assert result.ratified_count == 0
+ assert (await _sole_edge(driver))["status"] == "tentative"