Adaptive behaviour requires the ability to focus on a task and protect it from distraction (cognitive stability) and to rapidly switch tasks when circumstances change (cognitive flexibility). Burgeoning research literatures have aimed to understand how people achieve task focus and task switch readiness. In this Perspective, I integrate these literatures to derive a cognitive architecture and functional rules underlying the regulation of cognitive stability and flexibility. I propose that task focus and task switch readiness are supported by independent mechanisms. However, I also suggest that the strategic regulation of both mechanisms is governed by shared learning principles: an incremental, online learner that nudges control up or down based on the recent history of task demands (a recency heuristic) and episodic reinstatement when the current context matches a past experience (a recognition heuristic). Finally, I discuss algorithmic and neural implementations of these processes, as well as clinical implications.