Uploaded October 2026 | Updated November 2026, 2 hours ago
In this episode I take on a very different kind of AI coding task:
refactoring old production code.
Tsugi has accumulated more than a decade of history, and some of that history lives in places like UI/lessons.php — large utility files that mix HTML rendering, reusable logic, legacy compatibility, and application behavior.
It works.
But architecturally, it is time to clean it up.
The goal is conservative:
HTML generation belongs in controllers
reusable business logic belongs in services
UI utilities should actually be reusable UI widgets
legacy APIs should keep working
production behavior should not change
So we begin moving lesson rendering into the Lessons controller, along with similar rendering code for assignments, badges, and discussions.
Then the refactor gets deeper.
The remaining lesson-processing code begins to move out of UI/lessons.php and into a real Lessons service, along with supporting Common Cartridge and legacy lesson-processing classes.
But then comes the dangerous realization:
old LTI tools outside this repository already import UI/lessons.php.
Those tools are not using it for rendering. They are using it to parse lessons.json and look up content by resource-link ID.
If I simply delete the old class, a bunch of legacy tools scattered around the ecosystem could break.
So instead of removing the old API completely, we preserve a backwards-compatible shim that delegates into the new Lessons service.
That gives us the best of both worlds:
cleaner internal architecture without immediately breaking old integrations.
Along the way, I also refactor analytics code, move utility behavior into services, reorganize tests, and repeatedly ask AI to review its own work from a fresh thread before trusting the branch.
The hardest part of refactoring is not moving code.
It is knowing when to move it.
Too early, and you are designing abstractions before you understand the system.
Too late, and every new feature piles more dependencies onto the old architecture.
This felt like the right moment: enough of the new Tsugi architecture exists to know the controller/service patterns, but there is still plenty of development ahead to exercise and test the refactor before it settles into production.
AI makes this kind of large-scale cleanup dramatically faster — but the human still has to decide what belongs where, what compatibility must be preserved, and how much risk is acceptable.
In this episode I take on a very different kind of AI coding task:
refactoring old production code.
Tsugi has accumulated more than a decade of history, and some of that history lives in places like UI/lessons.php — large utility files that mix HTML rendering, reusable logic, legacy compatibility, and application behavior.
It works.
But architecturally, it is time to clean it up.
The goal is conservative:
HTML generation belongs in controllers
reusable business logic belongs in services
UI utilities should actually be reusable UI widgets
legacy APIs should keep working
production behavior should not change
So we begin moving lesson rendering into the Lessons controller, along with similar rendering code for assignments, badges, and discussions.
Then the refactor gets deeper.
The remaining lesson-processing code begins to move out of UI/lessons.php and into a real Lessons service, along with supporting Common Cartridge and legacy lesson-processing classes.
But then comes the dangerous realization:
old LTI tools outside this repository already import UI/lessons.php.
Those tools are not using it for rendering. They are using it to parse lessons.json and look up content by resource-link ID.
If I simply delete the old class, a bunch of legacy tools scattered around the ecosystem could break.
So instead of removing the old API completely, we preserve a backwards-compatible shim that delegates into the new Lessons service.
That gives us the best of both worlds:
cleaner internal architecture without immediately breaking old integrations.
Along the way, I also refactor analytics code, move utility behavior into services, reorganize tests, and repeatedly ask AI to review its own work from a fresh thread before trusting the branch.
The hardest part of refactoring is not moving code.
It is knowing when to move it.
Too early, and you are designing abstractions before you understand the system.
Too late, and every new feature piles more dependencies onto the old architecture.
This felt like the right moment: enough of the new Tsugi architecture exists to know the controller/service patterns, but there is still plenty of development ahead to exercise and test the refactor before it settles into production.
AI makes this kind of large-scale cleanup dramatically faster — but the human still has to decide what belongs where, what compatibility must be preserved, and how much risk is acceptable.










