To generate adaptive behavior, the nervous system is coupled to the environment. The coupling constrains the dynamical properties that the nervous system and the environment must have relative to each other if adaptive behavior is to be produced. In previous computational studies, such constraints have been used to evolve controllers or artificial agents to perform a behavioral task in a given environment. Often, however, we already know the controller, the real nervous system, and its dynamics. Here we propose that the constraints can also be used to solve the inverse problem--to predict from the dynamics of the nervous system the environment to which they are adapted, and so reconstruct the production of the adaptive behavior by the entire coupled system. We illustrate how this can be done in the feeding system of the sea slug Aplysia. At the core of this system is a central pattern generator (CPG) that, with dynamics on both fast and slow time scales, integrates incoming sensory stimuli to produce ingestive and egestive motor programs. We run models embodying these CPG dynamics--in effect, autonomous Aplysia agents--in various feeding environments and analyze the performance of the entire system in a realistic feeding task. We find that the dynamics of the system are tuned for optimal performance in a narrow range of environments that correspond well to those that Aplysia encounter in the wild. In these environments, the slow CPG dynamics implement efficient ingestion of edible seaweed strips with minimal sensory information about them. The fast dynamics then implement a switch to a different behavioral mode in which the system ignores the sensory information completely and follows an internal "goal," emergent from the dynamics, to egest again a strip that proves to be inedible. Key predictions of this reconstruction are confirmed in real feeding animals.