AgentSkillsCN

code-graph

分析代码库并查询其依赖图。在进行代码更改之前,务必咨询此技能——使用查询工具检查文件修改的影响、依赖关系以及潜在风险。切勿一次性读取完整的JSON——始终采用有针对性的查询。

SKILL.md
--- frontmatter
name: code-graph
description: Analyze a codebase and query its dependency graph. This skill MUST be consulted before making code changes — use the query tool to check impact, dependencies, and risk of files being modified. Never read the full JSON — always use targeted queries.

Code Graph — Codebase Intelligence for AI Development

🔴 MANDATORY: Query Before Every Code Change

Before modifying ANY code, Claude MUST run a targeted query to understand the impact.

NEVER read .code_graph.json directly — it's too large. Always use query_graph.py.

Quick Reference

bash
# What am I about to change? Check risk and connections
python3 .claude/skills/code-graph/scripts/query_graph.py file <path>

# What breaks if I change this? Full cascade analysis
python3 .claude/skills/code-graph/scripts/query_graph.py impact <path>

# What does this file depend on?
python3 .claude/skills/code-graph/scripts/query_graph.py deps <path>

# What depends on this file?
python3 .claude/skills/code-graph/scripts/query_graph.py dependents <path>

All Available Commands

CommandUsageReturns
file <path>Check a fileRisk level, all connections in/out
impact <path>Impact analysisFull cascade: what breaks if this changes (3 levels deep)
deps <path>DependenciesWhat this file depends on (outgoing)
dependents <path>Reverse depsWhat depends on this file (incoming)
model <name>Model usageAll readers/writers of a DB model
hubs [--top N]Hub nodesMost connected nodes = riskiest to change
cluster <path>File clusterAll files in the same connected component
path <from> <to>Find pathShortest connection path between two files
search <query>SearchFind nodes by name or file path
statsStatisticsProject summary, node/edge counts
dead-codeDead codeIsolated or unreferenced files
risky-files [--top N]Risk rankingFiles ranked by change risk score
endpoint <path>Endpoint chainFull request chain: endpoint → service → model → cache
overviewArchitectureCompact project structure overview
reportFull reportOne call: overview + risks + dead code + coverage gaps
changes <f1> <f2>...Multi-file checkPre-change analysis for multiple files at once

When to Use Which Command

"I need to edit a file"

bash
# Step 1: Check risk
python3 .claude/skills/code-graph/scripts/query_graph.py file views/users.py

# Step 2: If risk is MEDIUM or higher, check impact
python3 .claude/skills/code-graph/scripts/query_graph.py impact views/users.py

"I need to add a new feature"

bash
# Step 1: Understand the area
python3 .claude/skills/code-graph/scripts/query_graph.py overview

# Step 2: Find related existing code
python3 .claude/skills/code-graph/scripts/query_graph.py search <feature-keyword>

# Step 3: Check cluster patterns for consistency
python3 .claude/skills/code-graph/scripts/query_graph.py cluster <related-file>

"I need to change a model/schema"

bash
# ALWAYS check model usage before schema changes — this is HIGH RISK
python3 .claude/skills/code-graph/scripts/query_graph.py model User

"I need to fix a bug"

bash
# Step 1: Find the buggy file's connections
python3 .claude/skills/code-graph/scripts/query_graph.py file <buggy-file>

# Step 2: Check if the bug might exist in connected files
python3 .claude/skills/code-graph/scripts/query_graph.py deps <buggy-file>

# Step 3: Trace the request chain to find root cause
python3 .claude/skills/code-graph/scripts/query_graph.py endpoint <endpoint>

"I need to refactor"

bash
# Step 1: Identify the riskiest files
python3 .claude/skills/code-graph/scripts/query_graph.py risky-files --top 10

# Step 2: Check impact of each target file
python3 .claude/skills/code-graph/scripts/query_graph.py impact <file>

# Step 3: Find dead code to clean up
python3 .claude/skills/code-graph/scripts/query_graph.py dead-code

"How are these two things connected?"

bash
python3 .claude/skills/code-graph/scripts/query_graph.py path views/users.py models/user.py

Risk Levels

LevelMeaningAction
🟢 LOW≤3 connections, no dependentsSafe to change freely
🟡 MEDIUM4-10 connectionsCheck dependents before changing
🔴 HIGH10+ connections, widely importedRun full impact analysis, change carefully
⛔ CRITICAL20+ connections, hub nodeWarn user, suggest incremental approach

Generating / Updating the Graph

First-time setup

bash
python3 .claude/skills/code-graph/scripts/analyze_codebase.py . -o .code_graph.json --languages python,typescript --exclude migrations,node_modules,static,media,dist,.angular,__pycache__,htmlcov,fixtures

Regenerate after significant changes

bash
python3 .claude/skills/code-graph/scripts/analyze_codebase.py . -o .code_graph.json --languages python,typescript --exclude migrations,node_modules,static,media,dist,.angular,__pycache__,htmlcov,fixtures

Interactive visual viewer

bash
python3 .claude/skills/code-graph/scripts/generate_viewer.py .code_graph.json -o code_graph_viewer.html
open code_graph_viewer.html

Auto-update via git hook (recommended)

bash
# .git/hooks/post-commit
#!/bin/bash
python3 .claude/skills/code-graph/scripts/analyze_codebase.py . -o .code_graph.json --languages python,typescript --exclude migrations,node_modules,static,media,dist,.angular,__pycache__,htmlcov,fixtures