siqyin's profile picture
siqyin

repetition-guard

Public

18 Downloads

1 star

README

repetition-guard

English | 中文


中文

LM Studio 实时重复输出守卫:本地模型正文 / 思考流一开始"复读",立刻自动截停,已生成内容完整保留。

解决什么问题

量化模型(尤其是小模型)长生成时容易陷入循环:wait wait wait wait …、思考里反复念同一句话、工具调用参数刷同一段 JSON——白烧 token 还占着上下文。目前 LM Studio 没有内置的"检测到循环就停"机制,本插件补上这一环。

工作原理

插件注册一个 Guarded Generator,包裹本地已加载的模型,逐片段转发输出的同时做两层检测:

  • 连续单元重复:尾部出现同一单元(1–24 字符)连续重复 ≥ 阈值(默认 10)次。覆盖单字符循环、词语循环、短语循环,忽略空白差异。
  • 窗口吟唱:同一 32 字符窗口在尾部反复出现 ≥ 阈值(默认 4)次。覆盖超过单元长度的整句循环。

检测同时作用于三路流:正文、思考(reasoning)、工具调用参数,可分别开关。

触发后:底层预测立即 abort(服务端停止生成)、已输出片段保留、本轮正常收尾(等同用户点停止),并在末尾附一行提示(可关闭)。不删除任何内容。

防误杀

  • Markdown 表格分隔行、水平线(- | 默认忽略字符)不会误判。
  • 纯装饰/空白片段直接跳过。
  • 围栏代码块内阈值自动 ×3(可调),代码里合法的重复(分隔线、ASCII 表格、重复数据)不受影响。

配置项

项默认说明
Model Key(空=当前模型)要守护的本地模型
Temperature0.8透传给模型
Repeat Threshold10连续重复触发阈值,越小截停越早
Chant Threshold4整句循环触发阈值
Guard Thinking Stream开检测思考流
Guard Answer Stream开检测正文流
Notify On Stop开截停时附提示行

使用

npm install
npm run dev     # 开发模式(热重载)
npm run push    # 发布到 LM Studio Hub

在聊天的模型选择器里选本插件提供的 guarded 生成器即可生效。


English

Real-time repetition guard for LM Studio: the moment a local model starts looping on repeated text — in the answer, in the thinking stream, or in tool-call arguments — generation is stopped automatically. Everything generated before the loop is kept intact.

Problem

Quantized local models (especially small ones) can degenerate into wait wait wait wait … loops mid-generation, chant the same sentence in reasoning, or spam identical tool-call JSON — burning tokens and polluting the context. LM Studio ships no built-in loop breaker; this plugin adds one.

How it works

The plugin registers a Guarded Generator that wraps a locally loaded model. Every fragment is forwarded to the UI and inspected by two detectors:

  • Consecutive-unit repetition: the tail ends with the same unit (1–24 chars) repeated ≥ threshold (default 10) times — catches char loops, word loops and phrase loops, whitespace-insensitive.
  • Window chanting: the same 32-char window recurs ≥ threshold (default 4) times — catches long sentence-level loops.

All three streams are guarded independently: answer text, thinking/reasoning, and tool-call arguments, each toggleable.

On trigger: the underlying prediction is aborted immediately (the server stops generating), emitted fragments are preserved, the turn finishes cleanly (exactly like a user stop), and an optional one-line note is appended. Nothing is deleted.

False-positive protection

  • Markdown table separators / horizontal rules (- | are ignored chars) never trigger.
  • Purely decorative or whitespace fragments are skipped.
  • Threshold auto-scales ×3 inside fenced code blocks (configurable) — legitimate repetition in code (separators, ASCII tables, generated data) is unaffected.

Configuration

SettingDefaultMeaning
Model Key(empty = current)Local model to guard
Temperature0.8Passed through to the model
Repeat Threshold10Consecutive-repeat trigger; lower cuts earlier
Chant Threshold4Sentence-loop trigger
Guard Thinking StreamonDetect loops in reasoning
Guard Answer StreamonDetect loops in the answer
Notify On StoponAppend a note when cut off

Usage

npm install
npm run dev     # development mode (hot reload)
npm run push    # publish to LM Studio Hub

Pick the guarded generator provided by this plugin in the chat model selector.

Credits

Detection strategy inspired by dsh-repetition-guard / dsh-dupguard (DeepSeek Harness) and Qwen Code's LoopDetectionService.

License

MIT