"""No model/network calls: actual history projection and memory failure seams.""" from copy import deepcopy import json from types import SimpleNamespace from unittest.mock import AsyncMock, MagicMock import pytest from deeptutor.capabilities.mastery.history import project_mastery_replay from deeptutor.capabilities.mastery.memory import MasteryContextBuilder from deeptutor.services.session.context_builder import ( ContextBuilder, _ContextSummaryAgent, count_tokens, ) from deeptutor.services.session.model_history import normalize_model_turn @pytest.fixture(autouse=True) def isolated_agent_settings(monkeypatch): # The provider is stubbed below; no installation-local settings are needed. monkeypatch.setattr("deeptutor.agents.base_agent.get_agent_params", lambda _module: {}) @pytest.fixture(autouse=True) def fresh_truncation_backoff(): from deeptutor.capabilities.mastery import memory as memory_module memory_module._truncation_backoff.clear() yield memory_module._truncation_backoff.clear() def records(n=8, padding=2000): result = [] for i in range(n): call_id = f"call-{i}" messages = [ {"role": "user", "content": f"I confuse A with B; example {i} failed."}, { "role": "assistant", "content": "We tried a different explanation.", "tool_calls": [ { "id": call_id, "type": "function", "function": {"name": "mastery_grade", "arguments": '{"verdict":"partial"}'}, } ], }, { "role": "tool", "tool_call_id": call_id, "content": json.dumps({"verdict": "partial", "reason": f"distinct-{i} " * padding}), }, {"role": "assistant", "content": "This remains uncertain."}, ] result.extend( [ {"id": 2 * i + 1, "role": "user", "content": messages[0]["content"]}, { "id": 2 * i + 2, "role": "assistant", "content": "This remains uncertain.", "metadata": { "model_turn": { "version": 1, "messages": messages, "system": "Huge tutor instructions " * 500, "tools": [{"type": "function", "function": {"name": "mastery_grade"}}], } }, }, ] ) return result def store_for(rows, summary="", watermark=0): store = MagicMock() store.get_session = AsyncMock( return_value={"compressed_summary": summary, "summary_up_to_msg_id": watermark} ) store.get_messages_for_context = AsyncMock(return_value=rows) store.update_summary = AsyncMock() return store def memory(ids): return json.dumps( { "version": 1, "covered_archive_ids": ids, "learner_state": [ {"note": "A versus B remains uncertain; explanation failed.", "archive_ids": ids} ], "preferences": [], "open_items": [], "teaching_history": [], "source_anchors": [], } ) def test_runtime_replacements_do_not_consume_learning_history_or_mutate_archive(): messages = [ {"role": "user", "content": "literal snapshot quotation"}, { "role": "user", "content": "old runtime " * 1000, "_context_snapshot": "mastery_status_snapshot", }, {"role": "assistant", "content": "You struggled with A."}, ] original = deepcopy(messages) projected = project_mastery_replay(messages) assert projected == [messages[0], messages[2]] assert messages == original @pytest.mark.asyncio async def test_large_window_does_not_postpone_tutoring_compaction_and_recent_pairs_stay_full(): rows = records() original = deepcopy(rows) store = store_for(rows) builder = MasteryContextBuilder(store) builder._summarize = AsyncMock(return_value=(memory(["hs-tutor-2"]), [])) config = SimpleNamespace(model="gpt-6.1-sol", context_window=272000, max_tokens=8192) assert ContextBuilder(store)._history_budget(config) > 80000 built = await builder.build(session_id="s", llm_config=config, language="hu") builder._summarize.assert_awaited_once() store.update_summary.assert_awaited_once() assert built.budget <= 20000 assert rows[-1]["metadata"]["model_turn"]["messages"] == built.model_history[-4:] assert rows == original assert "I confuse A with B" in built.history_sources["hs-tutor-2"] assert "partial" in built.history_sources["hs-tutor-2"] assert "Huge tutor instructions" not in built.history_sources["hs-tutor-2"] @pytest.mark.asyncio async def test_summary_failure_bounds_inline_history_preserves_exact_recall_and_watermark(): rows = records() store = store_for(rows) builder = MasteryContextBuilder(store) builder._summarize = AsyncMock(side_effect=ValueError("invalid memory")) built = await builder.build( session_id="s", llm_config=SimpleNamespace(model="gpt-6.1-sol", context_window=272000, max_tokens=8192), ) store.update_summary.assert_not_awaited() assert built.token_count <= built.budget assert all(f"example {i} failed" in json.dumps(built.history_sources) for i in range(8)) assert "example 7 failed" in json.dumps(built.model_history) @pytest.mark.asyncio async def test_short_session_needs_no_extra_model_call_and_recall_stays_on_selected_branch(): rows = records(1, 1) store = store_for(rows, summary="other branch", watermark=900) builder = MasteryContextBuilder(store) builder._summarize = AsyncMock() built = await builder.build( session_id="s", leaf_message_id=2, llm_config=SimpleNamespace(model="gpt-6.1-sol", context_window=272000, max_tokens=8192), ) builder._summarize.assert_not_awaited() assert "other branch" not in json.dumps(built.model_history) assert set(built.history_sources) == {"hs-tutor-2"} assert all( call.kwargs["leaf_message_id"] == 2 for call in store.get_messages_for_context.call_args_list ) @pytest.mark.asyncio @pytest.mark.parametrize("invalid", ["not JSON", memory(["hs-tutor-999"]), memory([])]) async def test_invalid_or_incomplete_memory_is_rejected(monkeypatch, invalid): async def stream(_self, **kwargs): yield invalid monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) builder = MasteryContextBuilder(store_for(records(1, 1))) with pytest.raises(ValueError): await builder._summarize( session_id="s", language="hu", source_text="original", summary_budget=4096, replay_request={"rows": records(1, 1), "messages": [], "tools": []}, ) @pytest.mark.asyncio async def test_memory_call_is_task_scoped_preserves_language_and_exact_archive_anchors(monkeypatch): captured = {} async def stream(_self, **kwargs): captured.update(kwargs) yield memory(["hs-tutor-2"]) monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) builder = MasteryContextBuilder(store_for(records(1, 1))) text, _ = await builder._summarize( session_id="s", language="fr", source_text="original", summary_budget=4096, replay_request={"rows": records(1, 1), "messages": [], "tools": []}, ) assert json.loads(text)["learner_state"] assert "I confuse A with B" in captured["user_prompt"] assert "partial" in captured["user_prompt"] assert "hs-tutor-2" in captured["user_prompt"] assert "Français" in captured["system_prompt"] assert "tools" not in captured and "messages" not in captured assert "Huge tutor instructions" not in captured["user_prompt"] @pytest.mark.asyncio @pytest.mark.parametrize("indent", [None, 2]) async def test_complete_json_near_budget_is_not_cut_as_an_incomplete_sentence(monkeypatch, indent): data = json.loads(memory(["hs-tutor-2"])) data["learner_state"][0]["note"] = "membrán " * 1300 response = json.dumps(data, ensure_ascii=False, indent=indent) budget = count_tokens(response) + 20 assert count_tokens(response) >= int(budget * 0.95) async def stream(_self, **kwargs): if "outcome" in kwargs: kwargs["outcome"].finish_reason = "stop" yield response monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) builder = MasteryContextBuilder(store_for(records(1, 1))) text, _ = await builder._summarize( session_id="s", language="hu", source_text="original", summary_budget=budget, replay_request={"rows": records(1, 1)}, ) assert json.loads(text) == data @pytest.mark.asyncio async def test_provider_truncated_memory_never_advances_watermark_even_if_json_parses(monkeypatch): from deeptutor.capabilities.mastery.memory import archive_id from deeptutor.services.session.model_history import history_groups async def stream(_self, **kwargs): kwargs["outcome"].finish_reason = "max_tokens" yield memory([archive_id(g) for g in history_groups(builder._summary_rows)]) monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) store = store_for(records()) builder = MasteryContextBuilder(store) result = await builder.build( session_id="s", llm_config=SimpleNamespace(model="gpt-6.1-sol", context_window=272000, max_tokens=8192), ) store.update_summary.assert_not_awaited() assert result.token_count <= result.budget assert "example 0 failed" in json.dumps(result.history_sources) @pytest.mark.asyncio async def test_complete_memory_still_has_a_hard_retained_size_limit(monkeypatch): data = json.loads(memory(["hs-tutor-2"])) data["learner_state"][0]["note"] = "membrán " * 5000 async def stream(_self, **kwargs): kwargs["outcome"].finish_reason = "stop" yield json.dumps(data, ensure_ascii=False) monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) builder = MasteryContextBuilder(store_for(records(1, 1))) with pytest.raises(ValueError, match="exceeded its retained budget"): await builder._summarize( session_id="s", language="hu", source_text="original", summary_budget=4096, replay_request={"rows": records(1, 1)}, ) def test_native_manifest_is_not_duplicated_but_deferred_discovery_and_kb_note_survive(): from deeptutor.capabilities.mastery.pipeline import MasteryPromptAssembler from deeptutor.core.context import UnifiedContext assembler = MasteryPromptAssembler(prompts={}, language="hu") kwargs = dict( context=UnifiedContext(), tool_manifest="duplicate complete tool descriptions", deferred_tools_manifest="Deferred catalog retained", kb_note="KB scope retained", ) assembler.native_tool_schemas = True blocks = assembler.blocks(**kwargs) assert not any(b.name == "tools" for b in blocks) assert any(b.content == "Deferred catalog retained" for b in blocks) assert any(b.content == "KB scope retained" for b in blocks) assembler.native_tool_schemas = False assert any(b.name == "tools" for b in assembler.blocks(**kwargs)) def test_turn_factory_uses_tutoring_memory_only_for_tutoring(): from deeptutor.services.session.turns.context_assembler import TurnContextAssembler assembler = TurnContextAssembler() assembler.store = MagicMock() assert type(assembler._create_context_builder()) is ContextBuilder assert isinstance(assembler._create_context_builder(tutoring=True), MasteryContextBuilder) @pytest.mark.asyncio async def test_actual_store_recall_does_not_expose_sibling_branch(tmp_path): from deeptutor.services.session.sqlite_store import SQLiteSessionStore from deeptutor.tools.builtin import ReadSourceTool store = SQLiteSessionStore(db_path=tmp_path / "history.db") session = await store.create_session() sid = session["id"] root = await store.add_message(sid, "user", "Root") first = await store.add_message(sid, "assistant", "Branch A", parent_message_id=root) other = await store.add_message(sid, "assistant", "Branch B private", parent_message_id=root) result = await MasteryContextBuilder(store).build( session_id=sid, leaf_message_id=first, llm_config=SimpleNamespace(model="gpt-test", context_window=128000, max_tokens=4096), ) tool = ReadSourceTool() read = await tool.execute(source_index=result.history_sources, source_id=f"hs-tutor-{first}") assert read.success and "Branch A" in read.content and "Branch B private" not in read.content forbidden = await tool.execute( source_index=result.history_sources, source_id=f"hs-tutor-{other}" ) assert not forbidden.success @pytest.mark.asyncio async def test_one_oversized_exchange_uses_exact_recall_without_cutting_tool_pairs(): rows = records(n=1, padding=40000) original = deepcopy(rows) store = store_for(rows) builder = MasteryContextBuilder(store) builder._summarize = AsyncMock(side_effect=ValueError("invalid memory")) result = await builder.build( session_id="s", llm_config=SimpleNamespace(model="gpt-6.1-sol", context_window=272000, max_tokens=8192), ) store.update_summary.assert_not_awaited() assert result.token_count <= result.budget assert "hs-tutor-2" in json.dumps(result.model_history) assert "distinct-0 " * 40000 in result.history_sources["hs-tutor-2"] assert not any( message.get("tool_calls") or message.get("role") == "tool" for message in result.model_history ) assert rows == original @pytest.mark.asyncio async def test_trailing_memory_snapshot_counts_toward_tutoring_budget(): from deeptutor.services.session.context_builder import _count_model_context_tokens from deeptutor.services.session.model_history import summary_snapshot rows = records(n=2, padding=1) summary = memory(["hs-tutor-2"]) builder = MasteryContextBuilder(store_for(rows, summary=summary, watermark=2)) result = await builder.build( session_id="s", llm_config=SimpleNamespace(model="gpt-6.1-sol", context_window=272000, max_tokens=8192), ) assert result.conversation_summary == summary assert summary not in json.dumps(result.model_history) expected = _count_model_context_tokens(result.model_history) + _count_model_context_tokens( summary_snapshot(summary) ) assert result.token_count == expected assert builder._model_tokens([], summary) == _count_model_context_tokens( [] ) + _count_model_context_tokens(summary_snapshot(summary)) @pytest.mark.asyncio @pytest.mark.parametrize("configured_limit, saved", [(8192, True), (3000, False)]) async def test_reasoning_allowance_does_not_replace_the_retained_memory_budget( monkeypatch, configured_limit, saved ): """A 4k combined reasoning/output cap repeatedly cut off live JSON memory.""" from deeptutor.capabilities.mastery.memory import archive_id from deeptutor.services.session.model_history import history_groups async def stream(_self, **kwargs): kwargs["outcome"].finish_reason = "stop" if kwargs["max_tokens"] >= 5000 else "max_tokens" assert kwargs["max_tokens"] <= configured_limit yield memory([archive_id(g) for g in history_groups(builder._summary_rows)]) monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) store = store_for(records()) builder = MasteryContextBuilder(store) result = await builder.build( session_id="s", llm_config=SimpleNamespace( model="reasoning-model", context_window=272000, max_tokens=configured_limit ), ) assert store.update_summary.await_count == int(saved) assert result.token_count <= 20000 assert "example 0 failed" in json.dumps(result.history_sources) def _truncating_stream(monkeypatch, calls, *, truncate=lambda kwargs: True): from deeptutor.capabilities.mastery.memory import archive_id from deeptutor.services.session.model_history import history_groups async def stream(_self, **kwargs): calls.append(kwargs["max_tokens"]) kwargs["outcome"].finish_reason = "max_tokens" if truncate(kwargs) else "stop" yield memory([archive_id(g) for g in history_groups(builder_rows[0]._summary_rows)]) builder_rows = [] monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) return builder_rows async def _build(builder_rows, store, *, max_tokens=4096, model="reasoning-model"): builder = MasteryContextBuilder(store) builder_rows[:] = [builder] return await builder.build( session_id="s", llm_config=SimpleNamespace(model=model, context_window=272000, max_tokens=max_tokens), ) @pytest.mark.asyncio async def test_a_truncated_memory_is_not_retried_on_every_turn(monkeypatch): """The same call, model and limit would be cut off again after another long wait.""" calls = [] holder = _truncating_stream(monkeypatch, calls) store = store_for(records()) first = await _build(holder, store) second = await _build(holder, store) third = await _build(holder, store) assert len(calls) == 1 store.update_summary.assert_not_awaited() for result in (first, second, third): assert result.token_count <= result.budget assert "example 0 failed" in json.dumps(result.history_sources) @pytest.mark.asyncio async def test_raising_the_output_limit_retries_without_waiting_out_the_backoff(monkeypatch): calls = [] holder = _truncating_stream( monkeypatch, calls, truncate=lambda kwargs: kwargs["max_tokens"] < 5000 ) store = store_for(records()) await _build(holder, store, max_tokens=4096) await _build(holder, store, max_tokens=4096) assert len(calls) == 1 store.update_summary.assert_not_awaited() await _build(holder, store, max_tokens=8192) assert len(calls) == 2 assert store.update_summary.await_count == 1 @pytest.mark.asyncio async def test_the_backoff_expires(monkeypatch): import deeptutor.capabilities.mastery.memory as memory_module calls = [] holder = _truncating_stream(monkeypatch, calls) store = store_for(records()) now = [1000.0] monkeypatch.setattr(memory_module.time, "monotonic", lambda: now[0]) await _build(holder, store) now[0] += memory_module._TRUNCATION_BACKOFF_SECONDS - 1 await _build(holder, store) assert len(calls) == 1 now[0] += 2 await _build(holder, store) assert len(calls) == 2 @pytest.mark.asyncio async def test_other_failures_are_still_retried_every_turn(monkeypatch): calls = [] async def stream(_self, **kwargs): calls.append(kwargs["max_tokens"]) kwargs["outcome"].finish_reason = "stop" yield "not json" monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) store = store_for(records()) for _ in range(2): await MasteryContextBuilder(store).build( session_id="s", llm_config=SimpleNamespace( model="reasoning-model", context_window=272000, max_tokens=4096 ), ) assert len(calls) == 2 store.update_summary.assert_not_awaited() @pytest.mark.asyncio async def test_failed_memory_retries_stay_within_the_history_plan_as_the_session_grows( monkeypatch, ): """#2496: the watermark stays put, so every retry used to resend the whole backlog.""" prompts = [] async def stream(_self, **kwargs): prompts.append(kwargs["user_prompt"]) kwargs["outcome"].finish_reason = "stop" yield memory([]) monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) rows = records(n=24) config = SimpleNamespace(model="deepseek-flash", context_window=1048576, max_tokens=8192) for turns in (8, 16, 24): store = store_for(rows[: 2 * turns]) result = await MasteryContextBuilder(store).build(session_id="s", llm_config=config) store.update_summary.assert_not_awaited() assert result.token_count <= result.budget exchanges = [prompt.split("New archived exchanges (data):\n", 1)[1] for prompt in prompts] assert len(exchanges) == 3 assert all(count_tokens(sent) <= result.budget for sent in exchanges) # Each retry is the same oldest exchanges, not a call that grows every turn. assert len(set(prompts)) == 1 @pytest.mark.asyncio async def test_a_long_backlog_is_folded_in_bounded_steps(monkeypatch): from deeptutor.capabilities.mastery.memory import archive_id from deeptutor.services.session.model_history import history_groups covered = [] async def stream(_self, **kwargs): ids = [archive_id(g) for g in history_groups(builders[-1]._summary_rows)] covered.append(ids) kwargs["outcome"].finish_reason = "stop" yield memory(ids) monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) builders = [] rows = records(n=12) config = SimpleNamespace(model="deepseek-flash", context_window=1048576, max_tokens=8192) summary, watermark = "", 0 # About 6k tokens per exchange: three of them fit the 20k history plan. for expected in ( ["hs-tutor-2", "hs-tutor-4", "hs-tutor-6"], ["hs-tutor-8", "hs-tutor-10", "hs-tutor-12"], ): store = store_for(rows, summary=summary, watermark=watermark) builders.append(MasteryContextBuilder(store)) result = await builders[-1].build(session_id="s", llm_config=config) assert covered[-1] == expected # The watermark moves only past the exchanges the memory covered; the # rest of the backlog is folded in on the next turn. store.update_summary.assert_awaited_once() _, summary, watermark = store.update_summary.await_args.args assert watermark == int(expected[-1].removeprefix("hs-tutor-")) assert result.token_count <= result.budget assert "example 11 failed" in json.dumps(result.history_sources) @pytest.mark.asyncio async def test_an_oldest_exchange_over_the_plan_sends_no_memory_call(monkeypatch): """#2510 review: "at least one" exchange sent a single 45k one to a 20k plan.""" calls = [] async def stream(_self, **kwargs): calls.append(kwargs) kwargs["outcome"].finish_reason = "stop" yield "not json" monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) store = store_for(records(n=2, padding=15000)) config = SimpleNamespace(model="deepseek-flash", context_window=1048576, max_tokens=8192) for _ in range(2): result = await MasteryContextBuilder(store).build(session_id="s", llm_config=config) assert result.token_count <= result.budget assert "distinct-0 " * 15000 in result.history_sources["hs-tutor-2"] assert calls == [] store.update_summary.assert_not_awaited() @pytest.mark.asyncio async def test_the_memory_request_counts_prior_memory_and_instructions(monkeypatch): """#2510 review: two exchanges fit the plan, but not with the memory around them.""" from deeptutor.capabilities.mastery.memory import archive_id from deeptutor.services.session.model_history import history_groups calls = [] async def stream(_self, **kwargs): calls.append(kwargs) kwargs["outcome"].finish_reason = "stop" yield "not json" monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) prior = json.loads(memory(["hs-tutor-2"])) prior["learner_state"][0]["note"] = "remembered " * 1500 store = store_for(records(n=4, padding=3100), summary=json.dumps(prior), watermark=2) builder = MasteryContextBuilder(store) config = SimpleNamespace(model="deepseek-flash", context_window=1048576, max_tokens=8192) result = await builder.build(session_id="s", llm_config=config) assert len(calls) == 1 sent = count_tokens(calls[0]["user_prompt"]) + count_tokens(calls[0]["system_prompt"]) assert sent <= result.budget assert [archive_id(g) for g in history_groups(builder._summary_rows)] == ["hs-tutor-4"] store.update_summary.assert_not_awaited() def _fold_with(monkeypatch, covered, *, prior=None): """One fold of exchange hs-tutor-4 after hs-tutor-2 was folded and retained.""" data = json.loads(memory(["hs-tutor-4"])) data["covered_archive_ids"] = covered data["learner_state"][0]["archive_ids"] = ["hs-tutor-2", "hs-tutor-4"] async def stream(_self, **kwargs): kwargs["outcome"].finish_reason = "stop" yield json.dumps(data) monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) rows = records(3, 1) builder = MasteryContextBuilder(store_for(rows)) builder._archive_ids = {"hs-tutor-2", "hs-tutor-4", "hs-tutor-6"} builder._summary_prior = memory(["hs-tutor-2"]) if prior is None else prior return builder._summarize( session_id="s", language="en", source_text="original", summary_budget=4096, replay_request={"rows": rows[2:4]}, ) @pytest.mark.asyncio @pytest.mark.parametrize( "covered", [["hs-tutor-2", "hs-tutor-4"], ["hs-tutor-4", "hs-tutor-2", "hs-tutor-4"]] ) async def test_memory_may_list_previously_retained_exchanges_as_covered(monkeypatch, covered): """The prompt lets entries cite retained IDs; echoing them as covered is not invention.""" text, _ = await _fold_with(monkeypatch, covered) # Only the newly folded exchange is recorded as covered by this fold. assert json.loads(text)["covered_archive_ids"] == ["hs-tutor-4"] @pytest.mark.asyncio @pytest.mark.parametrize( ("covered", "prior"), [ # Still omits the newly supplied exchange. (["hs-tutor-2"], None), # Invented ID next to the real ones. (["hs-tutor-2", "hs-tutor-4", "hs-tutor-999"], None), # A real exchange on the branch that this fold never saw. (["hs-tutor-4", "hs-tutor-6"], None), # Not retained: the prior memory never covered or cited it. (["hs-tutor-2", "hs-tutor-4"], "plain legacy summary"), ], ) async def test_memory_coverage_still_rejects_omitted_or_unseen_exchanges( monkeypatch, covered, prior ): with pytest.raises(ValueError, match="omitted or invented"): await _fold_with(monkeypatch, covered, prior=prior) @pytest.mark.asyncio async def test_a_fold_that_echoes_retained_coverage_advances_the_watermark(monkeypatch): from deeptutor.capabilities.mastery.memory import archive_id from deeptutor.services.session.model_history import history_groups async def stream(_self, **kwargs): new = [archive_id(g) for g in history_groups(builder._summary_rows)] kwargs["outcome"].finish_reason = "stop" yield memory(["hs-tutor-2", *new]) monkeypatch.setattr(_ContextSummaryAgent, "stream_llm", stream) store = store_for(records(), summary=memory(["hs-tutor-2"]), watermark=2) builder = MasteryContextBuilder(store) config = SimpleNamespace(model="deepseek-flash", context_window=1048576, max_tokens=8192) result = await builder.build(session_id="s", llm_config=config) store.update_summary.assert_awaited_once() _, summary, watermark = store.update_summary.await_args.args new = [archive_id(g) for g in history_groups(builder._summary_rows)] assert new and "hs-tutor-2" not in new assert json.loads(summary)["covered_archive_ids"] == new assert watermark == int(new[-1].removeprefix("hs-tutor-")) assert result.token_count <= result.budget