Refactor companion
flowchart TD
Source[Authorized edit request] --> Gate{Entry gate met}
Gate -->|No| Ask[Ask for concrete surface]
Gate -->|Yes| Bind[Bind scope + boundary]
Bind --> Mode{Mode choice}
Mode -->|Discovery| Discover[Evidence trace]
Mode -->|Decision| Decide[Show one branch question]
Mode -->|Direct slice| Slice[Define one slice]
Mode -->|Cutover| Cutover[Residue sweep]
Discover --> DecisionPoint{Has decision?}
DecisionPoint -->|Yes| Decide --> DecisionOut[Pause for human choice]
DecisionPoint -->|No| Slice
Decide --> Next{User answers?}
Next -->|Yes| Slice
Slice --> Edit[Small coherent edit]
Edit --> Proof[Slice proof ladder]
Proof --> CheckCutover{Replace concept?}
CheckCutover -->|Yes| Cutover
CheckCutover -->|No| Report[Internal card + boundary]
Cutover --> Report
Report --> NextStep{Boundary permits continue}
NextStep -->|Yes| Slice
NextStep -->|No| Done[Finish handoff]
Preserve the developer's target design while changing existing code in small, coherent, proven slices. Read facts first. Ask only for material human decisions. Remove obsolete design residue and protect unrelated work.
Internal record → refactor-card.md. Mode selection → interaction-modes.md. Slice proof → slice-proof.md. Cutover rules → residue-and-cutover.md. User-facing reports → output-format.md.
Extended design vocabulary is available in codebase-design.md. The core workflow remains complete without companion skills.
Composition boundaries → process-skill-composition.md.
Entry gate
- The user authorizes edits to an existing code surface.
- The task replaces, simplifies, migrates, cuts over, reassigns ownership, or removes an old design shape.
- A repository, branch, worktree, commit, pull request, or named path is in scope.
- If the request is analysis-only, stay read-only and explain that implementation is not active.
- If no concrete surface exists, ask for one. Do not invent code from a scenario description.
Core contract
- Treat the developer's stated target, required shape, and prohibited shape as design authority.
- Inspect current behavior, callers, contracts, tests, local style, and worktree state before editing.
- Ask only when a material human-owned decision remains after repository inspection.
- Never reopen a resolved decision unless new evidence conflicts with it. Show that evidence first.
- Define one coherent slice and its proof before editing.
- Implement, prove, and inspect the slice for residue from the old design.
- Continue automatically while the next slice follows from agreed decisions and stays in scope.
- Stop at contract, authority, scope, or evidence boundaries.
- Preserve unrelated work. Git state changes need explicit user authority.
- Use pragmatic Simple English for previews, questions, progress, and reports.
- Use short Mermaid charts for transitions between modes, slice lifecycle, and residue decision points.
- Link every repository-backed claim in user-facing output to the exact file and line that supports it. Use a clickable Markdown file link with a repo-relative label and an absolute workspace target; the target is the absolute workspace path followed by
:line (for example, label src/owner.ts:42, target /absolute/workspace/app/src/owner.ts:42). Never leave a source path or file:line citation as bare text when a link can be made.
Refactor card
Keep this record internal and current:
Outcome:
Invariants:
Required shape:
Prohibited shape:
Style evidence:
Resolved decisions:
Open decision:
Current slice:
Proof:
Stop if:
Do not dump the card into routine user updates. Details → refactor-card.md.
flowchart LR
RequestedShape[Target shape] --> Outcome[Outcome]
Outcome --> Invariants[Invariant checks]
Invariants --> CurrentSlice[Current slice]
CurrentSlice --> Proof[Evidence proof]
Proof --> Residue[Residue decision]
Residue --> StopIf[Stop condition]
Choose the current mode
| Mode |
Use when |
| Discovery before change |
Behavior, consumers, or invariants are unclear |
| Decision checkpoint |
Two credible choices change behavior, ownership, compatibility, or scope |
| Direct slice |
Target, contract, and scope are clear |
| Cutover sweep |
A path, concept, or abstraction was replaced |
Use one mode for the current turn. A refactor can move between modes. Read interaction-modes.md.
Several unresolved architecture branches need dedicated design dialogue before implementation. One local blocking decision stays inside this workflow.
Slice loop
- Bind the worktree, scope, and protected unrelated changes.
- Fill the card from the request and repository evidence.
- Select the current mode.
- For a direct slice, state its outcome, change, scope, proof, and stop condition in compact form, with clickable links to the exact files and lines behind each material claim.
- Make the smallest coherent edit that expresses the target design.
- Run focused proof and inspect the changed path for old names, duplicate paths, wrappers, contracts, tests, fixtures, mocks, docs, and telemetry. Link the reported evidence to the exact files and lines inspected.
- Remove confirmed in-scope residue or retain it for a named live reason.
- Update the internal card. Continue or report the exact boundary.
Questions
Before asking, search callers, read the controlling contract, inspect nearby accepted code, and remove choices that evidence settles.
When a human decision remains:
- show the controlling evidence;
- ask one decision branch with one to three lettered options;
- mark one recommendation when evidence supports it;
- pause without editing the disputed shape.
Do not ask for repository facts or routine edit permission inside an agreed slice.
Boundaries
- Stop when the requested shape conflicts with a live caller, public contract, user decision, authority boundary, or coherent scope.
- Separate source failures from missing dependencies, unavailable services, permissions, and other environment limits.
- A green test does not prove that the target architecture exists. Use caller, contract, search, diff, and runtime evidence as relevant.
- Do not turn uncertainty into speculative cleanup.
- Do not commit, push, merge, reset, rebase, or clean unless the user explicitly authorizes that action.
- Formal findings and merge decisions remain separate review work.
Finish
Use output-format.md. Offer an installed walkthrough or formal review as a next action. Do not activate either automatically or present this process as merge readiness.
Consumer bindings
Project instructions supply local contracts, validation commands, and accepted style evidence. Do not edit installed copies in place.
1---2name: refactor-companion3description: Preserve a developer's target design through evidence-led, proven refactor slices. Use for existing-code replacement, simplification, migration, cutover, ownership change, or deliberate removal of an old shape. Not ordinary feature work, analysis-only review, or autonomous Git operations.4---56# Refactor companion78<!-- source-of-truth: evidence-led refactor collaboration from target design to proven cutover. -->9<!-- doc-meta: owner=eng | last-reviewed=2026-09-03 -->1011```mermaid12flowchart TD13 Source[Authorized edit request] --> Gate{Entry gate met}14 Gate -->|No| Ask[Ask for concrete surface]15 Gate -->|Yes| Bind[Bind scope + boundary]16 Bind --> Mode{Mode choice}17 Mode -->|Discovery| Discover[Evidence trace]18 Mode -->|Decision| Decide[Show one branch question]19 Mode -->|Direct slice| Slice[Define one slice]20 Mode -->|Cutover| Cutover[Residue sweep]21 Discover --> DecisionPoint{Has decision?}22 DecisionPoint -->|Yes| Decide --> DecisionOut[Pause for human choice]23 DecisionPoint -->|No| Slice24 Decide --> Next{User answers?}25 Next -->|Yes| Slice26 Slice --> Edit[Small coherent edit]27 Edit --> Proof[Slice proof ladder]28 Proof --> CheckCutover{Replace concept?}29 CheckCutover -->|Yes| Cutover30 CheckCutover -->|No| Report[Internal card + boundary]31 Cutover --> Report32 Report --> NextStep{Boundary permits continue}33 NextStep -->|Yes| Slice34 NextStep -->|No| Done[Finish handoff]35```3637Preserve the developer's target design while changing existing code in small, coherent, proven slices. Read facts first. Ask only for material human decisions. Remove obsolete design residue and protect unrelated work.3839Internal record → [refactor-card.md](references/refactor-card.md). Mode selection → [interaction-modes.md](references/interaction-modes.md). Slice proof → [slice-proof.md](references/slice-proof.md). Cutover rules → [residue-and-cutover.md](references/residue-and-cutover.md). User-facing reports → [output-format.md](references/output-format.md).4041Extended design vocabulary is available in [codebase-design.md](https://raw.githubusercontent.com/csark0812/toolbox/main/references/codebase-design.md). The core workflow remains complete without companion skills.4243Composition boundaries → [process-skill-composition.md](https://raw.githubusercontent.com/csark0812/toolbox/main/references/process-skill-composition.md).4445## Entry gate4647- The user authorizes edits to an existing code surface.48- The task replaces, simplifies, migrates, cuts over, reassigns ownership, or removes an old design shape.49- A repository, branch, worktree, commit, pull request, or named path is in scope.50- If the request is analysis-only, stay read-only and explain that implementation is not active.51- If no concrete surface exists, ask for one. Do not invent code from a scenario description.5253## Core contract54551. Treat the developer's stated target, required shape, and prohibited shape as design authority.562. Inspect current behavior, callers, contracts, tests, local style, and worktree state before editing.573. Ask only when a material human-owned decision remains after repository inspection.584. Never reopen a resolved decision unless new evidence conflicts with it. Show that evidence first.595. Define one coherent slice and its proof before editing.606. Implement, prove, and inspect the slice for residue from the old design.617. Continue automatically while the next slice follows from agreed decisions and stays in scope.628. Stop at contract, authority, scope, or evidence boundaries.639. Preserve unrelated work. Git state changes need explicit user authority.6410. Use pragmatic Simple English for previews, questions, progress, and reports.6511. Use short Mermaid charts for transitions between modes, slice lifecycle, and residue decision points.6612. Link every repository-backed claim in user-facing output to the exact file and line that supports it. Use a clickable Markdown file link with a repo-relative label and an absolute workspace target; the target is the absolute workspace path followed by `:line` (for example, label `src/owner.ts:42`, target `/absolute/workspace/app/src/owner.ts:42`). Never leave a source path or `file:line` citation as bare text when a link can be made.6768## Refactor card6970Keep this record internal and current:7172```text73Outcome:74Invariants:75Required shape:76Prohibited shape:77Style evidence:78Resolved decisions:79Open decision:80Current slice:81Proof:82Stop if:83```8485Do not dump the card into routine user updates. Details → [refactor-card.md](references/refactor-card.md).8687```mermaid88flowchart LR89 RequestedShape[Target shape] --> Outcome[Outcome]90 Outcome --> Invariants[Invariant checks]91 Invariants --> CurrentSlice[Current slice]92 CurrentSlice --> Proof[Evidence proof]93 Proof --> Residue[Residue decision]94 Residue --> StopIf[Stop condition]95```9697## Choose the current mode9899| Mode | Use when |100| --------------------------- | ------------------------------------------------------------------------ |101| **Discovery before change** | Behavior, consumers, or invariants are unclear |102| **Decision checkpoint** | Two credible choices change behavior, ownership, compatibility, or scope |103| **Direct slice** | Target, contract, and scope are clear |104| **Cutover sweep** | A path, concept, or abstraction was replaced |105106Use one mode for the current turn. A refactor can move between modes. Read [interaction-modes.md](references/interaction-modes.md).107108Several unresolved architecture branches need dedicated design dialogue before implementation. One local blocking decision stays inside this workflow.109110## Slice loop1111121. Bind the worktree, scope, and protected unrelated changes.1132. Fill the card from the request and repository evidence.1143. Select the current mode.1154. For a direct slice, state its outcome, change, scope, proof, and stop condition in compact form, with clickable links to the exact files and lines behind each material claim.1165. Make the smallest coherent edit that expresses the target design.1176. Run focused proof and inspect the changed path for old names, duplicate paths, wrappers, contracts, tests, fixtures, mocks, docs, and telemetry. Link the reported evidence to the exact files and lines inspected.1187. Remove confirmed in-scope residue or retain it for a named live reason.1198. Update the internal card. Continue or report the exact boundary.120121## Questions122123Before asking, search callers, read the controlling contract, inspect nearby accepted code, and remove choices that evidence settles.124125When a human decision remains:126127- show the controlling evidence;128- ask one decision branch with one to three lettered options;129- mark one recommendation when evidence supports it;130- pause without editing the disputed shape.131132Do not ask for repository facts or routine edit permission inside an agreed slice.133134## Boundaries135136- Stop when the requested shape conflicts with a live caller, public contract, user decision, authority boundary, or coherent scope.137- Separate source failures from missing dependencies, unavailable services, permissions, and other environment limits.138- A green test does not prove that the target architecture exists. Use caller, contract, search, diff, and runtime evidence as relevant.139- Do not turn uncertainty into speculative cleanup.140- Do not commit, push, merge, reset, rebase, or clean unless the user explicitly authorizes that action.141- Formal findings and merge decisions remain separate review work.142143## Finish144145Use [output-format.md](references/output-format.md). Offer an installed walkthrough or formal review as a next action. Do not activate either automatically or present this process as merge readiness.146147## Consumer bindings148149Project instructions supply local contracts, validation commands, and accepted style evidence. Do not edit installed copies in place.