Skip to content

The session listing comes back most recently active first, and its roles are computed for the page at once - #1264

Merged
misterjohannesson merged 2 commits into
development-2026-11-28-2from
fix/1261-session-list-order
Nov 28, 2026
Merged

misterjohannesson merged 2 commits into
development-2026-11-28-2from
fix/1261-session-list-order

Conversation

@misterjohannesson

Copy link
Copy Markdown
Contributor

Fixes #1261.

What was there. Sessions.visible_to/2 joined the ACL and needed distinct: s.id, and Ecto emits that as DISTINCT ON (s.id) ... ORDER BY s.id, s.last_active_at DESC, so sessions.list came back in id order and a limit kept the rows with the lowest ids. After the query, session_rows/2 in harness.ex called role_for/2 for every row: an ACL lookup, the team, the membership.

What changed.

  • visible_to/2 asks the ACL with exists (a semi-join on parent_as(:session)) and has no distinct. It orders by last_active_at descending, a session with no activity last, then inserted_at descending, then id, so the page boundary stays put. limit now keeps the most recently active rows. Who can see what has not changed: owner, ACL, or team visibility in a team the person is in.
  • New Sessions.roles_for/2 returns %{session_id => role} for a page. It makes one ACL query over the page's ids, fetches the person's team memberships once (only when the page has team sessions), and loads the page's teams once, each resolved through the ladder. Its rules are the same as role_for/2, which stays for single-session use. Both now share team_gives/1 for the members_may_control step. session_rows/2 uses roles_for/2, which covers session.get and every other method that answers a session row.
  • Other listings checked: for_admin/3 has no distinct and already orders by recency. count_visible_to/1 keeps its distinct but has no order and only counts. member_team_ids/1's distinct is on team ids with no order. None of them had the bug.

Queries for one sessions.list, counted from the repository's telemetry in the calling process:

visible sessions before after
2 (1 team, 1 invited) 17 16
10 (5 team, 5 invited) 57 16

Tests (harness_test.exs, describe "sessions.list"). Each new test was run against the old sessions.ex and harness.ex and failed:

  • "most recently active first, and a limit keeps the most recent": ids are set up to sort the opposite way to activity, plus one session that was never active. Before: ["s-0…", "s-a…", "s-b…", "s-c…"] (id order).
  • "a listing costs the same number of queries for two sessions as for ten". Before: 57 vs 17.
  • "the roles on a page are the ones asked one session at a time": roles_for/2 against role_for/2 for an owned session, a team session, a team session in a team the person is not in, a team-and-invited session, an invited session and a hidden one, before and after members_may_control. Before: roles_for/2 did not exist.

Verified

  • Plane suite, whole (wslx env KUBECONFIG=<empty file> TROUPE_PLANE_TEST_DB=troupe_plane_test_1261 mix test in apps/troupe_plane): Finished in 135.2 seconds (3.7s async, 131.4s sync) / Result: 1609 passed, 9 excluded
  • wslx mix credo --strict: 21532 mods/funs, found no issues.
  • wslx mix troupe.boundaries: boundaries ok: 4 app rule(s), 0 module rule(s), no violations
  • elixir scripts/decision-citations.exs --base origin/development-2026-11-28-2: 3 file(s) changed since origin/development-2026-11-28-2, none adds a decision number to code
  • No method's shape changed, so there is no troupe.schema.diff.

Decisions. I read the decisions for sessions.ex (mix troupe.decisions --for). Decision 87 (a session nobody may see is not_found) and the visibility rules are unchanged. I took no decision.

Left. There is still no default limit. No client passes one. The desktop and the console re-sort what they receive (fleet.ts byActivity), and the TUI's client/remote.ex sends only team, state and archived. A default page size, and a way for a client to ask for the next page, is a behaviour change people would see. It should be its own change with a decision, PROTOCOL.md, the schema, and paging in the clients that need it.

…ome in one pass

visible_to/2 joined the ACL and needed distinct on the id, which Postgres sorts by before last_active_at: rows came back in id order and a limit kept the lowest ids. The ACL is now asked with exists, the order is last activity descending (never-active last, then newest made), and a limit keeps the most recent rows.

sessions.list computed each row's role with role_for/2, up to three queries a session. Sessions.roles_for/2 answers a page with one ACL query, the memberships once and the page's teams once, by role_for/2's rules; harness.ex's session_rows/2 uses it. A listing of 2 or 10 visible sessions now costs 16 queries either way (17 and 57 before).

Signed-off-by: Martin Johannesson <martin.johannesson@it-minds.dk>
Credo's nesting depth: the owner, team and invited cases of roles_for/2 are function heads.

Signed-off-by: Martin Johannesson <martin.johannesson@it-minds.dk>
@misterjohannesson
misterjohannesson merged commit bf51c8b into development-2026-11-28-2 Nov 28, 2026
14 checks passed
@misterjohannesson
misterjohannesson deleted the fix/1261-session-list-order branch November 28, 2026 17:30
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.

1 participant