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

# Relocate ws_str and is_temp_ws before eff_req_auth
bad_block = '''        promo_id = promotion_id or _new_id("promo")
        w_root = Path(workspace_root).resolve()

        eff_req_auth = require_authenticated_provenance
        if eff_req_auth is None:
            eff_req_auth = True if telemetry_audit is not None else (False if is_temp_ws else True)

        details_payload: Dict[str, Any] = {
            "promotion_id": promo_id,
            "incident_id": incident_id,
            "mission_id": incident_id,
            "telemetry_audit": telemetry_audit,
            "require_authenticated_provenance": eff_req_auth,
        }
        if rollback_plan:
            details_payload["rollback_plan"] = rollback_plan

        # Replay guard check at executor gate if telemetry_audit is provided
        if eff_req_auth and telemetry_audit:
            if hasattr(telemetry_audit, "verify_authenticity"):
                is_valid, reason = telemetry_audit.verify_authenticity(
                    expected_workspace_root=str(w_root),
                    enforce_replay_guard=True,
                )
                if not is_valid:
                    dec = PromotionDecision(
                        promotion_allowed=False,
                        decision="BLOCK",
                        reason_code=f"TELEMETRY_INVALID: {reason}",
                        target_file=target_file,
                        details=details_payload,
                    )
                    return PromotionExecutionResult(
                        promotion_id=promo_id,
                        target_file=target_file,
                        decision=dec,
                        auto_promotion=False,
                        promotion_verified=False,
                        production_file_changed=False,
                        error_message=f"PROMOTION_BLOCKED: {reason}",
                    )

        ws_str = str(w_root).replace("\\\\", "/").lower()
        is_temp_ws = any(m in ws_str for m in ("temp", "tmp", "pytest", "test_ws"))
        is_prod_workspace = not is_temp_ws or (ws_str in ("c:/xampp/htdocs/digital optimus", "c:/xampp/htdocs/digital optimus/"))'''

good_block = '''        promo_id = promotion_id or _new_id("promo")
        w_root = Path(workspace_root).resolve()

        ws_str = str(w_root).replace("\\\\", "/").lower()
        is_temp_ws = any(m in ws_str for m in ("temp", "tmp", "pytest", "test_ws"))
        is_prod_workspace = not is_temp_ws or (ws_str in ("c:/xampp/htdocs/digital optimus", "c:/xampp/htdocs/digital optimus/"))

        eff_req_auth = require_authenticated_provenance
        if eff_req_auth is None:
            eff_req_auth = True if telemetry_audit is not None else (False if is_temp_ws else True)

        details_payload: Dict[str, Any] = {
            "promotion_id": promo_id,
            "incident_id": incident_id,
            "mission_id": incident_id,
            "telemetry_audit": telemetry_audit,
            "require_authenticated_provenance": eff_req_auth,
        }
        if rollback_plan:
            details_payload["rollback_plan"] = rollback_plan

        # Replay guard check at executor gate if telemetry_audit is provided
        if eff_req_auth and telemetry_audit:
            if hasattr(telemetry_audit, "verify_authenticity"):
                is_valid, reason = telemetry_audit.verify_authenticity(
                    expected_workspace_root=str(w_root),
                    enforce_replay_guard=True,
                )
                if not is_valid:
                    dec = PromotionDecision(
                        promotion_allowed=False,
                        decision="BLOCK",
                        reason_code=f"TELEMETRY_INVALID: {reason}",
                        target_file=target_file,
                        details=details_payload,
                    )
                    return PromotionExecutionResult(
                        promotion_id=promo_id,
                        target_file=target_file,
                        decision=dec,
                        auto_promotion=False,
                        promotion_verified=False,
                        production_file_changed=False,
                        error_message=f"PROMOTION_BLOCKED: {reason}",
                    )'''

# Normalize single backslash in ws_str
bad_block_alt = bad_block.replace("\\\\", "\\")
good_block_alt = good_block.replace("\\\\", "\\")

if bad_block in content:
    content = content.replace(bad_block, good_block, 1)
    print("Replaced with double backslash")
elif bad_block_alt in content:
    content = content.replace(bad_block_alt, good_block_alt, 1)
    print("Replaced with single backslash")
else:
    print("Failed to match bad_block")

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

