fix(flow): don't double-append the turn reply when a handler trims history#6510
Open
joaomdmoura wants to merge 1 commit into
Open
Conversation
…story handle_turn() (and stream_turn) decided "did the handler append its reply?" by snapshotting the assistant-message count before kickoff and appending the stringified result when the count came back unchanged. A handler that appends its reply and then trims state.messages to a cap — a normal bounded-context pattern — left the count unchanged, so the fallback appended the reply a second time on every turn once trimming engaged, and the duplicates then crowded real turns out of the capped window. Replace the count heuristic with an explicit per-turn flag: append_assistant_message() sets _assistant_reply_appended, handle_turn and stream_turn clear it before kickoff and only fall back when no assistant message was appended during the turn. The now-unused _assistant_message_count() helper is removed. Fixes EPD-181. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe conversational mixin now uses an explicit per-turn flag to detect appended assistant replies. Both regular and streaming turns avoid duplicate fallback messages, while silent handlers still receive fallback replies. Regression tests cover capped-history and non-appending handlers. ChangesConversational reply fallback
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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
Fixes EPD-181: in the experimental Conversational Flow,
handle_turn()'s safety net — "append the handler's return value if the handler didn't append an assistant message itself" — inferred that by comparing assistant-message counts before and after kickoff. A handler that appends its reply and trimsstate.messagesto a cap (a normal bounded-context pattern) leaves the count unchanged, so the fallback appended the reply a second time — on every turn once trimming engaged. The duplicates then crowd real turns out of the capped window. Silent, starts mid-conversation, nasty to debug.Fix
Replace the count heuristic with an explicit per-turn signal, as the ticket suggests:
append_assistant_message()sets an_assistant_reply_appendedinstance flag.handle_turn()andstream_turn()clear the flag right before kickoff and only run the fallback when no assistant message was appended during the turn — robust to any history mutation (trimming, rewriting, compaction).append_agent_result(..., visibility="public")and the built-in handlers, which all append throughappend_assistant_message()._assistant_message_count()helper is removed.Testing
NOT REPRODUCED: no duplicate assistant messages across 4 turns— where 1.15.2 duplicated from turn 3 onward — and the capped window correctly holds the last two full turns instead of stale echo copies.TestHandleTurnReplyFallbackregression tests: the trim scenario across 4 turns (no duplicates + exact window contents), and the fallback still appending exactly once for handlers that return without appending.test_flow_conversation.py(44), broader flow suites (305: flow, definition, from-definition, persistence, human-input integration), ruff, and mypy.🤖 Generated with Claude Code
Note
Low Risk
Targeted fix in experimental conversational mixin with regression tests; behavior change only affects when the turn reply fallback runs.
Overview
Fixes EPD-181: experimental conversational
handle_turn()/stream_turn()no longer infer “did the handler already reply?” by comparing assistant message counts before and after kickoff.append_assistant_message()now sets a per-turn_assistant_reply_appendedflag; each turn resets it before kickoff and only runs the return-value fallback when the flag is still false. That stays correct when handlers cap or rewritestate.messagesmid-turn (where count-based logic falsely triggered a second append).Removes
_assistant_message_count(). AddsTestHandleTurnReplyFallbackfor trim-with-cap and silent-handler fallback behavior.Reviewed by Cursor Bugbot for commit 8d2d6b1. Bugbot is set up for automated code reviews on this repo. Configure here.