KMP Refactor Safety Skill
Use this skill when the task is primarily a refactor, migration, reliability hardening, or architectural cleanup of existing code (not a greenfield feature).
This skill is about change discipline and reviewability, not system architecture itself. Pair it with your architecture/style/testing skills as needed.
Primary objective
Deliver refactors that are:
- minimal in surface area
- safe to review and merge
- free of parallel/competing implementations
- regression-resistant (tests + observability)
Non-negotiables
1) Scope control (no opportunistic rewrites)
- Touch only files required to achieve the goal.
- Do not reformat, rename, or “clean up” unrelated code.
- Avoid broad mechanical changes unless explicitly requested.
- Keep diffs small and intention-revealing.
2) No parallel implementations
- Do not introduce a new pattern while leaving the old pattern active.
- End state must be one of:
- old path removed, OR
- old path gated behind a feature flag with a clear removal plan (explicit TODO + tracking issue).
- Avoid duplicate sources of truth. If ownership must move, move it once and rewire callers deterministically.
3) Preserve public contracts unless required
- Keep public APIs stable (interfaces, model shapes, routing/public endpoints, configuration surface).
- If a contract must change:
- update all call sites in the same change-set
- document the breaking change in PR notes/release notes
- add targeted tests proving compatibility and intent
4) Refactor in safe phases
Prefer this order:
- Foundation: introduce the new core abstraction (e.g., manager/store/service) and wire it without changing behavior.
- Adoption: migrate existing call sites incrementally to the new abstraction.
- Lock-in: remove the old path or gate it behind a flag (default off) and make the new path the default.
- Cleanup: delete dead code, consolidate configuration, reduce complexity.
Never ship an ambiguous half-state where both old and new paths may run without a flag and clear routing.
5) Observability (redacted)
For flows prone to edge cases (auth, payments, retries, background/foreground, webhooks, state restoration):
- add logs/metrics around transitions and decisions
- never log secrets (tokens, passwords, codes, personal data)
- log only:
- booleans/branch decisions
- status codes and operation names
- hashed or truncated IDs if needed (e.g., last 4 chars)
- ensure logs can be disabled or are appropriate for production environments
6) Tests are part of the refactor
Minimum expectations:
- unit tests for pure logic/state transitions/mappers
- regression tests for the bug or failure mode motivating the refactor
- coverage for:
- success path
- failure path
- concurrency/race behavior when applicable (mutex, retry, idempotency)
- avoid brittle tests; prefer deterministic inputs/outputs and stable assertions
7) Idempotency and re-entrancy for callbacks
For events that can repeat (callbacks, deep links, webhooks, background resume):
- handle duplicates safely
- validate inputs before side effects
- operations should be safe to retry without corrupting state
8) Backwards compatibility and migration safety
- Use versioning or feature flags for behavior changes that might break existing users.
- Provide migration steps (data migrations, config updates) in a single place.
- If data shape changes:
- define transitional support window
- ensure rollback strategy (or non-destructive migrations)
9) Performance and resource safety
- Avoid adding disk/network reads on hot paths (e.g., per-request storage reads).
- Prefer caching with explicit invalidation when correctness requires it.
- Ensure retries are bounded (max 1 retry unless specified) and do not create loops.
10) PR hygiene (reviewable output)
- Prefer small, coherent commits with clear messages.
- Avoid reordering code blocks unless necessary.
- Include PR notes:
- What changed
- Why
- How to test
- Flags/migration steps
- Rollback plan (if relevant)
Quick refactor checklist
1---2name: kotlin-kmp-refactor-safety3description: Use when the task is primarily a refactor, migration, reliability hardening, or architectural cleanup of existing Kotlin Multiplatform code — scope control, behavioral preservation, migration safety, compatibility, observability, and rollback planning.4license: Apache-2.05---67# KMP Refactor Safety Skill89Use this skill when the task is primarily a refactor, migration, reliability hardening, or architectural cleanup of existing code (not a greenfield feature).10This skill is about **change discipline** and **reviewability**, not system architecture itself. Pair it with your architecture/style/testing skills as needed.1112## Primary objective1314Deliver refactors that are:15- minimal in surface area16- safe to review and merge17- free of parallel/competing implementations18- regression-resistant (tests + observability)1920## Non-negotiables2122### 1) Scope control (no opportunistic rewrites)23- Touch only files required to achieve the goal.24- Do not reformat, rename, or “clean up” unrelated code.25- Avoid broad mechanical changes unless explicitly requested.26- Keep diffs small and intention-revealing.2728### 2) No parallel implementations29- Do not introduce a new pattern while leaving the old pattern active.30- End state must be one of:31 - old path removed, OR32 - old path gated behind a **feature flag** with a clear removal plan (explicit TODO + tracking issue).33- Avoid duplicate sources of truth. If ownership must move, move it once and rewire callers deterministically.3435### 3) Preserve public contracts unless required36- Keep public APIs stable (interfaces, model shapes, routing/public endpoints, configuration surface).37- If a contract must change:38 - update all call sites in the same change-set39 - document the breaking change in PR notes/release notes40 - add targeted tests proving compatibility and intent4142### 4) Refactor in safe phases43Prefer this order:441. **Foundation**: introduce the new core abstraction (e.g., manager/store/service) and wire it without changing behavior.452. **Adoption**: migrate existing call sites incrementally to the new abstraction.463. **Lock-in**: remove the old path or gate it behind a flag (default off) and make the new path the default.474. **Cleanup**: delete dead code, consolidate configuration, reduce complexity.4849Never ship an ambiguous half-state where both old and new paths may run without a flag and clear routing.5051### 5) Observability (redacted)52For flows prone to edge cases (auth, payments, retries, background/foreground, webhooks, state restoration):53- add logs/metrics around transitions and decisions54- never log secrets (tokens, passwords, codes, personal data)55- log only:56 - booleans/branch decisions57 - status codes and operation names58 - hashed or truncated IDs if needed (e.g., last 4 chars)59- ensure logs can be disabled or are appropriate for production environments6061### 6) Tests are part of the refactor62Minimum expectations:63- unit tests for pure logic/state transitions/mappers64- regression tests for the bug or failure mode motivating the refactor65- coverage for:66 - success path67 - failure path68 - concurrency/race behavior when applicable (mutex, retry, idempotency)69- avoid brittle tests; prefer deterministic inputs/outputs and stable assertions7071### 7) Idempotency and re-entrancy for callbacks72For events that can repeat (callbacks, deep links, webhooks, background resume):73- handle duplicates safely74- validate inputs before side effects75- operations should be safe to retry without corrupting state7677### 8) Backwards compatibility and migration safety78- Use versioning or feature flags for behavior changes that might break existing users.79- Provide migration steps (data migrations, config updates) in a single place.80- If data shape changes:81 - define transitional support window82 - ensure rollback strategy (or non-destructive migrations)8384### 9) Performance and resource safety85- Avoid adding disk/network reads on hot paths (e.g., per-request storage reads).86- Prefer caching with explicit invalidation when correctness requires it.87- Ensure retries are bounded (max 1 retry unless specified) and do not create loops.8889### 10) PR hygiene (reviewable output)90- Prefer small, coherent commits with clear messages.91- Avoid reordering code blocks unless necessary.92- Include PR notes:93 - What changed94 - Why95 - How to test96 - Flags/migration steps97 - Rollback plan (if relevant)9899## Quick refactor checklist100- [ ] Single source of truth after refactor101- [ ] No stale in-memory state alongside persisted state (unless intentionally synchronized)102- [ ] No competing interceptors/validators/plugins that overwrite each other103- [ ] Duplicate events/callbacks are handled idempotently104- [ ] Failure modes are explicit and do not cause silent data loss/logouts105- [ ] Tests cover the motivating regression and key edge cases106- [ ] Clear migration/flag/rollback notes included