He stayed there for three days. When the third day came, they said to Ali, "This is the last day of your companion's term, so tell him to leave."Ali informed him of this, and he said, "Yes," and departed.
فأقام بها ثلاثة أيام، فلما أن كان يوم الثالث، قالوا لعلي: هذا آخر يوم من شرط صاحبك فأمره فليخرج، فأخبره بذلك فقال: نعم فخرج.
Bukhari reported it in the Book of Maghazi (4251), and Muslim in the Book of Jihad (1783:92), both from the hadith of Abu Ishaq, from Bara, in a long hadith.