This thread raises a number of interesting questions, and necessarily definitive answers are not available unless and until some case arises in which the questions are presented and answered by the Supreme Judicial Court.
Nonethess, for what it is worth (that is to say, as a prediction of what the Court might hold, which prediction could be either right or wrong):
The term "dwelling", as applied to a single family house, should include the curtilege, i.e., the boundaries of the property line, while as applied to a multi-family occupancy, it would stop at the unit walls and would not include the common areas. This is the distinction that the Court applied under the former version of ch. 296, sec. 10(a). However, Commonwealth v. McKinnon, 446, Mass. 263 (2006), strongly suggests to the contrary. So the safe thing to do is to assume that the "Castle Law" stops at the wall of the "castle."
A person is "unlawfully" in a dwelling when he is a trespasser, and in Massachusetts a person is a trespasser when he unlawfully "enters or remains" in or on the property. See G.L. ch. 266, sec. 120, the criminal trespass statute:
"Whoever, without right enters or remains in or upon the dwelling house, buildings, boats or improved or enclosed land, wharf, or pier of another, or enters or remains in a school bus, as defined in section 1 of chapter 90, after having been forbidden so to do by the person who has lawful control of said premises, whether directly or by notice posted thereon, or in violation of a court order pursuant to section thirty-four B of chapter two hundred and eight or section three or four of chapter two hundred and nine A, shall be punished by a fine of not more than one hundred dollars or by imprisonment for not more than thirty days or both such fine and imprisonment. Proof that a court has given notice of such a court order to the alleged offender shall be prima facie evidence that the notice requirement of this section has been met. A person who is found committing such trespass may be arrested by a sheriff, deputy sheriff, constable or police officer and kept in custody in a convenient place, not more than twenty-four hours, Sunday excepted, until a complaint can be made against him for the offence, and he be taken upon a warrant issued upon such complaint.
"This section shall not apply to tenants or occupants of residential premises who, having rightfully entered said premises at the commencement of the tenancy or occupancy, remain therein after such tenancy or occupancy has been or is alleged to have been terminated. The owner or landlord of said premises may recover possession thereof only through appropriate civil proceedings. "
As a consequence, under the scenario where a malefactor secures permission to enter under false pretenses and is then ordered by the homeowner to leave, that malefactor should be considered to be "unlawfully" in the premises.
Final observation: the statutes in question were enacted to reverse the decision of the SJC in a then-famous case called Commonwealth v. Shaffer, 367 Mass. 508 (1975), in which the SJC declined to align Massachusetts with the rest of the states in recognizing an except to the duty to retreat when one was attacked in one's own home. The resulting "Castle Law" is something that may help you if you find yourself in such a situation and employ deadly force. However, it is not sound planning to rely on such a defense if there are alternatives. If the PD is on the way and you can safely scoot out the back door, doing so may save you a small amount of ammo and a huge amount of grief.