import os
import re

path = r'C:\ProgramData\Ableton\Live 12 Suite\Resources\MIDI Remote Scripts\AbletonMCP_AI\abletonmcp_runtime.py'
with open(path, 'r', encoding='utf-8') as f:
    content = f.read()

old_block = '''            created_positions = []
            for index, position in enumerate(cleaned_positions):
                created_clip = track.create_audio_clip(resolved_path, float(position))
                clip_name = str(name or \"\").strip()
                if clip_name:
                    if len(cleaned_positions) > 1:
                        clip_name = clip_name + \" \" + str(index + 1)
                    try:
                        if created_clip is not None and hasattr(created_clip, \"name\"):
                            created_clip.name = clip_name
                        else:
                            for clip in getattr(track, \"clips\", []):
                                if hasattr(clip, \"start_time\") and abs(float(clip.start_time) - float(position)) < 0.01:
                                    if hasattr(clip, \"name\"):
                                        clip.name = clip_name
                                    break
                    except Exception:
                        pass
                created_positions.append(float(position))'''

new_block = '''            created_positions = []
            for index, position in enumerate(cleaned_positions):
                success = False
                created_clip = None
                for attempt in range(3):
                    try:
                        created_clip = track.create_audio_clip(resolved_path, float(position))
                    except Exception as e:
                        self.log_message("Warning: Clip creation error at attempt " + str(attempt+1) + ": " + str(e))
                        
                    import time
                    time.sleep(0.1)
                    
                    clip_persisted = False
                    for clip in getattr(track, "arrangement_clips", getattr(track, "clips", [])):
                        if hasattr(clip, "start_time") and abs(float(clip.start_time) - float(position)) < 0.05:
                            clip_persisted = True
                            created_clip = clip
                            break
                    
                    if clip_persisted:
                        success = True
                        break
                        
                    self.log_message("Warning: Clip at " + str(position) + " not persisted on attempt " + str(attempt+1))
                    time.sleep(0.1)

                if not success:
                    self.log_message("Error: Failed to persist clip at " + str(position) + " after 3 attempts")
                    continue

                clip_name = str(name or "").strip()
                if clip_name:
                    if len(cleaned_positions) > 1:
                        clip_name = clip_name + " " + str(index + 1)
                    try:
                        if created_clip is not None and hasattr(created_clip, "name"):
                            created_clip.name = clip_name
                    except Exception:
                        pass
                
                created_positions.append(float(position))'''

import textwrap
content = content.replace(old_block, new_block)
with open(path, 'w', encoding='utf-8') as f:
    f.write(content)
print("Replaced:", old_block in content)
