feat: add streaming loop detection with sentence sampling - #421
Merged
Conversation
Add a streaming loop detection mechanism that samples sentences from the active message as it streams in, detects repetition via a sliding window, and issues a silent 'You're looping.' nudge to break the cycle. New files: - src/agent/sentence-detector.js: Sentence boundary detection component - src/agent/sliding-window-tracker.js: Sliding window sentence tracking - src/agent/loop-detector.js: Loop detection pipeline combining detector + tracker Modified files: - src/agent/react.js: Integrate loop detector into callReactAgentStreaming() - src/tui/app.js: Handle loop_detected events as silent nudge Tests: - tests/unit/agent/sentence-detector.test.js - tests/unit/agent/sliding-window-tracker.test.js - tests/unit/agent/loop-detector.test.js - tests/integration/streaming-loop-detection.test.js
Owner
Author
Audit Results: Streaming Loop DetectionCoverage Audit
Fidelity AuditAll specs faithfully represent the original intent of each goal. Key elements preserved:
Completeness AuditAll key requirements and acceptance criteria are captured in specs with scenarios. Minor notes:
Consistency AuditTasks map correctly to spec requirements:
VerdictNo errors found. Specs are comprehensive, faithful to goals, and tasks map correctly. Proceeding to Step 6. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a streaming loop detection mechanism that samples sentences from the active message as it streams in, detects repetition via a sliding window, and issues a silent "You're looping." nudge to break the cycle.
Motivation
The agent can get stuck in repetitive loops — re-checking status, re-running the same commands, or repeating the same phrasing across multiple responses. This wastes time, creates noise, and can lead to catastrophic decisions. A lightweight, streaming-aware detection mechanism catches loops early and gives the agent a chance to self-correct.
Implementation
New Components
Sentence Detector (
src/agent/sentence-detector.js).,!,?) are detected...,?!, abbreviations)Sliding Window Tracker (
src/agent/sliding-window-tracker.js)Loop Detector (
src/agent/loop-detector.js){ type: 'loop_detected' }callback eventIntegration Points
Streaming Pipeline (
src/agent/react.js)callReactAgentStreaming()TUI Handling (
src/tui/app.js)loop_detectedeventsTesting
Verification
Files Changed
src/agent/sentence-detector.js— New sentence boundary detection componentsrc/agent/sliding-window-tracker.js— New sliding window trackersrc/agent/loop-detector.js— New loop detection pipelinesrc/agent/react.js— Integrated loop detector into streaming pipelinesrc/tui/app.js— Added loop_detected event handlertests/unit/agent/sentence-detector.test.js— Unit teststests/unit/agent/sliding-window-tracker.test.js— Unit teststests/unit/agent/loop-detector.test.js— Unit teststests/integration/streaming-loop-detection.test.js— Integration testsopenspec/changes/streaming-loop-detection/— OpenSpec artifacts