-
Notifications
You must be signed in to change notification settings - Fork 2.3k
.NET: preserve cooperative workflow handler cancellation #8494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Naveen Chatlapalli (1aifanatic)
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
1aifanatic:contrib/8092-enterprise-agents
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/ExecutorCancellationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| #pragma warning disable CS0618 // Verify cancellation in the supported legacy reflection path too. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Agents.AI.Workflows.Reflection; | ||
|
|
||
| namespace Microsoft.Agents.AI.Workflows.UnitTests; | ||
|
|
||
| public class ExecutorCancellationTests | ||
| { | ||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public async Task RuntimeCancellationDoesNotEmitFailureAsync(bool useReflection) | ||
| { | ||
| // Arrange | ||
| using CancellationTokenSource source = new(); | ||
| async ValueTask CancelAsync(string message, IWorkflowContext context, CancellationToken cancellationToken) | ||
| { | ||
| source.Cancel(); | ||
| await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken); | ||
| } | ||
|
|
||
| Executor executor = useReflection | ||
| ? new ReflectingHandler(CancelAsync) | ||
| : new FunctionExecutor<string>("cancel", CancelAsync); | ||
| TestWorkflowContext context = new(executor.Id); | ||
|
|
||
| // Act | ||
| OperationCanceledException exception = await Assert.ThrowsAnyAsync<OperationCanceledException>( | ||
| () => executor.ExecuteCoreAsync("input", new(typeof(string)), context, source.Token).AsTask()); | ||
|
|
||
| // Assert | ||
| Assert.Equal(source.Token, exception.CancellationToken); | ||
| Assert.DoesNotContain(context.EmittedEvents, evt => evt is ExecutorFailedEvent or ExecutorCompletedEvent); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public async Task OrdinaryFailureRemainsFailureWhenRuntimeIsCancelledAsync(bool useReflection) | ||
| { | ||
| // Arrange | ||
| using CancellationTokenSource source = new(); | ||
| InvalidOperationException expected = new("handler failed"); | ||
| async ValueTask FailAsync(string message, IWorkflowContext context, CancellationToken cancellationToken) | ||
| { | ||
| await Task.Yield(); | ||
| source.Cancel(); | ||
| throw expected; | ||
| } | ||
|
|
||
| Executor executor = useReflection | ||
| ? new ReflectingHandler(FailAsync) | ||
| : new FunctionExecutor<string>("fail", FailAsync); | ||
| TestWorkflowContext context = new(executor.Id); | ||
|
|
||
| // Act | ||
| TargetInvocationException exception = await Assert.ThrowsAsync<TargetInvocationException>( | ||
| () => executor.ExecuteCoreAsync("input", new(typeof(string)), context, source.Token).AsTask()); | ||
|
|
||
| // Assert | ||
| Assert.Same(expected, exception.InnerException); | ||
| Assert.Contains(context.EmittedEvents, evt => evt is ExecutorFailedEvent); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public async Task CancellationWithoutRuntimeCancellationRetainsFailureBehaviorAsync(bool useReflection) | ||
| { | ||
| // Arrange | ||
| async ValueTask CancelAsync(string message, IWorkflowContext context, CancellationToken cancellationToken) | ||
| { | ||
| await Task.Yield(); | ||
| throw new OperationCanceledException(); | ||
| } | ||
|
|
||
| Executor executor = useReflection | ||
| ? new ReflectingHandler(CancelAsync) | ||
| : new FunctionExecutor<string>("cancel", CancelAsync); | ||
| TestWorkflowContext context = new(executor.Id); | ||
|
|
||
| // Act | ||
| TargetInvocationException exception = await Assert.ThrowsAsync<TargetInvocationException>( | ||
| () => executor.ExecuteCoreAsync("input", new(typeof(string)), context).AsTask()); | ||
|
|
||
| // Assert: preserve the existing distinction between the two routing paths. | ||
| if (useReflection) | ||
| { | ||
| Assert.Null(exception.InnerException); | ||
| } | ||
| else | ||
| { | ||
| Assert.IsType<OperationCanceledException>(exception.InnerException); | ||
| } | ||
|
|
||
| Assert.Contains(context.EmittedEvents, evt => evt is ExecutorFailedEvent); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public async Task RuntimeCancellationDoesNotSurfaceAsWorkflowErrorAsync(bool offThread) | ||
| { | ||
| // Arrange | ||
| using CancellationTokenSource timeout = new(TimeSpan.FromSeconds(30)); | ||
| TaskCompletionSource<bool> started = new(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| Task deadline = Task.Delay(Timeout.InfiniteTimeSpan, timeout.Token); | ||
| FunctionExecutor<string> executor = new("cancel", async (message, context, cancellationToken) => | ||
| { | ||
| started.SetResult(true); | ||
| await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken); | ||
| }); | ||
| Workflow workflow = new WorkflowBuilder(executor).Build(); | ||
| var environment = offThread ? InProcessExecution.OffThread : InProcessExecution.Lockstep; | ||
| List<WorkflowEvent> events = []; | ||
|
|
||
| // Act | ||
| await using StreamingRun run = await environment.RunStreamingAsync(workflow, "input"); | ||
| async Task ReadEventsAsync() | ||
| { | ||
| try | ||
| { | ||
| await foreach (WorkflowEvent evt in run.WatchStreamAsync(timeout.Token)) | ||
| { | ||
| events.Add(evt); | ||
| } | ||
| } | ||
| catch (OperationCanceledException) when (!timeout.IsCancellationRequested) | ||
| { | ||
| // The stream may terminate through cancellation rather than normal completion. | ||
| } | ||
| } | ||
|
|
||
| Task reading = ReadEventsAsync(); | ||
| Assert.Same(started.Task, await Task.WhenAny(started.Task, deadline)); | ||
| await run.CancelRunAsync(); | ||
| Assert.Same(reading, await Task.WhenAny(reading, deadline)); | ||
| await reading; | ||
|
|
||
| // Assert | ||
| Assert.False(timeout.IsCancellationRequested); | ||
| Assert.DoesNotContain(events, evt => evt is ExecutorFailedEvent or WorkflowErrorEvent); | ||
| timeout.Cancel(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(false, false)] | ||
| [InlineData(false, true)] | ||
| [InlineData(true, false)] | ||
| [InlineData(true, true)] | ||
| public async Task ForeignCancellationRemainsFailureWhenRuntimeIsCancelledAsync(bool useReflection, bool useDefaultToken) | ||
| { | ||
| // Arrange | ||
| using CancellationTokenSource runtime = new(); | ||
| using CancellationTokenSource foreign = new(); | ||
| foreign.Cancel(); | ||
| OperationCanceledException expected = new(useDefaultToken ? CancellationToken.None : foreign.Token); | ||
| async ValueTask CancelAsync(string message, IWorkflowContext context, CancellationToken cancellationToken) | ||
| { | ||
| await Task.Yield(); | ||
| runtime.Cancel(); | ||
| throw expected; | ||
| } | ||
|
|
||
| Executor executor = useReflection | ||
| ? new ReflectingHandler(CancelAsync) | ||
| : new FunctionExecutor<string>("foreign", CancelAsync); | ||
| TestWorkflowContext context = new(executor.Id); | ||
|
|
||
| // Act | ||
| TargetInvocationException exception = await Assert.ThrowsAsync<TargetInvocationException>( | ||
| () => executor.ExecuteCoreAsync("input", new(typeof(string)), context, runtime.Token).AsTask()); | ||
|
|
||
| // Assert: preserve each routing path's existing failure shape. | ||
| if (useReflection) | ||
| { | ||
| Assert.Null(exception.InnerException); | ||
| } | ||
| else | ||
| { | ||
| Assert.Same(expected, exception.InnerException); | ||
| } | ||
|
|
||
| Assert.Contains(context.EmittedEvents, evt => evt is ExecutorFailedEvent); | ||
| } | ||
| private sealed class ReflectingHandler(Func<string, IWorkflowContext, CancellationToken, ValueTask> handler) | ||
| : ReflectingExecutor<ReflectingHandler>("reflecting"), IMessageHandler<string> | ||
| { | ||
| public ValueTask HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default) | ||
| => handler(message, context, cancellationToken); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.