Buggy code:
public class BlockTreeTermsWriter extends FieldsConsumer { public static final int DEFAULT_MIN_BLOCK_SIZE = 25 ; public static final int DEFAULT_MAX_BLOCK_SIZE = 48 ; public static final boolean SAVE_DOT_FILES = false ; static final int OUTPUT_FLAGS_NUM_BITS = 2 ; static final int OUTPUT_FLAGS_MASK = 3 ; static final int OUTPUT_FLAG_IS_FLOOR = 1 ; static final int OUTPUT_FLAG_HAS_TERMS = 2 ; static final String TERMS_EXTENSION = "tim" ; static final String TERMS_CODEC_NAME = "BLOCK_TREE_TERMS_DICT" ; public static final int TERMS_VERSION_START = 0 ; public static final int TERMS_VERSION_CURRENT = BlockTreeTermsWriter . TERMS_VERSION_START ; static final String TERMS_INDEX_EXTENSION = "tip" ; static final String TERMS_INDEX_CODEC_NAME = "BLOCK_TREE_TERMS_INDEX" ; public static final int TERMS_INDEX_VERSION_START = 0 ; public static final int TERMS_INDEX_VERSION_CURRENT = BlockTreeTermsWriter . TERMS_INDEX_VERSION_START ; private final IndexOutput out ; private final IndexOutput indexOut ; final int minItemsInBlock ; final int maxItemsInBlock ; final PostingsWriterBase postingsWriter ; final FieldInfos fieldInfos ; FieldInfo currentField ; private final List < BlockTreeTermsWriter . TermsWriter > fields = new ArrayList < BlockTreeTermsWriter . TermsWriter > ( ) ; public BlockTreeTermsWriter ( SegmentWriteState state , PostingsWriterBase postingsWriter , int minItemsInBlock , int maxItemsInBlock ) throws IOException { } protected void writeHeader ( IndexOutput out ) throws IOException { } protected void writeIndexHeader ( IndexOutput out ) throws IOException { } protected void writeTrailer ( IndexOutput out , long dirStart ) throws IOException { } protected void writeIndexTrailer ( IndexOutput indexOut , long dirStart ) throws IOException { } @ Override public TermsConsumer addField ( FieldInfo field ) throws IOException { } static long encodeOutput ( long fp , boolean hasTerms , boolean isFloor ) { } private static class PendingEntry { public final boolean isTerm ; protected PendingEntry ( boolean isTerm ) { } } private static final class PendingTerm extends BlockTreeTermsWriter . PendingEntry { public final BytesRef term ; public final TermStats stats ; public PendingTerm ( BytesRef term , TermStats stats ) { } @ Override public String toString ( ) { } } private static final class PendingBlock extends BlockTreeTermsWriter . PendingEntry { public final BytesRef prefix ; public final long fp ; public FST < BytesRef > index ; public List < FST < BytesRef > > subIndices ; public final boolean hasTerms ; public final boolean isFloor ; public final int floorLeadByte ; private final IntsRef scratchIntsRef = new IntsRef ( ) ; public PendingBlock ( BytesRef prefix , long fp , boolean hasTerms , boolean isFloor , int floorLeadByte , List < FST < BytesRef > > subIndices ) { } @ Override public String toString ( ) { } public void compileIndex ( List < BlockTreeTermsWriter . PendingBlock > floorBlocks , RAMOutputStream scratchBytes ) throws IOException { } private void append ( Builder < BytesRef > builder , FST < BytesRef > subIndex ) throws IOException { } } final RAMOutputStream scratchBytes = new RAMOutputStream ( ) ; class TermsWriter extends TermsConsumer { private final FieldInfo fieldInfo ; private long numTerms ; long sumTotalTermFreq ; long sumDocFreq ; int docCount ; long indexStartFP ; private final NoOutputs noOutputs ; private final Builder < Object > blockBuilder ; private final List < BlockTreeTermsWriter . PendingEntry > pending = new ArrayList < BlockTreeTermsWriter . PendingEntry > ( ) ; private int lastBlockIndex = - 1 ; private int [ ] subBytes = new int [ 10 ] ; private int [ ] subTermCounts = new int [ 10 ] ; private int [ ] subTermCountSums = new int [ 10 ] ; private int [ ] subSubCounts = new int [ 10 ] ; private class FindBlocks extends Builder . FreezeTail < Object > { @ Override public void freeze ( final Builder < Object > [ ] frontier , int prefixLenPlus1 , final IntsRef lastInput ) throws IOException { } } void writeBlocks ( IntsRef prevTerm , int prefixLength , int count ) throws IOException { } @ SuppressWarnings ( "unused" ) private String toString ( BytesRef b ) { } private BlockTreeTermsWriter . PendingBlock writeBlock ( IntsRef prevTerm , int prefixLength , int indexPrefixLength , int startBackwards , int length , int futureTermCount , boolean isFloor , int floorLeadByte , boolean isLastInFloor ) throws IOException { } TermsWriter ( FieldInfo fieldInfo ) { } @ Override public Comparator < BytesRef > getComparator ( ) { } @ Override public PostingsConsumer startTerm ( BytesRef text ) throws IOException { } private final IntsRef scratchIntsRef = new IntsRef ( ) ; @ Override public void finishTerm ( BytesRef text , TermStats stats ) throws IOException { } @ Override public void finish ( long sumTotalTermFreq , long sumDocFreq , int docCount ) throws IOException { if ( ( numTerms ) > 0 ) { blockBuilder . finish ( ) ; assert ( ( pending . size ( ) ) == 1 ) && ( ! ( pending . get ( 0 ) . isTerm ) ) : ( ( "pending.size()=" + ( pending . size ( ) ) ) + "<seq2seq4repair_space>pending=" ) + ( pending ) ; final BlockTreeTermsWriter . PendingBlock root = ( ( BlockTreeTermsWriter . PendingBlock ) ( pending . get ( 0 ) ) ) ; assert ( root . prefix . length ) == 0 ; assert ( root . index . getEmptyOutput ( ) ) != null ; this . sumTotalTermFreq = sumTotalTermFreq ; this . sumDocFreq = sumDocFreq ; this . docCount = docCount ; indexStartFP = indexOut . getFilePointer ( ) ; root . index . save ( indexOut ) ; } else { <START_BUG> assert sumTotalTermFreq == 0 ; <END_BUG> assert sumDocFreq == 0 ; assert docCount == 0 ; } } private final RAMOutputStream bytesWriter = new RAMOutputStream ( ) ; private final RAMOutputStream bytesWriter2 = new RAMOutputStream ( ) ; } @ Override public void close ( ) throws IOException { } }

Fix:
assert ( sumTotalTermFreq == 0 ) || ( ( ( fieldInfo . getIndexOptions ( ) ) == ( IndexOptions . DOCS_ONLY ) ) && ( sumTotalTermFreq == ( - 1 ) ) ) ;