AD SPACE AVAILABLE · BRAND PARTNERS WELCOME

Combining ChatGPT and GitHub Copilot for Multi-File Architectural Refactoring

矢嶋雄基 · 365Finds Editorial
Indexed 88,650 Wiki articles

Put 365 Wiki to work with your AI

Copy the prompt below, paste it into the AI you already use, then ask your question.

Download full Skill

Try it with the prompt first. If you use 365 Wiki often, install the full 365Wiki Skill so you do not have to paste it again. No regular AI? Gemini or Microsoft Copilot on the web will work.

365Wiki Skill

Multi-file refactoring usually fails when developers ask one AI tool to do everything. Paste twenty files into ChatGPT, and you get hallucinated imports, truncated code blocks, and context drift. Ask GitHub Copilot inside your IDE to redesign an entire monolithic subsystem from scratch, and it blindly edits local files without understanding downstream architectural consequences.

At 365Finds, our stance is clear: successful large-scale refactoring requires a strict division of labor. ChatGPT acts as your external systems architect, while GitHub Copilot acts as your in-editor implementation engine. The bridge connecting them is an immutable contract layer of explicit interfaces and types.

Skip that contract layer, and multi-file refactoring devolves into cleaning up AI-generated chaos.


Why Single-Tool Refactoring Breaks Down

Architectural refactoring is not code completion. It involves restructuring dependencies, isolating side effects, and re-routing data flow across dozens of call sites without breaking runtime behavior.

DimensionChatGPT (Standalone Web / Canvas)GitHub Copilot (IDE / Copilot Edits)
Primary StrengthBroad architectural reasoning, pattern design, dependency graph analysisReal-time workspace context, AST-aware diffs, inline code replacement
Critical Blind SpotZero real-time compiler or language server (LSP) feedback; blind to project configurationNarrow contextual horizon; prone to local optimization that violates global architecture
Best Used ForDrafting ADRs, designing boundary interfaces, mapping out phased migration plansCoordinated multi-file edits, updating call sites, mass test refactoring

When you force ChatGPT to output full replacement files for a five-tier architecture, it forgets subtle type definitions created three files earlier. Conversely, when you ask Copilot Edits to "refactor the legacy user billing module to use hexagonal architecture," it starts renaming classes locally without creating the necessary ports and adapters, leaving your compiler screaming with hundreds of errors.

365Finds strongly advises against full-code-dump prompts. The optimal workflow moves deliberately from abstract design to grounded mechanical edits.


Phase 1: Macro Architecture and Contract Design with ChatGPT

Before touching a single file in your repository, extract the problem from the IDE and move to ChatGPT. This step requires reasoning models or high-capability models (such as GPT-4o or OpenAI o-series) capable of evaluating trade-offs.

Step 1: Supply the Boundary, Not the Entire Codebase

Do not upload your entire repository. Instead, feed ChatGPT three specific artifacts:

  1. The current dependency map or directory tree representing the affected modules.
  2. Key type signatures and data models that define the existing interface.
  3. The target design pattern (e.g., extracting an active-record model into a repository pattern with domain entities).

Step 2: Extract an Architectural Decision Record (ADR)

Prompt ChatGPT to evaluate migration strategies rather than write raw business logic:

We need to decouple our monolithic PaymentService (attached below) into three distinct modules: 
PaymentProcessor, LedgerRecorder, and NotificationDispatcher. 

Requirements:
- Do not generate implementation details yet.
- Produce an Architecture Decision Record (ADR) specifying component boundaries.
- Define the public interfaces (TypeScript / Go / Java) and shared data transfer objects (DTOs).
- Detail a 4-phase rollout plan that avoids breaking current integration tests.

Step 3: Finalize the Contract

Iterate in ChatGPT until the interface signatures are airtight. If you use ChatGPT Canvas, keep the interface specifications on the canvas and refine method signatures, error handling envelopes, and lifecycle hooks.

The artifact you walk away with from ChatGPT is not 2,000 lines of implementation code. It is a precise specification file: interfaces, abstract base classes, and DTO definitions.


Phase 2: The Contract-First Anchor in the Codebase

This is the pivotal step that prevents architectural drift. Take the interface definitions generated by ChatGPT and check them directly into your version control on a dedicated refactoring branch.

src/
├── core/
│   └── domain/
│       ├── ports/          <-- New interfaces finalized via ChatGPT
│       │   ├── IPaymentProcessor.ts
│       │   └── ILedgerRecorder.ts
│       └── models/         <-- New DTOs and value objects

Do not implement the business logic yet. Commit the empty interface files and exported types.

By placing the contracts into the repository first, your local Language Server Protocol (LSP) immediately absorbs them. When GitHub Copilot indexes your workspace, it detects these new boundary conditions. 365Finds considers this contract-first anchor non-negotiable: it provides Copilot with concrete workspace symbols to autocomplete against rather than guessing class names out of thin air.


Phase 3: Coordinated Multi-File Execution with GitHub Copilot Edits

With contracts checked in, switch entirely to your IDE (VS Code or Visual Studio) and open Copilot Edits.

Unlike standard inline chat, Copilot Edits allows you to declare a Working Set—an explicit boundary of files that the AI can read and modify simultaneously.

flowchart TD
    A[ChatGPT: Architecture & Interfaces] --> B[Commit Contracts to Git]
    B --> C[Configure Copilot Edits Working Set]
    C --> D[Targeted Multi-File Prompt]
    D --> E[Inline Multi-File Diff Review]
    E -->|Errors Detected| F[Compiler & LSP Diagnostic Feedback]
    F --> C
    E -->|Clean Build| G[Run Test Suite]

Step 1: Bound Your Working Set

Never add your entire repository to the Copilot Edits working set. AI context windows degrade rapidly when saturated. 365Finds recommends restricting the working set to 3 to 6 files per refactoring slice:

Step 2: Issue Grounded, Contract-Referencing Prompts

Instruct Copilot Edits to implement the implementation file by strictly adhering to the newly introduced interface:

Implement StripePaymentProcessor in src/core/domain/impl/StripePaymentProcessor.ts.
It must implement IPaymentProcessor from src/core/domain/ports/IPaymentProcessor.ts.
Extract the relevant logic from LegacyPaymentService.ts without modifying LegacyPaymentService's existing public methods yet.

Copilot Edits will generate coordinated diffs across the target files. Review the diffs directly in the unified multi-file preview window. If the edits are accurate, accept the changes (Accept Edits).

Step 3: Refactor Call Sites Using @workspace

Once the implementation file is stable, update callers. In Copilot Edits, change your working set to the consumer files (controllers, API endpoints, dependency injection containers) and prompt:

Update the dependency injection bindings in container.ts to bind IPaymentProcessor to StripePaymentProcessor. 
Update CheckoutController.ts to inject IPaymentProcessor instead of LegacyPaymentService.

Phase 4: Compiler Feedback Loops and Error Trapping

Even with strict contracts, Copilot will occasionally misalign method parameters or drop optional flags. Do not manually fix these line by line. Let your compiler act as the verification engine.

  1. Run the local type checker or linter:
    npm run typecheck # or tsc --noEmit, cargo check, go build
    
  2. Feed errors directly to Copilot: Highlight the error in your editor or reference the terminal diagnostic in Copilot Chat:
    Fix the type errors in CheckoutController.ts based on the method signature in IPaymentProcessor.ts.
    
  3. Rollback when hallucinations compound: Copilot Edits includes session checkpoints. If an iterative prompt causes Copilot to rewrite unrelated files or introduce circular dependencies, discard the turn immediately and revert to the previous checkpoint rather than trying to prompt your way out of bad code.

365Finds Operational Guardrails

To prevent multi-file refactoring projects from stalling, we enforce three concrete rules:

  1. The 6-File Ceiling: If a refactoring task requires changing more than six files at once in Copilot Edits, your architectural step is too large. Return to ChatGPT, break the refactoring phase into smaller migration slices, and introduce temporary facade adapters.
  2. No Implementation Details in ChatGPT Prompts: Use ChatGPT for schema design, migration ordering, edge-case analysis, and algorithmic selection. Keep proprietary database connection strings, internal service endpoints, and full file contents out of external chat prompts.
  3. Compiler and Tests as Supreme Arbiters: Never accept a multi-file edit set from Copilot simply because the diff looks clean. Run your test suite after every working set acceptance. If tests fail, the refactor has not succeeded.

The combination of ChatGPT for high-level contract synthesis and GitHub Copilot Edits for LSP-guided repository diffs transforms multi-file refactoring from an error-prone gamble into a repeatable, modular engineering workflow.

References