代码补丁可视化解析
patch-explainer
深度代码分析,通过 ASCII 可视化展示代码结构、流程和状态转换。适用于分析补丁/diff、解释类或子系统、理解代码架构、审查变更中的不一致之处,或被要求可视化代码的工作原理时。提供前后对比图、数据/控制流、并发分析、假设条件和失败模式。触发词:explain this patch/code/class、how does X work、show me the flow、visualize this change、代码审查请求,或在 PR 审查期间主动分析。
- License 许可
- Apache-2.0
- 所属仓库
- apache/cassandra
- 最近更新
- 2026-11-14
- Stars
- ★ 1.0w
- Forks
- 4.1k
01 前言 Front Matter
- name
- patch-explainer
- version
- 1.0.0
- description
- Deep code analysis with ASCII visualizations showing structure, flow, and state transitions. Use when analyzing patches/diffs, explaining classes or subsystems, understanding code architecture, reviewing changes for inconsistencies, or when asked to visualize how code works. Provides before/after diagrams, data/control flow, concurrency analysis, assumptions, and failure modes. Triggers on explain this patch/code/class, how does X work, show me the flow, visualize this change, code review requests, or proactive analysis during PR reviews.
02 资源文件 2
- 参考文档references/analysis_framework.md9.9 KB
- 参考文档references/ascii_patterns.md14.1 KB
03 SKILL.md 正文
Patch Explainer
Deeply analyze and visualize code using ASCII diagrams to reveal structure, behavior, and interactions.
What This Skill Does
Provides comprehensive code analysis with rich ASCII visualizations for:
- Patches/diffs: Before/after states, what changed and why
- Classes: Purpose, structure, state transitions, workflows
- Subsystems: Component interactions, data flow, architecture
- Repositories: Overall structure, key abstractions, major patterns
- Code reviews: Finding inconsistencies, analyzing assumptions and failure modes
Analysis Approach
For Any Code (Patches, Classes, Subsystems, Repositories)
-
Identify the core purpose - What does this code do and why does it exist?
-
Create ASCII visualizations showing:
- Structure (components, layers, dependencies)
- Behavior (data flow, control flow, state transitions)
- Interactions (sequence diagrams, message passing, concurrency)
- For patches: before/after comparisons
-
Deep reasoning about:
- Assumptions: What must be true for this to work correctly?
- Failure modes: What could go wrong at each step?
- Concurrency: If multiple threads/processes, show their interactions
- Invariants: What must always hold true?
-
Focus on the "why":
- Why does this code exist?
- Why was this approach chosen?
- For changes: Why this modification? What problem does it solve?
Visualization Strategy
Choose diagram types based on what you're explaining:
State machines - When code manages states/status:
[Initial] --event--> [Processing] --success--> [Complete]
|
failure
|
v
[Failed]
Data flow - When showing information movement:
Input → [Transform A] → [Transform B] → Output
Sequence diagrams - When showing component interactions:
Client Server Database
| | |
|--request----->| |
| |----query------->|
| |<---result-------|
|<--response----| |
Component structure - When showing architecture:
┌─────────────────────┐
│ Application │
└──────────┬──────────┘
|
┌──────────┴──────────┐
│ Service Layer │
└──────────┬──────────┘
|
┌──────────┴──────────┐
│ Data Layer │
└─────────────────────┘
Before/after - When explaining changes:
BEFORE AFTER
─────────────────────────────────
[A] → [B] → [C] [A] → [Cache?] ─Yes→ [C]
|
No
v
[B] → [C]
Reference Materials
ASCII diagram templates: See ascii_patterns.md for:
- State machine patterns
- Data flow diagrams
- Sequence diagrams
- Component/architecture diagrams
- Before/after comparisons
- Concurrency patterns
- Control flow patterns
- Dependency graphs
Analysis methodology: See analysis_framework.md for:
- Structured analysis process for patches vs existing code
- Concurrency analysis checklist
- Assumption identification patterns
- Failure mode identification
- Analysis depth guidelines by scope (function → class → subsystem → repository)
Analysis Workflow
For Patches/Diffs
- Read the patch to understand what changed
- Identify the change type (bug fix, feature, refactor, optimization)
- Create before/after ASCII diagrams showing:
- Old behavior vs new behavior
- State transitions that changed
- Data flow modifications
- Explain what fundamentally changed:
- What problem did the old code have?
- How does the new code solve it?
- What assumptions changed?
- Analyze failure modes:
- What could go wrong with this change?
- Are there edge cases not handled?
- Concurrency implications?
- Focus on the "why": What problem does each modification solve?
For Classes
- Read the class to understand its purpose
- Create ASCII diagram showing:
- Class structure (key fields/methods)
- State machine if it manages state
- How it fits with related classes
- Explain key workflows (2-3 most important methods)
- Identify assumptions and invariants
- Analyze failure modes and edge cases
- Focus on the "why": Why does this class exist? What problem does it solve?
For Subsystems
- Identify the components in the subsystem
- Create ASCII diagram showing:
- Component relationships
- Key interfaces/boundaries
- Main data flows
- Trace 2-3 key workflows through the subsystem
- Explain how components interact
- Identify assumptions across component boundaries
- Focus on the "why": What is this subsystem's role in the larger system?
For Repositories
- Understand the high-level architecture
- Create ASCII diagram showing:
- Major modules/packages
- Layered architecture
- Key abstractions
- Identify core workflows
- Explain main patterns used throughout
- Highlight critical subsystems
- Focus on the "why": What problem does this codebase solve?
Concurrency Analysis
When analyzing concurrent code, always show:
Thread interactions:
Thread A State Thread B
| | |
|--lock()------------>| |
| [LOCKED] |
|--modify()---------->| |
|--unlock()---------->| |
| [UNLOCKED] |
| |<---------lock()-----|
| [LOCKED] |
Race conditions:
Thread A Thread B
| |
|--read(x=10) |
| |--read(x=10)
|--x=x+1 |
| |--x=x+1
|--write(x=11) |
| |--write(x=11) ⚠ Lost update!
Result: x=11 (expected: x=12)
Check for:
- Shared mutable state without synchronization
- Lock ordering violations (potential deadlocks)
- Race conditions in read-modify-write operations
- Missing memory barriers
Output Structure
Structure your analysis as:
1. Executive Summary
Brief overview (2-3 sentences) of what this code does and why it matters.
2. Visual Overview
High-level ASCII diagram showing the main structure or flow.
3. Detailed Analysis
Organized by aspect:
- Purpose: What it does and why it exists
- Structure: Components and their relationships
- Key Workflows: Step-by-step execution of main scenarios
- State Management: States and transitions (if applicable)
- Assumptions: What must be true for correctness
- Failure Modes: What could go wrong
- Concurrency: Thread safety analysis (if applicable)
4. For Patches: Before/After
- Before state (with diagram)
- After state (with diagram)
- Why this change was made
- Critical modifications explained
5. Key Insights
Bullet points highlighting:
- Most important findings
- Potential issues or risks
- Recommendations (if applicable)
Prioritization
Focus on high-impact elements:
- Critical paths: Main workflows that matter most
- High-risk code: Concurrency, error handling, resource management
- Complex logic: Non-obvious algorithms or subtle interactions
- Key abstractions: Core interfaces and contracts
Minimize or skip:
- Boilerplate code
- Simple getters/setters
- Standard patterns done correctly
- Low-risk trivial changes
Example Scenarios
Scenario 1: "Explain this patch" → Show before/after diagrams, explain what changed and why, analyze assumptions and failure modes
Scenario 2: "How does SafeCommandStore work?" → Show class structure, explain the exclusive access pattern, trace key methods, analyze thread safety
Scenario 3: "Explain the coordination subsystem" → Show component diagram, trace PreAccept→Accept→Commit flow, explain message passing, show quorum tracking
Scenario 4: "What's the architecture of this codebase?" → Show high-level module structure, identify key abstractions, explain main patterns, highlight critical subsystems
Scenario 5: During code review (proactive) → Analyze changes for inconsistencies, check assumptions, identify potential race conditions, verify error handling
04 同仓库更多 Skills
Bug复现程序编写
write-reproducer
为任何项目编写最小化、可靠的 bug 复现程序(repro)。使用时机:(1) bug 报告或 issue 需要一个能够可靠演示失败的测试;(2) 将描述的失败场景转换为可运行代码;(3) 将现有复杂测试最小化为必要的触发条件。涵盖完整的复现工作流:失败特征化、范围选择、编写复现程序、验证其因正确的原因失败,以及最小化到尽可能小的触发条件。
TLA+形式化验证
tla-plus
创建、运行和验证 TLA+ 与 PlusCal 形式化规约。用于对分布式系统、协议、并发算法和状态机进行建模。可以从代码编写规约、发现规约与实现之间的分歧、识别并发 bug 和不变量违规。当用户要求“write a TLA+ spec”、“model check”、“verify protocol”、“find race conditions”或“formal verification”时使用。
定向代码审查
targeted-review
发现驱动的定向代码审查。通过 patch-explainer 理解补丁,通过 codebase-analysis 理解周边代码,然后查阅 bug 模式目录(references/categories/ 下约 11 个类别)。仅挑选 diff 信号与补丁匹配的类别,以及“look for”提示与实际代码形态匹配的条目,按审查重点(函数/文件/特性)分组,并生成并行子代理——每个子代理都有紧凑的范围和经过挑选的清单。适用于中大型补丁(50-1000 行代码),可作为整补丁集成式审查或整文件深度审查的聚焦替代方案。触发条件包括:“review this patch/diff/change”、“find bugs in this change”、“scoped review”、“what could go wrong here”、“review using findings”,或主动进行 PR 审查。
快速并行代码审查
shallow-review
使用 6 个专家代理并行开展浅层(快速)集成式 bug 查找审查。每个专家通过不同视角审查同一补丁:逻辑与类型、边界与 I/O、并发与状态、资源与序列化、缺失分析以及 API 完整性。发现的问题会被合并并去重。最适合:补丁的快速初审、diff 分诊、广泛的表层 bug 扫描。如需以文件为中心、包含完整模式目录和代码库调查的更深入审查,请改用 deep-review 技能。
大型补丁多轮深度代码审查
mega-review
针对大型补丁(1000+ 行代码)的多轮深度代码审查,最大化真实 Bug 的检出率。并行编排 targeted-review、shallow-review 和 deep-review,覆盖所有高风险(HIGH)和中风险(MEDIUM)文件与提交:从整体上理解特性,按文件和提交拆分,对每个 HIGH/MEDIUM 文件运行深度审查,在同一范围内运行定向审查,并对每个提交执行浅层审查,随后进行交叉一致性检查。适用场景:审查大型补丁(特性分支、多提交或单个大型 diff,1000+ 行代码);进行彻底的合并前审查;或当浅层审查因补丁规模过大而漏掉 Bug 时。触发词:"review this branch"、"review these commits"、"review this feature"、"mega review"、"thorough review"、"full review of X commits",或当用户指定提交范围或特性进行审查时。
Git 代码热力图分析
heatmap
使用 Git 热力图分析识别高变动量的文件和代码行,作为深入审查或 Bug 排查的候选对象。适用于 PR 审查、安全审计、Bug 排查或任何代码分析任务。