$Defense-in-Depth Validation

Use when invalid data causes failures deep in execution, requiring validation at multiple system layers - validates at every layer data passes through to make bugs structurally impossible

Views:
Rating:
Tags
#validation#error-handling#architecture#reliability
Version
1.0.0
Category
Development
Source
path: skills/defense-in-depth
Install
clawd add obra/defense-in-depth

Overview

This skill prevents bugs caused by invalid data by implementing validation at every layer of your system. Rather than fixing a bug with a single validation point (which can be bypassed), this approach makes the bug structurally impossible.

The Philosophy

Single validation: "We fixed the bug" Multiple layers: "We made the bug impossible"

Different layers catch different cases:

  • Entry validation catches most bugs
  • Business logic validation catches edge cases
  • Environment guards prevent context-specific dangers
  • Debug instrumentation helps when other layers fail

The Four Layers

  1. Entry Point Validation - Reject obviously invalid input at API boundaries
  2. Business Logic Validation - Ensure data makes semantic sense for the operation
  3. Environment Guards - Prevent dangerous operations in specific contexts (tests, production, etc.)
  4. Debug Instrumentation - Capture diagnostic context through logging and stack traces

Real-World Example

A bug where empty projectDir caused unintended git init in source code:

  • Layer 1: Project.create() validates directory exists and is writable
  • Layer 2: WorkspaceManager validates projectDir is not empty
  • Layer 3: WorktreeManager refuses git init outside tmpdir in tests
  • Layer 4: Stack trace logging before git init

Result: All 1847 tests passed, and the bug became structurally impossible to reproduce.

Key Insight

All four layers were necessary. Each layer caught bugs the others missed during testing, demonstrating that redundant validation isn't waste—it's essential defense.