This answer is based on standard ACLU advice. The ACLU is the American Civil Liberties Union, the pre-eminent civil rights organization in the USA. Their lawyers understand statute and case law extremely well.
The concept of a "consensual encounter" with the police seems to confuse a lot of people. That might exist if you're on a date with a cop, or standing in line at a hot dog stand. But if you are interacting with them in the course of their duties, consent is not yours to withdraw. I don't like that either, but you can't fix it with a downvote button, you need to talk to your political representataives.
It may feel like a consensual encounter, but that feeling comes from social cues. The police are trained to create those social cues to manipulate you. If this surprises you, look into behavioral science - the game of manipulation is well-studied.
You are either detained, arrested, or free to go.
And the cop decides that based on training and law.
Resident decides to end consensual encounter
That is not a thing. You don't get to decide when a police encounter ends. Here is the proper way to do that:
You ask "Am I free to go?"
If it's yes, shut up and skeedaddle. Otherwise
"Why are you detaining me?"
This forces the wheels of their mind to think about a reason, and it puts them on notice that a) you know your rights, b) you are likely to raise a ruckus later about the encounter, so they will probably have to justify that reason later to supervisors, when you start making noise.
With any luck they'll say "oh, I'm not... yeah, you're free to go".
Now if they have a reason, well, that's that. You have to sleep in that bed.
It's better you sleep in that bed outside the house.
Separate "you" from "your house".
The problem is, you're trying to use your house as a shield to make the cop go away. That doesn't work and causes a bad thing.
If you are not free to go, and clearly you are not, then withdrawing into the house is an invitation for the police to chase you. They will simply follow you into the house, "in order to continue detaining you", and you will still be having the conversation in whatever room you have led them to. This functionally destroys your ability to consent to a search. They now have a right to gaze at anything in plain view anywhere they can claim they have a reason to be, and they will stretch that like a rubberband.
You need to do something totally different. You need to flip your door latch so it auto-locks behind you, and then you need to step OUT of your house and pull the door shut behind you.
There you are, on your porch or sidewalk, right in front of them, outside your house. You are detained; there are no complications for them; they already have you. Thus they have no valid reason to enter the home.
DO NOT withdraw from the door to get keys, coat, whatever, because of the above! Obviously, don't bring your phone, unless you really, really understand your phone rights and your phone's abilities... and don't assume you do.
You have your conversation outside the house, you use the right words to defend your rights, and they leave (with or without you in custody). The home is not entered.
They are likely to play head games to get you to go back inside to get something, like ask to see your phone. That is a trick. You simply say "I do not consent to a search, I'm not going back inside. Am I free to go?"
Now when they asked you why you came outside, you say "so we could have a conversation". You might have said "because I need to stretch my legs" or "I want to walk down to the liquor store" or what have you. If you did, your next action should conform to that. Certainly don't think about re-entering the house until they are long gone.
If you have to go to a neighbor to have them call a locksmith, then that's what that is.
So the moral of the story is: don't answer your door in your underwear.