Getting Started Architecture Overview - fumihumi/phantom GitHub Wiki

Architecture Overview

This document provides a high-level overview of Phantom's system architecture and design principles.

System Architecture

graph TB
    subgraph "User Interface"
        CLI[CLI Commands]
    end
    
    subgraph "CLI Layer"
        Handlers[Command Handlers]
        Output[Output Formatter]
        Errors[Error Handler]
    end
    
    subgraph "Core Business Logic"
        Worktree[Worktree Operations]
        Git[Git Operations]
        Process[Process Execution]
        Paths[Path Management]
        Types[Type System]
    end
    
    subgraph "External Systems"
        FS[File System]
        GitCLI[Git CLI]
        Shell[System Shell]
    end
    
    CLI --> Handlers
    Handlers --> Output
    Handlers --> Errors
    Handlers --> Worktree
    Handlers --> Process
    
    Worktree --> Git
    Worktree --> Paths
    Worktree --> Types
    
    Process --> Types
    
    Git --> GitCLI
    Paths --> FS
    Process --> Shell
Loading

Design Principles

1. Layered Architecture

Source: CLAUDE.md#L26-L31

The architecture follows a clean layered approach:

  • CLI Layer: Handles user interaction, command parsing, and output formatting
  • Core Layer: Contains business logic, independent of CLI framework
  • External Layer: Interfaces with system resources (Git, file system, shell)

2. Separation of Concerns

Each module has a single, well-defined responsibility:

  • Command Handlers: Orchestrate operations, no business logic
  • Core Modules: Business logic, no CLI dependencies
  • Git Operations: Centralized Git command execution
  • Process Execution: Unified process spawning and management

3. Functional Error Handling

Source: src/core/types/result.ts

Uses a Result<T, E> type pattern for explicit error handling:

type Result<T, E = Error> = 
  | { ok: true; value: T }
  | { ok: false; error: E }

This enables:

  • Explicit error states
  • Type-safe error handling
  • No exceptions for expected errors
  • Composable error handling

Component Overview

CLI Components

Located in src/cli/:

  • handlers/: Command implementations

    • Each command has its own handler file
    • Handlers orchestrate core operations
    • Minimal business logic
  • output.ts: Centralized console output

    • Consistent formatting
    • Table rendering for lists
    • Error message formatting
  • errors.ts: CLI error handling

    • Exit codes management
    • User-friendly error messages

Core Components

Located in src/core/:

Worktree Module (worktree/)

Manages Git worktree operations:

  • create.ts: Creates new worktrees
  • delete.ts: Removes worktrees
  • list.ts: Lists existing worktrees
  • where.ts: Locates worktree paths
  • validate.ts: Validates worktree names and states

Git Module (git/)

Source: src/core/git/

Centralizes Git operations:

  • executor.ts: Main Git command executor
  • libs/: Specific Git operation wrappers
    • add-worktree.ts: Worktree creation
    • get-current-branch.ts: Branch detection
    • get-git-root.ts: Repository root finding

Process Module (process/)

Handles external process execution:

  • spawn.ts: Core process spawning logic
  • exec.ts: Command execution in worktrees
  • shell.ts: Interactive shell sessions

Supporting Modules

  • paths.ts: Centralized path management
  • version.ts: Version information
  • types/: Shared type definitions

Data Flow

Command Execution Flow

sequenceDiagram
    participant User
    participant CLI
    participant Handler
    participant Core
    participant Git
    participant FS
    
    User->>CLI: phantom create feature
    CLI->>Handler: ParsedCommand
    Handler->>Core: validateWorktreeName()
    Core-->>Handler: Result<void>
    
    Handler->>Core: createWorktree()
    Core->>Git: getBranch()
    Git-->>Core: currentBranch
    
    Core->>Git: addWorktree()
    Git->>FS: git worktree add
    FS-->>Git: success
    
    Git-->>Core: Result<WorktreeInfo>
    Core-->>Handler: Result<WorktreeInfo>
    Handler->>CLI: Format output
    CLI->>User: Success message
Loading

Error Handling Flow

graph LR
    A[Operation] --> B{Success?}
    B -->|Yes| C[Return Result.ok]
    B -->|No| D[Create Error]
    D --> E[Return Result.error]
    E --> F[Handler checks result]
    F --> G[Format error message]
    G --> H[Exit with code]
Loading

Key Architectural Decisions

1. Zero Runtime Dependencies

Source: package.json#L50

  • No external runtime dependencies
  • Faster installation and execution
  • Reduced security surface area
  • Simplified deployment

2. TypeScript with ESM

  • Modern ES modules throughout
  • Type safety at compile time
  • Better IDE support
  • Tree-shaking capable

3. Native Node.js Features

  • Uses Node.js built-in test runner
  • Native module mocking for tests
  • Built-in child process handling
  • No external test frameworks

4. Git CLI Integration

  • Delegates to Git CLI for operations
  • No Git library dependencies
  • Ensures compatibility with Git versions
  • Leverages Git's battle-tested code

Module Dependencies

graph TD
    CLI[CLI Layer]
    Core[Core Layer]
    Git[Git Operations]
    Process[Process Utils]
    Types[Type Definitions]
    
    CLI --> Core
    CLI --> Types
    Core --> Git
    Core --> Process
    Core --> Types
    Git --> Types
    Process --> Types
    
    style CLI fill:#f9f,stroke:#333,stroke-width:2px
    style Core fill:#9ff,stroke:#333,stroke-width:2px
    style Git fill:#ff9,stroke:#333,stroke-width:2px
Loading

Key rules:

  • CLI depends on Core (never reverse)
  • Core modules are independent of CLI
  • Shared types in types/ module
  • No circular dependencies

Security Considerations

Input Validation

  • All user inputs are validated
  • Worktree names are sanitized
  • Path traversal prevention
  • Command injection protection

Process Execution

  • Commands are properly escaped
  • Shell execution is explicit
  • Environment variables are controlled
  • No eval or dynamic code execution

Performance Characteristics

Startup Time

  • Minimal dependencies = fast startup
  • Direct execution without runtime overhead
  • Bundled with esbuild for optimization

Memory Usage

  • Lightweight process footprint
  • No persistent daemons
  • Garbage collected after execution

Scalability

  • Stateless operation
  • Can manage many worktrees
  • Parallel operation support
  • Linear complexity for most operations

Future Architecture Considerations

The architecture is designed to support:

  1. Plugin System: Modular extensions
  2. Configuration: User preferences
  3. Remote Operations: Network worktree support
  4. Performance Monitoring: Metrics collection
  5. Advanced Git Features: Submodules, sparse checkout

The clean separation of concerns and modular design make these enhancements possible without major refactoring.

⚠️ **GitHub.com Fallback** ⚠️