| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Collections.Immutable; |
| | | 3 | | using System.Linq; |
| | | 4 | | |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 7 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 8 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 9 | | |
| | | 10 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Detects calls to <c>CreateGraphWorkflow</c> (which returns a MAF <c>Workflow</c> |
| | | 14 | | /// using BSP execution) when the compilation also declares |
| | | 15 | | /// <c>[AgentGraphNode(JoinMode = GraphJoinMode.WaitAny)]</c> for any graph. |
| | | 16 | | /// </summary> |
| | | 17 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 18 | | public sealed class WaitAnyCreateGraphAnalyzer : DiagnosticAnalyzer |
| | | 19 | | { |
| | | 20 | | private const string AgentGraphNodeAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGraphNodeAttribute"; |
| | | 21 | | private const string CreateGraphWorkflowMethodName = "CreateGraphWorkflow"; |
| | | 22 | | |
| | | 23 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 69 | 24 | | ImmutableArray.Create(MafDiagnosticDescriptors.WaitAnyIncompatibleWithCreateGraphWorkflow); |
| | | 25 | | |
| | | 26 | | public override void Initialize(AnalysisContext context) |
| | | 27 | | { |
| | 11 | 28 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 11 | 29 | | context.EnableConcurrentExecution(); |
| | | 30 | | |
| | 11 | 31 | | context.RegisterCompilationStartAction(compilationContext => |
| | 11 | 32 | | { |
| | 6 | 33 | | var waitAnyGraphNames = new ConcurrentBag<string>(); |
| | 6 | 34 | | var createGraphCalls = new ConcurrentBag<(Location Location, string GraphName)>(); |
| | 11 | 35 | | |
| | 6 | 36 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 6 | 37 | | { |
| | 111 | 38 | | var namedType = (INamedTypeSymbol)symbolContext.Symbol; |
| | 432 | 39 | | foreach (var attr in namedType.GetAttributes()) |
| | 6 | 40 | | { |
| | 105 | 41 | | if (attr.AttributeClass?.ToDisplayString() != AgentGraphNodeAttributeName) |
| | 6 | 42 | | continue; |
| | 6 | 43 | | |
| | 5 | 44 | | string? graphName = null; |
| | 5 | 45 | | if (attr.ConstructorArguments.Length >= 1 && |
| | 5 | 46 | | attr.ConstructorArguments[0].Value is string gn) |
| | 6 | 47 | | { |
| | 5 | 48 | | graphName = gn; |
| | 6 | 49 | | } |
| | 6 | 50 | | |
| | 20 | 51 | | foreach (var named in attr.NamedArguments) |
| | 6 | 52 | | { |
| | 5 | 53 | | if (named.Key == "JoinMode" && |
| | 5 | 54 | | named.Value.Value is int joinModeValue && |
| | 5 | 55 | | joinModeValue == 1) |
| | 6 | 56 | | { |
| | 4 | 57 | | if (graphName is not null) |
| | 6 | 58 | | { |
| | 4 | 59 | | waitAnyGraphNames.Add(graphName); |
| | 6 | 60 | | } |
| | 6 | 61 | | } |
| | 6 | 62 | | } |
| | 6 | 63 | | } |
| | 117 | 64 | | }, SymbolKind.NamedType); |
| | 11 | 65 | | |
| | 6 | 66 | | compilationContext.RegisterSyntaxNodeAction(syntaxContext => |
| | 6 | 67 | | { |
| | 18 | 68 | | var invocation = (InvocationExpressionSyntax)syntaxContext.Node; |
| | 6 | 69 | | |
| | 18 | 70 | | string? methodName = null; |
| | 18 | 71 | | if (invocation.Expression is MemberAccessExpressionSyntax memberAccess) |
| | 6 | 72 | | { |
| | 18 | 73 | | methodName = memberAccess.Name.Identifier.Text; |
| | 6 | 74 | | } |
| | 0 | 75 | | else if (invocation.Expression is IdentifierNameSyntax identifier) |
| | 6 | 76 | | { |
| | 0 | 77 | | methodName = identifier.Identifier.Text; |
| | 6 | 78 | | } |
| | 6 | 79 | | |
| | 18 | 80 | | if (methodName != CreateGraphWorkflowMethodName) |
| | 12 | 81 | | return; |
| | 6 | 82 | | |
| | 6 | 83 | | var symbolInfo = syntaxContext.SemanticModel.GetSymbolInfo(invocation, syntaxContext.CancellationToken); |
| | 6 | 84 | | if (symbolInfo.Symbol is not IMethodSymbol methodSymbol) |
| | 0 | 85 | | return; |
| | 6 | 86 | | |
| | 6 | 87 | | var containingType = methodSymbol.ContainingType; |
| | 6 | 88 | | if (containingType?.Name != "IWorkflowFactory" && |
| | 6 | 89 | | containingType?.ToDisplayString() != "NexusLabs.Needlr.AgentFramework.IWorkflowFactory") |
| | 1 | 90 | | return; |
| | 6 | 91 | | |
| | 5 | 92 | | if (invocation.ArgumentList.Arguments.Count < 1) |
| | 0 | 93 | | return; |
| | 6 | 94 | | |
| | 5 | 95 | | var firstArg = invocation.ArgumentList.Arguments[0].Expression; |
| | 5 | 96 | | var constantValue = syntaxContext.SemanticModel.GetConstantValue(firstArg); |
| | 5 | 97 | | if (constantValue.HasValue && constantValue.Value is string graphName) |
| | 6 | 98 | | { |
| | 5 | 99 | | createGraphCalls.Add((invocation.GetLocation(), graphName)); |
| | 6 | 100 | | } |
| | 11 | 101 | | }, SyntaxKind.InvocationExpression); |
| | 11 | 102 | | |
| | 6 | 103 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 6 | 104 | | { |
| | 6 | 105 | | var waitAnyNames = new System.Collections.Generic.HashSet<string>(waitAnyGraphNames); |
| | 22 | 106 | | foreach (var (location, graphName) in createGraphCalls) |
| | 6 | 107 | | { |
| | 5 | 108 | | if (waitAnyNames.Contains(graphName)) |
| | 6 | 109 | | { |
| | 2 | 110 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 111 | | MafDiagnosticDescriptors.WaitAnyIncompatibleWithCreateGraphWorkflow, |
| | 2 | 112 | | location, |
| | 2 | 113 | | graphName)); |
| | 6 | 114 | | } |
| | 6 | 115 | | } |
| | 12 | 116 | | }); |
| | 17 | 117 | | }); |
| | 11 | 118 | | } |
| | | 119 | | } |