Question? How do they know this? Do the ballots look different for each party?
They know because the official ballot envelope has the voter’s names and addresses on it, which is matched with the voter rolls.
You have to sign the envelope, they know who you are and party affiliation. Theoretically, the ballots remain in the privacy folder and are treated with equal consideration.