Skip to content

refactor: decompose init.ts action handler into phase functions #139

@dean0x

Description

@dean0x

Problem

The init.ts action handler (src/cli/commands/init.ts:112-631) is 520+ lines with 10+ responsibilities:

  • Version detection
  • Scope determination (user/local)
  • Interactive prompts (TTY handling)
  • Plugin discovery and installation
  • Registry updates
  • Hook installation (memory, ambient, etc.)
  • Working memory bootstrap
  • Error handling and user feedback

Scope

This file is monolithic and violates single-responsibility principle. Extracting just 2-3 small blocks is insufficient. A proper refactor requires:

  1. Phase functions - Decompose into ~5-7 phase handlers:

    • detectVersion() - Package version loading
    • determineScope() - User/local scope selection
    • selectPlugins() - Interactive plugin selection
    • installPlugins() - Plugin installation orchestration
    • setupHooks() - Memory and ambient hook installation
    • bootstrapMemory() - Working memory initialization
    • concludeInstallation() - Summary and next steps
  2. Helper utilities - Extract reusable patterns:

    • Registry update logic
    • File system operations
    • Error recovery and logging
  3. Type safety - Define phase-to-phase contracts with Result types

Risk Assessment

  • Risk Level: HIGH
  • Why: The file is working and well-tested. Any refactor touches core installation logic
  • Mitigation: Comprehensive test coverage already exists; refactor can be validated via existing test suite

Testing Strategy

  • Keep all existing tests passing (no behavior changes)
  • Add unit tests for new phase functions
  • Integration test full installation flow end-to-end

Acceptance Criteria

  • Handler decomposed into phase functions
  • Phase functions are <100 lines each
  • All existing tests pass
  • New tests cover phase boundaries
  • Line count reduced from 520 to ~300-350 in main handler

Priority

Low - works well currently. Defer to post-v1.0.0 dedicated refactoring effort.

Metadata

Metadata

Assignees

No one assigned

    Labels

    tech-debtTechnical debt items to address

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions