These two invariants of the ShardServerCatalogCacheLoader verify that the ChunkVersions of the last task is contiguous to the ChunkVersions of the new task. However these invariants are incomplete: they do not properly cover scenarios in which there are stepdowns/stepups.
Note that when a new node is elected as primary it bumps its term. This is important because it changes how the minQueryVersion is computed: instead of looking at the in-memory list of tasks associated with the previous term (i.e., last time this node was primary) we end up taking as minQueryVersion whatever version is persisted on config.cache.chunks. However, when we add this new task to the list, the implementation does not consider the terms and ends up comparing versions that are not contiguous: the maxQueryVersion of the last task is different than the minQueryVersion of the new task.
The proposed solution is to avoid comparing tasks of different terms.