If a country wants to join an international organization or alliance, and that organization wants that country to join, then they're in as soon as they signed the papers or whatever ratifies that. But how can a country guarantee to not join?
For example, Russia has as one of its main demands in its current war with Ukraine, that Ukraine shall not join NATO. Let's assume the Ukrainian government finally agrees, they sign the peace treaty, the Russians go home, and then suddenly the Ukrainian government steps down or gets ousted in a coup, and the new government declares the treaty (made by the previous government they deem "traitorous") invalid, and joins NATO anyway before the Russians can react.
What measures can be done (or are usually done if it has been done before) to guarantee that the above "trick" cannot so easily void such an agreement?
Edit: of course nothing is 100% sure as there is no one to actually enforce such things between countries, but there surely can be measures to make it more binding. I doubt (for the example I used) that Russia will be satisfied with a simple "ok, sure, whatever", without any more complex agreements and guarantees. In the pre-modern eras it was usually done by exchanging family members of the royal families as hostages, but that practice died out a long time ago. So let's understand the question as how would it be made more difficult for such a treaty to be easily and trivially overridden?