| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.Linq; |
| | | 3 | | |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Analyzer that validates graph edge and entry point declarations reference declared agents. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <para> |
| | | 14 | | /// <b>NDLRMAF019</b> (Error): An <c>[AgentGraphEdge]</c> target type is not decorated with |
| | | 15 | | /// <c>[NeedlrAiAgent]</c>. |
| | | 16 | | /// </para> |
| | | 17 | | /// <para> |
| | | 18 | | /// <b>NDLRMAF020</b> (Warning): A class has <c>[AgentGraphEdge]</c> but is not itself decorated |
| | | 19 | | /// with <c>[NeedlrAiAgent]</c>. |
| | | 20 | | /// </para> |
| | | 21 | | /// <para> |
| | | 22 | | /// <b>NDLRMAF021</b> (Warning): A class has <c>[AgentGraphEntry]</c> but is not itself decorated |
| | | 23 | | /// with <c>[NeedlrAiAgent]</c>. |
| | | 24 | | /// </para> |
| | | 25 | | /// </remarks> |
| | | 26 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 27 | | public sealed class AgentGraphTopologyAnalyzer : DiagnosticAnalyzer |
| | | 28 | | { |
| | | 29 | | private const string AgentGraphEdgeAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEdgeAttribute"; |
| | | 30 | | private const string AgentGraphEntryAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEntryAttribute"; |
| | | 31 | | private const string NeedlrAiAgentAttributeName = "NexusLabs.Needlr.AgentFramework.NeedlrAiAgentAttribute"; |
| | | 32 | | |
| | | 33 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 185 | 34 | | ImmutableArray.Create( |
| | 185 | 35 | | MafDiagnosticDescriptors.GraphEdgeTargetNotAgent, |
| | 185 | 36 | | MafDiagnosticDescriptors.GraphEdgeSourceNotAgent, |
| | 185 | 37 | | MafDiagnosticDescriptors.GraphEntryPointNotAgent); |
| | | 38 | | |
| | | 39 | | public override void Initialize(AnalysisContext context) |
| | | 40 | | { |
| | 18 | 41 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 18 | 42 | | context.EnableConcurrentExecution(); |
| | | 43 | | |
| | 18 | 44 | | context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType); |
| | 18 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static void AnalyzeNamedType(SymbolAnalysisContext context) |
| | | 48 | | { |
| | 173 | 49 | | var typeSymbol = (INamedTypeSymbol)context.Symbol; |
| | 173 | 50 | | var attributes = typeSymbol.GetAttributes(); |
| | | 51 | | |
| | 173 | 52 | | var hasNeedlrAiAgent = attributes |
| | 319 | 53 | | .Any(a => a.AttributeClass?.ToDisplayString() == NeedlrAiAgentAttributeName); |
| | | 54 | | |
| | 173 | 55 | | var graphEdgeAttrs = attributes |
| | 150 | 56 | | .Where(a => a.AttributeClass?.ToDisplayString() == AgentGraphEdgeAttributeName) |
| | 173 | 57 | | .ToImmutableArray(); |
| | | 58 | | |
| | 173 | 59 | | var graphEntryAttrs = attributes |
| | 150 | 60 | | .Where(a => a.AttributeClass?.ToDisplayString() == AgentGraphEntryAttributeName) |
| | 173 | 61 | | .ToImmutableArray(); |
| | | 62 | | |
| | | 63 | | // NDLRMAF020: source class has [AgentGraphEdge] but no [NeedlrAiAgent] |
| | 173 | 64 | | if (!graphEdgeAttrs.IsEmpty && !hasNeedlrAiAgent) |
| | | 65 | | { |
| | 4 | 66 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 67 | | MafDiagnosticDescriptors.GraphEdgeSourceNotAgent, |
| | 4 | 68 | | typeSymbol.Locations[0], |
| | 4 | 69 | | typeSymbol.Name)); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | // NDLRMAF021: source class has [AgentGraphEntry] but no [NeedlrAiAgent] |
| | 173 | 73 | | if (!graphEntryAttrs.IsEmpty && !hasNeedlrAiAgent) |
| | | 74 | | { |
| | 2 | 75 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 76 | | MafDiagnosticDescriptors.GraphEntryPointNotAgent, |
| | 2 | 77 | | typeSymbol.Locations[0], |
| | 2 | 78 | | typeSymbol.Name)); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | // NDLRMAF019: each edge target must have [NeedlrAiAgent] |
| | 360 | 82 | | foreach (var attr in graphEdgeAttrs) |
| | | 83 | | { |
| | 7 | 84 | | if (attr.ConstructorArguments.Length < 2) |
| | | 85 | | continue; |
| | | 86 | | |
| | 7 | 87 | | var targetTypeArg = attr.ConstructorArguments[1]; |
| | 7 | 88 | | if (targetTypeArg.Kind != TypedConstantKind.Type || targetTypeArg.Value is not INamedTypeSymbol targetType) |
| | | 89 | | continue; |
| | | 90 | | |
| | 7 | 91 | | var targetHasNeedlrAiAgent = targetType.GetAttributes() |
| | 10 | 92 | | .Any(a => a.AttributeClass?.ToDisplayString() == NeedlrAiAgentAttributeName); |
| | | 93 | | |
| | 7 | 94 | | if (!targetHasNeedlrAiAgent) |
| | | 95 | | { |
| | 4 | 96 | | var location = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 4 | 97 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 4 | 98 | | : typeSymbol.Locations[0]; |
| | | 99 | | |
| | 4 | 100 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 101 | | MafDiagnosticDescriptors.GraphEdgeTargetNotAgent, |
| | 4 | 102 | | location, |
| | 4 | 103 | | targetType.Name)); |
| | | 104 | | } |
| | | 105 | | } |
| | 173 | 106 | | } |
| | | 107 | | } |