ouroboros/tests/test_context_memory_preparation.py
Ouroboros 790edca788 Consolidate complete memory sources before an oversized Nano turn
NOT_REVIEWED private integration checkpoint. Explicit startup pressure reuses dialogue, chronicle, scratchpad and knowledge maintenance with exact current task and retained sources. Large reflection inputs are read completely across authored working views. Max and pure context previews never start maintenance. Same task usage scope and recorded source progress remain authoritative. Source-domain classification reflects the pure book reader and shared native file read capability.
2026-09-13 02:52:04 +03:00

61 lines
3.3 KiB
Python

"""Only actual Nano startup relieves measured source pressure before Main."""
import json
from ouroboros import context
from ouroboros.context_fit import estimate_context_prompt_tokens
from ouroboros.tools.registry import ToolContext
from tests.test_doc_context import _make_env_and_memory
from tests import test_consolidator_context_fit as fit_helpers
from tests.test_memory_pressure_maintenance import SourceReader
fit = fit_helpers.fit
def _setup(tmp_path):
env, memory = _make_env_and_memory(tmp_path)
chat = env.drive_root / "logs/chat.jsonl"
raw = json.dumps({"chat_id": 1, "direction": "in", "text": "Complete original discussion. " * 16000 + "FINAL OWNER DECISION",
"ts": "2026-09-13T00:00:00Z"}) + "\n"
chat.write_text(raw)
task = {"id": "startup", "type": "task", "text": "Continue with my exact final decision."}
ctx = ToolContext(repo_dir=env.repo_dir, drive_root=env.drive_root, task_id=task["id"])
return env, memory, task, ctx, raw
def test_actual_nano_preparation_consolidates_complete_source_before_returning(tmp_path, fit, monkeypatch):
fit.window = 50000
env, memory, task, ctx, raw = _setup(tmp_path)
monkeypatch.setattr(context, "get_context_mode", lambda: "nano")
def fits(messages, tools):
measured = estimate_context_prompt_tokens(messages, tools)
return {"accepted": measured < 16000, "input_tokens": measured, "strict_bound_proven": False}
actor = SourceReader(env.drive_root, fit.window)
before = context.build_context_fit_plan(env, memory, task, preferred_mode="nano", ctx=ctx)
assert not fits(before.messages_for("nano"), [])["accepted"]
messages, info = context.build_llm_messages(env, memory, task, ctx=ctx, llm=actor,
tool_schemas=[], fit_candidate=fits)
assert info["context_memory_maintenance"]["status"] == "fitting"
assert fits(messages, [])["accepted"]
assert (env.drive_root / "logs/chat.jsonl").read_text() == raw
assert actor.calls and info["context_memory_maintenance"]["usage"]["cost"] > 0
events = [json.loads(line) for line in (env.drive_root / "logs/events.jsonl").read_text().splitlines()]
receipt = next(row for row in events if row.get("type") == "context_memory_maintenance")
assert receipt["task_id"] == task["id"] and receipt["changed_sources"]
assert receipt["status"] == "fitting"
assert messages[-1]["content"] == task["text"]
# Repeated preparation sees the consolidated source; it does not buy another maintenance run.
calls = len(actor.calls)
context.build_llm_messages(env, memory, task, ctx=ctx, llm=actor, tool_schemas=[], fit_candidate=fits)
assert len(actor.calls) == calls
def test_max_and_pure_preview_never_start_a_maintenance_model(tmp_path, fit, monkeypatch):
env, memory, task, ctx, _raw = _setup(tmp_path)
actor = SourceReader(env.drive_root, 50000)
monkeypatch.setattr(context, "get_context_mode", lambda: "max")
context.build_llm_messages(env, memory, task, ctx=ctx, llm=actor,
tool_schemas=[], fit_candidate=lambda m,t: {"accepted": False})
monkeypatch.setattr(context, "get_context_mode", lambda: "nano")
context.build_context_fit_plan(env, memory, task, preferred_mode="nano", ctx=ctx)
assert not actor.calls