| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Analyzer that validates <c>[AgentHandoffsTo]</c> topology declarations. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <para> |
| | | 13 | | /// <b>NDLRMAF001</b> (Error): The target type referenced by <c>[AgentHandoffsTo(typeof(X))]</c> is not |
| | | 14 | | /// decorated with <c>[NeedlrAiAgent]</c>. Handoff targets must be registered agent types. |
| | | 15 | | /// </para> |
| | | 16 | | /// <para> |
| | | 17 | | /// <b>NDLRMAF003</b> (Warning): The class carrying <c>[AgentHandoffsTo]</c> is not itself decorated with |
| | | 18 | | /// <c>[NeedlrAiAgent]</c>. The initial agent in a handoff workflow must be a declared agent. |
| | | 19 | | /// </para> |
| | | 20 | | /// </remarks> |
| | | 21 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 22 | | public sealed class AgentTopologyAnalyzer : DiagnosticAnalyzer |
| | | 23 | | { |
| | | 24 | | private const string AgentHandoffsToAttributeName = "NexusLabs.Needlr.AgentFramework.AgentHandoffsToAttribute"; |
| | | 25 | | private const string NeedlrAiAgentAttributeName = "NexusLabs.Needlr.AgentFramework.NeedlrAiAgentAttribute"; |
| | | 26 | | |
| | | 27 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 281 | 28 | | ImmutableArray.Create( |
| | 281 | 29 | | MafDiagnosticDescriptors.HandoffsToTargetNotNeedlrAgent, |
| | 281 | 30 | | MafDiagnosticDescriptors.HandoffsToSourceNotNeedlrAgent); |
| | | 31 | | |
| | | 32 | | public override void Initialize(AnalysisContext context) |
| | | 33 | | { |
| | 42 | 34 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 42 | 35 | | context.EnableConcurrentExecution(); |
| | | 36 | | |
| | 42 | 37 | | context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType); |
| | 42 | 38 | | } |
| | | 39 | | |
| | | 40 | | private static void AnalyzeNamedType(SymbolAnalysisContext context) |
| | | 41 | | { |
| | 353 | 42 | | var typeSymbol = (INamedTypeSymbol)context.Symbol; |
| | | 43 | | |
| | 353 | 44 | | var handoffsToAttrs = typeSymbol.GetAttributes() |
| | 332 | 45 | | .Where(a => a.AttributeClass?.ToDisplayString() == AgentHandoffsToAttributeName) |
| | 353 | 46 | | .ToImmutableArray(); |
| | | 47 | | |
| | 353 | 48 | | if (handoffsToAttrs.IsEmpty) |
| | 322 | 49 | | return; |
| | | 50 | | |
| | | 51 | | // NDLRMAF003: source class lacks [NeedlrAiAgent] |
| | 31 | 52 | | var hasNeedlrAiAgent = typeSymbol.GetAttributes() |
| | 62 | 53 | | .Any(a => a.AttributeClass?.ToDisplayString() == NeedlrAiAgentAttributeName); |
| | | 54 | | |
| | 31 | 55 | | if (!hasNeedlrAiAgent) |
| | | 56 | | { |
| | 10 | 57 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 10 | 58 | | MafDiagnosticDescriptors.HandoffsToSourceNotNeedlrAgent, |
| | 10 | 59 | | typeSymbol.Locations[0], |
| | 10 | 60 | | typeSymbol.Name)); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | // NDLRMAF001: each target type must have [NeedlrAiAgent] |
| | 128 | 64 | | foreach (var attr in handoffsToAttrs) |
| | | 65 | | { |
| | 33 | 66 | | if (attr.ConstructorArguments.Length < 1) |
| | | 67 | | continue; |
| | | 68 | | |
| | 33 | 69 | | var typeArg = attr.ConstructorArguments[0]; |
| | 33 | 70 | | if (typeArg.Kind != TypedConstantKind.Type || typeArg.Value is not INamedTypeSymbol targetType) |
| | | 71 | | continue; |
| | | 72 | | |
| | 33 | 73 | | var targetHasNeedlrAiAgent = targetType.GetAttributes() |
| | 54 | 74 | | .Any(a => a.AttributeClass?.ToDisplayString() == NeedlrAiAgentAttributeName); |
| | | 75 | | |
| | 33 | 76 | | if (!targetHasNeedlrAiAgent) |
| | | 77 | | { |
| | | 78 | | // Report on the attribute usage location when available, otherwise fall back to the class |
| | 12 | 79 | | var location = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 12 | 80 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 12 | 81 | | : typeSymbol.Locations[0]; |
| | | 82 | | |
| | 12 | 83 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 12 | 84 | | MafDiagnosticDescriptors.HandoffsToTargetNotNeedlrAgent, |
| | 12 | 85 | | location, |
| | 12 | 86 | | targetType.Name)); |
| | | 87 | | } |
| | | 88 | | } |
| | 31 | 89 | | } |
| | | 90 | | } |