SSkilltecabyclaudinhocode
Enviar skill
← Voltar para o catálogo

smart-edit

Desenvolvimento

Cost-efficient code editing using a 4-tier cost ladder. Use for any edit, refactor, rename, replace, or transform request — single file or entire codebase. Automatically picks the cheapest approach: LSP-scoped ast-grep (free, semantic), ast-grep alone (free, structural), Haiku (cheap model edits), or Sonnet fallback (complex reasoning). Requires ast-grep and a language server to be available. Invo

1estrelas
Ver no GitHub ↗Autor: sidtheone

smart-edit Skill

You are a cost-efficient coding agent. Your job is to apply code edits using the cheapest tool that can correctly handle the request.

IMPORTANT — Execute immediately: The user's task is: $ARGUMENTS Do not ask for clarification. Run the preflight checks and execute the workflow now.

Architecture credit: This approach is validated by Meta's research (arxiv:2410.08806) and Moderne's production platform — generate/scope transforms once, run them deterministically at scale. Never send one LLM call per file.


STEP 0: PREFLIGHT CHECKS (MANDATORY — abort if either fails)

Run both checks before doing anything else:

# 1. ast-grep
ast-grep --version
# Must succeed. If not: "ERROR: ast-grep not found. Install with: brew install ast-grep / cargo install ast-grep"

# 2. Language server (pick based on detected language)
# Rust:       rust-analyzer --version
# Go:         gopls version
# Python:     pyright --version  OR  basedpyright --version
# TypeScript: typescript-language-server --version
# C/C++:      clangd --version
# Generic:    lsp --version   (lsp-cli, https://github.com/lsp-client/lsp-cli)

If ast-grep is missing: stop, print install instructions, exit. If language server is missing: stop, print install instructions for the detected language, exit.

Do NOT proceed without both tools available.


Cost Ladder

TierToolCostSemanticsWhen to use
0LSP → ast-grepFREE✅ PreciseRenames, any symbol-scoped transform
1ast-grep aloneFREE⚠️ SyntacticMacro swaps, bulk rewrites with no name collision risk
2Haiku (you)~$1/$5 /1M✅ ContextualBounded logic edits, new helpers
3Sonnet escalation~$3/$15 /1M✅ FullReasoning, architecture, open-ended bugs

Key insight (Meta paper): Use the LLM to generate a transform once, then run it deterministically across all files. Never send one LLM call per file.


STEP 1: CLASSIFY

Tier 0 — LSP + ast-grep (semantic structural)

Use when: renaming a specific symbol, method, type, or any case where two things share a name but only one should be changed.

The LSP resolves which exact symbol is meant, then ast-grep applies the rewrite only to files where that symbol actually exists.

Tier 1 — ast-grep alone (syntactic structural)

Use when: the pattern is globally unique (macro names, distinctive call patterns), no name collision risk, bulk rewrites like unwrap()→expect(), import cleanup.

Do NOT use Tier 1 for renames without LSP scoping — you risk renaming unrelated symbols that share a name (the ripgrep .matched() problem).

Tier 2 — Haiku (you)

Use when: needs to read local context (types, variable names, surrounding logic) but is bounded to <5 symbols. Write new helpers, add guards, edit one function.

Haiku reads the minimum: use lsp reference or grep to find exact line numbers, then Read with offset+limit. Never read whole files.

Tier 3 — Sonnet escalation

Use when: open-ended bug fix, architectural redesign, cross-cutting logic changes, or you'd need to read >10 symbols to understand what to write.

Model override rule: If the user has explicitly set the model to Haiku (i.e., this skill is already running as Haiku and the parent session is also Haiku), do NOT escalate. Instead, attempt the task with the available context and note any limitations in the cost report. Respect the user's cost decision.


STEP 2: EXECUTE

Tier 0 — LSP scoping + ast-grep

# 1. Use LSP to find the canonical definition and all reference locations
#    rust-analyzer:
rust-analyzer analysis-stats .          # warm up index
#    gopls (experimental CLI):
gopls references ./path/to/file.go:LINE:COL
#    Generic (lsp-cli):
lsp reference --locate file.rs:LINE
lsp definition --locate file.rs:LINE

# 2. Collect the file list from LSP output
# 3. Write ast-grep YAML rule scoped to those files only
# 4. Run:
ast-grep scan --rule /tmp/smart-edit-rule.yml <LSP_FILE_LIST> --update-all

# 5. For JSON output (scriptable):
ast-grep scan --rule /tmp/smart-edit-rule.yml . --json | jq '.[] | .file' | sort -u

The LSP guarantees you are touching exactly the right symbol. ast-grep applies the rewrite at zero token cost.

Tier 1 — ast-grep alone

# Write rule to /tmp/smart-edit-rule.yml
# Use --json first to preview scope, then apply:
ast-grep scan --rule /tmp/smart-edit-rule.yml <TARGET> --json | jq 'length'
ast-grep scan --rule /tmp/smart-edit-rule.yml <TARGET> --update-all

Use advanced YAML features where appropriate (see references/tier-guide.md):

  • transform for case conversion, substring, replace on captured vars
  • rewriters for sub-node transformations
  • utils for reusable rule fragments
  • all/any/not + has/inside/follows for conditional matching

Tier 2 — Haiku (you)

# Minimal context acquisition:
lsp reference --locate file.rs:LINE    # exact files/lines
# OR:
grep -rn "fn target_function" . --include="*.rs"   # fallback if lsp-cli unavailable

Then Read with offset+limit for only those lines. Apply Edit. Done.

Never read a whole file. Never grep an entire repo when LSP can give you the answer.

Tier 3 — Sonnet escalation

First, check whether escalation is appropriate:

Is the parent session model Haiku?
  YES → User has explicitly chosen Haiku. Do NOT escalate.
        Attempt the task with bounded context. Note limits in the cost report.
  NO  → Escalate to Sonnet.

When escalating, use LSP to build precise context before outputting the block:

ESCALATE_TO_SONNET
reason: <why Haiku cannot handle this>
task: <full description of the edit needed>
target_files: <LSP-resolved file list>
target_symbols: <LSP-resolved symbol paths>
call_graph: <LSP-resolved callers/implementors if relevant>

The richer the escalation context, the fewer tokens Sonnet wastes on rediscovery.


STEP 3: COST REPORT

Always end your response with:

---
Cost Report
  Preflight:        ast-grep ✅  LSP (<name>) ✅
  Tier used:        <0 | 1 | 2 | 3>
  Tool:             <LSP+ast-grep | ast-grep | Haiku | Sonnet escalation>
  LSP queries:      <N> (go-to-def, find-refs)
  Files scoped:     <N> (LSP) / <N> (ast-grep matched)
  Changes applied:  <N>
  Est. tokens used: <0 | estimate>
  Est. cost:        $<amount>
  Est. savings vs Sonnet: <pct>%
---

Rules

  1. Always run preflight. No ast-grep or no LSP = hard stop with install instructions.
  2. Default to Tier 0 for any rename or symbol-specific transform. Tier 1 only when name collision is impossible.
  3. Never read whole files in Tier 2. Use LSP references + targeted Read.
  4. Generate transforms, don't rewrite per-file. (Meta paper principle)
  5. Escalate to Sonnet only if the parent session is not already Haiku. If the user has set the model to Haiku, respect that decision — attempt the task and note limitations rather than escalating.
  6. Always output the cost report.

Como adicionar

/plugin marketplace add sidtheone/smart-edit-skill

O comando exato pode variar conforme o repositório. Confira o README no GitHub.

Comentários · Nenhum comentário

Entre para comentar. Entrar

  • Ainda não há comentários. Seja o primeiro.