agentleFS
stoa-platformstoa-platform/stoa/.claude/skills/rewrite/SKILL.md

rewrite

Skill9 starsChanged 7 months agoApache-2.0View on GitHub

Force a full-file rewrite (Write) instead of patch (Edit) when Edit keeps failing or diff is large. Context rot mitigation — use when Edit has failed ≥2× on same file, or when estimated diff >30% of file size.

8 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

12 rules. 0 are common, 7 are distinctive, and 5 are very specific to this project.

  • 0

    Common

    Most of the 6 edited versions we read keep these.

  • 7

    Distinctive

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    If INTENT is empty, ask the user: "What should the rewrite accomplish?

    line 31
  2. Very specific:

    Always Read the full file before rewriting.

    line 34
  3. Very specific:

    If consecutive_fails ≥ 2, the rewrite is warranted.

    line 40
  4. Distinctive:

    Import structure and file-level conventions

    line 46
  5. Very specific:

    Use the Write tool with the full new content.

    line 56
  6. Distinctive:

    Run the relevant check for the file type:

    line 59
  7. Very specific:

    If the file has tests nearby (*_test.*, *.test.*, *.spec.*), run them.

    line 65
  8. Distinctive:

    Do NOT rewrite a file whose purpose you don't understand.

    line 75
  9. Distinctive:

    Do NOT rewrite while the main context is >70% full — delegate to a subagent instead (the pollution bias applies to rewrites too).

    line 76
  10. Distinctive:

    Do NOT bundle unrelated changes into the rewrite.

    line 77
  11. Distinctive:

    Do NOT remove comments that carry historical/compliance context (e.g.

    line 78
  12. Distinctive:

    Do NOT rewrite test files without running them after — silent test deletion is a known failure mode.

    line 79

“Common” is measured: kept by at least 6 in 10 of the 6 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rewrite — $ARGUMENTS

Invoked when Edit (patch) is the wrong tool. Research shows search/replace fails 20-30% on evolved code, and accumulated failed patches poison the context (Liu et al. "Lost in the Middle"; Databricks Mosaic). See feedback_rewrite_after_fail.md.

When to use this skill

Triggered automatically via the hook post-edit-failure-tracker.sh at 2 consecutive Edit failures. Also invoke manually when:

  • Diff estimated >30% of the file size
  • Refactoring where structure changes, not just content
  • File is small-to-medium (≤800 lines) and a clean rewrite is cheaper than hunting for correct search/replace anchors
  • User says "just rewrite it"

Do NOT use for:

  • Single-line changes (use Edit)
  • Files >1500 lines (split first, or delegate to subagent)
  • Files you haven't read yet in this session (read first, understand first)

Workflow

Step 1 — Parse arguments

Split $ARGUMENTS into:

  • FILE_PATH (first token, absolute path)
  • INTENT (remainder — what the rewrite should accomplish)

If INTENT is empty, ask the user: "What should the rewrite accomplish? (e.g. 'fix the auth middleware bug', 'add tenant filtering')". Do NOT guess the intent from context alone.

Step 2 — Read the current file

Always Read the full file before rewriting. If >1500 lines, stop and suggest splitting or subagent delegation instead.

Step 3 — Check pollution signal

jq --arg path "$FILE_PATH" '[.[] | select(.path == $path)] | .[0]' .claude/state/edit-attempts.json

If consecutive_fails ≥ 2, the rewrite is warranted. If total ≥ 5 but consecutive_fails < 2, consider whether a subagent with fresh context would be better than an inline rewrite (Main context already has noise).

Step 4 — Rewrite strategy

Preserve by default:

  • Public API surface (exported symbols, function signatures if referenced elsewhere)
  • Import structure and file-level conventions
  • Comments that explain non-obvious WHY (delete comments that explain WHAT)
  • Type annotations, docstrings where they exist

Change per INTENT:

  • Fix the bug described
  • Refactor the internal logic
  • Simplify control flow

Step 5 — Write the file

Use the Write tool with the full new content. Never emit partial content or placeholders like // ... rest unchanged.

Step 6 — Verify

Run the relevant check for the file type:

  • .py: ruff check <file> && ruff format --check <file> (or let the post-edit-format hook run)
  • .rs: rustfmt --check <file> + cargo check -p <crate> if feasible
  • .ts|.tsx|.js|.jsx: npx prettier --check <file>
  • .go: go vet ./... && gofmt -l <file>

If the file has tests nearby (*_test.*, *.test.*, *.spec.*), run them.

Step 7 — Reset the tracker

jq --arg path "$FILE_PATH" 'del(.[] | select(.path == $path))' .claude/state/edit-attempts.json > /tmp/et.json && mv /tmp/et.json .claude/state/edit-attempts.json

Removes the failure counter for this file so the next session starts clean.

Anti-patterns

  • Do NOT rewrite a file whose purpose you don't understand. Read + grep callers first.
  • Do NOT rewrite while the main context is >70% full — delegate to a subagent instead (the pollution bias applies to rewrites too).
  • Do NOT bundle unrelated changes into the rewrite. Stick to the stated INTENT.
  • Do NOT remove comments that carry historical/compliance context (e.g. "kept for backwards compat with X", "regression for CAB-XXXX") — these have load-bearing information.
  • Do NOT rewrite test files without running them after — silent test deletion is a known failure mode.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.