feat(api): Stamp a session's trigger origin, add liveness filters and last message - #5767
feat(api): Stamp a session's trigger origin, add liveness filters and last message#5767ardaerzin wants to merge 1 commit into
Conversation
…expose its last message Sessions started by an automation carry origin and trigger references, and the list hides them by default (origin filter). The list also filters by liveness and an explicit id set, and each session can report its last message for previews.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe PR expands session queries with filtering and total counts, adds batched latest-message previews, and links trigger deliveries to session IDs and origin metadata. It also wires the required services and adds unit coverage for these paths. ChangesSession infrastructure
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)Session query flowsequenceDiagram
participant Client
participant SessionsRootRouter
participant SessionsService
participant SessionStreamsService
participant RecordsService
Client->>SessionsRootRouter: Submit session query
SessionsRootRouter->>SessionsService: Build and execute SessionQuery
SessionsService->>SessionStreamsService: Query filtered streams
SessionsService->>RecordsService: Fetch latest message previews
SessionsRootRouter-->>Client: Return sessions and optional total
Trigger session attributionsequenceDiagram
participant TriggerBroker
participant TriggersDispatcher
participant Workflow
participant SessionStreamsService
TriggerBroker->>TriggersDispatcher: Dispatch trigger
TriggersDispatcher->>TriggersDispatcher: Generate session ID
TriggersDispatcher->>Workflow: Send session ID in request
TriggersDispatcher->>SessionStreamsService: Stamp trigger origin
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Railway Preview Environment
|
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (2)
api/oss/tests/pytest/unit/sessions/test_session_trigger_stamp.py (1)
68-139: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd a dispatcher-level attribution test.
These tests call
SessionStreamsService.set_origindirectly. They do not test the new dispatcher contract.Add a test that invokes
TriggersDispatcher._runand verifies that one generatedsession_idis present in bothTriggerDeliveryDataandWorkflowServiceRequest. Also verify the stamped trigger ID, name, and subscription or schedule kind.api/oss/tests/pytest/unit/sessions/test_query_sessions_filters.py (1)
83-148: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd origin tag filter coverage.
These tests do not verify
tagsorexclude_tags. Add one test for tag containment and one test that asserts excluded tags generate thetags IS NULL OR NOT tags @> ...predicate. This protects the required behavior for historic sessions with NULL tags.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: f4e5037d-cada-4a8a-ab44-a9f5f2828d5a
📒 Files selected for processing (22)
api/entrypoints/routers.pyapi/entrypoints/worker_queues.pyapi/oss/src/apis/fastapi/sessions/models.pyapi/oss/src/apis/fastapi/sessions/router.pyapi/oss/src/core/sessions/dtos.pyapi/oss/src/core/sessions/records/dtos.pyapi/oss/src/core/sessions/records/interfaces.pyapi/oss/src/core/sessions/records/service.pyapi/oss/src/core/sessions/service.pyapi/oss/src/core/sessions/streams/dtos.pyapi/oss/src/core/sessions/streams/interfaces.pyapi/oss/src/core/sessions/streams/service.pyapi/oss/src/core/triggers/dtos.pyapi/oss/src/dbs/postgres/sessions/records/dao.pyapi/oss/src/dbs/postgres/sessions/streams/dao.pyapi/oss/src/tasks/asyncio/triggers/dispatcher.pyapi/oss/tests/pytest/unit/sessions/test_query_sessions_filters.pyapi/oss/tests/pytest/unit/sessions/test_query_sessions_references.pyapi/oss/tests/pytest/unit/sessions/test_query_sessions_search.pyapi/oss/tests/pytest/unit/sessions/test_session_last_message.pyapi/oss/tests/pytest/unit/sessions/test_session_trigger_stamp.pyapi/oss/tests/pytest/unit/sessions/test_sessions_root_service.py
| async def latest_message_per_session( | ||
| self, | ||
| *, | ||
| project_id: UUID, | ||
| session_ids: List[str], | ||
| ) -> Dict[str, SessionMessagePreview]: | ||
| """One batched lookup for a whole page — never one call per row.""" | ||
| if not session_ids: | ||
| return {} | ||
|
|
||
| return await self.records_dao.latest_message_per_session( | ||
| project_id=project_id, | ||
| session_ids=session_ids, | ||
| ) |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | 🏗️ Heavy lift
Return a named DTO from latest_message_per_session.
RecordsService.latest_message_per_session returns a raw Dict. Add a Pydantic container DTO in api/oss/src/core/sessions/records/dtos.py, return that DTO from this method, and update SessionsService to consume it.
As per coding guidelines, service methods must return typed DTOs and must not return raw dicts.
Source: Coding guidelines
There was a problem hiding this comment.
The return is already fully typed: Dict[str, SessionMessagePreview], where SessionMessagePreview is a Pydantic DTO from records/dtos.py. A container DTO wrapping a keyed map adds a layer without adding type safety.
There was a problem hiding this comment.
@ardaerzin, SessionMessagePreview provides type safety for each map value. However, latest_message_per_session still returns a raw Python dict as its service result.
The coding guideline applies to the service return container, not only to its values. A small Pydantic DTO that contains the keyed previews would satisfy that contract. I will keep this finding open.
You are interacting with an AI system.
Context
The sessions UX rework needs three things the sessions API did not provide: knowing which sessions an automation started (so the list can hide them by default and show them as a mode), filtering the list by liveness or an explicit id set (for the "waiting" pushdown), and a per-session last message for previews.
Changes
origin: "trigger"and a reference to the trigger that started them. Queue-dispatched triggers stamp the same way.Tests / notes
test_session_trigger_stamp,test_session_last_message,test_query_sessions_filters; updated root-service, search and references tests.feat/mobile-parity-and-consolidation, notmain: the changes build on that branch's session liveness and records work, and the patch does not apply onmain(two of the test files do not exist there).