| | | 1 | | using System.Diagnostics; |
| | | 2 | | |
| | | 3 | | using Microsoft.Extensions.AI; |
| | | 4 | | |
| | | 5 | | using NexusLabs.Needlr.AgentFramework.Budget; |
| | | 6 | | using NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 7 | | using NexusLabs.Needlr.AgentFramework.Progress; |
| | | 8 | | using NexusLabs.Needlr.AgentFramework.Workspace; |
| | | 9 | | |
| | | 10 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Sequential; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Executes a linear sequence of <see cref="PipelineStage"/> instances, |
| | | 14 | | /// evaluating policies (skip, retry, budget) and producing an |
| | | 15 | | /// <see cref="IPipelineRunResult"/> with per-stage diagnostics. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// <para> |
| | | 19 | | /// This runner is a peer of <see cref="GraphWorkflowRunner"/> for linear pipelines. |
| | | 20 | | /// It supports hybrid agent/programmatic stages via <see cref="IStageExecutor"/>, |
| | | 21 | | /// conditional skipping, post-validation with retries, per-stage and overall token |
| | | 22 | | /// budgets, and structured progress reporting. |
| | | 23 | | /// </para> |
| | | 24 | | /// </remarks> |
| | | 25 | | /// <example> |
| | | 26 | | /// <code> |
| | | 27 | | /// var runner = new SequentialPipelineRunner(diagnosticsAccessor, budgetTracker, progressFactory, pipelineMetrics); |
| | | 28 | | /// var stages = new[] |
| | | 29 | | /// { |
| | | 30 | | /// new PipelineStage("Writer", new AgentStageExecutor(writerAgent, ctx => "Write a draft.")), |
| | | 31 | | /// new PipelineStage("Editor", new AgentStageExecutor(editorAgent, ctx => "Edit the draft.")), |
| | | 32 | | /// }; |
| | | 33 | | /// var result = await runner.RunAsync(workspace, stages, options: null, cancellationToken); |
| | | 34 | | /// </code> |
| | | 35 | | /// </example> |
| | | 36 | | [DoNotAutoRegister] |
| | | 37 | | public sealed class SequentialPipelineRunner |
| | | 38 | | { |
| | | 39 | | private readonly IAgentDiagnosticsAccessor _diagnosticsAccessor; |
| | | 40 | | private readonly ITokenBudgetTracker _budgetTracker; |
| | | 41 | | private readonly IProgressReporterFactory _progressReporterFactory; |
| | | 42 | | private readonly IPipelineMetrics _pipelineMetrics; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new <see cref="SequentialPipelineRunner"/>. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="diagnosticsAccessor">Accessor for capturing per-stage agent diagnostics.</param> |
| | | 48 | | /// <param name="budgetTracker">Token budget tracker for scoping per-stage and pipeline-level budgets.</param> |
| | | 49 | | /// <param name="progressReporterFactory">Factory for creating progress reporters.</param> |
| | | 50 | | /// <param name="pipelineMetrics"> |
| | | 51 | | /// Pipeline-shape metrics sink used to emit per-pipeline and per-stage instruments |
| | | 52 | | /// + spans. Resolved from DI; defaults to <see cref="NoOpPipelineMetrics"/> when no |
| | | 53 | | /// <see cref="PipelineMetricsOptions"/> was configured via |
| | | 54 | | /// <c>ConfigurePipelineMetrics</c> on the agent-framework syringe — observability |
| | | 55 | | /// is opt-in with zero overhead by default. |
| | | 56 | | /// </param> |
| | 81 | 57 | | public SequentialPipelineRunner( |
| | 81 | 58 | | IAgentDiagnosticsAccessor diagnosticsAccessor, |
| | 81 | 59 | | ITokenBudgetTracker budgetTracker, |
| | 81 | 60 | | IProgressReporterFactory progressReporterFactory, |
| | 81 | 61 | | IPipelineMetrics pipelineMetrics) |
| | | 62 | | { |
| | 81 | 63 | | _diagnosticsAccessor = diagnosticsAccessor; |
| | 81 | 64 | | _budgetTracker = budgetTracker; |
| | 81 | 65 | | _progressReporterFactory = progressReporterFactory; |
| | 81 | 66 | | _pipelineMetrics = pipelineMetrics; |
| | 81 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Runs all pipeline stages sequentially, applying policies and collecting results. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="workspace">The shared workspace for file I/O across stages.</param> |
| | | 73 | | /// <param name="stages">The ordered list of stages to execute.</param> |
| | | 74 | | /// <param name="options">Optional pipeline-level configuration.</param> |
| | | 75 | | /// <param name="cancellationToken">Token to observe for cancellation.</param> |
| | | 76 | | /// <returns>An <see cref="IPipelineRunResult"/> describing the pipeline outcome.</returns> |
| | | 77 | | public Task<IPipelineRunResult> RunAsync( |
| | | 78 | | IWorkspace workspace, |
| | | 79 | | IReadOnlyList<PipelineStage> stages, |
| | | 80 | | SequentialPipelineOptions? options, |
| | | 81 | | CancellationToken cancellationToken) => |
| | 45 | 82 | | RunCoreAsync(workspace, stages, pipelineState: null, options, cancellationToken); |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Runs all pipeline stages sequentially with a shared typed state object, |
| | | 86 | | /// applying policies and collecting results. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <typeparam name="TState">The type of the shared pipeline state.</typeparam> |
| | | 89 | | /// <param name="workspace">The shared workspace for file I/O across stages.</param> |
| | | 90 | | /// <param name="stages">The ordered list of stages to execute.</param> |
| | | 91 | | /// <param name="state">A shared state object accessible to all stages via |
| | | 92 | | /// <see cref="StageExecutionContext.GetRequiredState{T}"/>.</param> |
| | | 93 | | /// <param name="options">Optional pipeline-level configuration.</param> |
| | | 94 | | /// <param name="cancellationToken">Token to observe for cancellation.</param> |
| | | 95 | | /// <returns>An <see cref="IPipelineRunResult"/> describing the pipeline outcome.</returns> |
| | | 96 | | public Task<IPipelineRunResult> RunAsync<TState>( |
| | | 97 | | IWorkspace workspace, |
| | | 98 | | IReadOnlyList<PipelineStage> stages, |
| | | 99 | | TState state, |
| | | 100 | | SequentialPipelineOptions? options, |
| | | 101 | | CancellationToken cancellationToken) where TState : class => |
| | 2 | 102 | | RunCoreAsync(workspace, stages, state, options, cancellationToken); |
| | | 103 | | |
| | | 104 | | private async Task<IPipelineRunResult> RunCoreAsync( |
| | | 105 | | IWorkspace workspace, |
| | | 106 | | IReadOnlyList<PipelineStage> stages, |
| | | 107 | | object? pipelineState, |
| | | 108 | | SequentialPipelineOptions? options, |
| | | 109 | | CancellationToken cancellationToken) |
| | | 110 | | { |
| | 47 | 111 | | var stopwatch = Stopwatch.StartNew(); |
| | 47 | 112 | | var reporter = _progressReporterFactory.Create(Guid.NewGuid().ToString("N")); |
| | 47 | 113 | | var stageResults = new List<IAgentStageResult>(); |
| | 47 | 114 | | var pipelineName = ResolvePipelineName(options, reporter); |
| | | 115 | | |
| | 47 | 116 | | reporter.Report(new WorkflowStartedEvent( |
| | 47 | 117 | | DateTimeOffset.UtcNow, |
| | 47 | 118 | | reporter.WorkflowId, |
| | 47 | 119 | | reporter.AgentId, |
| | 47 | 120 | | ParentAgentId: null, |
| | 47 | 121 | | reporter.Depth, |
| | 47 | 122 | | reporter.NextSequence())); |
| | | 123 | | |
| | 47 | 124 | | var pipelineActivity = StartPipelineScope(pipelineName); |
| | 47 | 125 | | IDisposable? pipelineBudgetScope = null; |
| | | 126 | | try |
| | | 127 | | { |
| | 47 | 128 | | if (options?.TotalTokenBudget is { } totalBudget) |
| | | 129 | | { |
| | 0 | 130 | | pipelineBudgetScope = _budgetTracker.BeginScope(totalBudget); |
| | | 131 | | } |
| | | 132 | | |
| | 198 | 133 | | for (var i = 0; i < stages.Count; i++) |
| | | 134 | | { |
| | 65 | 135 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 136 | | |
| | 64 | 137 | | var stage = stages[i]; |
| | 64 | 138 | | var policy = stage.Policy; |
| | | 139 | | |
| | 64 | 140 | | var context = new StageExecutionContext( |
| | 64 | 141 | | workspace, |
| | 64 | 142 | | _diagnosticsAccessor, |
| | 64 | 143 | | reporter, |
| | 64 | 144 | | StageIndex: i, |
| | 64 | 145 | | TotalStages: stages.Count, |
| | 64 | 146 | | StageName: stage.Name, |
| | 64 | 147 | | CallerCancellationToken: cancellationToken, |
| | 64 | 148 | | PipelineState: pipelineState); |
| | | 149 | | |
| | | 150 | | // Evaluate ShouldSkip |
| | 64 | 151 | | if (policy?.ShouldSkip?.Invoke(context) == true) |
| | | 152 | | { |
| | 5 | 153 | | var skipResult = new AgentStageResult( |
| | 5 | 154 | | stage.Name, |
| | 5 | 155 | | FinalResponse: null, |
| | 5 | 156 | | Diagnostics: null, |
| | 5 | 157 | | Outcome: StageOutcome.Skipped, |
| | 5 | 158 | | Termination: new StageTermination.Skipped()); |
| | 5 | 159 | | stageResults.Add(skipResult); |
| | 5 | 160 | | _pipelineMetrics.RecordStageCompleted(pipelineName, skipResult, TimeSpan.Zero); |
| | 5 | 161 | | continue; |
| | | 162 | | } |
| | | 163 | | |
| | 59 | 164 | | var (stageStopwatch, stageActivity) = StartStageScope(pipelineName, stage.Name, phaseName: null); |
| | | 165 | | |
| | 59 | 166 | | reporter.Report(new AgentInvokedEvent( |
| | 59 | 167 | | DateTimeOffset.UtcNow, |
| | 59 | 168 | | reporter.WorkflowId, |
| | 59 | 169 | | stage.Name, |
| | 59 | 170 | | ParentAgentId: null, |
| | 59 | 171 | | reporter.Depth, |
| | 59 | 172 | | reporter.NextSequence(), |
| | 59 | 173 | | stage.Name)); |
| | | 174 | | |
| | 59 | 175 | | var maxAttempts = policy?.MaxAttempts ?? 1; |
| | 59 | 176 | | StageExecutionResult? stageResult = null; |
| | 59 | 177 | | string? validationError = null; |
| | | 178 | | |
| | 59 | 179 | | IDisposable? stageBudgetScope = null; |
| | | 180 | | try |
| | | 181 | | { |
| | 59 | 182 | | if (policy?.TokenBudget is { } stageBudget) |
| | | 183 | | { |
| | 0 | 184 | | stageBudgetScope = _budgetTracker.BeginChildScope(stage.Name, stageBudget); |
| | | 185 | | } |
| | | 186 | | |
| | 126 | 187 | | for (var attempt = 0; attempt < maxAttempts; attempt++) |
| | | 188 | | { |
| | 62 | 189 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 62 | 190 | | stageResult = await stage.Executor.ExecuteAsync(context, cancellationToken); |
| | | 191 | | |
| | 56 | 192 | | if (policy?.PostValidation is { } validate) |
| | | 193 | | { |
| | 5 | 194 | | validationError = validate(stageResult); |
| | 5 | 195 | | if (validationError is null) |
| | | 196 | | { |
| | | 197 | | break; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | // Last attempt failed — will throw after loop |
| | 4 | 201 | | if (attempt < maxAttempts - 1) |
| | | 202 | | { |
| | 3 | 203 | | validationError = null; |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | else |
| | | 207 | | { |
| | | 208 | | break; |
| | | 209 | | } |
| | | 210 | | } |
| | 53 | 211 | | } |
| | 6 | 212 | | catch (Exception ex) |
| | | 213 | | { |
| | | 214 | | // Always record the failed stage so it appears in diagnostics. |
| | | 215 | | // Capture any partial diagnostics the stage may have produced. |
| | 6 | 216 | | var partialDiag = _diagnosticsAccessor.LastRunDiagnostics; |
| | 6 | 217 | | var failedStageResult = new AgentStageResult( |
| | 6 | 218 | | stage.Name, |
| | 6 | 219 | | FinalResponse: null, |
| | 6 | 220 | | Diagnostics: partialDiag, |
| | 6 | 221 | | Outcome: StageOutcome.Failed, |
| | 6 | 222 | | Termination: new StageTermination.Failed(ex)); |
| | 6 | 223 | | stageResults.Add(failedStageResult); |
| | 6 | 224 | | EmitStageMetricsAndDisposeActivity(pipelineName, failedStageResult, stageStopwatch, stageActivity); |
| | | 225 | | |
| | 6 | 226 | | reporter.Report(new AgentFailedEvent( |
| | 6 | 227 | | DateTimeOffset.UtcNow, |
| | 6 | 228 | | reporter.WorkflowId, |
| | 6 | 229 | | stage.Name, |
| | 6 | 230 | | ParentAgentId: null, |
| | 6 | 231 | | reporter.Depth, |
| | 6 | 232 | | reporter.NextSequence(), |
| | 6 | 233 | | AgentName: stage.Name, |
| | 6 | 234 | | ErrorMessage: ex.Message)); |
| | | 235 | | |
| | 6 | 236 | | throw; |
| | | 237 | | } |
| | | 238 | | finally |
| | | 239 | | { |
| | 59 | 240 | | stageBudgetScope?.Dispose(); |
| | | 241 | | } |
| | | 242 | | |
| | 53 | 243 | | if (stageResult is not null && policy?.AfterExecution is { } afterExec) |
| | | 244 | | { |
| | 3 | 245 | | await afterExec(stageResult, context); |
| | | 246 | | } |
| | | 247 | | |
| | 53 | 248 | | if (validationError is not null) |
| | | 249 | | { |
| | 1 | 250 | | throw new StageValidationException(stage.Name, validationError); |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | // Handle explicit failure results from the stage executor. |
| | 52 | 254 | | if (!stageResult!.Succeeded) |
| | | 255 | | { |
| | 9 | 256 | | var failedExecResult = new AgentStageResult( |
| | 9 | 257 | | stage.Name, |
| | 9 | 258 | | FinalResponse: null, |
| | 9 | 259 | | Diagnostics: stageResult.Diagnostics, |
| | 9 | 260 | | Outcome: StageOutcome.Failed, |
| | 9 | 261 | | Termination: stageResult.Termination); |
| | 9 | 262 | | stageResults.Add(failedExecResult); |
| | 9 | 263 | | EmitStageMetricsAndDisposeActivity(pipelineName, failedExecResult, stageStopwatch, stageActivity); |
| | | 264 | | |
| | 9 | 265 | | reporter.Report(new AgentFailedEvent( |
| | 9 | 266 | | DateTimeOffset.UtcNow, |
| | 9 | 267 | | reporter.WorkflowId, |
| | 9 | 268 | | stage.Name, |
| | 9 | 269 | | ParentAgentId: null, |
| | 9 | 270 | | reporter.Depth, |
| | 9 | 271 | | reporter.NextSequence(), |
| | 9 | 272 | | AgentName: stage.Name, |
| | 9 | 273 | | ErrorMessage: stageResult.Exception?.Message ?? "Stage failed")); |
| | | 274 | | |
| | 9 | 275 | | if (stageResult.FailureDisposition == FailureDisposition.AbortPipeline) |
| | | 276 | | { |
| | 5 | 277 | | stopwatch.Stop(); |
| | 5 | 278 | | var errorMsg = stageResult.Exception?.Message |
| | 5 | 279 | | ?? $"Stage '{stage.Name}' failed"; |
| | 5 | 280 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, stopwatch.Elapsed, succeeded: |
| | 5 | 281 | | return new PipelineRunResult( |
| | 5 | 282 | | stageResults, |
| | 5 | 283 | | stopwatch.Elapsed, |
| | 5 | 284 | | succeeded: false, |
| | 5 | 285 | | errorMessage: errorMsg, |
| | 5 | 286 | | exception: stageResult.Exception, |
| | 5 | 287 | | plannedStageCount: stages.Count); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | // ContinueAdvisory — proceed to the next stage. |
| | | 291 | | continue; |
| | | 292 | | } |
| | | 293 | | |
| | 43 | 294 | | ChatResponse? chatResponse = stageResult!.ResponseText is not null |
| | 43 | 295 | | ? new ChatResponse(new ChatMessage(ChatRole.Assistant, stageResult.ResponseText)) |
| | 43 | 296 | | : null; |
| | | 297 | | |
| | 43 | 298 | | var successResult = new AgentStageResult( |
| | 43 | 299 | | stage.Name, |
| | 43 | 300 | | chatResponse, |
| | 43 | 301 | | stageResult.Diagnostics, |
| | 43 | 302 | | Termination: stageResult.Termination); |
| | 43 | 303 | | stageResults.Add(successResult); |
| | 43 | 304 | | EmitStageMetricsAndDisposeActivity(pipelineName, successResult, stageStopwatch, stageActivity); |
| | | 305 | | |
| | 43 | 306 | | reporter.Report(new AgentCompletedEvent( |
| | 43 | 307 | | DateTimeOffset.UtcNow, |
| | 43 | 308 | | reporter.WorkflowId, |
| | 43 | 309 | | stage.Name, |
| | 43 | 310 | | ParentAgentId: null, |
| | 43 | 311 | | reporter.Depth, |
| | 43 | 312 | | reporter.NextSequence(), |
| | 43 | 313 | | stage.Name, |
| | 43 | 314 | | Duration: stopwatch.Elapsed, |
| | 43 | 315 | | TotalTokens: stageResult.Diagnostics?.AggregateTokenUsage.TotalTokens ?? 0)); |
| | 43 | 316 | | } |
| | | 317 | | |
| | 34 | 318 | | stopwatch.Stop(); |
| | | 319 | | |
| | 34 | 320 | | var pipelineResult = new PipelineRunResult( |
| | 34 | 321 | | stageResults, |
| | 34 | 322 | | stopwatch.Elapsed, |
| | 34 | 323 | | succeeded: true, |
| | 34 | 324 | | errorMessage: null, |
| | 34 | 325 | | plannedStageCount: stages.Count); |
| | | 326 | | |
| | 34 | 327 | | if (options?.CompletionGate is { } gate) |
| | | 328 | | { |
| | 1 | 329 | | var gateError = gate(pipelineResult); |
| | 1 | 330 | | if (gateError is not null) |
| | | 331 | | { |
| | 1 | 332 | | var failedResult = new PipelineRunResult( |
| | 1 | 333 | | stageResults, |
| | 1 | 334 | | stopwatch.Elapsed, |
| | 1 | 335 | | succeeded: false, |
| | 1 | 336 | | errorMessage: gateError, |
| | 1 | 337 | | plannedStageCount: stages.Count); |
| | | 338 | | |
| | 1 | 339 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, stopwatch.Elapsed, succeeded: fal |
| | 1 | 340 | | return failedResult; |
| | | 341 | | } |
| | | 342 | | } |
| | | 343 | | |
| | 33 | 344 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, stopwatch.Elapsed, succeeded: true, error |
| | 33 | 345 | | return pipelineResult; |
| | | 346 | | } |
| | 4 | 347 | | catch (OperationCanceledException ex) when (ex.InnerException is TokenBudgetExceededException budgetEx) |
| | | 348 | | { |
| | 1 | 349 | | stopwatch.Stop(); |
| | 1 | 350 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, stopwatch.Elapsed, succeeded: false, budg |
| | 1 | 351 | | return new PipelineRunResult( |
| | 1 | 352 | | stageResults, |
| | 1 | 353 | | stopwatch.Elapsed, |
| | 1 | 354 | | succeeded: false, |
| | 1 | 355 | | errorMessage: budgetEx.Message, |
| | 1 | 356 | | exception: budgetEx, |
| | 1 | 357 | | plannedStageCount: stages.Count); |
| | | 358 | | } |
| | 3 | 359 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 360 | | { |
| | 1 | 361 | | stopwatch.Stop(); |
| | 1 | 362 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, stopwatch.Elapsed, succeeded: false, "Can |
| | 1 | 363 | | throw; |
| | | 364 | | } |
| | 2 | 365 | | catch (OperationCanceledException ex) |
| | | 366 | | { |
| | | 367 | | // HTTP timeouts and other non-user cancellations — treat as stage failure, |
| | | 368 | | // not as user cancellation. HttpClient.Timeout throws TaskCanceledException |
| | | 369 | | // which is OperationCanceledException, but the caller's token is NOT cancelled. |
| | 2 | 370 | | stopwatch.Stop(); |
| | 2 | 371 | | var message = ex.InnerException is TimeoutException |
| | 2 | 372 | | ? $"Stage timed out: {ex.InnerException.Message}" |
| | 2 | 373 | | : $"Operation cancelled (not by caller): {ex.Message}"; |
| | 2 | 374 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, stopwatch.Elapsed, succeeded: false, mess |
| | 2 | 375 | | return new PipelineRunResult( |
| | 2 | 376 | | stageResults, |
| | 2 | 377 | | stopwatch.Elapsed, |
| | 2 | 378 | | succeeded: false, |
| | 2 | 379 | | errorMessage: message, |
| | 2 | 380 | | exception: ex, |
| | 2 | 381 | | plannedStageCount: stages.Count); |
| | | 382 | | } |
| | 4 | 383 | | catch (Exception ex) |
| | | 384 | | { |
| | 4 | 385 | | stopwatch.Stop(); |
| | 4 | 386 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, stopwatch.Elapsed, succeeded: false, ex.M |
| | 4 | 387 | | return new PipelineRunResult( |
| | 4 | 388 | | stageResults, |
| | 4 | 389 | | stopwatch.Elapsed, |
| | 4 | 390 | | succeeded: false, |
| | 4 | 391 | | errorMessage: ex.Message, |
| | 4 | 392 | | exception: ex, |
| | 4 | 393 | | plannedStageCount: stages.Count); |
| | | 394 | | } |
| | | 395 | | finally |
| | | 396 | | { |
| | 47 | 397 | | pipelineBudgetScope?.Dispose(); |
| | | 398 | | } |
| | 46 | 399 | | } |
| | | 400 | | |
| | | 401 | | /// <summary> |
| | | 402 | | /// Runs a phased pipeline where stages are grouped into named phases with |
| | | 403 | | /// lifecycle hooks and optional phase-level token budgets. |
| | | 404 | | /// </summary> |
| | | 405 | | /// <param name="workspace">The shared workspace for file I/O across stages.</param> |
| | | 406 | | /// <param name="phases">The ordered list of phases, each containing stages.</param> |
| | | 407 | | /// <param name="options">Optional pipeline-level configuration.</param> |
| | | 408 | | /// <param name="cancellationToken">Token to observe for cancellation.</param> |
| | | 409 | | /// <returns>An <see cref="IPipelineRunResult"/> describing the pipeline outcome.</returns> |
| | | 410 | | public Task<IPipelineRunResult> RunPhasedAsync( |
| | | 411 | | IWorkspace workspace, |
| | | 412 | | IReadOnlyList<PipelinePhase> phases, |
| | | 413 | | SequentialPipelineOptions? options, |
| | | 414 | | CancellationToken cancellationToken) => |
| | 31 | 415 | | RunPhasedCoreAsync(workspace, phases, pipelineState: null, options, cancellationToken); |
| | | 416 | | |
| | | 417 | | /// <summary> |
| | | 418 | | /// Runs a phased pipeline with a shared typed state object accessible to both |
| | | 419 | | /// phase lifecycle hooks and stage executors. |
| | | 420 | | /// </summary> |
| | | 421 | | /// <typeparam name="TState">The type of the shared pipeline state.</typeparam> |
| | | 422 | | /// <param name="workspace">The shared workspace for file I/O across stages.</param> |
| | | 423 | | /// <param name="phases">The ordered list of phases, each containing stages.</param> |
| | | 424 | | /// <param name="state">A shared state object accessible via |
| | | 425 | | /// <see cref="PhaseContext.GetRequiredState{T}"/> and |
| | | 426 | | /// <see cref="StageExecutionContext.GetRequiredState{T}"/>.</param> |
| | | 427 | | /// <param name="options">Optional pipeline-level configuration.</param> |
| | | 428 | | /// <param name="cancellationToken">Token to observe for cancellation.</param> |
| | | 429 | | /// <returns>An <see cref="IPipelineRunResult"/> describing the pipeline outcome.</returns> |
| | | 430 | | public Task<IPipelineRunResult> RunPhasedAsync<TState>( |
| | | 431 | | IWorkspace workspace, |
| | | 432 | | IReadOnlyList<PipelinePhase> phases, |
| | | 433 | | TState state, |
| | | 434 | | SequentialPipelineOptions? options, |
| | | 435 | | CancellationToken cancellationToken) where TState : class => |
| | 1 | 436 | | RunPhasedCoreAsync(workspace, phases, state, options, cancellationToken); |
| | | 437 | | |
| | | 438 | | private async Task<IPipelineRunResult> RunPhasedCoreAsync( |
| | | 439 | | IWorkspace workspace, |
| | | 440 | | IReadOnlyList<PipelinePhase> phases, |
| | | 441 | | object? pipelineState, |
| | | 442 | | SequentialPipelineOptions? options, |
| | | 443 | | CancellationToken cancellationToken) |
| | | 444 | | { |
| | 32 | 445 | | var pipelineStopwatch = Stopwatch.StartNew(); |
| | 32 | 446 | | var reporter = _progressReporterFactory.Create(Guid.NewGuid().ToString("N")); |
| | 32 | 447 | | var allStageResults = new List<IAgentStageResult>(); |
| | 80 | 448 | | var totalStages = phases.Sum(p => p.Stages.Count); |
| | 32 | 449 | | var globalStageIndex = 0; |
| | 32 | 450 | | var pipelineName = ResolvePipelineName(options, reporter); |
| | | 451 | | |
| | 32 | 452 | | reporter.Report(new WorkflowStartedEvent( |
| | 32 | 453 | | DateTimeOffset.UtcNow, |
| | 32 | 454 | | reporter.WorkflowId, |
| | 32 | 455 | | reporter.AgentId, |
| | 32 | 456 | | ParentAgentId: null, |
| | 32 | 457 | | reporter.Depth, |
| | 32 | 458 | | reporter.NextSequence())); |
| | | 459 | | |
| | 32 | 460 | | var pipelineActivity = StartPipelineScope(pipelineName); |
| | 32 | 461 | | IDisposable? pipelineBudgetScope = null; |
| | | 462 | | try |
| | | 463 | | { |
| | 32 | 464 | | if (options?.TotalTokenBudget is { } totalBudget) |
| | | 465 | | { |
| | 1 | 466 | | pipelineBudgetScope = _budgetTracker.BeginScope(totalBudget); |
| | | 467 | | } |
| | | 468 | | |
| | 154 | 469 | | for (var phaseIndex = 0; phaseIndex < phases.Count; phaseIndex++) |
| | | 470 | | { |
| | 48 | 471 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 472 | | |
| | 48 | 473 | | var phase = phases[phaseIndex]; |
| | 48 | 474 | | var phasePolicy = phase.Policy; |
| | 48 | 475 | | var phaseStopwatch = Stopwatch.StartNew(); |
| | 48 | 476 | | var phaseSucceeded = true; |
| | | 477 | | |
| | 48 | 478 | | reporter.Report(new PhaseStartedEvent( |
| | 48 | 479 | | DateTimeOffset.UtcNow, |
| | 48 | 480 | | reporter.WorkflowId, |
| | 48 | 481 | | reporter.AgentId, |
| | 48 | 482 | | ParentAgentId: null, |
| | 48 | 483 | | reporter.Depth, |
| | 48 | 484 | | reporter.NextSequence(), |
| | 48 | 485 | | phase.Name, |
| | 48 | 486 | | phaseIndex, |
| | 48 | 487 | | phases.Count, |
| | 48 | 488 | | phase.Stages.Count)); |
| | | 489 | | |
| | 48 | 490 | | var phaseContext = new PhaseContext( |
| | 48 | 491 | | phase.Name, |
| | 48 | 492 | | phaseIndex, |
| | 48 | 493 | | phases.Count, |
| | 48 | 494 | | workspace, |
| | 48 | 495 | | pipelineState); |
| | | 496 | | |
| | 48 | 497 | | IDisposable? phaseBudgetScope = null; |
| | | 498 | | try |
| | | 499 | | { |
| | 48 | 500 | | if (phasePolicy?.TokenBudget is { } phaseBudget) |
| | | 501 | | { |
| | 3 | 502 | | phaseBudgetScope = _budgetTracker.BeginScope(phaseBudget); |
| | | 503 | | } |
| | | 504 | | |
| | 48 | 505 | | if (phasePolicy?.OnEnterAsync is { } onEnter) |
| | | 506 | | { |
| | 10 | 507 | | await onEnter(phaseContext, cancellationToken); |
| | | 508 | | } |
| | | 509 | | |
| | 202 | 510 | | for (var stageInPhase = 0; stageInPhase < phase.Stages.Count; stageInPhase++) |
| | | 511 | | { |
| | 56 | 512 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 513 | | |
| | 56 | 514 | | var stage = phase.Stages[stageInPhase]; |
| | 56 | 515 | | var policy = stage.Policy; |
| | | 516 | | |
| | 56 | 517 | | var context = new StageExecutionContext( |
| | 56 | 518 | | workspace, |
| | 56 | 519 | | _diagnosticsAccessor, |
| | 56 | 520 | | reporter, |
| | 56 | 521 | | StageIndex: globalStageIndex, |
| | 56 | 522 | | TotalStages: totalStages, |
| | 56 | 523 | | StageName: stage.Name, |
| | 56 | 524 | | CallerCancellationToken: cancellationToken, |
| | 56 | 525 | | PipelineState: pipelineState, |
| | 56 | 526 | | PhaseName: phase.Name, |
| | 56 | 527 | | PhaseIndex: phaseIndex, |
| | 56 | 528 | | StageIndexInPhase: stageInPhase, |
| | 56 | 529 | | TotalStagesInPhase: phase.Stages.Count); |
| | | 530 | | |
| | 56 | 531 | | if (policy?.ShouldSkip?.Invoke(context) == true) |
| | | 532 | | { |
| | 3 | 533 | | var skipResult = new AgentStageResult( |
| | 3 | 534 | | stage.Name, |
| | 3 | 535 | | FinalResponse: null, |
| | 3 | 536 | | Diagnostics: null, |
| | 3 | 537 | | Outcome: StageOutcome.Skipped, |
| | 3 | 538 | | PhaseName: phase.Name, |
| | 3 | 539 | | Termination: new StageTermination.Skipped()); |
| | 3 | 540 | | allStageResults.Add(skipResult); |
| | 3 | 541 | | _pipelineMetrics.RecordStageCompleted(pipelineName, skipResult, TimeSpan.Zero); |
| | 3 | 542 | | globalStageIndex++; |
| | 3 | 543 | | continue; |
| | | 544 | | } |
| | | 545 | | |
| | 53 | 546 | | var (stageStopwatch, stageActivity) = StartStageScope(pipelineName, stage.Name, phase.Name); |
| | | 547 | | |
| | 53 | 548 | | reporter.Report(new AgentInvokedEvent( |
| | 53 | 549 | | DateTimeOffset.UtcNow, |
| | 53 | 550 | | reporter.WorkflowId, |
| | 53 | 551 | | stage.Name, |
| | 53 | 552 | | ParentAgentId: null, |
| | 53 | 553 | | reporter.Depth, |
| | 53 | 554 | | reporter.NextSequence(), |
| | 53 | 555 | | stage.Name)); |
| | | 556 | | |
| | 53 | 557 | | var maxAttempts = policy?.MaxAttempts ?? 1; |
| | 53 | 558 | | StageExecutionResult? stageResult = null; |
| | 53 | 559 | | string? validationError = null; |
| | | 560 | | |
| | 53 | 561 | | IDisposable? stageBudgetScope = null; |
| | | 562 | | try |
| | | 563 | | { |
| | 53 | 564 | | if (policy?.TokenBudget is { } stageBudget) |
| | | 565 | | { |
| | 1 | 566 | | stageBudgetScope = _budgetTracker.BeginChildScope(stage.Name, stageBudget); |
| | | 567 | | } |
| | | 568 | | |
| | 106 | 569 | | for (var attempt = 0; attempt < maxAttempts; attempt++) |
| | | 570 | | { |
| | 53 | 571 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 53 | 572 | | stageResult = await stage.Executor.ExecuteAsync(context, cancellationToken); |
| | | 573 | | |
| | 51 | 574 | | if (policy?.PostValidation is { } validate) |
| | | 575 | | { |
| | 0 | 576 | | validationError = validate(stageResult); |
| | 0 | 577 | | if (validationError is null) |
| | | 578 | | { |
| | | 579 | | break; |
| | | 580 | | } |
| | | 581 | | |
| | 0 | 582 | | if (attempt < maxAttempts - 1) |
| | | 583 | | { |
| | 0 | 584 | | validationError = null; |
| | | 585 | | } |
| | | 586 | | } |
| | | 587 | | else |
| | | 588 | | { |
| | | 589 | | break; |
| | | 590 | | } |
| | | 591 | | } |
| | 51 | 592 | | } |
| | 2 | 593 | | catch (Exception ex) |
| | | 594 | | { |
| | 2 | 595 | | var partialDiag = _diagnosticsAccessor.LastRunDiagnostics; |
| | 2 | 596 | | var failedPhasedResult = new AgentStageResult( |
| | 2 | 597 | | stage.Name, |
| | 2 | 598 | | FinalResponse: null, |
| | 2 | 599 | | Diagnostics: partialDiag, |
| | 2 | 600 | | Outcome: StageOutcome.Failed, |
| | 2 | 601 | | PhaseName: phase.Name, |
| | 2 | 602 | | Termination: new StageTermination.Failed(ex)); |
| | 2 | 603 | | allStageResults.Add(failedPhasedResult); |
| | 2 | 604 | | EmitStageMetricsAndDisposeActivity(pipelineName, failedPhasedResult, stageStopwatch, stageAc |
| | | 605 | | |
| | 2 | 606 | | reporter.Report(new AgentFailedEvent( |
| | 2 | 607 | | DateTimeOffset.UtcNow, |
| | 2 | 608 | | reporter.WorkflowId, |
| | 2 | 609 | | stage.Name, |
| | 2 | 610 | | ParentAgentId: null, |
| | 2 | 611 | | reporter.Depth, |
| | 2 | 612 | | reporter.NextSequence(), |
| | 2 | 613 | | AgentName: stage.Name, |
| | 2 | 614 | | ErrorMessage: ex.Message)); |
| | | 615 | | |
| | 2 | 616 | | throw; |
| | | 617 | | } |
| | | 618 | | finally |
| | | 619 | | { |
| | 53 | 620 | | stageBudgetScope?.Dispose(); |
| | | 621 | | } |
| | | 622 | | |
| | 51 | 623 | | if (stageResult is not null && policy?.AfterExecution is { } afterExec) |
| | | 624 | | { |
| | 0 | 625 | | await afterExec(stageResult, context); |
| | | 626 | | } |
| | | 627 | | |
| | 51 | 628 | | if (validationError is not null) |
| | | 629 | | { |
| | 0 | 630 | | throw new StageValidationException(stage.Name, validationError); |
| | | 631 | | } |
| | | 632 | | |
| | 51 | 633 | | if (!stageResult!.Succeeded) |
| | | 634 | | { |
| | 0 | 635 | | var failedPhasedExecResult = new AgentStageResult( |
| | 0 | 636 | | stage.Name, |
| | 0 | 637 | | FinalResponse: null, |
| | 0 | 638 | | Diagnostics: stageResult.Diagnostics, |
| | 0 | 639 | | Outcome: StageOutcome.Failed, |
| | 0 | 640 | | PhaseName: phase.Name, |
| | 0 | 641 | | Termination: stageResult.Termination); |
| | 0 | 642 | | allStageResults.Add(failedPhasedExecResult); |
| | 0 | 643 | | EmitStageMetricsAndDisposeActivity(pipelineName, failedPhasedExecResult, stageStopwatch, sta |
| | | 644 | | |
| | 0 | 645 | | reporter.Report(new AgentFailedEvent( |
| | 0 | 646 | | DateTimeOffset.UtcNow, |
| | 0 | 647 | | reporter.WorkflowId, |
| | 0 | 648 | | stage.Name, |
| | 0 | 649 | | ParentAgentId: null, |
| | 0 | 650 | | reporter.Depth, |
| | 0 | 651 | | reporter.NextSequence(), |
| | 0 | 652 | | AgentName: stage.Name, |
| | 0 | 653 | | ErrorMessage: stageResult.Exception?.Message ?? "Stage failed")); |
| | | 654 | | |
| | 0 | 655 | | if (stageResult.FailureDisposition == FailureDisposition.AbortPipeline) |
| | | 656 | | { |
| | 0 | 657 | | phaseSucceeded = false; |
| | 0 | 658 | | pipelineStopwatch.Stop(); |
| | 0 | 659 | | var errorMsg = stageResult.Exception?.Message |
| | 0 | 660 | | ?? $"Stage '{stage.Name}' failed"; |
| | 0 | 661 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, pipelineStopwatch.Ela |
| | 0 | 662 | | return new PipelineRunResult( |
| | 0 | 663 | | allStageResults, |
| | 0 | 664 | | pipelineStopwatch.Elapsed, |
| | 0 | 665 | | succeeded: false, |
| | 0 | 666 | | errorMessage: errorMsg, |
| | 0 | 667 | | exception: stageResult.Exception, |
| | 0 | 668 | | plannedStageCount: totalStages); |
| | | 669 | | } |
| | | 670 | | |
| | 0 | 671 | | globalStageIndex++; |
| | 0 | 672 | | continue; |
| | | 673 | | } |
| | | 674 | | |
| | 51 | 675 | | ChatResponse? chatResponse = stageResult.ResponseText is not null |
| | 51 | 676 | | ? new ChatResponse(new ChatMessage(ChatRole.Assistant, stageResult.ResponseText)) |
| | 51 | 677 | | : null; |
| | | 678 | | |
| | 51 | 679 | | allStageResults.Add(new AgentStageResult( |
| | 51 | 680 | | stage.Name, |
| | 51 | 681 | | chatResponse, |
| | 51 | 682 | | stageResult.Diagnostics, |
| | 51 | 683 | | PhaseName: phase.Name, |
| | 51 | 684 | | Termination: stageResult.Termination)); |
| | 51 | 685 | | EmitStageMetricsAndDisposeActivity( |
| | 51 | 686 | | pipelineName, |
| | 51 | 687 | | allStageResults[^1], |
| | 51 | 688 | | stageStopwatch, |
| | 51 | 689 | | stageActivity); |
| | | 690 | | |
| | 51 | 691 | | reporter.Report(new AgentCompletedEvent( |
| | 51 | 692 | | DateTimeOffset.UtcNow, |
| | 51 | 693 | | reporter.WorkflowId, |
| | 51 | 694 | | stage.Name, |
| | 51 | 695 | | ParentAgentId: null, |
| | 51 | 696 | | reporter.Depth, |
| | 51 | 697 | | reporter.NextSequence(), |
| | 51 | 698 | | stage.Name, |
| | 51 | 699 | | Duration: pipelineStopwatch.Elapsed, |
| | 51 | 700 | | TotalTokens: stageResult.Diagnostics?.AggregateTokenUsage.TotalTokens ?? 0)); |
| | | 701 | | |
| | 51 | 702 | | globalStageIndex++; |
| | 51 | 703 | | } |
| | | 704 | | } |
| | | 705 | | finally |
| | | 706 | | { |
| | 48 | 707 | | if (phasePolicy?.OnExitAsync is { } onExit) |
| | | 708 | | { |
| | 5 | 709 | | await onExit(phaseContext, cancellationToken); |
| | | 710 | | } |
| | | 711 | | |
| | 48 | 712 | | phaseBudgetScope?.Dispose(); |
| | | 713 | | |
| | 48 | 714 | | phaseStopwatch.Stop(); |
| | 48 | 715 | | reporter.Report(new PhaseCompletedEvent( |
| | 48 | 716 | | DateTimeOffset.UtcNow, |
| | 48 | 717 | | reporter.WorkflowId, |
| | 48 | 718 | | reporter.AgentId, |
| | 48 | 719 | | ParentAgentId: null, |
| | 48 | 720 | | reporter.Depth, |
| | 48 | 721 | | reporter.NextSequence(), |
| | 48 | 722 | | phase.Name, |
| | 48 | 723 | | phaseIndex, |
| | 48 | 724 | | phases.Count, |
| | 48 | 725 | | phaseSucceeded, |
| | 48 | 726 | | phaseStopwatch.Elapsed)); |
| | | 727 | | } |
| | 45 | 728 | | } |
| | | 729 | | |
| | 29 | 730 | | pipelineStopwatch.Stop(); |
| | | 731 | | |
| | 29 | 732 | | var pipelineResult = new PipelineRunResult( |
| | 29 | 733 | | allStageResults, |
| | 29 | 734 | | pipelineStopwatch.Elapsed, |
| | 29 | 735 | | succeeded: true, |
| | 29 | 736 | | errorMessage: null, |
| | 29 | 737 | | plannedStageCount: totalStages); |
| | | 738 | | |
| | 29 | 739 | | if (options?.CompletionGate is { } gate) |
| | | 740 | | { |
| | 1 | 741 | | var gateError = gate(pipelineResult); |
| | 1 | 742 | | if (gateError is not null) |
| | | 743 | | { |
| | 1 | 744 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, pipelineStopwatch.Elapsed, succee |
| | 1 | 745 | | return new PipelineRunResult( |
| | 1 | 746 | | allStageResults, |
| | 1 | 747 | | pipelineStopwatch.Elapsed, |
| | 1 | 748 | | succeeded: false, |
| | 1 | 749 | | errorMessage: gateError, |
| | 1 | 750 | | plannedStageCount: totalStages); |
| | | 751 | | } |
| | | 752 | | } |
| | | 753 | | |
| | 28 | 754 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, pipelineStopwatch.Elapsed, succeeded: tru |
| | 28 | 755 | | return pipelineResult; |
| | | 756 | | } |
| | 0 | 757 | | catch (OperationCanceledException ex) when (ex.InnerException is TokenBudgetExceededException budgetEx) |
| | | 758 | | { |
| | 0 | 759 | | pipelineStopwatch.Stop(); |
| | 0 | 760 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, pipelineStopwatch.Elapsed, succeeded: fal |
| | 0 | 761 | | return new PipelineRunResult( |
| | 0 | 762 | | allStageResults, |
| | 0 | 763 | | pipelineStopwatch.Elapsed, |
| | 0 | 764 | | succeeded: false, |
| | 0 | 765 | | errorMessage: budgetEx.Message, |
| | 0 | 766 | | exception: budgetEx, |
| | 0 | 767 | | plannedStageCount: totalStages); |
| | | 768 | | } |
| | 0 | 769 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 770 | | { |
| | 0 | 771 | | pipelineStopwatch.Stop(); |
| | 0 | 772 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, pipelineStopwatch.Elapsed, succeeded: fal |
| | 0 | 773 | | throw; |
| | | 774 | | } |
| | 0 | 775 | | catch (OperationCanceledException ex) |
| | | 776 | | { |
| | 0 | 777 | | pipelineStopwatch.Stop(); |
| | 0 | 778 | | var message = ex.InnerException is TimeoutException |
| | 0 | 779 | | ? $"Stage timed out: {ex.InnerException.Message}" |
| | 0 | 780 | | : $"Operation cancelled (not by caller): {ex.Message}"; |
| | 0 | 781 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, pipelineStopwatch.Elapsed, succeeded: fal |
| | 0 | 782 | | return new PipelineRunResult( |
| | 0 | 783 | | allStageResults, |
| | 0 | 784 | | pipelineStopwatch.Elapsed, |
| | 0 | 785 | | succeeded: false, |
| | 0 | 786 | | errorMessage: message, |
| | 0 | 787 | | exception: ex, |
| | 0 | 788 | | plannedStageCount: totalStages); |
| | | 789 | | } |
| | 3 | 790 | | catch (Exception ex) |
| | | 791 | | { |
| | 3 | 792 | | pipelineStopwatch.Stop(); |
| | 3 | 793 | | ReportPipelineCompletion(reporter, pipelineActivity, pipelineName, pipelineStopwatch.Elapsed, succeeded: fal |
| | 3 | 794 | | return new PipelineRunResult( |
| | 3 | 795 | | allStageResults, |
| | 3 | 796 | | pipelineStopwatch.Elapsed, |
| | 3 | 797 | | succeeded: false, |
| | 3 | 798 | | errorMessage: ex.Message, |
| | 3 | 799 | | exception: ex, |
| | 3 | 800 | | plannedStageCount: totalStages); |
| | | 801 | | } |
| | | 802 | | finally |
| | | 803 | | { |
| | 32 | 804 | | pipelineBudgetScope?.Dispose(); |
| | | 805 | | } |
| | 32 | 806 | | } |
| | | 807 | | |
| | | 808 | | private static void ReportCompleted( |
| | | 809 | | IProgressReporter reporter, |
| | | 810 | | TimeSpan duration, |
| | | 811 | | bool succeeded, |
| | | 812 | | string? errorMessage) |
| | | 813 | | { |
| | 79 | 814 | | reporter.Report(new WorkflowCompletedEvent( |
| | 79 | 815 | | DateTimeOffset.UtcNow, |
| | 79 | 816 | | reporter.WorkflowId, |
| | 79 | 817 | | reporter.AgentId, |
| | 79 | 818 | | ParentAgentId: null, |
| | 79 | 819 | | reporter.Depth, |
| | 79 | 820 | | reporter.NextSequence(), |
| | 79 | 821 | | succeeded, |
| | 79 | 822 | | errorMessage, |
| | 79 | 823 | | duration)); |
| | 79 | 824 | | } |
| | | 825 | | |
| | | 826 | | private static string ResolvePipelineName(SequentialPipelineOptions? options, IProgressReporter reporter) => |
| | 79 | 827 | | options?.PipelineName ?? reporter.WorkflowId; |
| | | 828 | | |
| | | 829 | | private Activity? StartPipelineScope(string pipelineName) |
| | | 830 | | { |
| | 79 | 831 | | var activity = _pipelineMetrics.ActivitySource.StartActivity("pipeline.run"); |
| | 79 | 832 | | activity?.SetTag("pipeline_name", pipelineName); |
| | 79 | 833 | | _pipelineMetrics.RecordPipelineStarted(pipelineName); |
| | 79 | 834 | | return activity; |
| | | 835 | | } |
| | | 836 | | |
| | | 837 | | private void ReportPipelineCompletion( |
| | | 838 | | IProgressReporter reporter, |
| | | 839 | | Activity? pipelineActivity, |
| | | 840 | | string pipelineName, |
| | | 841 | | TimeSpan duration, |
| | | 842 | | bool succeeded, |
| | | 843 | | string? errorMessage) |
| | | 844 | | { |
| | 79 | 845 | | ReportCompleted(reporter, duration, succeeded, errorMessage); |
| | 79 | 846 | | pipelineActivity?.SetTag("outcome", succeeded ? "Succeeded" : "Failed"); |
| | 79 | 847 | | pipelineActivity?.Dispose(); |
| | 79 | 848 | | _pipelineMetrics.RecordPipelineCompleted(pipelineName, succeeded, duration); |
| | 79 | 849 | | } |
| | | 850 | | |
| | | 851 | | private (Stopwatch stopwatch, Activity? activity) StartStageScope( |
| | | 852 | | string pipelineName, |
| | | 853 | | string stageName, |
| | | 854 | | string? phaseName) |
| | | 855 | | { |
| | 112 | 856 | | var stopwatch = Stopwatch.StartNew(); |
| | 112 | 857 | | var activity = _pipelineMetrics.ActivitySource.StartActivity("pipeline.stage"); |
| | 112 | 858 | | activity?.SetTag("pipeline_name", pipelineName); |
| | 112 | 859 | | activity?.SetTag("stage_name", stageName); |
| | 112 | 860 | | activity?.SetTag("phase_name", phaseName ?? "(none)"); |
| | 112 | 861 | | return (stopwatch, activity); |
| | | 862 | | } |
| | | 863 | | |
| | | 864 | | private void EmitStageMetricsAndDisposeActivity( |
| | | 865 | | string pipelineName, |
| | | 866 | | IAgentStageResult stage, |
| | | 867 | | Stopwatch stageStopwatch, |
| | | 868 | | Activity? stageActivity) |
| | | 869 | | { |
| | 111 | 870 | | stageStopwatch.Stop(); |
| | 111 | 871 | | stageActivity?.SetTag("outcome", stage.Outcome.ToString()); |
| | 111 | 872 | | stageActivity?.SetTag("termination_cause", stage.Termination?.ToTagValue() ?? "Unspecified"); |
| | 111 | 873 | | stageActivity?.Dispose(); |
| | 111 | 874 | | _pipelineMetrics.RecordStageCompleted(pipelineName, stage, stageStopwatch.Elapsed); |
| | 111 | 875 | | } |
| | | 876 | | } |