| | | 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 detects terminal nodes that have outgoing edges. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <b>NDLRMAF027</b> (Error): A node marked as terminal via |
| | | 17 | | /// <c>[AgentGraphNode(IsTerminal = true)]</c> also has <c>[AgentGraphEdge]</c> declarations. |
| | | 18 | | /// Terminal nodes must not have outgoing edges. |
| | | 19 | | /// </remarks> |
| | | 20 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 21 | | public sealed class AgentGraphTerminalNodeAnalyzer : DiagnosticAnalyzer |
| | | 22 | | { |
| | | 23 | | private const string AgentGraphEdgeAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEdgeAttribute"; |
| | | 24 | | private const string AgentGraphNodeAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphNodeAttribute"; |
| | | 25 | | |
| | | 26 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 57 | 27 | | ImmutableArray.Create(MafDiagnosticDescriptors.GraphTerminalNodeHasOutgoingEdges); |
| | | 28 | | |
| | | 29 | | public override void Initialize(AnalysisContext context) |
| | | 30 | | { |
| | 11 | 31 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 11 | 32 | | context.EnableConcurrentExecution(); |
| | | 33 | | |
| | 11 | 34 | | context.RegisterCompilationStartAction(compilationContext => |
| | 11 | 35 | | { |
| | 11 | 36 | | // Collect per-type: which graph names have IsTerminal=true, and which have outgoing edges |
| | 11 | 37 | | // fqn → { graphName → (isTerminal, hasEdges, symbol, terminalLocation) } |
| | 6 | 38 | | var nodeData = new ConcurrentDictionary<string, ConcurrentDictionary<string, (bool IsTerminal, Location Term |
| | 6 | 39 | | StringComparer.Ordinal); |
| | 11 | 40 | | |
| | 6 | 41 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 6 | 42 | | { |
| | 94 | 43 | | var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| | 94 | 44 | | var fqn = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 6 | 45 | | |
| | 6 | 46 | | // Track terminal markers per graph |
| | 94 | 47 | | var terminalGraphs = new Dictionary<string, Location>(StringComparer.Ordinal); |
| | 94 | 48 | | var edgeGraphs = new HashSet<string>(StringComparer.Ordinal); |
| | 6 | 49 | | |
| | 370 | 50 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | 6 | 51 | | { |
| | 91 | 52 | | var attrName = attr.AttributeClass?.ToDisplayString(); |
| | 6 | 53 | | |
| | 91 | 54 | | if (attrName == AgentGraphNodeAttributeName) |
| | 6 | 55 | | { |
| | 5 | 56 | | if (attr.ConstructorArguments.Length < 1) |
| | 6 | 57 | | continue; |
| | 6 | 58 | | |
| | 5 | 59 | | if (attr.ConstructorArguments[0].Value is not string graphName) |
| | 6 | 60 | | continue; |
| | 6 | 61 | | |
| | 5 | 62 | | var isTerminal = false; |
| | 18 | 63 | | foreach (var namedArg in attr.NamedArguments) |
| | 6 | 64 | | { |
| | 4 | 65 | | if (namedArg.Key == "IsTerminal" && namedArg.Value.Value is bool val) |
| | 6 | 66 | | { |
| | 4 | 67 | | isTerminal = val; |
| | 6 | 68 | | } |
| | 6 | 69 | | } |
| | 6 | 70 | | |
| | 5 | 71 | | if (isTerminal) |
| | 6 | 72 | | { |
| | 4 | 73 | | var location = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 4 | 74 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 4 | 75 | | : typeSymbol.Locations[0]; |
| | 6 | 76 | | |
| | 4 | 77 | | terminalGraphs[graphName] = location; |
| | 6 | 78 | | } |
| | 6 | 79 | | } |
| | 6 | 80 | | |
| | 91 | 81 | | if (attrName == AgentGraphEdgeAttributeName) |
| | 6 | 82 | | { |
| | 4 | 83 | | if (attr.ConstructorArguments.Length < 1) |
| | 6 | 84 | | continue; |
| | 6 | 85 | | |
| | 4 | 86 | | if (attr.ConstructorArguments[0].Value is string graphName) |
| | 6 | 87 | | { |
| | 4 | 88 | | edgeGraphs.Add(graphName); |
| | 6 | 89 | | } |
| | 6 | 90 | | } |
| | 6 | 91 | | } |
| | 6 | 92 | | |
| | 6 | 93 | | // Check for conflicts |
| | 196 | 94 | | foreach (var kvp in terminalGraphs) |
| | 6 | 95 | | { |
| | 4 | 96 | | if (edgeGraphs.Contains(kvp.Key)) |
| | 6 | 97 | | { |
| | 4 | 98 | | var perNode = nodeData.GetOrAdd(fqn, _ => new ConcurrentDictionary<string, (bool, Location, bool |
| | 2 | 99 | | perNode[kvp.Key] = (true, kvp.Value, true, typeSymbol); |
| | 6 | 100 | | } |
| | 6 | 101 | | } |
| | 100 | 102 | | }, SymbolKind.NamedType); |
| | 11 | 103 | | |
| | 6 | 104 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 6 | 105 | | { |
| | 16 | 106 | | foreach (var nodeKvp in nodeData) |
| | 6 | 107 | | { |
| | 8 | 108 | | foreach (var graphKvp in nodeKvp.Value) |
| | 6 | 109 | | { |
| | 2 | 110 | | if (graphKvp.Value.IsTerminal && graphKvp.Value.HasEdges) |
| | 6 | 111 | | { |
| | 2 | 112 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 113 | | MafDiagnosticDescriptors.GraphTerminalNodeHasOutgoingEdges, |
| | 2 | 114 | | graphKvp.Value.TerminalLocation, |
| | 2 | 115 | | graphKvp.Value.Symbol.Name, |
| | 2 | 116 | | graphKvp.Key)); |
| | 6 | 117 | | } |
| | 6 | 118 | | } |
| | 6 | 119 | | } |
| | 12 | 120 | | }); |
| | 17 | 121 | | }); |
| | 11 | 122 | | } |
| | | 123 | | } |