Architecture - aku11i/phantom GitHub Wiki

Architecture

This document provides a comprehensive overview of Phantom's architecture, including technology stack, module structure, and design principles.

Table of Contents

System Overview

Phantom is a modern CLI tool built with TypeScript and Node.js, designed for managing Git worktrees efficiently. The architecture emphasizes modularity, minimal dependencies, and clear separation of concerns.

Key Architectural Features

  • Monorepo Design: 7 focused packages with clear boundaries
  • Minimal Dependencies: Core packages have zero runtime dependencies
  • Modular Integration: Optional features via separate packages
  • Type Safety: Full TypeScript coverage with strict mode
  • Modern Node.js: Leverages latest Node.js features (v22+)

Technology Stack

Core Technologies

TypeScript

  • Version: 5.8.3
  • Target: ES2023
  • Module System: ES Modules (ESM)
  • Features: Strict type checking, native execution via --experimental-strip-types

Node.js

  • Minimum Version: v22.0.0
  • Features Used:
    • Native ES modules
    • Built-in test runner
    • Native module mocking
    • Child process management

Runtime Dependencies

Core Packages (Zero Dependencies):

  • @aku11i/phantom-cli
  • @aku11i/phantom-core
  • @aku11i/phantom-git
  • @aku11i/phantom-process
  • @aku11i/phantom-shared

Integration Packages (Minimal Dependencies):

  • @aku11i/phantom-github: @octokit/rest, zod
  • @aku11i/phantom-mcp: @modelcontextprotocol/sdk, zod

Build & Development Tools

esbuild

  • Fast bundling for CLI distribution
  • ES modules output targeting Node.js 22
  • Source map support for debugging

Biome

  • Version: 1.9.4
  • Combined linting and formatting (replaces ESLint + Prettier)
  • 10-20x faster than traditional tools
  • Written in Rust for performance

pnpm

  • Version: 10.8.1+
  • Monorepo workspace management
  • Efficient dependency storage
  • Strict dependency resolution

Monorepo Structure

phantom/
├── packages/              # 7 focused packages
│   ├── cli/              # CLI interface (@aku11i/phantom-cli)
│   ├── core/             # Business logic (@aku11i/phantom-core)
│   ├── git/              # Git operations (@aku11i/phantom-git)
│   ├── github/           # GitHub integration (@aku11i/phantom-github)
│   ├── mcp/              # MCP server (@aku11i/phantom-mcp)
│   ├── process/          # Process utilities (@aku11i/phantom-process)
│   └── shared/           # Shared utilities (@aku11i/phantom-shared)
├── package.json          # Root configuration
├── pnpm-workspace.yaml   # Workspace setup
└── pnpm-lock.yaml        # Dependency lock

Module Dependencies

graph TB
    subgraph "CLI Layer"
        cli["@aku11i/phantom-cli<br/>Command Handlers"]
    end
    
    subgraph "Integration Layer"
        github["@aku11i/phantom-github<br/>GitHub API"]
        mcp["@aku11i/phantom-mcp<br/>AI Assistants"]
    end
    
    subgraph "Core Layer"
        core["@aku11i/phantom-core<br/>Business Logic"]
        git["@aku11i/phantom-git<br/>Git Operations"]
        process["@aku11i/phantom-process<br/>System Processes"]
    end
    
    subgraph "Foundation"
        shared["@aku11i/phantom-shared<br/>Common Utilities"]
    end
    
    cli --> github
    cli --> mcp
    cli --> core
    cli --> git
    cli --> process
    cli --> shared
    
    github --> core
    github --> git
    github --> process
    github --> shared
    
    mcp --> github
    mcp --> core
    mcp --> git
    mcp --> process
    mcp --> shared
    
    core --> git
    core --> process
    core --> shared
    
    git --> shared
    process --> shared
    
    classDef cli fill:#ff9,stroke:#333,stroke-width:2px
    classDef integration fill:#9ff,stroke:#333,stroke-width:2px
    classDef core fill:#9f9,stroke:#333,stroke-width:2px
    classDef foundation fill:#f9f,stroke:#333,stroke-width:2px
    
    class cli cli
    class github,mcp integration
    class core,git,process core
    class shared foundation
Loading

Design Principles

1. Single Responsibility

Each package has one clear, focused purpose:

  • CLI: User interface and command routing
  • Core: Phantom lifecycle and configuration
  • Git: Git command abstraction
  • GitHub: GitHub API integration
  • MCP: AI assistant tools
  • Process: System process management
  • Shared: Common utilities and types

2. Dependency Direction

Dependencies flow from higher-level packages to lower-level ones:

  • CLI packages depend on everything below
  • Integration packages depend on core packages
  • Core packages depend only on shared utilities
  • Shared utilities have no dependencies

3. Zero Runtime Dependencies (Core)

Core functionality maintains zero runtime dependencies:

  • Faster installation
  • Smaller package size
  • Better security posture
  • Reduced dependency conflicts

4. Optional Integration

Advanced features are in separate packages:

  • GitHub integration is optional
  • MCP server is optional
  • Core functionality works standalone

Package Details

@aku11i/phantom-cli

Purpose: Command-line interface and user interaction

  • Entry points (bin/)
  • Command handlers (handlers/)
  • Help text and CLI logic
  • Dependencies: All other phantom packages

@aku11i/phantom-core

Purpose: Core business logic and phantom management

  • Worktree lifecycle management
  • Configuration system (phantom.config.json)
  • Validation and error handling
  • Dependencies: git, process, shared

@aku11i/phantom-git

Purpose: Git command abstraction and execution

  • Git executor with Result pattern
  • Command libraries (libs/)
  • Git operation wrappers
  • Dependencies: shared only

@aku11i/phantom-github

Purpose: GitHub integration via REST API

  • PR and issue checkout
  • GitHub API client
  • Authentication handling
  • Dependencies: core, git, process, shared, @octokit/rest, zod

@aku11i/phantom-mcp

Purpose: Model Context Protocol server for AI assistants

  • MCP server implementation
  • Tool definitions for AI assistants
  • Claude Desktop/Cursor integration
  • Dependencies: All phantom packages, @modelcontextprotocol/sdk, zod

@aku11i/phantom-process

Purpose: System process management and execution

  • Safe process spawning
  • Command execution utilities
  • Cross-platform compatibility
  • Dependencies: shared only

@aku11i/phantom-shared

Purpose: Common utilities and type definitions

  • Result type for error handling
  • Shared interfaces and types
  • Utility functions
  • Dependencies: None

Performance Characteristics

Startup Performance

  • Cold Start: ~50ms
  • Factors: Zero core dependencies, bundled executable, minimal initialization

Memory Usage

  • Base Memory: ~30MB
  • Scaling: Linear with phantom count
  • No Background Processes: Zero memory when idle

Build Performance

  • Full Build: <5 seconds (esbuild + TypeScript)
  • Type Check: ~2 seconds
  • Test Suite: ~1 second (native Node.js runner)

Package Installation

  • Core CLI: Minimal dependencies for fast install
  • With Integrations: Additional dependencies loaded on demand
  • Development: Full monorepo setup with pnpm workspaces

Architectural Benefits

Maintainability

  • Clear package boundaries prevent coupling
  • Single responsibility makes reasoning easier
  • Type safety prevents runtime errors
  • Comprehensive test coverage

Performance

  • Zero-dependency core for fast startup
  • Modern build tools for quick iteration
  • Native Node.js features for efficiency
  • Minimal overhead design

Extensibility

  • Monorepo structure supports new packages
  • Clear interfaces for integration points
  • Optional feature packages
  • Plugin-ready architecture

Developer Experience

  • Native TypeScript execution in development
  • Fast feedback loops with modern tooling
  • Comprehensive error messages
  • Self-documenting code through types

This architecture enables Phantom to remain lightweight and fast while providing powerful features through optional integrations, making it suitable for both simple and complex development workflows.

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