A programming framework for agentic AI
fix: Add deferral to RegisterAgentType and (Add/Remove)Subscription (#5494)
Jacob Alber392aa144913b
diff --git a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.csindex 324874a00..83c77c5ef 100644--- a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs+++ b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs@@ -253,12 +253,33 @@ public sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSi}}- public ValueTask StartAsync(CancellationToken cancellationToken)+ public async ValueTask StartAsync(CancellationToken cancellationToken){- return this._messageRouter.StartAsync(cancellationToken);+ await this._messageRouter.StartAsync(cancellationToken);+ if (this._agentsContainer.RegisteredAgentTypes.Count > 0)+ {+ foreach (var type in this._agentsContainer.RegisteredAgentTypes)+ {+ await this._client.RegisterAgentAsync(new RegisterAgentTypeRequest+ {+ Type = type+ }, this.CallOptions);+ }+ }++ if (this._agentsContainer.Subscriptions.Count > 0)+ {+ foreach (var subscription in this._agentsContainer.Subscriptions.Values)+ {+ await this._client.AddSubscriptionAsync(new AddSubscriptionRequest+ {+ Subscription = subscription.ToProtobuf()+ }, this.CallOptions);+ }+ }}- Task IHostedService.StartAsync(CancellationToken cancellationToken) => this._messageRouter.StartAsync(cancellationToken).AsTask();+ Task IHostedService.StartAsync(CancellationToken cancellationToken) => this.StartAsync(cancellationToken).AsTask();public Task StopAsync(CancellationToken cancellationToken){@@ -344,30 +365,39 @@ public sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSi{this._agentsContainer.AddSubscription(subscription);- var _ = await this._client.AddSubscriptionAsync(new AddSubscriptionRequest+ if (this._messageRouter.IsChannelOpen){- Subscription = subscription.ToProtobuf()- }, this.CallOptions);+ var _ = await this._client.AddSubscriptionAsync(new AddSubscriptionRequest+ {+ Subscription = subscription.ToProtobuf()+ }, this.CallOptions);+ }}public async ValueTask RemoveSubscriptionAsync(string subscriptionId){this._agentsContainer.RemoveSubscriptionAsync(subscriptionId);- await this._client.RemoveSubscriptionAsync(new RemoveSubscriptionRequest+ if (this._messageRouter.IsChannelOpen){- Id = subscriptionId- }, this.CallOptions);+ await this._client.RemoveSubscriptionAsync(new RemoveSubscriptionRequest+ {+ Id = subscriptionId+ }, this.CallOptions);+ }}public async ValueTask<AgentType> RegisterAgentFactoryAsync(AgentType type, Func<Contracts.AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factoryFunc){this._agentsContainer.RegisterAgentFactory(type, factoryFunc);- await this._client.RegisterAgentAsync(new RegisterAgentTypeRequest+ if (this._messageRouter.IsChannelOpen){- Type = type,- }, this.CallOptions);+ await this._client.RegisterAgentAsync(new RegisterAgentTypeRequest+ {+ Type = type,+ }, this.CallOptions);+ }return type;}diff --git a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcMessageRouter.cs b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcMessageRouter.csindex e46b392c7..32317cbc7 100644--- a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcMessageRouter.cs+++ b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcMessageRouter.cs@@ -34,6 +34,8 @@ internal sealed class AutoRestartChannel : IDisposable_shutdownCts = CancellationTokenSource.CreateLinkedTokenSource(shutdownCancellation);}+ public bool Connected { get => _channel is not null; }+public void EnsureConnected(){_logger.LogInformation("Connecting to gRPC endpoint " + Environment.GetEnvironmentVariable("AGENT_HOST"));@@ -287,6 +289,8 @@ internal sealed class GrpcMessageRouter(AgentRpc.AgentRpcClient client,this._incomingMessageChannel.Dispose();}+ public bool IsChannelOpen => this._incomingMessageChannel.Connected;+public void Dispose(){_outboundMessagesChannel.Writer.TryComplete();diff --git a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/AgentGrpcTests.cs b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/AgentGrpcTests.csindex a2a970beb..dc74446ec 100644--- a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/AgentGrpcTests.cs+++ b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/AgentGrpcTests.cs@@ -10,13 +10,13 @@ using Xunit;namespace Microsoft.AutoGen.Core.Grpc.Tests;[Trait("Category", "GRPC")]-public class AgentGrpcTests+public class AgentGrpcTests : TestBase{[Fact]public async Task AgentShouldNotReceiveMessagesWhenNotSubscribedTest(){var fixture = new GrpcAgentRuntimeFixture();- var runtime = (GrpcAgentRuntime)await fixture.Start();+ var runtime = (GrpcAgentRuntime)await fixture.StartAsync();Logger<BaseAgent> logger = new(new LoggerFactory());TestProtobufAgent agent = null!;@@ -45,7 +45,7 @@ public class AgentGrpcTestspublic async Task AgentShouldReceiveMessagesWhenSubscribedTest(){var fixture = new GrpcAgentRuntimeFixture();- var runtime = (GrpcAgentRuntime)await fixture.Start();+ var runtime = (GrpcAgentRuntime)await fixture.StartAsync();Logger<BaseAgent> logger = new(new LoggerFactory());SubscribedProtobufAgent agent = null!;@@ -80,7 +80,7 @@ public class AgentGrpcTests{// Arrangevar fixture = new GrpcAgentRuntimeFixture();- var runtime = (GrpcAgentRuntime)await fixture.Start();+ var runtime = (GrpcAgentRuntime)await fixture.StartAsync();Logger<BaseAgent> logger = new(new LoggerFactory());await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) => await ValueTask.FromResult(new TestProtobufAgent(id, runtime, logger)));@@ -114,7 +114,7 @@ public class AgentGrpcTestspublic async Task SubscribeAsyncRemoveSubscriptionAsyncAndGetSubscriptionsTest(){var fixture = new GrpcAgentRuntimeFixture();- var runtime = (GrpcAgentRuntime)await fixture.Start();+ var runtime = (GrpcAgentRuntime)await fixture.StartAsync();ReceiverAgent? agent = null;await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) =>{diff --git a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/FreePortManager.cs b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/FreePortManager.csnew file mode 100644index 000000000..bbf3dbd54--- /dev/null+++ b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/FreePortManager.cs@@ -0,0 +1,67 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// FreePortManager.cs++using System.Diagnostics;++namespace Microsoft.AutoGen.Core.Grpc.Tests;++internal sealed class FreePortManager+{+ private HashSet<int> takenPorts = new();+ private readonly object mutex = new();++ [DebuggerDisplay($"{{{nameof(Port)}}}")]+ internal sealed class PortTicket(FreePortManager portManager, int port) : IDisposable+ {+ private FreePortManager? portManager = portManager;++ public int Port { get; } = port;++ public void Dispose()+ {+ FreePortManager? localPortManager = Interlocked.Exchange(ref this.portManager, null);+ localPortManager?.takenPorts.Remove(this.Port);+ }++ public override string ToString()+ {+ return this.Port.ToString();+ }++ public override bool Equals(object? obj)+ {+ return obj is PortTicket ticket && ticket.Port == this.Port;+ }++ public override int GetHashCode()+ {+ return this.Port.GetHashCode();+ }++ public static implicit operator int(PortTicket ticket) => ticket.Port;+ public static implicit operator string(PortTicket ticket) => ticket.ToString();+ }++ public PortTicket GetAvailablePort()+ {+ lock (mutex)+ {+ int port;+ do+ {+ using var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);+ listener.Start();+ port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;+ listener.Stop();+ listener.Dispose();+ Thread.Yield(); // Let the listener actually shut down before we try to use the port+ } while (takenPorts.Contains(port));++ takenPorts.Add(port);++ Console.WriteLine($"FreePortManager: Yielding port {port}");+ Debug.WriteLine($"FreePortManager: Yielding port {port}");+ return new PortTicket(this, port);+ }+ }+}diff --git a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentRuntimeFixture.cs b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentRuntimeFixture.csindex bade7f785..064f22cfa 100644--- a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentRuntimeFixture.cs+++ b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentRuntimeFixture.cs@@ -7,69 +7,89 @@ using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;namespace Microsoft.AutoGen.Core.Grpc.Tests;+/// <summary>/// Fixture for setting up the gRPC agent runtime for testing./// </summary>public sealed class GrpcAgentRuntimeFixture : IDisposable{+ private FreePortManager.PortTicket? portTicket;+/// the gRPC agent runtime.- public AgentsApp? Client { get; private set; }+ public AgentsApp? AgentsApp { get; private set; }+/// mock server for testing.- public WebApplication? Server { get; private set; }+ public WebApplication? GatewayServer { get; private set; }++ public GrpcAgentServiceCollector GrpcRequestCollector { get; }public GrpcAgentRuntimeFixture(){+ GrpcRequestCollector = new GrpcAgentServiceCollector();}+/// <summary>/// Start - gets a new port and starts fresh instances/// </summary>- public async Task<IAgentRuntime> Start(bool initialize = true)+ public async Task<IAgentRuntime> StartAsync(bool startRuntime = true, bool registerDefaultAgent = true){- int port = GetAvailablePort(); // Get a new port per test run+ this.portTicket = GrpcAgentRuntimeFixture.PortManager.GetAvailablePort(); // Get a new port per test run// Update environment variables so each test runs independently- Environment.SetEnvironmentVariable("ASPNETCORE_HTTPS_PORTS", port.ToString());- Environment.SetEnvironmentVariable("AGENT_HOST", $"https://localhost:{port}");+ Environment.SetEnvironmentVariable("ASPNETCORE_HTTPS_PORTS", portTicket);+ Environment.SetEnvironmentVariable("AGENT_HOST", $"https://localhost:{portTicket}");Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");- Server = ServerBuilder().Result;- await Server.StartAsync().ConfigureAwait(true);- Client = ClientBuilder().Result;- await Client.StartAsync().ConfigureAwait(true);- var worker = Client.Services.GetRequiredService<IAgentRuntime>();+ this.GatewayServer = await this.InitializeGateway();+ this.AgentsApp = await this.InitializeRuntime(startRuntime, registerDefaultAgent);+ var runtime = AgentsApp.Services.GetRequiredService<IAgentRuntime>();- return (worker);+ return runtime;}- private static async Task<AgentsApp> ClientBuilder()++ private async Task<AgentsApp> InitializeRuntime(bool callStartAsync, bool registerDefaultAgent){var appBuilder = new AgentsAppBuilder();appBuilder.AddGrpcAgentWorker();- appBuilder.AddAgent<TestProtobufAgent>("TestAgent");- return await appBuilder.BuildAsync();++ if (registerDefaultAgent)+ {+ appBuilder.AddAgent<TestProtobufAgent>("TestAgent");+ }++ AgentsApp result = await appBuilder.BuildAsync();++ if (callStartAsync)+ {+ await result.StartAsync().ConfigureAwait(true);+ }++ return result;}- private static async Task<WebApplication> ServerBuilder()++ private async Task<WebApplication> InitializeGateway(){var builder = WebApplication.CreateBuilder();builder.Services.AddGrpc();- var app = builder.Build();+ builder.Services.AddSingleton(this.GrpcRequestCollector);++ WebApplication app = builder.Build();app.MapGrpcService<GrpcAgentServiceFixture>();++ await app.StartAsync().ConfigureAwait(true);return app;}- private static int GetAvailablePort()- {- using var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);- listener.Start();- int port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;- listener.Stop();- return port;- }++ private static readonly FreePortManager PortManager = new();+/// <summary>/// Stop - stops the agent and ensures cleanup/// </summary>public void Stop(){- (Client as IHost)?.StopAsync(TimeSpan.FromSeconds(30)).GetAwaiter().GetResult();- Server?.StopAsync().GetAwaiter().GetResult();+ (AgentsApp as IHost)?.StopAsync(TimeSpan.FromSeconds(30)).GetAwaiter().GetResult();+ GatewayServer?.StopAsync().GetAwaiter().GetResult();+ portTicket?.Dispose();}/// <summary>diff --git a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentRuntimeTests.cs b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentRuntimeTests.csnew file mode 100644index 000000000..b48942f1d--- /dev/null+++ b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentRuntimeTests.cs@@ -0,0 +1,50 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// GrpcAgentRuntimeTests.cs++using FluentAssertions;+using Microsoft.AutoGen.Contracts;+using Microsoft.Extensions.Logging;+using Xunit;++namespace Microsoft.AutoGen.Core.Grpc.Tests;++[Trait("Category", "GRPC")]+public class GrpcAgentRuntimeTests : TestBase+{+ [Fact]+ public async Task GatewayShouldNotReceiveRegistrationsUntilRuntimeStart()+ {+ var fixture = new GrpcAgentRuntimeFixture();+ var runtime = (GrpcAgentRuntime)await fixture.StartAsync(startRuntime: false, registerDefaultAgent: false);++ Logger<BaseAgent> logger = new(new LoggerFactory());++ await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) =>+ {+ return await ValueTask.FromResult(new SubscribedProtobufAgent(id, runtime, logger));+ });+ await runtime.RegisterImplicitAgentSubscriptionsAsync<SubscribedProtobufAgent>("MyAgent");++ fixture.GrpcRequestCollector.RegisterAgentTypeRequests.Should().BeEmpty();+ fixture.GrpcRequestCollector.AddSubscriptionRequests.Should().BeEmpty();++ await fixture.AgentsApp!.StartAsync().ConfigureAwait(true);++ fixture.GrpcRequestCollector.RegisterAgentTypeRequests.Should().NotBeEmpty();+ fixture.GrpcRequestCollector.RegisterAgentTypeRequests.Single().Type.Should().Be("MyAgent");+ fixture.GrpcRequestCollector.AddSubscriptionRequests.Should().NotBeEmpty();++ fixture.GrpcRequestCollector.Clear();++ await runtime.RegisterAgentFactoryAsync("MyAgent2", async (id, runtime) =>+ {+ return await ValueTask.FromResult(new TestProtobufAgent(id, runtime, logger));+ });++ fixture.GrpcRequestCollector.RegisterAgentTypeRequests.Should().NotBeEmpty();+ fixture.GrpcRequestCollector.RegisterAgentTypeRequests.Single().Type.Should().Be("MyAgent2");+ fixture.GrpcRequestCollector.AddSubscriptionRequests.Should().BeEmpty();++ fixture.Dispose();+ }+}diff --git a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentServiceFixture.cs b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentServiceFixture.csindex 1ca37809a..af008f241 100644--- a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentServiceFixture.cs+++ b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/GrpcAgentServiceFixture.cs@@ -1,14 +1,37 @@// Copyright (c) Microsoft Corporation. All rights reserved.// GrpcAgentServiceFixture.cs+using Grpc.Core;using Microsoft.AutoGen.Protobuf;+using Microsoft.Extensions.DependencyInjection;+namespace Microsoft.AutoGen.Core.Grpc.Tests;+public sealed class GrpcAgentServiceCollector+{+ public List<AddSubscriptionRequest> AddSubscriptionRequests { get; } = new();+ public List<RemoveSubscriptionRequest> RemoveSubscriptionRequests { get; } = new();+ public List<RegisterAgentTypeRequest> RegisterAgentTypeRequests { get; } = new();++ internal void Clear()+ {+ this.AddSubscriptionRequests.Clear();+ this.RemoveSubscriptionRequests.Clear();+ this.RegisterAgentTypeRequests.Clear();+ }+}+/// <summary>/// This fixture is largely just a loopback as we are testing the client side logic of the GrpcAgentRuntime in isolation from the rest of the system./// </summary>-public sealed class GrpcAgentServiceFixture() : AgentRpc.AgentRpcBase+public class GrpcAgentServiceFixture : AgentRpc.AgentRpcBase{+ private GrpcAgentServiceCollector requestCollector;+ public GrpcAgentServiceFixture(IServiceProvider serviceProvider)+ {+ this.requestCollector = serviceProvider.GetService<GrpcAgentServiceCollector>() ?? new();+ }+public override async Task OpenChannel(IAsyncStreamReader<Message> requestStream, IServerStreamWriter<Message> responseStream, ServerCallContext context){try@@ -25,8 +48,27 @@ public sealed class GrpcAgentServiceFixture() : AgentRpc.AgentRpcBasethrow;}}- public override async Task<AddSubscriptionResponse> AddSubscription(AddSubscriptionRequest request, ServerCallContext context) => new AddSubscriptionResponse { };- public override async Task<RemoveSubscriptionResponse> RemoveSubscription(RemoveSubscriptionRequest request, ServerCallContext context) => new RemoveSubscriptionResponse { };++ public List<AddSubscriptionRequest> AddSubscriptionRequests => this.requestCollector.AddSubscriptionRequests;+ public override async Task<AddSubscriptionResponse> AddSubscription(AddSubscriptionRequest request, ServerCallContext context)+ {+ this.AddSubscriptionRequests.Add(request);+ return new AddSubscriptionResponse();+ }++ public List<RemoveSubscriptionRequest> RemoveSubscriptionRequests => this.requestCollector.RemoveSubscriptionRequests;+ public override async Task<RemoveSubscriptionResponse> RemoveSubscription(RemoveSubscriptionRequest request, ServerCallContext context)+ {+ this.RemoveSubscriptionRequests.Add(request);+ return new RemoveSubscriptionResponse();+ }+public override async Task<GetSubscriptionsResponse> GetSubscriptions(GetSubscriptionsRequest request, ServerCallContext context) => new GetSubscriptionsResponse { };- public override async Task<RegisterAgentTypeResponse> RegisterAgent(RegisterAgentTypeRequest request, ServerCallContext context) => new RegisterAgentTypeResponse { };++ public List<RegisterAgentTypeRequest> RegisterAgentTypeRequests => this.requestCollector.RegisterAgentTypeRequests;+ public override async Task<RegisterAgentTypeResponse> RegisterAgent(RegisterAgentTypeRequest request, ServerCallContext context)+ {+ this.RegisterAgentTypeRequests.Add(request);+ return new RegisterAgentTypeResponse();+ }}diff --git a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/TestBase.cs b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/TestBase.csnew file mode 100644index 000000000..77d9d552a--- /dev/null+++ b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/TestBase.cs@@ -0,0 +1,22 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// TestBase.cs++namespace Microsoft.AutoGen.Core.Grpc.Tests;++public class TestBase+{+ public TestBase()+ {+ try+ {+ // For some reason the first call to StartAsync() throws when these tests+ // run in parallel, even though the port does not actually collide between+ // different instances of GrpcAgentRuntimeFixture. This is a workaround.+ _ = new GrpcAgentRuntimeFixture().StartAsync().Result;+ }+ catch (Exception e)+ {+ Console.WriteLine(e);+ }+ }+}