import os

runner_path = r"C:\Users\intel\AppData\Local\OptimusRuntime\Labs\AR74\12_ar88_authority_operational_closure\evidence\run_ar88_verification.py"

with open(runner_path, "r", encoding="utf-8") as f:
    content = f.read()

# Replace the assertion
old_check = """    assert brain_route_res.get("autonomous_hook_dispatched"), f"BrainHost operational route failed: {brain_route_res}" """
# In AR-88, the result json records shadow_hook_requested, shadow_hook_accepted, shadow_hook_started, shadow_hook_completed_or_failed
new_check = """    assert brain_route_res.get("shadow_hook_requested") and brain_route_res.get("shadow_hook_accepted"), f"BrainHost operational route failed: {brain_route_res}" """

if old_check.strip() in content:
    content = content.replace(old_check.strip(), new_check.strip())
    print("Replaced assert brain_route_res cleanly!")
else:
    # Try exact substring replacement
    target = 'assert brain_route_res.get("autonomous_hook_dispatched"), f"BrainHost operational route failed: {brain_route_res}"'
    replacement = 'assert brain_route_res.get("shadow_hook_requested") and brain_route_res.get("shadow_hook_accepted"), f"BrainHost operational route failed: {brain_route_res}"'
    if target in content:
        content = content.replace(target, replacement)
        print("Replaced via direct string match!")
    else:
        print("WARNING: target not found!")

# Also add AR-88 negative test execution alongside antiforgery tests
old_af_run = """    print("\\n[AR-88] Executing 9 Anti-Forgery negative gate tests...")
    import pytest
    af_test_path = str(AR88_ROOT / "tests" / "test_ar84_antiforgery_gates.py")
    af_ret = pytest.main([af_test_path, "-v"])
    summary["antiforgery_gates_returncode"] = af_ret
    assert af_ret == 0, f"Anti-forgery gate tests failed with code {af_ret}" """

new_af_run = """    print("\\n[AR-88] Executing Anti-Forgery and Closure negative gate tests...")
    import pytest
    af_test_path = str(AR88_ROOT / "tests" / "test_ar84_antiforgery_gates.py")
    af_ret = pytest.main([af_test_path, "-v"])
    summary["antiforgery_gates_returncode"] = af_ret
    assert af_ret == 0, f"Anti-forgery gate tests failed with code {af_ret}"

    ar88_test_path = str(AR88_ROOT / "tests" / "test_ar88_closures.py")
    ar88_ret = pytest.main([ar88_test_path, "-v"])
    summary["ar88_closures_returncode"] = ar88_ret
    assert ar88_ret == 0, f"AR-88 closure tests failed with code {ar88_ret}" """

if old_af_run.strip() in content:
    content = content.replace(old_af_run.strip(), new_af_run.strip())
    print("Added AR-88 closure tests to runner cleanly!")
else:
    # Try finding snippet
    snip_target = 'assert af_ret == 0, f"Anti-forgery gate tests failed with code {af_ret}"'
    snip_repl = '''assert af_ret == 0, f"Anti-forgery gate tests failed with code {af_ret}"

    ar88_test_path = str(AR88_ROOT / "tests" / "test_ar88_closures.py")
    ar88_ret = pytest.main([ar88_test_path, "-v"])
    summary["ar88_closures_returncode"] = ar88_ret
    assert ar88_ret == 0, f"AR-88 closure tests failed with code {ar88_ret}"'''
    if snip_target in content:
        content = content.replace(snip_target, snip_repl)
        print("Replaced snip_target cleanly!")
    else:
        print("WARNING: snip_target not found!")

with open(runner_path, "w", encoding="utf-8") as f:
    f.write(content)

print("SUCCESS: Updated run_ar88_verification.py")

