AgentSkillsCN

phx:boundaries

构建递归调用树,追溯函数从入口点出发的调用路径。适用于调试“这个值究竟来自何处?”的问题,或规划函数签名变更时使用。

SKILL.md
--- frontmatter
name: phx:boundaries
description: Validate Phoenix context boundaries and dependencies using mix xref. Use when reviewing PRs or before major refactors.
argument-hint: [--assess|--fix]

Phoenix Context Boundary Validation

Analyze module dependencies to ensure clean context separation and proper architectural boundaries.

Usage

code
/phx:boundaries              # Check for violations
/phx:boundaries --assess     # Score context health (0-100)
/phx:boundaries --fix        # Suggest fixes for violations

--assess Mode: Context Health Score

Evaluate overall boundary health with a quantified score.

Metrics Calculated

MetricHealthy RangeRed FlagWeight
Modules per context3-15>20 or <220%
Public API surface5-30 funcs>40 funcs15%
Fan-out (contexts called)1-4>620%
Fan-in (called by contexts)1-6>1015%
Circular dependencies0>015%
Boundary violations0>015%

Commands for Assessment

bash
# Module count per context
for dir in lib/my_app/*/; do
  echo "$(basename $dir): $(find $dir -name '*.ex' | wc -l) modules"
done

# Public function count per context
for f in lib/my_app/*.ex; do
  echo "$(basename $f): $(grep -c '^  def ' $f 2>/dev/null || echo 0) public funcs"
done

# Dependency analysis
mix xref graph --format stats

# Circular dependencies
mix xref graph --format cycles

Output Format

markdown
## Context Health Assessment

### Overall Score: 82/100 (Good)

| Context | Modules | API | Fan-Out | Fan-In | Score |
|---------|---------|-----|---------|--------|-------|
| Accounts | 5 | 12 | 2 | 4 | 95 |
| Orders | 18 | 45 | 8 | 3 | 62 |
| Shared | 2 | 8 | 0 | 12 | 78 |

### Issues Found

1. **Orders** - Too large (18 modules, 45 funcs)
   - Consider: Extract Fulfillment, Invoicing sub-contexts

2. **Orders** - High fan-out (8 contexts)
   - Consider: Review if all dependencies necessary

### Recommendations

- Split Orders into Orders + Fulfillment
- Review Accounts ← Billing dependency

Iron Laws - Never Violate These

  1. Controllers call only contexts - No direct Repo access from web layer
  2. Schemas are pure data - No side effects, no Repo calls in schema modules
  3. Contexts own their schemas - Don't import schemas from other contexts
  4. Explicit dependencies only - Cross-context calls must be intentional

Dependency Rules

LayerCan CallCannot Call
ControllersContexts, Plug, ConnRepo, Schemas directly
LiveViewsContexts, Components, PubSubRepo, Schemas directly
ContextsOwn schemas, Repo, other contextsWeb layer modules
SchemasEcto types, validationsContexts, Repo

Analysis Commands

Check Compile Dependencies

bash
mix xref graph --label compile-connected

Find What Depends on a Context

bash
mix xref graph --sink MyApp.Accounts --label compile

Find What a Module Calls

bash
mix xref callers MyApp.Accounts.get_user!/1

Check for Circular Dependencies

bash
mix xref graph --format cycles

Red Flags to Detect

Direct Repo Access from Web Layer

bash
grep -r "Repo\." lib/my_app_web/ --include="*.ex"

If found: Move to context module.

Schema with Queries

bash
grep -r "import Ecto.Query" lib/my_app/**/schemas/ --include="*.ex"

If found: Move queries to context.

Cross-Context Schema Import

bash
grep -r "alias MyApp.OtherContext.Schema" lib/my_app/my_context/

If found: Call other context's API instead.

Business Logic in LiveView

bash
grep -r "Repo\.\|Ecto\.Multi" lib/my_app_web/live/ --include="*.ex"

If found: Extract to context function.

Boundary Verification Process

  1. Run mix xref graph --label compile-connected for overview
  2. Check for context cross-contamination
  3. Verify no direct Repo calls from web layer
  4. Ensure schemas have no side effects
  5. Validate explicit cross-context dependencies

References

For detailed patterns, see:

  • references/context-design.md - Context design principles
  • references/refactoring-boundaries.md - Fixing boundary violations