Amazon Ergo 1.0 · 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 Opus 5.5.

Gold bugs found
5 of 13
Comments published
7
Comments matching a bug
5
Comments not matching
2
Cost of this PR
$3.179

Published comments (7)

highbugapp/controllers/admin/groups_controller.rb:84-92

remove_member passes a user ID to a record-based association — The remove-member request raises `ActiveRecord::AssociationTypeMismatch` and returns 500 because `group.users.delete(user_id)` passes an Integer where Rails expects a `User` record. Use `group.users.delete(User.find(user_id))` or delete the `GroupUser` row directly.

highbugapp/controllers/admin/groups_controller.rb:66-85

Add/remove member actions read group_id instead of the route's id — Every add/remove-member request raises `ActionController::ParameterMissing` (400), so neither action can load its group. Use `params[:id]` in `add_members` and `remove_member`; the member routes in `config/routes.rb` supply it, while the controller specs pass only by bypassing routing with `group_id`.

highbugapp/controllers/groups_controller.rb:22-34

Public group members page cannot access members beyond the first 50 — Members beyond the first 50 become unreachable because `findMembers()` sends no pagination params and `group/members.hbs` renders no controls. Add pagination and pass `limit`/`offset`, or fetch the remaining pages so the public list stays complete. **Also found in:** - `app/assets/javascripts/discourse/routes/group-members.js.es6:8-12`

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

Re-adding an existing group member raises a uniqueness error — Because `add_members` calls `group.add(user)` unconditionally, re-adding a member inserts a duplicate `group_users` row; the request then raises `ActiveRecord::RecordNotUnique` and returns 500. Skip the call when `group.users.include?(user)` is true.

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

findMembers reloads an out-of-range page after the last member is removed — Clamping with stale `user_count` can request `offset == total` after deletion, leaving the admin on an empty out-of-range page. Before reloading, clamp against the fresh `total` to the last valid page start (`0` when empty, otherwise `Math.floor((total - 1) / limit) * limit`).

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

totalPages overcounts when user_count is an exact multiple of limit — With 50 members and `limit` 50, the UI shows `1/2` and can request an empty page at offset 50. Use `Math.ceil(user_count / limit)` in `totalPages`, keeping the `user_count == 0` special case.

mediumbugapp/assets/javascripts/admin/templates/group_member.hbs:1

Automatic groups still show the member remove link — Admins get a confirmation dialog followed by nothing because removal fails with `can_not_modify_automatic` (422). The item context is the member user, so `automatic` is undefined and `{{#unless automatic}}` always renders the link; check `controller.automatic` or pass the group's flag into the view.

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.
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.
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.
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.
mediumAfter removeMember succeeds, findMembers reloads with the old offset, clamped only by the stale user_count; removing the only member on the last page (e.g. 51 members, offset 50) requests offset equal to the new total and the admin sees an empty, out-of-range page.

Gold bugs missed (8)

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.
mediumCreating a group ignores the alias_level the admin selected, saving only name and visibility, so new groups keep the default alias level.
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.
mediumGroupsController#members now returns {members, meta} with pagination, but the public groups controller spec still expects a bare array and stays pending, so the new response contract is untested.
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.
mediumGroup#removeMember and #addMembers have no rejection handler and the admin controller discards their promises, so failed member additions/removals are silently swallowed with no error shown to the admin.