Skip to content

Conversation

@HyeockJinKim
Copy link
Collaborator

Remove all symbol re-exports from models/init.py to enforce direct
subpackage imports. This prevents circular import issues and improves
code organization.

Changes:

  • models/init.py: Remove all re-exports, keep only documentation
  • Update all imports across codebase to use direct subpackage imports
    (e.g., from ai.backend.manager.models.session import SessionRow)
  • Fix repository tests: Add ScalingGroupRow and ResourcePresetRow to
    with_tables for SQLAlchemy mapper initialization

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.5 [email protected]

resolves #NNN (BA-MMM)

Checklist: (if applicable)

  • Milestone metadata specifying the target backport version
  • Mention to the original issue
  • Installer updates including:
    • Fixtures for db schema changes
    • New mandatory config options
  • Update of end-to-end CLI integration tests in ai.backend.test
  • API server-client counterparts (e.g., manager API -> client SDK)
  • Test case(s) to:
    • Demonstrate the difference of before/after
    • Demonstrate the flow of abstract/conceptual models with a concrete implementation
  • Documentation
    • Contents in the docs directory
    • docstrings in public interfaces and type annotations

Copilot AI review requested due to automatic review settings January 3, 2026 14:05
@github-actions github-actions bot added size:L 100~500 LoC comp:manager Related to Manager component comp:cli Related to CLI component require:db-migration Automatically set when alembic migrations are added or updated labels Jan 3, 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 pull request refactors the models package to remove all re-exports from models/__init__.py and enforce direct subpackage imports across the codebase. This change improves code organization and prevents circular import issues by requiring explicit imports (e.g., from ai.backend.manager.models.session import SessionRow instead of from ai.backend.manager.models import SessionRow).

Key Changes:

  • Removed all symbol re-exports from models/__init__.py, leaving only documentation about the new import pattern
  • Updated hundreds of import statements across the codebase to use direct subpackage paths
  • Added UserRoleRow, ScalingGroupRow, and ResourcePresetRow to test fixtures to ensure proper SQLAlchemy mapper initialization
  • Introduced ensure_all_tables_registered() function in models/base.py to dynamically import all model modules for metadata operations

Reviewed changes

Copilot reviewed 54 out of 54 changed files in this pull request and generated 24 comments.

Show a summary per file
File Description
src/ai/backend/manager/models/__init__.py Removed all re-exports and replaced with documentation about direct imports
src/ai/backend/manager/models/base.py Added ensure_all_tables_registered() function for dynamic model loading
src/ai/backend/manager/cli/dbschema.py Call ensure_all_tables_registered() before schema operations
src/ai/backend/manager/models/vfolder/row.py Updated imports to use direct subpackage paths
src/ai/backend/manager/models/alembic/versions/*.py Updated migration imports to direct paths
src/ai/backend/manager/api/*.py Updated 15+ API module imports to direct subpackage paths
src/ai/backend/manager/scheduler/*.py Updated scheduler module imports to direct paths
src/ai/backend/manager/sweeper/*.py Updated sweeper module imports to direct paths
src/ai/backend/manager/registry.py Refactored large import block with direct subpackage imports
src/ai/backend/manager/plugin/error_monitor.py Updated import to direct path
src/ai/backend/manager/utils.py Updated model imports to direct paths
src/ai/backend/manager/types.py Updated SessionRow import to direct path
tests/unit/manager/repositories/**/*.py Added UserRoleRow, ScalingGroupRow, and ResourcePresetRow to table fixtures across 20+ test files

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2216 to 2214
UserRoleRow, # UserRow relationship dependency
UserRow,
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "UserRow relationship dependency" is incorrect and the table ordering is wrong. UserRoleRow has foreign keys to both UserRow and RoleRow, so it must come AFTER these tables. The correct order should place UserRoleRow after UserRow. Additionally, RoleRow is missing from this table list but UserRoleRow references it, which may cause foreign key constraint errors.

Copilot uses AI. Check for mistakes.
Comment on lines 59 to 60
UserRoleRow, # UserRow relationship dependency
KeyPairRow,
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "UserRow relationship dependency" is incorrect and the table ordering is wrong. UserRoleRow has foreign keys to both UserRow and RoleRow, so it must come AFTER these tables. The correct order should place UserRoleRow after UserRow. Additionally, RoleRow is missing from this table list but UserRoleRow references it, which may cause foreign key constraint errors.

Copilot uses AI. Check for mistakes.
UserRow,
KeyPairRow,
ScalingGroupRow,
ResourcePresetRow,
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comment explaining the table dependency. For consistency with other test files, consider adding a comment such as "# ResourcePresetRow has FK to ScalingGroupRow".

Copilot uses AI. Check for mistakes.
UserRoleRow, # UserRow relationship dependency
UserRow,
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "UserRow relationship dependency" is incorrect and the table ordering is wrong. UserRoleRow has foreign keys to both UserRow and RoleRow, so it must come AFTER these tables. The correct order should place UserRoleRow after UserRow. Additionally, RoleRow is missing from this table list but UserRoleRow references it, which may cause foreign key constraint errors.

Copilot uses AI. Check for mistakes.
Comment on lines +1831 to 1829
UserRoleRow, # UserRow relationship dependency
UserRow,
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "UserRow relationship dependency" is incorrect and the table ordering is wrong. UserRoleRow has foreign keys to both UserRow and RoleRow, so it must come AFTER these tables. The correct order should place UserRoleRow after UserRow. Additionally, RoleRow is missing from this table list but UserRoleRow references it, which may cause foreign key constraint errors.

Copilot uses AI. Check for mistakes.
Comment on lines 59 to 60
UserRoleRow, # UserRow relationship dependency
UserRow,
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "UserRow relationship dependency" is incorrect and the table ordering is wrong. UserRoleRow has foreign keys to both UserRow and RoleRow, so it must come AFTER these tables. The correct order should place UserRoleRow after UserRow. Additionally, RoleRow is missing from this table list but UserRoleRow references it, which may cause foreign key constraint errors.

Copilot uses AI. Check for mistakes.
Comment on lines 22 to 30
from ..models.domain import MAXIMUM_DOTFILE_SIZE, verify_dotfile_name
from ..models.group import (
association_groups_users as agus,
)
from ..models.group import (
groups,
query_group_domain,
query_group_dotfiles,
verify_dotfile_name,
)
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant imports from the same module. The imports from ..models.group on lines 23-25 and 26-30 should be consolidated into a single import statement for better readability and consistency.

Copilot uses AI. Check for mistakes.
Comment on lines 67 to 68
UserRoleRow, # UserRow relationship dependency
UserRow,
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "UserRow relationship dependency" is incorrect and the table ordering is wrong. UserRoleRow has foreign keys to both UserRow and RoleRow, so it must come AFTER these tables. The correct order should place UserRoleRow after UserRow. Additionally, RoleRow is missing from this table list but UserRoleRow references it, which may cause foreign key constraint errors.

Copilot uses AI. Check for mistakes.
Comment on lines 72 to 73
UserRoleRow, # UserRow relationship dependency
UserRow,
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "UserRow relationship dependency" is incorrect and the table ordering is wrong. UserRoleRow has foreign keys to both UserRow and RoleRow, so it must come AFTER these tables. The correct order should place UserRoleRow after UserRow. Additionally, RoleRow is missing from this table list but UserRoleRow references it, which may cause foreign key constraint errors.

Copilot uses AI. Check for mistakes.
Comment on lines 60 to 61
UserRoleRow, # UserRow relationship dependency
UserRow,
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "UserRow relationship dependency" is incorrect and the table ordering is wrong. UserRoleRow has foreign keys to both UserRow and RoleRow, so it must come AFTER these tables. The correct order should place UserRoleRow after UserRow. Additionally, RoleRow is missing from this table list but UserRoleRow references it, which may cause foreign key constraint errors.

Copilot uses AI. Check for mistakes.
@HyeockJinKim HyeockJinKim changed the title refactor(BA-3644): Remove re-exports from models/__init__.py for import isolation refactor(BA-3644): Models package import isolation and test fixture consolidation Jan 4, 2026
@HyeockJinKim HyeockJinKim added this pull request to the merge queue Jan 4, 2026
@HyeockJinKim HyeockJinKim removed this pull request from the merge queue due to a manual request Jan 4, 2026
HyeockJinKim and others added 5 commits January 5, 2026 10:45
…rt isolation

Remove all symbol re-exports from models/__init__.py to enforce direct
subpackage imports. This prevents circular import issues and improves
code organization.

Changes:
- models/__init__.py: Remove all re-exports, keep only documentation
- Update all imports across codebase to use direct subpackage imports
  (e.g., from ai.backend.manager.models.session import SessionRow)
- Fix repository tests: Add ScalingGroupRow and ResourcePresetRow to
  with_tables for SQLAlchemy mapper initialization

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
…fixtures

- Fix update_route_traffic_force to load all relations (model, image, session_owner)
  to prevent MissingGreenlet errors during lazy loading
- Refactor test fixtures to return IDs instead of Row objects
- Delete duplicate mock-based test files causing mapper initialization errors:
  - test_create_auto_scaling_rule_validated.py
  - test_create_endpoint_validated.py
  - test_get_endpoint_by_id_validated.py
  - test_get_endpoint_by_name_validated.py
  - test_list_endpoints_by_owner_validated.py
  - test_update_auto_scaling_rule_validated.py
  - test_update_endpoint_lifecycle_validated.py
- Fix AsyncGenerator type annotation in test_registry.py fixture
- Delete obsolete integration tests and scheduler test utilities
- Reorganize model tests to proper locations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
…manager:src deps

- Move PasswordColumn from user/row.py to hasher/types.py
  - Breaks base.py → user/__init__.py → user/row.py → 33 Row chain
  - base.py now has 0 Row dependencies

- Remove unused SessionGetter Protocol from manager/types.py
  - Eliminates TYPE_CHECKING import of SessionRow
  - manager/types.py now has 0 Row dependencies

- Remove explicit manager:src dependencies from repository test BUILD files
  - Allows pants to infer only required dependencies
  - Repository tests now have 0 transitive Row dependencies

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
…kage structure

- Updated import paths in `auth.py`, `cluster_template.py`, `domainconfig.py`, `events.py`, `groupconfig.py`, `logs.py`, `manager.py`, `scaling_group.py`, `service.py`, `session.py`, `session_template.py`, `stream.py`, `userconfig.py`, `utils.py`, `vfolder.py`, `dbschema.py`, `types.py`, `hasher/types.py`, `user/row.py`, `vfolder/row.py`, `plugin/error_monitor.py`, `repositories/model_serving/admin_repository.py`, `scheduler/agent_selector.py`, `scheduler/dispatcher.py`, `scheduler/drf.py`, `scheduler/fifo.py`, `scheduler/predicates.py`, `scheduler/types.py`, `sweeper/kernel.py`, `sweeper/session.py`, `tests/unit/manager/conftest.py`, and `tests/unit/manager/repositories/user/test_user_repository.py`.
- Removed unnecessary relative imports and replaced them with absolute imports for clarity and maintainability.
- Adjusted regex patterns in test cases for better matching of exception messages.
@HyeockJinKim HyeockJinKim force-pushed the feat/separate-import-models branch from 6df9d74 to e48d2f1 Compare January 5, 2026 03:37
@HyeockJinKim HyeockJinKim enabled auto-merge January 5, 2026 03:38
@github-actions github-actions bot added the area:docs Documentations label Jan 5, 2026
@HyeockJinKim HyeockJinKim added this pull request to the merge queue Jan 5, 2026
Merged via the queue into main with commit 3416e49 Jan 5, 2026
31 checks passed
@HyeockJinKim HyeockJinKim deleted the feat/separate-import-models branch January 5, 2026 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs Documentations comp:cli Related to CLI component comp:manager Related to Manager component require:db-migration Automatically set when alembic migrations are added or updated size:L 100~500 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants