Skip to content

The agent that grows with you

fix(groups): page groups.list over the rooms the actor may see

Teknium2e0ac30385db
diff --git a/gateway/session_group_controls.py b/gateway/session_group_controls.py
index 7acf4413327..43195086b75 100644
--- a/gateway/session_group_controls.py
+++ b/gateway/session_group_controls.py
@@ -126,22 +126,38 @@ def _group(authority, actor, home, method, params):
def listing():
limit, offset = params.get('limit', rooms.MAX_ROOM_LIST_LIMIT), params.get('offset', 0)
- result = rooms.list_rooms(db_path, **params)
- next_offset = offset + limit if len(result) == limit else None
- if getattr(authority, 'hosted_room_service', None) is not None:
- visible = []
- for room in result:
- if room_authorizer is None:
- raise RuntimeStoreError('permission_denied')
- try:
- room_authorizer(actor.subject, room['room_id'])
- except RuntimeStoreError as exc:
- if exc.reason != 'permission_denied':
- raise
- else:
- visible.append(room)
- result = visible
- return {'rooms': result, 'next_offset': next_offset}
+ if getattr(authority, 'hosted_room_service', None) is None:
+ result = rooms.list_rooms(db_path, **params)
+ return {'rooms': result, 'next_offset': offset + limit if len(result) == limit else None}
+ authorize = room_authorizer
+ if authorize is None:
+ raise RuntimeStoreError('permission_denied')
+
+ def visible(room):
+ try:
+ authorize(actor.subject, room['room_id'])
+ except RuntimeStoreError as exc:
+ if exc.reason != 'permission_denied':
+ raise
+ return False
+ return True
+ # Page the actor-visible collection, not the room table: scan the underlying cursor until
+ # the page holds ``limit`` visible rooms, and continue at the next VISIBLE room (or None),
+ # so a page is never short or empty while visible rooms remain after it. ``offset`` is
+ # that underlying continuation cursor, as returned in ``next_offset``.
+ found, cursor, next_offset = [], offset, None
+ while next_offset is None:
+ batch = rooms.list_rooms(db_path, **{**params, 'offset': cursor})
+ for room in batch:
+ if visible(room):
+ if len(found) == limit:
+ next_offset = cursor
+ break
+ found.append(room)
+ cursor += 1
+ if len(batch) < limit:
+ break
+ return {'rooms': found, 'next_offset': next_offset}
def create():
if service is not None:
diff --git a/tests/gateway/test_session_group_controls.py b/tests/gateway/test_session_group_controls.py
index 3ad1f354e0c..5ddc3b4136a 100644
--- a/tests/gateway/test_session_group_controls.py
+++ b/tests/gateway/test_session_group_controls.py
@@ -111,3 +111,32 @@ def test_profile_discovery_and_group_controls_enforce_actor_scope(tmp_path, monk
assert denied['error']['message'] == 'profile_mismatch', denied
assert (await call('profiles.delete'))['error']['code'] == -32601 # unknown method, not a bad argument
asyncio.run(probe())
+
+
+def test_groups_list_pages_the_actor_visible_rooms(tmp_path):
+ """keeltrace: pagination is applied to the rooms the actor may see, not to the room table
+ before the authorization filter (which returned empty/short pages ahead of visible rooms)."""
+ from gateway import hosted_rooms as rooms
+ from gateway.session_group_controls import _group
+ from hermes_state_runtime import RuntimeStoreError
+ db_path = tmp_path / 'state.db'
+ with SessionDB(db_path) as db:
+ for index, name in enumerate(['f1', 'f2', 'm1', 'f3', 'm2', 'm3']):
+ rooms.create_room(db_path, room_id=name, name=name, members=[], authority_gateway_id='gw',
+ now=1000.0 - index) # list order = updated_at DESC = this order
+
+ def authorize_room(subject, room_id):
+ if not room_id.startswith('m'):
+ raise RuntimeStoreError('permission_denied')
+ service = SimpleNamespace(db_path=db_path, authorize_room=authorize_room,
+ runtime=SimpleNamespace(status=lambda: {'running': True}))
+ authority = SimpleNamespace(db=db, hosted_room_service=service)
+ actor = SimpleNamespace(subject='me')
+ pages, params = [], {'limit': 2}
+ while True:
+ page = _group(authority, actor, tmp_path, 'groups.list', params)
+ pages.append([room['room_id'] for room in page['rooms']])
+ if page['next_offset'] is None:
+ break
+ params = {'limit': 2, 'offset': page['next_offset']}
+ assert pages == [['m1', 'm2'], ['m3']], pages