Skip to content

Stop restoring text selections to avoid a Compose draw crash - #1045

Merged
ivanfenenko merged 1 commit into
mainfrom
fix/1042-selection-draw-crash
Nov 15, 2026
Merged

ivanfenenko merged 1 commit into
mainfrom
fix/1042-selection-draw-crash

Conversation

@ivanfenenko

@ivanfenenko ivanfenenko commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

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 from MultiParagraph.getPathForRange, called by SelectionController.draw inside 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.draw swaps start and end only when handlesCrossed is true, then calls getPathForRange without checking that start ≤ end.
  • In a single Text, SingleSelectionLayout.crossStatus compares slots before offsets. If the end of a selection is dragged up to an earlier line and then past the Text's right edge, the end slot is "after" the Text. The result is a selection whose end offset is earlier than its start but whose handlesCrossed is false.
  • During a live drag, SingleSelectionLayout.createSubSelections repairs the flag before drawing. However, the unrepaired selection is the one stored in SelectionState, and SelectionState.Saver saves it.
  • The default SelectionContainer keeps its state with rememberSaveable, so the selection survives a rotation or a process restore. On restore, SelectionManager rebuilds sub-selections through the generic createSubSelections, which does not repair the flag for a single Text. The next draw throws.

The change. A new OpenCodeSelectionContainer passes a plain remember { SelectionState() } to SelectionContainer (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-kit and hexcode have no SelectionContainer. 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 a Text that is narrower than its container. It then drags to a point on line 1 past the Text's right edge, checks that two selection handles exist, and restores the saved state with StateRestorationTester:

  • restoredBackwardsSelection_doesNotCrash uses the wrapper and expects no crash and no restored selection.
  • restoredBackwardsSelection_withoutWrapper_reproducesCrash uses the default SelectionContainer and is expected to crash on 1.12.1. It is skipped unless the instrumentation argument reproduce1042=true is passed, so CI never runs a test that is meant to fail.

Checks

  • ./gradlew :app:assembleDebug :app:assembleDebugAndroidTest passes.
  • ./gradlew :app:testDebugUnitTest passes.
  • ktlintCheck reports no new violations. The import-order findings in OpenCodeRenderedMarkdown.kt and OpenCodeSessionComposer.kt were already there on main.
  • OpenCodeSelectionContainerTest on an API 37 emulator. With reproduce1042=true, the default SelectionContainer case fails with IllegalArgumentException: Start(19) or End(13) is out of range [0..33), or start > end! from SelectionController.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.
  • No UI change.

Risks / Follow-ups

  • Not checked by rotating the running app on a device; the test restores saved state with StateRestorationTester.
  • Drop the wrapper when a stable Foundation 1.13 BOM lands. 1.13 reportedly fixes the flag where it is computed.

🤖 Generated with Claude Code


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with 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 Compose SelectionContainer that holds SelectionState in plain remember instead of rememberSaveable, 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 SelectionContainer call 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 with StateRestorationTester, asserts the wrapper leaves no selection handles after restore, and optionally runs the raw SelectionContainer case when reproduce1042=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.

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
ivanfenenko marked this pull request as ready for review November 15, 2026 16:57

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:27 low: 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

@ivanfenenko

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

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.

Android crash: IllegalArgumentException in Compose SelectionController.draw / getPathForRange on 1.5.0 (40)

1 participant