Kimi K3.1 Pro · Balanced
fix-proper-handling-of-group-memberships-discourse-cursor
Discourse · Ruby · L · 300–799 lines · 6 files
The normal review, with one generalist agent. Judge: Claude Haiku 5.5.
- Gold bugs found
- 7 of 10
- Comments published
- 9
- Comments matching a bug
- 7
- Comments not matching
- 2
- Cost of this PR
- $1.367
Published comments (9)
totalPages adds a phantom page for exact multiples of the limit — Exact multiples of the limit expose an empty extra page; 100 members at 50 per page produce a third page with no members. In `totalPages`, replace `Math.floor(user_count / limit) + 1` with `Math.ceil(user_count / limit)` for nonzero counts.
Adding an existing group member can violate the unique membership index — Adding an existing member can hit the unique `(group_id, user_id)` index and raise `ActiveRecord::RecordNotUnique` instead of succeeding idempotently. Call `group.add` only when `group.users.include?(user)` is false, or use an idempotent membership insertion.
Member reload can request an out-of-range offset after deletion — Using stale `user_count` to clamp `offset` can request the reduced total after a deletion and render an empty page. Reconcile `offset` with `result.meta.total`, then retry the last valid page if the refreshed response has no members.
Stale findMembers responses can overwrite newer member pages — An earlier, slower `findMembers` response can overwrite a newer page after rapid navigation, replacing its members and metadata with stale data. Ignore stale responses, or serialize or cancel requests so only the latest page reaches `setProperties`.
Public group pages only expose the first 50 members — Members after the first 50 become unreachable because the public group-members template has no pagination controls or follow-up `offset` requests. Add public pagination, or keep this view unpaginated by leaving `limit` unset unless it is explicitly requested.
Unbounded limit parameter allows arbitrarily large member queries — An unbounded public `limit` lets one request load and serialize an arbitrarily large membership list, causing excessive database and response work. Clamp `params[:limit]` to a reasonable maximum, such as 100, before applying `limit` to the query.
Batch member addition performs O(N) lookups and membership writes — Large additions incur O(N) database round trips because every username triggers a separate `User.find_by_username` lookup and persisted `group.add` write. Bulk-fetch the requested users and insert their memberships in a batch.
Member removal bypasses counter-cache callbacks, leaving user_count inflated — `groups.user_count` remains inflated after removal because `group.users.delete(user_id)` uses `delete_all`, bypassing `GroupUser` counter-cache callbacks, and `group.save` does not recalculate it. Destroy the matching `GroupUser` records instead, or explicitly reset the counter.
Member removal leaves users with a stale primary group — Removed users can retain a `primary_group_id` for a group they no longer belong to because this path skips the cleanup previously handled by `Group#destroy_deletions`. Clear `primary_group_id` for the removed user whenever it points to this group.