If a refine shard key command finishes before this check in the migration recovery runs, this invariant will be triggered. The function findIntersectingChunk uses string comparisons to find the chunk that contains the key. However, the containsKey function uses BSONObj comparison. In the case where a refine shard key has run, the string comparison will return the correct chunk but the bsonobj comparison will fail because there is an extra field in the shard key.
The migration recovery should either specifically handle refine shard key, or the comparisons in findIntersectingChunk and containsKey should be fixed to not return conflicting information.