The theory of comparative advantage, like Ricardo/Rybczynski/Stolper-Samuleson, is classically a static theory, but you seem to be worried about dynamics.
You seem to think specialization and "unlearning by not doing" might be bad bad. The question is why?
Specialization is almost a necessary condition for trade: if we are all equally good at all tasks, what is the point of trading? We could just produce whatever we wanted in autarky. Specialization is about learning and seems to take place over time. This goes in the direction of innovation, perhaps like Schumpeter: the country that makes pens might innovate a new writing tool that makes pencils and pens obsolete, and then A will regret specializing. Similarly, in the theory of firms, there is an idea of Absorptive Capacity of ideas. If no one at Tesla knows anything about combustion engines, they might miss out on breakthroughs in that technology that would benefit Tesla. Maybe it's because the breakthroughs are valuable in themselves, or maybe it's because having an open mind means you can have more creative ideas. So perhaps firms should keep some capacity to do things in which they do not have a comparative advantage in order to expand their R&D capacity.
But you talk about countries. The US subsidizes food production because it wants to have its own food supply for national security reasons; look at the medical mask shortage right now. So perhaps it makes sense to subsidize production of things in which a country does not have a comparative advantage in order to avoid becoming completely dependent on other countries.
So, given the information provided, perhaps you are mixing together the short-run classical theory with concerns about innovation, uncertainty, or long-run fitness?