path = r"C:\Users\intel\AppData\Local\OptimusRuntime\Labs\AR74\10_ar84_trust_boundary\evidence\run_ar84_verification.py"
with open(path, "r", encoding="utf-8") as f:
    content = f.read()

# Pass telemetry_audit to execute_promotion calls in Section 6
old_exec_1 = '''    blocked_critical_res = promo_exec_default.execute_promotion(
        target_file="app/remediation/self_repair/circuit.py",
        patch_candidate=patch_plan_obj,
        workspace_root=str(brain_ws),
        confidence="HIGH",
        root_cause_proven=True,
        sandbox_verdict=cand_verdict_obj,
        focused_tests_pass=True,
        regression_tests_pass=True,
        evidence_comparison_pass=True,
        rollback_ready=True,
        provider_called="FALSE_VERIFIED",
        network_used="FALSE_VERIFIED",
        attempt_number=1,
        is_duplicate=False,
        max_attempts=1,
        incident_id=shadow_report.incident_id,
        rollback_plan=promo_prop.get("rollback_plan"),
    )'''

new_exec_1 = '''    blocked_critical_res = promo_exec_default.execute_promotion(
        target_file="app/remediation/self_repair/circuit.py",
        patch_candidate=patch_plan_obj,
        workspace_root=str(brain_ws),
        confidence="HIGH",
        root_cause_proven=True,
        sandbox_verdict=cand_verdict_obj,
        focused_tests_pass=True,
        regression_tests_pass=True,
        evidence_comparison_pass=True,
        rollback_ready=True,
        provider_called="FALSE_VERIFIED",
        network_used="FALSE_VERIFIED",
        attempt_number=1,
        is_duplicate=False,
        max_attempts=1,
        incident_id=shadow_report.incident_id,
        rollback_plan=promo_prop.get("rollback_plan"),
        telemetry_audit=telemetry_audit,
        require_authenticated_provenance=True,
    )'''

old_exec_2 = '''    promo_res = promo_exec_approved.execute_promotion(
        target_file="app/remediation/self_repair/circuit.py",
        patch_candidate=patch_plan_obj,
        workspace_root=str(brain_ws),
        confidence="HIGH",
        root_cause_proven=True,
        sandbox_verdict=cand_verdict_obj,
        focused_tests_pass=True,
        regression_tests_pass=True,
        evidence_comparison_pass=True,
        rollback_ready=True,
        provider_called="FALSE_VERIFIED",
        network_used="FALSE_VERIFIED",
        attempt_number=1,
        is_duplicate=False,
        max_attempts=1,
        incident_id=shadow_report.incident_id,
        rollback_plan=promo_prop.get("rollback_plan"),
    )'''

new_exec_2 = '''    # Generate fresh un-replayed audit record for approved promotion
    telemetry_audit_approved = AuthenticTelemetryEvaluator.evaluate_telemetry(
        mission_id=f"mission_ar84_brain_live_{run_id}",
        instance_id=f"inst_ar84_brain_live_{run_id}",
        workspace_root=str(brain_ws),
        observer_active=True,
        monitored_scopes=[SCOPE_URLLIB_HTTP, SCOPE_RAW_SOCKET],
        detected_attempts=[],
        required_scopes={SCOPE_URLLIB_HTTP, SCOPE_RAW_SOCKET},
    ).to_dict()

    promo_res = promo_exec_approved.execute_promotion(
        target_file="app/remediation/self_repair/circuit.py",
        patch_candidate=patch_plan_obj,
        workspace_root=str(brain_ws),
        confidence="HIGH",
        root_cause_proven=True,
        sandbox_verdict=cand_verdict_obj,
        focused_tests_pass=True,
        regression_tests_pass=True,
        evidence_comparison_pass=True,
        rollback_ready=True,
        provider_called="FALSE_VERIFIED",
        network_used="FALSE_VERIFIED",
        attempt_number=1,
        is_duplicate=False,
        max_attempts=1,
        incident_id=shadow_report.incident_id,
        rollback_plan=promo_prop.get("rollback_plan"),
        telemetry_audit=telemetry_audit_approved,
        require_authenticated_provenance=True,
    )'''

if old_exec_1 in content:
    content = content.replace(old_exec_1, new_exec_1, 1)
    print("Replaced exec_1")
else:
    print("Failed exec_1")

if old_exec_2 in content:
    content = content.replace(old_exec_2, new_exec_2, 1)
    print("Replaced exec_2")
else:
    print("Failed exec_2")

# Also in main(), invoke test_isolated_brain_route
old_main_calls = '''    summary_run1 = {
        "section_1_contracts": sec1,
        "section_2_3_isolation_and_pytest": sec2_3_run1,
        "section_4_cases": {"case_a": case_a_run1, "case_b": case_b_run1, "case_c": case_c_run1},
        "section_6_brain_and_rollback": sec6_run1,
    }'''

new_main_calls = '''    # AR-84: Run isolated BrainHost operational route test and 9 anti-forgery negative tests
    logger.info("=== Running AR-84 Authentic BrainHost Operational Route Test ===")
    from test_isolated_brain_route import run_isolated_brain_test
    brain_route_res = run_isolated_brain_test()
    assert brain_route_res.get("autonomous_hook_dispatched"), "BrainHost operational route failed to dispatch autonomous hook"

    logger.info("=== Running AR-84 Antiforgery Gates (Scenarios A through I) ===")
    import pytest
    antiforgery_test_path = str(AR84_ROOT / "tests" / "test_ar84_antiforgery_gates.py")
    af_ret = pytest.main([antiforgery_test_path, "-v"])
    assert af_ret == 0, f"AR-84 Antiforgery gate tests failed with code {af_ret}"

    summary_run1 = {
        "section_1_contracts": sec1,
        "section_2_3_isolation_and_pytest": sec2_3_run1,
        "section_4_cases": {"case_a": case_a_run1, "case_b": case_b_run1, "case_c": case_c_run1},
        "section_6_brain_and_rollback": sec6_run1,
        "section_ar84_brain_operational_route": brain_route_res,
        "section_ar84_antiforgery_gates_returncode": af_ret,
    }'''

if old_main_calls in content:
    content = content.replace(old_main_calls, new_main_calls, 1)
    print("Replaced main_calls")
else:
    print("Failed main_calls")

with open(path, "w", encoding="utf-8") as f:
    f.write(content)
print("Finished modifying run_ar84_verification.py")

