import chalk from 'chalk'; import * as fs from 'fs'; import * as path from 'path'; export type GovernanceMode = 'OPEN' | 'SOFT_LOCK' | 'HARD_LOCK'; export interface InterventionDecision { mode: GovernanceMode; message: string; blockCommit: boolean; } /** * The Silent Sentinel (Frank's Enforcer) * Determines the severity of an event and the required system response. */ export class InterventionProtocol { private activeViolators = new Set(); /** * Registers a violation outcome to update the global lock state. */ registerViolation(filePath: string, decision: InterventionDecision) { if (decision.mode === 'HARD_LOCK') { this.activeViolators.add(filePath); this.syncLockFile(); } } /** * Clears any active locks for a specific file (e.g. after a fix). */ clear(filePath: string) { if (this.activeViolators.has(filePath)) { this.activeViolators.delete(filePath); this.syncLockFile(); } } private syncLockFile() { try { const lockDir = path.join(process.cwd(), '.rigstate'); if (!fs.existsSync(lockDir)) fs.mkdirSync(lockDir, { recursive: true }); const lockPath = path.join(lockDir, 'guardian.lock'); if (this.activeViolators.size > 0) { const content = `HARD_LOCK_ACTIVE\nTimestamp: ${new Date().toISOString()}\n\nBlocking Files:\n${Array.from(this.activeViolators).join('\n')}`; fs.writeFileSync(lockPath, content, 'utf-8'); } else { if (fs.existsSync(lockPath)) fs.unlinkSync(lockPath); } } catch (e) { console.error('Failed to sync guardian lock file:', e); } } /** * Evaluate a Heuristic Trigger (Preventative) */ evaluateTrigger(skillId: string, confidence: string): InterventionDecision { // Example: If a skill is marked as 'HARD_LOCK' in its metadata, we block. // For now, most triggers are informational (JIT provisioning). if (skillId === 'rigstate-integrity-gate') { return { mode: 'SOFT_LOCK', message: 'Integrity Gate detected. Release Manifest required before final push.', blockCommit: false // Soft lock just warns }; } return { mode: 'OPEN', message: `Predictive activation: ${skillId}`, blockCommit: false }; } /** * Evaluate a Guardian Violation (Corrective) */ evaluateViolation(ruleId: string, severity: 'critical' | 'warning' | 'info'): InterventionDecision { if (severity === 'critical' || (severity as any) === 'error') { return { mode: 'HARD_LOCK', message: `CRITICAL VIOLATION: ${ruleId}. System Integrity Compromised.`, blockCommit: true }; } if (severity === 'warning') { return { mode: 'SOFT_LOCK', message: `Warning: ${ruleId}. Review recommended.`, blockCommit: false }; } return { mode: 'OPEN', message: 'Info notice.', blockCommit: false }; } /** * Logs the intervention to the console with appropriate visual weight */ enforce(decision: InterventionDecision) { if (decision.mode === 'OPEN') return; const icon = decision.mode === 'HARD_LOCK' ? '🚫' : '⚠️'; const color = decision.mode === 'HARD_LOCK' ? chalk.bgRed.white.bold : chalk.yellow.bold; console.log('\n' + color(` ${icon} [${decision.mode}] INTERVENTION `)); console.log(chalk.redBright(` ${decision.message}`)); if (decision.blockCommit) { console.log(chalk.dim(' 🔒 Commit functionality is logically suspended until fixed.')); } } } export function createInterventionProtocol() { return new InterventionProtocol(); }