Cooperation over finite horizons: a theory and experiments