validate-architecture -- Architecture Layer Violation Detection
Corresponding rule: architecture.md
Purpose
Statically detect inter-layer import rule violations and circular references in a project. If CLAUDE.md defines an architecture, use that layer definition; otherwise default to the general 3-layer model (presentation/business/data access).
Input -- Dynamic Target Resolution
Resolve target directories in this order:
- CLAUDE.md "Project Structure" section -- extract source code directories
- If CLAUDE.md is missing or has no structure info: Glob source files (
**/*.{ts,tsx,js,jsx,py,java,go,rs}) -- use the top-level directory containing matched files
- If no source files are found: report status as SHALLOW and state the reason
Default exclusions: node_modules/, .git/, dist/, build/, coverage/
Skill-specific exclusions: *.test.*, *.spec.*, test_*, *_test.*
Mandatory Output: Target Resolution Result
Output the following before proceeding with validation. Do not advance until resolution is complete.
| Step |
Result |
| CLAUDE.md check |
Found/Not found |
| Resolved directories |
{list or "none"} |
| Source file count |
{count} |
| Resolution method |
CLAUDE.md / Glob fallback / SHALLOW |
Execution Logic
Read the architecture definition from CLAUDE.md
- Use
Read to read the project root CLAUDE.md
- If an Architecture section exists, use its layer structure and dependency direction as the baseline
- If no Architecture section exists, default to the general 3-layer model:
- Presentation (presentation, ui, pages, components, views, routes, handlers)
- Business (domain, core, application, services, use-cases, usecases)
- Data access (infrastructure, infra, repositories, external, adapters, gateways)
- Dependency direction: presentation -> business <- data access. The business layer must not depend on other layers.
Collect source files
- Use
Glob to collect files matching src/**/*.{ts,tsx,js,jsx,py,java}
- Exclude test files (
*.test.*, *.spec.*, test_*, *_test.*)
Collect import/from statements
- Use
Grep to extract import statements from each file
- Patterns:
import .* from, from .* import, require(, @import
Detect layer violation patterns
- domain/core -> infrastructure/infra imports (business depends on data access)
- domain/core -> presentation/ui/pages/components imports (business depends on presentation)
- presentation -> direct DB imports: the presentation layer directly imports DB-related packages (
sqlalchemy, prisma, typeorm, drizzle, mongoose, sequelize, knex, pg, mysql, sqlite, etc.)
Detect circular references
- Detect patterns where module A imports module B while module B also imports module A
- Check bidirectional imports at the file level
Mandatory Output: Architecture Validation Matrix
Output the matrix below before generating the final report. Do not proceed until every check has been performed.
| Check Item |
Status |
Items Checked |
Violations |
Severity |
Evidence |
| Read CLAUDE.md architecture |
? |
? |
-- |
-- |
{Read result} |
| Source file collection |
? |
? |
-- |
-- |
{Glob pattern, file count} |
| domain -> infra import |
? |
? |
? |
ERROR |
{Grep pattern, searched files} |
| domain -> presentation import |
? |
? |
? |
ERROR |
{Grep pattern, searched files} |
| presentation -> DB import |
? |
? |
? |
ERROR |
{Grep pattern, searched files} |
| Circular references |
? |
? |
? |
WARN |
{Grep pattern, searched files} |
Status values: PASS (verification complete, no issues), NOT_APPLICABLE (no source files), SKIPPED (plugin issue), SHALLOW (target resolution failed)
Pre-Output Checklist (Mandatory Before Final Output)
Check every item before writing the report. If any item is unchecked, go back and complete it.
Schema Compliance Check (Mandatory Before Saving to .ww-w-ai/)
Before writing to .ww-w-ai/devtools/validate-architecture/, verify the JSON output:
Output
Generate the validation report in the user's conversation language.
Output violations as a Markdown table in this format:
===== Architecture Validation Report =====
[Architecture basis]: CLAUDE.md definition / General 3-layer
| File | Violation | Severity |
|------|-----------|----------|
| `src/domain/user.ts` | Infrastructure layer import (`../../infra/db`) | ERROR |
| `src/core/order.ts` | Presentation layer import (`../../pages/OrderView`) | ERROR |
| `src/pages/Dashboard.tsx` | Direct DB import (`prisma`) | ERROR |
| `src/services/auth.ts` <-> `src/services/user.ts` | Circular reference | WARN |
Total violations: {N} (ERROR: {E}, WARN: {W})
====================================
When there are no violations:
===== Architecture Validation Report =====
[Architecture basis]: CLAUDE.md definition / General 3-layer
No violations.
====================================
Severity Criteria
| Severity |
Condition |
| ERROR |
Layer dependency-direction violation (domain -> infra, domain -> presentation, presentation -> DB) |
| WARN |
Circular reference, ambiguous layer placement (file outside any layer directory) |
Output Persistence
After generating the architecture validation report, save results to .ww-w-ai/devtools/validate-architecture/:
- Create
.ww-w-ai/devtools/validate-architecture/ if missing
- Write
latest.json -- structured result following templates/schema.json
- Write
latest.md -- human-readable report following templates/report.template.md
- Archive to
history/ -- copy latest.json to .ww-w-ai/devtools/validate-architecture/history/{timestamp}.json
latest.md is generated in the user's conversation language. JSON field names stay in English regardless of language.
The JSON output enables machine-parseable history tracking and cross-run comparison.
The history/ directory preserves previous executions for trend analysis.
Permission Rationale
- Write: Exclusively for .ww-w-ai/ output persistence. No project source modification.
- Bash: Exclusively for read-only git/system queries. No file modifications.
Notes
- Bash is permitted for directory creation (
mkdir -p .ww-w-ai/devtools/validate-architecture/history). The validation itself is read-only and does not modify project files.
- Test files (
*.test.*, *.spec.*) are excluded from inspection (per testing.md exceptions, internal access inside tests is permitted).
- If CLAUDE.md defines an architecture, that definition takes precedence over the general principles.
- Layer directory naming may differ per project, so CLAUDE.md definitions take highest precedence.
Spec Reference
For detailed validation criteria, evidence tables, and examples:
- Corresponding rule spec:
../../docs/specs/architecture.md
1---2name: validate-architecture3description: Detects inter-layer import rule violations (Clean Architecture)4---56# validate-architecture -- Architecture Layer Violation Detection78Corresponding rule: `architecture.md`910## Purpose1112Statically detect inter-layer import rule violations and circular references in a project. If CLAUDE.md defines an architecture, use that layer definition; otherwise default to the general 3-layer model (presentation/business/data access).1314## Input -- Dynamic Target Resolution1516Resolve target directories in this order:171. CLAUDE.md "Project Structure" section -- extract source code directories182. If CLAUDE.md is missing or has no structure info: Glob source files (`**/*.{ts,tsx,js,jsx,py,java,go,rs}`) -- use the top-level directory containing matched files193. If no source files are found: report status as SHALLOW and state the reason2021Default exclusions: node_modules/, .git/, dist/, build/, coverage/22Skill-specific exclusions: `*.test.*`, `*.spec.*`, `test_*`, `*_test.*`2324### Mandatory Output: Target Resolution Result2526Output the following before proceeding with validation. Do not advance until resolution is complete.2728| Step | Result |29|------|--------|30| CLAUDE.md check | Found/Not found |31| Resolved directories | {list or "none"} |32| Source file count | {count} |33| Resolution method | CLAUDE.md / Glob fallback / SHALLOW |3435## Execution Logic36371. **Read the architecture definition from CLAUDE.md**38 - Use `Read` to read the project root CLAUDE.md39 - If an Architecture section exists, use its layer structure and dependency direction as the baseline40 - If no Architecture section exists, default to the general 3-layer model:41 - Presentation (presentation, ui, pages, components, views, routes, handlers)42 - Business (domain, core, application, services, use-cases, usecases)43 - Data access (infrastructure, infra, repositories, external, adapters, gateways)44 - Dependency direction: presentation -> business <- data access. The business layer must not depend on other layers.45462. **Collect source files**47 - Use `Glob` to collect files matching `src/**/*.{ts,tsx,js,jsx,py,java}`48 - Exclude test files (`*.test.*`, `*.spec.*`, `test_*`, `*_test.*`)49503. **Collect import/from statements**51 - Use `Grep` to extract import statements from each file52 - Patterns: `import .* from`, `from .* import`, `require(`, `@import`53544. **Detect layer violation patterns**55 - domain/core -> infrastructure/infra imports (business depends on data access)56 - domain/core -> presentation/ui/pages/components imports (business depends on presentation)57 - presentation -> direct DB imports: the presentation layer directly imports DB-related packages (`sqlalchemy`, `prisma`, `typeorm`, `drizzle`, `mongoose`, `sequelize`, `knex`, `pg`, `mysql`, `sqlite`, etc.)58595. **Detect circular references**60 - Detect patterns where module A imports module B while module B also imports module A61 - Check bidirectional imports at the file level6263### Mandatory Output: Architecture Validation Matrix6465Output the matrix below before generating the final report. Do not proceed until every check has been performed.6667| Check Item | Status | Items Checked | Violations | Severity | Evidence |68|------------|:------:|:-------------:|:----------:|:--------:|----------|69| Read CLAUDE.md architecture | ? | ? | -- | -- | {Read result} |70| Source file collection | ? | ? | -- | -- | {Glob pattern, file count} |71| domain -> infra import | ? | ? | ? | ERROR | {Grep pattern, searched files} |72| domain -> presentation import | ? | ? | ? | ERROR | {Grep pattern, searched files} |73| presentation -> DB import | ? | ? | ? | ERROR | {Grep pattern, searched files} |74| Circular references | ? | ? | ? | WARN | {Grep pattern, searched files} |7576Status values: PASS (verification complete, no issues), NOT_APPLICABLE (no source files), SKIPPED (plugin issue), SHALLOW (target resolution failed)7778### Pre-Output Checklist (Mandatory Before Final Output)7980Check every item before writing the report. If any item is unchecked, go back and complete it.8182- [ ] Every check item has a Status value (no empty Status cells)83- [ ] Every check with Status != SKIPPED has an Evidence value84- [ ] Architecture basis (CLAUDE.md or general 3-layer) is identified and stated85- [ ] All 4 violation-pattern checks performed86- [ ] Circular-reference check performed87- [ ] Report language matches the user's conversation language8889### Schema Compliance Check (Mandatory Before Saving to .ww-w-ai/)9091Before writing to .ww-w-ai/devtools/validate-architecture/, verify the JSON output:92- [ ] Every "required" field in schema.json is present and non-empty93- [ ] The findings[] array contains every detected violation94- [ ] summary counts match the actual finding count9596## Output9798Generate the validation report in the user's conversation language.99100Output violations as a Markdown table in this format:101102```103===== Architecture Validation Report =====104105[Architecture basis]: CLAUDE.md definition / General 3-layer106107| File | Violation | Severity |108|------|-----------|----------|109| `src/domain/user.ts` | Infrastructure layer import (`../../infra/db`) | ERROR |110| `src/core/order.ts` | Presentation layer import (`../../pages/OrderView`) | ERROR |111| `src/pages/Dashboard.tsx` | Direct DB import (`prisma`) | ERROR |112| `src/services/auth.ts` <-> `src/services/user.ts` | Circular reference | WARN |113114Total violations: {N} (ERROR: {E}, WARN: {W})115====================================116```117118When there are no violations:119120```121===== Architecture Validation Report =====122123[Architecture basis]: CLAUDE.md definition / General 3-layer124125No violations.126====================================127```128129### Severity Criteria130131| Severity | Condition |132|:--------:|-----------|133| ERROR | Layer dependency-direction violation (domain -> infra, domain -> presentation, presentation -> DB) |134| WARN | Circular reference, ambiguous layer placement (file outside any layer directory) |135136## Output Persistence137138After generating the architecture validation report, save results to `.ww-w-ai/devtools/validate-architecture/`:1391401. Create `.ww-w-ai/devtools/validate-architecture/` if missing1412. Write `latest.json` -- structured result following `templates/schema.json`1423. Write `latest.md` -- human-readable report following `templates/report.template.md`1434. Archive to `history/` -- copy latest.json to `.ww-w-ai/devtools/validate-architecture/history/{timestamp}.json`144145`latest.md` is generated in the user's conversation language. JSON field names stay in English regardless of language.146The JSON output enables machine-parseable history tracking and cross-run comparison.147The history/ directory preserves previous executions for trend analysis.148149## Permission Rationale150151- **Write**: Exclusively for .ww-w-ai/ output persistence. No project source modification.152- **Bash**: Exclusively for read-only git/system queries. No file modifications.153154## Notes155156- Bash is permitted for directory creation (`mkdir -p .ww-w-ai/devtools/validate-architecture/history`). The validation itself is read-only and does not modify project files.157- Test files (`*.test.*`, `*.spec.*`) are excluded from inspection (per testing.md exceptions, internal access inside tests is permitted).158- If CLAUDE.md defines an architecture, that definition takes precedence over the general principles.159- Layer directory naming may differ per project, so CLAUDE.md definitions take highest precedence.160161## Spec Reference162163For detailed validation criteria, evidence tables, and examples:164- Corresponding rule spec: `../../docs/specs/architecture.md`