| | | 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 | | |
| | | 25 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 165 | 26 | | ImmutableArray.Create(MafDiagnosticDescriptors.OrphanAgent); |
| | | 27 | | |
| | | 28 | | public override void Initialize(AnalysisContext context) |
| | | 29 | | { |
| | 19 | 30 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 19 | 31 | | context.EnableConcurrentExecution(); |
| | | 32 | | |
| | 19 | 33 | | context.RegisterCompilationStartAction(compilationContext => |
| | 19 | 34 | | { |
| | 11 | 35 | | var agents = new ConcurrentDictionary<string, (INamedTypeSymbol Symbol, Location Location)>(StringComparer.O |
| | 11 | 36 | | var topologyParticipants = new ConcurrentBag<string>(); |
| | 19 | 37 | | |
| | 11 | 38 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 11 | 39 | | { |
| | 120 | 40 | | var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| | 120 | 41 | | var fqn = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 11 | 42 | | |
| | 120 | 43 | | Location? agentAttrLocation = null; |
| | 120 | 44 | | bool inTopology = false; |
| | 11 | 45 | | |
| | 472 | 46 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | 11 | 47 | | { |
| | 116 | 48 | | var attrName = attr.AttributeClass?.ToDisplayString(); |
| | 11 | 49 | | |
| | 116 | 50 | | if (attrName == NeedlrAiAgentAttributeName) |
| | 11 | 51 | | { |
| | 20 | 52 | | agentAttrLocation = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 20 | 53 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 20 | 54 | | : typeSymbol.Locations[0]; |
| | 11 | 55 | | } |
| | 11 | 56 | | |
| | 116 | 57 | | if (attrName == AgentGroupChatMemberAttributeName || attrName == AgentSequenceMemberAttributeName) |
| | 4 | 58 | | inTopology = true; |
| | 11 | 59 | | |
| | 116 | 60 | | if (attrName == AgentHandoffsToAttributeName) |
| | 11 | 61 | | { |
| | 4 | 62 | | inTopology = true; |
| | 11 | 63 | | |
| | 11 | 64 | | // The handoff target is also a topology participant |
| | 4 | 65 | | if (attr.ConstructorArguments.Length >= 1 |
| | 4 | 66 | | && attr.ConstructorArguments[0].Kind == TypedConstantKind.Type |
| | 4 | 67 | | && attr.ConstructorArguments[0].Value is INamedTypeSymbol targetType) |
| | 11 | 68 | | { |
| | 4 | 69 | | topologyParticipants.Add( |
| | 4 | 70 | | targetType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)); |
| | 11 | 71 | | } |
| | 11 | 72 | | } |
| | 11 | 73 | | } |
| | 11 | 74 | | |
| | 120 | 75 | | if (agentAttrLocation is not null) |
| | 20 | 76 | | agents[fqn] = (typeSymbol, agentAttrLocation); |
| | 11 | 77 | | |
| | 120 | 78 | | if (inTopology) |
| | 8 | 79 | | topologyParticipants.Add(fqn); |
| | 131 | 80 | | }, SymbolKind.NamedType); |
| | 19 | 81 | | |
| | 11 | 82 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 11 | 83 | | { |
| | 11 | 84 | | var participantSet = new HashSet<string>(topologyParticipants, StringComparer.Ordinal); |
| | 11 | 85 | | |
| | 62 | 86 | | foreach (var kvp in agents) |
| | 11 | 87 | | { |
| | 20 | 88 | | if (!participantSet.Contains(kvp.Key)) |
| | 11 | 89 | | { |
| | 8 | 90 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 8 | 91 | | MafDiagnosticDescriptors.OrphanAgent, |
| | 8 | 92 | | kvp.Value.Location, |
| | 8 | 93 | | kvp.Value.Symbol.Name)); |
| | 11 | 94 | | } |
| | 11 | 95 | | } |
| | 22 | 96 | | }); |
| | 30 | 97 | | }); |
| | 19 | 98 | | } |
| | | 99 | | } |