Skip to content

refactor: migrate to Zeitwerk autoloading#76

Open
htcarr3 wants to merge 1 commit intomainfrom
zeitwerk-032126
Open

refactor: migrate to Zeitwerk autoloading#76
htcarr3 wants to merge 1 commit intomainfrom
zeitwerk-032126

Conversation

@htcarr3
Copy link
Contributor

@htcarr3 htcarr3 commented Mar 23, 2026

What

Migrate from 48 manual require_relative calls to Zeitwerk autoloading, enforcing one-class-per-file organization across the gem.

Why

Zeitwerk eliminates brittle load-order dependencies, enables lazy loading, and enforces consistent file-to-constant naming conventions. This makes the codebase easier to navigate and prevents the class of bugs where a missing or misordered require breaks loading.

How

  • Added zeitwerk ~> 2.7 runtime dependency
  • Split ~15 multi-class files into ~50 individual one-class-per-file files
  • Configured loader.collapse for content_blocks/, messages/, types/, hooks/ (classes stay in ClaudeAgent:: namespace)
  • Configured inflections for acronyms: MCP, CLI, JSON, SDK, API
  • Converted query.rb from module reopening to ClaudeAgent::Query module
  • Moved class methods from logging.rb and v2_session.rb into the entry point
  • Added include Message directly in each message/content block class (replaces iteration over MESSAGE_TYPES/CONTENT_BLOCK_TYPES)
  • Deleted umbrella files (content_blocks.rb, messages.rb, types.rb, errors.rb, permissions.rb)
  • Removed all require_relative from internal files (kept stdlib require for json, securerandom, open3)

Test plan

  • 1186 unit tests pass (0 failures, 0 errors)
  • RuboCop: 202 files, no offenses
  • RBS signatures parse
  • ClaudeAgent.loader.eager_load succeeds (24 message types, 8 content block types)

Replace 48 manual require_relative calls with Zeitwerk autoloader.
This enforces one-class-per-file organization, enables lazy loading,
and eliminates brittle load-order dependencies.

Key changes:
- Split multi-class files into individual files (~50 new, ~20 deleted)
- Collapse content_blocks/, messages/, types/, hooks/ directories
- Configure inflections for MCP, CLI, JSON, SDK, API acronyms
- Convert query.rb to ClaudeAgent::Query module
- Move class methods from logging.rb/v2_session.rb to entry point
- Add include Message directly in each message/content block class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant