Execute-Refactor 150 Protocol
Overview
Run in three stages:
- Research refactor options (iterative, no code changes).
- Plan + confirm + implement with tests and validation.
- Apply Scout105 cleanup (objective, small, user-approved).
Stage 1 — Research refactor options (iterative)
Create or reuse investigation log at .temp/INVESTIGATION.md.
- If the file does not exist, create it and add the template below.
- Keep all research notes in this file only (no scattered notes in chat).
Define the core question (the refactor goal).
- Write a single sentence: “We need to refactor X to achieve Y.”
- Example: “Refactor SermonCard to reduce duplication and isolate date logic.”
Define scope (Scope150).
- Core (100%): list files and behaviors you will directly change.
- Boundary (50%): list callers, dependent modules, tests, configs, and data flow.
- If unsure, add a “to verify” bullet and resolve it during observations.
Perform observations (search/read) in ordered layers.
- Interface layer: find entry points (routes, public APIs, UI entry points).
- Domain layer: identify entities, i18n keys, enums, types.
- Pattern layer: locate hooks, services, clients, shared utilities.
- Usage layer: trace imports and call sites.
- For each layer, read the minimum number of files that explain the behavior.
Record findings in the log with sources.
- Each fact must include a source (file path + line reference or command).
- Separate facts from hypotheses. Do not mix them.
Generate hypotheses and refactor options.
- For each option, note: goal, steps, benefits, risks, and test impact.
- Prefer at least 2 options so tradeoffs are explicit.
Decide if more research is needed.
- If user says “research more”, expand scope or inspect new files.
- Update the log with new branches and continue Stage 1.
Stop before implementation.
- End Stage 1 with: options summary, remaining unknowns, and a recommended path.
Investigation log template
# Investigation Log: <short topic>
## Core question
- ...
## Scope
- Core (100%):
- ...
- Boundary (50%):
- ...
## Findings
- <fact> (source: file path / command)
- Subfinding
## Hypotheses
- H1: ...
- Prediction: ...
- Test: ...
- Status: pending/confirmed/rejected
## Refactor options
- Option A: ... (pros/cons)
- Option B: ... (pros/cons)
## Next branches
- ...
Stage 2 — Plan, confirm, implement
Write a refactor plan.
- Break into ordered steps with file-level granularity.
- Include expected intermediate states (what should still pass after each step).
List risks and edge cases.
- Identify behavior changes, API contract risks, and hidden coupling.
- Explicitly note any breaking-change risk.
Define a validation checklist.
- Tests to run (unit/integration).
- Manual checks if needed (UI flows, API calls).
- Expected outputs and what would indicate failure.
Perform Scope150 validation planning.
- Core (100%): ensure each planned change has a test or validation step.
- Boundary (50%): list all callers/integrations/tests to be checked.
Ask for user confirmation before editing.
- Provide plan, risks, and validation checklist.
- Do not edit until the user approves.
Implement the refactor.
- Follow the plan in order.
- Keep changes minimal and reversible.
Add or update tests.
- Cover changed logic and any new boundaries.
- Avoid brittle tests; prefer behavior-based assertions.
Run validation.
- Execute all tests from the checklist.
- Record results and fix failures before moving to Stage 3.
Stage 3 — Scout105 cleanup protocol
Apply only objective cleanup after a Key Point (phase complete). Do not add features.
Trigger (Key Point)
- Apply only after a milestone where the user would see complete value.
- Do not run per-file; run once per phase.
Allowed cleanup categories (objective only)
- Unused code: unused imports/vars/fields (provable by grep/IDE).
- Typos: spelling errors in comments/strings (spellcheckable).
- Formatting inconsistencies: breaks the file’s own pattern.
- Dead code: commented debug lines or unreachable code.
- Obviously wrong logic: provably incorrect (duplicate checks, impossible types).
Constraints
- Only in files already touched by the primary task.
- Remove garbage only; no new features or refactors.
- Each cleanup must take ≤ 30 seconds to verify + fix.
- Total cleanup time ≤ 5% of primary task time.
- Must be objectively measurable; if not measurable, skip.
- Require user approval before executing cleanup.
Scout105 report (present to user)
✅ Key Point complete: <brief summary>
Scout105 opportunities:
- <file>: <item> (category, evidence)
- <file>: <item> (category, evidence)
Run Scout105 cleanup? [Yes / Skip / Selective]
Execution
- If approved, apply all selected cleanups in batch.
- Run tests/build to validate no regression.
- If failures occur, revert or investigate causality before proceeding.
Output expectations
- Stage 1: Provide refactor options + log path + remaining branches + recommended path.
- Stage 2: Provide plan, risks, validation checklist, and request confirmation.
- Stage 3: Provide Scout105 report, decision, and validation result.
1---2name: 52-execute-refactor-1503description: [52] EXECUTE. Three-stage refactoring workflow: (1) iterative research of refactor/modularization options, (2) plan + risk/edge-case analysis + Scope150 validation, then implement with tests after user confirmation, and (3) apply Scout105 cleanup protocol. Use when asked to refactor, modularize, or restructure code safely.4---5
6# Execute-Refactor 150 Protocol
7
8## Overview
9
10Run in three stages:
111. Research refactor options (iterative, no code changes).
122. Plan + confirm + implement with tests and validation.
133. Apply Scout105 cleanup (objective, small, user-approved).
14
15## Stage 1 — Research refactor options (iterative)
16
171. **Create or reuse investigation log** at `.temp/INVESTIGATION.md`.
18 - If the file does not exist, create it and add the template below.
19 - Keep all research notes in this file only (no scattered notes in chat).
20
212. **Define the core question** (the refactor goal).
22 - Write a single sentence: “We need to refactor X to achieve Y.”
23 - Example: “Refactor SermonCard to reduce duplication and isolate date logic.”
24
253. **Define scope (Scope150)**.
26 - **Core (100%)**: list files and behaviors you will directly change.
27 - **Boundary (50%)**: list callers, dependent modules, tests, configs, and data flow.
28 - If unsure, add a “to verify” bullet and resolve it during observations.
29
304. **Perform observations (search/read) in ordered layers**.
31 - **Interface layer**: find entry points (routes, public APIs, UI entry points).
32 - **Domain layer**: identify entities, i18n keys, enums, types.
33 - **Pattern layer**: locate hooks, services, clients, shared utilities.
34 - **Usage layer**: trace imports and call sites.
35 - For each layer, read the minimum number of files that explain the behavior.
36
375. **Record findings in the log with sources**.
38 - Each fact must include a source (file path + line reference or command).
39 - Separate facts from hypotheses. Do not mix them.
40
416. **Generate hypotheses and refactor options**.
42 - For each option, note: goal, steps, benefits, risks, and test impact.
43 - Prefer at least 2 options so tradeoffs are explicit.
44
457. **Decide if more research is needed**.
46 - If user says “research more”, expand scope or inspect new files.
47 - Update the log with new branches and continue Stage 1.
48
498. **Stop before implementation**.
50 - End Stage 1 with: options summary, remaining unknowns, and a recommended path.
51
52### Investigation log template
53
54```
55# Investigation Log: <short topic>
56
57## Core question
58- ...
59
60## Scope
61- Core (100%):
62 - ...
63- Boundary (50%):
64 - ...
65
66## Findings
67- <fact> (source: file path / command)
68 - Subfinding
69
70## Hypotheses
71- H1: ...
72 - Prediction: ...
73 - Test: ...
74 - Status: pending/confirmed/rejected
75
76## Refactor options
77- Option A: ... (pros/cons)
78- Option B: ... (pros/cons)
79
80## Next branches
81- ...
82```
83
84## Stage 2 — Plan, confirm, implement
85
861. **Write a refactor plan**.
87 - Break into ordered steps with file-level granularity.
88 - Include expected intermediate states (what should still pass after each step).
89
902. **List risks and edge cases**.
91 - Identify behavior changes, API contract risks, and hidden coupling.
92 - Explicitly note any breaking-change risk.
93
943. **Define a validation checklist**.
95 - Tests to run (unit/integration).
96 - Manual checks if needed (UI flows, API calls).
97 - Expected outputs and what would indicate failure.
98
994. **Perform Scope150 validation planning**.
100 - Core (100%): ensure each planned change has a test or validation step.
101 - Boundary (50%): list all callers/integrations/tests to be checked.
102
1035. **Ask for user confirmation before editing**.
104 - Provide plan, risks, and validation checklist.
105 - Do not edit until the user approves.
106
1076. **Implement the refactor**.
108 - Follow the plan in order.
109 - Keep changes minimal and reversible.
110
1117. **Add or update tests**.
112 - Cover changed logic and any new boundaries.
113 - Avoid brittle tests; prefer behavior-based assertions.
114
1158. **Run validation**.
116 - Execute all tests from the checklist.
117 - Record results and fix failures before moving to Stage 3.
118
119## Stage 3 — Scout105 cleanup protocol
120
121Apply only **objective** cleanup after a Key Point (phase complete). Do not add features.
122
123### Trigger (Key Point)
124- Apply only after a milestone where the user would see complete value.
125- Do not run per-file; run once per phase.
126
127### Allowed cleanup categories (objective only)
128- **Unused code**: unused imports/vars/fields (provable by grep/IDE).
129- **Typos**: spelling errors in comments/strings (spellcheckable).
130- **Formatting inconsistencies**: breaks the file’s own pattern.
131- **Dead code**: commented debug lines or unreachable code.
132- **Obviously wrong logic**: provably incorrect (duplicate checks, impossible types).
133
134### Constraints
135- Only in files already touched by the primary task.
136- Remove garbage only; no new features or refactors.
137- Each cleanup must take ≤ 30 seconds to verify + fix.
138- Total cleanup time ≤ 5% of primary task time.
139- Must be objectively measurable; if not measurable, skip.
140- Require user approval before executing cleanup.
141
142### Scout105 report (present to user)
143
144```
145✅ Key Point complete: <brief summary>
146
147Scout105 opportunities:
148- <file>: <item> (category, evidence)
149- <file>: <item> (category, evidence)
150
151Run Scout105 cleanup? [Yes / Skip / Selective]
152```
153
154### Execution
155- If approved, apply all selected cleanups in batch.
156- Run tests/build to validate no regression.
157- If failures occur, revert or investigate causality before proceeding.
158
159## Output expectations
160
161- Stage 1: Provide refactor options + log path + remaining branches + recommended path.
162- Stage 2: Provide plan, risks, validation checklist, and request confirmation.
163- Stage 3: Provide Scout105 report, decision, and validation result.