Buggy code:
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 { assert length > 0 ; final int start = ( pending . size ( ) ) - startBackwards ; assert start >= 0 : ( ( ( ( "pending.size()=" + ( pending . size ( ) ) ) + "<seq2seq4repair_space>startBackwards=" ) + startBackwards ) + "<seq2seq4repair_space>length=" ) + length ; final List < BlockTreeTermsWriter . PendingEntry > slice = pending . subList ( start , ( start + length ) ) ; final long startFP = out . getFilePointer ( ) ; final BytesRef prefix = new BytesRef ( indexPrefixLength ) ; for ( int m = 0 ; m < indexPrefixLength ; m ++ ) { prefix . bytes [ m ] = ( ( byte ) ( prevTerm . ints [ m ] ) ) ; } prefix . length = indexPrefixLength ; out . writeVInt ( ( ( length << 1 ) | ( isLastInFloor ? 1 : 0 ) ) ) ; final boolean isLeafBlock ; if ( ( lastBlockIndex ) < start ) { isLeafBlock = true ; } else if ( ! isFloor ) { isLeafBlock = false ; } else { boolean v = true ; for ( BlockTreeTermsWriter . PendingEntry ent : slice ) { if ( ! ( ent . isTerm ) ) { v = false ; break ; } } isLeafBlock = v ; } final List < FST < BytesRef > > subIndices ; int termCount ; if ( isLeafBlock ) { subIndices = null ; for ( BlockTreeTermsWriter . PendingEntry ent : slice ) { assert ent . isTerm ; BlockTreeTermsWriter . PendingTerm term = ( ( BlockTreeTermsWriter . PendingTerm ) ( ent ) ) ; final int suffix = ( term . term . length ) - prefixLength ; bytesWriter . writeVInt ( suffix ) ; bytesWriter . writeBytes ( term . term . bytes , prefixLength , suffix ) ; bytesWriter2 . writeVInt ( term . stats . docFreq ) ; if ( ( fieldInfo . getIndexOptions ( ) ) != ( IndexOptions . DOCS_ONLY ) ) { <START_BUG> assert ( term . stats . totalTermFreq ) >= ( term . stats . docFreq ) ; <END_BUG> bytesWriter2 . writeVLong ( ( ( term . stats . totalTermFreq ) - ( term . stats . docFreq ) ) ) ; } } termCount = length ; } else { subIndices = new ArrayList < FST < BytesRef > > ( ) ; termCount = 0 ; for ( BlockTreeTermsWriter . PendingEntry ent : slice ) { if ( ent . isTerm ) { BlockTreeTermsWriter . PendingTerm term = ( ( BlockTreeTermsWriter . PendingTerm ) ( ent ) ) ; final int suffix = ( term . term . length ) - prefixLength ; bytesWriter . writeVInt ( ( suffix << 1 ) ) ; bytesWriter . writeBytes ( term . term . bytes , prefixLength , suffix ) ; bytesWriter2 . writeVInt ( term . stats . docFreq ) ; if ( ( fieldInfo . getIndexOptions ( ) ) != ( IndexOptions . DOCS_ONLY ) ) { assert ( term . stats . totalTermFreq ) >= ( term . stats . docFreq ) ; bytesWriter2 . writeVLong ( ( ( term . stats . totalTermFreq ) - ( term . stats . docFreq ) ) ) ; } termCount ++ ; } else { BlockTreeTermsWriter . PendingBlock block = ( ( BlockTreeTermsWriter . PendingBlock ) ( ent ) ) ; final int suffix = ( block . prefix . length ) - prefixLength ; assert suffix > 0 ; bytesWriter . writeVInt ( ( ( suffix << 1 ) | 1 ) ) ; bytesWriter . writeBytes ( block . prefix . bytes , prefixLength , suffix ) ; assert ( block . fp ) < startFP ; bytesWriter . writeVLong ( ( startFP - ( block . fp ) ) ) ; subIndices . add ( block . index ) ; } } assert ( subIndices . size ( ) ) != 0 ; }

Fix:
assert ( term . stats . totalTermFreq ) >= ( term . stats . docFreq ) : ( ( term . stats . totalTermFreq ) + "<seq2seq4repair_space>vs<seq2seq4repair_space>" ) + ( term . stats . docFreq ) ;