Skip to content

feat: in-process ast-grep structural search tool#392

Merged
ScriptedAlchemy merged 4 commits into
masterfrom
codex/ast-grep-search
Jul 9, 2026
Merged

feat: in-process ast-grep structural search tool#392
ScriptedAlchemy merged 4 commits into
masterfrom
codex/ast-grep-search

Conversation

@ScriptedAlchemy

@ScriptedAlchemy ScriptedAlchemy commented Jul 9, 2026

Copy link
Copy Markdown
Owner

What

Adds tracedecay_ast_grep_search — a read-only structural (AST) search tool for call shapes, argument orders, and expression forms that a text regex cannot express. Structural search previously had no tool at all; only tracedecay_ast_grep_rewrite existed.

Backend: ast-grep as a library, not the CLI

Per the design directive, this evaluates ast-grep-core as an in-process backend and ships it:

  • Compat check (passed): ast-grep-core 0.44 pins tree-sitter ^0.26.3; this repo builds its grammars against tree-sitter 0.26.8 — same 0.26 minor, ABI-compatible. ast_grep_core::tree_sitter::TSLanguage is a re-export of tree_sitter::Language, exactly what extraction::ts_provider::try_language already returns.
  • Wiring: a small TdLang wrapper implements ast-grep-core's Language/LanguageExt traits over the repo's own bundled grammars — no new grammar crates, no external ast-grep binary, and no ast_grep_available() gating (the tool registers unconditionally). Metavariable pre-processing and per-language expando chars (µ, 𐀀, _) are mirrored from ast-grep-language so pattern semantics match the upstream CLI.
  • The existing CLI-backed ast_grep_rewrite tool is left unchanged (library-backed rewrite, with its apply/diff semantics, is a follow-up).

New deps: ast-grep-core (+ bit-set/bit-vec). No new tree-sitter grammar crates.

Tool

  • Args: pattern (required), lang (optional; auto-detected per file otherwise), path_glob (optional scope), max_results (default 50, cap 200).
  • Output: markdown (default) + format: json; each hit is graph-enriched with its enclosing symbol like tracedecay_grep, so the next call is tracedecay_body. CLI twin works: tracedecay tool tracedecay_ast_grep_search.

Discovery wiring

  • Hints: the search category now routes structural patterns to tracedecay_ast_grep_search (the edit category keeps its ast_grep_rewrite mention). Hint-signature drift guard stays green.
  • Skills: exploring-code gains a structural-search routing row; editing-safely links search→rewrite.
  • Evals: one active search scenario grounded in the fixture (reserve_stock(...) three-arg call shape in place_order), and one deferred rewrite scenario (fixture has a single call site and the rewrite tool is CLI-gated).

Gates (all green)

  • cargo fmt --check
  • cargo clippy --workspace --all-targets --locked -- -D warnings
  • cargo nextest run --test mcp_suite --test agent_suite --no-fail-fast → 852 + 488 pass
  • grade.py --lint-only (53 neutral), --check-hints (21 signatures intact), selftest.py (all pass)
  • 8 in-process engine unit tests

Live smoke (against this repo)

pattern: "ToolResult::new($$$)" → real matches with enclosing symbols, e.g.:

- src/mcp/tools/handlers/edit.rs:188 — text_tool_result (function)
  > ToolResult::new( json!({ "content": [{ "type": "text", "text": text }] }), touched_files, )
- src/mcp/tools/handlers/workflow.rs:245 — handle_diagnose (function)

🤖 Generated with Claude Code

Review follow-up

The working-tree walk and AST parsing now run on Tokio's blocking pool so a broad or zero-match search cannot stall the MCP async runtime. Added a current-thread wrapper regression; focused engine tests and full clippy remain green.

@changeset-bot

changeset-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: bce204b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8936c07b03

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/mcp/tools/handlers/mod.rs Outdated
Comment thread src/ast_grep_search.rs Outdated
@ScriptedAlchemy ScriptedAlchemy force-pushed the codex/ast-grep-search branch 3 times, most recently from 8a42521 to c8312fa Compare July 9, 2026 22:48
ScriptedAlchemy and others added 3 commits July 9, 2026 22:59
Add tracedecay_ast_grep_search: a read-only structural (AST) search tool
for call shapes, argument orders, and expression forms that a text regex
cannot express. Structural search previously had no tool at all — only
tracedecay_ast_grep_rewrite existed.

Backend: ast-grep-core as a LIBRARY, not the CLI. It is generic over a
tree-sitter Language, so the repo's own bundled 0.26 grammars (via
extraction::ts_provider) are wired into its Language trait. Compat check:
ast-grep-core 0.44 pins tree-sitter ^0.26.3, ABI-compatible with the 0.26
grammars this repo builds — TSLanguage is exactly tree_sitter::Language.
Result: no external ast-grep binary, no new grammar crates, no
ast_grep_available() gating (the tool registers unconditionally). The
existing CLI-backed ast_grep_rewrite tool is left unchanged.

Metavariable pre-processing and per-language expando chars mirror
ast-grep-language so pattern semantics match the upstream CLI. Each hit is
graph-enriched with its enclosing symbol like tracedecay_grep.

Also:
- hints: extend the search category to route structural patterns to
  ast_grep_search (existing rewrite mention in the edit category kept);
  hint-signature drift guard still green.
- skills: exploring-code gains a structural-search row; editing-safely
  links search->rewrite.
- evals: one active search scenario (grounded in the fixture) and one
  deferred rewrite scenario (fixture has a single call site; rewrite tool
  is CLI-gated).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ScriptedAlchemy ScriptedAlchemy force-pushed the codex/ast-grep-search branch from c8312fa to f7c8776 Compare July 9, 2026 23:07
@ScriptedAlchemy ScriptedAlchemy merged commit c4bc4e8 into master Jul 9, 2026
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant