📄 Domain 3 · Task Statement 3.3

Apply Path-Specific Rules for Conditional Convention Loading

📊 Domain Weight: 20% 🎯 Difficulty: Advanced Concept 🔗 Scenarios: Monorepos & Legacy Migrations

Claude Code isn't just aware of your project root; it's sensitive to where you are within it. Path-specific rules allow you to enforce different coding standards, testing patterns, and architectural constraints based on the current active directory, enabling seamless transitions between diverse tech stacks in a single repository.

📋 Contents

  1. Real-World Analogy: The Multi-Zone Security System
  2. Core Mechanics: How Path-Scoped Context Works
  3. Pattern: The Monorepo Handoff
  4. Conflict Resolution: Parent vs. Subdirectory
  5. Diagram: Context Resolution Path
  6. Code Examples: Backend vs. Frontend Scoping
  7. Conditional Patterns (Tech Stack, Performance, Safety)
  8. Anti-Patterns to Avoid
  9. Exam Readiness & Key Takeaways

🏭 Real-World Analogy: The Multi-Zone Security System

🩹 Analogy — Airport Security Zones

Imagine walking through a modern international airport. There is a "Global Protocol" for the whole airport (no smoking, be polite). However, as you move between zones, the specific rules change instantly:

  • Check-in Zone: Produce your ID and ticket.
  • Security Screening: Remove your shoes, take out your laptop.
  • Duty-Free: Show your boarding pass for tax-free purchases.
  • Boarding Gate: Present your passport and wait for your group number.

You don't take your shoes off in the Food Court, and you don't show your boarding pass at the Information Desk. The path you take through the airport triggers different conditional rules. CLAUDE.md files do the same for Claude: moving into the /frontend directory triggers React rules, while entering /infra triggers Terraform rules.

Core Mechanics: How Path-Scoped Context Works

Path-specific rules are implemented primarily through Subdirectory CLAUDE.md files. When Claude Code opens or analyzes a file, it builds its "active instruction set" by traversing the file path from the file's location back up to the project root (and finally the global config).

The Resolution Logic

  1. Start at the target file: (e.g., apps/api/src/handlers/user.py)
  2. Look for CLAUDE.md in current dir: (.../handlers/)
  3. Look for CLAUDE.md in all parent dirs: (.../src/, .../api/, .../apps/)
  4. Stop at Project Root CLAUDE.md.
  5. Merge with Global CLAUDE.md: (~/.claude/CLAUDE.md)
💡 Efficiency — Claude switches context only when needed

Claude only re-evaluates this chain when the active task or current file set changes. This ensures that if you are working on a Python backend file, Claude isn't distracted by your React frontend's Tailwind conventions, even if they're in the same repository.

📂 Pattern: The Monorepo Handoff

In large monorepos, keeping all rules in a single root CLAUDE.md is a recipe for context pollution. Path-specific rules solve this by delegating domain-specific expertise to the directories that actually use it.

Project PathPrimary PurposeScoped Instructions
/frontend/ Client-side React app Vite commands, styling guidelines (Tailwind), component structure.
/backend/ Python/FastAPI server Alembic migrations, SQLAlchemy patterns, Pydantic validation rules.
/shared/ Common JSON schemas Versioning protocols, breaking change policies, export formats.
/e2e/ Playwright tests Cypress/Playwright run commands, CI environment setup, mock data strategy.

Conflict Resolution: Parent vs. Subdirectory

Standard inheritance applies: The most local rule wins. This allows teams to set "Broad Defaults" at the root while allowing "Local Exceptions" for specialized modules.

Conflict Example — Root vs. Subdirectory
# /CLAUDE.md (Root)
- Naming: Use kebab-case for all file names.
- Testing: All tests must run with `npm test`.

# /services/legacy-auth/CLAUDE.md (Subdirectory)
- Naming: Override root — use PascalCase for files to match legacy Java naming.
- Testing: Override root — tests in this folder require `mvn test`, not npm.
🎯 Exam Focus — Selective Overriding

On the exam, watch for questions about partial overrides. A subdirectory CLAUDE.md does NOT erase the root file. It only overrides the specific bullet points it targets. If the root says "Don't use wildcard imports" and the subdirectory doesn't mention it, the "Don't use wildcard imports" rule still effectively applies in the subdirectory.

🕐 Diagram: Context Resolution Path

Figure 1 — How Claude Resolves Convention Priority
Global (~/.claude/) Base Layer - Personal Preferences Project Root (/) Corporate/Shared Standards /frontend/ CLAUDE.md React, TypeScript, CSS Highest Priority for UI files /backend/ CLAUDE.md Python, SQL, API Docs Highest Priority for API files

💻 Code Examples: Backend vs. Frontend Scoping

Markdown — frontend/CLAUDE.md
# Frontend Guidelines

## Technology Stack
- React 18, Tailwind CSS, TanStack Query.
- Component structure: Functional components with hooks only.

## Path-Specific Rules
- All components in `src/components/common` must be atomic and stateless.
- Components in `src/pages` handle layout and data fetching.
- Use `shadcn/ui` where possible.

## Testing Commands
- Run unit tests: `npm run test:ui`
- Run linting: `npm run lint:frontend`
Markdown — backend/CLAUDE.md
# Backend Guidelines

## Technology Stack
- Python 3.11+, FastAPI, PostgreSQL.
- ORM: SQLAlchemy (Async mode always).

## Path-Specific Rules
- API endpoints in `app/routers/` must have Pydantic response models.
- DB models in `app/models/` must inherit from `Base` in `database.py`.
- No raw SQL except in `app/migrations/`.

## Testing Commands
- Run tests: `pytest -v app/tests`
- Refresh DB mocks: `python scripts/seed_test_db.py`

🎯 Conditional Patterns

Beyond tech stacks, path-specific rules are often used for security, performance, or "mode" switching:

⚠ Warning — Complexity Overhead

Don't create a subdirectory CLAUDE.md for every folder. Use them only when the behavioral guidance needs to change. Over-fragmenting rules makes it harder for you to keep track of what instructions Claude is actually following.

Anti-Patterns to Avoid

⛔ Ghost Instructions

Deleting a folder but leaving its CLAUDE.md in a parent directory. Claude will still try to apply those rules to things "near" it, causing confusion.

⛔ "The Wall of rules"

Duplicating 100% of the root rules in a subdirectory file. If you update the root, the subdirectory stays stuck in the past. Only put delta (changed) rules in subdirs.

⛔ Scope Creep

Putting "Always use 2 spaces" in the /backend folder when the root already says it. It adds noise and consumes extra context tokens for no benefit.

Exam Readiness & Key Takeaways

🎯 Exam Focus — Path-Specific Scenario

Scenario: You have a monorepo with an /api (Java) and /web (React) directory. You want Claude to use Maven for the API and NPM for the web. Where do the commands go?

Answer: Create /api/CLAUDE.md with Maven commands and /web/CLAUDE.md with NPM commands. Do NOT put them in the root, as Claude would have to "guess" which command to use based on file extension alone, which is less reliable than path-based scoping.

1
Distance-Based Priority: Claude prioritizes the CLAUDE.md file closest to the active file.
2
Additive Inheritance: Subdirectory rules merge with parent rules; they don't replace the entire file.
3
Tech Stack Switching: Use path-scoping to switch between backend (Python/Go) and frontend (React/Vue) conventions automatically.
4
Safety Scoping: Use local rules to impose stricter safety or security constraints in sensitive directories (e.g., `/infra`, `/secrets`).
5
CLI Command Localizing: Map appropriate build/test tools (make, npm, pytest, mvn) to the directories where they are valid.