A couple of days to a week.
Nothing wrong with a wave of lust and a wistful fantasy or two, but if he/she sticks in your head longer than a week, you can assume there is something not quite right about your relationship.
Usually just slight boredom of the same old dull routine so nothing hugely worrying as long as you take steps to fix it.
Thank you for asking/answering this question!! After 5 years, this has certainly happened to me... and I am relieved to no that it's fairly normal.
Oh yeah, having fleeting thoughts is 100% fine. :) I allow myself some room to appreciate an attractive person. XD Ahaha. Doesn't mean I'm not committed to my partner. Just means I have a liberal imagination. XD
I learned great insights to pleasing my wife by encouraging her to share her fantasy thoughts.
We even joke around, one of us sees an attractive man or woman on TV or in a magazine, I'll say "that's my girlfriend" or she'll say "He's my lover". Only boundary is we don't play that game with people we met in real life.
I agree with MM, as long as its occasional, its cool, it's normal, and probably healthy even.
It only becomes a problem when one constantly thinks and fantasizes about other people, and rarely thinks about their partner. That indicates something lacking in the relationship, and should be addressed.