协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
karpathy-surgical-changes【开发与工程】Enforces surgical code modification discipline — touch only what the request requires, read full context before editing, match existing codebase conventions, clean up only your own orphans, and never refactor adjacent code that isn't broken.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: karpathy-surgical-changes description: Enforces surgical code modification discipline — touch only what the request requires, read full context before editing, match existing codebase conventions, clean up only your own orphans, and never refactor adjacent code that isn't broken. license: MIT compatibility: opencode archetypes:
Senior engineer applying surgical modification discipline: read the full context before editing, change only what the request demands, match existing codebase conventions even when you disagree, clean up only the orphans your changes create, and never refactor adjacent code that is not broken.
Derived from Andrej Karpathy's observations on LLM coding pitfalls — specifically the tendency to make sweeping changes when minimal edits are requested.
Use this skill when:
Avoid this skill for:
karpathy-coding-mindset insteadRead Before You Write — Open the file(s) you need to modify. Read the entire file top to bottom. Understand the module's imports, function signatures, naming conventions, error handling patterns, and data flow. Checkpoint: Before writing any code, you should be able to describe the file's structure and conventions from memory.
Define the Minimal Change Set — List every file and function you plan to touch. For each change, state in one sentence how it traces to the user's request. If a change cannot be directly traced, remove it from the plan. Checkpoint: The union of all planned changes should exactly equal the user's request — no more, no less.
Match Existing Conventions — Identify the codebase's existing patterns for: naming (snake_case vs camelCase), imports (absolute vs relative), error handling (exceptions vs returns), formatting (line length, indentation), and testing style. Apply these conventions in your changes — even if you prefer a different style. Checkpoint: Your diff should be indistinguishable from code written by the project's existing authors.
Make the Surgical Edit — Implement exactly the changes from your plan. Do not reformat adjacent lines, do not update comments unless they refer to your changed code, do not add blank lines or remove them unless it's part of your change. Checkpoint: Run git diff and verify each changed line traces to the request.
Clean Up Your Orphans — Scan for imports, variables, or functions that YOUR edits made unused. Remove them. Do NOT touch pre-existing dead code — if you find any, leave a comment like # TODO: unused — pre-existing, not part of this change rather than deleting it. Checkpoint: The only deletions in your diff should be orphans created by your additions.
Verify No Scope Creep — Re-read the diff one final time. Classify every change as: (a) required by the request, (b) orphan cleanup from your changes, or (c) anything else. If category (c) is non-empty, revert those changes immediately. Checkpoint: Category (c) must be empty.
# ❌ BAD — sweeping change: reformats, renames, refactors adjacent code
# Before: original file
def calc(a, b):
# Old calculation
res = a + b
return res
# After: "improved" version — renamed, reformatted, adjacent code changed
def calculate_total(value_one: float, value_two: float) -> float:
"""Calculate the total of two values.
This function was renamed from calc to be more descriptive.
Parameters were renamed from a,b to value_one,value_two.
Added type hints. Reformatted docstring. Changed return style.
None of these changes were requested.
"""
result_value = value_one + value_two
return result_value
# ✅ GOOD — surgical change: only what was requested
# Before: original file
def calc(a, b):
res = a + b
return res
# Request: handle negative numbers by returning 0
def calc(a, b):
res = a + b
# Handle negative results per request
if res < 0:
return 0
return res
# Changed lines: 3 lines added. Everything else preserved.
# ❌ BAD — deletes pre-existing dead code, misses own orphans
# Before: when adding a new function, you deleted unrelated dead code
import os # ← your code doesn't use os anymore, but it was unused before your change
import json # ← your code doesn't use json, pre-existing but should stay
from utils import old_helper, new_helper # old_helper was pre-existing dead code — you deleted it
def process(data):
# Your new implementation using new_helper
result = new_helper(data)
return result
# You forgot to remove the import of old_helper that YOUR change made unnecessary
# ✅ GOOD — removes own orphans, leaves pre-existing alone
import os # pre-existing, not touched
import json # pre-existing, not touched
from utils import old_helper, new_helper # old_helper was needed before, not touched
def process(data):
# Your new implementation using new_helper
result = new_helper(data)
return result
# Note: old_helper is still imported and that's fine — it was used elsewhere before
# ❌ BAD — imposing your style on an existing codebase
# Project convention: 2-space indentation, no type hints, exceptions for errors
# What you wrote:
def fetch_user(user_id: int) -> User | None:
"""Fetch user by ID from the database.
Args:
user_id: The user's unique identifier
Returns:
User object if found, None otherwise
"""
try:
return database.query(User).filter_by(id=user_id).first()
except DatabaseError:
return None
# ✅ GOOD — matches existing conventions
# Project convention: 2-space indentation, no type hints, exceptions for errors
# What you wrote (matching project style):
def fetch_user(user_id):
"""Fetch user by ID, raises on DB error, returns None if not found."""
return database.query(User).filter_by(id=user_id).first()
# ❌ BAD — "improving" adjacent code while making requested change
# Request: "Add validation that email is not empty"
def create_user(name, email):
# You noticed the variable name is vague so you "improved" it
# You also reformatted the docstring
# You also added type hints (not in project style)
# You also fixed a typo in a comment on line 3
# None of these were requested
# Create new user with provided name and emial ← typo existed, you didn't touch it
user = User(name=name, email=email) # ← you changed 'name' to 'user_name' here
user.save()
return user
# ✅ GOOD — only the requested change, nothing else
# Request: "Add validation that email is not empty"
def create_user(name, email):
# Create new user with provided name and emial ← pre-existing typo, not touched
if not email: # ← only addition, per request
raise ValueError("Email is required")
user = User(name=name, email=email)
user.save()
return user
git diff| Skill | Purpose |
|---|---|
karpathy-coding-mindset | Pre-implementation discipline for new code — state assumptions, keep it simple |
karpathy-goal-driven-execution | Post-implementation verification — define success criteria and verify |
code-review | Reviewing diffs for quality and correctness |
refactoring-techniques | When intentional, large-scale refactoring is the goal (not this skill) |