score:2
Your suggested scenario is correct (my wife travels as a dual national each year) and there shouldn't be any issues.
When you check-in at country X for your flight to country Y it will most likely be assumed that you are in the country legally so they probably won't ask questions about the lack of a visa in passport B. Even if they did then you would just explain that you're a dual national and show the visa in passport A to prove you were visiting legally.
When leaving country X you pass through passport control on passport A since that is the passport you arrived on, and they may ask to see passport B for proof that you can legally enter country Y (unfortunately my wife can't remember definitively if she usually needs to show both passports, but probably you would only need to show passport B if asked).
The general rule to remember is to enter and exit a country with the same passport.
edit: It may be worth stating what the the countries are for absolute peace of mind. If any of the countries involved have issues with each other (e.g. Israel and Arab countries) then that can be trickier.