From 7ae4b7b5370a811f1fda8be6339c563289334ff9 Mon Sep 17 00:00:00 2001
From: Copilot <198982749+Copilot@users.noreply.github.com>
Date: Mon, 16 Feb 2026 14:13:40 +0000
Subject: [PATCH] .NET: Add CreateSessionAsync overload with taskId for
A2AAgent session resumption (#3924)
* Initial plan
* Add CreateSessionAsync overload with contextId and taskId parameters
Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
* Update dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Add parameter validation to CreateSessionAsync methods
Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
* Inline parameter validation in CreateSessionAsync methods
Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---
.../src/Microsoft.Agents.AI.A2A/A2AAgent.cs | 11 ++-
.../A2AAgentTests.cs | 94 +++++++++++++++++++
2 files changed, 104 insertions(+), 1 deletion(-)
diff --git a/dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs b/dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs
index 5601653e8f..18d8bfacdd 100644
--- a/dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs
+++ b/dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs
@@ -63,7 +63,16 @@ public sealed class A2AAgent : AIAgent
/// The context id to continue.
/// A value task representing the asynchronous operation. The task result contains a new instance.
public ValueTask CreateSessionAsync(string contextId)
- => new(new A2AAgentSession() { ContextId = contextId });
+ => new(new A2AAgentSession() { ContextId = Throw.IfNullOrWhitespace(contextId) });
+
+ ///
+ /// Get a new instance using an existing context id and task id, to resume that conversation from a specific task.
+ ///
+ /// The context id to continue.
+ /// The task id to resume from.
+ /// A value task representing the asynchronous operation. The task result contains a new instance.
+ public ValueTask CreateSessionAsync(string contextId, string taskId)
+ => new(new A2AAgentSession() { ContextId = Throw.IfNullOrWhitespace(contextId), TaskId = Throw.IfNullOrWhitespace(taskId) });
///
protected override ValueTask SerializeSessionCoreAsync(AgentSession session, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
diff --git a/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs b/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs
index d6e736a528..50d83c140d 100644
--- a/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs
+++ b/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs
@@ -1146,6 +1146,100 @@ public sealed class A2AAgentTests : IDisposable
Assert.Equal("a2a", metadata.ProviderName);
}
+ ///
+ /// Verify that CreateSessionAsync with contextId creates a session with the correct context ID.
+ ///
+ [Fact]
+ public async Task CreateSessionAsync_WithContextId_CreatesSessionWithContextIdAsync()
+ {
+ // Arrange
+ const string ContextId = "test-context-123";
+
+ // Act
+ var session = await this._agent.CreateSessionAsync(ContextId);
+
+ // Assert
+ Assert.NotNull(session);
+ Assert.IsType(session);
+ var typedSession = (A2AAgentSession)session;
+ Assert.Equal(ContextId, typedSession.ContextId);
+ Assert.Null(typedSession.TaskId);
+ }
+
+ ///
+ /// Verify that CreateSessionAsync with contextId and taskId creates a session with both IDs set correctly.
+ ///
+ [Fact]
+ public async Task CreateSessionAsync_WithContextIdAndTaskId_CreatesSessionWithBothIdsAsync()
+ {
+ // Arrange
+ const string ContextId = "test-context-456";
+ const string TaskId = "test-task-789";
+
+ // Act
+ var session = await this._agent.CreateSessionAsync(ContextId, TaskId);
+
+ // Assert
+ Assert.NotNull(session);
+ Assert.IsType(session);
+ var typedSession = (A2AAgentSession)session;
+ Assert.Equal(ContextId, typedSession.ContextId);
+ Assert.Equal(TaskId, typedSession.TaskId);
+ }
+
+ ///
+ /// Verify that CreateSessionAsync throws when contextId is null, empty, or whitespace.
+ ///
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData(" ")]
+ [InlineData("\t")]
+ [InlineData("\r\n")]
+ public async Task CreateSessionAsync_WithInvalidContextId_ThrowsArgumentExceptionAsync(string? contextId)
+ {
+ // Act & Assert
+ await Assert.ThrowsAnyAsync(async () =>
+ await this._agent.CreateSessionAsync(contextId!));
+ }
+
+ ///
+ /// Verify that CreateSessionAsync with both parameters throws when contextId is null, empty, or whitespace.
+ ///
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData(" ")]
+ [InlineData("\t")]
+ [InlineData("\r\n")]
+ public async Task CreateSessionAsync_WithInvalidContextIdAndValidTaskId_ThrowsArgumentExceptionAsync(string? contextId)
+ {
+ // Arrange
+ const string TaskId = "valid-task-id";
+
+ // Act & Assert
+ await Assert.ThrowsAnyAsync(async () =>
+ await this._agent.CreateSessionAsync(contextId!, TaskId));
+ }
+
+ ///
+ /// Verify that CreateSessionAsync with both parameters throws when taskId is null, empty, or whitespace.
+ ///
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData(" ")]
+ [InlineData("\t")]
+ [InlineData("\r\n")]
+ public async Task CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(string? taskId)
+ {
+ // Arrange
+ const string ContextId = "valid-context-id";
+
+ // Act & Assert
+ await Assert.ThrowsAnyAsync(async () =>
+ await this._agent.CreateSessionAsync(ContextId, taskId!));
+ }
#endregion
public void Dispose()