Skip to content

Commit 809cc04

Browse files
baywetCopilot
andauthored
.NET: fix(dotnet): reset compiled workflow state between runs (#9540)
* fix(dotnet): reset compiled workflow state between runs Co-authored-by: Copilot <[email protected]> Copilot-Session: 12f29503-c7ae-410b-a598-d0ba4e8e937b * fix(dotnet): restore compiled workflow checkpoints consistently Co-authored-by: Copilot <[email protected]> Copilot-Session: 12f29503-c7ae-410b-a598-d0ba4e8e937b * fix(dotnet): reuse created conversation when retrying state writes Co-authored-by: Copilot <[email protected]> Copilot-Session: 12f29503-c7ae-410b-a598-d0ba4e8e937b * fix(workflows): restore allowed compiled environment bindings on resume Co-authored-by: Copilot <[email protected]> Copilot-Session: 12f29503-c7ae-410b-a598-d0ba4e8e937b * fix(workflows): restore shared state before subclass checkpoint hooks Co-authored-by: Copilot <[email protected]> Copilot-Session: 12f29503-c7ae-410b-a598-d0ba4e8e937b * fix(workflows): restore shared formula state on every checkpoint load Co-authored-by: Copilot <[email protected]> Copilot-Session: 12f29503-c7ae-410b-a598-d0ba4e8e937b --------- Co-authored-by: Copilot <[email protected]> Copilot-Session: 12f29503-c7ae-410b-a598-d0ba4e8e937b
1 parent c01db29 commit 809cc04

13 files changed

Lines changed: 723 additions & 16 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/Extensions/IWorkflowContextExtensions.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public static async ValueTask QueueConversationUpdateAsync(this IWorkflowContext
8282

8383
if (isExternal)
8484
{
85+
conversation = FormulaValue.NewRecordFromFields(conversation.Fields);
8586
conversation.UpdateField("Id", FormulaValue.New(conversationId));
8687
await context.QueueSystemUpdateAsync(SystemScope.Names.Conversation, conversation, cancellationToken).ConfigureAwait(false);
8788
await context.QueueSystemUpdateAsync(SystemScope.Names.ConversationId, FormulaValue.New(conversationId), cancellationToken).ConfigureAwait(false);

‎dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/Kit/ActionExecutor.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ protected ActionExecutor(string id, FormulaSession session)
6464
: base(id)
6565
{
6666
this._session = session;
67+
this.RegisterCheckpointRestoringCallback(session.State, session.State.RestoreCheckpointAsync);
6768
}
6869

6970
/// <inheritdoc/>

‎dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/Kit/RootExecutor.cs‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public abstract class RootExecutor<TInput> : Executor<TInput>, IResettableExecut
2626
private readonly bool _allowProcessEnvironmentVariableFallback;
2727
private readonly FrozenSet<string> _allowedEnvironmentVariables;
2828

29-
private string? _conversationId;
29+
private readonly string? _conversationId;
30+
private string? _generatedConversationId;
3031

3132
/// <summary>
3233
/// Get the shared formula session to provide to workflow <see cref="ActionExecutor"/> instances.
@@ -48,14 +49,18 @@ protected RootExecutor(string id, DeclarativeWorkflowOptions options, Func<TInpu
4849
this._inputTransform = inputTransform;
4950
this._allowProcessEnvironmentVariableFallback = options.AllowProcessEnvironmentVariableFallback;
5051
this._allowedEnvironmentVariables = (options.AllowedEnvironmentVariables ?? []).ToFrozenSet(StringComparer.Ordinal);
51-
this._state = new WorkflowFormulaState(options.CreateRecalcEngine());
52+
this._state = new WorkflowFormulaState(options.CreateRecalcEngine(), restorableEnvironmentVariables: this._allowedEnvironmentVariables);
5253
this._state.InitializeSystem();
54+
this._state.CaptureInitialState();
5355
this.Session = new RootFormulaSession(this._state);
56+
this.RegisterCheckpointRestoringCallback(this._state, this._state.RestoreCheckpointAsync);
5457
}
5558

5659
/// <inheritdoc/>
5760
public ValueTask ResetAsync()
5861
{
62+
this._state.Reset();
63+
this._generatedConversationId = null;
5964
return default;
6065
}
6166

@@ -67,13 +72,22 @@ public override async ValueTask HandleAsync(TInput message, IWorkflowContext con
6772

6873
ChatMessage input = (this._inputTransform ?? DefaultInputTransform).Invoke(message);
6974

70-
if (string.IsNullOrWhiteSpace(this._conversationId))
75+
string? conversationId = declarativeContext.GetWorkflowConversation();
76+
if (string.IsNullOrWhiteSpace(conversationId))
7177
{
72-
this._conversationId = await this._agentProvider.CreateConversationAsync(cancellationToken).ConfigureAwait(false);
78+
conversationId = this._conversationId;
7379
}
74-
await declarativeContext.QueueConversationUpdateAsync(this._conversationId, isExternal: true, cancellationToken).ConfigureAwait(false);
80+
if (string.IsNullOrWhiteSpace(conversationId))
81+
{
82+
if (string.IsNullOrWhiteSpace(this._generatedConversationId))
83+
{
84+
this._generatedConversationId = await this._agentProvider.CreateConversationAsync(cancellationToken).ConfigureAwait(false);
85+
}
86+
conversationId = this._generatedConversationId;
87+
}
88+
await declarativeContext.QueueConversationUpdateAsync(conversationId, isExternal: true, cancellationToken).ConfigureAwait(false);
7589

76-
ChatMessage inputMessage = await this._agentProvider.CreateMessageAsync(this._conversationId, input, cancellationToken).ConfigureAwait(false);
90+
ChatMessage inputMessage = await this._agentProvider.CreateMessageAsync(conversationId, input, cancellationToken).ConfigureAwait(false);
7791

7892
// Use the original input for System.LastMessage to ensure Text is preserved (the
7993
// service may strip text on round-trip), but substitute server-side media references

‎dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/PowerFx/WorkflowFormulaState.cs‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal sealed class WorkflowFormulaState
3131
private const string SensitivityScopePrefix = "__Microsoft_Agents_AI_Workflows_Declarative_Sensitivity:";
3232

3333
private readonly Dictionary<string, WorkflowScope> _scopes;
34+
private readonly FrozenSet<string> _restorableEnvironmentVariables;
3435

3536
private Dictionary<string, WorkflowScope> _initialScopes;
3637

@@ -42,9 +43,10 @@ internal sealed class WorkflowFormulaState
4243

4344
public bool AllowsSideEffects { get; }
4445

45-
public WorkflowFormulaState(RecalcEngine engine, bool allowsSideEffects = false)
46+
public WorkflowFormulaState(RecalcEngine engine, bool allowsSideEffects = false, IEnumerable<string>? restorableEnvironmentVariables = null)
4647
{
4748
this._scopes = VariableScopeNames.AllScopes.ToDictionary(scopeName => GetScopeName(scopeName), _ => new WorkflowScope());
49+
this._restorableEnvironmentVariables = (restorableEnvironmentVariables ?? []).ToFrozenSet(StringComparer.Ordinal);
4850
this._initialScopes = this.CreateScopeSnapshot();
4951

5052
this.Engine = engine;
@@ -112,25 +114,38 @@ public void Reset()
112114
this.Bind();
113115
}
114116

115-
public async ValueTask RestoreAsync(IWorkflowContext context, CancellationToken cancellationToken)
117+
public ValueTask RestoreAsync(IWorkflowContext context, CancellationToken cancellationToken)
116118
{
117119
if (!this.SetInitialized())
118120
{
119-
return;
121+
return default;
120122
}
121123

124+
return this.RestoreCheckpointAsync(context, cancellationToken);
125+
}
126+
127+
public async ValueTask RestoreCheckpointAsync(IWorkflowContext context, CancellationToken cancellationToken)
128+
{
122129
this.RestoreInitialState();
123130

124131
Stopwatch timer = Stopwatch.StartNew();
125132
Debug.WriteLine("RESTORE CHECKPOINT - BEGIN");
126133
await Task.WhenAll(RestorableScopes.Select(scopeName => ReadScopeAsync(scopeName))).ConfigureAwait(false);
134+
if (this._restorableEnvironmentVariables.Count > 0)
135+
{
136+
await ReadScopeAsync(VariableScopeNames.Environment, this._restorableEnvironmentVariables).ConfigureAwait(false);
137+
}
127138
Debug.WriteLine($"RESTORE CHECKPOINT - COMPLETE [{timer.Elapsed}]");
128139

129-
async Task ReadScopeAsync(string scopeName)
140+
async Task ReadScopeAsync(string scopeName, FrozenSet<string>? allowedKeys = null)
130141
{
131142
HashSet<string> keys = await context.ReadStateKeysAsync(scopeName, cancellationToken).ConfigureAwait(false);
132143
foreach (string key in keys)
133144
{
145+
if (allowedKeys?.Contains(key) == false)
146+
{
147+
continue;
148+
}
134149
PortableValue? value = await context.ReadStateAsync<PortableValue>(key, scopeName, cancellationToken).ConfigureAwait(false);
135150
SensitivityLevel sensitivity = await context.ReadStateAsync<SensitivityLevel>(key, GetSensitivityScopeName(scopeName), cancellationToken).ConfigureAwait(false);
136151
if (value is null)

‎dotnet/src/Microsoft.Agents.AI.Workflows/Executor.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.Agents.AI.Workflows.Execution;
1515
using Microsoft.Agents.AI.Workflows.Observability;
1616
using Microsoft.Agents.AI.Workflows.Reflection;
17+
using Microsoft.Shared.Diagnostics;
1718

1819
namespace Microsoft.Agents.AI.Workflows;
1920

@@ -198,6 +199,11 @@ protected Executor(string id, ExecutorOptions? options = null, bool declareCross
198199

199200
internal bool IsCrossRunShareable { get; }
200201

202+
private List<(object? SharedStateKey, Func<IWorkflowContext, CancellationToken, ValueTask> Callback)>? _checkpointRestoringCallbacks;
203+
204+
internal IReadOnlyList<(object? SharedStateKey, Func<IWorkflowContext, CancellationToken, ValueTask> Callback)> CheckpointRestoringCallbacks =>
205+
this._checkpointRestoringCallbacks ?? [];
206+
201207
/// <summary>
202208
/// Gets the configuration options for the executor.
203209
/// </summary>
@@ -346,6 +352,38 @@ protected internal virtual ValueTask InitializeAsync(IWorkflowContext context, C
346352
/// <returns>A ValueTask representing the asynchronous operation.</returns>
347353
protected internal virtual ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default) => default;
348354

355+
/// <summary>
356+
/// Registers state restoration that runs before checkpoint post-load hooks.
357+
/// </summary>
358+
/// <remarks>
359+
/// All registered callbacks across the workflow complete before any executor's
360+
/// <see cref="OnCheckpointRestoredAsync"/> hook is invoked. Register callbacks during construction.
361+
/// </remarks>
362+
/// <param name="callback">The state restoration callback.</param>
363+
protected void RegisterCheckpointRestoringCallback(Func<IWorkflowContext, CancellationToken, ValueTask> callback)
364+
{
365+
_ = Throw.IfNull(callback);
366+
(this._checkpointRestoringCallbacks ??= []).Add((null, callback));
367+
}
368+
369+
/// <summary>
370+
/// Registers shared state restoration that runs once per checkpoint load before post-load hooks.
371+
/// </summary>
372+
/// <remarks>
373+
/// Registrations with the same key instance are invoked once across the workflow for each checkpoint load,
374+
/// using the context of one registering executor. Shared state must use explicitly named scopes.
375+
/// All restoration callbacks complete before any <see cref="OnCheckpointRestoredAsync"/> hook is invoked.
376+
/// Register callbacks during construction.
377+
/// </remarks>
378+
/// <param name="sharedStateKey">An identity shared by executors that restore the same state.</param>
379+
/// <param name="callback">The shared state restoration callback.</param>
380+
protected void RegisterCheckpointRestoringCallback(object sharedStateKey, Func<IWorkflowContext, CancellationToken, ValueTask> callback)
381+
{
382+
_ = Throw.IfNull(sharedStateKey);
383+
_ = Throw.IfNull(callback);
384+
(this._checkpointRestoringCallbacks ??= []).Add((sharedStateKey, callback));
385+
}
386+
349387
/// <summary>
350388
/// A set of <see cref="Type"/>s, representing the messages this executor can handle.
351389
/// </summary>

‎dotnet/src/Microsoft.Agents.AI.Workflows/InProc/InProcessRunnerContext.cs‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,15 +402,44 @@ async Task InvokeCheckpointingAsync(Task<Executor> executorTask)
402402
}
403403
}
404404

405-
internal Task NotifyCheckpointLoadedAsync(CancellationToken cancellationToken = default)
405+
internal async Task NotifyCheckpointLoadedAsync(CancellationToken cancellationToken = default)
406406
{
407407
this.CheckEnded();
408408

409-
return Task.WhenAll(this._executors.Values.Select(InvokeCheckpointRestoredAsync));
409+
Executor[] executors = await Task.WhenAll(this._executors.Values).ConfigureAwait(false);
410+
List<object> sharedStateKeys = [];
411+
List<Task> restorationTasks = [];
412+
foreach (Executor executor in executors)
413+
{
414+
List<Func<IWorkflowContext, CancellationToken, ValueTask>> callbacks = [];
415+
foreach (var registration in executor.CheckpointRestoringCallbacks)
416+
{
417+
if (registration.SharedStateKey is { } sharedStateKey)
418+
{
419+
if (sharedStateKeys.Any(key => ReferenceEquals(key, sharedStateKey)))
420+
{
421+
continue;
422+
}
423+
sharedStateKeys.Add(sharedStateKey);
424+
}
425+
callbacks.Add(registration.Callback);
426+
}
427+
restorationTasks.Add(RestoreStateAsync(executor, callbacks));
428+
}
429+
// Shared state must finish restoring before any subclass post-load hook runs.
430+
await Task.WhenAll(restorationTasks).ConfigureAwait(false);
431+
await Task.WhenAll(executors.Select(InvokeCheckpointRestoredAsync)).ConfigureAwait(false);
410432

411-
async Task InvokeCheckpointRestoredAsync(Task<Executor> executorTask)
433+
async Task RestoreStateAsync(Executor executor, List<Func<IWorkflowContext, CancellationToken, ValueTask>> callbacks)
434+
{
435+
foreach (Func<IWorkflowContext, CancellationToken, ValueTask> restore in callbacks)
436+
{
437+
await restore(this.BindWorkflowContext(executor.Id), cancellationToken).ConfigureAwait(false);
438+
}
439+
}
440+
441+
async Task InvokeCheckpointRestoredAsync(Executor executor)
412442
{
413-
Executor executor = await executorTask.ConfigureAwait(false);
414443
await executor.OnCheckpointRestoredAsync(this.BindWorkflowContext(executor.Id), cancellationToken).ConfigureAwait(false);
415444
}
416445
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.Agents.AI.Workflows.Executor.RegisterCheckpointRestoringCallback(object! sharedStateKey, System.Func<Microsoft.Agents.AI.Workflows.IWorkflowContext!, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callback) -> void
3+
Microsoft.Agents.AI.Workflows.Executor.RegisterCheckpointRestoringCallback(System.Func<Microsoft.Agents.AI.Workflows.IWorkflowContext!, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callback) -> void
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.Agents.AI.Workflows.Executor.RegisterCheckpointRestoringCallback(object! sharedStateKey, System.Func<Microsoft.Agents.AI.Workflows.IWorkflowContext!, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callback) -> void
3+
Microsoft.Agents.AI.Workflows.Executor.RegisterCheckpointRestoringCallback(System.Func<Microsoft.Agents.AI.Workflows.IWorkflowContext!, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callback) -> void
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.Agents.AI.Workflows.Executor.RegisterCheckpointRestoringCallback(object! sharedStateKey, System.Func<Microsoft.Agents.AI.Workflows.IWorkflowContext!, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callback) -> void
3+
Microsoft.Agents.AI.Workflows.Executor.RegisterCheckpointRestoringCallback(System.Func<Microsoft.Agents.AI.Workflows.IWorkflowContext!, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callback) -> void
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.Agents.AI.Workflows.Executor.RegisterCheckpointRestoringCallback(object! sharedStateKey, System.Func<Microsoft.Agents.AI.Workflows.IWorkflowContext!, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callback) -> void
3+
Microsoft.Agents.AI.Workflows.Executor.RegisterCheckpointRestoringCallback(System.Func<Microsoft.Agents.AI.Workflows.IWorkflowContext!, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callback) -> void

0 commit comments

Comments
 (0)