-
Notifications
You must be signed in to change notification settings - Fork 165
refactor(BA-3687): consolidate admin_repository into repository for session domain #7868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR consolidates the AdminSessionRepository into the main SessionRepository by removing the separate admin repository class and moving role-based ownership validation logic from the repository layer to the service layer. This follows the same architectural pattern established in the Image domain consolidation (PR #7860).
Key changes:
- Removed the entire
AdminSessionRepositoryclass and file - Added
get_session_by_id_for_status()method toSessionRepositorythat fetches sessions without ownership checks - Updated
SessionService.check_and_transit_status()to handle role-based authorization directly in the service layer - Added comprehensive test coverage for all user roles (SUPERADMIN, ADMIN, USER) with authorization scenarios
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/ai/backend/manager/repositories/session/admin_repository.py | Removed entire admin-specific repository class (76 lines deleted) |
| src/ai/backend/manager/repositories/session/repository.py | Added get_session_by_id_for_status() method for ownership-agnostic session fetching |
| src/ai/backend/manager/repositories/session/repositories.py | Removed admin_repository instantiation and references |
| src/ai/backend/manager/services/session/service.py | Refactored check_and_transit_status() to handle role-based authorization in service layer; removed admin_session_repository dependency |
| src/ai/backend/manager/services/processors.py | Removed admin_session_repository from SessionService initialization |
| src/ai/backend/manager/services/session/README.md | Updated documentation to reflect single repository pattern with service-layer access control |
| tests/unit/manager/services/session/test_session_service.py | Added comprehensive test class with 5 test cases covering all role-based authorization scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @session_repository_resilience.apply() | ||
| async def get_session_by_id_for_status( | ||
| self, | ||
| session_id: SessionId, | ||
| ) -> SessionRow: | ||
| """ | ||
| Get session by ID for status determination without ownership checks. | ||
| Used by service layer which handles ownership validation based on user role. | ||
| """ | ||
| async with self._db.begin_readonly_session() as db_sess: | ||
| query_stmt = sa.select(SessionRow).where(SessionRow.id == session_id) | ||
| session_row = await db_sess.scalar(query_stmt) | ||
| if session_row is None: | ||
| raise SessionNotFound(f"Session not found (id:{session_id})") | ||
| return session_row |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new method get_session_by_id_for_status doesn't load the session's kernels relationship, but the service layer calls session_row.to_dataclass() which requires kernels to be loaded (it accesses self.main_kernel.service_ports). This will cause a lazy loading error or fail at runtime.
The method should include .options(selectinload(SessionRow.kernels)) in the query, similar to the existing get_session_to_determine_status method on line 1163 of the SessionRow model.
| log.warning( | ||
| f"You are not allowed to transit others's sessions status, skip (s:{session_id})" |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a grammatical error in this log message. The possessive form should use an apostrophe before the 's'.
…ository - Remove AdminSessionRepository class and file - Add get_session_by_id_for_status() to SessionRepository (replaces get_session_to_determine_status_force) - Move ownership check logic from repository to service layer - Update SessionService.check_and_transit_status() to handle role-based authorization - Update SessionServiceArgs and SessionRepositories to remove admin_repository - Add comprehensive tests for check_and_transit_status with all user roles - Update README.md to reflect the simplified repository structure 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
Remove "(service handles authorization)" from comment as it's already obvious within the session service context. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Replace hardcoded UUIDs with uuid4() generated random UUIDs in session service tests for better test isolation. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Role-based access control is not part of the session service layer. Updated the SessionRepository description to accurately reflect its purpose. Co-Authored-By: Claude Opus 4.5 <[email protected]>
0ec5762 to
58cdd25
Compare
Summary
AdminSessionRepositoryclass and its fileget_session_by_id_for_status()toSessionRepository(replacesget_session_to_determine_status_force)SessionService.check_and_transit_status()to handle role-based authorizationcheck_and_transit_statuswith all user roles (SUPERADMIN, ADMIN, USER)Related Issue
Changes
AdminSessionRepository, addedget_session_by_id_for_status()toSessionRepositorySessionServicenow handles ownership validation based on user roleTestCheckAndTransitStatusclass with 5 test cases covering all permission scenariosTest plan
pants lint)pants check)pants test tests/unit/manager/services/session/test_session_service.py)🤖 Generated with Claude Code