|
| 1 | +import simulationRuntime = require('./simulation-runtime'); |
| 2 | +import { publishStageStartMessages } from './consensus-gate-stage'; |
| 3 | +import type { |
| 4 | + ConsensusProducers, |
| 5 | + RequiredQualityGate, |
| 6 | + ScenarioContext, |
| 7 | +} from './consensus-gate-contracts'; |
| 8 | + |
| 9 | +interface SimulationMessageBus { |
| 10 | + publish(message: unknown): unknown; |
| 11 | +} |
| 12 | + |
| 13 | +interface EvaluateScenarioOptions extends ScenarioContext { |
| 14 | + publishMessages( |
| 15 | + messageBus: SimulationMessageBus, |
| 16 | + producers: ConsensusProducers, |
| 17 | + clusterId: string |
| 18 | + ): void; |
| 19 | +} |
| 20 | + |
| 21 | +interface PassingQualityGate { |
| 22 | + id: string; |
| 23 | + status: 'PASS'; |
| 24 | + scope: string; |
| 25 | + completedAt: number; |
| 26 | + evidence: { |
| 27 | + command: string; |
| 28 | + exitCode: 0; |
| 29 | + output: string; |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +interface ApprovedResultData { |
| 34 | + approved: true; |
| 35 | + qualityGates?: PassingQualityGate[]; |
| 36 | +} |
| 37 | + |
| 38 | +function evaluateScenario({ |
| 39 | + agentId, |
| 40 | + cluster, |
| 41 | + topic, |
| 42 | + script, |
| 43 | + requiredQualityGates, |
| 44 | + producers, |
| 45 | + producersByTopic, |
| 46 | + requiredStageTopics, |
| 47 | + allowExternalTopics, |
| 48 | + publishMessages, |
| 49 | +}: EvaluateScenarioOptions): boolean { |
| 50 | + const { ledger, messageBus, logicEngine } = simulationRuntime.createSimulationRuntime(cluster); |
| 51 | + |
| 52 | + publishStageStartMessages({ |
| 53 | + messageBus, |
| 54 | + clusterId: cluster.id, |
| 55 | + producersByTopic, |
| 56 | + requiredStageTopics, |
| 57 | + allowExternalTopics, |
| 58 | + }); |
| 59 | + publishMessages(messageBus, producers, cluster.id); |
| 60 | + |
| 61 | + const result = logicEngine.evaluate( |
| 62 | + script, |
| 63 | + { id: agentId, cluster_id: cluster.id, requiredQualityGates }, |
| 64 | + { topic } |
| 65 | + ); |
| 66 | + ledger.close(); |
| 67 | + return result; |
| 68 | +} |
| 69 | + |
| 70 | +function getPassingQualityGate(requiredGate: RequiredQualityGate): PassingQualityGate { |
| 71 | + const scope = requiredGate.scope || 'template-sim'; |
| 72 | + return { |
| 73 | + id: requiredGate.id, |
| 74 | + status: 'PASS', |
| 75 | + scope, |
| 76 | + completedAt: Date.now(), |
| 77 | + evidence: { |
| 78 | + command: `quality-check --scope ${scope}`, |
| 79 | + exitCode: 0, |
| 80 | + output: 'template simulation quality pass', |
| 81 | + }, |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +function getApprovedResultData(context: ScenarioContext): ApprovedResultData { |
| 86 | + const data: ApprovedResultData = { approved: true }; |
| 87 | + const requiredQualityGates = Array.isArray(context.requiredQualityGates) |
| 88 | + ? context.requiredQualityGates |
| 89 | + : []; |
| 90 | + if ( |
| 91 | + context.agentId === 'git-pusher' && |
| 92 | + context.topic === 'VALIDATION_RESULT' && |
| 93 | + requiredQualityGates.length > 0 |
| 94 | + ) { |
| 95 | + data.qualityGates = requiredQualityGates.map(getPassingQualityGate); |
| 96 | + } |
| 97 | + return data; |
| 98 | +} |
| 99 | + |
| 100 | +export function checkDuplicateProducerScenario(context: ScenarioContext): boolean { |
| 101 | + return evaluateScenario({ |
| 102 | + ...context, |
| 103 | + publishMessages(messageBus, producers, clusterId) { |
| 104 | + const message = { |
| 105 | + cluster_id: clusterId, |
| 106 | + topic: context.topic, |
| 107 | + sender: producers[0], |
| 108 | + content: { data: getApprovedResultData(context) }, |
| 109 | + }; |
| 110 | + messageBus.publish(message); |
| 111 | + messageBus.publish(message); |
| 112 | + }, |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +export function checkDistinctProducerScenario(context: ScenarioContext): boolean { |
| 117 | + return evaluateScenario({ |
| 118 | + ...context, |
| 119 | + publishMessages(messageBus, producers, clusterId) { |
| 120 | + for (const producer of producers) { |
| 121 | + messageBus.publish({ |
| 122 | + cluster_id: clusterId, |
| 123 | + topic: context.topic, |
| 124 | + sender: producer, |
| 125 | + content: { data: getApprovedResultData(context) }, |
| 126 | + }); |
| 127 | + } |
| 128 | + }, |
| 129 | + }); |
| 130 | +} |
0 commit comments