architecture-validate-architecture
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseValidate Architecture
架构验证
Table of Contents
目录
Reference → Layer Dependencies | Diff-Aware Validation | Quick Reference
Purpose
功能说明
Automates architecture validation for multiple architectural patterns (Clean Architecture, Hexagonal, Layered, MVC). Automatically detects the project's architectural style from ARCHITECTURE.md, scans all source files for import violations, validates dependency direction (inward only for Clean/Hexagonal), and reports violations with specific fixes. Adapts to any architectural pattern and provides actionable remediation guidance.
可自动完成多种架构模式(Clean Architecture、Hexagonal、分层架构、MVC)的架构验证,能从ARCHITECTURE.md自动识别项目的架构风格,扫描所有源文件的导入违规问题,验证依赖方向(Clean/Hexagonal架构仅允许向内依赖),并附带具体修复方案输出违规报告。它可适配任意架构模式,并提供可落地的修复指导。
Quick Start
快速开始
User asks: "Validate my architecture" or "Check if this follows Clean Architecture"
What happens:
- Reads project's to identify architectural pattern
ARCHITECTURE.md - Scans all source files for import violations
- Validates dependency direction (inward only for Clean/Hexagonal)
- Reports violations with file:line:fix recommendations
Result: ✅ All checks passed OR ❌ Violations with specific fixes
用户诉求:"验证我的架构" 或者 "检查是否符合Clean Architecture规范"
执行流程:
- 读取项目的识别架构模式
ARCHITECTURE.md - 扫描所有源文件的导入违规问题
- 验证依赖方向(Clean/Hexagonal架构仅允许向内依赖)
- 输出违规报告,包含文件路径、行号、修复建议
结果:✅ 所有检查通过 或者 ❌ 附带具体修复方案的违规列表
When to Use This Skill
适用场景
Invoke this skill when:
- User asks "validate architecture", "check layer boundaries", "architectural review"
- Before major refactoring or structural changes
- As part of pre-commit quality gates
- After adding new dependencies to any layer
- Reviewing code for architecture compliance
- User mentions "Clean Architecture", "Hexagonal", "Layered", or "MVC"
在以下场景调用本工具:
- 用户要求"验证架构"、"检查层边界"、"架构评审"
- 重大重构或结构调整前
- 作为pre-commit质量门禁的一部分
- 任意层新增依赖后
- 代码评审时检查架构合规性
- 用户提及"Clean Architecture"、"Hexagonal"、"分层架构"或"MVC"
Triggers
触发关键词
Trigger with phrases like:
- "validate architecture"
- "check layer boundaries"
- "architectural review"
- "validate my Clean Architecture"
- "check if this follows Hexagonal Architecture"
- "run architecture validation"
- "check for layer violations"
- "validate dependencies"
- "architectural compliance check"
可通过以下短语触发:
- "validate architecture"
- "check layer boundaries"
- "architectural review"
- "validate my Clean Architecture"
- "check if this follows Hexagonal Architecture"
- "run architecture validation"
- "check for layer violations"
- "validate dependencies"
- "architectural compliance check"
What This Skill Does
工具能力
Supported Architectural Patterns
支持的架构模式
This skill automatically adapts to:
- Clean Architecture (Concentric layers: Domain → Application → Infrastructure → Interface)
- Hexagonal Architecture (Ports and Adapters)
- Layered Architecture (Presentation → Business → Data)
- MVC (Model → View → Controller)
本工具可自动适配以下架构:
- Clean Architecture(同心层:Domain → Application → Infrastructure → Interface)
- Hexagonal Architecture(端口与适配器模式)
- 分层架构(Presentation → Business → Data)
- MVC(Model → View → Controller)
Validation Checks
验证检查项
1. Pattern Detection
- Reads or similar documentation
ARCHITECTURE.md - Identifies architectural style and layer definitions
- Parses dependency rules and constraints
2. Layer Boundary Validation
- Scans all import statements in source files
- Checks for violations (e.g., Domain importing Infrastructure)
- Detects circular dependencies between layers
3. Dependency Direction Validation
- Verifies dependencies flow correctly (inward for Clean/Hexagonal)
- Ensures outer layers depend on inner, never reverse
- Validates domain/core has no external dependencies
4. Pattern Compliance
- Checks for required patterns (ServiceResult, Repository, etc.)
- Verifies naming conventions (Services in application/, etc.)
- Validates file organization matches architectural layers
5. Anti-Pattern Detection
- Domain importing database/framework code
- Application importing concrete infrastructure
- Circular dependencies between layers
- Business logic in interface/presentation layers
1. 架构模式识别
- 读取或同类文档
ARCHITECTURE.md - 识别架构风格和层定义
- 解析依赖规则和约束
2. 层边界验证
- 扫描源文件中的所有导入语句
- 检查违规问题(例如Domain层导入Infrastructure层)
- 检测层之间的循环依赖
3. 依赖方向验证
- 验证依赖流向是否正确(Clean/Hexagonal架构仅允许向内依赖)
- 确保外层依赖内层,禁止反向依赖
- 验证domain/core层无外部依赖
4. 模式合规性检查
- 检查必填模式(ServiceResult、Repository等)是否存在
- 验证命名规范(例如Service类需放在application/目录下等)
- 验证文件组织方式是否符合架构分层要求
5. 反模式检测
- Domain层导入数据库/框架代码
- Application层导入具体的基础设施实现
- 层之间的循环依赖
- 业务逻辑写在interface/presentation层
Instructions
使用说明
Overview
概述
Validating architecture involves a 5-step process:
- Identify Architecture - Read ARCHITECTURE.md and detect pattern (Clean, Hexagonal, Layered, MVC)
- Extract Layer Definitions - Map directory structure to architectural layers
- Scan Imports - Analyze all import statements in source files
- Validate Rules - Check dependency direction and layer boundaries
- Report Violations - Generate actionable report with specific fixes
See detailed steps in Validation Process section below.
架构验证包含5个步骤:
- 识别架构 - 读取ARCHITECTURE.md并检测架构模式(Clean、Hexagonal、分层、MVC)
- 提取层定义 - 将目录结构映射到架构层
- 扫描导入语句 - 分析源文件中的所有导入语句
- 规则验证 - 检查依赖方向和层边界
- 违规报告 - 生成附带具体修复方案的可落地报告
可查看下方验证流程章节了解详细步骤。
Validation Process
验证流程
The validation process follows 5 steps:
- Identify Architecture - Read ARCHITECTURE.md and detect pattern (Clean, Hexagonal, Layered, MVC)
- Extract Layer Definitions - Map directory structure to architectural layers
- Scan Imports - Analyze all import statements in source files
- Validate Rules - Check dependency direction and layer boundaries
- Report Violations - Generate actionable report with specific fixes
See Code Examples for detailed examples of each step, including bash commands, layer definitions, validation rules, and sample output formats.
验证流程遵循5个步骤:
- 识别架构 - 读取ARCHITECTURE.md并检测架构模式(Clean、Hexagonal、分层、MVC)
- 提取层定义 - 将目录结构映射到架构层
- 扫描导入语句 - 分析源文件中的所有导入语句
- 规则验证 - 检查依赖方向和层边界
- 违规报告 - 生成附带具体修复方案的可落地报告
查看代码示例了解每个步骤的详细示例,包括bash命令、层定义、验证规则和示例输出格式。
Usage Examples
使用示例
Example 1: Validate Entire Codebase
示例1:验证全量代码库
User: "Validate architecture before I commit"
- Reads ARCHITECTURE.md and identifies pattern
- Scans all source files for imports
- Validates each layer's imports against rules
- Reports violations or confirms compliance
用户:"在我提交前验证架构"
- 读取ARCHITECTURE.md识别架构模式
- 扫描所有源文件的导入语句
- 对照规则验证每个层的导入是否合规
- 输出违规报告或确认合规
Example 2: Validate Specific Changes
示例2:验证变更内容
User: "Check if my refactoring follows Clean Architecture"
- Runs git diff to find changed files
- Filters to source files only
- Validates only modified files
- Reports violations in changed code
用户:"检查我的重构是否符合Clean Architecture规范"
- 执行git diff查找变更文件
- 过滤仅保留源文件
- 仅验证修改过的文件
- 输出变更代码中的违规问题
Example 3: Pre-Commit Hook Integration
示例3:Pre-commit钩子集成
Automatically invoked by pre-commit hook to validate architectural boundaries, block commits if critical violations found, and provide actionable fix recommendations.
可被pre-commit钩子自动调用验证架构边界,若发现严重违规则阻止提交,并提供可落地的修复建议。
Architecture-Specific Rules
架构专属规则
This skill supports four architectural patterns with specific dependency rules:
本工具支持四种架构模式的专属依赖规则:
Clean Architecture
Clean Architecture
- Dependency Rule: Dependencies flow inward only (Interface → Application → Domain ← Infrastructure)
- Layer Rules: Domain is pure, Application orchestrates, Infrastructure implements, Interface is entry points
- 依赖规则:仅允许向内依赖(Interface → Application → Domain ← Infrastructure)
- 层规则:Domain层为纯逻辑,Application层负责编排,Infrastructure层负责实现,Interface层为入口
Hexagonal Architecture
Hexagonal Architecture
- Dependency Rule: Core has no dependencies, adapters depend on ports
- Layer Rules: Domain/Core is pure, Ports are interfaces, Adapters connect to external systems
- 依赖规则:核心无任何依赖,适配器依赖端口
- 层规则:Domain/Core层为纯逻辑,端口为接口,适配器负责连接外部系统
Layered Architecture
分层架构
- Dependency Rule: Each layer depends only on layer below
- Layer Rules: Presentation → Business Logic → Data Access
- 依赖规则:每一层仅依赖下一层
- 层规则:Presentation层 → 业务逻辑层 → 数据访问层
MVC Architecture
MVC架构
- Dependency Rule: Model is independent, View/Controller depend on Model
- Layer Rules: Model is independent, View depends on Model, Controller orchestrates
See Code Examples for detailed dependency diagrams, layer rules, and detection patterns for each architectural pattern.
- 依赖规则:Model独立,View/Controller依赖Model
- 层规则:Model独立,View依赖Model,Controller负责编排
查看代码示例了解每种架构模式的详细依赖图、层规则和识别模式。
Common Anti-Patterns Detected
可检测的常见反模式
This skill detects and provides fixes for common architectural violations:
- Domain Importing Infrastructure - Domain layer importing database/framework code
- Application Importing Interfaces - Application layer importing from API/UI layers
- Circular Dependencies - Two or more modules importing each other
- Business Logic in Interface Layer - Business rules and validation in API/UI code
See Code Examples for detailed violation examples with complete before/after code showing how to fix each anti-pattern using dependency inversion, repository patterns, and proper layering.
本工具可检测常见架构违规问题并提供修复方案:
- Domain层导入Infrastructure层 - Domain层导入数据库/框架代码
- Application层导入Interface层 - Application层从API/UI层导入内容
- 循环依赖 - 两个或多个模块互相导入
- 接口层存在业务逻辑 - API/UI代码中包含业务规则和校验逻辑
查看代码示例了解详细的违规示例,包含完整的代码修改前后对比,展示如何通过依赖倒置、仓储模式和合理分层修复每个反模式。
Integration Points
集成方案
This skill integrates with:
- Pre-Commit Hooks - Block commits with architecture violations
- CI/CD Pipeline - Automated validation in GitHub Actions, GitLab CI
- Quality Gates - Part of comprehensive quality checks
See Code Examples for complete integration scripts for pre-commit hooks, CI/CD workflows, and quality gate configurations.
本工具可与以下系统集成:
- Pre-commit钩子 - 阻止存在架构违规的提交
- CI/CD流水线 - 可在GitHub Actions、GitLab CI中自动执行验证
- 质量门禁 - 作为全量质量检查的一部分
查看代码示例获取pre-commit钩子、CI/CD工作流和质量门禁配置的完整集成脚本。
Supporting Files
配套文件
- references/reference.md - Complete layer dependency matrices for all patterns
- references/code-examples.md - All code examples, detection patterns, and integration scripts
- references/diff-aware-validation.md - Diff-aware validation comprehensive guide
- references/diff-aware-validation-summary.md - Diff-aware validation summary
- references/diff-aware-validation-checklist.md - Pre-commit validation checklist
- references/diff-aware-validation-quickref.md - Quick reference for diff-aware validation
- references/installation.md - Installation and integration guide
- scripts/validate.py - Standalone validation script for all architectural patterns
- templates/arch-rules.yaml - Customizable rule definitions
- references/reference.md - 所有模式的完整层依赖矩阵
- references/code-examples.md - 所有代码示例、识别模式和集成脚本
- references/diff-aware-validation.md - 差异感知验证完整指南
- references/diff-aware-validation-summary.md - 差异感知验证摘要
- references/diff-aware-validation-checklist.md - Pre-commit验证检查清单
- references/diff-aware-validation-quickref.md - 差异感知验证快速参考
- references/installation.md - 安装和集成指南
- scripts/validate.py - 支持所有架构模式的独立验证脚本
- templates/arch-rules.yaml - 可自定义的规则定义
Expected Outcomes
预期输出
Success (No Violations)
验证通过(无违规)
When validation passes, you'll see confirmation that all layer boundaries are respected and dependencies flow correctly.
验证通过时,你会收到确认信息,表明所有层边界都已遵守,依赖流向正确。
Failure (Violations Found)
验证失败(发现违规)
When violations are detected, you'll receive a detailed report including:
- Violation severity (CRITICAL, HIGH, MEDIUM, LOW)
- File path and line number
- Specific import statement causing the violation
- Recommended fix with explanation
- Impact assessment
See Code Examples for complete example output showing success and failure reports with detailed violation listings.
检测到违规时,你会收到详细报告,包含:
- 违规严重程度(CRITICAL、HIGH、MEDIUM、LOW)
- 文件路径和行号
- 导致违规的具体导入语句
- 附带解释的推荐修复方案
- 影响评估
查看代码示例获取完整的输出示例,包含成功和失败报告,以及详细的违规列表。
Success Metrics
效果指标
After invoking this skill, measure:
- Violation Detection Rate: 95%+ (catches all major violations)
- False Positive Rate: <5% (minimal incorrect reports)
- Context Reduction: 90%+ vs manual agent review
- Execution Time: <2s for typical codebase
- Actionability: 100% of violations include specific fix
调用本工具后,可衡量以下指标:
- 违规检测率:95%+(可捕获所有重大违规)
- 误报率:<5%(错误报告极少)
- 上下文精简率:相比人工Agent评审提升90%+
- 执行时间:常规代码库<2秒
- 可落地性:100%的违规都附带具体修复方案
Customization
自定义配置
Define Custom Rules
定义自定义规则
Create in project root to customize layer paths, import rules, severity levels, and exceptions for your project's specific architectural needs.
arch-rules.yaml在项目根目录创建,可自定义层路径、导入规则、严重级别和例外情况,满足项目的专属架构需求。
arch-rules.yamlLanguage Support
语言支持
Currently supports Python (), JavaScript (), and TypeScript (). Extend by adding patterns in .
.py.js.tsscripts/validate.pySee templates/arch-rules.yaml for a complete example configuration with all available options and Code Examples for YAML configuration samples.
当前支持Python()、JavaScript()和TypeScript()。可在中添加模式扩展支持其他语言。
.py.js.tsscripts/validate.py查看templates/arch-rules.yaml获取包含所有可用选项的完整配置示例,查看代码示例获取YAML配置样例。
Troubleshooting
问题排查
Issue: False Positives
问题:误报
Valid imports flagged as violations. Add exceptions to .
arch-rules.yaml合法导入被标记为违规。可在中添加例外规则。
arch-rules.yamlIssue: Pattern Not Detected
问题:未识别架构模式
Skill doesn't recognize architectural pattern. Ensure contains pattern keywords: "Clean Architecture", "Hexagonal Architecture", "Layered Architecture", or "MVC".
ARCHITECTURE.md工具无法识别架构模式。请确保包含模式关键词:"Clean Architecture"、"Hexagonal Architecture"、"Layered Architecture"或"MVC"。
ARCHITECTURE.mdIssue: Missing Violations
问题:遗漏违规
Known violations not reported. Check file paths match layer definitions. Update layer patterns if needed.
See Code Examples for complete troubleshooting examples with exception configurations and pattern detection fixes.
已知违规未被报告。请检查文件路径是否匹配层定义,必要时更新层匹配模式。
查看代码示例获取完整的问题排查示例,包含例外配置和模式识别修复方案。
Expected Benefits
预期收益
| Metric | Without Validation | With Validation | Improvement |
|---|---|---|---|
| Architecture violations | 15-20 per quarter | 0-2 per quarter | 95% reduction |
| Time to detect violations | 2-5 days | 5-10 seconds | 99.9% faster |
| Refactoring cost | High (violations embedded) | Low (caught early) | 80% reduction |
| Code review time | 45-60 min | 15-20 min | 70% faster |
| Onboarding time | 2-3 weeks | 3-5 days | 75% faster |
| Technical debt | 20-30 violations/year | 2-5 violations/year | 90% reduction |
| 指标 | 无验证时 | 使用验证后 | 提升幅度 |
|---|---|---|---|
| 架构违规数 | 每季度15-20个 | 每季度0-2个 | 减少95% |
| 违规检测耗时 | 2-5天 | 5-10秒 | 速度提升99.9% |
| 重构成本 | 高(违规已嵌入代码) | 低(提前发现) | 减少80% |
| 代码评审耗时 | 45-60分钟 | 15-20分钟 | 速度提升70% |
| 新人上手耗时 | 2-3周 | 3-5天 | 速度提升75% |
| 技术债务 | 每年20-30个违规 | 每年2-5个违规 | 减少90% |
Requirements
依赖要求
- Python 3.10+ (for validation script)
- Source code in supported language (Python/JS/TS)
- or similar documentation defining layers
ARCHITECTURE.md - Read, Grep, Bash, Glob tools available
- Python 3.10+(运行验证脚本需要)
- 源码为支持的语言(Python/JS/TS)
- 存在或同类定义层的文档
ARCHITECTURE.md - 环境有Read、Grep、Bash、Glob工具可用
Red Flags to Avoid
需避免的风险点
Architecture Violations
架构违规
- Domain importing Infrastructure - Breaks dependency inversion
- Application importing Interface - Creates circular dependencies
- Infrastructure importing concrete Domain - Should use Protocols
- Business logic in Interface layer - Violates layer separation
- Domain层导入Infrastructure层 - 破坏依赖倒置原则
- Application层导入Interface层 - 产生循环依赖
- Infrastructure层导入具体Domain实现 - 应使用Protocols
- 接口层存在业务逻辑 - 违反分层隔离原则
Detection Anti-Patterns
检测反模式
- Ignoring False Positives - Add exceptions to arch-rules.yaml
- Skipping validation before major changes - Always validate first
- Not updating layer definitions - Keep ARCHITECTURE.md current
- Assuming pattern without verification - Read ARCHITECTURE.md first
- 忽略误报 - 请在arch-rules.yaml中添加例外规则
- 重大变更前跳过验证 - 请务必先执行验证
- 不更新层定义 - 请保持ARCHITECTURE.md同步更新
- 未验证就假设架构模式 - 请先读取ARCHITECTURE.md
Process Mistakes
流程错误
- Proceeding with violations - Fix before committing
- Not documenting exceptions - Explain why deviation needed
- Skipping after refactor - Validation most critical after structural changes
- Manual validation only - Automate in pre-commit hooks and CI
- 存在违规仍继续操作 - 提交前请先修复
- 不记录例外情况 - 请说明偏离规则的原因
- 重构后跳过验证 - 结构变更后验证最为重要
- 仅依赖人工验证 - 请在pre-commit钩子和CI中自动化执行
Utility Scripts
工具脚本
- Validate Architecture Script - Full-featured architecture validation tool supporting Clean, Hexagonal, Layered, and MVC patterns
- 架构验证脚本 - 功能完整的架构验证工具,支持Clean、Hexagonal、分层和MVC模式
See Also
相关参考
- validate-layer-boundaries - Project-specific validation for project-watch-mcp
- @architecture-guardian - Agent for architectural guidance and review
- ARCHITECTURE.md - Project's architecture documentation
- ADR-001 - Architecture Decision Record for Clean Architecture
- validate-layer-boundaries - project-watch-mcp的项目专属验证工具
- @architecture-guardian - 架构指导和评审Agent
- ARCHITECTURE.md - 项目架构文档
- ADR-001 - Clean Architecture的架构决策记录