ouroboros/tests/test_deep_review_book_sources.py
Ouroboros edde8d32ed Replace packed scope review with repository retrieval
Deliver exact change-relative sources and shared governance tiers to reviewers. Preserve received verdicts while recording read coverage as diagnostic evidence. Retire scope window authority and packed deep review.
2026-09-18 00:42:05 +03:00

129 lines
7.3 KiB
Python

"""Deep review navigates the books by address and consumes revision-bound coverage."""
import hashlib
import json
from ouroboros import deep_self_review as deep
from ouroboros.artifacts import read_actor_source_bytes
from tests.test_deep_review_slot import _native_row, _ScriptedLLM, _tool_call
def _corpus(root):
files = {
"BIBLE.md": "# Constitution\n\nThe constitutional source.\n",
"docs/CHECKLISTS.md": "# Checklists\n\n## Review\n\nCheck the actual contract.\n",
"worker.py": "def work(): return True\n",
}
for book_id, entry in (("architecture", "docs/ARCHITECTURE.md"), ("development", "docs/DEVELOPMENT.md")):
files[entry] = f"# {book_id.title()}\n\nPurpose of the {book_id} book.\n\n## Chapters\n\n- [Flow]({book_id}/flow.md)\n- [State]({book_id}/state.md)\n"
for name in ("flow", "state"):
files[f"docs/{book_id}/{name}.md"] = (
f"# {name.title()}\n\nIntroduction to {book_id} {name}.\n\n## Contract\n\nExact {book_id} {name} contract body.\n")
for rel, text in files.items():
target = root / rel
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(text.encode("utf-8"))
return files
def _required(root, rel):
raw = (root / rel).read_bytes()
normalized = raw.decode().replace("\r\n", "\n").replace("\r", "\n")
return {"root": "system_repo", "path": rel, "source_revision": hashlib.sha256(raw).hexdigest(),
"complete_sha256": hashlib.sha256(normalized.encode()).hexdigest(), "complete_chars": len(normalized),
"range_basis": "unicode_text_universal_newlines"}
def test_the_deep_task_inlines_the_review_protocol_and_navigates_the_books(tmp_path):
"""The deep surface reviews no change, so the governance tiers yield the
standing rules and nothing change-relative: the standing disclosures and
the review protocol this reviewer executes arrive in full, every reference
book arrives as navigation with the read instruction, and BIBLE.md arrives
inline like every other surface's tier-1 governance."""
repo, data = tmp_path / "repo", tmp_path / "data"
_corpus(repo)
(repo / "docs" / "CHECKLISTS_ARCHIVE.md").write_text(
"# Standing archive\n\nSTANDING DISCLOSURE: the archive body.\n", encoding="utf-8")
(repo / "docs" / "DESIGN.md").write_text("# Design\n\nThe design system body.\n", encoding="utf-8")
entry = repo / "docs" / "DEVELOPMENT.md"
entry.write_text(entry.read_text(encoding="utf-8")
+ "- [Protocol](development/05-review-and-commit-protocol.md)\n", encoding="utf-8")
(repo / "docs" / "development" / "05-review-and-commit-protocol.md").write_text(
"# Review & Commit Protocol\n\nIntro.\n\n## Stages\n\nThe protocol body.\n", encoding="utf-8")
task, _facts = deep._retrieving_task(repo, data, usable_window_tokens=200_000)
assert "STANDING DISCLOSURE: the archive body." in task # tier 1
assert "The protocol body." in task # tier 2, always
assert "The design system body." not in task # no touched web/ path
assert task.count("The constitutional source.") == 1 # tier 1
assert _facts["required_sources"][0]["coverage_basis"] == "delivered_inline"
assert "Governance navigation (read on demand)" in task
assert 'read_file(root="system_repo"' in task
assert "Source: `docs/architecture/flow.md`" in task
assert "Exact architecture flow contract body." not in task # the map is never whole
# Order: the standing rules and the map precede the change-relative body.
assert task.index("STANDING DISCLOSURE") < task.index("## Memory")
def test_retrieving_book_navigation_uses_only_physical_chapter_addresses(tmp_path, monkeypatch):
repo, data = tmp_path / "repo", tmp_path / "data"
_corpus(repo)
# This legacy mapper must not be fed a composed book with invented entrypoint lines.
# The deep-review task reads the chapter-addressed view (context_layout.book_navigation);
# the legacy monolith mapper is no longer imported here at all.
assert not hasattr(deep, "generate_doc_nav_map")
task, _facts = deep._retrieving_task(repo, data)
assert "Source: `docs/architecture/flow.md`" in task
assert "Source: `docs/development/state.md`" in task
assert "Introduction to architecture state." in task
assert "Exact architecture state contract body." not in task
assert "file you open" in task
assert task.index("Introduction to architecture flow.") < task.index("## Memory")
def test_exact_coverage_overrides_complete_legacy_lines_without_rebinding_current_file(tmp_path):
files = _corpus(tmp_path)
row = {**_required(tmp_path, "BIBLE.md"), "status": "incomplete", "covered_chars": 7,
"missing_ranges": [[7, len(files["BIBLE.md"])]]}
usage = {"native_read_coverage": {"status": "incomplete", "sources": [row]},
"native_tool_receipts": [{"tool": "read_file", "outcome": "executed", "path": "BIBLE.md",
"root": "system_repo", "start_line": 1, "end_line": 99, "total_lines": 99}]}
(tmp_path / "BIBLE.md").write_text("Changed after the observed review source.\n")
detail = deep._native_read_coverage(usage, tmp_path)["BIBLE.md"]
assert detail["state"] == "partial" and detail["covered_chars"] == 7
assert detail["source_revision"] == row["source_revision"]
assert detail["evidence_basis"] == "source_ranges" and "covered_lines" not in detail
assert deep._delivery_incomplete("native_tool_rounds", usage) == "none"
def test_legacy_line_evidence_stays_explicit_and_unsent_reads_never_complete_it(tmp_path):
receipt = {"tool": "read_file", "outcome": "executed", "path": "BIBLE.md", "root": "system_repo",
"start_line": 1, "end_line": 5, "total_lines": 5}
usage = {"native_tool_receipts": [receipt]}
detail = deep._native_read_coverage(usage, tmp_path)["BIBLE.md"]
assert detail["state"] == "read" and detail["evidence_basis"] == "legacy_lines"
assert "source_revision" not in detail
receipt["delivered"] = False
assert deep._native_read_coverage(usage, tmp_path)["BIBLE.md"]["state"] == "missing"
def test_native_deep_review_reports_exact_chapter_gap_without_changing_the_finding(tmp_path, monkeypatch):
repo, data = tmp_path / "repo", tmp_path / "data"
_corpus(repo)
monkeypatch.setenv("OPENROUTER_API_KEY", "fixture-only")
report = "Read: Constitution.\n\nCRITICAL: a real consumer violates the contract."
llm = _ScriptedLLM([
{"tool_calls": [_tool_call("read_file", {"path": "BIBLE.md"})]}, {"content": report}])
required = [_required(repo, "BIBLE.md"), _required(repo, "docs/architecture/flow.md")]
text, usage = deep.run_deep_self_review(repo, data, llm, lambda m: None,
slot=_native_row(), task_id="book-review", required_sources=required)
assert text.endswith(report)
assert usage["deep_review_coverage_basis"] == "source_ranges"
assert usage["native_incomplete"] == "required_source_coverage_incomplete"
assert "docs/architecture/flow.md NOT read" in text
assert "incomplete=none" in text
history = json.loads(read_actor_source_bytes(data, "book-review", usage["native_history_source"]))
assert history["required_sources"] == [
{**required[0], "coverage_basis": "delivered_inline"}, required[1]]