Skip to content

refactor: codebase optimization - module decomposition, test dedup, type safety#28

Merged
dimakis merged 7 commits intorefactor/large-file-decompositionfrom
refactor/codebase-optimization
Feb 14, 2026
Merged

refactor: codebase optimization - module decomposition, test dedup, type safety#28
dimakis merged 7 commits intorefactor/large-file-decompositionfrom
refactor/codebase-optimization

Conversation

@dimakis
Copy link
Owner

@dimakis dimakis commented Feb 14, 2026

Summary

  • Extract BaseAgent/AgentContext to agents/base.py and merge LibrarianAgent into agents/librarian.py; slim agents/__init__.py from 502 to ~50 lines of pure re-exports
  • Extract tool registry logic (get_all_tools, get_architect_tools) to tools/registry.py; slim tools/__init__.py from 212 to ~100 lines
  • Create shared tests/helpers/auth.py eliminating ~360 lines of duplicated auth setup across 12 test files
  • Fix dangerous asyncio.sleep(10) in timeout tests with asyncio.Event().wait() pattern
  • Fix 5 mypy errors in new registry.py, remove 5 modules from mypy strict override list
  • Resolve circular import chain in agent sub-modules

Motivation

Architecture review identified bloated __init__.py files with substantial logic, duplicated test helpers, unsafe test patterns, and mypy debt. This PR addresses the highest-impact structural issues.

Type of Change

  • refactor — Code restructuring (no feature or fix)
  • test — Adding or updating tests
  • fix — Bug fix (circular import, asyncio.sleep safety)

Changes

  • agents/init.py: Moved BaseAgent (431 lines) and AgentContext to canonical agents/base.py. Added LibrarianAgent to existing agents/librarian.py. Updated 5 agent sub-modules to import from agents.base directly, breaking circular import chain.
  • tools/init.py: Moved get_all_tools() and get_architect_tools() (with caching) to tools/registry.py. Fixed mypy type annotations (list -> list[Any]). Fixed attr-defined errors by importing from canonical source modules.
  • tests/helpers/auth.py: New shared module with make_test_settings() and make_test_jwt(). Updated 12 test files to use shared helpers.
  • test safety: Replaced asyncio.sleep(10) with asyncio.Event().wait() in 2 timeout test files.
  • pyproject.toml: Removed 5 modules from mypy override list (settings, llm, tracing.mlflow now pass strict).

Testing

  • Unit tests added/updated
  • All static checks pass (ruff lint, ruff format, mypy strict, bandit)
  • Full unit test suite passes

Security Considerations

  • No security implications

Checklist

  • My code follows the project's style (ruff, mypy pass)
  • I've written tests that prove my fix/feature works
  • I've added conventional commit messages
  • Coverage has not decreased
  • CI passed locally

Made with Cursor

dimakis and others added 7 commits February 14, 2026 15:11
Move BaseAgent and AgentContext to agents/base.py (canonical location).
Add LibrarianAgent to agents/librarian.py alongside LibrarianWorkflow.
Slim agents/__init__.py from 502 to ~50 lines of pure re-exports.
Update test patch targets from src.agents -> src.agents.base.

Co-authored-by: Cursor <cursoragent@cursor.com>
Move get_all_tools() and get_architect_tools() with caching logic to
tools/registry.py. Slim tools/__init__.py from 212 to ~100 lines of
pure imports and re-exports.

Co-authored-by: Cursor <cursoragent@cursor.com>
Create tests/helpers/auth.py with make_test_settings() and make_test_jwt()
to replace duplicated _make_settings/_make_jwt across 12 test files.
Eliminates ~360 lines of copy-pasted auth setup code.

Co-authored-by: Cursor <cursoragent@cursor.com>
Use asyncio.Event that is never set instead of asyncio.sleep(10) to
simulate slow tools. This avoids risking the 10s pytest timeout if
the timeout mechanism itself fails, while achieving the same blocking
behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>
…ride list

Add proper type annotations to registry.py (list -> list[Any]).
Import from canonical source modules to fix attr-defined errors.
Remove 5 modules from mypy overrides that now pass strict checking
(settings, llm, tracing.mlflow). 0 mypy errors across 271 files.

Co-authored-by: Cursor <cursoragent@cursor.com>
Auto-fix ruff I001 import sorting across 7 files.
Add list_automations and render_template to tools/__init__.py __all__.

Co-authored-by: Cursor <cursoragent@cursor.com>
Update 5 agent sub-modules to import BaseAgent from src.agents.base
instead of src.agents (the package __init__). This breaks the circular
dependency chain: __init__ imports sub-modules, sub-modules imported
from __init__.

Co-authored-by: Cursor <cursoragent@cursor.com>
@dimakis dimakis merged commit 939790c into refactor/large-file-decomposition Feb 14, 2026
1 check passed
@dimakis dimakis deleted the refactor/codebase-optimization branch February 14, 2026 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant