Repository navigation
Description
Task
T-083a· Milestone M7 — API · Size S · Priority P0 · Epic #7Implements FR-API-002
Branch
feat/t-083a-outbox-listener-in-process-event— one task = one branch = one PR (doc 32 §6)
Summary
Implement api/sse.py EventHub: one dedicated async psycopg connection LISTEN roboprobe_events; on notify (or every poll interval) read events.id > last_id; fan-out to subscriber queues filtered by campaign_id; bounded queues with drop-oldest + resync event.
Context / motivation
Outbox recovery: a missed NOTIFY is recovered by polling (doc 11 §4) — guarantees ordered delivery.
Scope
In scope
- Lifespan-managed start/stop in the app factory.
Implementation guidance
roboprobe/api/sse.py:class EventHub: async def start(self) -> None; async def stop(self) -> None; def subscribe(self, campaign_id: UUID | None, last_event_id: int | None) -> Subscription, whereSubscriptionis an async iterator ofOutboxEvent(id, type, payload, campaign_id).- One dedicated psycopg 3
AsyncConnectionwithautocommit=True;LISTEN {database.notify_channel}; on each notification or everyapi.sse.poll_interval_s(new, default 2 s) runSELECT … FROM events WHERE id > :last ORDER BY id LIMIT :batch. NOTIFY is only a wake-up hint (doc 11 §4); the table is the source of truth, which guarantees order and no loss. - Fan-out to per-subscriber
asyncio.Queue(maxsize=api.sse.queue_size)(new); on overflow drop that subscriber's oldest events and enqueue a syntheticresyncevent so the client refetches (doc 13 §4). - Reconnect the LISTEN connection with exponential backoff (cap 30 s); expose
roboprobe_sse_clientsgauge androboprobe_sse_reconnects_totalcounter (new metrics). - 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).
database.notify_channel- api.sse.poll_interval_s (new)
- api.sse.queue_size (new)
Acceptance criteria
- Integration: events emitted in order; killing the LISTEN connection still delivers via polling.
- No duplicate or out-of-order events across a forced LISTEN reconnect (integration test).
- A slow subscriber does not block others (test with one non-consuming subscriber).
- 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/integration/api/test_event_hub.py.
Dependencies
Blocked by: #63 (T-011c), #142 (T-080a)
Blocks: #156 (T-083b)
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/sse.py
Independent.
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-083a (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 11 §4 · doc 14 §6 · ADR-0011
Requirements: FR-API-002 (traceability: doc 28)
External:
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsBacklog
Activity
/meta,/metrics, request logging #142/api/v1/eventswith Last-Event-ID resume, pings and ≤ 2 s latency test #156