Skip to content

fix: clear loop detection after compression and detect text-only loops - #2058

Open
xielixing wants to merge 1 commit into
GCWing:mainfrom
xielixing:fix/issue-1179-compression-loop-detection
Open

fix: clear loop detection after compression and detect text-only loops#2058
xielixing wants to merge 1 commit into
GCWing:mainfrom
xielixing:fix/issue-1179-compression-loop-detection

Conversation

@xielixing

Copy link
Copy Markdown

Fix: Clear loop detection after compression and detect text-only loops (#1179)

Problem

After context compression succeeds, the response content keeps looping indefinitely (issue #1179).

Root Cause

Two bugs in execution_engine.rs:

  1. Stale loop-detection state after compression: When context compression succeeds, recent_tool_signatures and recent_failed_tool_signatures were NOT cleared. The compressed context is effectively a fresh start, but stale pre-compression tool signatures remained, biasing post-compression loop detection.

  2. No text-only loop detection: When the model produces text-only responses (no tool calls), recent_tool_signatures.clear() was called, resetting ALL loop detection. This allowed the model to loop indefinitely producing identical text responses — exactly the symptom in [Bug]: 压缩之后回答的内容一直循环,怀疑压缩有问题 #1179.

Fix

1. Clear loop detection state after compression — At both compression success sites (primary path at line ~3567 and overflow recovery at line ~3801), clear recent_tool_signatures, recent_failed_tool_signatures, failed_tool_recovery_attempts, and recent_text_fingerprints.

2. Add text-only loop detection — New recent_text_fingerprints: Vec<String> tracks normalized fingerprints of text-only responses (whitespace-stripped, lowercased, truncated to 500 chars). When max_consec consecutive identical fingerprints are detected, the turn is finalized with reason "repeated_text_responses".

Validation

  • cargo check -p bitfun-core passes (exit code 0, only pre-existing dead_code warning)

Closes #1179

After context compression succeeds, recent_tool_signatures and
recent_failed_tool_signatures were not cleared. The compressed
context is effectively a fresh start, so stale pre-compression tool
signatures must not bias post-compression loop detection.

Additionally, when the model produces text-only responses (no tool
calls), recent_tool_signatures.clear() reset all loop detection,
allowing the model to loop indefinitely producing identical text.
This is the exact symptom described in issue GCWing#1179: after compression,
the response content keeps looping.

Two fixes:

1. Clear loop detection state (recent_tool_signatures,
   recent_failed_tool_signatures, failed_tool_recovery_attempts) at
   both compression success sites (primary path and overflow recovery).

2. Add text-only loop detection via recent_text_fingerprints: a
   normalized fingerprint of assistant text content is tracked for
   text-only rounds. When max_consec consecutive identical
   fingerprints are detected, the turn is finalized with reason
   "repeated_text_responses".

Closes GCWing#1179
@xielixing
xielixing force-pushed the fix/issue-1179-compression-loop-detection branch from 8246dc8 to e9c88e3 Compare August 5, 2026 06:24
xielixing pushed a commit to xielixing/BitFun that referenced this pull request Aug 5, 2026
Three afternoon PRs ballooned to ~8.8k added lines because the agent
branched its fix worktrees from the host checkout's HEAD, which sat on
the in-flight feature branch; the whole feature diff rode along into
each PR. The preamble now requires basing every fix branch on
origin/<default> after a fetch, never on the host checkout's current
branch. The three contaminated branches were rebased onto origin/main
and force-pushed (PRs GCWing#2058/GCWing#2059/GCWing#2060 now carry only their own fix).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 压缩之后回答的内容一直循环,怀疑压缩有问题

1 participant