-
Notifications
You must be signed in to change notification settings - Fork 1.9k
.NET: Fix issue with resuming checkpoint after package version upgrade #6636
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
Open
peibekwe
wants to merge
1
commit into
main
Choose a base branch
from
peibekwe/workflow-version-resume
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.
Open
Changes from all commits
Commits
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
135 changes: 135 additions & 0 deletions
135
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/CheckpointVersionToleranceTests.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,135 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Microsoft.Agents.AI.Workflows.Checkpointing; | ||
| using Microsoft.Agents.AI.Workflows.InProc; | ||
|
|
||
| namespace Microsoft.Agents.AI.Workflows.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Verifies that a checkpoint serialized through <see cref="JsonCheckpointStore"/> can be restored | ||
| /// after every <c>Version=X.Y.Z.W</c> substring in the persisted JSON is rewritten to a different value. | ||
| /// </summary> | ||
| public class CheckpointVersionToleranceTests | ||
| { | ||
| private sealed class EchoExecutor() : Executor("Echo") | ||
| { | ||
| protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder) | ||
| => protocolBuilder.ConfigureRoutes(routeBuilder => | ||
| routeBuilder.AddHandler<string>((msg, ctx) => ctx.SendMessageAsync(msg))); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ExecutionEnvironment.InProcess_OffThread)] | ||
| [InlineData(ExecutionEnvironment.InProcess_Lockstep)] | ||
| internal async Task Test_Checkpoint_Resumes_AfterAssemblyVersionRewriteAsync(ExecutionEnvironment environment) | ||
| { | ||
|
peibekwe marked this conversation as resolved.
|
||
| // Arrange | ||
| RequestPort<string, string> requestPort = RequestPort.Create<string, string>("TestPort"); | ||
| EchoExecutor echo = new(); | ||
|
|
||
| Workflow workflow = new WorkflowBuilder(requestPort) | ||
| .AddEdge(requestPort, echo) | ||
| .Build(); | ||
|
|
||
| VersionMutatingJsonStore store = new(); | ||
| CheckpointManager checkpointManager = CheckpointManager.CreateJson(store); | ||
| InProcessExecutionEnvironment env = environment.ToWorkflowExecutionEnvironment(); | ||
|
|
||
| // Run the workflow and capture a checkpoint. | ||
| CheckpointInfo? checkpoint = null; | ||
| await using (StreamingRun firstRun = await env.WithCheckpointing(checkpointManager) | ||
| .RunStreamingAsync(workflow, "Hello")) | ||
| { | ||
| await foreach (WorkflowEvent evt in firstRun.WatchStreamAsync(blockOnPendingRequest: false)) | ||
| { | ||
| if (evt is SuperStepCompletedEvent step && step.CompletionInfo?.Checkpoint is { } cp) | ||
| { | ||
| checkpoint = cp; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| checkpoint.Should().NotBeNull(); | ||
| store.MutationApplied.Should().BeFalse(); | ||
|
|
||
| // Resume against the mutated store, which rewrites every Version=X.Y.Z.W in the persisted JSON. | ||
| Func<Task> resume = async () => | ||
| { | ||
| await using StreamingRun resumed = await env.WithCheckpointing(checkpointManager) | ||
| .ResumeStreamingAsync(workflow, checkpoint!); | ||
| using CancellationTokenSource cts = new(TimeSpan.FromSeconds(10)); | ||
| await foreach (WorkflowEvent _ in resumed.WatchStreamAsync(blockOnPendingRequest: false, cts.Token)) | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| await resume.Should().NotThrowAsync("resume must succeed when persisted assembly versions differ from loaded ones"); | ||
| store.MutationApplied.Should().BeTrue(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// JSON checkpoint store that rewrites every <c>Version=N.N.N.N</c> token in the persisted | ||
| /// payload at retrieval time. | ||
| /// </summary> | ||
| private sealed class VersionMutatingJsonStore : JsonCheckpointStore | ||
| { | ||
| private static readonly Regex s_versionPattern = new(@"Version=\d+\.\d+\.\d+\.\d+", RegexOptions.Compiled); | ||
|
|
||
| private readonly Dictionary<string, Dictionary<string, JsonElement>> _store = []; | ||
|
|
||
| public string ReplacementVersion { get; init; } = "99.0.0.0"; | ||
|
|
||
| public bool MutationApplied { get; private set; } | ||
|
|
||
| public override ValueTask<CheckpointInfo> CreateCheckpointAsync(string sessionId, JsonElement value, CheckpointInfo? parent = null) | ||
| { | ||
| if (!this._store.TryGetValue(sessionId, out Dictionary<string, JsonElement>? sessionStore)) | ||
| { | ||
| sessionStore = this._store[sessionId] = []; | ||
| } | ||
|
|
||
| CheckpointInfo info = new(sessionId); | ||
| sessionStore[info.CheckpointId] = value.Clone(); | ||
| return new ValueTask<CheckpointInfo>(info); | ||
| } | ||
|
|
||
| public override ValueTask<JsonElement> RetrieveCheckpointAsync(string sessionId, CheckpointInfo key) | ||
| { | ||
| if (!this._store.TryGetValue(sessionId, out Dictionary<string, JsonElement>? sessionStore) | ||
| || !sessionStore.TryGetValue(key.CheckpointId, out JsonElement raw)) | ||
| { | ||
| throw new KeyNotFoundException($"Could not retrieve checkpoint with id {key.CheckpointId} for session {sessionId}"); | ||
| } | ||
|
|
||
| string rawText = raw.GetRawText(); | ||
| string mutatedText = s_versionPattern.Replace(rawText, $"Version={this.ReplacementVersion}"); | ||
|
|
||
| if (!ReferenceEquals(rawText, mutatedText) && rawText != mutatedText) | ||
| { | ||
| this.MutationApplied = true; | ||
| } | ||
|
|
||
| using JsonDocument doc = JsonDocument.Parse(mutatedText); | ||
| return new ValueTask<JsonElement>(doc.RootElement.Clone()); | ||
| } | ||
|
|
||
| public override ValueTask<IEnumerable<CheckpointInfo>> RetrieveIndexAsync(string sessionId, CheckpointInfo? withParent = null) | ||
| { | ||
| if (!this._store.TryGetValue(sessionId, out Dictionary<string, JsonElement>? sessionStore)) | ||
| { | ||
| return new ValueTask<IEnumerable<CheckpointInfo>>(Array.Empty<CheckpointInfo>()); | ||
| } | ||
|
|
||
| IEnumerable<CheckpointInfo> infos = sessionStore.Keys.Select(id => new CheckpointInfo(sessionId, id)); | ||
| return new ValueTask<IEnumerable<CheckpointInfo>>(infos); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.