feat(session): a loop guard that can see a loop - #105
Merged
Conversation
The previous detector could only see three byte-identical parts in a row inside one assistant message, so any reasoning part hid the loop and a loop across steps was invisible. When it did fire it asked the user a question with no bound, which made our only loop defence another way to hang. Judge repetition across the turn, treat a changed result as evidence the world moved, and answer the repeated call with a correction the model can act on. Stop the turn only if that correction changed nothing. Claude-Session: https://claude.ai/code/session_01U29Yk1UscZJ5ZVBXV1Sn8b
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.
What was there
DOOM_LOOP_THRESHOLD = 3compared the last three parts of the current assistant message andrequired byte-identical serialized input. That meant:
was invisible;
permission.ask("doom_loop"), whose wait has no bound. Our only defenceagainst a loop was itself a way to hang. No test tripped it.
What replaces it
session/loop-guard.ts— pure, tested — judges repetition across the whole turn:calls returning different results is what polling looks like, and is left alone;
streak it started.
The guard is consulted in
tools.tsbefore the call runs, so a call whose answer is alreadyknown is never made again. The correction arrives as that tool's own result, quoting the arguments
and the answer being ignored, and naming the ways out. If the model keeps going anyway, the turn
ends (
ctx.blocked) rather than anyone being asked whether to continue.experimental.loop_guardsets the thresholds (3 and 5 by default) orfalsedisables; adoom_loop: "allow"permission rule still means "let it repeat", so that knob is not left dead.Verification
after.
untouched — then passing: the second identical call is answered by the correction, the third ends
the turn.
bun typecheckacross the workspace;packages/redcodesession suite 435 pass.test and
finalizes assistant when cancelled before processor creation completes, both timingout under local load. CI arbitrates.
https://claude.ai/code/session_01U29Yk1UscZJ5ZVBXV1Sn8b
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.