Does comparative advantage sustain over long periods of time? Let's say that given the same resources,

company A can produce 40 pens or 50 pencils,

company B can produce 30 pens or 30 pencils.

Since A has a comparative advantage in pencils, A decides to produce 50 pencils while B agrees to produce 30 pens. They trade 10 of A's pencils for 9 of B's pens.

Over time, A forgets how to make pens and B forgets how to make pencils. Does it still make sense for them to trade? Would it have made sense for them to trade if A never knew how to make pens and B how to make pencils? Since the situations of 'forgetting' and 'never knowing' are essentially the same.
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&amp;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?