Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/surreal_memory/engine/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,15 @@ async def run(
try:
brain = await self._storage.get_brain(self._storage.current_brain_id or "")
except Exception:
# Fail-soft on purpose: the refresh helper re-fetches per fiber, so the
# pass still completes. It is logged all the same, because two
# consequences are otherwise invisible: the one-lookup-per-pass
# optimisation quietly degrades to one lookup per fiber with nothing to
# explain the slowdown, and a persistent storage fault reaches the
# operator as a content_refresh warning blaming the embedding provider.
logger.warning(
"Brain pre-fetch failed; falling back to a per-fiber lookup", exc_info=True
)
brain = None

for idx, fiber in enumerate(fibers):
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/test_compression_brain_prefetch_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""The compression pass logs a failed brain pre-fetch instead of degrading in silence.

``CompressionEngine.run`` fetches the brain once per pass because the derived-field
refresh needs its embedding config. The fetch is deliberately fail-soft — the refresh
helper looks the brain up per fiber when it is missing — but a silent fallback hides
both the loss of the optimisation and the real cause of any downstream warning.
"""

from typing import Any

import pytest

from surreal_memory.core.brain import Brain
from surreal_memory.engine.compression import CompressionEngine
from surreal_memory.storage.memory_store import InMemoryStorage

_PREFETCH_WARNING = "Brain pre-fetch failed"


async def _storage_with_brain() -> InMemoryStorage:
storage = InMemoryStorage()
brain = Brain.create(name="prefetch-brain")
await storage.save_brain(brain)
storage.set_brain(brain.id)
return storage


@pytest.mark.asyncio
async def test_failed_brain_prefetch_is_logged(caplog: pytest.LogCaptureFixture) -> None:
storage = await _storage_with_brain()

async def _raise(_brain_id: str) -> Any:
raise RuntimeError("storage unavailable")

storage.get_brain = _raise # type: ignore[method-assign]

with caplog.at_level("WARNING", logger="surreal_memory.engine.compression"):
report = await CompressionEngine(storage).run()

assert report is not None, "the pass must still complete; the fallback is fail-soft"
warnings = [r for r in caplog.records if _PREFETCH_WARNING in r.getMessage()]
assert warnings, (
f"expected a {_PREFETCH_WARNING!r} warning; got: {[r.getMessage() for r in caplog.records]}"
)
assert warnings[0].exc_info is not None, "the cause must travel with the warning"

await storage.close()


@pytest.mark.asyncio
async def test_successful_brain_prefetch_stays_quiet(caplog: pytest.LogCaptureFixture) -> None:
"""Positive control: a pass that fetches its brain must not warn."""
storage = await _storage_with_brain()

with caplog.at_level("WARNING", logger="surreal_memory.engine.compression"):
await CompressionEngine(storage).run()

assert not [r for r in caplog.records if _PREFETCH_WARNING in r.getMessage()]

await storage.close()
Loading