一键导入
safe-refactoring
Change code structure without changing behavior, with zero tolerance for behavioral changes
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Change code structure without changing behavior, with zero tolerance for behavioral changes
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | safe-refactoring |
| description | Change code structure without changing behavior, with zero tolerance for behavioral changes |
| tags | ["refactoring","testing","scientific","quality"] |
| version | 1 |
Change code structure without changing behavior. Zero tolerance for behavioral changes during refactoring.
Core principle: Establish baseline, refactor, verify exact match (within floating-point noise).
Announce at start: "I'm using the safe-refactoring skill to restructure this code."
Use for:
Don't use for:
Copy to TodoWrite:
Safe Refactoring Progress:
- [ ] Run full test suite (establish baseline)
- [ ] Run snapshot tests (establish baseline)
- [ ] Capture coverage report
- [ ] Perform refactoring
- [ ] Run full test suite (must match baseline exactly)
- [ ] Run snapshot tests (must match baseline exactly)
- [ ] Compare coverage (should stay same or improve)
- [ ] Run quality checks (ruff + black)
- [ ] Verify no numerical differences
- [ ] Commit refactoring
ZERO tolerance for:
If any of these occur: Revert and investigate why.
/Users/edeno/miniconda3/envs/non_local_detector/bin/pytest -v 2>&1 | tee /tmp/baseline_tests.txt
Record:
grep "passed" /tmp/baseline_tests.txtExpected: All tests pass (or document any known failures)
/Users/edeno/miniconda3/envs/non_local_detector/bin/pytest -m snapshot -v 2>&1 | tee /tmp/baseline_snapshots.txt
CRITICAL: Snapshots must match exactly after refactoring.
Expected: All snapshot tests pass
/Users/edeno/miniconda3/envs/non_local_detector/bin/pytest --cov=non_local_detector --cov-report=term --cov-report=json:coverage_baseline.json
Record: Coverage percentage for files being refactored
Why: Coverage should not decrease during refactoring (ideally improves)
Refactoring techniques:
Extract function:
# Before
def complex_function():
# ... 50 lines of code
result = x * 2 + y
# ... more code
return final_result
# After
def complex_function():
# ... code
result = _calculate_intermediate(x, y)
# ... code
return final_result
def _calculate_intermediate(x, y):
return x * 2 + y
Rename for clarity:
# Before
def f(x):
return x * 2
# After
def calculate_doubled_value(value):
return value * 2
Reorganize structure:
# Before: All in one file
# After: Separated into modules
# - core_logic.py
# - utilities.py
# - validation.py
Optimize performance (numerically equivalent):
# Before
for i in range(n):
result[i] = f(x[i])
# After (JAX)
result = jax.vmap(f)(x)
During refactoring:
/Users/edeno/miniconda3/envs/non_local_detector/bin/pytest -v 2>&1 | tee /tmp/refactored_tests.txt
Compare to baseline:
diff /tmp/baseline_tests.txt /tmp/refactored_tests.txt
MUST verify:
If differences:
/Users/edeno/miniconda3/envs/non_local_detector/bin/pytest -m snapshot -v 2>&1 | tee /tmp/refactored_snapshots.txt
CRITICAL: Must match baseline EXACTLY.
Expected: All snapshot tests pass, no differences
If snapshot differences:
/Users/edeno/miniconda3/envs/non_local_detector/bin/pytest --cov=non_local_detector --cov-report=term --cov-report=json:coverage_refactored.json
Compare:
# If you have jq installed
jq '.totals.percent_covered' coverage_baseline.json
jq '.totals.percent_covered' coverage_refactored.json
Expected:
If coverage decreased:
/Users/edeno/miniconda3/envs/spectral_connectivity/bin/ruff check src/
/Users/edeno/miniconda3/envs/spectral_connectivity/bin/ruff format src/
/Users/edeno/miniconda3/envs/non_local_detector/bin/black src/
Expected: All checks pass
Fix any issues: Refactoring is good opportunity to improve code quality
For mathematical code, verify numerical equivalence:
# Run golden regression
/Users/edeno/miniconda3/envs/non_local_detector/bin/pytest \
src/non_local_detector/tests/test_golden_regression.py -v
Expected: Exact match (or differences < 1e-14)
If differences > 1e-14:
Only commit if ALL checks pass:
git add <refactored_files> <test_files>
git commit -m "refactor: improve <component> code structure
- Extract <function> for reusability
- Rename <variable> for clarity
- Reorganize <module> structure
No behavioral changes:
- All tests pass (N tests)
- Snapshots unchanged
- Coverage: X% → Y%
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>"
When optimizing for performance:
Capture performance baseline:
pytest --durations=10 > /tmp/baseline_durations.txt
Make optimization
Verify numerical equivalence (use numerical-validation skill)
Measure performance improvement:
pytest --durations=10 > /tmp/optimized_durations.txt
Document improvement:
Optimization: Use JAX vmap instead of for loop
Speedup: 3.2x (450ms → 140ms)
Numerical difference: < 1e-14 (verified)
Task: Extract position decoding logic into reusable function
1. Baseline:
- Run pytest: 427 passed, 0 failed
- Run snapshots: 15 passed, 0 failed
- Coverage: 69%
2. Refactor:
- Extract _decode_position_from_posterior() function
- Update 3 call sites to use new function
- No logic changes, just extraction
3. Verify:
- Run pytest: 427 passed, 0 failed ✓
- Run snapshots: 15 passed, 0 failed ✓
- Coverage: 69% (unchanged) ✓
4. Quality:
- Ruff: All checks pass ✓
- Black: Formatted ✓
5. Commit:
"refactor: extract position decoding into reusable function"
STOP and revert if:
Safe to proceed if:
"It's just a small behavioral change"
"I'll update the snapshots since the new output is better"
"Tests are slow, I'll skip them"
"Coverage went down but the code is better"