ast-grep
$ sg Community Low Riskast-grep (sg) matches and rewrites code by syntax tree structure rather than brittle regexes. It defaults to read-only diff/match previews unless --update-all is passed and emits structured --json results with exact AST node positions.
brew install ast-grep Direct sg run --json invokes Tree-sitter locally in milliseconds without running a background language server or MCP daemon.
Agent-Readiness Checklist
Static evaluation (not yet probed on this host). Run npm run data:probe locally to capture live verification.
FCRS-1.0 6-Dimension Breakdown
Supports --json output, --color never, non-interactive execution, and safe diff-only previews by default.
- Native
--jsonmatch output and--update-allopt-in for writes
Read-only unless --update-all (-U) is explicitly passed; local-only with zero network calls.
- Diff preview is default when
--rewriteis specified
Available via brew, npm, pip, cargo, and prebuilt binaries across macOS, Linux, and Windows.
- Multi-ecosystem distribution (
brew,npm,pypi,cargo)
Eliminates multi-line regex fragility by matching syntax trees and metavariables cleanly.
- Tree-sitter AST pattern matching across dozens of languages
Actively maintained OSS project (ast-grep/ast-grep) with rapid language and rule-engine support.
- Active GitHub repository under MIT license
Well-structured guide, rule playground, and CLI reference documentation.
- Detailed official guide at ast-grep.github.io
Security, Auth & Guardrails
None required; operates strictly on local source files.
100% free, offline, and open-source (MIT).
# ast-grep (sg) Agent Rules - Always preview rewrites with `sg run --pattern ... --rewrite ...` before adding `--update-all`. - Specify `--lang` and `--json` for deterministic AST matching and parsing.
Agent-Ready Command Recipes
Safe Read-Only Queries
sg run --pattern 'console.log($$$ARGS)' --lang ts --json src/
Emits JSON matches with file paths, byte/line spans, and captured metavariables.
sg run --pattern 'var $A = $B' --rewrite 'let $A = $B' --lang js src/
Prints a read-only diff preview; files remain untouched without --update-all.
Controlled Writes & Previews
sg run --pattern 'oldApi($$$ARGS)' --rewrite 'newApi($$$ARGS)' --lang ts --update-all src/
Writes AST-safe replacements in-place only when --update-all is explicitly supplied.
Strengths for Agents
- + Native
--jsonoutput returns exact AST node spans and captured metavariables. - + Rewrites preview diffs by default and require explicit
--update-allto touch disk.
Caveats & Pitfalls
- ! The
sgbinary name can collide withnewgrp/sgon some Linux distributions (useast-grepalias if needed).