Code Slob Cleanup Skill
This skill orchestrates the entire lifecycle of cleaning up "code slob": identification, safe refactoring, verification, and application.
References
- Refactoring:
references/refactor.md
- Prompts:
references/prompts.md
- Identification:
references/identification.md
Workflow Direction: If the user requests a cleanup, follow the "Refactor" workflow. If they request a revert, skip to the "Revert" workflow. Always strictly adhere to any exclusions specified in code-slob-cleanup.json OR by inline comments in the source code.
Refactor Workflow
Phase 1: Identification & Setup
Follow the instructions in references/identification.md to identify slob candidates and set up the temporary workspace.
- Result: A
.code-slob-tmp directory exists, containing subdirectories (jobs) for each identified target, each with an original.py and type_hints.json.
- CRITICAL: ONLY add type hints to
type_hints.json if the original function's parameters weren't already typed in the source code.
- Efficiency: Do NOT re-read
references/identification.md if you already have its content in memory from previous turns.
Phase 2: RefactoringFollow the instructions in references/refactor.md to generate refactored.py for the job(s) in the temporary workspace.
- Context: You are working inside
.code-slob-tmp/.
- Goal: Create
refactored.py next to original.py containing the refactored versions of the extracted functions.
- Efficiency: Do NOT re-read
references/refactor.md or references/prompts.md if you already have their content in memory from previous turns.
Phase 3: Verification
Follow the instructions in Section 3 of references/refactor.md to verify the refactoring.
- Command:
uv run scripts/orchestrator.py .code-slob-tmp
- Action: If verification fails, follow the iteration steps in
references/refactor.md (fix refactored.py and retry).
- Retry Limit: You have a maximum of 3 attempts to fix and verify.
- Failure Handling: If a function still fails verification after 3 attempts, explicitly IGNORE the refactored code for that function. Do NOT introduce it back into the original codebase. Report the failure to the user.
Phase 4: Application (Patch)
- Check Result: For each function, check if verification passed (
[PASS]).
- CRITICAL: ONLY apply refactored code for functions that received a
[PASS].
- IGNORE: If a function received
[FAIL] OR [SKIP], you MUST NOT apply the refactored version of that function to the original codebase.
- Patch: Apply the refactored code to the original source files.
- Tooling: Use the
write_file or replace tools directly. DO NOT use shell redirects (e.g., cat << EOF > file.py) as these may be blocked by security policies.
- Full Replacement: If
refactored.py represents the complete file (imports + code), overwrite the target file.
- Merge: If
refactored.py only contains functions, use text replacement to update the target file, preserving surrounding code.
- NO REDUNDANT READS: Do NOT re-read the target file (e.g.,
utils.py) if you have already read it. Do NOT perform "final check" reads after writing. Trust your tool outputs and memory.
- Run Existing Tests: After applying the patches, identify and run any existing tests in the repository.
- Discover: Look for
tests/, test_*.py, pytest.ini, or test commands in README.md / package.json / pyproject.toml.
- Execute: Run the tests using the appropriate tool (e.g.,
pytest, uv run tests/run_tests.py).
- Fix Brittle Tests: If existing tests fail for a function that Hypothesis verified as
[PASS]:
- Analyze: Determine if the test is "brittle" (e.g., asserting on internal state, specific log messages, or private members that were cleanly refactored away).
- Fix: Modify the existing test so it passes with the new, cleaner code. Ensure you maintain the original intent of the test (verifying functionality) while removing the brittle dependency on the "slob" implementation.
- Record Edits: Add an entry to the
edits dictionary in code-slob-cleanup.json for each successfully refactored function.
- Format:
"identifier": "commit-hash".
- Function Mapping: The identifier MUST include the full file path and class name (if any).
- Example (top-level):
"path/to/file.py:function_name": "5akekdl"
- Example (class method):
"path/to/file.py:ClassName.method_name": "5akekdl"
- Commit Hash: Use the first 7 characters of the most recent commit (the parent commit of your changes). Use
git rev-parse --short=7 HEAD to retrieve it.
- Constraint: ONLY log individual function refactors in the
edits dictionary. Do NOT log classes or files.
- Report: Inform the user that the code has been cleaned, verified by Hypothesis, and that existing tests have been run (and updated if necessary).
Phase 5: Cleanup
- Remove Workspace: Delete the temporary directory.
Revert Workflow
Constraint: If no code-slob-cleanup.json exists, or if the edits dictionary is empty, inform the user that there are no recorded cleanups to revert and exit. Otherwise, follow the steps below to revert the specified cleanup(s):
- Identify: If the user asks to revert a cleanup (e.g., "Revert the cleanup for
process_data", "Revert src/utils.py", or "Revert src/logic/"), search the edits dictionary in code-slob-cleanup.json for matches.
- Scan for Matches:
- Specific Function: Look for a direct match (e.g.,
path/to/file.py:process_data).
- Class: Search for all entries that include that class name in their identifier (e.g.,
path/to/file.py:MyClass.*).
- File: Search for all entries starting with that file's path (e.g.,
path/to/file.py:*).
- Folder: Search for all entries whose path starts with that folder (e.g.,
path/to/folder/*).
- Find Hashes: For each match, retrieve the associated 7-character commit hash (the parent commit where the original code exists).
- Locate & Extract: For each matched target:
- Use git to read the content of the original file at that commit (e.g.,
git show <hash>:<path/to/file.py>).
- Extract the original version of the function/class.
- Revert: Replace the refactored code in the current codebase with the original version extracted from git.
- Update: Remove all matching entries from the
edits dictionary in code-slob-cleanup.json.
- Verify: Run existing tests to ensure the revert didn't break anything.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: code-slob-cleanup3description: Use when working with a comprehensive skill to identify "code slob" (technical debt, duplication, slop), refactor it into clean, idiomatic Python, and rigorously verify the changes using property-based testing. Use when the user asks for their code to be refactored, cleaned up, or to have slob/slop removed. Also use when the user asks to remove code not covered by a given test. Also use when the user asks to revert a previous cleanup.4---56# Code Slob Cleanup Skill78This skill orchestrates the entire lifecycle of cleaning up "code slob": identification, safe refactoring, verification, and application.910## References11- **Refactoring**: `references/refactor.md`12- **Prompts**: `references/prompts.md`13- **Identification**: `references/identification.md`1415**Workflow Direction**: If the user requests a cleanup, follow the "Refactor" workflow. If they request a revert, skip to the "Revert" workflow. Always strictly adhere to any exclusions specified in `code-slob-cleanup.json` OR by inline comments in the source code.1617## Refactor Workflow1819### Phase 1: Identification & Setup20Follow the instructions in `references/identification.md` to identify slob candidates and set up the temporary workspace.21* **Result**: A `.code-slob-tmp` directory exists, containing subdirectories (jobs) for each identified target, each with an `original.py` and `type_hints.json`.22* **CRITICAL**: ONLY add type hints to `type_hints.json` if the original function's parameters weren't already typed in the source code.23* **Efficiency**: Do NOT re-read `references/identification.md` if you already have its content in memory from previous turns.2425### Phase 2: RefactoringFollow the instructions in `references/refactor.md` to generate `refactored.py` for the job(s) in the temporary workspace.26* **Context**: You are working inside `.code-slob-tmp/`.27* **Goal**: Create `refactored.py` next to `original.py` containing the refactored versions of the extracted functions.28* **Efficiency**: Do NOT re-read `references/refactor.md` or `references/prompts.md` if you already have their content in memory from previous turns.2930### Phase 3: Verification31Follow the instructions in **Section 3 of `references/refactor.md`** to verify the refactoring.32* **Command**: `uv run scripts/orchestrator.py .code-slob-tmp`33* **Action**: If verification fails, follow the iteration steps in `references/refactor.md` (fix `refactored.py` and retry).34 * **Retry Limit**: You have a maximum of 3 attempts to fix and verify.35 * **Failure Handling**: If a function still fails verification after 3 attempts, explicitly **IGNORE** the refactored code for that function. Do **NOT** introduce it back into the original codebase. Report the failure to the user.3637### Phase 4: Application (Patch)381. **Check Result**: For each function, check if verification passed (`[PASS]`). 39 * **CRITICAL**: ONLY apply refactored code for functions that received a `[PASS]`.40 * **IGNORE**: If a function received `[FAIL]` OR `[SKIP]`, you MUST NOT apply the refactored version of that function to the original codebase.412. **Patch**: Apply the refactored code to the original source files.42 * **Tooling**: Use the `write_file` or `replace` tools directly. **DO NOT** use shell redirects (e.g., `cat << EOF > file.py`) as these may be blocked by security policies.43 * **Full Replacement**: If `refactored.py` represents the complete file (imports + code), overwrite the target file.44 * **Merge**: If `refactored.py` only contains functions, use text replacement to update the target file, preserving surrounding code.45 * **NO REDUNDANT READS**: Do NOT re-read the target file (e.g., `utils.py`) if you have already read it. Do NOT perform "final check" reads after writing. Trust your tool outputs and memory.463. **Run Existing Tests**: After applying the patches, identify and run any existing tests in the repository.47 * **Discover**: Look for `tests/`, `test_*.py`, `pytest.ini`, or test commands in `README.md` / `package.json` / `pyproject.toml`.48 * **Execute**: Run the tests using the appropriate tool (e.g., `pytest`, `uv run tests/run_tests.py`).494. **Fix Brittle Tests**: If existing tests fail for a function that Hypothesis verified as `[PASS]`:50 * **Analyze**: Determine if the test is "brittle" (e.g., asserting on internal state, specific log messages, or private members that were cleanly refactored away).51 * **Fix**: Modify the existing test so it passes with the new, cleaner code. Ensure you maintain the original intent of the test (verifying functionality) while removing the brittle dependency on the "slob" implementation.525. **Record Edits**: Add an entry to the `edits` dictionary in `code-slob-cleanup.json` for each successfully refactored function.53 * **Format**: `"identifier": "commit-hash"`.54 * **Function Mapping**: The identifier MUST include the full file path and class name (if any).55 * Example (top-level): `"path/to/file.py:function_name": "5akekdl"`56 * Example (class method): `"path/to/file.py:ClassName.method_name": "5akekdl"`57 * **Commit Hash**: Use the first 7 characters of the most recent commit (the parent commit of your changes). Use `git rev-parse --short=7 HEAD` to retrieve it.58 * **Constraint**: ONLY log individual function refactors in the `edits` dictionary. Do NOT log classes or files.596. **Report**: Inform the user that the code has been cleaned, verified by Hypothesis, and that existing tests have been run (and updated if necessary).6061### Phase 5: Cleanup621. **Remove Workspace**: Delete the temporary directory.63 * `rm -rf .code-slob-tmp`6465## Revert Workflow6667**Constraint**: If no `code-slob-cleanup.json` exists, or if the `edits` dictionary is empty, inform the user that there are no recorded cleanups to revert and exit. Otherwise, follow the steps below to revert the specified cleanup(s):68691. **Identify**: If the user asks to revert a cleanup (e.g., "Revert the cleanup for `process_data`", "Revert `src/utils.py`", or "Revert `src/logic/`"), search the `edits` dictionary in `code-slob-cleanup.json` for matches.702. **Scan for Matches**:71 * **Specific Function**: Look for a direct match (e.g., `path/to/file.py:process_data`).72 * **Class**: Search for all entries that include that class name in their identifier (e.g., `path/to/file.py:MyClass.*`).73 * **File**: Search for all entries starting with that file's path (e.g., `path/to/file.py:*`).74 * **Folder**: Search for all entries whose path starts with that folder (e.g., `path/to/folder/*`).753. **Find Hashes**: For each match, retrieve the associated 7-character commit hash (the parent commit where the original code exists).764. **Locate & Extract**: For each matched target:77 * Use git to read the content of the original file at that commit (e.g., `git show <hash>:<path/to/file.py>`).78 * Extract the original version of the function/class.795. **Revert**: Replace the refactored code in the current codebase with the original version extracted from git.806. **Update**: Remove all matching entries from the `edits` dictionary in `code-slob-cleanup.json`.817. **Verify**: Run existing tests to ensure the revert didn't break anything.8283---84> Converted and distributed by [TomeVault](https://tomevault.io/claim/jazz23) — claim your Tome and manage your conversions.85<!-- tomevault:4.0:skill_md:2026-04-14 -->