| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | |
| | | 3 | | using Microsoft.Agents.AI; |
| | | 4 | | using Microsoft.Extensions.AI; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Thread-safe accumulator for diagnostics captured during a single agent run. |
| | | 10 | | /// Implements <see cref="IDiagnosticsSink"/> so writers (middleware, loops) record |
| | | 11 | | /// through a stable interface rather than coupling to the concrete builder. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <para> |
| | | 15 | | /// Stored in an <see cref="AsyncLocal{T}"/> so middleware layers access the same |
| | | 16 | | /// builder instance within an async flow. Call <see cref="StartNew(string)"/> at the |
| | | 17 | | /// beginning of an agent run and dispose the returned builder when the run completes. |
| | | 18 | | /// </para> |
| | | 19 | | /// <para> |
| | | 20 | | /// Each event class has a single designated writer. Chat completions are written |
| | | 21 | | /// exclusively by <see cref="DiagnosticsChatClientMiddleware"/>. Tool calls are |
| | | 22 | | /// written exclusively by the <c>IterativeAgentLoop</c> (MEAI path) or |
| | | 23 | | /// <c>DiagnosticsFunctionCallingMiddleware</c> (MAF path). Two writers for the same |
| | | 24 | | /// event class is a bug. |
| | | 25 | | /// </para> |
| | | 26 | | /// <para> |
| | | 27 | | /// Sequence numbers are reserved BEFORE async work begins (via |
| | | 28 | | /// <see cref="Interlocked.Increment(ref int)"/>), ensuring parallel tool calls are |
| | | 29 | | /// ordered by invocation time, not completion time. |
| | | 30 | | /// </para> |
| | | 31 | | /// </remarks> |
| | | 32 | | public sealed class AgentRunDiagnosticsBuilder : IDiagnosticsSink, IDisposable |
| | | 33 | | { |
| | 1 | 34 | | private static readonly AsyncLocal<AgentRunDiagnosticsBuilder?> CurrentBuilder = new(); |
| | | 35 | | |
| | 306 | 36 | | private readonly ConcurrentQueue<ChatCompletionDiagnostics> _chatCompletions = new(); |
| | 306 | 37 | | private readonly ConcurrentQueue<ToolCallDiagnostics> _toolCalls = new(); |
| | | 38 | | private readonly AgentRunDiagnosticsBuilder? _previousBuilder; |
| | | 39 | | private readonly IReadOnlyList<IDiagnosticsSink>? _secondarySinks; |
| | | 40 | | |
| | | 41 | | private int _nextChatCompletionSequence; |
| | | 42 | | private int _nextToolCallSequence; |
| | | 43 | | |
| | | 44 | | private long _totalInputTokens; |
| | | 45 | | private long _totalOutputTokens; |
| | | 46 | | private long _totalTokens; |
| | | 47 | | private long _cachedInputTokens; |
| | | 48 | | private long _reasoningTokens; |
| | | 49 | | |
| | | 50 | | private int _totalInputMessages; |
| | | 51 | | private int _totalOutputMessages; |
| | 306 | 52 | | private bool _succeeded = true; |
| | | 53 | | private string? _errorMessage; |
| | | 54 | | private string? _executionMode; |
| | | 55 | | private IReadOnlyList<ChatMessage>? _inputMessages; |
| | | 56 | | private AgentResponse? _outputResponse; |
| | | 57 | | |
| | 824 | 58 | | public string AgentName { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the name of the parent (outer) agent when this builder was created |
| | | 62 | | /// inside a nested sub-agent run, or <see langword="null"/> if this is a |
| | | 63 | | /// top-level agent. |
| | | 64 | | /// </summary> |
| | 108 | 65 | | public string? ParentAgentName => _previousBuilder?.AgentName; |
| | | 66 | | |
| | 568 | 67 | | public DateTimeOffset StartedAt { get; } |
| | | 68 | | |
| | 306 | 69 | | private AgentRunDiagnosticsBuilder( |
| | 306 | 70 | | string agentName, |
| | 306 | 71 | | AgentRunDiagnosticsBuilder? previous, |
| | 306 | 72 | | IReadOnlyList<IDiagnosticsSink>? secondarySinks) |
| | | 73 | | { |
| | 306 | 74 | | AgentName = agentName; |
| | 306 | 75 | | StartedAt = DateTimeOffset.UtcNow; |
| | 306 | 76 | | _previousBuilder = previous; |
| | 306 | 77 | | _secondarySinks = secondarySinks; |
| | 306 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Creates a new builder and stores it in the current async flow so middleware can access it. |
| | | 82 | | /// If a builder already exists (nested sub-agent scenario), the previous builder is saved |
| | | 83 | | /// and restored when this builder is disposed. |
| | | 84 | | /// </summary> |
| | | 85 | | public static AgentRunDiagnosticsBuilder StartNew(string agentName) |
| | | 86 | | { |
| | 298 | 87 | | var previous = CurrentBuilder.Value; |
| | 298 | 88 | | var builder = new AgentRunDiagnosticsBuilder(agentName, previous, secondarySinks: null); |
| | 298 | 89 | | CurrentBuilder.Value = builder; |
| | 298 | 90 | | return builder; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Creates a new builder with secondary sinks that receive forwarded diagnostic |
| | | 95 | | /// records. The builder remains the primary accumulator; secondary sinks receive |
| | | 96 | | /// copies of each <see cref="AddChatCompletion"/> and <see cref="AddToolCall"/> |
| | | 97 | | /// call on a best-effort basis (sink failures are swallowed). |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="agentName">The name of the agent being traced.</param> |
| | | 100 | | /// <param name="secondarySinks">Additional sinks to fan out to, or <see langword="null"/>.</param> |
| | | 101 | | public static AgentRunDiagnosticsBuilder StartNew( |
| | | 102 | | string agentName, |
| | | 103 | | IReadOnlyList<IDiagnosticsSink>? secondarySinks) |
| | | 104 | | { |
| | 8 | 105 | | var previous = CurrentBuilder.Value; |
| | 8 | 106 | | var builder = new AgentRunDiagnosticsBuilder(agentName, previous, secondarySinks); |
| | 8 | 107 | | CurrentBuilder.Value = builder; |
| | 8 | 108 | | return builder; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary>Gets the builder for the current async flow, or <see langword="null"/> if outside a run.</summary> |
| | 320 | 112 | | public static AgentRunDiagnosticsBuilder? GetCurrent() => CurrentBuilder.Value; |
| | | 113 | | |
| | | 114 | | /// <summary>Reserves a sequence number for a tool call (thread-safe).</summary> |
| | | 115 | | public int NextToolCallSequence() => |
| | 166 | 116 | | Interlocked.Increment(ref _nextToolCallSequence) - 1; |
| | | 117 | | |
| | | 118 | | /// <summary>Reserves a sequence number for a chat completion (thread-safe).</summary> |
| | | 119 | | public int NextChatCompletionSequence() => |
| | 295 | 120 | | Interlocked.Increment(ref _nextChatCompletionSequence) - 1; |
| | | 121 | | |
| | | 122 | | public void AddChatCompletion(ChatCompletionDiagnostics diagnostics) |
| | | 123 | | { |
| | 312 | 124 | | _chatCompletions.Enqueue(diagnostics); |
| | | 125 | | |
| | 312 | 126 | | Interlocked.Add(ref _totalInputTokens, diagnostics.Tokens.InputTokens); |
| | 312 | 127 | | Interlocked.Add(ref _totalOutputTokens, diagnostics.Tokens.OutputTokens); |
| | 312 | 128 | | Interlocked.Add(ref _totalTokens, diagnostics.Tokens.TotalTokens); |
| | 312 | 129 | | Interlocked.Add(ref _cachedInputTokens, diagnostics.Tokens.CachedInputTokens); |
| | 312 | 130 | | Interlocked.Add(ref _reasoningTokens, diagnostics.Tokens.ReasoningTokens); |
| | | 131 | | |
| | 312 | 132 | | ForwardToSecondarySinks(diagnostics); |
| | 312 | 133 | | } |
| | | 134 | | |
| | | 135 | | public void AddToolCall(ToolCallDiagnostics diagnostics) |
| | | 136 | | { |
| | 177 | 137 | | _toolCalls.Enqueue(diagnostics); |
| | 177 | 138 | | ForwardToSecondarySinks(diagnostics); |
| | 177 | 139 | | } |
| | | 140 | | |
| | | 141 | | public void RecordInputMessageCount(int count) => |
| | 295 | 142 | | Interlocked.Add(ref _totalInputMessages, count); |
| | | 143 | | |
| | | 144 | | public void RecordOutputMessageCount(int count) => |
| | 288 | 145 | | Interlocked.Add(ref _totalOutputMessages, count); |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Records the full input messages supplied to the agent for this run. Captured |
| | | 149 | | /// once at the agent-run boundary; calling more than once replaces the snapshot. |
| | | 150 | | /// </summary> |
| | | 151 | | public void RecordInputMessages(IReadOnlyList<ChatMessage> messages) => |
| | 46 | 152 | | Volatile.Write(ref _inputMessages, messages); |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Records the aggregated output response produced by the agent for this run. |
| | | 156 | | /// Pass <see langword="null"/> when no response was produced. |
| | | 157 | | /// </summary> |
| | | 158 | | public void RecordOutputResponse(AgentResponse? response) => |
| | 46 | 159 | | Volatile.Write(ref _outputResponse, response); |
| | | 160 | | |
| | | 161 | | public void RecordFailure(string? errorMessage) |
| | | 162 | | { |
| | 27 | 163 | | _succeeded = false; |
| | 27 | 164 | | _errorMessage = errorMessage; |
| | 27 | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Sets the execution mode label for these diagnostics. |
| | | 169 | | /// Known values: <c>"FunctionInvokingChatClient"</c>, <c>"IterativeLoop"</c>. |
| | | 170 | | /// </summary> |
| | | 171 | | public void SetExecutionMode(string executionMode) => |
| | 125 | 172 | | _executionMode = executionMode; |
| | | 173 | | |
| | | 174 | | public IAgentRunDiagnostics Build() |
| | | 175 | | { |
| | 284 | 176 | | var completedAt = DateTimeOffset.UtcNow; |
| | | 177 | | |
| | 284 | 178 | | return new AgentRunDiagnostics( |
| | 284 | 179 | | AgentName: AgentName, |
| | 284 | 180 | | TotalDuration: completedAt - StartedAt, |
| | 284 | 181 | | AggregateTokenUsage: new TokenUsage( |
| | 284 | 182 | | InputTokens: Volatile.Read(ref _totalInputTokens), |
| | 284 | 183 | | OutputTokens: Volatile.Read(ref _totalOutputTokens), |
| | 284 | 184 | | TotalTokens: Volatile.Read(ref _totalTokens), |
| | 284 | 185 | | CachedInputTokens: Volatile.Read(ref _cachedInputTokens), |
| | 284 | 186 | | ReasoningTokens: Volatile.Read(ref _reasoningTokens)), |
| | 211 | 187 | | ChatCompletions: _chatCompletions.OrderBy(c => c.Sequence).ToArray(), |
| | 110 | 188 | | ToolCalls: _toolCalls.OrderBy(t => t.Sequence).ToArray(), |
| | 284 | 189 | | TotalInputMessages: Volatile.Read(ref _totalInputMessages), |
| | 284 | 190 | | TotalOutputMessages: Volatile.Read(ref _totalOutputMessages), |
| | 284 | 191 | | InputMessages: Volatile.Read(ref _inputMessages) ?? Array.Empty<ChatMessage>(), |
| | 284 | 192 | | OutputResponse: Volatile.Read(ref _outputResponse), |
| | 284 | 193 | | Succeeded: _succeeded, |
| | 284 | 194 | | ErrorMessage: _errorMessage, |
| | 284 | 195 | | StartedAt: StartedAt, |
| | 284 | 196 | | CompletedAt: completedAt, |
| | 284 | 197 | | ExecutionMode: _executionMode); |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary>Clears the builder from the current async flow.</summary> |
| | 1 | 201 | | public static void ClearCurrent() => CurrentBuilder.Value = null; |
| | | 202 | | |
| | | 203 | | /// <summary> |
| | | 204 | | /// Restores the previous builder (if any) to the current async flow. If this builder |
| | | 205 | | /// was created inside a nested sub-agent run, the outer agent's builder is restored. |
| | | 206 | | /// Otherwise equivalent to <see cref="ClearCurrent"/>. |
| | | 207 | | /// </summary> |
| | 307 | 208 | | public void Dispose() => CurrentBuilder.Value = _previousBuilder; |
| | | 209 | | |
| | | 210 | | private void ForwardToSecondarySinks(ChatCompletionDiagnostics diagnostics) |
| | | 211 | | { |
| | 312 | 212 | | if (_secondarySinks is not { Count: > 0 }) |
| | | 213 | | { |
| | 308 | 214 | | return; |
| | | 215 | | } |
| | | 216 | | |
| | 18 | 217 | | for (var i = 0; i < _secondarySinks.Count; i++) |
| | | 218 | | { |
| | | 219 | | try |
| | | 220 | | { |
| | 5 | 221 | | _secondarySinks[i].AddChatCompletion(diagnostics); |
| | 3 | 222 | | } |
| | 2 | 223 | | catch |
| | | 224 | | { |
| | | 225 | | // Best-effort: secondary sink failures must not break agent execution. |
| | 2 | 226 | | } |
| | | 227 | | } |
| | 4 | 228 | | } |
| | | 229 | | |
| | | 230 | | private void ForwardToSecondarySinks(ToolCallDiagnostics diagnostics) |
| | | 231 | | { |
| | 177 | 232 | | if (_secondarySinks is not { Count: > 0 }) |
| | | 233 | | { |
| | 173 | 234 | | return; |
| | | 235 | | } |
| | | 236 | | |
| | 20 | 237 | | for (var i = 0; i < _secondarySinks.Count; i++) |
| | | 238 | | { |
| | | 239 | | try |
| | | 240 | | { |
| | 6 | 241 | | _secondarySinks[i].AddToolCall(diagnostics); |
| | 4 | 242 | | } |
| | 2 | 243 | | catch |
| | | 244 | | { |
| | | 245 | | // Best-effort: secondary sink failures must not break agent execution. |
| | 2 | 246 | | } |
| | | 247 | | } |
| | 4 | 248 | | } |
| | | 249 | | } |