| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 5 | | using Microsoft.CodeAnalysis.Operations; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Detects <c>.ToString()</c> invocations on <c>ToolCallResult.Result</c> and |
| | | 11 | | /// <c>FunctionResultContent.Result</c> properties, which are <c>object?</c> |
| | | 12 | | /// and may contain a <c>JsonElement</c> at runtime. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// <b>NDLRMAF015</b> (Warning): Calling <c>ToString()</c> on these properties |
| | | 16 | | /// produces a C# type name for complex objects instead of JSON. Developers |
| | | 17 | | /// should use <c>ToolResultSerializer.Serialize()</c> instead. |
| | | 18 | | /// </remarks> |
| | | 19 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 20 | | public sealed class ToolResultToStringAnalyzer : DiagnosticAnalyzer |
| | | 21 | | { |
| | 1 | 22 | | private static readonly ImmutableHashSet<string> TargetTypeNames = ImmutableHashSet.Create( |
| | 1 | 23 | | "NexusLabs.Needlr.AgentFramework.Iterative.ToolCallResult", |
| | 1 | 24 | | "Microsoft.Extensions.AI.FunctionResultContent"); |
| | | 25 | | |
| | | 26 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 167 | 27 | | ImmutableArray.Create(MafDiagnosticDescriptors.ToolResultToStringCall); |
| | | 28 | | |
| | | 29 | | public override void Initialize(AnalysisContext context) |
| | | 30 | | { |
| | 17 | 31 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 17 | 32 | | context.EnableConcurrentExecution(); |
| | 17 | 33 | | context.RegisterOperationAction(AnalyzeInvocation, OperationKind.Invocation); |
| | 17 | 34 | | } |
| | | 35 | | |
| | | 36 | | private static void AnalyzeInvocation(OperationAnalysisContext context) |
| | | 37 | | { |
| | 9 | 38 | | var invocation = (IInvocationOperation)context.Operation; |
| | | 39 | | |
| | | 40 | | // We're looking for .ToString() calls |
| | 9 | 41 | | if (invocation.TargetMethod.Name != "ToString" || |
| | 9 | 42 | | invocation.TargetMethod.Parameters.Length != 0) |
| | | 43 | | { |
| | 0 | 44 | | return; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | // The receiver must be a property access to .Result |
| | | 48 | | // This handles both direct access (result.Result.ToString()) |
| | | 49 | | // and null-conditional access (result.Result?.ToString()) |
| | 9 | 50 | | IPropertyReferenceOperation? propertyRef = null; |
| | | 51 | | |
| | 9 | 52 | | if (invocation.Instance is IPropertyReferenceOperation directProp) |
| | | 53 | | { |
| | 7 | 54 | | propertyRef = directProp; |
| | | 55 | | } |
| | 2 | 56 | | else if (invocation.Instance is IConditionalAccessInstanceOperation) |
| | | 57 | | { |
| | | 58 | | // For result.Result?.ToString(), walk up to the ConditionalAccessOperation |
| | | 59 | | // and check its operand |
| | 2 | 60 | | var parent = invocation.Parent; |
| | 2 | 61 | | while (parent is not null and not IConditionalAccessOperation) |
| | | 62 | | { |
| | 0 | 63 | | parent = parent.Parent; |
| | | 64 | | } |
| | | 65 | | |
| | 2 | 66 | | if (parent is IConditionalAccessOperation conditional && |
| | 2 | 67 | | conditional.Operation is IPropertyReferenceOperation condProp) |
| | | 68 | | { |
| | 2 | 69 | | propertyRef = condProp; |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 9 | 73 | | if (propertyRef is null || propertyRef.Property.Name != "Result") |
| | | 74 | | { |
| | 2 | 75 | | return; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | // Check if the containing type is one of our targets |
| | 7 | 79 | | var containingType = propertyRef.Property.ContainingType?.ToDisplayString(); |
| | 7 | 80 | | if (containingType is null || !TargetTypeNames.Contains(containingType)) |
| | | 81 | | { |
| | 1 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 6 | 85 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 6 | 86 | | MafDiagnosticDescriptors.ToolResultToStringCall, |
| | 6 | 87 | | invocation.Syntax.GetLocation(), |
| | 6 | 88 | | containingType)); |
| | 6 | 89 | | } |
| | | 90 | | } |