Grepai Trace Graph

yoanbernabeu/grepai-skills/skills/trace/grepai-trace-graph

作者 yoanbernabeu382d40261c0d41109c6e11872574ba0be9b064d0無授權條款收錄於 2026年10月9日更新於 2026年10月9日

Build complete call graphs with GrepAI trace. Use this skill for recursive dependency analysis.

AI 產生的概覽

指導使用 grepai trace graph 指令建立遞迴呼叫圖,用於程式碼相依性與影響分析。

功能
此技能說明如何執行 grepai trace graph 產生函式相依性的遞迴呼叫圖。內容涵蓋深度控制、擷取模式、JSON、精簡 JSON 與 TOON 輸出、循環標記,以及後處理為 Graphviz DOT 或 Mermaid 圖。也提供大型圖處理、複雜度追蹤與常見問題的建議。
適用情境
適用於梳理函式相依性、理解複雜程式流程、規劃重構或評估變更影響。也適合需要呼叫圖資料的程式碼審查與架構文件任務。
執行需求
需要具備 trace graph 指令的 grepai CLI,以及已建立索引的程式碼庫。選用後處理會用到 python3、jq 與 Graphviz dot;此技能未附帶指令碼。

GrepAI Trace Graph

This skill covers using grepai trace graph to build complete call graphs showing all dependencies recursively.

When to Use This Skill

  • Mapping complete function dependencies
  • Understanding complex code flows
  • Impact analysis for major refactoring
  • Visualizing application architecture

What is Trace Graph?

grepai trace graph builds a recursive dependency tree:

main├── initialize│   ├── loadConfig│   │   └── parseYAML│   └── connectDB│       ├── createPool│       └── ping├── startServer│   ├── registerRoutes│   │   ├── authMiddleware│   │   └── loggingMiddleware│   └── listen└── gracefulShutdown    └── closeDB

Basic Usage

bash
grepai trace graph "FunctionName"

Example

bash
grepai trace graph "main"

Output:

🔍 Call Graph for "main"
main├── initialize│   ├── loadConfig│   └── connectDB├── startServer│   ├── registerRoutes│   └── listen└── gracefulShutdown    └── closeDB
Nodes: 9Max depth: 3

Depth Control

Limit recursion depth with --depth:

bash
# Default depth (2 levels)grepai trace graph "main"
# Deeper analysis (3 levels)grepai trace graph "main" --depth 3
# Shallow (1 level, same as callees)grepai trace graph "main" --depth 1
# Very deep (5 levels)grepai trace graph "main" --depth 5

Depth Examples

--depth 1 (same as callees):

main├── initialize├── startServer└── gracefulShutdown

--depth 2 (default):

main├── initialize│   ├── loadConfig│   └── connectDB├── startServer│   ├── registerRoutes│   └── listen└── gracefulShutdown    └── closeDB

--depth 3:

main├── initialize│   ├── loadConfig│   │   └── parseYAML│   └── connectDB│       ├── createPool│       └── ping├── startServer│   ├── registerRoutes│   │   ├── authMiddleware│   │   └── loggingMiddleware│   └── listen└── gracefulShutdown    └── closeDB

JSON Output

bash
grepai trace graph "main" --depth 2 --json

Output:

json
{  "query": "main",  "mode": "graph",  "depth": 2,  "root": {    "name": "main",    "file": "cmd/main.go",    "line": 10,    "children": [      {        "name": "initialize",        "file": "cmd/main.go",        "line": 15,        "children": [          {            "name": "loadConfig",            "file": "config/config.go",            "line": 20,            "children": []          },          {            "name": "connectDB",            "file": "db/db.go",            "line": 30,            "children": []          }        ]      },      {        "name": "startServer",        "file": "server/server.go",        "line": 25,        "children": [          {            "name": "registerRoutes",            "file": "server/routes.go",            "line": 10,            "children": []          }        ]      }    ]  },  "stats": {    "nodes": 6,    "max_depth": 2  }}

Compact JSON

bash
grepai trace graph "main" --depth 2 --json --compact

Output:

json
{  "q": "main",  "d": 2,  "r": {    "n": "main",    "c": [      {"n": "initialize", "c": [{"n": "loadConfig"}, {"n": "connectDB"}]},      {"n": "startServer", "c": [{"n": "registerRoutes"}]}    ]  },  "s": {"nodes": 6, "depth": 2}}

TOON Output (v0.26.0+)

TOON format offers ~50% fewer tokens than JSON:

bash
grepai trace graph "main" --depth 2 --toon

Note: --json and --toon are mutually exclusive.

Extraction Modes

bash
# Fast mode (regex-based)grepai trace graph "main" --mode fast
# Precise mode (tree-sitter AST)grepai trace graph "main" --mode precise

Use Cases

Understanding Application Flow

bash
# Map entire application startupgrepai trace graph "main" --depth 4

Impact Analysis

bash
# What depends on this utility function?grepai trace graph "validateInput" --depth 3
# Full impact of changing database layergrepai trace graph "executeQuery" --depth 2

Code Review

bash
# Is this function too complex?grepai trace graph "processOrder" --depth 5# Many nodes = high complexity

Documentation

bash
# Generate architecture diagram datagrepai trace graph "main" --depth 3 --json > architecture.json

Refactoring Planning

bash
# What would break if we change this?grepai trace graph "legacyAuth" --depth 3

Handling Cycles

GrepAI detects and marks circular dependencies:

main├── processA│   └── processB│       └── processA [CYCLE]

In JSON:

json
{  "name": "processA",  "cycle": true}

Large Graphs

For very large codebases, graphs can be overwhelming:

Limit Depth

bash
# Start shallowgrepai trace graph "main" --depth 2

Focus on Specific Areas

bash
# Instead of main, trace specific subsystemgrepai trace graph "authMiddleware" --depth 3

Filter in Post-Processing

bash
# Get JSON and filtergrepai trace graph "main" --depth 3 --json | jq '...'

Visualizing Graphs

Export to DOT Format (Graphviz)

bash
# Convert JSON to DOTgrepai trace graph "main" --depth 3 --json | python3 << 'EOF'import jsonimport sys
data = json.load(sys.stdin)
print("digraph G {")print("  rankdir=TB;")
def traverse(node, parent=None):    name = node.get('name') or node.get('n')    if parent:        print(f'  "{parent}" -> "{name}";')    children = node.get('children') or node.get('c') or []    for child in children:        traverse(child, name)
traverse(data.get('root') or data.get('r'))print("}")EOF

Then render:

bash
dot -Tpng graph.dot -o graph.png

Mermaid Diagram

bash
grepai trace graph "main" --depth 2 --json | python3 << 'EOF'import jsonimport sys
data = json.load(sys.stdin)
print("```mermaid")print("graph TD")
def traverse(node, parent=None):    name = node.get('name') or node.get('n')    if parent:        print(f"  {parent} --> {name}")    children = node.get('children') or node.get('c') or []    for child in children:        traverse(child, name)
traverse(data.get('root') or data.get('r'))print("```")EOF

Comparing Graph Sizes

Track complexity over time:

bash
# Get node countgrepai trace graph "main" --depth 3 --json | jq '.stats.nodes'
# Compare before/after refactoringecho "Before: $(grepai trace graph 'main' --depth 3 --json | jq '.stats.nodes') nodes"# ... refactoring ...echo "After: $(grepai trace graph 'main' --depth 3 --json | jq '.stats.nodes') nodes"

Common Issues

❌ Problem: Graph too large / timeout ✅ Solutions:

  • Reduce depth: --depth 2
  • Trace specific function instead of main
  • Use --mode fast

❌ Problem: Many cycles detected ✅ Solution: This indicates circular dependencies in code. Consider refactoring.

❌ Problem: Missing branches ✅ Solutions:

  • Try --mode precise
  • Check if files are indexed
  • Verify language is enabled

Best Practices

  1. Start shallow: Begin with --depth 2, increase as needed
  2. Focus analysis: Trace specific functions, not always main
  3. Export for docs: Use JSON for generating diagrams
  4. Track over time: Monitor node count as complexity metric
  5. Investigate cycles: Circular dependencies are code smells

Output Format

Trace graph result:

🔍 Call Graph for "main"
Depth: 3Mode: fast
main├── initialize│   ├── loadConfig│   │   └── parseYAML│   └── connectDB│       ├── createPool│       └── ping├── startServer│   ├── registerRoutes│   │   ├── authMiddleware│   │   └── loggingMiddleware│   └── listen└── gracefulShutdown    └── closeDB
Statistics:- Total nodes: 12- Maximum depth reached: 3- Cycles detected: 0
Tip: Use --json for machine-readable output     Use --depth N to control recursion depth

來源與署名

來源:yoanbernabeu/grepai-skills位於skills/trace/grepai-trace-graph提交382d402

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架