AD SPACE AVAILABLE · BRAND PARTNERS WELCOME

How to Use Pieces Alongside GitHub Copilot for Cross-File Refactoring Without Hallucinations

Corrie Koh · 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

When you ask GitHub Copilot to refactor a single function inside an isolated module, it usually delivers clean code. When you ask it to refactor a shared data schema across eleven files spanning an API controller, domain models, database migrations, and frontend hooks, things fall apart fast.

GitHub Copilot begins to hallucinate method signatures, forgets type constraints introduced three files back, and silently invents helper functions that do not exist. The reason is structural: GitHub Copilot's @workspace uses heuristic indexing and static embeddings, limited by sliding context windows that degrade across iterative edits.

At 365Finds, our testing shows that pairing GitHub Copilot with Pieces completely changes the equation. Pieces serves as an external, persistent context engine, while GitHub Copilot acts as the in-editor execution muscle. By bridging the two via the Model Context Protocol (MCP), you anchor ground-truth definitions in Pieces and feed them on demand to GitHub Copilot, eliminating token drift and cross-file hallucinations.

Here is the exact architecture, setup, and practical workflow we recommend.


Why GitHub Copilot Drifts Across Multiple Files

Before fixing the problem, it helps to understand why GitHub Copilot struggles with cross-file refactoring on its own:

  1. Context Window Eviction: As you move through a multi-file refactoring sequence, previously touched files drop out of immediate prompt memory. Copilot guesses the omitted signatures based on general pre-training probabilities.
  2. Heuristic Search vs. Ground Truth: Copilot's @workspace agent scans files using similarity search. If your refactoring introduces breaking changes to an interface, @workspace often retrieves the old unrefactored definitions from disk, conflicting directly with the new architectural contract you want to enforce.
  3. External Context Blindness: Technical decisions rarely live solely in source code. They originate in PR discussions, Slack threads, migration tickets, and API draft specs. GitHub Copilot has no native memory of these artifacts.

Pieces fixes these exact gaps. Powered by PiecesOS, it runs a local Long-Term Memory (LTM) engine that tracks your workstream, indexes external documentation, and stores discrete code snippets with full provenance.


Architectural Separation of Concerns

At 365Finds, we strongly advise against forcing one tool to do everything. Treat Pieces and GitHub Copilot as complementary layers:

LayerToolPrimary ResponsibilityWhy It Excels
Context & Memory LayerPiecesAnchoring ground-truth schemas, tracking workstream history, storing reusable migration patternsLocal LTM engine; zero token penalty on your editor; cross-source indexing
Execution & Synthesis LayerGitHub CopilotIn-editor code generation, Agent mode multi-file diffs, inline completionsDeep VS Code / JetBrains language server integration; fast generation speed
Protocol BridgeModel Context Protocol (MCP)Exposing Pieces data as callable tools within GitHub Copilot ChatOpen standard; dynamic retrieval without manual copy-pasting

[!NOTE] Pieces previously offered standalone IDE extensions, but these have been sunset. The official and actively maintained integration path is the Pieces Model Context Protocol (MCP) server, which connects PiecesOS directly into GitHub Copilot's Agent mode.


Step-by-Step Implementation Guide

Step 1: Install and Run PiecesOS

PiecesOS operates as the background local daemon for context capture and MCP serving.

  1. Download and install Pieces for Developers and PiecesOS from the official portal.
  2. Launch PiecesOS and ensure it is running in your system tray or menu bar.
  3. In Pieces Desktop Settings, navigate to Long-Term Memory and verify that LTM is active.

Step 2: Connect Pieces to GitHub Copilot via MCP

You can establish the connection between PiecesOS and GitHub Copilot in Visual Studio Code using the Pieces CLI or the Pieces Desktop App.

Method A: Using Pieces CLI (Fastest)

Run the interactive setup command in your terminal:

pieces mcp setup
  1. Select VS Code when prompted for your client.
  2. Choose Workspace Settings if you want to scope the MCP server to your current repository, or User Settings to make Pieces tools available globally.
  3. Verify the status:
pieces mcp status

Method B: Manual Configuration in VS Code

If you prefer explicit configuration, add the Pieces MCP endpoint to your .vscode/mcp.json or your global VS Code MCP configuration:

{
  "mcpServers": {
    "pieces": {
      "transport": "sse",
      "url": "http://localhost:10000/model_context_protocol/v1/sse"
    }
  }
}

Restart VS Code after saving the configuration.


The 4-Phase Cross-File Refactoring Workflow

Once the bridge is active, execute complex refactors using this four-phase sequence.

Phase 1: Context Harvesting and Contract Anchoring (in Pieces)

Do not start by prompting Copilot to rewrite files immediately. First, establish the contract in Pieces:

  1. Capture the Target Contract: Copy the new interface, database migration DDL, or updated protobuf schema into Pieces. Label it clearly (e.g., refactor/v2-billing-contract).
  2. Anchor Supporting Knowledge: If you have an RFC note, an external API documentation page, or an architectural ticket, save those excerpts into Pieces.
  3. Pieces automatically parses language tags, dependencies, and origin metadata, establishing an immutable reference point.

Phase 2: Formulate the Staged Prompt in GitHub Copilot Agent Mode

Open GitHub Copilot Chat in VS Code and switch mode to Agent. This grants Copilot the authority to inspect workspace files and call external MCP tools.

Prompt Copilot using explicit tool grounding:

We are migrating our billing subsystem to the new `SubscriptionPlanV2` model.
Query the `pieces` MCP server for the snippet tagged `refactor/v2-billing-contract`.

Do not invent new properties. Using the retrieved schema as the absolute contract:
1. Identify all downstream files importing `SubscriptionPlan`.
2. Generate a step-by-step diff plan before modifying any code.

By querying the snippet from Pieces via MCP, GitHub Copilot loads the exact contract into its immediate prompt context rather than relying on stale workspace embeddings.

Phase 3: Targeted File-by-File Diff Execution

Review Copilot's proposed step-by-step diff plan. Instruct Copilot to execute changes file by file or domain by domain:

At each step, if Copilot's output begins to drift, rein it in with a targeted prompt:

"Re-verify against the SubscriptionPlanV2 definition fetched from Pieces. The interval_count field must remain an integer, not an optional string."

Phase 4: Snapshot Intermediate States Back to Pieces

During an extended refactoring session spanning dozens of commits, intermediate contracts change. When you finalize a tricky translation function or a database migration script:

  1. Save that successful snippet directly back into Pieces.
  2. Tag it as refactor/v2-migration-complete.
  3. If GitHub Copilot needs to write unit tests or client adapters later in the day, instruct it to reference that newly saved snippet from Pieces.

This turns Pieces into a dynamic cache of verified intermediate truths, preventing Copilot from regressing to pre-refactor patterns.


Practical Trade-offs and 365Finds Observations

At 365Finds, we believe in discussing the rough edges alongside the benefits:


References