| | | 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 validates entry point declarations for agent graphs. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <para> |
| | | 17 | | /// <b>NDLRMAF017</b> (Error): A named graph has edges but no <c>[AgentGraphEntry]</c>. |
| | | 18 | | /// </para> |
| | | 19 | | /// <para> |
| | | 20 | | /// <b>NDLRMAF018</b> (Error): A named graph has multiple <c>[AgentGraphEntry]</c> declarations. |
| | | 21 | | /// </para> |
| | | 22 | | /// </remarks> |
| | | 23 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 24 | | public sealed class AgentGraphEntryPointAnalyzer : DiagnosticAnalyzer |
| | | 25 | | { |
| | | 26 | | private const string AgentGraphEdgeAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEdgeAttribute"; |
| | | 27 | | private const string AgentGraphEntryAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEntryAttribute"; |
| | | 28 | | |
| | | 29 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 113 | 30 | | ImmutableArray.Create( |
| | 113 | 31 | | MafDiagnosticDescriptors.GraphNoEntryPoint, |
| | 113 | 32 | | MafDiagnosticDescriptors.GraphMultipleEntryPoints); |
| | | 33 | | |
| | | 34 | | public override void Initialize(AnalysisContext context) |
| | | 35 | | { |
| | 12 | 36 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 12 | 37 | | context.EnableConcurrentExecution(); |
| | | 38 | | |
| | 12 | 39 | | context.RegisterCompilationStartAction(compilationContext => |
| | 12 | 40 | | { |
| | 12 | 41 | | // graphName → list of (typeName, location) |
| | 7 | 42 | | var entryPoints = new ConcurrentDictionary<string, ConcurrentBag<(string TypeName, Location Location)>>(Stri |
| | 12 | 43 | | // graphName → set of edge attribute locations (for reporting NDLRMAF017) |
| | 7 | 44 | | var edgeGraphNames = new ConcurrentDictionary<string, ConcurrentBag<Location>>(StringComparer.Ordinal); |
| | 12 | 45 | | |
| | 7 | 46 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 7 | 47 | | { |
| | 114 | 48 | | var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| | 7 | 49 | | |
| | 460 | 50 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | 7 | 51 | | { |
| | 116 | 52 | | var attrName = attr.AttributeClass?.ToDisplayString(); |
| | 7 | 53 | | |
| | 116 | 54 | | if (attrName == AgentGraphEntryAttributeName) |
| | 7 | 55 | | { |
| | 7 | 56 | | if (attr.ConstructorArguments.Length < 1) |
| | 7 | 57 | | continue; |
| | 7 | 58 | | |
| | 7 | 59 | | if (attr.ConstructorArguments[0].Value is not string graphName) |
| | 7 | 60 | | continue; |
| | 7 | 61 | | |
| | 7 | 62 | | var location = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 7 | 63 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 7 | 64 | | : typeSymbol.Locations[0]; |
| | 7 | 65 | | |
| | 12 | 66 | | entryPoints.GetOrAdd(graphName, _ => new ConcurrentBag<(string, Location)>()) |
| | 7 | 67 | | .Add((typeSymbol.Name, location)); |
| | 7 | 68 | | } |
| | 7 | 69 | | |
| | 116 | 70 | | if (attrName == AgentGraphEdgeAttributeName) |
| | 7 | 71 | | { |
| | 9 | 72 | | if (attr.ConstructorArguments.Length < 1) |
| | 7 | 73 | | continue; |
| | 7 | 74 | | |
| | 9 | 75 | | if (attr.ConstructorArguments[0].Value is not string graphName) |
| | 7 | 76 | | continue; |
| | 7 | 77 | | |
| | 9 | 78 | | var location = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 9 | 79 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 9 | 80 | | : typeSymbol.Locations[0]; |
| | 7 | 81 | | |
| | 16 | 82 | | edgeGraphNames.GetOrAdd(graphName, _ => new ConcurrentBag<Location>()) |
| | 9 | 83 | | .Add(location); |
| | 7 | 84 | | } |
| | 7 | 85 | | } |
| | 121 | 86 | | }, SymbolKind.NamedType); |
| | 12 | 87 | | |
| | 7 | 88 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 7 | 89 | | { |
| | 7 | 90 | | // Collect all graph names (union of entry and edge declarations) |
| | 7 | 91 | | var allGraphNames = new HashSet<string>(edgeGraphNames.Keys, StringComparer.Ordinal); |
| | 7 | 92 | | |
| | 28 | 93 | | foreach (var graphName in allGraphNames) |
| | 7 | 94 | | { |
| | 7 | 95 | | if (!entryPoints.TryGetValue(graphName, out var entries) || entries.Count == 0) |
| | 7 | 96 | | { |
| | 7 | 97 | | // NDLRMAF017: edges exist but no entry point |
| | 2 | 98 | | if (edgeGraphNames.TryGetValue(graphName, out var edgeLocations)) |
| | 7 | 99 | | { |
| | 2 | 100 | | var firstEdgeLocation = edgeLocations.First(); |
| | 2 | 101 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 102 | | MafDiagnosticDescriptors.GraphNoEntryPoint, |
| | 2 | 103 | | firstEdgeLocation, |
| | 2 | 104 | | graphName)); |
| | 7 | 105 | | } |
| | 7 | 106 | | } |
| | 5 | 107 | | else if (entries.Count > 1) |
| | 7 | 108 | | { |
| | 7 | 109 | | // NDLRMAF018: multiple entry points |
| | 2 | 110 | | var entryList = entries.ToList(); |
| | 6 | 111 | | var typeNames = string.Join(", ", entryList.Select(e => e.TypeName)); |
| | 12 | 112 | | foreach (var entry in entryList) |
| | 7 | 113 | | { |
| | 4 | 114 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 115 | | MafDiagnosticDescriptors.GraphMultipleEntryPoints, |
| | 4 | 116 | | entry.Location, |
| | 4 | 117 | | graphName, |
| | 4 | 118 | | typeNames)); |
| | 7 | 119 | | } |
| | 7 | 120 | | } |
| | 7 | 121 | | } |
| | 7 | 122 | | |
| | 7 | 123 | | // Also check entry-only graphs (entry declared but no edges) for multiple entries |
| | 24 | 124 | | foreach (var graphName in entryPoints.Keys) |
| | 7 | 125 | | { |
| | 5 | 126 | | if (allGraphNames.Contains(graphName)) |
| | 7 | 127 | | continue; |
| | 7 | 128 | | |
| | 0 | 129 | | if (entryPoints.TryGetValue(graphName, out var entries) && entries.Count > 1) |
| | 7 | 130 | | { |
| | 0 | 131 | | var entryList = entries.ToList(); |
| | 0 | 132 | | var typeNames = string.Join(", ", entryList.Select(e => e.TypeName)); |
| | 0 | 133 | | foreach (var entry in entryList) |
| | 7 | 134 | | { |
| | 0 | 135 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 136 | | MafDiagnosticDescriptors.GraphMultipleEntryPoints, |
| | 0 | 137 | | entry.Location, |
| | 0 | 138 | | graphName, |
| | 0 | 139 | | typeNames)); |
| | 7 | 140 | | } |
| | 7 | 141 | | } |
| | 7 | 142 | | } |
| | 14 | 143 | | }); |
| | 19 | 144 | | }); |
| | 12 | 145 | | } |
| | | 146 | | } |