Skip to main content

Validation System

Mega Brain includes robust validation tools to ensure package integrity, layer compliance, and code quality. The validation system runs automatically during development and blocks publish if issues are found.

Validation Architecture

Core Validation Tools

validate-package.js

Purpose: Validates that npm package contains only L1 (Community) content Location: bin/validate-package.js Usage:
How it works:
1

Get Package Files

Runs npm pack --dry-run --json to get list of files that would be published:
2

Classify Files

Calls audit_layers.py via Python subprocess:
3

Check Violations

Any file not classified as L1 is a violation:
4

Return Status

  • Exit 0 - All files are L1 (PASSED)
  • Exit 1 - Non-L1 files found (FAILED)
  • Exit 2 - Validation error (ERROR)
See Layer Management for complete L1/L2/L3 classification rules.

pre-publish-gate.js

Purpose: Security gate that blocks npm publish if secrets or non-L1 content detected Location: bin/pre-publish-gate.js Trigger: Automatically via prepublishOnly npm script
Validation Steps:
Removes Python cache directories:
Same as validate-package.js:
Blocks forbidden files:
Scans text files for secret patterns:
Special handling:
  • Skips binary files (.png, .pdf, .zip, etc.)
  • Allows up to 3 emails per file (more = PII leak)
If trufflehog is installed, runs deep scan:
Install trufflehog for enhanced secret detection: brew install trufflehog
Calls validate-package.js:
Verdict:
The pre-publish gate uses fail-CLOSED design: if validation fails, publish is physically blocked.

Layer Validation

audit_layers.py

Purpose: Classify all repository files into L1/L2/L3/NEVER/DELETE/REVIEW Location: core/intelligence/audit_layers.py Usage:
Audit Report Structure:
The audit includes 20,797 items across all layers. REVIEW items (58.6%) need manual classification.

Validation Hooks

Mega Brain uses PreToolUse hooks for real-time validation:

creation_validator.py

Event: PreToolUse (Write|Edit) Purpose: Validates new file creation against layer rules

claude_md_guard.py

Event: PreToolUse (Write|Edit) Purpose: Prevents CLAUDE.md creation in invalid locations
CLAUDE.md files in subdirectories (e.g., data/CLAUDE.md) are strictly forbidden per system policy.

Quality Validation

quality_watchdog.py

Event: UserPromptSubmit Purpose: Monitors quality metrics and warns about potential issues

stop_hook_completeness.py

Event: Stop Purpose: Checks if tasks are complete before stopping

CI/CD Validation

GitHub Actions Integration

Validation Checklist

Before publishing or committing:
1

Run Package Validation

Ensure: PASSED: All {N} pack files are L1
2

Run Security Gate

Ensure: Security gate PASSED
3

Check Audit Report

Review DELETE and REVIEW items
4

Validate Layer Compliance

  • No L2/L3 files in package
  • No NEVER files anywhere
  • DELETE candidates removed
  • REVIEW items classified
5

Test Hooks

Troubleshooting

Problem: validate-package.js fails to run npm pack --dry-runSolutions:
  1. Check package.json has valid files field
  2. Run npm pack --dry-run manually to see error
  3. Ensure npm version >= 7
  4. Check for circular dependencies
Problem: File classified as wrong layerSolutions:
  1. Check audit_layers.py patterns match file path
  2. Verify path doesn’t have typos
  3. Update L1_PATTERNS / L2_PATTERNS if needed
  4. Re-run audit after pattern updates
Problem: Security gate blocks file that shouldn’t be blockedSolutions:
  1. Check if file matches FORBIDDEN_FILE_PATTERNS
  2. Verify file content doesn’t match SECRET_PATTERNS
  3. If false positive, update patterns in pre-publish-gate.js
  4. Add exception for specific file type
Problem: PreToolUse hooks slow down file operationsSolutions:
  1. Check hook timeout settings (should be 2-5s)
  2. Optimize validation logic (cache results)
  3. Move heavy validation to PostToolUse
  4. Use settings.local.json to disable non-critical hooks

Best Practices

Validation Guidelines

  1. Run validation early - Test before committing
  2. Trust the gates - Never bypass pre-publish checks
  3. Review REVIEW items - Classify unknown files promptly
  4. Update patterns - Keep layer patterns current
  5. Monitor false positives - Adjust secret patterns if needed
  6. Document exceptions - Note why files are excluded
  7. Automate in CI - Run validation on every PR

Layer Management

Complete L1/L2/L3 layer classification system

Hooks System

PreToolUse and PostToolUse validation hooks

Publishing

Publishing workflow and security gates

CI/CD

Continuous integration and deployment