Skip to content

[T-080d] API cross-cutting: cursor pagination, ETag/If-Match concurrency, Idempotency-Key store #145

Closed

Description

@CodewithJha

Task T-080d · Milestone M7 — API · Size S · Priority P1 · Epic #7

Implements FR-API-001 · NFR-PERF-004

Branch feat/t-080d-api-cross-cutting-cursor-pagination — one task = one branch = one PR (doc 32 §6)

Summary

Implement reusable helpers in api/pagination.py (opaque keyset cursors, {items, next_cursor}, limit default/max from settings), api/concurrency.py (ETag from version/updated_at; If-Match required on PATCH → 412/409) and api/idempotency.py (Idempotency-Key stored 24 h).

Context / motivation

Doc 12 §1 conventions; keyset pagination is what keeps list endpoints < 200 ms p95 at 10 000 runs.

Scope

In scope

  • Idempotency store table or reuse events? → new small table via migration 0002_idempotency_keys (expand-only).

Implementation guidance

  • Pagination (api/pagination.py): class Page(BaseModel, Generic[T]): items: list[T]; next_cursor: str | None; encode_cursor(sort_values: tuple, id: UUID) -> str and decode_cursor(s) -> Cursor; cursor = base64url(JSON) + . + truncated HMAC-SHA256 tag using api.cursor_secret (new, SecretStr, auto-generated per process if unset — cursors then become invalid after a restart, acceptable for a local tool). Tampered cursor → 422 VALIDATION_ERROR.
  • Keyset query helper for SQLAlchemy: apply_keyset(stmt, sort_cols, cursor, limit) producing WHERE (sort_key, id) < (:k, :id) backed by a matching composite index (doc 11 §3 indexes).
  • Limits: limit default api.page_limit_default (50), max api.page_limit_max (500); values above max are clamped (not an error) and documented in OpenAPI.
  • ETag (api/concurrency.py): weak ETag W/"<version>" from the row's version integer (add version columns in the same migration if absent). PATCH without If-Match or with a stale one → 412 PRECONDITION_FAILED (doc 12 §1.1) with a detail saying which.
  • Idempotency (api/idempotency.py): table idempotency_keys(key, method, path, body_sha256, status, response_json, created_at); same key + different body → 422; TTL api.idempotency_ttl_s; purge in the orchestrator maintenance loop (append-only hook).
  • Migration 0002_idempotency_keys is expand-only (doc 33 §5) with an up/down/up test.
  • Standards: respect the layering contracts (doc 16 §2) and conventions in doc 34; no hard-coded tunables — thresholds/defaults come from typed settings (doc 27); no magic numbers (ruff PLR2004); public APIs typed (mypy strict) and documented.

Configuration keys

From the configuration reference (doc 27). Keys marked (new) must be added to the typed settings models and appear in the generated reference (T-003c).

  • api.page_limit_default
  • api.page_limit_max
  • api.idempotency_ttl_s
  • api.cursor_secret (new)

Acceptance criteria

  • Pagination stable and complete across pages; limit capped.
  • PATCH without If-Match → 428/412 per doc; stale ETag → 412.
  • Replayed POST with same key returns the original response.
  • Keyset pagination over 1 000 rows returns every row exactly once in order (property test).
  • Same Idempotency-Key with a different body → 422.
  • Migration up/down/up green.
  • Tests listed below added and green in CI; coverage of new code meets the gate in doc 18 §3.
  • Docs, config reference and CHANGELOG Unreleased updated where behaviour, keys or interfaces changed.
  • Definition of Done (doc 32 §3) satisfied.

Tests required

  • tests/api/test_pagination.py, test_concurrency.py, test_idempotency.py.

Dependencies

Blocked by: #142 (T-080a)

Blocks: #147 (T-081b)

Parallelization notes

Files / paths owned by this task — avoid editing other paths; shared files (pyproject.toml entry points, cli/main.py, api/routers/__init__.py, router.tsx, docker/compose.yaml, ci.yml) are append-only:

  • backend/src/roboprobe/api/pagination.py
  • backend/src/roboprobe/api/concurrency.py
  • backend/src/roboprobe/api/idempotency.py
  • backend/migrations/versions/0002_idempotency_keys.py

Independent; routers adopt helpers.

Definition of Done

Per doc 32 §3 and AGENTS.md §5: lint, format, mypy strict and import-linter green; tests added and passing; docs updated; PR template completed with Closes #<this issue>; Conventional Commit messages referencing T-080d (doc 16 §6); commits authored only by the repository owner identity (AGENTS.md §4); CI green; reviewer checklist (doc 32 §5.1) satisfied.

References

Design docs: doc 12 §1 · doc 12 §7 · doc 33 §5 · doc 11 §3

Requirements: FR-API-001 · NFR-PERF-004 (traceability: doc 28)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:apiREST + SSE API (FastAPI)milestone:M7M7 — APIpriority:P1Required for milestone exitsize:S<= 1 daytaskChild task of a milestone epictype:featureNew functionality

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions