| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Collections.Immutable; |
| | | 3 | | |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Analyzer that detects agent types that participate in no topology declaration. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <b>NDLRMAF008</b> (Info): A class decorated with <c>[NeedlrAiAgent]</c> is not referenced in any |
| | | 14 | | /// topology attribute (<c>[AgentHandoffsTo]</c>, <c>[AgentGroupChatMember]</c>, or |
| | | 15 | | /// <c>[AgentSequenceMember]</c>). This may indicate an orphaned or work-in-progress agent. |
| | | 16 | | /// </remarks> |
| | | 17 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 18 | | public sealed class AgentOrphanAnalyzer : DiagnosticAnalyzer |
| | | 19 | | { |
| | | 20 | | private const string NeedlrAiAgentAttributeName = "NexusLabs.Needlr.AgentFramework.NeedlrAiAgentAttribute"; |
| | | 21 | | private const string AgentHandoffsToAttributeName = "NexusLabs.Needlr.AgentFramework.AgentHandoffsToAttribute"; |
| | | 22 | | private const string AgentGroupChatMemberAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGroupChatMemberAttrib |
| | | 23 | | private const string AgentSequenceMemberAttributeName = "NexusLabs.Needlr.AgentFramework.AgentSequenceMemberAttribut |
| | | 24 | | private const string AgentGraphEdgeAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEdgeAttribute"; |
| | | 25 | | private const string AgentGraphEntryAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEntryAttribute"; |
| | | 26 | | private const string AgentGraphNodeAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphNodeAttribute"; |
| | | 27 | | private const string AgentGraphReducerAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphReducerAttribute"; |
| | | 28 | | |
| | | 29 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 185 | 30 | | ImmutableArray.Create(MafDiagnosticDescriptors.OrphanAgent); |
| | | 31 | | |
| | | 32 | | public override void Initialize(AnalysisContext context) |
| | | 33 | | { |
| | 27 | 34 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 27 | 35 | | context.EnableConcurrentExecution(); |
| | | 36 | | |
| | 27 | 37 | | context.RegisterCompilationStartAction(compilationContext => |
| | 27 | 38 | | { |
| | 15 | 39 | | var agents = new ConcurrentDictionary<string, (INamedTypeSymbol Symbol, Location Location)>(StringComparer.O |
| | 15 | 40 | | var topologyParticipants = new ConcurrentBag<string>(); |
| | 27 | 41 | | |
| | 15 | 42 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 15 | 43 | | { |
| | 181 | 44 | | var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| | 181 | 45 | | var fqn = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 15 | 46 | | |
| | 181 | 47 | | Location? agentAttrLocation = null; |
| | 181 | 48 | | bool inTopology = false; |
| | 15 | 49 | | |
| | 708 | 50 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | 15 | 51 | | { |
| | 173 | 52 | | var attrName = attr.AttributeClass?.ToDisplayString(); |
| | 15 | 53 | | |
| | 173 | 54 | | if (attrName == NeedlrAiAgentAttributeName) |
| | 15 | 55 | | { |
| | 25 | 56 | | agentAttrLocation = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 25 | 57 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 25 | 58 | | : typeSymbol.Locations[0]; |
| | 15 | 59 | | } |
| | 15 | 60 | | |
| | 173 | 61 | | if (attrName == AgentGroupChatMemberAttributeName || |
| | 173 | 62 | | attrName == AgentSequenceMemberAttributeName || |
| | 173 | 63 | | attrName == AgentGraphEntryAttributeName || |
| | 173 | 64 | | attrName == AgentGraphNodeAttributeName || |
| | 173 | 65 | | attrName == AgentGraphReducerAttributeName) |
| | 7 | 66 | | inTopology = true; |
| | 15 | 67 | | |
| | 173 | 68 | | if (attrName == AgentGraphEdgeAttributeName) |
| | 15 | 69 | | { |
| | 1 | 70 | | inTopology = true; |
| | 15 | 71 | | |
| | 15 | 72 | | // The edge target is also a topology participant |
| | 1 | 73 | | if (attr.ConstructorArguments.Length >= 2 |
| | 1 | 74 | | && attr.ConstructorArguments[1].Kind == TypedConstantKind.Type |
| | 1 | 75 | | && attr.ConstructorArguments[1].Value is INamedTypeSymbol edgeTargetType) |
| | 15 | 76 | | { |
| | 1 | 77 | | topologyParticipants.Add( |
| | 1 | 78 | | edgeTargetType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)); |
| | 15 | 79 | | } |
| | 15 | 80 | | } |
| | 15 | 81 | | |
| | 173 | 82 | | if (attrName == AgentHandoffsToAttributeName) |
| | 15 | 83 | | { |
| | 4 | 84 | | inTopology = true; |
| | 15 | 85 | | |
| | 15 | 86 | | // The handoff target is also a topology participant |
| | 4 | 87 | | if (attr.ConstructorArguments.Length >= 1 |
| | 4 | 88 | | && attr.ConstructorArguments[0].Kind == TypedConstantKind.Type |
| | 4 | 89 | | && attr.ConstructorArguments[0].Value is INamedTypeSymbol targetType) |
| | 15 | 90 | | { |
| | 4 | 91 | | topologyParticipants.Add( |
| | 4 | 92 | | targetType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)); |
| | 15 | 93 | | } |
| | 15 | 94 | | } |
| | 15 | 95 | | } |
| | 15 | 96 | | |
| | 181 | 97 | | if (agentAttrLocation is not null) |
| | 25 | 98 | | agents[fqn] = (typeSymbol, agentAttrLocation); |
| | 15 | 99 | | |
| | 181 | 100 | | if (inTopology) |
| | 12 | 101 | | topologyParticipants.Add(fqn); |
| | 196 | 102 | | }, SymbolKind.NamedType); |
| | 27 | 103 | | |
| | 15 | 104 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 15 | 105 | | { |
| | 15 | 106 | | var participantSet = new HashSet<string>(topologyParticipants, StringComparer.Ordinal); |
| | 15 | 107 | | |
| | 80 | 108 | | foreach (var kvp in agents) |
| | 15 | 109 | | { |
| | 25 | 110 | | if (!participantSet.Contains(kvp.Key)) |
| | 15 | 111 | | { |
| | 8 | 112 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 8 | 113 | | MafDiagnosticDescriptors.OrphanAgent, |
| | 8 | 114 | | kvp.Value.Location, |
| | 8 | 115 | | kvp.Value.Symbol.Name)); |
| | 15 | 116 | | } |
| | 15 | 117 | | } |
| | 30 | 118 | | }); |
| | 42 | 119 | | }); |
| | 27 | 120 | | } |
| | | 121 | | } |