Expand description
Thinking-loop detector — ported from omp
packages/ai/src/utils/thinking-loop.ts.
MIT — attribution: adapted from omp (Can Berk Güder, earendil-works).
§Purpose
Reasoning models (Gemini, DeepSeek-R1, …) sometimes enter a degenerate loop in their thinking stream: the same paragraph reshuffled over and over, a short token repeated back-to-back, or filler that recycles the recent vocabulary without ever naming a new concrete reference. The model bills tokens indefinitely; the user never sees an answer.
This module provides ThinkingLoopDetector — a stateful detector
fed streamed thinking deltas. It recognises three loop shapes:
- Verbatim tail repetition — a short unit repeated back-to-back
at the tail of the rolling window (e.g.
🌊 🌊 🌊 …). - Near-duplicate segment cluster — paragraphs whose word-trigram fingerprints overlap above a Jaccard threshold (cosmetic rewording of the same paragraph).
- Progress-lexicon stall — paragraphs that recycle the recent vocabulary (low novelty) and introduce no new concrete reference (no new code span / path / identifier).
Structs§
- Thinking
Loop Detector - Stateful detector fed streamed thinking deltas.
Constants§
- THINKING_
LOOP_ MARKER - Stable lead phrase of the detector’s reason strings. Used by upstream retry classifiers to recognise the failure as a transient stream stall.