Skip to main content

Module thinking_loop

Module thinking_loop 

Source
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:

  1. Verbatim tail repetition — a short unit repeated back-to-back at the tail of the rolling window (e.g. 🌊 🌊 🌊 …).
  2. Near-duplicate segment cluster — paragraphs whose word-trigram fingerprints overlap above a Jaccard threshold (cosmetic rewording of the same paragraph).
  3. Progress-lexicon stall — paragraphs that recycle the recent vocabulary (low novelty) and introduce no new concrete reference (no new code span / path / identifier).

Structs§

ThinkingLoopDetector
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.