|
| 1 | +import type { AgentRiskReport, SwarmConsensusDecision } from '@agent-safe/shared'; |
| 2 | +import { CONSENSUS_THRESHOLD } from '@agent-safe/shared'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Coordinator Agent – aggregates individual agent reports and produces consensus. |
| 6 | + * TODO: Implement weighted voting, configurable thresholds. |
| 7 | + */ |
| 8 | +export async function runCoordinatorAgent( |
| 9 | + reports: AgentRiskReport[], |
| 10 | +): Promise<SwarmConsensusDecision> { |
| 11 | + // TODO: Implement weighted voting based on agent confidence |
| 12 | + // TODO: Configurable consensus threshold |
| 13 | + // TODO: Handle conflicting agent outputs |
| 14 | + |
| 15 | + const highRiskCount = reports.filter( |
| 16 | + (r) => r.risk_level === 'HIGH' || r.risk_level === 'CRITICAL', |
| 17 | + ).length; |
| 18 | + |
| 19 | + const riskScore = Math.round( |
| 20 | + (reports.reduce((sum, r) => { |
| 21 | + const levelScore = { LOW: 10, MEDIUM: 40, HIGH: 75, CRITICAL: 95 }[r.risk_level]; |
| 22 | + return sum + levelScore * r.confidence; |
| 23 | + }, 0) / |
| 24 | + reports.length) * |
| 25 | + 1, |
| 26 | + ); |
| 27 | + |
| 28 | + const consensusMet = highRiskCount >= CONSENSUS_THRESHOLD; |
| 29 | + |
| 30 | + return { |
| 31 | + final_decision: consensusMet ? 'BLOCK' : 'ALLOW', |
| 32 | + risk_score: riskScore, |
| 33 | + consensus: `${highRiskCount}/${reports.length} agents`, |
| 34 | + summary: consensusMet |
| 35 | + ? 'Multiple agents flagged high risk – transaction blocked.' |
| 36 | + : 'Risk level acceptable – transaction allowed.', |
| 37 | + actions: consensusMet ? ['BLOCK_TX'] : ['ALLOW'], |
| 38 | + agent_reports: reports, |
| 39 | + timestamp: new Date().toISOString(), |
| 40 | + }; |
| 41 | +} |
0 commit comments