repetition_guard.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. """Cheap content-sanity checks for model output, completed or still streaming.
  2. A model in a degenerate repetition loop can spend its ENTIRE output budget echoing one fragment;
  3. the ``finish_reason=length`` continuation would then stitch it into the final response with a
  4. "continue" nudge (one incident: a 60k-char turn delivered as 31 Discord messages). This detects
  5. repetition-dominated fragments BEFORE the nudge so the turn aborts with a clear error. Deliberately
  6. conservative: only LONG verbatim repeats (60+ chars) covering a majority of the fragment trip it.
  7. """
  8. from __future__ import annotations
  9. import math
  10. from collections import Counter
  11. # Below this length the check doesn't run: short truncations trivially
  12. # contain repeated tokens and are legitimately continued.
  13. MIN_FRAGMENT_LENGTH = 400
  14. # Exact-repeat window; far beyond ordinary phrasing reuse (citations, headings, similar code).
  15. _REPEAT_WINDOW = 60
  16. # A window repeating at least this often is a signal even for short fragments.
  17. _MIN_REPEAT_COUNT = 5
  18. # "Repetition-dominated" = repeated windows cover at least this fraction.
  19. _DOMINANCE_RATIO = 0.5
  20. # What an interrupt checkpoint says INSTEAD of a repetition-dominated partial. Replaying the
  21. # looped bytes (as the redirect's api_content or as the interrupted assistant row) re-seeds the
  22. # loop on the next request and the corruption survives restarts (#112764); the model only needs
  23. # to know the reply degenerated and was cut off.
  24. REPETITION_LOOP_INTERRUPTED = "[the reply degenerated into a repetition loop and was interrupted]"
  25. # Sampling bounds keep the general path linear in output size with a small,
  26. # fixed multiplier. A dominant contiguous run necessarily crosses many of
  27. # these evenly spaced anchors.
  28. _MAX_ANCHOR_SAMPLES = 32
  29. _MAX_ANCHOR_MATCHES = 8
  30. # ``is_runaway_repetition``: a multi-line partial must be mostly copies of a few lines. Batch-style
  31. # output (distinct INSERT rows, similar table rows) shares long prefixes and trips the window
  32. # scan, but every line is distinct; a loop re-emits the same line(s).
  33. _RUNAWAY_DISTINCT_LINE_RATIO = 0.5
  34. # The finish_reason="stop" path discards a COMPLETED answer, so it only aborts at runaway scale:
  35. # real stop-path loops (#100716) run 80k-350k chars, while asked-for repeats ("say X 50 times",
  36. # identical table rows, templated YAML) stay in the low KB and must be delivered.
  37. STOP_PATH_MIN_CHARS = 16_000
  38. # A live stream is judged on at most this much of its latest text, so one check stays bounded
  39. # however long the reply grows.
  40. _STREAM_TAIL_CHARS = 4 * STOP_PATH_MIN_CHARS
  41. def is_repetition_dominated(text: str) -> bool:
  42. """True when a contiguous run of at least five exact repetitions covers at least half
  43. of ``text`` — the signature of a model repetition loop (issue #86581). Coverage is measured
  44. with the true period, so long multi-line loop units count fully. Fail-open for short input.
  45. """
  46. if not isinstance(text, str):
  47. return False
  48. n = len(text)
  49. if n < MIN_FRAGMENT_LENGTH:
  50. return False
  51. # Fast path: one normalized line duplicated enough to cover half the fragment (the common echo shape).
  52. if _line_repetition_dominated(text, n):
  53. return True
  54. # Window-count scan (#86581) catches loops whose repeats differ by a counter or noise token;
  55. # the periodic scan catches long exact units whose 60-char windows each recur too rarely.
  56. return _window_count_dominated(text, n) or _periodic_run_dominated(text, n)
  57. def _window_count_dominated(text: str, n: int) -> bool:
  58. """True when one 60-char window recurs often enough to cover half of ``text``."""
  59. window = _REPEAT_WINDOW
  60. needed = max(_MIN_REPEAT_COUNT, math.ceil(n * _DOMINANCE_RATIO / window))
  61. counts: dict[str, int] = {}
  62. for i in range(n - window + 1):
  63. key = text[i : i + window]
  64. c = counts.get(key, 0) + 1
  65. if c >= needed:
  66. return True
  67. counts[key] = c
  68. return False
  69. def _periodic_run_dominated(text: str, n: int) -> bool:
  70. """Detect a dominant exact periodic run from evenly spaced anchors.
  71. Matching a 60-character anchor at a later position supplies a candidate
  72. period. Expanding the equality ``text[i] == text[i + period]`` in both
  73. directions recovers the full run, so coverage is measured using the true
  74. repeating unit rather than crediting every occurrence with only 60 chars.
  75. """
  76. window = _REPEAT_WINDOW
  77. max_start = n - window
  78. if max_start < 1:
  79. return False
  80. sample_step = max(
  81. 1,
  82. (max_start + _MAX_ANCHOR_SAMPLES - 2) // (_MAX_ANCHOR_SAMPLES - 1),
  83. )
  84. sample_starts = list(range(0, max_start + 1, sample_step))
  85. if sample_starts[-1] != max_start:
  86. sample_starts.append(max_start)
  87. # Runs already expanded and rejected, as (left, right, period). A later anchor inside one of
  88. # them whose period is a multiple of that run's period would re-walk the same run.
  89. rejected: list[tuple[int, int, int]] = []
  90. for start in sample_starts:
  91. anchor = text[start : start + window]
  92. search_from = start + 1
  93. for _ in range(_MAX_ANCHOR_MATCHES):
  94. match = text.find(anchor, search_from)
  95. if match < 0:
  96. break
  97. period = match - start
  98. search_from = match + 1
  99. if any(lo <= start < hi and period % p == 0 for lo, hi, p in rejected):
  100. continue
  101. left, right = _expand_run(text, n, start, period, window)
  102. if right - left >= _MIN_REPEAT_COUNT * period and right - left >= n * _DOMINANCE_RATIO:
  103. return True
  104. rejected.append((left, right, period))
  105. return False
  106. def _expand_run(text: str, n: int, start: int, period: int, matched: int) -> tuple[int, int]:
  107. """Expand one known equal window to the ``[left, right)`` bounds of its exact periodic run."""
  108. left = start
  109. while left > 0 and text[left - 1] == text[left - 1 + period]:
  110. left -= 1
  111. right = start + matched
  112. while right + period < n and text[right] == text[right + period]:
  113. right += 1
  114. return left, right + period
  115. def is_runaway_repetition(text: str) -> bool:
  116. """Stricter than :func:`is_repetition_dominated`: also require the runaway shape.
  117. An interrupt checkpoint DROPS the partial when this fires, so a legitimately repetitive but
  118. correct reply (distinct batch rows) must not qualify: repeated windows have to dominate AND,
  119. when the text has line structure, at most half of its non-empty lines may be distinct.
  120. """
  121. if not is_repetition_dominated(text):
  122. return False
  123. lines = [line.strip() for line in text.splitlines()]
  124. lines = [line for line in lines if line]
  125. if len(lines) < _MIN_REPEAT_COUNT:
  126. return True # no line structure to judge by: a dominated single-line loop
  127. return len(set(lines)) <= len(lines) * _RUNAWAY_DISTINCT_LINE_RATIO
  128. def _line_repetition_dominated(text: str, n: int) -> bool:
  129. """True when a single normalized line covers half the fragment via repeats."""
  130. counts = Counter(norm for norm in (line.strip() for line in text.splitlines()) if norm)
  131. return any(c >= _MIN_REPEAT_COUNT and c * len(line) >= n * _DOMINANCE_RATIO for line, c in counts.items())
  132. class RunawayStreamWatch:
  133. """The stop-path criterion applied WHILE one channel (visible text or reasoning) streams.
  134. A stream that keeps producing never reaches a completion check, and endpoints without an
  135. output cap (#127234) keep a looping model going for as long as the turn lives. The first
  136. check runs at ``STOP_PATH_MIN_CHARS`` (asked-for repeats stay below it, as on the stop path)
  137. and the gap doubles until it reaches one tail window, then stays there. A fixed stride keeps a
  138. loop that starts late in a long reply from streaming as long as the reply already was before
  139. a check sees it; each check reads only the tail, so the total work stays linear in the output.
  140. Each check also trims the held text to the tail, and checks are at most one tail window apart,
  141. so the watch never holds more than two tail windows plus the latest delta.
  142. """
  143. __slots__ = ("_chars", "_next_check", "_parts")
  144. def __init__(self) -> None:
  145. self._parts: list[str] = []
  146. self._chars = 0
  147. self._next_check = STOP_PATH_MIN_CHARS
  148. def feed(self, text: str) -> bool:
  149. """Add one delta; True once the channel has become a runaway loop."""
  150. if not text:
  151. return False
  152. self._parts.append(text)
  153. self._chars += len(text)
  154. if self._chars < self._next_check:
  155. return False
  156. self._next_check = self._chars + min(self._chars, _STREAM_TAIL_CHARS)
  157. tail = "".join(self._parts)[-_STREAM_TAIL_CHARS:]
  158. self._parts = [tail]
  159. return is_runaway_repetition(tail)