/** * Metacognitive Verifier Service * Implements AI self-verification before proposing actions * * Core Tractatus Service: Provides structured "pause and verify" mechanism * where AI checks its own reasoning before execution. * * Verification Checks: * 1. Alignment: Does action align with stated user goals? * 2. Coherence: Is reasoning internally consistent? * 3. Completeness: Are all requirements addressed? * 4. Safety: Could this action cause harm or confusion? * 5. Alternatives: Have better approaches been considered? */ const classifier = require('./InstructionPersistenceClassifier.service'); const validator = require('./CrossReferenceValidator.service'); const enforcer = require('./BoundaryEnforcer.service'); const monitor = require('./ContextPressureMonitor.service'); const logger = require('../utils/logger.util'); /** * Verification dimensions */ const VERIFICATION_DIMENSIONS = { ALIGNMENT: { name: 'Alignment', description: 'Action aligns with user goals and explicit instructions', weight: 0.3, criticalThreshold: 0.7 }, COHERENCE: { name: 'Coherence', description: 'Reasoning is internally consistent and logical', weight: 0.2, criticalThreshold: 0.7 }, COMPLETENESS: { name: 'Completeness', description: 'All requirements and constraints addressed', weight: 0.2, criticalThreshold: 0.8 }, SAFETY: { name: 'Safety', description: 'Action will not cause harm, confusion, or data loss', weight: 0.2, criticalThreshold: 0.9 }, ALTERNATIVES: { name: 'Alternatives', description: 'Better alternative approaches have been considered', weight: 0.1, criticalThreshold: 0.6 } }; /** * Confidence levels */ const CONFIDENCE_LEVELS = { HIGH: { min: 0.8, action: 'PROCEED', description: 'High confidence, proceed' }, MEDIUM: { min: 0.6, action: 'PROCEED_WITH_CAUTION', description: 'Medium confidence, proceed with notification' }, LOW: { min: 0.4, action: 'REQUEST_CONFIRMATION', description: 'Low confidence, request user confirmation' }, VERY_LOW: { min: 0.0, action: 'REQUIRE_REVIEW', description: 'Very low confidence, require human review' } }; class MetacognitiveVerifier { constructor() { this.dimensions = VERIFICATION_DIMENSIONS; this.confidenceLevels = CONFIDENCE_LEVELS; this.classifier = classifier; this.validator = validator; this.enforcer = enforcer; this.monitor = monitor; logger.info('MetacognitiveVerifier initialized'); } /** * Verify a proposed action before execution * @param {Object} action - The proposed action * @param {Object} reasoning - The reasoning behind the action * @param {Object} context - Conversation/session context * @returns {Object} Verification result */ verify(action, reasoning, context) { try { // Run all verification checks const alignmentScore = this._checkAlignment(action, reasoning, context); const coherenceScore = this._checkCoherence(action, reasoning, context); const completenessScore = this._checkCompleteness(action, reasoning, context); const safetyScore = this._checkSafety(action, reasoning, context); const alternativesScore = this._checkAlternatives(action, reasoning, context); // Calculate weighted confidence score const scores = { alignment: alignmentScore, coherence: coherenceScore, completeness: completenessScore, safety: safetyScore, alternatives: alternativesScore }; const confidence = this._calculateConfidence(scores); // Determine confidence level const confidenceLevel = this._determineConfidenceLevel(confidence); // Check for critical failures const criticalFailures = this._checkCriticalFailures(scores); // Get pressure analysis const pressureAnalysis = this.monitor.analyzePressure(context); // Adjust confidence based on pressure const adjustedConfidence = this._adjustForPressure( confidence, pressureAnalysis ); // Generate verification result const verification = { confidence: adjustedConfidence, originalConfidence: confidence, level: confidenceLevel.action, description: confidenceLevel.description, scores, criticalFailures, pressureLevel: pressureAnalysis.pressureName, pressureAdjustment: adjustedConfidence - confidence, recommendations: this._generateRecommendations( scores, criticalFailures, pressureAnalysis ), decision: this._makeVerificationDecision( adjustedConfidence, criticalFailures, pressureAnalysis ), timestamp: new Date() }; // Log verification if (verification.decision !== 'PROCEED') { logger.warn('Action verification flagged', { action: action.description?.substring(0, 50), decision: verification.decision, confidence: adjustedConfidence }); } return verification; } catch (error) { logger.error('Verification error:', error); return this._failSafeVerification(action); } } /** * Quick verification for low-risk actions */ quickVerify(action, context) { // Simplified verification for routine actions const boundaryCheck = this.enforcer.enforce(action, context); const pressureCheck = this.monitor.shouldProceed(action, context); if (!boundaryCheck.allowed || !pressureCheck.proceed) { return { confidence: 0.3, level: 'REQUIRE_REVIEW', decision: 'BLOCK', reason: 'Failed boundary or pressure check', timestamp: new Date() }; } return { confidence: 0.7, level: 'PROCEED', decision: 'PROCEED', quickCheck: true, timestamp: new Date() }; } /** * Private verification methods */ _checkAlignment(action, reasoning, context) { let score = 0.5; // Base score // Check cross-reference validation const validation = this.validator.validate(action, context); if (validation.status === 'APPROVED') { score += 0.3; } else if (validation.status === 'WARNING') { score += 0.1; } else if (validation.status === 'REJECTED') { score -= 0.3; } // Check if action addresses stated user goal if (reasoning.userGoal && reasoning.addresses) { score += 0.2; } // Check consistency with recent user statements if (context.recentUserStatements) { const consistencyScore = this._checkConsistencyWithStatements( action, context.recentUserStatements ); score += consistencyScore * 0.2; } return Math.min(1.0, Math.max(0.0, score)); } _checkCoherence(action, reasoning, context) { let score = 0.7; // Default to reasonable coherence // Check if reasoning steps are provided if (!reasoning.steps || reasoning.steps.length === 0) { score -= 0.2; } // Check for logical consistency if (reasoning.assumptions && reasoning.conclusions) { const logicallySound = this._checkLogicalFlow( reasoning.assumptions, reasoning.conclusions ); if (logicallySound) { score += 0.2; } else { score -= 0.3; } } // Check for internal contradictions if (this._hasContradictions(reasoning)) { score -= 0.4; } return Math.min(1.0, Math.max(0.0, score)); } _checkCompleteness(action, reasoning, context) { let score = 0.6; // Base score // Check if all stated requirements are addressed if (context.requirements) { const addressedCount = context.requirements.filter(req => this._isRequirementAddressed(req, action, reasoning) ).length; score += (addressedCount / context.requirements.length) * 0.3; } // Check for edge cases consideration if (reasoning.edgeCases && reasoning.edgeCases.length > 0) { score += 0.1; } // Check for error handling if (reasoning.errorHandling || action.errorHandling) { score += 0.1; } return Math.min(1.0, Math.max(0.0, score)); } _checkSafety(action, reasoning, context) { let score = 0.8; // Default to safe unless red flags // Check boundary enforcement const boundaryCheck = this.enforcer.enforce(action, context); if (!boundaryCheck.allowed) { score -= 0.5; // Major safety concern } // Check for destructive operations const destructivePatterns = [ /delete|remove|drop|truncate/i, /force|--force|-f\s/i, /rm\s+-rf/i ]; const actionText = action.description || action.command || ''; for (const pattern of destructivePatterns) { if (pattern.test(actionText)) { score -= 0.2; break; } } // Check if data backup is mentioned for risky operations if (score < 0.7 && !reasoning.backupMentioned) { score -= 0.1; } // Check for validation before execution if (action.requiresValidation && !reasoning.validationPlanned) { score -= 0.1; } return Math.min(1.0, Math.max(0.0, score)); } _checkAlternatives(action, reasoning, context) { let score = 0.5; // Base score // Check if alternatives were considered if (reasoning.alternativesConsidered && reasoning.alternativesConsidered.length > 0) { score += 0.3; } // Check if rationale for chosen approach is provided if (reasoning.chosenBecause) { score += 0.2; } // Lower score if action seems like first idea without exploration if (!reasoning.alternativesConsidered && !reasoning.explored) { score -= 0.2; } return Math.min(1.0, Math.max(0.0, score)); } _calculateConfidence(scores) { let confidence = 0; for (const [dimension, dimensionConfig] of Object.entries(this.dimensions)) { const key = dimension.toLowerCase(); const score = scores[key] || 0.5; confidence += score * dimensionConfig.weight; } return Math.min(1.0, Math.max(0.0, confidence)); } _determineConfidenceLevel(confidence) { if (confidence >= CONFIDENCE_LEVELS.HIGH.min) { return CONFIDENCE_LEVELS.HIGH; } if (confidence >= CONFIDENCE_LEVELS.MEDIUM.min) { return CONFIDENCE_LEVELS.MEDIUM; } if (confidence >= CONFIDENCE_LEVELS.LOW.min) { return CONFIDENCE_LEVELS.LOW; } return CONFIDENCE_LEVELS.VERY_LOW; } _checkCriticalFailures(scores) { const failures = []; for (const [dimension, config] of Object.entries(this.dimensions)) { const key = dimension.toLowerCase(); const score = scores[key]; if (score < config.criticalThreshold) { failures.push({ dimension: config.name, score, threshold: config.criticalThreshold, severity: score < 0.3 ? 'CRITICAL' : 'WARNING' }); } } return failures; } _adjustForPressure(confidence, pressureAnalysis) { // Reduce confidence based on pressure level const pressureReduction = { NORMAL: 0, ELEVATED: 0.05, HIGH: 0.10, CRITICAL: 0.15, DANGEROUS: 0.25 }; const reduction = pressureReduction[pressureAnalysis.pressureName] || 0; return Math.max(0.0, confidence - reduction); } _generateRecommendations(scores, criticalFailures, pressureAnalysis) { const recommendations = []; // Recommendations based on low scores for (const [key, score] of Object.entries(scores)) { if (score < 0.5) { const dimension = this.dimensions[key.toUpperCase()]; recommendations.push({ type: 'LOW_SCORE', dimension: dimension.name, score, message: `Low ${dimension.name.toLowerCase()} score - ${dimension.description}`, action: `Improve ${dimension.name.toLowerCase()} before proceeding` }); } } // Recommendations based on critical failures for (const failure of criticalFailures) { recommendations.push({ type: 'CRITICAL_FAILURE', dimension: failure.dimension, severity: failure.severity, message: `${failure.dimension} below critical threshold`, action: 'Address this issue before proceeding' }); } // Include pressure recommendations if (pressureAnalysis.recommendations) { recommendations.push(...pressureAnalysis.recommendations); } return recommendations; } _makeVerificationDecision(confidence, criticalFailures, pressureAnalysis) { // Block if critical failures if (criticalFailures.some(f => f.severity === 'CRITICAL')) { return 'BLOCK'; } // Block if dangerous pressure if (pressureAnalysis.pressureLevel >= 4) { return 'BLOCK'; } // Require review if very low confidence if (confidence < 0.4) { return 'REQUIRE_REVIEW'; } // Request confirmation if low confidence if (confidence < 0.6) { return 'REQUEST_CONFIRMATION'; } // Proceed with caution if medium confidence if (confidence < 0.8) { return 'PROCEED_WITH_CAUTION'; } // Proceed if high confidence return 'PROCEED'; } /** * Helper methods */ _checkConsistencyWithStatements(action, statements) { // Simplified consistency check return 0.5; // Default to neutral } _checkLogicalFlow(assumptions, conclusions) { // Simplified logical flow check return true; // Assume logical unless obviously not } _hasContradictions(reasoning) { // Simplified contradiction detection return false; // Assume no contradictions unless detected } _isRequirementAddressed(requirement, action, reasoning) { // Simplified requirement matching const actionText = (action.description || '').toLowerCase(); const requirementText = requirement.toLowerCase(); return actionText.includes(requirementText); } _failSafeVerification(action) { return { confidence: 0.3, originalConfidence: 0.3, level: 'REQUIRE_REVIEW', description: 'Verification failed, requiring human review', scores: {}, criticalFailures: [{ dimension: 'ERROR', score: 0, threshold: 1, severity: 'CRITICAL' }], pressureLevel: 'ELEVATED', pressureAdjustment: 0, recommendations: [{ type: 'ERROR', severity: 'CRITICAL', message: 'Verification process encountered error', action: 'Require human review before proceeding' }], decision: 'REQUIRE_REVIEW', timestamp: new Date() }; } } // Singleton instance const verifier = new MetacognitiveVerifier(); module.exports = verifier;