Prompt file imported from reckerswartz/resume_builder (
.windsurf/workflows/tdd-refactoring-agent.md). Copyright stays with the author.
Continuous TDD Cycle — Refactor Phase
This workflow is one phase of the repeating TDD cycle: Red → Green → Refactor → Re-assess → Red. Each invocation improves code structure while preserving behavior. The cycle continues until the codebase meets quality standards.
Phase 1: Context & Regression-Aware Green Baseline
- Treat any text supplied after
/tdd-refactoring-agentas the feature scope, code area, or green-phase context. - Invoke
@tdd-refactoring-agent. - Verify a green test baseline before refactoring — run the targeted spec suite first. Check for pending migrations with
bin/rails db:migrate:status. If the work touches views, components, helpers, presenters, CSS, Stimulus, user-facing copy, or page structure, also readdocs/ui_guidelines.md,docs/behance_product_ui_system.md, anddocs/references/behance/ai_voice_generator_reference.mdbefore refactoring. - Assess current cycle position and regression baseline: identify which areas were just implemented (Green phase) and which have the most refactoring value. Prioritize recently-implemented code that followed minimal-change conventions. If a previously-clean area has regressed structurally, restore that baseline before widening the refactor.
Git Sync Gate (mandatory — keeps main up-to-date)
All work happens directly on the main branch. No feature branches.
GIT-1. Before starting any work, sync with remote:
bash // turbo git checkout main
bash // turbo git pull origin main
If there are uncommitted local changes, stash or commit them first.
GIT-2. After validation passes (Phase 4), stage, commit, and push:
bash git add -A git commit -m "tdd-refactoring-agent: <description of the refactor>" git push origin main
Phase 2: Refactor
- Refactor using established project extraction patterns:
- Controller → Service: extract transaction/orchestration logic into service objects with
callinterface (Admin::SettingsUpdateService,Admin::LlmProviderCatalogSyncService,Resumes::DraftBuilderpatterns) - View → Presenter: extract inline view state assembly into
*Statepresenters wired through helpers with memoization (Admin::SettingsPageState,Resumes::TemplatePickerState,ResumeBuilder::EditorStatepatterns). IncludeI18n.localein memoization keys when locale-sensitive. - UI baseline: when refactoring UI-facing code, preserve shared
Ui::*components,ui_*helper APIs, page-family rules, andatelier-*tokens instead of introducing a new page-local visual system - Hardcoded strings → I18n: replace hardcoded flash/notice/label strings with
I18n.t(...)backed by domain-scoped locale keys. Usecontroller_message(...)pattern in controllers. Never usetitleize/humanizefor display labels — use sharedresume_templates.catalog.labels.*or domain-scoped keys. - Inline data → Catalog: extract static/configuration-driven data into catalog services (
ResumeTemplates::Catalog,Resumes::CloudImportProviderCatalog,Resumes::SummarySuggestionCatalogpatterns) - Duplication → Shared partial/component: extract repeated view patterns into shared partials or ViewComponents with density variants
- Complex conditionals → Query objects/scopes: extract filtering logic into model scopes or dedicated query objects
- Controller → Service: extract transaction/orchestration logic into service objects with
- Improve structure, naming, duplication, and layering while preserving behavior.
- Keep refactors aligned with this project's
.windsurfrules, especially around services, policies, components, and HTML-first Rails flows.
Phase 3: Refine Data
- Refine underlying data alongside structural refactors:
- Update
db/seeds.rbwhen refactors change model structure, service interfaces, or demo data shape - Update locale files when extracting hardcoded strings to I18n
- Update specs to cover the refactored path — add new service/presenter specs and remove stale assertions
- Update documentation (
docs/architecture_overview.md) when extraction patterns change architectural boundaries
- Update
Phase 4: Validate
- After each meaningful refactor, verify:
Also verify YAML syntax on any modified locale files andruby -c <modified_ruby_files> bundle exec rspec <affected_spec_files>ruby -c db/seeds.rbif seeds changed. - Regression check: after refactoring shared code, run specs for all consumers of the refactored module to confirm no behavior changes.
Phase 5: Re-assess & Cycle Forward
- Re-assess the refactored area after validation: identify any remaining duplication, newly-exposed weak coverage, or adjacent stale patterns that should become the next cycle slice.
- When the refactor phase is complete, assess the next step:
- Recommend
/rspec-agentif coverage gaps were identified during refactoring - Recommend
/code-reviewfor broader quality assessment of the refactored area - Recommend
/tdd-red-agentif the feature has additional behavior slices to implement - Recommend
/maintainability-auditif the refactoring surfaced architectural concerns beyond the current scope
- Recommend
- Full TDD cycle chain: Red (
/tdd-red-agent) → Green (/implementation-agent) → Refactor (/tdd-refactoring-agent) → Re-assess coverage (/rspec-agent) → back to Red for the next behavior slice. Each workflow feeds into the next in a continuous loop. - If refactoring reveals new issues (e.g., untested paths, stale patterns in adjacent files), record them as candidates for the next cycle iteration rather than expanding the current refactor scope.
