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:
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
Step 1: Clean __pycache__
Step 1: Clean __pycache__
Removes Python cache directories:
Step 2: Get Package Files
Step 2: Get Package Files
Same as
validate-package.js:Step 3: Check File Names
Step 3: Check File Names
Blocks forbidden files:
Step 4: Scan File Contents
Step 4: Scan File Contents
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)
Step 5: Run trufflehog (Optional)
Step 5: Run trufflehog (Optional)
If
trufflehog is installed, runs deep scan:Step 6: Layer Validation
Step 6: Layer Validation
Calls
validate-package.js:Layer Validation
audit_layers.py
Purpose: Classify all repository files into L1/L2/L3/NEVER/DELETE/REVIEW Location:core/intelligence/audit_layers.py
Usage:
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 rulesclaude_md_guard.py
Event: PreToolUse (Write|Edit) Purpose: Prevents CLAUDE.md creation in invalid locationsQuality Validation
quality_watchdog.py
Event: UserPromptSubmit Purpose: Monitors quality metrics and warns about potential issuesstop_hook_completeness.py
Event: Stop Purpose: Checks if tasks are complete before stoppingCI/CD Validation
GitHub Actions Integration
Validation Checklist
Before publishing or committing:1
Run Package Validation
PASSED: All {N} pack files are L12
Run Security Gate
Security gate PASSED3
Check Audit Report
4
Validate Layer Compliance
- No L2/L3 files in package
- No NEVER files anywhere
- DELETE candidates removed
- REVIEW items classified
5
Test Hooks
Troubleshooting
Validation Fails with 'npm pack error'
Validation Fails with 'npm pack error'
Problem:
validate-package.js fails to run npm pack --dry-runSolutions:- Check
package.jsonhas validfilesfield - Run
npm pack --dry-runmanually to see error - Ensure npm version >= 7
- Check for circular dependencies
Layer Classification Incorrect
Layer Classification Incorrect
Problem: File classified as wrong layerSolutions:
- Check
audit_layers.pypatterns match file path - Verify path doesn’t have typos
- Update
L1_PATTERNS/L2_PATTERNSif needed - Re-run audit after pattern updates
Pre-Publish Gate Blocks Valid File
Pre-Publish Gate Blocks Valid File
Problem: Security gate blocks file that shouldn’t be blockedSolutions:
- Check if file matches
FORBIDDEN_FILE_PATTERNS - Verify file content doesn’t match
SECRET_PATTERNS - If false positive, update patterns in
pre-publish-gate.js - Add exception for specific file type
Hook Validation Too Slow
Hook Validation Too Slow
Problem: PreToolUse hooks slow down file operationsSolutions:
- Check hook timeout settings (should be 2-5s)
- Optimize validation logic (cache results)
- Move heavy validation to PostToolUse
- Use
settings.local.jsonto disable non-critical hooks
Best Practices
Validation Guidelines
- Run validation early - Test before committing
- Trust the gates - Never bypass pre-publish checks
- Review REVIEW items - Classify unknown files promptly
- Update patterns - Keep layer patterns current
- Monitor false positives - Adjust secret patterns if needed
- Document exceptions - Note why files are excluded
- Automate in CI - Run validation on every PR
Related Documentation
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
