Skip to content

Conversation

@MinJuTur
Copy link
Contributor

@MinJuTur MinJuTur commented Jan 8, 2026

Summary

  • Remove AdminSessionRepository class and its file
  • Add get_session_by_id_for_status() to SessionRepository (replaces get_session_to_determine_status_force)
  • Move ownership check logic from repository layer to service layer
  • Update SessionService.check_and_transit_status() to handle role-based authorization
  • Add comprehensive tests for check_and_transit_status with all user roles (SUPERADMIN, ADMIN, USER)

Related Issue

Changes

  • Repository Layer: Removed AdminSessionRepository, added get_session_by_id_for_status() to SessionRepository
  • Service Layer: SessionService now handles ownership validation based on user role
  • Tests: Added TestCheckAndTransitStatus class with 5 test cases covering all permission scenarios

Test plan

  • Lint passed (pants lint)
  • Type check passed (pants check)
  • Unit tests passed (pants test tests/unit/manager/services/session/test_session_service.py)
  • Pre-commit hooks passed

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings January 8, 2026 05:51
@github-actions github-actions bot added size:L 100~500 LoC comp:manager Related to Manager component labels Jan 8, 2026
@MinJuTur MinJuTur changed the title refactor(BA-3687): consolidate AdminSessionRepository into SessionRepository refactor(BA-3687): consolidate admin_repository into repository for session domain Jan 8, 2026
Copy link
Contributor

Copilot AI left a 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 AdminSessionRepository class and file
  • Added get_session_by_id_for_status() method to SessionRepository that 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.

Comment on lines +110 to +124
@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
Copy link

Copilot AI Jan 8, 2026

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.

Copilot uses AI. Check for mistakes.
Comment on lines 1408 to 1423
log.warning(
f"You are not allowed to transit others's sessions status, skip (s:{session_id})"
Copy link

Copilot AI Jan 8, 2026

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'.

Copilot uses AI. Check for mistakes.
@MinJuTur MinJuTur requested a review from jopemachine January 9, 2026 08:10
MinJuTur and others added 5 commits January 9, 2026 09:20
…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]>
@MinJuTur MinJuTur force-pushed the refactor/BA-3687-session branch from 0ec5762 to 58cdd25 Compare January 9, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:manager Related to Manager component size:L 100~500 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants