Files
EverOS/tests/unit/test_service/test_memorize_span.py
T
zhanghuiandClaude Opus 4.8 357f619c64 feat(observability): instrument memory ops + OME trace linking
Open spans at the memory hot paths (all no-op when tracing is off):

- add / flush (service.memorize), extract + persist.markdown (user pipeline).
- search: everos.memory.search retriever + a uniform recall / rank
  decomposition across keyword / vector / hybrid / agentic (manager, agentic
  modules, cross-encoder callbacks); query-embedding tokens land on recall.
- recall quality: top_score / hit on the search span, plus recall_top_score /
  recall_hit pushed to Langfuse scores via the bounded-queue sink (method
  tagged; off the request path).
- OME: everos.ome.<strategy> agent span + everos.reflect.consolidate
  generation; a W3C traceparent captured at enqueue is threaded through the
  APScheduler job and re-attached in the Runner, so strategies fanned out
  from a request nest under that request's trace.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-23 20:47:39 +08:00

68 lines
2.1 KiB
Python

"""``memorize`` opens an everos.memory.add / everos.memory.flush span.
The inner critical section is mocked out — this asserts only the span
wrapping + name selection (add vs flush) driven by ``is_final``.
"""
from __future__ import annotations
import importlib
from collections.abc import AsyncIterator, Iterator
from contextlib import asynccontextmanager
from unittest.mock import AsyncMock
import pytest
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
InMemorySpanExporter,
)
from everos.config import Settings
from everos.core.observability.tracing import (
force_flush,
init_tracing,
shutdown_tracing,
)
mm = importlib.import_module("everos.service.memorize")
@pytest.fixture(autouse=True)
def _patch(monkeypatch: pytest.MonkeyPatch) -> Iterator[InMemorySpanExporter]:
monkeypatch.setattr(mm, "load_settings", lambda: Settings())
monkeypatch.setattr(
mm,
"_memorize_locked",
AsyncMock(return_value=mm.MemorizeResult(message_count=0, status="extracted")),
)
@asynccontextmanager
async def _fake_lock(session_id: str) -> AsyncIterator[None]:
yield
monkeypatch.setattr(mm, "get_session_lock", _fake_lock)
exporter = InMemorySpanExporter()
shutdown_tracing()
init_tracing(
Settings().observability.model_copy(update={"enabled": True}),
span_processor=SimpleSpanProcessor(exporter),
)
yield exporter
shutdown_tracing()
async def test_add_emits_memory_add_span(_patch: InMemorySpanExporter) -> None:
await mm.memorize({"session_id": "s1", "messages": []}, is_final=False)
force_flush()
spans = {s.name: s for s in _patch.get_finished_spans()}
assert "everos.memory.add" in spans
assert spans["everos.memory.add"].attributes["langfuse.session.id"] == "s1"
async def test_flush_emits_memory_flush_span(_patch: InMemorySpanExporter) -> None:
await mm.memorize({"session_id": "s2", "messages": []}, is_final=True)
force_flush()
names = {s.name for s in _patch.get_finished_spans()}
assert "everos.memory.flush" in names