"""Tests for Primal Verification Agent (ported from primal-verification-agent).""" from __future__ import annotations import asyncio from symbolic_recursion.verification import ( PrimalVerificationAgent, calculate_semantic_fatigue, update_primal_kalman, ) from symbolic_recursion.verification.kalman import KalmanParams, PrimalKalmanFilter from symbolic_recursion.tools.kernel_tools import KernelToolRunner def test_kalman_update_bounds() -> None: step = update_primal_kalman(1.0, 0.0, 0.2, KalmanParams()) assert 0.0 <= step.xk <= 1.0 assert step.innovation < 0 def test_kalman_sequence_drops_on_hallucination() -> None: kf = PrimalKalmanFilter(KalmanParams(gain_k=0.9, l_star=0.5, l_fact=1.0)) kf.observe(0.95) high = kf.xk kf.observe(0.05) assert kf.xk < high assert kf.status_from_zk(0.05) == "HALLUCINATION" def test_semantic_fatigue_detects_redundancy() -> None: clean = calculate_semantic_fatigue("Unique diverse vocabulary appears here once.", []) noisy = calculate_semantic_fatigue( "repeat repeat repeat repeat words words words words always always always", [], ) assert noisy.redundancy_score > clean.redundancy_score assert noisy.cumulative_fatigue >= clean.cumulative_fatigue def test_verification_agent_flags_sun_claim() -> None: agent = PrimalVerificationAgent() report = agent.verify_text( "Hamlet was written by William Shakespeare around 1601. " "Captain John Vance stepped on the sun in 1984." ) statuses = {s.status for s in report.statements} assert "HALLUCINATION" in statuses or any(s.confidence < 0.4 for s in report.statements) assert 0.0 <= report.coherence_score <= 1.0 assert report.fatigue is not None def test_tool_verify_claims() -> None: data = asyncio.run( KernelToolRunner().run( "verify_claims", text="Shakespeare wrote Hamlet around 1601. Graphene-magma suits let humans walk on the sun.", ) ) assert data["agent"] == "primal-verification-agent" assert "statements" in data assert data["coherence_score"] is not None def test_tool_semantic_fatigue_and_kalman() -> None: fat = asyncio.run(KernelToolRunner().run("semantic_fatigue", text="word word word word again again again")) assert "cumulative_fatigue" in fat kal = asyncio.run(KernelToolRunner().run("primal_kalman", scores=[0.9, 0.1, 0.8])) assert "final_xk" in kal assert len(kal["steps"]) == 3