Repository navigation
Description
Task
T-080d· Milestone M7 — API · Size S · Priority P1 · Epic #7Implements 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 migration0002_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) -> stranddecode_cursor(s) -> Cursor; cursor = base64url(JSON) +.+ truncated HMAC-SHA256 tag usingapi.cursor_secret(new, SecretStr, auto-generated per process if unset — cursors then become invalid after a restart, acceptable for a local tool). Tampered cursor → 422VALIDATION_ERROR. - Keyset query helper for SQLAlchemy:
apply_keyset(stmt, sort_cols, cursor, limit)producingWHERE (sort_key, id) < (:k, :id)backed by a matching composite index (doc 11 §3 indexes). - Limits:
limitdefaultapi.page_limit_default(50), maxapi.page_limit_max(500); values above max are clamped (not an error) and documented in OpenAPI. - ETag (
api/concurrency.py): weak ETagW/"<version>"from the row'sversioninteger (addversioncolumns in the same migration if absent).PATCHwithoutIf-Matchor with a stale one → 412PRECONDITION_FAILED(doc 12 §1.1) with a detail saying which. - Idempotency (
api/idempotency.py): tableidempotency_keys(key, method, path, body_sha256, status, response_json, created_at); same key + different body → 422; TTLapi.idempotency_ttl_s; purge in the orchestrator maintenance loop (append-only hook). - Migration
0002_idempotency_keysis 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_defaultapi.page_limit_maxapi.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
Unreleasedupdated 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.pybackend/src/roboprobe/api/concurrency.pybackend/src/roboprobe/api/idempotency.pybackend/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
Labels
Projects
- StatusShow more project fieldsBacklog
Activity
/meta,/metrics, request logging #142CampaignCreatevalidation #147