fix: Claude DAG fork detection traverses full subtree#231
Merged
Conversation
countUserTurns previously followed only the first child at each node to count user turns. In sessions with nested forks, the first-child path could dead-end quickly (e.g. after 0-1 user turns), making the algorithm treat large conversation branches as small retries and discarding them. Now uses a stack-based traversal to count all reachable user entries in the subtree, correctly identifying large branches that should be preserved as the main conversation path. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The countUserTurns fix changes how forked sessions are parsed, so existing databases need a full resync to pick up the corrected message content. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
roborev: Combined Review (
|
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
countUserTurnsnow traverses the full subtree (stack-based DFS) instead of following only the first child at each node. The old approach undercounted user turns in sessions with nested forks, causing the fork heuristic to discard large conversation branches as "small retries."dataVersionto 6 so existing databases trigger a full resync on upgrade.Context
Sessions with many nested fork points (e.g. 17 in the reported case) would have their main conversation branch dropped entirely. The first-child-only traversal would dead-end after 0-1 user turns at nested forks, making a 124-entry branch look like a trivial retry. Result: a session with 110 real messages would show only 11.
Test plan
TestForkDetection_NestedForkCountsFullSubtreecovers the specific scenariogo test -tags fts5 ./... -short)Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com