| | | 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 fan-out nodes where all outgoing edges are optional. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <b>NDLRMAF024</b> (Warning): All outgoing edges from a fan-out node have |
| | | 17 | | /// <c>IsRequired = false</c>. If all optional branches fail, the graph produces empty results. |
| | | 18 | | /// </remarks> |
| | | 19 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 20 | | public sealed class AgentGraphOptionalFanOutAnalyzer : DiagnosticAnalyzer |
| | | 21 | | { |
| | | 22 | | private const string AgentGraphEdgeAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphEdgeAttribute"; |
| | | 23 | | |
| | | 24 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 57 | 25 | | ImmutableArray.Create(MafDiagnosticDescriptors.GraphAllEdgesOptional); |
| | | 26 | | |
| | | 27 | | public override void Initialize(AnalysisContext context) |
| | | 28 | | { |
| | 11 | 29 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 11 | 30 | | context.EnableConcurrentExecution(); |
| | | 31 | | |
| | 11 | 32 | | context.RegisterCompilationStartAction(compilationContext => |
| | 11 | 33 | | { |
| | 11 | 34 | | // graphName → { sourceFqn → list of (isRequired, symbol, location) } |
| | 6 | 35 | | var edgeData = new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentBag<(bool IsRequired, |
| | 6 | 36 | | StringComparer.Ordinal); |
| | 11 | 37 | | |
| | 6 | 38 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 6 | 39 | | { |
| | 99 | 40 | | var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| | 99 | 41 | | var fqn = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 6 | 42 | | |
| | 390 | 43 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | 6 | 44 | | { |
| | 96 | 45 | | if (attr.AttributeClass?.ToDisplayString() != AgentGraphEdgeAttributeName) |
| | 6 | 46 | | continue; |
| | 6 | 47 | | |
| | 9 | 48 | | if (attr.ConstructorArguments.Length < 2) |
| | 6 | 49 | | continue; |
| | 6 | 50 | | |
| | 9 | 51 | | if (attr.ConstructorArguments[0].Value is not string graphName) |
| | 6 | 52 | | continue; |
| | 6 | 53 | | |
| | 6 | 54 | | // IsRequired defaults to true |
| | 9 | 55 | | var isRequired = true; |
| | 30 | 56 | | foreach (var namedArg in attr.NamedArguments) |
| | 6 | 57 | | { |
| | 6 | 58 | | if (namedArg.Key == "IsRequired" && namedArg.Value.Value is bool val) |
| | 6 | 59 | | { |
| | 6 | 60 | | isRequired = val; |
| | 6 | 61 | | } |
| | 6 | 62 | | } |
| | 6 | 63 | | |
| | 9 | 64 | | var location = typeSymbol.Locations[0]; |
| | 6 | 65 | | |
| | 14 | 66 | | var perGraph = edgeData.GetOrAdd(graphName, _ => new ConcurrentDictionary<string, ConcurrentBag<(boo |
| | 14 | 67 | | perGraph.GetOrAdd(fqn, _ => new ConcurrentBag<(bool, INamedTypeSymbol, Location)>()) |
| | 9 | 68 | | .Add((isRequired, typeSymbol, location)); |
| | 6 | 69 | | } |
| | 105 | 70 | | }, SymbolKind.NamedType); |
| | 11 | 71 | | |
| | 6 | 72 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 6 | 73 | | { |
| | 22 | 74 | | foreach (var graphKvp in edgeData) |
| | 6 | 75 | | { |
| | 5 | 76 | | var graphName = graphKvp.Key; |
| | 6 | 77 | | |
| | 20 | 78 | | foreach (var sourceKvp in graphKvp.Value) |
| | 6 | 79 | | { |
| | 5 | 80 | | var edges = sourceKvp.Value.ToList(); |
| | 6 | 81 | | |
| | 6 | 82 | | // Only flag fan-out (2+ outgoing edges) where all are optional |
| | 5 | 83 | | if (edges.Count < 2) |
| | 6 | 84 | | continue; |
| | 6 | 85 | | |
| | 11 | 86 | | if (edges.Any(e => e.IsRequired)) |
| | 6 | 87 | | continue; |
| | 6 | 88 | | |
| | 6 | 89 | | // All edges are optional |
| | 2 | 90 | | var symbol = edges[0].Symbol; |
| | 2 | 91 | | var location = edges[0].Location; |
| | 6 | 92 | | |
| | 2 | 93 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 94 | | MafDiagnosticDescriptors.GraphAllEdgesOptional, |
| | 2 | 95 | | location, |
| | 2 | 96 | | edges.Count, |
| | 2 | 97 | | symbol.Name, |
| | 2 | 98 | | graphName)); |
| | 6 | 99 | | } |
| | 6 | 100 | | } |
| | 12 | 101 | | }); |
| | 17 | 102 | | }); |
| | 11 | 103 | | } |
| | | 104 | | } |