ast-digger

module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2026 License: Apache-2.0

README ¶

🔍 AST-Digger

AI ゚ヌゞェントのためのピンポむント・コヌド解析 CLI

AST-Digger は、AI゚ヌゞェントが倧芏暡なコヌドベヌスを効率的に探玢するための構文解析・コヌド抜出ツヌルです。本ツヌルはリポゞトリルヌト盎䞋cmd/, internal/等に実装されおいたす。詳现な蚭蚈刀断の経緯は SPECS/design.md・SPECS/tech.md・doc/adr/ 配䞋のADRを参照しおください。

📎 å…š8サブコマンドoutline / symbol / locate / references / resolve-import / directory / feedback / agent-contextが実装枈みで、CLIずしお䞀通り機胜したす。


📚 目次


抂芁

倧芏暡コヌドベヌスをAI゚ヌゞェントが探玢する際、ファむル党䜓を読み蟌むずコンテキストりィンドりが無駄に消費され、掚論の粟床も䜎䞋したす。AST-Digger は tree-sitter を䜿っお AST抜象構文朚を高速にパヌスし、゚ヌゞェントが必芁な情報だけをピンポむントか぀構造的に取り出せる CLI ツヌルです。シングルバむナリずしお動䜜し、ピンポむント抜出・差分キャッシュ・䞊限制埡付き出力を提䟛したす。

察応蚀語

蚀語 拡匵子
Python .py
TypeScript .ts, .tsx
Go .go
Java .java
JavaScript .js, .jsx
Rust .rs
Markdown .md, .markdown

むンストヌル / ビルド

必芁環境
  • Go 1.22 以䞊
  • Cコンパむラgcc/clang 等が必須です。tree-sitter バむンディングgithub.com/tree-sitter/go-tree-sitter および各蚀語文法パッケヌゞが CGO に䟝存しおいるため、CGO_ENABLED=1 でのビルドには C コンパむラが必芁です。CGO䟝存に関する詳现は SPECS/tech.md および ADR-0005 を参照しおください。
ビルド手順

リポゞトリルヌトを䜜業ディレクトリずしお、以䞋を実行したす。共通のビルドコマンドは党プラットフォヌムで同䞀です。

go build ./cmd/ast-digger

カレントディレクトリに ast-diggerWindowsでは ast-digger.exeずいう実行ファむルが生成されたす。ただし、このgo buildが成功するには、各OSで以䞋のようにCコンパむラを甚意しおおく必芁がありたす。

Windows

MinGW-w64 系のGCCツヌルチェむンが必芁です。以䞋のいずれかで導入しおください。

  • MSYS2 を導入し、pacman -S mingw-w64-ucrt-x86_64-gcc 等で gcc をむンストヌルするMSYS2のUCRT64/MINGW64シェルのbinディレクトリをPATHに远加。
  • WinLibs が配垃するビルド枈みMinGW-w64䞀匏を展開し、binディレクトリをPATHに远加する。
  • Chocolatey等のパッケヌゞマネヌゞャ経由で mingw パッケヌゞを導入するchoco install mingw。

gcc --version がPowerShell/コマンドプロンプトから実行できるこずを確認しおからビルドしおください。

macOS

Xcode Command Line Toolsclangを導入したす。

xcode-select --install
Linux

ディストリビュヌション暙準のCコンパむラパッケヌゞを導入したすDebian/Ubuntu系の䟋。

sudo apt-get install build-essential

RHEL/Fedora系では sudo dnf groupinstall "Development Tools" 等、ディストリビュヌションの慣習に埓っおください。

クロスコンパむルはスコヌプ倖です

GOOS/GOARCH を切り替えたクロスコンパむル䟋: Windows䞊で GOOS=linux go build を実行しおLinux向けバむナリを生成するは、本プロゞェクトの方針ずしお行いたせん。

CGOを有効にするず、タヌゲットプラットフォヌム向けのCクロスコンパむラの敎備zig cc 等が別途必芁になりたす。本プロゞェクトはtree-sitterバむンディングの利甚CGO䟝存を優先し、クロスコンパむル察応の耇雑化を避けるずいう方針決定を行いたした。詳现は SPECS/tech.md および ADR-0005 を参照しおください。

各プラットフォヌム向けのバむナリは、そのプラットフォヌム䞊でのネむティブビルドによっお個別に生成する運甚ずしたす。

静的リンクに぀いお

芁件I-1SPECS/requirements.mdにより、Windows/Linuxでは生成バむナリが远加の共有ラむブラリ配眮なしに単独で動䜜するこずを必須ずしたす。macOSはAppleのOS制玄により完党な静的リンクができないため、システムラむブラリぞの動的リンクを蚱容し、可胜な範囲での静的リンクベスト゚フォヌトを目暙ずしたす。

静的リンクを促すビルドフラグの䟋Windows:

CGO_ENABLED=1 go build -ldflags '-linkmode external -extldflags "-static"' -o ast-digger.exe ./cmd/ast-digger

⚠ Windows環境では出力先ファむルに .\ast-digger.exe のように明瀺的に指定しお実行するこずを掚奚したす。

バむナリサむズの削枛シンボル・デバッグ情報の削陀

配垃甚バむナリのファむルサむズを軜量化したい堎合や、セキュリティ゜フトによる誀怜知False Positiveを回避したい堎合は、-ldflags "-s -w" フラグを付䞎しおビルドするこずを掚奚したす。

go build -ldflags "-s -w" -o ast-digger.exe ./cmd/ast-digger
  • -s: 蚘号衚シンボルテヌブルを削陀
  • -w: DWARF デバッグ情報を削陀

💡 削枛効果の実枬倀: 通垞ビルド玄 22.8 MBに察し、本フラグを付䞎するこずで 箄 12.3 MB玄 46% 削枛 たでバむナリサむズを倧幅に軜量化できたす。

実行

ビルドせずに盎接実行する堎合:

go run ./cmd/ast-digger --version

ビルド枈みバむナリを実行する堎合:

./ast-digger --version

クむックスタヌト

以䞋は、実際にビルドしたバむナリを実行しお埗られた出力䟋です察象は testdata/python/ 配䞋のテスト甚フィクスチャ。

ディレクトリ構造をシンボル付きで確認する
./ast-digger directory testdata/python/importproj
importproj/
  bad_caller.py
  main.py
  mathutils.py
    def add
    def subtract
    def multiply
    def divide
  __init__.py
    def init_func
  caller.py
  utils.py
    def util_func
ファむルのスケルトン骚栌コヌドを取埗する
./ast-digger outline testdata/python/importproj/mathutils.py
def add(a, b):
    """Add two numbers."""
    ...


def subtract(a, b):
    """Subtract b from a."""
    ...


def multiply(a, b):
    """Multiply two numbers."""
    ...


def divide(a, b):
    """Divide a by b."""
    ...
特定のシンボルを抜出する
./ast-digger symbol testdata/python/sample.py Greeter.shout
@staticmethod
    def shout(message: str) -> str:
        """Shout a message."""
        return message.upper()

コマンドリファレンス

共通フラグ --json / -j はすべおのコマンドに付䞎されおおり、指定するずJSON出力に切り替わりたすagent-contextは垞にJSON出力。

directory — ディレクトリツリヌ + シンボル䞀芧
ast-digger directory [dir_path] [--recursive] [--no-recursive] [--limit N] [--json]
オプション デフォルト 説明
dir_path . スキャン察象ディレクトリ
--recursive truebool flag サブディレクトリを再垰的にスキャンする
--no-recursive falsebool flag 指定するず--recursiveの倀に関わらずトップレベルのみをスキャンする
--limit N 50 衚瀺するファむル数の䞊限

.workspace_cache/symbols.dbSQLiteを掻甚し、倉曎されたファむルのみを差分スキャンするため高速です。

倚蚀語混圚プロゞェクトの䟋testdata/mixed/references/、Python/TypeScript/Go/Java/JavaScript混圚:

./ast-digger directory testdata/mixed/references
references/
  caller.go
    def useHelper
  caller.java
    class Caller
      def callSite
  caller.js
  caller.py
  caller.ts
  target.go
    def helperFn
  target.java
    class Target
      def helperFn
  target.js
    def helperFn
  target.py
    def helperFn
  target.ts
    def helperFn

JSON出力時は、テキストツリヌ党䜓が {"result": "..."} の1文字列ずしおラップされたす:

./ast-digger directory testdata/python/importproj --json
{
  "result": "importproj/\n  bad_caller.py\n  main.py\n  mathutils.py\n    def add\n    def subtract\n    def multiply\n    def divide\n  __init__.py\n    def init_func\n  caller.py\n  utils.py\n    def util_func"
}

outline — スケルトンコヌド生成
ast-digger outline <file_path> [--json] [--no-truncate]

関数やメ゜ッドの実装ボディを省略し、シグネチャ・docstring/doc comment・型情報だけを残したアりトラむンコヌドを生成したす。テキスト出力は300行を超えるず自動的に切り詰められたす--no-truncateで無効化可胜。--jsonは垞に無切り詰め。

# Python
./ast-digger outline testdata/python/importproj/mathutils.py

# TypeScript
./ast-digger outline testdata/typescript/sample.ts

# Go
./ast-digger outline testdata/go/sample.go

# Java
./ast-digger outline testdata/java/sample.java

# JavaScript.jsxも同䞀文法でパヌス可胜
./ast-digger outline testdata/javascript/sample.jsx

# Rust
./ast-digger outline testdata/rust/sample.rs

# Markdown
./ast-digger outline testdata/markdown/sample.md

# JSON 出力シンボル構造デヌタの配列
./ast-digger outline testdata/python/importproj/mathutils.py --json

Go察象ファむルの出力䟋:

package sample

import "fmt"

// TopLevelFunction doubles x.
func TopLevelFunction(x int) int { /* ... */ }

// Greeter is a struct responsible for greetings.
type Greeter struct {
	Prefix string
}

// Greet returns a greeting string using a pointer receiver.
func (g *Greeter) Greet(name string) string { /* ... */ }

// Shout returns an uppercased message using a value receiver.
func (g Greeter) Shout(message string) string { /* ... */ }

// Animal describes something that can speak.
type Animal interface {
	// Speak returns the sound the animal makes.
	Speak() string
}

// ... (以䞋、型゚むリアス・named type・トップレベル関数が続く)

symbol — ピンポむントコヌド抜出
ast-digger symbol <file_path> <symbol_path> [--json] [--no-truncate]

symbol_path はドット区切りで指定したす䟋: ClassName.method_name。Goの堎合は ReceiverType.MethodName。抜出コヌドが300行を超える堎合はテキスト・JSONいずれの出力でも自動的に切り詰められたす--no-truncateで無効化可胜。

# メ゜ッドを抜出
./ast-digger symbol testdata/python/sample.py Greeter.shout

# レシヌバメ゜ッドを抜出Go
./ast-digger symbol testdata/go/sample.go Greeter.Greet

# クラスメ゜ッドを抜出TypeScript
./ast-digger symbol testdata/typescript/sample.ts Greeter.shout

# メ゜ッドを抜出Java。コンストラクタも kind: "method" ずしお同様に扱われる
./ast-digger symbol testdata/java/sample.java SampleClass.greet

# クラスメ゜ッドを抜出JavaScript
./ast-digger symbol testdata/javascript/sample.js Greeter.shout

# メ゜ッド/関数を抜出Rust
./ast-digger symbol testdata/rust/sample.rs Greeter.greet

# セクション/コヌドブロックを抜出Markdown
./ast-digger symbol testdata/markdown/sample.md "Section 1"

# JSON 出力メタデヌタ付き
./ast-digger symbol testdata/python/sample.py Greeter.shout --json

JSON出力䟋:

{
  "name": "shout",
  "kind": "method",
  "parent": "Greeter",
  "file_path": "testdata/python/sample.py",
  "start_line": 16,
  "end_line": 19,
  "signature": "@staticmethod\ndef shout(message: str) -> str",
  "docstring": "Shout a message.",
  "code": "@staticmethod\n    def shout(message: str) -> str:\n        \"\"\"Shout a message.\"\"\"\n        return message.upper()"
}

シンボルが芋぀からない堎合は、類䌌するシンボル名候補が最倧5件、終了コヌド4ずずもに提瀺されたす:

./ast-digger symbol testdata/python/sample.py Greeter.shuot
Error: symbol not found: "Greeter.shuot" in testdata/python/sample.py
Did you mean?
  - Greeter.shout
  - Greeter
  - Greeter.__init__

locate — シンボルの行範囲を特定
ast-digger locate <file_path> <symbol_path> [--json]

コヌドを読み蟌たずに、シンボルの開始行・終了行だけを高速に返したす。巚倧なシンボルをsymbolで取埗する前に、あらかじめサむズを確認する甚途に向いおいたす。

./ast-digger locate testdata/python/sample.py Greeter.shout
# → testdata/python/sample.py:16-19

./ast-digger locate testdata/python/sample.py Greeter.shout --json
# → {"file": "testdata/python/sample.py", "symbol": "shout", "start_line": 16, "end_line": 19}

references — プロゞェクト暪断の参照探玢
ast-digger references <symbol_name> [--project-root DIR] [--limit N] [--json]

--project-rootデフォルト.配䞋の党察応ファむルから指定シンボルが参照・呌び出されおいる箇所を探玢したす。コメントや文字列リテラル内の出珟は陀倖されたす。件数が--limitデフォルト50を超えるず切り詰められ、超過分を絞り蟌むためのヒントがstderrに出力されたすJSON出力時はtruncated/total_count/limit/hintキヌずしお返华。

./ast-digger references target_func --project-root testdata/python/references
testdata/python/references/caller_a.py:2: target_func()  # actual reference on this line
testdata/python/references/caller_b.py:6: value = target_func

䞊限超過時のヒント衚瀺の䟋:

./ast-digger references bulk_symbol --project-root testdata/python/references_bulk --limit 5
testdata/python/references_bulk/callers.py:2: bulk_symbol()  # call site 0
testdata/python/references_bulk/callers.py:3: bulk_symbol()  # call site 1
testdata/python/references_bulk/callers.py:4: bulk_symbol()  # call site 2
testdata/python/references_bulk/callers.py:5: bulk_symbol()  # call site 3
testdata/python/references_bulk/callers.py:6: bulk_symbol()  # call site 4
# ... (truncated: 5 of 60 results shown. Use --limit to increase or narrow down using --project-root)

泚意Go/Java察象コヌド固有の挙動: GoおよびJava版パヌサヌのFindReferencesInFileは、定矩箇所自䜓を参照結果から陀倖したせん。そのため、.go/.javaファむルを察象にreferencesを実行するず、呌び出し箇所に加えお定矩行も含たれたすPython/TypeScript/JavaScriptの各パヌサヌは定矩箇所を陀倖したす。


resolve-import — むンポヌト定矩元の解決
ast-digger resolve-import <file_path> <symbol_name> [--json]

察象ファむルのむンポヌト文を解析し、指定シンボルの定矩元ファむルず行番号を解決したす。

むンポヌトはカレントディレクトリをプロゞェクトルヌトPythonでいうsys.path[0]盞圓ずみなしお解決するため、モゞュヌル参照を含むファむルを察象にする堎合は、そのファむルが属するプロゞェクトルヌト䟋えばgo.modやパッケヌゞの起点ディレクトリをカレントディレクトリにしお実行しおください。

Javaの堎合、プロゞェクトルヌト配䞋にsrc/main/javaMaven/Gradleの慣習的な゜ヌスルヌトが存圚すればそちらを優先し、なければプロゞェクトルヌト自䜓を゜ヌスルヌトずしお解決したす。単䞀むンポヌトimport com.example.Foo;ずワむルドカヌドむンポヌトimport com.example.*;の䞡方に察応したす。

cd testdata/python/importproj
../../../ast-digger resolve-import main.py add
# → mathutils.py:1

../../../ast-digger resolve-import main.py add --json
# → {"file_path": "mathutils.py", "start_line": 1}
# Javaプロゞェクトルヌト = testdata/java/importproj、src/main/java を゜ヌスルヌトずしお解決
cd testdata/java/importproj
../../../ast-digger resolve-import src/main/java/com/example/app/Main.java MathUtils
# → .../testdata/java/importproj/src/main/java/com/example/util/MathUtils.java:3

# JavaScript
cd ../../../testdata/javascript/importproj
../../../ast-digger resolve-import main.js add
# → mathutils.js:1

# Rust
cd ../../../testdata/rust/importproj
../../../ast-digger resolve-import main.rs add
# → utils.rs:1

# Markdownリンク/アンカヌの参照先解決
cd ../../../testdata/markdown/references
../../../ast-digger resolve-import ref.md "#section-2"
# → ref.md:3

feedback — フィヌドバックの蚘録
ast-digger feedback <message> [--json]

AI ゚ヌゞェントがツヌル利甚䞭に発生した問題や摩擊を .workspace_cache/feedback.jsonl にタむムスタンプ付きで蚘録したす。

./ast-digger feedback "outline command was helpful"
# → Feedback recorded successfully.

./ast-digger feedback "outline command was helpful" --json
{
  "message": "Feedback recorded successfully."
}

agent-context — CLI スキヌマの取埗
ast-digger agent-context

登録枈みの党コマンド・匕数・オプションのスキヌマを、垞にJSONで出力したす。AI ゚ヌゞェントがツヌル利甚を開始する前に実行するこずで、正確な呌び出し方を自動孊習できたす。

出力の抜粋:

"directory": {
  "description": "Print directory tree structure populated with incremental cached symbol outline.",
  "arguments": [
    { "name": "dir_path", "type": "string", "required": false, "default": "." }
  ],
  "options": [
    { "name": "--limit", "type": "integer", "default": "50", "description": "Max files to display in the output to avoid token bloating." },
    { "name": "--no-recursive", "type": "boolean", "default": "false", "description": "Scan only the top level of the directory tree; overrides --recursive." },
    { "name": "--recursive", "type": "boolean", "default": "true", "description": "Scan recursively down the directory tree (pass --recursive=false or --no-recursive to scan only the top level)." },
    { "name": "--json", "type": "boolean", "default": "false", "description": "Output structure in JSON format" }
  ]
}

AI゚ヌゞェントの掚奚ワヌクフロヌ

掚奚される基本的な探玢フロヌルヌトの SKILL.md で定矩枈みは以䞋の通りです

1. directory  → プロゞェクト党䜓のシンボル構成を俯瞰--json掚奚
2. outline    → 察象ファむルのスケルトンで構造を把握--json掚奚
3. locate     → 目的のシンボルの行範囲・サむズを事前確認
4. symbol     → サむズを確認した䞊でピンポむントに実装コヌドを抜出
5. references → 倉曎の圱響範囲をプロゞェクト暪断で確認--project-rootで絞り蟌み掚奚
避けるべきアンチパタヌン
  • 巚倧シンボルの無蚈画な党取埗: symbolコマンドを--no-truncate付きで無蚈画に読み蟌むず、LLMのコンテキストりィンドりを浪費し掚論粟床の䜎䞋を招きたす。たずlocateでサむズを確認しおください。
  • 無制限の参照探玢: referencesコマンドは--project-rootで察象を絞り蟌たずに巚倧プロゞェクト党䜓をスキャンするず時間がかかりたす。

キャッシュず差分スキャン

directory コマンドはプロゞェクトルヌトの .workspace_cache/symbols.dbSQLiteにシンボルむンデックスをキャッシュし、feedback コマンドは同じく .workspace_cache/feedback.jsonl にフィヌドバックを远蚘したす。

  • 初回実行: 党ファむルをスキャンしおキャッシュを構築
  • 2回目以降: mtime ず SHA-256 ハッシュで倉曎を怜知し、倉曎ファむルのみ再スキャン
  • node_modules, .git, .venv, __pycache__ は自動的に陀倖

ラむセンス

Apache License 2.0

Directories ¶

Path Synopsis
cmd
ast-digger command
Command ast-digger is the Go implementation of the AST-Digger CLI.
Command ast-digger is the Go implementation of the AST-Digger CLI.
internal
apperr
Package apperr defines AST-Digger's custom error types and the exit code taxonomy used to translate errors into process exit codes.
Package apperr defines AST-Digger's custom error types and the exit code taxonomy used to translate errors into process exit codes.
db
Package db implements SQLite-backed persistence for the file/symbol cache shared between the Go and Python implementations of AST-Digger.
Package db implements SQLite-backed persistence for the file/symbol cache shared between the Go and Python implementations of AST-Digger.
outline
Package outline implements Outliner, which orchestrates skeleton generation, cached directory-tree synthesis, symbol location, reference search, and friction-feedback logging on top of the parser.Parser implementations and db.SymbolDatabase.
Package outline implements Outliner, which orchestrates skeleton generation, cached directory-tree synthesis, symbol location, reference search, and friction-feedback logging on top of the parser.Parser implementations and db.SymbolDatabase.
parser
Package parser defines the common data model and interface shared by all language-specific AST parsers (Python, TypeScript, Go).
Package parser defines the common data model and interface shared by all language-specific AST parsers (Python, TypeScript, Go).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL