AgentSkillsCN

arch

架构发现、验证与规划。当用户输入 /arch、/arch configure、/arch learn、/arch check 或 /arch plan 时,可使用此技能。

SKILL.md
--- frontmatter
name: arch
description: Architecture discovery, validation, and planning. Use when user says /arch, /arch configure, /arch learn, /arch check, or /arch plan.
user-invocable: true
allowed-tools:
  - Read
  - Bash
  - Glob
  - Grep

Architecture Skill

Purpose

Discover, validate, and guide project architecture. Ensures code follows layer boundaries and helps plan where new code should go.

Quick Reference

  • Validates: Layer boundaries, import directions, architectural compliance
  • Modes: configure, learn, check, plan

Modes Overview

ModeCommandPurpose
configure/arch configureAuto-discover from docs/configs (setup)
learn/arch learnInfer from code analysis or description
check/arch checkValidate layer boundaries
plan/arch plan <desc>Suggest where new code belongs

Configure Mode: /arch configure (Setup)

Auto-discovers your project's architecture based on the selected style.

Run during plugin installation or manually:

bash
/arch configure

Discovery Flow:

  1. User selects architecture style:

    • hexagonal, layered, clean, mvc, custom
  2. Skill searches for architecture documentation:

    • CLAUDE.md (Architecture section)
    • README.md (Project Structure section)
    • docs/architecture.md
    • docs/ADR/*.md (Architecture Decision Records)
  3. Skill checks config files:

    • tsconfig.json (path aliases → layers)
    • .eslintrc (import restriction rules)
    • .dependency-cruiser.js
    • package.json (workspaces)
  4. Skill scans directory structure:

    • src/, lib/, app/, packages/
    • Matches directories to style template
  5. Presents discovered config for confirmation:

code
## Discovered Architecture

**Style:** hexagonal
**Source Root:** src/
**Confidence:** High (documented in CLAUDE.md)

### Layers (lowest → highest)

| Level | Name | Path | Responsibility |
|-------|------|------|----------------|
| 0 | config | src/config/ | Configuration, schemas |
| 1 | domain | src/domain/ | Core business logic |
| 2 | infrastructure | src/infrastructure/ | External integrations |
| 3 | application | src/application/ | Use cases, orchestration |

### Sources Used
- CLAUDE.md (Architecture section)
- tsconfig.json (path aliases)
- Directory scan (src/)

**Is this correct?** [Confirm / Adjust]

Learn Mode: /arch learn

Learn architecture from code analysis or a provided description.

Options:

bash
# Analyze actual import patterns (default)
/arch learn --analyze

# Parse a natural language description
/arch learn --from "We use hexagonal architecture with domain at center..."

# Learn from labeled examples
/arch learn --example "src/domain/user.py=domain" --example "src/api/routes.py=application"

What --analyze does:

  1. Builds import graph - Scans all source files, extracts imports
  2. Identifies clusters - Groups by directory, calculates import metrics
  3. Infers layer levels - Uses heuristics based on import direction
  4. Detects violations - Finds cycles and backward imports
  5. Compares to docs - Generates drift report if architecture already configured

Output:

code
## Inferred Architecture

**Method:** Import analysis
**Confidence:** Medium (no docs, inferred from code)

### Detected Layers

| Level | Directory | Inbound | Outbound | Role |
|-------|-----------|---------|----------|------|
| 0 | src/domain/ | 47 | 3 | Core (most depended on) |
| 1 | src/infrastructure/ | 23 | 15 | Middle (adapters) |
| 2 | src/application/ | 5 | 38 | Orchestrator |

### Import Graph Summary

domain ← infrastructure ← application ↖________________↙

code

### Cycles Detected: 2
1. src/infra/cache.py ↔ src/infra/db.py
2. src/app/service.py → src/domain/user.py → src/app/utils.py

**Save this configuration?** [Confirm / Adjust]

Layer inference heuristics:

  • Lower layers (L0, L1): High inbound imports, low outbound (many depend on them)
  • Higher layers (L2, L3): Low inbound imports, high outbound (orchestrators)
  • Formula: layer_level ∝ outbound_imports / inbound_imports

Check Mode: /arch check

Validate that code follows architecture layer boundaries.

bash
/arch check

What it checks:

  • Import violations (inner layers importing outer)
  • Circular dependencies
  • Layer bypass (skipping intermediate layers)

Output:

code
## Architecture Check

Status: FAIL

### Violations Found (2)

1. src/domain/user.ts:5
   Import: from '../infrastructure/database'
   Rule: Domain cannot import Infrastructure
   Fix: Use dependency injection or move to Application layer

2. src/business/order.ts:12
   Import: from '../presentation/api'
   Rule: Business cannot import Presentation
   Fix: Invert dependency with interface

Plan Mode: /arch plan <description>

Get guidance on where new code should go.

bash
/arch plan Add user authentication with JWT tokens

Output:

code
## Architecture Plan: User Authentication

Based on your hexagonal architecture:

### Layer Distribution

| Layer | What Goes Here | Files |
|-------|----------------|-------|
| Domain | User entity, AuthToken value object | src/domain/user/, src/domain/auth/ |
| Infrastructure | JWTProvider adapter | src/infrastructure/jwt/ |
| Application | AuthenticationService use case | src/application/auth/ |

### Reasoning

- **Domain**: User identity and tokens are core business concepts
- **Infrastructure**: JWT is an external technology detail
- **Application**: Authentication orchestrates domain + infra

### Implementation Order

1. Domain first (no dependencies)
2. Infrastructure (depends on domain types)
3. Application (orchestrates both)

Discovery Sources

The skill configures architecture from these sources (in priority order):

Documentation Files

FileWhat's Extracted
CLAUDE.mdArchitecture section, layer descriptions
README.mdProject Structure section
docs/architecture.mdFull architecture documentation
docs/ADR/*.mdArchitecture Decision Records

Config Files (Language-Specific)

The skill auto-detects project language and checks relevant configs:

TypeScript/JavaScript:

FileWhat's Extracted
tsconfig.jsonPath aliases (often mirror layers)
.eslintrc*import/no-restricted-paths rules
.dependency-cruiser.jsForbidden dependency rules
package.jsonWorkspaces (monorepo structure)

Python:

FileWhat's Extracted
pyproject.tomlPackage structure, import-linter config
.importlinterLayer contracts and forbidden imports
setup.cfgpackages and package_dir
mypy.iniPer-module type settings

Go:

FileWhat's Extracted
go.modModule name reveals structure
go.workWorkspace members (monorepo)
.golangci.ymldepguard rules for import restrictions
internal/Go convention for private packages

Rust:

FileWhat's Extracted
Cargo.tomlWorkspace members
.cargo/config.tomlAlias paths

Java/Kotlin:

FileWhat's Extracted
build.gradleSubprojects/modules
pom.xmlMulti-module structure
archunit.propertiesArchUnit layer rules

Generic (all languages):

FileWhat's Extracted
MakefileTargets often mirror layers
docker-compose.ymlServices may represent boundaries
justfileRecipes may mirror structure

Directory Scan (Fallback)

If documentation doesn't fully define layers:

  • Scans common source roots: src/, lib/, app/, packages/
  • Go projects: also scans pkg/, internal/, cmd/
  • Matches directories against style template
  • Assigns default responsibilities based on naming conventions

Architecture Styles

StyleDescriptionTypical Layers
hexagonalPorts & Adaptersconfig → domain → infrastructure → application
layeredTraditional layersdata → business → presentation
cleanClean Architectureentities → usecases → adapters → frameworks
mvcModel-View-Controllermodels → controllers/views
customUser-definedUser configures

Confidence Levels

LevelMeaningSources
HighArchitecture explicitly documentedCLAUDE.md, README.md, docs/architecture.md
MediumInferred from configtsconfig.json paths, eslint rules
LowOnly directory structureFallback scan of src/

Integration with Developer Skill

The developer skill uses /arch plan before implementation:

code
1. User: "Add user authentication"
2. Developer calls: /arch plan Add user authentication
3. Arch returns: layer assignments + reasoning
4. Developer routes to appropriate patterns

Anti-Patterns Detected

PatternProblemFix
Domain imports InfrastructureViolates dependency inversionUse DI or move code
Circular importsRuntime issues, tight couplingExtract to lower layer
Layer bypassHidden dependenciesRoute through proper layers
God moduleToo many responsibilitiesSplit into focused modules

Configuration

Config Location

Config path depends on how the plugin was installed:

Plugin ScopeConfig FileGit
project.claude/skills/arch.yamlCommitted (shared)
local.claude/skills/arch.local.yamlIgnored (personal)
user.claude/skills/arch.local.yamlIgnored (personal)

Precedence when reading (first found wins):

  1. .claude/skills/arch.local.yaml
  2. .claude/skills/arch.yaml
  3. Skill defaults

Config Schema

After discovery, saved to the config file:

yaml
ARCHITECTURE_STYLE: hexagonal
SOURCE_ROOT: src/
LAYERS:
  - name: config
    level: 0
    path: src/config/
    responsibility: Configuration, schemas
    can_import: [external]
  - name: domain
    level: 1
    path: src/domain/
    responsibility: Core business logic
    can_import: [config, external]
  - name: infrastructure
    level: 2
    path: src/infrastructure/
    responsibility: External integrations
    can_import: [config, domain, external]
  - name: application
    level: 3
    path: src/application/
    responsibility: Use cases, orchestration
    can_import: [config, domain, infrastructure, external]