Skip to content

Recipe data-paginated-list: cover search, and rewrite the deferrals that hide traps - #6

Open
adrianstevens wants to merge 1 commit into
mainfrom
adrian/recipe-search
Open

adrianstevens wants to merge 1 commit into
mainfrom
adrian/recipe-search

Conversation

@adrianstevens

Copy link
Copy Markdown
Contributor

Why

The recipe taught pagination well and listed filtering under Does NOT cover:

Filtering and search — paginating a filtered subset | Independent concern; the cursor encoding extends naturally to carry the filter, but multi-axis pagination has its own design space

That deferral is the problem. A reader — human or agent — does not become cautious at it; the gap gets filled from priors, and the prior is a client-side .filter() over the fetched pages, because that is what generic React material teaches. Three first-party apps shipped exactly that. In each one the search could only find matches inside the page the client happened to hold, with no error and no empty state to show for it.

The recipe also recommended the shape that failed. It closed by suggesting an IntersectionObserver-driven infinite scroll without noting that filtering the accumulated pages shrinks the rendered list, which can lift the sentinel out of the viewport, so the observer never fires and searching prevents its own results from loading.

What changed

Search is part of the recipe now, not deferred out of it.

  • ListRecordsRequest gains search, applied server-side in the same SQL as the seek, so paging walks the matching rows rather than walking every row and discarding non-matches.
  • The cursor payload carries the term it was issued under, and the server restarts from the first page when the term differs. This is the payoff of the existing opaque-cursor decision: the payload gained a field and no client had to change.
  • The client debounces at 300ms, resets pagination on a term change, and filters nothing.
  • The first page of any query now replaces rather than appends. That makes it idempotent, which let the useRef StrictMode latch be deleted outright — designing the operation to be repeatable beats latching around a repeat.
  • The test driver passes search through so the harness can assert the filtered case.

Second change, structural. Does NOT cover is split in two: one table for concerns that genuinely live elsewhere and carry no hazard, and one for extensions where the obvious approach is silently wrong — multi-axis filtering, ties in the sort column, user-chosen sorting — each with the trap named and the working shape beside it. Previously both kinds looked identical on the page, which is how a warning came to read as permission. Other recipes likely have the same issue; not swept here.

Verification

9/9 against the compiled service with a real sqlite database seeded as db.ts seeds it. The discriminating case is built into the fixture: page one is ids 100..81, and "Record 5" matches ids 5 and 50..59, so every match sits outside page one and a client-side filter over it returns nothing.

PASS  unfiltered page one is ids 100..81
PASS  no "Record 5" match on page one
PASS  server-side search reaches matches beyond page one — 11 rows
PASS  filtered paging walks matches, not rows — 59,58,57,56,55 then 54,53,52,51,50
PASS  a cursor from another query restarts instead of seeking — got 100
PASS  search is case-insensitive
PASS  no-match term returns an exhausted empty page

What is NOT yet proven

The justification for this change is that it alters what an agent writes when asked to add a search box. That has not been tested. The intended check is an A/B — the same task against this branch and against main, graded on whether the term reaches the server — with a control arm, because the model may well get it right unaided and this would be taking credit for its priors. Adrian is running that against other systems separately.

If it turns out agents get this right without the recipe, the code change still stands on its own (the recipe demonstrably lacked a common case) but the Does NOT cover restructure is the part that would need rethinking.

Note on comment density

This recipe is teaching material and its established style is dense explanatory comments — that is the product here, and the additions match it. Not the standard applied to app code.

…hat hide traps

The recipe taught pagination well and listed "filtering and search" under
"Does NOT cover" as an independent concern with its own design space. That
deferral is the problem. An agent reading it does not become careful; it fills
the gap from its priors, and the prior is a client-side .filter() over the
fetched pages, because that is what generic React material teaches. Three
first-party apps shipped exactly that — Suggestions, RaidPlanner and BlogCraft
— and in each one the search could only ever find matches inside the page the
client happened to hold, with no error and no empty state to show for it.

The recipe also pointed at the shape that failed: it closed by suggesting an
IntersectionObserver-driven infinite scroll, without noting that filtering the
accumulated pages shrinks the list, which can lift the sentinel out of the
viewport, so searching prevents its own results from loading. That is
RaidPlanner's bug, recommended.

So search is now part of the recipe rather than deferred out of it:

- ListRecordsRequest gains `search`, applied server-side in the same SQL as
  the seek, so paging walks the matching rows instead of walking every row and
  discarding non-matches.
- The cursor payload carries the term it was issued under, and the server
  restarts from the first page when the term differs. This is the payoff of
  the existing opaque-cursor design: the payload gained a field and no client
  had to change.
- The client debounces (300ms), resets pagination on a term change, and does
  no filtering. The first page of any query now REPLACES rather than appends,
  which makes it idempotent and removes the useRef StrictMode latch entirely —
  designing the operation to be repeatable beats latching around a repeat.
- The test driver passes `search` through so the harness can assert the
  filtered case, which is the one that cannot be checked by eye.

Second change, structural: "Does NOT cover" is split in two. One table for
concerns that genuinely live elsewhere and carry no hazard, and one for
extensions where the obvious approach is silently wrong — multi-axis
filtering, ties in the sort column, user-chosen sorting — each with the trap
named and the working shape beside it. The two used to be the same shape on
the page, which is how a warning came to read as permission.

Verified against the compiled service with a real sqlite database seeded as
db.ts seeds it, 9/9. The discriminating case is built in: page one is ids
100..81, "Record 5" matches ids 5 and 50..59, so every match is outside page
one and a client-side filter over it would return nothing. Also asserted that
filtered paging walks matches (59..55, then 54..50) and that a cursor from
another query restarts instead of seeking.
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