Skip to content
Merged
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
5 changes: 5 additions & 0 deletions dotnet/test/MultiClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public async Task One_Client_Approves_Permission_And_Both_See_The_Result()
var client1Events = new ConcurrentBag<SessionEvent>();
var client2Events = new ConcurrentBag<SessionEvent>();

// Wait for PermissionCompletedEvent on client2 which may arrive slightly after session1 goes idle
var client2PermissionCompleted = TestHelper.GetNextEventOfTypeAsync<PermissionCompletedEvent>(session2);

using var sub1 = session1.On(evt => client1Events.Add(evt));
using var sub2 = session2.On(evt => client2Events.Add(evt));

Expand All @@ -181,6 +184,8 @@ public async Task One_Client_Approves_Permission_And_Both_See_The_Result()
Assert.NotNull(response);
Assert.NotEmpty(client1PermissionRequests);

await client2PermissionCompleted;

Assert.Contains(client1Events, e => e is PermissionRequestedEvent);
Assert.Contains(client2Events, e => e is PermissionRequestedEvent);
Assert.Contains(client1Events, e => e is PermissionCompletedEvent);
Expand Down
14 changes: 14 additions & 0 deletions dotnet/test/SessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,18 @@ public async Task SendAndWait_Throws_On_Timeout()
{
var session = await CreateSessionAsync();

var sessionIdleTask = TestHelper.GetNextEventOfTypeAsync<SessionIdleEvent>(session);

// Use a slow command to ensure timeout triggers before completion
var ex = await Assert.ThrowsAsync<TimeoutException>(() =>
session.SendAndWaitAsync(new MessageOptions { Prompt = "Run 'sleep 2 && echo done'" }, TimeSpan.FromMilliseconds(100)));

Assert.Contains("timed out", ex.Message);

// The timeout only cancels the client-side wait; abort the agent and wait for idle
// so leftover requests don't leak into subsequent tests.
await session.AbortAsync();
await sessionIdleTask;
}

[Fact]
Expand All @@ -446,6 +453,7 @@ public async Task SendAndWait_Throws_OperationCanceledException_When_Token_Cance

// Set up wait for tool execution to start BEFORE sending
var toolStartTask = TestHelper.GetNextEventOfTypeAsync<ToolExecutionStartEvent>(session);
var sessionIdleTask = TestHelper.GetNextEventOfTypeAsync<SessionIdleEvent>(session);

Comment on lines 454 to 457
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

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

sessionIdleTask is created immediately after CreateSessionAsync(). Because session events are dispatched asynchronously, this waiter can race and capture an already-queued initial session.idle from session creation/resume rather than the post-cancellation idle you intend to wait for. To make this deterministic, set up the SessionIdleEvent waiter after you’ve observed ToolExecutionStartEvent (and before calling cts.Cancel()), so it can only match the idle corresponding to this in-flight operation.

Copilot uses AI. Check for mistakes.
using var cts = new CancellationTokenSource();

Expand All @@ -461,6 +469,12 @@ public async Task SendAndWait_Throws_OperationCanceledException_When_Token_Cance
cts.Cancel();

await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sendTask);

// Cancelling the token only cancels the client-side wait, not the server-side agent loop.
// Explicitly abort so the agent stops, then wait for idle to ensure we're not still
// running this agent's operations in the context of a subsequent test.
await session.AbortAsync();
await sessionIdleTask;
}

[Fact]
Expand Down
Loading