tractatus/scripts/audit-enforcement.js
TheFlow 86d7042f42 feat(governance): implement comprehensive enforcement architecture
Completes enforcement implementation from ENFORCEMENT_AUDIT.md analysis:

 Implemented (6 enforcement mechanisms):
1. Token checkpoint monitoring (inst_075)
   - .claude/hooks/check-token-checkpoint.js
   - PostToolUse hook integration

2. Trigger word detection (inst_078, inst_082)
   - .claude/hooks/trigger-word-checker.js (already completed)
   - "ff" and "ffs" triggers architecturally enforced

3. Framework activity verification (inst_064)
   - Enhanced scripts/session-init.js with fade detection
   - Alerts when components stale >20 messages

4. Test requirement enforcement (inst_068)
   - Enhanced .git/hooks/pre-commit
   - Runs tests if test files exist for modified code
   - Blocks commits on test failures

5. Background process tracking (inst_023)
   - scripts/track-background-process.js
   - Integrated into session-init.js and session-closedown.js
   - Tracks persistent vs temporary processes

6. Security logging verification (inst_046)
   - scripts/verify-security-logging.js
   - Can be integrated into deployment workflow

7. Meta-enforcement monitoring system
   - scripts/audit-enforcement.js
   - Scans HIGH persistence instructions for imperatives
   - Reports enforcement gaps (currently 28/39 gaps)

🔒 Protection Added:
- inst_027: Hard block on instruction-history.json edits
- Conventional commit format enforcement (inst_066)
- CSP + test validation in pre-commit hook

📊 Current Enforcement Status:
- Baseline: 11/39 imperative instructions enforced (28%)
- Framework fade detection operational
- Token checkpoints architecturally monitored

🎯 Philosophy:
"If it's MANDATORY, it must be ENFORCED architecturally, not documented."

This addresses the root cause of voluntary compliance failures identified
when Claude missed "ffs" trigger and token checkpoints despite active
HIGH persistence instructions.

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-25 13:15:06 +13:00

135 lines
5 KiB
JavaScript
Executable file

#!/usr/bin/env node
/**
* Meta-Enforcement Monitoring System
* Scans instructions for MUST/NEVER/MANDATORY language and verifies enforcement
*
* Per ENFORCEMENT_AUDIT.md: "If it's MANDATORY, it must be ENFORCED architecturally"
*/
const fs = require('fs');
const path = require('path');
const INSTRUCTION_FILE = path.join(__dirname, '../.claude/instruction-history.json');
// Known enforcement mechanisms
const ENFORCEMENT_MAP = {
inst_008: ['.git/hooks/pre-commit', 'scripts/check-csp-violations.js'],
inst_023: ['scripts/track-background-process.js', 'scripts/session-init.js', 'scripts/session-closedown.js'],
inst_027: ['.claude/hooks/framework-audit-hook.js'],
inst_038: ['.claude/hooks/framework-audit-hook.js'],
inst_046: ['scripts/verify-security-logging.js'],
inst_064: ['scripts/session-init.js'], // Framework activity verification
inst_065: ['scripts/session-init.js'],
inst_066: ['.git/hooks/commit-msg'],
inst_068: ['.git/hooks/pre-commit'],
inst_070: ['.git/hooks/pre-commit'],
inst_071: ['scripts/deploy.sh'],
inst_075: ['.claude/hooks/check-token-checkpoint.js'],
inst_077: ['scripts/session-closedown.js'],
inst_078: ['.claude/hooks/trigger-word-checker.js'],
inst_082: ['.claude/hooks/trigger-word-checker.js']
};
function loadInstructions() {
const data = JSON.parse(fs.readFileSync(INSTRUCTION_FILE, 'utf8'));
return data.instructions.filter(i => i.active);
}
function hasImperativeLanguage(text) {
const imperatives = [
/\bMUST\b/i,
/\bNEVER\b/i,
/\bMANDATORY\b/i,
/\bREQUIRED\b/i,
/\bBLOCK(S|ED)?\b/i,
/\bCRITICAL\b.*\bFAILURE\b/i,
/\bALWAYS\b/i,
/\bSHOULD NOT\b/i
];
return imperatives.some(pattern => pattern.test(text));
}
function checkEnforcementExists(instId, enforcementPaths) {
const missing = [];
const exists = [];
enforcementPaths.forEach(p => {
const fullPath = path.join(__dirname, '..', p);
if (fs.existsSync(fullPath)) {
exists.push(p);
} else {
missing.push(p);
}
});
return { exists, missing };
}
function main() {
console.log('\n🔍 Meta-Enforcement Audit\n');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
const instructions = loadInstructions();
const highPersistence = instructions.filter(i => i.persistence === 'HIGH');
console.log(`Total active instructions: ${instructions.length}`);
console.log(`HIGH persistence instructions: ${highPersistence.length}\n`);
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
const imperativeInstructions = highPersistence.filter(i => hasImperativeLanguage(i.text));
console.log(`Instructions with imperative language: ${imperativeInstructions.length}\n`);
let enforced = 0;
let unenforced = 0;
const gaps = [];
imperativeInstructions.forEach(inst => {
const hasEnforcement = ENFORCEMENT_MAP[inst.id];
if (hasEnforcement) {
const check = checkEnforcementExists(inst.id, hasEnforcement);
if (check.missing.length === 0) {
console.log(`${inst.id}: ENFORCED`);
console.log(` Mechanisms: ${check.exists.join(', ')}`);
enforced++;
} else {
console.log(`⚠️ ${inst.id}: PARTIALLY ENFORCED`);
console.log(` Exists: ${check.exists.join(', ')}`);
console.log(` Missing: ${check.missing.join(', ')}`);
gaps.push({ id: inst.id, missing: check.missing, text: inst.text.substring(0, 80) + '...' });
unenforced++;
}
} else {
console.log(`${inst.id}: NO ENFORCEMENT`);
console.log(` Text: ${inst.text.substring(0, 80)}...`);
gaps.push({ id: inst.id, missing: ['No enforcement mechanism defined'], text: inst.text.substring(0, 80) + '...' });
unenforced++;
}
console.log('');
});
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
console.log('Summary:\n');
console.log(` Imperative instructions: ${imperativeInstructions.length}`);
console.log(` Enforced: ${enforced} (${Math.round(enforced/imperativeInstructions.length*100)}%)`);
console.log(` Unenforced/Partial: ${unenforced} (${Math.round(unenforced/imperativeInstructions.length*100)}%)`);
if (gaps.length > 0) {
console.log(`\n⚠️ ${gaps.length} enforcement gap(s) detected\n`);
console.log('Gaps should be addressed to prevent voluntary compliance failures.\n');
} else {
console.log('\n✅ All imperative instructions have enforcement mechanisms!\n');
}
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
if (gaps.length > 0) {
process.exit(1); // Exit with error if gaps exist
}
}
main();