foldRepeats

private fun foldRepeats(lines: List<String>): List<String>

Collapse consecutive repeats of a block of lines into one copy plus a count.

Period-aware, which is the whole difficulty: folding only ADJACENT EQUAL lines catches direct recursion and nothing else, since mutual recursion alternates two frames and never puts equal ones side by side.

The winner at each position is the period covering the most frames, ties to the shortest — so a a b twice folds as one 3-line block instead of a x2 plus an unfoldable remainder. It must also replace more than period + 1 lines, or the marker costs what it saves; that subsumes "repeats at least twice".

Lines, not StackTraceElements: what repeats for the reader is the rendered text, which also keeps the fold independent of the mapping.