// 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;
///
/// Verifies that a checkpoint serialized through can be restored
/// after every Version=X.Y.Z.W substring in the persisted JSON is rewritten to a different value.
///
public class CheckpointVersionToleranceTests
{
private sealed class EchoExecutor() : Executor("Echo")
{
protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder)
=> protocolBuilder.ConfigureRoutes(routeBuilder =>
routeBuilder.AddHandler((msg, ctx) => ctx.SendMessageAsync(msg)));
}
[Theory]
[InlineData(ExecutionEnvironment.InProcess_OffThread)]
[InlineData(ExecutionEnvironment.InProcess_Lockstep)]
internal async Task Test_Checkpoint_Resumes_AfterAssemblyVersionRewriteAsync(ExecutionEnvironment environment)
{
// Arrange
RequestPort requestPort = RequestPort.Create("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 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();
}
///
/// JSON checkpoint store that rewrites every Version=N.N.N.N token in the persisted
/// payload at retrieval time.
///
private sealed class VersionMutatingJsonStore : JsonCheckpointStore
{
private static readonly Regex s_versionPattern = new(@"Version=\d+\.\d+\.\d+\.\d+", RegexOptions.Compiled);
private readonly Dictionary> _store = [];
public string ReplacementVersion { get; init; } = "99.0.0.0";
public bool MutationApplied { get; private set; }
public override ValueTask CreateCheckpointAsync(string sessionId, JsonElement value, CheckpointInfo? parent = null)
{
if (!this._store.TryGetValue(sessionId, out Dictionary? sessionStore))
{
sessionStore = this._store[sessionId] = [];
}
CheckpointInfo info = new(sessionId);
sessionStore[info.CheckpointId] = value.Clone();
return new ValueTask(info);
}
public override ValueTask RetrieveCheckpointAsync(string sessionId, CheckpointInfo key)
{
if (!this._store.TryGetValue(sessionId, out Dictionary? 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(doc.RootElement.Clone());
}
public override ValueTask> RetrieveIndexAsync(string sessionId, CheckpointInfo? withParent = null)
{
if (!this._store.TryGetValue(sessionId, out Dictionary? sessionStore))
{
return new ValueTask>(Array.Empty());
}
IEnumerable infos = sessionStore.Keys.Select(id => new CheckpointInfo(sessionId, id));
return new ValueTask>(infos);
}
}
}