mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
cbcdb2d29e
* .NET: [Feature Branch] Add basic durable workflow support (#3648) * Add basic durable workflow support. * PR feedback fixes * Add conditional edge sample. * PR feedback fixes. * Minor cleanup. * Minor cleanup * Minor formatting improvements. * Improve comments/documentation on the execution flow. * .NET: [Feature Branch] Add Azure Functions hosting support for durable workflows (#3935) * Adding azure functions workflow support. * - PR feedback fixes. - Add example to demonstrate complex Object as payload. * rename instanceId to runId. * Use custom ITaskOrchestrator to run orchestrator function. * .NET: [Feature Branch] Adding support for events & shared state in durable workflows (#4020) * Adding support for events & shared state in durable workflows. * PR feedback fixes * PR feedback fixes. * Add YieldOutputAsync calls to 05_WorkflowEvents sample executors The integration test asserts that WorkflowOutputEvent is found in the stream, but the sample executors only used AddEventAsync for custom events and never called YieldOutputAsync. Since WorkflowOutputEvent is only emitted via explicit YieldOutputAsync calls, the assertion would fail. Added YieldOutputAsync to each executor to match the test expectation and demonstrate the API in the sample. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix deserialization to use shared serializer options. * PR feedback updates. * Sample cleanup * PR feedback fixes * Addressing PR review feedback for DurableStreamingWorkflowRun - Use -1 instead of 0 for taskId in TaskFailedException when task ID is not relevant. - Add [NotNullWhen(true)] to TryParseWorkflowResult out parameter following .NET TryXXX conventions. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * .NET: [Feature Branch] Add nested sub-workflow support for durable workflows (#4190) * .NET: [Feature Branch] Add nested sub-workflow support for durable workflows * fix readme path * Switch Orchestration output from string to DurableWorkflowResult. * PR feedback fixes * Minor cleanup based on PR feedback. * .NET: [Feature Branch] Add Human In the Loop support for durable workflows (#4358) * Add Azure Functions HITL workflow sample Add 06_WorkflowHITL Azure Functions sample demonstrating Human-in-the-Loop workflow support with HTTP endpoints for status checking and approval responses. The sample includes: - ExpenseReimbursement workflow with RequestPort for manager approval - Custom HTTP endpoint to check workflow status and pending approvals - Custom HTTP endpoint to send approval responses via RaiseEventAsync - demo.http file with step-by-step interaction examples * PR feedback fixes * Minor comment cleanup * Minor comment clReverted the `!context.IsReplaying` guards on `PendingEvents.Add`/`RemoveAll` and `SetCustomStatus` in `ExecuteRequestPortAsync`. The guards broke fan-out scenarios where parallel RequestPorts need to be discoverable after replay. `SetCustomStatus` is idempotent metadata that doesn't affect replay determinism.eanup * fix for PR feedback * PR feedback updates * Improvements to samples * Improvements to README * Update samples to use parallel request ports. * Unit tests * Introduce local variables to improve readability of Workflows.Workflows access patter * Use GitHub-style callouts and add PowerShell command variants in HITL sample README * Add changelog entries for durable workflow support (#4436) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Bump Microsoft.DurableTask.Worker to 1.19.1 to fix version downgrade Microsoft.Azure.Functions.Worker.Extensions.DurableTask 1.13.1 requires Microsoft.DurableTask.Worker >= 1.19.1 via its transitive dependency on Microsoft.DurableTask.Worker.Grpc 1.19.1. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix broken markdown links in durable workflow sample READMEs - Create Workflow/README.md with environment setup docs - Fix ../README.md -> ../../README.md in ConsoleApps 01, 02, 03, 08 - Fix SubWorkflows relative path (3 levels -> 4 levels up) - Fix dead Durable Task Scheduler URL Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix build errors from main merge: Throw conflict, ExecuteAsync rename, GetNewSessionAsync rename - Remove InjectSharedThrow from DurableTask csproj (uses Workflows' internal Throw via InternalsVisibleTo) - Update ExecuteAsync -> ExecuteCoreAsync with WorkflowTelemetryContext.Disabled - Update GetNewSessionAsync -> CreateSessionAsync Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Move durable workflow samples to 04-hosting/DurableWorkflows Aligns with main branch sample reorganization where durable samples live under 04-hosting/ (alongside DurableAgents/). - Move samples/Durable/Workflow/ -> samples/04-hosting/DurableWorkflows/ - Add Directory.Build.props matching DurableAgents pattern - Update slnx project paths - Update integration test sample paths - Update README cd paths and cross-references Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix build errors: remove duplicate base class members, update renamed APIs - Remove duplicate OutputLog, WriteInputAsync, CreateTestTimeoutCts, etc. from ConsoleAppSamplesValidation (already in SamplesValidationBase) - Update AddFanInEdge -> AddFanInBarrierEdge in workflow samples - Update GetNewSessionAsync -> CreateSessionAsync in workflow samples - Update SourceId -> ExecutorId (obsolete) in workflow samples Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix dotnet format issues: add UTF-8 BOM and remove unused using - Add UTF-8 BOM to 20 .cs files across DurableTask, AzureFunctions, unit tests, and workflow samples - Remove unnecessary using directive in 07_SubWorkflows/Executors.cs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix typo PaymentProcesser -> PaymentProcessor and garbled arrows in README Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix GetExecutorName to handle agent names with underscores Split on last underscore instead of first, and validate that the suffix is a 32-char hex string (sanitized GUID) before stripping it. This prevents truncation of agent names like 'my_agent' when the executor ID is 'my_agent_<guid>'. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Align DurableTask.Client.AzureManaged to 1.19.1 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Bump DurableTask and Azure Functions extension package versions - DurableTask.* packages: 1.19.1 -> 1.22.0 - Functions.Worker.Extensions.DurableTask: 1.13.1 -> 1.16.0 - Functions.Worker.Extensions.DurableTask.AzureManaged: 1.0.1 -> 1.5.0 (telemetry bug fix) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Bump DurableTask SDK packages to 1.22.0 - DurableTask.Client: 1.19.1 -> 1.22.0 - DurableTask.Client.AzureManaged: 1.19.1 -> 1.22.0 - DurableTask.Worker: 1.19.1 -> 1.22.0 - DurableTask.Worker.AzureManaged: 1.19.1 -> 1.22.0 - Azure Functions extensions kept at original versions (1.13.1/1.0.1) due to host-side DurableTask.Core 3.7.0 incompatibility with newer extensions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Update Microsoft.Azure.Functions.Worker.Extensions.DurableTask to "1.16.0" * Add the local.settings.json files to the sample which were previously ignored. This aligns with our other samples. * Increase timeout for tests as CI has them failing transiently. * increaset timeout value for azure functions integration tests. * Add YieldsOutput(string) to workflow shared state sample executors ValidateOrder and EnrichOrder call YieldOutputAsync with string messages, but only their TOutput (OrderDetails) was in the allowed yield types. This caused TargetInvocationException in the WorkflowSharedState sample validation integration test. * Downgrade the durable packages to 1.18.0 * Downgrading Worker.Extensions.DurableTask to 1.12.1 --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
620 lines
24 KiB
C#
620 lines
24 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// ConfigureAwait Usage in Orchestration Code:
|
|
// This file uses ConfigureAwait(true) because it runs within orchestration context.
|
|
// Durable Task orchestrations require deterministic replay - the same code must execute
|
|
// identically across replays. ConfigureAwait(true) ensures continuations run on the
|
|
// orchestration's synchronization context, which is essential for replay correctness.
|
|
// Using ConfigureAwait(false) here could cause non-deterministic behavior during replay.
|
|
|
|
// Superstep execution walkthrough for a workflow like below:
|
|
//
|
|
// [A] ──► [B] ──► [C] ──► [E] (B→D has condition: x => x.NeedsReview)
|
|
// │ ▲
|
|
// └──► [D] ──────┘
|
|
//
|
|
// Superstep 1 — A runs
|
|
// Queues before: A:[input] Results: {}
|
|
// Dispatch: A executes, returns resultA
|
|
// Route: EdgeMap routes A's output → B's queue
|
|
// Queues after: B:[resultA] Results: {A: resultA}
|
|
//
|
|
// Superstep 2 — B runs
|
|
// Queues before: B:[resultA] Results: {A: resultA}
|
|
// Dispatch: B executes, returns resultB (type: Order)
|
|
// Route: FanOutRouter sends resultB to:
|
|
// C's queue (unconditional)
|
|
// D's queue (only if resultB.NeedsReview == true)
|
|
// Queues after: C:[resultB], D:[resultB] Results: {A: .., B: resultB}
|
|
// (D may be empty if condition was false)
|
|
//
|
|
// Superstep 3 — C and D run in parallel
|
|
// Queues before: C:[resultB], D:[resultB]
|
|
// Dispatch: C and D execute concurrently via Task.WhenAll
|
|
// Route: Both route output → E's queue
|
|
// Queues after: E:[resultC, resultD] Results: {.., C: resultC, D: resultD}
|
|
//
|
|
// Superstep 4 — E runs (fan-in)
|
|
// Queues before: E:[resultC, resultD] ◄── IsFanInExecutor("E") = true
|
|
// Collect: AggregateQueueMessages merges into JSON array ["resultC","resultD"]
|
|
// Dispatch: E executes with aggregated input
|
|
// Route: E has no successors → nothing enqueued
|
|
// Queues after: (all empty) Results: {.., E: resultE}
|
|
//
|
|
// Superstep 5 — loop exits (no pending messages)
|
|
// GetFinalResult returns resultE
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text.Json;
|
|
using Microsoft.Agents.AI.DurableTask.Workflows.EdgeRouters;
|
|
using Microsoft.Agents.AI.Workflows;
|
|
using Microsoft.DurableTask;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Microsoft.Agents.AI.DurableTask.Workflows;
|
|
|
|
// Superstep loop:
|
|
//
|
|
// ┌───────────────┐ ┌───────────────┐ ┌───────────────────┐
|
|
// │ Collect │───►│ Dispatch │───►│ Process Results │
|
|
// │ Executor │ │ Executors │ │ & Route Messages │
|
|
// │ Inputs │ │ in Parallel │ │ │
|
|
// └───────────────┘ └───────────────┘ └───────────────────┘
|
|
// ▲ │
|
|
// └───────────────────────────────────────────┘
|
|
// (repeat until no pending messages)
|
|
|
|
/// <summary>
|
|
/// Runs workflow orchestrations using message-driven superstep execution with Durable Task.
|
|
/// </summary>
|
|
internal sealed class DurableWorkflowRunner
|
|
{
|
|
private const int MaxSupersteps = 100;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DurableWorkflowRunner"/> class.
|
|
/// </summary>
|
|
/// <param name="durableOptions">The durable options containing workflow configurations.</param>
|
|
public DurableWorkflowRunner(DurableOptions durableOptions)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(durableOptions);
|
|
|
|
this.Options = durableOptions.Workflows;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the workflow options.
|
|
/// </summary>
|
|
private DurableWorkflowOptions Options { get; }
|
|
|
|
/// <summary>
|
|
/// Runs a workflow orchestration.
|
|
/// </summary>
|
|
/// <param name="context">The task orchestration context.</param>
|
|
/// <param name="workflowInput">The workflow input envelope containing workflow input and metadata.</param>
|
|
/// <param name="logger">The replay-safe logger for orchestration logging.</param>
|
|
/// <returns>The result of the workflow execution.</returns>
|
|
/// <exception cref="InvalidOperationException">Thrown when the specified workflow is not found.</exception>
|
|
internal async Task<DurableWorkflowResult> RunWorkflowOrchestrationAsync(
|
|
TaskOrchestrationContext context,
|
|
DurableWorkflowInput<object> workflowInput,
|
|
ILogger logger)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(context);
|
|
ArgumentNullException.ThrowIfNull(workflowInput);
|
|
|
|
Workflow workflow = this.GetWorkflowOrThrow(context.Name);
|
|
|
|
string workflowName = context.Name;
|
|
string instanceId = context.InstanceId;
|
|
logger.LogWorkflowStarting(workflowName, instanceId);
|
|
|
|
WorkflowGraphInfo graphInfo = WorkflowAnalyzer.BuildGraphInfo(workflow);
|
|
DurableEdgeMap edgeMap = new(graphInfo);
|
|
|
|
// Extract input - the start executor determines the expected input type from its own InputTypes
|
|
object input = workflowInput.Input;
|
|
|
|
return await RunSuperstepLoopAsync(context, workflow, edgeMap, input, logger).ConfigureAwait(true);
|
|
}
|
|
|
|
private Workflow GetWorkflowOrThrow(string orchestrationName)
|
|
{
|
|
string workflowName = WorkflowNamingHelper.ToWorkflowName(orchestrationName);
|
|
|
|
if (!this.Options.Workflows.TryGetValue(workflowName, out Workflow? workflow))
|
|
{
|
|
throw new InvalidOperationException($"Workflow '{workflowName}' not found.");
|
|
}
|
|
|
|
return workflow;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs the workflow execution loop using superstep-based processing.
|
|
/// </summary>
|
|
[UnconditionalSuppressMessage("AOT", "IL2026:RequiresUnreferencedCode", Justification = "Input types are preserved by the Durable Task framework's DataConverter.")]
|
|
[UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode", Justification = "Input types are preserved by the Durable Task framework's DataConverter.")]
|
|
private static async Task<DurableWorkflowResult> RunSuperstepLoopAsync(
|
|
TaskOrchestrationContext context,
|
|
Workflow workflow,
|
|
DurableEdgeMap edgeMap,
|
|
object initialInput,
|
|
ILogger logger)
|
|
{
|
|
SuperstepState state = new(workflow, edgeMap);
|
|
|
|
// Convert input to string for the message queue.
|
|
// When DurableWorkflowInput<string> is deserialized as DurableWorkflowInput<object>,
|
|
// the Input property becomes a JsonElement instead of a string.
|
|
// We must extract the raw string value to avoid double-serialization.
|
|
string inputString = initialInput switch
|
|
{
|
|
string s => s,
|
|
JsonElement je when je.ValueKind == JsonValueKind.String => je.GetString() ?? string.Empty,
|
|
_ => JsonSerializer.Serialize(initialInput)
|
|
};
|
|
|
|
edgeMap.EnqueueInitialInput(inputString, state.MessageQueues);
|
|
|
|
bool haltRequested = false;
|
|
|
|
for (int superstep = 1; superstep <= MaxSupersteps; superstep++)
|
|
{
|
|
List<ExecutorInput> executorInputs = CollectExecutorInputs(state, logger);
|
|
if (executorInputs.Count == 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
logger.LogSuperstepStarting(superstep, executorInputs.Count);
|
|
if (logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
logger.LogSuperstepExecutors(superstep, string.Join(", ", executorInputs.Select(e => e.ExecutorId)));
|
|
}
|
|
|
|
string[] results = await DispatchExecutorsInParallelAsync(context, executorInputs, state, logger).ConfigureAwait(true);
|
|
|
|
haltRequested = ProcessSuperstepResults(executorInputs, results, state, context, logger);
|
|
|
|
if (haltRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
// Check if we've reached the limit and still have work remaining
|
|
int remainingExecutors = CountRemainingExecutors(state.MessageQueues);
|
|
if (superstep == MaxSupersteps && remainingExecutors > 0)
|
|
{
|
|
logger.LogWorkflowMaxSuperstepsExceeded(context.InstanceId, MaxSupersteps, remainingExecutors);
|
|
}
|
|
}
|
|
|
|
// Publish final events for live streaming (skip during replay)
|
|
if (!context.IsReplaying)
|
|
{
|
|
PublishEventsToLiveStatus(context, state);
|
|
}
|
|
|
|
string finalResult = GetFinalResult(state.LastResults);
|
|
logger.LogWorkflowCompleted();
|
|
|
|
// Return wrapper with both result and events so streaming clients can
|
|
// retrieve events from SerializedOutput after the orchestration completes
|
|
// (SerializedCustomStatus is cleared by the framework on completion).
|
|
// SentMessages carries the final result so parent workflows can route it
|
|
// to connected executors, matching the in-process WorkflowHostExecutor behavior.
|
|
return new DurableWorkflowResult
|
|
{
|
|
Result = finalResult,
|
|
Events = state.AccumulatedEvents,
|
|
SentMessages = !string.IsNullOrEmpty(finalResult)
|
|
? [new TypedPayload { Data = finalResult }]
|
|
: [],
|
|
HaltRequested = haltRequested
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts the number of executors with pending messages in their queues.
|
|
/// </summary>
|
|
private static int CountRemainingExecutors(Dictionary<string, Queue<DurableMessageEnvelope>> messageQueues)
|
|
{
|
|
return messageQueues.Count(kvp => kvp.Value.Count > 0);
|
|
}
|
|
|
|
private static async Task<string[]> DispatchExecutorsInParallelAsync(
|
|
TaskOrchestrationContext context,
|
|
List<ExecutorInput> executorInputs,
|
|
SuperstepState state,
|
|
ILogger logger)
|
|
{
|
|
Task<string>[] dispatchTasks = executorInputs
|
|
.Select(input => DurableExecutorDispatcher.DispatchAsync(context, input.Info, input.Envelope, state.SharedState, state.LiveStatus, logger))
|
|
.ToArray();
|
|
|
|
return await Task.WhenAll(dispatchTasks).ConfigureAwait(true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Holds state that accumulates and changes across superstep iterations during workflow execution.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// <c>MessageQueues</c> starts with one entry (the start executor's queue, seeded by
|
|
/// <see cref="DurableEdgeMap.EnqueueInitialInput"/>). After each superstep, <c>RouteOutputToSuccessors</c>
|
|
/// adds entries for successor executors that receive routed messages. Queues are drained during
|
|
/// <c>CollectExecutorInputs</c>; empty queues are skipped.
|
|
/// </para>
|
|
/// <para>
|
|
/// <c>LastResults</c> is updated after every superstep with the result of each executor that ran.
|
|
/// At workflow completion, the last non-empty value is returned as the workflow's final result.
|
|
/// </para>
|
|
/// </remarks>
|
|
private sealed class SuperstepState
|
|
{
|
|
public SuperstepState(Workflow workflow, DurableEdgeMap edgeMap)
|
|
{
|
|
this.EdgeMap = edgeMap;
|
|
this.ExecutorBindings = workflow.ReflectExecutors();
|
|
}
|
|
|
|
public DurableEdgeMap EdgeMap { get; }
|
|
|
|
public Dictionary<string, ExecutorBinding> ExecutorBindings { get; }
|
|
|
|
public Dictionary<string, Queue<DurableMessageEnvelope>> MessageQueues { get; } = [];
|
|
|
|
public Dictionary<string, string> LastResults { get; } = [];
|
|
|
|
/// <summary>
|
|
/// Shared state dictionary across supersteps (scope-prefixed key -> serialized value).
|
|
/// </summary>
|
|
public Dictionary<string, string> SharedState { get; } = [];
|
|
|
|
/// <summary>
|
|
/// Accumulated workflow events for the durable workflow status (streaming consumption).
|
|
/// </summary>
|
|
public List<string> AccumulatedEvents { get; } = [];
|
|
|
|
/// <summary>
|
|
/// Workflow status published via <c>SetCustomStatus</c> so external clients can poll for streaming events and pending HITL requests.
|
|
/// </summary>
|
|
public DurableWorkflowLiveStatus LiveStatus { get; } = new();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Represents prepared input for an executor ready for dispatch.
|
|
/// </summary>
|
|
private sealed record ExecutorInput(string ExecutorId, DurableMessageEnvelope Envelope, WorkflowExecutorInfo Info);
|
|
|
|
/// <summary>
|
|
/// Collects inputs for all active executors, applying Fan-In aggregation where needed.
|
|
/// </summary>
|
|
private static List<ExecutorInput> CollectExecutorInputs(
|
|
SuperstepState state,
|
|
ILogger logger)
|
|
{
|
|
List<ExecutorInput> inputs = [];
|
|
|
|
// Only process queues that have pending messages
|
|
foreach ((string executorId, Queue<DurableMessageEnvelope> queue) in state.MessageQueues
|
|
.Where(kvp => kvp.Value.Count > 0))
|
|
{
|
|
DurableMessageEnvelope envelope = GetNextEnvelope(executorId, queue, state.EdgeMap, logger);
|
|
WorkflowExecutorInfo executorInfo = CreateExecutorInfo(executorId, state.ExecutorBindings);
|
|
|
|
inputs.Add(new ExecutorInput(executorId, envelope, executorInfo));
|
|
}
|
|
|
|
return inputs;
|
|
}
|
|
|
|
private static DurableMessageEnvelope GetNextEnvelope(
|
|
string executorId,
|
|
Queue<DurableMessageEnvelope> queue,
|
|
DurableEdgeMap edgeMap,
|
|
ILogger logger)
|
|
{
|
|
bool shouldAggregate = edgeMap.IsFanInExecutor(executorId) && queue.Count > 1;
|
|
|
|
return shouldAggregate
|
|
? AggregateQueueMessages(queue, executorId, logger)
|
|
: queue.Dequeue();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggregates all messages in a queue into a JSON array for Fan-In executors.
|
|
/// </summary>
|
|
private static DurableMessageEnvelope AggregateQueueMessages(
|
|
Queue<DurableMessageEnvelope> queue,
|
|
string executorId,
|
|
ILogger logger)
|
|
{
|
|
List<string> messages = [];
|
|
List<string> sourceIds = [];
|
|
|
|
while (queue.Count > 0)
|
|
{
|
|
DurableMessageEnvelope envelope = queue.Dequeue();
|
|
messages.Add(envelope.Message);
|
|
|
|
if (envelope.SourceExecutorId is not null)
|
|
{
|
|
sourceIds.Add(envelope.SourceExecutorId);
|
|
}
|
|
}
|
|
|
|
if (logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
logger.LogFanInAggregated(executorId, messages.Count, string.Join(", ", sourceIds));
|
|
}
|
|
|
|
return new DurableMessageEnvelope
|
|
{
|
|
Message = SerializeToJsonArray(messages),
|
|
InputTypeName = typeof(string[]).FullName,
|
|
SourceExecutorId = sourceIds.Count > 0 ? string.Join(",", sourceIds) : null
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processes results from a superstep, updating state and routing messages to successors.
|
|
/// </summary>
|
|
/// <returns><c>true</c> if a halt was requested by any executor; otherwise, <c>false</c>.</returns>
|
|
private static bool ProcessSuperstepResults(
|
|
List<ExecutorInput> inputs,
|
|
string[] rawResults,
|
|
SuperstepState state,
|
|
TaskOrchestrationContext context,
|
|
ILogger logger)
|
|
{
|
|
bool haltRequested = false;
|
|
|
|
for (int i = 0; i < inputs.Count; i++)
|
|
{
|
|
string executorId = inputs[i].ExecutorId;
|
|
ExecutorResultInfo resultInfo = ParseActivityResult(rawResults[i]);
|
|
|
|
logger.LogExecutorResultReceived(executorId, resultInfo.Result.Length, resultInfo.SentMessages.Count);
|
|
|
|
state.LastResults[executorId] = resultInfo.Result;
|
|
|
|
// Merge state updates from activity into shared state
|
|
MergeStateUpdates(state, resultInfo.StateUpdates, resultInfo.ClearedScopes);
|
|
|
|
// Accumulate events for the durable workflow status (streaming)
|
|
state.AccumulatedEvents.AddRange(resultInfo.Events);
|
|
|
|
// Check for halt request
|
|
haltRequested |= resultInfo.HaltRequested;
|
|
|
|
// Publish events for live streaming (skip during replay)
|
|
if (!context.IsReplaying)
|
|
{
|
|
PublishEventsToLiveStatus(context, state);
|
|
}
|
|
|
|
RouteOutputToSuccessors(executorId, resultInfo.Result, resultInfo.SentMessages, state, logger);
|
|
}
|
|
|
|
return haltRequested;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Merges state updates from an executor into the shared state.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// When concurrent executors in the same superstep modify keys in the same scope,
|
|
/// last-write-wins semantics apply.
|
|
/// </remarks>
|
|
private static void MergeStateUpdates(
|
|
SuperstepState state,
|
|
Dictionary<string, string?> stateUpdates,
|
|
List<string> clearedScopes)
|
|
{
|
|
Dictionary<string, string> shared = state.SharedState;
|
|
|
|
ApplyClearedScopes(shared, clearedScopes);
|
|
|
|
// Apply individual state updates
|
|
foreach ((string key, string? value) in stateUpdates)
|
|
{
|
|
if (value is null)
|
|
{
|
|
shared.Remove(key);
|
|
}
|
|
else
|
|
{
|
|
shared[key] = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes all keys belonging to the specified scopes from the shared state dictionary.
|
|
/// </summary>
|
|
private static void ApplyClearedScopes(Dictionary<string, string> shared, List<string> clearedScopes)
|
|
{
|
|
if (clearedScopes.Count == 0 || shared.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
List<string> keysToRemove = [];
|
|
|
|
foreach (string clearedScope in clearedScopes)
|
|
{
|
|
string scopePrefix = string.Concat(clearedScope, ":");
|
|
keysToRemove.Clear();
|
|
|
|
foreach (string key in shared.Keys)
|
|
{
|
|
if (key.StartsWith(scopePrefix, StringComparison.Ordinal))
|
|
{
|
|
keysToRemove.Add(key);
|
|
}
|
|
}
|
|
|
|
foreach (string key in keysToRemove)
|
|
{
|
|
shared.Remove(key);
|
|
}
|
|
|
|
if (shared.Count == 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Publishes accumulated workflow events to the durable workflow's custom status,
|
|
/// making them available to <see cref="DurableStreamingWorkflowRun"/> for live streaming.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Custom status is the only orchestration state readable by external clients while
|
|
/// the orchestration is still running. It is cleared by the framework on completion,
|
|
/// so events are also included in <see cref="DurableWorkflowResult"/> for final retrieval.
|
|
/// </remarks>
|
|
private static void PublishEventsToLiveStatus(
|
|
TaskOrchestrationContext context,
|
|
SuperstepState state)
|
|
{
|
|
state.LiveStatus.Events = state.AccumulatedEvents;
|
|
|
|
// Pass the object directly — the framework's DataConverter handles serialization.
|
|
// Pre-serializing would cause double-serialization (string wrapped in JSON quotes).
|
|
context.SetCustomStatus(state.LiveStatus);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Routes executor output (explicit messages or return value) to successor executors.
|
|
/// </summary>
|
|
private static void RouteOutputToSuccessors(
|
|
string executorId,
|
|
string result,
|
|
List<TypedPayload> sentMessages,
|
|
SuperstepState state,
|
|
ILogger logger)
|
|
{
|
|
if (sentMessages.Count > 0)
|
|
{
|
|
// Only route messages that have content
|
|
foreach (TypedPayload message in sentMessages.Where(m => !string.IsNullOrEmpty(m.Data)))
|
|
{
|
|
state.EdgeMap.RouteMessage(executorId, message.Data!, message.TypeName, state.MessageQueues, logger);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(result))
|
|
{
|
|
state.EdgeMap.RouteMessage(executorId, result, inputTypeName: null, state.MessageQueues, logger);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializes a list of messages into a JSON array.
|
|
/// </summary>
|
|
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Serializing string array.")]
|
|
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Serializing string array.")]
|
|
private static string SerializeToJsonArray(List<string> messages)
|
|
{
|
|
return JsonSerializer.Serialize(messages);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a <see cref="WorkflowExecutorInfo"/> for the given executor ID.
|
|
/// </summary>
|
|
/// <exception cref="InvalidOperationException">Thrown when the executor ID is not found in bindings.</exception>
|
|
private static WorkflowExecutorInfo CreateExecutorInfo(
|
|
string executorId,
|
|
Dictionary<string, ExecutorBinding> executorBindings)
|
|
{
|
|
if (!executorBindings.TryGetValue(executorId, out ExecutorBinding? binding))
|
|
{
|
|
throw new InvalidOperationException($"Executor '{executorId}' not found in workflow bindings.");
|
|
}
|
|
|
|
bool isAgentic = WorkflowAnalyzer.IsAgentExecutorType(binding.ExecutorType);
|
|
RequestPort? requestPort = (binding is RequestPortBinding rpb) ? rpb.Port : null;
|
|
Workflow? subWorkflow = (binding is SubworkflowBinding swb) ? swb.WorkflowInstance : null;
|
|
|
|
return new WorkflowExecutorInfo(executorId, isAgentic, requestPort, subWorkflow);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the last non-empty result from executed steps, or empty string if none.
|
|
/// </summary>
|
|
private static string GetFinalResult(Dictionary<string, string> lastResults)
|
|
{
|
|
return lastResults.Values.LastOrDefault(value => !string.IsNullOrEmpty(value)) ?? string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Output from an executor invocation, including its result,
|
|
/// messages, state updates, and emitted workflow events.
|
|
/// </summary>
|
|
private sealed record ExecutorResultInfo(
|
|
string Result,
|
|
List<TypedPayload> SentMessages,
|
|
Dictionary<string, string?> StateUpdates,
|
|
List<string> ClearedScopes,
|
|
List<string> Events,
|
|
bool HaltRequested);
|
|
|
|
/// <summary>
|
|
/// Parses the raw activity result to extract result, messages, events, and state updates.
|
|
/// </summary>
|
|
private static ExecutorResultInfo ParseActivityResult(string rawResult)
|
|
{
|
|
if (string.IsNullOrEmpty(rawResult))
|
|
{
|
|
return new ExecutorResultInfo(rawResult, [], [], [], [], false);
|
|
}
|
|
|
|
try
|
|
{
|
|
DurableExecutorOutput? output = JsonSerializer.Deserialize(
|
|
rawResult,
|
|
DurableWorkflowJsonContext.Default.DurableExecutorOutput);
|
|
|
|
if (output is null || !HasMeaningfulContent(output))
|
|
{
|
|
return new ExecutorResultInfo(rawResult, [], [], [], [], false);
|
|
}
|
|
|
|
return new ExecutorResultInfo(
|
|
output.Result ?? string.Empty,
|
|
output.SentMessages,
|
|
output.StateUpdates,
|
|
output.ClearedScopes,
|
|
output.Events,
|
|
output.HaltRequested);
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
return new ExecutorResultInfo(rawResult, [], [], [], [], false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the activity output contains meaningful content.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Distinguishes actual activity output from arbitrary JSON that deserialized
|
|
/// successfully but with all default/empty values.
|
|
/// </remarks>
|
|
private static bool HasMeaningfulContent(DurableExecutorOutput output)
|
|
{
|
|
return output.Result is not null
|
|
|| output.SentMessages?.Count > 0
|
|
|| output.Events?.Count > 0
|
|
|| output.StateUpdates?.Count > 0
|
|
|| output.ClearedScopes?.Count > 0
|
|
|| output.HaltRequested;
|
|
}
|
|
}
|