grab_next_visits: make the handling of CTEs more modular
This allows us to insert extra CTEs if a scheduling policy needs it.