GitHub Copilot Stacked Sessions: A New Paradigm for Refactoring Legacy Code

GitHub Copilot's Stacked Sessions enable structured, reviewable AI-assisted refactoring of legacy codebases.
GitHub Copilot's Stacked Sessions and Pull Requests features introduce a new paradigm for refactoring legacy code. By layering AI coding sessions and integrating them with PR workflows, developers can decompose complex refactoring into manageable, reviewable steps while maintaining engineering standards through CI/CD checks and code review.
Introduction: A New Workflow for AI-Assisted Refactoring
In the daily work of software development, refactoring legacy codebases has always been a time-consuming and risky endeavor. Developers must not only understand the intent behind historical code but also modernize it incrementally without breaking existing functionality. A recent practice article shared on GitHub's official blog demonstrates how to systematically refactor an aging codebase using Stacked Sessions and Pull Requests features in the GitHub Copilot application.
The core value of this practice lies in elevating AI-assisted programming from simple "code completion" to "task-level collaboration." Developers no longer just have AI generate scattered code snippets. Instead, through structured session management, complex refactoring tasks are decomposed, chained together, and ultimately form a complete change chain that is reviewable and rollback-capable.

What Are Stacked Sessions?
Core Concept
Stacked Sessions are a task organization mechanism introduced in the GitHub Copilot application. They allow developers to layer multiple interrelated AI coding sessions like a "stack," where each session layer builds upon the work output of the previous one. This design is particularly well-suited for handling sequential tasks with dependencies.
This design philosophy didn't emerge from thin air—it draws from the mature concept of "stacked branches" in version control. In traditional Git workflows, stacked branches involve splitting large feature development into multiple smaller branches, each created on top of the previous one, forming a chain of dependencies. This pattern has been practiced for years in internal tools at major tech companies like Google and Meta—Google's Critique code review system and Meta's Sapling version control tool both natively support this workflow. GitHub Copilot's application of this concept to AI coding session management essentially brings a proven divide-and-conquer strategy from software engineering into the human-machine interaction layer.
For example, when refactoring a legacy codebase, you might need to first upgrade dependency versions, then refactor core modules, and finally update test cases. In the traditional approach, these steps often get mixed together and become difficult to track clearly. With stacked sessions, each step can become an independent layer, maintaining logical continuity while preserving clear boundaries.
Differences from Traditional Single Sessions
In single-session mode, all AI interactions occur within the same context. As tasks progress, the context becomes increasingly bloated, and the quality of AI understanding and output tends to degrade. This relates to a core technical limitation of Large Language Models (LLMs)—the Context Window. The context window refers to the maximum number of tokens a model can process in a single inference. Although modern models like GPT-4 Turbo have expanded context windows to 128K tokens, and the Claude series supports 200K tokens, in practical code comprehension tasks, longer contexts tend to reduce the model's attention to detail and reasoning accuracy. Researchers call this the "Lost in the Middle" phenomenon, where the model's information recall rate for the middle portions of input is significantly lower than for the beginning and end.
Stacked sessions address this through layered isolation, letting each layer focus on a specific subtask. This effectively controls the context size of each session layer, enabling the model to maintain high-quality output within a relatively focused scope while making it easier for human developers to intervene and review.
Deep Integration with Pull Requests
Seamless Transition from Sessions to PRs
Another key point of this practice is the tight coupling of stacked sessions with GitHub's pull request workflow. The output of each session can be directly transformed into a pull request, or additional changes can be stacked on top of an existing PR. This means AI-generated code no longer exists outside the team collaboration process but is fully incorporated into the standard pipeline of code review, CI/CD checks, and version control.
CI/CD (Continuous Integration/Continuous Deployment) is a cornerstone process in modern software engineering. Continuous integration requires that every code change automatically triggers builds, tests, and static analysis to ensure new code doesn't break existing functionality. Continuous deployment automatically releases code that passes checks to the production environment. GitHub's pull request mechanism is the core trigger point for this process—when developers create or update a PR, it automatically triggers CI pipelines defined in GitHub Actions. Incorporating AI-generated code into this standard process means AI output must also pass automated tests, code style checks (such as ESLint, Prettier), security scans (such as Dependabot, CodeQL), and other quality gates, thereby ensuring a quality baseline for AI-assisted development.
For high-risk operations like refactoring, this is especially important. Each PR is an independently evaluable unit of change. Team members can review every AI modification to ensure changes meet expectations while retaining the ability to roll back at any time.
Controllability Through Layered PRs
By splitting large refactoring tasks into multiple stacked pull requests, developers gain unprecedented controllability. Compared to "mega PRs" that commit thousands of lines of changes at once, layered small PRs are easier to review, and when problems arise, they're easier to locate and fix. This essentially combines the software engineering best practice of "small, frequent steps" with AI-assisted programming capabilities.
Practical Application: Modernizing a Legacy Codebase
Typical Refactoring Scenarios
The article's author uses one of their own aging projects to demonstrate the complete modernization process. These Legacy Codebases typically refer to code systems that have been running for years, lack maintenance documentation, and use outdated technology stacks. According to a 2018 industry survey by Stripe, developers spend an average of 42% of their work time dealing with technical debt and maintaining legacy systems. Modernizing these codebases faces multiple challenges: dependencies may have been abandoned and contain security vulnerabilities, API designs don't conform to current best practices, low test coverage makes refactoring extremely risky, and implicit business logic is scattered throughout the code and difficult to extract.
With the Copilot application, the author was able to let AI handle much of the tedious analysis and rewriting work while focusing personally on decision-making and review. AI tools have unique value in such scenarios—they can rapidly analyze large amounts of code, identify patterns and anti-patterns, and generate alternative implementations that conform to modern standards.
In this process, stacked sessions serve as the organizer: the first session layer might handle sorting out and upgrading project dependencies, the second layer refactors business logic on that foundation, and the third layer supplements and improves tests. The output of each layer is clearly visible, interrelated yet not chaotic.
The Shift in AI Collaboration Patterns
This workflow reveals a deep transformation occurring in AI programming tools: from passive "code suggestion tools" to active "collaboration partners" participating in engineering processes. Looking back at the evolution of AI programming tools, several stages of progression are clearly visible: the first stage was rule-based code completion, like traditional IDE IntelliSense; the second stage was intelligent completion based on statistical models, like early versions of TabNine; the third stage was code generation based on large language models, represented by GitHub Copilot (released in 2021), capable of generating multiple lines of code based on comments and context; the fourth stage is the currently unfolding "Agent" mode, where AI no longer merely responds passively but can proactively plan tasks, execute multi-step operations, and interact with external tools.
The stacked sessions feature in the GitHub Copilot application sits at a critical transition point between the third and fourth stages, granting AI certain task planning and state management capabilities. The developer's role transforms accordingly—from writing code line by line to defining tasks, reviewing output, and maintaining quality control. This transformation holds significant implications for handling large-scale, long-cycle engineering tasks.
Implications for Developers
A Better AI Collaboration Approach for Complex Tasks
The combination of stacked sessions and pull requests provides a viable methodology for handling complex, multi-step development tasks. It addresses two major pain points in previous AI programming—"context bloat" and "uncontrollable changes"—enabling AI to truly participate in engineering tasks of meaningful scale.
The Importance of Maintaining Engineering Standards
It's worth emphasizing that this workflow doesn't weaken engineering standards by introducing AI—instead, it reinforces them through mechanisms like PR reviews and CI checks. This reminds us that the value of AI tools lies not in bypassing established engineering practices but in dramatically improving efficiency while adhering to them. Only when AI-generated code is subject to the dual verification of human review and automated testing can it truly earn team trust and be incorporated into production-level development workflows.
Conclusion
The stacked sessions and pull requests features in the GitHub Copilot application represent an important direction for AI-assisted programming moving toward mature engineering practices. It's not just a new feature but a way of thinking about "how to enable AI to participate in real software engineering with high quality." For teams and individual developers facing the challenge of legacy codebase modernization, this methodology provides a practical paradigm that leverages AI efficiency while maintaining engineering controllability. As these tools continue to evolve, the boundaries of collaboration between AI and human developers will continue to expand.
Related articles

OpenAI's Git Optimization: Tackling Performance Bottlenecks in Massive Repositories
Analysis of how OpenAI optimizes Git for massive repositories, covering monorepo bottlenecks, partial clone, sparse checkout, fsmonitor, and practical tips for engineering teams.

AI Can't Build Usable Products — Developers' Jobs Haven't Disappeared
AI can generate code snippets and demos, but usable products still require human engineers' judgment and responsibility. This article analyzes AI coding tools' limits and developers' evolving roles.

Solid Queue 1.6.0 Fiber Worker Support: A New Concurrency Option for Rails Background Jobs
Solid Queue 1.6.0 introduces Fiber Worker support, offering a lightweight and efficient concurrency model for I/O-intensive Rails background jobs.