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