The details of this type of study (last time this was posted) state that certain caterpillar neurons must exist at a certain state of development, if the caterpillars are taught the aversion behavior too soon, the butterflies never retain the behavior so this is how they know that detail. Then only a percentage (IIRC < 70 percent) of the butterflies remember the behavior they learned as caterpillars.
Here was a followup direct to a study with details and moths. https://journals.plos.org/plosone/article?id=10.1371/journal...