Skip to content

.NET: Add CreateSessionAsync overload with taskId for A2AAgent session resumption#3924

Open
Copilot wants to merge 5 commits intomainfrom
copilot/add-resume-session-taskid-support
Open

.NET: Add CreateSessionAsync overload with taskId for A2AAgent session resumption#3924
Copilot wants to merge 5 commits intomainfrom
copilot/add-resume-session-taskid-support

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

Motivation and Context

A2AAgent sessions track both ContextId (conversation) and TaskId (current task), but CreateSessionAsync only accepted contextId, preventing session resumption from a specific task.

Description

Added CreateSessionAsync(string contextId, string taskId) overload to enable resuming sessions with both conversation and task context. Both overloads use expression-bodied members with inlined parameter validation for a concise implementation.

Changes:

  • API Addition: New overload creates A2AAgentSession with both IDs set
  • Parameter Validation: Both overloads validate parameters using inlined Throw.IfNullOrWhitespace calls within object initializers
  • Expression-Bodied Members: Both methods use lambda-style expression bodies for concise implementation
  • Backward Compatibility: Existing CreateSessionAsync(string contextId) overload unchanged in functionality
  • Tests: Added comprehensive coverage for both overloads including parameter validation tests (17 total tests)

Example:

// Resume conversation only (existing)
var session1 = await agent.CreateSessionAsync("context-123");

// Resume from specific task (new)
var session2 = await agent.CreateSessionAsync("context-123", "task-456");

// Both methods validate parameters and throw ArgumentException for null/empty/whitespace

Implementation:

public ValueTask<AgentSession> CreateSessionAsync(string contextId)
    => new(new A2AAgentSession() { ContextId = Throw.IfNullOrWhitespace(contextId) });

public ValueTask<AgentSession> CreateSessionAsync(string contextId, string taskId)
    => new(new A2AAgentSession() { ContextId = Throw.IfNullOrWhitespace(contextId), TaskId = Throw.IfNullOrWhitespace(taskId) });

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.
Original prompt

This section details on the original issue you should resolve

<issue_title>.NET: A2AAgent should support the ability to resume a session based on contextid and taskid</issue_title>
<issue_description>Currently the .net A2AAgent has a CreateSession overload that allows a user to provide a context id, however it does not allow the user to also provide a task id. It's important for users to also optionally be able to provide a task id, so that a session can be resumed from a specific task.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for resuming session with task id .NET: Add CreateSessionAsync overload with taskId for A2AAgent session resumption Feb 13, 2026
Copilot AI requested a review from westey-m February 13, 2026 15:35
@westey-m westey-m marked this pull request as ready for review February 13, 2026 16:02
Copilot AI review requested due to automatic review settings February 13, 2026 16:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an additional CreateSessionAsync overload to A2AAgent to enable session resumption with both context and task identifiers. Previously, only the context ID could be specified when resuming a session, but A2A protocol sessions track both ContextId (conversation) and TaskId (current task). This change enables users to resume from a specific task within a conversation.

Changes:

  • Added CreateSessionAsync(string contextId, string taskId) overload to A2AAgent
  • Added unit tests for both existing and new CreateSessionAsync overloads

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs Added new CreateSessionAsync overload accepting both contextId and taskId parameters
dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs Added two test methods to verify both CreateSessionAsync overloads create sessions with correct IDs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@westey-m
Copy link
Contributor

@copilot, the unit tests are failing, please fix, and address the PR comments.

  Failed Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(taskId: "\r\n") [1 ms]
  Error Message:
   Assert.Throws() Failure: No exception was thrown
Expected: typeof(System.ArgumentException)
  Stack Trace:
     at Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(String taskId) in /home/runner/work/agent-framework/agent-framework/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs:line 1240
--- End of stack trace from previous location ---
  Failed Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(taskId: null) [< 1 ms]
  Error Message:
   Assert.Throws() Failure: No exception was thrown
Expected: typeof(System.ArgumentException)
  Stack Trace:
     at Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(String taskId) in /home/runner/work/agent-framework/agent-framework/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs:line 1240
--- End of stack trace from previous location ---
  Failed Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(taskId: " ") [< 1 ms]
  Error Message:
   Assert.Throws() Failure: No exception was thrown
Expected: typeof(System.ArgumentException)
  Stack Trace:
     at Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(String taskId) in /home/runner/work/agent-framework/agent-framework/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs:line 1240
--- End of stack trace from previous location ---
  Failed Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(taskId: "\t") [< 1 ms]
  Error Message:
   Assert.Throws() Failure: No exception was thrown
Expected: typeof(System.ArgumentException)
  Stack Trace:
     at Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(String taskId) in /home/runner/work/agent-framework/agent-framework/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs:line 1240
--- End of stack trace from previous location ---
  Failed Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(taskId: "") [< 1 ms]
  Error Message:
   Assert.Throws() Failure: No exception was thrown
Expected: typeof(System.ArgumentException)
  Stack Trace:
     at Microsoft.Agents.AI.A2A.UnitTests.A2AAgentTests.CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(String taskId) in /home/runner/work/agent-framework/agent-framework/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs:line 1240
     ```

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

@copilot, the unit tests are failing, please fix, and address the PR comments.

...

Fixed in commit 0f48857. Added parameter validation using Throw.IfNullOrWhitespace to both CreateSessionAsync overloads and updated tests to use ThrowsAnyAsync to handle both ArgumentNullException and ArgumentException. All 108 tests now passing.

Copy link
Contributor

@westey-m westey-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please address

public ValueTask<AgentSession> CreateSessionAsync(string contextId)
=> new(new A2AAgentSession() { ContextId = contextId });
{
_ = Throw.IfNullOrWhitespace(contextId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw.IfNullOrWhitespace(contextId) can be inline inside the new(new A2AAgentSession() { ContextId = contextId }); statement, e.g. new(new A2AAgentSession() { ContextId = Throw.IfNullOrWhitespace(contextId) });
Using this pattern means we can use a lambda style for the method body reducing the lines of code. Same below for CreateSessionAsync(string contextId, string taskId)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit 74a6622. Inlined Throw.IfNullOrWhitespace calls within the object initializers, reducing both methods to lambda-style expression bodies.

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

@copilot please address

Addressed in commit 74a6622.

Copilot AI requested a review from westey-m February 13, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: A2AAgent should support the ability to resume a session based on contextid and taskid

3 participants