Commit Graph
2 Commits
Author SHA1 Message Date
yetone 6d1c7df9e7 gateway: a reply looping on the same few words with no line or sentence's end ("mcp mcp mcp …") is found and ended, as a loop of lines is (#1489, bfxh)
bfxh: the model loops, saying "mcp" over and over. The loop guard cuts a
reply into lines and sentences and finds a loop in 256 of them. Words
repeated with no line break and no sentence stop form no such unit: the
text is cut only every unitMost (4096) bytes, so 256 units would be about
1 MB, far past any reply's output limit. A loop like that ran on to the
limit.

A unit cut at unitMost, with no break in it, is now read by itself
(runOnWords). It is a loop when it is one piece repeated (period of at
most 64 bytes, "mcpmcp…" or "重复说mcp重复说mcp…"), or when its words, at
least one per 32 bytes, are no more than 8 different ones. Two such units
in a row, about 8K characters or 2,000 tokens, end the reply. Text and
reasoning are both read. Reasoning that loops before anything is said is
asked again once, as 8424a004 does for a loop of lines. A loop broken
into lines longer than a unit still counts. The error says "the same over
and over without a break" and quotes the words.

Run:
- TestLoopGuardFindsARunOnLoop (new): spaced, comma, unspaced, a few
  words, Chinese; as text and as reasoning; found within 3 units.
- TestLoopGuardLeavesOrdinaryRepliesAlone also feeds, with no break at
  all: gateway.go, LESSONS.md, i18n.js, a log, a CSV, hex, table rules,
  and a one-line JSON array of 2,000 records. None trips.
- Break checks: runOnUnits = 1000 turns the new test red. No distinct
  limit and no word density check turns 10 of the ordinary texts red.

- TestArchiveWholeBodies' long vendor streamed "lorem ipsum dolor "
  25,000 times with no break, which is such a loop and is now ended.
  Its words are now word0 word1 …, still 450 KB, so it tests the
  archive again, not the guard.
- go test -tags nogui ./internal/gateway ./internal/provider: ok.
  gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui clean.
2026-10-10 18:54:16 +08:00
yetone 69c21cdd77 gateway, settings, gui: a reply whose reasoning or text has become a loop of the same few lines is ended with an error the agent can act on, instead of running on to its output limit (#1359, Moody-Sin: WorkBuddy's DeepSeek v4.1 flash at max effort reasoned "Now." "Writing." "Go." "Let me write." "OK." "Producing." … until max tokens, while dsh waited and Pi hung)
holdWriter.Write, which every streamed try goes through (passed through or translated; a key's, a built-in subscription's, or a plugin's fetch, as a moved WorkBuddy's), feeds a loopGuard (loop_guard.go). It reads the reasoning and the text of each protocol's deltas (Anthropic, Chat, Responses, Gemini) and cuts each into units: lines and sentences, compared without case or a closing stop. A list's "1." or a decimal doesn't end a sentence. A unit with no word in it (braces, rules, 0x00, hashes, rows of numbers) isn't counted. When the last 256 counted units of either are among 24 or fewer distinct ones, the try is cut. What came with that write goes out, then the protocol's stream error (Anthropic error event, Chat error chunk, Responses response.failed server_error, Gemini 502 UNAVAILABLE), naming the loop and quoting its units, and the vendor's request is cancelled. The cycle needn't be exact; the reporter's has 9 units in two variants. It falls back to nobody and rests nobody (fail "loop"), and the usage record's err_type is reply_loop. A reply sent whole (no stream, or Gemini without alt=sse) isn't read. Settings › Models › Replies › Stop looping replies (noLoopGuard, on by default) turns it off; new strings in zh, zh-TW, ja and de.

Tested:
- loop_guard_test.go, with the screenshot's 28 lines cut into random 1–7 byte pieces, as one line, and in Chinese. It trips within the window. It reads every protocol's shapes, and tool-call arguments never trip it.
- No trip on magpie's own sources fed as both reasoning and text: gateway.go, fallback.go, anthropic.go, app.js, i18n.js, app.css, routing.js, sessions.js, index.html, docs, LESSONS.md and go.sum. Nor on generated numbered lists, logs, CSV, hex, rules and base64.
- Through the gateway on all 4 protocols with a fake DeepSeek, and on WorkBuddy both built-in and through the community plugin's fetch in the plugin host (TestLoopEndsWorkBuddysReply). In each case the agent gets the reasoning and then the error, the vendor is let go within 2000 lines, nobody rests, and usage keeps reply_loop.
- gateway.go as 4,900 lines of reasoning passes whole on every protocol. With the setting off, a loop runs to its end.
- With the guard removed, the end-to-end and WorkBuddy tests fail on every route.
- The built binary (magpie serve on 127.0.0.1:3592, temp HOME, fake DeepSeek upstream), checked with curl on chat, messages, responses and gemini alt=sse. Each loop ended with that protocol's error, the upstream was let go after 256–271 lines, and the usage log had err_type reply_loop and routing fail "loop". The long gateway.go reasoning came through to [DONE], message_stop, response.completed and STOP. With noLoopGuard, all 100,000 lines passed.
- GUI: loop-guard-setting.test.cjs in Chromium and WebKit, en/zh/ja/de, at 1100 and 420 wide. gui-ja, gui-de, gui-zh-tw, i18n-keys, member-model-setting, click-scroll and settings-sections/groups pass.
- go vet, GOOS=linux/windows vet and builds, go test -race ./internal/gateway ./internal/settings, and the full go test -tags nogui ./... all pass.

Not tried with the real WorkBuddy or DeepSeek model.
2026-10-09 22:32:31 +08:00