"""UnifiedContextEngine — EverOS lane (recall + SkillForgeRouter) behavior. Formerly the ``DefaultContextEngine`` two-track tests. The two-track gather (``backend.recall`` for ``# Memory`` + ``SkillForgeRouter`` for ``# Skills``) is now the EverOS lane of the single :class:`UnifiedContextEngine`. These tests exercise that lane through the fast path (empty history → no Curator LLM call), which is where the recall / router outputs land in the prompt. """ from __future__ import annotations import asyncio from pathlib import Path from typing import Any import pytest from raven.agent.context import ContextBuilder from raven.config.raven import ContextConfig from raven.context_engine import ContextAssembler, TurnContext from raven.context_engine.segments import ( ActiveSkillsSegmentBuilder, BootstrapSegmentBuilder, IdentitySegmentBuilder, MemorySegmentBuilder, SkillsSegmentBuilder, ) from raven.context_engine.segments.curator import CuratorSegmentBuilder from raven.contracts.assembled import TokenBudget from raven.contracts.memory import Memory from raven.memory_engine.skill_forge import RouterHit, SkillForgeRouter # --------------------------------------------------------------------------- # Test doubles # --------------------------------------------------------------------------- class _StubSource: """Configurable SkillSource — records call args + delay.""" def __init__( self, name: str, weight: float = 1.0, hits: list[RouterHit] | None = None, delay_s: float = 0.0, ) -> None: self.name = name self.weight = weight self._hits = hits or [] self._delay = delay_s self.calls: list[tuple[str, int]] = [] async def search(self, query, history, k): await asyncio.sleep(self._delay) self.calls.append((query, k)) return list(self._hits) class _StubBackend: """Minimal MemoryBackend mock — recall returns canned hits.""" def __init__( self, recall_response: list[Memory] | None = None, delay_s: float = 0.0, recall_raises: Exception | None = None, ) -> None: self._recall_response = recall_response or [] self._delay = delay_s self._recall_raises = recall_raises self.recall_calls: list[dict[str, Any]] = [] async def start(self): pass async def stop(self): pass async def feedback(self, signals): pass async def store(self, session_id, messages, *, metadata=None): pass async def recall(self, query, *, user_id=None, agent_id=None, top_k): await asyncio.sleep(self._delay) self.recall_calls.append( { "query": query, "user_id": user_id, "agent_id": agent_id, "top_k": top_k, } ) if self._recall_raises is not None: raise self._recall_raises return list(self._recall_response) class _StubProvider: api_key = "test" def get_default_model(self) -> str: return "stub" async def chat_with_retry(self, *args, **kwargs): # Only reached on the slow path; these tests stay on fast path. raise NotImplementedError def _budget() -> TokenBudget: return TokenBudget( context_length=100_000, reserved_output=4_000, reserved_tools=2_000, reserved_system=1_000, available_history=93_000, ) def _turn(msg: str = "hi", **kw) -> TurnContext: return TurnContext( current_message=msg, media=kw.get("media"), channel=kw.get("channel"), chat_id=kw.get("chat_id"), selected_skills=kw.get("selected_skills"), ) @pytest.fixture def builder(tmp_path: Path) -> ContextBuilder: return ContextBuilder(workspace=tmp_path) def _engine( builder: ContextBuilder, *, router: SkillForgeRouter, backend: _StubBackend | None, user_id: str = "default", skill_top_k: int = 5, memory_top_k: int = 5, ) -> ContextAssembler: builders = [ IdentitySegmentBuilder(builder.workspace), BootstrapSegmentBuilder(builder.workspace), MemorySegmentBuilder( builder.memory, backend, user_id=user_id, memory_top_k=memory_top_k, ), ActiveSkillsSegmentBuilder(builder.skills), SkillsSegmentBuilder(router, skill_top_k=skill_top_k), CuratorSegmentBuilder( workspace=builder.workspace, config=ContextConfig(), provider=_StubProvider(), model="stub", context_window_tokens=100_000, get_tool_definitions=lambda: [], ), ] return ContextAssembler(builders, lambda: []) # --------------------------------------------------------------------------- # Identity / lifecycle # --------------------------------------------------------------------------- class TestEngineIdentity: def test_name(self, builder: ContextBuilder) -> None: eng = _engine(builder, router=SkillForgeRouter([]), backend=_StubBackend()) assert eng.name == "context_assembler" def test_owns_compaction_is_true(self, builder: ContextBuilder) -> None: # The unified engine owns its own archival compaction (Curator # lane), so AgentLoop hands it the full append-only log and skips # the host MemoryConsolidator. eng = _engine(builder, router=SkillForgeRouter([]), backend=_StubBackend()) assert eng.owns_compaction is True # --------------------------------------------------------------------------- # Two-track concurrency (recall + router.select) # --------------------------------------------------------------------------- class TestTwoTrackConcurrency: async def test_skill_and_memory_run_concurrently( self, builder: ContextBuilder, ) -> None: """Neither track is let through until both have arrived, so this passes only if they overlap. The wall-clock total this used to assert on raced whatever else the machine was doing, and failed under load.""" rendezvous = asyncio.Barrier(2) class _RendezvousSource(_StubSource): async def search(self, query, history, k): await rendezvous.wait() return await super().search(query, history, k) class _RendezvousBackend(_StubBackend): async def recall(self, query, *, user_id=None, agent_id=None, top_k): await rendezvous.wait() return await super().recall(query, user_id=user_id, agent_id=agent_id, top_k=top_k) eng = _engine( builder, router=SkillForgeRouter([_RendezvousSource("local", hits=[])]), backend=_RendezvousBackend(recall_response=[]), ) async with asyncio.timeout(5): await eng.assemble( session_key="s1", session_messages=[], budget=_budget(), turn=_turn(), ) async def test_track_ids_passed_to_recall( self, builder: ContextBuilder, ) -> None: backend = _StubBackend() eng = _engine( builder, router=SkillForgeRouter([]), backend=backend, user_id="alice", ) await eng.assemble("s", [], _budget(), turn=_turn("git resolver")) assert backend.recall_calls == [ { "query": "git resolver", "user_id": "alice", "agent_id": None, "top_k": 5, }, ] async def test_top_k_propagated_per_track( self, builder: ContextBuilder, ) -> None: source = _StubSource("local", hits=[]) backend = _StubBackend() eng = _engine( builder, router=SkillForgeRouter([source]), backend=backend, skill_top_k=3, memory_top_k=7, ) await eng.assemble("s", [], _budget(), turn=_turn("q")) # SkillForgeRouter applies an over-fetch factor; the source sees k*2 # by default. assert source.calls[0][1] == 6 # 3 × default over_fetch_factor 2 assert backend.recall_calls[0]["top_k"] == 7 # --------------------------------------------------------------------------- # AssembledContext metadata # --------------------------------------------------------------------------- class TestAssembledMetadata: async def test_injected_skill_ids(self, builder: ContextBuilder) -> None: hits = [ RouterHit(qualified_id="local/a", name="a", content="", score=0.5), RouterHit(qualified_id="everos/b", name="b", content="", score=0.5), ] source = _StubSource("local", hits=hits) eng = _engine(builder, router=SkillForgeRouter([source]), backend=_StubBackend()) ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert set(ac.metadata["injected_skill_ids"]) == {"local/a", "everos/b"} async def test_memory_hits_count(self, builder: ContextBuilder) -> None: memories = [Memory(text=f"fact-{i}") for i in range(3)] eng = _engine( builder, router=SkillForgeRouter([]), backend=_StubBackend(recall_response=memories), ) ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert ac.metadata["memory_hits"] == 3 async def test_engine_label(self, builder: ContextBuilder) -> None: eng = _engine(builder, router=SkillForgeRouter([]), backend=_StubBackend()) ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert ac.metadata["engine"] == "context_assembler" # --------------------------------------------------------------------------- # Block rendering — recall → # Memory, router → # Skills # --------------------------------------------------------------------------- class TestRendering: async def test_recalled_memory_merged_into_memory_segment( self, builder: ContextBuilder, ) -> None: backend = _StubBackend( recall_response=[Memory(text="user likes espresso")], ) eng = _engine(builder, router=SkillForgeRouter([]), backend=backend) ac = await eng.assemble("s", [], _budget(), turn=_turn()) sys_msg = ac.messages[0] assert sys_msg["role"] == "system" assert "# Memory" in sys_msg["content"] assert "# Recalled memory" not in sys_msg["content"] assert "user likes espresso" in sys_msg["content"] async def test_router_skills_in_skills_segment( self, builder: ContextBuilder, ) -> None: hits = [ RouterHit( qualified_id="local/git-resolver", name="git-resolver", content="resolves git refs.", score=0.8, ), ] source = _StubSource("local", hits=hits) eng = _engine(builder, router=SkillForgeRouter([source]), backend=_StubBackend()) ac = await eng.assemble("s", [], _budget(), turn=_turn()) sys_content = ac.messages[0]["content"] # No addendum channel and no "# Retrieved skills" heading — the # router body lands in segment 5 (# Skills). assert ac.system_prompt_addition is None assert "# Retrieved skills" not in sys_content assert "# Skills" in sys_content assert "git-resolver" in sys_content assert "[local/git-resolver]" in sys_content assert "resolves git refs" in sys_content async def test_no_skills_addition_when_empty( self, builder: ContextBuilder, ) -> None: eng = _engine(builder, router=SkillForgeRouter([]), backend=_StubBackend()) ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert ac.system_prompt_addition is None async def test_no_recalled_block_when_empty( self, builder: ContextBuilder, ) -> None: eng = _engine( builder, router=SkillForgeRouter([]), backend=_StubBackend(recall_response=[]), ) ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert "# Recalled memory" not in ac.messages[0]["content"] # --------------------------------------------------------------------------- # Graceful degrade — no backend wired # --------------------------------------------------------------------------- class TestNoBackendDegrade: async def test_recall_skipped_router_local_only( self, builder: ContextBuilder, ) -> None: """With ``backend=None`` the recall lane yields [] and the local router still feeds # Skills.""" hits = [ RouterHit( qualified_id="local/x", name="x", content="body", score=0.5, ) ] source = _StubSource("local", hits=hits) eng = _engine(builder, router=SkillForgeRouter([source]), backend=None) ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert ac.metadata["memory_hits"] == 0 assert "local/x" in ac.metadata["injected_skill_ids"] assert "# Skills" in ac.messages[0]["content"] # --------------------------------------------------------------------------- # Failure semantics # --------------------------------------------------------------------------- class TestFailureSemantics: async def test_backend_recall_exception_degrades_to_no_hits( self, builder: ContextBuilder, ) -> None: """Memory backend outage no longer surfaces to AgentLoop: recall is bounded and isolated the same way SkillForgeRouter's sources are.""" backend = _StubBackend(recall_raises=RuntimeError("backend down")) eng = _engine(builder, router=SkillForgeRouter([]), backend=backend) ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert ac.metadata["memory_hits"] == 0 async def test_phase_a_builder_failure_degrades_its_segment_only( self, builder: ContextBuilder, ) -> None: """One failing segment builder loses its segment, never the turn; the name lands in metadata so the loop can tell the user.""" class _Boom: name = "boom" order = 5 async def build(self, ctx): raise RuntimeError("segment source down") eng = _engine(builder, router=SkillForgeRouter([]), backend=_StubBackend()) eng._phase_a = [*eng._phase_a, _Boom()] ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert ac.metadata["degraded_segments"] == ["boom"] assert ac.messages is not None # the turn assembled; only the segment is gone async def test_phase_b_builder_failure_degrades_its_segment_only( self, builder: ContextBuilder, ) -> None: """The prefix-dependent phase is isolated the same way: a Curator that raises loses its segment and is named, and the turn still assembles on the prefix and the raw history.""" class _Boom: name = "curator-boom" order = 60 needs_prefix = True async def build(self, ctx): raise RuntimeError("curator down") eng = _engine(builder, router=SkillForgeRouter([]), backend=_StubBackend()) eng._phase_b = [*eng._phase_b, _Boom()] ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert ac.metadata["degraded_segments"] == ["curator-boom"] assert ac.messages is not None async def test_single_skill_source_failure_isolated( self, builder: ContextBuilder, ) -> None: """SkillForgeRouter's _safe_search swallows per-source exceptions; assemble still returns an AssembledContext.""" class _Failing: name = "broken" weight = 1.0 async def search(self, q, h, k): raise RuntimeError("source dead") good_hits = [ RouterHit( qualified_id="local/x", name="x", content="", score=0.5, ) ] eng = _engine( builder, router=SkillForgeRouter([_Failing(), _StubSource("local", hits=good_hits)]), backend=_StubBackend(), ) ac = await eng.assemble("s", [], _budget(), turn=_turn()) assert "local/x" in ac.metadata["injected_skill_ids"] # --------------------------------------------------------------------------- # Turn fields passthrough # --------------------------------------------------------------------------- class TestTurnPassthrough: async def test_channel_chat_id_propagate_to_builder( self, builder: ContextBuilder, ) -> None: eng = _engine(builder, router=SkillForgeRouter([]), backend=_StubBackend()) ac = await eng.assemble( "s", [], _budget(), turn=_turn("hello", channel="slack", chat_id="C123"), ) joined = "\n".join(str(m.get("content")) for m in ac.messages) assert "slack" in joined assert "C123" in joined # ── consecutive-assistant coalescing (2b deliver_text support) ── def test_coalesce_assistant_merges_adjacent_plain_only(): from raven.context_engine.assembler import _coalesce_assistant # The ack ("on it") + a verbatim deliver_text answer land as two adjacent # assistant messages; they merge into one so providers that reject # consecutive same-role messages do not choke on the next turn. history = [ {"role": "user", "content": "research X"}, {"role": "assistant", "content": "on it"}, {"role": "assistant", "content": "FULL REPORT"}, ] out = _coalesce_assistant(history) assert [m["role"] for m in out] == ["user", "assistant"] assert out[-1]["content"] == "on it\n\nFULL REPORT" def test_coalesce_assistant_preserves_tool_call_adjacency(): from raven.context_engine.assembler import _coalesce_assistant # An assistant carrying tool_calls is never merged — its tool result follows # it, so merging would break tool-call adjacency. history = [ {"role": "assistant", "content": "", "tool_calls": [{"id": "t1"}]}, {"role": "tool", "tool_call_id": "t1", "content": "res"}, {"role": "assistant", "content": "done"}, ] assert _coalesce_assistant(history) == history def test_coalesce_assistant_ignores_non_adjacent(): from raven.context_engine.assembler import _coalesce_assistant history = [ {"role": "assistant", "content": "a"}, {"role": "user", "content": "u"}, {"role": "assistant", "content": "b"}, ] assert _coalesce_assistant(history) == history def test_coalesce_assistant_skips_when_merged_in_carries_reasoning(): from raven.context_engine.assembler import _coalesce_assistant # The merged-in message carries reasoning_content — merging would silently # drop it (contra the reasoning-field history projection), so skip the merge. history = [ {"role": "assistant", "content": "a"}, {"role": "assistant", "content": "b", "reasoning_content": "why"}, ] assert _coalesce_assistant(history) == history