代码架构图生成
从 Trailmark 代码图生成 Mermaid 图表。可生成调用图、类层次结构、模块依赖图、包含关系图、复杂度热力图以及攻击面数据流可视化。适用于可视化代码架构、绘制调用图、生成类图、创建依赖图、生成复杂度热力图或可视化数据流和攻击面路径。
文件预览
---
name: diagramming-code
description: >
Generates Mermaid diagrams from Trailmark code graphs. Produces call graphs,
class hierarchies, module dependency maps, containment diagrams, complexity
heatmaps, and attack surface data flow visualizations. Use when visualizing
code architecture, drawing call graphs, generating class diagrams, creating
dependency maps, producing complexity heatmaps, or visualizing data flow
and attack surface paths as Mermaid diagrams.
---
# Diagramming Code
Generates Mermaid diagrams from Trailmark's code graph. A pre-made script
handles Mermaid syntax generation; Claude selects the diagram type and
parameters.
## When to Use
- Visualizing call paths between functions
- Drawing class inheritance hierarchies
- Mapping module import dependencies
- Showing class structure with members
- Highlighting complexity hotspots with color coding
- Tracing data flow from entrypoints to sensitive functions
## When NOT to Use
- Querying the graph without visualization (use the `trailmark` skill)
- Mutation testing triage (use the `genotoxic` skill)
- Architecture diagrams not derived from code (draw by hand)
## Prerequisites
**trailmark** must be installed. If `uv run trailmark` fails, run:
```bash
uv pip install trailmark
```
**DO NOT** fall back to hand-writing Mermaid from source code reading. The
script uses Trailmark's parsed graph for accuracy. If installation fails,
report the error to the user.
---
## Quick Start
```bash
uv run {baseDir}/scripts/diagram.py \
--target {targetDir} --language auto --type call-graph \
--focus main --depth 2
```
Output is raw Mermaid text. Wrap in a fenced code block:
````markdown
```mermaid
flowchart TB
...
```
````
---
## Diagram Types
```
├─ "Who calls what?" → --type call-graph
├─ "Class inheritance?" → --type class-hierarchy
├─ "Module dependencies?" → --type module-deps
├─ "Class members and structure?" → --type containment
├─ "Where is complexity highest?" → --type complexity
└─ "Path from input to function?" → --type data-flow
```
For detailed examples of each type, see
[references/diagram-types.md](references/diagram-types.md).
---
## Workflow
```
Diagram Progress:
- [ ] Step 1: Verify trailmark is installed
- [ ] Step 2: Identify diagram type from user request
- [ ] Step 3: Determine focus node and parameters
- [ ] Step 4: Run diagram.py script
- [ ] Step 5: Verify output is non-empty and well-formed
- [ ] Step 6: Embed diagram in response
```
**Step 1:** Run `uv run trailmark analyze --language auto --summary {targetDir}`. Install
if it fails. Then run pre-analysis via the programmatic API:
```python
from trailmark.query.api import QueryEngine
engine = QueryEngine.from_directory("{targetDir}", language="auto")
engine.preanalysis()
```
Pre-analysis enriches the graph with blast radius, taint propagation,
and privilege boundary data used by `data-flow` diagrams.
If auto-detection is wrong for the target, rerun with an explicit language or
comma-separated list such as `python,rust`.
**Step 2:** Match the user's request to a `--type` using the decision tree
above.
**Step 3:** For `call-graph` and `data-flow`, identify the focus function.
Default `--depth 2`. Use `--direction LR` for dependency flows.
**Step 4:** Run the script and capture stdout.
**Step 5:** Check: output starts with `flowchart` or `classDiagram`,
contains at least one node. If empty or malformed, consult
[references/mermaid-syntax.md](references/mermaid-syntax.md).
**Step 6:** Wrap output in ` ```mermaid ``` ` code fence.
---
## Script Reference
```
uv run {baseDir}/scripts/diagram.py [OPTIONS]
```
| Argument | Short | Default | Description |
|---|---|---|---|
| `--target` | `-t` | required | Directory to analyze |
| `--language` | `-l` | `python` | Source language |
| `--type` | `-T` | required | Diagram type (see above) |
| `--focus` | `-f` | none | Center diagram on this node |
| `--depth` | `-d` | `2` | BFS traversal depth |
| `--direction` | | `TB` | Layout: `TB` (top-bottom) or `LR` (left-right) |
| `--threshold` | | `10` | Min complexity for `complexity` type |
### Examples
```bash
# Call graph centered on a function
uv run {baseDir}/scripts/diagram.py -t src/ -T call-graph -f parse_file
# Class hierarchy for a Rust project
uv run {baseDir}/scripts/diagram.py -t src/ -l rust -T class-hierarchy
# Module dependency map, left-to-right
uv run {baseDir}/scripts/diagram.py -t src/ -T module-deps --direction LR
# Class members
uv run {baseDir}/scripts/diagram.py -t src/ -T containment
# Complexity heatmap (threshold 5)
uv run {baseDir}/scripts/diagram.py -t src/ -T complexity --threshold 5
# Data flow from entrypoints to a specific function
uv run {baseDir}/scripts/diagram.py -t src/ -T data-flow -f execute_query
```
---
## Customization
**Direction:** Use `TB` (default) for hierarchical views, `LR` for
left-to-right flows like dependency chains.
**Depth:** Increase `--depth` to see more of the call graph. Decrease to
reduce clutter. The script warns if the diagram exceeds 100 nodes.
**Focus:** Always use `--focus` for `call-graph` on non-trivial codebases.
For `data-flow`, omitting focus auto-targets the top 10 complexity hotspots.
**Language:** Prefer `--language auto` for polyglot or unfamiliar repos.
Use an explicit language only when you know the target is single-language or
you need to exclude unrelated components.
---
## Supporting Documentation
- **[references/diagram-types.md](references/diagram-types.md)** -
Detailed docs and Mermaid examples for each diagram type
- **[references/mermaid-syntax.md](references/mermaid-syntax.md)** -
ID sanitization, escaping, style definitions, and common pitfalls
SKILL.md
| name | diagramming-code |
|---|---|
| description | 从 Trailmark 代码图生成 Mermaid 图表。可生成调用图、类层次结构、模块依赖图、包含关系图、复杂度热力图以及攻击面数据流可视化。适用于可视化代码架构、绘制调用图、生成类图、创建依赖图、生成复杂度热力图或可视化数据流和攻击面路径。 |
代码绘图
从 Trailmark 的代码图生成 Mermaid 图表。一个预制的脚本处理 Mermaid 语法生成;Claude 选择图表类型和参数。
使用时机
- 可视化函数之间的调用路径
- 绘制类继承层次结构
- 映射模块导入依赖关系
- 显示包含成员的类结构
- 使用颜色编码突出复杂度热点
- 追踪从入口点到敏感函数的数据流
不使用时机
- 在没有可视化的情况下查询图(使用
trailmark技能) - 变异测试分类(使用
genotoxic技能) - 不是从代码推导出的架构图(手动绘制)
前提条件
必须安装 trailmark。如果 uv run trailmark 失败,运行:
uv pip install trailmark不要 从源代码阅读手动编写 Mermaid 作为回退方案。该脚本使用 Trailmark 解析后的图以确保准确性。如果安装失败,向用户报告错误。
快速开始
uv run {baseDir}/scripts/diagram.py \
--target {targetDir} --language auto --type call-graph \
--focus main --depth 2输出为原始 Mermaid 文本。放入带代码围栏的代码块中:
```mermaid
flowchart TB
...
```图表类型
├─ “谁调用了谁?” → --type call-graph
├─ “类继承?” → --type class-hierarchy
├─ “模块依赖?” → --type module-deps
├─ “类成员和结构?” → --type containment
├─ “哪里复杂度最高?” → --type complexity
└─ “从输入到函数的路径?” → --type data-flow每种类型的详细示例见 references/diagram-types.md。
工作流程
图表制作进度:
- [ ] 步骤 1:验证 trailmark 已安装
- [ ] 步骤 2:根据用户请求确定图表类型
- [ ] 步骤 3:确定焦点节点和参数
- [ ] 步骤 4:运行 diagram.py 脚本
- [ ] 步骤 5:验证输出非空且格式正确
- [ ] 步骤 6:在响应中嵌入图表步骤 1: 运行 uv run trailmark analyze --language auto --summary {targetDir}。如果失败则安装。然后通过编程接口运行预分析:
from trailmark.query.api import QueryEngine
engine = QueryEngine.from_directory("{targetDir}", language="auto")
engine.preanalysis()预分析通过冲击半径、污点传播和权限边界数据丰富图,供 data-flow 图表使用。
如果目标的自动检测错误,使用明确的语言或逗号分隔的列表(如 python,rust)重新运行。
步骤 2: 使用上述决策树将用户的请求匹配到一个 --type。
步骤 3: 对于 call-graph 和 data-flow,确定焦点函数。默认 --depth 2。依赖流使用 --direction LR。
步骤 4: 运行脚本并捕获标准输出。
步骤 5: 检查:输出以 flowchart 或 classDiagram 开头,且至少包含一个节点。如果为空或格式错误,查阅
references/mermaid-syntax.md。
步骤 6: 将输出包裹在 ```mermaid ``` 代码围栏中。
脚本参考
uv run {baseDir}/scripts/diagram.py [OPTIONS]| 参数 | 短选项 | 默认值 | 描述 |
|---|---|---|---|
--target | -t | 必须 | 要分析的目录 |
--language | -l | python | 源语言 |
--type | -T | 必须 | 图表类型(见上文) |
--focus | -f | 无 | 将图表中心设为此节点 |
--depth | -d | 2 | BFS 遍历深度 |
--direction | TB | 布局:TB(上下)或 LR(左右) | |
--threshold | 10 | complexity 类型的最低复杂度 |
示例
# 以函数为中心的调用图
uv run {baseDir}/scripts/diagram.py -t src/ -T call-graph -f parse_file
# Rust 项目的类层次结构
uv run {baseDir}/scripts/diagram.py -t src/ -l rust -T class-hierarchy
# 模块依赖图,从左到右
uv run {baseDir}/scripts/diagram.py -t src/ -T module-deps --direction LR
# 类成员
uv run {baseDir}/scripts/diagram.py -t src/ -T containment
# 复杂度热力图(阈值 5)
uv run {baseDir}/scripts/diagram.py -t src/ -T complexity --threshold 5
# 从入口点到指定函数的数据流
uv run {baseDir}/scripts/diagram.py -t src/ -T data-flow -f execute_query自定义
方向: 层次化视图使用 TB(默认),从左到右的流(如依赖链)使用 LR。
深度: 增加 --depth 可查看调用图的更多内容。减少以减少杂乱。如果图表超过 100 个节点,脚本会发出警告。
焦点: 对于非平凡代码库,始终为 call-graph 使用 --focus。对于 data-flow,省略焦点会自动瞄准前 10 个复杂度热点。
语言: 对于多语言或不熟悉的仓库,优先使用 --language auto。只有当你知道目标是单语言或需要排除不相关组件时,才使用明确的语言。
支持文档
- references/diagram-types.md - 每种图表类型的详细文档和 Mermaid 示例
- references/mermaid-syntax.md - ID 清理、转义、样式定义和常见陷阱