fix(ttsr): interrupt paragraph loops by near-duplicate frequency - #1763
Merged
Merged
Conversation
A captured incident streamed a 12-minute narration loop whose paragraphs were paraphrases of one another, so the byte-exact paragraph-repeat mechanism never counted three occurrences. The fixture is the sanitized incident tail; the negatives pin that ordinary prose, a minority of echoes, fenced code and tool argument streams stay untouched.
buildHealthyPrefix emitted one sentence template whose only varying token was a counter, and the paragraph-repeat cycle did the same, so every paragraph carried the same words. That is healthy input for a byte-exact rule and a narration loop for a normalized one. Both generators now vary their wording; the assertions are unchanged.
A paragraph counts as an echo when its normalized word set reaches 0.5 Jaccard against any of the last 32 eligible paragraphs, and the detector fires only when at least 8 of the last 12 eligible paragraphs are echoes. The density gate is what keeps ordinary prose out: a repeated sentence, a callback or a closing summary never reaches it. Fenced code blocks are skipped, and truncation keeps the anchor paragraph and drops the echoed run.
Fifteen paraphrases of one action, none byte-identical, streamed from the local fake model server. The scenario asserts the abort, the truncated persisted message, the collapse-repetition interrupt in the recovery request, and the recovered answer.
code-yeongyu
deleted the
fix/1330-near-duplicate-paragraph-frequency
branch
September 16, 2026 09:02
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
Closes the near-repeat gap #1330 describes, with a frequency rule instead of a looser match.
paragraph-repeat(#1326) compares paragraphs byte for byte over a 64-entry ring, so it misses two shapes. A model that restates the same step in different words every time never produces three identical hashes. And a cycle longer than the ring stays invisible even where it repeats verbatim.A captured incident hit both at once:
claude-fable-5-1streamed narration for 12 minutes with zero tool calls, paraphrasing one action ("I'm assembling the final delivery ... — task complete." / "Both batches sent with photos and reaction cleared — done."), then settled into an exact 114-paragraph cycle. Replaying that text through the shippedcollapseDetectorreturns no match on any mechanism. Nothing ended the generation; the user killed the session.What this adds
detectors/collapse-near-duplicates.ts(near-duplicate-paragraphs), wired last in the collapse chain for text and thinking streams:The density gate is what keeps ordinary prose out. #1330 warns that a looser match alone would interrupt a legitimate "processing item N" traversal; a rate gate answers that directly, because one similar pair, a callback to an earlier point, or a closing summary never reaches 8-of-12. Cycle length does not matter, so the 114-paragraph case is covered too. Paragraphs inside fenced code blocks are skipped and never enter the history.
Remediation reuses the existing collapse path: abort, truncate from the first echoed paragraph in the firing window (its anchor is kept), then the collapse nudge.
Calibration evidence
Replaying the shipped detector over the local session store - 12,499 real assistant text and thinking parts, 28.9 MB - fires twice, and both firings are known runaway generations from one 2026-09-03 session (38,193 chars, and 363,042 chars ending in
stopReason: length). No other match.Tests
test/ttsr/detector-collapse-near-duplicates.test.tsreplays the sanitized incident tail, pins chunk-boundary independence, and pins every negative above plus tool-stream exclusion and the hand-off toparagraph-repeaton byte-identical cycles.Mutation proof: removing the one wiring line in
collapse.tsfails exactly the three positive tests (incident replay, synthetic paraphrases, thinking stream); restoring it returns 9/9.Fixture changes, and why they are not loosened assertions
Two synthetic generators varied only by a counter, so after digit normalization every paragraph carried the same tokens - healthy input for a byte-exact rule, a narration loop for a normalized one:
collapse-test-inputs.tsbuildHealthyPrefixnow composes varied vocabulary;detector-collapse-paragraphs.test.tsnarration()now emits seven lexically distinct steps.Their assertions are untouched, so the exact-repeat contract still tests exact repetition. The digit-only-drift shape those fixtures used to have is the shape #1330 asks to catch, and it is now covered by the new test.
Real-CLI QA
A new
senpi-qamock-loop scenario,ttsr-near-duplicate-loop, streams fifteen paraphrases of one action (none byte-identical) from the local fake model server through the real CLI:The existing scenarios still pass on the same build:
ttsr-paragraph-loop6/6,ttsr-collapse4/4.Verification
Run on a separate macOS machine (bun 1.4.2) against
origin/main 9e1f97f634:bunx vitest run test/ttsr- 18 files, 321 tests passedbun run check- exit 0bun scripts/check-pr-changelog.mjs- PASSEvidence for the incident itself is held locally; the in-tree fixture is the sanitized tail.
Fixes #1330