GPT-6.1 Sol · Heavy

fix-proper-handling-of-group-memberships-discourse-cursor

Discourse · Ruby · L · 300–799 lines · 6 files

The generalist plus 9 specialist lens agents in parallel, then the same verify and dedup steps. Judge: Claude Haiku 5.5.

Gold bugs found
5 of 10
Comments published
8
Comments matching a bug
5
Comments not matching
3
Cost of this PR
$1.870

Published comments (8)

highbugapp/controllers/groups_controller.rb:22-26

Pagination hides custom group members beyond the first 50 — Update that consumer to fetch subsequent pages, or preserve the endpoint's complete-list behavior for custom groups. **Also found in:** - `app/assets/javascripts/discourse/routes/group-members.js.es6:8-11`

mediumbugapp/controllers/admin/groups_controller.rb:90

Removing a member leaves their primary group pointing to the removed group — `users.primary_group_id` becomes stale because `group.users.delete` bypasses the cleanup in `Group.destroy_deletions`. Clear the removed user's primary group when it matches this group, within the same transaction.

mediumbugapp/controllers/admin/groups_controller.rb:71-74

Adding an existing group member violates the unique membership index — Clicking Add again can raise a database exception because `Group.add` inserts another membership against the unique `(group_id, user_id)` index. Check `group.users.exists?` before adding each user so repeated submissions succeed.

mediumbugapp/assets/javascripts/discourse/models/group.js:30-35

Stale member responses can overwrite the latest group page or refresh — Out-of-order responses let `self.setProperties` replace the latest member page or membership refresh with stale data. Track a request sequence token for each group and apply a response only if it belongs to the latest request.

mediumbugspec/controllers/admin/groups_controller_spec.rb:114-116

Automatic-group removal test does not verify membership is preserved — A `remove_member` implementation that deletes membership before returning 422 would still pass because this test checks only `response.status` without establishing membership. Create an automatic group with a real member, attempt removal, and assert both rejection and preserved membership.

mediumbugspec/controllers/admin/groups_controller_spec.rb:93-95

Automatic-group addition test does not verify membership stays unchanged — Because `add_members` skips unresolved usernames, using an uncreated `l77t` and checking only `response.status` cannot detect membership changes before rejection. Create a real nonmember, submit that user's username, and assert both the 422 response and continued absence from the group.

mediumbugapp/assets/javascripts/admin/controllers/admin-group.js.es6:13

Exact multiples of the page limit create an extra empty page — With 50 members and a limit of 50, `totalPages` becomes 2, enabling navigation to an empty page. Use `Math.ceil` instead of `Math.floor` plus one to count only populated pages.

mediumbugapp/assets/javascripts/discourse/models/group.js:23

Member pagination can remain on an empty page after membership shrinks — Removing the last member on a page can leave `removeMember` displaying an empty page because `Math.min` uses stale `user_count` and permits `offset` equal to the total. Clamp `offset` to the last nonempty page using the updated total, and refetch when necessary.

Gold bugs found (5)

mediumThe admin group pager computes total pages as floor(user_count / limit) + 1, so when the member count is an exact multiple of the limit it shows an extra page and Next navigates to an empty member list.
mediumPaging actions fire overlapping member requests and each response unconditionally overwrites offset and members, so a stale response arriving last can revert the list to an earlier page.
highThe public members endpoint now always caps results at 50 per page, but the public group members page has no pagination, so groups with more than 50 members are silently truncated for users.
mediumRemoving a member deletes the membership row directly and skips clearing the user's primary_group_id, so a removed user can keep this group as their primary group.
mediumAdmin add_members pushes every username into group.users without skipping existing members, so adding someone already in the group hits the unique group_users index, returns a 500 and leaves earlier users partially added.

Gold bugs missed (5)

mediumCreating a group ignores the alias_level the admin selected, saving only name and visibility, so new groups keep the default alias level.
mediumGroup member pagination is off by one when user_count is a multiple of limit: Next moves to offset == user_count and shows an empty page.
mediumAdmin remove_member deletes memberships with group.users.delete, which skips GroupUser callbacks, so the cached groups.user_count is never decremented and stays inflated.
mediumThe public group members endpoint takes `limit` from params with no upper bound or positivity check, letting a visitor request huge pages or trigger a 500 with a negative limit.
mediumAdmin remove_member calls group.users.delete with a raw integer id, which raises RecordNotFound (404/500) when the user is not or no longer a member, e.g. on concurrent removal.