Skip to content

feat(coding-agent/tools): added ast_grep empty-result hints and enriched AST descriptions#2665

Open
metaphorics wants to merge 1 commit into
can1357:mainfrom
metaphorics:ultra/ast
Open

feat(coding-agent/tools): added ast_grep empty-result hints and enriched AST descriptions#2665
metaphorics wants to merge 1 commit into
can1357:mainfrom
metaphorics:ultra/ast

Conversation

@metaphorics

Copy link
Copy Markdown
Contributor

Summary

Adds a pure pattern-hints module wired into ast_grep: a zero-match result appends a one-line hint naming the likely mistake (regex leaking into an AST pattern, or a structurally incomplete declaration) and routes text/alternation/cross-language searches to search. Also enriches the ast_grep/ast_edit descriptions and adds AST shape-routing + parallel-exploration guidance to the system prompt.

Tests

Pure hint-detector + language-inference regression test; bun check green.

Note: touches prompts/system/system-prompt.md alongside #2658 and #2659 (distinct sections); rebase if flagged.

Closes #2657

…hed AST descriptions

- Added a pure ast-pattern-hints module (getPatternHint + regex-misuse and language-shape detectors + best-effort language inference) and wired it into ast_grep so a zero-match result appends a one-line hint naming the likely mistake and routing text/alternation/cross-language searches to the search tool.
- Enriched the ast_grep description with an anti-regex routing block and per-language gotchas/examples (Python no-trailing-colon, Go/Rust/TS need params+body), and the ast_edit description with the dry-run to resolve apply flow and its stale-preview guard.
- Added AST shape-routing and parallel-exploration guidance to the system prompt, and a focused regression test for the hint detectors and language inference.
Copilot AI review requested due to automatic review settings June 15, 2026 13:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds user-facing guidance when ast_grep returns zero matches by detecting common “pattern is regex/text” mistakes and a few language-specific incomplete-declaration shapes, plus updates tool docs/prompts accordingly.

Changes:

  • Added ast-pattern-hints utilities (language inference + hint detectors) and tests.
  • Integrated hints into ast_grep zero-match output.
  • Expanded ast_grep/ast_edit docs and system prompt routing guidance; updated changelog.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/coding-agent/src/tools/ast-pattern-hints.ts Introduces hint detection + language inference for zero-match ast_grep patterns.
packages/coding-agent/src/tools/ast-pattern-hints.test.ts Adds Bun tests to lock in hint behavior and inference rules.
packages/coding-agent/src/tools/ast-grep.ts Appends one-line hint to zero-match output for faster user correction.
packages/coding-agent/src/prompts/tools/ast-grep.md Documents anti-regex guidance and language-specific “shape” gotchas.
packages/coding-agent/src/prompts/tools/ast-edit.md Clarifies that rewrite patterns/outputs are AST (not regex/text) and adds stale-preview note.
packages/coding-agent/src/prompts/system/system-prompt.md Adds routing guidance between structural tools vs search/find and parallel exploration advice.
packages/coding-agent/CHANGELOG.md Records the new hint behavior and documentation updates.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +60 to +62
if (!src.includes("$") && /\w\.[*+]/.test(src)) {
return 'Hint: ".*" and ".+" are regex wildcards. In ast_grep use $$$ for multiple AST nodes and $VAR for a single node; for text patterns use the `search` tool.';
}
Comment on lines +85 to +97
if (lang === "javascript" || lang === "typescript" || lang === "tsx") {
if (/^(export\s+)?(async\s+)?function\s+\$[A-Z_]+\s*$/i.test(src)) {
return 'Hint: function patterns need params and a body. Try "function $NAME($$$) { $$$ }".';
}
}

if (lang === "go" && /^func\s+\$[A-Z_]+\s*$/i.test(src)) {
return 'Hint: Go function patterns need params and a body. Try "func $NAME($$$) { $$$ }".';
}

if (lang === "rust" && /^fn\s+\$[A-Z_]+\s*$/i.test(src)) {
return 'Hint: Rust fn patterns need params and a body. Try "fn $NAME($$$) { $$$ }".';
}
Comment on lines +5 to +10
* The empty-match hints turn the two most common `ast_grep` mistakes — regex
* leaking into an AST pattern, and a structurally incomplete declaration — into
* a one-line nudge. These lock the detection contract: every regex construct is
* caught and routed to `search`, valid patterns stay silent (no false nudge),
* regex detection outranks the language check, and language inference only
* commits when the extensions agree.
Comment on lines +77 to +82
if (src.startsWith("class ") && src.endsWith(":")) {
return `Hint: drop the trailing colon — ast_grep patterns are not full statements. Try: "${src.slice(0, -1)}"`;
}
if ((src.startsWith("def ") || src.startsWith("async def ")) && src.endsWith(":")) {
return `Hint: drop the trailing colon — ast_grep patterns are not full statements. Try: "${src.slice(0, -1)}"`;
}

@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: 88d448c81d

ℹ️ 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".

<critical>
- Parse issues mean the rewrite is malformed or mis-scoped — fix the pattern before assuming a clean no-op
- For one-off local text edits, you SHOULD prefer the Edit tool
- The apply re-runs the rewrite against the files at apply time and is rejected as a **stale preview** if the match set drifted since the preview (e.g. the file was edited in between) — re-run `ast_edit` to refresh, then `resolve`

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid promising stale applies are rejected

If a file changes between the ast_edit preview and resolve apply, this guidance says the apply is rejected as stale, but the apply handler actually reruns runAstEditOnce(... dryRun: false) before computing stalePreview, so a mismatched nonzero run can already have written partial or extra replacements before returning an error. The prompt should not tell the model that drift prevents mutation unless the apply path is changed to dry-run/check first or roll back.

Useful? React with 👍 / 👎.

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.

tools: ast_grep empty-result hints + enriched AST descriptions

2 participants