Metadata-Version: 2.4
Name: mimo-stable
Version: 1.1.4
Summary: Record, detect, and apply engineering guardrails for degenerate loops in LLM output and tool calls
Author: xli498
License: MIT License
        
        Copyright (c) 2026 xli498
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/xli498/mimo-stable
Project-URL: Repository, https://github.com/xli498/mimo-stable
Project-URL: Changelog, https://github.com/xli498/mimo-stable/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/xli498/mimo-stable/issues
Keywords: llm,guardrails,loop-detection,observability,agent
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Quality Assurance
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# LLM Degenerate Loop Guardrails

[![Quality Gate](https://github.com/xli498/mimo-stable/actions/workflows/quality.yml/badge.svg)](https://github.com/xli498/mimo-stable/actions/workflows/quality.yml)
[![Release](https://img.shields.io/github/v/release/xli498/mimo-stable)](https://github.com/xli498/mimo-stable/releases)
[![License](https://img.shields.io/github/license/xli498/mimo-stable)](LICENSE)
[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](pyproject.toml)

[中文说明](README.md)

A zero-runtime-dependency toolkit for detecting degenerate loops in LLM output
and tool calls, producing conservative engineering decisions, and preserving
reproducible evidence.

> [!IMPORTANT]
> This project detects loop signals and emits recovery decisions. It does not
> claim to repair a model or execute retries, model switches, tool calls, or
> process control. The upper-layer runner owns those side effects.

## 30-second start

![Guardrails architecture](docs/architecture.svg)

Run the included synthetic fixture:

```bash
python3 scripts/detect_loop.py \
  --json \
  --timeout 60 \
  --log fixtures/loop_detected.log
```

The command exits with status `1` when a loop signal is detected. A typical
summary looks like this:

```json
{
  "loop_detected": true,
  "details": {
    "type": "consecutive_identical_output"
  }
}
```

| Exit code | Meaning |
| :---: | :--- |
| `0` | No loop detected |
| `1` | Loop detected |
| `2` | Invalid input or arguments |

## What it detects

The detector is designed for observable signals such as:

- consecutive identical or near-identical output blocks;
- repeated tool calls, including normalized JSON key order;
- suspicious non-consecutive tool-call patterns;
- language drift and related output anomalies when evidence supports them.

Detection results are signals, not model-level explanations. MiMo is the initial
case study; observations from MiMo, GLM, and other models must be recorded
independently and must not be treated as universal conclusions.

## Recovery decisions

Pipe a detector summary into the conservative policy layer:

```bash
python3 scripts/detect_loop.py \
  --json \
  --timeout 60 \
  --log fixtures/loop_detected.log \
  > /tmp/loop-summary.json

python3 scripts/recovery_policy.py \
  --summary /tmp/loop-summary.json \
  --retryable \
  --retry-count 0
```

The policy emits a decision only. The upper-layer runner decides whether to
pause, retry, switch models, or request human review under its own safety and
idempotency rules.

## Installation

The project has no runtime dependencies and currently supports source execution
and local CLI installation.

### Run from source

```bash
git clone https://github.com/xli498/mimo-stable.git
cd mimo-stable
python3 scripts/detect_loop.py --log fixtures/loop_detected.log
```

### Install the CLI from PyPI

```bash
python3 -m pip install mimo-stable
mimo-loop-detect --json --timeout 60 --log fixtures/loop_detected.log
```

> [!NOTE]
> `mimo-stable` provides the `mimo-loop-detect` command-line tool. It does not
> currently provide a Python import API named `mimo_stable`.

## Examples

- [Basic text detection](examples/basic-text-detection.md)
- [Tool-call detection](examples/tool-call-detection.md)
- [Recovery policy integration](examples/recovery-policy-integration.md)

Each example shows inputs, signals, or decisions only. Actual stopping,
retrying, model switching, tool execution, and human review remain the
responsibility of the upper-layer runner.

## Repository layout

```text
scripts/detect_loop.py       # Signal detection
scripts/recovery_policy.py   # Conservative decision output
fixtures/                    # Synthetic regression fixtures
examples/                    # Minimal integration examples
references/                  # Evidence and parameter notes
tests/                       # Behavioral tests
```

## Testing

```bash
python3 scripts/check_version.py
python3 tests/test_detector.py
python3 scripts/benchmark_fixtures.py
bash scripts/test_short.sh
bash scripts/test_long.sh
```

CI tests Python 3.10, 3.11, and 3.12.

## Evidence and boundaries

- Fixtures are synthetic or fully redacted.
- Do not commit API keys, tokens, private prompts, user data, private endpoints,
  or raw production payloads.
- Historical model parameters are case-study evidence, not cross-model defaults.
- The detector and policy layer have no implicit side effects.

## License

MIT. See [LICENSE](LICENSE).
