The algorithm goes like this:
If the text has previously been adjusted, remove last time’s adjustments.
If the text is currently overset:
Try the maximum allowed amount of shrink.
If the text is still overset
Give up – there is too much text
Choose the minimum amount of shrink that fixes the overset
Otherwise
Try the maximum allowed amount of stretch
If the text is now overset
Choose the minimum stretch that does not overset
Otherwise, if the box still has plenty of spare space
Give up – there is too little text
Otherwise
Retain this maximum stretch
The adjustments are then saved as a box slug.