Summary
Add both structural search AND structural rewrite as new tools, creating a complete "ast-grep toolkit" within CGR. This combines Options 1 and 2 into a single cohesive implementation that delivers the most user-facing value with the least architectural risk.
Motivation
Search + rewrite is the natural pair (find pattern, then fix it). This requires no changes to the graph schema, parser infrastructure, or indexing pipeline. It is purely additive: new tools alongside existing ones with zero regression risk. Both the agentic chat and MCP server get new capabilities, and the LLM agent can chain: structural_search -> review matches -> structural_replace.
Implementation
Files to Create
codebase_rag/tools/ast_grep_service.py (~200 lines) wrapping ast-grep-py with search() and replace() methods
codebase_rag/tools/structural_search.py (~80 lines) agentic tool wrapper
codebase_rag/tools/structural_editor.py (~80 lines) agentic tool wrapper
Files to Modify
- Add enums:
STRUCTURAL_SEARCH, STRUCTURAL_REPLACE to AgenticToolName and MCPToolName
- Add descriptions to
tool_descriptions.py
- Add types to
types_defs.py: StructuralSearchMatch, StructuralReplaceChange
- Register in
main.py and mcp/tools.py
- Add
ast-grep-py as optional dependency ([ast-grep] extra) in pyproject.toml
Design Decisions
- Shared service layer (
ast_grep_service.py) avoids code duplication between search and replace tools
- Optional dependency with graceful degradation
- Dry-run mode for replace operations with diff preview
- Integration with existing approval workflow
Acceptance Criteria
Related
This is the combined implementation of:
Serves as foundation for:
Summary
Add both structural search AND structural rewrite as new tools, creating a complete "ast-grep toolkit" within CGR. This combines Options 1 and 2 into a single cohesive implementation that delivers the most user-facing value with the least architectural risk.
Motivation
Search + rewrite is the natural pair (find pattern, then fix it). This requires no changes to the graph schema, parser infrastructure, or indexing pipeline. It is purely additive: new tools alongside existing ones with zero regression risk. Both the agentic chat and MCP server get new capabilities, and the LLM agent can chain: structural_search -> review matches -> structural_replace.
Implementation
Files to Create
codebase_rag/tools/ast_grep_service.py(~200 lines) wrapping ast-grep-py withsearch()andreplace()methodscodebase_rag/tools/structural_search.py(~80 lines) agentic tool wrappercodebase_rag/tools/structural_editor.py(~80 lines) agentic tool wrapperFiles to Modify
STRUCTURAL_SEARCH,STRUCTURAL_REPLACEtoAgenticToolNameandMCPToolNametool_descriptions.pytypes_defs.py:StructuralSearchMatch,StructuralReplaceChangemain.pyandmcp/tools.pyast-grep-pyas optional dependency ([ast-grep]extra) inpyproject.tomlDesign Decisions
ast_grep_service.py) avoids code duplication between search and replace toolsAcceptance Criteria
ast-grep-pyadded as optional dependency under[ast-grep]extraast-grep-pyis not installedRelated
This is the combined implementation of:
Serves as foundation for: