Project 33: Multi-Repository Refactoring Agent
Project 33: Multi-Repository Refactoring Agent
Build an agent that coordinates refactoring across multiple repositories - renaming interfaces, updating imports, and ensuring consistency across your entire organizationโs codebase with proper dependency ordering and atomic changes.
Quick Reference
| Attribute | Value |
|---|---|
| Difficulty | Expert (Level 4) |
| Time Estimate | 1 month+ |
| Primary Language | TypeScript |
| Alternative Languages | Python, Go |
| Prerequisites | Projects 7, 10, 14, 25 (GitHub MCP, Subagents, Checkpoints, Code Review) |
| Key Topics | Dependency graphs, parallel processing, atomic changes, PR coordination, rollback strategies |
| Kiro CLI Concepts | Subagent orchestration, GitHub MCP, checkpoints, hooks, parallel execution |
| Main Book Reference | โMonorepo Developmentโ by Ben Awad; โPro Gitโ by Scott Chacon |
1. Learning Objectives
By completing this project, you will:
- Master cross-repository dependency mapping: Build dependency graphs across multiple repos and understand topological ordering
- Implement subagent orchestration patterns: Coordinate parallel agents working on different repositories with proper synchronization
- Design atomic multi-repo changes: Create change sets that can be applied or rolled back as a unit across repositories
- Build sophisticated PR coordination: Create linked pull requests with proper dependencies and merge ordering
- Implement enterprise-grade rollback: Handle failures gracefully with checkpoint-based recovery across distributed systems
2. Deep Theoretical Foundation
2.1 The Multi-Repository Challenge
Large organizations often have polyrepo architectures where code is spread across many repositories. Making consistent changes across these repos is one of software engineeringโs hardest problems.
THE POLYREPO CHALLENGE
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ ORGANIZATION CODEBASE โ
โ โโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ @org/core-types โ โ @org/user-api โ โ @org/web-client โ โ
โ โ โ โ โ โ โ โ
โ โ interface โ โ import { โ โ import { โ โ
โ โ UserService { โโโโถโ UserService โโโโถโ UserService โ โ
โ โ ... โ โ } from โ โ } from โ โ
โ โ } โ โ '@org/core' โ โ '@org/user-api' โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ โ
โ โ โ โ โ
โ โโโโโโโโดโโโโโโโ โโโโโโโโดโโโโโโโ โโโโโโโโดโโโโโโโ โ
โ โ 3 files โ โ 7 files โ โ 15 files โ โ
โ โ 150 LOC โ โ 2,500 LOC โ โ 8,000 LOC โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ
โ THE PROBLEM: Rename UserService โ AccountService โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ Manual approach: โ
โ 1. Update @org/core-types (wait for CI) โ
โ 2. Publish new version (wait for npm) โ
โ 3. Update @org/user-api to use new version (wait for CI) โ
โ 4. Publish @org/user-api (wait for npm) โ
โ 5. Update @org/web-client (wait for CI) โ
โ 6. Repeat for ALL dependent repos... โ
โ โ
โ Time: Days to weeks โ
โ Risk: Inconsistent state during migration โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.2 Dependency Graph Theory
Understanding dependency graphs is fundamental to multi-repo refactoring. From โAlgorithmsโ (Sedgewick, Ch. 4):
DEPENDENCY GRAPH FUNDAMENTALS
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ DIRECTED ACYCLIC GRAPH (DAG) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ @org/core-types (no dependencies - ROOT) โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ
โ โผ โผ โ
โ @org/user-api @org/auth-service โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ โ
โ โผ โผ โผ โ
โ @org/web-client @org/mobile-app @org/admin-dashboard โ
โ โ
โ โ
โ TOPOLOGICAL SORT: Processing order that respects dependencies โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ Level 0: @org/core-types (process first) โ
โ Level 1: @org/user-api, @org/auth-service โ
โ Level 2: @org/web-client, @org/mobile-app, @org/admin-dashboard โ
โ โ
โ Key insight: Repos at the same level can be processed in PARALLEL โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ CRITICAL PATH ANALYSIS โ
โ โโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ The critical path is the longest path through the dependency graph. โ
โ It determines the minimum time to complete the refactoring. โ
โ โ
โ Critical path: core-types โ user-api โ web-client โ
โ Length: 3 levels (cannot be parallelized) โ
โ โ
โ Non-critical: auth-service โ mobile-app (can parallel with above) โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.3 The Subagent Orchestration Pattern
For multi-repo work, a single agent is insufficient. We need coordinated subagents:
SUBAGENT ORCHESTRATION ARCHITECTURE
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ ORCHESTRATOR AGENT โ
โ โโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ
โ โ Responsibilities: โ โ
โ โ โข Parse user request โ โ
โ โ โข Build dependency graph โ โ
โ โ โข Determine processing order โ โ
โ โ โข Spawn and coordinate subagents โ โ
โ โ โข Aggregate results โ โ
โ โ โข Handle failures and rollback โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ โ
โ โผ โผ โผ โ
โ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โ
โ โ SUBAGENT 1 โ โ SUBAGENT 2 โ โ SUBAGENT 3 โ โ
โ โ โโโโโโโโโโโ โ โ โโโโโโโโโโโ โ โ โโโโโโโโโโโ โ โ
โ โ โ โ โ โ โ โ
โ โ Target: โ โ Target: โ โ Target: โ โ
โ โ core-types โ โ user-api โ โ auth-service โ โ
โ โ โ โ โ โ โ โ
โ โ Status: โ โ Status: โ โ Status: โ โ
โ โ โ Complete โ โ ~ Running โ โ โธ Waiting โ โ
โ โ โ โ โ โ โ โ
โ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โ
โ โ
โ SYNCHRONIZATION BARRIER โ
โ โโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ Level 0 complete โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโถ โ
โ Level 1 agents spawned โ
โ Level 1 complete โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโถ โ
โ Level 2 agents spawned โ
โ ... โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.4 Atomic Change Sets and Two-Phase Commit
To ensure consistency, changes must be applied atomically. This pattern comes from distributed database theory:
TWO-PHASE COMMIT FOR MULTI-REPO CHANGES
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ PHASE 1: PREPARE โ
โ โโโโโโโโโโโโโโโโ โ
โ โ
โ Orchestrator: "Prepare to rename UserService โ AccountService" โ
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ core-types โ โ user-api โ โ auth-serviceโ โ web-client โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ Create โ โ Create โ โ Create โ โ Create โ โ
โ โ branch โ โ branch โ โ branch โ โ branch โ โ
โ โ โ โ โ โ โ โ โ โ โ โ โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ Apply โ โ Apply โ โ Apply โ โ Apply โ โ
โ โ changes โ โ changes โ โ changes โ โ changes โ โ
โ โ โ โ โ โ โ โ โ โ โ โ โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ Run tests โ โ Run tests โ โ Run tests โ โ Run tests โ โ
โ โ โ โ โ โ โ โ โ โ โ โ FAIL โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ VOTE: YES โ โ VOTE: YES โ โ VOTE: YES โ โ VOTE: NO โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ
โ DECISION: One "NO" vote โ ABORT โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ PHASE 2A: ABORT (if any failures) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ Orchestrator: "Abort - delete all branches" โ
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ core-types โ โ user-api โ โ auth-serviceโ โ web-client โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ Delete โ โ Delete โ โ Delete โ โ Delete โ โ
โ โ branch โ โ branch โ โ branch โ โ branch โ โ
โ โ โ Rolled โ โ โ Rolled โ โ โ Rolled โ โ โ Rolled โ โ
โ โ back โ โ back โ โ back โ โ back โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ PHASE 2B: COMMIT (if all success) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ Orchestrator: "Commit - create PRs in dependency order" โ
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ core-types โ โ user-api โ โ auth-serviceโ โ web-client โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ Create PR โ โ Create PR โ โ Create PR โ โ Create PR โ โ
โ โ #101 โ โ #234 โ โ #567 โ โ #890 โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ Deps: none โ โ Deps: #101 โ โ Deps: #101 โ โ Deps: #234 โ โ
โ โ โ โ โ โ โ โ Deps: #567 โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.5 Handling the Diamond Dependency Problem
A common challenge in multi-repo refactoring is the diamond dependency:
THE DIAMOND DEPENDENCY PROBLEM
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ @org/core-types โ
โ โ โ
โ โโโโโโโโโโโดโโโโโโโโโโ โ
โ โ โ โ
โ โผ โผ โ
โ @org/user-api @org/auth-service โ
โ โ โ โ
โ โโโโโโโโโโโฌโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ @org/web-client โ
โ (depends on BOTH) โ
โ โ
โ โ
โ PROBLEM: @org/web-client imports from both user-api and auth-service โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ During migration: โ
โ โข core-types renames UserService โ AccountService โ
โ โข user-api is updated and published โ
โ โข auth-service is NOT yet updated โ
โ โข web-client now sees CONFLICTING types! โ
โ - user-api exports AccountService โ
โ - auth-service still exports UserService โ
โ โ
โ SOLUTION: Ensure level-1 repos are updated ATOMICALLY โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ 1. Update core-types (don't publish yet) โ
โ 2. Update user-api AND auth-service in parallel โ
โ 3. Wait for BOTH to pass tests โ
โ 4. Merge core-types PR first โ
โ 5. Publish core-types โ
โ 6. Merge user-api and auth-service PRs โ
โ 7. Publish both โ
โ 8. Now web-client can be updated safely โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.6 GitHub MCP for PR Coordination
The GitHub MCP provides the interface for cross-repo PR management:
GITHUB MCP OPERATIONS
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ REPOSITORY ANALYSIS โ
โ โโโโโโโโโโโโโโโโโโโ โ
โ โ
โ gh repo list @org --json name,description,updatedAt โ
โ gh api repos/{owner}/{repo}/contents/package.json โ
โ โ Parse dependencies to build graph โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ BRANCH OPERATIONS โ
โ โโโโโโโโโโโโโโโโโ โ
โ โ
โ # Create feature branch across repos โ
โ gh api repos/{owner}/{repo}/git/refs \ โ
โ --method POST \ โ
โ --field ref="refs/heads/refactor/rename-user-service" \ โ
โ --field sha="{base_sha}" โ
โ โ
โ # Delete branch on rollback โ
โ gh api repos/{owner}/{repo}/git/refs/heads/{branch} --method DELETE โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ PULL REQUEST OPERATIONS โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ # Create PR with dependencies in description โ
โ gh pr create \ โ
โ --repo @org/user-api \ โ
โ --title "refactor: Rename UserService to AccountService" \ โ
โ --body "Depends on: org/core-types#101" \ โ
โ --label "refactoring" \ โ
โ --draft โ
โ โ
โ # Mark PR as ready when dependencies are merged โ
โ gh pr ready 234 --repo @org/user-api โ
โ โ
โ # Auto-merge when checks pass โ
โ gh pr merge 234 --auto --squash --repo @org/user-api โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ CI/CD COORDINATION โ
โ โโโโโโโโโโโโโโโโโโ โ
โ โ
โ # Watch for CI completion โ
โ gh run watch --repo @org/user-api โ
โ โ
โ # Get CI status โ
โ gh pr checks 234 --repo @org/user-api โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
3. Complete Project Specification
3.1 What You Will Build
A Kiro CLI agent that:
- Accepts a refactoring request (rename, move, update API)
- Analyzes all organization repositories for impact
- Builds dependency graph and determines processing order
- Spawns subagents to process repositories in parallel
- Creates coordinated PRs with proper dependencies
- Handles failures with atomic rollback
- Reports progress and final status
3.2 Functional Requirements
| Requirement | Description |
|---|---|
| Repository Discovery | Find all repos containing the target symbol |
| Dependency Analysis | Build cross-repo dependency graph |
| Parallel Processing | Process independent repos simultaneously |
| Change Application | Apply refactoring transformations |
| Test Validation | Run tests in each repo after changes |
| PR Creation | Create linked PRs with dependencies |
| Rollback Handling | Revert all changes on failure |
| Progress Reporting | Show real-time status of all repos |
3.3 Non-Functional Requirements
- Atomicity: All repos updated or none
- Consistency: No repo left in broken state
- Isolation: Parallel agents donโt interfere
- Durability: Checkpoints survive failures
4. Solution Architecture
4.1 System Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MULTI-REPO REFACTORING AGENT โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ CLI INTERFACE โ โ
โ โ โ โ
โ โ kiro-cli chat โ โ
โ โ > "Rename UserService to AccountService across all repos" โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ ORCHESTRATOR AGENT โ โ
โ โ โ โ
โ โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ โ
โ โ โ Repo Analyzer โ โ Graph Builder โ โ Subagent โ โ โ
โ โ โ โ โ โ โ Coordinator โ โ โ
โ โ โ โข List repos โ โ โข Parse deps โ โ โ โ โ
โ โ โ โข Find usages โ โ โข Topo sort โ โ โข Spawn agents โ โ โ
โ โ โ โข Count files โ โ โข Level assign โ โ โข Sync barrier โ โ โ
โ โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ โข Aggregate โ โ โ
โ โ โโโโโโโโโโโโโโโโโโ โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ SUBAGENT POOL โ โ
โ โ โ โ
โ โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ โ
โ โ โ Subagent โ โ Subagent โ โ Subagent โ โ โ
โ โ โ core-types โ โ user-api โ โ auth-service โ โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ โ Clone โ โ Clone โ โ Clone โ โ โ
โ โ โ Branch โ โ Branch โ โ Branch โ โ โ
โ โ โ Transform โ โ Transform โ โ Transform โ โ โ
โ โ โ Test โ โ Test โ โ Test โ โ โ
โ โ โ Report โ โ Report โ โ Report โ โ โ
โ โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ INTEGRATION LAYER โ โ
โ โ โ โ
โ โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ โ
โ โ โ GitHub MCP โ โ Checkpoint โ โ Test Runner โ โ โ
โ โ โ โ โ Manager โ โ Hook โ โ โ
โ โ โ โข PR ops โ โ โ โ โ โ โ
โ โ โ โข Branch ops โ โ โข Save state โ โ โข Run tests โ โ โ
โ โ โ โข CI status โ โ โข Restore โ โ โข Report โ โ โ
โ โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
4.2 Data Flow
REFACTORING DATA FLOW
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ USER REQUEST โ
โ "Rename UserService โ AccountService" โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ REPOSITORY SCAN โ โ
โ โ โ โ
โ โ Output: ImpactAnalysis โ โ
โ โ { โ โ
โ โ targetSymbol: "UserService", โ โ
โ โ affectedRepos: [ โ โ
โ โ { name: "core-types", files: 3, usages: 5 }, โ โ
โ โ { name: "user-api", files: 7, usages: 23 }, โ โ
โ โ { name: "auth-service", files: 4, usages: 12 }, โ โ
โ โ { name: "web-client", files: 12, usages: 45 } โ โ
โ โ ] โ โ
โ โ } โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ DEPENDENCY GRAPH โ โ
โ โ โ โ
โ โ Output: DependencyGraph โ โ
โ โ { โ โ
โ โ levels: [ โ โ
โ โ ["core-types"], โ โ
โ โ ["user-api", "auth-service"], โ โ
โ โ ["web-client"] โ โ
โ โ ], โ โ
โ โ edges: [ โ โ
โ โ { from: "core-types", to: "user-api" }, โ โ
โ โ { from: "core-types", to: "auth-service" }, โ โ
โ โ { from: "user-api", to: "web-client" }, โ โ
โ โ { from: "auth-service", to: "web-client" } โ โ
โ โ ] โ โ
โ โ } โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ PARALLEL EXECUTION โ โ
โ โ โ โ
โ โ Level 0: โ โ
โ โ โโโ Subagent: core-types โ Success โ โ
โ โ โโโ โ Barrier: Level 0 complete โ โ
โ โ โ โ
โ โ Level 1: โ โ
โ โ โโโ Subagent: user-api โ Success โ โ
โ โ โโโ Subagent: auth-service โ Success โ โ
โ โ โโโ โ Barrier: Level 1 complete โ โ
โ โ โ โ
โ โ Level 2: โ โ
โ โ โโโ Subagent: web-client โ Success โ โ
โ โ โโโ โ Barrier: Level 2 complete โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ PR CREATION โ โ
โ โ โ โ
โ โ Output: PRCoordination โ โ
โ โ { โ โ
โ โ prs: [ โ โ
โ โ { repo: "core-types", number: 101, deps: [] }, โ โ
โ โ { repo: "user-api", number: 234, deps: [101] }, โ โ
โ โ { repo: "auth-service", number: 567, deps: [101] }, โ โ
โ โ { repo: "web-client", number: 890, deps: [234, 567] } โ โ
โ โ ], โ โ
โ โ mergeOrder: ["core-types", "user-api", "auth-service", โ โ
โ โ "web-client"] โ โ
โ โ } โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
4.3 Key Data Structures
// Core types for multi-repo refactoring
interface RefactoringRequest {
type: 'rename' | 'move' | 'deprecate' | 'update-api';
target: {
symbol: string;
type: 'interface' | 'class' | 'function' | 'type' | 'constant';
sourceRepo: string;
};
replacement: {
symbol: string;
location?: string; // For move operations
};
}
interface ImpactAnalysis {
targetSymbol: string;
affectedRepos: RepoImpact[];
totalFiles: number;
totalUsages: number;
estimatedTime: string;
}
interface RepoImpact {
name: string;
files: string[];
usages: UsageLocation[];
dependencies: string[];
isSource: boolean; // true if this repo defines the symbol
}
interface UsageLocation {
file: string;
line: number;
column: number;
context: string; // Surrounding code
usageType: 'import' | 'type-reference' | 'value-reference';
}
interface DependencyGraph {
nodes: string[]; // Repo names
edges: DependencyEdge[];
levels: string[][]; // Repos grouped by dependency level
criticalPath: string[];
}
interface DependencyEdge {
from: string; // Repo that provides
to: string; // Repo that consumes
symbols: string[]; // Shared symbols
}
interface SubagentResult {
repo: string;
status: 'success' | 'failure' | 'skipped';
changes: FileChange[];
testResults: TestResult;
branchName: string;
commitSha: string;
error?: string;
}
interface PRCoordination {
prs: PRInfo[];
mergeOrder: string[];
autoMergeEnabled: boolean;
}
interface PRInfo {
repo: string;
number: number;
title: string;
branch: string;
dependencies: number[]; // PR numbers this depends on
status: 'draft' | 'ready' | 'merged' | 'closed';
checksStatus: 'pending' | 'passing' | 'failing';
}
interface MultiRepoCheckpoint {
id: string;
timestamp: Date;
request: RefactoringRequest;
repoStates: Map<string, RepoCheckpoint>;
phase: 'analyzing' | 'processing' | 'creating-prs' | 'complete';
}
interface RepoCheckpoint {
repo: string;
branchName: string;
baseSha: string;
headSha: string;
status: 'pending' | 'processing' | 'success' | 'failure' | 'rolled-back';
}
5. Phased Implementation Guide
Phase 1: Repository Analysis (Days 1-7)
Goals:
- Scan organization repositories
- Find symbol usages across repos
- Build initial impact analysis
Tasks:
- Implement GitHub MCP integration for repo listing
- Build symbol search using ripgrep or AST parsing
- Parse package.json for dependency mapping
- Generate impact analysis report
- Create visualization of affected repos
Milestone Checkpoint:
$ kiro-cli chat
> "Analyze impact of renaming UserService in @org"
[Analysis] Scanning organization repositories...
โโโ Found 12 repositories with TypeScript
โโโ Searching for "UserService" usages...
โโโ Building dependency graph...
[Impact Analysis]
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Repository โ Files โ Usages โ Dependencies โ
โโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ @org/core-types โ 3 โ 5 โ None (source) โ
โ @org/user-api โ 7 โ 23 โ @org/core-types โ
โ @org/auth-service โ 4 โ 12 โ @org/core-types โ
โ @org/web-client โ 12 โ 45 โ user-api, auth-service โ
โโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Total: 4 repos, 26 files, 85 usages
Estimated time: 2-3 hours with AI assistance
Hint 1: Use gh api repos/{owner}/{repo}/contents/package.json to fetch package.json files directly from GitHub.
Phase 2: Dependency Graph (Days 8-14)
Goals:
- Build complete dependency graph
- Implement topological sorting
- Determine processing levels
Tasks:
- Parse all package.json dependencies
- Build directed graph of repo dependencies
- Implement topological sort algorithm
- Group repos by dependency level
- Identify critical path
Milestone Checkpoint:
> "Show dependency graph for refactoring"
[Dependency Graph]
Level 0 (no dependencies):
โโโ @org/core-types
Level 1 (depends on Level 0):
โโโ @org/user-api
โโโ @org/auth-service
Level 2 (depends on Level 1):
โโโ @org/web-client
Critical Path: core-types โ user-api โ web-client
Parallel Opportunities: user-api and auth-service can run in parallel
Hint 2: Use Kahnโs algorithm for topological sort - it naturally gives you level assignments.
Phase 3: Subagent Orchestration (Days 15-21)
Goals:
- Implement subagent spawning
- Build synchronization barriers
- Handle parallel execution
Tasks:
- Create subagent configuration for repo processing
- Implement barrier synchronization between levels
- Build result aggregation system
- Add progress reporting for parallel agents
- Implement timeout handling
Milestone Checkpoint:
> "Start refactoring UserService โ AccountService"
[Orchestrator] Creating checkpoint: multi-repo-refactor-20241222
[Level 0] Processing 1 repository...
โโโ [Subagent: core-types] Cloning... โ
โโโ [Subagent: core-types] Creating branch... โ
โโโ [Subagent: core-types] Applying changes... 3 files
โโโ [Subagent: core-types] Running tests... โ
โโโ โ Level 0 complete
[Level 1] Processing 2 repositories in parallel...
โโโ [Subagent: user-api] Applying changes... 7 files
โโโ [Subagent: auth-service] Applying changes... 4 files
โโโ [Subagent: user-api] Running tests... โ
โโโ [Subagent: auth-service] Running tests... โ
โโโ โ Level 1 complete
[Level 2] Processing 1 repository...
...
Hint 3: Use Promise.all() for parallel execution within a level, but await between levels.
Phase 4: PR Coordination (Days 22-28)
Goals:
- Create linked pull requests
- Express dependencies between PRs
- Enable auto-merge in correct order
Tasks:
- Create PRs with proper descriptions
- Add dependency links in PR descriptions
- Implement PR status monitoring
- Set up auto-merge with dependency checks
- Handle merge conflicts
Milestone Checkpoint:
> "Create pull requests for the changes"
[PR Creation]
โโโ Creating PR in @org/core-types...
โ โ PR #101: "refactor: Rename UserService to AccountService"
โ
โโโ Creating PR in @org/user-api...
โ โ PR #234: "refactor: Update to AccountService"
โ โโโ Dependencies: org/core-types#101
โ
โโโ Creating PR in @org/auth-service...
โ โ PR #567: "refactor: Update to AccountService"
โ โโโ Dependencies: org/core-types#101
โ
โโโ Creating PR in @org/web-client...
โ PR #890: "refactor: Update to AccountService"
โโโ Dependencies: org/user-api#234, org/auth-service#567
[Auto-merge enabled]
Merge order: #101 โ #234, #567 โ #890
Phase 5: Rollback and Recovery (Days 29-35)
Goals:
- Implement atomic rollback
- Handle partial failures
- Provide recovery options
Tasks:
- Build checkpoint restoration
- Implement branch deletion for rollback
- Add PR closure on failure
- Create recovery commands
- Test failure scenarios
Hint 4: Store the base SHA for each repo before changes - this is your rollback target.
6. Testing Strategy
6.1 Unit Tests
describe('DependencyGraph', () => {
it('should detect circular dependencies', () => {
const graph = new DependencyGraph();
graph.addEdge('A', 'B');
graph.addEdge('B', 'C');
graph.addEdge('C', 'A'); // Creates cycle
expect(() => graph.topologicalSort()).toThrow('Circular dependency detected');
});
it('should group repos by dependency level', () => {
const graph = new DependencyGraph();
graph.addEdge('core', 'api');
graph.addEdge('core', 'auth');
graph.addEdge('api', 'web');
graph.addEdge('auth', 'web');
const levels = graph.getLevels();
expect(levels[0]).toEqual(['core']);
expect(levels[1]).toContain('api');
expect(levels[1]).toContain('auth');
expect(levels[2]).toEqual(['web']);
});
});
6.2 Integration Tests
describe('MultiRepoRefactoring', () => {
it('should rollback all repos on single failure', async () => {
const orchestrator = new RefactoringOrchestrator();
// Mock one repo to fail tests
mockTestRunner.mockImplementation((repo) => {
if (repo === 'auth-service') {
return { passed: false, errors: ['Test failed'] };
}
return { passed: true };
});
await orchestrator.execute({
type: 'rename',
target: { symbol: 'UserService', sourceRepo: 'core-types' },
replacement: { symbol: 'AccountService' }
});
// Verify all repos rolled back
expect(mockGitHub.deleteBranch).toHaveBeenCalledWith('core-types', 'refactor/rename-user-service');
expect(mockGitHub.deleteBranch).toHaveBeenCalledWith('user-api', 'refactor/rename-user-service');
expect(mockGitHub.deleteBranch).toHaveBeenCalledWith('auth-service', 'refactor/rename-user-service');
});
});
7. Common Pitfalls and Debugging
7.1 Common Issues
| Pitfall | Symptom | Solution |
|---|---|---|
| Race conditions | Parallel agents conflict | Use level-based synchronization |
| Stale dependencies | Package versions donโt match | Verify versions before starting |
| Merge conflicts | PRs canโt auto-merge | Process in strict dependency order |
| Test flakiness | Random failures block progress | Implement retry with backoff |
| API rate limits | GitHub API errors | Add rate limiting and queuing |
7.2 Debugging Strategies
1. Enable detailed logging per subagent:
const subagent = createSubagent({
repo: 'user-api',
onProgress: (step, detail) => {
console.log(`[${repo}] ${step}: ${detail}`);
},
onError: (error) => {
console.error(`[${repo}] ERROR: ${error.message}`);
console.error(`[${repo}] Stack: ${error.stack}`);
}
});
2. Visualize dependency graph:
> "Show me the dependency graph in Mermaid format"
graph TD
core-types --> user-api
core-types --> auth-service
user-api --> web-client
auth-service --> web-client
3. Inspect checkpoint state:
> "Show checkpoint details for multi-repo-refactor-20241222"
[Checkpoint: multi-repo-refactor-20241222]
โโโ Phase: creating-prs
โโโ Repos:
โ โโโ core-types: success (branch: refactor/rename-user-service)
โ โโโ user-api: success (branch: refactor/rename-user-service)
โ โโโ auth-service: failure (tests failed)
โ โโโ web-client: skipped (blocked by auth-service)
โโโ Action: Rollback recommended
8. Extensions and Challenges
8.1 Beginner Extensions
- Add dry-run mode that shows planned changes without applying
- Generate detailed change report with diffs
- Add notification system (Slack, email) for progress
8.2 Intermediate Extensions
- Support for monorepo structures (Lerna, Nx)
- Implement semantic versioning bump automation
- Add approval workflow before PR creation
8.3 Advanced Extensions
- Machine learning for effort estimation based on history
- Integration with code review platforms (Reviewable, Graphite)
- Support for cross-language refactoring (shared API types)
9. Real-World Connections
9.1 Industry Tools
| Tool | Pattern Used | Company |
|---|---|---|
| Sourcegraph Batch Changes | Multi-repo refactoring | Sourcegraph |
| Renovate Bot | Dependency updates | Mend |
| Dependabot | Automated PRs | GitHub |
| Atomist | Code transformation | Atomist |
9.2 Enterprise Use Cases
- API versioning: Update all consumers when API changes
- Security patches: Apply fixes across all repositories
- Framework upgrades: Migrate React/Vue/Angular versions
- Deprecation: Remove usage of deprecated APIs
10. Self-Assessment Checklist
Understanding Verification
- I can explain topological sorting and why it matters
- I understand the two-phase commit pattern for atomic changes
- I can describe the diamond dependency problem and solutions
- I know how to coordinate PRs with dependencies
- I can explain subagent orchestration patterns
Implementation Verification
- Repository analysis correctly finds all usages
- Dependency graph is built correctly
- Subagents process repos in parallel within levels
- Synchronization barriers work between levels
- PRs are created with correct dependencies
- Rollback works when any repo fails
Integration Verification
- Full refactoring works on test repositories
- PR auto-merge respects dependency order
- Recovery works from any checkpoint
- Progress is reported accurately
11. Completion Criteria
Minimum Viable Completion
- Analyzes repositories and builds dependency graph
- Processes repos in correct dependency order
- Creates PRs with dependency information
- Handles single-repo failures with rollback
Full Completion
- Parallel processing within dependency levels
- Full atomic rollback across all repos
- Auto-merge with dependency checking
- Comprehensive progress reporting
- Checkpoint-based recovery
Excellence (Going Above and Beyond)
- Monorepo support
- Cross-organization refactoring
- Machine learning for effort estimation
- Integration with code review platforms
- Visual dependency graph in dashboard
This guide was expanded from LEARN_KIRO_CLI_MASTERY.md. For the complete learning path, see the README.