|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * G5 — Semantic Handshake Gate |
| 5 | + * |
| 6 | + * Agent: @dev |
| 7 | + * Type: Automated, Blocking when Semantic Handshake reports BLOCKER violations |
| 8 | + * |
| 9 | + * Purpose: Preserve intent integrity between planning and implementation by |
| 10 | + * evaluating proposed code against executable planning constraints. |
| 11 | + */ |
| 12 | + |
| 13 | +const path = require('path'); |
| 14 | +const { VerificationGate } = require(path.resolve(__dirname, '../verification-gate.js')); |
| 15 | +const { |
| 16 | + SemanticHandshakeEngine, |
| 17 | +} = require(path.resolve(__dirname, '../../synapse/context/semantic-handshake-engine.js')); |
| 18 | + |
| 19 | +const G5_DEFAULT_TIMEOUT_MS = 2000; |
| 20 | + |
| 21 | +class G5SemanticHandshakeGate extends VerificationGate { |
| 22 | + constructor(options = {}) { |
| 23 | + super({ |
| 24 | + gateId: 'G5', |
| 25 | + agent: '@dev', |
| 26 | + blocking: true, |
| 27 | + timeoutMs: options.timeoutMs ?? G5_DEFAULT_TIMEOUT_MS, |
| 28 | + circuitBreakerOptions: options.circuitBreakerOptions, |
| 29 | + logger: options.logger, |
| 30 | + }); |
| 31 | + |
| 32 | + this._engine = options.engine || new SemanticHandshakeEngine({ |
| 33 | + constraints: options.constraints || [], |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + async _doVerify(context = {}) { |
| 38 | + this._registerContextConstraints(context); |
| 39 | + |
| 40 | + const constraints = this._engine.getConstraints(); |
| 41 | + if (constraints.length === 0) { |
| 42 | + return { |
| 43 | + passed: true, |
| 44 | + warnings: ['No Semantic Handshake constraints registered'], |
| 45 | + opportunities: [], |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + if (!this._hasCodeContext(context)) { |
| 50 | + return { |
| 51 | + passed: true, |
| 52 | + warnings: ['Semantic Handshake constraints registered, but no proposed code was provided'], |
| 53 | + opportunities: constraints.map(constraint => ({ |
| 54 | + entity: constraint.id, |
| 55 | + recommendation: constraint.description, |
| 56 | + severity: constraint.severity, |
| 57 | + })), |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + const result = await this._engine.validateExecutionIntent(context); |
| 62 | + const report = this._engine.generateComplianceReport(result); |
| 63 | + |
| 64 | + return { |
| 65 | + passed: result.passed, |
| 66 | + warnings: [ |
| 67 | + ...result.warnings, |
| 68 | + ...result.blockingViolations.map(violation => violation.message), |
| 69 | + ], |
| 70 | + opportunities: [ |
| 71 | + ...result.verifiedConstraints.map(constraint => ({ |
| 72 | + entity: constraint.id, |
| 73 | + recommendation: 'Constraint verified', |
| 74 | + severity: constraint.severity, |
| 75 | + })), |
| 76 | + ...result.violations.map(violation => ({ |
| 77 | + entity: violation.id, |
| 78 | + recommendation: violation.message, |
| 79 | + severity: violation.severity, |
| 80 | + matches: violation.matches, |
| 81 | + })), |
| 82 | + ], |
| 83 | + override: result.passed ? null : { |
| 84 | + reason: 'Semantic Handshake blocker violation', |
| 85 | + correctionPrompt: result.correctionPrompt, |
| 86 | + report, |
| 87 | + }, |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + _registerContextConstraints(context) { |
| 92 | + const planningText = [ |
| 93 | + context.planningOutput, |
| 94 | + context.planningText, |
| 95 | + context.architectureText, |
| 96 | + context.storyText, |
| 97 | + ].filter(Boolean).join('\n\n'); |
| 98 | + |
| 99 | + if (planningText.trim()) { |
| 100 | + this._engine.registerConstraints(planningText, { |
| 101 | + source: context.source || '@architect', |
| 102 | + metadata: { |
| 103 | + storyId: context.storyId || 'unknown', |
| 104 | + }, |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + if (Array.isArray(context.constraints)) { |
| 109 | + for (const constraint of context.constraints) { |
| 110 | + this._engine.addConstraint(constraint); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + _hasCodeContext(context) { |
| 116 | + return Boolean( |
| 117 | + context.proposedCode || |
| 118 | + (Array.isArray(context.files) && context.files.length > 0) || |
| 119 | + (Array.isArray(context.diffs) && context.diffs.length > 0) || |
| 120 | + (Array.isArray(context.codeFiles) && context.codeFiles.length > 0), |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +module.exports = { |
| 126 | + G5SemanticHandshakeGate, |
| 127 | + G5_DEFAULT_TIMEOUT_MS, |
| 128 | +}; |
0 commit comments