| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | using System.Linq; |
| | | 6 | | |
| | | 7 | | using Microsoft.CodeAnalysis; |
| | | 8 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 9 | | |
| | | 10 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Analyzer that detects agents in a graph that are not reachable from the entry point. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <b>NDLRMAF022</b> (Warning): An agent declares edges in a named graph but is not reachable |
| | | 17 | | /// from that graph's entry point via any path. |
| | | 18 | | /// </remarks> |
| | | 19 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 20 | | public sealed class AgentGraphReachabilityAnalyzer : DiagnosticAnalyzer |
| | | 21 | | { |
| | | 22 | | private const string AgentGraphEdgeAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEdgeAttribute"; |
| | | 23 | | private const string AgentGraphEntryAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEntryAttribute"; |
| | | 24 | | |
| | | 25 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 52 | 26 | | ImmutableArray.Create(MafDiagnosticDescriptors.GraphUnreachableAgent); |
| | | 27 | | |
| | | 28 | | public override void Initialize(AnalysisContext context) |
| | | 29 | | { |
| | 9 | 30 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 9 | 31 | | context.EnableConcurrentExecution(); |
| | | 32 | | |
| | 9 | 33 | | context.RegisterCompilationStartAction(compilationContext => |
| | 9 | 34 | | { |
| | 9 | 35 | | // graphName → entryFqn |
| | 5 | 36 | | var entryPoints = new ConcurrentDictionary<string, string>(StringComparer.Ordinal); |
| | 9 | 37 | | // graphName → { sourceFqn → list of targetFqns } |
| | 5 | 38 | | var graphEdges = new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentBag<string>>>(Strin |
| | 9 | 39 | | // graphName → { fqn → (symbol, location) } for all edge source nodes |
| | 5 | 40 | | var edgeSourceNodes = new ConcurrentDictionary<string, ConcurrentDictionary<string, (INamedTypeSymbol Symbol |
| | 9 | 41 | | |
| | 5 | 42 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 5 | 43 | | { |
| | 82 | 44 | | var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| | 82 | 45 | | var fqn = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 5 | 46 | | |
| | 328 | 47 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | 5 | 48 | | { |
| | 82 | 49 | | var attrName = attr.AttributeClass?.ToDisplayString(); |
| | 5 | 50 | | |
| | 82 | 51 | | if (attrName == AgentGraphEntryAttributeName) |
| | 5 | 52 | | { |
| | 3 | 53 | | if (attr.ConstructorArguments.Length >= 1 |
| | 3 | 54 | | && attr.ConstructorArguments[0].Value is string graphName) |
| | 5 | 55 | | { |
| | 3 | 56 | | entryPoints.TryAdd(graphName, fqn); |
| | 5 | 57 | | } |
| | 5 | 58 | | } |
| | 5 | 59 | | |
| | 82 | 60 | | if (attrName == AgentGraphEdgeAttributeName) |
| | 5 | 61 | | { |
| | 7 | 62 | | if (attr.ConstructorArguments.Length < 2) |
| | 5 | 63 | | continue; |
| | 5 | 64 | | |
| | 7 | 65 | | if (attr.ConstructorArguments[0].Value is not string graphName) |
| | 5 | 66 | | continue; |
| | 5 | 67 | | |
| | 7 | 68 | | if (attr.ConstructorArguments[1].Kind != TypedConstantKind.Type |
| | 7 | 69 | | || attr.ConstructorArguments[1].Value is not INamedTypeSymbol targetType) |
| | 5 | 70 | | continue; |
| | 5 | 71 | | |
| | 7 | 72 | | var targetFqn = targetType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 5 | 73 | | |
| | 11 | 74 | | var perGraph = graphEdges.GetOrAdd(graphName, _ => new ConcurrentDictionary<string, ConcurrentBa |
| | 14 | 75 | | perGraph.GetOrAdd(fqn, _ => new ConcurrentBag<string>()).Add(targetFqn); |
| | 5 | 76 | | |
| | 7 | 77 | | var location = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 7 | 78 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 7 | 79 | | : typeSymbol.Locations[0]; |
| | 5 | 80 | | |
| | 11 | 81 | | var nodeMap = edgeSourceNodes.GetOrAdd(graphName, _ => new ConcurrentDictionary<string, (INamedT |
| | 7 | 82 | | nodeMap.TryAdd(fqn, (typeSymbol, location)); |
| | 5 | 83 | | } |
| | 5 | 84 | | } |
| | 87 | 85 | | }, SymbolKind.NamedType); |
| | 9 | 86 | | |
| | 5 | 87 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 5 | 88 | | { |
| | 18 | 89 | | foreach (var graphKvp in graphEdges) |
| | 5 | 90 | | { |
| | 4 | 91 | | var graphName = graphKvp.Key; |
| | 5 | 92 | | |
| | 4 | 93 | | if (!entryPoints.TryGetValue(graphName, out var entryFqn)) |
| | 5 | 94 | | continue; |
| | 5 | 95 | | |
| | 3 | 96 | | var adjacency = graphKvp.Value.ToDictionary( |
| | 6 | 97 | | kvp => kvp.Key, |
| | 6 | 98 | | kvp => kvp.Value.Distinct().ToList(), |
| | 3 | 99 | | StringComparer.Ordinal); |
| | 5 | 100 | | |
| | 5 | 101 | | // BFS from entry point |
| | 3 | 102 | | var reachable = new HashSet<string>(StringComparer.Ordinal); |
| | 3 | 103 | | var queue = new Queue<string>(); |
| | 3 | 104 | | queue.Enqueue(entryFqn); |
| | 3 | 105 | | reachable.Add(entryFqn); |
| | 5 | 106 | | |
| | 10 | 107 | | while (queue.Count > 0) |
| | 5 | 108 | | { |
| | 7 | 109 | | var current = queue.Dequeue(); |
| | 7 | 110 | | if (adjacency.TryGetValue(current, out var targets)) |
| | 5 | 111 | | { |
| | 16 | 112 | | foreach (var target in targets) |
| | 5 | 113 | | { |
| | 4 | 114 | | if (reachable.Add(target)) |
| | 5 | 115 | | { |
| | 4 | 116 | | queue.Enqueue(target); |
| | 5 | 117 | | } |
| | 5 | 118 | | } |
| | 5 | 119 | | } |
| | 5 | 120 | | } |
| | 5 | 121 | | |
| | 5 | 122 | | // Report unreachable edge-source nodes |
| | 3 | 123 | | if (!edgeSourceNodes.TryGetValue(graphName, out var nodeMap)) |
| | 5 | 124 | | continue; |
| | 5 | 125 | | |
| | 18 | 126 | | foreach (var nodeKvp in nodeMap) |
| | 5 | 127 | | { |
| | 6 | 128 | | if (!reachable.Contains(nodeKvp.Key)) |
| | 5 | 129 | | { |
| | 2 | 130 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 131 | | MafDiagnosticDescriptors.GraphUnreachableAgent, |
| | 2 | 132 | | nodeKvp.Value.Location, |
| | 2 | 133 | | nodeKvp.Value.Symbol.Name, |
| | 2 | 134 | | graphName)); |
| | 5 | 135 | | } |
| | 5 | 136 | | } |
| | 5 | 137 | | } |
| | 10 | 138 | | }); |
| | 14 | 139 | | }); |
| | 9 | 140 | | } |
| | | 141 | | } |