Yeah its the boundary detection bug, we are getting confused about where emojis can start and end.
The tricky thing here is that maybe the whole logic is wrong, I wonder why it even needs to make this kind of decision given it knows exactly where the cursor is and how many letters we passed on to emoji autocomplete. Maybe just work back N chars and then replace that… not sure.