surgical-code-refactoring

Patches large existing codebases with dependency-aware edits and pre-integration validation.

5|Updated Jun 13, 2026
One-click install
npx skills add https://github.com/PatriotAi/ai-lab --skill surgical-code-refactoring-patriotai
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: surgical-code-refactoring
Source: https://github.com/PatriotAi/ai-lab/tree/main/melania-skills-ecosystem/skills/surgical-code-refactoring
Command: npx skills add https://github.com/PatriotAi/ai-lab --skill surgical-code-refactoring-patriotai

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve? Adding features or fixing bugs in large working codebases often leads to full-file rewrites that break dependencies, waste tokens, and introduce invisible regressions. This Skill enforces a patch-over-rewrite methodology so existing functionality stays intact. ## Core Features & Use Cases - 6-Phase Process: Audit the current file state, map priorities by dependency, apply targeted str_replace patches, validate syntax and feature presence before integration, verify dependency wiring, and run regression checks on all prior features. - Token Economy: Uses grep and ranged file views instead of re-reading entire files, keeping context usage low on files with thousands of lines. - Language-Specific Patterns: Covers TypeScript typing, React component decomposition thresholds, CSS/Tailwind safe changes, Python pitfalls, dead code elimination, dependency injection, strangler fig migration, and SQL query optimization. - Use Case: You need to add a PDF export feature to a 2000-line single-file React/HTML app. The Skill audits insertion points, patches only the necessary spots, validates JSX via Babel before integrating, and confirms every old feature still works. ## Quick Start Ask the AI to add a feature to your large existing file surgically without rewriting it, validating the change before integrating and regression-checking afterwards.

Frequently Asked Questions about surgical-code-refactoring

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add a feature to a large file without breaking existing code?▼

Audit the exact insertion points with grep first, then apply targeted str_replace patches with unique surrounding context instead of rewriting the file. Validate syntax and simulate feature presence before integrating, then regression-check that all prior features still work.

How to refactor a large React component into smaller pieces?▼

Split the component when it exceeds 150 lines of JSX, more than 3 useState hooks, or more than 2 useEffect hooks. Extract logic into custom hooks, separate presentational from container components, and verify behavior stays unchanged after each extraction.

When should I not use a surgical patching approach?▼

Avoid this method for greenfield projects built from scratch or tiny scripts under 100 lines, where full authorship is simpler. It is optimized for editing large existing codebases where preserving working functionality matters.

Why do fullwidth characters appear after pasting code blocks?▼

Heredoc or copy-paste insertion can silently convert ASCII parentheses and commas into fullwidth variants, breaking the code. After every large insertion, grep for fullwidth punctuation characters and fix any occurrences before validating.

How do I validate JSX changes before integrating them?▼

Extract the inline babel script block and run it through Babel transformSync with the React preset to catch syntax errors. Then run a feature-presence simulation that greps for each promised capability marker in the file.

What is the strangler fig pattern for legacy code migration?▼

Build the new code alongside the old system, redirect one path at a time to the new implementation, and verify each step while the old system remains as fallback. Once all paths migrate, delete the legacy code with minimal risk.