Skip to content

[T-083b] SSE endpoint /api/v1/events with Last-Event-ID resume, pings and ≤ 2 s latency test #156

Closed

Description

@CodewithJha

Task T-083b · Milestone M7 — API · Size S · Priority P0 · Epic #7

Implements FR-API-002 · FR-DSH-002

Branch feat/t-083b-sse-endpoint-with-last-event — one task = one branch = one PR (doc 32 §6)

Summary

Implement api/routers/events.py using sse-starlette: event names/payloads of doc 12 §4, id: = outbox id, Last-Event-ID/last_event_id resume from the events table, : ping every api.sse.ping_interval_s.

Context / motivation

Live console updates (FR-DSH-002: ≤ 2 s latency).

Scope

In scope

  • Latency test: insert event → received by client < 2 s (p95 over 50 events).

Implementation guidance

  • Router api/routers/events.py: GET /api/v1/events?campaign_id=&last_event_id= returning sse_starlette.EventSourceResponse (doc 12 §4).
  • Resume: the Last-Event-ID header takes precedence over the last_event_id query param; subscribe first, then replay the backlog from the events table, de-duplicating by id, so there is no gap between backlog and live.
  • Each message: id: <events.id>, event: <type>, data: <compact JSON>; ping comment every api.sse.ping_interval_s (15 s); retry: hint from api.sse.retry_ms (new).
  • Headers: Cache-Control: no-cache, X-Accel-Buffering: no (nginx, RB-06).
  • Auth via T-080c (?token= accepted here only); detect disconnects via request.is_disconnected() to release subscriptions.
  • 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.sse.ping_interval_s
  • api.sse.retry_ms (new)

Acceptance criteria

  • Resume replays missed events exactly once.
  • Latency ≤ 2 s.
  • Event payload shapes match the doc 12 §4 table (schema test per event type).
  • Client disconnect releases its subscription (gauge returns to 0).
  • p95 latency ≤ 2 s over 50 events (FR-DSH-002).
  • 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/integration/api/test_sse.py.

Dependencies

Blocked by: #155 (T-083a), #144 (T-080c)

Blocks: #158 (T-084b)

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/routers/events.py

Independent file.

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-083b (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 §4 · doc 14 §6 · doc 30 §RB-06 · ADR-0011

Requirements: FR-API-002 · FR-DSH-002 (traceability: doc 28)

External:

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:apiREST + SSE API (FastAPI)milestone:M7M7 — APIpriority:P0Critical path / blocks otherssize:S<= 1 daytaskChild task of a milestone epictype:featureNew functionality

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions