GetPagedPlayouts: TotalCount ignores the query filter, so a filtered page reports the unfiltered total #758

Closed
opened 2026-08-06 21:49:47 +02:00 by timothy · 2 comments
Owner

GetPagedPlayoutsHandler computes the count before applying the filter:

int count = await dbContext.Playouts.CountAsync(cancellationToken);   // unfiltered, and pre-`Channel != null`
...
if (!string.IsNullOrWhiteSpace(request.Query))
    query = query.Where(p => EF.Functions.Like(p.Channel.Name, $"%{request.Query}%"));

So PagedPlayoutsViewModel.Count is the total number of playout rows, not the number matching
query — and it does not even account for the Channel != null filter applied to the page query.

Why it surfaced now: until ersatztv#754/#757 no MCP caller could pass query at all
(ToolArgumentValidator rejects undeclared arguments), so the discrepancy was unreachable from that
surface. ersatztv_list_playouts now declares it, so an agent filtering by channel name sees a
totalCount that does not match its own page and will page to a completeness target it can never
reach. Pre-existing server bug; #757 is what exposed it, not what caused it.

This is the same family as #690 (rerun collections: paged list TotalCount ignores the search query).
Worth checking whether the two share a fix.

Done-when

  • GetPagedPlayoutsHandler counts the SAME query it pages, filter and all
  • A test pins the filtered count (assert an expected set, not a filter-derived one)
  • Checked against #690 for a shared idiom / shared fix
  • Adversarial review passed
`GetPagedPlayoutsHandler` computes the count **before** applying the filter: ```csharp int count = await dbContext.Playouts.CountAsync(cancellationToken); // unfiltered, and pre-`Channel != null` ... if (!string.IsNullOrWhiteSpace(request.Query)) query = query.Where(p => EF.Functions.Like(p.Channel.Name, $"%{request.Query}%")); ``` So `PagedPlayoutsViewModel.Count` is the total number of playout rows, not the number matching `query` — and it does not even account for the `Channel != null` filter applied to the page query. **Why it surfaced now:** until ersatztv#754/#757 no MCP caller could pass `query` at all (`ToolArgumentValidator` rejects undeclared arguments), so the discrepancy was unreachable from that surface. `ersatztv_list_playouts` now declares it, so an agent filtering by channel name sees a `totalCount` that does not match its own page and will page to a completeness target it can never reach. **Pre-existing server bug; #757 is what exposed it, not what caused it.** This is the same family as #690 (rerun collections: paged list `TotalCount` ignores the search query). Worth checking whether the two share a fix. ## Done-when - [x] `GetPagedPlayoutsHandler` counts the SAME query it pages, filter and all - [x] A test pins the filtered count (assert an expected set, not a filter-derived one) - [x] Checked against #690 for a shared idiom / shared fix - [x] Adversarial review passed
timothy added the apibugpriority: medium labels 2026-08-06 21:49:57 +02:00
timothy added the in-progress label 2026-08-26 07:51:20 +02:00
Author
Owner

Claiming this (Claude Code session, 2026-08-26), bundled with the sibling issue — #758 and #690 are the same defect shape: a paged query handler computes CountAsync on the unfiltered DbSet while the page query applies a Query/Where predicate, so TotalCount reports the whole table.

Pre-claim checks per process.parallel-session-claim: no open PR referencing either issue (only Renovate PRs are open), no remote branch naming 690/758/paged/totalcount, no prior comments on either issue, fresh git fetch origin main (at 7453dd3a8).

Scope taken: not just the two named handlers. Per #690 Done-when box 3 ("check the sibling paged handlers for the same shape") and the "an issue's file list is not the population" lesson, this sweeps every paged handler in ErsatzTV.Application for the shape and records each site, including any deliberate non-fix.

Claiming this (Claude Code session, 2026-08-26), bundled with the sibling issue — #758 and #690 are the same defect shape: a paged query handler computes `CountAsync` on the unfiltered `DbSet` while the page query applies a `Query`/`Where` predicate, so `TotalCount` reports the whole table. Pre-claim checks per `process.parallel-session-claim`: no open PR referencing either issue (only Renovate PRs are open), no remote branch naming 690/758/paged/totalcount, no prior comments on either issue, fresh `git fetch origin main` (at 7453dd3a8). Scope taken: not just the two named handlers. Per #690 Done-when box 3 ("check the sibling paged handlers for the same shape") and the "an issue's file list is not the population" lesson, this sweeps every paged handler in `ErsatzTV.Application` for the shape and records each site, including any deliberate non-fix.
Author
Owner

Closing record

Outcome: Fixed in PR #833, bundled with #690 — box 3 ("checked against #690 for a shared idiom /
shared fix") resolved as yes, and it is the same fix. GetPagedPlayoutsHandler now derives both its
count and its page from one IQueryable. The issue's premise was right on both counts: pre-existing
server bug, and #757 exposed rather than caused it.

Root cause: The count was dbContext.Playouts.CountAsync(ct) — the whole table — while the page
applied both a Channel != null filter and the optional Query. One mechanism shared with #690 and
four other handlers; the fix is structural so the two cannot drift again.

Correction to the issue's framing, on evidence: the Channel != null half is DEFENSIVE, not a
live defect. Playout.ChannelId is non-nullable with DeleteBehavior.Cascade and both production
connection strings set foreign keys=true, so an orphaned playout is unreachable while the FK holds —
the pre-fix and post-fix counts are equal on every real database. The Query half is the live bug,
and it is reachable exactly as described: ersatztv_list_playouts declaring query (#757) is what
made it visible from the MCP surface.

Decisions/conventions changed: added api.paged-count-matches-page-query (see #690's record for
the full statement).

Reusable knowledge:

  1. A paged handler's filter is not only the search string. The clause this handler was missing from
    its count was a structural one applied unconditionally, so no search query was needed to expose the
    discrepancy in principle. Enumerate every clause on the page query, not just the if (query) one.
  2. Scope harm to the callers that exist. Of the six handlers fixed, only three
    (rerun-collections, multi-collections, playouts) reach a controller; the other three have no
    production caller and were latent. The SPA reads this total in two places
    (DashboardScreen, PlayoutsScreen) via a single un-looped request, so no completeness loop was
    affected — worth checking before describing a count bug as user-visible paging breakage.

Verification: see #690's closing record — same PR, same suites, same mutation proofs. Playout-
specific: the test seeds a playout whose channel row is absent to make the Channel != null clause
observable at all, which is only possible because the test harness disables FKs; that is labelled in
the test as the corruption-only case it is, not presented as a reachable state.

Deferred: #832 (MediaCards: 1-based paging, delete-or-keep, and the include-chain gap).

Docs updated: docs/decisions/records/api/paged-count-matches-page-query.md (new),
docs/decisions/README.md (regenerated), docs/api-conventions.md, docs/README.md.

## Closing record **Outcome:** Fixed in PR #833, bundled with #690 — box 3 ("checked against #690 for a shared idiom / shared fix") resolved as *yes, and it is the same fix*. `GetPagedPlayoutsHandler` now derives both its count and its page from one `IQueryable`. The issue's premise was right on both counts: pre-existing server bug, and #757 exposed rather than caused it. **Root cause:** The count was `dbContext.Playouts.CountAsync(ct)` — the whole table — while the page applied both a `Channel != null` filter and the optional `Query`. One mechanism shared with #690 and four other handlers; the fix is structural so the two cannot drift again. **Correction to the issue's framing, on evidence:** the `Channel != null` half is DEFENSIVE, not a live defect. `Playout.ChannelId` is non-nullable with `DeleteBehavior.Cascade` and both production connection strings set `foreign keys=true`, so an orphaned playout is unreachable while the FK holds — the pre-fix and post-fix counts are equal on every real database. The `Query` half is the live bug, and it is reachable exactly as described: `ersatztv_list_playouts` declaring `query` (#757) is what made it visible from the MCP surface. **Decisions/conventions changed:** added `api.paged-count-matches-page-query` (see #690's record for the full statement). **Reusable knowledge:** 1. **A paged handler's filter is not only the search string.** The clause this handler was missing from its count was a structural one applied unconditionally, so no search query was needed to expose the discrepancy in principle. Enumerate every clause on the page query, not just the `if (query)` one. 2. **Scope harm to the callers that exist.** Of the six handlers fixed, only three (rerun-collections, multi-collections, playouts) reach a controller; the other three have no production caller and were latent. The SPA reads this total in two places (`DashboardScreen`, `PlayoutsScreen`) via a single un-looped request, so no completeness loop was affected — worth checking before describing a count bug as user-visible paging breakage. **Verification:** see #690's closing record — same PR, same suites, same mutation proofs. Playout- specific: the test seeds a playout whose channel row is absent to make the `Channel != null` clause observable at all, which is only possible because the test harness disables FKs; that is labelled in the test as the corruption-only case it is, not presented as a reachable state. **Deferred:** #832 (MediaCards: 1-based paging, delete-or-keep, and the include-chain gap). **Docs updated:** `docs/decisions/records/api/paged-count-matches-page-query.md` (new), `docs/decisions/README.md` (regenerated), `docs/api-conventions.md`, `docs/README.md`.
timothy removed the in-progress label 2026-08-26 09:40:10 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: timothy/ersatztv#758