Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9bac12f
fix: detect reasoning loops by hashing stream segments
avoidwork Nov 17, 2026
979c4c1
fix: hash app stream segments in loop detector
avoidwork Nov 17, 2026
3c1091a
test: replay captured reasoning loop through the loop detector
avoidwork Nov 17, 2026
92316f9
fix: hash streamed deltas to detect looping segments
avoidwork Nov 17, 2026
347e7e1
fix: hash streamed deltas to detect looping segments
avoidwork Nov 17, 2026
55b4a83
fix: send loop nudge from the TUI like the silent Continue
avoidwork Nov 18, 2026
8668c79
fix: read loop nudge limit once from config
avoidwork Nov 18, 2026
c06c05a
fix: read autoContinueLimit once from config
avoidwork Nov 18, 2026
c5c9897
fix: restore loop detection escalation and ttl config
avoidwork Nov 18, 2026
9f6906f
fix: make the loop nudge visible
avoidwork Nov 18, 2026
232e833
fix: detect loops from accumulated text in a worker thread
avoidwork Nov 18, 2026
db37a03
fix: make the loop nudge silent again
avoidwork Nov 18, 2026
b5058ee
perf: split each streamed delta once and keep a bounded window
avoidwork Nov 18, 2026
5603328
feat: make the word window configurable and large enough
avoidwork Nov 18, 2026
d47bf59
fix: restrict loop detection to a single stream block id
avoidwork Nov 18, 2026
1000e6f
refactor: rename maxBlocks to maxStreams and add it to config
avoidwork Nov 18, 2026
fe9e4f5
fix: release detector state when a turn ends and set maxStreams to 4
avoidwork Nov 18, 2026
3622662
fix: release detector state at the end of each message turn
avoidwork Nov 18, 2026
083e5b5
fix: compact on the third loop strike, not after three nudges
avoidwork Nov 18, 2026
6f50ec3
feat: send a silent Continue after a loop-triggered compaction
avoidwork Nov 18, 2026
2a416f6
merge: bring main into fix/stream-loop-detection
avoidwork Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ loopDetection:
enabled: true
escalationCount: 3
escalationWindowMs: 60000
maxStreams: 4
threshold: 2
ttlMs: 60000
windowSize: 10
windowSize: 20
wordWindowSize: 1000
memory:
checkpointsDir: memory/checkpoints/
contextDir: memory/context/
Expand Down
16 changes: 9 additions & 7 deletions coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ℹ compaction.js | 98.84 | 78.43 | 100.00 | 89-90
ℹ contextBackend.js | 100.00 | 100.00 | 100.00 |
ℹ coreBackend.js | 100.00 | 100.00 | 100.00 |
ℹ deepAgents.js | 94.93 | 67.65 | 60.87 | 157-158 182 198-200 310-317 346-347 352-353 460-462 512 548-550 597-598 608-610 617-618
ℹ deepAgents.js | 93.72 | 69.39 | 66.67 | 156-157 181 197-199 309-316 345-346 459-461 488-500 511 579-580 590-592
ℹ evictionCeiling.js | 100.00 | 100.00 | 100.00 |
ℹ skillsMiddleware.js | 100.00 | 87.50 | 100.00 |
ℹ config | | | |
Expand Down Expand Up @@ -60,9 +60,8 @@
ℹ copilotAuth.js | 96.80 | 78.87 | 90.00 | 12-13 131-132 196-197 237-239 352-353 378-379 391-393 484 565-566 575-576
ℹ copilotRetryMiddleware.js | 88.52 | 79.49 | 83.33 | 75-88
ℹ index.js | 100.00 | 100.00 | 100.00 |
ℹ loopDetectionMiddleware.js | 99.59 | 83.08 | 100.00 | 32
ℹ modelInfo.js | 98.17 | 96.39 | 100.00 | 128-129 179-180
ℹ openai.js | 91.60 | 73.61 | 78.57 | 65 90-92 138-139 145-146 181-184 233 236-238 257-259 267-271 279-280 292-295
ℹ modelInfo.js | 97.25 | 93.98 | 100.00 | 128-129 131-132 179-180
ℹ openai.js | 92.72 | 75.71 | 78.57 | 65 90-92 138-139 145-146 233 236-238 257-259 267-271 279-280 292-295
ℹ summarizationMiddleware.js | 97.01 | 94.00 | 83.33 | 107 126-128 149
ℹ tokenBudget.js | 97.14 | 85.71 | 82.35 | 50-51 83-84
ℹ tokenBudgetMiddleware.js | 98.10 | 78.67 | 87.50 | 94-95 97-98
Expand Down Expand Up @@ -96,8 +95,11 @@
ℹ types.js | 100.00 | 100.00 | 100.00 |
ℹ validator.js | 100.00 | 100.00 | 100.00 |
ℹ stream | | | |
ℹ consume.js | 85.51 | 74.29 | 100.00 | 52-58 88-93 104-110
ℹ consume.js | 96.48 | 98.36 | 100.00 | 156-162
ℹ interrupt.js | 100.00 | 100.00 | 100.00 |
ℹ loopCheck.js | 100.00 | 100.00 | 100.00 |
ℹ loopDetector.js | 100.00 | 100.00 | 100.00 |
ℹ loopDetectorWorker.js | 100.00 | 100.00 | 100.00 |
ℹ namespace.js | 100.00 | 100.00 | 100.00 |
ℹ transformers | | | |
ℹ index.js | 100.00 | 100.00 | 100.00 |
Expand Down Expand Up @@ -185,7 +187,7 @@
ℹ xlsxParser.js | 82.21 | 83.33 | 88.89 | 47-48 93-112 130-137 150-156
ℹ zipExtractor.js | 82.32 | 77.78 | 66.67 | 62-63 104-107 124-142 149-152
ℹ git | | | |
ℹ index.js | 87.41 | 64.13 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458
ℹ index.js | 87.41 | 64.37 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458
ℹ graphql | | | |
ℹ index.js | 97.85 | 98.13 | 93.75 | 57-68
ℹ image | | | |
Expand Down Expand Up @@ -284,6 +286,6 @@
ℹ loadAgents.js | 74.29 | 78.57 | 50.00 | 25-26 41 56-70
ℹ project.js | 100.00 | 100.00 | 100.00 |
ℹ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ℹ all files | 93.34 | 86.41 | 88.20 |
ℹ all files | 93.39 | 86.74 | 88.37 |
ℹ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ℹ end of coverage report
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
import { resumeSession } from "./src/session/resume.js";
import { logger, flush as flushLogger } from "./src/shared/logger.js";
import { consumeStream } from "./src/stream/consume.js";
import {
createSegmentLoopDetector,
destroyPool as destroyLoopPool,
} from "./src/stream/loopDetector.js";

// ---------------------------------------------------------------------------
// CLI arguments
Expand Down Expand Up @@ -296,6 +300,17 @@ async function doBootstrap() {
* @param {AbortSignal} [signal] - Abort signal
*/
async function runAgent(input, threadExtras, streamingCallback, signal) {
const loopConfig = config.loopDetection ?? {};
const loopDetector =
loopConfig.enabled === false
? undefined
: createSegmentLoopDetector({
windowSize: loopConfig.windowSize,
wordWindowSize: loopConfig.wordWindowSize,
maxStreams: loopConfig.maxStreams,
threshold: loopConfig.threshold,
ttlMs: loopConfig.ttlMs,
});
const stream = await agent.stream(input, {
configurable: { thread_id: sessionState.getSessionId(), ...threadExtras },
maxTokens: providerConfig.maxTokens,
Expand All @@ -304,11 +319,14 @@ async function runAgent(input, threadExtras, streamingCallback, signal) {
streamMode: ["messages", "tools", "values"],
subgraphs: true,
});
const { content, reasoning, interrupted, lastEventType } = await consumeStream(
// A detected loop abandons the stream; the caller (the TUI) sends the silent
// nudge, the same way it sends the reasoning-only "Continue".
const { content, reasoning, interrupted, lastEventType, loopDetected } = await consumeStream(
stream,
streamingCallback,
{
showToolResults,
loopDetector,
},
);
return {
Expand All @@ -318,6 +336,7 @@ async function runAgent(input, threadExtras, streamingCallback, signal) {
tokens: { input: 0, output: 0 },
interrupted,
lastEventType,
loopDetected,
};
}

Expand Down Expand Up @@ -410,6 +429,7 @@ async function runShutdown() {
await handleShutdown({
onShutdown: async () => {
if (gcManager) gcManager.stop();
await destroyLoopPool();
if (mcpAdapter) await mcpAdapter.close();
if (shutdownFn) await shutdownFn();
},
Expand Down
26 changes: 0 additions & 26 deletions src/agent/deepAgents.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { getModelContextLength } from "../provider/modelInfo.js";
import { getActiveProviderConfig, getActiveProviderName } from "../provider/index.js";
import { createTokenBudgetMiddleware } from "../provider/tokenBudgetMiddleware.js";
import { createCopilotRetryMiddleware } from "../provider/copilotRetryMiddleware.js";
import { createLoopDetectionMiddleware } from "../provider/loopDetectionMiddleware.js";
import { createSummarizationMiddlewareFromConfig } from "../provider/summarizationMiddleware.js";
import { createEvictionAwareFilesystemMiddleware } from "./evictionCeiling.js";
import { createSkillsMiddleware } from "./skillsMiddleware.js";
Expand Down Expand Up @@ -535,22 +534,6 @@ export async function createDeepAgentsOrchestrator(checkpointer = null) {
? createCopilotRetryMiddleware({ maxRetries: 3 })
: null;

// Loop-detection middleware. Detects when the model repeats the same message
// or reasoning content and injects a "You're looping" nudge, escalating to the
// agent's compaction path when the loop persists. The `onCompact` callback is
// wired via closure after the agent exists (mirroring
// `compactOnContextWindowExceeded`), so it can call `agent.compactContext`.
// Returns null when disabled, so unset config is a true no-op.
let loopDetectionOnCompact;
const loopDetectionMiddleware = createLoopDetectionMiddleware({
config: config.loopDetection,
onCompact: async (threadId) => {
if (typeof loopDetectionOnCompact === "function") {
await loopDetectionOnCompact(threadId);
}
},
});

// Replacement for deepagents' FilesystemMiddleware with an eviction ceiling
// fixed to fit image payloads. The library evicts any ToolMessage whose
// stringified content exceeds `toolTokenLimitBeforeEvict * 4` characters
Expand Down Expand Up @@ -583,7 +566,6 @@ export async function createDeepAgentsOrchestrator(checkpointer = null) {
: []),
...(summarizationMiddleware ? [summarizationMiddleware] : []),
...(copilotRetryMiddleware ? [copilotRetryMiddleware] : []),
...(loopDetectionMiddleware ? [loopDetectionMiddleware] : []),
...(tokenBudgetMiddleware ? [tokenBudgetMiddleware] : []),
],
streamTransformers: [() => createTurnTransformer()],
Expand All @@ -610,14 +592,6 @@ export async function createDeepAgentsOrchestrator(checkpointer = null) {
await agent.compactContext(config);
};

// Wire the loop-detection middleware's escalation to the agent's compaction
// path. The middleware is created before the agent, so this closure is set
// once the agent exists. It compacts the current thread's context.
loopDetectionOnCompact = async (threadId) => {
const config = threadId ? { configurable: { thread_id: threadId } } : undefined;
await agent.compactContext(config);
};

// Expose the MCP adapter (if any) so the caller can close it on shutdown.
// `allTools` is an array with the adapter attached as a property when MCP
// servers are configured.
Expand Down
45 changes: 31 additions & 14 deletions src/config/schemas/loopDetection.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
import { z } from "zod";

/**
* Configuration for the `LoopDetection` middleware.
* Configuration for streaming loop detection.
*
* When `enabled` is false (or the section is absent), madz does not register
* the loop-detection middleware and no loop detection occurs. When enabled,
* the middleware hashes the most recent model message text and reasoning
* content independently per thread, and injects a "You're looping" nudge when
* a hash repeats more than `threshold` times within a rolling `windowSize`
* window. If `escalationCount` nudges occur within `escalationWindowMs`, the
* middleware invokes the agent's compaction path instead of nudging again.
* When `enabled` is false, no loop detection occurs. When enabled, each
* streamed delta is split once, as it arrives, into completed sentences and
* words per kind (reasoning, message), and only the last `windowSize` sentences
* and `wordWindowSize` words are kept. A loop is a sentence repeated more than
* `threshold` times within the sentence window, or a run of words repeated
* back-to-back more than `threshold` times (loops with no punctuation). State
* is kept per stream (a kind plus an id), at most `maxStreams` at once. The
* stream is then abandoned and the TUI sends a silent "You're looping" nudge.
* If `escalationCount` nudges occur within `escalationWindowMs`, the context is
* compacted instead of nudging again.
*/
export const LoopDetectionSchema = z
.object({
enabled: z.boolean().default(true),
// Number of recent content hashes retained per thread for loop detection.
windowSize: z.number().int().positive().default(10),
// A hash older than this many ms is pruned from the window, so past
// Number of recent completed sentences compared for duplicates.
windowSize: z.number().int().positive().default(20),
// Number of recent completed words compared for a back-to-back repeat (loops
// with no sentence terminator, such as "ok ok ok"). Must hold `threshold + 1`
// copies of the longest repeating unit: the two-paragraph capture is 190
// words, so 3 copies need 570.
wordWindowSize: z.number().int().positive().default(1000),
// Maximum number of streams (a kind plus an id) tracked at once. Two live at
// the same moment is the realistic edge case; 4 leaves headroom. Streams that
// are done are released, so a long turn with hundreds of sequential messages
// never holds more than this. When more are live, the least recently updated
// is evicted; a looping stream is always receiving deltas, so it is never the
// one evicted. A stream with both windows full holds about 30-40 KB (measured).
maxStreams: z.number().int().positive().default(4),
// Accumulated stream text older than this many ms is dropped, so past
// content cannot cause false positives as new content streams in.
ttlMs: z.number().int().positive().default(60000),
// A hash must appear more than this many times within the window to flag a loop.
// A sentence (or word run) must repeat more than this many times to flag a loop.
threshold: z.number().int().positive().default(2),
// Number of loop nudges within the escalation window before compacting.
// Strike, within the escalation window, that compacts the context instead of
// nudging: three strikes and you're out, so strikes 1 and 2 nudge and strike 3
// compacts.
escalationCount: z.number().int().positive().default(3),
// Rolling window (ms) for counting loop nudges before escalation.
// Rolling window (ms) for counting loop strikes before escalation.
escalationWindowMs: z.number().int().positive().default(60000),
})
.strict()
Expand Down
Loading
Loading