Repository navigation
Stop restoring text selections to avoid a Compose draw crash - #1045
Merged
Merged
Conversation
Compose Foundation 1.12 can save a backwards selection with a wrong "handles crossed" flag. Restoring it after a configuration change makes SelectionController.draw call getPathForRange with start > end, which throws on the main thread. Every SelectionContainer now goes through OpenCodeSelectionContainer, which holds a plain remembered SelectionState, so no selection is restored. Add an instrumented test for the restore path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ivanfenenko
marked this pull request as ready for review
November 15, 2026 16:57
There was a problem hiding this comment.
Automated review: no finding that 2+ of 3 passes agreed on.
1 finding(s) from a single pass, not posted
android/app/src/main/java/dev/opencode/mobile/ui/components/OpenCodeSelectionContainer.kt:27low: Selection is now also lost on scroll-away of keyed timeline items, not just on rotation (glm-5.3-flash)
opencode pr-review · passes: muse-spark-1.3-contributor, glm-5.3-flash, deepseek-v4.1-flash · $0.036 · 1 pass(es) stopped early
Collaborator
Author
|
@cursor review |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit f2b1e92. Configure here.
This was referenced Nov 15, 2026
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 #1042. Android only: this is a Compose Foundation bug, and iOS is not affected.
The crash.
IllegalArgumentException: Start(17) or End(1) is out of range [0..23), or start > end!on the main thread. It is thrown fromMultiParagraph.getPathForRange, called bySelectionController.drawinside Foundation's own text draw node, so the app cannot catch it there.Cause (from Foundation 1.12.1 source, reproduced on an emulator).
SelectionController.drawswaps start and end only whenhandlesCrossedis true, then callsgetPathForRangewithout checking that start ≤ end.Text,SingleSelectionLayout.crossStatuscompares slots before offsets. If the end of a selection is dragged up to an earlier line and then past theText's right edge, the end slot is "after" theText. The result is a selection whose end offset is earlier than its start but whosehandlesCrossedis false.SingleSelectionLayout.createSubSelectionsrepairs the flag before drawing. However, the unrepaired selection is the one stored inSelectionState, andSelectionState.Saversaves it.SelectionContainerkeeps its state withrememberSaveable, so the selection survives a rotation or a process restore. On restore,SelectionManagerrebuilds sub-selections through the genericcreateSubSelections, which does not repair the flag for a singleText. The next draw throws.The change. A new
OpenCodeSelectionContainerpasses a plainremember { SelectionState() }toSelectionContainer(public API in 1.12.1), so no selection is saved or restored. All five app call sites use it: rendered markdown, the plain assistant message, the Select text sheet, the tool result sheet and the permission details in the composer.opencode-kitandhexcodehave noSelectionContainer. The only behaviour change is that a text selection no longer survives rotation. The wrapper's comment says to remove it once the BOM ships Foundation 1.13 stable.Test.
OpenCodeSelectionContainerTest(instrumented) long-presses a word on line 2 of aTextthat is narrower than its container. It then drags to a point on line 1 past theText's right edge, checks that two selection handles exist, and restores the saved state withStateRestorationTester:restoredBackwardsSelection_doesNotCrashuses the wrapper and expects no crash and no restored selection.restoredBackwardsSelection_withoutWrapper_reproducesCrashuses the defaultSelectionContainerand is expected to crash on 1.12.1. It is skipped unless the instrumentation argumentreproduce1042=trueis passed, so CI never runs a test that is meant to fail.Checks
./gradlew :app:assembleDebug :app:assembleDebugAndroidTestpasses../gradlew :app:testDebugUnitTestpasses.ktlintCheckreports no new violations. The import-order findings inOpenCodeRenderedMarkdown.ktandOpenCodeSessionComposer.ktwere already there onmain.OpenCodeSelectionContainerTeston an API 37 emulator. Withreproduce1042=true, the defaultSelectionContainercase fails withIllegalArgumentException: Start(19) or End(13) is out of range [0..33), or start > end!fromSelectionController.draw→getPathForRange, the same stack as Android crash: IllegalArgumentException in Compose SelectionController.draw / getPathForRange on 1.5.0 (40) #1042. The wrapper case passes in both runs.Risks / Follow-ups
StateRestorationTester.🤖 Generated with Claude Code
Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.Note
Low Risk
Localized UI workaround with a small behavioral change (selection not restored on rotate); no auth or data-path changes, backed by an instrumented regression test.
Overview
Adds
OpenCodeSelectionContainer, a thin wrapper around ComposeSelectionContainerthat holdsSelectionStatein plainrememberinstead ofrememberSaveable, so text selections are not restored after rotation or process death. That sidesteps a Foundation 1.12 bug where a backwards drag can save a wrong "handles crossed" flag and crash on draw after restore (#1042).All five in-app
SelectionContainercall sites switch to the wrapper: assistant plain text, rendered markdown (non-outgoing), session Select text sheet, tool result unrecognized raw body, and permission details in the composer.Adds
OpenCodeSelectionContainerTest: it reproduces the backwards-selection restore path withStateRestorationTester, asserts the wrapper leaves no selection handles after restore, and optionally runs the rawSelectionContainercase whenreproduce1042=true(expected crash, skipped in CI).User-visible tradeoff: text selection no longer survives configuration changes until Foundation 1.13 ships and the wrapper can be removed.
Reviewed by Cursor Bugbot for commit f2b1e92. Bugbot is set up for automated code reviews on this repo. Configure here.