Skip to content

Refactor db/agents.py - Split into focused modules #102

Closed

Description

@vybe

Summary

The src/backend/db/agents.py file has grown to ~1000 lines with 12+ distinct functional areas mixed into a single AgentOperations class. This violates single responsibility principle and makes the codebase harder to navigate and maintain.

Current State

The file contains these unrelated concerns in one class:

Section Lines Purpose
Ownership Management 42-113 Owner registration, access control
Sharing Management 165-296 Share/unshare agents by email
API Key Settings 298-324 Platform API key toggle
Autonomy Mode 326-362 Scheduled task autonomy
Resource Limits 364-414 Memory/CPU limits
Full Capabilities 416-461 Container security settings
Read-Only Mode 463-516 Filesystem protection
Parallel Capacity 518-580 Max concurrent tasks
Execution Timeout 582-629 Task timeout settings
Batch Metadata 631-667 N+1 query optimization
Agent Rename 669-849 Cross-table rename
Avatar Identity 851-993 Avatar prompts and defaults

Proposed Refactor

Split into focused modules under src/backend/db/agent_settings/:

src/backend/db/
├── agents.py              # Core ownership + access control only
└── agent_settings/
    ├── __init__.py        # Re-export for backwards compatibility
    ├── sharing.py         # Agent sharing operations
    ├── resources.py       # Memory, CPU, timeout, parallel capacity
    ├── security.py        # Full capabilities, read-only mode
    ├── autonomy.py        # Autonomy mode settings
    ├── avatar.py          # Avatar identity management
    └── metadata.py        # Batch queries, rename operations

Acceptance Criteria

  • Each module handles a single concern
  • AgentOperations class remains as facade for backwards compatibility
  • All existing tests pass without modification
  • No API changes - purely internal refactor
  • Each new module has clear docstrings

Technical Notes

  • The _user_ops dependency injection pattern should be preserved
  • Database connection pattern (get_db_connection()) is consistent across all methods
  • Some methods have cross-cutting concerns (e.g., get_all_agent_metadata joins multiple tables)

Priority

P1 - Important for maintainability but not blocking any features.

Activity

  1. added
    priority-p1
    type-refactor
    on Mar 12, 2026
  2. vybe commented on Mar 13, 2026

    @vybe
    ContributorAuthor

    Implemented in this session. Split db/agents.py (~1000 lines) into 6 focused mixin modules under db/agent_settings/. AgentOperations remains as facade class with identical method signatures for full backwards compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority-p1type-refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions