Implement a simple memory buffer class in Python that retains the last N turns of a conversation but prioritizes keeping system prompts and critical metadata intact.
A turn is a dictionary. Preserve turns whose role is system or whose type is metadata and critical is True; evict the oldest unprotected turns first. Implement MemoryBuffer(capacity), add(turn), and get_turns(), returning retained turns in original order. If protected turns exceed the capacity, retain all protected turns.
def retain_turns(turns, capacity):