Carry pagination metadata on Ruby's lazy enumerators, cap with max_items

Merged
JE
4mo ago
+1097-388

Closes the last Ruby pagination parity gaps — waivers 2C.2 (X-Total-Count exposed), 2C.4 (maxItems early-stop), and 2C.6 (truncation metadata). Every other SDK ships all three; Ruby's lazy Enumerator could express none of them.

Design: lazy-preserving, non-breaking

The Enumerator is Ruby's public pagination API (32 of 47 services return one), so laziness had to survive. Paginators now return a ListEnumerator — an Enumerator subclass carrying ListMeta (total_count, truncated) — built by one shared core behind paginate/paginate_key/paginate_wrapped:

  • Eager first page (precedent: paginate_wrapped already fetched page 1 eagerly): meta.total_count (X-Total-Count, 0 when absent) is available the moment the list call returns, and page-1 errors surface from the call itself rather than first iteration.
  • Lazy tail: pages beyond the first are fetched only as iteration demands them — .first(n), .take(n), .next/.peek, and .lazy all work unchanged and never over-fetch (pinned by WebMock request-count tests).
  • max_items: kwarg on every generated list method (33 service files, uniformly additive), threaded into the base paginators. Non-positive values disable the cap at every paginator entry, matching TS (maxItems > 0), Swift (n > 0), and Python (#540).
  • Truncation contract (same as #540's Python): truncated is true only when items were dropped by max_items or a next Link was left unfetched (at max_items or the max_pages safety cap). Landing exactly on the final item of the last page is not truncation.

Metadata visibility semantics

meta.total_count is final at call time (eager page 1). meta.truncated starts false and is final only once enumeration completes — the lazy adaptation of the eager SDKs' post-collection metadata. The conformance runner consumes-then-asserts accordingly, and this is documented in ListMeta, the README, and SPEC Appendix F.

Hook-timing consequence

With page 1 fetched inside wrap_paginated's yield, on_operation_start now fires eagerly at call time (the pattern wrap_paginated_wrapped already used), and both hook wrappers re-wrap as ListEnumerator sharing the inner meta so metadata survives the hook layer. on_operation_end still fires when iteration completes, errors, or is cut short. The hook pins in operation_hooks_test.rb are updated to the new timing.

Red proofs

Conformance, five skips removed + runner wired, against the pristine SDK:

  FAIL: Total count header is accessible
        Expected responseMeta.totalCount = 42, got nil
  FAIL: Pagination stops at maxPages safety cap
        Expected responseMeta.truncated = true, got nil
  FAIL: Missing X-Total-Count returns zero
        Expected responseMeta.totalCount = 0, got nil
  FAIL: maxItems caps results across pages
        Expected >= 2 requests (SDK auto-paginates), got 0; Expected no error, got: ArgumentError: unknown keyword: :max_items; Expected responseMeta.truncated = true, got nil
  FAIL: maxItems landing exactly on the final item is not truncated
        Expected >= 2 requests (SDK auto-paginates), got 0; Expected no error, got: ArgumentError: unknown keyword: :max_items; Expected responseMeta.truncated = false, got nil
Results: 106 passed, 5 failed, 13 skipped
REAL_EXIT=2

New unit tests (ruby/test/basecamp/pagination_meta_test.rb, 31 runs) against the pristine SDK: 22 red —

12 × ArgumentError: unknown keyword: :max_items
 5 × NoMethodError: undefined method 'meta' for an instance of Enumerator
 4 × NameError: uninitialized constant Basecamp::ListEnumerator
 1 × Failure: test_first_page_fetched_eagerly — expected 1 request, got 0
31 runs, 1 failures, 21 errors
REAL_EXIT=1

The 9 green-on-pristine tests are the labeled laziness/iteration pins (.first(n) fetches exactly one page, .next/.peek, .lazy, block form, wrapped {key => Enumerator} shape), which must hold before and after.

Green

  • make conformance-ruby: Results: 111 passed, 0 failed, 13 skipped (REAL_EXIT=0; remaining skips are the rostered 2B.3 retry family + DownloadURL pair)
  • make rb-check (drift + tests + rubocop): 979 runs, 2204 assertions, 0 failures, 0 errors, 0 skips, 143 files inspected, no offenses detected, No drift detected. (REAL_EXIT=0)

Governance (same PR)

  • SPEC §19: exactly the five Ruby pagination roster lines deleted.
  • rubric-audit.json: 2C.2/2C.4/2C.6 flipped to plain passes with obsolete-waiver notes (3C.6 style).
  • SPEC §5 Known-Gaps bullet and Appendix F Ruby pagination row (no/no → yes/yes, truncated final after enumeration).
  • ruby/README.md pagination section: metadata, max_items, and the truncation contract, mirroring the Python docs from #540.

No wire-format change: max_items is a client-side collection cap, never sent as a query parameter. Additive keyword — zero breakage for existing callers.


Summary by cubic

Adds pagination metadata and a client-side item cap to Ruby’s paginators via a lazy ListEnumerator with ListMeta. Page 1 is fetched at call time for meta.total_count; re-enumeration now refetches all pages and refreshes metadata for a fresh snapshot.

  • New Features

    • Return ListEnumerator (Enumerator subclass) with .meta (ListMeta): total_count from X-Total-Count (0 when absent); truncated finalized after enumeration.
    • Eager first page; lazy tail with no over-fetch for .first/.take/.lazy.
    • Add max_items: to all list methods; non-positive disables; client-side only.
    • Hooks: on_operation_start at call time; metadata preserved through wrappers.
    • Update runner, docs, and SPEC: Ruby conformance passes; remove waivers 2C.2/2C.4/2C.6.
  • Bug Fixes

    • Re-enumeration refetches all pages and refreshes .meta; hooks fire once per traversal (start at call, end on first completion).
    • Truncation is recorded before the capping yield so .first/.take with max_items correctly set meta.truncated.