How do miners decide when to give up on a block? So in my understanding, miners bundle a list of transactions into a block and begin working on it until they can generate a valid proof of work. There are various reasons to group fewer or more transactions together into a block, but it's ultimately up to the miner.
I'm assuming that different miners may group the same transaction into different blocks and begin working on them. One miner might group transaction A into a block of 100 transactions and start working while another miner might group transaction A into a different block of 250 transactions and also start working.
When one miner finds a valid proof of work and broadcasts the new block, do all the other miners who included even a single transaction from the valid block in their own block have to drop what they're doing and start over?
Is there some system in place to ensure that most miners are working on blocks with the exact same set of transactions except for the miner's reward transaction?
You're right, miners can freely choose among the unconfirmed transactions when they're building their block template. However, miners are optimizing for revenue, so they'll be mostly selecting transactions in order of descending feerate.
Since transactions propagate quickly on the network, most miners will have a largely overlapping view of the queue, and their block templates should be almost the same.
Since each block commits to a specific predecessor, the best chain can only have one block at each height. Mining is progress-free in that each hash independently has a (tiny) chance of yielding a valid block. So, when a new block is found, miners do not have any sunk cost, they just update for the new predecessor, pick a new set of transactions, and go right back to mining at the new height.