SOUL.md compliance: 'When I have verified sources, I must consult them before I generate from pattern alone. Retrieval is not a feature. It is the primary mechanism by which I avoid lying.' scripts/grounding.py: GroundingLayer with ground() - queries memory files + context before generation GroundingResult with grounded flag, confidence, sources, hedging indicator format_sources() for display Searches memory/*.md and provided context text Tests: 6 passing
68 lines
2.5 KiB
Python
68 lines
2.5 KiB
Python
"""Tests for grounding-before-generation - SOUL.md compliance."""
|
|
import pytest
|
|
from pathlib import Path
|
|
import tempfile
|
|
|
|
|
|
class TestGrounding:
|
|
def test_ground_with_memory(self, tmp_path):
|
|
from scripts.grounding import GroundingLayer
|
|
mem_dir = tmp_path / "memory"
|
|
mem_dir.mkdir()
|
|
(mem_dir / "test.md").write_text("Python is a programming language created by Guido.")
|
|
|
|
layer = GroundingLayer(memory_dir=mem_dir)
|
|
result = layer.ground("What is Python?")
|
|
|
|
assert result.grounded
|
|
assert result.confidence > 0
|
|
assert len(result.sources_found) > 0
|
|
|
|
def test_ground_no_sources(self, tmp_path):
|
|
from scripts.grounding import GroundingLayer
|
|
mem_dir = tmp_path / "memory"
|
|
mem_dir.mkdir()
|
|
|
|
layer = GroundingLayer(memory_dir=mem_dir)
|
|
result = layer.ground("What is quantum physics?")
|
|
|
|
assert not result.grounded
|
|
assert result.needs_hedging
|
|
assert result.confidence == 0.0
|
|
|
|
def test_ground_with_context(self):
|
|
from scripts.grounding import GroundingLayer
|
|
layer = GroundingLayer(memory_dir=Path("/nonexistent"))
|
|
|
|
context = [{"content": "The fleet uses tmux for agent management", "source": "fleet-ops"}]
|
|
result = layer.ground("How does the fleet work?", context=context)
|
|
|
|
assert result.grounded
|
|
assert result.source_type == "context"
|
|
|
|
def test_format_sources_grounded(self):
|
|
from scripts.grounding import GroundingLayer, GroundingResult
|
|
layer = GroundingLayer()
|
|
result = GroundingResult(
|
|
query="test", grounded=True,
|
|
sources_found=[{"text": "test info", "source": "test.md", "type": "memory", "score": 0.8}],
|
|
)
|
|
formatted = layer.format_sources(result)
|
|
assert "verified sources" in formatted
|
|
assert "test.md" in formatted
|
|
|
|
def test_format_sources_ungrounded(self):
|
|
from scripts.grounding import GroundingLayer, GroundingResult
|
|
layer = GroundingLayer()
|
|
result = GroundingResult(query="test", grounded=False)
|
|
formatted = layer.format_sources(result)
|
|
assert "pattern matching" in formatted
|
|
|
|
def test_empty_memory_dir(self, tmp_path):
|
|
from scripts.grounding import GroundingLayer
|
|
mem_dir = tmp_path / "empty"
|
|
mem_dir.mkdir()
|
|
layer = GroundingLayer(memory_dir=mem_dir)
|
|
result = layer.ground("anything")
|
|
assert not result.grounded
|