Skip to content

fix(list): ignore invalid removal indices before changing state - #1090

Open
janearc wants to merge 1 commit into
charmbracelet:mainfrom
janearc:fix/list-ignore-invalid-removal
Open

janearc wants to merge 1 commit into
charmbracelet:mainfrom
janearc:fix/list-ignore-invalid-removal

Conversation

@janearc

@janearc janearc commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Return early for indices outside the backing list, preventing negative-index panics and preserving filters when an ignored removal has no matches.

Keep the bounds guard proposed in #932 separate from its cursor changes. Add positive, negative, and regression tests. Filtered index handling in #632 and #638 remains outside this change.

  • [👩🏻‍🎤] I have read CONTRIBUTING.md.
  • [🚳 ] I have created a discussion that was approved by a maintainer (for new features).

fix(list): ignore out-of-range removal indices

RemoveItem promises to ignore out-of-range indices, but a negative index currently panics. A past-end removal can also clear an active filter when it has no matches.

Check the index against the backing item list before changing items, filter state or pagination. Invalid removals become no-ops, while valid removals retain their existing behavior.

The patch includes three separate tests:

  • Positive: removing the first, middle or last valid item leaves the expected items in order.
  • Negative: past-end removals preserve items, filter matches, filter text and selection.
  • Regression: negative indices are safe on populated, filtered and empty lists; a past-end removal preserves a filter with no matches.

On unpatched main, the negative-index regression panics and the empty-match-filter case fails. The positive and negative controls pass. Deliberately skipping all removals makes the positive test fail; resetting the filter on invalid removal makes the negative test fail.

Closed, unmerged PR #932 proposed the same three-line bounds guard alongside changes to cursor positioning after valid removals. The guard itself is identical here. This PR isolates that safety fix and adds separate positive, negative and regression tests, deliberate-break checks, and the validation below, so it can be reviewed independently of the cursor changes.

Open PR #638 targets the mismatch between filtered and backing-list indices described in issue #632. That work determines which item a valid filtered removal should delete. This guard handles invalid backing-list indices before any state changes: it prevents the negative-index panic and keeps an ignored removal from clearing an empty-match filter.

Both reproductions from #632 still fail with this patch: removing by filtered index can delete a different backing item, and removing by backing index can leave a deleted item visible in the filtered list. This PR therefore does not close #632 or replace #638. Its contribution is a small, independently tested bounds fix; valid-removal indexing and cursor behavior remain unchanged.

Validation on main 1deada954e6e, Go 1.26.10:

  • Full native arm64 race suite passes with -count=4 -cpu=1,4; the new tests also pass 20 race repetitions.
  • Full amd64 tests and race suite pass under Rosetta.
  • The exact CI-pinned golangci-lint v2.13.1, configured formatting, vet and module tidy checks pass. No module files change.
  • Linux/Windows 386 (i386), amd64 and arm64, plus Darwin amd64: build, all-package test compilation, vet and configured lint pass.
  • Aggregate root-module statement coverage increases from 55.9557% (1818/3249) to 56.2904% (1830/3251) using identical atomic coverage settings.

Cross-target binaries were compiled rather than executed. These are local checks; remote CI remains to be confirmed.

disclosure: i use ai, me and claude are buddies. this came up when working on my own harness, i root-caused it withclaude, claude wrote the patch, ir eviewed the patch, i asked codex to review the patch, and qwen said that i have great taste in bikes. lmk if you need anything changed; i think this is actually a good pr because the scope is very (very) narrow despite it touching/interacting with several others.

Return early for indices outside the backing list, preventing
negative-index panics and preserving filters when an ignored
removal has no matches.

Keep the bounds guard proposed in charmbracelet#932 separate from its cursor
changes. Add positive, negative, and regression tests. Filtered
index handling in charmbracelet#632 and charmbracelet#638 remains outside this change.
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.

Inconsistent & potentially misleading behaviour when calling List RemoveItem

2 participants