Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Agents.AI;
Expand All @@ -17,6 +18,18 @@ namespace Microsoft.Agents.AI;
/// </remarks>
internal sealed class BackgroundAgentRuntimeState
{
/// <summary>
/// Gets an object used to synchronize access to the runtime references held by this instance.
/// </summary>
/// <remarks>
/// Background task registration happens on the agent's tool-invocation path, while
/// <see cref="BackgroundAgentsProvider.ReleaseSessionAsync"/> may be called concurrently by a host.
/// All mutations of the dictionaries below, and of <see cref="IsReleased"/>, must be performed under this lock
/// so that a task can never be registered into an already-released runtime.
/// </remarks>
[JsonIgnore]
public object SyncRoot { get; } = new();

/// <summary>
/// Gets the mapping of task IDs to their in-flight <see cref="Task{AgentResponse}"/> instances.
/// </summary>
Expand All @@ -29,4 +42,25 @@ internal sealed class BackgroundAgentRuntimeState
/// </summary>
[JsonIgnore]
public Dictionary<int, AgentSession> BackgroundTaskSessions { get; } = [];

/// <summary>
/// Gets the mapping of task IDs to the <see cref="CancellationTokenSource"/> controlling their run.
/// </summary>
/// <remarks>
/// A source is created when a task is started or continued, and is disposed and removed when the task is
/// finalized, cleared, or when the session is released via <see cref="BackgroundAgentsProvider.ReleaseSessionAsync"/>.
/// </remarks>
[JsonIgnore]
public Dictionary<int, CancellationTokenSource> TaskCancellations { get; } = [];

/// <summary>
/// Gets or sets a value indicating whether this runtime has been released via
/// <see cref="BackgroundAgentsProvider.ReleaseSessionAsync"/>.
/// </summary>
/// <remarks>
/// Once released, all in-flight tasks have been cancelled and awaited, and the runtime references have been
/// dropped. Tools that would start new background work refuse to run against a released runtime.
/// </remarks>
[JsonIgnore]
public bool IsReleased { get; set; }
}
Loading
Loading